diff --git a/packages/zcode/dual.test.mjs b/packages/zcode/dual.test.mjs index 076dbb5..fa995c7 100644 --- a/packages/zcode/dual.test.mjs +++ b/packages/zcode/dual.test.mjs @@ -83,7 +83,7 @@ test("free first spends the gift on the plain model while it has quota", async ( expect(m.path).toBe("/api/v1/zcode-plan/anthropic/v1/messages") expect(m.headers.authorization).toBe("Bearer " + jwt) expect(m.headers["x-api-key"]).toBeUndefined() - expect(JSON.parse(m.body).model).toBe("glm-5.3-flash") + expect(JSON.parse(m.body).model).toBe("GLM-5.3-Flash") }) test("free first falls back to the coding plan once the gift is spent", async () => { @@ -109,7 +109,7 @@ test("free first follows the provider's options, and a blocked model waits on th const first = await opts.fetch(MSG, { method: "POST", headers: { "content-type": "application/json" }, body: body("GLM-5.3-Flash") }) expect(first.status).toBe(200) expect(sent.at(-1).origin).toBe("https://zcode.z.ai") - expect(JSON.parse(sent.at(-1).body).model).toBe("glm-5.3-flash") + expect(JSON.parse(sent.at(-1).body).model).toBe("GLM-5.3-Flash") // a refusal the gift gave a moment ago (the blocked map) sends the plain // model to the plan until the minute is out _internal.blocked.set(KEY + "\0" + jwt + "\0glm-5.3-flash", Date.now() + 60_000) @@ -149,7 +149,7 @@ test("free first is on when nothing sets it", async () => { 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(JSON.parse(sent.at(-1).body).model).toBe("glm-5.3-flash") + expect(JSON.parse(sent.at(-1).body).model).toBe("GLM-5.3-Flash") }) test("freeFirst false on the provider keeps the plan, over a plugin that says nothing", async () => { @@ -209,7 +209,7 @@ test("a gift that answers spent is answered from the plan in the same request", expect(res.status).toBe(200) const giftTries = sent.filter((c) => c.origin === "https://zcode.z.ai" && c.path.endsWith("/messages")) expect(giftTries).toHaveLength(1) - expect(JSON.parse(giftTries[0].body).model).toBe("glm-5.3-flash") + expect(JSON.parse(giftTries[0].body).model).toBe("GLM-5.3-Flash") const planTries = sent.filter((c) => c.origin === "https://api.z.ai" && c.path.endsWith("/messages")) expect(planTries).toHaveLength(1) expect(JSON.parse(planTries[0].body).model).toBe("GLM-5.3-Flash") @@ -606,7 +606,7 @@ test("a trial entry goes to the Start Plan, dressed", async () => { expect(m.headers.authorization).toBe("Bearer " + jwt) expect(m.headers["x-api-key"]).toBeUndefined() expect(JSON.parse(m.body).system.length).toBeGreaterThan(1) // dressed - expect(JSON.parse(m.body).model).toBe("glm-5.3-flash") // the entry's own model + expect(JSON.parse(m.body).model).toBe("GLM-5.3-Flash") // the entry's own model }) test("a model the gift does not serve goes to the coding plan as sent", async () => { diff --git a/packages/zcode/gift.test.mjs b/packages/zcode/gift.test.mjs index d9e3f2c..3533d5a 100644 --- a/packages/zcode/gift.test.mjs +++ b/packages/zcode/gift.test.mjs @@ -34,7 +34,7 @@ beforeEach(() => { if (url.pathname === "/api/v1/zcode-plan/billing/balance") return ok(balance) if (url.pathname.endsWith("/messages")) { const text = init.body ?? (input instanceof Request ? await input.text() : undefined) - sent.push({ origin: url.origin, path: url.pathname, body: JSON.parse(text) }) + sent.push({ origin: url.origin, path: url.pathname, body: JSON.parse(text), headers: new Headers(init.headers ?? (input instanceof Request ? input.headers : {})) }) return url.origin === "https://zcode.z.ai" ? giftReply.clone() : ok({ type: "message", content: [] }) } throw new Error("unmocked request blocked: " + url.pathname) @@ -383,3 +383,20 @@ test("a coding plan account's ZCODE GLOBAL BUILD gift is named by its model firs expect(u.windows[2]).toMatchObject({ used: 15, amount: 450_000, limit: 3_000_000, models: ["GLM-5.3-Trial"] }) expect(u.windows[3]).toMatchObject({ used: 0, limit: 5_000_000, models: ["GLM-5.3-Flash-Trial"] }) }) + +test("a flash gift goes out under ZCode's registry spelling, as the app sends it", async () => { + balance.balances[0].capabilities = ["model:glm-5.3-flash"] + expect((await send("GLM-5.3-Flash-Trial")).status).toBe(200) + expect(sent).toHaveLength(1) + expect(sent[0].origin).toBe("https://zcode.z.ai") + expect(sent[0].body.model).toBe("GLM-5.3-Flash") +}) + +test("a refused-then-rested gift says why it is refused locally", async () => { + giftReply = new Response(JSON.stringify({ error: { code: "1302", message: "rate limit" } }), { status: 429, headers: { "content-type": "application/json" } }) + expect((await send()).status).toBe(429) + const again = await send() + expect(again.status).toBe(429) + expect(sent).toHaveLength(1) + expect((await again.json()).error.message).toContain("not asked again for a minute") +}) diff --git a/packages/zcode/index.mjs b/packages/zcode/index.mjs index 4e518b2..14305ed 100644 --- a/packages/zcode/index.mjs +++ b/packages/zcode/index.mjs @@ -152,6 +152,11 @@ const uniqueModels = (names) => { return [...byName.values()] } const giftNames = (g) => g?.allModels ?? g?.models ?? [] +// wireModel is a gift model as ZCode sends it: the buckets name it lowercased +// ("glm-5.3-flash"), the app's request carries its registry id +// ("GLM-5.3-Flash"; normalizeOfficialGlmModelId's spelling). Names ZCode +// does not know go as the bucket wrote them. +const wireModel = (m) => MODELS.find((x) => x.id.toLowerCase() === String(m).toLowerCase())?.id ?? m const giftModel = (g, id) => giftNames(g).find((m) => giftID(m).toLowerCase() === id.toLowerCase()) const bucketModels = (x) => (Array.isArray(x.capabilities) ? x.capabilities : []) .map((c) => String(c ?? "").trim()).filter((c) => c.startsWith("model:") || !c.includes(":")) @@ -273,6 +278,7 @@ 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()) +const giftRested = (s, m) => Date.now() < (blocked.get(s.key + "\0" + s.jwt + "\0" + m.toLowerCase()) ?? 0) const giftUsable = (s, gift, m) => !jwtExpired(s.jwt) && giftServes(gift, m) && Date.now() >= (blocked.get(s.key + "\0" + s.jwt + "\0" + m.toLowerCase()) ?? 0) @@ -2405,10 +2411,10 @@ export async function ZCodeAuthPlugin({ client }, options = {}) { const raw = giftModel(gift, m) const available = raw && giftUsable(s, gift, raw) if (!available) { - const error = { type: raw ? "rate_limit_error" : "invalid_request_error", message: raw ? `ZCode's limited quota for ${raw} is exhausted or temporarily unavailable` : `ZCode has no active limited quota for ${m}` } + const error = { type: raw ? "rate_limit_error" : "invalid_request_error", message: raw ? `ZCode's limited quota for ${raw} is exhausted or temporarily unavailable${giftRested(s, raw) ? " (ZCode answered 429 to the last request for it, so it is not asked again for a minute)" : ""}` : `ZCode has no active limited quota for ${m}` } return kept(new Response(JSON.stringify({ type: "error", error }), { status: raw ? 429 : 400, headers: { "content-type": "application/json" } })) } - orig = opts.body = JSON.stringify({ ...JSON.parse(orig), model: raw }) + orig = opts.body = JSON.stringify({ ...JSON.parse(orig), model: wireModel(raw) }) m = raw forcedGift = true } else if (!alone && !edgeHeld() && freeFirst(provider?.options) && s.jwt && known && giftUsable(s, known, m)) { @@ -2421,7 +2427,7 @@ export async function ZCodeAuthPlugin({ client }, options = {}) { // it was. A refusal past this check is replayed on the plan // in the same request (replayOnPlan below). const raw = giftNames(known).find((x) => x.toLowerCase() === m.toLowerCase()) - orig = opts.body = JSON.stringify({ ...JSON.parse(orig), model: raw }) + orig = opts.body = JSON.stringify({ ...JSON.parse(orig), model: wireModel(raw) }) m = raw freeGift = true } diff --git a/packages/zcode/package.json b/packages/zcode/package.json index 37a44cb..06fa428 100644 --- a/packages/zcode/package.json +++ b/packages/zcode/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-zcode-auth", - "version": "0.1.46", + "version": "0.1.47", "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, and claims its gift plans daily", "type": "module", "main": "./opencode.mjs", diff --git a/packages/zcode/team-gift.test.mjs b/packages/zcode/team-gift.test.mjs index f996664..8078a54 100644 --- a/packages/zcode/team-gift.test.mjs +++ b/packages/zcode/team-gift.test.mjs @@ -96,7 +96,7 @@ test("ordinary models spend the team; trials spend the personal gift without tea expect(messages[1].headers.get("authorization")).toBe("Bearer " + jwt) expect(messages[1].headers.get("x-api-key")).toBeNull() expect(messages[1].headers.get("Bigmodel-Organization")).toBeNull() - expect(JSON.parse(messages[1].body).model).toBe("glm-5.3") + expect(JSON.parse(messages[1].body).model).toBe("GLM-5.3") }) test("free first is on by default for a team seat: the plain model spends the personal gift", async () => {