Files
yetone d05f55e7c9 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.
2026-10-31 18:04:23 +08:00

120 lines
5.6 KiB
JavaScript

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