glob.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package tools
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. "os/exec"
  8. "path/filepath"
  9. "sort"
  10. "strings"
  11. "github.com/sst/opencode/internal/config"
  12. "github.com/sst/opencode/internal/fileutil"
  13. "github.com/sst/opencode/internal/status"
  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 GlobParams struct {
  47. Pattern string `json:"pattern"`
  48. Path string `json:"path"`
  49. }
  50. type GlobResponseMetadata struct {
  51. NumberOfFiles int `json:"number_of_files"`
  52. Truncated bool `json:"truncated"`
  53. }
  54. type globTool struct{}
  55. func NewGlobTool() BaseTool {
  56. return &globTool{}
  57. }
  58. func (g *globTool) Info() ToolInfo {
  59. return ToolInfo{
  60. Name: GlobToolName,
  61. Description: globDescription,
  62. Parameters: map[string]any{
  63. "pattern": map[string]any{
  64. "type": "string",
  65. "description": "The glob pattern to match files against",
  66. },
  67. "path": map[string]any{
  68. "type": "string",
  69. "description": "The directory to search in. Defaults to the current working directory.",
  70. },
  71. },
  72. Required: []string{"pattern"},
  73. }
  74. }
  75. func (g *globTool) Run(ctx context.Context, call ToolCall) (ToolResponse, error) {
  76. var params GlobParams
  77. if err := json.Unmarshal([]byte(call.Input), &params); err != nil {
  78. return NewTextErrorResponse(fmt.Sprintf("error parsing parameters: %s", err)), nil
  79. }
  80. if params.Pattern == "" {
  81. return NewTextErrorResponse("pattern is required"), nil
  82. }
  83. searchPath := params.Path
  84. if searchPath == "" {
  85. searchPath = config.WorkingDirectory()
  86. }
  87. files, truncated, err := globFiles(params.Pattern, searchPath, 100)
  88. if err != nil {
  89. return ToolResponse{}, fmt.Errorf("error finding files: %w", err)
  90. }
  91. var output string
  92. if len(files) == 0 {
  93. output = "No files found"
  94. } else {
  95. output = strings.Join(files, "\n")
  96. if truncated {
  97. output += "\n\n(Results are truncated. Consider using a more specific path or pattern.)"
  98. }
  99. }
  100. return WithResponseMetadata(
  101. NewTextResponse(output),
  102. GlobResponseMetadata{
  103. NumberOfFiles: len(files),
  104. Truncated: truncated,
  105. },
  106. ), nil
  107. }
  108. func globFiles(pattern, searchPath string, limit int) ([]string, bool, error) {
  109. cmdRg := fileutil.GetRgCmd(pattern)
  110. if cmdRg != nil {
  111. cmdRg.Dir = searchPath
  112. matches, err := runRipgrep(cmdRg, searchPath, limit)
  113. if err == nil {
  114. return matches, len(matches) >= limit && limit > 0, nil
  115. }
  116. status.Warn(fmt.Sprintf("Ripgrep execution failed: %v. Falling back to doublestar.", err))
  117. }
  118. return fileutil.GlobWithDoublestar(pattern, searchPath, limit)
  119. }
  120. func runRipgrep(cmd *exec.Cmd, searchRoot string, limit int) ([]string, error) {
  121. out, err := cmd.CombinedOutput()
  122. if err != nil {
  123. if ee, ok := err.(*exec.ExitError); ok && ee.ExitCode() == 1 {
  124. return nil, nil
  125. }
  126. return nil, fmt.Errorf("ripgrep: %w\n%s", err, out)
  127. }
  128. var matches []string
  129. for _, p := range bytes.Split(out, []byte{0}) {
  130. if len(p) == 0 {
  131. continue
  132. }
  133. absPath := string(p)
  134. if !filepath.IsAbs(absPath) {
  135. absPath = filepath.Join(searchRoot, absPath)
  136. }
  137. if fileutil.SkipHidden(absPath) {
  138. continue
  139. }
  140. matches = append(matches, absPath)
  141. }
  142. sort.SliceStable(matches, func(i, j int) bool {
  143. return len(matches[i]) < len(matches[j])
  144. })
  145. if limit > 0 && len(matches) > limit {
  146. matches = matches[:limit]
  147. }
  148. return matches, nil
  149. }