diff --git a/packages/zcode/dual.test.mjs b/packages/zcode/dual.test.mjs index bbf7b81..296bb5f 100644 --- a/packages/zcode/dual.test.mjs +++ b/packages/zcode/dual.test.mjs @@ -373,9 +373,10 @@ test("every live plan spends first by its buckets' models, and the card shows th 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 + // the card shows both plans' live buckets, spent ones included, none of the + // ended plan's, each named after its plan as there are two const u = await (await hooks()).usage(async () => coding, { id: "zcode" }) - expect(u.windows.map((w) => w.name)).toEqual(["5 hours", "Trust Build", "GLM-5.2-Trial", "GLM-5.2 late"]) + expect(u.windows.map((w) => w.name)).toEqual(["5 hours", "Start Plan · Trust Build", "GLM Festival Grant · GLM-5.2-Trial", "GLM Festival Grant · GLM-5.2 late"]) expect(u.windows.some((w) => w.name === "Old")).toBe(false) expect(u.windows[0].notModels).toEqual(["glm-5.3-flash-trial", "glm-5.2-trial"]) expect(u.windows.at(-1).aside).toBe(true) // a spent sibling cannot cap a live bucket diff --git a/packages/zcode/gift.test.mjs b/packages/zcode/gift.test.mjs index 2915c71..919f3b2 100644 --- a/packages/zcode/gift.test.mjs +++ b/packages/zcode/gift.test.mjs @@ -208,7 +208,7 @@ test("a gift-only account's spent Start Plan bucket doesn't hold up a model its } expect(counting("GLM-5.3")[0].used).toBe(0) // the spent bucket is still on the card, as ZCode shows it - expect(u.windows.find((w) => w.used === 100)).toMatchObject({ name: "GLM-5.3-Flash", aside: true }) + expect(u.windows.find((w) => w.used === 100)).toMatchObject({ name: "ZCode Start Plan · GLM-5.3-Flash", aside: true }) expect(u.windows).toHaveLength(3) }) @@ -246,3 +246,52 @@ test("a coding plan account's gift stays on its trial entries alone", async () = const models = await (await hooks()).provider.models({ models: {} }, { auth }) expect(Object.keys(models).filter((id) => !(id in coding))).toEqual(["GLM-5.3-Trial"]) }) + +// 01huadalang's account (Discord, 10-24): the new-user Start Plan and the +// 1024 Dev Day plan both give GLM-5.3, and the new-user one is spent. The +// 1024 plan's id, name, show_names, capabilities and grant_units are the +// real /billing/preview bytes of an account it was offered to; its balance +// rows are written in the shape the balance gives (no 1024 balance was +// read), with the Start Plan's name as ganlerk's balance has it. +const twoPlans = () => { + balance.plans = [ + { plan_id: "zcode-v3-start-plan", user_plan_id: "s1", name: "ZCode Start Plan", status: "active", ends_at: now + 7 * 86400 }, + { plan_id: "zcode-v3-start-plan-1024", user_plan_id: "d1", name: "ZCode 1024 Dev Day", status: "active", ends_at: now + 30 * 86400 }, + ] + balance.balances = [ + { plan_id: "zcode-v3-start-plan", user_plan_id: "s1", show_name: "GLM-5.3", capabilities: ["model:glm-5.3"], total_units: 3_000_000, used_units: 3_000_000, remaining_units: 0 }, + { plan_id: "zcode-v3-start-plan", user_plan_id: "s1", show_name: "GLM-5.3-Flash", capabilities: ["model:glm-5.3-flash"], total_units: 5_000_000, used_units: 0, remaining_units: 5_000_000 }, + { plan_id: "zcode-v3-start-plan-1024", user_plan_id: "d1", entitlement_id: "ent-zcode-v3-start-plan-1024-1", show_name: "GLM-5.3-Flash", capabilities: ["model:glm-5.3-flash"], total_units: 300_000_000, used_units: 30_000_000, remaining_units: 270_000_000 }, + { plan_id: "zcode-v3-start-plan-1024", user_plan_id: "d1", entitlement_id: "ent-zcode-v3-start-plan-1024-2", show_name: "GLM-5.3", capabilities: ["model:glm-5.3"], total_units: 200_000_000, used_units: 50_000_000, remaining_units: 150_000_000 }, + ] +} + +test("two gift plans' GLM-5.3 each show, named by plan, the live one first (01huadalang)", async () => { + twoPlans() + const only = { type: "oauth", access: jwt, refresh: JSON.stringify({ ...state, key: undefined, plan: undefined }), expires: 0 } + const u = await (await hooks()).auth.usage(async () => only, { id: "zcode" }) + const names = u.windows.map((w) => w.name) + expect(new Set(names).size).toBe(names.length) + // the 1024 plan's GLM-5.3 shows its own amount left, and counts the model + expect(u.windows.find((w) => w.name === "ZCode 1024 Dev Day · GLM-5.3")).toMatchObject({ used: 25, amount: 50_000_000, limit: 200_000_000, models: ["glm-5.3", "GLM-5.3-Trial"] }) + expect(u.windows.find((w) => w.name === "ZCode 1024 Dev Day · GLM-5.3").aside).toBeUndefined() + // the spent new-user plan's GLM-5.3 still shows, used up, set aside + expect(u.windows.find((w) => w.name === "ZCode Start Plan · GLM-5.3")).toMatchObject({ used: 100, amount: 3_000_000, aside: true }) + // magpie's account row shows a card's first two windows: what is spent now + expect(u.windows.slice(0, 2).every((w) => !w.aside)).toBe(true) + expect(u.windows.slice(0, 2).some((w) => w.name === "ZCode 1024 Dev Day · GLM-5.3")).toBe(true) +}) + +test("a coding plan account's two gift plans' GLM-5.3 each show, named by plan", async () => { + twoPlans() + const u = await (await hooks()).auth.usage(async () => auth, { id: "zcode" }) + const gift = u.windows.slice(1) + expect(gift.map((w) => w.name)).toEqual([ + "ZCode Start Plan · GLM-5.3-Flash-Trial", + "ZCode 1024 Dev Day · GLM-5.3-Trial", + "ZCode Start Plan · GLM-5.3", + "ZCode 1024 Dev Day · GLM-5.3-Flash", + ]) + expect(gift[1]).toMatchObject({ used: 25, models: ["GLM-5.3-Trial"] }) + expect(gift[2]).toMatchObject({ used: 100, aside: true }) +}) diff --git a/packages/zcode/index.mjs b/packages/zcode/index.mjs index 4a8fd69..f47bb4d 100644 --- a/packages/zcode/index.mjs +++ b/packages/zcode/index.mjs @@ -578,13 +578,16 @@ async function claimHint(s) { // 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. +// the windows of one plan keep their own names; with several plans each +// bucket is named after its plan (see byPlan). 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 } const best = bestBuckets(g.windows) - g.windows = g.windows.map(({ _plan, _spent, ...w }, i) => { + const label = byPlan(g.windows) + g.windows = countedFirst(g.windows.map(({ _plan, _spent, ...w }, i) => { + w.name = label(w.name, _plan) if (!w.models?.length) return w // a model two plans give (Trust Build's and the Start Plan's Flash) // counts on the bucket ZCode spends now; the other stays on the card @@ -595,10 +598,28 @@ async function startUsage(s) { return { ...w, aside: true } } return { ...w, models: [...models, ...models.map(giftID)] } - }) + })) return g } +// byPlan names a bucket after its plan ("ZCode 1024 Dev Day · GLM-5.3") +// when the gift's buckets come from more than one plan: two plans each +// give a GLM-5.3, and named alike a spent one and a live one can't be +// told apart (01huadalang, Discord: the new-user plan's spent GLM-5.3 +// showed, the 1024 plan's own GLM-5.3 didn't). One plan's buckets keep +// their own names, as the card's header names the plan. +function byPlan(windows) { + const several = new Set(windows.map((w) => w._plan || "")).size > 1 + return (name, plan) => (several && plan ? plan + " · " + name : name) +} + +// countedFirst puts the buckets a request spends before those set aside +// (a spent bucket another plan's live one stands in for), each group in +// the balance's order: magpie's account row shows a card's first two +// windows and leaves the rest to its tooltip, so a spent plan listed first +// hid the live plan's allowance for the same model. +const countedFirst = (ws) => [...ws.filter((w) => !w.aside), ...ws.filter((w) => w.aside)] + // bestBuckets is, for each model the gift's buckets serve, the index of // the one a request spends: a live one before a spent one, the least // used of those. The server picks among an account's buckets itself @@ -641,19 +662,21 @@ async function dualUsage(s) { if (notModels.length) out.windows = out.windows.map((w) => w.aside ? w : { ...w, notModels }) // Several buckets may serve one model: one spent sibling cannot block it. const best = bestBuckets(g.windows) - out.windows.push(...g.windows.map(({ _plan: p, _spent, ...w }, i) => { + const label = byPlan(g.windows) + out.windows.push(...countedFirst(g.windows.map(({ _plan: p, _spent, ...w }, i) => { const selected = gift.allModels.filter((m) => best.get(m.toLowerCase()) === i) const models = selected.map(giftID) const scoped = models.length ? { ...w, models } : { ...w, aside: true } // the card names a bucket's model as the picker does (GLM-5.3-Flash-Trial); - // no plan prefix — the card's section header already says the account + // no plan prefix while one plan gives them all — the card's section + // header already says the account; with several, byPlan names each let name = w.name for (const m of selected) { const at = name.toLowerCase().indexOf(m.toLowerCase()) if (at >= 0) name = name.slice(0, at) + giftID(m) + name.slice(at + m.length) } - return { ...scoped, name } - })) + return { ...scoped, name: label(name, p) } + }))) } } catch { if (cached) { diff --git a/packages/zcode/package.json b/packages/zcode/package.json index 3c88b4a..1c1a486 100644 --- a/packages/zcode/package.json +++ b/packages/zcode/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-zcode-auth", - "version": "0.1.22", + "version": "0.1.23", "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",