loregrep 0.6.0

Repository indexing library for AI coding assistants. Tree-sitter parsing, fast in-memory indexing, and tool APIs for LLM 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
#!/usr/bin/env python3
"""
Enhanced API usage example for the loregrep Python package.

This example demonstrates the enhanced Python API of loregrep with real-time feedback
and convenient setup options for repository indexing and code analysis. It covers:

1. Importing and checking the loregrep package
2. Getting available AI tools for code analysis
3. Three enhanced setup options:
   - Auto-discovery (zero configuration)
   - Enhanced builder with convenience methods
   - Project-specific presets
4. Scanning a sample repository with multiple file types
5. Executing various analysis tools (search_functions, search_structs, etc.)

Prerequisites:
- Install with: maturin develop --features python
- Or build wheel with: maturin build --features python --release

The example creates temporary files for demonstration and cleans them up afterwards.
"""

import os
import tempfile
import asyncio
from pathlib import Path


async def main():
    """Main example function."""
    print("Loregrep Python Package - Builder Pattern API Example")
    print("=" * 55)
    
    try:
        import loregrep
        print(f"โœ… Successfully imported loregrep v{loregrep.__version__}")
    except ImportError as e:
        print(f"โŒ Failed to import loregrep: {e}")
        print("Make sure to build the package with: maturin develop --features python")
        return
    
    # Show available tools
    print("\n1. Available AI Tools:")
    tools = loregrep.LoreGrep.get_tool_definitions()
    for i, tool in enumerate(tools, 1):
        print(f"   {i}. {tool.name}: {tool.description}")
    
    # Create a LoreGrep instance using the enhanced API
    print("\n2. Creating LoreGrep instance with enhanced API...")
    
    # Demo 1: Zero-configuration auto-discovery
    print("   Option 1: Auto-discovery (zero configuration)")
    try:
        auto_loregrep = loregrep.LoreGrep.auto_discover(".")
        print("   โœ… Auto-discovery instance created successfully")
        print(f"      ๐Ÿ” Detected project languages and configured analyzers")
    except Exception as e:
        print(f"   โš ๏ธ  Auto-discovery failed: {e}")
        auto_loregrep = None
    
    # Demo 2: Enhanced builder with convenience methods
    print("\n   Option 2: Enhanced builder with convenience methods")
    try:
        enhanced_loregrep = (loregrep.LoreGrep.builder()
                           .with_rust_analyzer()         # โœ… Real-time feedback
                           .with_python_analyzer()       # โœ… Registration confirmation  
                           .optimize_for_performance()   # ๐Ÿš€ Speed-optimized preset
                           .exclude_test_dirs()          # ๐Ÿšซ Skip test directories
                           .build())                     # ๐ŸŽ† Configuration summary
        print("   โœ… Enhanced builder instance created successfully")
    except Exception as e:
        print(f"   โš ๏ธ  Enhanced builder failed: {e}")
        enhanced_loregrep = None
    
    # Demo 3: Project-specific presets
    print("\n   Option 3: Project-specific presets")
    try:
        preset_loregrep = loregrep.LoreGrep.polyglot_project(".")  # Multi-language preset
        print("   โœ… Polyglot project preset created successfully")
    except Exception as e:
        print(f"   โš ๏ธ  Project preset failed: {e}")
        preset_loregrep = None
    
    # Use the first successful instance for the rest of the demo
    loregrep_instance = auto_loregrep or enhanced_loregrep or preset_loregrep
    if not loregrep_instance:
        print("โŒ Failed to create any LoreGrep instance")
        return
        
    print(f"\n   ๐Ÿ“‹ Using instance: {loregrep_instance}")
    
    # Create some sample files for demonstration
    print("\n3. Creating sample repository...")
    temp_dir = create_sample_repository()
    
    try:
        # Scan the sample repository
        print(f"\n4. Scanning repository at {temp_dir}...")
        result = await loregrep_instance.scan(temp_dir)
        
        print("โœ… Repository scan completed!")
        print(f"   ๐Ÿ“ Files scanned: {result.files_scanned}")
        print(f"   ๐Ÿ”ง Functions found: {result.functions_found}")
        print(f"   ๐Ÿ“ฆ Structs found: {result.structs_found}")
        print(f"   โฑ๏ธ  Duration: {result.duration_ms}ms")
        
        # Demonstrate tool execution (if available)
        if len(tools) > 0:
            print(f"\n5. Demonstrating tool execution...")
            
            # Test search_functions tool
            try:
                print("   ๐Ÿ” Testing search_functions tool...")
                func_result = await loregrep_instance.execute_tool("search_functions", {
                    "pattern": "Config",
                    "limit": 5
                })
                print(f"   โœ… search_functions executed successfully")
                print(f"   ๐Ÿ“„ Functions found:")
                print(f"   {func_result.content}")
                
            except Exception as e:
                print(f"   โš ๏ธ  search_functions demo failed: {e}")
            
            # Test search_structs tool
            try:
                print("\n   ๐Ÿ” Testing search_structs tool...")
                struct_result = await loregrep_instance.execute_tool("search_structs", {
                    "pattern": "Config",
                    "limit": 5
                })
                print(f"   โœ… search_structs executed successfully")
                print(f"   ๐Ÿ“ฆ Structs found:")
                print(f"   {struct_result.content}")
                
            except Exception as e:
                print(f"   โš ๏ธ  search_structs demo failed: {e}")
            
            # Test get_repository_tree tool
            try:
                print("\n   ๐ŸŒณ Testing get_repository_tree tool...")
                tree_result = await loregrep_instance.execute_tool("get_repository_tree", {
                    "include_file_details": True,
                    "max_depth": 2
                })
                print(f"   โœ… get_repository_tree executed successfully")
                print(f"   ๐Ÿ—๏ธ  Repository structure:")
                print(f"   {tree_result.content}")
                
            except Exception as e:
                print(f"   โš ๏ธ  get_repository_tree demo failed: {e}")
            
            # Test analyze_file tool (if we have Rust files)
            try:
                rust_file_path = os.path.join(temp_dir, "config.rs")
                if os.path.exists(rust_file_path):
                    print("\n   ๐Ÿ“„ Testing analyze_file tool...")
                    analyze_result = await loregrep_instance.execute_tool("analyze_file", {
                        "file_path": rust_file_path,
                        "include_source": False
                    })
                    print(f"   โœ… analyze_file executed successfully")
                    print(f"   ๐Ÿ” File analysis (config.rs):")
                    # Parse and display the JSON result in a more readable way
                    import json
                    try:
                        analysis_data = json.loads(analyze_result.content)
                        if "functions" in analysis_data:
                            print(f"   ๐Ÿ“‹ Functions: {len(analysis_data['functions'])}")
                            for func in analysis_data['functions'][:3]:  # Show first 3
                                print(f"     โ€ข {func.get('name', 'unknown')} (line {func.get('line_number', '?')})")
                        if "structs" in analysis_data:
                            print(f"   ๐Ÿ“ฆ Structs: {len(analysis_data['structs'])}")
                            for struct in analysis_data['structs'][:3]:  # Show first 3
                                print(f"     โ€ข {struct.get('name', 'unknown')} (line {struct.get('line_number', '?')})")
                    except json.JSONDecodeError:
                        # If it's not JSON, just show the raw content (truncated)
                        content_preview = analyze_result.content[:200] + "..." if len(analyze_result.content) > 200 else analyze_result.content
                        print(f"   {content_preview}")
                
            except Exception as e:
                print(f"   โš ๏ธ  analyze_file demo failed: {e}")
        
    except Exception as e:
        print(f"โŒ Error during scanning: {e}")
    
    finally:
        # Clean up sample files
        cleanup_sample_repository(temp_dir)
        print("\n๐Ÿงน Sample repository cleaned up")


def create_sample_repository():
    """Create a temporary sample repository with various file types."""
    temp_dir = tempfile.mkdtemp(prefix="loregrep_example_")
    base_path = Path(temp_dir)
    
    # Python file
    python_file = base_path / "calculator.py"
    python_file.write_text('''
"""
A simple calculator module demonstrating Python code parsing.
"""

def fibonacci(n):
    """Calculate the nth Fibonacci number."""
    if n <= 1:
        return n
    return fibonacci(n-1) + fibonacci(n-2)

class Calculator:
    """A simple calculator class."""
    
    def __init__(self):
        self.history = []
    
    def add(self, a, b):
        """Add two numbers."""
        result = a + b
        self.history.append(f"{a} + {b} = {result}")
        return result
    
    def multiply(self, a, b):
        """Multiply two numbers."""
        result = a * b
        self.history.append(f"{a} * {b} = {result}")
        return result
    
    def get_history(self):
        """Get calculation history."""
        return self.history.copy()
''')
    
    # JavaScript file
    js_file = base_path / "utils.js"
    js_file.write_text('''
/**
 * Utility functions for various operations.
 */

function calculateSum(numbers) {
    return numbers.reduce((sum, num) => sum + num, 0);
}

function findMax(numbers) {
    return Math.max(...numbers);
}

class UserManager {
    constructor() {
        this.users = new Map();
    }
    
    addUser(id, name, email) {
        this.users.set(id, { name, email, createdAt: new Date() });
    }
    
    getUser(id) {
        return this.users.get(id);
    }
    
    getAllUsers() {
        return Array.from(this.users.values());
    }
    
    deleteUser(id) {
        return this.users.delete(id);
    }
}

export { calculateSum, findMax, UserManager };
''')
    
    # Rust file
    rust_file = base_path / "config.rs"
    rust_file.write_text('''
//! Configuration management module
//! 
//! This module provides utilities for handling application configuration.

use std::collections::HashMap;
use std::fs;
use std::path::Path;

#[derive(Debug, Clone)]
pub struct Config {
    pub name: String,
    pub values: HashMap<String, String>,
}

impl Config {
    pub fn new(name: &str) -> Self {
        Self {
            name: name.to_string(),
            values: HashMap::new(),
        }
    }
    
    pub fn set(&mut self, key: &str, value: &str) {
        self.values.insert(key.to_string(), value.to_string());
    }
    
    pub fn get(&self, key: &str) -> Option<&String> {
        self.values.get(key)
    }
    
    pub fn load_from_file<P: AsRef<Path>>(path: P) -> Result<Self, Box<dyn std::error::Error>> {
        let content = fs::read_to_string(path)?;
        // Simple key=value parser
        let mut config = Config::new("loaded");
        
        for line in content.lines() {
            if let Some((key, value)) = line.split_once('=') {
                config.set(key.trim(), value.trim());
            }
        }
        
        Ok(config)
    }
}

pub fn process_data(data: &[i32]) -> Vec<i32> {
    data.iter()
        .filter(|&&x| x > 0)
        .map(|&x| x * 2)
        .collect()
}

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

    #[test]
    fn test_config_creation() {
        let config = Config::new("test");
        assert_eq!(config.name, "test");
        assert!(config.values.is_empty());
    }
    
    #[test]
    fn test_process_data() {
        let input = vec![-1, 0, 1, 2, 3];
        let output = process_data(&input);
        assert_eq!(output, vec![2, 4, 6]);
    }
}
''')
    
    # Create a subdirectory with more files
    subdir = base_path / "lib"
    subdir.mkdir()
    
    # TypeScript file in subdirectory
    ts_file = subdir / "types.ts"
    ts_file.write_text('''
/**
 * Type definitions for the application
 */

export interface User {
    id: number;
    name: string;
    email: string;
    createdAt: Date;
}

export interface Config {
    apiUrl: string;
    timeout: number;
    retries: number;
}

export class ApiClient {
    private config: Config;
    
    constructor(config: Config) {
        this.config = config;
    }
    
    async fetchUser(id: number): Promise<User | null> {
        try {
            const response = await fetch(`${this.config.apiUrl}/users/${id}`);
            if (!response.ok) {
                throw new Error(`HTTP ${response.status}`);
            }
            return await response.json();
        } catch (error) {
            console.error('Failed to fetch user:', error);
            return null;
        }
    }
    
    async createUser(userData: Omit<User, 'id' | 'createdAt'>): Promise<User | null> {
        try {
            const response = await fetch(`${this.config.apiUrl}/users`, {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify(userData)
            });
            
            if (!response.ok) {
                throw new Error(`HTTP ${response.status}`);
            }
            
            return await response.json();
        } catch (error) {
            console.error('Failed to create user:', error);
            return null;
        }
    }
}

export enum Status {
    Pending = 'pending',
    Success = 'success',
    Error = 'error'
}
''')
    
    return temp_dir


def cleanup_sample_repository(temp_dir):
    """Clean up the temporary sample repository."""
    import shutil
    try:
        shutil.rmtree(temp_dir)
    except OSError:
        pass  # Ignore cleanup errors


if __name__ == "__main__":
    asyncio.run(main())