diff --git a/packages/workbuddy/README.md b/packages/workbuddy/README.md index 0c610c6..f018ef5 100644 --- a/packages/workbuddy/README.md +++ b/packages/workbuddy/README.md @@ -249,6 +249,9 @@ instead of passing it to the agent as an answer. Only text that is still the filter's line is held, so any other reply's words go as they come; the same words ending `stop`, or the filter cutting off a reply after other words, go through as they came. +copilot.tencent.com sends the line with a newline on each side +(`"\nSorry, I can't respond to this question.\n"`); since 0.2.5 white +space around it no longer lets it through. ## Models diff --git a/packages/workbuddy/filtered.test.mjs b/packages/workbuddy/filtered.test.mjs index 7cbd750..98212d9 100644 --- a/packages/workbuddy/filtered.test.mjs +++ b/packages/workbuddy/filtered.test.mjs @@ -121,3 +121,41 @@ test("an answer's words are not held", async () => { globalThis.fetch = realFetch } }) + +// The last chunks copilot.tencent.com sent itspig, byte for byte (plugins +// #540, 2026-10-30): the filter's line comes with a newline on each side, in +// the chunk that ends content_filter. +const LITERAL = + 'data: {"choices":[{"delta":{"content":"","extra_fields":null,"function_call":null,"reasoning_content":" node","refusal":""},"finish_reason":null,"index":0,"logprobs":null}],"created":1793401068,"id":"c56c77084382622efa17c799396db5cf","model":"deepseek-v4.1-flash","object":"chat.completion.chunk","usage":null}\n\n' + + 'data: {"choices":[{"delta":{"content":"\\nSorry, I can\'t respond to this question.\\n","extra_fields":null,"function_call":null,"refusal":"","role":"assistant"},"finish_reason":"content_filter","index":0,"logprobs":null}],"created":0,"id":"c56c77084382622efa17c799396db5cf","model":"deepseek-v4.1-flash","object":"chat.completion.chunk","usage":null}\n\n' + + "data: [DONE]\n\n" + +for (const cuts of [1, 7, 1 << 20]) { + test(`WorkBuddy's own refusal chunks, newlines and all, say nothing (cut every ${cuts} bytes)`, async () => { + const out = await through(LITERAL, cuts) + expect(out).not.toContain("Sorry") + const cs = contents(out) + expect(cs.map((c) => c.delta.reasoning_content ?? "").join("")).toBe(" node") + expect(cs.some((c) => typeof c.delta.content === "string" && c.delta.content)).toBe(false) + expect(cs.at(-1).finish_reason).toBe("content_filter") + expect(out.endsWith("data: [DONE]\n\n")).toBe(true) + }) +} + +test("the filter's line with its newlines in chunks of their own is taken out", async () => { + const upstream = chunk({ reasoning_content: "hm" }) + chunk({ content: "\n" }) + chunk({ content: said }) + chunk({ content: "\n" }) + chunk({}, "content_filter", zero) + "data: [DONE]\n\n" + const out = await through(upstream) + expect(out).not.toContain("Sorry") + expect(contents(out).some((c) => typeof c.delta.content === "string" && c.delta.content)).toBe(false) + expect(contents(out).at(-1).finish_reason).toBe("content_filter") +}) + +test("the filter's line followed by more words goes through whole", async () => { + const upstream = chunk({ content: `\n${said}\n` }) + chunk({ content: "But here is the answer." }) + chunk({}, "content_filter") + "data: [DONE]\n\n" + expect(await through(upstream)).toBe(upstream) +}) + +test("a reply that opens with a newline and then answers goes through whole", async () => { + const upstream = chunk({ content: "\n" }) + chunk({ content: "Hello" }) + chunk({}, "stop") + "data: [DONE]\n\n" + expect(await through(upstream)).toBe(upstream) +}) diff --git a/packages/workbuddy/index.mjs b/packages/workbuddy/index.mjs index 65e8564..67a3be5 100644 --- a/packages/workbuddy/index.mjs +++ b/packages/workbuddy/index.mjs @@ -1605,10 +1605,14 @@ function renamed(res, names) { // no output tokens counted (itspig, plugins #540). const FILTER_SAYS = ["Sorry, I can't respond to this question."] -// filterSaid tells whether text said so far may still be FILTER_SAYS. +// filterSaid tells whether text said so far may still be FILTER_SAYS, with +// white space on either side: copilot.tencent.com sends it as +// "\nSorry, I can't respond to this question.\n" (itspig, plugins #540). +// Text that is only white space so far may still be it; text that goes on +// past it is not. const filterSaid = (text) => { - const t = text.replace(/’/g, "'").trimStart() - return t !== "" && FILTER_SAYS.some((s) => s.startsWith(t)) + const t = text.replace(/’/g, "'").trim() + return FILTER_SAYS.some((s) => s.startsWith(t)) } // unsaid is a streamed chat whose reply WorkBuddy's filter refused with its diff --git a/packages/workbuddy/package.json b/packages/workbuddy/package.json index a1424d6..8599bb4 100644 --- a/packages/workbuddy/package.json +++ b/packages/workbuddy/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-workbuddy-auth", - "version": "0.2.4", + "version": "0.2.5", "description": "OpenCode provider plugins for WorkBuddy's plans: WorkBuddy (CodeBuddy, copilot.tencent.com) and WorkBuddy AI (www.workbuddy.ai)", "type": "module", "main": "./index.mjs",