import { rawPost, rawGet, rawSSE, save, short } from "./lib.ts"; const out: any = {}; // A) Responses API web_search on kimi-k3 (streamed) — server-side tool const s = await rawSSE("/responses", { model: "kimi-k3", input: "What was the closing value of the S&P 500 yesterday? Cite the source URL.", tools: [{ type: "web_search" }], reasoning: { effort: "low" }, max_output_tokens: 4000 }); const types = [...new Set(s.chunks.filter((c) => c !== "[DONE]").map((c: any) => c.type))]; console.log("responses web_search:", s.status, "event types:", types, "error:", short(s.error, 400)); const completed = s.chunks.find((c: any) => c?.type === "response.completed"); if (completed) { const resp = (completed as any).response; console.log(" output item types:", resp.output?.map((o: any) => o.type), "usage:", JSON.stringify(resp.usage)); const txt = resp.output?.find((o: any) => o.type === "message"); console.log(" text:", short(txt?.content?.[0]?.text, 600)); console.log(" annotations:", short(txt?.content?.[0]?.annotations, 800)); const ws = resp.output?.find((o: any) => o.type === "web_search_call"); console.log(" web_search_call item:", short(ws, 800)); console.log(" response keys:", Object.keys(resp)); } const evLines = s.rawLines.filter((l) => l.startsWith("event:")).slice(0, 5); console.log(" sample event: lines:", evLines); out.responsesWebSearch = { status: s.status, types, chunks: s.chunks, error: s.error, headers: s.headers }; // B) Responses API plain (no tools) — check basic shape + reasoning const s2 = await rawPost("/responses", { model: "kimi-k3", input: "Say hi in 3 words.", reasoning: { effort: "low" }, max_output_tokens: 2000 }); console.log("responses plain:", s2.status, short((s2.body as any)?.output ?? s2.body, 600), JSON.stringify((s2.body as any)?.usage)); out.responsesPlain = s2; // Responses on k2.6 (docs: k3 only) const s3 = await rawPost("/responses", { model: "kimi-k2.6", input: "Say hi in 3 words.", max_output_tokens: 500 }); console.log("responses k2.6:", s3.status, short(s3.body, 300)); out.responsesK26 = s3; // C) $web_search builtin on kimi-k2.6 (thinking enabled) verbatim round trip { const model = "kimi-k2.6"; 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." }]; const r1 = await rawPost("/chat/completions", { model, messages, tools, max_tokens: 6000 }); const m = (r1.body as any)?.choices?.[0]?.message; console.log(`[k2.6] $web_search step1 ${r1.status} finish=${(r1.body as any)?.choices?.[0]?.finish_reason} usage=${JSON.stringify((r1.body as any)?.usage)} msg=${short(m ?? r1.body, 700)}`); out.k26step1 = r1; if (m?.tool_calls?.length) { const toolMsgs = m.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, m, ...toolMsgs], tools, max_tokens: 6000 }); console.log(`[k2.6] $web_search step2 verbatim ${r2.status} usage=${JSON.stringify((r2.body as any)?.usage)} ->`, short((r2.body as any)?.choices?.[0]?.message?.content ?? r2.body, 900)); out.k26step2 = r2; } } // D) $web_search on k2.7-code (one shot) { const r = await rawPost("/chat/completions", { model: "kimi-k2.7-code", messages: [{ role: "user", content: "Search the web: who won the 2026 Stanley Cup?" }], tools: [{ type: "builtin_function", function: { name: "$web_search" } }], max_tokens: 4000 }); console.log(`[k2.7-code] $web_search step1 ${r.status}`, short((r.body as any)?.choices?.[0]?.message ?? r.body, 500)); out.k27step1 = r; } // E) formulas (official tools) discovery for (const p of ["/formulas", "/formulas/moonshot/web-search/tools", "/formulas/web-search/tools"]) { const r = await rawGet(p); console.log(`GET ${p}:`, r.status, short(r.body, 500)); out[`formulas:${p}`] = r; } save("07-websearch-alt", out);