agentis-ctx 0.2.1

Fast CLI tool that generates AI-ready context from your codebase, with built-in code intelligence
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
517
518
519
520
521
522
523
524
//! Code parsing module using tree-sitter.
//!
//! This module extracts symbols and relationships from source code files
//! using tree-sitter grammars.

mod go;
mod python;
mod rust;
mod solidity;
mod typescript;

use std::path::Path;

use tree_sitter::{Node, Query, QueryCursor};

use crate::db::{Edge, EdgeKind, ParseResult, Symbol, SymbolKind};

/// Supported programming languages.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Language {
    Rust,
    TypeScript,
    Tsx,
    JavaScript,
    Jsx,
    Python,
    Go,
    Solidity,
    Yaml,
    Unknown,
}

impl Language {
    /// Detect language from file extension.
    pub fn from_path(path: &Path) -> Self {
        match path.extension().and_then(|e| e.to_str()) {
            Some("rs") => Language::Rust,
            Some("ts") => Language::TypeScript,
            Some("tsx") => Language::Tsx,
            Some("js") | Some("mjs") | Some("cjs") => Language::JavaScript,
            Some("jsx") => Language::Jsx,
            Some("py") | Some("pyi") => Language::Python,
            Some("go") => Language::Go,
            Some("sol") => Language::Solidity,
            Some("yaml") | Some("yml") => Language::Yaml,
            _ => Language::Unknown,
        }
    }

    /// Get the language name as a string.
    pub fn as_str(&self) -> &'static str {
        match self {
            Language::Rust => "rust",
            Language::TypeScript => "typescript",
            Language::Tsx => "tsx",
            Language::JavaScript => "javascript",
            Language::Jsx => "jsx",
            Language::Python => "python",
            Language::Go => "go",
            Language::Solidity => "solidity",
            Language::Yaml => "yaml",
            Language::Unknown => "unknown",
        }
    }
}

/// Code parser that extracts symbols and relationships.
pub struct CodeParser {
    go_parser: go::GoParser,
    python_parser: python::PythonParser,
    rust_parser: rust::RustParser,
    solidity_parser: solidity::SolidityParser,
    typescript_parser: typescript::TypeScriptParser,
}

impl CodeParser {
    /// Create a new code parser.
    pub fn new() -> Self {
        Self {
            go_parser: go::GoParser::new(),
            python_parser: python::PythonParser::new(),
            rust_parser: rust::RustParser::new(),
            solidity_parser: solidity::SolidityParser::new(),
            typescript_parser: typescript::TypeScriptParser::new(),
        }
    }

    /// Parse a source file and extract symbols/edges.
    pub fn parse(&mut self, path: &Path, source: &str) -> Option<ParseResult> {
        let language = Language::from_path(path);
        let file_path = path.to_string_lossy().to_string();

        match language {
            Language::Rust => self.rust_parser.parse(&file_path, source),
            Language::Solidity => self.solidity_parser.parse(&file_path, source),
            Language::TypeScript => {
                self.typescript_parser
                    .parse(&file_path, source, typescript::JsVariant::TypeScript)
            }
            Language::Tsx => {
                self.typescript_parser
                    .parse(&file_path, source, typescript::JsVariant::Tsx)
            }
            Language::JavaScript => {
                self.typescript_parser
                    .parse(&file_path, source, typescript::JsVariant::JavaScript)
            }
            Language::Jsx => {
                self.typescript_parser
                    .parse(&file_path, source, typescript::JsVariant::Jsx)
            }
            Language::Python => self.python_parser.parse(&file_path, source),
            Language::Go => self.go_parser.parse(&file_path, source),
            _ => {
                // Return a minimal result for unsupported languages
                Some(ParseResult {
                    file_path,
                    language: language.as_str().to_string(),
                    symbols: Vec::new(),
                    edges: Vec::new(),
                    module: None,
                })
            }
        }
    }

    /// Check if a language is supported for full parsing.
    /// Note: YAML is detected but not parsed (no tree-sitter grammar available).
    pub fn is_supported(&self, path: &Path) -> bool {
        Self::is_supported_static(path)
    }

    /// Static version of is_supported (for parallel indexing).
    pub fn is_supported_static(path: &Path) -> bool {
        matches!(
            Language::from_path(path),
            Language::Rust
                | Language::Solidity
                | Language::TypeScript
                | Language::Tsx
                | Language::JavaScript
                | Language::Jsx
                | Language::Python
                | Language::Go // Language::Yaml excluded - detected but not parsed
        )
    }
}

impl Default for CodeParser {
    fn default() -> Self {
        Self::new()
    }
}

/// Extract a brief description from a docstring.
pub fn extract_brief(docstring: &str) -> Option<String> {
    let trimmed = docstring.trim();
    if trimmed.is_empty() {
        return None;
    }

    // Take the first line or sentence
    let first_line = trimmed.lines().next()?;
    let brief = first_line.trim();

    // If it ends with a period, take it as-is
    if brief.ends_with('.') {
        return Some(brief.to_string());
    }

    // Otherwise, try to find the first sentence
    if let Some(idx) = brief.find(". ") {
        return Some(brief[..=idx].to_string());
    }

    Some(brief.to_string())
}

/// Truncate a string to a maximum length, adding "..." if truncated.
pub fn truncate_context(s: &str, max_len: usize) -> String {
    let s = s.trim();
    if s.len() <= max_len {
        s.to_string()
    } else {
        // Find a valid UTF-8 char boundary for truncation
        let target = max_len.saturating_sub(3);
        let mut end = target;
        while end > 0 && !s.is_char_boundary(end) {
            end -= 1;
        }
        format!("{}...", &s[..end])
    }
}

/// Get a snippet of context around a line.
#[allow(dead_code)]
pub fn get_context_snippet(source: &str, line: usize, col: usize) -> Option<String> {
    let lines: Vec<&str> = source.lines().collect();
    if line == 0 || line > lines.len() {
        return None;
    }

    let target_line = lines[line - 1];

    // Get a reasonable snippet (up to 80 chars)
    // Find valid UTF-8 char boundaries
    let mut start = col.saturating_sub(20);
    while start > 0 && !target_line.is_char_boundary(start) {
        start -= 1;
    }
    let mut end = (col + 60).min(target_line.len());
    while end < target_line.len() && !target_line.is_char_boundary(end) {
        end += 1;
    }

    let snippet = &target_line[start..end];
    Some(snippet.trim().to_string())
}

/// Maps a capture name prefix (e.g., "func", "class") to a SymbolKind.
///
/// This is used to reduce the complexity of extract_symbols functions.
/// Capture names follow the pattern "prefix.name" or "prefix.def".
pub struct SymbolKindMapping {
    /// The prefix to match (e.g., "func", "class", "method")
    pub prefix: &'static str,
    /// The SymbolKind to assign when this prefix is matched
    pub kind: SymbolKind,
}

impl SymbolKindMapping {
    /// Create a new mapping from prefix to kind.
    pub const fn new(prefix: &'static str, kind: SymbolKind) -> Self {
        Self { prefix, kind }
    }
}

/// Find the SymbolKind for a capture name based on a list of mappings.
///
/// Returns Some(kind) if the capture name matches "prefix.name" for any mapping,
/// or None if no match is found.
pub fn find_symbol_kind(capture_name: &str, mappings: &[SymbolKindMapping]) -> Option<SymbolKind> {
    if !capture_name.ends_with(".name") {
        return None;
    }
    let prefix = capture_name.trim_end_matches(".name");
    mappings.iter().find(|m| m.prefix == prefix).map(|m| m.kind)
}

/// Check if a capture name is a definition capture (ends with ".def").
pub fn is_def_capture(capture_name: &str) -> bool {
    capture_name.ends_with(".def")
}

/// Capture name patterns for call extraction.
/// Each tuple contains (name_patterns, expr_patterns) that the query captures should match.
pub struct CallCapturePatterns {
    /// Patterns that match the function/method name being called
    pub name_patterns: &'static [&'static str],
    /// Patterns that match the call expression node
    pub expr_patterns: &'static [&'static str],
}

impl CallCapturePatterns {
    /// Standard patterns for Python (call.name, method_call.name)
    pub const STANDARD: CallCapturePatterns = CallCapturePatterns {
        name_patterns: &["call.name", "method_call.name"],
        expr_patterns: &["call.expr", "method_call.expr"],
    };

    /// Rust patterns include scoped calls (e.g., module::function())
    pub const RUST: CallCapturePatterns = CallCapturePatterns {
        name_patterns: &["call.name", "method_call.name", "scoped_call.name"],
        expr_patterns: &["call.expr", "method_call.expr", "scoped_call.expr"],
    };

    /// TypeScript/JavaScript patterns include new expressions (e.g., new Class())
    pub const TYPESCRIPT: CallCapturePatterns = CallCapturePatterns {
        name_patterns: &["call.name", "method_call.name", "new.name"],
        expr_patterns: &["call.expr", "method_call.expr", "new.expr"],
    };
}

/// Extract call edges from an AST using a tree-sitter query.
///
/// This is a shared helper for all language parsers. The query should capture:
/// - Call/method names with patterns like "call.name", "method_call.name"
/// - Call expressions with patterns like "call.expr", "method_call.expr"
pub fn extract_call_edges(
    query: &Query,
    root: &Node,
    source: &str,
    symbols: &[Symbol],
    edges: &mut Vec<Edge>,
    patterns: &CallCapturePatterns,
) {
    // Build a map of function ranges to their symbol IDs
    let func_ranges: Vec<_> = symbols
        .iter()
        .filter(|s| matches!(s.kind, SymbolKind::Function | SymbolKind::Method))
        .map(|s| (s.line_start, s.line_end, s.id.clone()))
        .collect();

    let mut cursor = QueryCursor::new();
    let matches = cursor.matches(query, *root, source.as_bytes());

    for m in matches {
        let mut call_name: Option<&str> = None;
        let mut call_node: Option<Node> = None;

        for capture in m.captures {
            let capture_name = &query.capture_names()[capture.index as usize];
            let node = capture.node;
            let text = node.utf8_text(source.as_bytes()).unwrap_or("");

            if patterns.name_patterns.contains(&capture_name.as_str()) {
                call_name = Some(text);
            } else if patterns.expr_patterns.contains(&capture_name.as_str()) {
                call_node = Some(node);
            }
        }

        if let (Some(name), Some(node)) = (call_name, call_node) {
            let line = node.start_position().row as u32 + 1;
            let col = node.start_position().column as u32;

            // Find which function this call is in
            let source_id = func_ranges
                .iter()
                .find(|(start, end, _)| line >= *start && line <= *end)
                .map(|(_, _, id)| id.clone());

            if let Some(source_id) = source_id {
                let context = node
                    .utf8_text(source.as_bytes())
                    .ok()
                    .map(|s| truncate_context(s, 80));

                // Try to resolve the target only if the context contains the qualified name
                // (e.g., "TypeScriptParser::new()").
                //
                // We don't resolve based on "unique name in file" because:
                // 1. Calls like "Vec::new()" or "Parser::new()" would match local "new" functions
                // 2. Cross-file resolution in post-indexing phase is more accurate
                //
                // Common names that are almost always external are left unresolved here.
                let target_id = if let Some(ctx) = &context {
                    symbols
                        .iter()
                        .find(|s| {
                            s.name == name
                                && s.qualified_name
                                    .as_ref()
                                    .map(|qn| ctx.contains(qn))
                                    .unwrap_or(false)
                        })
                        .map(|s| s.id.clone())
                } else {
                    None
                };

                edges.push(Edge {
                    source_id,
                    target_id,
                    target_name: name.to_string(),
                    kind: EdgeKind::Calls,
                    line: Some(line),
                    col: Some(col),
                    context,
                });
            }
        }
    }
}

/// Extract module name from a file path.
///
/// This is a shared helper for all language parsers. The `index_names` parameter
/// specifies which file stems should use the parent directory name instead
/// (e.g., "index" for TypeScript, "__init__" for Python, "mod" for Rust).
pub fn extract_module_name(file_path: &str, index_names: &[&str]) -> Option<String> {
    let path = std::path::Path::new(file_path);
    let stem = path.file_stem()?.to_str()?;

    if index_names.contains(&stem) {
        // Use parent directory name for index/entry files
        path.parent()?.file_name()?.to_str().map(String::from)
    } else {
        Some(stem.to_string())
    }
}

/// Parse a block doc comment (/** ... */ or /*! ... */) into clean content.
///
/// This is a shared helper for extracting doc comments in JSDoc, NatSpec, and Rust doc styles.
/// It strips the comment delimiters and leading asterisks from each line.
pub fn parse_block_doc_comment(text: &str) -> String {
    // Strip the opening delimiter (/** or /*!)
    let content = text
        .trim_start_matches("/**")
        .trim_start_matches("/*!")
        .trim_end_matches("*/");

    // Process each line: strip leading whitespace and asterisks
    content
        .lines()
        .map(|l| l.trim().trim_start_matches('*').trim())
        .filter(|l| !l.is_empty())
        .collect::<Vec<_>>()
        .join("\n")
}

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

    #[test]
    fn test_language_detection() {
        assert_eq!(Language::from_path(Path::new("main.rs")), Language::Rust);
        assert_eq!(
            Language::from_path(Path::new("app.ts")),
            Language::TypeScript
        );
        assert_eq!(Language::from_path(Path::new("App.tsx")), Language::Tsx);
        assert_eq!(
            Language::from_path(Path::new("script.js")),
            Language::JavaScript
        );
        assert_eq!(Language::from_path(Path::new("Button.jsx")), Language::Jsx);
        assert_eq!(Language::from_path(Path::new("main.py")), Language::Python);
        assert_eq!(Language::from_path(Path::new("main.go")), Language::Go);
        assert_eq!(
            Language::from_path(Path::new("Token.sol")),
            Language::Solidity
        );
        assert_eq!(
            Language::from_path(Path::new("config.yaml")),
            Language::Yaml
        );
        assert_eq!(Language::from_path(Path::new("ci.yml")), Language::Yaml);
        assert_eq!(
            Language::from_path(Path::new("data.json")),
            Language::Unknown
        );
    }

    #[test]
    fn test_extract_brief() {
        // Multi-line extracts first line
        assert_eq!(
            extract_brief("This is a brief.\nMore details here."),
            Some("This is a brief.".to_string())
        );
        assert_eq!(
            extract_brief("Single line"),
            Some("Single line".to_string())
        );
        assert_eq!(extract_brief(""), None);
    }

    #[test]
    fn test_truncate_context_ascii() {
        // Short string - no truncation
        assert_eq!(truncate_context("hello", 10), "hello");

        // Exact length - no truncation
        assert_eq!(truncate_context("hello", 5), "hello");

        // Needs truncation
        assert_eq!(truncate_context("hello world", 8), "hello...");

        // With leading/trailing whitespace
        assert_eq!(truncate_context("  hello world  ", 8), "hello...");
    }

    #[test]
    fn test_truncate_context_unicode() {
        // Box drawing characters (3 bytes each: ─ is \xe2\x94\x80)
        let box_line = "┌────────────────────────────────────────────────────────┐";

        // Should not panic on Unicode
        let result = truncate_context(box_line, 20);
        assert!(result.ends_with("..."));
        assert!(result.len() <= 20);

        // Emoji (4 bytes each)
        let emoji_str = "Hello 🎉🎊🎁 World";
        let result = truncate_context(emoji_str, 12);
        assert!(result.ends_with("..."));

        // Mixed content
        let mixed = "console.log(\"├──────┤\")";
        let result = truncate_context(mixed, 15);
        assert!(result.ends_with("..."));

        // Chinese characters (3 bytes each)
        let chinese = "你好世界这是一个测试";
        let result = truncate_context(chinese, 10);
        assert!(result.ends_with("..."));
    }

    #[test]
    fn test_truncate_context_edge_cases() {
        // Very short max_len
        assert_eq!(truncate_context("hello", 3), "...");
        assert_eq!(truncate_context("hi", 3), "hi");

        // Empty string
        assert_eq!(truncate_context("", 10), "");

        // Only whitespace
        assert_eq!(truncate_context("   ", 10), "");
    }

    #[test]
    fn test_get_context_snippet_unicode() {
        // Source with Unicode box drawing
        let source = "line1\nconsole.log(\"┌────────────────────┐\")\nline3";

        // Should not panic when getting snippet from line with Unicode
        let result = get_context_snippet(source, 2, 15);
        assert!(result.is_some());
    }
}