Files
sproutclaw/packages/ai/src/agent/tools/get-current-time.ts
Mario Zechner 35fe8f21e9 feat(ai): Implement Zod-based tool validation and improve Agent API
- Replace JSON Schema with Zod schemas for tool parameter definitions
- Add runtime validation for all tool calls at provider level
- Create shared validation module with detailed error formatting
- Update Agent API with comprehensive event system
- Add agent tests with calculator tool for multi-turn execution
- Add abort test to verify proper handling of aborted requests
- Update documentation with detailed event flow examples
- Rename generate.ts to stream.ts for clarity
2025-09-09 14:58:54 +02:00

42 lines
1.2 KiB
TypeScript

import { z } from "zod";
import type { AgentTool } from "../../agent";
import type { AgentToolResult } from "../types";
export interface GetCurrentTimeResult extends AgentToolResult<{ utcTimestamp: number }> {}
export async function getCurrentTime(timezone?: string): Promise<GetCurrentTimeResult> {
const date = new Date();
if (timezone) {
try {
return {
output: date.toLocaleString("en-US", {
timeZone: timezone,
dateStyle: "full",
timeStyle: "long",
}),
details: { utcTimestamp: date.getTime() },
};
} catch (e) {
throw new Error(`Invalid timezone: ${timezone}. Current UTC time: ${date.toISOString()}`);
}
}
return {
output: date.toLocaleString("en-US", { dateStyle: "full", timeStyle: "long" }),
details: { utcTimestamp: date.getTime() },
};
}
const getCurrentTimeSchema = z.object({
timezone: z.string().optional().describe("Optional timezone (e.g., 'America/New_York', 'Europe/London')"),
});
export const getCurrentTimeTool: AgentTool<typeof getCurrentTimeSchema, { utcTimestamp: number }> = {
label: "Current Time",
name: "get_current_time",
description: "Get the current date and time",
parameters: getCurrentTimeSchema,
execute: async (_toolCallId, args) => {
return getCurrentTime(args.timezone);
},
};