Repository navigation
Expand file tree
/
Copy patharray-items.mjs
More file actions
207 lines (191 loc) · 7.67 KB
/
Copy patharray-items.mjs
File metadata and controls
207 lines (191 loc) · 7.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Fill a sibling `items` on array JSON Schemas so Gemini function_declarations
// are not rejected with `items: missing field`. Called as a tree walk after
// sanitizeInputSchema, so this module does not have to live inside that
// function (the RE2 PR also edits the end of sanitizeInputSchema).
function appendSchemaHint(node, hint) {
if (!hint) return
if (typeof node.description === "string" && node.description.trim()) {
if (!node.description.includes(hint)) node.description = `${node.description} (${hint})`
} else {
node.description = hint
}
}
function nodeDeclaresArray(node) {
if (!node || typeof node !== "object" || Array.isArray(node)) return false
if (typeof node.type === "string") return node.type === "array"
if (Array.isArray(node.type)) return node.type.includes("array")
return false
}
function typelessItems(items) {
if (items == null || items === true) return true
if (items === false) return false
if (Array.isArray(items)) return false
if (typeof items !== "object") return true
if (typeof items.$ref === "string" && items.$ref) return false
if (typeof items.type === "string" && items.type) return false
if (Array.isArray(items.type) && items.type.length) return false
return !items.anyOf && !items.oneOf && !items.allOf
}
function cloneSchemaNode(node) {
if (Array.isArray(node)) return node.map((item) => cloneSchemaNode(item))
if (node && typeof node !== "object") return node
if (node && typeof node === "object") return { ...node }
return node
}
function branchCanAcceptArray(branch) {
if (branch == null || branch === false) return false
if (branch === true) return true
if (typeof branch !== "object" || Array.isArray(branch)) return false
if (typeof branch.type === "string") return branch.type === "array"
if (Array.isArray(branch.type)) return branch.type.includes("array")
// Typeless schemas, including {}, accept arrays. Object-only keywords still
// accept non-objects in JSON Schema unless type is set, but a missing type
// must not be treated as "not an array" or an anyOf [{}] would be ignored.
return true
}
function itemsFromCombinators(node) {
const collected = []
for (const key of ["anyOf", "oneOf", "allOf"]) {
if (!Array.isArray(node[key])) continue
let openArray = false
const definite = []
for (const branch of node[key]) {
if (!branchCanAcceptArray(branch)) continue
if (Array.isArray(branch.items)) continue
// An anyOf/oneOf alternative that accepts any array must not let a
// sibling's items become a global constraint (e.g. string items next to
// {} would reject [1]). allOf alternatives all apply, so typeless ones
// do not loosen the others.
if (typelessItems(branch.items)) {
if (key !== "allOf") openArray = true
continue
}
definite.push(cloneSchemaNode(branch.items))
}
if (openArray) return null
collected.push(...definite)
}
if (!collected.length) return null
const unique = []
const seen = new Set()
for (const item of collected) {
const sig = JSON.stringify(item)
if (seen.has(sig)) continue
seen.add(sig)
unique.push(item)
}
if (unique.length === 1) return unique[0]
return { anyOf: unique }
}
function inferItemsFromExisting(existing) {
// Do not stamp type:"object" onto a properties-only items schema: JSON Schema
// would then reject non-object values that were previously allowed.
if (existing.properties || existing.additionalProperties !== undefined) {
return { ...existing }
}
if (Array.isArray(existing.enum) && existing.enum.length && !existing.type) {
const samples = existing.enum.filter((item) => item !== null && item !== undefined)
if (!samples.length) return null
const types = new Set(samples.map((sample) => (
typeof sample === "boolean" ? "boolean"
: typeof sample === "number" ? (Number.isInteger(sample) ? "integer" : "number")
: "string"
)))
if (types.has("number") && types.has("integer")) types.delete("integer")
if (types.size !== 1) return null
return { ...existing, type: [...types][0] }
}
return null
}
export const PERMISSIVE_LEAF_TYPES = ["string", "number", "integer", "boolean", "object", "null"]
// Gemini Schema cannot express an unbounded nested array without $ref, which
// the translator may not honor. Eight levels is enough for tool arguments and
// is idempotent because the innermost array already has typed items.
export const PERMISSIVE_ARRAY_DEPTH = 8
const TUPLE_ITEMS_HINT = "tuple items were left as a list; positional tuples may not be honored"
const UNTYPED_ITEMS_HINT = "array items were untyped; a permissive items schema was added"
function leafSchemas() {
return PERMISSIVE_LEAF_TYPES.map((type) => ({ type }))
}
function permissiveValueSchema(depth) {
const leaves = leafSchemas()
if (depth <= 1) {
return { anyOf: [...leaves, { type: "array", items: { anyOf: leaves.map((item) => ({ ...item })) } }] }
}
return { anyOf: [...leaves, { type: "array", items: permissiveValueSchema(depth - 1) }] }
}
function permissiveItemSchema(existing = {}) {
return { ...existing, ...permissiveValueSchema(PERMISSIVE_ARRAY_DEPTH) }
}
function mergeItems(fromCombo, existing) {
if (!existing || Object.keys(existing).length === 0) return fromCombo
// Keep constraints already on the typeless items node (e.g. maxLength).
return { ...fromCombo, ...existing }
}
function fillGeminiArrayItems(node) {
if (!nodeDeclaresArray(node)) return
if (Array.isArray(node.items)) {
appendSchemaHint(node, TUPLE_ITEMS_HINT)
return
}
const existing = (node.items && typeof node.items === "object" && !Array.isArray(node.items))
? { ...node.items }
: {}
if (Array.isArray(node.prefixItems)) {
appendSchemaHint(node, TUPLE_ITEMS_HINT)
if (!typelessItems(node.items)) return
// prefixItems alone still leaves sibling items missing; fill a rest schema.
const fromCombo = itemsFromCombinators(node)
if (fromCombo) node.items = mergeItems(fromCombo, existing)
else {
const inferred = inferItemsFromExisting(existing)
node.items = inferred || permissiveItemSchema(existing)
if (!inferred) appendSchemaHint(node, UNTYPED_ITEMS_HINT)
}
return
}
if (!typelessItems(node.items)) return
const fromCombo = itemsFromCombinators(node)
if (fromCombo) {
node.items = mergeItems(fromCombo, existing)
return
}
const inferred = inferItemsFromExisting(existing)
if (inferred) {
node.items = inferred
return
}
node.items = permissiveItemSchema(existing)
appendSchemaHint(node, UNTYPED_ITEMS_HINT)
}
const SCHEMA_CHILD_KEYS = [
"additionalProperties", "propertyNames", "contains", "if", "then", "else",
"not", "unevaluatedProperties", "unevaluatedItems", "items",
]
const SCHEMA_ARRAY_KEYS = ["anyOf", "oneOf", "allOf", "prefixItems"]
const SCHEMA_MAP_KEYS = ["properties", "patternProperties", "dependentSchemas", "$defs", "definitions"]
function walk(node) {
if (!node || typeof node !== "object") return
if (Array.isArray(node)) {
for (const item of node) walk(item)
return
}
// Children first so combinator branches are filled before we copy items up.
for (const key of SCHEMA_CHILD_KEYS) walk(node[key])
for (const key of SCHEMA_ARRAY_KEYS) {
if (Array.isArray(node[key])) for (const item of node[key]) walk(item)
}
for (const key of SCHEMA_MAP_KEYS) {
const map = node[key]
if (map && typeof map === "object" && !Array.isArray(map)) {
for (const child of Object.values(map)) walk(child)
}
}
fillGeminiArrayItems(node)
}
// Mutates and returns the schema tree so callers can write
// `ensureGeminiArrayItems(sanitizeInputSchema(raw))`.
export function ensureGeminiArrayItems(node) {
walk(node)
return node
}