From b14b4d80c562ea198a5e55641fa7ef4820c98ca9 Mon Sep 17 00:00:00 2001 From: yetone Date: Mon, 5 Oct 2026 00:02:33 +0800 Subject: [PATCH] =?UTF-8?q?trae=200.1.8:=20a=20tool=20call=20Trae=20stream?= =?UTF-8?q?s=20over=20several=20events=20goes=20on=20whole,=20not=20as=20i?= =?UTF-8?q?ts=20first=20event=20(magpie#799,=20mintonight:=20Trae=20CN=20m?= =?UTF-8?q?odels=20fail=20many=20tool=20calls=20=E2=80=94=20bash=20got=20`?= =?UTF-8?q?echo=20"T`,=20read=20got=20`C:/Users/j`,=20edit=20got=20`{}`)?= =?UTF-8?q?=20=E2=80=94=20Trae=20sends=20a=20native=20tool=20call=20in=20s?= =?UTF-8?q?everal=20output=20events=20under=20one=20id=20or=20index,=20eac?= =?UTF-8?q?h=20carrying=20the=20arguments=20so=20far=20or=20only=20the=20n?= =?UTF-8?q?ew=20piece;=20the=20plugin=20sent=20the=20first=20event=20of=20?= =?UTF-8?q?each=20id=20on=20and=20dropped=20the=20rest=20as=20repeats,=20s?= =?UTF-8?q?o=20a=20call=20whose=20arguments=20came=20in=20more=20than=20on?= =?UTF-8?q?e=20piece=20reached=20the=20agent=20cut=20short.=20NativeCalls?= =?UTF-8?q?=20now=20puts=20each=20call=20together=20across=20events=20(arg?= =?UTF-8?q?uments=20that=20start=20with=20what=20came=20before=20replace?= =?UTF-8?q?=20it,=20a=20piece=20is=20appended,=20a=20repeat=20is=20ignored?= =?UTF-8?q?,=20a=20piece=20without=20a=20name=20joins=20its=20call=20by=20?= =?UTF-8?q?index)=20and=20sends=20the=20calls=20once=20the=20answer=20is?= =?UTF-8?q?=20whole.=20A=20test=20streaming=20one=20call=20cumulatively=20?= =?UTF-8?q?and=20one=20in=20pieces=20fails=20on=200.1.7;=20bun=20test=20(3?= =?UTF-8?q?2=20pass)=20and=20bun=20scripts/check.mjs=20trae=20pass.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/trae/index.mjs | 62 +++++++++++++++++++++++++++------- packages/trae/package.json | 2 +- packages/trae/request.test.mjs | 23 +++++++++++++ 3 files changed, 74 insertions(+), 13 deletions(-) diff --git a/packages/trae/index.mjs b/packages/trae/index.mjs index c5fd16c..20e6ef5 100644 --- a/packages/trae/index.mjs +++ b/packages/trae/index.mjs @@ -610,16 +610,60 @@ class TextTools { function nativeCall(tc) { const f = tc?.function ?? tc?.function_call ?? tc const name = f?.name ?? tc?.tool_name ?? "" - if (!name) return null - const a = f?.arguments ?? tc?.params ?? tc?.input ?? tc?.parameters ?? {} - return { id: tc?.id || tc?.tool_call_id || "", name: String(name), arguments: typeof a === "string" ? a : JSON.stringify(a) } + 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) } +} + +// NativeCalls puts together the tool calls Trae streams: a call comes in +// several events under one id (or index), each with the arguments so far +// or only the new piece, so a call is sent on once the answer is whole, +// not from its first event (#799: bash's command cut to `echo "T`) +class NativeCalls { + constructor() { + this.calls = [] + } + push(tc) { + const c = nativeCall(tc) + if (!c) return + 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)))) + if (!o) { + if (!c.name) return + this.calls.push({ ...c }) + return + } + if (!o.name) o.name = c.name + if (c.index !== null && o.index === null) o.index = c.index + const prev = o.arguments + 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 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 || "{}" })) + } +} + +// done says arguments are a whole JSON value already +function done(a) { + if (!a) return false + try { + JSON.parse(a) + return true + } catch { + return false + } } // parts turns Trae's events into what the answer is made of: text, // reasoning, tool calls, the token counts, the finish, or an error. async function* parts(events) { const tt = new TextTools() - const seen = new Set() + const nc = new NativeCalls() for await (const { event, data } of events) { const name = eventName(event) const d = data && typeof data === "object" ? data : {} @@ -650,20 +694,14 @@ async function* parts(events) { if (r.text) yield { text: r.text } for (const c of r.calls) yield { call: { ...c, id: callId() } } } - for (const tc of Array.isArray(d.tool_calls) ? d.tool_calls : []) { - const c = nativeCall(tc) - if (!c) continue - const key = c.id || c.name + c.arguments - if (seen.has(key)) continue - seen.add(key) - yield { call: { ...c, id: c.id || callId() } } - } + for (const tc of Array.isArray(d.tool_calls) ? d.tool_calls : []) nc.push(tc) const u = tokensOf(d.usage) if (u) yield { usage: u } } const r = tt.push("", true) if (r.text) yield { text: r.text } for (const c of r.calls) yield { call: { ...c, id: callId() } } + for (const c of nc.take()) yield { call: c } } // first reads events up to the first that is the answer's, so an answer diff --git a/packages/trae/package.json b/packages/trae/package.json index 15c26ad..85fe507 100644 --- a/packages/trae/package.json +++ b/packages/trae/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-trae-auth", - "version": "0.1.7", + "version": "0.1.8", "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 cb397d2..6a339d3 100644 --- a/packages/trae/request.test.mjs +++ b/packages/trae/request.test.mjs @@ -90,6 +90,29 @@ test("a stream: text, reasoning, a tool block split across events, a native call expect(c.at(-1)).toBe("[DONE]") }) +test("a native call streamed over several events goes on whole (#799)", async () => { + f = fakeTrae() + f.route("POST /api/agent/v3/llm_utils_chat", () => sse([ + // the arguments so far, each event + ["output", { tool_calls: [{ id: "b1", index: 0, function: { name: "bash", arguments: "" } }] }], + ["output", { tool_calls: [{ id: "b1", index: 0, function: { name: "bash", arguments: '{"command":"echo \\"T' } }] }], + ["output", { tool_calls: [{ id: "b1", index: 0, function: { name: "bash", arguments: '{"command":"echo \\"Trae\\""}' } }] }], + // only the new piece, the name only at first + ["output", { tool_calls: [{ id: "r1", index: 1, function: { name: "read", arguments: '{"filePath":"C:/Users/j' } }] }], + ["output", { tool_calls: [{ index: 1, function: { arguments: 'oe/a.txt"}' } }] }], + // a whole call sent again + ["output", { tool_calls: [{ id: "r1", index: 1, function: { name: "read", arguments: '{"filePath":"C:/Users/joe/a.txt"}' } }] }], + ["done", {}], + ])) + const res = await ask({ model: "glm-5", stream: true, tools: TOOLS, messages: [{ role: "user", content: "hi" }] }) + const c = await chunks(res) + const calls = c.filter((x) => x !== "[DONE]" && x.choices?.length).flatMap((x) => x.choices[0].delta.tool_calls ?? []) + expect(calls.map((t) => [t.id, t.function.name, JSON.parse(t.function.arguments)])).toEqual([ + ["b1", "bash", { command: 'echo "Trae"' }], + ["r1", "read", { filePath: "C:/Users/joe/a.txt" }], + ]) +}) + test("no stream asked: one chat completion", async () => { f = fakeTrae() f.route("POST /api/agent/v3/llm_utils_chat", () => sse([["output", { content: "Hel" }], ["output", { content: "lo" }], ["done", {}]]))