use serde_json::Value;
use std::collections::HashMap;
use std::future::Future;
use std::pin::Pin;
pub trait Tool: Send + Sync {
fn name(&self) -> &str;
fn description(&self) -> &str;
fn input_schema(&self) -> Value;
fn execute<'a>(
&'a self,
input: Value,
) -> Pin<Box<dyn Future<Output = Result<Value, String>> + Send + 'a>>;
}
pub struct ToolRegistry {
tools: HashMap<String, Box<dyn Tool>>,
}
impl ToolRegistry {
pub fn new() -> Self {
Self {
tools: HashMap::new(),
}
}
pub fn register(&mut self, tool: Box<dyn Tool>) {
self.tools.insert(tool.name().to_string(), tool);
}
pub fn get(&self, name: &str) -> Option<&dyn Tool> {
self.tools.get(name).map(|b| b.as_ref())
}
pub fn as_anthropic_tools(&self) -> Vec<anyllm_translate::anthropic::Tool> {
self.tools
.values()
.map(|t| anyllm_translate::anthropic::Tool {
name: t.name().to_string(),
description: Some(t.description().to_string()),
input_schema: t.input_schema(),
})
.collect()
}
pub fn contains(&self, name: &str) -> bool {
self.tools.contains_key(name)
}
pub fn list_names(&self) -> Vec<&str> {
self.tools.keys().map(|s| s.as_str()).collect()
}
}
impl Default for ToolRegistry {
fn default() -> Self {
Self::new()
}
}