@magpie-community/opencode-grok-auth 0.1.11 → 0.1.12

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 +20 -3
  2. package/index.mjs +52 -5
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -55,7 +55,11 @@ API (`@ai-sdk/openai`), signed as the CLI signs them:
55
55
  - `x-grok-client-mode: headless`
56
56
  - `x-grok-client-version` and `User-Agent: grok-shell/<version> (<os>; <arch>)`.
57
57
  The version is 1.0.41, or the installed CLI's if it is newer.
58
- - `x-grok-model-override`: the request's model.
58
+ - `x-grok-model-override`: the request's model. A window suffix
59
+ (`grok-4.7@500k`) is the plain model (`grok-4.7`) here and in the body.
60
+ - `x-grok-context-window`: the token count a suffixed model names
61
+ (`500000` for `@500k`). The plain id sends no such header and stays on
62
+ the default window.
59
63
  - `x-grok-conv-id`: the request's `prompt_cache_key`.
60
64
 
61
65
  The backend turns away some of what a request may carry, so the plugin
@@ -73,8 +77,17 @@ unrecognized content stays unchanged; the plugin does not decrypt tasks.
73
77
  ## Models
74
78
 
75
79
  Grok 4.7 until signed in. Signed in, the models are the ones the account
76
- lists at `/v1/models` (Responses models only), each with its context
77
- window and its reasoning efforts as variants.
80
+ lists at `/v1/models` (Responses models only), each with its reasoning
81
+ efforts as variants.
82
+
83
+ `context_window` is the default window and keeps the plain id, so a saved
84
+ `grok-4.7` stays a 256k model. Each other size in `context_windows` is
85
+ offered beside it, named `Grok 4.7 @ 500k` with id `grok-4.7@500k` and
86
+ that many tokens as its window. A request for it sends
87
+ `x-grok-context-window: 500000`. The proxy serves up to its largest
88
+ window either way and refuses past it; the header names the window the
89
+ caller asked to compact against. Pick `grok-4.7@500k` where a client
90
+ compacts at the advertised window, such as Claude Code.
78
91
 
79
92
  ## Usage
80
93
 
@@ -87,3 +100,7 @@ read 0% used while its requests come back 429. After a 429 the card shows
87
100
  a spent "Rate limit" window until the reset it named (`Retry-After`, or a
88
101
  `*ratelimit*reset*` header). When it named none, the window stays until a
89
102
  request goes through, or for 5 minutes.
103
+
104
+ Grok Bot's separate allowance is shown by the [Cursor plugin](../cursor/README.md#usage).
105
+ Sign in there with the Cursor account linked in Grok Bot, including for
106
+ access through SuperGrok.
package/index.mjs CHANGED
@@ -384,12 +384,48 @@ async function listModels(key) {
384
384
  for (const d of JSON.parse(text)?.data ?? []) {
385
385
  if (!d?.id || (d.api_backend && d.api_backend !== "responses")) continue
386
386
  const efforts = (d.reasoning_efforts ?? []).map((e) => e?.value).filter(Boolean).reverse()
387
- out.push({ id: d.id, name: d.name || d.id, context: d.context_window ?? 0, efforts })
387
+ out.push(...windowsOf(d, efforts))
388
388
  }
389
389
  if (!out.length) throw new Error("Grok listed no models")
390
390
  return out
391
391
  }
392
392
 
393
+ // labelOf is a window as the picker shows it: 500000 is "500k".
394
+ const labelOf = (n) => (n % 1000 === 0 ? `${n / 1000}k` : String(n))
395
+
396
+ // windowsOf is one model for each context window Grok lists. context_window
397
+ // is the default and keeps the plain id (grok-4.7), so a saved agent stays
398
+ // where it was. Each other size in context_windows is offered beside it
399
+ // (grok-4.7@500k), the way the CLI's /context-window offers it. A list with
400
+ // one size, or none, stays one model.
401
+ function windowsOf(d, efforts) {
402
+ const name = d.name || d.id
403
+ const fallback = Number(d.context_window) || 0
404
+ const listed = (Array.isArray(d.context_windows) ? d.context_windows : [])
405
+ .map(Number)
406
+ .filter((n) => Number.isInteger(n) && n > 0)
407
+ const windows = [...new Set(listed.length ? listed : [fallback].filter(Boolean))]
408
+ const base = windows.includes(fallback) ? fallback : windows[0] ?? 0
409
+ const out = [{ id: d.id, apiId: d.id, name, context: base || fallback, window: base, efforts }]
410
+ for (const w of windows) {
411
+ if (w === base) continue
412
+ out.push({ id: `${d.id}@${labelOf(w)}`, apiId: d.id, name: `${name} @ ${labelOf(w)}`, context: w, window: w, efforts })
413
+ }
414
+ return out
415
+ }
416
+
417
+ // modelOf splits a requested id into the model Grok lists and the window
418
+ // the id names. grok-4.7 is grok-4.7 at its default window (no header);
419
+ // grok-4.7@500k is grok-4.7 at 500000 tokens. The header is what the CLI
420
+ // sends as x-grok-context-window. The proxy serves up to its largest
421
+ // window either way and refuses past it; the header names the window the
422
+ // caller asked to compact against.
423
+ function modelOf(id) {
424
+ const at = typeof id === "string" ? id.match(/^(.*)@(\d+)k$/) : null
425
+ if (!at) return { model: id, window: 0 }
426
+ return { model: at[1], window: Number(at[2]) * 1000 }
427
+ }
428
+
393
429
  // ---- sign-in ------------------------------------------------------------------
394
430
 
395
431
  // login runs `grok login --device-auth` in home and hands on the first link
@@ -692,7 +728,7 @@ export const GrokAuthPlugin = async ({ client }) => {
692
728
  const home = auth?.refresh
693
729
  if (!home) throw new Error("Grok is not signed in; run `grok login`")
694
730
  const req = new Request(input, init)
695
- const body = rewrite(bodyText(init?.body))
731
+ let body = rewrite(bodyText(init?.body))
696
732
  let parsed = {}
697
733
  if (typeof body === "string") {
698
734
  try {
@@ -703,7 +739,15 @@ export const GrokAuthPlugin = async ({ client }) => {
703
739
  await remember(auth, c)
704
740
  const headers = new Headers(req.headers)
705
741
  await sign(headers, c.key)
706
- if (typeof parsed.model === "string" && parsed.model) headers.set("x-grok-model-override", parsed.model)
742
+ const asked = modelOf(parsed.model)
743
+ if (asked.model) {
744
+ headers.set("x-grok-model-override", asked.model)
745
+ if (asked.window && typeof body === "string") {
746
+ parsed.model = asked.model
747
+ body = JSON.stringify(parsed)
748
+ }
749
+ }
750
+ if (asked.window) headers.set("x-grok-context-window", String(asked.window))
707
751
  if (typeof parsed.prompt_cache_key === "string" && parsed.prompt_cache_key) headers.set("x-grok-conv-id", parsed.prompt_cache_key)
708
752
  headers.delete("content-length")
709
753
  // sent once, as the built-in sent it: Grok's answer goes on as
@@ -757,12 +801,15 @@ export const GrokAuthPlugin = async ({ client }) => {
757
801
  const base = have["grok-4.7"] ?? Object.values(have)[0] ?? {}
758
802
  const out = {}
759
803
  for (const m of list) {
760
- const was = have[m.id] ?? base
804
+ const was = have[m.id] ?? have[m.apiId] ?? base
761
805
  out[m.id] = {
762
806
  ...was,
763
807
  id: m.id,
764
808
  providerID: provider.id,
765
809
  name: m.name,
810
+ // the id as offered, window and all: magpie and the AI SDK send
811
+ // api.id as the body's model, and fetch reads the window from it
812
+ // (modelOf) before Grok sees the plain model
766
813
  api: { ...(was.api ?? {}), id: m.id, url: BASE, npm: "@ai-sdk/openai" },
767
814
  limit: { ...(was.limit ?? {}), context: m.context || was.limit?.context || 0, output: was.limit?.output ?? 0 },
768
815
  capabilities: {
@@ -782,4 +829,4 @@ export const GrokAuthPlugin = async ({ client }) => {
782
829
  }
783
830
 
784
831
  // for tests
785
- export const _internal = { rewrite, objectRoot, bodyText, limited, HOLD_MS }
832
+ export const _internal = { rewrite, objectRoot, bodyText, limited, HOLD_MS, windowsOf, modelOf }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-grok-auth",
3
- "version": "0.1.11",
3
+ "version": "0.1.12",
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",