refactor: 清理未使用代码并添加未来功能标记
Some checks failed
CI / Rust Check (push) Has been cancelled
CI / Lint & TypeCheck (push) Has been cancelled
CI / Unit Tests (push) Has been cancelled
CI / Build Frontend (push) Has been cancelled
CI / Security Scan (push) Has been cancelled
CI / E2E Tests (push) Has been cancelled
Some checks failed
CI / Rust Check (push) Has been cancelled
CI / Lint & TypeCheck (push) Has been cancelled
CI / Unit Tests (push) Has been cancelled
CI / Build Frontend (push) Has been cancelled
CI / Security Scan (push) Has been cancelled
CI / E2E Tests (push) Has been cancelled
style: 统一代码格式和注释风格 docs: 更新多个功能文档的完整度和状态 feat(runtime): 添加路径验证工具支持 fix(pipeline): 改进条件判断和变量解析逻辑 test(types): 为ID类型添加全面测试用例 chore: 更新依赖项和Cargo.lock文件 perf(mcp): 优化MCP协议传输和错误处理
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
//! Tool system for agent capabilities
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
use async_trait::async_trait;
|
||||
use serde_json::Value;
|
||||
use zclaw_types::{AgentId, Result};
|
||||
|
||||
use crate::driver::ToolDefinition;
|
||||
use crate::tool::builtin::PathValidator;
|
||||
|
||||
/// Tool trait for implementing agent tools
|
||||
#[async_trait]
|
||||
@@ -43,6 +45,8 @@ pub struct ToolContext {
|
||||
pub working_directory: Option<String>,
|
||||
pub session_id: Option<String>,
|
||||
pub skill_executor: Option<Arc<dyn SkillExecutor>>,
|
||||
/// Path validator for file system operations
|
||||
pub path_validator: Option<PathValidator>,
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for ToolContext {
|
||||
@@ -52,6 +56,7 @@ impl std::fmt::Debug for ToolContext {
|
||||
.field("working_directory", &self.working_directory)
|
||||
.field("session_id", &self.session_id)
|
||||
.field("skill_executor", &self.skill_executor.as_ref().map(|_| "SkillExecutor"))
|
||||
.field("path_validator", &self.path_validator.as_ref().map(|_| "PathValidator"))
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
@@ -63,41 +68,78 @@ impl Clone for ToolContext {
|
||||
working_directory: self.working_directory.clone(),
|
||||
session_id: self.session_id.clone(),
|
||||
skill_executor: self.skill_executor.clone(),
|
||||
path_validator: self.path_validator.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Tool registry for managing available tools
|
||||
/// Uses HashMap for O(1) lookup performance
|
||||
#[derive(Clone)]
|
||||
pub struct ToolRegistry {
|
||||
tools: Vec<Arc<dyn Tool>>,
|
||||
/// Tool lookup by name (O(1))
|
||||
tools: HashMap<String, Arc<dyn Tool>>,
|
||||
/// Registration order for consistent iteration
|
||||
tool_order: Vec<String>,
|
||||
}
|
||||
|
||||
impl ToolRegistry {
|
||||
pub fn new() -> Self {
|
||||
Self { tools: Vec::new() }
|
||||
Self {
|
||||
tools: HashMap::new(),
|
||||
tool_order: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn register(&mut self, tool: Box<dyn Tool>) {
|
||||
self.tools.push(Arc::from(tool));
|
||||
let tool: Arc<dyn Tool> = Arc::from(tool);
|
||||
let name = tool.name().to_string();
|
||||
|
||||
// Track order for new tools
|
||||
if !self.tools.contains_key(&name) {
|
||||
self.tool_order.push(name.clone());
|
||||
}
|
||||
|
||||
self.tools.insert(name, tool);
|
||||
}
|
||||
|
||||
/// Get tool by name - O(1) lookup
|
||||
pub fn get(&self, name: &str) -> Option<Arc<dyn Tool>> {
|
||||
self.tools.iter().find(|t| t.name() == name).cloned()
|
||||
self.tools.get(name).cloned()
|
||||
}
|
||||
|
||||
/// List all tools in registration order
|
||||
pub fn list(&self) -> Vec<&dyn Tool> {
|
||||
self.tools.iter().map(|t| t.as_ref()).collect()
|
||||
self.tool_order
|
||||
.iter()
|
||||
.filter_map(|name| self.tools.get(name).map(|t| t.as_ref()))
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Get tool definitions in registration order
|
||||
pub fn definitions(&self) -> Vec<ToolDefinition> {
|
||||
self.tools.iter().map(|t| {
|
||||
ToolDefinition::new(
|
||||
t.name(),
|
||||
t.description(),
|
||||
t.input_schema(),
|
||||
)
|
||||
}).collect()
|
||||
self.tool_order
|
||||
.iter()
|
||||
.filter_map(|name| {
|
||||
self.tools.get(name).map(|t| {
|
||||
ToolDefinition::new(
|
||||
t.name(),
|
||||
t.description(),
|
||||
t.input_schema(),
|
||||
)
|
||||
})
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Get number of registered tools
|
||||
pub fn len(&self) -> usize {
|
||||
self.tools.len()
|
||||
}
|
||||
|
||||
/// Check if registry is empty
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.tools.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user