from personal_agent.tools.base import Tool, ToolError, ToolResult class CalculatorTool(Tool): @property def name(self) -> str: return "calculator" @property def description(self) -> str: return "Perform basic arithemtic" @property def schema(self) -> dict[str, object]: return { "type": "object", "properties": { "a": { "type": "number", "description": "The first number.", }, "b": { "type": "number", "description": "The second number.", }, "operation": { "type": "string", "enum": ["add", "subtract", "multiply", "divide"], "description": "The arithemtic operation to perform", }, }, "required": ["a", "b", "operation"], } def execute(self, arguments: dict[str, object]) -> ToolResult: a = arguments["a"] b = arguments["b"] operation = arguments["operation"] if not isinstance(a, (int, float)): raise ToolError("a must be a number") if not isinstance(b, (int, float)): raise ToolError("b must be a number") if not isinstance(operation, str): raise ToolError("operation must be a string") if operation == "add": result = a + b elif operation == "subtract": result = a - b elif operation == "multiply": result = a * b elif operation == "divide": if b == 0: raise ToolError("cannot divide by zero") result = a / b else: raise ToolError(f"unknown operation: {operation}") return ToolResult(content=str(result))