agcodex_core/tools/
mod.rs

1//! Internal agent tools for AGCodex
2//!
3//! This module provides sophisticated tools for task planning, code analysis,
4//! and automated workflow management. These tools are designed to work seamlessly
5//! with the subagent orchestration system.
6
7pub mod edit;
8pub mod glob;
9pub mod index;
10pub mod integration_example;
11pub mod output;
12pub mod patch;
13pub mod plan;
14pub mod think;
15pub mod tree;
16
17// Unified tool registry and adapters
18pub mod adapters;
19pub mod registry;
20
21#[cfg(test)]
22mod integration_test_glob;
23
24#[cfg(test)]
25mod integration_test_registry;
26
27// Use simplified grep implementation to avoid ast-grep API issues
28pub mod grep_simple;
29pub use grep_simple::GrepConfig;
30pub use grep_simple::GrepError;
31pub use grep_simple::GrepMatch;
32pub use grep_simple::GrepQuery;
33pub use grep_simple::GrepResult;
34pub use grep_simple::GrepTool;
35pub use grep_simple::RuleType;
36pub use grep_simple::SupportedLanguage as GrepSupportedLanguage;
37
38pub use edit::AmbiguousMatches;
39pub use edit::EditError;
40pub use edit::EditResult;
41pub use edit::EditTool;
42pub use edit::MatchCandidate;
43pub use glob::FileMatch;
44pub use glob::FileType;
45pub use glob::GlobError;
46pub use glob::GlobResult;
47pub use glob::GlobTool;
48pub use index::BuildInput;
49pub use index::IndexConfig;
50pub use index::IndexError;
51pub use index::IndexResult;
52pub use index::IndexStats;
53pub use index::IndexTool;
54pub use index::IndexedDocument;
55pub use index::MergePolicyConfig;
56pub use index::SearchInput;
57pub use index::SearchQuery;
58pub use index::SearchResult;
59pub use index::Symbol;
60pub use index::UpdateInput;
61pub use output::{
62    ApiCompatibility,
63    BreakingChange,
64    BreakingSeverity,
65    CacheStats,
66    Change,
67    ChangeBuilder,
68    ChangeKind,
69    ComplexityChange,
70    ComplexityMetrics,
71    ComprehensiveAstSummary,
72    ComprehensivePerformanceImpact,
73    ComprehensiveSemanticImpact,
74    ComprehensiveSymbol,
75    // Core output types
76    ComprehensiveToolOutput,
77    ContextLine,
78    ContextLineType,
79    ContextSnapshot,
80    CoverageImpact,
81    CpuUsage,
82    DependencyInfo,
83    Diagnostic,
84    DiagnosticLevel,
85    HalsteadMetrics,
86    ImpactLevel,
87    ImpactScope,
88    ImportChangeType,
89    IoStats,
90    LanguageContext,
91    MemoryImpact,
92    MemoryUsage,
93    ModificationType,
94    OperationContext,
95    OperationMetadata,
96    OperationScope,
97    // Builder patterns
98    OutputBuilder,
99    PerformanceChange,
100    PerformanceMetrics,
101    // Performance timing
102    PerformanceTimer,
103    ProjectContext,
104    RefactorType,
105    // Extension traits
106    ResultExt,
107    ScopeType,
108    SecurityImpact,
109    SecurityLevel,
110    SecurityVulnerability,
111    SymbolType,
112    TestCategory,
113    TestImpact,
114    VersionImpact,
115    Visibility,
116    multi_file_changes,
117    simple_error,
118    // Convenience functions
119    simple_success,
120    single_file_modification,
121};
122// Simplified patch tool exports
123pub use patch::ExtractStats;
124pub use patch::ImportStats;
125pub use patch::PatchError;
126pub use patch::PatchResult;
127pub use patch::PatchTool;
128pub use patch::RenameScope;
129pub use patch::RenameStats;
130pub use plan::AgentType;
131pub use plan::DependencyGraph;
132pub use plan::MetaTask;
133pub use plan::MetaTaskPlanner;
134pub use plan::Plan;
135pub use plan::PlanContext;
136pub use plan::PlanError;
137pub use plan::PlanExecutionPlan;
138pub use plan::PlanExecutionStep;
139pub use plan::PlanIntelligenceLevel;
140pub use plan::PlanTool;
141pub use plan::SubTask;
142pub use plan::SubTaskPlanner;
143pub use plan::TaskGroup;
144pub use plan::TaskPriority;
145pub use plan::TaskStatus;
146// ToolOutput is already exported from output module, no need to import from plan
147pub use think::ThinkError;
148pub use think::ThinkResult;
149pub use think::ThinkStep;
150pub use think::ThinkTool;
151pub use tree::DiffNode;
152pub use tree::ModifiedNode;
153pub use tree::MovedNode;
154pub use tree::ParsedAst;
155pub use tree::Point;
156pub use tree::QueryCapture;
157pub use tree::QueryMatch;
158pub use tree::SemanticDiff;
159pub use tree::SupportedLanguage;
160pub use tree::Symbol as TreeSymbol;
161pub use tree::SymbolKind;
162pub use tree::TreeError;
163pub use tree::TreeInput;
164pub use tree::TreeOutput;
165pub use tree::TreeResult;
166pub use tree::TreeTool;
167
168/// Tool trait for internal AGCodex tools
169#[async_trait::async_trait]
170pub trait InternalTool {
171    type Input;
172    type Output;
173    type Error;
174
175    /// Execute the tool with the given input
176    async fn execute(&self, input: Self::Input) -> Result<Self::Output, Self::Error>;
177
178    /// Get tool metadata
179    fn metadata(&self) -> ToolMetadata;
180}
181
182/// Metadata about a tool
183#[derive(Debug, Clone)]
184pub struct ToolMetadata {
185    pub name: String,
186    pub description: String,
187    pub version: String,
188    pub author: String,
189}
190
191/// Common result type for all internal tools
192pub type ToolResult<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
193
194// Re-export registry types for unified tool access
195pub use registry::ToolCategory;
196pub use registry::ToolError;
197pub use registry::ToolInfo;
198pub use registry::ToolOutput;
199pub use registry::ToolRegistry;
200
201/// Create the default tool registry with all tools registered
202///
203/// This provides a simple, unified interface to all AGCodex tools.
204///
205/// # Example
206/// ```
207/// let registry = create_default_registry();
208/// let tools = registry.list_tools();
209/// println!("Available tools: {:?}", tools);
210/// ```
211pub fn create_default_registry() -> ToolRegistry {
212    let mut registry = ToolRegistry::new();
213
214    // Register search tools
215    registry.register(ToolInfo {
216        name: "search",
217        description: "Search for code patterns and symbols",
218        category: ToolCategory::Search,
219        example: r#"{"query": "function main", "path": "src/"}"#,
220        execute: adapters::adapt_search_tool,
221    });
222
223    registry.register(ToolInfo {
224        name: "grep",
225        description: "Pattern-based text search",
226        category: ToolCategory::Search,
227        example: r#"{"pattern": "TODO", "path": "."}"#,
228        execute: adapters::adapt_grep_tool,
229    });
230
231    registry.register(ToolInfo {
232        name: "glob",
233        description: "Find files by pattern",
234        category: ToolCategory::Search,
235        example: r#"{"pattern": "*.rs", "path": "src/"}"#,
236        execute: adapters::adapt_glob_tool,
237    });
238
239    // Register edit tools
240    registry.register(ToolInfo {
241        name: "edit",
242        description: "Simple text replacement in files",
243        category: ToolCategory::Edit,
244        example: r#"{"file": "main.rs", "old_text": "foo", "new_text": "bar"}"#,
245        execute: adapters::adapt_edit_tool,
246    });
247
248    registry.register(ToolInfo {
249        name: "patch",
250        description: "Bulk code transformations",
251        category: ToolCategory::Edit,
252        example: r#"{"operation": "rename_symbol", "old_name": "foo", "new_name": "bar"}"#,
253        execute: adapters::adapt_patch_tool,
254    });
255
256    // Register analysis tools
257    registry.register(ToolInfo {
258        name: "think",
259        description: "Step-by-step reasoning about problems",
260        category: ToolCategory::Analysis,
261        example: r#"{"problem": "How to optimize database queries?"}"#,
262        execute: adapters::adapt_think_tool,
263    });
264
265    registry.register(ToolInfo {
266        name: "plan",
267        description: "Create task plans with dependencies",
268        category: ToolCategory::Analysis,
269        example: r#"{"description": "Refactor authentication", "constraints": ["maintain API"]}"#,
270        execute: adapters::adapt_plan_tool,
271    });
272
273    registry.register(ToolInfo {
274        name: "tree",
275        description: "Parse and analyze code structure",
276        category: ToolCategory::Analysis,
277        example: r#"{"file": "main.rs", "language": "rust"}"#,
278        execute: adapters::adapt_tree_tool,
279    });
280
281    // Register utility tools
282    registry.register(ToolInfo {
283        name: "bash",
284        description: "Execute safe shell commands",
285        category: ToolCategory::Utility,
286        example: r#"{"command": "ls -la"}"#,
287        execute: adapters::adapt_bash_tool,
288    });
289
290    registry
291}