commandcode 0.1.4: a Go account's failure reads as the built-in's cmdFailure says it — an empty reply is its status's name as Go's http.StatusText gives it (an empty 429 "Too Many Requests", which magpie's quotaWords know), where it was "HTTP 429"; credits run out are said as "out of credits: …" unless the message is already in quotaWords' words ("Monthly limit hit" is not, "余额不足" is), where a looser test of its own kept "limit" and prefixed Chinese; an error object without a message is its JSON. errors.test.mjs fails without the change.

This commit is contained in:
yetone
2026-10-01 03:44:06 +08:00
parent e39f4f4dc0
commit f62d7d1057
3 changed files with 59 additions and 4 deletions
+10 -3
View File
@@ -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
}