Files
plugins/packages/qoder/jsoncall.test.mjs
T
yetone 76cfb7fe86 qoder 0.2.68: a bare {"tool_calls":[...]} reply is its calls (#1113)
A model on Qoder CN (auto/minimax-m3.1-flash) answered with OpenAI's own
wire shape written as text, {"tool_calls":[{"name","arguments"}]}. The
Splitter only knew <tool_call> and the bare XML tags, so the block went
out as the answer with finish "stop" and the calls were lost.

The Splitter now reads such an object as those calls and finishes
tool_calls. It counts only when the object starts its own line, its first
key is "tool_calls", and it is outside code (quoted()). Each entry needs a
name and arguments that are an object or a JSON string ({name, arguments}
or {function:{...}}). It is held until its braces close. A block cut short
at the end of the stream, or with an entry that can't be read, ends the
turn as an error (502 when not streamed), as #132's <tool_call> block
does. An empty array, other JSON, and JSON in prose or code stay text.
Unlike the XML path, this isn't gated on declared tools: the reporter's
request declared none, and the <tool_call> path isn't gated either.

jsoncall.test.mjs uses the issue's bytes: bare, prose then bare, whole
and in 3- and 1-char pieces, streamed and not, the <tool_call> control,
string arguments, four error shapes, and seven text-stays-text cases.

Run (WSL magpie-desktop, bun, HOME=$(mktemp -d)): packages/qoder
`bun test`: 440 pass, 0 fail. jsoncall+textcall+xmlcall at the pushed
tree: 26 pass. `bun scripts/check.mjs qoder`: ok.
Broken by hand (detection turned off): jsoncall goes 7 fail, 2 pass
(the control and text-stays-text).
Siblings filed: #1114 (doubao-work, orcaterm, trae, zen-free).
Not tried: a live Qoder CN account.
2026-11-14 08:35:54 +08:00

135 lines
6.5 KiB
JavaScript

// plugins#1113: a reply that was one JSON object in OpenAI's own wire shape,
// {"tool_calls":[{"name","arguments"}]}, written as text, went out as the
// answer with "stop", the calls lost. The replies are the issue's, byte for
// byte; the object is now those calls, finished tool_calls. One cut short,
// or with an entry that can't be read, ends the turn as an error (as
// plugins#132's <tool_call> block does); JSON in prose or code stays text.
import { afterEach, expect, test } from "bun:test"
import { QoderAuthPlugin } from "./index.mjs"
const real = globalThis.fetch
afterEach(() => (globalThis.fetch = real))
const API_CHAT = "https://api3.qoder.sh/v1/chat/completions"
const LISTING = { chat: [{ key: "efficient", display_name: "Efficient", enable: true }] }
const line = (body) => `data:${JSON.stringify({ headers: {}, body: typeof body === "string" ? body : JSON.stringify(body), statusCodeValue: 200, statusCode: "OK" })}\n\n`
const chunk = (delta, extra = {}) => line({ choices: [{ delta, index: 0, ...extra }], object: "chat.completion.chunk" })
const pieces = (content, size = 3) => Array.from({ length: Math.ceil(content.length / size) }, (_, k) => chunk({ content: content.slice(k * size, (k + 1) * size) })).join("")
const STOP = chunk({}, { finish_reason: "stop" })
// the issue's replies
const BARE = '{"tool_calls":[{"name":"read_file","arguments":{"path":"a.go"}},{"name":"viking_search","arguments":{"query":"x"}}]}'
const CALLS = [
["read_file", { path: "a.go" }],
["viking_search", { query: "x" }],
]
async function ask(reply, stream) {
const auth = { type: "oauth", access: "jt", refresh: "rt", expires: Date.now() + 3_600_000, accountId: "one@x", uid: "u1", machineId: "m1" }
globalThis.fetch = async (url) =>
new URL(String(url)).pathname.endsWith("/model/list")
? new Response(JSON.stringify(LISTING), { headers: { "Content-Type": "application/json" } })
: new Response(reply, { headers: { "Content-Type": "text/event-stream" } })
const hooks = await QoderAuthPlugin({ client: { auth: { set: async () => {} } } })
const l = await hooks.auth.loader(async () => auth)
const body = { model: "efficient", messages: [{ role: "user", content: "hi" }], ...(stream ? { stream: true } : {}) }
return l.fetch(API_CHAT, { method: "POST", body: JSON.stringify(body) })
}
async function streamed(res) {
expect(res.status).toBe(200)
const frames = (await res.text())
.split("\n")
.filter((l) => l.startsWith("data: "))
.map((l) => l.slice(6))
.filter((d) => d !== "[DONE]")
.map((d) => JSON.parse(d))
const deltas = frames.filter((f) => f.choices?.length).map((f) => f.choices[0].delta)
return {
text: deltas.map((d) => d.content ?? "").join(""),
calls: deltas.flatMap((d) => d.tool_calls ?? []).map((t) => [t.function.name, JSON.parse(t.function.arguments)]),
finish: frames.filter((f) => f.choices?.[0]?.finish_reason).map((f) => f.choices[0].finish_reason),
errors: frames.filter((f) => f.error).map((f) => f.error.message),
}
}
async function whole(reply) {
const res = await ask(reply, false)
expect(res.status).toBe(200)
const c = (await res.json()).choices[0]
return { text: c.message.content ?? "", calls: (c.message.tool_calls ?? []).map((t) => [t.function.name, JSON.parse(t.function.arguments)]), finish: c.finish_reason }
}
for (const [label, prose] of [
["bare JSON, no wrapper", ""],
["bare JSON after a line of prose", "Let me read that.\n"],
]) {
test(`${label}: the calls, finished tool_calls, streamed and not`, async () => {
const replies = [(prose ? chunk({ content: prose }) : "") + chunk({ content: BARE }) + STOP, pieces(prose + BARE) + STOP, pieces(prose + BARE, 1) + STOP]
for (const r of replies) {
const out = await streamed(await ask(r, true))
expect(out.text).toBe(prose)
expect(out.calls).toEqual(CALLS)
expect(out.finish).toEqual(["tool_calls"])
expect(out.errors).toEqual([])
expect(await whole(r)).toEqual({ text: prose, calls: CALLS, finish: "tool_calls" })
}
})
}
test("the control: the same call inside <tool_call> is still the call", async () => {
const r = chunk({ content: '<tool_call>{"name":"read_file","arguments":{"path":"a.go"}}</tool_call>' }) + chunk({}, { finish_reason: "tool_calls" })
const out = await streamed(await ask(r, true))
expect(out.calls).toEqual([CALLS[0]])
expect(out.finish).toEqual(["tool_calls"])
})
test("arguments as a JSON string, and the function wrapper, are read too", async () => {
const content = '{"tool_calls":[{"name":"read_file","arguments":"{\\"path\\":\\"a.go\\"}"},{"type":"function","function":{"name":"viking_search","arguments":{"query":"x"}}}]}\n'
const out = await streamed(await ask(chunk({ content }) + STOP, true))
expect(out.calls).toEqual(CALLS)
expect(out.text).toBe("")
expect(out.finish).toEqual(["tool_calls"])
})
for (const [label, content] of [
["cut short", "Let me read that.\n" + BARE.slice(0, 60)],
["an entry with no name", 'Let me read that.\n{"tool_calls":[{"arguments":{"path":"a.go"}}]}'],
["arguments that aren't an object", 'Let me read that.\n{"tool_calls":[{"name":"read_file","arguments":"a.go"}]}'],
["not JSON once whole", 'Let me read that.\n{"tool_calls":[{"name":"read_file","arguments":{"path":}}]}'],
]) {
test(`${label}: the turn ends as an error, not the block as the answer`, async () => {
for (const r of [chunk({ content }) + STOP, pieces(content) + STOP]) {
const out = await streamed(await ask(r, true))
expect(out.text).toBe("Let me read that.\n")
expect(out.calls).toEqual([])
expect(out.finish).toEqual([])
expect(out.errors).toHaveLength(1)
const res = await ask(r, false)
expect(res.status).toBe(502)
expect((await res.json()).error.message).toMatch(/tool call/)
}
})
}
test("JSON in prose, in code, or not a tool_calls object stays text", async () => {
for (const content of [
'Reply with {"tool_calls":[{"name":"read_file","arguments":{"path":"a.go"}}]} to call it.',
"An example:\n```json\n" + BARE + "\n```\n",
"Inline: `" + BARE + "`",
'{"tool_calls":[]}',
'{"answer":42}',
"{tool_calls are how a model asks}",
"{",
]) {
for (const r of [chunk({ content }) + STOP, pieces(content) + STOP]) {
const out = await streamed(await ask(r, true))
expect(out.text).toBe(content)
expect(out.calls).toEqual([])
expect(out.finish).toEqual(["stop"])
expect(out.errors).toEqual([])
expect(await whole(r)).toEqual({ text: content, calls: [], finish: "stop" })
}
}
})