mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-18 15:03:02 +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,4 +1,4 @@
|
|||
import { z } from "zod";
|
||||
import { type Static, Type } from "@sinclair/typebox";
|
||||
import type { AgentTool } from "../../agent";
|
||||
|
||||
export interface CalculateResult {
|
||||
|
|
@ -15,10 +15,12 @@ export function calculate(expression: string): CalculateResult {
|
|||
}
|
||||
}
|
||||
|
||||
const calculateSchema = z.object({
|
||||
expression: z.string().describe("The mathematical expression to evaluate"),
|
||||
const calculateSchema = Type.Object({
|
||||
expression: Type.String({ description: "The mathematical expression to evaluate" }),
|
||||
});
|
||||
|
||||
type CalculateParams = Static<typeof calculateSchema>;
|
||||
|
||||
export const calculateTool: AgentTool<typeof calculateSchema, undefined> = {
|
||||
label: "Calculator",
|
||||
name: "calculate",
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import { z } from "zod";
|
||||
import { type Static, Type } from "@sinclair/typebox";
|
||||
import type { AgentTool } from "../../agent";
|
||||
import type { AgentToolResult } from "../types";
|
||||
|
||||
|
|
@ -26,10 +26,14 @@ export async function getCurrentTime(timezone?: string): Promise<GetCurrentTimeR
|
|||
};
|
||||
}
|
||||
|
||||
const getCurrentTimeSchema = z.object({
|
||||
timezone: z.string().optional().describe("Optional timezone (e.g., 'America/New_York', 'Europe/London')"),
|
||||
const getCurrentTimeSchema = Type.Object({
|
||||
timezone: Type.Optional(
|
||||
Type.String({ description: "Optional timezone (e.g., 'America/New_York', 'Europe/London')" }),
|
||||
),
|
||||
});
|
||||
|
||||
type GetCurrentTimeParams = Static<typeof getCurrentTimeSchema>;
|
||||
|
||||
export const getCurrentTimeTool: AgentTool<typeof getCurrentTimeSchema, { utcTimestamp: number }> = {
|
||||
label: "Current Time",
|
||||
name: "get_current_time",
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import type { ZodSchema, z } from "zod";
|
||||
import type { Static, TSchema } from "@sinclair/typebox";
|
||||
import type {
|
||||
AssistantMessage,
|
||||
AssistantMessageEvent,
|
||||
|
|
@ -17,12 +17,12 @@ export interface AgentToolResult<T> {
|
|||
}
|
||||
|
||||
// AgentTool extends Tool but adds the execute function
|
||||
export interface AgentTool<TParameters extends ZodSchema = ZodSchema, TDetails = any> extends Tool<TParameters> {
|
||||
export interface AgentTool<TParameters extends TSchema = TSchema, TDetails = any> extends Tool<TParameters> {
|
||||
// A human-readable label for the tool to be displayed in UI
|
||||
label: string;
|
||||
execute: (
|
||||
toolCallId: string,
|
||||
params: z.infer<TParameters>,
|
||||
params: Static<TParameters>,
|
||||
signal?: AbortSignal,
|
||||
) => Promise<AgentToolResult<TDetails>>;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
export { z } from "zod";
|
||||
export { type Static, Type } from "@sinclair/typebox";
|
||||
export * from "./agent/index.js";
|
||||
export * from "./models.js";
|
||||
export * from "./providers/anthropic.js";
|
||||
|
|
@ -6,4 +6,5 @@ export * from "./providers/google.js";
|
|||
export * from "./providers/openai-completions.js";
|
||||
export * from "./providers/openai-responses.js";
|
||||
export * from "./stream.js";
|
||||
export * from "./typebox-helpers.js";
|
||||
export * from "./types.js";
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@ import type {
|
|||
MessageCreateParamsStreaming,
|
||||
MessageParam,
|
||||
} from "@anthropic-ai/sdk/resources/messages.js";
|
||||
import { zodToJsonSchema } from "zod-to-json-schema";
|
||||
import { AssistantMessageEventStream } from "../event-stream.js";
|
||||
import { calculateCost } from "../models.js";
|
||||
import type {
|
||||
|
|
@ -442,7 +441,7 @@ function convertTools(tools: Tool[]): Anthropic.Messages.Tool[] {
|
|||
if (!tools) return [];
|
||||
|
||||
return tools.map((tool) => {
|
||||
const jsonSchema = zodToJsonSchema(tool.parameters, { $refStrategy: "none" }) as any;
|
||||
const jsonSchema = tool.parameters as any; // TypeBox already generates JSON Schema
|
||||
|
||||
return {
|
||||
name: tool.name,
|
||||
|
|
|
|||
|
|
@ -7,7 +7,6 @@ import {
|
|||
GoogleGenAI,
|
||||
type Part,
|
||||
} from "@google/genai";
|
||||
import { zodToJsonSchema } from "zod-to-json-schema";
|
||||
import { AssistantMessageEventStream } from "../event-stream.js";
|
||||
import { calculateCost } from "../models.js";
|
||||
import type {
|
||||
|
|
@ -394,7 +393,7 @@ function convertTools(tools: Tool[]): any[] | undefined {
|
|||
functionDeclarations: tools.map((tool) => ({
|
||||
name: tool.name,
|
||||
description: tool.description,
|
||||
parameters: zodToJsonSchema(tool.parameters, { $refStrategy: "none" }),
|
||||
parameters: tool.parameters as any, // TypeBox already generates JSON Schema
|
||||
})),
|
||||
},
|
||||
];
|
||||
|
|
|
|||
|
|
@ -7,7 +7,6 @@ import type {
|
|||
ChatCompletionContentPartText,
|
||||
ChatCompletionMessageParam,
|
||||
} from "openai/resources/chat/completions.js";
|
||||
import { zodToJsonSchema } from "zod-to-json-schema";
|
||||
import { AssistantMessageEventStream } from "../event-stream.js";
|
||||
import { calculateCost } from "../models.js";
|
||||
import type {
|
||||
|
|
@ -396,7 +395,7 @@ function convertTools(tools: Tool[]): OpenAI.Chat.Completions.ChatCompletionTool
|
|||
function: {
|
||||
name: tool.name,
|
||||
description: tool.description,
|
||||
parameters: zodToJsonSchema(tool.parameters, { $refStrategy: "none" }),
|
||||
parameters: tool.parameters as any, // TypeBox already generates JSON Schema
|
||||
},
|
||||
}));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,7 +10,6 @@ import type {
|
|||
ResponseOutputMessage,
|
||||
ResponseReasoningItem,
|
||||
} from "openai/resources/responses/responses.js";
|
||||
import { zodToJsonSchema } from "zod-to-json-schema";
|
||||
import { AssistantMessageEventStream } from "../event-stream.js";
|
||||
import { calculateCost } from "../models.js";
|
||||
import type {
|
||||
|
|
@ -462,7 +461,7 @@ function convertTools(tools: Tool[]): OpenAITool[] {
|
|||
type: "function",
|
||||
name: tool.name,
|
||||
description: tool.description,
|
||||
parameters: zodToJsonSchema(tool.parameters, { $refStrategy: "none" }),
|
||||
parameters: tool.parameters as any, // TypeBox already generates JSON Schema
|
||||
strict: null,
|
||||
}));
|
||||
}
|
||||
|
|
|
|||
24
packages/ai/src/typebox-helpers.ts
Normal file
24
packages/ai/src/typebox-helpers.ts
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
import { type TUnsafe, Type } from "@sinclair/typebox";
|
||||
|
||||
/**
|
||||
* Creates a string enum schema compatible with Google's API and other providers
|
||||
* that don't support anyOf/const patterns.
|
||||
*
|
||||
* @example
|
||||
* const OperationSchema = StringEnum(["add", "subtract", "multiply", "divide"], {
|
||||
* description: "The operation to perform"
|
||||
* });
|
||||
*
|
||||
* type Operation = Static<typeof OperationSchema>; // "add" | "subtract" | "multiply" | "divide"
|
||||
*/
|
||||
export function StringEnum<T extends readonly string[]>(
|
||||
values: T,
|
||||
options?: { description?: string; default?: T[number] },
|
||||
): TUnsafe<T[number]> {
|
||||
return Type.Unsafe<T[number]>({
|
||||
type: "string",
|
||||
enum: values as any,
|
||||
...(options?.description && { description: options.description }),
|
||||
...(options?.default && { default: options.default }),
|
||||
});
|
||||
}
|
||||
|
|
@ -119,9 +119,9 @@ export interface ToolResultMessage<TDetails = any> {
|
|||
|
||||
export type Message = UserMessage | AssistantMessage | ToolResultMessage;
|
||||
|
||||
import type { ZodSchema } from "zod";
|
||||
import type { TSchema } from "@sinclair/typebox";
|
||||
|
||||
export interface Tool<TParameters extends ZodSchema = ZodSchema> {
|
||||
export interface Tool<TParameters extends TSchema = TSchema> {
|
||||
name: string;
|
||||
description: string;
|
||||
parameters: TParameters;
|
||||
|
|
|
|||
|
|
@ -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