branch: externals/ellama
commit 99bf1ecef89d6d9938a7337f3d81f84480fc3fa8
Merge: 3ddbe73e86 258246350d
Author: Sergey Kostyaev <[email protected]>
Commit: GitHub <[email protected]>

    Merge pull request #363 from s-kostyaev/reading-files-by-lines
    
    Add file line counting, grep in file and range retrieval tools
---
 NEWS.org        |   5 +++
 ellama-tools.el | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 ellama.el       |   2 +-
 3 files changed, 106 insertions(+), 1 deletion(-)

diff --git a/NEWS.org b/NEWS.org
index 959fd2eab8..13b8e59188 100644
--- a/NEWS.org
+++ b/NEWS.org
@@ -1,3 +1,8 @@
+* Version 1.10.5
+- Added ~count_lines~ and ~lines_range~ tools for counting lines and retrieving
+  file content by line range.
+- Introduced ~grep_in_file~ tool with confirmation prompt and updated
+  ~ellama-tools-available~ list.
 * Version 1.10.4
 - Added a new tool called "ask_user" that allows the system to pose questions 
to
   the user and receive clarifications.
diff --git a/ellama-tools.el b/ellama-tools.el
index 2eb1a1c194..ee9ff58062 100644
--- a/ellama-tools.el
+++ b/ellama-tools.el
@@ -477,6 +477,27 @@ Replace OLDCONTENT with NEWCONTENT."
                 :description
                 "Grep SEARCH-STRING in directory files."))
 
+(defun ellama-tools-grep-in-file-tool (search-string file)
+  "Grep SEARCH-STRING in FILE."
+  (shell-command-to-string (format "grep --color=never -nh %s %s" 
search-string file)))
+
+(defun ellama-tools-grep-in-file-tool-confirm (search-string file)
+  "Confirm grepping for SEARCH-STRING in FILE."
+  (ellama-tools-confirm
+   (format "Allow grepping for %s in file %s?" search-string file)
+   'ellama-tools-grep-in-file-tool
+   (list search-string file)))
+
+(add-to-list
+ 'ellama-tools-available
+ (llm-make-tool :function
+                'ellama-tools-grep-in-file-tool-confirm
+                :name "grep_in_file"
+                :args (list
+                       '(:name "search_string" :type string :description 
"String to search for.")
+                       '(:name "file" :type file :description "File to search 
in."))
+                :description "Grep SEARCH-STRING in FILE."))
+
 (defun ellama-tools-list-tool ()
   "List all available tools."
   (json-encode (mapcar
@@ -631,5 +652,84 @@ ANSWER-VARIANT-LIST is a list of possible answer variants."
                 "Ask user a QUESTION to receive a clarification.
 ANSWER-VARIANT-LIST is a list of possible answer variants."))
 
+(defun ellama-tools-count-lines-tool (path)
+  "Count lines in file located at PATH."
+  (with-current-buffer (find-file-noselect path)
+    (count-lines (point-min) (point-max))))
+
+(defun ellama-tools-count-lines-tool-confirm (path)
+  "Count lines in file located at PATH."
+  (ellama-tools-confirm
+   (format "Allow counting lines in file %s?" path)
+   'ellama-tools-count-lines-tool
+   (list path)))
+
+(add-to-list
+ 'ellama-tools-available
+ (llm-make-tool :function
+                'ellama-tools-count-lines-tool-confirm
+                :name
+                "count_lines"
+                :args
+                (list '(:name
+                        "path"
+                        :type
+                        string
+                        :description
+                        "Path to the file to count lines in."))
+                :description
+                "Count lines in file located at PATH."))
+
+(defun ellama-tools-lines-range-tool (path from to)
+  "Return content of file located at PATH lines in range FROM TO."
+  (with-current-buffer (find-file-noselect path)
+    (save-excursion
+      (let ((start (progn
+                     (goto-char (point-min))
+                     (forward-line (1- from))
+                     (beginning-of-line)
+                     (point)))
+            (end (progn
+                   (goto-char (point-min))
+                   (forward-line (1- to))
+                   (end-of-line)
+                   (point))))
+        (buffer-substring-no-properties start end)))))
+
+(defun ellama-tools-lines-range-tool-confirm (path from to)
+  "Return content of file located at PATH lines in range FROM TO."
+  (ellama-tools-confirm
+   (format "Allow getting lines %d to %d from file %s?" from to path)
+   'ellama-tools-lines-range-tool
+   (list path from to)))
+
+(add-to-list
+ 'ellama-tools-available
+ (llm-make-tool :function
+                'ellama-tools-lines-range-tool-confirm
+                :name
+                "lines_range"
+                :args
+                (list '(:name
+                        "path"
+                        :type
+                        string
+                        :description
+                        "Path to the file to get lines from.")
+                      '(:name
+                        "from"
+                        :type
+                        number
+                        :description
+                        "Starting line number.")
+                      '(:name
+                        "to"
+                        :type
+                        number
+                        :description
+                        "Ending line number."))
+                :description
+                "Return content of file located at PATH lines in range FROM 
TO."))
+
 (provide 'ellama-tools)
 ;;; ellama-tools.el ends here
diff --git a/ellama.el b/ellama.el
index 9204c1f03b..5b173df62c 100644
--- a/ellama.el
+++ b/ellama.el
@@ -6,7 +6,7 @@
 ;; URL: http://github.com/s-kostyaev/ellama
 ;; Keywords: help local tools
 ;; Package-Requires: ((emacs "28.1") (llm "0.24.0") (plz "0.8") (transient 
"0.7") (compat "29.1"))
-;; Version: 1.10.4
+;; Version: 1.10.5
 ;; SPDX-License-Identifier: GPL-3.0-or-later
 ;; Created: 8th Oct 2023
 

Reply via email to