diff --git a/packages/workbuddy/README.md b/packages/workbuddy/README.md index f018ef5..14be472 100644 --- a/packages/workbuddy/README.md +++ b/packages/workbuddy/README.md @@ -251,7 +251,9 @@ 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. +space around it no longer lets it through. It also puts `"tool_calls": []` on +every delta; since 0.2.6 only a list with a call in it lets the reply +through as it came. ## Models diff --git a/packages/workbuddy/filtered.test.mjs b/packages/workbuddy/filtered.test.mjs index 98212d9..fb488e5 100644 --- a/packages/workbuddy/filtered.test.mjs +++ b/packages/workbuddy/filtered.test.mjs @@ -159,3 +159,36 @@ test("a reply that opens with a newline and then answers goes through whole", as const upstream = chunk({ content: "\n" }) + chunk({ content: "Hello" }) + chunk({}, "stop") + "data: [DONE]\n\n" expect(await through(upstream)).toBe(upstream) }) + +// copilot.tencent.com puts "tool_calls": [] on every delta, text too, and +// "finish_reason": "" until the last (magpie f2c7536d7, ARNO on Discord). +// magpie's gateway drops the empty list (chatTidy) and writes the chunk +// back with its keys sorted, so itspig's paste, read off magpie, had +// neither. An empty list is no call: the refusal is still the filter's. +const WIRE = + 'data: {"choices":[{"delta":{"content":"","extra_fields":null,"function_call":null,"reasoning_content":" node","refusal":"","tool_calls":[]},"finish_reason":"","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,"reasoning_content":"","refusal":"","role":"assistant","tool_calls":[]},"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(`the refusal with WorkBuddy's empty tool_calls on every delta says nothing (cut every ${cuts} bytes)`, async () => { + const out = await through(WIRE, cuts) + expect(out).not.toContain("Sorry") + const cs = contents(out) + expect(cs.map((c) => c.delta.reasoning_content ?? "").join("")).toBe(" node") + expect(cs.at(-1).finish_reason).toBe("content_filter") + }) +} + +test("the filter's line split over deltas that each carry an empty tool_calls is taken out", async () => { + const d = (content, finish = "") => chunk({ content, reasoning_content: "", tool_calls: [] }, finish) + const upstream = chunk({ role: "assistant", content: "", reasoning_content: "hm", tool_calls: [] }, "") + d("\n") + d("Sorry") + d(", I can't respond") + d(" to this question.") + d("\n") + d("", "content_filter") + "data: [DONE]\n\n" + const out = await through(upstream) + expect(out).not.toContain("Sorry") + expect(contents(out).at(-1).finish_reason).toBe("content_filter") +}) + +test("a real tool call still lets the reply through as it came", async () => { + const upstream = chunk({ content: "", tool_calls: [{ index: 0, id: "c1", type: "function", function: { name: "grep", arguments: "{}" } }] }) + chunk({ content: said }) + chunk({}, "content_filter") + "data: [DONE]\n\n" + expect(await through(upstream)).toBe(upstream) +}) diff --git a/packages/workbuddy/index.mjs b/packages/workbuddy/index.mjs index 67a3be5..797c690 100644 --- a/packages/workbuddy/index.mjs +++ b/packages/workbuddy/index.mjs @@ -1660,7 +1660,10 @@ function unsaid(res) { const c = chunkOf(ev)?.choices?.[0] const text = typeof c?.delta?.content === "string" ? c.delta.content : "" const finish = c?.finish_reason - if (c?.delta?.tool_calls || (text && !filterSaid(said + text))) { + // copilot.tencent.com sends "tool_calls": [] on every delta, text + // too: only a list with a call in it is one + const calls = c?.delta?.tool_calls + if ((Array.isArray(calls) ? calls.length > 0 : calls) || (text && !filterSaid(said + text))) { open = false const out = [...held, ev] held = [] diff --git a/packages/workbuddy/package.json b/packages/workbuddy/package.json index 8599bb4..99aee1e 100644 --- a/packages/workbuddy/package.json +++ b/packages/workbuddy/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-workbuddy-auth", - "version": "0.2.5", + "version": "0.2.6", "description": "OpenCode provider plugins for WorkBuddy's plans: WorkBuddy (CodeBuddy, copilot.tencent.com) and WorkBuddy AI (www.workbuddy.ai)", "type": "module", "main": "./index.mjs",