grep.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. package tools
  2. import (
  3. "bufio"
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. "os"
  8. "os/exec"
  9. "path/filepath"
  10. "regexp"
  11. "sort"
  12. "strconv"
  13. "strings"
  14. "time"
  15. "github.com/sst/opencode/internal/config"
  16. "github.com/sst/opencode/internal/fileutil"
  17. )
  18. type GrepParams struct {
  19. Pattern string `json:"pattern"`
  20. Path string `json:"path"`
  21. Include string `json:"include"`
  22. LiteralText bool `json:"literal_text"`
  23. }
  24. type grepMatch struct {
  25. path string
  26. modTime time.Time
  27. lineNum int
  28. lineText string
  29. }
  30. type GrepResponseMetadata struct {
  31. NumberOfMatches int `json:"number_of_matches"`
  32. Truncated bool `json:"truncated"`
  33. }
  34. type grepTool struct{}
  35. const (
  36. GrepToolName = "grep"
  37. grepDescription = `Fast content search tool that finds files containing specific text or patterns, returning matching file paths sorted by modification time (newest first).
  38. WHEN TO USE THIS TOOL:
  39. - Use when you need to find files containing specific text or patterns
  40. - Great for searching code bases for function names, variable declarations, or error messages
  41. - Useful for finding all files that use a particular API or pattern
  42. HOW TO USE:
  43. - Provide a regex pattern to search for within file contents
  44. - Set literal_text=true if you want to search for the exact text with special characters (recommended for non-regex users)
  45. - Optionally specify a starting directory (defaults to current working directory)
  46. - Optionally provide an include pattern to filter which files to search
  47. - Results are sorted with most recently modified files first
  48. REGEX PATTERN SYNTAX (when literal_text=false):
  49. - Supports standard regular expression syntax
  50. - 'function' searches for the literal text "function"
  51. - 'log\..*Error' finds text starting with "log." and ending with "Error"
  52. - 'import\s+.*\s+from' finds import statements in JavaScript/TypeScript
  53. COMMON INCLUDE PATTERN EXAMPLES:
  54. - '*.js' - Only search JavaScript files
  55. - '*.{ts,tsx}' - Only search TypeScript files
  56. - '*.go' - Only search Go files
  57. LIMITATIONS:
  58. - Results are limited to 100 files (newest first)
  59. - Performance depends on the number of files being searched
  60. - Very large binary files may be skipped
  61. - Hidden files (starting with '.') are skipped
  62. TIPS:
  63. - For faster, more targeted searches, first use Glob to find relevant files, then use Grep
  64. - When doing iterative exploration that may require multiple rounds of searching, consider using the Agent tool instead
  65. - Always check if results are truncated and refine your search pattern if needed
  66. - Use literal_text=true when searching for exact text containing special characters like dots, parentheses, etc.`
  67. )
  68. func NewGrepTool() BaseTool {
  69. return &grepTool{}
  70. }
  71. func (g *grepTool) Info() ToolInfo {
  72. return ToolInfo{
  73. Name: GrepToolName,
  74. Description: grepDescription,
  75. Parameters: map[string]any{
  76. "pattern": map[string]any{
  77. "type": "string",
  78. "description": "The regex pattern to search for in file contents",
  79. },
  80. "path": map[string]any{
  81. "type": "string",
  82. "description": "The directory to search in. Defaults to the current working directory.",
  83. },
  84. "include": map[string]any{
  85. "type": "string",
  86. "description": "File pattern to include in the search (e.g. \"*.js\", \"*.{ts,tsx}\")",
  87. },
  88. "literal_text": map[string]any{
  89. "type": "boolean",
  90. "description": "If true, the pattern will be treated as literal text with special regex characters escaped. Default is false.",
  91. },
  92. },
  93. Required: []string{"pattern"},
  94. }
  95. }
  96. // escapeRegexPattern escapes special regex characters so they're treated as literal characters
  97. func escapeRegexPattern(pattern string) string {
  98. specialChars := []string{"\\", ".", "+", "*", "?", "(", ")", "[", "]", "{", "}", "^", "$", "|"}
  99. escaped := pattern
  100. for _, char := range specialChars {
  101. escaped = strings.ReplaceAll(escaped, char, "\\"+char)
  102. }
  103. return escaped
  104. }
  105. func (g *grepTool) Run(ctx context.Context, call ToolCall) (ToolResponse, error) {
  106. var params GrepParams
  107. if err := json.Unmarshal([]byte(call.Input), &params); err != nil {
  108. return NewTextErrorResponse(fmt.Sprintf("error parsing parameters: %s", err)), nil
  109. }
  110. if params.Pattern == "" {
  111. return NewTextErrorResponse("pattern is required"), nil
  112. }
  113. // If literal_text is true, escape the pattern
  114. searchPattern := params.Pattern
  115. if params.LiteralText {
  116. searchPattern = escapeRegexPattern(params.Pattern)
  117. }
  118. searchPath := params.Path
  119. if searchPath == "" {
  120. searchPath = config.WorkingDirectory()
  121. }
  122. matches, truncated, err := searchFiles(searchPattern, searchPath, params.Include, 100)
  123. if err != nil {
  124. return ToolResponse{}, fmt.Errorf("error searching files: %w", err)
  125. }
  126. var output string
  127. if len(matches) == 0 {
  128. output = "No files found"
  129. } else {
  130. output = fmt.Sprintf("Found %d matches\n", len(matches))
  131. currentFile := ""
  132. for _, match := range matches {
  133. if currentFile != match.path {
  134. if currentFile != "" {
  135. output += "\n"
  136. }
  137. currentFile = match.path
  138. output += fmt.Sprintf("%s:\n", match.path)
  139. }
  140. if match.lineNum > 0 {
  141. output += fmt.Sprintf(" Line %d: %s\n", match.lineNum, match.lineText)
  142. } else {
  143. output += fmt.Sprintf(" %s\n", match.path)
  144. }
  145. }
  146. if truncated {
  147. output += "\n(Results are truncated. Consider using a more specific path or pattern.)"
  148. }
  149. }
  150. return WithResponseMetadata(
  151. NewTextResponse(output),
  152. GrepResponseMetadata{
  153. NumberOfMatches: len(matches),
  154. Truncated: truncated,
  155. },
  156. ), nil
  157. }
  158. func searchFiles(pattern, rootPath, include string, limit int) ([]grepMatch, bool, error) {
  159. matches, err := searchWithRipgrep(pattern, rootPath, include)
  160. if err != nil {
  161. matches, err = searchFilesWithRegex(pattern, rootPath, include)
  162. if err != nil {
  163. return nil, false, err
  164. }
  165. }
  166. sort.Slice(matches, func(i, j int) bool {
  167. return matches[i].modTime.After(matches[j].modTime)
  168. })
  169. truncated := len(matches) > limit
  170. if truncated {
  171. matches = matches[:limit]
  172. }
  173. return matches, truncated, nil
  174. }
  175. func searchWithRipgrep(pattern, path, include string) ([]grepMatch, error) {
  176. _, err := exec.LookPath("rg")
  177. if err != nil {
  178. return nil, fmt.Errorf("ripgrep not found: %w", err)
  179. }
  180. // Use -n to show line numbers and include the matched line
  181. args := []string{"-n", pattern}
  182. if include != "" {
  183. args = append(args, "--glob", include)
  184. }
  185. args = append(args, path)
  186. cmd := exec.Command("rg", args...)
  187. output, err := cmd.Output()
  188. if err != nil {
  189. if exitErr, ok := err.(*exec.ExitError); ok && exitErr.ExitCode() == 1 {
  190. return []grepMatch{}, nil
  191. }
  192. return nil, err
  193. }
  194. lines := strings.Split(strings.TrimSpace(string(output)), "\n")
  195. matches := make([]grepMatch, 0, len(lines))
  196. for _, line := range lines {
  197. if line == "" {
  198. continue
  199. }
  200. // Parse ripgrep output format: file:line:content
  201. parts := strings.SplitN(line, ":", 3)
  202. if len(parts) < 3 {
  203. continue
  204. }
  205. filePath := parts[0]
  206. lineNum, err := strconv.Atoi(parts[1])
  207. if err != nil {
  208. continue
  209. }
  210. lineText := parts[2]
  211. fileInfo, err := os.Stat(filePath)
  212. if err != nil {
  213. continue // Skip files we can't access
  214. }
  215. matches = append(matches, grepMatch{
  216. path: filePath,
  217. modTime: fileInfo.ModTime(),
  218. lineNum: lineNum,
  219. lineText: lineText,
  220. })
  221. }
  222. return matches, nil
  223. }
  224. func searchFilesWithRegex(pattern, rootPath, include string) ([]grepMatch, error) {
  225. matches := []grepMatch{}
  226. regex, err := regexp.Compile(pattern)
  227. if err != nil {
  228. return nil, fmt.Errorf("invalid regex pattern: %w", err)
  229. }
  230. var includePattern *regexp.Regexp
  231. if include != "" {
  232. regexPattern := globToRegex(include)
  233. includePattern, err = regexp.Compile(regexPattern)
  234. if err != nil {
  235. return nil, fmt.Errorf("invalid include pattern: %w", err)
  236. }
  237. }
  238. err = filepath.Walk(rootPath, func(path string, info os.FileInfo, err error) error {
  239. if err != nil {
  240. return nil // Skip errors
  241. }
  242. if info.IsDir() {
  243. return nil // Skip directories
  244. }
  245. if fileutil.SkipHidden(path) {
  246. return nil
  247. }
  248. if includePattern != nil && !includePattern.MatchString(path) {
  249. return nil
  250. }
  251. match, lineNum, lineText, err := fileContainsPattern(path, regex)
  252. if err != nil {
  253. return nil // Skip files we can't read
  254. }
  255. if match {
  256. matches = append(matches, grepMatch{
  257. path: path,
  258. modTime: info.ModTime(),
  259. lineNum: lineNum,
  260. lineText: lineText,
  261. })
  262. if len(matches) >= 200 {
  263. return filepath.SkipAll
  264. }
  265. }
  266. return nil
  267. })
  268. if err != nil {
  269. return nil, err
  270. }
  271. return matches, nil
  272. }
  273. func fileContainsPattern(filePath string, pattern *regexp.Regexp) (bool, int, string, error) {
  274. file, err := os.Open(filePath)
  275. if err != nil {
  276. return false, 0, "", err
  277. }
  278. defer file.Close()
  279. scanner := bufio.NewScanner(file)
  280. lineNum := 0
  281. for scanner.Scan() {
  282. lineNum++
  283. line := scanner.Text()
  284. if pattern.MatchString(line) {
  285. return true, lineNum, line, nil
  286. }
  287. }
  288. return false, 0, "", scanner.Err()
  289. }
  290. func globToRegex(glob string) string {
  291. regexPattern := strings.ReplaceAll(glob, ".", "\\.")
  292. regexPattern = strings.ReplaceAll(regexPattern, "*", ".*")
  293. regexPattern = strings.ReplaceAll(regexPattern, "?", ".")
  294. re := regexp.MustCompile(`\{([^}]+)\}`)
  295. regexPattern = re.ReplaceAllStringFunc(regexPattern, func(match string) string {
  296. inner := match[1 : len(match)-1]
  297. return "(" + strings.ReplaceAll(inner, ",", "|") + ")"
  298. })
  299. return regexPattern
  300. }