commandcode 0.1.21: a Google model's tool schemas have nested $refs written out
kthink on Discord: dsh on google/gemini-3.8-flash was refused 400 AI_UnsupportedFunctionalityError "Google schema conversion only supports references to direct children of root-level $defs or definitions ... #/properties/func/properties/triggers/items". dsh reuses part of its own schema by a pointer into properties. For a Google model (google/..., or an id naming gemini) such a $ref is now written out in place, on the Provider API and on Go's /alpha/generate; a ref to a direct child of $defs or definitions goes as it came; a loop or 32 deep ends at the target's type. Data named $ref (default, enum) and refs that name nothing are untouched. Every other model's schema goes as it came. Run (mote, HOME from mktemp -d): bun test in packages/commandcode, 94 pass; bun scripts/check.mjs commandcode ok. google-schema.test.mjs against the old index.mjs: 6 of 7 red, including both end-to-end tests (GOAT, Go) that send the dsh-shaped schema to google/gemini-3.8-flash. Not tried: a real request. Both Command Code keys in magpie's plugin-auth answer 401, so the fix is not checked against Command Code's gateway. Siblings (other plugins with Gemini models) are not checked: #557.
This commit is contained in:
@@ -0,0 +1,119 @@
|
||||
// kthink on Discord: dsh on google/gemini-3.8-flash through Command Code
|
||||
// was refused 400 AI_UnsupportedFunctionalityError "Google schema
|
||||
// conversion only supports references to direct children of root-level
|
||||
// $defs or definitions … JSON Schema reference:
|
||||
// #/properties/func/properties/triggers/items". A tool schema that reuses
|
||||
// an earlier part of itself by a JSON pointer has that part written out
|
||||
// for a Google model, on the Provider API and on Go's /alpha/generate; a
|
||||
// ref to a direct child of $defs, which Google takes, goes as it came, and
|
||||
// every other model's schema goes as it came.
|
||||
import { afterEach, expect, test } from "bun:test"
|
||||
import { CommandCodePlugin, _internal } from "./index.mjs"
|
||||
|
||||
const real = globalThis.fetch
|
||||
afterEach(() => {
|
||||
globalThis.fetch = real
|
||||
_internal.subsSeen.clear()
|
||||
_internal.extraSeen.clear()
|
||||
})
|
||||
|
||||
const TRIGGER = { type: "object", properties: { on: { type: "string", enum: ["push", "pull"] } }, required: ["on"] }
|
||||
// shaped as the error's path: a later property reuses
|
||||
// #/properties/func/properties/triggers/items
|
||||
const DSH = {
|
||||
type: "object",
|
||||
properties: {
|
||||
func: {
|
||||
type: "object",
|
||||
properties: {
|
||||
name: { type: "string" },
|
||||
triggers: { type: "array", items: TRIGGER },
|
||||
},
|
||||
},
|
||||
extra: { type: "array", items: { $ref: "#/properties/func/properties/triggers/items", description: "another trigger" } },
|
||||
},
|
||||
required: ["func"],
|
||||
}
|
||||
const tool = (parameters) => ({ type: "function", function: { name: "schedule", description: "d", parameters } })
|
||||
|
||||
const refsIn = (s) => [...JSON.stringify(s).matchAll(/"\$ref":"([^"]*)"/g)].map((m) => m[1])
|
||||
|
||||
test("a pointer into the schema is written out for a Google model", () => {
|
||||
const g = _internal.googleSchema(DSH)
|
||||
expect(refsIn(g)).toEqual([])
|
||||
expect(g.properties.extra.items).toEqual({ ...TRIGGER, description: "another trigger" })
|
||||
expect(g.properties.func).toEqual(DSH.properties.func)
|
||||
expect(DSH.properties.extra.items.$ref).toBe("#/properties/func/properties/triggers/items") // not changed in place
|
||||
})
|
||||
|
||||
test("a ref to a direct child of $defs goes as it came, and its def is gone through", () => {
|
||||
const s = {
|
||||
type: "object",
|
||||
properties: { a: { $ref: "#/$defs/A" }, b: { $ref: "#/definitions/B" } },
|
||||
$defs: { A: { type: "object", properties: { x: { $ref: "#/properties/b" } } } },
|
||||
definitions: { B: { type: "string" } },
|
||||
}
|
||||
const g = _internal.googleSchema(s)
|
||||
expect(g.properties.a).toEqual({ $ref: "#/$defs/A" })
|
||||
expect(g.properties.b).toEqual({ $ref: "#/definitions/B" })
|
||||
expect(g.$defs.A.properties.x).toEqual({ $ref: "#/definitions/B" })
|
||||
expect(g.definitions).toEqual(s.definitions)
|
||||
})
|
||||
|
||||
test("a schema that refers to itself ends", () => {
|
||||
const s = { type: "object", properties: { name: { type: "string" }, children: { type: "array", items: { $ref: "#" } } } }
|
||||
const g = _internal.googleSchema(s)
|
||||
expect(refsIn(g)).toEqual([])
|
||||
expect(g.properties.children.items.properties.children.items).toEqual({ type: "object" })
|
||||
const deep = { type: "object", properties: { a: { type: "object", properties: { b: { $ref: "#/properties/a" } } } } }
|
||||
expect(refsIn(_internal.googleSchema(deep))).toEqual([])
|
||||
})
|
||||
|
||||
test("data named $ref (an enum, a default) and a ref that names nothing go as they came", () => {
|
||||
const s = {
|
||||
type: "object",
|
||||
properties: {
|
||||
e: { type: "object", default: { $ref: "#/properties/x" }, enum: [{ $ref: "#/properties/x" }] },
|
||||
lost: { $ref: "#/properties/nowhere" },
|
||||
remote: { $ref: "https://example.com/s.json" },
|
||||
},
|
||||
}
|
||||
expect(_internal.googleSchema(s)).toEqual(s)
|
||||
})
|
||||
|
||||
test("Google's models are told apart from the rest", () => {
|
||||
for (const m of ["google/gemini-3.8-flash", "google/gemini-3.5-flash-lite", "gemini-3.8-flash"]) expect(_internal.isGoogle(m)).toBe(true)
|
||||
for (const m of ["anthropic/claude-sonnet-4-6", "deepseek/deepseek-v4-pro", "moonshotai/Kimi-K3", "openai/gpt-5.5", undefined]) expect(_internal.isGoogle(m)).toBe(false)
|
||||
})
|
||||
|
||||
// sends schema's tool for model through a plan's fetch and gives the tool
|
||||
// schema the gateway was sent
|
||||
async function sent(plan, model, schema) {
|
||||
const out = []
|
||||
globalThis.fetch = async (input, init = {}) => {
|
||||
const path = new URL(typeof input === "string" ? input : input.url ?? String(input)).pathname
|
||||
if (path.endsWith("/chat/completions")) {
|
||||
const b = init.body ?? (input instanceof Request ? await input.text() : undefined)
|
||||
out.push(JSON.parse(b).tools[0].function.parameters)
|
||||
return Response.json({ choices: [] })
|
||||
}
|
||||
if (path === "/alpha/generate") {
|
||||
out.push(JSON.parse(init.body).params.tools[0].input_schema)
|
||||
return new Response(JSON.stringify({ type: "finish", finishReason: "stop", totalUsage: { inputTokens: 1, outputTokens: 1 } }) + "\n")
|
||||
}
|
||||
return new Response("", { status: 404 })
|
||||
}
|
||||
const hooks = await CommandCodePlugin()
|
||||
const opts = await hooks.auth.loader(async () => ({ type: "api", key: plan.toLowerCase(), metadata: { plan } }))
|
||||
const body = { model, stream: false, messages: [{ role: "user", content: "hi" }], tools: [tool(schema)] }
|
||||
const res = await opts.fetch("https://api.commandcode.ai/provider/v1/chat/completions", { method: "POST", body: JSON.stringify(body) })
|
||||
await res.text()
|
||||
expect(out.length).toBe(1)
|
||||
return out[0]
|
||||
}
|
||||
|
||||
for (const plan of ["GOAT", "Go"])
|
||||
test(`${plan}: a Google model is sent the schema written out, any other the schema as it came`, async () => {
|
||||
expect(refsIn(await sent(plan, "google/gemini-3.8-flash", DSH))).toEqual([])
|
||||
for (const m of ["anthropic/claude-sonnet-4-6", "deepseek/deepseek-v4-pro", "moonshotai/Kimi-K3"]) expect(await sent(plan, m, DSH)).toEqual(DSH)
|
||||
})
|
||||
@@ -939,6 +939,66 @@ function workPlace(h) {
|
||||
return place
|
||||
}
|
||||
|
||||
// googleSchema is a tool's parameters as Command Code's gateway takes them
|
||||
// for a Google model. It converts the schema for Google (Vertex) and
|
||||
// refuses a $ref to anything but a direct child of the root's $defs or
|
||||
// definitions: 400 AI_UnsupportedFunctionalityError "Google schema
|
||||
// conversion only supports references to direct children of root-level
|
||||
// $defs or definitions … JSON Schema reference:
|
||||
// #/properties/func/properties/triggers/items" (kthink on Discord, dsh on
|
||||
// google/gemini-3.8-flash). Such a $ref is written out in place, with the
|
||||
// notes beside it kept, and a ref to "#/$defs/x" goes as it came. A ref
|
||||
// already being written out above it (a loop) becomes its target's type
|
||||
// alone, and so does one 32 deep, so a schema that refers to itself ends.
|
||||
// A ref that names nothing is left as it came. Only a Google model gets
|
||||
// this; every other model's schema goes as it came.
|
||||
const DEF_REF = /^#\/(?:\$defs|definitions)\/[^/]+$/
|
||||
const SCHEMA_MAPS = ["properties", "patternProperties", "$defs", "definitions", "dependentSchemas"]
|
||||
const SCHEMA_ONE = ["items", "additionalProperties", "contains", "propertyNames", "not", "if", "then", "else", "unevaluatedProperties", "unevaluatedItems", "contentSchema", "additionalItems"]
|
||||
const SCHEMA_LISTS = ["anyOf", "oneOf", "allOf", "prefixItems"]
|
||||
|
||||
const isGoogle = (model) => /^google\/|(?:^|\/)gemini/i.test(String(model ?? ""))
|
||||
|
||||
function pointerTarget(root, ref) {
|
||||
if (ref !== "#" && !ref.startsWith("#/")) return undefined
|
||||
let cur = root
|
||||
for (let tok of ref.split("/").slice(1)) {
|
||||
try {
|
||||
tok = decodeURIComponent(tok)
|
||||
} catch {}
|
||||
tok = tok.replaceAll("~1", "/").replaceAll("~0", "~")
|
||||
if (cur === null || typeof cur !== "object" || !Object.hasOwn(cur, tok)) return undefined
|
||||
cur = cur[tok]
|
||||
}
|
||||
return cur
|
||||
}
|
||||
|
||||
function googleSchema(schema) {
|
||||
if (schema === null || typeof schema !== "object" || !JSON.stringify(schema).includes('"$ref"')) return schema
|
||||
const root = schema
|
||||
const walk = (s, open, depth) => {
|
||||
if (Array.isArray(s)) return s.map((v) => walk(v, open, depth))
|
||||
if (s === null || typeof s !== "object") return s
|
||||
let out = s
|
||||
if (typeof s.$ref === "string" && !(DEF_REF.test(s.$ref) && pointerTarget(root, s.$ref) !== undefined)) {
|
||||
const t = pointerTarget(root, s.$ref)
|
||||
if (t !== null && typeof t === "object" && !Array.isArray(t)) {
|
||||
const { $ref, ...notes } = s
|
||||
if (open.includes($ref) || depth >= 32) return typeof t.type === "string" ? { ...notes, type: t.type } : notes
|
||||
return walk({ ...t, ...notes }, [...open, $ref], depth + 1)
|
||||
}
|
||||
}
|
||||
out = { ...s }
|
||||
for (const k of SCHEMA_MAPS)
|
||||
if (out[k] && typeof out[k] === "object" && !Array.isArray(out[k]))
|
||||
out[k] = Object.fromEntries(Object.entries(out[k]).map(([n, v]) => [n, walk(v, open, depth)]))
|
||||
for (const k of SCHEMA_ONE) if (out[k] && typeof out[k] === "object") out[k] = walk(out[k], open, depth)
|
||||
for (const k of SCHEMA_LISTS) if (Array.isArray(out[k])) out[k] = out[k].map((v) => walk(v, open, depth))
|
||||
return out
|
||||
}
|
||||
return walk(schema, [], 0)
|
||||
}
|
||||
|
||||
// generateBody is a chat completion request as the CLI asks
|
||||
// /alpha/generate, with no structure, branch or status behind it. The
|
||||
// working directory and whether it is a git repository are the agent's as
|
||||
@@ -996,12 +1056,13 @@ function generateBody(chat, place = {}) {
|
||||
if (parts.length) messages.push({ role: "user", content: parts })
|
||||
}
|
||||
flush()
|
||||
const google = isGoogle(chat.model)
|
||||
const tools = (chat.tools ?? [])
|
||||
.filter((t) => t?.type === "function" && t.function?.name)
|
||||
.map((t) => ({
|
||||
name: t.function.name,
|
||||
description: t.function.description ?? "",
|
||||
input_schema: t.function.parameters ?? { type: "object", properties: {} },
|
||||
input_schema: google ? googleSchema(t.function.parameters ?? { type: "object", properties: {} }) : t.function.parameters ?? { type: "object", properties: {} },
|
||||
}))
|
||||
const params = {
|
||||
model: chat.model,
|
||||
@@ -1339,14 +1400,30 @@ async function generate(key, chat, signal, place, zdr = false) {
|
||||
// providerBody is the chat completion chat as the Provider API is sent it,
|
||||
// or undefined when it goes as it came: off, magpie's none or a client's
|
||||
// "off", is "off" for a model that turns thinking off (THINK_OFF), and
|
||||
// left out for any other, which refuses both "none" and "off" (#334).
|
||||
// left out for any other, which refuses both "none" and "off" (#334). A
|
||||
// Google model's tools have the $refs Google can't take written out
|
||||
// (googleSchema).
|
||||
function providerBody(chat) {
|
||||
let out = chat
|
||||
const e = chat?.reasoning_effort
|
||||
if (e !== "none" && e !== "off") return undefined
|
||||
const out = { ...chat }
|
||||
if (THINK_OFF.has(chat.model)) out.reasoning_effort = "off"
|
||||
else delete out.reasoning_effort
|
||||
return e === out.reasoning_effort ? undefined : JSON.stringify(out)
|
||||
if (e === "none" || e === "off") {
|
||||
out = { ...chat }
|
||||
if (THINK_OFF.has(chat.model)) out.reasoning_effort = "off"
|
||||
else delete out.reasoning_effort
|
||||
if (e === out.reasoning_effort) out = chat
|
||||
}
|
||||
if (isGoogle(chat?.model) && Array.isArray(chat.tools)) {
|
||||
let changed = false
|
||||
const tools = chat.tools.map((t) => {
|
||||
const p = t?.function?.parameters
|
||||
const g = googleSchema(p)
|
||||
if (g === p) return t
|
||||
changed = true
|
||||
return { ...t, function: { ...t.function, parameters: g } }
|
||||
})
|
||||
if (changed) out = { ...out, tools }
|
||||
}
|
||||
return out === chat ? undefined : JSON.stringify(out)
|
||||
}
|
||||
|
||||
// systemOne asks Command Code's System One as the CLI asks it: the
|
||||
@@ -1376,7 +1453,7 @@ async function systemOne(key, init, input, place, zdr) {
|
||||
|
||||
// ---- the plugin ---------------------------------------------------------------
|
||||
|
||||
export const _internal = { JEV, projectSlug, subsSeen, extraSeen, waits, liveKey, failure, goModels, refuses, GO_MODELS, GO_REFUSED, zdrOn }
|
||||
export const _internal = { googleSchema, isGoogle, JEV, projectSlug, subsSeen, extraSeen, waits, liveKey, failure, goModels, refuses, GO_MODELS, GO_REFUSED, zdrOn }
|
||||
|
||||
export async function CommandCodePlugin({ client, magpie } = {}) {
|
||||
// isGo keeps a key as Go's, for its models and endpoint: in the
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@magpie-community/opencode-commandcode-auth",
|
||||
"version": "0.1.20",
|
||||
"version": "0.1.21",
|
||||
"description": "Command Code plans (Pro, GOAT, Max, Ultra, Go, Teams) in OpenCode and magpie",
|
||||
"type": "module",
|
||||
"main": "./index.mjs",
|
||||
|
||||
Reference in New Issue
Block a user