mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-18 21:22:28 +03:00
This commit is contained in:
@@ -727,5 +727,33 @@ export function cleanJSONSchemaForAntigravity(schema: unknown): unknown {
|
||||
|
||||
injectObjectType(cleaned);
|
||||
|
||||
// Phase 8: Ensure array types have an items schema (#10578).
|
||||
// Gemini strictly requires array parameters to define their `items` schema.
|
||||
// If an MCP tool defines an array but forgets the items, inject a safe default.
|
||||
function ensureArrayItems(obj: unknown): void {
|
||||
if (!obj || typeof obj !== "object") return;
|
||||
|
||||
if (Array.isArray(obj)) {
|
||||
for (const item of obj) {
|
||||
ensureArrayItems(item);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const record = obj as JsonRecord;
|
||||
if (record.type === "array" && !record.items) {
|
||||
record.items = { type: "string" };
|
||||
}
|
||||
|
||||
// Recurse into remaining values.
|
||||
for (const value of Object.values(record)) {
|
||||
if (value && typeof value === "object") {
|
||||
ensureArrayItems(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ensureArrayItems(cleaned);
|
||||
|
||||
return cleaned;
|
||||
}
|
||||
|
||||
47
tests/unit/gemini-array-items.test.ts
Normal file
47
tests/unit/gemini-array-items.test.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import { describe, it } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { cleanJSONSchemaForAntigravity } from "../../open-sse/translator/helpers/geminiHelper";
|
||||
|
||||
type SchemaNode = {
|
||||
type?: string;
|
||||
properties?: Record<string, SchemaNode>;
|
||||
items?: SchemaNode;
|
||||
required?: string[];
|
||||
[key: string]: unknown;
|
||||
};
|
||||
|
||||
describe("Gemini array items sanitizer (#10578)", () => {
|
||||
it("should inject a string items schema for array types that are missing it", () => {
|
||||
const sloppySchema = {
|
||||
type: "object",
|
||||
properties: {
|
||||
emails: {
|
||||
type: "array",
|
||||
},
|
||||
},
|
||||
required: ["emails"],
|
||||
};
|
||||
|
||||
const cleanedSchema = cleanJSONSchemaForAntigravity(sloppySchema as unknown) as SchemaNode;
|
||||
|
||||
assert.equal(cleanedSchema.properties?.emails?.type, "array");
|
||||
assert.ok(cleanedSchema.properties?.emails?.items, "items should be injected");
|
||||
assert.equal(cleanedSchema.properties?.emails?.items?.type, "string");
|
||||
});
|
||||
|
||||
it("should safely ignore arrays that already have valid items", () => {
|
||||
const goodSchema = {
|
||||
type: "object",
|
||||
properties: {
|
||||
tags: {
|
||||
type: "array",
|
||||
items: { type: "number" },
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const cleanedSchema = cleanJSONSchemaForAntigravity(goodSchema as unknown) as SchemaNode;
|
||||
|
||||
assert.equal(cleanedSchema.properties?.tags?.items?.type, "number");
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user