import { MODELS, rawPost, rawSSE, save, short, WEATHER_TOOL } from "./lib.ts"; const out: Record = {}; await Promise.all(MODELS.map(async (model) => { const messages: any[] = [{ role: "user", content: "What is the weather in Montreal right now? Use the tool." }]; const s = await rawSSE("/chat/completions", { model, messages, tools: [WEATHER_TOOL], tool_choice: "auto", max_tokens: 4000, stream_options: { include_usage: true } }); // accumulate const acc: any = { role: "assistant", content: "", reasoning_content: "", tool_calls: [] as any[] }; const toolChunks: any[] = []; let finish: string | null = null; let usage: any = null; 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) { toolChunks.push(d.tool_calls); for (const tc of d.tool_calls) { const i = tc.index ?? 0; acc.tool_calls[i] ??= { id: "", type: "function", 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(`\n[${model}] step1 stream status=${s.status} finish=${finish} nToolChunks=${toolChunks.length} reasoningLen=${acc.reasoning_content.length} content=${short(acc.content, 60)}`); console.log(" tool_calls delta chunks (first 3):", short(toolChunks.slice(0, 3), 700)); console.log(" accumulated:", short(acc.tool_calls, 300), "usage:", JSON.stringify(usage)); if (s.error) console.log(" error:", short(s.error, 400)); const results: any = { step1: { status: s.status, finish, toolChunks, acc, usage, error: s.error } }; if (acc.tool_calls.length) { const toolMsgs = acc.tool_calls.map((tc: any) => ({ role: "tool", tool_call_id: tc.id, name: tc.function.name, content: JSON.stringify({ city: "Montreal", temp_c: 21, condition: "sunny" }) })); // (A) full replay incl. reasoning_content const withR = 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: [WEATHER_TOOL], max_tokens: 4000 }); console.log(` step2 WITH reasoning_content: ${withR.status} finish=${(withR.body as any)?.choices?.[0]?.finish_reason} ${short((withR.body as any)?.choices?.[0]?.message?.content ?? withR.body, 120)}`); // (B) replay WITHOUT reasoning_content const noR = await rawPost("/chat/completions", { model, messages: [...messages, { role: "assistant", content: acc.content || null, tool_calls: acc.tool_calls }, ...toolMsgs], tools: [WEATHER_TOOL], max_tokens: 4000 }); console.log(` step2 WITHOUT reasoning_content: ${noR.status} finish=${(noR.body as any)?.choices?.[0]?.finish_reason} ${short((noR.body as any)?.choices?.[0]?.message?.content ?? noR.body, 200)}`); // (C) replay with EMPTY reasoning_content string const emptyR = await rawPost("/chat/completions", { model, messages: [...messages, { role: "assistant", content: acc.content || null, reasoning_content: "", tool_calls: acc.tool_calls }, ...toolMsgs], tools: [WEATHER_TOOL], max_tokens: 4000 }); console.log(` step2 EMPTY reasoning_content: ${emptyR.status} finish=${(emptyR.body as any)?.choices?.[0]?.finish_reason} ${short((emptyR.body as any)?.choices?.[0]?.message?.content ?? emptyR.body, 200)}`); results.step2 = { withReasoning: withR, withoutReasoning: noR, emptyReasoning: emptyR }; results.step2WithUsage = (withR.body as any)?.usage; console.log(" step2 usage (with):", JSON.stringify((withR.body as any)?.usage)); } out[model] = results; })); // k2.6 thinking disabled: tool round trip without reasoning at all { const model = "kimi-k2.6"; const messages: any[] = [{ role: "user", content: "What is the weather in Montreal right now? Use the tool." }]; const r1 = await rawPost("/chat/completions", { model, messages, tools: [WEATHER_TOOL], thinking: { type: "disabled" }, max_tokens: 500 }); const m = (r1.body as any)?.choices?.[0]?.message; console.log(`\n[k2.6 thinking=disabled] step1 ${r1.status} finish=${(r1.body as any)?.choices?.[0]?.finish_reason} keys=${Object.keys(m ?? {})} tc=${m?.tool_calls?.length}`); let r2: any = null; if (m?.tool_calls?.length) { r2 = await rawPost("/chat/completions", { model, messages: [...messages, m, ...m.tool_calls.map((tc: any) => ({ role: "tool", tool_call_id: tc.id, name: tc.function.name, content: "{\"temp_c\":21}" }))], tools: [WEATHER_TOOL], thinking: { type: "disabled" }, max_tokens: 500 }); console.log(` step2 ${r2.status} ${short((r2.body as any)?.choices?.[0]?.message?.content ?? r2.body, 150)}`); } out["kimi-k2.6/thinking-disabled"] = { r1, r2 }; } save("03-tools-stream", out);