#pragma once
#include <string>
#include "tool.hpp"
class CalculatorTool : public Tool
{
public:
std::string name() const override
{
return "calculator";
}
std::string description() const override
{
return "Performs basic arithmetic operations.";
}
nlohmann::json parametersSchema() const override
{
return R"({
"type": "object",
"properties": {
"operation": {
"type": "string",
"enum": ["add", "subtract", "multiply", "divide"],
"description": "The operation to perform"
},
"a": {
"type": "number"
},
"b": {
"type": "number"
}
},
"required": ["operation", "a", "b"]
})"_json;
}
Task<mw::E<std::string>> execute(const nlohmann::json& arguments) override
{
if(!arguments.contains("operation") || !arguments.contains("a") ||
!arguments.contains("b"))
{
co_return std::unexpected(mw::runtimeError("Missing arguments"));
}
std::string op = arguments["operation"];
double a = arguments["a"];
double b = arguments["b"];
double result = 0.0;
if(op == "add")
{
result = a + b;
}
else if(op == "subtract")
{
result = a - b;
}
else if(op == "multiply")
{
result = a * b;
}
else if(op == "divide")
{
if(b == 0)
{
co_return std::unexpected(mw::runtimeError("Division by zero"));
}
result = a / b;
}
else
{
co_return std::unexpected(mw::runtimeError("Unknown operation"));
}
co_return std::to_string(result);
}
};