vtcode_core/tools/
traits.rs

1//! Core traits for the composable tool system
2
3use anyhow::Result;
4use async_trait::async_trait;
5use serde_json::Value;
6use std::path::PathBuf;
7
8/// Core trait for all agent tools
9#[async_trait]
10pub trait Tool: Send + Sync {
11    /// Execute the tool with given arguments
12    async fn execute(&self, args: Value) -> Result<Value>;
13
14    /// Get the tool's name
15    fn name(&self) -> &'static str;
16
17    /// Get the tool's description
18    fn description(&self) -> &'static str;
19
20    /// Validate arguments before execution
21    fn validate_args(&self, _args: &Value) -> Result<()> {
22        // Default implementation - tools can override for specific validation
23        Ok(())
24    }
25}
26
27/// Trait for tools that operate on files
28#[async_trait]
29pub trait FileTool: Tool {
30    /// Get the workspace root
31    fn workspace_root(&self) -> &PathBuf;
32
33    /// Check if a path should be excluded
34    async fn should_exclude(&self, path: &std::path::Path) -> bool;
35}
36
37/// Trait for tools that support multiple execution modes
38#[async_trait]
39pub trait ModeTool: Tool {
40    /// Get supported modes
41    fn supported_modes(&self) -> Vec<&'static str>;
42
43    /// Execute with specific mode
44    async fn execute_mode(&self, mode: &str, args: Value) -> Result<Value>;
45}
46
47/// Trait for caching tool results
48#[async_trait]
49pub trait CacheableTool: Tool {
50    /// Generate cache key for given arguments
51    fn cache_key(&self, args: &Value) -> String;
52
53    /// Check if result should be cached
54    fn should_cache(&self, _args: &Value) -> bool {
55        true // Default: cache everything
56    }
57
58    /// Get cache TTL in seconds
59    fn cache_ttl(&self) -> u64 {
60        300 // Default: 5 minutes
61    }
62}
63
64/// Main tool executor that coordinates all tools
65#[async_trait]
66pub trait ToolExecutor: Send + Sync {
67    /// Execute a tool by name
68    async fn execute_tool(&self, name: &str, args: Value) -> Result<Value>;
69
70    /// List available tools
71    fn available_tools(&self) -> Vec<String>;
72
73    /// Check if a tool exists
74    fn has_tool(&self, name: &str) -> bool;
75}