diff --git a/packages/grok/index.mjs b/packages/grok/index.mjs index 45d545a..7d9cfbf 100644 --- a/packages/grok/index.mjs +++ b/packages/grok/index.mjs @@ -29,6 +29,12 @@ const TOOLS = new Set(["function", "web_search", "x_search", "image_generation", const ANSI = /\x1b\[[0-9;?]*[A-Za-z]/g const LINK = /https:\/\/\S+/ +// linkIn is the page a line of `grok login` gives to open, "" for none: a +// line saying it failed names the endpoint it couldn't reach (`Error: error +// sending request for url (https://auth.x.ai/oauth2/device/code): …`), +// which is no page to open. +const linkIn = (line) => (/\berror\b/i.test(line) ? "" : (line.match(LINK)?.[0] ?? "")) + // ---- the CLI ------------------------------------------------------------------ // cliHome is where the CLI keeps its own sign-in and settings. @@ -300,26 +306,30 @@ function login(exe, home, own) { const line = buf.slice(0, i).replace(/\r$/, "").replace(ANSI, "").trim() buf = buf.slice(i + 1) if (line) tail.push(line) - const u = line.match(LINK)?.[0] + const u = linkIn(line) if (u) found(u) } } child.stdout.on("data", onData) child.stderr.on("data", onData) const done = new Promise((resolve) => { - child.on("error", (e) => resolve({ ok: false, why: e.message })) + child.on("error", (e) => { + tail.push(e.message) + found("") + resolve({ ok: false, why: e.message }) + }) child.on("close", (code) => { const rest = buf.replace(ANSI, "").trim() if (rest) { tail.push(rest) - const u = rest.match(LINK)?.[0] + const u = linkIn(rest) if (u) found(u) } found("") resolve({ ok: code === 0, why: tail.at(-1) ?? "grok login didn't finish" }) }) }) - return { child, link, done } + return { child, link, done, tail } } function newHome() { @@ -336,12 +346,15 @@ async function signIn() { if (!exe) throw new Error(`install Grok Build first: ${INSTALL}`) const own = !!credential(cliHome()) const home = own ? newHome() : cliHome() - const { child, link, done } = login(exe, home, own) + const { child, link, done, tail } = login(exe, home, own) const url = await Promise.race([link, new Promise((r) => setTimeout(() => r(""), LINK_WAIT))]) if (!url) { child.kill() if (own) rmSync(home, { recursive: true, force: true }) - throw new Error("grok login gave no link to open") + // what it said last is why: where x.ai is out of reach without a + // proxy, it says it couldn't reach it (𝕏 on Discord) + const why = tail.at(-1) + throw new Error(why ? `grok login gave no link to open: ${why}` : "grok login gave no link to open and said nothing: can this machine reach auth.x.ai? Set a proxy in magpie's Settings if it needs one") } return { url, diff --git a/packages/grok/login.test.mjs b/packages/grok/login.test.mjs new file mode 100644 index 0000000..1d238bd --- /dev/null +++ b/packages/grok/login.test.mjs @@ -0,0 +1,48 @@ +// A `grok login` that prints no link says why (𝕏 on Discord): the sign-in +// said only "grok login gave no link to open", where the CLI had said it +// couldn't reach x.ai. +import { afterAll, expect, test } from "bun:test" +import { chmodSync, mkdtempSync, rmSync, writeFileSync } from "node:fs" +import { tmpdir } from "node:os" +import { join } from "node:path" +import { GrokAuthPlugin } from "./index.mjs" + +const dir = mkdtempSync(join(tmpdir(), "grok-login-")) +afterAll(() => rmSync(dir, { recursive: true, force: true })) + +async function signIn(script) { + const bin = mkdtempSync(join(dir, "bin-")) + writeFileSync(join(bin, "grok"), `#!/bin/sh\n${script}\n`) + chmodSync(join(bin, "grok"), 0o755) + const env = { GROK_BIN_DIR: process.env.GROK_BIN_DIR, GROK_HOME: process.env.GROK_HOME } + process.env.GROK_BIN_DIR = bin + process.env.GROK_HOME = mkdtempSync(join(dir, "home-")) + try { + const hooks = await GrokAuthPlugin({ client: { auth: { set: async () => {} } } }) + return await hooks.auth.methods[0].authorize() + } finally { + for (const [k, v] of Object.entries(env)) v === undefined ? delete process.env[k] : (process.env[k] = v) + } +} + +test.skipIf(process.platform === "win32")("a login that fails says what the CLI said", async () => { + await expect(signIn(`printf 'Starting sign-in\\n\\033[31merror: request to auth.x.ai timed out\\033[0m\\n' >&2; exit 1`)).rejects.toThrow( + "grok login gave no link to open: error: request to auth.x.ai timed out", + ) +}) + +// as grok 1.0.46 says a proxy it can't get through: the URL in it is the +// endpoint, not a page to open +test.skipIf(process.platform === "win32")("the endpoint a failed login names is no link", async () => { + const said = "Error: error sending request for url (https://auth.x.ai/oauth2/device/code): client error (Connect): tunnel error: failed to create underlying connection: tcp connect error: Connection refused (os error 61)" + await expect(signIn(`echo '${said}' >&2; exit 1`)).rejects.toThrow(`grok login gave no link to open: ${said}`) +}) + +test.skipIf(process.platform === "win32")("a login that says nothing asks whether x.ai is reachable", async () => { + await expect(signIn("exit 1")).rejects.toThrow("can this machine reach auth.x.ai?") +}) + +test.skipIf(process.platform === "win32")("a login's link is still handed on", async () => { + const a = await signIn(`echo 'To sign in, open this URL in your browser:'; echo ' https://accounts.x.ai/oauth2/device?user_code=AB12-CD34'; exit 1`) + expect(a.url).toBe("https://accounts.x.ai/oauth2/device?user_code=AB12-CD34") +}) diff --git a/packages/grok/package.json b/packages/grok/package.json index 3c6b158..097705c 100644 --- a/packages/grok/package.json +++ b/packages/grok/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-grok-auth", - "version": "0.1.7", + "version": "0.1.8", "description": "OpenCode provider plugin: Grok (SuperGrok / X Premium+) through the Grok Build CLI's sign-in", "type": "module", "main": "./index.mjs",