diff --git a/packages/workbuddy/index.mjs b/packages/workbuddy/index.mjs index 18b8fda..d455aab 100644 --- a/packages/workbuddy/index.mjs +++ b/packages/workbuddy/index.mjs @@ -1350,6 +1350,18 @@ 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" const REFUSED = /unapproved channel|illegal api invocation/i +// RESTRICTED_HINT is what the user can do about WorkBuddy's 403 code 11140 +// "request illegal", whose displayMsg says "Your account has been restricted +// due to unusual activity" (magpie #1849): the account is out until WorkBuddy +// lifts the restriction, which an appeal asks for. It ends the error's +// message as magpie's built-in puts it (provider.WBRestrictedHint), word for +// word, so magpie rests the account for hours rather than minutes. +const RESTRICTED_HINT = + "WorkBuddy restricted this account for unusual activity; it stays out until WorkBuddy lifts that, which an appeal (Submit feedback in WorkBuddy) asks for" +const RESTRICTED_CODE = 11140 +// The code is matched whole, as with 14018: 111400 is another error. +const RESTRICTED_TEXT = /account has been restricted due to unusual activity|账号存在异常行为|帳號存在異常行為|["']code["']\s*:\s*["']?11140(?![\d.])/i + // EXHAUSTED_HINT explains WorkBuddy's requirement of an active package // even for 0-rate / free models. When an account has no packages or 0 credits, // WorkBuddy returns code 14018 ("Credits exhausted..."). @@ -1367,7 +1379,7 @@ function kept(res) { return new Response(res.body, { status: res.status, statusText: res.statusText, headers }) } -// explained is res with REFUSED_HINT or EXHAUSTED_HINT added to that refusal's message. +// explained is res with REFUSED_HINT, RESTRICTED_HINT or EXHAUSTED_HINT added to that refusal's message. // Each refusal says the sign-in is kept: the built-in passed WorkBuddy's // 401 on and never marked a WorkBuddy account lapsed. async function explained(res) { @@ -1386,8 +1398,9 @@ async function explained(res) { const at = v?.error && typeof v.error === "object" ? v.error : v && typeof v === "object" ? v : null const code = at?.data?.code ?? at?.code ?? v?.code ?? v?.data?.code const isRefused = REFUSED.test(text) - const isExhausted = code === 14018 || Number(code) === 14018 || EXHAUSTED_TEXT.test(text) - if (!isRefused && !isExhausted) return again(text) + const isRestricted = !isRefused && (Number(code) === RESTRICTED_CODE || RESTRICTED_TEXT.test(text)) + const isExhausted = !isRestricted && (code === 14018 || Number(code) === 14018 || EXHAUSTED_TEXT.test(text)) + if (!isRefused && !isRestricted && !isExhausted) return again(text) let msg = text.trim() if (at && typeof at.message === "string") msg = at.message @@ -1396,9 +1409,9 @@ async function explained(res) { else if (typeof v?.msg === "string" && v.msg) msg = v.msg // WorkBuddy's own {"code", "msg"} else if (typeof v?.data?.msg === "string") msg = v.data.msg - const hint = isRefused ? REFUSED_HINT : EXHAUSTED_HINT - const type = isRefused ? "permission_error" : "insufficient_quota" - const errCode = isRefused ? null : 14018 + const hint = isRefused ? REFUSED_HINT : isRestricted ? RESTRICTED_HINT : EXHAUSTED_HINT + const type = isRefused || isRestricted ? "permission_error" : "insufficient_quota" + const errCode = isRefused ? null : isRestricted ? RESTRICTED_CODE : 14018 msg = `${msg} — ${hint}` const headers = new Headers(res.headers) headers.set("content-type", "application/json") @@ -1412,4 +1425,4 @@ export const WorkBuddyAuthPlugin = makePlugin(SITES.workbuddy) export const WorkBuddyAIAuthPlugin = makePlugin(SITES["workbuddy-ai"]) // for tests -export const _internal = { textOnly, runsOutOf, packagesOf, endOf, enterpriseHeaders, enterpriseOf, liveModels, browserSignIn, withSystem, normalizeToolChoice, fitTools, fitted, renamed, unflagged, usageOf, desktopHeld, explained, REFUSED_HINT, EXHAUSTED_HINT, freeCredits, creditsOf, fresh, SITES, renewing, renewed, SESSION, attend, turnKey, conversationKey, signatureOf } +export const _internal = { textOnly, runsOutOf, packagesOf, endOf, enterpriseHeaders, enterpriseOf, liveModels, browserSignIn, withSystem, normalizeToolChoice, fitTools, fitted, renamed, unflagged, usageOf, desktopHeld, explained, REFUSED_HINT, RESTRICTED_HINT, EXHAUSTED_HINT, freeCredits, creditsOf, fresh, SITES, renewing, renewed, SESSION, attend, turnKey, conversationKey, signatureOf } diff --git a/packages/workbuddy/package.json b/packages/workbuddy/package.json index d7e32db..8dbb663 100644 --- a/packages/workbuddy/package.json +++ b/packages/workbuddy/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-workbuddy-auth", - "version": "0.1.16", + "version": "0.1.17", "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/refused.test.mjs b/packages/workbuddy/refused.test.mjs index e40e6b8..1d38236 100644 --- a/packages/workbuddy/refused.test.mjs +++ b/packages/workbuddy/refused.test.mjs @@ -4,7 +4,7 @@ import { expect, test } from "bun:test" import { _internal } from "./index.mjs" -const { explained, REFUSED_HINT, EXHAUSTED_HINT } = _internal +const { explained, REFUSED_HINT, RESTRICTED_HINT, EXHAUSTED_HINT } = _internal test("the refusal ends in the hint", async () => { const res = await explained(new Response(JSON.stringify({ error: { message: "Illegal API invocation from an unapproved channel", code: 11004 } }), { status: 403 })) @@ -106,3 +106,47 @@ test("a chat body in bytes is read, and given the system message", () => { expect(JSON.parse(withSystem(b))).toEqual(want) expect(JSON.parse(withSystem(JSON.stringify(chat)))).toEqual(want) }) + +// magpie #1849: the body WorkBuddy AI answered for an account it restricted, +// byte for byte as the reporter posted it. +const RESTRICTED_BODY = `{"code":11140,"msg":"request illegal", + "displayMsg":{"en":"Your account has been restricted due to unusual activity", + "zh":"账号存在异常行为,已被限制使用","zh-hant":"帳號存在異常行為,已被限制使用"}, + "displayTips":{"en":"We detected unusual activity on this account. Submit feedback to appeal this restriction", + "zh":"系统检测到该账号存在异常使用行为,如需申诉请提交反馈"}, + "actions":["EDIT_INPUT","SUBMIT_FEEDBACK"]}` + +test("a restricted account's 11140 says its msg, then the restricted hint, word for word magpie's", async () => { + const res = await explained(new Response(RESTRICTED_BODY, { status: 403 })) + expect(res.status).toBe(403) + expect(res.headers.get("X-Magpie-Sign-In")).toBe("kept") + const body = await res.json() + expect(body.error).toEqual({ message: "request illegal — " + RESTRICTED_HINT, type: "permission_error", code: 11140 }) + // magpie's routing.go looks for provider.WBRestrictedHint itself + expect(RESTRICTED_HINT).toBe( + "WorkBuddy restricted this account for unusual activity; it stays out until WorkBuddy lifts that, which an appeal (Submit feedback in WorkBuddy) asks for", + ) +}) + +test("the restriction is known by a string code, under error, or by its words alone", async () => { + for (const b of [ + '{"code":"11140","msg":"request illegal"}', + '{"error":{"code":11140,"message":"request illegal"}}', + '{"error":{"data":{"code":11140,"msg":"request illegal"}}}', + '{"msg":"账号存在异常行为,已被限制使用"}', + '{"code":11140,"msg":"request ill', + ]) { + const body = await (await explained(new Response(b, { status: 403 }))).json() + expect(body.error.code).toBe(11140) + expect(body.error.message.endsWith(" — " + RESTRICTED_HINT)).toBe(true) + } +}) + +test("a code that only begins with 11140, and 11128, are not a restriction", async () => { + for (const b of ['{"code":111400,"msg":"request illegal"}', '{"error":{"code":111400,"message":"x"}}']) { + const res = await explained(new Response(b, { status: 403 })) + expect(await res.text()).toBe(b) + } + const body = await (await explained(new Response('{"error":{"message":"Illegal API invocation from an unapproved channel","code":11128}}', { status: 403 }))).json() + expect(body.error.message.endsWith(REFUSED_HINT)).toBe(true) +})