restflow_core/tools/
math_tools.rs

1use anyhow::Result;
2use rig::completion::ToolDefinition;
3use rig::tool::Tool;
4use serde::{Deserialize, Serialize};
5use serde_json::json;
6use tracing::debug;
7
8#[derive(Deserialize)]
9pub struct AddArgs {
10    pub x: i32,
11    pub y: i32,
12}
13
14#[derive(Debug, thiserror::Error)]
15#[error("Math error")]
16pub struct MathError;
17
18#[derive(Deserialize, Serialize)]
19pub struct AddTool;
20
21impl Tool for AddTool {
22    const NAME: &'static str = "add";
23    type Error = MathError;
24    type Args = AddArgs;
25    type Output = i32;
26
27    async fn definition(&self, _prompt: String) -> ToolDefinition {
28        ToolDefinition {
29            name: Self::NAME.to_string(),
30            description: "Add two numbers together".to_string(),
31            parameters: json!({
32                "type": "object",
33                "properties": {
34                    "x": {
35                        "type": "number",
36                        "description": "The first number to add"
37                    },
38                    "y": {
39                        "type": "number",
40                        "description": "The second number to add"
41                    }
42                },
43                "required": ["x", "y"]
44            }),
45        }
46    }
47
48    async fn call(&self, args: Self::Args) -> Result<Self::Output, Self::Error> {
49        let result = args.x + args.y;
50        debug!(x = args.x, y = args.y, result, "AddTool executed");
51        Ok(result)
52    }
53}