autoclaw 0.2.4: a 403 for a spent quota is a 429 with the gateway's words, not 登录已失效 (#274)
The CN managed gateway answers an exhausted free quota with
403 {"code":810000,"message":"GLM-5.3 免费额度已用完,开通连续包月会员继续使用"};
the reporter's same token got as far as "400 非法模型" with an unknown model,
so the sign-in is good. Both chat paths read every 401/403 as a dead
sign-in, so magpie showed 登录已失效 and sent the user to sign in again.
A 403 whose JSON body carries code 810000, or a message saying the quota or
balance is spent (额度, 余额不足, 用完, 用尽, quota, exhausted), now goes on as
a 429 with "AutoClaw: <the gateway's message>" and X-Magpie-Sign-In: kept. On
the overseas path such a 403 isn't asked of the second host. A 401, and any
other 403 (no body, another code), is still the sign-in, as before.
Run at this commit on origin/main 5ec8377: bun test in packages/autoclaw 28
pass 0 fail; bun scripts/check.mjs autoclaw ok. Broken by hand: with
0.2.3's index.mjs both new tests fail. The fixture is the reporter's body;
not tried against a live AutoClaw account (none here).
This commit is contained in:
@@ -86,6 +86,7 @@ AutoClaw 对话网关对请求形状有硬性要求,缺任一条都回 `406 No
|
||||
## 已知行为
|
||||
|
||||
- 手机号账号只认国内 host(走 managed gateway,401/403 标记登录失效);z.ai/Google 账号只认国际 host(401/403 自动换侧重试,均拒绝时标记失效)。
|
||||
- 额度用完不算登录失效:上游以 `403` + `{"code":810000,"message":"GLM-5.3 免费额度已用完,…"}` 回答时(或 403 正文说额度/余额已用完),插件转成 `429`,原样带上游文案,登录保留(`X-Magpie-Sign-In: kept`),国际版也不再换 host 重试(plugins#274)。
|
||||
- 上游模型白名单随版本变化,模型下线时上游返回 `400 非法模型`(原样透传错误消息)。
|
||||
- refresh token 与 AutoClaw 桌面端**共用会互踢**(双方都轮换),插件账号请勿同时在桌面端登录。
|
||||
- 上游偶发改协议时,`CLIENT_VERSION` / 端点路径可能需要随官方客户端更新。
|
||||
|
||||
@@ -111,6 +111,31 @@ async function explained(res) {
|
||||
);
|
||||
}
|
||||
|
||||
// A 403 is not always the sign-in: the CN managed gateway answers an
|
||||
// exhausted quota 403 {"code":810000,"message":"GLM-5.3 免费额度已用完,…"}
|
||||
// while the same token's next request with an unknown model gets as far as
|
||||
// "非法模型" (plugins#274). Such a refusal is the account's quota: it goes on
|
||||
// as a 429 with the gateway's own words, and the sign-in is kept.
|
||||
const QUOTA_CODES = new Set(["810000"]);
|
||||
const QUOTA_WORDS = /额度|余额不足|用完|用尽|quota|insufficient[_ ]?(balance|credit)|exhausted/i;
|
||||
|
||||
/** quotaRefusal is a 403 chat answer read as a spent quota, or null when it isn't one (a 401 or any other 403 is still the sign-in). */
|
||||
async function quotaRefusal(res) {
|
||||
if (res.status !== 403) return null;
|
||||
const text = await res.clone().text().catch(() => "");
|
||||
let d;
|
||||
try { d = JSON.parse(text); } catch { return null; }
|
||||
if (!d || typeof d !== "object") return null;
|
||||
const code = d.code ?? d.error?.code ?? null;
|
||||
const message = String(d.message ?? d.msg ?? d.error?.message ?? "").trim();
|
||||
if (!QUOTA_CODES.has(String(code)) && !(message && QUOTA_WORDS.test(message))) return null;
|
||||
try { res.body?.cancel?.(); } catch {}
|
||||
return new Response(JSON.stringify({ error: { message: `AutoClaw: ${message || "quota exhausted"}`, type: "rate_limit_error", code } }), {
|
||||
status: 429,
|
||||
headers: { "Content-Type": "application/json", "X-Magpie-Sign-In": "kept" },
|
||||
});
|
||||
}
|
||||
|
||||
function autoclawRewriteBody(rawBody) {
|
||||
let payload;
|
||||
try {
|
||||
@@ -625,7 +650,13 @@ function makeAutoclawPlugin({ providerId, providerName, homeHost, loginMethod })
|
||||
|
||||
const res1 = await tryFetch(hosts[0]);
|
||||
if (res1.status !== 401 && res1.status !== 403) return explained(res1);
|
||||
// a spent quota is the account's on every host: not asked again
|
||||
const spent1 = await quotaRefusal(res1);
|
||||
if (spent1) return spent1;
|
||||
try { res1.body?.cancel?.(); } catch {}
|
||||
const res2 = await tryFetch(hosts[1]);
|
||||
const spent2 = await quotaRefusal(res2);
|
||||
if (spent2) return spent2;
|
||||
if (res2.status === 401 || res2.status === 403) {
|
||||
try { res2.body?.cancel?.(); } catch {}
|
||||
return new Response(JSON.stringify({ error: { message: "AutoClaw 登录已失效(两个 host 均拒绝该 token)", type: "invalid_request_error" } }), {
|
||||
@@ -647,6 +678,8 @@ function makeAutoclawPlugin({ providerId, providerName, homeHost, loginMethod })
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
if (res.status !== 401 && res.status !== 403) return explained(res);
|
||||
const spent = await quotaRefusal(res);
|
||||
if (spent) return spent;
|
||||
try { res.body?.cancel?.(); } catch {}
|
||||
return new Response(JSON.stringify({ error: { message: "AutoClaw 登录已失效", type: "invalid_request_error" } }), {
|
||||
status: 401,
|
||||
@@ -1219,6 +1252,7 @@ export const AutoclawCnAuthPlugin = makeAutoclawPlugin({
|
||||
|
||||
/** 测试辅助(magpie/OpenCode 不会把它当插件调用)。 */
|
||||
export const _internal = {
|
||||
quotaRefusal,
|
||||
makePluginForTest: makeAutoclawPlugin,
|
||||
md5Hex, jwtClaim, tokenFields, codeValue, bizHeaders, bareToken, newDeviceId,
|
||||
dailySignin, signinStatus, maybeSignin, signinAccountKey, signinConfig, checkinOutcome, refreshTokens,
|
||||
|
||||
@@ -611,3 +611,64 @@ test("signinOnUsage signs every account, also two with the same name", async ()
|
||||
globalThis.fetch = realFetch
|
||||
}
|
||||
})
|
||||
|
||||
// plugins#274: the CN managed gateway answers a spent quota with a 403 whose
|
||||
// body says so (code 810000); the same token's next request gets as far as
|
||||
// the model check, so it is the quota, not the sign-in
|
||||
const SPENT = { code: 810000, message: "GLM-5.3 免费额度已用完,开通连续包月会员继续使用" }
|
||||
|
||||
test("CN chat: a 403 for a spent quota is a 429 with the gateway's words, and the sign-in is kept", async () => {
|
||||
const realFetch = globalThis.fetch
|
||||
let answer = () => Response.json(SPENT, { status: 403 })
|
||||
globalThis.fetch = async () => answer()
|
||||
try {
|
||||
const hooks = await AutoclawCnAuthPlugin({ client: { auth: { set: async () => {} } } })
|
||||
const loader = await hooks.auth.loader(async () => ({ type: "oauth", access: "a1" }))
|
||||
const body = JSON.stringify({ model: "zaicoding_glm-5.3", messages: [{ role: "user", content: "hi" }] })
|
||||
const spent = await loader.fetch("x", { method: "POST", body })
|
||||
expect(spent.status).toBe(429)
|
||||
expect(spent.headers.get("X-Magpie-Sign-In")).toBe("kept")
|
||||
const e = (await spent.json()).error
|
||||
expect(e.message).toBe("AutoClaw: GLM-5.3 免费额度已用完,开通连续包月会员继续使用")
|
||||
expect(e.code).toBe(810000)
|
||||
|
||||
// any other 403, and a 401, is still the sign-in
|
||||
for (const [status, raw] of [[403, ""], [403, '{"code":401,"message":"token invalid"}'], [401, JSON.stringify(SPENT)]]) {
|
||||
answer = () => new Response(raw, { status })
|
||||
const dead = await loader.fetch("x", { method: "POST", body })
|
||||
expect(dead.status).toBe(401)
|
||||
expect(dead.headers.get("X-Magpie-Sign-In")).toBe("expired")
|
||||
}
|
||||
} finally {
|
||||
globalThis.fetch = realFetch
|
||||
}
|
||||
})
|
||||
|
||||
test("overseas chat: a spent quota on the first host isn't asked of the second, and isn't the sign-in", async () => {
|
||||
const realFetch = globalThis.fetch
|
||||
const urls = []
|
||||
let answer = () => Response.json(SPENT, { status: 403 })
|
||||
globalThis.fetch = async (input) => {
|
||||
urls.push(String(input))
|
||||
return answer()
|
||||
}
|
||||
try {
|
||||
const hooks = await AutoclawAuthPlugin({ client: { auth: { set: async () => {} } } })
|
||||
const loader = await hooks.auth.loader(async () => ({ type: "oauth", access: "a1" }))
|
||||
const body = JSON.stringify({ model: "zai_glm-5.3-flash", messages: [{ role: "user", content: "hi" }] })
|
||||
const spent = await loader.fetch("x", { method: "POST", body })
|
||||
expect(spent.status).toBe(429)
|
||||
expect(spent.headers.get("X-Magpie-Sign-In")).toBe("kept")
|
||||
expect(urls.length).toBe(1)
|
||||
|
||||
// both hosts refusing the token is still the sign-in
|
||||
urls.length = 0
|
||||
answer = () => new Response("forbidden", { status: 403 })
|
||||
const dead = await loader.fetch("x", { method: "POST", body })
|
||||
expect(dead.status).toBe(401)
|
||||
expect(dead.headers.get("X-Magpie-Sign-In")).toBe("expired")
|
||||
expect(urls.length).toBe(2)
|
||||
} finally {
|
||||
globalThis.fetch = realFetch
|
||||
}
|
||||
})
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@magpie-community/opencode-autoclaw-auth",
|
||||
"version": "0.2.3",
|
||||
"version": "0.2.4",
|
||||
"description": "OpenCode provider plugin for AutoClaw (Zhipu OpenClaw): oversea web sign-in (Google/z.ai) and CN phone SMS, multi-account pool, credit balance and daily check-in",
|
||||
"type": "module",
|
||||
"main": "./index.mjs",
|
||||
|
||||
Reference in New Issue
Block a user