summaryrefslogtreecommitdiff
path: root/personal_agent/tools/calculator.py
diff options
context:
space:
mode:
Diffstat (limited to 'personal_agent/tools/calculator.py')
-rw-r--r--personal_agent/tools/calculator.py62
1 files changed, 62 insertions, 0 deletions
diff --git a/personal_agent/tools/calculator.py b/personal_agent/tools/calculator.py
new file mode 100644
index 0000000..dee8b77
--- /dev/null
+++ b/personal_agent/tools/calculator.py
@@ -0,0 +1,62 @@
+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))