diff --git a/packages/zcode/dual.test.mjs b/packages/zcode/dual.test.mjs index 89294ad..8b9f6c5 100644 --- a/packages/zcode/dual.test.mjs +++ b/packages/zcode/dual.test.mjs @@ -230,6 +230,19 @@ test("a gift 200 that says the quota is spent is answered from the plan too", as expect(sent.filter((c) => c.origin === "https://api.z.ai" && c.path.endsWith("/messages"))).toHaveLength(1) }) +// the plan free first falls back on, itself spent inside a 200, says 429 +// as a plain request's plan does (plugins#817) +test("free first's plan answer that says it is spent inside a 200 says 429", async () => { + planAndBalance() + answers["/api/v1/zcode-plan/anthropic/v1/messages"] = new Response(JSON.stringify({ code: 1005, msg: "exceed quota limit" }), { headers: { "content-type": "application/json" } }) + answers["/api/anthropic/v1/messages"] = new Response(JSON.stringify({ code: 1113, msg: "Insufficient balance or no resource package. Please recharge." }), { headers: { "content-type": "application/json" } }) + const opts = await freePlugin({ freeFirst: true }).then((h) => h.loader(async () => coding, { id: "zcode" })) + const res = await opts.fetch(MSG, { method: "POST", headers: { "content-type": "application/json" }, body: body("GLM-5.3-Flash") }) + expect(res.status).toBe(429) + expect((await res.json()).code).toBe(1113) + expect(sent.filter((c) => c.origin === "https://api.z.ai" && c.path.endsWith("/messages"))).toHaveLength(1) +}) + test("after the minute's rest the plan keeps serving, inside the route's ten", async () => { planAndBalance() answers["/api/v1/zcode-plan/anthropic/v1/messages"] = new Response(JSON.stringify({ error: { message: "Insufficient balance or no resource package", code: 1113 } }), { status: 429, headers: { "content-type": "application/json" } }) diff --git a/packages/zcode/gift.test.mjs b/packages/zcode/gift.test.mjs index b90b399..65c7ea2 100644 --- a/packages/zcode/gift.test.mjs +++ b/packages/zcode/gift.test.mjs @@ -14,13 +14,14 @@ const MSG = "https://api.z.ai/api/anthropic/v1/messages" const ok = (data) => new Response(JSON.stringify({ code: 0, data }), { headers: { "content-type": "application/json" } }) const real = globalThis.fetch const sent = [] -let balance, paid, giftReply +let balance, paid, giftReply, planReply beforeEach(() => { _internal.routes.clear() _internal.blocked.clear() sent.length = 0 paid = 100 + planReply = null giftReply = new Response(JSON.stringify({ type: "message", content: [] }), { headers: { "content-type": "application/json" } }) balance = { server_time: now, @@ -35,7 +36,7 @@ beforeEach(() => { if (url.pathname.endsWith("/messages")) { const text = init.body ?? (input instanceof Request ? await input.text() : undefined) sent.push({ origin: url.origin, path: url.pathname, body: JSON.parse(text), headers: new Headers(init.headers ?? (input instanceof Request ? input.headers : {})) }) - return url.origin === "https://zcode.z.ai" ? giftReply.clone() : ok({ type: "message", content: [] }) + return url.origin === "https://zcode.z.ai" ? giftReply.clone() : planReply ? planReply.clone() : ok({ type: "message", content: [] }) } throw new Error("unmocked request blocked: " + url.pathname) } @@ -115,6 +116,37 @@ test("a gift quota error inside HTTP 200 becomes 429 without paid fallback", asy expect(sent[0].origin).toBe("https://zcode.z.ai") }) +// plugins#817 (zane668): a gift-only account's own model, not a -Trial +// entry, out of its bucket for the day is answered 200 "exceed quota +// limit" as the trial entry's is. It says 429 too, its words kept, so +// magpie rests that model and not the account, whose other buckets +// (GLM-5.3-Flash's) still serve. +test("a gift-only account's plain model spent inside HTTP 200 becomes 429", async () => { + const only = { type: "oauth", access: jwt, refresh: JSON.stringify({ ...state, key: undefined, plan: undefined }), expires: 0 } + const said = { code: 1005, msg: "exceed quota limit", logid: "x" } + giftReply = new Response(JSON.stringify(said), { headers: { "content-type": "application/json" } }) + const res = await send(original, only) + expect(res.status).toBe(429) + expect(await res.json()).toEqual(said) + expect(sent).toHaveLength(1) + expect(sent[0]).toMatchObject({ origin: "https://zcode.z.ai", body: { model: original } }) + // an answer that is a reply stays the reply it was + giftReply = new Response(JSON.stringify({ type: "message", content: [{ type: "text", text: "1005" }] }), { headers: { "content-type": "application/json" } }) + const fine = await send(original, only) + expect(fine.status).toBe(200) + expect((await fine.json()).content[0].text).toBe("1005") +}) + +// the coding plan's own spent answer inside a 200 is said the same way +test("the coding plan's spent answer inside HTTP 200 becomes 429", async () => { + planReply = new Response(JSON.stringify({ code: 1113, msg: "Insufficient balance or no resource package. Please recharge." }), { headers: { "content-type": "application/json" } }) + const res = await send("glm-5.3") + expect(res.status).toBe(429) + expect((await res.json()).code).toBe(1113) + expect(sent).toHaveLength(1) + expect(sent[0].origin).toBe("https://api.z.ai") +}) + test("an exhausted gift alias is refused locally without a paid request", async () => { paid = 0 Object.assign(balance.balances[0], { used_units: 100, remaining_units: 0 }) diff --git a/packages/zcode/index.mjs b/packages/zcode/index.mjs index 37d0edb..28d0155 100644 --- a/packages/zcode/index.mjs +++ b/packages/zcode/index.mjs @@ -315,6 +315,19 @@ function spentUp(text, status) { return c1 === "1113" || c1 === "1005" || c2 === "1113" || c2 === "1005" } +// spentSaid is an answer that carries spentUp's code inside a 200, as a +// spent bucket's can, said as the 429 it is (plugins#817: a Start Plan +// account's GLM-5.3 out for the day answered 200 "exceed quota limit", +// which magpie took for a reply that did not stream and rested the whole +// account, GLM-5.3-Flash's own bucket with it). Any other answer goes on +// as it came; a stream's body is never read here. +async function spentSaid(res) { + if (res.status >= 400 || !(res.headers.get("content-type") ?? "").includes("application/json")) return res + const text = await res.text() + const status = spentUp(text, res.status) ? 429 : res.status + return new Response(text, { status, statusText: status === 429 ? "Too Many Requests" : res.statusText, headers: res.headers }) +} + // ---- keys ------------------------------------------------------------------------ function teamHeaders(base, org, project) { @@ -2708,7 +2721,7 @@ export async function ZCodeAuthPlugin({ client, magpie } = {}, options = {}) { if (EDGE_PAGE.test(text)) edge.until = Date.now() + EDGE_HOLD if (freeGift) { restModel(false) - return done(kept(await replayOnPlan())) + return done(kept(await spentSaid(await replayOnPlan()))) } if (s.key && !forcedGift) routes.delete(s.key + "\0" + s.jwt) return kept(startBlocked(res, text)) @@ -2756,18 +2769,20 @@ export async function ZCodeAuthPlugin({ client, magpie } = {}, options = {}) { const spent = !cap && spentUp(text, res.status) if (spent) { restModel(true) - return done(kept(await replayOnPlan())) + return done(kept(await spentSaid(await replayOnPlan()))) } if (res.status >= 400) { if (!cap && (res.status === 401 || res.status === 403)) routes.delete(s.key + "\0" + s.jwt) - return done(kept(await replayOnPlan())) + return done(kept(await spentSaid(await replayOnPlan()))) } return done(kept(res, text)) } } // the plan's answer goes on as it came, the account kept: the - // built-in never marked a ZCode account lapsed nor cleared one - return done(kept(res)) + // built-in never marked a ZCode account lapsed nor cleared one. + // A spent bucket's 200 says 429 (spentSaid), as a trial + // entry's does, so magpie rests the model, not the account + return done(kept(await spentSaid(res))) }, } }, diff --git a/packages/zcode/package.json b/packages/zcode/package.json index 868ae26..e076c15 100644 --- a/packages/zcode/package.json +++ b/packages/zcode/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-zcode-auth", - "version": "0.1.52", + "version": "0.1.53", "description": "OpenCode provider plugin: signs in to Z.ai's or BigModel's (智谱) GLM Coding Plan as ZCode does, including team seats and ZCode's Start Plan, and claims its gift plans daily", "type": "module", "main": "./opencode.mjs",