use std::collections::HashMap;
pub trait Tool: Send + Sync + std::fmt::Debug {
fn name(&self) -> &'static str;
fn description(&self) -> &'static str;
fn parameters_schema(&self) -> serde_json::Value;
fn execute(
&self,
parameters: serde_json::Value,
) -> std::pin::Pin<
Box<
dyn std::future::Future<Output = Result<serde_json::Value, crate::AgentError>>
+ Send
+ '_,
>,
>;
}
#[derive(Debug, Default)]
pub struct ToolRegistry {
tools: HashMap<String, Box<dyn Tool>>,
}
impl ToolRegistry {
pub fn new() -> Self {
Self::default()
}
pub fn register<T: Tool + 'static>(&mut self, tool: T) {
self.tools.insert(tool.name().to_string(), Box::new(tool));
}
pub fn get(&self, name: &str) -> Option<&dyn Tool> {
self.tools.get(name).map(|tool| tool.as_ref())
}
pub fn list(&self) -> Vec<&dyn Tool> {
self.tools.values().map(|tool| tool.as_ref()).collect()
}
pub fn to_openai_tools(&self) -> Vec<serde_json::Value> {
self.tools
.values()
.map(|tool| {
serde_json::json!({
"type": "function",
"function": {
"name": tool.name(),
"description": tool.description(),
"parameters": tool.parameters_schema()
}
})
})
.collect()
}
}