diff --git a/packages/zcode/README.md b/packages/zcode/README.md index 48cbc77..62a792d 100644 --- a/packages/zcode/README.md +++ b/packages/zcode/README.md @@ -120,7 +120,10 @@ minutes. When the config can't be read, the plugin falls back to this list: | GLM-5.2 | 1M | 128K | none, high, max | | GLM-5-Turbo | 200K | 64K | none, high | -The Start Plan has all of these except GLM-5.3. +The Start Plan's config has all of these except GLM-5.3. An account +with no GLM Coding Plan also lists every model its gift plans serve under +the model's own name, as ZCode does (the Start Plan's GLM-5.3 bucket makes +GLM-5.3 a model), since all of its requests go to the gift anyway. A gift plan's models appear again as `-Trial` entries, so a request can spend the gift's quota on purpose. A trial entry is listed diff --git a/packages/zcode/gift.test.mjs b/packages/zcode/gift.test.mjs index 4c925fb..2915c71 100644 --- a/packages/zcode/gift.test.mjs +++ b/packages/zcode/gift.test.mjs @@ -211,3 +211,38 @@ test("a gift-only account's spent Start Plan bucket doesn't hold up a model its expect(u.windows.find((w) => w.used === 100)).toMatchObject({ name: "GLM-5.3-Flash", aside: true }) expect(u.windows).toHaveLength(3) }) + +test("a gift-only account lists its gift's models by their own names, as ZCode does (#1261)", async () => { + // sun1090's account: no GLM Coding Plan, the Start Plan's GLM-5.3 and + // GLM-5.3-Flash buckets; the card counted GLM-5.3 while the list had only + // GLM-5.3-Trial, as the Start Plan's config leaves GLM-5.3 out + balance.plans = [{ plan_id: "start", user_plan_id: "s1", name: "ZCode Start Plan", status: "active", ends_at: now + 7 * 86400 }] + balance.balances = [ + { plan_id: "start", user_plan_id: "s1", show_name: "GLM-5.3", capabilities: ["model:GLM-5.3"], total_units: 3_000_000, used_units: 41_030, remaining_units: 2_958_970 }, + { plan_id: "start", user_plan_id: "s1", show_name: "GLM-5.3-Flash", capabilities: ["model:GLM-5.3-Flash"], total_units: 5_000_000, used_units: 170_088, remaining_units: 4_829_912 }, + ] + const only = { type: "oauth", access: jwt, refresh: JSON.stringify({ ...state, key: undefined, plan: undefined }), expires: 0 } + const h = await hooks() + const models = await h.provider.models({ models: {} }, { auth: only }) + expect(models["GLM-5.3"]).toMatchObject({ id: "GLM-5.3", name: "GLM-5.3", api: { id: "GLM-5.3" } }) + expect(Object.keys(models["GLM-5.3"].variants ?? {})).toEqual(["low", "high", "max"]) + // the trial entries a user may have picked stay, and nothing is listed twice + expect(models["GLM-5.3-Trial"]).toBeDefined() + expect(models["GLM-5.3-Flash-Trial"]).toBeDefined() + const ids = Object.keys(models).map((id) => id.toLowerCase()) + expect(new Set(ids).size).toBe(ids.length) + // every model the card counts is one the list has + const u = await h.auth.usage(async () => only, { id: "zcode" }) + for (const w of u.windows) for (const m of w.models ?? []) expect(models[m]).toBeDefined() + // and a request for it goes to the Start Plan + const loader = await h.auth.loader(async () => only, { id: "zcode" }) + await loader.fetch(MSG, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ model: "GLM-5.3", messages: [{ role: "user", content: "hi" }], max_tokens: 10 }) }) + expect(sent.at(-1)).toMatchObject({ origin: "https://zcode.z.ai", body: { model: "GLM-5.3" } }) +}) + +test("a coding plan account's gift stays on its trial entries alone", async () => { + balance.balances[0].capabilities = ["model:GLM-5.3"] + const coding = await (await hooks()).provider.models({ models: {} }, { auth: { type: "api", key: state.key } }) + const models = await (await hooks()).provider.models({ models: {} }, { auth }) + expect(Object.keys(models).filter((id) => !(id in coding))).toEqual(["GLM-5.3-Trial"]) +}) diff --git a/packages/zcode/index.mjs b/packages/zcode/index.mjs index 433e375..c52e30f 100644 --- a/packages/zcode/index.mjs +++ b/packages/zcode/index.mjs @@ -1421,12 +1421,22 @@ export async function ZCodeAuthPlugin({ client }) { if (fell) ms = start ? START_MODELS : MODELS const url = s.base + "/v1" const out = Object.fromEntries(ms.map((m) => [m.id, model(m, url)])) - const details = [...ms, ...modelsOf(cfg, planID(START_BASE)), ...START_MODELS] + const details = [...ms, ...modelsOf(cfg, planID(START_BASE)), ...modelsOf(cfg, planID(ZAI_BASE)), ...MODELS] + const about = (raw) => details.find((m) => m.id.toLowerCase() === raw.toLowerCase()) ?? { efforts: [] } + const has = new Set(Object.keys(out).map((id) => id.toLowerCase())) for (const raw of giftNames(gift)) { + // a gift-only account's models are its gift's, by their own + // names, as ZCode lists them (its Start Plan's GLM-5.3, which the + // plan's config leaves out): every request of it goes to the gift + // anyway, and the card counts them by that name too (#1261) + if (start && !has.has(raw.toLowerCase())) { + const plain = about(raw).id ?? raw + has.add(raw.toLowerCase()) + out[plain] = model({ ...about(raw), id: plain }, url) + } const id = giftID(raw) if (out[id]) continue - const m = details.find((m) => m.id.toLowerCase() === raw.toLowerCase()) ?? { efforts: [] } - out[id] = model({ ...m, id }, url) + out[id] = model({ ...about(raw), id }, url) } if (fell) out[Symbol.for("magpie.fellBack")] = true return out diff --git a/packages/zcode/package.json b/packages/zcode/package.json index 81181f3..8c5528e 100644 --- a/packages/zcode/package.json +++ b/packages/zcode/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-zcode-auth", - "version": "0.1.13", + "version": "0.1.14", "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",