diff --git a/packages/zed/README.md b/packages/zed/README.md index a81ec61..fd19f9b 100644 --- a/packages/zed/README.md +++ b/packages/zed/README.md @@ -67,7 +67,11 @@ GitHub login. - **After sign-in:** the account's own list comes from Zed's `GET /models`, read when you sign in and every 10 minutes after. Disabled models are - left out. Each model's effort levels become its variants. + left out. Each model's effort levels become its variants. When a read + fails, the list read last is kept; when none was ever read (Zed down, + or a list with nothing callable, as for an organization without an + active subscription), the hook throws why, and magpie shows that reason + with the account, the declared models marked as not its list (#229). - **Before sign-in:** the config hook declares one model of each family (Claude Sonnet 5, Opus 5.5, Haiku 4.5, GPT-5.5, Gemini 3.5 Flash, Grok 4.7), which the account's list replaces. diff --git a/packages/zed/index.mjs b/packages/zed/index.mjs index dc08ad3..4aba52d 100644 --- a/packages/zed/index.mjs +++ b/packages/zed/index.mjs @@ -1029,7 +1029,14 @@ export async function ZedAuthPlugin({ client } = {}) { // ZedToken → zedLapse), so magpie is told as an answer's // X-Magpie-Sign-In would tell it if (e instanceof SignInExpired) throw Object.assign(e, { signIn: "expired" }) - if (!l) return provider.models + // never read for this account: the six declared before sign-in + // aren't its list, nor their limits its limits, so say why + // there is none rather than hand them back as if they were. + // A 200 that lists nothing callable (every model disabled, an + // organization without a subscription) is a failure too, which + // a host judging by the response's status alone wouldn't see + // (#229, ReneXiong). A list read before is kept below. + if (!l) throw /^Zed\b/.test(e?.message ?? "") ? e : new Error(`Zed: this account's models couldn't be read: ${e?.message ?? e}`) } } return Object.fromEntries(l.models.map((m) => [m.id, model(m)])) diff --git a/packages/zed/models.test.mjs b/packages/zed/models.test.mjs index 19341a8..d88de86 100644 --- a/packages/zed/models.test.mjs +++ b/packages/zed/models.test.mjs @@ -42,19 +42,52 @@ test("the model token refused: the sign-in expired, as zedLapse marked it", asyn expect(seen).toEqual(["/client/llm_tokens"]) }) -test("the list refused with the token just minted: kept, the declared models", async () => { +test("the list refused with the token just minted: no word on the sign-in, and no list", async () => { // a stale token is minted again once; /models refusing both is no word on - // the account's own sign-in, which the built-in left unmarked + // the account's own sign-in, which the built-in left unmarked. The + // declared models aren't handed back as the account's (#229) const { run, seen } = await models(auth(), (p) => p === "/client/llm_tokens" ? new Response(JSON.stringify({ token: "tok" })) : new Response("nope", { status: 401 }), ) - expect(await run()).toEqual({ declared: {} }) + const err = await run().catch((e) => e) + expect(err).toBeInstanceOf(Error) + expect(err.signIn).toBeUndefined() + expect(err.message).toContain("(401)") expect(seen).toEqual(["/client/llm_tokens", "/models", "/client/llm_tokens", "/models"]) }) -test("Zed down: no word on the sign-in, the declared models", async () => { +test("Zed down before any list: the failure, not the declared models", async () => { const { run } = await models(auth(), () => new Response("bad gateway", { status: 502 })) - expect(await run()).toEqual({ declared: {} }) + const err = await run().catch((e) => e) + expect(err).toBeInstanceOf(Error) + expect(err.signIn).toBeUndefined() + expect(err.message).toContain("bad gateway") +}) + +// #229 (ReneXiong): an organization without an active subscription gets a +// 200 whose models are all disabled. The plugin handed back the six +// declared before sign-in, with their limits, and a host judging by the +// 200 showed them as the account's list with no error +test("a 200 listing nothing callable: says so, not the declared models", async () => { + const off = JSON.stringify({ models: [{ provider: "anthropic", id: "claude-sonnet-5", is_disabled: true, max_token_count: 1000, max_output_tokens: 100 }] }) + const { run } = await models(auth(), (p) => (p === "/client/llm_tokens" ? new Response(JSON.stringify({ token: "tok" })) : new Response(off))) + const err = await run().catch((e) => e) + expect(err).toBeInstanceOf(Error) + expect(err.message).toContain("Zed lists no models for this account") +}) + +test("Zed down after a list was read: the list read last", async () => { + const a = auth() + let down = false + const { run } = await models(a, (p) => { + if (down) return new Response("bad gateway", { status: 502 }) + return p === "/client/llm_tokens" ? new Response(JSON.stringify({ token: "tok" })) : new Response(list) + }) + expect(Object.keys(await run())).toEqual(["claude-sonnet-5"]) + const uid = JSON.parse(a.refresh).userId + _internal.lists.set(uid, { at: 0, models: _internal.lists.get(uid).models }) + down = true + expect(Object.keys(await run())).toEqual(["claude-sonnet-5"]) }) test("a list read: the account's models", async () => { diff --git a/packages/zed/package.json b/packages/zed/package.json index 17262c4..006c675 100644 --- a/packages/zed/package.json +++ b/packages/zed/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-zed-auth", - "version": "0.1.8", + "version": "0.1.9", "description": "Sign in to a Zed subscription (Zed Pro, trial, student, business) from OpenCode or magpie", "type": "module", "main": "./index.mjs",