- 在发送任务消息时增加可选的任务仓库参数支持子任务重发 - 新增 extract_parent_task_id 函数用于提取孙智能体的父任务 ID - 补发子任务(孙智能体)的 TaskStarted 事件,解决视图重进导致的 navigateToTaskId 丢失 - 判断并附加子任务的父任务 ID,完善日志记录与事件发送 - 在子智能体运行时根据深度排除 task 工具,防止无限嵌套调用 - ToolRegistry 新增 without 方法,可创建排除指定工具的新实例用于子智能体配置
97 lines
2.7 KiB
Rust
97 lines
2.7 KiB
Rust
use std::collections::HashMap;
|
|
use std::sync::{Arc, RwLock};
|
|
|
|
use crate::domain::tools::{Tool, ToolFunction};
|
|
|
|
use super::traits::Tool as ToolTrait;
|
|
|
|
pub struct ToolRegistry {
|
|
tools: RwLock<HashMap<String, Arc<dyn ToolTrait>>>,
|
|
}
|
|
|
|
impl ToolRegistry {
|
|
pub fn new() -> Self {
|
|
Self {
|
|
tools: RwLock::new(HashMap::new()),
|
|
}
|
|
}
|
|
|
|
pub fn register<T: ToolTrait + 'static>(&self, tool: T) {
|
|
self.tools
|
|
.write()
|
|
.expect("ToolRegistry lock poisoned")
|
|
.insert(tool.name().to_string(), Arc::new(tool));
|
|
}
|
|
|
|
pub fn get(&self, name: &str) -> Option<Arc<dyn ToolTrait>> {
|
|
self.tools
|
|
.read()
|
|
.expect("ToolRegistry lock poisoned")
|
|
.get(name)
|
|
.cloned()
|
|
}
|
|
|
|
/// Get all registered tools.
|
|
/// Used for concurrent tool execution when we need to look up tools by name.
|
|
pub fn get_all(&self) -> Vec<Arc<dyn ToolTrait>> {
|
|
self.tools
|
|
.read()
|
|
.expect("ToolRegistry lock poisoned")
|
|
.values()
|
|
.cloned()
|
|
.collect()
|
|
}
|
|
|
|
pub fn get_definitions(&self) -> Vec<Tool> {
|
|
self.tools
|
|
.read()
|
|
.expect("ToolRegistry lock poisoned")
|
|
.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
|
|
.read()
|
|
.expect("ToolRegistry lock poisoned")
|
|
.is_empty()
|
|
}
|
|
|
|
pub fn tool_names(&self) -> Vec<String> {
|
|
self.tools
|
|
.read()
|
|
.expect("ToolRegistry lock poisoned")
|
|
.keys()
|
|
.cloned()
|
|
.collect()
|
|
}
|
|
|
|
/// 创建一个排除指定工具的新 registry 副本
|
|
pub fn without(&self, exclude: &[&str]) -> Self {
|
|
let exclude_set: std::collections::HashSet<&str> = exclude.iter().copied().collect();
|
|
let tools = self.tools.read().expect("ToolRegistry lock poisoned");
|
|
let filtered: HashMap<String, Arc<dyn ToolTrait>> = tools
|
|
.iter()
|
|
.filter(|(name, _)| !exclude_set.contains(name.as_str()))
|
|
.map(|(k, v)| (k.clone(), v.clone()))
|
|
.collect();
|
|
let new_registry = ToolRegistry::new();
|
|
*new_registry.tools.write().expect("ToolRegistry lock poisoned") = filtered;
|
|
new_registry
|
|
}
|
|
}
|
|
|
|
impl Default for ToolRegistry {
|
|
fn default() -> Self {
|
|
Self::new()
|
|
}
|
|
}
|