get-current-time.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { type Static, Type } from "typebox";
  2. import type { AgentTool, AgentToolResult } from "../../src/types.ts";
  3. export interface GetCurrentTimeResult extends AgentToolResult<{ utcTimestamp: number }> {}
  4. export async function getCurrentTime(timezone?: string): Promise<GetCurrentTimeResult> {
  5. const date = new Date();
  6. if (timezone) {
  7. try {
  8. const timeStr = date.toLocaleString("en-US", {
  9. timeZone: timezone,
  10. dateStyle: "full",
  11. timeStyle: "long",
  12. });
  13. return {
  14. content: [{ type: "text", text: timeStr }],
  15. details: { utcTimestamp: date.getTime() },
  16. };
  17. } catch (_e) {
  18. throw new Error(`Invalid timezone: ${timezone}. Current UTC time: ${date.toISOString()}`);
  19. }
  20. }
  21. const timeStr = date.toLocaleString("en-US", { dateStyle: "full", timeStyle: "long" });
  22. return {
  23. content: [{ type: "text", text: timeStr }],
  24. details: { utcTimestamp: date.getTime() },
  25. };
  26. }
  27. const getCurrentTimeSchema = Type.Object({
  28. timezone: Type.Optional(
  29. Type.String({ description: "Optional timezone (e.g., 'America/New_York', 'Europe/London')" }),
  30. ),
  31. });
  32. type GetCurrentTimeParams = Static<typeof getCurrentTimeSchema>;
  33. export const getCurrentTimeTool: AgentTool<typeof getCurrentTimeSchema, { utcTimestamp: number }> = {
  34. label: "Current Time",
  35. name: "get_current_time",
  36. description: "Get the current date and time",
  37. parameters: getCurrentTimeSchema,
  38. execute: async (_toolCallId: string, args: GetCurrentTimeParams) => {
  39. return getCurrentTime(args.timezone);
  40. },
  41. };