PicoBot/src/tools/registry.rs
xiaoxixi 9834bd75cf feat: add calculator tool and integrate with agent loop
- Introduced a new CalculatorTool for performing various arithmetic and statistical calculations.
- Enhanced the AgentLoop to support tool execution, including handling tool calls in the process flow.
- Updated ChatMessage structure to include optional fields for tool call identification and names.
- Modified the Session and SessionManager to manage tool registrations and pass them to agents.
- Updated the OpenAIProvider to serialize tool-related message fields.
- Added a ToolRegistry for managing multiple tools and their definitions.
- Implemented tests for the CalculatorTool to ensure functionality and correctness.
2026-04-06 23:43:45 +08:00

54 lines
1.2 KiB
Rust

use std::collections::HashMap;
use crate::providers::{Tool, ToolFunction};
use super::traits::Tool as ToolTrait;
pub struct ToolRegistry {
tools: HashMap<String, Box<dyn ToolTrait>>,
}
impl ToolRegistry {
pub fn new() -> Self {
Self {
tools: HashMap::new(),
}
}
pub fn register<T: ToolTrait + 'static>(&mut self, tool: T) {
self.tools.insert(tool.name().to_string(), Box::new(tool));
}
pub fn get(&self, name: &str) -> Option<&Box<dyn ToolTrait>> {
self.tools.get(name)
}
pub fn get_definitions(&self) -> Vec<Tool> {
self.tools
.values()
.map(|tool| Tool {
tool_type: "function".to_string(),
function: ToolFunction {
name: tool.name().to_string(),
description: tool.description().to_string(),
parameters: tool.parameters_schema(),
},
})
.collect()
}
pub fn has_tools(&self) -> bool {
!self.tools.is_empty()
}
pub fn tool_names(&self) -> Vec<String> {
self.tools.keys().cloned().collect()
}
}
impl Default for ToolRegistry {
fn default() -> Self {
Self::new()
}
}