trae 0.1.8: a tool call Trae streams over several events goes on whole, not as its first event (magpie#799, mintonight: Trae CN models fail many tool calls — bash got echo "T, read got C:/Users/j, edit got {}) — Trae sends a native tool call in several output events under one id or index, each carrying the arguments so far or only the new piece; the plugin sent the first event of each id on and dropped the rest as repeats, so a call whose arguments came in more than one piece reached the agent cut short. NativeCalls now puts each call together across events (arguments that start with what came before replace it, a piece is appended, a repeat is ignored, a piece without a name joins its call by index) and sends the calls once the answer is whole. A test streaming one call cumulatively and one in pieces fails on 0.1.7; bun test (32 pass) and bun scripts/check.mjs trae pass.
This commit is contained in:
+50
-12
@@ -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
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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", {}]]))
|
||||
|
||||
Reference in New Issue
Block a user