grep.go 7.4 KB

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