grok 0.1.8: a grok login that prints no link says why, and the endpoint a failed one names isn't taken for the page to open (𝕏 on Discord) — the sign-in said only "grok login gave no link to open", dropping what the CLI said; it now ends with the CLI's last line, or asks whether auth.x.ai is reachable when it said nothing. grok 1.0.46, unable to reach x.ai, prints "Error: error sending request for url (https://auth.x.ai/oauth2/device/code): …", whose URL was handed on as the link; a line saying error gives none. A spawn that fails settles the link at once instead of after 30s. login.test.mjs is new (fails on 0.1.7); bun test and check.mjs pass. Checked with grok 1.0.46 itself through magpie's plugin host, given a proxy that refuses: the error now names it.

This commit is contained in:
yetone
2026-10-05 22:10:57 +08:00
parent 82330dfa3f
commit 719ba0c621
3 changed files with 68 additions and 7 deletions
+19 -6
View File
@@ -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,
+48
View File
@@ -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")
})
+1 -1
View File
@@ -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",