Files
yetone 07ba4eb88d 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.
2026-11-14 10:05:13 +08:00

59 lines
2.5 KiB
JavaScript

// 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 })
})