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:
Mario Zechner 2025-09-16 01:10:40 +02:00
parent f5ac1ef521
commit e8370436d7
16 changed files with 196 additions and 121 deletions

View file

@ -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",

View file

@ -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",

View file

@ -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>>;
}