diff --git a/packages/trae/index.mjs b/packages/trae/index.mjs index c8a1386..7724566 100644 --- a/packages/trae/index.mjs +++ b/packages/trae/index.mjs @@ -557,6 +557,11 @@ class TextTools { 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 + // a tag cut short at the end ( 1 && MARKS.some((k) => k.startsWith(this.buf.trimEnd()))) { + this.buf = "" + break + } this.after = false } const i = this.buf.indexOf(OPEN) @@ -597,6 +602,16 @@ class TextTools { continue } const o = objectEnd(this.buf, i + OPEN.length) + const inner = this.buf.slice(i + OPEN.length).trimStart() + if (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) + 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) diff --git a/packages/trae/package.json b/packages/trae/package.json index f40f2ee..d72d719 100644 --- a/packages/trae/package.json +++ b/packages/trae/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-trae-auth", - "version": "0.1.12", + "version": "0.1.13", "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", diff --git a/packages/trae/request.test.mjs b/packages/trae/request.test.mjs index 76244a6..2ffd2b4 100644 --- a/packages/trae/request.test.mjs +++ b/packages/trae/request.test.mjs @@ -227,3 +227,22 @@ test("a block DeepSeek closes with its own tags, or writes as its own invoke, is ]) expect(c.at(-2).choices[0].finish_reason).toBe("tool_calls") }) + +test("a with only DeepSeek's closing tags after it says nothing; one wrapping its invoke is the call (magpie#823 again)", () => { + // Dazzle-sys' pi screenshot: the whole reply, cut short + const junk = "\n\n\n o.text).join("")).toBe("") + expect(out.flatMap((o) => o.calls)).toEqual([]) + } + const wrapped = 'Looking.\n<|DSML|invoke name="bash">\n<|DSML|parameter name="command" string="true">ls\n\n' + const t = new _internal.TextTools() + const out = [t.push(wrapped.slice(0, 20)), t.push(wrapped.slice(20)), t.push("", true)] + expect(out.map((o) => o.text).join("")).toBe("Looking.") + expect(out.flatMap((o) => o.calls).map((c) => [c.name, JSON.parse(c.arguments)])).toEqual([["bash", { command: "ls" }]]) + // a block that's text, not tags, stays as it was + const u = new _internal.TextTools() + expect([u.push("hello ok"), u.push("", true)].map((o) => o.text).join("")).toBe("hello ok") +})