@wavequery/conductor
Version:
Modular LLM orchestration framework
103 lines • 3.15 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ToolValidator = void 0;
const zod_1 = require("zod");
const zod_to_json_schema_1 = require("zod-to-json-schema");
class ToolValidator {
constructor(inputSchema, outputSchema) {
this.inputSchema = inputSchema;
this.outputSchema = outputSchema;
}
async validateInput(input) {
try {
const data = (await this.inputSchema.parseAsync(input));
return { success: true, data };
}
catch (error) {
if (error instanceof zod_1.z.ZodError) {
return {
success: false,
errors: this.formatZodErrors(error),
};
}
throw error;
}
}
async validateOutput(output) {
try {
const data = (await this.outputSchema.parseAsync(output));
return { success: true, data };
}
catch (error) {
if (error instanceof zod_1.z.ZodError) {
return {
success: false,
errors: this.formatZodErrors(error),
};
}
throw error;
}
}
formatZodErrors(error) {
return error.errors.map((err) => ({
path: err.path.map((p) => p.toString()),
message: err.message,
}));
}
getInputSchema() {
return this.schemaToJSON(this.inputSchema);
}
getOutputSchema() {
return this.schemaToJSON(this.outputSchema);
}
schemaToJSON(schema) {
return (0, zod_to_json_schema_1.zodToJsonSchema)(schema, {
$refStrategy: "none",
definitionPath: "definitions",
});
}
}
exports.ToolValidator = ToolValidator;
// Example schema builders for common tool patterns
// export const schemaBuilders = {
// /**
// * Creates a schema for paginated inputs
// */
// withPagination: <T extends z.ZodSchema>(schema: T) => {
// return schema.extend({
// page: z.number().int().min(1).optional().default(1),
// limit: z.number().int().min(1).max(100).optional().default(10)
// });
// },
// /**
// * Creates a schema for filterable inputs
// */
// withFilters: <T extends z.ZodSchema>(schema: T, filterSchema: z.ZodSchema) => {
// return schema.extend({
// filters: z.array(filterSchema).optional()
// });
// },
// /**
// * Creates a schema for sortable inputs
// */
// withSorting: <T extends z.ZodSchema>(schema: T, sortableFields: string[]) => {
// return schema.extend({
// sortBy: z.enum(sortableFields as [string, ...string[]]).optional(),
// sortOrder: z.enum(['asc', 'desc']).optional().default('asc')
// });
// }
// };
// Helper function to create tool validator
// export function createToolValidator<
// Input = any,
// Output = any
// >(config: {
// input: z.ZodSchema<Input>;
// output: z.ZodSchema<Output>;
// }): ToolValidator {
// return new ToolValidator(
// config.input,
// config.output
// );
// }
//# sourceMappingURL=tool-validation.js.map