grok 0.1.18: a tool_choice with no tools beside it is left out before Grok (yetone/magpie#3110)

magpie now hands a request that turned tools off with none declared
(Chat's tool_choice "none", Anthropic's {"type":"none"}, Gemini's
functionCallingConfig NONE) to a Responses plugin as tool_choice "none"
with no tools, so a plugin that adds a server-side tool (x_search, #263)
can tell it from no choice. Grok's backend refuses a tool_choice with no
tools ("A tool_choice was set on the request but no tools were
specified", yetone/magpie#378), which magpie's grokBody used to leave out
for the plugin. rewrite now leaves out any tool_choice with no tools, or
none left after the dropped types, as the built-in's grokBody does.

What was run:
- TMPDIR=<scratch> HOME=<scratch>/home bun test: 2018 pass, 0 fail.
  (HOME outside os.tmpdir() trips zcode's isolated-home guard: 19 fail,
  2 errors, all that guard; not this change.)
- bun scripts/check.mjs: passes.
- tool-choice.test.mjs against origin/main's index.mjs: 1 pass, 1 fail
  (the lone-tool_choice case), green with this change.
Not tried against Grok's live backend.
This commit is contained in:
yetone
2026-10-21 09:59:50 +08:00
parent d87dd8f3f9
commit aee6b9de5e
4 changed files with 35 additions and 3 deletions
+2
View File
@@ -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
+9 -2
View File
@@ -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") ||
+1 -1
View File
@@ -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",
+23
View File
@@ -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)
})