From 82330dfa3f0e953aa4dce75bdb51c295411bebc4 Mon Sep 17 00:00:00 2001 From: yetone Date: Mon, 5 Oct 2026 22:01:28 +0800 Subject: [PATCH] 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. --- packages/trae/index.mjs | 14 ++++++++++++++ packages/trae/package.json | 2 +- packages/trae/request.test.mjs | 27 +++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/packages/trae/index.mjs b/packages/trae/index.mjs index d0f5db8..f464c0d 100644 --- a/packages/trae/index.mjs +++ b/packages/trae/index.mjs @@ -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 diff --git a/packages/trae/package.json b/packages/trae/package.json index 92875dd..0a5ddd2 100644 --- a/packages/trae/package.json +++ b/packages/trae/package.json @@ -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", diff --git a/packages/trae/request.test.mjs b/packages/trae/request.test.mjs index 2ffd2b4..4ba1214 100644 --- a/packages/trae/request.test.mjs +++ b/packages/trae/request.test.mjs @@ -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 })