branch: externals/ellama
commit 4c1391eab228d8a7a12715ea444a4ed006f751af
Author: Sergey Kostyaev <[email protected]>
Commit: Sergey Kostyaev <[email protected]>

    Add file line counting and range retrieval tools
    
    Added two new tools to ellama-tools:
    1. count_lines - counts the number of lines in a file
    2. lines_range - retrieves content from a file within a specified line range
    
    Both tools include confirmation prompts for security and are integrated 
into the
    ellama-tools-available list. The tools are implemented with proper error
    handling and use Emacs buffer operations to access file content.
---
 ellama-tools.el | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 79 insertions(+)

diff --git a/ellama-tools.el b/ellama-tools.el
index 2eb1a1c194..202862f5d6 100644
--- a/ellama-tools.el
+++ b/ellama-tools.el
@@ -631,5 +631,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

Reply via email to