@magpie-community/opencode-grok-auth 0.1.1 → 0.1.3
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 +81 -29
- package/package.json +1 -1
package/index.mjs
CHANGED
|
@@ -174,6 +174,13 @@ async function sign(headers, key) {
|
|
|
174
174
|
headers.set("User-Agent", `grok-shell/${v} (${os}; ${arch})`)
|
|
175
175
|
}
|
|
176
176
|
|
|
177
|
+
// bodyText is a request body handed over as bytes read as the text it
|
|
178
|
+
// is, so it can be reshaped; any other body is as it came.
|
|
179
|
+
function bodyText(body) {
|
|
180
|
+
if (body instanceof ArrayBuffer || ArrayBuffer.isView(body)) return new TextDecoder().decode(body)
|
|
181
|
+
return body
|
|
182
|
+
}
|
|
183
|
+
|
|
177
184
|
// rewrite leaves out what Grok's backend doesn't take: tools of other
|
|
178
185
|
// types (a freeform apply_patch, a namespace of sub-agent tools), a web
|
|
179
186
|
// search's external_web_access, a tool_choice naming a dropped type, and a
|
|
@@ -353,16 +360,52 @@ const RFC3339 = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:\d{2})
|
|
|
353
360
|
|
|
354
361
|
// statusText is a refused request's status as magpie says it (Go's
|
|
355
362
|
// http.StatusText).
|
|
363
|
+
// http.StatusText); one Go has no words for is "HTTP <n>", never empty.
|
|
356
364
|
const statusText = (s) =>
|
|
357
|
-
(
|
|
365
|
+
((GO_TEXT[s] ?? STATUS_CODES[s]) || `HTTP ${s}`)
|
|
366
|
+
// where Go's words differ from Node's (509 Go has none for)
|
|
367
|
+
const GO_TEXT = { 413: "Request Entity Too Large", 414: "Request URI Too Long", 416: "Requested Range Not Satisfiable", 418: "I'm a teapot", 509: "" }
|
|
368
|
+
|
|
369
|
+
// netError is a request that got no answer, as Go's http client words it
|
|
370
|
+
// (`Get "<url>": …`), with the words magpie's keepLast looks for to keep
|
|
371
|
+
// the card it had: no such host, connection refused, timeout, EOF.
|
|
372
|
+
function netError(url, e) {
|
|
373
|
+
const m = String(e?.message ?? e)
|
|
374
|
+
const code = String(e?.code ?? e?.cause?.code ?? "")
|
|
375
|
+
let why = m
|
|
376
|
+
if (e?.name === "TimeoutError" || e?.name === "AbortError" || /timed? ?out/i.test(m)) why = "timeout"
|
|
377
|
+
else if (code === "ENOTFOUND" || code === "EAI_AGAIN" || /getaddrinfo|ENOTFOUND|resolve|DNS/i.test(m)) why = "dial tcp: lookup: no such host"
|
|
378
|
+
else if (code === "ECONNREFUSED" || /ECONNREFUSED|Unable to connect|refused/i.test(m)) why = "dial tcp: connection refused"
|
|
379
|
+
else if (/ECONNRESET|socket|closed|fetch failed/i.test(m + code)) why = "EOF"
|
|
380
|
+
return `Get "${url}": ${why}`
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
// jsonError is JSON that won't read, as Go's encoding/json says it.
|
|
384
|
+
function jsonError(text) {
|
|
385
|
+
const c = text.trimStart()[0]
|
|
386
|
+
return c === undefined ? "unexpected end of JSON input" : `invalid character '${c}' looking for beginning of value`
|
|
387
|
+
}
|
|
358
388
|
|
|
359
389
|
async function usage(key) {
|
|
360
|
-
const
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
390
|
+
const url = `${BASE}/billing?format=credits`
|
|
391
|
+
let res, text
|
|
392
|
+
try {
|
|
393
|
+
res = await fetch(url, {
|
|
394
|
+
headers: { Authorization: `Bearer ${key}`, Accept: "application/json" },
|
|
395
|
+
signal: AbortSignal.timeout(15_000),
|
|
396
|
+
})
|
|
397
|
+
if (res.ok) text = await res.text()
|
|
398
|
+
} catch (e) {
|
|
399
|
+
return { error: netError(url, e), windows: [] }
|
|
400
|
+
}
|
|
364
401
|
if (!res.ok) return { error: statusText(res.status), windows: [] }
|
|
365
|
-
|
|
402
|
+
let data
|
|
403
|
+
try {
|
|
404
|
+
data = JSON.parse(text)
|
|
405
|
+
} catch {
|
|
406
|
+
return { error: jsonError(text), windows: [] }
|
|
407
|
+
}
|
|
408
|
+
const cfg = data?.config ?? {}
|
|
366
409
|
const num = (v) => (typeof v === "number" ? v : 0)
|
|
367
410
|
let [name, span] = ["Allowance", 0]
|
|
368
411
|
let end = cfg.billingPeriodEnd
|
|
@@ -377,6 +420,15 @@ async function usage(key) {
|
|
|
377
420
|
return { windows: out }
|
|
378
421
|
}
|
|
379
422
|
|
|
423
|
+
// kept is res saying magpie is to leave the account's sign-in be.
|
|
424
|
+
function kept(res) {
|
|
425
|
+
const headers = new Headers(res.headers)
|
|
426
|
+
headers.delete("content-length")
|
|
427
|
+
headers.delete("content-encoding")
|
|
428
|
+
headers.set("X-Magpie-Sign-In", "kept")
|
|
429
|
+
return new Response(res.body, { status: res.status, statusText: res.statusText, headers })
|
|
430
|
+
}
|
|
431
|
+
|
|
380
432
|
// ---- the plugin ---------------------------------------------------------------
|
|
381
433
|
|
|
382
434
|
export const GrokAuthPlugin = async ({ client }) => {
|
|
@@ -411,26 +463,24 @@ export const GrokAuthPlugin = async ({ client }) => {
|
|
|
411
463
|
const home = auth?.refresh
|
|
412
464
|
if (!home) throw new Error("Grok is not signed in; run `grok login`")
|
|
413
465
|
const req = new Request(input, init)
|
|
414
|
-
const body = rewrite(init?.body)
|
|
466
|
+
const body = rewrite(bodyText(init?.body))
|
|
415
467
|
let parsed = {}
|
|
416
468
|
if (typeof body === "string") {
|
|
417
469
|
try {
|
|
418
470
|
parsed = JSON.parse(body) ?? {}
|
|
419
471
|
} catch {}
|
|
420
472
|
}
|
|
421
|
-
const
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
if (res.status !== 401) return res
|
|
433
|
-
return send(true)
|
|
473
|
+
const c = await token(home, false)
|
|
474
|
+
await remember(auth, c)
|
|
475
|
+
const headers = new Headers(req.headers)
|
|
476
|
+
await sign(headers, c.key)
|
|
477
|
+
if (typeof parsed.model === "string" && parsed.model) headers.set("x-grok-model-override", parsed.model)
|
|
478
|
+
if (typeof parsed.prompt_cache_key === "string" && parsed.prompt_cache_key) headers.set("x-grok-conv-id", parsed.prompt_cache_key)
|
|
479
|
+
headers.delete("content-length")
|
|
480
|
+
// sent once, as the built-in sent it: Grok's answer goes on as
|
|
481
|
+
// it came, the account kept, as the built-in never marked a Grok
|
|
482
|
+
// account lapsed nor cleared one
|
|
483
|
+
return kept(await fetch(req.url, { ...init, method: req.method, headers, body }))
|
|
434
484
|
},
|
|
435
485
|
}
|
|
436
486
|
},
|
|
@@ -446,7 +496,9 @@ export const GrokAuthPlugin = async ({ client }) => {
|
|
|
446
496
|
authorize: current,
|
|
447
497
|
},
|
|
448
498
|
],
|
|
449
|
-
// magpie's: how much of the subscription's allowance is gone
|
|
499
|
+
// magpie's: how much of the subscription's allowance is gone; each
|
|
500
|
+
// read keeps the sign-in (signIn), as the built-in's marked no
|
|
501
|
+
// account lapsed nor cleared one
|
|
450
502
|
async usage(getAuth) {
|
|
451
503
|
const auth = await getAuth()
|
|
452
504
|
let c
|
|
@@ -454,10 +506,10 @@ export const GrokAuthPlugin = async ({ client }) => {
|
|
|
454
506
|
if (!auth || auth.type !== "oauth" || !auth.refresh) throw new Error("Grok is not signed in; run `grok login`")
|
|
455
507
|
c = await token(auth.refresh, false)
|
|
456
508
|
} catch (e) {
|
|
457
|
-
return { error: e.message, windows: [] }
|
|
509
|
+
return { error: e.message, windows: [], signIn: "kept" }
|
|
458
510
|
}
|
|
459
511
|
await remember(auth, c)
|
|
460
|
-
return usage(c.key)
|
|
512
|
+
return { ...(await usage(c.key)), signIn: "kept" }
|
|
461
513
|
},
|
|
462
514
|
},
|
|
463
515
|
|
|
@@ -466,12 +518,9 @@ export const GrokAuthPlugin = async ({ client }) => {
|
|
|
466
518
|
async models(provider, { auth } = {}) {
|
|
467
519
|
const have = provider?.models ?? {}
|
|
468
520
|
if (!auth || auth.type !== "oauth" || !auth.refresh) return have
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
} catch {
|
|
473
|
-
return have
|
|
474
|
-
}
|
|
521
|
+
// a list Grok couldn't give is a failure, not the few configured:
|
|
522
|
+
// magpie keeps the list it had
|
|
523
|
+
const list = await listModels((await token(auth.refresh, false)).key)
|
|
475
524
|
const base = have["grok-4.7"] ?? Object.values(have)[0] ?? {}
|
|
476
525
|
const out = {}
|
|
477
526
|
for (const m of list) {
|
|
@@ -498,3 +547,6 @@ export const GrokAuthPlugin = async ({ client }) => {
|
|
|
498
547
|
},
|
|
499
548
|
}
|
|
500
549
|
}
|
|
550
|
+
|
|
551
|
+
// for tests
|
|
552
|
+
export const _internal = { rewrite, bodyText }
|
package/package.json
CHANGED