mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 19:14:58 +00:00
fix(agents): guard gemini tool schema properties against null
This commit is contained in:
committed by
Peter Steinberger
parent
4e4d94cd38
commit
1a7a18d0bc
46
src/agents/schema/clean-for-gemini.test.ts
Normal file
46
src/agents/schema/clean-for-gemini.test.ts
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
import { cleanSchemaForGemini } from "./clean-for-gemini.js";
|
||||||
|
|
||||||
|
describe("cleanSchemaForGemini", () => {
|
||||||
|
it("coerces null properties to an empty object", () => {
|
||||||
|
const cleaned = cleanSchemaForGemini({
|
||||||
|
type: "object",
|
||||||
|
properties: null,
|
||||||
|
}) as { type?: unknown; properties?: unknown };
|
||||||
|
|
||||||
|
expect(cleaned.type).toBe("object");
|
||||||
|
expect(cleaned.properties).toEqual({});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("coerces non-object properties to an empty object", () => {
|
||||||
|
const cleaned = cleanSchemaForGemini({
|
||||||
|
type: "object",
|
||||||
|
properties: "invalid",
|
||||||
|
}) as { properties?: unknown };
|
||||||
|
|
||||||
|
expect(cleaned.properties).toEqual({});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("coerces nested null properties while preserving valid siblings", () => {
|
||||||
|
const cleaned = cleanSchemaForGemini({
|
||||||
|
type: "object",
|
||||||
|
properties: {
|
||||||
|
bad: {
|
||||||
|
type: "object",
|
||||||
|
properties: null,
|
||||||
|
},
|
||||||
|
good: {
|
||||||
|
type: "string",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}) as {
|
||||||
|
properties?: {
|
||||||
|
bad?: { properties?: unknown };
|
||||||
|
good?: { type?: unknown };
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(cleaned.properties?.bad?.properties).toEqual({});
|
||||||
|
expect(cleaned.properties?.good?.type).toBe("string");
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -304,14 +304,20 @@ function cleanSchemaForGeminiWithDefs(
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (key === "properties" && value && typeof value === "object") {
|
if (key === "properties") {
|
||||||
const props = value as Record<string, unknown>;
|
if (value && typeof value === "object" && !Array.isArray(value)) {
|
||||||
cleaned[key] = Object.fromEntries(
|
const props = value as Record<string, unknown>;
|
||||||
Object.entries(props).map(([k, v]) => [
|
cleaned[key] = Object.fromEntries(
|
||||||
k,
|
Object.entries(props).map(([k, v]) => [
|
||||||
cleanSchemaForGeminiWithDefs(v, nextDefs, refStack),
|
k,
|
||||||
]),
|
cleanSchemaForGeminiWithDefs(v, nextDefs, refStack),
|
||||||
);
|
]),
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
// Guard malformed schemas (e.g. properties: null) that can trigger
|
||||||
|
// downstream Object.* crashes in strict provider validators.
|
||||||
|
cleaned[key] = {};
|
||||||
|
}
|
||||||
} else if (key === "items" && value) {
|
} else if (key === "items" && value) {
|
||||||
if (Array.isArray(value)) {
|
if (Array.isArray(value)) {
|
||||||
cleaned[key] = value.map((entry) =>
|
cleaned[key] = value.map((entry) =>
|
||||||
|
|||||||
Reference in New Issue
Block a user