Skip to main content

limit_agent/
tool.rs

1use crate::error::AgentError;
2use async_trait::async_trait;
3use serde_json::Value;
4
5#[async_trait]
6pub trait Tool: Send + Sync {
7    fn name(&self) -> &str;
8
9    async fn execute(&self, args: Value) -> Result<Value, AgentError>;
10}
11
12/// Example tool that echoes back its input arguments
13pub struct EchoTool;
14
15impl EchoTool {
16    pub fn new() -> Self {
17        EchoTool
18    }
19}
20
21impl Default for EchoTool {
22    fn default() -> Self {
23        Self::new()
24    }
25}
26
27#[async_trait]
28impl Tool for EchoTool {
29    fn name(&self) -> &str {
30        "echo"
31    }
32
33    async fn execute(&self, args: Value) -> Result<Value, AgentError> {
34        // Echo back the input arguments
35        Ok(args)
36    }
37}
38
39#[cfg(test)]
40mod tests {
41    use super::*;
42
43    #[tokio::test]
44    async fn test_echo_tool_name() {
45        let tool = EchoTool::new();
46        assert_eq!(tool.name(), "echo");
47    }
48
49    #[tokio::test]
50    async fn test_echo_tool_execute() {
51        let tool = EchoTool::new();
52        let input = serde_json::json!({
53            "message": "hello",
54            "count": 42
55        });
56
57        let result = tool.execute(input.clone()).await.unwrap();
58        assert_eq!(result, input);
59    }
60
61    #[tokio::test]
62    async fn test_echo_tool_default() {
63        let tool = EchoTool;
64        let input = serde_json::json!("test");
65
66        let result = tool.execute(input.clone()).await.unwrap();
67        assert_eq!(result, input);
68    }
69}