diff --git a/packages/grok/index.mjs b/packages/grok/index.mjs index d6a57ea..81df335 100644 --- a/packages/grok/index.mjs +++ b/packages/grok/index.mjs @@ -462,12 +462,24 @@ function modelOf(id) { // ---- sign-in ------------------------------------------------------------------ +// opensPage is whether `grok login --device-auth` opens its page in the +// browser itself, which it does wherever it can: on macOS (NSWorkspace) and +// Windows (ShellExecute) always, with nothing to turn it off; on Linux and +// the BSDs through $BROWSER, else xdg-open where a display is. There it is +// given a $BROWSER that opens nothing, and the one who asked (magpie, which +// knows where the user's browser is) opens it; here the sign-in says the +// page is open already, so it isn't opened a second time (andyfmyth on X: +// "每次都会弹两次验证"). +const opensPage = () => process.platform === "darwin" || process.platform === "win32" + // login runs `grok login --device-auth` in home and hands on the first link // it prints; done is settled when the command is. function login(exe, home) { + const env = envFor(home) + if (!opensPage()) env.BROWSER = "true" const child = spawn(exe, ["login", "--device-auth"], { cwd: homedir(), - env: envFor(home), + env, stdio: ["ignore", "pipe", "pipe"], }) const tail = [] @@ -533,6 +545,8 @@ async function signIn() { } return { url, + // not OpenCode's: the page is open already, by Grok Build itself + opened: opensPage(), instructions: "Approve the sign-in on the page; Grok Build finishes it.", method: "auto", callback: async () => { diff --git a/packages/grok/login.test.mjs b/packages/grok/login.test.mjs index 1981ed7..d33a66a 100644 --- a/packages/grok/login.test.mjs +++ b/packages/grok/login.test.mjs @@ -28,6 +28,7 @@ afterAll(() => { async function signIn(body) { const bin = mkdtempSync(join(dir, "bin-")) symlinkSync("/bin/sh", join(bin, "grok")) + symlinkSync("/bin/sh", join(bin, "grok.exe")) // as a test that says it is Windows looks for it writeFileSync(script, `${body}\n`) const env = { GROK_BIN_DIR: process.env.GROK_BIN_DIR, GROK_HOME: process.env.GROK_HOME } process.env.GROK_BIN_DIR = bin @@ -62,6 +63,43 @@ test.skipIf(process.platform === "win32")("a login's link is still handed on", a expect(a.url).toBe("https://accounts.x.ai/oauth2/device?user_code=AB12-CD34") }) +// `grok login --device-auth` opens its page itself where it can, and the +// sign-in's opener opened it too: two pages every time (andyfmyth on X). +// On macOS and Windows, where nothing turns Grok Build's off, the sign-in +// says the page is open already; elsewhere Grok Build is given a $BROWSER +// that opens nothing, and the page is the opener's to open. +async function signInOn(platform, body) { + const was = Object.getOwnPropertyDescriptor(process, "platform") + const browser = process.env.BROWSER + delete process.env.BROWSER + Object.defineProperty(process, "platform", { ...was, value: platform }) + try { + return await signIn(body) + } finally { + Object.defineProperty(process, "platform", was) + if (browser !== undefined) process.env.BROWSER = browser + } +} + +// the link carries the $BROWSER Grok Build was given +const saysBrowser = `echo "https://accounts.x.ai/oauth2/device?user_code=AB12-CD34&browser=$BROWSER"; exit 1` + +test.skipIf(process.platform === "win32")("on macOS and Windows the page Grok Build opened isn't opened again", async () => { + for (const os of ["darwin", "win32"]) { + const a = await signInOn(os, saysBrowser) + expect(a.opened).toBe(true) + expect(a.url).toBe("https://accounts.x.ai/oauth2/device?user_code=AB12-CD34&browser=") + } +}) + +test.skipIf(process.platform === "win32")("elsewhere Grok Build opens nothing and the page is the opener's", async () => { + for (const os of ["linux", "freebsd"]) { + const a = await signInOn(os, saysBrowser) + expect(a.opened).toBe(false) + expect(a.url).toBe("https://accounts.x.ai/oauth2/device?user_code=AB12-CD34&browser=true") + } +}) + // A login that ends signed in is kept, or says where it looked and what it // found (llllleo9 on 𝕏: "grok login finished without an account"). Run in a // sandbox against a stand-in auth.x.ai, grok 1.0.62 writes "email": null diff --git a/packages/grok/package.json b/packages/grok/package.json index c1a0511..f72969d 100644 --- a/packages/grok/package.json +++ b/packages/grok/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-grok-auth", - "version": "0.1.16", + "version": "0.1.17", "description": "OpenCode provider plugin: Grok (SuperGrok / X Premium+) through the Grok Build CLI's sign-in", "type": "module", "main": "./index.mjs",