@magpie-community/opencode-grok-auth 0.1.10 → 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.
- package/README.md +24 -3
- package/index.mjs +70 -6
- 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
|
|
@@ -66,11 +70,24 @@ leaves these out:
|
|
|
66
70
|
- a `tool_choice` naming a dropped type
|
|
67
71
|
- a reasoning item's `"content": null`
|
|
68
72
|
|
|
73
|
+
Plain Codex collaboration items (`agent_message`) reach Grok as user
|
|
74
|
+
messages, with their sender, recipient and content preserved. Sealed or
|
|
75
|
+
unrecognized content stays unchanged; the plugin does not decrypt tasks.
|
|
76
|
+
|
|
69
77
|
## Models
|
|
70
78
|
|
|
71
79
|
Grok 4.7 until signed in. Signed in, the models are the ones the account
|
|
72
|
-
lists at `/v1/models` (Responses models only), each with its
|
|
73
|
-
|
|
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.
|
|
74
91
|
|
|
75
92
|
## Usage
|
|
76
93
|
|
|
@@ -83,3 +100,7 @@ read 0% used while its requests come back 429. After a 429 the card shows
|
|
|
83
100
|
a spent "Rate limit" window until the reset it named (`Retry-After`, or a
|
|
84
101
|
`*ratelimit*reset*` header). When it named none, the window stays until a
|
|
85
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
|
@@ -288,7 +288,7 @@ const isObj = (v) => !!v && typeof v === "object" && !Array.isArray(v)
|
|
|
288
288
|
// which Grok turns away otherwise: "tool parameter root must be an object
|
|
289
289
|
// type" (magpie#1271).
|
|
290
290
|
function rewrite(body) {
|
|
291
|
-
if (typeof body !== "string" || (!body.includes('"tools"') && !body.includes('"reasoning"'))) return body
|
|
291
|
+
if (typeof body !== "string" || (!body.includes('"tools"') && !body.includes('"reasoning"') && !body.includes('"agent_message"'))) return body
|
|
292
292
|
let m
|
|
293
293
|
try {
|
|
294
294
|
m = JSON.parse(body)
|
|
@@ -323,6 +323,23 @@ function rewrite(body) {
|
|
|
323
323
|
dirty = true
|
|
324
324
|
}
|
|
325
325
|
}
|
|
326
|
+
if (Array.isArray(m.input)) {
|
|
327
|
+
m.input = m.input.map((it) => {
|
|
328
|
+
if (!isObj(it) || it.type !== "agent_message" || Object.hasOwn(it, "encrypted_content") ||
|
|
329
|
+
!Array.isArray(it.content) || !it.content.length ||
|
|
330
|
+
!it.content.every((part) => isObj(part) && !Object.hasOwn(part, "encrypted_content") &&
|
|
331
|
+
(part.type === "input_text" && typeof part.text === "string" ||
|
|
332
|
+
part.type === "input_image" && typeof part.image_url === "string"))) return it
|
|
333
|
+
// Grok takes ordinary messages, not Codex's collaboration item.
|
|
334
|
+
// Sealed tasks remain untouched for the gateway's existing guard.
|
|
335
|
+
const from = typeof it.author === "string" ? it.author : ""
|
|
336
|
+
const to = typeof it.recipient === "string" ? it.recipient : ""
|
|
337
|
+
const header = from && to ? `From ${from} to ${to}` : from ? `From ${from}` : to ? `To ${to}` : ""
|
|
338
|
+
dirty = true
|
|
339
|
+
return { type: "message", role: "user", content: header
|
|
340
|
+
? [{ type: "input_text", text: `${header}\n\n` }, ...it.content] : it.content }
|
|
341
|
+
})
|
|
342
|
+
}
|
|
326
343
|
for (const it of Array.isArray(m.input) ? m.input : []) {
|
|
327
344
|
if (it && it.type === "reasoning" && "content" in it && it.content === null) {
|
|
328
345
|
delete it.content
|
|
@@ -367,12 +384,48 @@ async function listModels(key) {
|
|
|
367
384
|
for (const d of JSON.parse(text)?.data ?? []) {
|
|
368
385
|
if (!d?.id || (d.api_backend && d.api_backend !== "responses")) continue
|
|
369
386
|
const efforts = (d.reasoning_efforts ?? []).map((e) => e?.value).filter(Boolean).reverse()
|
|
370
|
-
out.push(
|
|
387
|
+
out.push(...windowsOf(d, efforts))
|
|
371
388
|
}
|
|
372
389
|
if (!out.length) throw new Error("Grok listed no models")
|
|
373
390
|
return out
|
|
374
391
|
}
|
|
375
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
|
+
|
|
376
429
|
// ---- sign-in ------------------------------------------------------------------
|
|
377
430
|
|
|
378
431
|
// login runs `grok login --device-auth` in home and hands on the first link
|
|
@@ -675,7 +728,7 @@ export const GrokAuthPlugin = async ({ client }) => {
|
|
|
675
728
|
const home = auth?.refresh
|
|
676
729
|
if (!home) throw new Error("Grok is not signed in; run `grok login`")
|
|
677
730
|
const req = new Request(input, init)
|
|
678
|
-
|
|
731
|
+
let body = rewrite(bodyText(init?.body))
|
|
679
732
|
let parsed = {}
|
|
680
733
|
if (typeof body === "string") {
|
|
681
734
|
try {
|
|
@@ -686,7 +739,15 @@ export const GrokAuthPlugin = async ({ client }) => {
|
|
|
686
739
|
await remember(auth, c)
|
|
687
740
|
const headers = new Headers(req.headers)
|
|
688
741
|
await sign(headers, c.key)
|
|
689
|
-
|
|
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))
|
|
690
751
|
if (typeof parsed.prompt_cache_key === "string" && parsed.prompt_cache_key) headers.set("x-grok-conv-id", parsed.prompt_cache_key)
|
|
691
752
|
headers.delete("content-length")
|
|
692
753
|
// sent once, as the built-in sent it: Grok's answer goes on as
|
|
@@ -740,12 +801,15 @@ export const GrokAuthPlugin = async ({ client }) => {
|
|
|
740
801
|
const base = have["grok-4.7"] ?? Object.values(have)[0] ?? {}
|
|
741
802
|
const out = {}
|
|
742
803
|
for (const m of list) {
|
|
743
|
-
const was = have[m.id] ?? base
|
|
804
|
+
const was = have[m.id] ?? have[m.apiId] ?? base
|
|
744
805
|
out[m.id] = {
|
|
745
806
|
...was,
|
|
746
807
|
id: m.id,
|
|
747
808
|
providerID: provider.id,
|
|
748
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
|
|
749
813
|
api: { ...(was.api ?? {}), id: m.id, url: BASE, npm: "@ai-sdk/openai" },
|
|
750
814
|
limit: { ...(was.limit ?? {}), context: m.context || was.limit?.context || 0, output: was.limit?.output ?? 0 },
|
|
751
815
|
capabilities: {
|
|
@@ -765,4 +829,4 @@ export const GrokAuthPlugin = async ({ client }) => {
|
|
|
765
829
|
}
|
|
766
830
|
|
|
767
831
|
// for tests
|
|
768
|
-
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