a3s-code-core 1.9.3

A3S Code Core - Embeddable AI agent library with tool execution
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
//! Ripgrep Context Provider
//!
//! Provides indexless, real-time code search using ripgrep-style pattern matching.
//! Unlike vector-based RAG, this approach:
//! - Requires no pre-indexing or embedding generation
//! - Works directly on raw source files
//! - Supports real-time code changes without re-indexing
//! - Uses fast regex-based search with relevance scoring

use crate::context::{ContextItem, ContextProvider, ContextQuery, ContextResult, ContextType};
use async_trait::async_trait;
use ignore::WalkBuilder;
use regex::Regex;
use std::fs;
use std::path::{Path, PathBuf};

/// Ripgrep context provider configuration
#[derive(Debug, Clone)]
pub struct RipgrepContextConfig {
    /// Root directory to search
    pub root_path: PathBuf,
    /// Include patterns (glob syntax: ["**/*.rs", "**/*.md"])
    pub include_patterns: Vec<String>,
    /// Exclude patterns (glob syntax: ["**/target/**", "**/node_modules/**"])
    pub exclude_patterns: Vec<String>,
    /// Maximum file size in bytes (default: 1MB)
    pub max_file_size: usize,
    /// Case insensitive search (default: true)
    pub case_insensitive: bool,
    /// Number of context lines before/after match (default: 2)
    pub context_lines: usize,
}

impl RipgrepContextConfig {
    /// Create a new config with default settings
    pub fn new(root_path: impl Into<PathBuf>) -> Self {
        Self {
            root_path: root_path.into(),
            include_patterns: vec![
                "**/*.rs".to_string(),
                "**/*.py".to_string(),
                "**/*.ts".to_string(),
                "**/*.tsx".to_string(),
                "**/*.js".to_string(),
                "**/*.jsx".to_string(),
                "**/*.go".to_string(),
                "**/*.java".to_string(),
                "**/*.c".to_string(),
                "**/*.cpp".to_string(),
                "**/*.h".to_string(),
                "**/*.hpp".to_string(),
                "**/*.md".to_string(),
                "**/*.toml".to_string(),
                "**/*.yaml".to_string(),
                "**/*.yml".to_string(),
                "**/*.json".to_string(),
            ],
            exclude_patterns: vec![
                "**/target/**".to_string(),
                "**/node_modules/**".to_string(),
                "**/.git/**".to_string(),
                "**/dist/**".to_string(),
                "**/build/**".to_string(),
                "**/*.lock".to_string(),
                "**/vendor/**".to_string(),
                "**/__pycache__/**".to_string(),
            ],
            max_file_size: 1024 * 1024, // 1MB
            case_insensitive: true,
            context_lines: 2,
        }
    }

    /// Set include patterns
    pub fn with_include_patterns(mut self, patterns: Vec<String>) -> Self {
        self.include_patterns = patterns;
        self
    }

    /// Set exclude patterns
    pub fn with_exclude_patterns(mut self, patterns: Vec<String>) -> Self {
        self.exclude_patterns = patterns;
        self
    }

    /// Set max file size
    pub fn with_max_file_size(mut self, size: usize) -> Self {
        self.max_file_size = size;
        self
    }

    /// Set case sensitivity
    pub fn with_case_insensitive(mut self, enabled: bool) -> Self {
        self.case_insensitive = enabled;
        self
    }

    /// Set context lines
    pub fn with_context_lines(mut self, lines: usize) -> Self {
        self.context_lines = lines;
        self
    }
}

/// Search result from a single file
#[derive(Debug, Clone)]
struct FileMatch {
    path: PathBuf,
    matches: Vec<MatchResult>,
    relevance: f32,
}

/// A single match within a file
#[derive(Debug, Clone)]
struct MatchResult {
    line_number: usize,
    line_content: String,
    context_before: Vec<String>,
    context_after: Vec<String>,
}

/// Ripgrep context provider
pub struct RipgrepContextProvider {
    config: RipgrepContextConfig,
}

impl RipgrepContextProvider {
    /// Create a new ripgrep context provider
    pub fn new(config: RipgrepContextConfig) -> Self {
        Self { config }
    }

    /// Search files for pattern matches
    async fn search_files(
        &self,
        query: &str,
        max_results: usize,
    ) -> anyhow::Result<Vec<FileMatch>> {
        let root = self.config.root_path.clone();
        let max_file_size = self.config.max_file_size;
        let include = self.config.include_patterns.clone();
        let exclude = self.config.exclude_patterns.clone();
        let case_insensitive = self.config.case_insensitive;
        let context_lines = self.config.context_lines;
        let query = query.to_string();

        // Run search in blocking task
        tokio::task::spawn_blocking(move || {
            // Build regex pattern
            let pattern = if case_insensitive {
                format!("(?i){}", regex::escape(&query))
            } else {
                regex::escape(&query)
            };

            let regex = Regex::new(&pattern)?;

            let mut file_matches = Vec::new();

            let walker = WalkBuilder::new(&root)
                .hidden(false)
                .git_ignore(true)
                .build();

            for entry in walker {
                let entry = entry.map_err(|e| anyhow::anyhow!("Walk error: {}", e))?;
                let path = entry.path();

                if !path.is_file() {
                    continue;
                }

                let metadata = fs::metadata(path)
                    .map_err(|e| anyhow::anyhow!("Metadata error for {}: {}", path.display(), e))?;

                if metadata.len() > max_file_size as u64 {
                    continue;
                }

                if !matches_patterns(path, &include, true) {
                    continue;
                }

                if matches_patterns(path, &exclude, false) {
                    continue;
                }

                let content = match fs::read_to_string(path) {
                    Ok(c) => c,
                    Err(_) => continue, // Skip binary files
                };

                if content.trim().is_empty() {
                    continue;
                }

                // Search for matches in this file
                let lines: Vec<&str> = content.lines().collect();
                let mut matches = Vec::new();

                for (line_idx, line) in lines.iter().enumerate() {
                    if regex.is_match(line) {
                        let context_before = if line_idx >= context_lines {
                            lines[line_idx - context_lines..line_idx]
                                .iter()
                                .map(|s| s.to_string())
                                .collect()
                        } else {
                            lines[0..line_idx].iter().map(|s| s.to_string()).collect()
                        };

                        let context_after = if line_idx + context_lines < lines.len() {
                            lines[line_idx + 1..=line_idx + context_lines]
                                .iter()
                                .map(|s| s.to_string())
                                .collect()
                        } else {
                            lines[line_idx + 1..]
                                .iter()
                                .map(|s| s.to_string())
                                .collect()
                        };

                        matches.push(MatchResult {
                            line_number: line_idx + 1,
                            line_content: line.to_string(),
                            context_before,
                            context_after,
                        });
                    }
                }

                if !matches.is_empty() {
                    // Calculate relevance score based on match count and file size
                    let relevance = (matches.len() as f32) / (lines.len() as f32).sqrt();

                    file_matches.push(FileMatch {
                        path: path.to_path_buf(),
                        matches,
                        relevance,
                    });
                }
            }

            // Sort by relevance (descending)
            file_matches.sort_by(|a, b| {
                b.relevance
                    .partial_cmp(&a.relevance)
                    .unwrap_or(std::cmp::Ordering::Equal)
            });
            file_matches.truncate(max_results);

            Ok::<_, anyhow::Error>(file_matches)
        })
        .await
        .map_err(|e| anyhow::anyhow!("Spawn blocking failed: {}", e))?
    }

    /// Format match results into context content
    fn format_match(&self, file_match: &FileMatch, depth: &crate::context::ContextDepth) -> String {
        let mut output = String::new();
        let path_str = file_match.path.display().to_string();

        match depth {
            crate::context::ContextDepth::Abstract => {
                // Just show file path and match count
                output.push_str(&format!(
                    "{}: {} matches\n",
                    path_str,
                    file_match.matches.len()
                ));
            }
            crate::context::ContextDepth::Overview => {
                // Show first few matches with limited context
                output.push_str(&format!("{}:\n", path_str));
                for (idx, m) in file_match.matches.iter().take(3).enumerate() {
                    if idx > 0 {
                        output.push('\n');
                    }
                    output.push_str(&format!("  Line {}:\n", m.line_number));
                    output.push_str(&format!("    {}\n", m.line_content));
                }
                if file_match.matches.len() > 3 {
                    output.push_str(&format!(
                        "  ... and {} more matches\n",
                        file_match.matches.len() - 3
                    ));
                }
            }
            crate::context::ContextDepth::Full => {
                // Show all matches with full context
                output.push_str(&format!("{}:\n", path_str));
                for (idx, m) in file_match.matches.iter().enumerate() {
                    if idx > 0 {
                        output.push('\n');
                    }
                    output.push_str(&format!("  Line {}:\n", m.line_number));
                    for ctx in &m.context_before {
                        output.push_str(&format!("    {}\n", ctx));
                    }
                    output.push_str(&format!("  > {}\n", m.line_content));
                    for ctx in &m.context_after {
                        output.push_str(&format!("    {}\n", ctx));
                    }
                }
            }
        }

        output
    }
}

#[async_trait]
impl ContextProvider for RipgrepContextProvider {
    fn name(&self) -> &str {
        "ripgrep"
    }

    async fn query(&self, query: &ContextQuery) -> anyhow::Result<ContextResult> {
        let file_matches = self.search_files(&query.query, query.max_results).await?;

        let mut result = ContextResult::new("ripgrep");
        let mut total_tokens = 0usize;

        for file_match in file_matches {
            if total_tokens >= query.max_tokens {
                result.truncated = true;
                break;
            }

            let content = self.format_match(&file_match, &query.depth);
            let token_count = content.split_whitespace().count();

            if total_tokens + token_count > query.max_tokens {
                result.truncated = true;
                break;
            }

            total_tokens += token_count;

            result.add_item(
                ContextItem::new(
                    file_match.path.to_string_lossy().to_string(),
                    ContextType::Resource,
                    content,
                )
                .with_token_count(token_count)
                .with_relevance(file_match.relevance)
                .with_source(format!("file:{}", file_match.path.display()))
                .with_metadata("match_count", serde_json::json!(file_match.matches.len())),
            );
        }

        Ok(result)
    }
}

// ============================================================================
// Helpers
// ============================================================================

/// Check if a path matches any of the given glob patterns
fn matches_patterns(path: &Path, patterns: &[String], default_if_empty: bool) -> bool {
    if patterns.is_empty() {
        return default_if_empty;
    }
    let path_str = path.to_string_lossy().replace('\\', "/");
    patterns.iter().any(|pattern| {
        glob::Pattern::new(pattern)
            .map(|p| p.matches(&path_str))
            .unwrap_or(false)
    })
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs::File;
    use std::io::Write;
    use tempfile::TempDir;

    fn setup_test_workspace() -> TempDir {
        let dir = TempDir::new().unwrap();
        let root = dir.path();

        // Create test files
        let mut f1 = File::create(root.join("main.rs")).unwrap();
        writeln!(f1, "fn main() {{\n    println!(\"Hello, world!\");\n}}").unwrap();

        let mut f2 = File::create(root.join("lib.rs")).unwrap();
        writeln!(
            f2,
            "pub mod auth;\npub mod database;\n\npub fn init() -> Result<()> {{\n    Ok(())\n}}"
        )
        .unwrap();

        let mut f3 = File::create(root.join("README.md")).unwrap();
        writeln!(
            f3,
            "# My Project\n\nA Rust project for testing ripgrep context."
        )
        .unwrap();

        std::fs::create_dir(root.join("src")).unwrap();
        let mut f4 = File::create(root.join("src/auth.rs")).unwrap();
        writeln!(
            f4,
            "use jwt::Token;\n\npub fn verify_token(token: &str) -> Result<Claims> {{\n    // JWT verification logic\n    todo!()\n}}"
        )
        .unwrap();

        dir
    }

    #[test]
    fn test_config_defaults() {
        let config = RipgrepContextConfig::new("/tmp/test");
        assert_eq!(config.root_path, PathBuf::from("/tmp/test"));
        assert!(!config.include_patterns.is_empty());
        assert!(!config.exclude_patterns.is_empty());
        assert_eq!(config.max_file_size, 1024 * 1024);
        assert!(config.case_insensitive);
        assert_eq!(config.context_lines, 2);
    }

    #[test]
    fn test_config_builders() {
        let config = RipgrepContextConfig::new("/tmp")
            .with_include_patterns(vec!["**/*.rs".to_string()])
            .with_exclude_patterns(vec!["**/test/**".to_string()])
            .with_max_file_size(2048)
            .with_case_insensitive(false)
            .with_context_lines(5);

        assert_eq!(config.include_patterns, vec!["**/*.rs"]);
        assert_eq!(config.exclude_patterns, vec!["**/test/**"]);
        assert_eq!(config.max_file_size, 2048);
        assert!(!config.case_insensitive);
        assert_eq!(config.context_lines, 5);
    }

    #[tokio::test]
    async fn test_provider_search() {
        let dir = setup_test_workspace();
        let config = RipgrepContextConfig::new(dir.path());
        let provider = RipgrepContextProvider::new(config);

        let query = ContextQuery::new("Rust");
        let result = provider.query(&query).await.unwrap();

        assert_eq!(result.provider, "ripgrep");
        assert!(!result.items.is_empty());
        // Should find "Rust" in README.md
        assert!(result
            .items
            .iter()
            .any(|item| item.content.contains("Rust")));
    }

    #[tokio::test]
    async fn test_provider_case_insensitive() {
        let dir = setup_test_workspace();
        let config = RipgrepContextConfig::new(dir.path()).with_case_insensitive(true);
        let provider = RipgrepContextProvider::new(config);

        let query = ContextQuery::new("RUST");
        let result = provider.query(&query).await.unwrap();

        assert!(!result.items.is_empty());
    }

    #[tokio::test]
    async fn test_provider_max_results() {
        let dir = setup_test_workspace();
        let config = RipgrepContextConfig::new(dir.path());
        let provider = RipgrepContextProvider::new(config);

        let query = ContextQuery::new("fn").with_max_results(1);
        let result = provider.query(&query).await.unwrap();

        assert!(result.items.len() <= 1);
    }

    #[tokio::test]
    async fn test_provider_name() {
        let dir = TempDir::new().unwrap();
        let config = RipgrepContextConfig::new(dir.path());
        let provider = RipgrepContextProvider::new(config);
        assert_eq!(provider.name(), "ripgrep");
    }

    #[test]
    fn test_matches_patterns_empty_default_true() {
        assert!(matches_patterns(Path::new("test.rs"), &[], true));
    }

    #[test]
    fn test_matches_patterns_empty_default_false() {
        assert!(!matches_patterns(Path::new("test.rs"), &[], false));
    }

    #[test]
    fn test_matches_patterns_include() {
        let patterns = vec!["**/*.rs".to_string()];
        assert!(matches_patterns(Path::new("src/main.rs"), &patterns, false));
        assert!(!matches_patterns(
            Path::new("src/main.py"),
            &patterns,
            false
        ));
    }
}