branch: externals/ellama
commit e421fac701b1bd9d457fea215eb6f6c912a16f4f
Merge: 52df399929 8bbb3bf1c5
Author: Sergey Kostyaev <[email protected]>
Commit: GitHub <[email protected]>

    Merge pull request #378 from s-kostyaev/simplify-confirm-logic
    
    Improve loop control in confirmation dialog
---
 NEWS.org        |  5 ++++
 ellama-tools.el | 90 ++++++++++++++++++++++++++++-----------------------------
 ellama.el       |  2 +-
 3 files changed, 51 insertions(+), 46 deletions(-)

diff --git a/NEWS.org b/NEWS.org
index 5f90a0f471..7dc3f3f6cf 100644
--- a/NEWS.org
+++ b/NEWS.org
@@ -1,3 +1,8 @@
+* Version 1.12.3
+- Improve loop control in confirmation dialog. Refactored the while loop in the
+  confirmation prompt to use explicit t/nil returns for loop continuation
+  instead of throw/catch. This improves code readability and maintainability
+  while preserving all existing functionality.
 * Version 1.12.2
 - Add view and reply options to ellama-tools confirmation.
 - Remove tools list tool to minimize LLMs distraction.
diff --git a/ellama-tools.el b/ellama-tools.el
index 4bd82f9ea0..ad78c26e09 100644
--- a/ellama-tools.el
+++ b/ellama-tools.el
@@ -156,51 +156,51 @@ approved, \"Forbidden by the user\" otherwise."
               (prompt (format "Allow calling %s with arguments: %s?"
                               function-name
                               (mapconcat #'identity args-display ", ")))
-              answer result)
-         (catch 'done
-           (while t
-             (setq answer (read-char-choice
-                           (format "%s (y)es, (a)lways, (n)o, (r)eply, (v)iew: 
" prompt)
-                           '(?y ?a ?n ?r ?v)))
-             (cond
-              ;; View - show buffer with full details
-              ((eq answer ?v)
-               (let* ((buf (get-buffer-create "*Ellama Confirmation*"))
-                      (args-full
-                       (mapcar (lambda (arg)
-                                 (cond
-                                  ((stringp arg)
-                                   arg)
-                                  (t
-                                   (format "%S" arg))))
-                               args)))
-                 (with-current-buffer buf
-                   (erase-buffer)
-                   (insert (propertize "Ellama Function Call Confirmation\n"
-                                       'face '(:weight bold :height 1.2)))
-                   (insert "\n")
-                   (insert (format "Function: %s\n\n" function-name))
-                   (insert "Arguments:\n")
-                   (dolist (arg args-full)
-                     (insert (format "  - %s\n" arg))))
-                 (display-buffer buf)))
-              ;; Yes - execute function once
-              ((eq answer ?y)
-               (setq result (apply function args))
-               (throw 'done t))
-              ;; Always - remember approval and execute function
-              ((eq answer ?a)
-               (puthash function t ellama-tools-confirm-allowed)
-               (setq result (apply function args))
-               (throw 'done t))
-              ;; No - return nil
-              ((eq answer ?n)
-               (setq result "Forbidden by the user")
-               (throw 'done t))
-              ;; Reply - custom response
-              ((eq answer ?r)
-               (setq result (read-string "Answer to the agent: "))
-               (throw 'done t)))))
+              result)
+          (while
+              (let ((answer (read-char-choice
+                             (format "%s (y)es, (a)lways, (n)o, (r)eply, 
(v)iew: " prompt)
+                             '(?y ?a ?n ?r ?v))))
+                (cond
+                 ;; View - show buffer with full details
+                 ((eq answer ?v)
+                  (let* ((buf (get-buffer-create "*Ellama Confirmation*"))
+                         (args-full
+                          (mapcar (lambda (arg)
+                                    (cond
+                                     ((stringp arg)
+                                      arg)
+                                     (t
+                                      (format "%S" arg))))
+                                  args)))
+                    (with-current-buffer buf
+                      (erase-buffer)
+                      (insert (propertize "Ellama Function Call Confirmation\n"
+                                          'face '(:weight bold :height 1.2)))
+                      (insert "\n")
+                      (insert (format "Function: %s\n\n" function-name))
+                      (insert "Arguments:\n")
+                      (dolist (arg args-full)
+                        (insert (format "  - %s\n" arg))))
+                    (display-buffer buf))
+                  t) ;; Try again.
+                 ;; Yes - execute function once
+                 ((eq answer ?y)
+                  (setq result (apply function args))
+                  nil) ;; Done.
+                 ;; Always - remember approval and execute function
+                 ((eq answer ?a)
+                  (puthash function t ellama-tools-confirm-allowed)
+                  (setq result (apply function args))
+                  nil) ;; done
+                 ;; No - return nil
+                 ((eq answer ?n)
+                  (setq result "Forbidden by the user")
+                  nil) ;; Done.
+                 ;; Reply - custom response
+                 ((eq answer ?r)
+                  (setq result (read-string "Answer to the agent: "))
+                  nil))))
          (when result (if (stringp result)
                           result
                         (json-encode result)))))))))
diff --git a/ellama.el b/ellama.el
index a1924d7d5c..11582f3697 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") (yaml "1.2.3"))
-;; Version: 1.12.2
+;; Version: 1.12.3
 ;; SPDX-License-Identifier: GPL-3.0-or-later
 ;; Created: 8th Oct 2023
 

Reply via email to