diff --git a/packages/grok/include.test.mjs b/packages/grok/include.test.mjs new file mode 100644 index 0000000..ce25237 --- /dev/null +++ b/packages/grok/include.test.mjs @@ -0,0 +1,34 @@ +import { noRealGrok } from "./nocli.mjs" +import { expect, test } from "bun:test" +import { _internal } from "./index.mjs" + +noRealGrok() + +// #524: Grok answers 400 'Argument not supported: "output.encrypted_content" +// in "include" field'. Codex asks for it beside reasoning.encrypted_content +// when output token replay is on (codex-rs/core/src/client.rs). +const send = (body, search = false) => JSON.parse(_internal.rewrite(JSON.stringify(body), search)) + +test("an include-only body loses output.encrypted_content (reporter's case 1)", () => { + expect(send({ model: "grok-4.7", include: ["output.encrypted_content"] })).toEqual({ model: "grok-4.7" }) +}) + +test("a body with tools loses it too (reporter's case 2)", () => { + expect(send({ model: "grok-4.7", tools: [], include: ["output.encrypted_content"] })).toEqual({ model: "grok-4.7", tools: [] }) +}) + +for (const search of [false, true]) { + test(`Codex's include keeps reasoning.encrypted_content (x_search ${search})`, () => { + const sent = send({ + model: "grok-4.7", input: "hi", store: false, stream: true, reasoning: { effort: "medium" }, + include: ["reasoning.encrypted_content", "output.encrypted_content"], + }, search) + expect(sent.include).toEqual(["reasoning.encrypted_content"]) + expect(sent.store).toBe(false) + }) +} + +test("values Grok takes are left as sent", () => { + const body = JSON.stringify({ model: "grok-4.7", input: "hi", include: ["reasoning.encrypted_content", "web_search_call.action.sources"] }) + expect(_internal.rewrite(body)).toBe(body) +}) diff --git a/packages/grok/index.mjs b/packages/grok/index.mjs index 0ce1b3f..c51f135 100644 --- a/packages/grok/index.mjs +++ b/packages/grok/index.mjs @@ -395,6 +395,10 @@ function foldRoot(root, ps, depth) { return out } +// what Codex asks an OpenAI provider for with output token replay on +// (codex-rs/core/src/client.rs) +const OUTPUT_REPLAY = "output.encrypted_content" + const isObj = (v) => !!v && typeof v === "object" && !Array.isArray(v) // rewrite leaves out what Grok's backend doesn't take: tools of other @@ -407,9 +411,13 @@ const isObj = (v) => !!v && typeof v === "object" && !Array.isArray(v) // encrypted reasoning. A function's parameters with a union at the root // (Codex desktop's codex_app automation_update) are folded to an object, // which Grok turns away otherwise: "tool parameter root must be an object -// type" (magpie#1271). +// type" (magpie#1271). Codex's output token replay asks for each message's +// own sealed copy ("output.encrypted_content" in include), which Grok turns +// away: 'Argument not supported: "output.encrypted_content" in "include" +// field' (#524). Only that value goes; reasoning.encrypted_content and the +// tool-output values xAI documents stay. function rewrite(body, xSearch = false) { - if (typeof body !== "string" || (!xSearch && !body.includes('"tools"') && !body.includes('"tool_choice"') && !body.includes('"reasoning"') && !body.includes('"agent_message"'))) return body + 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 let m try { m = JSON.parse(body) @@ -475,6 +483,11 @@ function rewrite(body, xSearch = false) { ? [{ type: "input_text", text: `${header}\n\n` }, ...it.content] : it.content } }) } + if (Array.isArray(m.include) && m.include.includes(OUTPUT_REPLAY)) { + m.include = m.include.filter((v) => v !== OUTPUT_REPLAY) + if (!m.include.length) delete m.include + dirty = true + } for (const it of Array.isArray(m.input) ? m.input : []) { if (it && it.type === "reasoning" && "content" in it && it.content === null) { delete it.content diff --git a/packages/grok/package.json b/packages/grok/package.json index dfcace2..5c582e3 100644 --- a/packages/grok/package.json +++ b/packages/grok/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-grok-auth", - "version": "0.2.2", + "version": "0.2.3", "description": "OpenCode provider plugin: Grok (SuperGrok / X Premium+) through the Grok Build CLI's sign-in", "type": "module", "main": "./index.mjs",