diff --git a/packages/zcode/README.md b/packages/zcode/README.md index c6f79fc..61f8355 100644 --- a/packages/zcode/README.md +++ b/packages/zcode/README.md @@ -83,7 +83,11 @@ the ZCode app's sign-in (`refresh` then holds `"source": "zcode"`). ## How requests are routed Before each request, the plugin checks which plan the account is on, and -asks again every 10 minutes: +asks again every 10 minutes, and with every read of the usage card, so a +plan bought, a gift claimed or a plan lapsed reaches the requests when the +card shows it. A key whose plan list once reads no plan after reading one +is asked again after a minute, and a Start Plan refusal on such a key reads +the plans again on the next request: - If the key's account has a GLM Coding Plan (`/api/biz/subscription/list`), its requests go to that plan. A model diff --git a/packages/zcode/dual.test.mjs b/packages/zcode/dual.test.mjs index 248ee67..c14a2fe 100644 --- a/packages/zcode/dual.test.mjs +++ b/packages/zcode/dual.test.mjs @@ -393,3 +393,52 @@ test("a gift-only account sends every request to the gift, and reads no balance expect(sent.at(-1).origin).toBe("https://zcode.z.ai") expect(sent.filter((c) => c.path === "/api/v1/zcode-plan/billing/balance").length).toBe(0) }) + +// #369: a GLM Coding Plan account whose plan list read no plan once was sent +// to the Start Plan for ten minutes, and every request answered 405 +test("a key's plan list that once reads no plan after reading one is asked again in a minute", async () => { + answers["/api/biz/subscription/list"] = ok([{ productName: "GLM Coding Lite", status: "VALID" }]) + answers["/api/v1/zcode-plan/billing/balance"] = ok(balance) + answers["/api/anthropic/v1/messages"] = ok({ type: "message", content: [] }) + answers["/api/v1/zcode-plan/anthropic/v1/messages"] = ok({ type: "message", content: [] }) + await sendTo(MSG, body("GLM-5.3")) + const id = KEY + "\0" + jwt + expect(_internal.routes.get(id)).toMatchObject({ start: false, ttl: 600_000 }) + _internal.routes.get(id).at = 0 // the ten minutes are up + answers["/api/biz/subscription/list"] = ok([]) + await sendTo(MSG, body("GLM-5.3")) + expect(_internal.routes.get(id)).toMatchObject({ start: true, ttl: 60_000 }) + // read the same way again, it is known: ten minutes + _internal.routes.get(id).at = 0 + await sendTo(MSG, body("GLM-5.3")) + expect(_internal.routes.get(id)).toMatchObject({ start: true, ttl: 600_000 }) +}) + +test("a Start Plan refusal on a key account reads its plans again on the next request", async () => { + answers["/api/biz/subscription/list"] = ok([{ productName: "GLM Coding Lite", status: "VALID" }]) + answers["/api/v1/zcode-plan/billing/balance"] = ok(balance) + answers["/api/anthropic/v1/messages"] = ok({ type: "message", content: [] }) + answers["/api/v1/zcode-plan/anthropic/v1/messages"] = new Response(JSON.stringify({ error: { message: "Method Not Allowed" } }), { status: 405 }) + const id = KEY + "\0" + jwt + _internal.routes.set(id, { start: true, gift: null, at: Date.now(), ttl: 600_000 }) + let res = await sendTo(MSG, body("GLM-5.3")) + expect(res.status).toBe(405) + expect(sent.at(-1).origin).toBe("https://zcode.z.ai") + res = await sendTo(MSG, body("GLM-5.3")) + expect(res.status).toBe(200) + expect(sent.at(-1).origin).toBe("https://api.z.ai") + expect(sent.at(-1).headers["x-api-key"]).toBe(KEY) +}) + +// #379: a plan bought, a gift claimed or a plan lapsed reaches the requests +// when the card shows it +test("a card read reads the route again", async () => { + answers["/api/biz/subscription/list"] = ok([{ productName: "GLM Coding Pro", status: "VALID" }]) + answers["/api/v1/zcode-plan/billing/balance"] = ok(balance) + answers["/api/monitor/usage/quota/limit"] = ok({ limits: [{ type: "CREDIT_LIMIT", unit: 6, number: 1, percentage: 10 }] }) + const id = KEY + "\0" + jwt + _internal.routes.set(id, { start: true, gift: null, at: Date.now(), ttl: 600_000 }) + const u = await (await hooks()).usage(async () => coding, { id: "zcode" }) + expect(u.windows[0].used).toBe(10) + expect(_internal.routes.get(id)).toMatchObject({ start: false, gift: { models: ["glm-5.3-flash"] } }) +}) diff --git a/packages/zcode/index.mjs b/packages/zcode/index.mjs index 0274bc0..4cf0150 100644 --- a/packages/zcode/index.mjs +++ b/packages/zcode/index.mjs @@ -224,7 +224,12 @@ async function plansOf(s) { gift = await startPlan(s.jwt, s.device).catch(() => ((failed = !jwtExpired(s.jwt)), null)) sure = !failed } - if (start) sure = true + // a key whose plan list read no plan, where the last read found one, + // is asked again in a minute rather than sent to the Start Plan for + // ten: one list without the plan (a renewal's boundary, a hiccup) + // turned a GLM Coding Plan account's every request into the Start + // Plan's 405 until the route was read again (#369) + if (start) sure = !(s.key && r && !r.start) } catch { // the plan list could not be read: a key account takes the dual route // (start stays false, the gift is kept, a minute's ttl), so a gift @@ -1916,7 +1921,13 @@ export async function ZCodeAuthPlugin({ client }) { // it: the account blocked, or the network if (start && res.status >= 400) { const text = await res.text() - if (startRefused(res.status, text)) return kept(startBlocked(res, text)) + if (startRefused(res.status, text)) { + // a key account sent to the Start Plan (its plan list read + // none) reads its plans again on the next request, so a + // coding plan that is there after all is used (#369) + if (s.key && !forcedGift) routes.delete(s.key + "\0" + s.jwt) + return kept(startBlocked(res, text)) + } res = new Response(text, { status: res.status, statusText: res.statusText, headers: res.headers }) } // a key with no plan behind it: what Z.ai said, and why a @@ -1972,6 +1983,10 @@ export async function ZCodeAuthPlugin({ client }) { return out } try { + // a card read is a fresh read: the route is read again with it, + // so a plan bought, a gift claimed or a plan lapsed reaches the + // requests when the card shows it, not ten minutes later (#379) + routes.delete(s.key + "\0" + s.jwt) const { start, gift } = await plansOf(s) let card if (isTeam(s)) { diff --git a/packages/zcode/package.json b/packages/zcode/package.json index 8fbb2ec..c040af6 100644 --- a/packages/zcode/package.json +++ b/packages/zcode/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-zcode-auth", - "version": "0.1.27", + "version": "0.1.28", "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", "type": "module", "main": "./index.mjs",