diff --git a/packages/trae/README.md b/packages/trae/README.md
index d61a6af..769a8ab 100644
--- a/packages/trae/README.md
+++ b/packages/trae/README.md
@@ -96,6 +96,14 @@ agent's `POST https://trae-api-cn.mchost.guru/api/agent/v3/llm_utils_chat`:
v` (a string kept as written, other
types read as JSON), and sometimes a whole call into a native call's
name; both are read as the call.
+ - A block is read as models get it wrong too: every quote escaped
+ (`{\"name\":…}`), quotes or backslashes in a string left unescaped, an
+ opener with the name as an attribute (`` with
+ `v`), or JSON that is whole when the
+ stream ends without ``. A block that still can't be read,
+ or is cut short, is never sent on as the answer: the reply ends with an
+ error (a 502 without a stream), so the gateway can try again
+ (plugins#38). A `` in backticks or a code fence is text.
- Calls from either source become OpenAI `tool_calls`. A name matching a
requested tool but for case or punctuation (`Read`) goes on as the
request's (`read`); any other goes on as written, so the agent can say
diff --git a/packages/trae/index.mjs b/packages/trae/index.mjs
index 9af177c..36c82c1 100644
--- a/packages/trae/index.mjs
+++ b/packages/trae/index.mjs
@@ -889,6 +889,8 @@ class TextTools {
this.buf = ""
this.after = false
this.tools = tools
+ this.sent = "" // the text given out so far, to tell a block in code from a call
+ this.bad = new Set() // where the waiting block's JSON was tried and not read, not tried again
}
push(s, end = false) {
this.buf += s
@@ -907,7 +909,8 @@ class TextTools {
}
this.after = false
}
- const i = this.buf.indexOf(OPEN)
+ const om = this.buf.match(OPEN_AT)
+ const i = om ? om.index : -1
const d = this.buf.search(DSML_AT)
const r = this.buf.search(REASON_AT)
if (r >= 0 && (i < 0 || r < i) && (d < 0 || r < d)) {
@@ -965,53 +968,271 @@ class TextTools {
this.after = true
continue
}
- const o = objectEnd(this.buf, i + OPEN.length)
- const inner = this.buf.slice(i + OPEN.length).trimStart()
- if (inner.startsWith("<") || (end && !inner)) {
+ const head = this.buf.slice(0, i)
+ const body = this.buf.slice(i + om[0].length)
+ if (quoted(this.sent + text + head)) {
+ // a in backticks or a code fence is prose about one
+ text += head + om[0]
+ this.buf = body
+ continue
+ }
+ const named = om[1]?.match(/\bname\s*=\s*"([^"]+)"/)?.[1]
+ const inner = body.trimStart()
+ if (!named && (inner.startsWith("<") || (end && !inner))) {
// no JSON in it: DeepSeek's own invoke, or only the closes of one
// (magpie#823 again); the opener is dropped and what follows read
// as it is
- text += this.buf.slice(0, i)
+ text += head
this.buf = inner
this.after = true
continue
}
- const v = o > 0 ? looseJSON(this.buf.slice(i + OPEN.length, o)) : null
- if (v?.name) {
- text += this.buf.slice(0, i)
- calls.push({ name: String(v.name), arguments: callArgs(v) })
- this.buf = this.buf.slice(o)
- this.after = true
+ const b = this.block(body, named, end)
+ if (b.wait) break
+ this.bad.clear()
+ if (b.text) {
+ // not a call: the opener is text, and what follows read on
+ text += head + om[0]
+ this.buf = body
continue
}
- let j = this.buf.indexOf(CLOSE, i + OPEN.length)
- if (j < 0) {
- // GLM's call, cut short at the end, is still the call
- if (!end || !glmCall(this.buf.slice(i + OPEN.length), this.tools)) break
- j = this.buf.length
+ text += head
+ if (b.error) {
+ // a call that can't be read is not the answer (plugins#38): the
+ // turn ends as a failure, so the gateway can try it again
+ this.buf = ""
+ this.sent += text
+ return { text, calls, reasoning, error: b.error }
}
- text += this.buf.slice(0, i)
- const raw = this.buf.slice(i + OPEN.length, j).trim()
- this.buf = this.buf.slice(j + CLOSE.length)
- const w = looseJSON(raw) ?? glmCall(raw, this.tools) ?? {}
- if (w.name) {
- calls.push({ name: String(w.name), arguments: callArgs(w) })
- this.after = true
- } else text += OPEN + raw + CLOSE
+ calls.push(b.call)
+ this.buf = b.rest
+ this.after = true
}
if (end) {
text += this.buf
this.buf = ""
} else {
// keep back an opened block, or what may be the start of one
- const at = [this.buf.indexOf(OPEN), this.buf.search(DSML_AT), this.buf.search(REASON_AT)].filter((x) => x >= 0)
+ const at = [this.buf.search(OPEN_AT), this.buf.search(OPEN_HOLD), this.buf.search(DSML_AT), this.buf.search(REASON_AT)].filter((x) => x >= 0)
let keep = at.length ? this.buf.length - Math.min(...at) : 0
if (!keep) for (let k = Math.min(MARK_MAX - 1, this.buf.length); k > 0; k--) if (MARKS.some((m) => m.startsWith(this.buf.slice(-k)))) { keep = k; break }
text += this.buf.slice(0, this.buf.length - keep)
this.buf = this.buf.slice(this.buf.length - keep)
}
+ this.sent += text
return { text, calls, reasoning }
}
+
+ // block reads what follows a opener: {call, rest} for a
+ // call, {wait} while it may still become one, {text} when it is no call
+ // at all, {error} for one that can't be read: cut short, or not JSON
+ // even as models get it wrong (plugins#38). named is the name an opener
+ // gave as .
+ block(body, named, end) {
+ if (!end && !body.trim()) return { wait: true }
+ const close = body.indexOf(CLOSE)
+ if (named) {
+ if (close < 0 && !end) return { wait: true }
+ const region = close >= 0 ? body.slice(0, close) : body
+ const rest = close >= 0 ? body.slice(close + CLOSE.length) : ""
+ if (region.trimStart().startsWith("{")) {
+ const v = repairedJSON(region.trim())
+ if (!v) return { error: close >= 0 ? UNREADABLE : CUT_SHORT }
+ return { call: v.name ? { name: String(v.name), arguments: callArgs(v) } : { name: named, arguments: JSON.stringify(v) }, rest }
+ }
+ if (region.includes("")) {
+ const w = glmCall(named + region, this.tools)
+ return w ? { call: { name: w.name, arguments: callArgs(w) }, rest } : { error: close >= 0 ? UNREADABLE : CUT_SHORT }
+ }
+ const p = paramArgs(region, named, this.tools)
+ // cut short at the end: whole only when each parameter was closed
+ if (close < 0 && !p.closed) return { error: CUT_SHORT }
+ return { call: { name: named, arguments: JSON.stringify(p.args) }, rest }
+ }
+ if (body.trimStart().startsWith("{")) {
+ const ph = body.match(PARAM_HEAD)
+ if (ph) {
+ // DeepSeek's half-and-half: the name as JSON, then its parameters
+ // (magpie#917)
+ if (close < 0 && !end) return { wait: true }
+ const region = body.slice(ph[0].length, close >= 0 ? close : body.length)
+ const p = paramArgs(region, ph[1], this.tools)
+ if (close < 0 && !p.closed) return { error: CUT_SHORT }
+ return { call: { name: ph[1], arguments: JSON.stringify(p.args) }, rest: close >= 0 ? body.slice(close + CLOSE.length) : "" }
+ }
+ const o = objectEnd(body, 0)
+ if (o > 0 && !this.bad.has(":" + o)) {
+ const v = callJSON(body.slice(0, o))
+ // a name alone may have its parameters still to come
+ if (v && !end && Object.keys(v).length === 1 && "?\s*/, ""))) return { wait: true }
+ if (v) return { call: { name: String(v.name), arguments: callArgs(v) }, rest: body.slice(o) }
+ this.bad.add(":" + o)
+ }
+ // not whole, or not JSON as it stands: read up to a close (a file's
+ // content may hold one), or at the end all of it
+ for (let j = close; j >= 0; j = body.indexOf(CLOSE, j + 1)) {
+ if (this.bad.has("<" + j)) continue
+ const v = callJSON(body.slice(0, j))
+ if (v) return { call: { name: String(v.name), arguments: callArgs(v) }, rest: body.slice(j + CLOSE.length) }
+ this.bad.add("<" + j)
+ }
+ if (!end) return { wait: true }
+ const v = callJSON(body)
+ if (v) return { call: { name: String(v.name), arguments: callArgs(v) }, rest: "" }
+ return { error: close >= 0 ? UNREADABLE : CUT_SHORT }
+ }
+ // GLM's own (a name, then / pairs), or text
+ if (!/^\s*[\w.:-]{1,128}\s*(?:<|$)/.test(body)) return { text: true }
+ if (close < 0 && !end) return { wait: true }
+ const region = close >= 0 ? body.slice(0, close) : body
+ const w = glmCall(region, this.tools)
+ // GLM's call, cut short at the end, is still the call
+ if (w) return { call: { name: w.name, arguments: callArgs(w) }, rest: close >= 0 ? body.slice(close + CLOSE.length) : "" }
+ if (/^\s*[\w.:-]{1,128}\s*/.test(body)) return { error: close >= 0 ? UNREADABLE : CUT_SHORT }
+ return { text: true }
+ }
+}
+
+const CUT_SHORT = "the model's tool call, written as text, was cut short; it is not sent on as the answer"
+const UNREADABLE = "the model's tool call, written as text, can't be read; it is not sent on as the answer"
+
+// an opener: , one with attributes (),
+// or {}]*)?>|(?=\s*\{))/
+const OPEN_HOLD = /{}\n]{0,200})?$/
+// DeepSeek's half-and-half call: {"name":"x"} (or its brace or > left
+// out), then v or Qwen's
+const PARAM_HEAD = /^\s*\{\s*"name"\s*:\s*"([^"]+)"\s*\}?\s*>?\s*(?=]+))\s*>/g
+
+// quoted says text ends inside a code fence or inline code: what comes
+// next is shown, not done
+function quoted(text) {
+ if ((text.match(/```/g)?.length ?? 0) % 2) return true
+ const line = text.slice(text.lastIndexOf("\n") + 1).replace(/```/g, "")
+ return (line.match(/`/g)?.length ?? 0) % 2 === 1
+}
+
+// paramArgs reads v (or )
+// arguments; a value runs to its close, the next parameter or the end. A
+// value is its text where the tool's schema says string (or it isn't
+// JSON), else its JSON. closed says each one had its close.
+function paramArgs(s, name, tools = []) {
+ const props = (tools.find((t) => (t.function ?? t)?.name === name)?.function ?? {}).parameters?.properties ?? {}
+ const opens = [...s.matchAll(PARAM_OPEN)]
+ const args = {}
+ let closed = opens.length > 0
+ opens.forEach((m, k) => {
+ const from = m.index + m[0].length
+ const to = k + 1 < opens.length ? opens[k + 1].index : s.length
+ let v = s.slice(from, to)
+ const c = v.indexOf("")
+ if (c >= 0) v = v.slice(0, c)
+ else closed = false
+ v = v.replace(/^\r?\n|\r?\n$/g, "")
+ const key = m[1] ?? m[2]
+ if (props[key]?.type === "string") {
+ args[key] = v
+ return
+ }
+ try {
+ args[key] = JSON.parse(v.trim())
+ } catch {
+ args[key] = v
+ }
+ })
+ return { args, closed }
+}
+
+// callJSON is a block's call ({name, …}) as a model may get its JSON
+// wrong, null when it isn't one even so
+function callJSON(s) {
+ const v = repairedJSON(s)
+ return v && typeof v.name === "string" && v.name ? v : null
+}
+
+// repairedJSON reads a JSON object as looseJSON does, and else as models
+// get it wrong (plugins#38): every quote escaped ({\"name\":…}), a
+// backslash that escapes nothing JSON knows (\_ \| C:\Users in a file's
+// content), or quotes inside a string left unescaped ("官方" in a write's
+// content)
+function repairedJSON(s) {
+ const v = looseJSON(s)
+ if (v) return v
+ const t = escapeRaw(s)
+ const tries = []
+ if (/^\s*\{\s*\\"/.test(s)) {
+ try {
+ const u = JSON.parse('"' + s.trim().replace(/[\n\r\t]/g, (c) => (c === "\n" ? "\\n" : c === "\r" ? "\\r" : "\\t")) + '"')
+ tries.push(u, escapeRaw(u))
+ } catch {}
+ tries.push(s.replace(/\\"/g, '"'))
+ }
+ tries.push(fixEscapes(t), quoteFix(fixEscapes(t)))
+ for (const x of tries) {
+ try {
+ const v = JSON.parse(x)
+ if (v && typeof v === "object" && !Array.isArray(v)) return v
+ } catch {}
+ }
+ return null
+}
+
+// fixEscapes makes a backslash that escapes nothing JSON knows a backslash
+// of its own (\' is the quote it meant)
+function fixEscapes(s) {
+ return s.replace(/\\(u[0-9a-fA-F]{4}|["\\/bfnrt])|\\'|\\/g, (m, ok) => (ok ? m : m === "\\'" ? "'" : "\\\\"))
+}
+
+// quoteFix escapes the quotes inside strings a model left unescaped: a
+// quote ends a key only before its colon, and a value only before what
+// may follow the last of one (closing braces or brackets, then the end or
+// a comma and the next key or value)
+function quoteFix(s) {
+ let out = ""
+ const stack = []
+ let key = false
+ for (let i = 0; i < s.length; ) {
+ const ch = s[i]
+ if (ch !== '"') {
+ if (ch === "{") {
+ stack.push("{")
+ key = true
+ } else if (ch === "[") stack.push("[")
+ else if (ch === "}" || ch === "]") stack.pop()
+ else if (ch === ",") key = stack.at(-1) === "{"
+ else if (ch === ":") key = false
+ out += ch
+ i++
+ continue
+ }
+ const isKey = key && stack.at(-1) === "{"
+ const inArray = stack.at(-1) === "["
+ out += ch
+ i++
+ for (; i < s.length; i++) {
+ const c = s[i]
+ if (c === "\\") {
+ out += c + (s[i + 1] ?? "")
+ i++
+ continue
+ }
+ if (c !== '"') {
+ out += c
+ continue
+ }
+ const after = s.slice(i + 1)
+ const ends = isKey ? /^\s*:/.test(after) : (inArray ? /^\s*(?:[}\]]\s*)*(?:$|,\s*["{[\d\-tfn])/ : /^\s*(?:[}\]]\s*)*(?:$|,\s*")/).test(after)
+ if (ends) {
+ out += c
+ i++
+ break
+ }
+ out += '\\"'
+ }
+ key = false
+ }
+ return out
}
// GLM's call template: the tool's name, then each argument as
@@ -1328,6 +1549,10 @@ async function* parts(events, tools = []) {
if (t) yield { reasoning: t }
if (r.text) yield { text: r.text }
for (const c of r.calls) yield call({ ...c, id: callId() })
+ if (r.error) {
+ yield { error: r.error, code: null }
+ return
+ }
}
for (const tc of Array.isArray(d.tool_calls) ? d.tool_calls : []) nc.push(tc)
const u = tokensOf(d.usage)
@@ -1339,6 +1564,10 @@ async function* parts(events, tools = []) {
if (t) yield { reasoning: t }
if (r.text) yield { text: r.text }
for (const c of r.calls) yield call({ ...c, id: callId() })
+ if (r.error) {
+ yield { error: r.error, code: null }
+ return
+ }
for (const c of nc.take(tools)) yield call(c)
if (finish) yield { finish }
}
diff --git a/packages/trae/package.json b/packages/trae/package.json
index 8a59128..f594dc8 100644
--- a/packages/trae/package.json
+++ b/packages/trae/package.json
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-trae-auth",
- "version": "0.2.4",
+ "version": "0.2.5",
"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/textcall.test.mjs b/packages/trae/textcall.test.mjs
new file mode 100644
index 0000000..4840035
--- /dev/null
+++ b/packages/trae/textcall.test.mjs
@@ -0,0 +1,172 @@
+// plugins#38: a tool call the model wrote into its text that wasn't read
+// went out as the answer, the call lost, with a 200 nothing could fail
+// over on. The shapes are the issue's (deepseek-v4.1-flash on trae-cn) and
+// ARNO's on Discord (trae-cn, content redacted by them as xxxx).
+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 deltasOf = (c) => c.filter((x) => x !== "[DONE]" && x.choices?.length).map((x) => x.choices[0].delta)
+const callsOf = (c) => deltasOf(c).flatMap((d) => d.tool_calls ?? []).map((t) => [t.function.name, JSON.parse(t.function.arguments)])
+const textOf = (c) => deltasOf(c).map((d) => d.content ?? "").join("")
+const finishOf = (c) => c.filter((x) => x !== "[DONE]" && x.choices?.[0]?.finish_reason).map((x) => x.choices[0].finish_reason)
+
+const TOOLS = [
+ { type: "function", function: { name: "bash", parameters: { type: "object", properties: { command: { type: "string" } } } } },
+ { type: "function", function: { name: "write", parameters: { type: "object", properties: { content: { type: "string" }, filePath: { type: "string" } } } } },
+]
+
+// what TextTools makes of s given whole, and given a piece at a time
+function read(s, size = 0, tools = TOOLS) {
+ const t = new _internal.TextTools(tools)
+ const pieces = size ? Array.from({ length: Math.ceil(s.length / size) }, (_, k) => s.slice(k * size, (k + 1) * size)) : [s]
+ const out = { text: "", calls: [], error: null }
+ for (const [k, p] of [...pieces, ""].entries()) {
+ const r = t.push(p, k === pieces.length)
+ out.text += r.text
+ out.calls.push(...r.calls.map((c) => [c.name, JSON.parse(c.arguments)]))
+ if (r.error) {
+ out.error = r.error
+ break
+ }
+ }
+ return out
+}
+
+// ARNO's two replies, as sent
+const ARNO_1 = '官方资料齐了。我把完整分析写成文件,聊天里给你重点。\n{"name":"write","arguments":{"content":"xxxxxxxx"}}'
+const ARNO_2 = '文件确实丢了(可能是之前写入没成功),我重写完整版。\n{"name":"write","arguments":{"content":"xxxxxx"}}'
+
+test("ARNO's two replies are calls: a block closed, and one whose JSON is whole when the stream ends", () => {
+ for (const size of [0, 1, 7]) {
+ expect(read(ARNO_1, size)).toEqual({ text: "官方资料齐了。我把完整分析写成文件,聊天里给你重点。\n", calls: [["write", { content: "xxxxxxxx" }]], error: null })
+ expect(read(ARNO_2, size)).toEqual({ text: "文件确实丢了(可能是之前写入没成功),我重写完整版。\n", calls: [["write", { content: "xxxxxx" }]], error: null })
+ }
+})
+
+test("a write's content as models get its JSON wrong is still the call: quotes left unescaped, backslashes JSON doesn't know", () => {
+ // ARNO's second reply, its redacted content as a written-out analysis
+ // may have it
+ const contents = [
+ '# 分析\n他说"官方"的资料齐了',
+ "| 列 | a \\| b |\n| --- | --- |",
+ "snake\\_case 和 \\*强调\\*",
+ "路径 C:\\Users\\arno\\x",
+ "```json\n{\"a\": 1, \"b\": \"c\"}\n```\n完",
+ ]
+ const want = [
+ '# 分析\n他说"官方"的资料齐了',
+ "| 列 | a \\| b |\n| --- | --- |",
+ "snake\\_case 和 \\*强调\\*",
+ "路径 C:\\Users\\arno\\x",
+ '```json\n{"a": 1, "b": "c"}\n```\n完',
+ ]
+ contents.forEach((c, k) => {
+ const s = '文件确实丢了(可能是之前写入没成功),我重写完整版。\n{"name":"write","arguments":{"content":"' + c + '"}}'
+ for (const size of [0, 5]) {
+ const r = read(s, size)
+ expect(r.error).toBe(null)
+ expect(r.text).toBe("文件确实丢了(可能是之前写入没成功),我重写完整版。\n")
+ expect(r.calls).toEqual([["write", { content: want[k] }]])
+ }
+ })
+})
+
+test("#38's shapes: escaped quotes and an attribute-framed opener are calls; a block cut short is an error, never text", () => {
+ // the issue's repro, case by case
+ expect(read('Sure. {"name":"bash","arguments":{"command":"ls"}}')).toEqual({ text: "Sure. ", calls: [["bash", { command: "ls" }]], error: null })
+ for (const size of [0, 3]) {
+ expect(read('{\\"name\\":\\"bash\\",\\"arguments\\":{\\"command\\":\\"ls\\"}}', size)).toEqual({ text: "", calls: [["bash", { command: "ls" }]], error: null })
+ expect(read('\nls\n', size)).toEqual({ text: "", calls: [["bash", { command: "ls" }]], error: null })
+ // the name as JSON, then its parameters (magpie#917's shape)
+ expect(read('{"name":"bash"}>\nls -la\n', size)).toEqual({ text: "", calls: [["bash", { command: "ls -la" }]], error: null })
+ }
+ // a quote in a command escaped as a whole block's are
+ expect(read('{\\"name\\":\\"bash\\",\\"arguments\\":{\\"command\\":\\"echo \\\\\\"hi\\\\\\"\\"}}').calls).toEqual([["bash", { command: 'echo "hi"' }]])
+ for (const cut of [
+ '{"name":"bash","arguments":{"command":"cd "C:/x"',
+ '{"name":"bash","arguments":{"command":"ls"}',
+ '{"name":"write","arguments":{"content":"## 结论\\n1. 先',
+ '\nls -',
+ ]) {
+ for (const size of [0, 4]) {
+ const r = read("Running it.\n" + cut, size)
+ expect(r.error).toContain("cut short")
+ expect(r.text).toBe("Running it.\n")
+ expect(r.calls).toEqual([])
+ }
+ }
+ // closed, but not JSON however read
+ const bad = read('{"name":"bash","arguments":{"command":}}')
+ expect(bad.error).toContain("can't be read")
+ expect(bad.text).toBe("")
+})
+
+test("prose that mentions stays text: in backticks, in a code fence, or a tag named in passing", () => {
+ for (const s of [
+ 'Write `{"name":"bash","arguments":{}}` to call a tool.',
+ "Each call goes in a `` block.",
+ 'Example:\n```\n{"name":"bash","arguments":{"command":"ls"}}\n```\nThat is all.',
+ "The tag wraps a call; ends it.",
+ "hello ok",
+ ]) {
+ for (const size of [0, 1, 6]) expect(read(s, size)).toEqual({ text: s, calls: [], error: null })
+ }
+})
+
+test("a stream whose call is cut short ends with an error, not a finish, and the block isn't the answer", async () => {
+ f = fakeTrae()
+ f.route("POST /api/agent/v3/llm_utils_chat", () => sse([
+ ["output", { response: "Running it.\n" }],
+ ["output", { response: '{"name":"bash","arguments":{"command":"cd "C:/x"' }],
+ ["done", { finish_reason: "stop" }],
+ ]))
+ const res = await ask({ model: "deepseek-v4.1-flash", stream: true, tools: TOOLS, messages: [{ role: "user", content: "hi" }] })
+ const c = await chunks(res)
+ expect(textOf(c)).toBe("Running it.\n")
+ expect(textOf(c)).not.toContain(" x !== "[DONE]" && x.error).error.message).toContain("cut short")
+ expect(c.at(-1)).toBe("[DONE]")
+
+ // asked without a stream, it is a failure the gateway can fail over on
+ const res2 = await ask({ model: "deepseek-v4.1-flash", tools: TOOLS, messages: [{ role: "user", content: "hi" }] })
+ expect(res2.status).toBe(502)
+ expect((await res2.json()).error.message).toContain("cut short")
+})
+
+test("ARNO's second reply through the plugin: a write call, the reply finished as one that called tools", async () => {
+ f = fakeTrae()
+ f.route("POST /api/agent/v3/llm_utils_chat", () => sse([
+ ["output", { response: ARNO_2.slice(0, 30) }],
+ ["output", { response: ARNO_2.slice(30, 60) }],
+ ["output", { response: ARNO_2.slice(60) }],
+ ["done", { finish_reason: "stop" }],
+ ]))
+ // the agent declared the tool as Write
+ const tools = [{ type: "function", function: { name: "Write", parameters: { type: "object", properties: { content: { type: "string" } } } } }]
+ const c = await chunks(await ask({ model: "deepseek-v4.1-flash", stream: true, tools, messages: [{ role: "user", content: "hi" }] }))
+ expect(textOf(c)).toBe("文件确实丢了(可能是之前写入没成功),我重写完整版。\n")
+ expect(callsOf(c)).toEqual([["Write", { content: "xxxxxx" }]])
+ expect(finishOf(c)).toEqual(["tool_calls"])
+})