Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion skills/rig/rig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,9 @@ function serializeSchema(schema: Schema): JsonSchemaObject {
const withDescription = (obj: JsonSchemaObject): JsonSchemaObject =>
description === undefined ? obj : { ...obj, description };
if ("enum" in schema) {
return withDescription({ enum: schema.enum });
const enumValues = schema.enum as readonly unknown[];
const allStrings = enumValues.length > 0 && enumValues.every((v) => typeof v === "string");
return withDescription(allStrings ? { type: "string", enum: schema.enum } : { enum: schema.enum });
}
if ("items" in schema) {
return withDescription({ type: "array", items: serializeSchema(schema.items) });
Expand Down
11 changes: 9 additions & 2 deletions src/rig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1002,8 +1002,15 @@ describe("toJsonSchema", () => {
});

it("converts enum schemas", () => {
expect(toJsonSchema(s.enum("a", "b", "c"))).toEqual({ enum: ["a", "b", "c"] });
expect(toJsonSchema(s.enum(["x", "y"], "A choice"))).toEqual({ enum: ["x", "y"], description: "A choice" });
expect(toJsonSchema(s.enum("a", "b", "c"))).toEqual({ type: "string", enum: ["a", "b", "c"] });
expect(toJsonSchema(s.enum(["x", "y"], "A choice"))).toEqual({ type: "string", enum: ["x", "y"], description: "A choice" });
});

it("adds type:string to all-string enums but not mixed enums", () => {
expect(toJsonSchema(s.enum("low", "medium", "high"))).toEqual({ type: "string", enum: ["low", "medium", "high"] });
expect(toJsonSchema(s.enum(1, 2, 3))).toEqual({ enum: [1, 2, 3] });
expect(toJsonSchema(s.enum("yes", 1))).toEqual({ enum: ["yes", 1] });
expect(toJsonSchema(s.enum())).toEqual({ enum: [] });
});

it("converts array schemas", () => {
Expand Down
Loading