Skip to main content

vtcode_core/tools/registry/
inventory_facade.rs

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