trae 0.2.35: a bare {"tool_calls":[…]} reply is its calls (#1114)

The sibling of qoder's #1113: a model can write OpenAI's own wire shape
as text, {"tool_calls":[{"name","arguments"}]} on a line of its own,
and trae passed it through as the answer. TextTools now reads a whole,
readable object as its calls ({"name","arguments"} entries and OpenAI's
{"type":"function","function":{…}} both), drops the line's indent with
it, and ends the turn with the cut-short failure when the stream ends
inside one, as a <tool_call> block does. While streaming, a last line
that may still grow into {"tool_calls":[{ is held until it settles.

The shape in prose, in a code fence, {"tool_calls":[]}, an entry with
no name and other JSON lines stay text.

Siblings: doubao-work 0.1.12 (4ea39f01) and orcaterm 0.1.8 (2db9b0ad).
zen-free reads no calls out of text (its upstream sends tool_calls
itself), so it has nothing to change.

Run (WSL magpie-desktop, HOME=$(mktemp -d)):
- packages/trae bun test: 265 pass, 0 fail. jsoncall.test.mjs uses
  #1113's bytes, whole and in 1-, 5- and 4096-character pieces.
- On the old code jsoncall.test.mjs is 2 of 3 red; with jsonHoldAt
  returning -1 by hand, the streamed cases go red again.
- bun scripts/check.mjs trae: ok.
Not tried: a real Trae account producing this reply.
This commit is contained in:
yetone
2026-11-14 10:05:13 +08:00
parent 2db9b0ad7f
commit 07ba4eb88d
3 changed files with 132 additions and 2 deletions
+73 -1
View File
@@ -1787,6 +1787,38 @@ class TextTools {
const sm = this.buf.match(STRAY_AT)
const s = sm ? sm.index : -1
const x = this.buf.search(XML_AT)
const j = jsonCallAt(this.buf, this.sent + text)
if (j >= 0 && [i, d, r, x, s].every((y) => y < 0 || j < y)) {
// OpenAI's own wire shape written as text, {"tool_calls":[{…}]} on
// a line of its own (plugins#1114): whole and readable, its calls;
// cut short, a failure; in code or not readable, text
const head = this.buf.slice(0, j)
const brace = this.buf.indexOf("{", j)
const o = quoted(this.sent + text + head) ? -2 : objectEnd(this.buf, brace)
if (o === -1 && !end) {
text += head
this.buf = this.buf.slice(j)
break
}
if (o === -1) {
this.buf = ""
text += head
this.sent += text
return { text, calls, reasoning, error: CUT_SHORT }
}
const got = o > 0 ? jsonCalls(this.buf.slice(brace, o)) : null
if (!got) {
text += this.buf.slice(0, brace + 1)
this.buf = this.buf.slice(brace + 1)
continue
}
text += head
calls.push(...got)
this.mark = this.sent.length + text.length
this.buf = this.buf.slice(o)
this.after = true
continue
}
if (s >= 0 && [i, d, r, x].every((y) => y < 0 || s < y)) {
// a close with no call open, with what GLM left before it
// (}}</tool_call>, magpie#1945): not the answer, dropped; in code,
@@ -1949,7 +1981,7 @@ class TextTools {
this.buf = ""
} else {
// keep back an opened block, or what may be the start of one
const at = [this.buf.search(OPEN_AT), this.buf.search(OPEN_HOLD), this.buf.search(DSML_AT), this.buf.search(REASON_AT), this.buf.search(XML_AT), this.buf.search(XML_HOLD)].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), this.buf.search(XML_AT), this.buf.search(XML_HOLD), jsonCallAt(this.buf, this.sent + text), jsonHoldAt(this.buf, this.sent + text)].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 }
// and braces, brackets, quotes or a > a close may still come after
@@ -2105,6 +2137,46 @@ class TextTools {
}
}
// A {"tool_calls":[{ at a line's start (plugins#1114); before refers to
// what came before buf, the start of a line only after a line break or
// blanks.
const JSON_CALL_AT = /(^|\n)[ \t]*\{\s*"tool_calls"\s*:\s*\[\s*\{/g
const JSON_CALL_KEY = '{"tool_calls":[{'
const atLineStart = (before) => !/\S/.test(before.slice(before.lastIndexOf("\n") + 1))
// jsonCallAt is where in buf the line holding such an object starts, or -1
function jsonCallAt(buf, before) {
for (const m of buf.matchAll(JSON_CALL_AT)) {
const at = m.index + m[1].length
if (at > 0 || atLineStart(before)) return at
}
return -1
}
// jsonHoldAt is where buf's last line starts when it may still grow into
// one (its indent alone too), or -1
function jsonHoldAt(buf, before) {
const k = buf.lastIndexOf("\n") + 1
if (k === 0 && !atLineStart(before)) return -1
const line = buf.slice(k)
return line && JSON_CALL_KEY.startsWith(line.replace(/\s+/g, "")) ? k : -1
}
// jsonCalls reads such an object's calls: {"name","arguments"} entries or
// OpenAI's {"type":"function","function":{…}}; null when there are none or
// one has no name
function jsonCalls(s) {
const list = looseJSON(s)?.tool_calls
if (!Array.isArray(list) || !list.length) return null
const out = []
for (const e of list) {
const f = e?.function ?? e
if (!f || typeof f.name !== "string" || !f.name) return null
out.push({ name: f.name, arguments: callArgs(f) })
}
return out
}
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"
+58
View File
@@ -0,0 +1,58 @@
// plugins#1114, the sibling of qoder's #1113: a reply that is one JSON
// object in OpenAI's own wire shape, {"tool_calls":[{"name","arguments"}]},
// written as text, went out as the answer. The object is the issue's, byte
// for byte; TextTools now reads it as those calls, whole or a piece at a
// time, and never gives it out as text. JSON in prose or code, an empty
// list and one that can't be read stay text; one cut short ends the turn.
import "./nonet.mjs"
import { expect, test } from "bun:test"
import { _internal } from "./index.mjs"
const BARE = '{"tool_calls":[{"name":"read_file","arguments":{"path":"a.go"}},{"name":"viking_search","arguments":{"query":"x"}}]}'
const CALLS = [["read_file", { path: "a.go" }], ["viking_search", { query: "x" }]]
function read(s, size = 0) {
const t = new _internal.TextTools([])
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
}
test('a bare {"tool_calls":[…]} reply is its calls, whole or a piece at a time', () => {
for (const size of [0, 1, 5, 4096]) {
expect(read(BARE, size)).toEqual({ text: "", calls: CALLS, error: null })
// after a line of text, indented, with OpenAI's own entries
const q = 'I will read it.\n {"tool_calls": [{"type":"function","function":{"name":"read_file","arguments":"{\\"path\\":\\"a.go\\"}"}}]}\n'
expect(read(q, size)).toEqual({ text: "I will read it.\n", calls: [CALLS[0]], error: null })
}
})
test("one cut short ends the turn as a failure, never the answer", () => {
for (const size of [0, 1, 5]) {
const r = read("Reading it.\n" + BARE.slice(0, 40), size)
expect(r.text).toBe("Reading it.\n")
expect(r.calls).toEqual([])
expect(r.error).toContain("cut short")
}
})
test("the same shape in prose, in code, with no calls or unreadable stays the answer", () => {
const texts = [
'OpenAI answers with {"tool_calls":[{"name":"x"}]} when it calls a tool.',
"Like this:\n```json\n" + BARE + "\n```\n",
'{"tool_calls":[]}',
'{"tool_calls":[{"arguments":{}}]}',
'{"total": 3}\nok',
"a < b and {x}",
]
for (const size of [0, 1, 5]) for (const s of texts) expect(read(s, size)).toEqual({ text: s, calls: [], error: null })
})
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-trae-auth",
"version": "0.2.34",
"version": "0.2.35",
"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": "./opencode.mjs",