* 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>
109 lines
4.9 KiB
JavaScript
109 lines
4.9 KiB
JavaScript
// WorkBuddy's refusal of Codex's and Claude Code's system prompt carries
|
|
// the hint magpie's built-in added (provider.WBRefusedHint), at the end of
|
|
// the message, where magpie's routing page looks for it.
|
|
import { expect, test } from "bun:test"
|
|
import { _internal } from "./index.mjs"
|
|
|
|
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 }))
|
|
expect(res.status).toBe(403)
|
|
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)
|
|
for (const c of ["x0.03", "", null, undefined, "free", 1]) expect(freeCredits(c)).toBe(false)
|
|
})
|
|
|
|
// wbExplain's refusal in WorkBuddy's own envelope reads its msg
|
|
test("a refusal as {code, msg} says its msg, then the hint", async () => {
|
|
const res = await explained(new Response(JSON.stringify({ code: 11004, msg: "Illegal API invocation from an unapproved channel" }), { status: 403 }))
|
|
expect(res.status).toBe(403)
|
|
expect((await res.json()).error.message).toBe("Illegal API invocation from an unapproved channel — " + REFUSED_HINT)
|
|
})
|
|
|
|
// a body handed over as bytes still gets WorkBuddy's system message
|
|
test("a chat body in bytes is read, and given the system message", () => {
|
|
const { withSystem } = _internal
|
|
const chat = { model: "m", messages: [{ role: "user", content: "hi" }] }
|
|
const want = { model: "m", messages: [{ role: "system", content: "You are a helpful assistant." }, { role: "user", content: "hi" }] }
|
|
for (const b of [Buffer.from(JSON.stringify(chat)), new TextEncoder().encode(JSON.stringify(chat)), new TextEncoder().encode(JSON.stringify(chat)).buffer])
|
|
expect(JSON.parse(withSystem(b))).toEqual(want)
|
|
expect(JSON.parse(withSystem(JSON.stringify(chat)))).toEqual(want)
|
|
})
|