glob.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package tools
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "io/fs"
  7. "os"
  8. "path/filepath"
  9. "sort"
  10. "strings"
  11. "time"
  12. "github.com/bmatcuk/doublestar/v4"
  13. "github.com/opencode-ai/opencode/internal/config"
  14. )
  15. const (
  16. GlobToolName = "glob"
  17. globDescription = `Fast file pattern matching tool that finds files by name and pattern, returning matching paths sorted by modification time (newest first).
  18. WHEN TO USE THIS TOOL:
  19. - Use when you need to find files by name patterns or extensions
  20. - Great for finding specific file types across a directory structure
  21. - Useful for discovering files that match certain naming conventions
  22. HOW 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. GLOB PATTERN SYNTAX:
  27. - '*' matches any sequence of non-separator characters
  28. - '**' matches any sequence of characters, including separators
  29. - '?' matches any single non-separator character
  30. - '[...]' matches any character in the brackets
  31. - '[!...]' matches any character not in the brackets
  32. COMMON PATTERN EXAMPLES:
  33. - '*.js' - Find all JavaScript files in the current directory
  34. - '**/*.js' - Find all JavaScript files in any subdirectory
  35. - 'src/**/*.{ts,tsx}' - Find all TypeScript files in the src directory
  36. - '*.{html,css,js}' - Find all HTML, CSS, and JS files
  37. LIMITATIONS:
  38. - Results are limited to 100 files (newest first)
  39. - Does not search file contents (use Grep tool for that)
  40. - Hidden files (starting with '.') are skipped
  41. TIPS:
  42. - For the most useful results, combine with the Grep tool: first find files with Glob, then search their contents with Grep
  43. - When doing iterative exploration that may require multiple rounds of searching, consider using the Agent tool instead
  44. - Always check if results are truncated and refine your search pattern if needed`
  45. )
  46. type fileInfo struct {
  47. path string
  48. modTime time.Time
  49. }
  50. type GlobParams struct {
  51. Pattern string `json:"pattern"`
  52. Path string `json:"path"`
  53. }
  54. type GlobResponseMetadata struct {
  55. NumberOfFiles int `json:"number_of_files"`
  56. Truncated bool `json:"truncated"`
  57. }
  58. type globTool struct{}
  59. func NewGlobTool() BaseTool {
  60. return &globTool{}
  61. }
  62. func (g *globTool) Info() ToolInfo {
  63. return ToolInfo{
  64. Name: GlobToolName,
  65. Description: globDescription,
  66. Parameters: map[string]any{
  67. "pattern": map[string]any{
  68. "type": "string",
  69. "description": "The glob pattern to match files against",
  70. },
  71. "path": map[string]any{
  72. "type": "string",
  73. "description": "The directory to search in. Defaults to the current working directory.",
  74. },
  75. },
  76. Required: []string{"pattern"},
  77. }
  78. }
  79. func (g *globTool) Run(ctx context.Context, call ToolCall) (ToolResponse, error) {
  80. var params GlobParams
  81. if err := json.Unmarshal([]byte(call.Input), &params); err != nil {
  82. return NewTextErrorResponse(fmt.Sprintf("error parsing parameters: %s", err)), nil
  83. }
  84. if params.Pattern == "" {
  85. return NewTextErrorResponse("pattern is required"), nil
  86. }
  87. searchPath := params.Path
  88. if searchPath == "" {
  89. searchPath = config.WorkingDirectory()
  90. }
  91. files, truncated, err := globFiles(params.Pattern, searchPath, 100)
  92. if err != nil {
  93. return ToolResponse{}, fmt.Errorf("error finding files: %w", err)
  94. }
  95. var output string
  96. if len(files) == 0 {
  97. output = "No files found"
  98. } else {
  99. output = strings.Join(files, "\n")
  100. if truncated {
  101. output += "\n\n(Results are truncated. Consider using a more specific path or pattern.)"
  102. }
  103. }
  104. return WithResponseMetadata(
  105. NewTextResponse(output),
  106. GlobResponseMetadata{
  107. NumberOfFiles: len(files),
  108. Truncated: truncated,
  109. },
  110. ), nil
  111. }
  112. func globFiles(pattern, searchPath string, limit int) ([]string, bool, error) {
  113. if !strings.HasPrefix(pattern, "/") && !strings.HasPrefix(pattern, searchPath) {
  114. if !strings.HasSuffix(searchPath, "/") {
  115. searchPath += "/"
  116. }
  117. pattern = searchPath + pattern
  118. }
  119. fsys := os.DirFS("/")
  120. relPattern := strings.TrimPrefix(pattern, "/")
  121. var matches []fileInfo
  122. err := doublestar.GlobWalk(fsys, relPattern, func(path string, d fs.DirEntry) error {
  123. if d.IsDir() {
  124. return nil
  125. }
  126. if skipHidden(path) {
  127. return nil
  128. }
  129. info, err := d.Info()
  130. if err != nil {
  131. return nil // Skip files we can't access
  132. }
  133. absPath := "/" + path // Restore absolute path
  134. matches = append(matches, fileInfo{
  135. path: absPath,
  136. modTime: info.ModTime(),
  137. })
  138. if len(matches) >= limit*2 { // Collect more than needed for sorting
  139. return fs.SkipAll
  140. }
  141. return nil
  142. })
  143. if err != nil {
  144. return nil, false, fmt.Errorf("glob walk error: %w", err)
  145. }
  146. sort.Slice(matches, func(i, j int) bool {
  147. return matches[i].modTime.After(matches[j].modTime)
  148. })
  149. truncated := len(matches) > limit
  150. if truncated {
  151. matches = matches[:limit]
  152. }
  153. results := make([]string, len(matches))
  154. for i, m := range matches {
  155. results[i] = m.path
  156. }
  157. return results, truncated, nil
  158. }
  159. func skipHidden(path string) bool {
  160. // Check for hidden files (starting with a dot)
  161. base := filepath.Base(path)
  162. if base != "." && strings.HasPrefix(base, ".") {
  163. return true
  164. }
  165. // List of commonly ignored directories in development projects
  166. commonIgnoredDirs := map[string]bool{
  167. "node_modules": true,
  168. "vendor": true,
  169. "dist": true,
  170. "build": true,
  171. "target": true,
  172. ".git": true,
  173. ".idea": true,
  174. ".vscode": true,
  175. "__pycache__": true,
  176. "bin": true,
  177. "obj": true,
  178. "out": true,
  179. "coverage": true,
  180. "tmp": true,
  181. "temp": true,
  182. "logs": true,
  183. "generated": true,
  184. "bower_components": true,
  185. "jspm_packages": true,
  186. }
  187. // Check if any path component is in our ignore list
  188. parts := strings.SplitSeq(path, string(os.PathSeparator))
  189. for part := range parts {
  190. if commonIgnoredDirs[part] {
  191. return true
  192. }
  193. }
  194. return false
  195. }