MCPcopy Create free account
hub / github.com/MoonshotAI/kimi-code / captureRequestBody

Function captureRequestBody

packages/kosong/test/google-genai.test.ts:48–81  ·  view source on GitHub ↗

Capture the request body by mocking the client's generateContentStream.

(
  provider: GoogleGenAIChatProvider,
  systemPrompt: string,
  tools: Tool[],
  history: Message[],
)

Source from the content-addressed store, hash-verified

46
47/** Capture the request body by mocking the client's generateContentStream. */
48async 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. */
84async function collectParts(msg: {

Callers 2

captureThinkingConfigFunction · 0.70

Calls 4

mockStreamFunction · 0.70
resolveMethod · 0.65
generateMethod · 0.65

Tested by

no test coverage detected