From f62d7d10572e69ee87520bc055313459075563ab Mon Sep 17 00:00:00 2001 From: yetone Date: Thu, 1 Oct 2026 03:44:03 +0800 Subject: [PATCH] =?UTF-8?q?commandcode=200.1.4:=20a=20Go=20account's=20fai?= =?UTF-8?q?lure=20reads=20as=20the=20built-in's=20cmdFailure=20says=20it?= =?UTF-8?q?=20=E2=80=94=20an=20empty=20reply=20is=20its=20status's=20name?= =?UTF-8?q?=20as=20Go's=20http.StatusText=20gives=20it=20(an=20empty=20429?= =?UTF-8?q?=20"Too=20Many=20Requests",=20which=20magpie's=20quotaWords=20k?= =?UTF-8?q?now),=20where=20it=20was=20"HTTP=20429";=20credits=20run=20out?= =?UTF-8?q?=20are=20said=20as=20"out=20of=20credits:=20=E2=80=A6"=20unless?= =?UTF-8?q?=20the=20message=20is=20already=20in=20quotaWords'=20words=20("?= =?UTF-8?q?Monthly=20limit=20hit"=20is=20not,=20"=E4=BD=99=E9=A2=9D?= =?UTF-8?q?=E4=B8=8D=E8=B6=B3"=20is),=20where=20a=20looser=20test=20of=20i?= =?UTF-8?q?ts=20own=20kept=20"limit"=20and=20prefixed=20Chinese;=20an=20er?= =?UTF-8?q?ror=20object=20without=20a=20message=20is=20its=20JSON.=20error?= =?UTF-8?q?s.test.mjs=20fails=20without=20the=20change.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/commandcode/errors.test.mjs | 48 ++++++++++++++++++++++++++++ packages/commandcode/index.mjs | 13 ++++++-- packages/commandcode/package.json | 2 +- 3 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 packages/commandcode/errors.test.mjs diff --git a/packages/commandcode/errors.test.mjs b/packages/commandcode/errors.test.mjs new file mode 100644 index 0000000..9a771d1 --- /dev/null +++ b/packages/commandcode/errors.test.mjs @@ -0,0 +1,48 @@ +// A Go account's failures read as magpie's built-in Command Code account +// answers them (internal/gateway/commandcode.go, cmdFailure), so magpie's +// gateway moves on to the next account or member just as it did. +import { afterEach, expect, test } from "bun:test" +import { CommandCodePlugin, _internal } from "./index.mjs" + +const real = globalThis.fetch +afterEach(() => { + globalThis.fetch = real + _internal.subsSeen.clear() +}) + +// ask sends a chat completion as a Go account, /alpha/generate answering +// with reply. +async function ask(reply, stream = false) { + globalThis.fetch = async (url) => { + const path = new URL(String(url)).pathname + if (path === "/alpha/generate") return reply() + return new Response("", { status: 404 }) + } + const auth = { type: "api", key: "go-key", metadata: { plan: "Go" } } + const hooks = await CommandCodePlugin() + const opts = await hooks.auth.loader(async () => auth) + const res = await opts.fetch("https://api.commandcode.ai/provider/v1/chat/completions", { + method: "POST", + body: JSON.stringify({ model: "deepseek/deepseek-v4-pro", stream, messages: [{ role: "user", content: "hi" }] }), + }) + return { status: res.status, message: (await res.json()).error?.message } +} + +test("an empty 429 reads as Go's http.StatusText says it, which quotaWords knows", async () => { + expect(await ask(() => new Response("", { status: 429 }))).toEqual({ status: 429, message: "Too Many Requests" }) +}) + +test("credits out whose words aren't quotaWords' say so, as cmdFailure does", async () => { + const r = await ask(() => Response.json({ error: { type: "payment", message: "PREMIUM_CREDITS_EXHAUSTED: Monthly limit hit" } }, { status: 403 })) + expect(r).toEqual({ status: 402, message: "out of credits: Monthly limit hit" }) +}) + +test("credits out already in quotaWords' words are left as they are", async () => { + const r = await ask(() => Response.json({ error: { message: "PREMIUM_CREDITS_EXHAUSTED: 余额不足" } }, { status: 400 })) + expect(r).toEqual({ status: 402, message: "余额不足" }) +}) + +test("an error line before any content keeps its status", async () => { + const line = JSON.stringify({ type: "error", error: { message: '429 {"error":{"message":"RATE_LIMITED: slow down"}}' } }) + expect(await ask(() => new Response(line + "\n"), true)).toEqual({ status: 429, message: "RATE_LIMITED: slow down" }) +}) diff --git a/packages/commandcode/index.mjs b/packages/commandcode/index.mjs index 9a14a04..bbb617c 100644 --- a/packages/commandcode/index.mjs +++ b/packages/commandcode/index.mjs @@ -667,6 +667,10 @@ const generateHeaders = (key) => ({ Authorization: `Bearer ${key}`, }) +// QUOTA_WORDS are magpie's quotaWords (internal/gateway/fallback.go): a +// message already in them isn't said again as out of credits. +const QUOTA_WORDS = /quota|insufficient|balance|credit|billing|exceeded|rate.?limit|usage.?limit|limit.?reached|hit your .*limit|limit.{0,24}resets|too many requests|overloaded|余额|额度|欠费|限流|频率|套餐|用量|上限/i + // failure is the status and message for a failure, from Command Code's // {"error":{"type","message"}} or its text: a model the plan hasn't is a // 403, credits run out a 402, a window's limit a 429. @@ -674,9 +678,12 @@ function failure(status, text) { let msg = "" try { const j = JSON.parse(text) - msg = [j?.error?.message, j?.message, typeof j?.error === "string" ? j.error : ""].find((s) => s && String(s).trim()) ?? "" + const err = j?.error && typeof j.error === "object" ? JSON.stringify(j.error) : j?.error + msg = [j?.error?.message, j?.message, err].find((s) => s != null && String(s).trim()) ?? "" } catch {} - msg = String(msg || text || "").trim() || `HTTP ${status}` + // an empty body is its status's name, as Go's http.StatusText says it: + // an empty 429 reads "Too Many Requests", which quotaWords knows + msg = String(msg || text || "").trim() || statusText(status) if (msg.length > 600) msg = msg.slice(0, 600) const low = msg.toLowerCase() if (msg.includes("MODEL_NOT_IN_PLAN:")) { @@ -686,7 +693,7 @@ function failure(status, text) { else if (low.includes("premium_credits_exhausted") || low.includes("insufficient credits")) { status = 402 msg = msg.replace("PREMIUM_CREDITS_EXHAUSTED:", "").trim() - if (!/credit|quota|balance|limit/i.test(msg)) msg = "out of credits: " + msg + if (!QUOTA_WORDS.test(msg)) msg = "out of credits: " + msg } else if (msg.includes("RATE_LIMITED") || low.includes("usage limit") || low.includes("window_limit")) { if (status < 400 || status === 500) status = 429 } diff --git a/packages/commandcode/package.json b/packages/commandcode/package.json index b8e6365..919eaa3 100644 --- a/packages/commandcode/package.json +++ b/packages/commandcode/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-commandcode-auth", - "version": "0.1.3", + "version": "0.1.4", "description": "Command Code plans (Pro, GOAT, Max, Ultra, Go, Teams) in OpenCode and magpie", "type": "module", "main": "./index.mjs",