trae 0.1.11: a tool call DeepSeek closes with its own tags, or writes as its own invoke, is a call, and its tags don't reach the agent (magpie#823, Dazzle-sys: Trae CN's DeepSeek V4.1 Flash in a group, in Pi, ended the session) — DeepSeek V4.1 Flash wrote <tool_call>{"name":"bash",…}</|DSML|parameter></|DSML|invoke, closing our block with its own tags rather than </tool_call>, or its own <|DSML|invoke name="…"> with <|DSML|parameter>s: neither was read, so the call went to the agent as text. A <tool_call> block is now taken once its JSON object is whole, whatever closes it; DSML invokes (full-width or plain bars, in function_calls or not, several in a row) are calls, a string="true" parameter its text and any other its JSON; the tags and blanks left around a call are dropped, a stray tag too. The new test fails on 0.1.10; bun test (34 pass) and bun scripts/check.mjs trae under a temp HOME pass. Not tried with a real Trae CN account (none here).

This commit is contained in:
yetone
2026-10-05 03:23:58 +08:00
parent 32aaeda5a6
commit 20cd6f1bb2
3 changed files with 148 additions and 8 deletions
+119 -7
View File
@@ -534,25 +534,79 @@ const callId = () => "call_" + randomBytes(12).toString("hex") + (callSeq++).toS
// TextTools splits the model's text into what the agent sees and the tool
// calls it wrote as blocks, holding back the start of a block until it
// is whole
// is whole. A block is ours (<tool_call>{…}</tool_call>), its JSON taken
// once whole even when DeepSeek closes it with its own tags in place of
// </tool_call>, or DeepSeek's (<|DSML|invoke name="…">, magpie#823); the
// tags left around a call are dropped.
class TextTools {
constructor() {
this.buf = ""
this.after = false
}
push(s, end = false) {
this.buf += s
let text = ""
const calls = []
for (;;) {
if (this.after) {
const m = this.buf.match(LEFTOVER)
if (m) this.buf = this.buf.slice(m[0].length)
if (!this.buf || (!end && MARKS.some((k) => k.startsWith(this.buf)))) break
this.after = false
}
const i = this.buf.indexOf(OPEN)
if (i < 0) break
const d = this.buf.search(DSML_AT)
if (i < 0 && d < 0) break
if (d >= 0 && (i < 0 || d < i)) {
text += this.buf.slice(0, d)
this.buf = this.buf.slice(d)
const tag = this.buf.match(DSML_TAG)
if (!tag) {
// a tag not closed yet; at the end, or with another tag after it,
// a stray one dropped
const next = this.buf.indexOf("<", 1)
if (next < 0 && !end) break
this.buf = next < 0 ? "" : this.buf.slice(next)
continue
}
const inv = tag[0].match(DSML_INVOKE)
if (!inv) {
// function_calls, a parameter's close, any other tag: dropped,
// with the blanks after it
this.buf = this.buf.slice(tag[0].length)
this.after = true
continue
}
const body = this.buf.slice(tag[0].length)
const c = body.match(DSML_CLOSE)
const n = body.search(DSML_INVOKE_AT)
let stop = c ? c.index : n >= 0 ? n : -1
if (n >= 0 && n < stop) stop = n
if (stop < 0) {
if (!end) break
stop = body.length
}
calls.push({ name: inv[1], arguments: dsmlArgs(body.slice(0, stop)) })
this.buf = body.slice(c && c.index === stop ? stop + c[0].length : stop)
this.after = true
continue
}
const o = objectEnd(this.buf, i + OPEN.length)
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
continue
}
const j = this.buf.indexOf(CLOSE, i + OPEN.length)
if (j < 0) break
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 v = looseJSON(raw) ?? {}
if (v.name) calls.push({ name: String(v.name), arguments: callArgs(v) })
const w = looseJSON(raw) ?? {}
if (w.name) calls.push({ name: String(w.name), arguments: callArgs(w) })
else text += OPEN + raw + CLOSE
}
if (end) {
@@ -560,9 +614,9 @@ class TextTools {
this.buf = ""
} else {
// keep back an opened block, or what may be the start of one
const i = this.buf.indexOf(OPEN)
let keep = i >= 0 ? this.buf.length - i : 0
if (!keep) for (let k = Math.min(OPEN.length - 1, this.buf.length); k > 0; k--) if (OPEN.startsWith(this.buf.slice(-k))) { keep = k; break }
const at = [this.buf.indexOf(OPEN), this.buf.search(DSML_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)
}
@@ -570,6 +624,64 @@ class TextTools {
}
}
// 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)
const DSML_TAG = new RegExp("^" + DSML + "[^<>]*>")
const DSML_INVOKE = new RegExp(String.raw`^<\s*[||][\s||]*DSML[\s||]*invoke\s+name\s*=\s*"([^"]+)"\s*>`)
const DSML_INVOKE_AT = new RegExp(String.raw`<\s*[||][\s||]*DSML[\s||]*invoke\s+name\s*=`)
const DSML_CLOSE = new RegExp(String.raw`<\s*\/\s*[||][\s||]*DSML[\s||]*invoke\s*>`)
const DSML_PARAM = new RegExp(String.raw`<\s*[||][\s||]*DSML[\s||]*parameter\s+name\s*=\s*"([^"]+)"((?:\s+\w+\s*=\s*"[^"]*")*)\s*>([\s\S]*?)` + DSML + String.raw`parameter\s*>`, "g")
// what is left after a call: blanks, our close, DeepSeek's tags but the
// next call's
const LEFTOVER = new RegExp(String.raw`^(?:\s+|<\/tool_calls?>|<tool_calls>|` + DSML + String.raw`(?![\s||]*invoke\b)[^<>]*>)+`)
// what text may be the start of, so held back
const MARKS = [OPEN, CLOSE, "<|DSML|", "<|DSML|", "</|DSML|", "</|DSML|"]
const MARK_MAX = Math.max(...MARKS.map((m) => m.length))
// dsmlArgs is the arguments of a DSML invoke as JSON: a parameter marked
// string="true" is its text, any other its JSON (its text when not JSON)
function dsmlArgs(body) {
const args = {}
for (const p of body.matchAll(DSML_PARAM)) {
if (/string\s*=\s*"true"/.test(p[2])) {
args[p[1]] = p[3]
continue
}
try {
args[p[1]] = JSON.parse(p[3].trim())
} catch {
args[p[1]] = p[3]
}
}
return JSON.stringify(args)
}
// objectEnd is where the JSON object starting at from (after blanks) ends:
// past its closing brace, -1 when it isn't whole yet, -2 when there's none
function objectEnd(s, from) {
let k = from
while (k < s.length && /\s/.test(s[k])) k++
if (k >= s.length) return -1
if (s[k] !== "{") return -2
let depth = 0
let str = false
let esc = false
for (; k < s.length; k++) {
const ch = s[k]
if (str) {
if (esc) esc = false
else if (ch === "\\") esc = true
else if (ch === '"') str = false
continue
}
if (ch === '"') str = true
else if (ch === "{") depth++
else if (ch === "}" && --depth === 0) return k + 1
}
return -1
}
// looseJSON reads a JSON object as a model writes one: a string holding a
// raw newline or tab (a file's content written out, #799) is read as if
// they were escaped. null when it isn't one even so.
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-trae-auth",
"version": "0.1.10",
"version": "0.1.11",
"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",
+28
View File
@@ -199,3 +199,31 @@ test("looseJSON reads raw control characters in strings only", () => {
expect(_internal.looseJSON('{"a":\n1}')).toEqual({ a: 1 })
expect(_internal.looseJSON('{"a":"x')).toBe(null)
})
test("a block DeepSeek closes with its own tags, or writes as its own invoke, is a call; its tags don't reach the agent (magpie#823)", async () => {
// as Dazzle-sys' screenshot had it, in pieces
const hermes = '<tool_call>{"name":"bash","arguments":{"command":"cd /e/Project && echo \\"### $spec\\"; done"}}</|DSML|parameter>\n</|DSML|invoke'
const t = new _internal.TextTools()
const out = [t.push("Checking. " + hermes.slice(0, 30)), t.push(hermes.slice(30, 95)), t.push(hermes.slice(95)), t.push("", true)]
expect(out.map((o) => o.text).join("")).toBe("Checking. ")
expect(out.flatMap((o) => o.calls).map((c) => [c.name, JSON.parse(c.arguments)])).toEqual([["bash", { command: 'cd /e/Project && echo "### $spec"; done' }]])
f = fakeTrae()
const dsml = '<|DSML|function_calls>\n<|DSML|invoke name="read">\n<|DSML|parameter name="path" string="true">a b.py</|DSML|parameter>\n<|DSML|parameter name="limit" string="false">20</|DSML|parameter>\n</|DSML|invoke>\n<|DSML|invoke name="read"><|DSML|parameter name="path" string="true">c</|DSML|parameter></|DSML|invoke>\n</|DSML|function_calls>'
f.route("POST /api/agent/v3/llm_utils_chat", () => sse([
["output", { response: "Reading. " + dsml.slice(0, 50) }],
["output", { response: dsml.slice(50, 140) }],
["output", { response: dsml.slice(140) }],
["done", {}],
]))
const res = await ask({ model: "glm-5", stream: true, tools: TOOLS, messages: [{ role: "user", content: "hi" }] })
const c = await chunks(res)
const deltas = c.filter((x) => x !== "[DONE]" && x.choices?.length).map((x) => x.choices[0].delta)
expect(deltas.map((d) => d.content ?? "").join("")).toBe("Reading. ")
const calls = deltas.flatMap((d) => d.tool_calls ?? [])
expect(calls.map((t) => [t.function.name, JSON.parse(t.function.arguments)])).toEqual([
["read", { path: "a b.py", limit: 20 }],
["read", { path: "c" }],
])
expect(c.at(-2).choices[0].finish_reason).toBe("tool_calls")
})