// Model requests: OpenAI chat completions translated for the IDE agent's // llm_utils_chat, and its SSE events back into an OpenAI answer. import "./nonet.mjs" import { afterEach, expect, test } from "bun:test" import { TraeCNAuthPlugin, _internal } from "./index.mjs" import { fakeTrae, json, signedIn, sse } from "./fake.mjs" let f afterEach(() => f?.close()) async function ask(body, auth = signedIn()) { const hooks = await TraeCNAuthPlugin({ client: {} }) const opts = await hooks.auth.loader(async () => auth) return opts.fetch(opts.baseURL + "/chat/completions", { method: "POST", body: JSON.stringify(body) }) } async function chunks(res) { const out = [] for (const line of (await res.text()).split("\n")) { if (!line.startsWith("data: ")) continue const d = line.slice(6) out.push(d === "[DONE]" ? d : JSON.parse(d)) } return out } const TOOLS = [{ type: "function", function: { name: "read", description: "Read a file", parameters: { type: "object", properties: { path: { type: "string" } } } } }] test("the request is the IDE's: its headers, the device, text parts, the tools natively and in the prompt", async () => { f = fakeTrae() f.route("POST /api/agent/v3/llm_utils_chat", () => sse([["output", { response: "ok" }], ["done", {}]])) await ask({ model: "glm-5", max_tokens: 100, tools: TOOLS, messages: [{ role: "system", content: "be brief" }, { role: "user", content: [{ type: "text", text: "hi" }] }] }) const r = f.seen[0] expect(r.headers.get("authorization")).toBe("Cloud-IDE-JWT jwt-1") expect(r.headers.get("x-cloudide-token")).toBe("jwt-1") expect(r.headers.get("x-device-id")).toBe("1234567890123456789") expect(r.headers.get("x-machine-id")).toBe("ab".repeat(16)) expect(r.headers.get("x-uid")).toBe("u-1") expect(r.headers.get("accept")).toBe("text/event-stream") const b = r.json expect(b.function).toBe("chat_v3") expect(b.config_name).toBe("glm-5") expect(b.model).toBe("glm-5") expect(b.max_tokens).toBe(100) expect(b.stream).toBe(true) expect(b.messages.map((m) => m.role)).toEqual(["system", "system", "user"]) expect(b.messages[0].content[0].text).toContain("") expect(b.messages[2].content).toEqual([{ type: "text", text: "hi" }]) expect(b.tools[0].function.name).toBe("read") expect(typeof b.tools[0].function.parameters).toBe("string") }) test("tool history goes in as text", () => { const m = _internal.traeMessages({ messages: [ { role: "user", content: "read a" }, { role: "assistant", content: null, tool_calls: [{ id: "c1", type: "function", function: { name: "read", arguments: "{\"path\":\"a\"}" } }] }, { role: "tool", tool_call_id: "c1", content: "AAA" }, { role: "tool", tool_call_id: "c2", content: "BBB" }, ] }) expect(m.map((x) => x.role)).toEqual(["user", "assistant", "user"]) expect(m[1].content[0].text).toBe('{"name":"read","arguments":{"path":"a"}}') expect(m[2].content[0].text).toContain("Result of read (call c1):\nAAA") expect(m[2].content[0].text).toContain("BBB") }) test("a stream: text, reasoning, a tool block split across events, a native call, usage", async () => { f = fakeTrae() f.route("POST /api/agent/v3/llm_utils_chat", () => sse([ ["request_wait_in_queue", { position: 3 }], ["output", { reasoning_content: "think" }], ["output", { response: "Let me look. {"name":"read","arguments":{"path":"a"}}' }], ["output", { tool_calls: [{ id: "n1", function: { name: "read", arguments: { path: "b" } } }] }], ["token_usage", { usage: { prompt_tokens: 10, completion_tokens: 5 } }], ["done", { finish_reason: "stop" }], ])) const res = await ask({ model: "glm-5", stream: true, tools: TOOLS, messages: [{ role: "user", content: "hi" }] }) expect(res.status).toBe(200) expect(res.headers.get("content-type")).toBe("text/event-stream") expect(res.headers.get("X-Magpie-Sign-In")).toBe("kept") const c = await chunks(res) const deltas = c.filter((x) => x !== "[DONE]" && x.choices?.length).map((x) => x.choices[0].delta) expect(deltas.map((d) => d.content ?? "").join("")).toBe("Let me look. ") expect(deltas.find((d) => d.reasoning_content).reasoning_content).toBe("think") const calls = deltas.flatMap((d) => d.tool_calls ?? []) expect(calls.map((t) => [t.index, t.function.name, t.function.arguments])).toEqual([[0, "read", '{"path":"a"}'], [1, "read", '{"path":"b"}']]) expect(calls[1].id).toBe("n1") expect(c.at(-3).choices[0].finish_reason).toBe("tool_calls") expect(c.at(-2).usage).toEqual({ prompt_tokens: 10, completion_tokens: 5, total_tokens: 15 }) expect(c.at(-1)).toBe("[DONE]") }) test("a native call streamed over several events goes on whole (#799)", async () => { f = fakeTrae() f.route("POST /api/agent/v3/llm_utils_chat", () => sse([ // the arguments so far, each event ["output", { tool_calls: [{ id: "b1", index: 0, function: { name: "bash", arguments: "" } }] }], ["output", { tool_calls: [{ id: "b1", index: 0, function: { name: "bash", arguments: '{"command":"echo \\"T' } }] }], ["output", { tool_calls: [{ id: "b1", index: 0, function: { name: "bash", arguments: '{"command":"echo \\"Trae\\""}' } }] }], // only the new piece, the name only at first ["output", { tool_calls: [{ id: "r1", index: 1, function: { name: "read", arguments: '{"filePath":"C:/Users/j' } }] }], ["output", { tool_calls: [{ index: 1, function: { arguments: 'oe/a.txt"}' } }] }], // a whole call sent again ["output", { tool_calls: [{ id: "r1", index: 1, function: { name: "read", arguments: '{"filePath":"C:/Users/joe/a.txt"}' } }] }], ["done", {}], ])) const res = await ask({ model: "glm-5", stream: true, tools: TOOLS, messages: [{ role: "user", content: "hi" }] }) const c = await chunks(res) const calls = c.filter((x) => x !== "[DONE]" && x.choices?.length).flatMap((x) => x.choices[0].delta.tool_calls ?? []) expect(calls.map((t) => [t.id, t.function.name, JSON.parse(t.function.arguments)])).toEqual([ ["b1", "bash", { command: 'echo "Trae"' }], ["r1", "read", { filePath: "C:/Users/joe/a.txt" }], ]) }) test("no stream asked: one chat completion", async () => { f = fakeTrae() f.route("POST /api/agent/v3/llm_utils_chat", () => sse([["output", { content: "Hel" }], ["output", { content: "lo" }], ["done", {}]])) const res = await ask({ model: "glm-5", messages: [{ role: "user", content: "hi" }] }) const b = await res.json() expect(b.object).toBe("chat.completion") expect(b.choices[0].message).toEqual({ role: "assistant", content: "Hello" }) expect(b.choices[0].finish_reason).toBe("stop") }) test("a text like a tag that isn't one goes through whole", () => { const t = new _internal.TextTools() expect(t.push("a b").text).toBe(" b") expect(t.push("", true).text).toBe("") }) test("a model chat_v3 doesn't know is asked of SOLO's function, which is kept for the account", async () => { f = fakeTrae() f.route("POST /api/agent/v3/llm_utils_chat", (r) => (r.json.function === "chat_v3" ? sse([["error", { code: 4023, message: "model is unknown" }]]) : sse([["output", { response: "solo" }], ["done", {}]]))) const hooks = await TraeCNAuthPlugin({ client: {} }) const opts = await hooks.auth.loader(async () => signedIn()) const body = JSON.stringify({ model: "kimi-k3", messages: [{ role: "user", content: "hi" }] }) const res = await opts.fetch(opts.baseURL + "/chat/completions", { method: "POST", body }) expect((await res.json()).choices[0].message.content).toBe("solo") expect(f.seen.map((r) => r.json.function)).toEqual(["chat_v3", "solo_work_lite"]) await opts.fetch(opts.baseURL + "/chat/completions", { method: "POST", body }) expect(f.seen.at(-1).json.function).toBe("solo_work_lite") }) test("signed out (1001, or a 401) is a 401 that marks the account", async () => { f = fakeTrae() f.route("POST /api/agent/v3/llm_utils_chat", () => sse([["error", { code: 1001, message: "not login" }]])) let res = await ask({ model: "glm-5", messages: [{ role: "user", content: "hi" }] }) expect(res.status).toBe(401) expect(res.headers.get("X-Magpie-Sign-In")).toBe("expired") expect((await res.json()).error.message).toContain("sign in again") f.route("POST /api/agent/v3/llm_utils_chat", () => json({ code: 401, message: "unauthorized" }, 401)) res = await ask({ model: "glm-5", messages: [{ role: "user", content: "hi" }] }) expect(res.status).toBe(401) expect(res.headers.get("X-Magpie-Sign-In")).toBe("expired") }) test("out of quota (4008) is a 429, the account kept", async () => { f = fakeTrae() f.route("POST /api/agent/v3/llm_utils_chat", () => sse([["error", { code: 4008, message: "quota exceeded" }]])) const res = await ask({ model: "glm-5", stream: true, messages: [{ role: "user", content: "hi" }] }) expect(res.status).toBe(429) expect(res.headers.get("X-Magpie-Sign-In")).toBe("kept") expect((await res.json()).error.message).toBe("Trae CN: quota exceeded") }) test("a tool block with a file's content written out raw, or its arguments beside the name, goes on whole (#799)", async () => { f = fakeTrae() const block = '{"name":"write","arguments":{"path":"a.py","content":"import sys\n\tprint(\\"hi\\")\n"}}' f.route("POST /api/agent/v3/llm_utils_chat", () => sse([ ["output", { response: "Writing it. " + block }], ["output", { response: '{"name":"read","path":"a.py"}' }], ["done", {}], ])) const res = await ask({ model: "glm-5", stream: true, tools: TOOLS, messages: [{ role: "user", content: "hi" }] }) const c = await chunks(res) const deltas = c.filter((x) => x !== "[DONE]" && x.choices?.length).map((x) => x.choices[0].delta) expect(deltas.map((d) => d.content ?? "").join("")).toBe("Writing it. ") const calls = deltas.flatMap((d) => d.tool_calls ?? []) expect(calls.map((t) => [t.function.name, JSON.parse(t.function.arguments)])).toEqual([ ["write", { path: "a.py", content: 'import sys\n\tprint("hi")\n' }], ["read", { path: "a.py" }], ]) }) test("a native call's pieces that say neither id nor name join the call being written (#799)", () => { const nc = new _internal.NativeCalls() nc.push({ id: "w1", function: { name: "write", arguments: '{"path":"a.py",' } }) nc.push({ index: 0, function: { arguments: '"content":"x\ny"' } }) nc.push({ function: { arguments: "}" } }) expect(nc.take().map((t) => [t.id, t.name, JSON.parse(t.arguments)])).toEqual([["w1", "write", { path: "a.py", content: "x\ny" }]]) }) test("a whole snapshot Trae sends again, written apart from the partial one, replaces it, not joins it (plugins#24)", () => { const nc = new _internal.NativeCalls() nc.push({ id: "b1", function: { name: "bash", arguments: '{"command":"git log -' } }) // the same call again, whole, its punctuation not the partial one's nc.push({ id: "b1", function: { arguments: '{"command":"git log −1 --format=′%s′"}' } }) expect(nc.take().map((t) => [t.name, JSON.parse(t.arguments)])).toEqual([["bash", { command: "git log −1 --format=′%s′" }]]) }) test("pieces that are JSON values of their own still join the call (plugins#24)", () => { for (const pieces of [ ['{"n":', "12", "}"], ['{"cmd":', '"ls"', "}"], ['{"a":', "{}", "}"], ['{"a":{"b":1},', '"c":', '{"b":2}', "}"], ["{", "}"], ]) { const nc = new _internal.NativeCalls() nc.push({ id: "x", function: { name: "t", arguments: pieces[0] } }) for (const p of pieces.slice(1)) nc.push({ id: "x", function: { arguments: p } }) expect(JSON.parse(nc.take()[0].arguments)).toEqual(JSON.parse(pieces.join(""))) } // snapshots as before: each the arguments so far const nc = new _internal.NativeCalls() for (const s of ['{"p', '{"path":"a', '{"path":"a.py"}']) nc.push({ id: "r", function: { name: "read", arguments: s } }) expect(JSON.parse(nc.take()[0].arguments)).toEqual({ path: "a.py" }) }) test("looseJSON reads raw control characters in strings only", () => { expect(_internal.looseJSON('{"a":"x\ny","b":"\\"q\\""}')).toEqual({ a: "x\ny", b: '"q"' }) expect(_internal.looseJSON('{"a":\n1}')).toEqual({ a: 1 }) expect(_internal.looseJSON('{"a":"x')).toBe(null) }) test("a block DeepSeek closes with its own tags, or writes as its own invoke, is a call; its tags don't reach the agent (magpie#823)", async () => { // as Dazzle-sys' screenshot had it, in pieces const hermes = '{"name":"bash","arguments":{"command":"cd /e/Project && echo \\"### $spec\\"; done"}}\n o.text).join("")).toBe("Checking. ") expect(out.flatMap((o) => o.calls).map((c) => [c.name, JSON.parse(c.arguments)])).toEqual([["bash", { command: 'cd /e/Project && echo "### $spec"; done' }]]) f = fakeTrae() const dsml = '<|DSML|function_calls>\n<|DSML|invoke name="read">\n<|DSML|parameter name="path" string="true">a b.py\n<|DSML|parameter name="limit" string="false">20\n\n<|DSML|invoke name="read"><|DSML|parameter name="path" string="true">c\n' f.route("POST /api/agent/v3/llm_utils_chat", () => sse([ ["output", { response: "Reading. " + dsml.slice(0, 50) }], ["output", { response: dsml.slice(50, 140) }], ["output", { response: dsml.slice(140) }], ["done", {}], ])) const res = await ask({ model: "glm-5", stream: true, tools: TOOLS, messages: [{ role: "user", content: "hi" }] }) const c = await chunks(res) const deltas = c.filter((x) => x !== "[DONE]" && x.choices?.length).map((x) => x.choices[0].delta) expect(deltas.map((d) => d.content ?? "").join("")).toBe("Reading. ") const calls = deltas.flatMap((d) => d.tool_calls ?? []) expect(calls.map((t) => [t.function.name, JSON.parse(t.function.arguments)])).toEqual([ ["read", { path: "a b.py", limit: 20 }], ["read", { path: "c" }], ]) expect(c.at(-2).choices[0].finish_reason).toBe("tool_calls") }) test("a with only DeepSeek's closing tags after it says nothing; one wrapping its invoke is the call (magpie#823 again)", () => { // Dazzle-sys' pi screenshot: the whole reply, cut short const junk = "\n\n\n o.text).join("")).toBe("") expect(out.flatMap((o) => o.calls)).toEqual([]) } const wrapped = 'Looking.\n<|DSML|invoke name="bash">\n<|DSML|parameter name="command" string="true">ls\n\n' const t = new _internal.TextTools() const out = [t.push(wrapped.slice(0, 20)), t.push(wrapped.slice(20)), t.push("", true)] expect(out.map((o) => o.text).join("")).toBe("Looking.") expect(out.flatMap((o) => o.calls).map((c) => [c.name, JSON.parse(c.arguments)])).toEqual([["bash", { command: "ls" }]]) // a block that's text, not tags, stays as it was const u = new _internal.TextTools() expect([u.push("hello ok"), u.push("", true)].map((o) => o.text).join("")).toBe("hello ok") })