calculate.ts 1.0 KB

1234567891011121314151617181920212223242526272829303132
  1. import { type Static, Type } from "typebox";
  2. import type { AgentTool, AgentToolResult } from "../../src/types.ts";
  3. export interface CalculateResult extends AgentToolResult<undefined> {
  4. content: Array<{ type: "text"; text: string }>;
  5. details: undefined;
  6. }
  7. export function calculate(expression: string): CalculateResult {
  8. try {
  9. const result = new Function(`return ${expression}`)();
  10. return { content: [{ type: "text", text: `${expression} = ${result}` }], details: undefined };
  11. } catch (e: any) {
  12. throw new Error(e.message || String(e));
  13. }
  14. }
  15. const calculateSchema = Type.Object({
  16. expression: Type.String({ description: "The mathematical expression to evaluate" }),
  17. });
  18. type CalculateParams = Static<typeof calculateSchema>;
  19. export const calculateTool: AgentTool<typeof calculateSchema, undefined> = {
  20. label: "Calculator",
  21. name: "calculate",
  22. description: "Evaluate mathematical expressions",
  23. parameters: calculateSchema,
  24. execute: async (_toolCallId: string, args: CalculateParams) => {
  25. return calculate(args.expression);
  26. },
  27. };