@magpie-community/opencode-grok-auth 0.1.6 → 0.1.7

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.
Files changed (3) hide show
  1. package/README.md +12 -0
  2. package/index.mjs +58 -3
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -71,3 +71,15 @@ leaves these out:
71
71
  Grok 4.7 until signed in. Signed in, the models are the ones the account
72
72
  lists at `/v1/models` (Responses models only), each with its context
73
73
  window and its reasoning efforts as variants.
74
+
75
+ ## Usage
76
+
77
+ magpie's card shows the credits of the current period
78
+ (`/v1/billing?format=credits`): weekly for SuperGrok, with on-demand
79
+ spending beside it when it has a cap.
80
+
81
+ Grok's billing says nothing of a plan's rate limit, so a free account can
82
+ read 0% used while its requests come back 429. After a 429 the card shows
83
+ a spent "Rate limit" window until the reset it named (`Retry-After`, or a
84
+ `*ratelimit*reset*` header). When it named none, the window stays until a
85
+ request goes through, or for 5 minutes.
package/index.mjs CHANGED
@@ -451,6 +451,57 @@ async function usage(key) {
451
451
  return { windows: out }
452
452
  }
453
453
 
454
+ // ---- rate limit -------------------------------------------------------------------
455
+
456
+ // Grok's billing says nothing of a plan's rate limit: a free account's
457
+ // credits read 0% used while every request comes back 429. So the account's
458
+ // last 429 is kept here, by its CLI home, and usage shows a "Rate limit"
459
+ // window spent until the reset the 429 named (Retry-After, or a
460
+ // *ratelimit*reset* header), or, when it named none, until a request goes
461
+ // through or HOLD_MS has passed. Aside: magpie's gateway already rests an
462
+ // account that answered 429; this is only what the card says.
463
+ const HOLD_MS = 5 * 60_000
464
+ const limited = new Map()
465
+
466
+ // resetOf is when a 429 says the limit lifts, in ms since the epoch, or 0.
467
+ function resetOf(headers, now) {
468
+ const ra = headers.get("retry-after")
469
+ if (ra) {
470
+ const n = Number(ra)
471
+ if (Number.isFinite(n) && n >= 0) return now + n * 1000
472
+ const d = Date.parse(ra)
473
+ if (!Number.isNaN(d)) return d
474
+ }
475
+ for (const [k, v] of headers) {
476
+ if (!/ratelimit.*reset/i.test(k)) continue
477
+ const n = Number(v)
478
+ if (!Number.isFinite(n) || n < 0) continue
479
+ // a time (epoch ms or seconds) or seconds from now
480
+ if (n > 1e12) return n
481
+ if (n > 1e9) return n * 1000
482
+ return now + n * 1000
483
+ }
484
+ return 0
485
+ }
486
+
487
+ // note keeps what a request's answer says of the rate limit.
488
+ function note(home, res, now = Date.now()) {
489
+ if (res.status === 429) limited.set(home, { at: now, until: resetOf(res.headers, now) })
490
+ else if (res.ok) limited.delete(home)
491
+ }
492
+
493
+ // rateWindow is the window a recent 429 makes, or none.
494
+ function rateWindow(home, now = Date.now()) {
495
+ const l = limited.get(home)
496
+ if (!l) return []
497
+ if (l.until ? l.until <= now : now - l.at >= HOLD_MS) {
498
+ limited.delete(home)
499
+ return []
500
+ }
501
+ const end = l.until || l.at + HOLD_MS
502
+ return [{ name: "Rate limit", used: 100, resetsAt: new Date(end).toISOString(), aside: true }]
503
+ }
504
+
454
505
  // kept is res saying magpie is to leave the account's sign-in be.
455
506
  function kept(res) {
456
507
  const headers = new Headers(res.headers)
@@ -540,7 +591,9 @@ export const GrokAuthPlugin = async ({ client }) => {
540
591
  // sent once, as the built-in sent it: Grok's answer goes on as
541
592
  // it came, the account kept, as the built-in never marked a Grok
542
593
  // account lapsed nor cleared one
543
- return kept(await fetch(req.url, { ...init, method: req.method, headers, body }))
594
+ const res = await fetch(req.url, { ...init, method: req.method, headers, body })
595
+ note(home, res)
596
+ return kept(res)
544
597
  },
545
598
  }
546
599
  },
@@ -569,7 +622,9 @@ export const GrokAuthPlugin = async ({ client }) => {
569
622
  return { error: e.message, windows: [], signIn: "kept" }
570
623
  }
571
624
  await remember(auth, c)
572
- return { ...(await usage(c.key)), signIn: "kept" }
625
+ const u = await usage(c.key)
626
+ if (!u.error) u.windows = [...rateWindow(auth.refresh), ...u.windows]
627
+ return { ...u, signIn: "kept" }
573
628
  },
574
629
  },
575
630
 
@@ -609,4 +664,4 @@ export const GrokAuthPlugin = async ({ client }) => {
609
664
  }
610
665
 
611
666
  // for tests
612
- export const _internal = { rewrite, bodyText }
667
+ export const _internal = { rewrite, bodyText, limited, HOLD_MS }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-grok-auth",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
4
4
  "description": "OpenCode provider plugin: Grok (SuperGrok / X Premium+) through the Grok Build CLI's sign-in",
5
5
  "type": "module",
6
6
  "main": "./index.mjs",