From 2db9b0ad7f0ffd18f366a49ddeff9d0f7345b837 Mon Sep 17 00:00:00 2001 From: yetone Date: Sat, 14 Nov 2026 10:02:43 +0800 Subject: [PATCH] =?UTF-8?q?orcaterm=200.1.8:=20a=20bare=20{"tool=5Fcalls":?= =?UTF-8?q?[=E2=80=A6]}=20reply=20is=20refused,=20not=20sent=20as=20the=20?= =?UTF-8?q?answer=20(#1114)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #1113's model wrote OpenAI's own wire shape as text, on a line of its own: {"tool_calls":[{"id":…,"function":{…}}]}. OrcaTerm sends no tools upstream, so such a reply is a call with nothing to call: it is now cut where the call starts and the turn ends with the text_tool_call error, the same as a block. Streamed, a last line that may still grow into {"tool_calls":[{ (its indent too) is held until it settles. Prose that mentions the shape, a fenced or backticked copy, {"tool_calls":[]} and other JSON lines stay the answer. Sibling: doubao-work 0.1.12 (4ea39f01) lifts the same shape as calls, since doubao-work does carry tools. Run (WSL magpie-desktop, HOME=$(mktemp -d)): - packages/orcaterm bun test: 164 pass, 0 fail; jsoncall.test.mjs covers 1-, 5- and 4096-character frames with #1113's bytes. - Broken by hand: without the new regex alternative, all 4 jsoncall tests fail; restored, all pass. - bun scripts/check.mjs orcaterm: ok. Not tried: a real OrcaTerm account producing this reply. --- packages/orcaterm/index.mjs | 22 +++++++++-- packages/orcaterm/jsoncall.test.mjs | 57 +++++++++++++++++++++++++++++ packages/orcaterm/package.json | 2 +- 3 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 packages/orcaterm/jsoncall.test.mjs diff --git a/packages/orcaterm/index.mjs b/packages/orcaterm/index.mjs index 529848c..d29e55b 100644 --- a/packages/orcaterm/index.mjs +++ b/packages/orcaterm/index.mjs @@ -950,6 +950,10 @@ const TEXT_CALL_OPEN = new RegExp( String.raw``, String.raw` m.toLowerCase().startsWith(low)) ? at : -1 + const tag = textCallAt(tail, true) === 0 || TEXT_CALL_MARKS.some((m) => m.toLowerCase().startsWith(low)) ? at : -1 + return json >= 0 && (tag < 0 || json < tag) ? json : tag +} + +/** + * Whether a last line may still grow into {"tool_calls":[{ (plugins#1114). + * A line of indent alone is held too, so the indent before such a call + * isn't sent ahead of it; the next character settles it. + */ +function jsonCallHold(line) { + const bare = line.replace(/\s+/g, "") + return line !== "" && '{"tool_calls":[{'.startsWith(bare) } /** A call the model wrote as text, with nothing left to send. */ diff --git a/packages/orcaterm/jsoncall.test.mjs b/packages/orcaterm/jsoncall.test.mjs new file mode 100644 index 0000000..be66dd3 --- /dev/null +++ b/packages/orcaterm/jsoncall.test.mjs @@ -0,0 +1,57 @@ +// plugins#1114, the sibling of qoder's #1113: a reply that writes its call +// as one JSON object in OpenAI's own wire shape, {"tool_calls":[{…}]}, went +// out as the answer. OrcaTerm is sent no tools, so, as with a +// block here, the text stops where the object begins and the turn ends with +// the text_tool_call error. The object is #1113's, byte for byte. JSON in +// prose or code, and other objects, stay the answer. +import { test, expect } from "bun:test" +import { _internal } from "./index.mjs" + +process.env.ORCATERM_NO_PACE = "1" + +const BARE = '{"tool_calls":[{"name":"read_file","arguments":{"path":"a.go"}},{"name":"viking_search","arguments":{"query":"x"}}]}' +const frame = (content) => `data: ${JSON.stringify({ content, reasoningContent: "", type: "ai", code: 0 })}\n\n` +const sse = (frames) => new ReadableStream({ start(c) { for (const f of frames) c.enqueue(new TextEncoder().encode(f)); c.close() } }) +const pieces = (s, n) => Array.from({ length: Math.ceil(s.length / n) }, (_, i) => s.slice(i * n, (i + 1) * n)) + +function read(text) { + let content = "" + let error = null + for (const line of text.split("\n")) { + if (!line.startsWith("data: ") || line.trim() === "data: [DONE]") continue + const j = JSON.parse(line.slice(6)) + if (j.error) error = j.error + content += j.choices?.[0]?.delta?.content ?? "" + } + return { content, error } +} +const asked = async (s, n = 4096) => read(await _internal.upstreamToOpenAI(sse(pieces(s, n).map(frame)), { model: "m" }).text()) + +for (const n of [1, 5, 4096]) { + test(`a bare {"tool_calls":[…]} reply in ${n}-character frames is refused, never sent as the answer`, async () => { + const a = await asked(BARE, n) + expect(a.content).toBe("") + expect(a.error?.code).toBe("text_tool_call") + const b = await asked("Reading it.\n " + BARE, n) + expect(b.content).toBe("Reading it.\n") + expect(b.error?.code).toBe("text_tool_call") + }) +} + +test("the same shape in prose, in code, or with no calls is the answer", async () => { + for (const t of [ + 'The API answers {"tool_calls":[{"name":"a"}]} there.', + "```json\n" + BARE + "\n```", + '{"tool_calls":[]}', + '{"total": 3}', + ]) { + for (const n of [1, 4096]) { + const a = await asked(t, n) + expect(a.error).toBeNull() + expect(a.content).toBe(t) + } + } + expect(_internal.textCallIn(BARE)).toBe(0) + expect(_internal.textCallHold('ok\n{"tool')).toBe(3) + expect(_internal.textCallHold('ok\n{"total"')).toBe(-1) +}) diff --git a/packages/orcaterm/package.json b/packages/orcaterm/package.json index ef352ab..2478729 100644 --- a/packages/orcaterm/package.json +++ b/packages/orcaterm/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-orcaterm-auth", - "version": "0.1.7", + "version": "0.1.8", "description": "OrcaTerm (Tencent Cloud 遨驰终端) AI quota as an OpenAI-compatible provider for magpie and OpenCode", "type": "module", "main": "./opencode.mjs",