vtcode_core/tools/registry/
trait_impls.rs1use anyhow::Result;
9use serde_json::Value;
10use std::path::PathBuf;
11use std::sync::Arc;
12use std::time::Duration;
13
14use crate::config::types::CapabilityLevel;
15use crate::llm::provider::ToolDefinition;
16use crate::llm::providers::gemini::wire::FunctionDeclaration;
17use crate::mcp::{McpClient, McpToolInfo};
18use crate::tool_policy::ToolPolicy;
19use crate::tools::exec_session::ExecSessionManager;
20use crate::tools::handlers::{SessionSurface, SessionToolsConfig, ToolCallError, ToolSchemaEntry};
21use crate::tools::registry::{ToolExecutionRecord, ToolPermissionDecision, ToolRegistration, ToolTimeoutCategory};
22
23use super::ToolRegistry;
24use super::interfaces::{McpBridge, PtySessionControl, ToolCatalog, ToolMetrics, ToolResilience, ToolSecurity};
25
26#[async_trait::async_trait]
31impl ToolSecurity for ToolRegistry {
32 async fn evaluate_tool_policy(&self, tool_name: &str) -> Result<ToolPermissionDecision> {
33 self.evaluate_tool_policy(tool_name).await
34 }
35
36 async fn get_tool_policy(&self, tool_name: &str) -> ToolPolicy {
37 self.get_tool_policy(tool_name).await
38 }
39
40 async fn set_tool_policy(&self, tool_name: &str, policy: ToolPolicy) -> Result<()> {
41 self.set_tool_policy(tool_name, policy).await
42 }
43
44 async fn mark_tool_preapproved(&self, tool_name: &str) {
45 self.mark_tool_preapproved(tool_name).await;
46 }
47
48 async fn enable_full_auto_permission(&self, allowed_tools: &[String]) {
49 self.enable_full_auto_permission(allowed_tools).await;
50 }
51
52 async fn disable_full_auto_permission(&self) {
53 self.disable_full_auto_permission().await;
54 }
55
56 async fn is_allowed_in_full_auto(&self, tool_name: &str) -> bool {
57 self.is_allowed_in_full_auto(tool_name).await
58 }
59
60 async fn is_denied_in_full_auto(&self, tool_name: &str) -> bool {
61 self.is_denied_in_full_auto(tool_name).await
62 }
63
64 async fn apply_config_policies(&self, tools_config: &crate::config::ToolsConfig) -> Result<()> {
65 self.apply_config_policies(tools_config).await
66 }
67
68 fn sandbox_config(&self) -> vtcode_config::SandboxConfig {
69 self.sandbox_config()
70 }
71
72 fn apply_sandbox_config(&self, config: &vtcode_config::SandboxConfig) {
73 self.apply_sandbox_config(config);
74 }
75
76 async fn persist_approval_cache_key(&self, key: &str) -> Result<()> {
77 self.persist_approval_cache_key(key).await
78 }
79
80 async fn has_persisted_approval(&self, key: &str) -> bool {
81 self.has_persisted_approval(key).await
82 }
83}
84
85#[async_trait::async_trait]
90impl PtySessionControl for ToolRegistry {
91 fn can_start_session(&self) -> bool {
92 self.can_start_pty_session()
93 }
94
95 fn active_session_count(&self) -> usize {
96 self.active_pty_sessions()
97 }
98
99 async fn terminate_all_sessions(&self) -> Result<()> {
100 self.terminate_all_pty_sessions_async().await
101 }
102
103 fn exec_session_manager(&self) -> ExecSessionManager {
104 self.exec_session_manager()
105 }
106}
107
108#[async_trait::async_trait]
113impl McpBridge for ToolRegistry {
114 async fn set_mcp_client(&self, client: Arc<McpClient>) {
115 self.set_mcp_client(client).await;
116 }
117
118 async fn clear_mcp_client(&self) {
119 self.clear_mcp_client().await;
120 }
121
122 fn mcp_client(&self) -> Option<Arc<McpClient>> {
123 self.mcp_client()
124 }
125
126 async fn list_mcp_tools(&self) -> Result<Vec<McpToolInfo>> {
127 self.list_mcp_tools().await
128 }
129
130 async fn has_mcp_tool(&self, tool_name: &str) -> bool {
131 self.has_mcp_tool(tool_name).await
132 }
133
134 async fn execute_mcp_tool(&self, tool_name: &str, args: Value) -> Result<Value> {
135 self.execute_mcp_tool(tool_name, args).await
136 }
137
138 async fn refresh_mcp_tools(&self) -> Result<()> {
139 self.refresh_mcp_tools().await
140 }
141}
142
143impl ToolResilience for ToolRegistry {
148 fn effective_timeout(&self, category: ToolTimeoutCategory) -> Option<Duration> {
149 self.effective_timeout(category)
150 }
151
152 fn record_failure(&self, category: ToolTimeoutCategory) -> bool {
153 self.record_tool_failure(category)
154 }
155
156 fn reset_failure(&self, category: ToolTimeoutCategory) {
157 self.reset_tool_failure(category);
158 }
159
160 fn record_latency(&self, category: ToolTimeoutCategory, duration: Duration) {
161 self.record_tool_latency(category, duration);
162 }
163
164 fn should_circuit_break(&self, category: ToolTimeoutCategory) -> Option<Duration> {
165 self.should_circuit_break(category)
166 }
167
168 fn decay_adaptive_timeout(&self, category: ToolTimeoutCategory) {
169 self.decay_adaptive_timeout(category);
170 }
171}
172
173#[async_trait::async_trait]
178impl ToolCatalog for ToolRegistry {
179 async fn register_tool(&self, registration: ToolRegistration) -> Result<()> {
180 self.register_tool(registration).await
181 }
182
183 async fn unregister_tool(&self, name: &str) -> Result<bool> {
184 self.unregister_tool(name).await
185 }
186
187 fn get_tool(&self, name: &str) -> Option<Arc<dyn crate::tools::traits::Tool>> {
188 self.get_tool(name)
189 }
190
191 fn workspace_root(&self) -> PathBuf {
192 self.workspace_root_owned()
193 }
194
195 async fn public_tool_names(&self, surface: SessionSurface, capability_level: CapabilityLevel) -> Vec<String> {
196 self.public_tool_names(surface, capability_level).await
197 }
198
199 async fn schema_entries(&self, config: SessionToolsConfig) -> Vec<ToolSchemaEntry> {
200 self.schema_entries(config).await
201 }
202
203 async fn function_declarations(&self, config: SessionToolsConfig) -> Vec<FunctionDeclaration> {
204 self.function_declarations(config).await
205 }
206
207 async fn model_tools(&self, config: SessionToolsConfig) -> Vec<ToolDefinition> {
208 self.model_tools(config).await
209 }
210
211 fn resolve_tool_name(&self, name: &str) -> Result<String, ToolCallError> {
212 self.resolve_public_tool_name(name)
213 }
214}
215
216impl ToolMetrics for ToolRegistry {
221 fn record_execution(&self, _record: ToolExecutionRecord) {
222 }
225
226 fn call_count(&self) -> u64 {
227 self.tool_call_count()
228 }
229
230 fn pty_poll_count(&self) -> u64 {
231 self.pty_poll_count()
232 }
233
234 fn metrics_collector(&self) -> Arc<crate::metrics::MetricsCollector> {
235 self.metrics_collector()
236 }
237}