use std::sync::Arc; use collections::{HashMap, HashSet, IndexMap}; use gpui::App; use parking_lot::Mutex; use crate::{Tool, ToolRegistry, ToolSource}; #[derive(Copy, Clone, PartialEq, Eq, Hash, Default)] pub struct ToolId(usize); /// A working set of tools for use in one instance of the Assistant Panel. #[derive(Default)] pub struct ToolWorkingSet { state: Mutex, } #[derive(Default)] struct WorkingSetState { context_server_tools_by_id: HashMap>, context_server_tools_by_name: HashMap>, disabled_tools_by_source: HashMap>>, is_scripting_tool_disabled: bool, next_tool_id: ToolId, } impl ToolWorkingSet { pub fn tool(&self, name: &str, cx: &App) -> Option> { self.state .lock() .context_server_tools_by_name .get(name) .cloned() .or_else(|| ToolRegistry::global(cx).tool(name)) } pub fn tools(&self, cx: &App) -> Vec> { let mut tools = ToolRegistry::global(cx).tools(); tools.extend( self.state .lock() .context_server_tools_by_id .values() .cloned(), ); tools } pub fn enabled_tools(&self, cx: &App) -> Vec> { let all_tools = self.tools(cx); all_tools .into_iter() .filter(|tool| self.is_enabled(&tool.source(), &tool.name().into())) .collect() } pub fn tools_by_source(&self, cx: &App) -> IndexMap>> { let mut tools_by_source = IndexMap::default(); for tool in self.tools(cx) { tools_by_source .entry(tool.source()) .or_insert_with(Vec::new) .push(tool); } for tools in tools_by_source.values_mut() { tools.sort_by_key(|tool| tool.name()); } tools_by_source.sort_unstable_keys(); tools_by_source } pub fn insert(&self, tool: Arc) -> ToolId { let mut state = self.state.lock(); let tool_id = state.next_tool_id; state.next_tool_id.0 += 1; state .context_server_tools_by_id .insert(tool_id, tool.clone()); state.tools_changed(); tool_id } pub fn is_enabled(&self, source: &ToolSource, name: &Arc) -> bool { !self.is_disabled(source, name) } pub fn is_disabled(&self, source: &ToolSource, name: &Arc) -> bool { let state = self.state.lock(); state .disabled_tools_by_source .get(source) .map_or(false, |disabled_tools| disabled_tools.contains(name)) } pub fn enable(&self, source: ToolSource, tools_to_enable: &[Arc]) { let mut state = self.state.lock(); state .disabled_tools_by_source .entry(source) .or_default() .retain(|name| !tools_to_enable.contains(name)); } pub fn disable(&self, source: ToolSource, tools_to_disable: &[Arc]) { let mut state = self.state.lock(); state .disabled_tools_by_source .entry(source) .or_default() .extend(tools_to_disable.into_iter().cloned()); } pub fn remove(&self, tool_ids_to_remove: &[ToolId]) { let mut state = self.state.lock(); state .context_server_tools_by_id .retain(|id, _| !tool_ids_to_remove.contains(id)); state.tools_changed(); } pub fn is_scripting_tool_enabled(&self) -> bool { let state = self.state.lock(); !state.is_scripting_tool_disabled } pub fn enable_scripting_tool(&self) { let mut state = self.state.lock(); state.is_scripting_tool_disabled = false; } pub fn disable_scripting_tool(&self) { let mut state = self.state.lock(); state.is_scripting_tool_disabled = true; } } impl WorkingSetState { fn tools_changed(&mut self) { self.context_server_tools_by_name.clear(); self.context_server_tools_by_name.extend( self.context_server_tools_by_id .values() .map(|tool| (tool.name(), tool.clone())), ); } }