pi-antigravity-rotator 2.1.6 → 2.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,277 @@
1
+ export function isRecord(value: unknown): value is Record<string, unknown> {
2
+ return typeof value === "object" && value !== null && !Array.isArray(value);
3
+ }
4
+
5
+ /**
6
+ * Gemini's function_declarations accept a restricted subset of JSON Schema.
7
+ * Keywords like `const`, `$schema`, `$ref`, `$defs`, `if/then/else`, `not`,
8
+ * `patternProperties`, etc. are not supported and will cause a 400.
9
+ * This function recursively strips those unsupported keywords.
10
+ */
11
+ export function sanitizeGeminiSchema(schema: unknown): unknown {
12
+ if (!isRecord(schema)) return schema;
13
+
14
+ // Keywords Gemini does not support
15
+ const UNSUPPORTED = new Set([
16
+ "const", "$schema", "$id", "$ref", "$defs", "definitions",
17
+ "if", "then", "else", "not",
18
+ "patternProperties", "unevaluatedProperties", "unevaluatedItems",
19
+ "contentEncoding", "contentMediaType", "examples",
20
+ "exclusiveMinimum", "exclusiveMaximum", "minimum", "maximum",
21
+ "multipleOf", "minLength", "maxLength", "pattern",
22
+ "minItems", "maxItems", "uniqueItems",
23
+ "minProperties", "maxProperties", "title", "default",
24
+ ]);
25
+
26
+ const out: Record<string, unknown> = {};
27
+ for (const [key, value] of Object.entries(schema)) {
28
+ if (UNSUPPORTED.has(key)) continue;
29
+
30
+ if (key === "anyOf" || key === "oneOf" || key === "allOf") {
31
+ if (Array.isArray(value)) {
32
+ // Special case: all items are pure {const: value} — this is the
33
+ // JSON Schema way of writing an enum. Convert to Gemini's `enum` array.
34
+ const allConst = value.every(
35
+ (item) => isRecord(item) && Object.keys(item).length === 1 && "const" in item,
36
+ );
37
+ if (allConst) {
38
+ out["enum"] = value.map((item) => (item as Record<string, unknown>)["const"]);
39
+ // Infer type:string when all const values are strings (covers most tool params)
40
+ if (value.every((item) => typeof (item as Record<string, unknown>)["const"] === "string")) {
41
+ if (!out["type"]) out["type"] = "string";
42
+ }
43
+ } else {
44
+ const cleaned = value.map(sanitizeGeminiSchema).filter(
45
+ // Drop entries that become empty objects after sanitisation
46
+ (v) => isRecord(v) && Object.keys(v).length > 0,
47
+ );
48
+ // If only one variant remains, unwrap it (Gemini prefers flat schemas)
49
+ if (cleaned.length === 1) {
50
+ Object.assign(out, cleaned[0]);
51
+ } else if (cleaned.length > 1) {
52
+ out[key] = cleaned;
53
+ }
54
+ // cleaned.length === 0: skip entirely
55
+ }
56
+ }
57
+ continue;
58
+ }
59
+
60
+ // Inline union type: `type: ["number","null"]`. Gemini's proto `type`
61
+ // field is a single enum, not repeating — collapse to the first non-null
62
+ // type and lift nullability into the proto-supported `nullable` flag.
63
+ if (key === "type" && Array.isArray(value)) {
64
+ const nonNull = (value as unknown[]).filter((t) => t !== "null");
65
+ if ((value as unknown[]).includes("null")) out["nullable"] = true;
66
+ out["type"] = (nonNull[0] ?? "string");
67
+ continue;
68
+ }
69
+
70
+ if (key === "properties" && isRecord(value)) {
71
+ out[key] = Object.fromEntries(
72
+ Object.entries(value).map(([k, v]) => [k, sanitizeGeminiSchema(v)]),
73
+ );
74
+ continue;
75
+ }
76
+
77
+ if (key === "items") {
78
+ out[key] = sanitizeGeminiSchema(value);
79
+ continue;
80
+ }
81
+
82
+ out[key] = isRecord(value) ? sanitizeGeminiSchema(value) : value;
83
+ }
84
+ return out;
85
+ }
86
+
87
+ /**
88
+ * Lighter sanitization for Claude models routed through Gemini's API.
89
+ * Gemini's outer API still validates schemas before routing to Claude, so
90
+ * we must remove fields Gemini's protobuf doesn't know about (like `const`,
91
+ * `$ref`, etc.). However, unlike the Gemini-native sanitizer, we KEEP
92
+ * standard JSON Schema Draft 2020-12 keywords (minimum, maximum, pattern,
93
+ * etc.) that Claude requires and that Gemini's API does pass through.
94
+ */
95
+ export function sanitizeClaudeViaGeminiSchema(schema: unknown): unknown {
96
+ if (!isRecord(schema)) return schema;
97
+
98
+ // Only remove fields that Gemini's API layer truly rejects at the network level.
99
+ // We keep standard Draft 2020-12 keywords but must strip exclusiveMinimum/exclusiveMaximum
100
+ // as boolean values (Draft 4) — the API layer rejects them even for Claude-bound requests.
101
+ const UNSUPPORTED = new Set([
102
+ "$schema", "$id", "$ref", "$defs", "definitions",
103
+ "if", "then", "else", "not",
104
+ "patternProperties", "unevaluatedProperties", "unevaluatedItems",
105
+ "contentEncoding", "contentMediaType",
106
+ // Gemini's protobuf layer rejects these regardless of target model
107
+ "exclusiveMinimum", "exclusiveMaximum",
108
+ ]);
109
+
110
+ const out: Record<string, unknown> = {};
111
+ for (const [key, value] of Object.entries(schema)) {
112
+ if (UNSUPPORTED.has(key)) continue;
113
+
114
+ // `const` is not supported by Gemini's API — convert to a single-value enum
115
+ if (key === "const") {
116
+ out["enum"] = [value];
117
+ continue;
118
+ }
119
+
120
+ if (key === "anyOf" || key === "oneOf" || key === "allOf") {
121
+ if (Array.isArray(value)) {
122
+ // Case 1: all items are pure {const: value} — convert to flat enum.
123
+ const allPureConst = value.every(
124
+ (item) => isRecord(item) && Object.keys(item).length === 1 && "const" in item,
125
+ );
126
+ if (allPureConst) {
127
+ out["enum"] = value.map((item) => (item as Record<string, unknown>)["const"]);
128
+ if (value.every((item) => typeof (item as Record<string, unknown>)["const"] === "string")) {
129
+ if (!out["type"]) out["type"] = "string";
130
+ }
131
+ continue;
132
+ }
133
+
134
+ // Case 2: all items are {type: T, const: V} (same type, each with a const).
135
+ // e.g. [{type:"string",const:"fact"},{type:"string",const:"lesson"}]
136
+ // Merge into a single flat {type: T, enum: [V1, V2, ...]} — avoids
137
+ // the redundant anyOf-with-single-enum pattern that Claude rejects.
138
+ const allTypeConst = value.every(
139
+ (item) =>
140
+ isRecord(item) &&
141
+ Object.keys(item).length === 2 &&
142
+ "type" in item &&
143
+ "const" in item,
144
+ );
145
+ if (allTypeConst) {
146
+ const firstType = (value[0] as Record<string, unknown>)["type"];
147
+ const allSameType = value.every((item) => (item as Record<string, unknown>)["type"] === firstType);
148
+ if (allSameType) {
149
+ if (!out["type"]) out["type"] = firstType;
150
+ out["enum"] = value.map((item) => (item as Record<string, unknown>)["const"]);
151
+ continue;
152
+ }
153
+ }
154
+
155
+ // Sanitize all variants first.
156
+ const cleaned = value.map(sanitizeClaudeViaGeminiSchema).filter(
157
+ (v) => isRecord(v) && Object.keys(v).length > 0,
158
+ ) as Record<string, unknown>[];
159
+
160
+ if (cleaned.length === 0) {
161
+ // All variants collapsed to nothing — skip entirely.
162
+ continue;
163
+ }
164
+
165
+ // Case 3: nullable pattern — anyOf/oneOf with exactly one {type:"null"}
166
+ // variant and one or more real variants. Convert to the real variant
167
+ // with nullable:true. This is lossless — Gemini's proto supports nullable.
168
+ // e.g. anyOf:[{type:"string"},{type:"null"}] → {type:"string",nullable:true}
169
+ if (key !== "allOf") {
170
+ const nullIdx = cleaned.findIndex((v) => v.type === "null" && Object.keys(v).length === 1);
171
+ if (nullIdx !== -1) {
172
+ const nonNull = cleaned.filter((_, i) => i !== nullIdx);
173
+ if (nonNull.length === 1) {
174
+ Object.assign(out, nonNull[0], { nullable: true });
175
+ continue;
176
+ }
177
+ if (nonNull.length > 1) {
178
+ // Multiple non-null variants + null → collapse non-null variants,
179
+ // then mark nullable. Still lossy but preserves nullability.
180
+ Object.assign(out, nonNull[0], { nullable: true });
181
+ continue;
182
+ }
183
+ }
184
+ }
185
+
186
+ // Case 4: allOf — deep merge all variants (allOf = intersection).
187
+ // Merging properties from all variants is semantically correct.
188
+ if (key === "allOf") {
189
+ const merged: Record<string, unknown> = {};
190
+ let mergedProperties: Record<string, unknown> = {};
191
+ let mergedRequired: string[] = [];
192
+ for (const variant of cleaned) {
193
+ for (const [vk, vv] of Object.entries(variant)) {
194
+ if (vk === "properties" && isRecord(vv)) {
195
+ mergedProperties = { ...mergedProperties, ...vv };
196
+ } else if (vk === "required" && Array.isArray(vv)) {
197
+ mergedRequired = [...new Set([...mergedRequired, ...vv])];
198
+ } else {
199
+ merged[vk] = vv;
200
+ }
201
+ }
202
+ }
203
+ if (Object.keys(mergedProperties).length > 0) merged["properties"] = mergedProperties;
204
+ if (mergedRequired.length > 0) merged["required"] = mergedRequired;
205
+ Object.assign(out, merged);
206
+ continue;
207
+ }
208
+
209
+ // Case 5: anyOf/oneOf where all variants are objects with properties —
210
+ // merge all properties together, making all optional (union of shapes).
211
+ // This is mildly lossy (accepts wider input) but doesn't reject valid inputs.
212
+ const allObjects = cleaned.every(
213
+ (v) => v.type === "object" && isRecord(v.properties),
214
+ );
215
+ if (allObjects && cleaned.length > 1) {
216
+ const unionProperties: Record<string, unknown> = {};
217
+ for (const variant of cleaned) {
218
+ const props = variant.properties as Record<string, unknown>;
219
+ for (const [pk, pv] of Object.entries(props)) {
220
+ if (!(pk in unionProperties)) unionProperties[pk] = pv;
221
+ }
222
+ }
223
+ // Only keep required fields that exist in ALL variants
224
+ const allRequired = cleaned.map((v) =>
225
+ Array.isArray(v.required) ? new Set(v.required as string[]) : new Set<string>(),
226
+ );
227
+ const commonRequired = [...allRequired[0]].filter((r) =>
228
+ allRequired.every((s) => s.has(r)),
229
+ );
230
+ const base = { ...cleaned[0] };
231
+ base["properties"] = unionProperties;
232
+ if (commonRequired.length > 0) {
233
+ base["required"] = commonRequired;
234
+ } else {
235
+ delete base["required"];
236
+ }
237
+ Object.assign(out, base);
238
+ continue;
239
+ }
240
+
241
+ // Fallback: collapse to the first valid variant.
242
+ // Gemini's Schema proto serialization corrupts complex anyOf/oneOf
243
+ // during the round-trip to Claude, causing JSON Schema draft 2020-12
244
+ // validation failures. Collapsing is lossy but functional — the tool
245
+ // still works, just with a narrower accepted input type.
246
+ Object.assign(out, cleaned[0]);
247
+ }
248
+ continue;
249
+ }
250
+
251
+ // Inline union type: `type: ["number","null"]`. Gemini's proto `type`
252
+ // field is a single enum, not repeating — an array value triggers a 400
253
+ // ('Proto field is not repeating'). Collapse to the first non-null type
254
+ // and lift nullability into the proto-supported `nullable` flag.
255
+ if (key === "type" && Array.isArray(value)) {
256
+ const nonNull = (value as unknown[]).filter((t) => t !== "null");
257
+ if ((value as unknown[]).includes("null")) out["nullable"] = true;
258
+ out["type"] = (nonNull[0] ?? "string");
259
+ continue;
260
+ }
261
+
262
+ if (key === "properties" && isRecord(value)) {
263
+ out[key] = Object.fromEntries(
264
+ Object.entries(value).map(([k, v]) => [k, sanitizeClaudeViaGeminiSchema(v)]),
265
+ );
266
+ continue;
267
+ }
268
+
269
+ if (key === "items") {
270
+ out[key] = sanitizeClaudeViaGeminiSchema(value);
271
+ continue;
272
+ }
273
+
274
+ out[key] = isRecord(value) ? sanitizeClaudeViaGeminiSchema(value) : value;
275
+ }
276
+ return out;
277
+ }