trae 0.2.18: native tool turns (#257), a result with no output isn't empty (#251), no tools keeps the text form

Releases #257 (WuJian1421), #260 and #273 (moxuanchen), with three fixes on
top of #257's native tool turns:

- A tool result with no text went out as an empty text part. wangjianjq
  measured on live Trae CN (#251) that Trae then refuses the whole request
  with 4027 ("insufficient tool messages following tool_calls message"),
  for either of two parallel results; " " or "0" passes. An empty result
  now goes as "(no output)", or "(the result's image follows)" when its
  image rides the user turn after the run. Credit wangjianjq (#251).
- A request whose body names no tools (none sent, or tool_choice "none",
  such as a summary) still sends past calls and results as text, as 0.2.17
  did: every live run of the native turns carried the tools in the body.
- A tool result answering no call the history sends goes back as a user
  text turn ("Result of ... (call id)"), not as a tool turn naming no call.
- A Windows APPDATA spelled with other case, separators or a trailing
  separator is the same root as the home's AppData\Roaming (test for #273's
  fix; it fails on 0.2.17's clientStoragePaths).

Run at this commit on origin/main c7a7379: bun test in packages/trae 175
pass 0 fail; bun scripts/check.mjs trae ok. Broken by hand: with the
merged #257 index.mjs, the three new request tests and the image placeholder
test fail (4 of 28 in request+images); with 0.2.17's index.mjs the new
APPDATA case fails. Not tried against a live Trae account (none here); the
live evidence is the PR authors'.
This commit is contained in:
yetone
2026-10-20 22:06:55 +08:00
parent c7a7379733
commit 5ec8377093
6 changed files with 91 additions and 16 deletions
+8
View File
@@ -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 - 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 with it: Trae requires the name, and an empty `tool_calls` list with a
result naming no call is not a turn it takes. 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, `<tool_call>name<arg_key>k</arg_key> - GLM also writes its own template, `<tool_call>name<arg_key>k</arg_key>
<arg_value>v</arg_value></tool_call>` (a string kept as written, other <arg_value>v</arg_value></tool_call>` (a string kept as written, other
types read as JSON), and sometimes a whole call into a native call's types read as JSON), and sometimes a whole call into a native call's
+6 -3
View File
@@ -10,6 +10,9 @@ import { afterEach, expect, test } from "bun:test"
import { TraeCNAuthPlugin, _internal } from "./index.mjs" import { TraeCNAuthPlugin, _internal } from "./index.mjs"
import { fakeTrae, json, signedIn, sse } from "./fake.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 let f
afterEach(() => f?.close()) 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", () => { 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: "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: "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: "{}" } }] }, { 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 } }]) expect(m[3].content).toEqual([{ type: "image_url", image_url: { url: PNG } }])
// the images of parallel results come out after every result, in order // 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: "user", content: [{ type: "text", text: "two shots" }] },
{ role: "assistant", content: null, tool_calls: [ { role: "assistant", content: null, tool_calls: [
{ id: "c1", type: "function", function: { name: "shot", arguments: "{}" } }, { 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" } }] }, { role: "tool", tool_call_id: "c2", content: [{ type: "text", text: "second" }, { type: "image_url", image_url: { url: "https://example.invalid/b.png" } }] },
] }, true) ] }, true)
expect(p.map((x) => x.role)).toEqual(["user", "assistant", "tool", "tool", "user"]) 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[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" } }]) expect(p[4].content).toEqual([{ type: "image_url", image_url: { url: PNG } }, { type: "image_url", image_url: { url: "https://example.invalid/b.png" } }])
}) })
+28 -2
View File
@@ -963,6 +963,11 @@ const partsOf = (content, sees) => {
// the model takes images: Trae's list says so of each model (multimodal), // 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. // and a model it says doesn't is sent the placeholder, not the image.
function traeMessages(req, sees = false) { 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 names = new Map()
const out = [] const out = []
// dropped is every call id a turn carried but Trae cannot be sent: an // 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 ?? []) { for (const m of req.messages ?? []) {
const role = m.role === "developer" ? "system" : m.role const role = m.role === "developer" ? "system" : m.role
let { text, images } = partsOf(m.content, sees) 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) { 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 // 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 // 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") { } else if (role === "tool") {
const id = String(m.tool_call_id ?? "") const id = String(m.tool_call_id ?? "")
if (dropped.has(id)) continue 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 // 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 // (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 // 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 // (Doubao-Seed), so they go in a user message after the whole run
// of results, which every multimodal model reads // of results, which every multimodal model reads
if (images.length) pending.push(...images) 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 { } else {
flushImages() flushImages()
out.push({ role: role === "system" ? "system" : "user", text, images }) out.push({ role: role === "system" ? "system" : "user", text, images })
@@ -1065,7 +1091,7 @@ function traeMessages(req, sees = false) {
return merged.map((m) => { return merged.map((m) => {
const content = [...(m.text ? [{ type: "text", text: m.text }] : []), ...(m.images ?? [])] 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.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 } return { role: m.role, content }
}) })
} }
+3
View File
@@ -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",
"/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 // and a different APPDATA is a root of its own, beside the home's
expect(same(p("win32", "/h", { APPDATA: "/other/Roam" }))).toEqual([ expect(same(p("win32", "/h", { APPDATA: "/other/Roam" }))).toEqual([
"/other/Roam/Trae CN/User/globalStorage/storage.json", "/other/Roam/Trae CN/User/globalStorage/storage.json",
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "@magpie-community/opencode-trae-auth", "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", "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", "type": "module",
"main": "./index.mjs", "main": "./index.mjs",
+45 -10
View File
@@ -5,6 +5,9 @@ import { afterEach, expect, test } from "bun:test"
import { TraeCNAuthPlugin, _internal } from "./index.mjs" import { TraeCNAuthPlugin, _internal } from "./index.mjs"
import { fakeTrae, json, signedIn, sse } from "./fake.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 let f
afterEach(() => f?.close()) 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", () => { 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: "user", content: "read a" },
{ role: "assistant", content: null, tool_calls: [{ id: "c1", type: "function", function: { name: "read", arguments: "{\"path\":\"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" }, { 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", () => { 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 // 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: "user", content: "read a" },
{ role: "assistant", content: "let me read" }, { role: "assistant", content: "let me read" },
{ role: "assistant", content: null, tool_calls: [{ id: "c1", type: "function", function: { name: "read", arguments: "{\"path\":\"a\"}" } }] }, { 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"]) expect(a[1].tool_calls.map((c) => c.id)).toEqual(["c1"])
// parallel calls split over two assistant turns (magpie#1275) // 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: "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: "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\"}" } }] }, { 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", () => { 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: "user", content: "do it" },
{ role: "assistant", content: null, tool_calls: [{ id: "c1", type: "function", function: { name: " ", arguments: "{}" } }] }, { role: "assistant", content: null, tool_calls: [{ id: "c1", type: "function", function: { name: " ", arguments: "{}" } }] },
{ role: "tool", tool_call_id: "c1", content: "RESULT" }, { 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) expect(a.some((x) => x.role === "tool")).toBe(false)
// a named call beside an unnamed one keeps its own // 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: "user", content: "do both" },
{ role: "assistant", content: null, tool_calls: [ { role: "assistant", content: null, tool_calls: [
{ id: "c1", type: "function", function: { name: "", arguments: "{}" } }, { 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", () => { 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: "user", content: "hi" },
{ role: "assistant", content: "thinking out loud", tool_calls: [{ id: "c1", type: "function", function: { name: "", arguments: "{}" } }] }, { role: "assistant", content: "thinking out loud", tool_calls: [{ id: "c1", type: "function", function: { name: "", arguments: "{}" } }] },
{ role: "user", content: "ok" }, { 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() 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", () => { test("a result for a call the history doesn't carry goes as text, after the results that answer calls", () => {
const m = _internal.traeMessages({ messages: [ const m = _internal.traeMessages({ tools: TOOLS_ON, messages: [
{ role: "assistant", content: null, tool_calls: [{ id: "c1", type: "function", function: { name: "read", arguments: "{}" } }] }, { 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: "c1", content: "AAA" },
{ role: "tool", tool_call_id: "c2", content: "BBB" }, { 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[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<tool_call>{"name":"read","arguments":{"path":"a"}}</tool_call>')
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 () => { test("a stream: text, reasoning, a tool block split across events, a native call, usage", async () => {