agcodex-core 0.1.0

Core business logic with AST-RAG engine and tree-sitter integration
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
//! TreeTool Usage Examples
//!
//! This file demonstrates various use cases for the TreeTool with different
//! programming languages and operations.

use agcodex_core::subagents::config::IntelligenceLevel;
use agcodex_core::tools::InternalTool;
use agcodex_core::tools::SupportedLanguage;
use agcodex_core::tools::TreeInput;
use agcodex_core::tools::TreeOutput;
use agcodex_core::tools::TreeTool;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Initialize the tree tool with medium intelligence
    let tree_tool = TreeTool::new(IntelligenceLevel::Medium)?;

    println!("🌳 TreeTool Usage Examples\n");

    // Example 1: Parse Rust code
    println!("1. Parsing Rust Code");
    let rust_code = r#"
        pub struct Calculator {
            value: f64,
        }
        
        impl Calculator {
            pub fn new() -> Self {
                Self { value: 0.0 }
            }
            
            pub fn add(&mut self, n: f64) -> f64 {
                self.value += n;
                self.value
            }
        }
        
        fn main() {
            let mut calc = Calculator::new();
            println!("Result: {}", calc.add(5.0));
        }
    "#;

    let parse_input = TreeInput::Parse {
        code: rust_code.to_string(),
        language: Some(SupportedLanguage::Rust),
        file_path: None,
    };

    let result = tree_tool.execute(parse_input).await?;
    match &result.result {
        TreeOutput::Parsed {
            language,
            node_count,
            has_errors,
            error_count,
            parse_time_ms,
        } => {
            println!("  ✅ Language: {:?}", language);
            println!("  ✅ Nodes: {}", node_count);
            println!("  ✅ Errors: {} ({})", has_errors, error_count);
            println!("  ✅ Parse time: {}ms", parse_time_ms);
        }
        _ => println!("  ❌ Unexpected output type"),
    }
    println!();

    // Example 2: Query for function definitions
    println!("2. Query for Function Definitions");
    let query_pattern = "(function_item name: (identifier) @func_name)";

    let query_input = TreeInput::Query {
        code: rust_code.to_string(),
        language: Some(SupportedLanguage::Rust),
        pattern: query_pattern.to_string(),
        file_path: None,
    };

    let result = tree_tool.execute(query_input).await?;
    match &result.result {
        TreeOutput::QueryResults {
            matches,
            total_matches,
            query_time_ms,
        } => {
            println!(
                "  ✅ Found {} functions in {}ms:",
                total_matches, query_time_ms
            );
            for query_match in matches {
                for capture in &query_match.captures {
                    if capture.name == "func_name" {
                        println!(
                            "    - {} at line {}",
                            capture.text,
                            capture.start_point.row + 1
                        );
                    }
                }
            }
        }
        _ => println!("  ❌ Unexpected output type"),
    }
    println!();

    // Example 3: Parse Python code
    println!("3. Parsing Python Code");
    let python_code = r#"
class DataProcessor:
    def __init__(self, data):
        self.data = data
        self.processed = False
    
    def process(self):
        """Process the data and return results."""
        if not self.processed:
            self.data = [x * 2 for x in self.data]
            self.processed = True
        return self.data
    
    def reset(self):
        self.processed = False

def main():
    processor = DataProcessor([1, 2, 3, 4, 5])
    result = processor.process()
    print(f"Processed data: {result}")

if __name__ == "__main__":
    main()
    "#;

    let parse_input = TreeInput::Parse {
        code: python_code.to_string(),
        language: Some(SupportedLanguage::Python),
        file_path: None,
    };

    let result = tree_tool.execute(parse_input).await?;
    match &result.result {
        TreeOutput::Parsed {
            language,
            node_count,
            has_errors,
            error_count,
            parse_time_ms,
        } => {
            println!("  ✅ Language: {:?}", language);
            println!("  ✅ Nodes: {}", node_count);
            println!("  ✅ Errors: {} ({})", has_errors, error_count);
            println!("  ✅ Parse time: {}ms", parse_time_ms);
        }
        _ => println!("  ❌ Unexpected output type"),
    }
    println!();

    // Example 4: Query Python class definitions
    println!("4. Query Python Classes");
    let python_query = "(class_definition name: (identifier) @class_name)";

    let query_input = TreeInput::Query {
        code: python_code.to_string(),
        language: Some(SupportedLanguage::Python),
        pattern: python_query.to_string(),
        file_path: None,
    };

    let result = tree_tool.execute(query_input).await?;
    match &result.result {
        TreeOutput::QueryResults {
            matches,
            total_matches,
            query_time_ms,
        } => {
            println!(
                "  ✅ Found {} classes in {}ms:",
                total_matches, query_time_ms
            );
            for query_match in matches {
                for capture in &query_match.captures {
                    if capture.name == "class_name" {
                        println!(
                            "    - {} at line {}",
                            capture.text,
                            capture.start_point.row + 1
                        );
                    }
                }
            }
        }
        _ => println!("  ❌ Unexpected output type"),
    }
    println!();

    // Example 5: JavaScript/TypeScript parsing
    println!("5. Parsing TypeScript Code");
    let typescript_code = r#"
interface User {
    id: number;
    name: string;
    email?: string;
}

class UserService {
    private users: User[] = [];
    
    constructor() {
        this.loadUsers();
    }
    
    async loadUsers(): Promise<void> {
        // Simulate API call
        this.users = [
            { id: 1, name: "Alice", email: "alice@example.com" },
            { id: 2, name: "Bob" }
        ];
    }
    
    findUser(id: number): User | undefined {
        return this.users.find(user => user.id === id);
    }
    
    addUser(user: Omit<User, 'id'>): User {
        const newUser: User = {
            id: Math.max(...this.users.map(u => u.id)) + 1,
            ...user
        };
        this.users.push(newUser);
        return newUser;
    }
}

const userService = new UserService();
    "#;

    let parse_input = TreeInput::Parse {
        code: typescript_code.to_string(),
        language: Some(SupportedLanguage::TypeScript),
        file_path: None,
    };

    let result = tree_tool.execute(parse_input).await?;
    match &result.result {
        TreeOutput::Parsed {
            language,
            node_count,
            has_errors,
            error_count,
            parse_time_ms,
        } => {
            println!("  ✅ Language: {:?}", language);
            println!("  ✅ Nodes: {}", node_count);
            println!("  ✅ Errors: {} ({})", has_errors, error_count);
            println!("  ✅ Parse time: {}ms", parse_time_ms);
        }
        _ => println!("  ❌ Unexpected output type"),
    }
    println!();

    // Example 6: Extract symbols
    println!("6. Symbol Extraction from Rust Code");
    let symbol_input = TreeInput::ExtractSymbols {
        code: rust_code.to_string(),
        language: Some(SupportedLanguage::Rust),
        file_path: None,
    };

    let result = tree_tool.execute(symbol_input).await?;
    match &result.result {
        TreeOutput::Symbols {
            symbols,
            total_symbols,
            extraction_time_ms,
        } => {
            println!(
                "  ✅ Extracted {} symbols in {}ms",
                total_symbols, extraction_time_ms
            );
            // Note: Symbol extraction would need implementation for each language
            if symbols.is_empty() {
                println!("    (Symbol extraction implementation pending for Rust)");
            }
        }
        _ => println!("  ❌ Unexpected output type"),
    }
    println!();

    // Example 7: Code diffing
    println!("7. Semantic Code Diff");
    let old_rust_code = r#"
        fn calculate(a: i32, b: i32) -> i32 {
            a + b
        }
    "#;

    let new_rust_code = r#"
        fn calculate(a: i32, b: i32, c: i32) -> i32 {
            a + b + c
        }
        
        fn multiply(x: i32, y: i32) -> i32 {
            x * y
        }
    "#;

    let diff_input = TreeInput::Diff {
        old_code: old_rust_code.to_string(),
        new_code: new_rust_code.to_string(),
        language: Some(SupportedLanguage::Rust),
    };

    let result = tree_tool.execute(diff_input).await?;
    match &result.result {
        TreeOutput::Diff { diff, diff_time_ms } => {
            println!("  ✅ Computed diff in {}ms", diff_time_ms);
            println!("  ✅ Similarity: {:.2}%", diff.similarity_score * 100.0);
            println!("  ✅ Added: {} nodes", diff.added.len());
            println!("  ✅ Removed: {} nodes", diff.removed.len());
            println!("  ✅ Modified: {} nodes", diff.modified.len());
        }
        _ => println!("  ❌ Unexpected output type"),
    }
    println!();

    // Example 8: Cache performance demonstration
    println!("8. Cache Performance Test");
    let start = std::time::Instant::now();

    // Parse the same code multiple times to demonstrate caching
    for i in 0..5 {
        let parse_input = TreeInput::Parse {
            code: rust_code.to_string(),
            language: Some(SupportedLanguage::Rust),
            file_path: None,
        };

        let result = tree_tool.execute(parse_input).await?;
        if let TreeOutput::Parsed { parse_time_ms, .. } = result.result {
            println!("  Parse #{}: {}ms", i + 1, parse_time_ms);
        }
    }

    let total_time = start.elapsed();
    println!("  ✅ Total time for 5 parses: {:?}", total_time);
    println!("  ✅ Average per parse: {:?}", total_time / 5);

    // Show cache statistics
    let cache_stats = tree_tool.cache_stats()?;
    println!("  📊 Cache Statistics:");
    for (key, value) in cache_stats {
        println!("    - {}: {:?}", key, value);
    }

    println!("\n🎉 All examples completed successfully!");
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_multi_language_parsing() {
        let tool = TreeTool::new(IntelligenceLevel::Light).unwrap();

        let test_cases = vec![
            ("fn main() {}", SupportedLanguage::Rust),
            ("def main(): pass", SupportedLanguage::Python),
            ("function main() {}", SupportedLanguage::JavaScript),
            ("func main() {}", SupportedLanguage::Go),
            ("public class Main {}", SupportedLanguage::Java),
            ("int main() { return 0; }", SupportedLanguage::C),
        ];

        for (code, lang) in test_cases {
            let input = TreeInput::Parse {
                code: code.to_string(),
                language: Some(lang),
                file_path: None,
            };

            let result = tool.execute(input).await;
            assert!(
                result.is_ok(),
                "Failed to parse {} code: {}",
                lang.as_str(),
                code
            );

            if let Ok(output) = result {
                // Check for errors in diagnostics
                let has_errors = output
                    .diagnostics
                    .iter()
                    .any(|d| matches!(d.level, agcodex_core::tools::DiagnosticLevel::Error));
                assert!(!has_errors, "Parse had errors for {}", lang.as_str());
                match output.result {
                    TreeOutput::Parsed { has_errors, .. } => {
                        assert!(!has_errors, "Parse had errors for {}", lang.as_str());
                    }
                    _ => panic!("Expected Parsed output"),
                }
            }
        }
    }

    #[tokio::test]
    async fn test_query_functionality() {
        let tool = TreeTool::new(IntelligenceLevel::Medium).unwrap();

        let rust_code = r#"
            fn first() -> i32 { 1 }
            fn second() -> i32 { 2 }  
            fn third() -> i32 { 3 }
        "#;

        let input = TreeInput::Query {
            code: rust_code.to_string(),
            language: Some(SupportedLanguage::Rust),
            pattern: "(function_item name: (identifier) @func_name)".to_string(),
            file_path: None,
        };

        let result = tool.execute(input).await.unwrap();
        // Check for errors in diagnostics
        let has_errors = result
            .diagnostics
            .iter()
            .any(|d| matches!(d.level, agcodex_core::tools::DiagnosticLevel::Error));
        assert!(!has_errors, "Query execution had errors");

        match result.result {
            TreeOutput::QueryResults { total_matches, .. } => {
                assert_eq!(total_matches, 3, "Should find exactly 3 functions");
            }
            _ => panic!("Expected QueryResults output"),
        }
    }

    #[tokio::test]
    async fn test_caching_behavior() {
        let tool = TreeTool::new(IntelligenceLevel::Light).unwrap();
        let code = "fn test() -> i32 { 42 }".to_string();

        // Parse the same code twice
        let input1 = TreeInput::Parse {
            code: code.clone(),
            language: Some(SupportedLanguage::Rust),
            file_path: None,
        };

        let input2 = TreeInput::Parse {
            code,
            language: Some(SupportedLanguage::Rust),
            file_path: None,
        };

        let _result1 = tool.execute(input1).await.unwrap();
        let _result2 = tool.execute(input2).await.unwrap();

        // Check cache statistics
        let stats = tool.cache_stats().unwrap();
        let cache_size = stats.get("ast_cache_size").unwrap().as_i64().unwrap();
        assert_eq!(cache_size, 1, "Should have 1 cached entry");
    }
}