import { client, rawPost, rawGet, save, short, last429, sleep } from "./lib.ts"; import { readFileSync } from "node:fs"; const out: any = {}; const q = "What was the closing value of the S&P 500 on its most recent trading day? Search the web and cite the source URL."; const builtin = [{ type: "builtin_function", function: { name: "$web_search" } }]; // (a) k3 $web_search verbatim, default effort { const messages: any[] = [{ role: "user", content: q }]; const r1 = await rawPost("/chat/completions", { model: "kimi-k3", messages, tools: builtin, max_tokens: 8000 }); const m = (r1.body as any)?.choices?.[0]?.message; console.log("[k3 default effort] step1", r1.status, short(m, 400)); if (m?.tool_calls?.length) { const r2 = await rawPost("/chat/completions", { model: "kimi-k3", messages: [...messages, m, ...m.tool_calls.map((tc: any) => ({ role: "tool", tool_call_id: tc.id, name: tc.function.name, content: tc.function.arguments }))], tools: builtin, max_tokens: 8000 }); console.log("[k3 default effort] step2 verbatim", r2.status, JSON.stringify((r2.body as any)?.usage), short((r2.body as any)?.choices?.[0]?.message?.content ?? r2.body, 500)); out.k3builtin = { r1, r2 }; } } // (b) k2.7-code verbatim step2 from saved step1 { const prev = JSON.parse(readFileSync(new URL("./out/07-websearch-alt.json", import.meta.url), "utf8")); const m = prev.k27step1?.body?.choices?.[0]?.message; if (m?.tool_calls?.length) { const messages = [{ role: "user", content: "Search the web: who won the 2026 Stanley Cup?" }]; const r2 = await rawPost("/chat/completions", { model: "kimi-k2.7-code", messages: [...messages, m, ...m.tool_calls.map((tc: any) => ({ role: "tool", tool_call_id: tc.id, name: tc.function.name, content: tc.function.arguments }))], tools: builtin, max_tokens: 8000 }); console.log("[k2.7-code] $web_search step2 verbatim", r2.status, JSON.stringify((r2.body as any)?.usage), short((r2.body as any)?.choices?.[0]?.message?.content ?? r2.body, 500)); out.k27builtinStep2 = r2; } } // (c) formulas: official web-search tool via fibers on k3 { const decl = await rawGet("/formulas/moonshot/web-search:latest/tools"); console.log("[formula] GET tools", decl.status, short(decl.body, 300)); const tools = (decl.body as any)?.tools ?? []; const messages: any[] = [{ role: "user", content: q }]; const r1 = await rawPost("/chat/completions", { model: "kimi-k3", messages, tools, reasoning_effort: "low", max_tokens: 8000 }); const m = (r1.body as any)?.choices?.[0]?.message; console.log("[formula] step1", r1.status, JSON.stringify((r1.body as any)?.usage), short(m, 500)); out.formula = { decl, r1 }; if (m?.tool_calls?.length) { const tc = m.tool_calls[0]; const fiber = await rawPost("/formulas/moonshot/web-search:latest/fibers", { name: tc.function.name, arguments: tc.function.arguments }); const fb: any = fiber.body; console.log("[formula] fiber", fiber.status, "keys:", Object.keys(fb ?? {}), "status:", fb?.status, "context keys:", Object.keys(fb?.context ?? {}), "output:", short(fb?.context?.output, 300), "encrypted:", short(fb?.context?.encrypted_output, 120)); out.formula.fiber = fiber; const content = fb?.context?.encrypted_output ?? fb?.context?.output ?? ""; const r2 = await rawPost("/chat/completions", { model: "kimi-k3", messages: [...messages, m, { role: "tool", tool_call_id: tc.id, content }], tools, reasoning_effort: "low", max_tokens: 8000 }); console.log("[formula] step2", r2.status, JSON.stringify((r2.body as any)?.usage), short((r2.body as any)?.choices?.[0]?.message?.content ?? r2.body, 600)); out.formula.r2 = r2; } } // (d) k2.6 json_schema strict again: thinking on and off { const schema = { type: "object", properties: { city: { type: "string" }, population: { type: "integer" }, size: { type: "string", enum: ["small", "medium", "large"] } }, required: ["city", "population", "size"], additionalProperties: false }; for (const thinking of [{ type: "enabled" }, { type: "disabled" }]) { const r = await rawPost("/chat/completions", { model: "kimi-k2.6", messages: [{ role: "user", content: "Describe Montreal." }], response_format: { type: "json_schema", json_schema: { name: "city_info", strict: true, schema } }, thinking, max_tokens: 3000 }); const c = (r.body as any)?.choices?.[0]?.message?.content; let ok = false; try { JSON.parse(c); ok = true; } catch {} console.log(`[k2.6 json_schema thinking=${thinking.type}]`, r.status, "parses:", ok, short(c, 200), JSON.stringify((r.body as any)?.usage)); out[`k26schema:${thinking.type}`] = r; } } // (e) highspeed logprobs single; 429 headers if any { await sleep(2000); const r = await rawPost("/chat/completions", { model: "kimi-k2.7-code-highspeed", messages: [{ role: "user", content: "Reply: ok" }], logprobs: true, top_logprobs: 2, max_tokens: 60 }); console.log("[highspeed logprobs]", r.status, r.attempts, short((r.body as any)?.choices?.[0]?.logprobs ?? r.body, 300)); out.hsLogprobs = r; console.log("last429 headers:", JSON.stringify(last429?.headers), short(last429?.body, 200)); out.last429 = last429; } // (f) OpenAI SDK smoke test: streaming + thinking + tools on k2.6 { const stream = await client.chat.completions.create({ model: "kimi-k2.6", messages: [{ role: "user", content: "Weather in Montreal? Use the tool." }], tools: [{ type: "function", function: { name: "get_weather", parameters: { type: "object", properties: { city: { type: "string" } }, required: ["city"] } } }], stream: true, stream_options: { include_usage: true }, max_tokens: 3000, ...( { thinking: { type: "enabled" } } as any ), }); let reasoning = "", args = "", usage: any = null, finish: string | null = null, id = ""; for await (const chunk of stream) { const d: any = chunk.choices[0]?.delta ?? {}; if (d.reasoning_content) reasoning += d.reasoning_content; for (const tc of d.tool_calls ?? []) { if (tc.id) id = tc.id; args += tc.function?.arguments ?? ""; } if (chunk.choices[0]?.finish_reason) finish = chunk.choices[0].finish_reason; if (chunk.usage) usage = chunk.usage; } console.log("[SDK] finish:", finish, "reasoning chars:", reasoning.length, "tool id:", id, "args:", args, "usage:", JSON.stringify(usage)); out.sdk = { finish, reasoningLen: reasoning.length, id, args, usage }; } save("08-misc", out);