1use async_trait::async_trait;
2use serde_json::json;
3use std::path::{Path, PathBuf};
4use tracing::info;
5
6use crate::config::WorkingDirIsolation;
7use microclaw_core::llm_types::ToolDefinition;
8
9use super::{schema_object, Tool, ToolResult};
10
11pub struct GrepTool {
12 working_dir: PathBuf,
13 working_dir_isolation: WorkingDirIsolation,
14}
15
16impl GrepTool {
17 pub fn new(working_dir: &str) -> Self {
18 Self::new_with_isolation(working_dir, WorkingDirIsolation::Shared)
19 }
20
21 pub fn new_with_isolation(
22 working_dir: &str,
23 working_dir_isolation: WorkingDirIsolation,
24 ) -> Self {
25 Self {
26 working_dir: PathBuf::from(working_dir),
27 working_dir_isolation,
28 }
29 }
30}
31
32#[async_trait]
33impl Tool for GrepTool {
34 fn name(&self) -> &str {
35 "grep"
36 }
37
38 fn definition(&self) -> ToolDefinition {
39 ToolDefinition {
40 name: "grep".into(),
41 description: "Search file contents using a regex pattern. Returns matching lines with file paths and line numbers. Directory searches respect .gitignore/.ignore files and skip hidden, node_modules and target directories; set include_ignored to search ignored files too.".into(),
42 input_schema: schema_object(
43 json!({
44 "pattern": {
45 "type": "string",
46 "description": "Regex pattern to search for"
47 },
48 "path": {
49 "type": "string",
50 "description": "File or directory to search in (default: current directory)"
51 },
52 "glob": {
53 "type": "string",
54 "description": "Glob pattern to filter files (e.g., '*.rs')"
55 },
56 "include_ignored": {
57 "type": "boolean",
58 "description": "Also search files excluded by .gitignore/.ignore (default: false)"
59 }
60 }),
61 &["pattern"],
62 ),
63 }
64 }
65
66 async fn execute(&self, input: serde_json::Value) -> ToolResult {
67 let pattern = match input.get("pattern").and_then(|v| v.as_str()) {
68 Some(p) => p,
69 None => return ToolResult::error("Missing 'pattern' parameter".into()),
70 };
71 let path = input.get("path").and_then(|v| v.as_str()).unwrap_or(".");
72 let working_dir =
73 super::resolve_tool_working_dir(&self.working_dir, self.working_dir_isolation, &input);
74 let resolved_path = super::resolve_tool_path(&working_dir, path);
75 let resolved_path_str = resolved_path.to_string_lossy().to_string();
76 if let Err(msg) = crate::internal::tool_runtime::path_guard::check_path(&resolved_path_str)
77 {
78 return ToolResult::error(msg);
79 }
80 let file_glob = input.get("glob").and_then(|v| v.as_str());
81 let include_ignored = input
82 .get("include_ignored")
83 .and_then(|v| v.as_bool())
84 .unwrap_or(false);
85
86 info!("Grep: {} in {}", pattern, resolved_path.display());
87
88 let re = match regex::Regex::new(pattern) {
89 Ok(r) => r,
90 Err(e) => return ToolResult::error(format!("Invalid regex: {e}")),
91 };
92
93 let mut results = Vec::new();
94 let mut file_count = 0;
95
96 if let Err(e) = grep_recursive(
97 &resolved_path,
98 file_glob,
99 include_ignored,
100 &re,
101 &mut results,
102 &mut file_count,
103 ) {
104 return ToolResult::error(format!("Search error: {e}"));
105 }
106
107 if results.is_empty() {
108 ToolResult::success("No matches found.".into())
109 } else {
110 if results.len() > 500 {
111 results.truncate(500);
112 results.push("... (results truncated)".into());
113 }
114 ToolResult::success(results.join("\n"))
115 }
116 }
117}
118
119fn grep_recursive(
120 path: &Path,
121 file_glob: Option<&str>,
122 include_ignored: bool,
123 re: ®ex::Regex,
124 results: &mut Vec<String>,
125 file_count: &mut usize,
126) -> std::io::Result<()> {
127 let metadata = std::fs::metadata(path)?;
128
129 if metadata.is_file() {
130 grep_file(path, re, results)?;
131 } else if metadata.is_dir() {
132 let glob_pattern = file_glob.and_then(|g| glob::Pattern::new(g).ok());
133 let options = super::workspace_walk::WalkOptions {
134 include_ignored,
135 include_dirs: false,
136 };
137 for entry_path in super::workspace_walk::walk(path, options) {
138 if let Some(ref pat) = glob_pattern {
139 let name = entry_path
140 .file_name()
141 .map(|n| n.to_string_lossy().to_string())
142 .unwrap_or_default();
143 let relative = entry_path.strip_prefix(path).unwrap_or(&entry_path);
144 if !pat.matches(&name) && !pat.matches_path(relative) {
145 continue;
146 }
147 }
148 *file_count += 1;
149 grep_file(&entry_path, re, results)?;
150 if results.len() >= 500 {
151 return Ok(());
152 }
153 }
154 }
155 Ok(())
156}
157
158fn grep_file(path: &Path, re: ®ex::Regex, results: &mut Vec<String>) -> std::io::Result<()> {
159 let content = match std::fs::read_to_string(path) {
160 Ok(c) => c,
161 Err(_) => return Ok(()), };
163
164 for (line_num, line) in content.lines().enumerate() {
165 if re.is_match(line) {
166 results.push(format!("{}:{}: {}", path.display(), line_num + 1, line));
167 if results.len() >= 500 {
168 return Ok(());
169 }
170 }
171 }
172 Ok(())
173}
174
175#[cfg(test)]
176mod tests {
177 use super::*;
178 use serde_json::json;
179
180 fn setup_grep_dir() -> std::path::PathBuf {
181 let dir = std::env::temp_dir().join(format!("microclaw_grep_{}", uuid::Uuid::new_v4()));
182 std::fs::create_dir_all(&dir).unwrap();
183 std::fs::write(
184 dir.join("hello.rs"),
185 "fn main() {\n println!(\"hello\");\n}\n",
186 )
187 .unwrap();
188 std::fs::write(dir.join("world.txt"), "hello world\ngoodbye world\n").unwrap();
189 dir
190 }
191
192 #[tokio::test]
193 async fn test_grep_finds_matches() {
194 let dir = setup_grep_dir();
195 let tool = GrepTool::new(".");
196 let result = tool
197 .execute(json!({"pattern": "hello", "path": dir.to_str().unwrap()}))
198 .await;
199 assert!(!result.is_error);
200 assert!(result.content.contains("hello"));
201 assert!(result.content.contains(":"));
203 let _ = std::fs::remove_dir_all(&dir);
204 }
205
206 #[tokio::test]
207 async fn test_grep_no_matches() {
208 let dir = setup_grep_dir();
209 let tool = GrepTool::new(".");
210 let result = tool
211 .execute(json!({"pattern": "zzzzzzz", "path": dir.to_str().unwrap()}))
212 .await;
213 assert!(!result.is_error);
214 assert!(result.content.contains("No matches"));
215 let _ = std::fs::remove_dir_all(&dir);
216 }
217
218 #[tokio::test]
219 async fn test_grep_with_file_glob() {
220 let dir = setup_grep_dir();
221 let tool = GrepTool::new(".");
222 let result = tool
224 .execute(json!({
225 "pattern": "hello",
226 "path": dir.to_str().unwrap(),
227 "glob": "*.txt"
228 }))
229 .await;
230 assert!(!result.is_error);
231 assert!(result.content.contains("world.txt"));
232 assert!(!result.content.contains("hello.rs"));
234 let _ = std::fs::remove_dir_all(&dir);
235 }
236
237 #[tokio::test]
238 async fn test_grep_invalid_regex() {
239 let tool = GrepTool::new(".");
240 let result = tool
241 .execute(json!({"pattern": "[invalid", "path": "."}))
242 .await;
243 assert!(result.is_error);
244 assert!(result.content.contains("Invalid regex"));
245 }
246
247 #[tokio::test]
248 async fn test_grep_missing_pattern() {
249 let tool = GrepTool::new(".");
250 let result = tool.execute(json!({})).await;
251 assert!(result.is_error);
252 assert!(result.content.contains("Missing 'pattern'"));
253 }
254
255 #[test]
256 fn test_grep_file_function() {
257 let dir = std::env::temp_dir().join(format!("microclaw_gf_{}", uuid::Uuid::new_v4()));
258 std::fs::create_dir_all(&dir).unwrap();
259 let file = dir.join("test.txt");
260 std::fs::write(&file, "foo bar\nbaz qux\nfoo again\n").unwrap();
261
262 let re = regex::Regex::new("foo").unwrap();
263 let mut results = Vec::new();
264 grep_file(&file, &re, &mut results).unwrap();
265 assert_eq!(results.len(), 2);
266 assert!(results[0].contains(":1:"));
267 assert!(results[1].contains(":3:"));
268
269 let _ = std::fs::remove_dir_all(&dir);
270 }
271
272 #[test]
273 fn test_grep_recursive_skips_hidden_dirs() {
274 let dir = std::env::temp_dir().join(format!("microclaw_gr_{}", uuid::Uuid::new_v4()));
275 std::fs::create_dir_all(dir.join(".hidden")).unwrap();
276 std::fs::write(dir.join(".hidden").join("secret.txt"), "match_me").unwrap();
277 std::fs::write(dir.join("visible.txt"), "match_me").unwrap();
278
279 let re = regex::Regex::new("match_me").unwrap();
280 let mut results: Vec<String> = Vec::new();
281 let mut count = 0;
282 grep_recursive(&dir, None, false, &re, &mut results, &mut count).unwrap();
283
284 assert_eq!(results.len(), 1);
286 assert!(results[0].contains("visible.txt"));
287
288 let _ = std::fs::remove_dir_all(&dir);
289 }
290
291 #[tokio::test]
292 async fn test_grep_defaults_to_working_dir() {
293 let root = std::env::temp_dir().join(format!("microclaw_grep2_{}", uuid::Uuid::new_v4()));
294 let work = root.join("workspace");
295 let shared = work.join("shared");
296 std::fs::create_dir_all(&shared).unwrap();
297 std::fs::write(shared.join("a.txt"), "needle").unwrap();
298
299 let tool = GrepTool::new(work.to_str().unwrap());
300 let result = tool.execute(json!({"pattern":"needle"})).await;
301 assert!(!result.is_error);
302 assert!(result.content.contains("a.txt"));
303
304 let _ = std::fs::remove_dir_all(&root);
305 }
306
307 #[tokio::test]
308 async fn test_grep_respects_gitignore() {
309 let dir = tempfile::tempdir().unwrap();
310 let root = dir.path();
311 std::fs::write(root.join(".gitignore"), "generated/\n").unwrap();
312 std::fs::create_dir_all(root.join("generated")).unwrap();
313 std::fs::write(root.join("generated/out.txt"), "needle").unwrap();
314 std::fs::write(root.join("kept.txt"), "needle").unwrap();
315
316 let tool = GrepTool::new(".");
317 let result = tool
318 .execute(json!({"pattern": "needle", "path": root.to_str().unwrap()}))
319 .await;
320 assert!(result.content.contains("kept.txt"));
321 assert!(!result.content.contains("out.txt"));
322
323 let result = tool
324 .execute(json!({
325 "pattern": "needle",
326 "path": root.to_str().unwrap(),
327 "include_ignored": true
328 }))
329 .await;
330 assert!(result.content.contains("out.txt"));
331 }
332}