use serde_json::Value;
use std::collections::HashMap;
pub trait OllamaTool: Send + Sync {
fn name(&self) -> &'static str;
fn tool_definition(&self) -> Value;
fn execute_from_json(&self, args: Value) -> Result<String, Box<dyn std::error::Error + Send + Sync>>;
}
pub struct ToolRegistry {
tools: HashMap<String, Box<dyn OllamaTool>>,
}
impl Default for ToolRegistry {
fn default() -> Self {
Self::new()
}
}
impl ToolRegistry {
pub fn new() -> Self {
Self {
tools: HashMap::new(),
}
}
pub fn register<T: OllamaTool + 'static>(&mut self, tool: T) {
self.tools.insert(tool.name().to_string(), Box::new(tool));
}
pub fn get_definitions(&self) -> Vec<Value> {
self.tools.values().map(|t| t.tool_definition()).collect()
}
pub fn execute(&self, name: &str, args: Value) -> Result<String, String> {
if let Some(tool) = self.tools.get(name) {
tool.execute_from_json(args).map_err(|e| e.to_string())
} else {
Err(format!("Tool not found: {}", name))
}
}
}