From 71021b8f7243bf79a9df8166ecf5152aebc9e46a Mon Sep 17 00:00:00 2001 From: yetone Date: Sat, 24 Oct 2026 16:37:39 +0800 Subject: [PATCH] zcode 0.1.25: a key with no GLM Coding Plan says a ZCode gift plan (Start Plan, 1024 Dev Day) needs ZCode's sign-in rtbeilin (X, 10-24) has ZCode 1024 Dev Day and no GLM Coding Plan. "ZCode app's sign-in" works in magpie; the browser sign-in and the API key don't. The key: ZCode's gift plans are served by zcode.z.ai to ZCode's session token (zcodejwttoken, the poll's `token`). A key carries none, so a key reaches api.z.ai's / open.bigmodel.cn's coding plan alone, and a gift-only account has nothing there. The card was empty, no error (usage.test's own "API key, BigModel's" case: an empty list and no limits gave {windows: []}), and each request got Z.ai's 1113 "no resource package" with nothing saying why. Now: - codingUsage: a subscription list read as having no VALID plan, with no level, no counted window and no saved plan, is the card's error NO_PLAN_KEY, which names the gift plans and says to add the account with "ZCode app's sign-in" or a ZCode browser sign-in. A list that can't be read keeps the card as it was. - the fetch: a coding-plan refusal >= 400 to a sign-in with no ZCode session, whose code is 1113 (error.code, error.type or the envelope's code), keeps its status and Z.ai's words and adds NO_PLAN_KEY. The body is read only after the request failed. A sign-in with a session and any other code go as they came. README says it under the key method. The browser sign-in: no cause found. 3.16.3's bundle (the current stable, downloaded from zcode.z.ai's release manifest) starts, polls and reads the flow as the plugin does: same init body, the same redirect / redirect_uri override, the same ready fields, Z.ai's token exchanged at /api/auth/z/login. yg6888's screenshot in the same thread shows two Dev Day accounts working in magpie. Not changed here. Run: - bun test packages/zcode, scratch HOME: 99 pass. The two new tests (usage "a key with no GLM Coding Plan...", signin "a key's 1113...") fail on 0.1.24's index.mjs. bun scripts/check.mjs zcode: ok. - Real: the owner's real Z.ai key through the new usage hook: card "GLM Coding Lite", 5 hours + Weekly, no error. A bogus key: card "Authentication Failed", a request 401 {"error":{"message": "Authentication Failed","type":"1000"}} unchanged. Not tried: a real key with no plan (none at hand), so the 1113 body is magpie's zhipu_coding_test fixture, not a captured one. BigModel's real answer neither. The browser sign-in with a Dev Day account. --- packages/zcode/README.md | 5 ++++ packages/zcode/index.mjs | 50 +++++++++++++++++++++++++++++++++- packages/zcode/package.json | 2 +- packages/zcode/signin.test.mjs | 33 ++++++++++++++++++++++ packages/zcode/usage.test.mjs | 26 ++++++++++++++++-- 5 files changed, 112 insertions(+), 4 deletions(-) diff --git a/packages/zcode/README.md b/packages/zcode/README.md index 3fa6638..5bbaabd 100644 --- a/packages/zcode/README.md +++ b/packages/zcode/README.md @@ -53,6 +53,11 @@ over Anthropic's Messages API. Provider id: `zcode`. another's credentials: to use both, add the app's sign-in once for each. - **GLM Coding Plan API key** - Paste a key (`.`) and say which site it is from. + - A key reaches the GLM Coding Plan alone. ZCode's gift plans (the + Start Plan, ZCode 1024 Dev Day) are served to ZCode's own session, + which a key doesn't carry: for an account with only a gift plan, the + card and each refused request (1113) say to sign in with ZCode + instead. ## Where the sign-in is kept diff --git a/packages/zcode/index.mjs b/packages/zcode/index.mjs index 3bf0045..95dbd10 100644 --- a/packages/zcode/index.mjs +++ b/packages/zcode/index.mjs @@ -437,14 +437,54 @@ async function codingUsage(s) { if (level) out.plan = "GLM Coding " + level[0].toUpperCase() + level.slice(1) out.windows = limitWindows(d) const resets = personalResets(root, s.key) + let subs = null try { - Object.assign(out, termOf(await call("GET", root + "/api/biz/subscription/list", { auth: s.key }))) + subs = await call("GET", root + "/api/biz/subscription/list", { auth: s.key }) + Object.assign(out, termOf(subs)) } catch {} const r = await resets if (r) out.resets = r + // a key whose account has no plan in force, read as such, says so + // rather than showing an empty card: what a key can spend is a GLM + // Coding Plan alone (a ZCode gift plan is never one, see NO_PLAN_KEY). + // A list that couldn't be read, a level, a counted window or a plan + // the sign-in saved keeps the card as it was. + const valid = (Array.isArray(subs) ? subs : []).some((x) => String(x?.status ?? "").toUpperCase() === "VALID") + if (subs !== null && !valid && !level && !s.plan && !out.windows.some((w) => !w.aside)) return { error: NO_PLAN_KEY } return out } +// NO_PLAN_KEY is a key with no GLM Coding Plan behind it. ZCode's gift +// plans (its Start Plan, ZCode 1024 Dev Day) are served by zcode.z.ai +// to ZCode's own session token, which a sign-in with ZCode (the app's, +// or Z.ai's or BigModel's in the browser) carries and a key never does: +// with a key, a gift-only account has nothing to spend, and Z.ai answers +// each request 1113 (rtbeilin on X, 10-24). +const NO_PLAN_KEY = + "this key's account has no GLM Coding Plan; ZCode's gift plans (the Start Plan, ZCode 1024 Dev Day) are spent only with ZCode's sign-in, never with a key — add the account with \"ZCode app's sign-in\" or a ZCode sign-in in the browser" + +// noPlanKey is a coding plan's 1113 refusal (no plan, no resource +// package) to a sign-in with no ZCode session, its message followed by +// NO_PLAN_KEY; anything else goes on as it came. Read only once the +// answer has failed. +function noPlanKey(res, text) { + let v + try { + v = JSON.parse(text) + } catch { + return null + } + // Z.ai names the code in error.code ("1113", as magpie's + // zhipu_coding_test has it) or error.type (its 401's "1000"), BigModel's + // envelope in code + if (![v?.error?.code, v?.error?.type, v?.code].some((c) => String(c ?? "") === "1113")) return null + const said = first(v?.error?.message, v?.msg, v?.message) + const out = v?.error && typeof v.error === "object" ? { ...v, error: { ...v.error, message: (said ? said + " — " : "") + NO_PLAN_KEY } } : { ...v, msg: (said ? said + " — " : "") + NO_PLAN_KEY } + const h = new Headers(res.headers) + h.set("content-type", "application/json") + return new Response(JSON.stringify(out), { status: res.status, statusText: res.statusText, headers: h }) +} + // startNum reads a number the balance gives as a number or a string. function startNum(v) { if (isNum(v)) return v @@ -1840,6 +1880,14 @@ export async function ZCodeAuthPlugin({ client }) { if (startRefused(res.status, text)) 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 + // ZCode gift plan can't be had with a key (NO_PLAN_KEY) + if (!start && !s.jwt && res.status >= 400) { + const text = await res.text() + const why = noPlanKey(res, text) + if (why) return done(kept(why)) + res = new Response(text, { status: res.status, statusText: res.statusText, headers: res.headers }) + } // a trial entry's gift answer that can't serve the request is // the agent's answer, not the coding plan's: the user picked the // gift, so a spent bucket says 429 (its quota error inside a 200 diff --git a/packages/zcode/package.json b/packages/zcode/package.json index 46a2099..0addd84 100644 --- a/packages/zcode/package.json +++ b/packages/zcode/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-zcode-auth", - "version": "0.1.24", + "version": "0.1.25", "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", diff --git a/packages/zcode/signin.test.mjs b/packages/zcode/signin.test.mjs index d7bb811..7ad01c4 100644 --- a/packages/zcode/signin.test.mjs +++ b/packages/zcode/signin.test.mjs @@ -48,3 +48,36 @@ test("a Start Plan sign-in past its end fails as errZCodeExpired", async () => { const auth = { type: "oauth", access: s.jwt, refresh: JSON.stringify(s), expires: 0 } await expect(send(auth, () => new Response(""))).rejects.toThrow("ZCode's sign-in has expired; sign in to ZCode again (or add the account again in magpie)") }) + +// rtbeilin (X, 10-24): a key whose account has only ZCode's gift plan is +// refused 1113 by the coding plan; the answer says why, status kept +test("a key's 1113 says a ZCode gift plan needs ZCode's sign-in", async () => { + const body = { error: { code: "1113", message: "Insufficient balance or no resource package. Please recharge." } } + const res = await send(key, () => Response.json(body, { status: 429 })) + expect(res.status).toBe(429) + expect(res.headers.get("X-Magpie-Sign-In")).toBe("kept") + const got = await res.json() + expect(got.error.code).toBe("1113") + expect(got.error.message).toStartWith("Insufficient balance or no resource package. Please recharge. — this key's account has no GLM Coding Plan;") + expect(got.error.message).toContain("ZCode app's sign-in") + // the code as Z.ai's 401 names its own (error.type), and in BigModel's envelope + const typed = await send(key, () => Response.json({ error: { type: "1113", message: "no package" } }, { status: 429 })) + expect((await typed.json()).error.message).toStartWith("no package — this key's account has no GLM Coding Plan;") + const env = await send(key, () => Response.json({ code: 1113, msg: "余额不足", success: false }, { status: 429 })) + expect((await env.json()).msg).toStartWith("余额不足 — this key's account has no GLM Coding Plan;") + // another refusal, and a ZCode sign-in's 1113, go as they came + const other = await send(key, () => Response.json({ error: { code: "1302", message: "rate" } }, { status: 429 })) + expect(await other.json()).toEqual({ error: { code: "1302", message: "rate" } }) + const s = { site: "zai", key: "one.secret", jwt: jwt(Math.floor(Date.now() / 1000) + 3600) } + const withJwt = { type: "oauth", access: s.key, refresh: JSON.stringify(s), expires: 0 } + globalThis.fetch = async (u) => { + // the plan list says a plan, so the request stays on the coding plan + if (String(u).includes("/subscription/list")) return Response.json({ code: 200, data: [{ productName: "GLM Coding Lite", status: "VALID" }] }) + if (String(u).includes("/billing/balance")) return Response.json({ code: 0, data: { plans: [], balances: [] } }) + return Response.json(body, { status: 429 }) + } + const hooks = await ZCodeAuthPlugin({ client: { auth: { set: async () => {} } } }) + const opts = await hooks.auth.loader(async () => withJwt, { id: "zcode" }) + const own = await opts.fetch("https://api.z.ai/api/anthropic/v1/messages", { method: "POST", body: "{}" }) + expect(await own.json()).toEqual(body) +}) diff --git a/packages/zcode/usage.test.mjs b/packages/zcode/usage.test.mjs index 4bdec23..37274c3 100644 --- a/packages/zcode/usage.test.mjs +++ b/packages/zcode/usage.test.mjs @@ -63,11 +63,33 @@ test("a Coding Plan's five hours and week", async () => { }) expect(calls.every((c) => c.url.origin === "https://api.z.ai")).toBe(true) // an API key, BigModel's, is asked on its own site - const bm = serve(({ url }) => (url.pathname.endsWith("/limit") ? ok({ limits: [] }) : ok([]))) - expect(await usage({ type: "api", key: "k", metadata: { site: "bigmodel" } })).toEqual({ signIn: "kept", windows: [] }) + const bm = serve(({ url }) => (url.pathname.endsWith("/limit") ? ok({ level: "lite", limits: [] }) : ok([{ productName: "GLM Coding Lite", status: "VALID" }]))) + expect(await usage({ type: "api", key: "k", metadata: { site: "bigmodel" } })).toEqual({ signIn: "kept", plan: "GLM Coding Lite", windows: [] }) expect(bm[0].url.origin).toBe("https://open.bigmodel.cn") }) +// rtbeilin (X, 10-24): ZCode 1024 Dev Day, no GLM Coding Plan, "API Key +// doesn't work". The gift is ZCode's session's alone; a key's card says +// so instead of showing nothing +test("a key with no GLM Coding Plan says a ZCode gift plan needs ZCode's sign-in", async () => { + const none = /no GLM Coding Plan; ZCode's gift plans .* are spent only with ZCode's sign-in, never with a key/ + for (const site of ["zai", "bigmodel"]) { + serve(({ url }) => (url.pathname.endsWith("/limit") ? ok({ limits: [] }) : ok([]))) + expect((await usage({ type: "api", key: "k", metadata: { site } })).error).toMatch(none) + // a subscription that ran out is no plan in force either + serve(({ url }) => (url.pathname.endsWith("/limit") ? ok({ limits: [] }) : ok([{ productName: "GLM Coding Lite", status: "EXPIRED" }]))) + expect((await usage({ type: "api", key: "k", metadata: { site } })).error).toMatch(none) + } + // a list that couldn't be read is not "no plan" + serve(({ url }) => (url.pathname.endsWith("/limit") ? ok({ limits: [] }) : new Response("", { status: 500 }))) + expect(await usage({ type: "api", key: "k", metadata: { site: "zai" } })).toEqual({ signIn: "kept", windows: [] }) + // a counted window or a level is a plan, whatever the list says + serve(({ url }) => (url.pathname.endsWith("/limit") ? ok({ limits: [{ unit: 3, number: 5, percentage: 3 }] }) : ok([]))) + expect((await usage({ type: "api", key: "k", metadata: { site: "zai" } })).error).toBeUndefined() + serve(({ url }) => (url.pathname.endsWith("/limit") ? ok({ level: "pro", limits: [] }) : ok([]))) + expect((await usage({ type: "api", key: "k", metadata: { site: "zai" } })).error).toBeUndefined() +}) + test("windows, names and terms as Go reads them", () => { const ws = _internal.limitWindows({ limits: [ { unit: 3, usage: 1000, currentValue: 420, percentage: 42 }, // the percentage stands