diff --git a/packages/trae/index.mjs b/packages/trae/index.mjs index d53edc5..190ed71 100644 --- a/packages/trae/index.mjs +++ b/packages/trae/index.mjs @@ -1067,6 +1067,15 @@ class TextTools { this.after = true } if (end) { + // an opener the stream ended inside (= 0 && !quoted(this.sent + text + this.buf.slice(0, h))) { + text += this.buf.slice(0, h) + this.buf = "" + this.sent += text + return { text, calls, reasoning, error: CUT_SHORT } + } text += this.buf this.buf = "" } else { @@ -1088,11 +1097,11 @@ class TextTools { // gave as . block(body, named, end) { if (!end && !body.trim()) return { wait: true } - const close = body.indexOf(CLOSE) + const [close, closeLen] = closeAt(body) 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) : "" + const rest = close >= 0 ? body.slice(close + closeLen) : "" if (region.trimStart().startsWith("{")) { const v = repairedJSON(region.trim()) if (!v) return { error: close >= 0 ? UNREADABLE : CUT_SHORT } @@ -1116,7 +1125,7 @@ class TextTools { 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) : "" } + return { call: { name: ph[1], arguments: JSON.stringify(p.args) }, rest: close >= 0 ? body.slice(close + closeLen) : "" } } const o = objectEnd(body, 0) if (o > 0 && !this.bad.has(":" + o)) { @@ -1128,10 +1137,10 @@ class TextTools { } // 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)) { + for (let [j, n] = [close, closeLen]; j >= 0; [j, n] = closeAt(body, 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) } + if (v) return { call: { name: String(v.name), arguments: callArgs(v) }, rest: body.slice(j + n) } this.bad.add("<" + j) } if (!end) return { wait: true } @@ -1145,7 +1154,7 @@ class TextTools { 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 (w) return { call: { name: w.name, arguments: callArgs(w) }, rest: close >= 0 ? body.slice(close + closeLen) : "" } if (/^\s*[\w.:-]{1,128}\s*/.test(body)) return { error: close >= 0 ? UNREADABLE : CUT_SHORT } return { text: true } } @@ -1155,9 +1164,18 @@ const CUT_SHORT = "the model's tool call, written as text, was cut short; it is 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})?$/ +// or (plugins#38) +const OPEN_AT = /{}]*)?>|(?=\s*\{))/ +const OPEN_HOLD = /{}\n]{0,200})?$/ +// closeAt is where the next close is from i, or , +// and its length: [-1, 0] when there is none +function closeAt(s, i = 0) { + const re = /<\/tool_?call>/g + re.lastIndex = i + const m = re.exec(s) + return m ? [m.index, m[0].length] : [-1, 0] +} // 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*([\s\S]*?)\s*<\/arg_key>\s*([\s\S]*?)<\/ // a value that isn't a string. A name alone is a call only to a tool the // request has. function glmCall(s, tools = []) { - s = String(s ?? "").replace(/<\/tool_call>\s*$/, "") + s = String(s ?? "").replace(/<\/tool_?call>\s*$/, "") const k = s.indexOf("") const name = (k < 0 ? s : s.slice(0, k)).trim() if (!/^[\w.:-]{1,128}$/.test(name)) return null @@ -1373,9 +1391,9 @@ const DSML_CLOSE = new RegExp(String.raw`<\s*\/\s*[||][\s||]*DSML[\s||]*in 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?>||` + DSML + String.raw`(?![\s||]*invoke\b)[^<>]*>)+`) +const LEFTOVER = new RegExp(String.raw`^(?:\s+|<\/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|", " m.length)) diff --git a/packages/trae/package.json b/packages/trae/package.json index 0fcde62..a73c940 100644 --- a/packages/trae/package.json +++ b/packages/trae/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-trae-auth", - "version": "0.2.8", + "version": "0.2.9", "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 index 4840035..574979a 100644 --- a/packages/trae/textcall.test.mjs +++ b/packages/trae/textcall.test.mjs @@ -122,6 +122,25 @@ test("#38's shapes: escaped quotes and an attribute-framed opener are calls; a b expect(bad.text).toBe("") }) +test("XcodeFish's two on 0.2.6 (#38): without the underscore is read as is, and a stream that ends inside an opener is cut short", () => { + for (const size of [0, 1, 4]) { + // glm-5.3-flash's empty block beside a real call: no call, and not text + expect(read("", size)).toEqual({ text: "", calls: [], error: null }) + 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 }) + expect(read('{"name":"bash","arguments":{"command":}}', size).error).toContain("can't be read") + for (const cut of [" is + for (const s of ["Each call goes in a `` block.", "The tag is `{\"name\":\"x\"}` to call."]) { + for (const size of [0, 1, 6]) expect(read(s, size)).toEqual({ text: s, calls: [], error: null }) + } +}) + 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.',