tiny_agent_rs/tools/
tool.rs1use std::collections::HashMap;
2
3pub trait Tool: Send + Sync + std::fmt::Debug {
5 fn name(&self) -> &'static str;
7
8 fn description(&self) -> &'static str;
10
11 fn parameters_schema(&self) -> serde_json::Value;
13
14 fn execute(
16 &self,
17 parameters: serde_json::Value,
18 ) -> std::pin::Pin<
19 Box<
20 dyn std::future::Future<Output = Result<serde_json::Value, crate::AgentError>>
21 + Send
22 + '_,
23 >,
24 >;
25}
26
27#[derive(Debug, Default)]
29pub struct ToolRegistry {
30 tools: HashMap<String, Box<dyn Tool>>,
31}
32
33impl ToolRegistry {
34 pub fn new() -> Self {
36 Self::default()
37 }
38
39 pub fn register<T: Tool + 'static>(&mut self, tool: T) {
41 self.tools.insert(tool.name().to_string(), Box::new(tool));
42 }
43
44 pub fn get(&self, name: &str) -> Option<&dyn Tool> {
46 self.tools.get(name).map(|tool| tool.as_ref())
47 }
48
49 pub fn list(&self) -> Vec<&dyn Tool> {
51 self.tools.values().map(|tool| tool.as_ref()).collect()
52 }
53
54 pub fn to_openai_tools(&self) -> Vec<serde_json::Value> {
56 self.tools
57 .values()
58 .map(|tool| {
59 serde_json::json!({
60 "type": "function",
61 "function": {
62 "name": tool.name(),
63 "description": tool.description(),
64 "parameters": tool.parameters_schema()
65 }
66 })
67 })
68 .collect()
69 }
70}