mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-19 17:01:37 +03:00
fix(ai): send Kimi models the right tool arguments when schemas use tuples, defaults or keyword-like names (#59207)
This commit is contained in:
committed by
GitHub
Unverified
parent
b9e594be04
commit
3a7ba7a988
@@ -3,7 +3,7 @@ import { isRecord } from "../../utils/record.js"
|
||||
|
||||
// Gemini's `parametersJsonSchema` accepts standard JSON Schema, but rejects a few shapes that
|
||||
// published tool schemas commonly contain. Rewrite only those and send everything else unchanged.
|
||||
const SCHEMA_MAPS = new Set([
|
||||
export const SCHEMA_MAPS = new Set([
|
||||
"properties",
|
||||
"patternProperties",
|
||||
"$defs",
|
||||
@@ -11,9 +11,9 @@ const SCHEMA_MAPS = new Set([
|
||||
"dependentSchemas",
|
||||
"dependencies",
|
||||
])
|
||||
const VALUES = new Set(["const", "default", "enum", "examples", "dependentRequired"])
|
||||
export const VALUES = new Set(["const", "default", "enum", "examples", "dependentRequired"])
|
||||
|
||||
const mapValues = (record: Record<string, unknown>, map: (value: unknown, key: string) => unknown) =>
|
||||
export const mapValues = (record: Record<string, unknown>, map: (value: unknown, key: string) => unknown) =>
|
||||
Object.fromEntries(Object.entries(record).map(([key, value]) => [key, map(value, key)]))
|
||||
|
||||
const normalizeNode = (schema: unknown): unknown => {
|
||||
|
||||
@@ -2,6 +2,8 @@ import { ToolDefinition, type JsonSchema, type LanguageModel, type LLMRequest }
|
||||
import { isRecord } from "../../utils/record.js"
|
||||
import { GeminiJsonSchema } from "./gemini-json-schema.js"
|
||||
|
||||
const { SCHEMA_MAPS, VALUES, mapValues } = GeminiJsonSchema
|
||||
|
||||
const tupleItemsSchema = (items: ReadonlyArray<unknown>) => {
|
||||
const projected = items.map(moonshotNode)
|
||||
if (projected.length === 0) return {}
|
||||
@@ -27,10 +29,16 @@ const moonshotNode = (schema: unknown): unknown => {
|
||||
return Object.fromEntries([
|
||||
...(type === undefined ? [] : [["type", type]]),
|
||||
...Object.entries(schema).flatMap(([key, value]) => {
|
||||
if (key === "items" && Array.isArray(value)) return [[key, tupleItemsSchema(value)]]
|
||||
if (VALUES.has(key)) return [[key, value]]
|
||||
if (SCHEMA_MAPS.has(key) && isRecord(value)) return [[key, mapValues(value, moonshotNode)]]
|
||||
if (key === "items") {
|
||||
if (Array.isArray(schema.prefixItems)) return []
|
||||
if (Array.isArray(value)) return [[key, tupleItemsSchema(value)]]
|
||||
}
|
||||
if (key === "prefixItems") {
|
||||
if ("items" in schema) return []
|
||||
return [["items", tupleItemsSchema(Array.isArray(value) ? value : [])]]
|
||||
if (!Array.isArray(value)) return "items" in schema ? [] : [["items", {}]]
|
||||
const rest = isRecord(schema.items) ? [schema.items] : Array.isArray(schema.items) ? schema.items : []
|
||||
return [["items", tupleItemsSchema([...value, ...rest])]]
|
||||
}
|
||||
if (key === "unevaluatedItems") return []
|
||||
return [[key, moonshotNode(value)]]
|
||||
|
||||
@@ -204,7 +204,20 @@ describe("tool schema projections", () => {
|
||||
linked: { $ref: "#/$defs/Linked", description: "drop me" },
|
||||
tuple: { type: "array", items: [{ type: "string" }, { type: "number" }] },
|
||||
prefixTuple: { type: "array", prefixItems: [{ type: "boolean" }, { type: "string" }] },
|
||||
closedTuple: { type: "array", prefixItems: [{ type: "string" }, { type: "number" }], items: false },
|
||||
restTuple: { type: "array", prefixItems: [{ type: "string" }], items: { type: "number" } },
|
||||
prefixItems: { type: "string" },
|
||||
unevaluatedItems: { type: "boolean" },
|
||||
items: { type: "array", items: { type: "string" } },
|
||||
cart: {
|
||||
type: "object",
|
||||
default: { items: ["a", "b"], $ref: "#/literal", count: 1 },
|
||||
const: { enum: ["literal"] },
|
||||
examples: [{ items: ["x", "y"] }],
|
||||
},
|
||||
},
|
||||
dependentRequired: { items: ["cart"] },
|
||||
dependencies: { items: ["cart"], prefixItems: { required: ["items"] } },
|
||||
}),
|
||||
).toEqual({
|
||||
type: "object",
|
||||
@@ -212,7 +225,20 @@ describe("tool schema projections", () => {
|
||||
linked: { $ref: "#/$defs/Linked" },
|
||||
tuple: { type: "array", items: { anyOf: [{ type: "string" }, { type: "number" }] } },
|
||||
prefixTuple: { type: "array", items: { anyOf: [{ type: "boolean" }, { type: "string" }] } },
|
||||
closedTuple: { type: "array", items: { anyOf: [{ type: "string" }, { type: "number" }] } },
|
||||
restTuple: { type: "array", items: { anyOf: [{ type: "string" }, { type: "number" }] } },
|
||||
prefixItems: { type: "string" },
|
||||
unevaluatedItems: { type: "boolean" },
|
||||
items: { type: "array", items: { type: "string" } },
|
||||
cart: {
|
||||
type: "object",
|
||||
default: { items: ["a", "b"], $ref: "#/literal", count: 1 },
|
||||
const: { enum: ["literal"] },
|
||||
examples: [{ items: ["x", "y"] }],
|
||||
},
|
||||
},
|
||||
dependentRequired: { items: ["cart"] },
|
||||
dependencies: { items: ["cart"], prefixItems: { required: ["items"] } },
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user