trae 0.1.10: a tool call written as a tagged block with a file's content raw, or with its arguments beside the name, goes on whole, and an argument piece that names no call joins the one being written (magpie#799, mintonight: pi's write went out as {} and was refused, the turn ended mid-sentence; earlier <tool_call> blocks showed up as text) — a model writing a file through <tool_call>{"name":"write","arguments":{"content":"…"}}</tool_call> puts real newlines and tabs in the JSON string, which JSON.parse refuses: the block went to the agent as text, and a call parsed with nothing under arguments went as {}. Blocks are now read leniently (raw control characters inside strings taken as escaped), a block that gives its arguments beside the name has them taken, and a string of arguments is normalised. Native calls: a piece with neither id nor name (or an index its call's first event didn't give) joins the call still being written instead of being dropped, and arguments still not JSON at the end are read the same lenient way. Three tests fail on 0.1.9; bun test (33 pass) and bun scripts/check.mjs trae under a temp HOME pass. Not tried with a real Trae CN account (none here).

This commit is contained in:
yetone
2026-10-05 02:11:07 +08:00
parent 64c9e6c34c
commit 32aaeda5a6
3 changed files with 97 additions and 7 deletions
+63 -6
View File
@@ -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 }
+1 -1
View File
@@ -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",
+33
View File
@@ -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 = '<tool_call>{"name":"write","arguments":{"path":"a.py","content":"import sys\n\tprint(\\"hi\\")\n"}}</tool_call>'
f.route("POST /api/agent/v3/llm_utils_chat", () => sse([
["output", { response: "Writing it. " + block }],
["output", { response: '<tool_call>{"name":"read","path":"a.py"}</tool_call>' }],
["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)
})