matrixcode-core 0.4.5

MatrixCode Agent Core - Pure logic, no UI
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
use anyhow::Result;
use async_trait::async_trait;
use serde_json::{Value, json};

use super::{Tool, ToolDefinition};

/// Grep search options bundled into a single struct.
struct GrepOptions {
    pattern: String,
    path: String,
    glob_pattern: Option<String>,
    file_type: Option<String>,
    output_mode: String,
    case_insensitive: bool,
    show_line_numbers: bool,
    context_lines: usize,
    head_limit: usize,
}

impl GrepOptions {
    fn from_params(params: &Value) -> Result<Self> {
        let pattern = params["pattern"]
            .as_str()
            .ok_or_else(|| anyhow::anyhow!("missing 'pattern'"))?
            .to_string();
        let path = params["path"].as_str().unwrap_or(".").to_string();
        let glob_pattern = params["glob"].as_str().map(|s| s.to_string());
        let file_type = params["type"].as_str().map(|s| s.to_string());
        let output_mode = params["output_mode"]
            .as_str()
            .unwrap_or("content")
            .to_string();
        let case_insensitive = params["-i"].as_bool().unwrap_or(false);
        let show_line_numbers = params["-n"].as_bool().unwrap_or(true);
        let context_lines = params["-C"].as_u64().unwrap_or(0) as usize;
        let head_limit = params["head_limit"].as_u64().unwrap_or(100) as usize;

        Ok(Self {
            pattern,
            path,
            glob_pattern,
            file_type,
            output_mode,
            case_insensitive,
            show_line_numbers,
            context_lines,
            head_limit,
        })
    }
}

/// High-performance grep tool with advanced filtering options
pub struct GrepTool;

#[async_trait]
impl Tool for GrepTool {
    fn definition(&self) -> ToolDefinition {
        ToolDefinition {
            name: "grep".to_string(),
            description: "高性能内容搜索工具,适用于任意规模代码库。支持正则表达式、文件类型过滤和多种输出模式。".to_string(),
            parameters: json!({
                "type": "object",
                "properties": {
                    "pattern": {
                        "type": "string",
                        "description": "要搜索的正则表达式模式"
                    },
                    "path": {
                        "type": "string",
                        "description": "搜索的文件或目录(默认当前目录)"
                    },
                    "glob": {
                        "type": "string",
                        "description": "Glob 文件过滤模式(如 '*.ts'、'**/*.rs')"
                    },
                    "type": {
                        "type": "string",
                        "enum": ["js", "ts", "py", "rs", "go", "java", "c", "cpp", "md", "json", "yaml", "html", "css"],
                        "description": "按文件类型搜索(映射到常用扩展名)"
                    },
                    "output_mode": {
                        "type": "string",
                        "enum": ["content", "files_with_matches", "count"],
                        "default": "content",
                        "description": "输出模式:'content' 显示匹配行,'files_with_matches' 列出文件,'count' 显示匹配数"
                    },
                    "-i": {
                        "type": "boolean",
                        "default": false,
                        "description": "忽略大小写"
                    },
                    "-n": {
                        "type": "boolean",
                        "default": true,
                        "description": "显示行号"
                    },
                    "-C": {
                        "type": "integer",
                        "default": 0,
                        "description": "匹配行前后显示的上下文行数"
                    },
                    "head_limit": {
                        "type": "integer",
                        "default": 100,
                        "description": "最大返回结果数"
                    }
                },
                "required": ["pattern"]
            }),
        }
    }

    async fn execute(&self, params: Value) -> Result<String> {
        let opts = GrepOptions::from_params(&params)?;

        tokio::task::spawn_blocking(move || grep_search(&opts)).await?
    }
}

/// File type to extension mapping
fn get_extensions_for_type(file_type: &str) -> Vec<&'static str> {
    match file_type {
        "js" => vec!["js", "jsx", "mjs", "cjs"],
        "ts" => vec!["ts", "tsx", "mts", "cts"],
        "py" => vec!["py", "pyw", "pyi"],
        "rs" => vec!["rs"],
        "go" => vec!["go"],
        "java" => vec!["java"],
        "c" => vec!["c", "h"],
        "cpp" => vec!["cpp", "cc", "cxx", "hpp", "hh", "hxx"],
        "md" => vec!["md", "markdown"],
        "json" => vec!["json", "json5", "jsonc"],
        "yaml" => vec!["yaml", "yml"],
        "html" => vec!["html", "htm", "xhtml"],
        "css" => vec!["css", "scss", "sass", "less"],
        _ => vec![],
    }
}

fn grep_search(opts: &GrepOptions) -> Result<String> {
    use std::fs;
    use std::path::Path;

    // Build regex with case-insensitive option
    let regex_pattern = if opts.case_insensitive {
        regex::RegexBuilder::new(&opts.pattern)
            .case_insensitive(true)
            .build()?
    } else {
        regex::Regex::new(&opts.pattern)?
    };

    let root = Path::new(&opts.path);
    let mut results: Vec<String> = Vec::new();
    let mut match_count = 0;
    let mut files_with_matches: Vec<String> = Vec::new();

    // Get file extensions for type filter
    let type_extensions = opts.file_type.as_deref().map(get_extensions_for_type);

    let entries = collect_grep_files(
        root,
        opts.glob_pattern.as_deref(),
        type_extensions.as_deref(),
    )?;

    for file_path in entries {
        if results.len() >= opts.head_limit && opts.output_mode == "content" {
            results.push(format!("... (limited to {} results)", opts.head_limit));
            break;
        }

        let content = match fs::read_to_string(&file_path) {
            Ok(c) => c,
            Err(_) => continue,
        };

        let lines: Vec<&str> = content.lines().collect();
        let mut file_has_match = false;
        let mut file_match_count = 0;

        for (line_idx, line) in lines.iter().enumerate() {
            if regex_pattern.is_match(line) {
                file_has_match = true;
                file_match_count += 1;
                match_count += 1;

                if opts.output_mode == "content" && results.len() < opts.head_limit {
                    // Add context lines before the match
                    if opts.context_lines > 0 {
                        let start_ctx = line_idx.saturating_sub(opts.context_lines);
                        for (ctx_idx, ctx_line) in lines
                            .iter()
                            .enumerate()
                            .skip(start_ctx)
                            .take(line_idx - start_ctx)
                        {
                            results.push(format_line(
                                &file_path,
                                ctx_idx + 1,
                                ctx_line,
                                opts.show_line_numbers,
                                true,
                            ));
                        }
                    }

                    // Add the matching line
                    results.push(format_line(
                        &file_path,
                        line_idx + 1,
                        line,
                        opts.show_line_numbers,
                        false,
                    ));

                    // Add context lines after the match
                    if opts.context_lines > 0 {
                        let end_ctx = (line_idx + opts.context_lines).min(lines.len() - 1);
                        for (ctx_idx, ctx_line) in lines
                            .iter()
                            .enumerate()
                            .skip(line_idx + 1)
                            .take(end_ctx - line_idx)
                        {
                            results.push(format_line(
                                &file_path,
                                ctx_idx + 1,
                                ctx_line,
                                opts.show_line_numbers,
                                true,
                            ));
                        }
                    }
                }
            }
        }

        if file_has_match && opts.output_mode == "files_with_matches" {
            files_with_matches.push(file_path.display().to_string());
        }

        if opts.output_mode == "count" && file_match_count > 0 {
            results.push(format!(
                "{}: {} matches",
                file_path.display(),
                file_match_count
            ));
        }
    }

    // Format output based on mode
    match opts.output_mode.as_str() {
        "files_with_matches" => {
            if files_with_matches.is_empty() {
                Ok("No files matched.".to_string())
            } else {
                Ok(files_with_matches.join("\n"))
            }
        }
        "count" => {
            if results.is_empty() {
                Ok("No matches found.".to_string())
            } else {
                Ok(format!(
                    "Total: {} matches\n{}",
                    match_count,
                    results.join("\n")
                ))
            }
        }
        _ => {
            // content
            if results.is_empty() {
                Ok("No matches found.".to_string())
            } else {
                Ok(results.join("\n"))
            }
        }
    }
}

/// Format a single line with optional line number and context marker.
fn format_line(
    file_path: &std::path::Path,
    line_num: usize,
    line: &str,
    show_line_numbers: bool,
    is_context: bool,
) -> String {
    let marker = if is_context { "-" } else { ":" };
    if show_line_numbers {
        format!(
            "{}:{}{} {}",
            file_path.display(),
            line_num,
            marker,
            line.trim()
        )
    } else {
        format!("{}{} {}", file_path.display(), marker, line.trim())
    }
}

fn collect_grep_files(
    root: &std::path::Path,
    glob_pattern: Option<&str>,
    type_extensions: Option<&[&str]>,
) -> Result<Vec<std::path::PathBuf>> {
    let mut files = Vec::new();

    if root.is_file() {
        files.push(root.to_path_buf());
        return Ok(files);
    }

    // Build glob matcher
    let glob_matcher = glob_pattern.map(glob::Pattern::new).transpose()?;

    let walker = walkdir_grep(root)?;

    for entry in walker {
        let path = entry;

        // Check glob pattern
        if let Some(ref matcher) = glob_matcher {
            let relative = path.strip_prefix(root).unwrap_or(&path);
            let relative_str = relative.to_string_lossy();
            if !matcher.matches(&relative_str) {
                // Also try just the filename
                if let Some(name) = path.file_name().and_then(|n| n.to_str()) {
                    if !matcher.matches(name) {
                        continue;
                    }
                } else {
                    continue;
                }
            }
        }

        // Check file type extensions
        if let Some(extensions) = type_extensions {
            let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("");
            if !extensions.contains(&ext) {
                continue;
            }
        }

        files.push(path);
    }

    Ok(files)
}

fn walkdir_grep(root: &std::path::Path) -> Result<Vec<std::path::PathBuf>> {
    use std::fs;

    let mut files = Vec::new();
    let mut stack = vec![root.to_path_buf()];

    // Directories to skip
    const SKIP_DIRS: &[&str] = &[
        ".git",
        ".svn",
        ".hg",
        "node_modules",
        "vendor",
        "target",
        "build",
        "dist",
        "out",
        ".cache",
        ".npm",
        ".cargo",
        "__pycache__",
        ".venv",
        "venv",
        ".idea",
        ".vscode",
    ];

    while let Some(dir) = stack.pop() {
        let entries = match fs::read_dir(&dir) {
            Ok(e) => e,
            Err(_) => continue,
        };

        for entry in entries.flatten() {
            let path = entry.path();
            let name = entry.file_name();
            let name_str = name.to_string_lossy();

            // Skip hidden files and known directories
            if name_str.starts_with('.') || SKIP_DIRS.contains(&name_str.as_ref()) {
                continue;
            }

            if path.is_dir() {
                stack.push(path);
            } else if path.is_file() {
                files.push(path);
            }
        }
    }

    Ok(files)
}