diff --git a/packages/grok/README.md b/packages/grok/README.md index 725c62e..2b9411c 100644 --- a/packages/grok/README.md +++ b/packages/grok/README.md @@ -87,6 +87,8 @@ leaves these out: - tools of types it doesn't take (a freeform `apply_patch`, namespaces) - `external_web_access` on web search - a `tool_choice` naming a dropped type +- a `tool_choice` with no tools beside it (magpie sends `"none"` so when a + client turned tools off) - a reasoning item's `"content": null` Plain Codex collaboration items (`agent_message`) reach Grok as user diff --git a/packages/grok/index.mjs b/packages/grok/index.mjs index 81df335..4e5021d 100644 --- a/packages/grok/index.mjs +++ b/packages/grok/index.mjs @@ -314,14 +314,17 @@ const isObj = (v) => !!v && typeof v === "object" && !Array.isArray(v) // rewrite leaves out what Grok's backend doesn't take: tools of other // types (a freeform apply_patch, a namespace of sub-agent tools), a web -// search's external_web_access, a tool_choice naming a dropped type, and a +// search's external_web_access, a tool_choice naming a dropped type or +// with no tools beside it (magpie sends "none" so when a client turned +// tools off, yetone/magpie#3110; Grok refuses it: "A tool_choice was set on +// the request but no tools were specified"), and a // reasoning item's "content": null, beside which it can't read the // 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). function rewrite(body) { - if (typeof body !== "string" || (!body.includes('"tools"') && !body.includes('"reasoning"') && !body.includes('"agent_message"'))) return body + if (typeof body !== "string" || (!body.includes('"tools"') && !body.includes('"tool_choice"') && !body.includes('"reasoning"') && !body.includes('"agent_message"'))) return body let m try { m = JSON.parse(body) @@ -356,6 +359,10 @@ function rewrite(body) { dirty = true } } + if (Object.hasOwn(m, "tool_choice") && !(Array.isArray(m.tools) && m.tools.length)) { + delete m.tool_choice + dirty = true + } if (Array.isArray(m.input)) { m.input = m.input.map((it) => { if (!isObj(it) || it.type !== "agent_message" || Object.hasOwn(it, "encrypted_content") || diff --git a/packages/grok/package.json b/packages/grok/package.json index f72969d..b9ef812 100644 --- a/packages/grok/package.json +++ b/packages/grok/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-grok-auth", - "version": "0.1.17", + "version": "0.1.18", "description": "OpenCode provider plugin: Grok (SuperGrok / X Premium+) through the Grok Build CLI's sign-in", "type": "module", "main": "./index.mjs", diff --git a/packages/grok/tool-choice.test.mjs b/packages/grok/tool-choice.test.mjs new file mode 100644 index 0000000..d0090f9 --- /dev/null +++ b/packages/grok/tool-choice.test.mjs @@ -0,0 +1,23 @@ +import { expect, test } from "bun:test" +import { _internal } from "./index.mjs" + +// magpie hands a request that turned tools off with none declared +// "tool_choice": "none" (yetone/magpie#3110); Grok refuses a tool_choice +// with no tools beside it, so it goes before the request does. +test("a tool_choice with no tools beside it is left out", () => { + for (const q of [ + { model: "grok-4.7", tool_choice: "none", input: [] }, + { model: "grok-4.7", tools: [], tool_choice: "none", input: [] }, + { model: "grok-4.7", tool_choice: "auto", input: [] }, + { model: "grok-4.7", tools: [{ type: "custom", name: "apply_patch" }], tool_choice: "none", input: [] }, + ]) { + const sent = JSON.parse(_internal.rewrite(JSON.stringify(q))) + expect(Object.hasOwn(sent, "tool_choice")).toBe(false) + expect(sent.model).toBe("grok-4.7") + } +}) + +test("a tool_choice with tools stays", () => { + const body = JSON.stringify({ tools: [{ type: "function", name: "x", parameters: { type: "object" } }], tool_choice: "none", input: [] }) + expect(_internal.rewrite(body)).toBe(body) +})