diff --git a/packages/trae/index.mjs b/packages/trae/index.mjs index a7a44cd..d53edc5 100644 --- a/packages/trae/index.mjs +++ b/packages/trae/index.mjs @@ -1338,6 +1338,31 @@ function toolNamed(name, tools) { return names.find((n) => norm(n) === norm(name)) ?? name } +// unwrapped is a call's arguments with the layer a model put around them +// taken off: asked for {"name":…,"arguments":{…}}, some write the +// arguments as {"arguments":{…}} again, natively or in the block, and the +// agent got {"arguments":{"content":…,"path":…}} for its write (bee2an on +// X). A lone arguments, input, parameters or args key holding an object is +// that layer when the tool has no parameter of that name. +const WRAPS = ["arguments", "input", "parameters", "args"] +function unwrapped(args, name, tools) { + const t = tools.map((x) => x.function ?? x).find((f) => f?.name === name) + if (!t) return args + const props = t.parameters?.properties ?? {} + let v = looseJSON(args) + let changed = false + for (let k = 0; k < 3 && v && typeof v === "object" && !Array.isArray(v); k++) { + const keys = Object.keys(v) + if (keys.length !== 1 || !WRAPS.includes(keys[0]) || keys[0] in props) break + let inner = v[keys[0]] + if (typeof inner === "string") inner = looseJSON(inner) + if (!inner || typeof inner !== "object" || Array.isArray(inner)) break + v = inner + changed = true + } + return changed ? JSON.stringify(v) : args +} + // DeepSeek's tags, with its full-width bar or a plain one const DSML = String.raw`<\s*\/?\s*[||][\s||]*DSML[\s||]*` const DSML_AT = new RegExp(DSML) @@ -1569,7 +1594,10 @@ async function* parts(events, tools = []) { const tt = new TextTools(tools) const nc = new NativeCalls() const th = new Thoughts() - const call = (c) => ({ call: { ...c, name: toolNamed(c.name, tools) } }) + const call = (c) => { + const name = toolNamed(c.name, tools) + return { call: { ...c, name, arguments: unwrapped(c.arguments, name, tools) } } + } let finish = "" for await (const { event, data } of events) { const name = eventName(event) @@ -2301,4 +2329,4 @@ const makePlugin = (site) => async ({ client }) => { export const TraeCNAuthPlugin = makePlugin(SITES["trae-cn"]) export const TraeGlobalAuthPlugin = makePlugin(SITES["trae-global"]) -export const _internal = { HOSTS: SITES["trae-cn"].hosts, MODELS: SITES["trae-cn"].models, TextTools, NativeCalls, looseJSON, glmCall, toolNamed, traeMessages, chatBody, credits, dollarUsageOf, whenOf, newDevice, proofOf, SITES, SESSION, conversationID } +export const _internal = { HOSTS: SITES["trae-cn"].hosts, MODELS: SITES["trae-cn"].models, TextTools, NativeCalls, looseJSON, glmCall, toolNamed, unwrapped, traeMessages, chatBody, credits, dollarUsageOf, whenOf, newDevice, proofOf, SITES, SESSION, conversationID } diff --git a/packages/trae/package.json b/packages/trae/package.json index 5b8b19c..0fcde62 100644 --- a/packages/trae/package.json +++ b/packages/trae/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-trae-auth", - "version": "0.2.7", + "version": "0.2.8", "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/unwrap.test.mjs b/packages/trae/unwrap.test.mjs new file mode 100644 index 0000000..bddd310 --- /dev/null +++ b/packages/trae/unwrap.test.mjs @@ -0,0 +1,75 @@ +// bee2an on X (yetone/magpie, 2026-10-10): asked for +// {"name":…,"arguments":{…}}, the model wrote the arguments wrapped once +// more, and their agent's write got {"arguments":{"content":…,"path":…}}. +// The arguments are the ones in their screenshot, byte for byte. +import "./nonet.mjs" +import { afterEach, expect, test } from "bun:test" +import { TraeCNAuthPlugin, _internal } from "./index.mjs" +import { fakeTrae, signedIn, sse } from "./fake.mjs" + +let f +afterEach(() => f?.close()) + +async function ask(body, auth = signedIn()) { + const hooks = await TraeCNAuthPlugin({ client: {} }) + const opts = await hooks.auth.loader(async () => auth) + return opts.fetch(opts.baseURL + "/chat/completions", { method: "POST", body: JSON.stringify(body) }) +} + +async function chunks(res) { + const out = [] + for (const line of (await res.text()).split("\n")) { + if (!line.startsWith("data: ")) continue + const d = line.slice(6) + out.push(d === "[DONE]" ? d : JSON.parse(d)) + } + return out +} + +const callsOf = (c) => c.filter((x) => x !== "[DONE]" && x.choices?.length).flatMap((x) => x.choices[0].delta.tool_calls ?? []).map((t) => [t.function.name, JSON.parse(t.function.arguments)]) + +// pi-go's write +const TOOLS = [ + { type: "function", function: { name: "write", parameters: { type: "object", properties: { path: { type: "string" }, content: { type: "string" } }, required: ["path", "content"] } } }, + { type: "function", function: { name: "task", parameters: { type: "object", properties: { input: { type: "object" } } } } }, +] +const BEE = '{"arguments":{"content":"P\\n\\n","path":"/Users/bee/j/pi-go/playground/cases/run-20261010-1805/trail2.txt"}}' +const BARE = { content: "P\n\n", path: "/Users/bee/j/pi-go/playground/cases/run-20261010-1805/trail2.txt" } + +test("bee2an's write, wrapped in a block's arguments, reaches the agent bare (streamed)", async () => { + f = fakeTrae() + const block = '{"name":"write","arguments":' + BEE + "}" + f.route("POST /api/agent/v3/llm_utils_chat", () => sse([ + ["output", { response: block.slice(0, 40) }], + ["output", { response: block.slice(40) }], + ["done", { finish_reason: "stop" }], + ])) + const c = await chunks(await ask({ model: "deepseek-v4.1-flash", stream: true, tools: TOOLS, messages: [{ role: "user", content: "hi" }] })) + expect(callsOf(c)).toEqual([["write", BARE]]) +}) + +test("bee2an's write as Trae's native call, wrapped, reaches the agent bare (not streamed)", async () => { + f = fakeTrae() + f.route("POST /api/agent/v3/llm_utils_chat", () => sse([ + ["output", { tool_calls: [{ id: "c1", function: { name: "write", arguments: BEE.slice(0, 25) } }] }], + ["output", { tool_calls: [{ id: "c1", function: { name: "write", arguments: BEE } }] }], + ["done", { finish_reason: "tool_calls" }], + ])) + const res = await ask({ model: "deepseek-v4.1-flash", tools: TOOLS, messages: [{ role: "user", content: "hi" }] }) + const m = (await res.json()).choices[0].message + expect(m.tool_calls.map((t) => [t.function.name, JSON.parse(t.function.arguments)])).toEqual([["write", BARE]]) +}) + +test("a layer is only taken off where the tool has no parameter of its name", () => { + const u = (a, n) => JSON.parse(_internal.unwrapped(a, n, TOOLS)) + expect(u(BEE, "write")).toEqual(BARE) + // twice wrapped, and wrapped as a string + expect(u('{"arguments":{"input":{"path":"a","content":"b"}}}', "write")).toEqual({ path: "a", content: "b" }) + expect(u('{"arguments":"{\\"path\\":\\"a\\",\\"content\\":\\"b\\"}"}', "write")).toEqual({ path: "a", content: "b" }) + // the tool's own input parameter stays + expect(u('{"input":{"q":1}}', "task")).toEqual({ input: { q: 1 } }) + // bare arguments, a second key, a tool not in the request: as they are + expect(u(JSON.stringify(BARE), "write")).toEqual(BARE) + expect(u('{"arguments":{"path":"a"},"content":"b"}', "write")).toEqual({ arguments: { path: "a" }, content: "b" }) + expect(u(BEE, "other")).toEqual(JSON.parse(BEE)) +})