feat(core): Implement ToolRegistry

This commit is contained in:
2025-12-26 19:18:30 +01:00
parent 4f0b91adda
commit 1d7c584b55
2 changed files with 57 additions and 0 deletions

View File

@@ -3,6 +3,7 @@ mod messages;
mod engine;
mod state;
mod agent_manager;
mod tool_registry;
use clap::{Parser, ValueEnum};
use color_eyre::eyre::{Result, eyre};

View File

@@ -0,0 +1,56 @@
use std::collections::HashMap;
use serde_json::Value;
use llm_core::Tool;
/// Registry for tools available to the agent
pub struct ToolRegistry {
tools: HashMap<String, Tool>,
}
impl ToolRegistry {
pub fn new() -> Self {
Self {
tools: HashMap::new(),
}
}
/// Register a tool
pub fn register(&mut self, tool: Tool) {
self.tools.insert(tool.function.name.clone(), tool);
}
/// Get a tool by name
pub fn get(&self, name: &str) -> Option<&Tool> {
self.tools.get(name)
}
/// Get all registered tools as a list (for LLM context)
pub fn list_tools(&self) -> Vec<Tool> {
self.tools.values().cloned().collect()
}
}
#[cfg(test)]
mod tests {
use super::*;
use llm_core::{ToolParameters, ToolFunction};
fn create_mock_tool(name: &str) -> Tool {
Tool::function(
name,
"Description",
ToolParameters::object(serde_json::json!({}), vec![])
)
}
#[test]
fn test_registry() {
let mut registry = ToolRegistry::new();
let tool = create_mock_tool("test_tool");
registry.register(tool.clone());
assert!(registry.get("test_tool").is_some());
assert_eq!(registry.list_tools().len(), 1);
}
}