From 32aaeda5a61d01b111bbb5466370723e0c0fffbd Mon Sep 17 00:00:00 2001 From: yetone Date: Mon, 5 Oct 2026 02:11:07 +0800 Subject: [PATCH] =?UTF-8?q?trae=200.1.10:=20a=20tool=20call=20written=20as?= =?UTF-8?q?=20a=20tagged=20block=20with=20a=20file's=20content=20raw,=20or?= =?UTF-8?q?=20with=20its=20arguments=20beside=20the=20name,=20goes=20on=20?= =?UTF-8?q?whole,=20and=20an=20argument=20piece=20that=20names=20no=20call?= =?UTF-8?q?=20joins=20the=20one=20being=20written=20(magpie#799,=20mintoni?= =?UTF-8?q?ght:=20pi's=20write=20went=20out=20as=20{}=20and=20was=20refuse?= =?UTF-8?q?d,=20the=20turn=20ended=20mid-sentence;=20earlier=20=20blocks=20showed=20up=20as=20text)=20=E2=80=94=20a=20model?= =?UTF-8?q?=20writing=20a=20file=20through=20{"name":"write",?= =?UTF-8?q?"arguments":{"content":"=E2=80=A6"}}=20puts=20rea?= =?UTF-8?q?l=20newlines=20and=20tabs=20in=20the=20JSON=20string,=20which?= =?UTF-8?q?=20JSON.parse=20refuses:=20the=20block=20went=20to=20the=20agen?= =?UTF-8?q?t=20as=20text,=20and=20a=20call=20parsed=20with=20nothing=20und?= =?UTF-8?q?er=20arguments=20went=20as=20{}.=20Blocks=20are=20now=20read=20?= =?UTF-8?q?leniently=20(raw=20control=20characters=20inside=20strings=20ta?= =?UTF-8?q?ken=20as=20escaped),=20a=20block=20that=20gives=20its=20argumen?= =?UTF-8?q?ts=20beside=20the=20name=20has=20them=20taken,=20and=20a=20stri?= =?UTF-8?q?ng=20of=20arguments=20is=20normalised.=20Native=20calls:=20a=20?= =?UTF-8?q?piece=20with=20neither=20id=20nor=20name=20(or=20an=20index=20i?= =?UTF-8?q?ts=20call's=20first=20event=20didn't=20give)=20joins=20the=20ca?= =?UTF-8?q?ll=20still=20being=20written=20instead=20of=20being=20dropped,?= =?UTF-8?q?=20and=20arguments=20still=20not=20JSON=20at=20the=20end=20are?= =?UTF-8?q?=20read=20the=20same=20lenient=20way.=20Three=20tests=20fail=20?= =?UTF-8?q?on=200.1.9;=20bun=20test=20(33=20pass)=20and=20bun=20scripts/ch?= =?UTF-8?q?eck.mjs=20trae=20under=20a=20temp=20HOME=20pass.=20Not=20tried?= =?UTF-8?q?=20with=20a=20real=20Trae=20CN=20account=20(none=20here).?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/trae/index.mjs | 69 +++++++++++++++++++++++++++++++--- packages/trae/package.json | 2 +- packages/trae/request.test.mjs | 33 ++++++++++++++++ 3 files changed, 97 insertions(+), 7 deletions(-) diff --git a/packages/trae/index.mjs b/packages/trae/index.mjs index 7c52e6b..f51678d 100644 --- a/packages/trae/index.mjs +++ b/packages/trae/index.mjs @@ -551,8 +551,8 @@ class TextTools { text += this.buf.slice(0, i) const raw = this.buf.slice(i + OPEN.length, j).trim() this.buf = this.buf.slice(j + CLOSE.length) - const v = parseJSON(raw) - if (v.name) calls.push({ name: String(v.name), arguments: typeof v.arguments === "string" ? v.arguments : JSON.stringify(v.arguments ?? v.input ?? v.parameters ?? {}) }) + const v = looseJSON(raw) ?? {} + if (v.name) calls.push({ name: String(v.name), arguments: callArgs(v) }) else text += OPEN + raw + CLOSE } if (end) { @@ -570,14 +570,62 @@ class TextTools { } } +// looseJSON reads a JSON object as a model writes one: a string holding a +// raw newline or tab (a file's content written out, #799) is read as if +// they were escaped. null when it isn't one even so. +function looseJSON(s) { + for (const t of [s, escapeRaw(s)]) { + try { + const v = JSON.parse(t) + if (v && typeof v === "object" && !Array.isArray(v)) return v + } catch {} + } + return null +} + +// escapeRaw escapes the control characters inside JSON strings +function escapeRaw(s) { + let out = "" + let str = false + let esc = false + for (const ch of String(s ?? "")) { + if (str && !esc && ch < " ") { + out += ch === "\n" ? "\\n" : ch === "\r" ? "\\r" : ch === "\t" ? "\\t" : "\\u" + ch.charCodeAt(0).toString(16).padStart(4, "0") + continue + } + if (str && ch === "\\" && !esc) esc = true + else { + if (ch === '"' && !esc) str = !str + esc = false + } + out += ch + } + return out +} + +// callArgs is a tagged block's arguments as JSON: under arguments (an +// object, or a string of one), input or parameters, else the block's other +// keys, as a model that writes them beside the name means them (#799: a +// write that went out as {}) +function callArgs(v) { + let a = v.arguments ?? v.input ?? v.parameters + if (a === undefined) { + const { name, type, id, ...rest } = v + a = rest + } + if (typeof a === "string") return a.trim() ? (looseJSON(a) ? JSON.stringify(looseJSON(a)) : a) : "{}" + return JSON.stringify(a ?? {}) +} + // nativeCall is a tool call as Trae's events carry one function nativeCall(tc) { const f = tc?.function ?? tc?.function_call ?? tc const name = f?.name ?? tc?.tool_name ?? "" const index = Number.isInteger(tc?.index) ? tc.index : null - if (!name && index === null && !tc?.id) return null const a = f?.arguments ?? f?.args ?? tc?.params ?? tc?.input ?? tc?.parameters ?? "" - return { id: tc?.id || tc?.tool_call_id || "", index, name: String(name), arguments: typeof a === "string" ? a : JSON.stringify(a) } + const args = typeof a === "string" ? a : JSON.stringify(a) + if (!name && index === null && !tc?.id && !args) return null + return { id: tc?.id || tc?.tool_call_id || "", index, name: String(name), arguments: args } } // NativeCalls puts together the tool calls Trae streams: a call comes in @@ -594,6 +642,12 @@ class NativeCalls { let o = this.calls.find((x) => (c.id && x.id === c.id) || (!c.id && c.index !== null && x.index === c.index)) if (!o && !c.id && c.index === null) o = this.calls.findLast((x) => x.name === c.name && (x.arguments === c.arguments || (!done(x.arguments) && c.arguments.startsWith(x.arguments)))) + // a piece with nothing to say whose it is (or an index its call's + // first event didn't give) belongs to the call still being written + if (!o && !c.name && !c.id) { + o = this.calls.findLast((x) => !done(x.arguments)) + if (o && c.index !== null && o.index !== null && o.index !== c.index) o = null + } if (!o) { if (!c.name) return this.calls.push({ ...c }) @@ -608,7 +662,10 @@ class NativeCalls { else o.arguments = prev + next // a piece } take() { - return this.calls.filter((c) => c.name).map(({ id, name, arguments: a }) => ({ id: id || callId(), name, arguments: a || "{}" })) + return this.calls.filter((c) => c.name).map(({ id, name, arguments: a }) => { + const v = a && !done(a) ? looseJSON(a) : null + return { id: id || callId(), name, arguments: v ? JSON.stringify(v) : a || "{}" } + }) } } @@ -1154,4 +1211,4 @@ export const TraeCNAuthPlugin = async ({ client }) => { } // for tests -export const _internal = { HOSTS, MODELS, TextTools, traeMessages, chatBody, credits, whenOf, newDevice } +export const _internal = { HOSTS, MODELS, TextTools, NativeCalls, looseJSON, traeMessages, chatBody, credits, whenOf, newDevice } diff --git a/packages/trae/package.json b/packages/trae/package.json index b34ea75..dc54d7d 100644 --- a/packages/trae/package.json +++ b/packages/trae/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-trae-auth", - "version": "0.1.9", + "version": "0.1.10", "description": "OpenCode provider plugin for Trae CN (trae.cn): your Trae CN account's models, free tier included, with Trae's own browser sign-in", "type": "module", "main": "./index.mjs", diff --git a/packages/trae/request.test.mjs b/packages/trae/request.test.mjs index 6a339d3..5206979 100644 --- a/packages/trae/request.test.mjs +++ b/packages/trae/request.test.mjs @@ -166,3 +166,36 @@ test("out of quota (4008) is a 429, the account kept", async () => { 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("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) +})