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

Function captureRequestBody

packages/kosong/test/anthropic.test.ts:60–91  ·  view source on GitHub ↗

Capture the request body sent to Anthropic by mocking the client (non-stream mode).

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

Source from the content-addressed store, hash-verified

58
59/** Capture the request body sent to Anthropic by mocking the client (non-stream mode). */
60async 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. */
94function mockStream(events: unknown[]) {

Callers 2

anthropic.test.tsFile · 0.70
maxTokensForFunction · 0.70

Calls 3

makeAnthropicResponseFunction · 0.70
resolveMethod · 0.65
generateMethod · 0.65

Tested by

no test coverage detected