From e256030cf5a23a652bb310bd6f2fbd1f2f7d9ffd Mon Sep 17 00:00:00 2001 From: Vineet Kumar Date: Tue, 26 May 2026 01:20:57 -0400 Subject: fix grep tool --- llm-tools.el | 51 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/llm-tools.el b/llm-tools.el index c0f859e..905a072 100644 --- a/llm-tools.el +++ b/llm-tools.el @@ -5,7 +5,7 @@ ;; read_file (hl-file-read) (gptel-make-tool :name "read_file" - :function 'llm-tools--hl-file-read + :function #'llm-tools--hl-file-read :description "\ Read a file (or a range of lines) and return its contents in hashline format. Each line is represented as LINE_NUMBER HASH|CONTENT, where @@ -346,23 +346,32 @@ or editing them." :include t) ;; grep (just grep (or git grep) :D) +(defun llm-tools--grep (regexp &optional glob path) + (with-temp-buffer + (if (magit-gitdir) + (let ((pathspec (cond + ((and path glob) + (concat (directory-file-name path) "/" glob)) + (glob glob) + (path path) + (t ".")))) + (call-process "git" nil t nil + "grep" "-rnEH" + regexp "--" pathspec)) + (apply #'call-process + (append + '("grep" nil t nil) + (delq nil + `("-rnEH" + ,(if glob (concat "--include=" glob)) + ,regexp + ,(or path ".")))))) + (buffer-string))) + (gptel-make-tool :name "grep" :function (lambda (regexp &optional glob path) - (with-temp-buffer - (if (magit-gitdir) - (call-process "git" nil t nil - "grep" - "-rnEH" - regexp - (and glob (format "--include=%s" glob)) - (or path ".")) - (call-process "grep" nil t nil - "-rnEH" - regexp - (and glob (format "--include=%s" glob)) - (or path "."))) - (buffer-string))) + (llm-tools--grep regexp glob path)) :description "\ Recursively search for lines matching a regexp pattern in files under a given path. Uses `git grep` when inside a Git repository, otherwise falls @@ -379,17 +388,17 @@ omit it to search from the current directory." :type string :description "\ Regular expression pattern to search for. Uses GNU grep extended syntax.") - (:name "path" - :type string - :description "\ -Optional file or directory path to search within, relative to the -working directory. Defaults to current directory if omitted." - :optional t) (:name "glob" :type string :description "\ Optional restricts search to files matching the glob pattern. (e.g. \"*.el\", \"*.org\", \"*.c\")" + :optional t) + (:name "path" + :type string + :description "\ +Optional file or directory path to search within, relative to the +working directory. Defaults to current directory if omitted." :optional t)) :async nil :confirm nil -- cgit v1.2.3