vtcode_core/tools/registry/
declarations.rs

1use std::collections::HashMap;
2
3use crate::config::constants::tools;
4use crate::config::types::CapabilityLevel;
5use crate::gemini::FunctionDeclaration;
6use serde_json::json;
7
8use super::builtins::builtin_tool_registrations;
9
10pub fn build_function_declarations() -> Vec<FunctionDeclaration> {
11    vec![
12        // Ripgrep search tool
13        FunctionDeclaration {
14            name: tools::GREP_SEARCH.to_string(),
15            description: "Performs advanced code search across the workspace using ripgrep, supporting multiple search modes and patterns. This tool is ideal for finding specific code patterns, function definitions, variable usages, or text matches across multiple files. It should be used when you need to locate code elements, search for TODO comments, find function calls, or identify patterns in the codebase. The tool supports exact matching, fuzzy search, multi-pattern searches with AND/OR logic, and similarity-based searches. Results can be returned in concise format (recommended for most cases) or detailed raw ripgrep JSON format. Always specify a reasonable max_results limit to prevent token overflow.".to_string(),
16            parameters: json!({
17                "type": "object",
18                "properties": {
19                    "pattern": {"type": "string", "description": "Search pattern. Example: 'fn \\w+' or 'TODO|FIXME'"},
20                    "path": {"type": "string", "description": "Directory path to search in (relative). Default: '.'", "default": "."},
21                    "mode": {"type": "string", "description": "Search mode: 'exact' | 'fuzzy' | 'multi' | 'similarity'", "default": "exact"},
22                    "max_results": {"type": "integer", "description": "Max results (token efficiency). Default: 100", "default": 100},
23                    "case_sensitive": {"type": "boolean", "description": "Case sensitive search. Default: true", "default": true},
24                    // Multi-pattern search parameters
25                    "patterns": {"type": "array", "items": {"type": "string"}, "description": "For mode='multi'. Example: ['fn \\w+','use \\w+']"},
26                    "logic": {"type": "string", "description": "For mode='multi': 'AND' or 'OR'", "default": "AND"},
27                    // Fuzzy search parameters
28                    "fuzzy_threshold": {"type": "number", "description": "Fuzzy matching threshold (0.0-1.0)", "default": 0.7},
29                    // Similarity search parameters
30                    "reference_file": {"type": "string", "description": "For mode='similarity': reference file path"},
31                    "content_type": {"type": "string", "description": "For mode='similarity': 'structure'|'imports'|'functions'|'all'", "default": "all"},
32                    "response_format": {"type": "string", "description": "'concise' (default) or 'detailed' (raw rg JSON)", "default": "concise"}
33                },
34                "required": ["pattern"]
35            }),
36        },
37
38        // Consolidated file operations tool
39        FunctionDeclaration {
40            name: "list_files".to_string(),
41            description: "Explores and lists files and directories in the workspace with multiple discovery modes. This tool is essential for understanding project structure, finding files by name or content, and navigating the codebase. Use this tool when you need to see what files exist in a directory, find files matching specific patterns, or search for files containing certain content. It supports recursive directory traversal, pagination for large directories, and various filtering options. The tool can operate in different modes: 'list' for basic directory contents, 'recursive' for deep directory traversal, 'find_name' for filename-based searches, and 'find_content' for content-based file discovery. PAGINATION BEST PRACTICES: Always use pagination (page and per_page parameters) for large directories to prevent token overflow and timeouts. Default per_page=50 for optimal performance. Monitor the 'has_more' flag and continue with subsequent pages. For very large directories (>1000 items), consider reducing per_page to 25. The concise response format is recommended for most cases as it omits low-value metadata.".to_string(),
42            parameters: json!({
43                "type": "object",
44                "properties": {
45                    "path": {"type": "string", "description": "Path to search from (relative). Example: 'src'"},
46                    "mode": {"type": "string", "description": "'list' | 'recursive' | 'find_name' | 'find_content'", "default": "list"},
47                    "max_items": {"type": "integer", "description": "Cap total items scanned (token safety). Default: 1000", "default": 1000},
48                    "page": {"type": "integer", "description": "Page number (1-based). Default: 1", "default": 1},
49                    "per_page": {"type": "integer", "description": "Items per page. Default: 50", "default": 50},
50                    "response_format": {"type": "string", "description": "'concise' (default) omits low-signal fields; 'detailed' includes them", "default": "concise"},
51                    "include_hidden": {"type": "boolean", "description": "Include hidden files", "default": false},
52                    "name_pattern": {"type": "string", "description": "Optional pattern for 'recursive'/'find_name' modes. Use '*' or omit for all files. Example: '*.rs'", "default": "*"},
53                    "content_pattern": {"type": "string", "description": "For 'find_content' mode. Example: 'fn main'"},
54                    "file_extensions": {"type": "array", "items": {"type": "string"}, "description": "Filter by file extensions"},
55                    "case_sensitive": {"type": "boolean", "description": "Case sensitive pattern matching", "default": true},
56                    "ast_grep_pattern": {"type": "string", "description": "Optional AST pattern to filter files"}
57                },
58                "required": ["path"]
59            }),
60        },
61
62        // File reading tool
63        FunctionDeclaration {
64            name: tools::READ_FILE.to_string(),
65            description: "Reads the contents of a specific file from the workspace with intelligent chunking for large files. This tool automatically handles large files by reading the first and last portions when files exceed size thresholds, ensuring efficient token usage while preserving important content. For files larger than 2,000 lines, it reads the first 800 and last 800 lines with a truncation indicator. Use chunk_lines or max_lines parameters to customize the threshold. The tool provides structured logging of chunking operations for debugging.".to_string(),
66            parameters: json!({
67                "type": "object",
68                "properties": {
69                    "path": {"type": "string", "description": "File path to read"},
70                    "max_bytes": {"type": "integer", "description": "Maximum bytes to read (optional)", "default": null},
71                    "chunk_lines": {"type": "integer", "description": "Line threshold for chunking (optional, default: 2000)", "default": 2000},
72                    "max_lines": {"type": "integer", "description": "Alternative parameter for chunk_lines (optional)", "default": null}
73                },
74                "required": ["path"]
75            }),
76        },
77
78        // File writing tool
79        FunctionDeclaration {
80            name: tools::WRITE_FILE.to_string(),
81            description: "Creates new files or overwrites existing files with specified content. This tool is essential for creating new source files, configuration files, documentation, or any text-based content. Use this tool when you need to create a new file from scratch, replace an entire file's contents, or append content to an existing file. The tool supports different write modes: 'overwrite' (default) completely replaces the file content, 'append' adds content to the end of the file, and 'skip_if_exists' prevents overwriting existing files. Always ensure you have the correct file path and that the directory exists before writing. This tool cannot create directories automatically - use the terminal command tool for directory creation if needed. The tool validates that the content is properly written and returns success/failure status.".to_string(),
82            parameters: json!({
83                "type": "object",
84                "properties": {
85                    "path": {"type": "string", "description": "File path to write to"},
86                    "content": {"type": "string", "description": "Content to write to the file"},
87                    "mode": {"type": "string", "description": "Write mode: 'overwrite' (default) or 'append'", "default": "overwrite"}
88                },
89                "required": ["path", "content"]
90            }),
91        },
92
93        // File editing tool
94        FunctionDeclaration {
95            name: tools::EDIT_FILE.to_string(),
96            description: "Performs precise text replacements within existing files by finding and replacing exact text matches. This tool is crucial for making targeted code changes, fixing bugs, updating configurations, or modifying documentation. Use this tool when you need to change specific text in a file without affecting the rest of the content. Always read the file first using the read_file tool to identify the exact text to replace, including proper indentation and surrounding context. The old_str parameter must match the existing text exactly, including whitespace and formatting. This tool is preferred over write_file when you only need to modify part of a file, as it preserves the rest of the file's content. Note that this tool performs exact string matching - it cannot handle complex refactoring or pattern-based replacements.".to_string(),
97            parameters: json!({
98                "type": "object",
99                "properties": {
100                    "path": {"type": "string", "description": "File path to edit"},
101                    "old_str": {"type": "string", "description": "Exact text to replace (must match exactly)"},
102                    "new_str": {"type": "string", "description": "New text to replace with"}
103                },
104                "required": ["path", "old_str", "new_str"]
105            }),
106        },
107
108        // Consolidated command execution tool
109        FunctionDeclaration {
110            name: tools::RUN_TERMINAL_CMD.to_string(),
111            description: "Executes shell commands and external programs in the workspace environment with intelligent output truncation for large command outputs. This tool automatically handles verbose command outputs by truncating to the first and last portions when output exceeds 10,000 lines, ensuring efficient token usage while preserving important information. For commands producing excessive output, it shows the first 5,000 and last 5,000 lines with a truncation indicator. Use this tool for build processes, package managers, test suites, and system operations. Supports 'terminal' (default), 'pty' (interactive), and 'streaming' (long-running) modes. Always specify timeouts and prefer specialized tools for file operations.".to_string(),
112            parameters: json!({
113                "type": "object",
114                "properties": {
115                    "command": {"type": "array", "items": {"type": "string"}, "description": "Program + args as array"},
116                    "working_dir": {"type": "string", "description": "Working directory relative to workspace"},
117                    "timeout_secs": {"type": "integer", "description": "Command timeout in seconds (default: 30)", "default": 30},
118                    "mode": {"type": "string", "description": "Execution mode: 'terminal' | 'pty' | 'streaming'", "default": "terminal"},
119                    "response_format": {"type": "string", "description": "'concise' (default) or 'detailed'", "default": "concise"}
120                },
121                "required": ["command"]
122            }),
123        },
124        FunctionDeclaration {
125            name: tools::CURL.to_string(),
126            description: "Fetches HTTPS text content through a sandboxed curl wrapper with strict validation. Use this tool to inspect trusted documentation or small JSON payloads from public HTTPS endpoints. It blocks localhost and private networks, enforces HTTPS-only URLs, limits responses to policy-capped byte sizes, and returns a security_notice so you can remind the user what was fetched and why it is safe.".to_string(),
127            parameters: json!({
128                "type": "object",
129                "properties": {
130                    "url": {"type": "string", "description": "HTTPS URL to fetch (public hosts only)."},
131                    "method": {"type": "string", "description": "HTTP method: 'GET' (default) or 'HEAD'.", "default": "GET"},
132                    "max_bytes": {"type": "integer", "description": "Maximum response bytes to read (must respect policy cap).", "default": 65536},
133                    "timeout_secs": {"type": "integer", "description": "Request timeout in seconds (<=30)", "default": 10},
134                    "save_response": {"type": "boolean", "description": "When true, saves the body to /tmp/vtcode-curl and returns the path so you can inspect then delete it.", "default": false}
135                },
136                "required": ["url"]
137            }),
138        },
139
140        // AST-grep search and transformation tool
141        FunctionDeclaration {
142            name: tools::AST_GREP_SEARCH.to_string(),
143            description: "Performs advanced syntax-aware code analysis and transformation using AST-grep patterns. This tool excels at structural code searches, automated refactoring, and complex code transformations that require understanding of programming language syntax. Use this tool for finding function definitions, class structures, import statements, or complex code patterns that cannot be easily found with simple text search. It supports multiple operations: 'search' for finding code patterns, 'transform' for automated code changes, 'lint' for code quality checks, 'refactor' for structural improvements, and 'custom' for specialized operations. The tool can work with multiple programming languages and provides context-aware results. Always specify reasonable limits for context_lines and max_results to prevent token overflow. Preview mode is enabled by default for transform operations to allow safe review before applying changes.".to_string(),
144            parameters: json!({
145                "type": "object",
146                "properties": {
147                    "operation": {"type": "string", "description": "Operation type: 'search', 'transform', 'lint', 'refactor', 'custom'", "default": "search"},
148                    "pattern": {"type": "string", "description": "AST-grep pattern to search for"},
149                    "path": {"type": "string", "description": "File or directory path to search in", "default": "."},
150                    "language": {"type": "string", "description": "Programming language (auto-detected if not specified)"},
151                    "replacement": {"type": "string", "description": "Replacement pattern for transform operations"},
152                    "refactor_type": {"type": "string", "description": "Type of refactoring: 'extract_function', 'remove_console_logs', 'simplify_conditions', 'extract_constants', 'modernize_syntax'"},
153                    "context_lines": {"type": "integer", "description": "Number of context lines to show", "default": 0},
154                    "max_results": {"type": "integer", "description": "Maximum number of results", "default": 100},
155                    "preview_only": {"type": "boolean", "description": "Preview changes without applying (transform only)", "default": true},
156                    "update_all": {"type": "boolean", "description": "Update all matches (transform only)", "default": false},
157                    "interactive": {"type": "boolean", "description": "Interactive mode (custom only)", "default": false},
158                    "severity_filter": {"type": "string", "description": "Filter lint results by severity"}
159                },
160                "required": ["pattern", "path"]
161            }),
162        },
163
164        // Simple bash-like search tool
165        FunctionDeclaration {
166            name: tools::SIMPLE_SEARCH.to_string(),
167            description: "Provides simple bash-like file operations and searches for quick, straightforward tasks. This tool offers direct access to common Unix commands like grep, find, ls, cat, head, tail, and file indexing. Use this tool when you need basic file operations without the complexity of advanced search features. It is ideal for quick file content previews, directory listings, or simple pattern matching. The tool supports various commands: 'grep' for text searching, 'find' for file discovery, 'ls' for directory listing, 'cat' for full file reading, 'head'/'tail' for partial file reading, and 'index' for file indexing. This tool is less powerful than specialized search tools but provides fast, intuitive access to common operations. Use appropriate max_results limits to prevent excessive output, especially with recursive operations.".to_string(),
168            parameters: json!({
169                "type": "object",
170                "properties": {
171                    "command": {"type": "string", "description": "Command to execute: 'grep', 'find', 'ls', 'cat', 'head', 'tail', 'index'", "default": "grep"},
172                    "pattern": {"type": "string", "description": "Search pattern for grep/find commands"},
173                    "file_pattern": {"type": "string", "description": "File pattern filter for grep"},
174                    "file_path": {"type": "string", "description": "File path for cat/head/tail commands"},
175                    "path": {"type": "string", "description": "Directory path for ls/find/index commands", "default": "."},
176                    "start_line": {"type": "integer", "description": "Start line number for cat command"},
177                    "end_line": {"type": "integer", "description": "End line number for cat command"},
178                    "lines": {"type": "integer", "description": "Number of lines for head/tail commands", "default": 10},
179                    "max_results": {"type": "integer", "description": "Maximum results to return", "default": 50},
180                    "show_hidden": {"type": "boolean", "description": "Show hidden files for ls command", "default": false}
181                },
182                "required": []
183            }),
184        },
185
186        // Bash-like command tool
187        FunctionDeclaration {
188            name: tools::BASH.to_string(),
189            description: "Executes bash-like commands through a pseudo-terminal interface for interactive operations. This tool provides access to common shell commands with enhanced terminal emulation. Use this tool when you need interactive command execution, complex shell pipelines, or commands that require a proper terminal environment. It supports essential commands like 'ls' for directory listing, 'pwd' for current directory, 'grep' for text search, 'find' for file discovery, 'cat'/'head'/'tail' for file content viewing, and file manipulation commands like 'mkdir', 'rm', 'cp', 'mv'. The tool includes safety restrictions and should be used as a complement to specialized tools rather than a replacement. Prefer 'run_terminal_cmd' for non-interactive commands and file/search tools for file operations. Always use appropriate flags and be aware of the recursive and force options which can affect multiple files.".to_string(),
190            parameters: json!({
191                "type": "object",
192                "properties": {
193                    "bash_command": {"type": "string", "description": "Bash command to execute: 'ls', 'pwd', 'grep', 'find', 'cat', 'head', 'tail', 'mkdir', 'rm', 'cp', 'mv', 'stat', 'run'", "default": "ls"},
194                    "path": {"type": "string", "description": "Path for file/directory operations"},
195                    "source": {"type": "string", "description": "Source path for cp/mv operations"},
196                    "dest": {"type": "string", "description": "Destination path for cp/mv operations"},
197                    "pattern": {"type": "string", "description": "Search pattern for grep/find"},
198                    "recursive": {"type": "boolean", "description": "Recursive operation", "default": false},
199                    "show_hidden": {"type": "boolean", "description": "Show hidden files", "default": false},
200                    "parents": {"type": "boolean", "description": "Create parent directories", "default": false},
201                    "force": {"type": "boolean", "description": "Force operation", "default": false},
202                    "lines": {"type": "integer", "description": "Number of lines for head/tail", "default": 10},
203                    "start_line": {"type": "integer", "description": "Start line for cat"},
204                    "end_line": {"type": "integer", "description": "End line for cat"},
205                    "name_pattern": {"type": "string", "description": "Name pattern for find"},
206                    "type_filter": {"type": "string", "description": "Type filter for find (f=file, d=directory)"},
207                    "command": {"type": "string", "description": "Command to run for arbitrary execution"},
208                    "args": {"type": "array", "items": {"type": "string"}, "description": "Arguments for command execution"}
209                },
210                "required": []
211            }),
212        },
213
214        // Apply patch tool (Codex patch format)
215        FunctionDeclaration {
216            name: tools::APPLY_PATCH.to_string(),
217            description: "Applies Codex-style patch blocks to modify multiple files in the workspace. This tool is specialized for applying structured patches that contain changes to multiple files or complex modifications. Use this tool when you receive patch content in the Codex format (marked with '*** Begin Patch' and '*** End Patch') instead of making individual file edits. The tool parses the patch format, validates the changes, and applies them atomically to prevent partial updates. It is particularly useful for applying code review suggestions, automated refactoring changes, or complex multi-file modifications. The tool provides detailed feedback on which files were modified and any issues encountered during application. Always ensure the patch content is complete and properly formatted before using this tool.".to_string(),
218            parameters: json!({
219                "type": "object",
220                "properties": {
221                    "input": {"type": "string", "description": "Patch content in Codex patch format"}
222                },
223                "required": ["input"]
224            }),
225        },
226        FunctionDeclaration {
227            name: tools::UPDATE_PLAN.to_string(),
228            description: "Records or updates the agent's current multi-step plan. Provide a concise explanation (optional) and a list of plan steps with their status. Exactly one step may be marked 'in_progress'; all other steps must be 'pending' or 'completed'. Use this tool to keep the user informed about your approach for complex tasks, render the plan as a Markdown TODO list with checkboxes, and update it whenever progress changes.".to_string(),
229            parameters: json!({
230                "type": "object",
231                "properties": {
232                    "explanation": {
233                        "type": "string",
234                        "description": "Optional summary explaining the plan or changes made."
235                    },
236                    "plan": {
237                        "type": "array",
238                        "description": "Ordered list of plan steps with status metadata.",
239                        "items": {
240                            "type": "object",
241                            "properties": {
242                                "step": {
243                                    "type": "string",
244                                    "description": "Description of the work to perform."
245                                },
246                                "status": {
247                                    "type": "string",
248                                    "enum": ["pending", "in_progress", "completed"],
249                                    "description": "Current state of the step."
250                                }
251                            },
252                            "required": ["step", "status"],
253                            "additionalProperties": false
254                        }
255                    }
256                },
257                "required": ["plan"],
258                "additionalProperties": false
259            }),
260        },
261    ]
262}
263
264/// Build function declarations filtered by capability level
265pub fn build_function_declarations_for_level(level: CapabilityLevel) -> Vec<FunctionDeclaration> {
266    let tool_capabilities: HashMap<&'static str, CapabilityLevel> = builtin_tool_registrations()
267        .into_iter()
268        .filter(|registration| registration.expose_in_llm())
269        .map(|registration| (registration.name(), registration.capability()))
270        .collect();
271
272    build_function_declarations()
273        .into_iter()
274        .filter(|fd| {
275            tool_capabilities
276                .get(fd.name.as_str())
277                .map(|required| level >= *required)
278                .unwrap_or(false)
279        })
280        .collect()
281}