Capture the request body by mocking the client's generateContentStream.
( provider: GoogleGenAIChatProvider, systemPrompt: string, tools: Tool[], history: Message[], )
| 46 | |
| 47 | /** Capture the request body by mocking the client's generateContentStream. */ |
| 48 | async function captureRequestBody( |
| 49 | provider: GoogleGenAIChatProvider, |
| 50 | systemPrompt: string, |
| 51 | tools: Tool[], |
| 52 | history: Message[], |
| 53 | ): Promise<Record<string, unknown>> { |
| 54 | let capturedBody: Record<string, unknown> | undefined; |
| 55 | |
| 56 | const mockModels = (provider as any)._client.models as Record<string, unknown>; |
| 57 | |
| 58 | async function* mockStream() { |
| 59 | yield makeGenerateContentResponse(); |
| 60 | } |
| 61 | |
| 62 | mockModels['generateContentStream'] = vi.fn().mockImplementation((params: unknown) => { |
| 63 | capturedBody = params as Record<string, unknown>; |
| 64 | return Promise.resolve(mockStream()); |
| 65 | }); |
| 66 | |
| 67 | mockModels['generateContent'] = vi.fn().mockImplementation((params: unknown) => { |
| 68 | capturedBody = params as Record<string, unknown>; |
| 69 | return Promise.resolve(makeGenerateContentResponse()); |
| 70 | }); |
| 71 | |
| 72 | const stream = await provider.generate(systemPrompt, tools, history); |
| 73 | for await (const part of stream) { |
| 74 | void part; |
| 75 | } |
| 76 | |
| 77 | if (capturedBody === undefined) { |
| 78 | throw new Error('Expected provider.generate() to call a Google GenAI model endpoint'); |
| 79 | } |
| 80 | return capturedBody; |
| 81 | } |
| 82 | |
| 83 | /** Collect all parts from a StreamedMessage. */ |
| 84 | async function collectParts(msg: { |
no test coverage detected