mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-05 22:10:21 +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;
|
||||
}
|
||||
|
||||
if (key === "properties" && value && typeof value === "object") {
|
||||
const props = value as Record<string, unknown>;
|
||||
cleaned[key] = Object.fromEntries(
|
||||
Object.entries(props).map(([k, v]) => [
|
||||
k,
|
||||
cleanSchemaForGeminiWithDefs(v, nextDefs, refStack),
|
||||
]),
|
||||
);
|
||||
if (key === "properties") {
|
||||
if (value && typeof value === "object" && !Array.isArray(value)) {
|
||||
const props = value as Record<string, unknown>;
|
||||
cleaned[key] = Object.fromEntries(
|
||||
Object.entries(props).map(([k, v]) => [
|
||||
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) {
|
||||
if (Array.isArray(value)) {
|
||||
cleaned[key] = value.map((entry) =>
|
||||
|
||||
Reference in New Issue
Block a user