diff --git a/packages/workbuddy/index.mjs b/packages/workbuddy/index.mjs index bc4a6cd..4beefb5 100644 --- a/packages/workbuddy/index.mjs +++ b/packages/workbuddy/index.mjs @@ -257,17 +257,75 @@ function sign(site, a, headers) { headers.set("X-Request-ID", msg) } +// FLAGGED are the words WorkBuddy refuses a chat for, "Illegal API +// invocation from an unapproved channel" (magpie #182), each with words +// that say the same and pass: tried one by one against both builds with +// the requests Claude Code 2.1.280 and Codex 0.159.2 send. WorkBuddy reads +// every message, not only the system prompt (Claude Code puts its main +// branch in the first user message), and pays no mind to case. Claude +// Code's billing header means nothing to WorkBuddy's models and goes. +const FLAGGED = [ + [/x-anthropic-billing-header:[^\n]*\n?/gi, ""], + [/You are Claude Code, Anthropic['’]s official CLI for Claude/gi, "You are Claude Code, Anthropic's CLI for Claude"], + [/Main branch \(you will usually use this for PRs\)/gi, "Main branch (usually the base for PRs)"], + [/(?= 0; i--) { + const m = messages[i] + if (typeof m?.content === "string") { + const t = unflagged(m.content) + if (t === m.content) continue + changed = true + if (t.trim() || m.role !== "system") m.content = t + else messages.splice(i, 1) + } else if (Array.isArray(m?.content)) { + const parts = [] + let here = false + for (const p of m.content) { + const t = typeof p?.text === "string" ? unflagged(p.text) : null + if (t === null || t === p.text) { + parts.push(p) + continue + } + here = true + if (t.trim()) parts.push({ ...p, text: t }) + } + if (!here) continue + changed = true + if (parts.length || m.role !== "system") m.content = parts + else messages.splice(i, 1) + } + } + return changed +} + // withSystem gives a chat that doesn't open with a system message -// WorkBuddy's default one, as its app does. +// WorkBuddy's default one, as its app does, and takes FLAGGED's words out +// of it. function withSystem(body) { // a body magpie or OpenCode hands over as bytes is read as the text it is if (body instanceof ArrayBuffer || ArrayBuffer.isView(body)) body = new TextDecoder().decode(body) if (typeof body !== "string") return body try { const b = JSON.parse(body) - if (!Array.isArray(b?.messages) || !b.messages.length || b.messages[0]?.role === "system") return body - b.messages.unshift({ role: "system", content: "You are a helpful assistant." }) - return JSON.stringify(b) + if (!Array.isArray(b?.messages) || !b.messages.length) return body + let changed = unflag(b.messages) + if (b.messages[0]?.role !== "system") { + b.messages.unshift({ role: "system", content: "You are a helpful assistant." }) + changed = true + } + return changed ? JSON.stringify(b) : body } catch { return body } @@ -696,8 +754,8 @@ function makePlugin(site) { // REFUSED_HINT is what the user can do about WorkBuddy's "Illegal API // invocation from an unapproved channel": both builds answer it to a chat -// whose system prompt is Codex's or Claude Code's own (magpie #182), -// whatever the headers. It ends the error's message, as magpie's built-in +// holding words of Codex's or Claude Code's own (magpie #182), whatever the +// headers. FLAGGED rewrites the ones known; this is for one it doesn't know. It ends the error's message, as magpie's built-in // put it (provider.WBRefusedHint), so magpie's routing page says it apart. const REFUSED_HINT = "WorkBuddy refuses chats from Codex and Claude Code (their system prompt); use it from Hermes, OpenCode or Pi, or add another provider to this group" @@ -746,4 +804,4 @@ export const WorkBuddyAuthPlugin = makePlugin(SITES.workbuddy) export const WorkBuddyAIAuthPlugin = makePlugin(SITES["workbuddy-ai"]) // for tests -export const _internal = { withSystem, usageOf, desktopHeld, explained, REFUSED_HINT, freeCredits, creditsOf, fresh, SITES, renewing, renewed } +export const _internal = { withSystem, unflagged, usageOf, desktopHeld, explained, REFUSED_HINT, freeCredits, creditsOf, fresh, SITES, renewing, renewed } diff --git a/packages/workbuddy/package.json b/packages/workbuddy/package.json index de1eb60..7922bb1 100644 --- a/packages/workbuddy/package.json +++ b/packages/workbuddy/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-workbuddy-auth", - "version": "0.1.8", + "version": "0.1.9", "description": "OpenCode provider plugins for WorkBuddy's plans: WorkBuddy (CodeBuddy, copilot.tencent.com) and WorkBuddy AI (www.workbuddy.ai)", "type": "module", "main": "./index.mjs", diff --git a/packages/workbuddy/unflag.test.mjs b/packages/workbuddy/unflag.test.mjs new file mode 100644 index 0000000..3641e67 --- /dev/null +++ b/packages/workbuddy/unflag.test.mjs @@ -0,0 +1,51 @@ +// WorkBuddy refuses a chat holding words of Codex's or Claude Code's own +// prompts (magpie #182); withSystem puts words that pass in their place, +// in every message, as tried against both builds. +import { expect, test } from "bun:test" +import { _internal } from "./index.mjs" + +const { withSystem, unflagged } = _internal + +test("each flagged phrase is put in other words, whatever its case", () => { + expect(unflagged("You are Claude Code, Anthropic's official CLI for Claude.")).toBe("You are Claude Code, Anthropic's CLI for Claude.") + expect(unflagged("Main branch (you will usually use this for PRs): main")).toBe("Main branch (usually the base for PRs): main") + expect(unflagged("MAIN BRANCH (YOU WILL USUALLY USE THIS FOR PRS): main")).toBe("Main branch (usually the base for PRs): main") + const codex = "You are a coding agent running in the Codex CLI, a terminal-based coding assistant. Codex CLI is an open source project led by OpenAI. You are expected to be precise, safe, and helpful." + expect(unflagged(codex)).toBe(codex.replace("Codex CLI is an open", "The Codex CLI is an open")) + expect(unflagged(unflagged(codex))).toBe(unflagged(codex)) + expect(unflagged("x-anthropic-billing-header: cc_version=2.1.280.31f; cc_entrypoint=sdk-cli;\nhello")).toBe("hello") + expect(unflagged("nothing to see")).toBe("nothing to see") +}) + +test("Claude Code's request through magpie passes: header block gone, the rest rewritten", () => { + const body = JSON.stringify({ + model: "glm-5.3", + messages: [ + { role: "system", content: [ + { type: "text", text: "x-anthropic-billing-header: cc_version=2.1.280.31f; cc_entrypoint=cli;" }, + { type: "text", text: "You are Claude Code, Anthropic's official CLI for Claude." }, + ] }, + { role: "user", content: [{ type: "text", text: "gitStatus\nMain branch (you will usually use this for PRs): main" }, { type: "image_url", image_url: { url: "data:," } }] }, + ], + }) + const b = JSON.parse(withSystem(body)) + expect(b.messages[0]).toEqual({ role: "system", content: [{ type: "text", text: "You are Claude Code, Anthropic's CLI for Claude." }] }) + expect(b.messages[1].content[0].text).toBe("gitStatus\nMain branch (usually the base for PRs): main") + expect(b.messages[1].content[1]).toEqual({ type: "image_url", image_url: { url: "data:," } }) +}) + +test("a system message of the header alone goes, and WorkBuddy's default takes its place", () => { + const body = JSON.stringify({ messages: [ + { role: "system", content: "x-anthropic-billing-header: cc_version=2.1.280; cc_entrypoint=cli;" }, + { role: "user", content: "hi" }, + ] }) + expect(JSON.parse(withSystem(body)).messages).toEqual([ + { role: "system", content: "You are a helpful assistant." }, + { role: "user", content: "hi" }, + ]) +}) + +test("a chat with nothing flagged is sent as it came", () => { + const body = JSON.stringify({ messages: [{ role: "system", content: "be brief" }, { role: "user", content: "hi" }] }) + expect(withSystem(body)).toBe(body) +})