diff --git a/packages/trae/README.md b/packages/trae/README.md
index b0ba674..5f04870 100644
--- a/packages/trae/README.md
+++ b/packages/trae/README.md
@@ -136,6 +136,14 @@ agent's `POST https://trae-api-cn.mchost.guru/api/agent/v3/llm_utils_chat`:
- A call with no name is dropped, and the `tool` result answering it
with it: Trae requires the name, and an empty `tool_calls` list with a
result naming no call is not a turn it takes.
+ - A result with no text goes as `(no output)` (`(the result's image
+ follows)` when it had only an image): Trae refuses the whole request
+ with 4027 when a result's text is empty, reading the calls turn as
+ unanswered (measured on Trae CN in #251). A result answering no call
+ the history sends goes back as text in a `user` turn.
+ - A request that names no tools to Trae (none sent, or `tool_choice:
+ "none"`, such as a summary) sends its past calls and results as text,
+ as before: every live run of the native turns carried the tools.
- GLM also writes its own template, `namek
v` (a string kept as written, other
types read as JSON), and sometimes a whole call into a native call's
diff --git a/packages/trae/images.test.mjs b/packages/trae/images.test.mjs
index c22a677..3b9aec4 100644
--- a/packages/trae/images.test.mjs
+++ b/packages/trae/images.test.mjs
@@ -10,6 +10,9 @@ import { afterEach, expect, test } from "bun:test"
import { TraeCNAuthPlugin, _internal } from "./index.mjs"
import { fakeTrae, json, signedIn, sse } from "./fake.mjs"
+// the tools named natively in the body: past calls then go back as Trae's own turns
+const TOOLS_ON = [{ type: "function", function: { name: "read", parameters: { type: "object" } } }]
+
let f
afterEach(() => f?.close())
@@ -70,7 +73,7 @@ test("a model Trae marks text-only is told of the image, not sent it", async ()
})
test("an image alone, a tool's image and two user turns in a row keep every image in order", () => {
- const m = _internal.traeMessages({ messages: [
+ const m = _internal.traeMessages({ tools: TOOLS_ON, messages: [
{ role: "user", content: [{ type: "image_url", image_url: { url: PNG } }] },
{ role: "user", content: [{ type: "text", text: "and this" }, { type: "image_url", image_url: "https://example.invalid/b.png" }] },
{ role: "assistant", content: null, tool_calls: [{ id: "c1", type: "function", function: { name: "shot", arguments: "{}" } }] },
@@ -85,7 +88,7 @@ test("an image alone, a tool's image and two user turns in a row keep every imag
expect(m[3].content).toEqual([{ type: "image_url", image_url: { url: PNG } }])
// the images of parallel results come out after every result, in order
- const p = _internal.traeMessages({ messages: [
+ const p = _internal.traeMessages({ tools: TOOLS_ON, messages: [
{ role: "user", content: [{ type: "text", text: "two shots" }] },
{ role: "assistant", content: null, tool_calls: [
{ id: "c1", type: "function", function: { name: "shot", arguments: "{}" } },
@@ -95,7 +98,7 @@ test("an image alone, a tool's image and two user turns in a row keep every imag
{ role: "tool", tool_call_id: "c2", content: [{ type: "text", text: "second" }, { type: "image_url", image_url: { url: "https://example.invalid/b.png" } }] },
] }, true)
expect(p.map((x) => x.role)).toEqual(["user", "assistant", "tool", "tool", "user"])
- expect(p[2].content).toEqual([{ type: "text", text: "" }])
+ expect(p[2].content).toEqual([{ type: "text", text: "(the result's image follows)" }])
expect(p[3].content).toEqual([{ type: "text", text: "second" }])
expect(p[4].content).toEqual([{ type: "image_url", image_url: { url: PNG } }, { type: "image_url", image_url: { url: "https://example.invalid/b.png" } }])
})
diff --git a/packages/trae/index.mjs b/packages/trae/index.mjs
index 29e1e38..b08be21 100644
--- a/packages/trae/index.mjs
+++ b/packages/trae/index.mjs
@@ -963,6 +963,11 @@ const partsOf = (content, sees) => {
// the model takes images: Trae's list says so of each model (multimodal),
// and a model it says doesn't is sent the placeholder, not the image.
function traeMessages(req, sees = false) {
+ // native is whether the body names the tools to Trae (chatBody's own
+ // test). Past calls go back as Trae's own turns only then: every live
+ // run of that shape carried the tools, and a request without them (a
+ // summary, tool_choice "none") keeps the text form it always had
+ const native = Array.isArray(req.tools) && req.tools.length > 0 && req.tool_choice !== "none"
const names = new Map()
const out = []
// dropped is every call id a turn carried but Trae cannot be sent: an
@@ -978,6 +983,18 @@ function traeMessages(req, sees = false) {
for (const m of req.messages ?? []) {
const role = m.role === "developer" ? "system" : m.role
let { text, images } = partsOf(m.content, sees)
+ if (!native && role === "assistant" && Array.isArray(m.tool_calls) && m.tool_calls.length) {
+ const calls = m.tool_calls.map((c) => {
+ names.set(String(c.id ?? ""), c.function?.name)
+ let args = c.function?.arguments ?? "{}"
+ try {
+ args = JSON.parse(args)
+ } catch {}
+ return OPEN + JSON.stringify({ name: c.function?.name, arguments: args }) + CLOSE
+ })
+ out.push({ role: "assistant", text: [text, ...calls].filter(Boolean).join("\n") })
+ continue
+ }
if (role === "assistant" && Array.isArray(m.tool_calls) && m.tool_calls.length) {
// a past call goes back as the turn Trae made it: an assistant
// tool_calls message, its result in the tool message after it. Trae
@@ -1015,13 +1032,22 @@ function traeMessages(req, sees = false) {
} else if (role === "tool") {
const id = String(m.tool_call_id ?? "")
if (dropped.has(id)) continue
+ // a result answering no call this history sends (the text form, or
+ // a call cut away before it) is no turn Trae takes: it goes as text
+ if (!native || !names.has(id)) {
+ flushImages()
+ out.push({ role: "user", text: `Result of ${names.get(id) || m.name || "the tool"} (call ${m.tool_call_id ?? "?"}):\n${text}`, images })
+ continue
+ }
// Trae's own result turn: the call it answers, its text as parts
// (an empty one for a result with no text). Its images wait: a
// tool turn's image_url reaches some models (Kimi) and not others
// (Doubao-Seed), so they go in a user message after the whole run
// of results, which every multimodal model reads
if (images.length) pending.push(...images)
- out.push({ role: "tool", tool_call_id: id, text })
+ // Trae refuses a result with no text (4027: the calls turn reads as
+ // unanswered), so one that printed nothing says so (plugins#251)
+ out.push({ role: "tool", tool_call_id: id, text: text || (images.length ? "(the result's image follows)" : "(no output)") })
} else {
flushImages()
out.push({ role: role === "system" ? "system" : "user", text, images })
@@ -1065,7 +1091,7 @@ function traeMessages(req, sees = false) {
return merged.map((m) => {
const content = [...(m.text ? [{ type: "text", text: m.text }] : []), ...(m.images ?? [])]
if (m.calls) return { role: "assistant", content: content.length ? content : null, tool_calls: m.calls }
- if (m.role === "tool") return { role: "tool", tool_call_id: m.tool_call_id, content: content.length ? content : [{ type: "text", text: "" }] } // results of one turn stay consecutive: nothing may go between them (11148)
+ if (m.role === "tool") return { role: "tool", tool_call_id: m.tool_call_id, content } // results of one turn stay consecutive: nothing may go between them (11148)
return { role: m.role, content }
})
}
diff --git a/packages/trae/ownclient.test.mjs b/packages/trae/ownclient.test.mjs
index a25c08b..02b45f3 100644
--- a/packages/trae/ownclient.test.mjs
+++ b/packages/trae/ownclient.test.mjs
@@ -159,6 +159,9 @@ test("where each OS keeps the client's storage.json", () => {
"/h/AppData/Roaming/Trae CN/User/globalStorage/storage.json",
"/h/AppData/Roaming/trae-cn/User/globalStorage/storage.json",
])
+ // the same folder spelled with another case, separator or a trailing one
+ // is still that folder (the fix: string equality read it twice)
+ expect(same(p("win32", "/h", { APPDATA: "\\H\\APPDATA\\roaming\\" })).length).toBe(2)
// and a different APPDATA is a root of its own, beside the home's
expect(same(p("win32", "/h", { APPDATA: "/other/Roam" }))).toEqual([
"/other/Roam/Trae CN/User/globalStorage/storage.json",
diff --git a/packages/trae/package.json b/packages/trae/package.json
index 57fc9fd..96c852d 100644
--- a/packages/trae/package.json
+++ b/packages/trae/package.json
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-trae-auth",
- "version": "0.2.17",
+ "version": "0.2.18",
"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 e7b96a7..cf40920 100644
--- a/packages/trae/request.test.mjs
+++ b/packages/trae/request.test.mjs
@@ -5,6 +5,9 @@ import { afterEach, expect, test } from "bun:test"
import { TraeCNAuthPlugin, _internal } from "./index.mjs"
import { fakeTrae, json, signedIn, sse } from "./fake.mjs"
+// the tools named natively in the body: past calls then go back as Trae's own turns
+const TOOLS_ON = [{ type: "function", function: { name: "read", parameters: { type: "object" } } }]
+
let f
afterEach(() => f?.close())
@@ -51,7 +54,7 @@ test("the request is the IDE's: its headers, the device, text parts, the tools n
})
test("tool history goes back as the turns Trae made: a call and its result", () => {
- const m = _internal.traeMessages({ messages: [
+ const m = _internal.traeMessages({ tools: TOOLS_ON, messages: [
{ role: "user", content: "read a" },
{ role: "assistant", content: null, tool_calls: [{ id: "c1", type: "function", function: { name: "read", arguments: "{\"path\":\"a\"}" } }] },
{ role: "tool", tool_call_id: "c1", content: "AAA" },
@@ -65,7 +68,7 @@ test("tool history goes back as the turns Trae made: a call and its result", ()
test("an assistant turn folds into the calls turn after it, and parallel calls join one list", () => {
// what agents send: a "let me read" turn, then the call
- const a = _internal.traeMessages({ messages: [
+ const a = _internal.traeMessages({ tools: TOOLS_ON, messages: [
{ role: "user", content: "read a" },
{ role: "assistant", content: "let me read" },
{ role: "assistant", content: null, tool_calls: [{ id: "c1", type: "function", function: { name: "read", arguments: "{\"path\":\"a\"}" } }] },
@@ -76,7 +79,7 @@ test("an assistant turn folds into the calls turn after it, and parallel calls j
expect(a[1].tool_calls.map((c) => c.id)).toEqual(["c1"])
// parallel calls split over two assistant turns (magpie#1275)
- const b = _internal.traeMessages({ messages: [
+ const b = _internal.traeMessages({ tools: TOOLS_ON, messages: [
{ role: "user", content: "read a and b" },
{ role: "assistant", content: null, tool_calls: [{ id: "c1", type: "function", function: { name: "read", arguments: "{\"path\":\"a\"}" } }] },
{ role: "assistant", content: null, tool_calls: [{ id: "c2", type: "function", function: { name: "read", arguments: "{\"path\":\"b\"}" } }] },
@@ -89,7 +92,7 @@ test("an assistant turn folds into the calls turn after it, and parallel calls j
})
test("a call with no name is dropped, and the result it would answer", () => {
- const a = _internal.traeMessages({ messages: [
+ const a = _internal.traeMessages({ tools: TOOLS_ON, messages: [
{ role: "user", content: "do it" },
{ role: "assistant", content: null, tool_calls: [{ id: "c1", type: "function", function: { name: " ", arguments: "{}" } }] },
{ role: "tool", tool_call_id: "c1", content: "RESULT" },
@@ -102,7 +105,7 @@ test("a call with no name is dropped, and the result it would answer", () => {
expect(a.some((x) => x.role === "tool")).toBe(false)
// a named call beside an unnamed one keeps its own
- const b = _internal.traeMessages({ messages: [
+ const b = _internal.traeMessages({ tools: TOOLS_ON, messages: [
{ role: "user", content: "do both" },
{ role: "assistant", content: null, tool_calls: [
{ id: "c1", type: "function", function: { name: "", arguments: "{}" } },
@@ -117,7 +120,7 @@ test("a call with no name is dropped, and the result it would answer", () => {
})
test("an assistant turn that loses every call keeps its text", () => {
- const m = _internal.traeMessages({ messages: [
+ const m = _internal.traeMessages({ tools: TOOLS_ON, messages: [
{ role: "user", content: "hi" },
{ role: "assistant", content: "thinking out loud", tool_calls: [{ id: "c1", type: "function", function: { name: "", arguments: "{}" } }] },
{ role: "user", content: "ok" },
@@ -127,15 +130,47 @@ test("an assistant turn that loses every call keeps its text", () => {
expect(m[1].tool_calls).toBeUndefined()
})
-test("a result for a call the history doesn't carry, and two in a row, keep their own turns", () => {
- const m = _internal.traeMessages({ messages: [
+test("a result for a call the history doesn't carry goes as text, after the results that answer calls", () => {
+ const m = _internal.traeMessages({ tools: TOOLS_ON, messages: [
{ role: "assistant", content: null, tool_calls: [{ id: "c1", type: "function", function: { name: "read", arguments: "{}" } }] },
{ role: "tool", tool_call_id: "c1", content: "AAA" },
{ role: "tool", tool_call_id: "c2", content: "BBB" },
] })
- expect(m.map((x) => x.role)).toEqual(["assistant", "tool", "tool"])
+ // a tool turn naming no call sent is not a turn Trae takes
+ expect(m.map((x) => x.role)).toEqual(["assistant", "tool", "user"])
expect(m[1].content[0].text).toBe("AAA")
- expect(m[2].content[0].text).toBe("BBB")
+ expect(m[2].content[0].text).toBe("Result of the tool (call c2):\nBBB")
+})
+
+test("a result with no output says so: Trae refuses an empty one (4027)", () => {
+ const m = _internal.traeMessages({ tools: TOOLS_ON, messages: [
+ { role: "user", content: "touch a and b" },
+ { role: "assistant", content: null, tool_calls: [
+ { id: "c1", type: "function", function: { name: "read", arguments: "{}" } },
+ { id: "c2", type: "function", function: { name: "read", arguments: "{}" } },
+ ] },
+ { role: "tool", tool_call_id: "c1", content: "" },
+ { role: "tool", tool_call_id: "c2", content: [] },
+ ] })
+ expect(m.map((x) => x.role)).toEqual(["user", "assistant", "tool", "tool"])
+ expect(m[2].content).toEqual([{ type: "text", text: "(no output)" }])
+ expect(m[3].content).toEqual([{ type: "text", text: "(no output)" }])
+})
+
+test("without tools in the body, past calls and results go back as text, as they always did", () => {
+ const msgs = [
+ { role: "user", content: "read a" },
+ { role: "assistant", content: "let me read", tool_calls: [{ id: "c1", type: "function", function: { name: "read", arguments: "{\"path\":\"a\"}" } }] },
+ { role: "tool", tool_call_id: "c1", content: "AAA" },
+ { role: "user", content: "sum it up" },
+ ]
+ for (const req of [{ messages: msgs }, { tools: TOOLS_ON, tool_choice: "none", messages: msgs }]) {
+ const m = _internal.traeMessages(req)
+ expect(m.map((x) => x.role)).toEqual(["user", "assistant", "user"])
+ expect(m.some((x) => x.tool_calls || x.role === "tool")).toBe(false)
+ expect(m[1].content[0].text).toBe('let me read\n{"name":"read","arguments":{"path":"a"}}')
+ expect(m[2].content[0].text).toBe("Result of read (call c1):\nAAA\n\nsum it up")
+ }
})
test("a stream: text, reasoning, a tool block split across events, a native call, usage", async () => {