@magpie-community/opencode-grok-auth 0.1.2 → 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 +25 -16
- package/package.json +1 -1
package/index.mjs
CHANGED
|
@@ -420,6 +420,15 @@ async function usage(key) {
|
|
|
420
420
|
return { windows: out }
|
|
421
421
|
}
|
|
422
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
|
+
|
|
423
432
|
// ---- the plugin ---------------------------------------------------------------
|
|
424
433
|
|
|
425
434
|
export const GrokAuthPlugin = async ({ client }) => {
|
|
@@ -461,19 +470,17 @@ export const GrokAuthPlugin = async ({ client }) => {
|
|
|
461
470
|
parsed = JSON.parse(body) ?? {}
|
|
462
471
|
} catch {}
|
|
463
472
|
}
|
|
464
|
-
const
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
if (res.status !== 401) return res
|
|
476
|
-
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 }))
|
|
477
484
|
},
|
|
478
485
|
}
|
|
479
486
|
},
|
|
@@ -489,7 +496,9 @@ export const GrokAuthPlugin = async ({ client }) => {
|
|
|
489
496
|
authorize: current,
|
|
490
497
|
},
|
|
491
498
|
],
|
|
492
|
-
// 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
|
|
493
502
|
async usage(getAuth) {
|
|
494
503
|
const auth = await getAuth()
|
|
495
504
|
let c
|
|
@@ -497,10 +506,10 @@ export const GrokAuthPlugin = async ({ client }) => {
|
|
|
497
506
|
if (!auth || auth.type !== "oauth" || !auth.refresh) throw new Error("Grok is not signed in; run `grok login`")
|
|
498
507
|
c = await token(auth.refresh, false)
|
|
499
508
|
} catch (e) {
|
|
500
|
-
return { error: e.message, windows: [] }
|
|
509
|
+
return { error: e.message, windows: [], signIn: "kept" }
|
|
501
510
|
}
|
|
502
511
|
await remember(auth, c)
|
|
503
|
-
return usage(c.key)
|
|
512
|
+
return { ...(await usage(c.key)), signIn: "kept" }
|
|
504
513
|
},
|
|
505
514
|
},
|
|
506
515
|
|
package/package.json
CHANGED