Skip to main content

llama_cpp_v3_agent_sdk/tools/
glob.rs

1use crate::error::AgentError;
2use crate::tool::{Tool, ToolResult};
3
4/// Find files matching a glob pattern.
5pub struct GlobTool;
6
7impl Tool for GlobTool {
8    fn name(&self) -> &str {
9        "glob"
10    }
11
12    fn description(&self) -> &str {
13        "Find files matching a glob pattern. Returns a list of matching file paths. \
14         Supports standard glob patterns like '**/*.rs', 'src/*.cpp', etc."
15    }
16
17    fn parameters_schema(&self) -> serde_json::Value {
18        serde_json::json!({
19            "type": "object",
20            "properties": {
21                "pattern": {
22                    "type": "string",
23                    "description": "Glob pattern to match files (e.g. '**/*.rs', 'src/**/*.cpp')"
24                }
25            },
26            "required": ["pattern"]
27        })
28    }
29
30    fn execute(&self, args: &serde_json::Value) -> Result<ToolResult, AgentError> {
31        let pattern = args["pattern"].as_str().ok_or_else(|| AgentError::Tool {
32            tool: "glob".to_string(),
33            message: "Missing 'pattern' argument".to_string(),
34        })?;
35
36        match glob::glob(pattern) {
37            Ok(paths) => {
38                let mut results = Vec::new();
39                let mut errors = 0;
40
41                for entry in paths {
42                    match entry {
43                        Ok(path) => results.push(path.display().to_string()),
44                        Err(_) => errors += 1,
45                    }
46                }
47
48                if results.is_empty() {
49                    return Ok(ToolResult::ok(format!(
50                        "No files matched pattern '{}'",
51                        pattern
52                    )));
53                }
54
55                let mut output = format!("Found {} file(s) matching '{}':\n", results.len(), pattern);
56                for path in &results {
57                    output.push_str(&format!("  {}\n", path));
58                }
59                if errors > 0 {
60                    output.push_str(&format!("({} entries could not be read)\n", errors));
61                }
62
63                Ok(ToolResult::ok(output))
64            }
65            Err(e) => Ok(ToolResult::err(format!(
66                "Invalid glob pattern '{}': {}",
67                pattern, e
68            ))),
69        }
70    }
71
72    fn requires_permission(&self) -> bool {
73        false
74    }
75}