use std::collections::HashMap;
use wesichain_core::{Tool, ToolError, ToolSpec, Value};
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 async fn call(&self, name: &str, input: Value) -> Result<Value, ToolError> {
let tool = self
.tools
.get(name)
.ok_or_else(|| ToolError::ExecutionFailed("not found".to_string()))?;
tool.invoke(input).await
}
pub fn to_specs(&self) -> Vec<ToolSpec> {
let mut specs: Vec<ToolSpec> = self
.tools
.values()
.map(|tool| ToolSpec {
name: tool.name().to_string(),
description: tool.description().to_string(),
parameters: tool.schema(),
})
.collect();
specs.sort_by(|a, b| a.name.cmp(&b.name));
specs
}
}
impl Default for ToolRegistry {
fn default() -> Self {
Self::new()
}
}