branch: externals/ellama
commit c15b4e2ec021cb672fb524c347ea68cbb90974d9
Author: Sergey Kostyaev <[email protected]>
Commit: Sergey Kostyaev <[email protected]>
Refactor tool confirmation and response handling
This commit refactors the tool confirmation logic in ellama-tools.el to
improve
code structure and adds JSON encoding for tool lists. The response handling
in
ellama.el is also simplified by restructuring the conditional logic for
async
requests, ensuring consistent behavior whether async is enabled or not.
---
ellama-tools.el | 49 ++++++++++++++++++++++++++-----------------------
ellama.el | 22 ++++++++++------------
2 files changed, 36 insertions(+), 35 deletions(-)
diff --git a/ellama-tools.el b/ellama-tools.el
index 3eda3b7255..c1149c2a52 100644
--- a/ellama-tools.el
+++ b/ellama-tools.el
@@ -57,24 +57,27 @@ otherwise."
(funcall function))))
;; Otherwise, ask for confirmation
(t
- (let ((answer (read-char-choice
- (format "%s (y)es, (a)lways, (n)o: " prompt)
- '(?y ?a ?n))))
- (cond
- ;; Yes - execute function once
- ((eq answer ?y)
- (if args
- (apply function args)
- (funcall function)))
- ;; Always - remember approval and execute function
- ((eq answer ?a)
- (puthash function t ellama-tools-confirm-allowed)
- (if args
- (apply function args)
- (funcall function)))
- ;; No - return nil
- ((eq answer ?n)
- "Forbidden by the user")))))))
+ (let* ((answer (read-char-choice
+ (format "%s (y)es, (a)lways, (n)o: " prompt)
+ '(?y ?a ?n)))
+ (result (cond
+ ;; Yes - execute function once
+ ((eq answer ?y)
+ (if args
+ (apply function args)
+ (funcall function)))
+ ;; Always - remember approval and execute function
+ ((eq answer ?a)
+ (puthash function t ellama-tools-confirm-allowed)
+ (if args
+ (apply function args)
+ (funcall function)))
+ ;; No - return nil
+ ((eq answer ?n)
+ "Forbidden by the user"))))
+ (if (stringp result)
+ result
+ (json-encode result)))))))
(defun ellama-tools--enable-by-name (name)
"Add to `ellama-tools-enabled' each tool that matches NAME."
@@ -421,11 +424,11 @@ Replace OLDCONTENT with NEWCONTENT."
(defun ellama-tools--list ()
"List all available tools."
- (mapcar
- (lambda (tool)
- `(("name" . ,(llm-tool-name tool))
- ("description" . ,(llm-tool-description tool))))
- ellama-tools-available))
+ (json-encode (mapcar
+ (lambda (tool)
+ `(("name" . ,(llm-tool-name tool))
+ ("description" . ,(llm-tool-description tool))))
+ ellama-tools-available)))
(defun ellama-tools-list ()
"List all available tools."
diff --git a/ellama.el b/ellama.el
index ac0bc8f8cf..6e1231381a 100644
--- a/ellama.el
+++ b/ellama.el
@@ -1391,22 +1391,20 @@ ASYNC flag is for asyncronous requests."
(reasoning (plist-get response :reasoning))
(tool-result (plist-get response :tool-results)))
(if tool-result
- (progn
- (message "tool result: %s\nprompt: %s" tool-result llm-prompt)
- (if async
- (llm-chat-async
- provider
- llm-prompt
- (ellama--response-handler handler reasoning-buffer buffer
donecb errcb provider llm-prompt async)
- (ellama--error-handler buffer errcb)
- t)
- (llm-chat-streaming
+ (if async
+ (llm-chat-async
provider
llm-prompt
- handler
(ellama--response-handler handler reasoning-buffer buffer donecb
errcb provider llm-prompt async)
(ellama--error-handler buffer errcb)
- t)))
+ t)
+ (llm-chat-streaming
+ provider
+ llm-prompt
+ handler
+ (ellama--response-handler handler reasoning-buffer buffer donecb
errcb provider llm-prompt async)
+ (ellama--error-handler buffer errcb)
+ t))
(funcall handler response)
(when (or ellama--current-session
(not reasoning))