mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-16 15:02:32 +00:00
Replace Zod with TypeBox for schema validation
- Switch from Zod to TypeBox for tool parameter schemas - TypeBox schemas can be serialized/deserialized as JSON - Use AJV for runtime validation instead of Zod's parse - Add StringEnum helper for Google API compatibility (avoids anyOf/const patterns) - Export Type and Static from main package for convenience - Update all tests and documentation to reflect TypeBox usage
This commit is contained in:
parent
f5ac1ef521
commit
e8370436d7
16 changed files with 196 additions and 121 deletions
|
|
@ -1,32 +1,37 @@
|
|||
import { z } from "zod";
|
||||
import Ajv from "ajv";
|
||||
import addFormats from "ajv-formats";
|
||||
import type { Tool, ToolCall } from "./types.js";
|
||||
|
||||
// Create a singleton AJV instance with formats
|
||||
const ajv = new Ajv({ allErrors: true, strict: false });
|
||||
addFormats(ajv);
|
||||
|
||||
/**
|
||||
* Validates tool call arguments against the tool's Zod schema
|
||||
* @param tool The tool definition with Zod schema
|
||||
* Validates tool call arguments against the tool's TypeBox schema
|
||||
* @param tool The tool definition with TypeBox schema
|
||||
* @param toolCall The tool call from the LLM
|
||||
* @returns The validated arguments
|
||||
* @throws ZodError with formatted message if validation fails
|
||||
* @throws Error with formatted message if validation fails
|
||||
*/
|
||||
export function validateToolArguments(tool: Tool, toolCall: ToolCall): any {
|
||||
try {
|
||||
// Validate arguments with Zod schema
|
||||
return tool.parameters.parse(toolCall.arguments);
|
||||
} catch (e) {
|
||||
if (e instanceof z.ZodError) {
|
||||
// Format validation errors nicely
|
||||
const errors = e.issues
|
||||
.map((err) => {
|
||||
const path = err.path.length > 0 ? err.path.join(".") : "root";
|
||||
return ` - ${path}: ${err.message}`;
|
||||
})
|
||||
.join("\n");
|
||||
// Compile the schema
|
||||
const validate = ajv.compile(tool.parameters);
|
||||
|
||||
const errorMessage = `Validation failed for tool "${toolCall.name}":\n${errors}\n\nReceived arguments:\n${JSON.stringify(toolCall.arguments, null, 2)}`;
|
||||
|
||||
// Throw a new error with the formatted message
|
||||
throw new Error(errorMessage);
|
||||
}
|
||||
throw e;
|
||||
// Validate the arguments
|
||||
if (validate(toolCall.arguments)) {
|
||||
return toolCall.arguments;
|
||||
}
|
||||
|
||||
// Format validation errors nicely
|
||||
const errors =
|
||||
validate.errors
|
||||
?.map((err) => {
|
||||
const path = err.instancePath ? err.instancePath.substring(1) : err.params.missingProperty || "root";
|
||||
return ` - ${path}: ${err.message}`;
|
||||
})
|
||||
.join("\n") || "Unknown validation error";
|
||||
|
||||
const errorMessage = `Validation failed for tool "${toolCall.name}":\n${errors}\n\nReceived arguments:\n${JSON.stringify(toolCall.arguments, null, 2)}`;
|
||||
|
||||
throw new Error(errorMessage);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue