@magpie-community/opencode-grok-auth 0.1.7 → 0.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.mjs +19 -6
- package/package.json +1 -1
package/index.mjs
CHANGED
|
@@ -29,6 +29,12 @@ const TOOLS = new Set(["function", "web_search", "x_search", "image_generation",
|
|
|
29
29
|
const ANSI = /\x1b\[[0-9;?]*[A-Za-z]/g
|
|
30
30
|
const LINK = /https:\/\/\S+/
|
|
31
31
|
|
|
32
|
+
// linkIn is the page a line of `grok login` gives to open, "" for none: a
|
|
33
|
+
// line saying it failed names the endpoint it couldn't reach (`Error: error
|
|
34
|
+
// sending request for url (https://auth.x.ai/oauth2/device/code): …`),
|
|
35
|
+
// which is no page to open.
|
|
36
|
+
const linkIn = (line) => (/\berror\b/i.test(line) ? "" : (line.match(LINK)?.[0] ?? ""))
|
|
37
|
+
|
|
32
38
|
// ---- the CLI ------------------------------------------------------------------
|
|
33
39
|
|
|
34
40
|
// cliHome is where the CLI keeps its own sign-in and settings.
|
|
@@ -300,26 +306,30 @@ function login(exe, home, own) {
|
|
|
300
306
|
const line = buf.slice(0, i).replace(/\r$/, "").replace(ANSI, "").trim()
|
|
301
307
|
buf = buf.slice(i + 1)
|
|
302
308
|
if (line) tail.push(line)
|
|
303
|
-
const u = line
|
|
309
|
+
const u = linkIn(line)
|
|
304
310
|
if (u) found(u)
|
|
305
311
|
}
|
|
306
312
|
}
|
|
307
313
|
child.stdout.on("data", onData)
|
|
308
314
|
child.stderr.on("data", onData)
|
|
309
315
|
const done = new Promise((resolve) => {
|
|
310
|
-
child.on("error", (e) =>
|
|
316
|
+
child.on("error", (e) => {
|
|
317
|
+
tail.push(e.message)
|
|
318
|
+
found("")
|
|
319
|
+
resolve({ ok: false, why: e.message })
|
|
320
|
+
})
|
|
311
321
|
child.on("close", (code) => {
|
|
312
322
|
const rest = buf.replace(ANSI, "").trim()
|
|
313
323
|
if (rest) {
|
|
314
324
|
tail.push(rest)
|
|
315
|
-
const u = rest
|
|
325
|
+
const u = linkIn(rest)
|
|
316
326
|
if (u) found(u)
|
|
317
327
|
}
|
|
318
328
|
found("")
|
|
319
329
|
resolve({ ok: code === 0, why: tail.at(-1) ?? "grok login didn't finish" })
|
|
320
330
|
})
|
|
321
331
|
})
|
|
322
|
-
return { child, link, done }
|
|
332
|
+
return { child, link, done, tail }
|
|
323
333
|
}
|
|
324
334
|
|
|
325
335
|
function newHome() {
|
|
@@ -336,12 +346,15 @@ async function signIn() {
|
|
|
336
346
|
if (!exe) throw new Error(`install Grok Build first: ${INSTALL}`)
|
|
337
347
|
const own = !!credential(cliHome())
|
|
338
348
|
const home = own ? newHome() : cliHome()
|
|
339
|
-
const { child, link, done } = login(exe, home, own)
|
|
349
|
+
const { child, link, done, tail } = login(exe, home, own)
|
|
340
350
|
const url = await Promise.race([link, new Promise((r) => setTimeout(() => r(""), LINK_WAIT))])
|
|
341
351
|
if (!url) {
|
|
342
352
|
child.kill()
|
|
343
353
|
if (own) rmSync(home, { recursive: true, force: true })
|
|
344
|
-
|
|
354
|
+
// what it said last is why: where x.ai is out of reach without a
|
|
355
|
+
// proxy, it says it couldn't reach it (𝕏 on Discord)
|
|
356
|
+
const why = tail.at(-1)
|
|
357
|
+
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")
|
|
345
358
|
}
|
|
346
359
|
return {
|
|
347
360
|
url,
|
package/package.json
CHANGED