hataraku
Version:
An autonomous coding agent for building AI-powered development tools. The name "Hataraku" (働く) means "to work" in Japanese.
96 lines • 2.94 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.zodToJsonSchema = zodToJsonSchema;
exports.zodToSchemaString = zodToSchemaString;
const zod_1 = require("zod");
/**
* Convert a Zod schema to JSON Schema format
*/
function zodToJsonSchema(schema) {
if (schema instanceof zod_1.z.ZodObject) {
const shape = schema._def.shape();
const properties = {};
const required = [];
Object.entries(shape).forEach(([key, value]) => {
if (value instanceof zod_1.z.ZodType) {
properties[key] = zodTypeToJsonSchema(value);
if (!value.isOptional()) {
required.push(key);
}
}
});
return {
type: 'object',
properties,
...(required.length > 0 ? { required } : {})
};
}
// Default to empty object schema if not an object schema
return {
type: 'object',
properties: {}
};
}
function zodTypeToJsonSchema(schema) {
if (schema instanceof zod_1.z.ZodString) {
return { type: 'string' };
}
if (schema instanceof zod_1.z.ZodNumber) {
return { type: 'number' };
}
if (schema instanceof zod_1.z.ZodBoolean) {
return { type: 'boolean' };
}
if (schema instanceof zod_1.z.ZodArray) {
return {
type: 'array',
items: zodTypeToJsonSchema(schema.element)
};
}
if (schema instanceof zod_1.z.ZodObject) {
return zodToJsonSchema(schema);
}
if (schema instanceof zod_1.z.ZodEnum) {
return {
type: 'string',
enum: schema._def.values
};
}
if (schema instanceof zod_1.z.ZodUnion) {
return {
oneOf: schema._def.options.map((opt) => zodTypeToJsonSchema(opt))
};
}
if (schema instanceof zod_1.z.ZodOptional) {
return zodTypeToJsonSchema(schema._def.innerType);
}
if (schema instanceof zod_1.z.ZodNullable) {
return {
oneOf: [
zodTypeToJsonSchema(schema._def.innerType),
{ type: 'null' }
]
};
}
// Default to any type if unknown
return {};
}
function zodToSchemaString(schema) {
if (schema instanceof zod_1.z.ZodObject) {
const shape = schema._def.shape();
const shapeEntries = Object.entries(shape).map(([key, value]) => {
if (value instanceof zod_1.z.ZodString) {
return `"${key}": z.string()`;
}
if (value instanceof zod_1.z.ZodNumber) {
return `"${key}": z.number()`;
}
// Add more types as needed
return `"${key}": z.unknown()`;
});
return `{${shapeEntries.join(', ')}}`;
}
// Handle other schema types as needed
return 'z.unknown()';
}
//# sourceMappingURL=schema.js.map