Skip to main content

vtcode_core/tools/registry/
inventory_facade.rs

1//! Inventory-related accessors for ToolRegistry.
2
3use std::path::PathBuf;
4use std::sync::Arc;
5
6use crate::tools::edited_file_monitor::EditedFileMonitor;
7use crate::tools::file_ops::FileOpsTool;
8use crate::tools::grep_file::GrepSearchManager;
9
10use super::{Tool, ToolHandler, ToolRegistry};
11
12impl ToolRegistry {
13    /// Get a tool by name from the inventory (with hot cache optimization).
14    pub fn get_tool(&self, name: &str) -> Option<Arc<dyn Tool>> {
15        // Check hot cache first if optimizations are enabled
16        if self.optimization_config.tool_registry.use_optimized_registry {
17            // Use a separate read and write operation to avoid borrow checker issues
18            {
19                let cache = self.hot_tool_cache.read();
20                if let Some(cached_tool) = cache.peek(name) {
21                    return Some(cached_tool.clone());
22                }
23            }
24        }
25
26        // Fallback to inventory lookup
27        let tool = self.inventory.get_registration(name).and_then(|reg| match reg.handler() {
28            ToolHandler::TraitObject(tool) => Some(tool.clone()),
29            _ => None,
30        });
31
32        // Cache the result if optimizations are enabled and tool was found
33        if let Some(ref tool_arc) = tool
34            && self.optimization_config.tool_registry.use_optimized_registry
35        {
36            self.hot_tool_cache.write().put(name.to_string(), tool_arc.clone());
37        }
38
39        tool
40    }
41    pub fn workspace_root(&self) -> &PathBuf {
42        self.inventory.workspace_root()
43    }
44
45    /// Get the workspace root as an owned PathBuf.
46    pub fn workspace_root_owned(&self) -> PathBuf {
47        self.inventory.workspace_root().clone()
48    }
49
50    pub fn file_ops_tool(&self) -> &FileOpsTool {
51        self.inventory.file_ops_tool()
52    }
53
54    /// Borrow the edited-file monitor without exposing shared ownership.
55    pub fn edited_file_monitor_ref(&self) -> &EditedFileMonitor {
56        self.edited_file_monitor.as_ref()
57    }
58
59    /// Get the shared edited-file monitor handle for callers that need to clone it.
60    pub fn edited_file_monitor(&self) -> &Arc<EditedFileMonitor> {
61        &self.edited_file_monitor
62    }
63
64    pub fn grep_file_manager(&self) -> Arc<GrepSearchManager> {
65        self.inventory.grep_file_manager()
66    }
67}