|
1 | 1 | type Schema = Record<string, unknown>; |
2 | 2 |
|
3 | | -// Gemini / Antigravity (CCA) accept only an OpenAPI-3.0 subset for function `parameters`. Codex |
4 | | -// emits full JSON-Schema (draft 2020-12) tool definitions, so passing them through verbatim makes |
5 | | -// CCA reject the whole request with "Request contains an invalid argument" / "Unknown name ...". |
6 | | -// Every keyword below was confirmed live against the Antigravity backend to trigger a 400. |
7 | | -// `encrypted` is Codex's Responses-only marker (openai/codex 5f4d06ef, PR #26210) stamped on v2 |
8 | | -// collaboration tool schemas (spawn_agent/send_message/followup_task `message`); CCA rejects it |
9 | | -// with 400 "Unknown name \"encrypted\"" (issue #85). It is an annotation for the ChatGPT backend |
10 | | -// only, so dropping it never changes tool behavior. |
11 | | -const DROPPED_SCHEMA_KEYS = new Set([ |
12 | | - "$schema", "$id", "$comment", "$ref", "$defs", "definitions", |
13 | | - "examples", "patternProperties", "if", "then", "else", |
14 | | - "uniqueItems", "additionalItems", "unevaluatedProperties", "unevaluatedItems", |
15 | | - "dependentRequired", "dependentSchemas", "propertyNames", "contains", |
16 | | - "encrypted", |
17 | | -]); |
18 | | - |
19 | | -const MAX_DEREF_DEPTH = 64; |
| 3 | +// Google documents this function-schema subset: type, nullable, required, format, description, |
| 4 | +// properties, items, enum, anyOf, $ref, and $defs. We inline local refs and normalize anyOf, so |
| 5 | +// only the eight scalar/container keywords below are ever emitted. Building from an allowlist |
| 6 | +// prevents new MCP/JSON-Schema annotations from turning into provider-wide 400 responses. |
| 7 | +const ALLOWED_TYPES = new Set(["string", "integer", "number", "boolean", "array", "object"]); |
| 8 | +const MAX_SCHEMA_DEPTH = 24; // Google's documented nesting limit is 32; leave headroom for CCA. |
| 9 | +const MAX_DEREF_DEPTH = 16; |
| 10 | + |
| 11 | +function isRecord(value: unknown): value is Schema { |
| 12 | + return !!value && typeof value === "object" && !Array.isArray(value); |
| 13 | +} |
20 | 14 |
|
21 | 15 | function resolveRef(ref: string, defs: Map<string, unknown>): unknown { |
22 | | - // Only local pointers into the schema's own $defs/definitions are supported (e.g. |
23 | | - // "#/$defs/Foo"). Anything else cannot be inlined, so it collapses to an unconstrained object. |
| 16 | + // Only local pointers into the schema's own $defs/definitions are safe to inline. |
24 | 17 | const match = /^#\/(?:\$defs|definitions)\/(.+)$/.exec(ref); |
25 | 18 | if (!match) return undefined; |
26 | | - return defs.get(decodeURIComponent(match[1].replace(/~1/g, "/").replace(/~0/g, "~"))); |
| 19 | + try { |
| 20 | + return defs.get(decodeURIComponent(match[1].replace(/~1/g, "/").replace(/~0/g, "~"))); |
| 21 | + } catch { |
| 22 | + return undefined; |
| 23 | + } |
27 | 24 | } |
28 | 25 |
|
29 | 26 | function collectDefs(root: unknown, defs: Map<string, unknown>): void { |
30 | | - if (!root || typeof root !== "object") return; |
| 27 | + if (!isRecord(root)) return; |
31 | 28 | for (const bag of ["$defs", "definitions"] as const) { |
32 | | - const group = (root as Schema)[bag]; |
33 | | - if (group && typeof group === "object" && !Array.isArray(group)) { |
34 | | - for (const [name, value] of Object.entries(group as Schema)) { |
35 | | - if (!defs.has(name)) defs.set(name, value); |
36 | | - } |
| 29 | + const group = root[bag]; |
| 30 | + if (!isRecord(group)) continue; |
| 31 | + for (const [name, value] of Object.entries(group)) { |
| 32 | + if (!defs.has(name)) defs.set(name, value); |
37 | 33 | } |
38 | 34 | } |
39 | 35 | } |
40 | 36 |
|
41 | | -function normalizeType(value: unknown, out: Schema): void { |
42 | | - // JSON-Schema allows `type` to be an array (e.g. ["string","null"]); OpenAPI 3.0 does not. |
43 | | - // Collapse to the first non-null type and mark the field nullable when "null" was present. |
44 | | - if (!Array.isArray(value)) { |
45 | | - out.type = value; |
46 | | - return; |
| 37 | +function normalizeType(value: unknown, out: Schema, preserveNullType: boolean): void { |
| 38 | + const candidates = Array.isArray(value) ? value : [value]; |
| 39 | + let sawNull = false; |
| 40 | + |
| 41 | + for (const candidate of candidates) { |
| 42 | + if (typeof candidate !== "string") continue; |
| 43 | + const type = candidate.toLowerCase(); |
| 44 | + if (type === "null") { |
| 45 | + sawNull = true; |
| 46 | + } else if (out.type === undefined && ALLOWED_TYPES.has(type)) { |
| 47 | + out.type = type; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + if (!sawNull) return; |
| 52 | + if (out.type !== undefined) out.nullable = true; |
| 53 | + else if (preserveNullType) out.type = "null"; |
| 54 | + else out.nullable = true; |
| 55 | +} |
| 56 | + |
| 57 | +function sanitizeEnum(value: unknown): string[] | undefined { |
| 58 | + if (!Array.isArray(value)) return undefined; |
| 59 | + const values = [...new Set(value.filter((item): item is string => typeof item === "string"))]; |
| 60 | + return values.length > 0 ? values : undefined; |
| 61 | +} |
| 62 | + |
| 63 | +function normalizeAnyOf( |
| 64 | + value: unknown, |
| 65 | + defs: Map<string, unknown>, |
| 66 | + depth: number, |
| 67 | + refDepth: number, |
| 68 | +): Schema { |
| 69 | + if (!Array.isArray(value) || value.length === 0) return {}; |
| 70 | + const schemas = value.map(item => sanitizeSchema(item, defs, depth + 1, refDepth, true)); |
| 71 | + |
| 72 | + const nonNullSchemas = schemas.filter(schema => schema.type !== "null"); |
| 73 | + const nullSchemas = schemas.filter(schema => schema.type === "null"); |
| 74 | + if ( |
| 75 | + nonNullSchemas.length === 1 |
| 76 | + && nullSchemas.length > 0 |
| 77 | + && nullSchemas.every(schema => Object.keys(schema).every(key => key === "type")) |
| 78 | + ) { |
| 79 | + return { ...nonNullSchemas[0], nullable: true }; |
| 80 | + } |
| 81 | + |
| 82 | + const type = schemas[0]?.type; |
| 83 | + const sameType = schemas.length > 0 && schemas.every(schema => schema.type === type); |
| 84 | + const enumOnly = schemas.every(schema => { |
| 85 | + const allowedKeys = type === undefined ? new Set(["enum"]) : new Set(["type", "enum"]); |
| 86 | + return Array.isArray(schema.enum) && Object.keys(schema).every(key => allowedKeys.has(key)); |
| 87 | + }); |
| 88 | + if (sameType && enumOnly && type !== "null") { |
| 89 | + const values = sanitizeEnum(schemas.flatMap(schema => schema.enum as unknown[])); |
| 90 | + if (values) return { ...(typeof type === "string" ? { type } : {}), enum: values }; |
| 91 | + } |
| 92 | + |
| 93 | + // CCA's Claude bridge turns typed anyOf branches into an invalid input_schema. Widen only this |
| 94 | + // node when a union cannot be collapsed losslessly; parent annotations and structure survive. |
| 95 | + return {}; |
| 96 | +} |
| 97 | + |
| 98 | +function sanitizeProperties( |
| 99 | + value: unknown, |
| 100 | + defs: Map<string, unknown>, |
| 101 | + depth: number, |
| 102 | + refDepth: number, |
| 103 | +): Record<string, Schema> | undefined { |
| 104 | + if (!isRecord(value)) return undefined; |
| 105 | + const properties: Record<string, Schema> = Object.create(null) as Record<string, Schema>; |
| 106 | + for (const [name, schema] of Object.entries(value)) { |
| 107 | + // Property names form a name bag and must never be interpreted as schema keywords. |
| 108 | + properties[name] = sanitizeSchema(schema, defs, depth + 1, refDepth, false); |
47 | 109 | } |
48 | | - const nonNull = value.filter(t => t !== "null"); |
49 | | - if (value.includes("null")) out.nullable = true; |
50 | | - if (nonNull.length > 0) out.type = nonNull[0]; |
| 110 | + return properties; |
51 | 111 | } |
52 | 112 |
|
53 | | -function sanitize(node: unknown, defs: Map<string, unknown>, depth: number): unknown { |
54 | | - if (Array.isArray(node)) return node.map(item => sanitize(item, defs, depth)); |
55 | | - if (!node || typeof node !== "object") return node; |
56 | | - const input = node as Schema; |
| 113 | +function sanitizeSchema( |
| 114 | + node: unknown, |
| 115 | + defs: Map<string, unknown>, |
| 116 | + depth: number, |
| 117 | + refDepth: number, |
| 118 | + preserveNullType: boolean, |
| 119 | +): Schema { |
| 120 | + if (depth >= MAX_SCHEMA_DEPTH || !isRecord(node)) return {}; |
57 | 121 |
|
58 | | - if (typeof input.$ref === "string" && depth < MAX_DEREF_DEPTH) { |
59 | | - const target = resolveRef(input.$ref, defs); |
60 | | - if (target && typeof target === "object") { |
61 | | - const merged: Schema = { ...(target as Schema) }; |
62 | | - for (const [key, value] of Object.entries(input)) { |
| 122 | + if (typeof node.$ref === "string" && refDepth < MAX_DEREF_DEPTH) { |
| 123 | + const target = resolveRef(node.$ref, defs); |
| 124 | + if (isRecord(target)) { |
| 125 | + const merged: Schema = { ...target }; |
| 126 | + for (const [key, value] of Object.entries(node)) { |
63 | 127 | if (key !== "$ref") merged[key] = value; |
64 | 128 | } |
65 | | - return sanitize(merged, defs, depth + 1); |
| 129 | + return sanitizeSchema(merged, defs, depth, refDepth + 1, preserveNullType); |
66 | 130 | } |
67 | 131 | } |
68 | 132 |
|
69 | 133 | const out: Schema = {}; |
70 | | - for (const [key, value] of Object.entries(input)) { |
71 | | - if (DROPPED_SCHEMA_KEYS.has(key)) continue; |
72 | | - if (key === "type") { normalizeType(value, out); continue; } |
73 | | - if (key === "const") { out.enum = [value]; continue; } |
74 | | - if (key === "exclusiveMinimum" && typeof value === "number") { out.minimum = value; continue; } |
75 | | - if (key === "exclusiveMaximum" && typeof value === "number") { out.maximum = value; continue; } |
76 | | - if (key === "required" && Array.isArray(value)) { |
77 | | - out.required = [...new Set(value.filter((item): item is string => typeof item === "string"))]; |
78 | | - continue; |
79 | | - } |
80 | | - if (key === "additionalProperties") { |
81 | | - // A boolean additionalProperties is accepted, but a nested schema is only meaningful with |
82 | | - // its own sanitize pass. |
83 | | - out.additionalProperties = typeof value === "boolean" ? value : sanitize(value, defs, depth); |
84 | | - continue; |
85 | | - } |
86 | | - out[key] = sanitize(value, defs, depth); |
| 134 | + normalizeType(node.type, out, preserveNullType); |
| 135 | + |
| 136 | + if (typeof node.nullable === "boolean") out.nullable = node.nullable; |
| 137 | + if (typeof node.description === "string") out.description = node.description; |
| 138 | + if (typeof node.format === "string") out.format = node.format; |
| 139 | + |
| 140 | + const enumValues = sanitizeEnum(node.enum ?? (typeof node.const === "string" ? [node.const] : undefined)); |
| 141 | + if (enumValues) out.enum = enumValues; |
| 142 | + |
| 143 | + const properties = sanitizeProperties(node.properties, defs, depth, refDepth); |
| 144 | + if (properties) out.properties = properties; |
| 145 | + |
| 146 | + if (isRecord(node.items)) { |
| 147 | + out.items = sanitizeSchema(node.items, defs, depth + 1, refDepth, false); |
| 148 | + } |
| 149 | + |
| 150 | + if (Array.isArray(node.required)) { |
| 151 | + out.required = [...new Set(node.required.filter((item): item is string => typeof item === "string"))]; |
87 | 152 | } |
| 153 | + |
| 154 | + if (node.anyOf !== undefined) Object.assign(out, normalizeAnyOf(node.anyOf, defs, depth, refDepth)); |
88 | 155 | return out; |
89 | 156 | } |
90 | 157 |
|
91 | 158 | export function sanitizeGeminiToolParameters(parameters: unknown): Record<string, unknown> { |
92 | | - const defs = new Map<string, unknown>(); |
93 | | - collectDefs(parameters, defs); |
94 | | - const result = sanitize(parameters, defs, 0); |
95 | | - return result && typeof result === "object" && !Array.isArray(result) |
96 | | - ? result as Record<string, unknown> |
97 | | - : { type: "object" }; |
| 159 | + try { |
| 160 | + const defs = new Map<string, unknown>(); |
| 161 | + collectDefs(parameters, defs); |
| 162 | + const root = sanitizeSchema(parameters, defs, 0, 0, false); |
| 163 | + |
| 164 | + // Function arguments are always an object. Claude additionally rejects root composition and a |
| 165 | + // missing root type even when those forms are valid general-purpose JSON Schema. |
| 166 | + root.type = "object"; |
| 167 | + if (!isRecord(root.properties)) root.properties = {}; |
| 168 | + return root; |
| 169 | + } catch { |
| 170 | + // Last-resort containment: no third-party schema may break every tool in the request. |
| 171 | + return { type: "object", properties: {} }; |
| 172 | + } |
98 | 173 | } |
0 commit comments