Files
plugins/packages/grok/tool-choice.test.mjs
T
yetone aee6b9de5e 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.
2026-10-21 09:59:50 +08:00

24 lines
1.1 KiB
JavaScript

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)
})