workbuddy 0.1.10: explain credits exhausted (code 14018) errors with hint and extract nested error message (#25)

* workbuddy 0.1.10: explain credits exhausted (code 14018) errors with hint and extract nested error message

When calling WorkBuddy / WorkBuddy AI models (including 0-rate / free models), accounts with no active packages or depleted credits receive 429 code 14018 ('Credits exhausted...').

- Extract nested error message from v?.error?.data?.msg and v?.data?.msg
- Match code 14018 on parsed fields (error.data.code, code, error.code) or 'code':14018 to avoid false positives on requestId/timestamps
- Append EXHAUSTED_HINT explaining that WorkBuddy requires an active credit package even for 0-rate free models
- Return standard OpenAI-shaped { error: { message, type: 'insufficient_quota', code: 14018 } }
- Add tests in refused.test.mjs covering code 14018, error.data.msg extraction, and untouched pass-through of unrelated errors with 14018 in requestId

* workbuddy 0.1.10: match code 14018 whole, so a code that only begins with it (140185) passes through instead of resting the account as out of credit

---------

Co-authored-by: yetone <yetoneful@gmail.com>
This commit is contained in:
midas
2026-10-07 18:47:32 +08:00
committed by GitHub
co-authored by yetone
parent c2b0462227
commit ac3b697e5b
3 changed files with 93 additions and 8 deletions
+25 -6
View File
@@ -761,6 +761,15 @@ 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
// 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...").
const EXHAUSTED_HINT =
"WorkBuddy requires an active credit package even for 0-rate free models; check your account package balance"
// The code is matched whole: a code that only begins with 14018 (140185) is
// another error, and insufficient_quota would rest the account as out of credit.
const EXHAUSTED_TEXT = /credits exhausted|额度已用尽|["']code["']\s*:\s*["']?14018(?![\d.])/i
// kept is an answer that went through, saying the sign-in is kept: the
// built-in never cleared a WorkBuddy account's mark, as it never set one.
function kept(res) {
@@ -769,7 +778,7 @@ function kept(res) {
return new Response(res.body, { status: res.status, statusText: res.statusText, headers })
}
// explained is res with REFUSED_HINT added to that refusal's message.
// explained is res with REFUSED_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) {
@@ -781,27 +790,37 @@ async function explained(res) {
headers.set("X-Magpie-Sign-In", "kept")
return new Response(b, { status: res.status, statusText: res.statusText, headers })
}
if (!REFUSED.test(text)) return again(text)
let msg = text.trim()
let v
try {
v = JSON.parse(text)
} catch {}
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)
let msg = text.trim()
if (at && typeof at.message === "string") msg = at.message
else if (typeof at?.data?.msg === "string") msg = at.data.msg
else if (typeof v?.error === "string") msg = v.error
else if (typeof v?.msg === "string" && v.msg) msg = v.msg // WorkBuddy's own {"code", "msg"}
msg = `${msg} — ${REFUSED_HINT}`
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
msg = `${msg} — ${hint}`
const headers = new Headers(res.headers)
headers.set("content-type", "application/json")
headers.delete("content-length")
headers.delete("content-encoding")
headers.set("X-Magpie-Sign-In", "kept")
return new Response(JSON.stringify({ error: { message: msg, type: "permission_error", code: null } }), { status: res.status, statusText: res.statusText, headers })
return new Response(JSON.stringify({ error: { message: msg, type, code: errCode } }), { status: res.status, statusText: res.statusText, headers })
}
export const WorkBuddyAuthPlugin = makePlugin(SITES.workbuddy)
export const WorkBuddyAIAuthPlugin = makePlugin(SITES["workbuddy-ai"])
// for tests
export const _internal = { withSystem, unflagged, usageOf, desktopHeld, explained, REFUSED_HINT, freeCredits, creditsOf, fresh, SITES, renewing, renewed }
export const _internal = { withSystem, unflagged, usageOf, desktopHeld, explained, REFUSED_HINT, EXHAUSTED_HINT, freeCredits, creditsOf, fresh, SITES, renewing, renewed }
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-workbuddy-auth",
"version": "0.1.9",
"version": "0.1.10",
"description": "OpenCode provider plugins for WorkBuddy's plans: WorkBuddy (CodeBuddy, copilot.tencent.com) and WorkBuddy AI (www.workbuddy.ai)",
"type": "module",
"main": "./index.mjs",
+67 -1
View File
@@ -4,7 +4,7 @@
import { expect, test } from "bun:test"
import { _internal } from "./index.mjs"
const { explained, REFUSED_HINT } = _internal
const { explained, REFUSED_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 }))
@@ -12,12 +12,78 @@ test("the refusal ends in the hint", async () => {
expect((await res.json()).error.message).toBe("Illegal API invocation from an unapproved channel — " + REFUSED_HINT)
})
test("credits exhausted error ends in the exhausted hint", async () => {
const res = await explained(
new Response(
JSON.stringify({
error: {
data: {
code: 14018,
msg: "Credits exhausted. Please visit the link below to purchase add-on packs and get more credits: https://www.codebuddy.ai/profile/usage ",
requestId: "test-req",
},
},
}),
{ status: 429 }
)
)
expect(res.status).toBe(429)
const body = await res.json()
expect(body.error.type).toBe("insufficient_quota")
expect(body.error.code).toBe(14018)
expect(body.error.message).toBe(
"Credits exhausted. Please visit the link below to purchase add-on packs and get more credits: https://www.codebuddy.ai/profile/usage — " +
EXHAUSTED_HINT
)
})
test("credits exhausted as {code, msg} says its msg, then the exhausted hint", async () => {
const res = await explained(
new Response(JSON.stringify({ code: 14018, msg: "Credits exhausted" }), { status: 429 })
)
expect(res.status).toBe(429)
const body = await res.json()
expect(body.error.type).toBe("insufficient_quota")
expect(body.error.code).toBe(14018)
expect(body.error.message).toBe("Credits exhausted — " + EXHAUSTED_HINT)
})
test("other errors pass as they came", async () => {
const res = await explained(new Response('{"error":{"message":"rate limited"}}', { status: 429 }))
expect(res.status).toBe(429)
expect(await res.text()).toBe('{"error":{"message":"rate limited"}}')
})
test("unrelated errors containing 14018 in requestId pass through untouched", async () => {
const timeout = '{"error":{"message":"upstream timeout","requestId":"req-1759614018"}}'
const res1 = await explained(new Response(timeout, { status: 504 }))
expect(res1.status).toBe(504)
expect(await res1.text()).toBe(timeout)
const notfound = '{"error":{"message":"model not found","requestId":"a1b2-14018-ff"}}'
const res2 = await explained(new Response(notfound, { status: 404 }))
expect(res2.status).toBe(404)
expect(await res2.text()).toBe(notfound)
})
test("a code that only begins with 14018 passes through untouched", async () => {
for (const body of ['{"error":{"code":140185,"message":"slow down"}}', '{"error":{"code":140185,"message":"slow do']) {
const res = await explained(new Response(body, { status: 429 }))
expect(res.status).toBe(429)
expect(await res.text()).toBe(body)
}
})
test("truncated body with code 14018 is explained", async () => {
const trunc = '{"error":{"data":{"code":14018,"msg":"Credits exh'
const res = await explained(new Response(trunc, { status: 429 }))
expect(res.status).toBe(429)
const body = await res.json()
expect(body.error.type).toBe("insufficient_quota")
expect(body.error.code).toBe(14018)
expect(body.error.message).toBe(trunc + " — " + EXHAUSTED_HINT)
})
test("a model of no credits is free, as magpie's built-in read them", () => {
const { freeCredits } = _internal
for (const c of ["x0.00", "0", 0, " X0 "]) expect(freeCredits(c)).toBe(true)