trae 0.2.2: a tool call Trae sends again whole, its punctuation not the partial one's, replaces the partial arguments instead of being appended to them (#24, Zhangwier)

Trae's llm_utils_chat streams a call's arguments as snapshots, and may send
the whole call again written apart from the partial one (U+2212 for -,
U+2032 for '), which neither extends it nor follows a whole one, so
NativeCalls joined the two and the call's arguments came out twice. A
whole JSON object under the same first key as the partial arguments, which
the two joined don't make whole, now replaces them. A piece that is a
JSON value of its own (12, "ls", {}) still joins the call: it doesn't
open with the call's first key. The new test of the resent snapshot fails
without the change; the one of pieces that are values of their own
passes either way. bun test (trae: 68) and bun scripts/check.mjs pass.
This commit is contained in:
yetone
2026-10-05 22:01:28 +08:00
parent 3acb0fc377
commit 82330dfa3f
3 changed files with 42 additions and 1 deletions
+14
View File
@@ -1083,6 +1083,7 @@ class NativeCalls {
const next = c.arguments
if (!next || prev.startsWith(next)) return // nothing new
if (next.startsWith(prev) || done(prev)) o.arguments = next // the arguments so far
else if (resent(prev, next)) o.arguments = next // the whole call again, written apart (plugins#24)
else o.arguments = prev + next // a piece
}
take(tools = []) {
@@ -1100,6 +1101,19 @@ class NativeCalls {
}
}
// resent says next is the call's whole arguments sent again after a
// partial snapshot, not a piece of them: Trae resends a snapshot whose
// punctuation isn't the first one's (− for -, ′ for '), so it doesn't
// start with it. A whole object under the same first key as prev's, which
// prev and it joined don't make whole; a piece that is a value of its own
// (12, "ls", {}) doesn't open with prev's first key.
function resent(prev, next) {
const key = (s) => s.match(/^\s*\{\s*"((?:[^"\\]|\\.)*)"/)?.[1]
if (!next.trimStart().startsWith("{") || !done(next) || done(prev + next)) return false
const k = key(prev)
return k !== undefined && k === key(next)
}
// done says arguments are a whole JSON value already
function done(a) {
if (!a) return false
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-trae-auth",
"version": "0.2.1",
"version": "0.2.2",
"description": "OpenCode provider plugins for Trae: Trae CN (trae.cn) and Trae Global (trae.ai), free tier included, with Trae's own browser sign-in",
"type": "module",
"main": "./index.mjs",
+27
View File
@@ -194,6 +194,33 @@ test("a native call's pieces that say neither id nor name join the call being wr
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 })