diff --git a/packages/ai/src/protocols/utils/gemini-json-schema.ts b/packages/ai/src/protocols/utils/gemini-json-schema.ts index d9af7870a9d..b5d491d148b 100644 --- a/packages/ai/src/protocols/utils/gemini-json-schema.ts +++ b/packages/ai/src/protocols/utils/gemini-json-schema.ts @@ -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, map: (value: unknown, key: string) => unknown) => +export const mapValues = (record: Record, map: (value: unknown, key: string) => unknown) => Object.fromEntries(Object.entries(record).map(([key, value]) => [key, map(value, key)])) const normalizeNode = (schema: unknown): unknown => { diff --git a/packages/ai/src/protocols/utils/tool-schema.ts b/packages/ai/src/protocols/utils/tool-schema.ts index b2525eb93d5..05af2e48665 100644 --- a/packages/ai/src/protocols/utils/tool-schema.ts +++ b/packages/ai/src/protocols/utils/tool-schema.ts @@ -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) => { 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)]] diff --git a/packages/ai/test/tool-schema-projection.test.ts b/packages/ai/test/tool-schema-projection.test.ts index 5888c74edeb..018680d98d5 100644 --- a/packages/ai/test/tool-schema-projection.test.ts +++ b/packages/ai/test/tool-schema-projection.test.ts @@ -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"] } }, }) })