import { rawPost, rawSSE, save, short } from "./lib.ts"; const model = process.argv[2] ?? "kimi-k3"; const tools = [{ type: "builtin_function", function: { name: "$web_search" } }]; const messages: any[] = [{ role: "user", content: "What was the closing value of the S&P 500 yesterday? Search the web and cite the source URL." }]; // step 1 streamed const s = await rawSSE("/chat/completions", { model, messages, tools, max_tokens: 6000, stream_options: { include_usage: true }, ...(model === "kimi-k3" ? { reasoning_effort: "low" } : {}) }); const acc: any = { role: "assistant", content: "", reasoning_content: "", tool_calls: [] as any[] }; let finish: string | null = null; let usage: any = null; const tcChunks: any[] = []; for (const c of s.chunks) { if (c === "[DONE]") continue; if (c.usage) usage = c.usage; for (const ch of c.choices ?? []) { const d = ch.delta ?? {}; if (d.content) acc.content += d.content; if (d.reasoning_content) acc.reasoning_content += d.reasoning_content; if (d.tool_calls) { tcChunks.push(d.tool_calls); for (const tc of d.tool_calls) { const i = tc.index ?? 0; acc.tool_calls[i] ??= { id: "", type: "", function: { name: "", arguments: "" } }; if (tc.id) acc.tool_calls[i].id = tc.id; if (tc.type) acc.tool_calls[i].type = tc.type; if (tc.function?.name) acc.tool_calls[i].function.name += tc.function.name; if (tc.function?.arguments) acc.tool_calls[i].function.arguments += tc.function.arguments; } } if (ch.finish_reason) finish = ch.finish_reason; } } console.log(`[${model}] step1 ${s.status} finish=${finish} tcChunks=${tcChunks.length} usage=${JSON.stringify(usage)} error=${short(s.error, 300)}`); console.log(" first tool_calls chunk:", short(tcChunks[0], 500)); console.log(" accumulated tool_calls:", short(acc.tool_calls, 800)); console.log(" content:", short(acc.content, 200), "reasoning len:", acc.reasoning_content.length); const out: any = { step1: { status: s.status, finish, usage, tcChunks, acc, error: s.error } }; if (acc.tool_calls.length) { // step 2: return arguments verbatim const toolMsgs = acc.tool_calls.map((tc: any) => ({ role: "tool", tool_call_id: tc.id, name: tc.function.name, content: tc.function.arguments })); const r2 = await rawPost("/chat/completions", { model, messages: [...messages, { role: "assistant", content: acc.content || null, reasoning_content: acc.reasoning_content, tool_calls: acc.tool_calls }, ...toolMsgs], tools, max_tokens: 6000, ...(model === "kimi-k3" ? { reasoning_effort: "low" } : {}) }); const m = (r2.body as any)?.choices?.[0]?.message; console.log(` step2 ${r2.status} finish=${(r2.body as any)?.choices?.[0]?.finish_reason} keys=${Object.keys(m ?? {})} usage=${JSON.stringify((r2.body as any)?.usage)}`); console.log(" step2 content:", short(m?.content ?? r2.body, 1200)); console.log(" step2 top-level keys:", Object.keys((r2.body as any) ?? {})); console.log(" step2 choice keys:", Object.keys((r2.body as any)?.choices?.[0] ?? {})); out.step2 = r2; // if it called search again, do a 3rd round if (m?.tool_calls?.length) { const r3 = await rawPost("/chat/completions", { model, messages: [...messages, { role: "assistant", content: acc.content || null, reasoning_content: acc.reasoning_content, tool_calls: acc.tool_calls }, ...toolMsgs, m, ...m.tool_calls.map((tc: any) => ({ role: "tool", tool_call_id: tc.id, name: tc.function.name, content: tc.function.arguments }))], tools, max_tokens: 6000, ...(model === "kimi-k3" ? { reasoning_effort: "low" } : {}) }); console.log(` step3 ${r3.status} finish=${(r3.body as any)?.choices?.[0]?.finish_reason}`, short((r3.body as any)?.choices?.[0]?.message?.content ?? r3.body, 1200), JSON.stringify((r3.body as any)?.usage)); out.step3 = r3; } } save(`06-websearch-${model}`, out);