grok 0.1.10: a tool whose parameters are a union at the root reaches Grok as an object, so Codex desktop's turns are no longer refused (yetone/magpie#1271)

xiaocui12: Codex on Grok failed every turn with "[invalid_client_tool_schema]
mcp__codex_app__automation_update: tool parameter root must be an object
type (root schema is an anyOf/oneOf union with a non-object branch)".
Codex desktop's codex_app automation_update has an anyOf at its
parameters' root (ChatGPT.app 26.930 builds it with zod 4's toJSONSchema),
and its create and update branches are unions themselves.

rewrite() now folds a function's parameters to an object root as magpie's
built-in provider.ObjectRoot does, nested union branches folded first so
their fields (name, prompt, rrule, kind...) are kept and only "mode" is
required. Before, the plugin sent the union as it came; magpie's grokBody
folded it for requests through magpie, but not for OpenCode's own.

Verified: the real schema (testdata, rebuilt with zod 4.1.8) sent to the
real Grok subscription raw gets 400 with the reporter's exact message;
folded it gets 200 and a correct create call
({"mode":"create","kind":"heartbeat",...,"rrule":"FREQ=MINUTELY;INTERVAL=30"}).
schema.test.mjs: 2 of its 3 tests fail on 0.1.9 and pass now.
HOME=$(mktemp -d) bun test: the rest of the package matches main (login and
refresh tests time out here on main as well; CI runs them on Linux).
This commit is contained in:
yetone
2026-10-08 16:51:46 +08:00
parent 972c0a9f81
commit a6910fd50a
4 changed files with 518 additions and 3 deletions
+90 -2
View File
@@ -202,11 +202,91 @@ function bodyText(body) {
return body
}
// objectRoot is a tool's parameters as a plain object at the root, with no
// anyOf, oneOf or allOf there, folded as magpie's built-in folds them
// (provider.ObjectRoot): allOf's branches are all merged; of anyOf's and
// oneOf's object branches, a union itself folded first, the properties are
// merged, a field each of them requires stays required, and the other
// branches go. Branches may be local $refs. A schema already a plain object
// is returned as it came; the caller's own is never changed.
function objectRoot(ps) {
return foldRoot(ps, ps, 0)
}
function foldRoot(root, ps, depth) {
if (!isObj(ps)) return ps
const has = (k) => Object.hasOwn(ps, k)
if (ps.type === "object" && !has("anyOf") && !has("oneOf") && !has("allOf")) return ps
const out = { ...ps }
const props = { ...(isObj(ps.properties) ? ps.properties : {}) }
let required = Array.isArray(ps.required) ? [...ps.required] : []
const ref = (b) => {
if (!isObj(b)) return null
if (typeof b.$ref !== "string" || !b.$ref) return b
for (const k of ["$defs", "definitions"]) {
const pre = "#/" + k + "/"
if (b.$ref.startsWith(pre)) {
const d = root[k]?.[b.$ref.slice(pre.length)]
return isObj(d) ? d : null
}
}
return null
}
for (const b of Array.isArray(ps.allOf) ? ps.allOf : []) {
const bm = ref(b)
if (!bm) continue
if (isObj(bm.properties)) for (const [k, v] of Object.entries(bm.properties)) if (!Object.hasOwn(props, k)) props[k] = v
if (Array.isArray(bm.required)) required.push(...bm.required)
}
delete out.allOf
const branches = []
for (const k of ["anyOf", "oneOf"]) {
for (const b of Array.isArray(ps[k]) ? ps[k] : []) {
let bm = ref(b)
if (!bm) continue
// a branch that is itself a union (zod's discriminated create and
// update in automation_update) is folded first, not dropped
if (depth < 8 && ["anyOf", "oneOf", "allOf"].some((u) => Object.hasOwn(bm, u))) bm = foldRoot(root, bm, depth + 1)
if (bm.type !== "object" && !Object.hasOwn(bm, "properties")) continue
branches.push(bm)
}
delete out[k]
}
// a field every branch requires stays required, beside the root's and
// allOf's own, which no branch can drop; a field the root doesn't define
// takes each schema the branches give it, as anyOf when they differ
let common = null
const from = new Map()
for (const b of branches) {
for (const [k, v] of Object.entries(isObj(b.properties) ? b.properties : {})) {
if (Object.hasOwn(props, k)) continue
const vs = from.get(k) ?? []
if (!vs.some((w) => JSON.stringify(w) === JSON.stringify(v))) vs.push(v)
from.set(k, vs)
}
const br = Array.isArray(b.required) ? b.required : []
common = common === null ? [...br] : common.filter((r) => br.includes(r))
}
for (const [k, vs] of from) props[k] = vs.length === 1 ? vs[0] : { anyOf: vs }
for (const r of common ?? []) if (!required.includes(r)) required.push(r)
required = [...new Set(required)]
out.type = "object"
out.properties = props
if (required.length) out.required = required
else delete out.required
return out
}
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
// reasoning item's "content": null, beside which it can't read the
// encrypted reasoning.
// 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"'))) return body
let m
@@ -228,6 +308,14 @@ function rewrite(body) {
delete t.external_web_access
dirty = true
}
if (t.type === "function") {
const fn = isObj(t.function) ? t.function : t
const ps = objectRoot(fn.parameters)
if (ps !== fn.parameters) {
fn.parameters = ps
dirty = true
}
}
return true
})
if (m.tool_choice && typeof m.tool_choice === "object" && !TOOLS.has(m.tool_choice.type)) {
@@ -677,4 +765,4 @@ export const GrokAuthPlugin = async ({ client }) => {
}
// for tests
export const _internal = { rewrite, bodyText, limited, HOLD_MS }
export const _internal = { rewrite, objectRoot, bodyText, limited, HOLD_MS }
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-grok-auth",
"version": "0.1.9",
"version": "0.1.10",
"description": "OpenCode provider plugin: Grok (SuperGrok / X Premium+) through the Grok Build CLI's sign-in",
"type": "module",
"main": "./index.mjs",
+44
View File
@@ -0,0 +1,44 @@
// Codex desktop offers codex_app's automation_update, whose parameters are
// a union at the root whose create and update branches are unions
// themselves (testdata: what ChatGPT.app 26.930's zod builds). Grok turned
// the request away: "tool parameter root must be an object type (root
// schema is an anyOf/oneOf union with a non-object branch)" (magpie#1271).
import { expect, test } from "bun:test"
import { readFileSync } from "node:fs"
import { _internal } from "./index.mjs"
const SCHEMA = readFileSync(new URL("./testdata/codex_automation_update_schema.json", import.meta.url), "utf8")
const body = (parameters) =>
JSON.stringify({
model: "grok-4.7",
tools: [
{ type: "function", name: "exec_command", parameters: { type: "object", properties: { cmd: { type: "string" } } } },
{ type: "function", name: "mcp__codex_app__automation_update", strict: false, parameters },
],
})
test("a root union goes to Grok as an object, the nested branches' fields kept", () => {
const sent = JSON.parse(_internal.rewrite(body(JSON.parse(SCHEMA))))
const ps = sent.tools[1].parameters
expect(ps.type).toBe("object")
expect(ps.anyOf).toBeUndefined()
expect(ps.oneOf).toBeUndefined()
for (const k of ["mode", "id", "name", "prompt", "rrule", "status", "kind", "projectId", "model", "targetThreadId", "executionEnvironment"])
expect(Object.keys(ps.properties)).toContain(k)
expect(ps.required).toEqual(["mode"])
const mode = JSON.stringify(ps.properties.mode)
for (const v of ["view", "delete", "__schema11"]) expect(mode).toContain(v)
// the plain tool goes as it came
expect(sent.tools[0]).toEqual({ type: "function", name: "exec_command", parameters: { type: "object", properties: { cmd: { type: "string" } } } })
})
test("a body with no root union is passed on byte for byte", () => {
const b = body({ type: "object", properties: { a: { anyOf: [{ type: "string" }, { type: "null" }] } } })
expect(_internal.rewrite(b)).toBe(b)
})
test("a Chat function's parameters are folded too", () => {
const b = JSON.stringify({ tools: [{ type: "function", function: { name: "t", parameters: { anyOf: [{ type: "object", properties: { a: { type: "string" } } }, { type: "string" }] } } }] })
expect(JSON.parse(_internal.rewrite(b)).tools[0].function.parameters).toEqual({ type: "object", properties: { a: { type: "string" } } })
})
@@ -0,0 +1,383 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"anyOf": [
{
"type": "object",
"properties": {
"mode": {
"type": "string",
"const": "view"
},
"id": {
"$ref": "#/$defs/__schema0"
}
},
"required": [
"mode",
"id"
],
"additionalProperties": false
},
{
"anyOf": [
{
"type": "object",
"properties": {
"name": {
"$ref": "#/$defs/__schema2"
},
"prompt": {
"$ref": "#/$defs/__schema3"
},
"rrule": {
"$ref": "#/$defs/__schema4"
},
"status": {
"$ref": "#/$defs/__schema5"
},
"notificationPolicy": {
"$ref": "#/$defs/__schema6"
},
"kind": {
"$ref": "#/$defs/__schema7"
},
"projectId": {
"$ref": "#/$defs/__schema8"
},
"model": {
"$ref": "#/$defs/__schema9"
},
"reasoningEffort": {
"$ref": "#/$defs/__schema10"
},
"mode": {
"$ref": "#/$defs/__schema11"
},
"destination": {
"type": "string",
"const": "local"
},
"executionEnvironment": {
"type": "string",
"const": "local"
}
},
"required": [
"name",
"prompt",
"rrule",
"status",
"kind",
"projectId",
"model",
"reasoningEffort",
"mode",
"executionEnvironment"
],
"additionalProperties": false
},
{
"type": "object",
"properties": {
"name": {
"$ref": "#/$defs/__schema2"
},
"prompt": {
"$ref": "#/$defs/__schema3"
},
"rrule": {
"$ref": "#/$defs/__schema4"
},
"status": {
"$ref": "#/$defs/__schema5"
},
"notificationPolicy": {
"$ref": "#/$defs/__schema6"
},
"kind": {
"$ref": "#/$defs/__schema12"
},
"destination": {
"$ref": "#/$defs/__schema13"
},
"targetThreadId": {
"$ref": "#/$defs/__schema14"
},
"mode": {
"$ref": "#/$defs/__schema11"
}
},
"required": [
"name",
"prompt",
"rrule",
"status",
"kind",
"mode"
],
"additionalProperties": false
}
]
},
{
"anyOf": [
{
"type": "object",
"properties": {
"name": {
"$ref": "#/$defs/__schema2"
},
"prompt": {
"$ref": "#/$defs/__schema3"
},
"rrule": {
"$ref": "#/$defs/__schema15"
},
"status": {
"$ref": "#/$defs/__schema5"
},
"notificationPolicy": {
"$ref": "#/$defs/__schema6"
},
"kind": {
"$ref": "#/$defs/__schema7"
},
"projectId": {
"$ref": "#/$defs/__schema8"
},
"model": {
"$ref": "#/$defs/__schema9"
},
"reasoningEffort": {
"$ref": "#/$defs/__schema10"
},
"mode": {
"$ref": "#/$defs/__schema16"
},
"id": {
"$ref": "#/$defs/__schema0"
},
"destination": {
"type": "string",
"enum": [
"local",
"worktree"
]
},
"executionEnvironment": {
"description": "env",
"type": "string",
"enum": [
"worktree",
"local"
]
},
"localEnvironmentConfigPath": {
"anyOf": [
{
"type": "string",
"minLength": 1
},
{
"type": "null"
}
]
}
},
"required": [
"name",
"prompt",
"rrule",
"status",
"kind",
"projectId",
"model",
"reasoningEffort",
"mode",
"id",
"executionEnvironment"
],
"additionalProperties": false
},
{
"type": "object",
"properties": {
"name": {
"$ref": "#/$defs/__schema2"
},
"prompt": {
"$ref": "#/$defs/__schema3"
},
"rrule": {
"$ref": "#/$defs/__schema15"
},
"status": {
"$ref": "#/$defs/__schema5"
},
"notificationPolicy": {
"$ref": "#/$defs/__schema6"
},
"kind": {
"$ref": "#/$defs/__schema12"
},
"destination": {
"$ref": "#/$defs/__schema13"
},
"targetThreadId": {
"$ref": "#/$defs/__schema14"
},
"mode": {
"$ref": "#/$defs/__schema16"
},
"id": {
"$ref": "#/$defs/__schema0"
}
},
"required": [
"name",
"prompt",
"rrule",
"status",
"kind",
"mode",
"id"
],
"additionalProperties": false
}
]
},
{
"type": "object",
"properties": {
"mode": {
"type": "string",
"const": "delete"
},
"id": {
"$ref": "#/$defs/__schema0"
}
},
"required": [
"mode",
"id"
],
"additionalProperties": false
}
],
"$defs": {
"__schema0": {
"description": "Automation id.",
"$ref": "#/$defs/__schema1"
},
"__schema1": {
"type": "string",
"minLength": 1
},
"__schema2": {
"description": "name",
"$ref": "#/$defs/__schema1"
},
"__schema3": {
"description": "prompt",
"$ref": "#/$defs/__schema1"
},
"__schema4": {
"description": "rrule create",
"$ref": "#/$defs/__schema1"
},
"__schema5": {
"description": "status",
"type": "string",
"enum": [
"ACTIVE",
"PAUSED"
]
},
"__schema6": {
"description": "np",
"anyOf": [
{
"type": "string",
"enum": [
"all",
"failed_runs_only"
]
},
{
"type": "null"
}
]
},
"__schema7": {
"description": "cron",
"type": "string",
"const": "cron"
},
"__schema8": {
"description": "project",
"anyOf": [
{
"type": "string",
"minLength": 1
},
{
"type": "null"
}
]
},
"__schema9": {
"description": "model",
"$ref": "#/$defs/__schema1"
},
"__schema10": {
"description": "effort",
"type": "string",
"enum": [
"none",
"minimal",
"low",
"medium",
"high",
"xhigh",
"max",
"ultra"
]
},
"__schema11": {
"type": "string",
"enum": [
"create",
"suggested_create"
]
},
"__schema12": {
"description": "hb",
"type": "string",
"const": "heartbeat"
},
"__schema13": {
"description": "dest",
"type": "string",
"enum": [
"local",
"thread"
]
},
"__schema14": {
"format": "uuid",
"description": "tt",
"$ref": "#/$defs/__schema1"
},
"__schema15": {
"description": "rrule",
"$ref": "#/$defs/__schema1"
},
"__schema16": {
"type": "string",
"enum": [
"update",
"suggested_update"
]
}
}
}