@magpie-community/opencode-grok-auth 0.2.1 → 0.2.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.
Files changed (3) hide show
  1. package/README.md +3 -0
  2. package/index.mjs +25 -2
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -93,6 +93,9 @@ API (`@ai-sdk/openai`), signed as the CLI signs them:
93
93
  (`500000` for `@500k`). The plain id sends no such header and stays on
94
94
  the default window.
95
95
  - `x-grok-conv-id`: the request's `prompt_cache_key`.
96
+ - `x-grok-doom-loop-check: 1024` and `x-grok-exact-repetition-check: 64`
97
+ on a Responses request, as Grok Build sends them: the proxy then stops a
98
+ reply that repeats itself after ~1K tokens instead of letting it run on.
96
99
 
97
100
  The backend turns away some of what a request may carry, so the plugin
98
101
  leaves these out:
package/index.mjs CHANGED
@@ -19,6 +19,7 @@ const BASE = "https://cli-chat-proxy.grok.com/v1"
19
19
  const CLIENT_VERSION = "1.0.41"
20
20
  const CPA_CLIENT_ID = "b1a00492-073a-47ea-816f-4c329264a828"
21
21
  const CPA_SCOPE = `https://auth.x.ai::${CPA_CLIENT_ID}`
22
+ const LOOP_CHECKS = [["x-grok-doom-loop-check", "1024"], ["x-grok-exact-repetition-check", "64"]]
22
23
  const REFRESH_MARGIN = 5 * 60 * 1000 // a token this close to its end is renewed before a request
23
24
  const LEAD_MS = REFRESH_MARGIN // and this close, magpie renews it ahead of time (auth.refresh)
24
25
  const LINK_WAIT = 30 * 1000
@@ -394,6 +395,10 @@ function foldRoot(root, ps, depth) {
394
395
  return out
395
396
  }
396
397
 
398
+ // what Codex asks an OpenAI provider for with output token replay on
399
+ // (codex-rs/core/src/client.rs)
400
+ const OUTPUT_REPLAY = "output.encrypted_content"
401
+
397
402
  const isObj = (v) => !!v && typeof v === "object" && !Array.isArray(v)
398
403
 
399
404
  // rewrite leaves out what Grok's backend doesn't take: tools of other
@@ -406,9 +411,13 @@ const isObj = (v) => !!v && typeof v === "object" && !Array.isArray(v)
406
411
  // encrypted reasoning. A function's parameters with a union at the root
407
412
  // (Codex desktop's codex_app automation_update) are folded to an object,
408
413
  // which Grok turns away otherwise: "tool parameter root must be an object
409
- // type" (magpie#1271).
414
+ // type" (magpie#1271). Codex's output token replay asks for each message's
415
+ // own sealed copy ("output.encrypted_content" in include), which Grok turns
416
+ // away: 'Argument not supported: "output.encrypted_content" in "include"
417
+ // field' (#524). Only that value goes; reasoning.encrypted_content and the
418
+ // tool-output values xAI documents stay.
410
419
  function rewrite(body, xSearch = false) {
411
- if (typeof body !== "string" || (!xSearch && !body.includes('"tools"') && !body.includes('"tool_choice"') && !body.includes('"reasoning"') && !body.includes('"agent_message"'))) return body
420
+ if (typeof body !== "string" || (!xSearch && !body.includes('"tools"') && !body.includes('"tool_choice"') && !body.includes('"reasoning"') && !body.includes('"agent_message"') && !body.includes(`"${OUTPUT_REPLAY}"`))) return body
412
421
  let m
413
422
  try {
414
423
  m = JSON.parse(body)
@@ -474,6 +483,11 @@ function rewrite(body, xSearch = false) {
474
483
  ? [{ type: "input_text", text: `${header}\n\n` }, ...it.content] : it.content }
475
484
  })
476
485
  }
486
+ if (Array.isArray(m.include) && m.include.includes(OUTPUT_REPLAY)) {
487
+ m.include = m.include.filter((v) => v !== OUTPUT_REPLAY)
488
+ if (!m.include.length) delete m.include
489
+ dirty = true
490
+ }
477
491
  for (const it of Array.isArray(m.input) ? m.input : []) {
478
492
  if (it && it.type === "reasoning" && "content" in it && it.content === null) {
479
493
  delete it.content
@@ -987,6 +1001,15 @@ export const GrokAuthPlugin = async ({ client, magpie }) => {
987
1001
  }
988
1002
  if (asked.window) headers.set("x-grok-context-window", String(asked.window))
989
1003
  if (typeof parsed.prompt_cache_key === "string" && parsed.prompt_cache_key) headers.set("x-grok-conv-id", parsed.prompt_cache_key)
1004
+ // Grok Build asks the proxy to watch every Responses stream for a
1005
+ // model stuck repeating itself: without these the proxy cuts a
1006
+ // loop only after ~4K tokens, and a looser one (a fence or a line
1007
+ // over and over) runs to max_output_tokens (yetone/magpie#3594).
1008
+ // The values are Grok Build 1.0.67's (doom_loop_recovery's
1009
+ // max_threshold is the 64).
1010
+ if (new URL(req.url).pathname.endsWith("/responses")) {
1011
+ for (const [k, v] of LOOP_CHECKS) if (!headers.has(k)) headers.set(k, v)
1012
+ }
990
1013
  headers.delete("content-length")
991
1014
  const send = async (key) => {
992
1015
  await sign(headers, key)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-grok-auth",
3
- "version": "0.2.1",
3
+ "version": "0.2.3",
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",