@magpie-community/opencode-grok-auth 0.1.5 → 0.1.6
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/README.md +14 -3
- package/index.mjs +49 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -29,9 +29,20 @@ The CLI keeps the sign-in in its home, as `auth.json`. OpenCode (or
|
|
|
29
29
|
magpie) keeps where that home is, the token and its expiry.
|
|
30
30
|
|
|
31
31
|
The plugin only reads `auth.json`: the CLI's refresh gives a new token
|
|
32
|
-
each time.
|
|
33
|
-
|
|
34
|
-
|
|
32
|
+
each time. The plugin runs `grok models` in that home, so the CLI renews
|
|
33
|
+
it, then reads it again:
|
|
34
|
+
|
|
35
|
+
- magpie renews the token 5 minutes before it ends, through the plugin's
|
|
36
|
+
`auth.refresh`, once for the account and before its requests, models
|
|
37
|
+
and usage need it. magpie saves what the CLI gave.
|
|
38
|
+
- OpenCode doesn't call that hook: there the token is renewed when it is
|
|
39
|
+
5 minutes from expiring, before a request.
|
|
40
|
+
- The CLI runs once for a home at a time: a request or renewal that
|
|
41
|
+
comes while it runs waits for it and reads what it gave. A token the
|
|
42
|
+
CLI already renewed is taken as it is.
|
|
43
|
+
- When the CLI no longer holds a sign-in in that home (`grok logout`),
|
|
44
|
+
the account has to be signed in again. When it couldn't renew a token
|
|
45
|
+
that has ended, magpie tries again later.
|
|
35
46
|
|
|
36
47
|
## Requests
|
|
37
48
|
|
package/index.mjs
CHANGED
|
@@ -17,7 +17,8 @@ import { STATUS_CODES } from "node:http"
|
|
|
17
17
|
const PROVIDER = "grok"
|
|
18
18
|
const BASE = "https://cli-chat-proxy.grok.com/v1"
|
|
19
19
|
const CLIENT_VERSION = "1.0.41"
|
|
20
|
-
const REFRESH_MARGIN = 5 * 60 * 1000
|
|
20
|
+
const REFRESH_MARGIN = 5 * 60 * 1000 // a token this close to its end is renewed before a request
|
|
21
|
+
const LEAD_MS = REFRESH_MARGIN // and this close, magpie renews it ahead of time (auth.refresh)
|
|
21
22
|
const LINK_WAIT = 30 * 1000
|
|
22
23
|
const INSTALL = process.platform === "win32" ? "irm https://x.ai/cli/install.ps1 | iex" : "curl -fsSL https://x.ai/cli/install.sh | bash"
|
|
23
24
|
|
|
@@ -119,18 +120,32 @@ function run(file, args, opts) {
|
|
|
119
120
|
})
|
|
120
121
|
}
|
|
121
122
|
|
|
123
|
+
// refreshing runs the CLI's renewals one after another; renewing is the one
|
|
124
|
+
// asked for each home, which those who come while it is due or running join
|
|
125
|
+
// rather than running the CLI there again.
|
|
122
126
|
let refreshing = Promise.resolve()
|
|
127
|
+
const renewing = new Map()
|
|
128
|
+
|
|
129
|
+
// renew has the CLI renew the sign-in in home: a `grok models` there renews
|
|
130
|
+
// it as any use of the CLI does.
|
|
131
|
+
function renew(exe, home) {
|
|
132
|
+
let r = renewing.get(home)
|
|
133
|
+
if (!r) {
|
|
134
|
+
r = refreshing.then(() => run(exe, ["models"], { cwd: dirname(home), env: envFor(home) })).finally(() => renewing.delete(home))
|
|
135
|
+
refreshing = r.catch(() => {})
|
|
136
|
+
renewing.set(home, r)
|
|
137
|
+
}
|
|
138
|
+
return r
|
|
139
|
+
}
|
|
123
140
|
|
|
124
141
|
// token is the CLI's sign-in in home with a token still good. One about to
|
|
125
142
|
// expire (or that a request found expired) is first renewed by the CLI in
|
|
126
|
-
// its own home
|
|
143
|
+
// its own home.
|
|
127
144
|
async function token(home, expired) {
|
|
128
145
|
let c = credential(home)
|
|
129
146
|
const exe = executable()
|
|
130
147
|
if (c && exe && (expired || c.expires - Date.now() < REFRESH_MARGIN)) {
|
|
131
|
-
|
|
132
|
-
refreshing = done.catch(() => {})
|
|
133
|
-
await done
|
|
148
|
+
await renew(exe, home)
|
|
134
149
|
c = credential(home)
|
|
135
150
|
}
|
|
136
151
|
if (!c) throw new Error("Grok is not signed in; run `grok login`")
|
|
@@ -468,6 +483,35 @@ export const GrokAuthPlugin = async ({ client }) => {
|
|
|
468
483
|
|
|
469
484
|
auth: {
|
|
470
485
|
provider: PROVIDER,
|
|
486
|
+
// magpie renews the sign-in LEAD_MS before its end, once for the
|
|
487
|
+
// account, before its requests, models and usage ask for it: the CLI
|
|
488
|
+
// renews it in its home, as before a request, and what it gives is
|
|
489
|
+
// read back. The check before each request stays for OpenCode, which
|
|
490
|
+
// doesn't call this.
|
|
491
|
+
refreshLead: LEAD_MS,
|
|
492
|
+
async refresh(auth) {
|
|
493
|
+
const home = auth?.refresh
|
|
494
|
+
if (auth?.type !== "oauth" || !home) return undefined
|
|
495
|
+
let c = credential(home)
|
|
496
|
+
// renewed already (by the CLI, or before a request), the store not
|
|
497
|
+
// yet saying so
|
|
498
|
+
const renewed = c && c.key !== auth.access && c.expires - Date.now() >= LEAD_MS
|
|
499
|
+
const exe = executable()
|
|
500
|
+
if (!renewed && exe) {
|
|
501
|
+
await renew(exe, home)
|
|
502
|
+
c = credential(home)
|
|
503
|
+
}
|
|
504
|
+
// the CLI holds no sign-in there any more (`grok logout`): one
|
|
505
|
+
// has to be made again
|
|
506
|
+
if (!c) throw Object.assign(new Error("Grok is not signed in; run `grok login`"), { signIn: "expired" })
|
|
507
|
+
// the CLI couldn't renew it: tried again later
|
|
508
|
+
if (exe && c.expires && c.expires <= Date.now()) throw new Error("Grok's sign-in has expired and Grok Build couldn't renew it")
|
|
509
|
+
if (c.key === auth.access && c.expires === auth.expires) return undefined
|
|
510
|
+
// magpie saves what this gives
|
|
511
|
+
const out = { access: c.key, expires: c.expires }
|
|
512
|
+
if (c.email && c.email !== auth.accountId) out.accountId = c.email
|
|
513
|
+
return out
|
|
514
|
+
},
|
|
471
515
|
async loader(getAuth) {
|
|
472
516
|
const auth = await getAuth()
|
|
473
517
|
if (!auth || auth.type !== "oauth") return {}
|
package/package.json
CHANGED