diff --git a/packages/zcode/README.md b/packages/zcode/README.md index e13a76a..f14a805 100644 --- a/packages/zcode/README.md +++ b/packages/zcode/README.md @@ -54,12 +54,42 @@ Before each request, the plugin checks which plan the account is on, and asks again every 10 minutes: - If the key's account has a GLM Coding Plan - (`/api/biz/subscription/list`), requests go to that plan. -- Otherwise they go to the Start Plan. + (`/api/biz/subscription/list`), its requests go to that plan — + except for a model the account's gift plans also serve, which goes to + the gift first (see below). +- Otherwise they go to the gift plans. Every request carries the key as both `x-api-key` and `Authorization: Bearer`. +**Spending the gift first.** An account with a GLM Coding Plan can also +hold gift plans from ZCode activities, ones that expire on a fixed date. +Their allowance would lapse unused if every request went to the coding +plan, so the plugin reads the gift's balance (per the sign-in's session, +kept for 10 minutes) and sends a request for a model the gift still has +quota to spend to the gift first, dressed as ZCode's own. A bucket is +judged spent only by its `remaining_units` — a missing one is unknown, +not spent — and a model whose every bucket says none goes straight to +the coding plan without asking. When the gift can't serve the request — +any answer 400 or over (its 405, its 401, its 5xx), or its quota error +(code 1113 "Insufficient balance or no resource package", code 1005 +"exceed quota limit", which it can answer inside an HTTP 200) — the +plugin replays the request once to the coding plan, as the agent sent +it. Only a quota answer keeps that model off the gift for a minute; a +refusal that says nothing about quota is asked again next request. Every +429 counts as spent, Z.ai's 1302/1303 rate limits among them. A stream's +body is never read or buffered on the way. + +## The usage card + +An account with both plans shows both allowances: the coding plan's +windows first, then the gift's buckets, each with its own name, models +and end. The card's plan and term stay the coding plan's. A gift read +that fails leaves just the coding card — magpie hides a card's windows +behind an error, so a gift hiccup must not black out a working Coding +Plan. A gift that goes out of date while requests use it simply stops +showing. + ## Models The models are the ones ZCode offers for the account's plan. They come diff --git a/packages/zcode/dual.test.mjs b/packages/zcode/dual.test.mjs new file mode 100644 index 0000000..3aec388 --- /dev/null +++ b/packages/zcode/dual.test.mjs @@ -0,0 +1,260 @@ +// A dual account (a GLM Coding Plan and gift plans) shows both allowances on +// its card and spends the gift first: a request for a model the gift serves +// goes to the Start Plan, dressed as ZCode's; when the gift says it is spent +// the same request is replayed once to the coding plan, undressed, and that +// model keeps to the coding plan for a minute. Anything else the gift says +// (its 405, its "unusual activity") stands as the answer. Nothing leaves the +// machine: the plugin's fetch is the fake here. +import { afterEach, beforeEach, expect, test } from "bun:test" +import { homedir, tmpdir } from "node:os" +import { realpathSync } from "node:fs" + +let ZCodeAuthPlugin, _internal +async function setup() { + if (![tmpdir(), realpathSync(tmpdir())].some((t) => homedir().startsWith(t))) throw new Error("run with HOME=$(mktemp -d) bun test") + ;({ ZCodeAuthPlugin, _internal } = await import("./index.mjs")) +} +await setup() + +const jwt = ["{}", JSON.stringify({ exp: Math.floor(Date.now() / 1000) + 3600 })].map((s) => Buffer.from(s).toString("base64url")).join(".") + ".sig" +const DEVICE = "11111111-2222-4333-8444-555555555555" +const KEY = "dual.secret" +const now = Math.trunc(Date.now() / 1000) +// a gift whose only live bucket serves glm-5.3-flash, a quarter used +const balance = { + server_time: now, + plans: [{ plan_id: "zai-start-plan", user_plan_id: "up1", name: "Start Plan", status: "active", ends_at: now + 7 * 86400, + entitlements: [{ entitlement_id: "e1", period: "daily" }] }], + balances: [{ plan_id: "zai-start-plan", user_plan_id: "up1", entitlement_id: "e1", show_name: "Trust Build", capabilities: ["model:glm-5.3-flash"], + total_units: 1000, used_units: 250, remaining_units: 750, expires_at: now + 3600 }], +} +const coding = { type: "oauth", access: KEY, refresh: JSON.stringify({ site: "zai", base: "https://api.z.ai/api/anthropic", key: KEY, jwt, device: DEVICE, plan: "GLM Coding Pro" }), expires: 0 } + +const sent = [] +const answers = {} +const real = globalThis.fetch +beforeEach(async () => { + _internal.routes.clear() + _internal.blocked.clear() + sent.length = 0 + for (const k of Object.keys(answers)) delete answers[k] + globalThis.fetch = async (input, init = {}) => { + const url = new URL(input instanceof Request ? input.url : String(input)) + const c = { path: url.pathname, origin: url.origin, method: init.method ?? (input instanceof Request ? input.method : "GET"), headers: Object.fromEntries(new Headers(init.headers ?? (input instanceof Request ? input.headers : {}))), body: init.body ?? await (input.body ? input.text() : undefined) } + sent.push(c) + const a = answers[c.path] + if (!a) return new Response("", { status: 404 }) + return a instanceof Response ? a.clone() : typeof a === "function" ? await a(c) : a.clone() + } +}) +afterEach(() => (globalThis.fetch = real)) +const ok = (data) => new Response(JSON.stringify({ code: 0, data })) + +function planAndBalance() { + answers["/api/biz/subscription/list"] = ok([{ productName: "GLM Coding Pro", status: "VALID" }]) + answers["/api/v1/zcode-plan/billing/balance"] = ok(balance) +} +const hooks = async () => (await ZCodeAuthPlugin({ client: { auth: { set: async () => {} } } })).auth +const loader = async () => (await hooks()).loader(async () => coding, { id: "zcode" }) +async function sendTo(url, body) { + const opts = await loader() + return opts.fetch(url, { method: "POST", headers: { "content-type": "application/json", "x-api-key": "zcode", "anthropic-version": "2023-06-01" }, body }) +} +const MSG = "https://api.z.ai/api/anthropic/v1/messages" +const body = (model) => JSON.stringify({ model, max_tokens: 100, messages: [{ role: "user", content: "hi" }] }) + +test("the card shows the Coding windows and the gift's buckets together", async () => { + planAndBalance() + answers["/api/monitor/usage/quota/limit"] = ok({ level: "pro", limits: [{ type: "CREDIT_LIMIT", unit: 3, number: 5, usage: 2000, remaining: 1500, percentage: 25 }] }) + const u = await (await hooks()).usage(async () => coding, { id: "zcode" }) + expect(u.plan).toBe("GLM Coding Pro") + expect(u.signIn).toBe("kept") + expect(u.error).toBeUndefined() + expect(u.windows.map((w) => w.name)).toEqual(["5 hours", "Start Plan · Trust Build"]) + expect(u.windows[1]).toMatchObject({ used: 25, display: "250 / 1000", models: ["glm-5.3-flash"] }) + expect(u.until).toBeUndefined() + expect(u.renew).toBeUndefined() + expect(u.windows[1].resetsAt).toBe(new Date((now + 3600) * 1000).toISOString()) +}) + +test("a gift read that fails after the gift was known leaves just the Coding card", async () => { + answers["/api/biz/subscription/list"] = ok([{ productName: "GLM Coding Pro", status: "VALID" }]) + answers["/api/monitor/usage/quota/limit"] = ok({ limits: [{ type: "CREDIT_LIMIT", unit: 3, number: 5, usage: 2000, remaining: 1500, percentage: 25 }] }) + answers["/api/v1/zcode-plan/billing/balance"] = ok(balance) + await (await hooks()).usage(async () => coding, { id: "zcode" }) // reads and caches the gift + answers["/api/v1/zcode-plan/billing/balance"] = new Response("", { status: 500 }) + const u = await (await hooks()).usage(async () => coding, { id: "zcode" }) + expect(u.windows.map((w) => w.name)).toEqual(["5 hours"]) + expect(u.error).toBeUndefined() +}) + +test("a model the gift serves goes to the Start Plan, dressed", async () => { + planAndBalance() + answers["/api/v1/zcode-plan/anthropic/v1/messages"] = ok({ type: "message", content: [] }) + const res = await sendTo(MSG, body("glm-5.3-flash")) + expect(res.status).toBe(200) + expect(sent.length).toBe(3) // the plan list and the balance read, then the message + const m = sent.at(-1) + expect(m.origin).toBe("https://zcode.z.ai") + expect(m.path).toBe("/api/v1/zcode-plan/anthropic/v1/messages") + expect(m.headers["x-title"]).toBe("Z Code@cli") + expect(m.headers.authorization).toBe("Bearer " + jwt) + expect(m.headers["x-api-key"]).toBeUndefined() + expect(JSON.parse(m.body).system.length).toBeGreaterThan(1) // dressed +}) + +test("a model the gift does not serve goes to the coding plan as sent", async () => { + planAndBalance() + answers["/api/anthropic/v1/messages"] = ok({ type: "message", content: [] }) + const res = await sendTo(MSG, body("GLM-5.3")) + expect(res.status).toBe(200) + const m = sent.at(-1) + expect(m.origin).toBe("https://api.z.ai") + expect(m.path).toBe("/api/anthropic/v1/messages") + expect(m.body).toBe(body("GLM-5.3")) + expect(m.headers["x-api-key"]).toBe(KEY) + expect(m.headers["x-title"]).toBeUndefined() +}) + +test("a spent gift replays the request once to the coding plan, undressed", async () => { + planAndBalance() + answers["/api/v1/zcode-plan/anthropic/v1/messages"] = new Response(JSON.stringify({ error: { code: "1113", message: "Insufficient balance or no resource package" } }), { status: 429 }) + answers["/api/anthropic/v1/messages"] = ok({ type: "message", content: [] }) + const res = await sendTo(MSG, body("glm-5.3-flash")) + expect(res.status).toBe(200) + const m = sent.at(-1) + expect(m.origin).toBe("https://api.z.ai") + expect(m.body).toBe(body("glm-5.3-flash")) // the agent's body, not dressed + expect(m.headers["x-api-key"]).toBe(KEY) + // the second try for the same model skips the gift: kept off for a minute + const before = sent.length + await sendTo(MSG, body("glm-5.3-flash")) + expect(sent.slice(before).at(-1).origin).toBe("https://api.z.ai") +}) + +test("a gift's 200 with a quota error replays to the coding plan too", async () => { + planAndBalance() + answers["/api/v1/zcode-plan/anthropic/v1/messages"] = new Response(JSON.stringify({ code: 1005, msg: "exceed quota limit", logid: "x" }), { status: 200, headers: { "content-type": "application/json" } }) + answers["/api/anthropic/v1/messages"] = ok({ type: "message", content: [] }) + const res = await sendTo(MSG, body("glm-5.3-flash")) + expect(res.status).toBe(200) + const m = sent.at(-1) + expect(m.origin).toBe("https://api.z.ai") + expect(m.body).toBe(body("glm-5.3-flash")) +}) + +test("a gift's 200 success answers normally: no replay", async () => { + planAndBalance() + const sse = new Response("data: {}\n\n", { status: 200, headers: { "content-type": "text/event-stream" } }) + answers["/api/v1/zcode-plan/anthropic/v1/messages"] = sse + const res = await sendTo(MSG, body("glm-5.3-flash")) + expect(res.status).toBe(200) + expect(res.headers.get("x-magpie-sign-in")).toBe("kept") + expect(sent.filter((c) => c.path === "/api/anthropic/v1/messages").length).toBe(0) + expect(await res.text()).toBe("data: {}\n\n") +}) + +test("a gift refusal replays to the coding plan but keeps the gift unblocked", async () => { + planAndBalance() + answers["/api/v1/zcode-plan/anthropic/v1/messages"] = new Response(JSON.stringify({ code: "3012", msg: "unusual activity" }), { status: 405 }) + answers["/api/anthropic/v1/messages"] = ok({ type: "message", content: [] }) + const res = await sendTo(MSG, body("glm-5.3-flash")) + expect(res.status).toBe(200) + expect(sent.at(-1).origin).toBe("https://api.z.ai") + expect(_internal.blocked.size).toBe(0) + const before = sent.length + await sendTo(MSG, body("glm-5.3-flash")) + expect(sent.slice(before).some((c) => c.origin === "https://zcode.z.ai")).toBe(true) +}) + +test("a ZCode sign-in that expired after the gift was cached goes to the coding plan", async () => { + planAndBalance() + answers["/api/anthropic/v1/messages"] = ok({ type: "message", content: [] }) + const dead = ["{}", JSON.stringify({ exp: Math.floor(Date.now() / 1000) - 10 })].map((s) => Buffer.from(s).toString("base64url")).join(".") + ".sig" + _internal.routes.set(KEY + "\0" + dead, { start: false, gift: { models: ["glm-5.3-flash"] }, at: Date.now(), ttl: 600_000 }) + const aged = { ...coding, refresh: JSON.stringify({ ...JSON.parse(coding.refresh), jwt: dead }) } + const opts = await (await hooks()).loader(async () => aged, { id: "zcode" }) + const res = await opts.fetch(MSG, { method: "POST", headers: { "content-type": "application/json", "x-api-key": "zcode" }, body: body("glm-5.3-flash") }) + 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) +}) + +test("a gift read that fails at once still trusts the plan list for ten minutes", async () => { + answers["/api/biz/subscription/list"] = ok([{ productName: "GLM Coding Pro", status: "VALID" }]) + answers["/api/v1/zcode-plan/billing/balance"] = new Response("", { status: 500 }) + await sendTo(MSG, body("glm-5.3-flash")) + const r = _internal.routes.get(KEY + "\0" + jwt) + expect(r.start).toBe(false) // on the coding plan + expect(r.gift).toBeNull() // the balance read failed: no gift known + expect(r.ttl).toBe(600_000) // the plan list read fine: 10 minutes +}) + +test("a coding account with no gift keeps its 10-minute cache", async () => { + answers["/api/biz/subscription/list"] = ok([{ productName: "GLM Coding Pro", status: "VALID" }]) + answers["/api/v1/zcode-plan/billing/balance"] = ok({ server_time: now, plans: [], balances: [] }) + answers["/api/anthropic/v1/messages"] = ok({ type: "message", content: [] }) + await sendTo(MSG, body("GLM-5.3")) + const r = _internal.routes.get(KEY + "\0" + jwt) + expect(r.start).toBe(false) + expect(r.gift).toBeNull() + expect(r.ttl).toBe(600_000) // plan list read fine: 10 minutes, not 1 +}) + +test("a plan-list failure on a key account takes the dual route, not gift-only", async () => { + answers["/api/biz/subscription/list"] = new Response("", { status: 500 }) + answers["/api/v1/zcode-plan/billing/balance"] = ok(balance) + answers["/api/anthropic/v1/messages"] = ok({ type: "message", content: [] }) + await sendTo(MSG, body("GLM-5.3")) + const r = _internal.routes.get(KEY + "\0" + jwt) + expect(r.start).toBe(false) // dual: coding first, gift for its models + expect(r.gift).toMatchObject({ models: ["glm-5.3-flash"] }) + expect(r.ttl).toBe(60_000) // unsure: the plan list failed +}) + +// several plans can be active at once: the gift is what any live plan's live +// bucket can spend, whatever the plan is named +test("every live plan spends first by its buckets' models, and the card shows them all", async () => { + answers["/api/biz/subscription/list"] = ok([{ productName: "GLM Coding Pro", status: "VALID" }]) + answers["/api/monitor/usage/quota/limit"] = ok({ limits: [{ type: "CREDIT_LIMIT", unit: 3, number: 5, usage: 2000, remaining: 1500, percentage: 25 }] }) + answers["/api/v1/zcode-plan/billing/balance"] = ok({ + server_time: now, + plans: [ + { plan_id: "zai-start-plan", user_plan_id: "up1", name: "Start Plan", status: "active", ends_at: now + 7 * 86400, entitlements: [{ entitlement_id: "e1", period: "daily" }] }, + { plan_id: "zai-bonus-plan", user_plan_id: "up2", name: "GLM Festival Grant", status: "active", ends_at: now + 3 * 86400, entitlements: [{ entitlement_id: "e2", period: "weekly" }] }, + { plan_id: "zai-old-plan", user_plan_id: "up3", name: "Old Grant", status: "active", ends_at: now - 86400, entitlements: [{ entitlement_id: "e3", period: "daily" }] }, + ], + balances: [ + ...balance.balances, + { plan_id: "zai-bonus-plan", user_plan_id: "up2", entitlement_id: "e2", show_name: "GLM-5.2", capabilities: ["model:GLM-5.2"], total_units: 100, used_units: 10, remaining_units: 90, expires_at: now + 3600 }, + { plan_id: "zai-bonus-plan", user_plan_id: "up2", entitlement_id: "e2b", show_name: "GLM-5.2 late", capabilities: ["model:glm-5.2"], total_units: 100, used_units: 100, remaining_units: 0, expires_at: now + 3600 }, + { plan_id: "zai-old-plan", user_plan_id: "up3", entitlement_id: "e3", show_name: "Old", capabilities: ["model:glm-4"], total_units: 10, used_units: 0, remaining_units: 10, expires_at: now + 3600 }, + ], + }) + answers["/api/anthropic/v1/messages"] = ok({ type: "message", content: [] }) + answers["/api/v1/zcode-plan/anthropic/v1/messages"] = ok({ type: "message", content: [] }) + // models of both live plans go to the gift first + for (const m of ["glm-5.3-flash", "GLM-5.2"]) { + const before = sent.length + expect((await sendTo(MSG, body(m))).status).toBe(200) + expect(sent.slice(before).at(-1).origin).toBe("https://zcode.z.ai") + } + // the ended plan's bucket does not put GLM-4 on the gift + const before = sent.length + expect((await sendTo(MSG, body("GLM-4"))).status).toBe(200) + expect(sent.slice(before).at(-1).origin).toBe("https://api.z.ai") + // the card shows both plans' live buckets, spent ones included, none of the ended plan's + const u = await (await hooks()).usage(async () => coding, { id: "zcode" }) + expect(u.windows.map((w) => w.name)).toEqual(["5 hours", "Start Plan · Trust Build", "GLM Festival Grant · GLM-5.2", "GLM Festival Grant · GLM-5.2 late"]) + expect(u.windows.some((w) => w.name === "Old")).toBe(false) +}) + +test("a gift-only account sends every request to the gift, and reads no balance to do it", async () => { + answers["/api/v1/zcode-plan/anthropic/v1/messages"] = ok({ type: "message", content: [] }) + const only = { type: "oauth", access: jwt, refresh: JSON.stringify({ site: "zai", base: "https://api.z.ai/api/anthropic", jwt, device: DEVICE }), expires: 0 } + const opts = await (await hooks()).loader(async () => only, { 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(200) + 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) +}) diff --git a/packages/zcode/index.mjs b/packages/zcode/index.mjs index 8a3a968..2021d32 100644 --- a/packages/zcode/index.mjs +++ b/packages/zcode/index.mjs @@ -130,48 +130,103 @@ const num = (v) => { return typeof n === "number" && Number.isFinite(n) ? n : undefined } -// startPlan is the Start Plan the account has now ({name, until}), as -// ZCode reads its balance: an "active" plan past its end is over. +// startPlan is the gift an account can spend now ({models}), as ZCode reads +// its balance: several plans can be active at once, and a model routes to +// the gift when any of them has a live bucket serving it — a bucket past +// its plan's own end, or one whose remaining_units says none, is skipped +// rather than asked and refused. models are those live buckets' names. async function startPlan(jwt, device) { if (!jwt) throw new Error("not signed in to ZCode") if (jwtExpired(jwt)) throw new Error(EXPIRED) const b = (await call("GET", `${ZCODE}/api/v1/zcode-plan/billing/balance?app_version=${APP_VERSION}`, { auth: "Bearer " + jwt, device })) ?? {} const now = num(b.server_time) > 0 ? num(b.server_time) : Date.now() / 1000 - const isStart = (x) => x.includes("start-plan") || x.includes("start plan") - for (const p of b.plans ?? []) { - if (String(p.status ?? "").trim().toLowerCase() !== "active") continue - const end = num(p.ends_at) - if (end > 0 && end <= now) continue - const id = String(p.plan_id ?? "").trim().toLowerCase() - const n = String(p.name ?? "").trim().toLowerCase() - if ((id || n) && !isStart(id) && !isStart(n)) continue - return { name: first(p.name, "Start Plan"), until: end > 0 ? end * 1000 : 0 } + const same = (x, p) => (x.user_plan_id && p.user_plan_id ? x.user_plan_id === p.user_plan_id : String(x.plan_id ?? "") === String(p.plan_id ?? "")) + const plans = Array.isArray(b.plans) ? b.plans : [] + const balances = Array.isArray(b.balances) ? b.balances : [] + const ms = new Set() + let any = false + let name = "" + for (const p of plans) { + const st = String(p?.status ?? "").trim().toLowerCase() + const end = num(p?.ends_at) + if (st !== "active" || (end > 0 && end <= now)) continue + any = true + if (!name) name = String(p?.name ?? "").trim() + for (const x of balances) { + if (!same(x, p)) continue + const exp = num(x.expires_at) + if (exp > 0 && exp <= now) continue + // a bucket spends when it has quota: only remaining_units says so, and + // a model whose every bucket says none drops out of models, so its + // requests go to the coding plan without first asking the gift + if ((num(x.remaining_units) ?? 1) <= 0) continue + for (const c of Array.isArray(x.capabilities) ? x.capabilities : []) { + const m = String(c ?? "").trim().replace(/^model:/, "").trim() + if (m) ms.add(m) + } + } } - return null + return any ? { name: name || "Start Plan", models: [...ms] } : null } -// onStart says whether an account's requests go to the Start Plan: when it -// has ZCode's token and no coding plan key, or a key whose account has no -// coding plan. Asked again after 10 minutes (a minute when unsure). +// plansOf says where an account's requests go and what more it can spend: +// {start, gift}. start is what onStart said: an account with no coding plan +// sends every request to the gift and reads no balance to do it; a team's +// seat, a sign-in with no ZCode token and a key whose plan-list read found +// no gift stay on the coding plan. gift is what a coding account also holds: +// the gift plans ({models}), and its requests spend the gift for a model it +// serves, until it says it is spent (see blocked). Asked again after 10 +// minutes (a minute when unsure — a plan-list read that failed is retried +// sooner; a gift read that failed simply leaves gift null until the next +// ask). const routes = new Map() -async function onStart(s) { - if (!s.jwt || isTeam(s)) return false // a team's seat is on the team's plan - if (!s.key) return true +async function plansOf(s) { + if (isTeam(s) || !s.jwt) return { start: false, gift: null } + if (!s.key) return { start: true, gift: null } const id = s.key + "\0" + s.jwt const r = routes.get(id) - if (r && Date.now() - r.at < r.ttl) return r.start + if (r && Date.now() - r.at < r.ttl) return r let start = false + let gift = null let sure = false try { - start = !(await plan(s)) + if (!(await plan(s))) start = true + else gift = await startPlan(s.jwt, s.device).catch(() => null) sure = true } 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 + // refusal still replays to the coding plan rather than blacking out try { - if (await startPlan(s.jwt, s.device)) start = sure = true + gift = await startPlan(s.jwt, s.device) + sure = false } catch {} } - routes.set(id, { start, at: Date.now(), ttl: sure ? 600_000 : 60_000 }) - return start + const out = { start, gift, at: Date.now(), ttl: sure ? 600_000 : 60_000 } + routes.set(id, out) + return out +} + +// blocked remembers the models a gift said it has none left for: a key+jwt+ +// model for a minute, so one turn that is refused does not ask again. +const blocked = new Map() + +// giftServes: the gift's buckets name their models lowercased ("glm-5.3- +// flash"); what is asked for arrives as the plan lists it ("GLM-5.3-Flash"). +const giftServes = (gift, m) => (gift.models ?? []).some((x) => x.toLowerCase() === m.toLowerCase()) + +// spentUp is the gift saying it has nothing left to spend: its own no- +// resource-package code 1113, its "exceed quota limit" 1005 — a spent gift +// can answer a 200 with a JSON body carrying one of those codes. Every 429 +// counts as spent, Z.ai's 1302/1303 rate limits among them. +function spentUp(text, status) { + if (status === 429) return true + let b + try { b = JSON.parse(text) } catch { return false } + if (!b || typeof b !== "object") return false + const code = (v) => String(v ?? "") + const c1 = code(b.code), c2 = code(b.error?.code) + return c1 === "1113" || c1 === "1005" || c2 === "1113" || c2 === "1005" } // ---- keys ------------------------------------------------------------------------ @@ -366,10 +421,12 @@ function startNum(v) { return undefined } -// startUsage is the Start Plan's allowance: a window for each of its -// buckets, a model's tokens for the day or for the plan's time. A plan -// still "active" past its end is over, and its buckets are left out. -async function startUsage(s) { +// giftOf reads the balance and returns the gift's card fields: its plans and +// windows. Several plans can be active at once, so every live plan's buckets +// show, each keeping its own models and end; the plan and end put on the +// card (for a gift-only account) are the soonest-ending live plan's. null +// when there is no live plan to show. +async function giftOf(s) { if (!s.jwt) throw new Error("not signed in to ZCode") if (jwtExpired(s.jwt)) throw new Error(EXPIRED) const b = (await call("GET", `${ZCODE}/api/v1/zcode-plan/billing/balance?app_version=${APP_VERSION}`, { auth: "Bearer " + s.jwt, device: s.device })) ?? {} @@ -382,7 +439,7 @@ async function startUsage(s) { if (end > 0 && end <= now && p.status.trim().toLowerCase() === "active") p.status = "expired" if (p.status.trim().toLowerCase() === "expired") over.add(p.user_plan_id + "\0" + p.plan_id) } - const same = (x, p) => (x.user_plan_id && p.user_plan_id ? x.user_plan_id === p.user_plan_id : x.plan_id === p.plan_id) + const same = (x, p) => (x.user_plan_id && p.user_plan_id ? x.user_plan_id === p.user_plan_id : String(x.plan_id ?? "") === String(p.plan_id ?? "")) const balances = (Array.isArray(b.balances) ? b.balances : []) .map((x) => ({ ...x, user_plan_id: str(x?.user_plan_id), plan_id: str(x?.plan_id) })) .filter((x) => { @@ -395,32 +452,41 @@ async function startUsage(s) { return keep }) + // every live plan counts as the gift: no plan-name filter decides it, + // the balance says what the session can spend let active = null for (const p of plans) { if (p.status.trim().toLowerCase() !== "active") continue - const id = p.plan_id.trim().toLowerCase(), n = str(p.name).trim().toLowerCase() - const isStart = (x) => x.includes("start-plan") || x.includes("start plan") - if ((id || n) && !isStart(id) && !isStart(n)) continue const end = startNum(p.ends_at) - active = { name: first(str(p.name), "Start Plan"), until: end > 0 ? new Date(Math.trunc(end) * 1000).toISOString() : "" } - break + if (end > 0 && end <= now) continue + if (active && !(end > 0 && (!startNum(active.end) || end < startNum(active.end)))) continue + active = { name: first(str(p.name), "Start Plan"), end, until: end > 0 ? new Date(Math.trunc(end) * 1000).toISOString() : "" } } - if (!active) return { error: "this account has no GLM Coding Plan, and ZCode's Start Plan has ended or was never started" } + if (!active) return null const out = { plan: active.name, windows: [] } if (active.until) Object.assign(out, { until: active.until, renew: "off" }) + const live = plans.filter((p) => { + if (p.status.trim().toLowerCase() !== "active") return false + const end = startNum(p.ends_at) + return !(end > 0 && end <= now) + }) for (const x of balances) { + const owner = live.find((p) => same(x, p)) + if (!owner) continue + const exp = startNum(x.expires_at) + if (exp > 0 && exp <= now) continue // a bucket past its own end is over const total = startNum(x.total_units), left = startNum(x.remaining_units) let used = startNum(x.used_units) if (total === undefined && used === undefined && left === undefined) continue const models = (Array.isArray(x.capabilities) ? x.capabilities : []) .map((c) => String(c ?? "").trim().replace(/^model:/, "").trim()).filter(Boolean) - const w = { name: first(str(x.show_name), models.join(", "), "Credits"), used: 0 } + const base = first(str(x.show_name), models.join(", "), "Credits") + const w = { name: base, used: 0, _plan: str(owner.name) } if (used === undefined) used = total !== undefined && left !== undefined ? total - left : 0 if (total > 0) { w.used = (100 * used) / total w.display = `${compact(used)} / ${compact(total)}` } - const exp = startNum(x.expires_at) if (exp > 0) w.resetsAt = new Date(Math.trunc(exp) * 1000).toISOString() let span = 0 for (const p of plans) { @@ -440,6 +506,37 @@ async function startUsage(s) { return out } +// startUsage is a gift-only account's card: its buckets, or the words +// for having none to show. The card's header already names the plan, so +// the windows keep their own names. +const NO_START = "this account has no GLM Coding Plan, and ZCode's Start Plan has ended or was never started" +async function startUsage(s) { + const g = await giftOf(s) + if (!g) return { error: NO_START } + for (const w of g.windows) delete w._plan + return g +} + +// dualUsage is an account with both a Coding Plan and a gift: its card shows +// both allowances, the Coding windows first and the gift's buckets after, +// each keeping its own models and end. The gift's windows carry their plan's +// name as a prefix so they are not mistaken for the coding plan's own. A +// gift read that fails leaves just the Coding card: magpie hides a card's +// windows behind an error and leaves it out of the menu bar and auto-switch, +// so erroring here would hide a working Coding Plan over a gift hiccup. +async function dualUsage(s) { + const out = await codingUsage(s) + try { + const g = await giftOf(s) + if (g) out.windows = [...out.windows, ...g.windows.map((w) => { + const p = w._plan + delete w._plan + return p && p !== w.name ? { ...w, name: p + " · " + w.name } : w + })] + } catch {} + return out +} + // periodOf reads an entitlement's period: "daily", "weekly", "monthly". function periodOf(p) { p = p.toLowerCase() @@ -989,6 +1086,26 @@ const entry = (m) => ({ }) // model is a model as OpenCode's provider hands it to provider.models. +// modelOf reads the model a Messages body asks for. +function modelOf(text) { + try { + const m = JSON.parse(text)?.model + return typeof m === "string" ? m : "" + } catch { + return "" + } +} + +// kept passes a response on as it came, the account kept: the built-in +// never marked a ZCode account lapsed nor cleared one. +function kept(res, text) { + const h = new Headers(res.headers) + h.delete("content-length") + h.delete("content-encoding") + h.set("X-Magpie-Sign-In", "kept") + return new Response(text !== undefined ? text : res.body, { status: res.status, statusText: res.statusText, headers: h }) +} + function model(m, url) { const e = entry(m) return { @@ -1168,7 +1285,7 @@ export async function ZCodeAuthPlugin({ client }) { async models(provider, { auth } = {}) { const s = stateOf(auth) if (!s) return provider.models - const start = await onStart(s).catch(() => false) + const start = await plansOf(s).then((r) => r.start).catch(() => false) const base = start ? START_BASE : s.base let ms = modelsOf(await zcodeConfig().catch(() => null), planID(base)) // ZCode's config can't be had: its table stands in, and magpie keeps @@ -1196,14 +1313,7 @@ export async function ZCodeAuthPlugin({ client }) { if (!s) throw new Error("not signed in to ZCode") let url = input instanceof Request ? input.url : String(input) const opts = input instanceof Request ? { method: input.method, headers: input.headers, body: input.body, signal: input.signal, duplex: "half", ...init } : { ...init } - const start = await onStart(s) - // the request goes to the plan the account is on, wherever it was made for - for (const b of [START_BASE, ZAI_BASE, BIGMODEL_BASE]) { - if (url.startsWith(b)) { - url = (start ? START_BASE : s.base) + url.slice(b.length) - break - } - } + const { start: alone, gift } = await plansOf(s) let key = s.key if (isTeam(s) && !key) { key = await teamKey(s) @@ -1212,32 +1322,79 @@ export async function ZCodeAuthPlugin({ client }) { await client?.auth?.set?.({ path: { id }, body: next }).catch?.(() => {}) } } - const h = new Headers(opts.headers) + // a dual account's body is read ahead of sending, so a gift that + // says it is spent can spend nothing and replay what was asked for + let start = alone + let orig + if (!alone && gift && opts.body != null) { + orig = opts.body = typeof opts.body === "string" ? opts.body : await new Response(opts.body).text() + delete opts.duplex + const m = modelOf(orig) + // a ZCode sign-in past its end reaches no gift: the coding plan + // takes the request, as it did before the gift was known + start = !!m && !jwtExpired(s.jwt) && giftServes(gift, m) && Date.now() >= (blocked.get(s.key + "\0" + s.jwt + "\0" + m) ?? 0) + } + const swap = (base) => { + for (const b of [START_BASE, ZAI_BASE, BIGMODEL_BASE]) { + if (url.startsWith(b)) { + url = base + url.slice(b.length) + break + } + } + } + let h = new Headers(opts.headers) if (start) { if (jwtExpired(s.jwt)) throw new Error(EXPIRED) if (!s.jwt) throw new Error("ZCode's sign-in has no key; sign in again") + swap(START_BASE) // the Start Plan is served only to what looks like ZCode's own request startHeaders(h, s.jwt) if (opts.body != null) { - const text = typeof opts.body === "string" ? opts.body : await new Response(opts.body).text() + const text = orig ?? (typeof opts.body === "string" ? opts.body : await new Response(opts.body).text()) opts.body = dress(text, s.site, s.device) delete opts.duplex h.delete("content-length") } } else { if (!key) throw new Error("ZCode's sign-in has no key; sign in again") + swap(s.base) h.delete("authorization") h.set("x-api-key", key) h.set("Authorization", "Bearer " + key) } - const res = await fetch(url, { ...opts, headers: h }) + let res = await fetch(url, { ...opts, headers: h }) + // a dual account's gift answer that can't serve the request goes + // to its coding plan instead, as the agent sent it: any status + // 400 or over (its 405, its 401, its 5xx — a paid plan is waiting, + // and a gift that keeps refusing would otherwise black out the + // account), and its quota answers inside a 200. Only a quota + // answer keeps that model off the gift for a minute: a refusal + // that says nothing about quota is asked again next request. + // Every 429 counts as spent, Z.ai's 1302/1303 rate limits among + // them — replaying one to the coding plan is the good answer, and + // the minute's block keeps a rate limit from being knocked twice. + // A stream's body is never read here. + if (start && !alone) { + const json = (res.headers.get("content-type") ?? "").includes("application/json") + if (res.status >= 400 || json) { + const text = await res.text() + const spent = spentUp(text, res.status) + if (!spent && res.status < 400) return kept(res, text) + if (spent) { + for (const [k, until] of blocked) if (Date.now() >= until) blocked.delete(k) + blocked.set(s.key + "\0" + s.jwt + "\0" + modelOf(orig), Date.now() + 60_000) + } + swap(s.base) + h = new Headers(opts.headers) + h.delete("authorization") + h.set("x-api-key", key) + h.set("Authorization", "Bearer " + key) + res = await fetch(url, { ...opts, body: orig, headers: h }) + } + } // the plan's answer goes on as it came, the account kept: the // built-in never marked a ZCode account lapsed nor cleared one - const kept = new Headers(res.headers) - kept.delete("content-length") - kept.delete("content-encoding") - kept.set("X-Magpie-Sign-In", "kept") - return new Response(res.body, { status: res.status, statusText: res.statusText, headers: kept }) + return kept(res) }, } }, @@ -1269,7 +1426,9 @@ export async function ZCodeAuthPlugin({ client }) { } return saved(await teamUsage(s, key)) } - if (await onStart(s)) return saved(await startUsage(s)) + const { start, gift } = await plansOf(s) + if (start) return saved(await startUsage(s)) + if (gift) return saved(await dualUsage(s)) return saved(await codingUsage(s)) } catch (e) { return saved({ error: e?.message ?? String(e) }) @@ -1300,4 +1459,4 @@ export async function ZCodeAuthPlugin({ client }) { } // for tests -export const _internal = { entry, limitWindows, termOf, startUsage, routes, teamKeys, ownSignIn, stateOf, dress, PROMPT } +export const _internal = { entry, limitWindows, termOf, startUsage, routes, blocked, plansOf, giftOf, dualUsage, spentUp, giftServes, modelOf, teamKeys, ownSignIn, stateOf, dress, PROMPT } diff --git a/packages/zcode/package.json b/packages/zcode/package.json index 02ca0aa..f61e0ce 100644 --- a/packages/zcode/package.json +++ b/packages/zcode/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-zcode-auth", - "version": "0.1.7", + "version": "0.1.8", "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",