1use async_trait::async_trait;
2use regex::Regex;
3use serde_json::{json, Value};
4use std::collections::HashMap;
5use std::fs;
6use std::path::{Path, PathBuf};
7
8use crate::error::{MojenticError, Result};
9use crate::llm::tools::{FunctionDescriptor, LlmTool, ToolDescriptor};
10
11#[derive(Debug, Clone)]
16pub struct FilesystemGateway {
17 base_path: PathBuf,
18}
19
20impl FilesystemGateway {
21 pub fn new<P: AsRef<Path>>(base_path: P) -> Result<Self> {
23 let base_path = base_path.as_ref();
24
25 if !base_path.is_dir() {
26 return Err(MojenticError::ToolError(format!(
27 "Base path {:?} is not a directory",
28 base_path
29 )));
30 }
31
32 Ok(Self {
33 base_path: base_path.canonicalize()?,
34 })
35 }
36
37 pub fn resolve_path<P: AsRef<Path>>(&self, path: P) -> Result<PathBuf> {
39 let path = path.as_ref();
40
41 let resolved = match self.base_path.join(path).canonicalize() {
43 Ok(canonical) => canonical,
44 Err(_) => {
45 let joined = self.base_path.join(path);
48
49 let mut normalized = PathBuf::new();
51 for component in joined.components() {
52 match component {
53 std::path::Component::ParentDir => {
54 if !normalized.pop() {
56 return Err(MojenticError::ToolError(format!(
58 "Path {:?} attempts to escape the sandbox",
59 path
60 )));
61 }
62 }
63 std::path::Component::CurDir => {
64 }
66 _ => {
67 normalized.push(component);
68 }
69 }
70 }
71 normalized
72 }
73 };
74
75 if !resolved.starts_with(&self.base_path) {
77 return Err(MojenticError::ToolError(format!(
78 "Path {:?} attempts to escape the sandbox",
79 path
80 )));
81 }
82
83 Ok(resolved)
84 }
85
86 pub fn ls<P: AsRef<Path>>(&self, path: P) -> Result<Vec<String>> {
88 let resolved_path = self.resolve_path(path)?;
89 let entries = fs::read_dir(&resolved_path)?;
90
91 let mut files = Vec::new();
92 for entry in entries {
93 let entry = entry?;
94 let relative = entry
95 .path()
96 .strip_prefix(&self.base_path)
97 .unwrap()
98 .to_string_lossy()
99 .to_string();
100 files.push(relative);
101 }
102
103 Ok(files)
104 }
105
106 pub fn list_all_files<P: AsRef<Path>>(&self, path: P) -> Result<Vec<String>> {
108 let resolved_path = self.resolve_path(path)?;
109 let mut files = Vec::new();
110
111 self.collect_files_recursively(&resolved_path, &mut files)?;
112
113 Ok(files)
114 }
115
116 fn collect_files_recursively(&self, dir: &Path, files: &mut Vec<String>) -> Result<()> {
117 if !dir.is_dir() {
118 return Ok(());
119 }
120
121 let entries = fs::read_dir(dir)?;
122
123 for entry in entries {
124 let entry = entry?;
125 let path = entry.path();
126
127 if path.is_dir() {
128 self.collect_files_recursively(&path, files)?;
129 } else {
130 let relative =
131 path.strip_prefix(&self.base_path).unwrap().to_string_lossy().to_string();
132 files.push(relative);
133 }
134 }
135
136 Ok(())
137 }
138
139 pub fn find_files_by_glob<P: AsRef<Path>>(
141 &self,
142 path: P,
143 pattern: &str,
144 ) -> Result<Vec<String>> {
145 let resolved_path = self.resolve_path(path)?;
146 let glob_pattern = resolved_path.join(pattern);
147 let glob_str = glob_pattern.to_string_lossy();
148
149 let mut files = Vec::new();
150 for entry in glob::glob(&glob_str)
151 .map_err(|e| MojenticError::ToolError(format!("Invalid glob pattern: {}", e)))?
152 {
153 match entry {
154 Ok(path) => {
155 if let Ok(relative) = path.strip_prefix(&self.base_path) {
156 files.push(relative.to_string_lossy().to_string());
157 }
158 }
159 Err(_) => continue,
160 }
161 }
162
163 Ok(files)
164 }
165
166 pub fn find_files_containing<P: AsRef<Path>>(
168 &self,
169 path: P,
170 pattern: &str,
171 ) -> Result<Vec<String>> {
172 let resolved_path = self.resolve_path(path)?;
173 let regex = Regex::new(pattern)
174 .map_err(|e| MojenticError::ToolError(format!("Invalid regex pattern: {}", e)))?;
175
176 let mut files = Vec::new();
177 self.find_matching_files(&resolved_path, ®ex, &mut files)?;
178
179 Ok(files)
180 }
181
182 fn find_matching_files(
183 &self,
184 dir: &Path,
185 regex: &Regex,
186 files: &mut Vec<String>,
187 ) -> Result<()> {
188 if !dir.is_dir() {
189 return Ok(());
190 }
191
192 let entries = fs::read_dir(dir)?;
193
194 for entry in entries {
195 let entry = entry?;
196 let path = entry.path();
197
198 if path.is_dir() {
199 self.find_matching_files(&path, regex, files)?;
200 } else if path.is_file() {
201 if let Ok(content) = fs::read_to_string(&path) {
202 if regex.is_match(&content) {
203 let relative = path
204 .strip_prefix(&self.base_path)
205 .unwrap()
206 .to_string_lossy()
207 .to_string();
208 files.push(relative);
209 }
210 }
211 }
212 }
213
214 Ok(())
215 }
216
217 pub fn find_lines_matching<P: AsRef<Path>>(
219 &self,
220 path: P,
221 file_name: &str,
222 pattern: &str,
223 ) -> Result<Vec<Value>> {
224 let resolved_path = self.resolve_path(path)?;
225 let file_path = resolved_path.join(file_name);
226 let regex = Regex::new(pattern)
227 .map_err(|e| MojenticError::ToolError(format!("Invalid regex pattern: {}", e)))?;
228
229 let content = fs::read_to_string(&file_path)?;
230 let mut matching_lines = Vec::new();
231
232 for (i, line) in content.lines().enumerate() {
233 if regex.is_match(line) {
234 matching_lines.push(json!({
235 "line_number": i + 1,
236 "content": line
237 }));
238 }
239 }
240
241 Ok(matching_lines)
242 }
243
244 pub fn read<P: AsRef<Path>>(&self, path: P, file_name: &str) -> Result<String> {
246 let resolved_path = self.resolve_path(path)?;
247 let file_path = resolved_path.join(file_name);
248 Ok(fs::read_to_string(file_path)?)
249 }
250
251 pub fn write<P: AsRef<Path>>(&self, path: P, file_name: &str, content: &str) -> Result<()> {
253 let resolved_path = self.resolve_path(path)?;
254 let file_path = resolved_path.join(file_name);
255 fs::write(file_path, content)?;
256 Ok(())
257 }
258}
259
260#[derive(Clone)]
262pub struct ListFilesTool {
263 fs: FilesystemGateway,
264}
265
266impl ListFilesTool {
267 pub fn new(fs: FilesystemGateway) -> Self {
268 Self { fs }
269 }
270}
271
272#[async_trait]
273impl LlmTool for ListFilesTool {
274 fn descriptor(&self) -> ToolDescriptor {
275 ToolDescriptor {
276 r#type: "function".to_string(),
277 function: FunctionDescriptor {
278 name: "list_files".to_string(),
279 description: "List files in the specified directory (non-recursive), optionally filtered by extension. Use this when you need to see what files are available in a specific directory without including subdirectories.".to_string(),
280 parameters: json!({
281 "type": "object",
282 "properties": {
283 "path": {
284 "type": "string",
285 "description": "The path relative to the sandbox root to list files from. For example, '.' for the root directory, 'src' for the src directory, or 'docs/images' for a nested directory."
286 },
287 "extension": {
288 "type": "string",
289 "description": "The file extension to filter by (e.g., '.py', '.txt', '.md'). If not provided, all files will be listed. For example, using '.py' will only list Python files in the directory."
290 }
291 },
292 "additionalProperties": false,
293 "required": ["path"]
294 }),
295 },
296 }
297 }
298
299 async fn run(
300 &self,
301 args: &HashMap<String, Value>,
302 _ctx: &crate::llm::tools::ToolRunCtx,
303 ) -> Result<Value> {
304 let path = args
305 .get("path")
306 .and_then(|v| v.as_str())
307 .ok_or_else(|| MojenticError::ToolError("Missing 'path' parameter".to_string()))?;
308
309 let extension = args.get("extension").and_then(|v| v.as_str());
310
311 let files = self.fs.ls(path)?;
312
313 let filtered: Vec<String> = if let Some(ext) = extension {
314 files.into_iter().filter(|f| f.ends_with(ext)).collect()
315 } else {
316 files
317 };
318
319 Ok(json!(filtered))
320 }
321 fn clone_box(&self) -> Box<dyn LlmTool> {
322 Box::new(self.clone())
323 }
324}
325
326#[derive(Clone)]
328pub struct ReadFileTool {
329 fs: FilesystemGateway,
330}
331
332impl ReadFileTool {
333 pub fn new(fs: FilesystemGateway) -> Self {
334 Self { fs }
335 }
336}
337
338#[async_trait]
339impl LlmTool for ReadFileTool {
340 fn descriptor(&self) -> ToolDescriptor {
341 ToolDescriptor {
342 r#type: "function".to_string(),
343 function: FunctionDescriptor {
344 name: "read_file".to_string(),
345 description: "Read the entire content of a file as a string. Use this when you need to access or analyze the complete contents of a file.".to_string(),
346 parameters: json!({
347 "type": "object",
348 "properties": {
349 "path": {
350 "type": "string",
351 "description": "The full relative path including the filename of the file to read. For example, 'README.md' for a file in the root directory, 'src/main.py' for a file in the src directory, or 'docs/images/diagram.png' for a file in a nested directory."
352 }
353 },
354 "additionalProperties": false,
355 "required": ["path"]
356 }),
357 },
358 }
359 }
360
361 async fn run(
362 &self,
363 args: &HashMap<String, Value>,
364 _ctx: &crate::llm::tools::ToolRunCtx,
365 ) -> Result<Value> {
366 let path = args
367 .get("path")
368 .and_then(|v| v.as_str())
369 .ok_or_else(|| MojenticError::ToolError("Missing 'path' parameter".to_string()))?;
370
371 let (directory, file_name) = split_path(path);
372 let content = self.fs.read(directory, file_name)?;
373 Ok(json!(content))
374 }
375 fn clone_box(&self) -> Box<dyn LlmTool> {
376 Box::new(self.clone())
377 }
378}
379
380#[derive(Clone)]
382pub struct WriteFileTool {
383 fs: FilesystemGateway,
384}
385
386impl WriteFileTool {
387 pub fn new(fs: FilesystemGateway) -> Self {
388 Self { fs }
389 }
390}
391
392#[async_trait]
393impl LlmTool for WriteFileTool {
394 fn descriptor(&self) -> ToolDescriptor {
395 ToolDescriptor {
396 r#type: "function".to_string(),
397 function: FunctionDescriptor {
398 name: "write_file".to_string(),
399 description: "Write content to a file, completely overwriting any existing content. Use this when you want to replace the entire contents of a file with new content.".to_string(),
400 parameters: json!({
401 "type": "object",
402 "properties": {
403 "path": {
404 "type": "string",
405 "description": "The full relative path including the filename where the file should be written. For example, 'output.txt' for a file in the root directory, 'src/main.py' for a file in the src directory, or 'docs/images/diagram.png' for a file in a nested directory."
406 },
407 "content": {
408 "type": "string",
409 "description": "The content to write to the file. This will completely replace any existing content in the file. For example, 'Hello, world!' for a simple text file, or a JSON string for a configuration file."
410 }
411 },
412 "additionalProperties": false,
413 "required": ["path", "content"]
414 }),
415 },
416 }
417 }
418
419 async fn run(
420 &self,
421 args: &HashMap<String, Value>,
422 _ctx: &crate::llm::tools::ToolRunCtx,
423 ) -> Result<Value> {
424 let path = args
425 .get("path")
426 .and_then(|v| v.as_str())
427 .ok_or_else(|| MojenticError::ToolError("Missing 'path' parameter".to_string()))?;
428
429 let content = args
430 .get("content")
431 .and_then(|v| v.as_str())
432 .ok_or_else(|| MojenticError::ToolError("Missing 'content' parameter".to_string()))?;
433
434 let (directory, file_name) = split_path(path);
435 self.fs.write(directory, file_name, content)?;
436 Ok(json!(format!("Successfully wrote to {}", path)))
437 }
438 fn clone_box(&self) -> Box<dyn LlmTool> {
439 Box::new(self.clone())
440 }
441}
442
443#[derive(Clone)]
445pub struct ListAllFilesTool {
446 fs: FilesystemGateway,
447}
448
449impl ListAllFilesTool {
450 pub fn new(fs: FilesystemGateway) -> Self {
451 Self { fs }
452 }
453}
454
455#[async_trait]
456impl LlmTool for ListAllFilesTool {
457 fn descriptor(&self) -> ToolDescriptor {
458 ToolDescriptor {
459 r#type: "function".to_string(),
460 function: FunctionDescriptor {
461 name: "list_all_files".to_string(),
462 description: "List all files recursively in the specified directory, including files in subdirectories. Use this when you need a complete inventory of all files in a directory and its subdirectories.".to_string(),
463 parameters: json!({
464 "type": "object",
465 "properties": {
466 "path": {
467 "type": "string",
468 "description": "The path relative to the sandbox root to list files from recursively. For example, '.' for the root directory and all subdirectories, 'src' for the src directory and all its subdirectories, or 'docs/images' for a nested directory and its subdirectories."
469 }
470 },
471 "additionalProperties": false,
472 "required": ["path"]
473 }),
474 },
475 }
476 }
477
478 async fn run(
479 &self,
480 args: &HashMap<String, Value>,
481 _ctx: &crate::llm::tools::ToolRunCtx,
482 ) -> Result<Value> {
483 let path = args
484 .get("path")
485 .and_then(|v| v.as_str())
486 .ok_or_else(|| MojenticError::ToolError("Missing 'path' parameter".to_string()))?;
487
488 let files = self.fs.list_all_files(path)?;
489 Ok(json!(files))
490 }
491 fn clone_box(&self) -> Box<dyn LlmTool> {
492 Box::new(self.clone())
493 }
494}
495
496#[derive(Clone)]
498pub struct FindFilesByGlobTool {
499 fs: FilesystemGateway,
500}
501
502impl FindFilesByGlobTool {
503 pub fn new(fs: FilesystemGateway) -> Self {
504 Self { fs }
505 }
506}
507
508#[async_trait]
509impl LlmTool for FindFilesByGlobTool {
510 fn descriptor(&self) -> ToolDescriptor {
511 ToolDescriptor {
512 r#type: "function".to_string(),
513 function: FunctionDescriptor {
514 name: "find_files_by_glob".to_string(),
515 description: "Find files matching a glob pattern in the specified directory. Use this when you need to locate files with specific patterns in their names or paths (e.g., all Python files with '*.py' or all text files in any subdirectory with '**/*.txt').".to_string(),
516 parameters: json!({
517 "type": "object",
518 "properties": {
519 "path": {
520 "type": "string",
521 "description": "The path relative to the sandbox root to search in. For example, '.' for the root directory, 'src' for the src directory, or 'docs/images' for a nested directory."
522 },
523 "pattern": {
524 "type": "string",
525 "description": "The glob pattern to match files against. Examples: '*.py' for all Python files in the specified directory, '**/*.txt' for all text files in the specified directory and any subdirectory, or '**/*test*.py' for all Python files with 'test' in their name in the specified directory and any subdirectory."
526 }
527 },
528 "additionalProperties": false,
529 "required": ["path", "pattern"]
530 }),
531 },
532 }
533 }
534
535 async fn run(
536 &self,
537 args: &HashMap<String, Value>,
538 _ctx: &crate::llm::tools::ToolRunCtx,
539 ) -> Result<Value> {
540 let path = args
541 .get("path")
542 .and_then(|v| v.as_str())
543 .ok_or_else(|| MojenticError::ToolError("Missing 'path' parameter".to_string()))?;
544
545 let pattern = args
546 .get("pattern")
547 .and_then(|v| v.as_str())
548 .ok_or_else(|| MojenticError::ToolError("Missing 'pattern' parameter".to_string()))?;
549
550 let files = self.fs.find_files_by_glob(path, pattern)?;
551 Ok(json!(files))
552 }
553 fn clone_box(&self) -> Box<dyn LlmTool> {
554 Box::new(self.clone())
555 }
556}
557
558#[derive(Clone)]
560pub struct FindFilesContainingTool {
561 fs: FilesystemGateway,
562}
563
564impl FindFilesContainingTool {
565 pub fn new(fs: FilesystemGateway) -> Self {
566 Self { fs }
567 }
568}
569
570#[async_trait]
571impl LlmTool for FindFilesContainingTool {
572 fn descriptor(&self) -> ToolDescriptor {
573 ToolDescriptor {
574 r#type: "function".to_string(),
575 function: FunctionDescriptor {
576 name: "find_files_containing".to_string(),
577 description: "Find files containing text matching a regex pattern in the specified directory. Use this when you need to search for specific content across multiple files, such as finding all files that contain a particular function name or text string.".to_string(),
578 parameters: json!({
579 "type": "object",
580 "properties": {
581 "path": {
582 "type": "string",
583 "description": "The path relative to the sandbox root to search in. For example, '.' for the root directory, 'src' for the src directory, or 'docs/images' for a nested directory."
584 },
585 "pattern": {
586 "type": "string",
587 "description": "The regex pattern to search for in files. Examples: 'function\\s+main' to find files containing a main function, 'import\\s+os' to find files importing the os module, or 'TODO|FIXME' to find files containing TODO or FIXME comments. The pattern uses Rust's regex crate syntax."
588 }
589 },
590 "additionalProperties": false,
591 "required": ["path", "pattern"]
592 }),
593 },
594 }
595 }
596
597 async fn run(
598 &self,
599 args: &HashMap<String, Value>,
600 _ctx: &crate::llm::tools::ToolRunCtx,
601 ) -> Result<Value> {
602 let path = args
603 .get("path")
604 .and_then(|v| v.as_str())
605 .ok_or_else(|| MojenticError::ToolError("Missing 'path' parameter".to_string()))?;
606
607 let pattern = args
608 .get("pattern")
609 .and_then(|v| v.as_str())
610 .ok_or_else(|| MojenticError::ToolError("Missing 'pattern' parameter".to_string()))?;
611
612 let files = self.fs.find_files_containing(path, pattern)?;
613 Ok(json!(files))
614 }
615 fn clone_box(&self) -> Box<dyn LlmTool> {
616 Box::new(self.clone())
617 }
618}
619
620#[derive(Clone)]
622pub struct FindLinesMatchingTool {
623 fs: FilesystemGateway,
624}
625
626impl FindLinesMatchingTool {
627 pub fn new(fs: FilesystemGateway) -> Self {
628 Self { fs }
629 }
630}
631
632#[async_trait]
633impl LlmTool for FindLinesMatchingTool {
634 fn descriptor(&self) -> ToolDescriptor {
635 ToolDescriptor {
636 r#type: "function".to_string(),
637 function: FunctionDescriptor {
638 name: "find_lines_matching".to_string(),
639 description: "Find all lines in a file matching a regex pattern, returning both line numbers and content. Use this when you need to locate specific patterns within a single file and need to know exactly where they appear.".to_string(),
640 parameters: json!({
641 "type": "object",
642 "properties": {
643 "path": {
644 "type": "string",
645 "description": "The full relative path including the filename of the file to search in. For example, 'README.md' for a file in the root directory, 'src/main.py' for a file in the src directory, or 'docs/images/diagram.png' for a file in a nested directory."
646 },
647 "pattern": {
648 "type": "string",
649 "description": "The regex pattern to match lines against. Examples: 'def\\s+\\w+' to find all function definitions, 'class\\s+\\w+' to find all class definitions, or 'TODO|FIXME' to find all TODO or FIXME comments. The pattern uses Rust's regex crate syntax."
650 }
651 },
652 "additionalProperties": false,
653 "required": ["path", "pattern"]
654 }),
655 },
656 }
657 }
658
659 async fn run(
660 &self,
661 args: &HashMap<String, Value>,
662 _ctx: &crate::llm::tools::ToolRunCtx,
663 ) -> Result<Value> {
664 let path = args
665 .get("path")
666 .and_then(|v| v.as_str())
667 .ok_or_else(|| MojenticError::ToolError("Missing 'path' parameter".to_string()))?;
668
669 let pattern = args
670 .get("pattern")
671 .and_then(|v| v.as_str())
672 .ok_or_else(|| MojenticError::ToolError("Missing 'pattern' parameter".to_string()))?;
673
674 let (directory, file_name) = split_path(path);
675 let lines = self.fs.find_lines_matching(directory, file_name, pattern)?;
676 Ok(json!(lines))
677 }
678 fn clone_box(&self) -> Box<dyn LlmTool> {
679 Box::new(self.clone())
680 }
681}
682
683#[derive(Clone)]
685pub struct CreateDirectoryTool {
686 fs: FilesystemGateway,
687}
688
689impl CreateDirectoryTool {
690 pub fn new(fs: FilesystemGateway) -> Self {
691 Self { fs }
692 }
693}
694
695#[async_trait]
696impl LlmTool for CreateDirectoryTool {
697 fn descriptor(&self) -> ToolDescriptor {
698 ToolDescriptor {
699 r#type: "function".to_string(),
700 function: FunctionDescriptor {
701 name: "create_directory".to_string(),
702 description: "Create a new directory at the specified path. If the directory already exists, this operation will succeed without error. Use this when you need to create a directory structure before writing files to it.".to_string(),
703 parameters: json!({
704 "type": "object",
705 "properties": {
706 "path": {
707 "type": "string",
708 "description": "The relative path where the directory should be created. For example, 'new_folder' for a directory in the root, 'src/new_folder' for a directory in the src directory, or 'docs/images/new_folder' for a nested directory. Parent directories will be created automatically if they don't exist."
709 }
710 },
711 "additionalProperties": false,
712 "required": ["path"]
713 }),
714 },
715 }
716 }
717
718 async fn run(
719 &self,
720 args: &HashMap<String, Value>,
721 _ctx: &crate::llm::tools::ToolRunCtx,
722 ) -> Result<Value> {
723 let path = args
724 .get("path")
725 .and_then(|v| v.as_str())
726 .ok_or_else(|| MojenticError::ToolError("Missing 'path' parameter".to_string()))?;
727
728 let resolved_path = self.fs.resolve_path(path)?;
729 fs::create_dir_all(&resolved_path)?;
730 Ok(json!(format!("Successfully created directory '{}'", path)))
731 }
732 fn clone_box(&self) -> Box<dyn LlmTool> {
733 Box::new(self.clone())
734 }
735}
736
737fn split_path(path: &str) -> (&str, &str) {
738 let path_obj = Path::new(path);
739 let directory = path_obj.parent().and_then(|p| p.to_str()).unwrap_or(".");
740 let file_name = path_obj.file_name().and_then(|f| f.to_str()).unwrap_or("");
741 (directory, file_name)
742}
743
744#[cfg(test)]
745mod tests {
746 use super::*;
747 use tempfile::TempDir;
748
749 #[test]
750 fn test_filesystem_gateway_new() {
751 let temp_dir = TempDir::new().unwrap();
752 let gateway = FilesystemGateway::new(temp_dir.path());
753 assert!(gateway.is_ok());
754 }
755
756 #[test]
757 fn test_resolve_path_security() {
758 let temp_dir = TempDir::new().unwrap();
759 let gateway = FilesystemGateway::new(temp_dir.path()).unwrap();
760
761 let result = gateway.resolve_path("../../../etc/passwd");
763 assert!(result.is_err());
764 }
765
766 #[test]
767 fn test_ls() {
768 let temp_dir = TempDir::new().unwrap();
769 fs::write(temp_dir.path().join("test.txt"), "content").unwrap();
770
771 let gateway = FilesystemGateway::new(temp_dir.path()).unwrap();
772 let files = gateway.ls(".").unwrap();
773
774 assert_eq!(files.len(), 1);
775 assert!(files[0].contains("test.txt"));
776 }
777
778 #[test]
779 fn test_read_write() {
780 let temp_dir = TempDir::new().unwrap();
781 let gateway = FilesystemGateway::new(temp_dir.path()).unwrap();
782
783 gateway.write(".", "test.txt", "Hello, world!").unwrap();
784 let content = gateway.read(".", "test.txt").unwrap();
785
786 assert_eq!(content, "Hello, world!");
787 }
788}