1package tools
2
3import (
4 "context"
5 "encoding/json"
6 "fmt"
7 "strings"
8 "sync"
9
10 "github.com/charmbracelet/crush/internal/fsext"
11)
12
13const (
14 GlobToolName = "glob"
15 globDescription = `Fast file pattern matching tool that finds files by name and pattern, returning matching paths sorted by modification time (newest first).
16
17WHEN TO USE THIS TOOL:
18- Use when you need to find files by name patterns or extensions
19- Great for finding specific file types across a directory structure
20- Useful for discovering files that match certain naming conventions
21
22HOW TO USE:
23- Provide a glob pattern to match against file paths
24- Optionally specify a starting directory (defaults to current working directory)
25- Results are sorted with most recently modified files first
26
27GLOB PATTERN SYNTAX:
28- '*' matches any sequence of non-separator characters
29- '**' matches any sequence of characters, including separators
30- '?' matches any single non-separator character
31- '[...]' matches any character in the brackets
32- '[!...]' matches any character not in the brackets
33
34COMMON PATTERN EXAMPLES:
35- '*.js' - Find all JavaScript files in the current directory
36- '**/*.js' - Find all JavaScript files in any subdirectory
37- 'src/**/*.{ts,tsx}' - Find all TypeScript files in the src directory
38- '*.{html,css,js}' - Find all HTML, CSS, and JS files
39
40LIMITATIONS:
41- Results are limited to 100 files (newest first)
42- Does not search file contents (use Grep tool for that)
43- Hidden files (starting with '.') are skipped
44
45WINDOWS NOTES:
46- Path separators are handled automatically (both / and \ work)
47- Uses built-in Go implementation and bmatcuk/doublestar/v4 for globbing
48
49TIPS:
50- Patterns should use forward slashes (/) for cross-platform compatibility
51- For the most useful results, combine with the Grep tool: first find files with Glob, then search their contents with Grep
52- When doing iterative exploration that may require multiple rounds of searching, consider using the Agent tool instead
53- Always check if results are truncated and refine your search pattern if needed`
54)
55
56type GlobParams struct {
57 Pattern string `json:"pattern"`
58 Path string `json:"path"`
59}
60
61type GlobResponseMetadata struct {
62 NumberOfFiles int `json:"number_of_files"`
63 Truncated bool `json:"truncated"`
64}
65
66type globTool struct {
67 workingDir string
68 mu sync.Mutex
69}
70
71func NewGlobTool(workingDir string) BaseTool {
72 return &globTool{
73 workingDir: workingDir,
74 }
75}
76
77func (g *globTool) Name() string {
78 return GlobToolName
79}
80
81func (g *globTool) Info() ToolInfo {
82 return ToolInfo{
83 Name: GlobToolName,
84 Description: globDescription,
85 Parameters: map[string]any{
86 "pattern": map[string]any{
87 "type": "string",
88 "description": "The glob pattern to match files against",
89 },
90 "path": map[string]any{
91 "type": "string",
92 "description": "The directory to search in. Defaults to the current working directory.",
93 },
94 },
95 Required: []string{"pattern"},
96 }
97}
98
99func (g *globTool) Run(ctx context.Context, call ToolCall) (ToolResponse, error) {
100 var params GlobParams
101 if err := json.Unmarshal([]byte(call.Input), ¶ms); err != nil {
102 return NewTextErrorResponse(fmt.Sprintf("error parsing parameters: %s", err)), nil
103 }
104
105 if params.Pattern == "" {
106 return NewTextErrorResponse("pattern is required"), nil
107 }
108
109 searchPath := params.Path
110 if searchPath == "" {
111 searchPath = g.workingDir
112 }
113
114 g.mu.Lock()
115 files, truncated, err := globFiles(params.Pattern, searchPath, 100)
116 if err != nil {
117 g.mu.Unlock()
118 return ToolResponse{}, fmt.Errorf("error finding files: %w", err)
119 }
120
121 output := "No files found"
122 if len(files) > 0 {
123 output = strings.Join(files, "\n")
124 if truncated {
125 output += "\n\n(Results are truncated. Consider using a more specific path or pattern.)"
126 }
127 }
128 g.mu.Unlock()
129
130 return WithResponseMetadata(
131 NewTextResponse(output),
132 GlobResponseMetadata{
133 NumberOfFiles: len(files),
134 Truncated: truncated,
135 },
136 ), nil
137}
138
139func globFiles(pattern, searchPath string, limit int) ([]string, bool, error) {
140 return fsext.GlobWithDoubleStar(pattern, searchPath, limit)
141}