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    /// Resolve trusted network metadata through the same alias inventory as execution.
9    pub fn tool_network_access(&self, name: &str) -> super::ToolNetworkAccess {
10        self.inventory
11            .registration_for(name)
12            .map(|registration| registration.metadata().network_access())
13            .unwrap_or_default()
14    }
15    /// Register or replace a tool with the registry.
16    ///
17    /// # Arguments
18    /// * `registration` - The tool registration to add
19    ///
20    /// # Returns
21    /// `Result<()>` indicating success. A duplicate canonical tool name replaces
22    /// the previous registration in place; alias conflicts remain errors.
23    pub async fn register_tool(&self, registration: ToolRegistration) -> Result<()> {
24        let registration = if let Some(mode) = self.current_cgp_mode() {
25            if registration.is_cgp_wrapped() {
26                registration
27            } else if let Some(handler) = self.cgp_handler_for_registration(&registration, mode) {
28                registration.with_handler(handler).with_cgp_wrapped(true)
29            } else {
30                registration
31            }
32        } else {
33            registration
34        };
35        self.inventory.register_tool(registration)?;
36        // Invalidate cache
37        *self.cached_available_tools.write() = None;
38        self.rebuild_tool_assembly().await;
39        self.tool_catalog_state.note_explicit_refresh("tool_registration");
40        self.sync_policy_catalog().await;
41        Ok(())
42    }
43
44    /// Unregister a tool from the registry.
45    pub async fn unregister_tool(&self, name: &str) -> Result<bool> {
46        let removed = self.inventory.remove_tool(name)?.is_some();
47        if removed {
48            *self.cached_available_tools.write() = None;
49            self.rebuild_tool_assembly().await;
50            self.tool_catalog_state.note_explicit_refresh("tool_unregistration");
51            self.sync_policy_catalog().await;
52        }
53        Ok(removed)
54    }
55}