An AI agent built to do Ralph loops - plan mode for planning and ralph mode for implementing.
1use anyhow::Result;
2use async_trait::async_trait;
3use std::collections::HashMap;
4use std::sync::{Arc, RwLock};
5
6use crate::llm::ToolDefinition;
7
8/// Trait for tools that can be used by the agent
9#[async_trait]
10pub trait Tool: Send + Sync {
11 /// Returns the name of the tool
12 fn name(&self) -> &str;
13
14 /// Returns a description of what the tool does
15 fn description(&self) -> &str;
16
17 /// Returns the JSON schema for the tool's parameters
18 fn parameters(&self) -> serde_json::Value;
19
20 /// Executes the tool with the given parameters
21 async fn execute(&self, params: serde_json::Value) -> Result<String>;
22}
23
24/// Registry for managing available tools
25pub struct ToolRegistry {
26 tools: Arc<RwLock<HashMap<String, Arc<dyn Tool>>>>,
27}
28
29impl ToolRegistry {
30 /// Creates a new empty tool registry
31 pub fn new() -> Self {
32 Self {
33 tools: Arc::new(RwLock::new(HashMap::new())),
34 }
35 }
36
37 /// Registers a new tool in the registry
38 pub fn register(&self, tool: Arc<dyn Tool>) {
39 let mut tools = self.tools.write().unwrap();
40 tools.insert(tool.name().to_string(), tool);
41 }
42
43 /// Gets a tool by name
44 pub fn get(&self, name: &str) -> Option<Arc<dyn Tool>> {
45 let tools = self.tools.read().unwrap();
46 tools.get(name).cloned()
47 }
48
49 /// Lists all registered tool names
50 pub fn list(&self) -> Vec<String> {
51 let tools = self.tools.read().unwrap();
52 tools.keys().cloned().collect()
53 }
54
55 /// Converts all registered tools to LLM tool definitions
56 pub fn definitions(&self) -> Vec<ToolDefinition> {
57 let tools = self.tools.read().unwrap();
58 tools
59 .values()
60 .map(|tool| ToolDefinition {
61 name: tool.name().to_string(),
62 description: tool.description().to_string(),
63 parameters: tool.parameters(),
64 })
65 .collect()
66 }
67}
68
69impl Clone for ToolRegistry {
70 fn clone(&self) -> Self {
71 Self {
72 tools: Arc::clone(&self.tools),
73 }
74 }
75}
76
77impl Default for ToolRegistry {
78 fn default() -> Self {
79 Self::new()
80 }
81}
82
83pub mod factory;
84pub mod file;
85pub mod permission_check;
86pub mod shell;
87pub mod signal;