fix(coding-agent/tools): deduplicate JTD primitive detection
This commit is contained in:
@@ -135,6 +135,18 @@ function convertSchema(schema: unknown): unknown {
|
||||
return {};
|
||||
}
|
||||
|
||||
const jtdOnlyPrimitiveTypes: Record<string, true> = {
|
||||
timestamp: true,
|
||||
float32: true,
|
||||
float64: true,
|
||||
int8: true,
|
||||
uint8: true,
|
||||
int16: true,
|
||||
uint16: true,
|
||||
int32: true,
|
||||
uint32: true,
|
||||
};
|
||||
|
||||
/**
|
||||
* Detect if a schema is JTD format (vs JSON Schema).
|
||||
*
|
||||
@@ -156,11 +168,8 @@ export function isJTDSchema(schema: unknown): boolean {
|
||||
if ("ref" in obj) return true;
|
||||
|
||||
// JTD type primitives (JSON Schema doesn't have int32, float64, etc.)
|
||||
if ("type" in obj) {
|
||||
const jtdPrimitives = ["timestamp", "float32", "float64", "int8", "uint8", "int16", "uint16", "int32", "uint32"];
|
||||
if (jtdPrimitives.includes(obj.type as string)) {
|
||||
return true;
|
||||
}
|
||||
if (typeof obj.type === "string" && Object.hasOwn(jtdOnlyPrimitiveTypes, obj.type)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// JTD properties form without type: "object" (JSON Schema requires it)
|
||||
@@ -171,18 +180,6 @@ export function isJTDSchema(schema: unknown): boolean {
|
||||
return false;
|
||||
}
|
||||
|
||||
const jtdOnlyPrimitiveTypes: Record<string, true> = {
|
||||
timestamp: true,
|
||||
float32: true,
|
||||
float64: true,
|
||||
int8: true,
|
||||
uint8: true,
|
||||
int16: true,
|
||||
uint16: true,
|
||||
int32: true,
|
||||
uint32: true,
|
||||
};
|
||||
|
||||
function isUnambiguousJTDSchema(schema: unknown): boolean {
|
||||
if (!isRecord(schema)) return false;
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { describe, expect, it } from "bun:test";
|
||||
import { jtdToJsonSchema } from "@oh-my-pi/pi-coding-agent/tools/jtd-to-json-schema";
|
||||
import { isJTDSchema, jtdToJsonSchema } from "@oh-my-pi/pi-coding-agent/tools/jtd-to-json-schema";
|
||||
|
||||
describe("jtdToJsonSchema", () => {
|
||||
it("converts JTD elements and int32 primitives into JSON Schema", () => {
|
||||
@@ -33,6 +33,8 @@ describe("jtdToJsonSchema", () => {
|
||||
required: ["results"],
|
||||
additionalProperties: false,
|
||||
});
|
||||
expect(jtdToJsonSchema({ type: "int32" })).toEqual({ type: "integer" });
|
||||
expect(isJTDSchema({ type: "int32" })).toBe(true);
|
||||
});
|
||||
|
||||
it("normalizes nested JTD fragments inside JSON Schema nodes", () => {
|
||||
|
||||
Reference in New Issue
Block a user