summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVineet Kumar <git@vineetk.net>2026-05-24 15:06:03 -0400
committerVineet Kumar <git@vineetk.net>2026-05-24 15:09:59 -0400
commit137bec6d0706cfc341f99b12da52bb6932508580 (patch)
tree6c8136db671d1d6a44c7d336f35c54c06f793e40
parent4c1f5482333f0c8c3d0ce3279f59a6037c98c9b1 (diff)
add grep tool
-rw-r--r--llm-tools.el50
1 files changed, 46 insertions, 4 deletions
diff --git a/llm-tools.el b/llm-tools.el
index 2218ab8..bbae522 100644
--- a/llm-tools.el
+++ b/llm-tools.el
@@ -60,11 +60,11 @@ final file."))
60;; TODO edit_file (hashline + patch apply) 60;; TODO edit_file (hashline + patch apply)
61 61
62;; TODO bash_readonly (shell-command-to-string, whitelisted commands) 62;; TODO bash_readonly (shell-command-to-string, whitelisted commands)
63;; TODO compile command (calls build system, doesn't neenfirmation, whitelisted commands) 63;; TODO compile command (calls build system, doesn't need confirmation, whitelisted commands)
64 64
65;; bash (make-process, taken from gptel-agent) 65;; bash (make-process, taken from gptel-agent)
66;; TODO refuse destructive operations like rm and dd, and refuse 66;; TODO refuse destructive operations like rm and dd
67;; operations that have dedicated tool definitions like grep and compile 67;; TODO refuse operations that have dedicated tool definitions like grep and compile
68(defun llm-tools--execute-bash (callback command) 68(defun llm-tools--execute-bash (callback command)
69 "Execute COMMAND asynchronously in bash and call CALLBACK with output. 69 "Execute COMMAND asynchronously in bash and call CALLBACK with output.
70 70
@@ -150,6 +150,48 @@ or editing them."
150 :confirm nil 150 :confirm nil
151 :include t) 151 :include t)
152 152
153;; TODO grep (just grep :D) 153;; grep (just grep (or git grep) :D)
154(gptel-make-tool
155 :name "grep"
156 :function (lambda (regex &optional path)
157 (with-temp-buffer
158 (if (magit-gitdir)
159 (call-process "git" nil t nil
160 "--no-pager"
161 "grep"
162 "-rnEH"
163 regex
164 (or path "."))
165 (call-process "grep" nil t nil
166 "-rnEH"
167 regex
168 (or path ".")))
169 (buffer-string)))
170 :description "\
171Recursively search for lines matching a regex pattern in files under a
172given path. Uses `git grep` when inside a Git repository, otherwise falls
173back to GNU grep. Both are invoked with flags -rnEH (recursive, line
174numbers, extended regex, always show filename). When using `git grep`,
175only tracked files are searched. Returns raw output with one match per
176line in the format FILE:LINE:CONTENT. Use this tool to find code
177patterns, locate function definitions, search for strings, or identify
178files containing specific text before reading them. For simple file name
179discovery, use `find_files` instead. For reading file contents, use
180`read_file`. The path argument is relative to the working directory;
181omit it to search from the current directory."
182 :args '((:name "regex"
183 :type string
184 :description "\
185Regular expression pattern to search for. Uses GNU grep extended syntax.")
186 (:name "path"
187 :type string
188 :description "\
189Optional file or directory path to search within, relative to the
190working directory. Defaults to current directory if omitted."
191 :optional t))
192 :async nil
193 :confirm nil
194 :include t)
195
154;; TODO fetch_url (use gptel-agent's one as a base, but needs mime handling like PDFs and images) 196;; TODO fetch_url (use gptel-agent's one as a base, but needs mime handling like PDFs and images)
155;; TODO web_search (use gptel-agent's eww/shr method as a base) 197;; TODO web_search (use gptel-agent's eww/shr method as a base)