diff --git a/packages/grok/index.mjs b/packages/grok/index.mjs index e3dd87b..cb873f4 100644 --- a/packages/grok/index.mjs +++ b/packages/grok/index.mjs @@ -249,6 +249,10 @@ async function listModels(key) { await sign(headers, key) const res = await fetch(`${BASE}/models`, { headers }) const text = await res.text() + // a token Grok turned away before its time (signed out elsewhere) is a + // sign-in gone, said as one past its time is: a move takes it along + // untried rather than stopping on it + if (res.status === 401) throw new Error("Grok's sign-in has expired; run `grok login`") if (!res.ok) throw new Error(`Grok models: ${res.status} ${text.slice(0, 200)}`) const out = [] for (const d of JSON.parse(text)?.data ?? []) { diff --git a/packages/grok/models.test.mjs b/packages/grok/models.test.mjs index f5f0081..f0892c0 100644 --- a/packages/grok/models.test.mjs +++ b/packages/grok/models.test.mjs @@ -13,3 +13,25 @@ test("the configured grok-4.7 carries the built-in's levels", async () => { expect(Object.keys(m.variants)).toEqual(["low", "medium", "high", "xhigh"]) expect(m.variants.xhigh).toEqual({ reasoningEffort: "xhigh" }) }) + +// A token Grok refuses before its time says the sign-in is gone, as one +// past its time does, so magpie's move takes the account along untried; +// "Grok models: 401 …" stopped every Grok account's move. +test("a refused token says the sign-in has expired", async () => { + const { mkdtempSync, writeFileSync } = await import("node:fs") + const { tmpdir } = await import("node:os") + const { join } = await import("node:path") + const home = mkdtempSync(join(tmpdir(), "grok-models-")) + writeFileSync(join(home, "auth.json"), JSON.stringify({ "https://auth.x.ai": { key: "revoked", expires_at: "2099-01-01T00:00:00Z", email: "a@x.ai" } })) + const [path, h, fetch0] = [process.env.PATH, process.env.HOME, globalThis.fetch] + process.env.PATH = "" + process.env.HOME = home + globalThis.fetch = async () => new Response('{"error":"Invalid or expired credentials"}', { status: 401 }) + try { + const hooks = await GrokAuthPlugin({ client: { auth: { set: async () => {} } } }) + const err = await hooks.provider.models({ id: "grok", models: {} }, { auth: { type: "oauth", refresh: home } }).catch((e) => e) + expect(err?.message).toMatch(/sign-in has expired/) + } finally { + ;[process.env.PATH, process.env.HOME, globalThis.fetch] = [path, h, fetch0] + } +}) diff --git a/packages/grok/package.json b/packages/grok/package.json index 6844ec9..21d84bf 100644 --- a/packages/grok/package.json +++ b/packages/grok/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-grok-auth", - "version": "0.1.4", + "version": "0.1.5", "description": "OpenCode provider plugin: Grok (SuperGrok / X Premium+) through the Grok Build CLI's sign-in", "type": "module", "main": "./index.mjs",