Skip to main content

vtcode_core/tools/registry/
registration_facade.rs

1//! Registration-related ToolRegistry helpers.
2
3use anyhow::Result;
4
5use super::{ToolRegistration, ToolRegistry};
6
7impl ToolRegistry {
8    /// Register a new tool with the registry.
9    ///
10    /// # Arguments
11    /// * `registration` - The tool registration to add
12    ///
13    /// # Returns
14    /// `Result<()>` indicating success or an error if the tool is already registered
15    pub async fn register_tool(&self, registration: ToolRegistration) -> Result<()> {
16        let registration = if let Some(mode) = self.current_cgp_mode() {
17            if registration.is_cgp_wrapped() {
18                registration
19            } else if let Some(handler) = self.cgp_handler_for_registration(&registration, mode) {
20                registration.with_handler(handler).with_cgp_wrapped(true)
21            } else {
22                registration
23            }
24        } else {
25            registration
26        };
27        self.inventory.register_tool(registration)?;
28        // Invalidate cache
29        if let Ok(mut cache) = self.cached_available_tools.write() {
30            *cache = None;
31        }
32        self.rebuild_tool_assembly().await;
33        self.tool_catalog_state
34            .note_explicit_refresh("tool_registration");
35        self.sync_policy_catalog().await;
36        Ok(())
37    }
38
39    /// Unregister a tool from the registry.
40    pub async fn unregister_tool(&self, name: &str) -> Result<bool> {
41        let removed = self.inventory.remove_tool(name)?.is_some();
42        if removed {
43            if let Ok(mut cache) = self.cached_available_tools.write() {
44                *cache = None;
45            }
46            self.rebuild_tool_assembly().await;
47            self.tool_catalog_state
48                .note_explicit_refresh("tool_unregistration");
49            self.sync_policy_catalog().await;
50        }
51        Ok(removed)
52    }
53}