Capture the request body sent to Anthropic by mocking the client (non-stream mode).
( provider: AnthropicChatProvider, systemPrompt: string, tools: Tool[], history: Message[], )
| 58 | |
| 59 | /** Capture the request body sent to Anthropic by mocking the client (non-stream mode). */ |
| 60 | async function captureRequestBody( |
| 61 | provider: AnthropicChatProvider, |
| 62 | systemPrompt: string, |
| 63 | tools: Tool[], |
| 64 | history: Message[], |
| 65 | ): Promise<Record<string, unknown>> { |
| 66 | let capturedParams: Record<string, unknown> | undefined; |
| 67 | let capturedOptions: Record<string, unknown> | undefined; |
| 68 | |
| 69 | (provider as any)._client.messages.create = vi |
| 70 | .fn() |
| 71 | .mockImplementation((params: unknown, options?: unknown) => { |
| 72 | capturedParams = params as Record<string, unknown>; |
| 73 | capturedOptions = options as Record<string, unknown> | undefined; |
| 74 | return Promise.resolve(makeAnthropicResponse()); |
| 75 | }); |
| 76 | |
| 77 | const stream = await provider.generate(systemPrompt, tools, history); |
| 78 | for await (const part of stream) { |
| 79 | void part; |
| 80 | } |
| 81 | |
| 82 | if (capturedParams === undefined) { |
| 83 | throw new Error('Expected provider.generate() to call messages.create'); |
| 84 | } |
| 85 | |
| 86 | const result = { ...capturedParams }; |
| 87 | if (capturedOptions !== undefined && capturedOptions['headers'] !== undefined) { |
| 88 | result['_extra_headers'] = capturedOptions['headers']; |
| 89 | } |
| 90 | return result; |
| 91 | } |
| 92 | |
| 93 | /** Create a mock stream that yields the given events as an async iterable. */ |
| 94 | function mockStream(events: unknown[]) { |
no test coverage detected