branch: elpa/gptel
commit 514746f897078adddc1d62e919ec3750da40b731
Author: Karthik Chikmagalur <[email protected]>
Commit: Karthik Chikmagalur <[email protected]>
gptel-request: Change post-request cleanup handlers API
Change the experimental per-request :post handler API for
`gptel-request'. Now each function in :post is passed the INFO
plist as an argument. This API is still internal/experimental,
since it's not yet clear what the best argument is.
Use the per-request :post API for deleting temporary JSON payload
files written to by `gptel-request'. This is cleaner than
accumulating these deletions in `gptel-post-response-functions',
which runs only after gptel-send and is not request-specific.
For the same reasons, deleting the Org conversion buffers also
needs to happen in the :post hook, and will be moved over from
`gptel-post-response-functions' in the future.
* gptel-request.el (gptel-request--handlers, gptel--handle-post,
gptel-curl--get-args): Use the :post functions list to handle temp
file deletion. Add transition handlers to
`gptel-request--transitions' to ensure that the :post hook always
runs.
* gptel.el (gptel--handle-post-insert, gptel--handle-error,
gptel--handle-abort): Change the calls to the :post functions to
pass them the INFO plist.
---
gptel-request.el | 30 ++++++++++++++++++------------
gptel.el | 24 ++++++------------------
2 files changed, 24 insertions(+), 30 deletions(-)
diff --git a/gptel-request.el b/gptel-request.el
index ff7cd3ce8a3..00cc34736cc 100644
--- a/gptel-request.el
+++ b/gptel-request.el
@@ -1618,7 +1618,10 @@ considered a success and acts as a default.")
(defvar gptel-request--handlers
`((WAIT ,#'gptel--handle-wait)
- (TOOL ,#'gptel--handle-tool-use))
+ (TOOL ,#'gptel--handle-tool-use)
+ (DONE ,#'gptel--handle-post)
+ (ERRS ,#'gptel--handle-post)
+ (ABRT ,#'gptel--handle-post))
"Alist specifying handlers for gptel's default state transitions.
Each entry is a list whose car is a request state (a symbol) and
@@ -1778,6 +1781,12 @@ MACHINE is an instance of `gptel-fsm'"
(funcall (plist-get info :callback)
(cons 'tool-call pending-calls) info))))))
+(defun gptel--handle-post (fsm)
+ "Run cleanup for `gptel-request' with FSM."
+ (when-let* ((info (gptel-fsm-info fsm))
+ (post (plist-get info :post)))
+ (mapc (lambda (f) (funcall f info)) post)))
+
;;;; State machine predicates
;; Predicates used to find the next state to transition to, see
;; `gptel-request--transitions'.
@@ -2553,17 +2562,14 @@ INFO contains the request data, TOKEN is a unique
identifier."
(list (format "-w(%s . %%{size_header})" token))
(if (length< data-json gptel-curl-file-size-threshold)
(list (format "-d%s" data-json))
- (letrec
- ((write-region-inhibit-fsync t)
- (file-name-handler-alist nil)
- (temp-filename (make-temp-file "gptel-curl-data" nil ".json"
data-json))
- (cleanup-fn (lambda (&rest _)
- (when (file-exists-p temp-filename)
- (delete-file temp-filename)
- (remove-hook 'gptel-post-response-functions
cleanup-fn)))))
- (add-hook 'gptel-post-response-functions cleanup-fn)
- (list "--data-binary"
- (format "@%s" temp-filename))))
+ (let* ((write-region-inhibit-fsync t)
+ (file-name-handler-alist nil)
+ (inhibit-message t)
+ (temp-filename (make-temp-file "gptel-curl-data" nil ".json"
data-json))
+ (cleanup-fn (lambda (&rest _) (when (file-exists-p temp-filename)
+ (delete-file temp-filename)))))
+ (plist-put info :post (cons cleanup-fn (plist-get info :post)))
+ (list "--data-binary" (format "@%s" temp-filename))))
(when (not (string-empty-p gptel-proxy))
(list "--proxy" gptel-proxy
"--proxy-negotiate"
diff --git a/gptel.el b/gptel.el
index bc15b478d87..84656cd6f50 100644
--- a/gptel.el
+++ b/gptel.el
@@ -1139,16 +1139,12 @@ No state transition here since that's handled by the
process sentinels."
;; Run hook in visible window to set window-point, BUG #269
(if-let* ((gptel-window (get-buffer-window gptel-buffer 'visible)))
(with-selected-window gptel-window
- (mapc (lambda (f) (funcall f (marker-position start-marker)
- (marker-position tracking-marker)))
- (plist-get info :post))
+ (mapc (lambda (f) (funcall f info)) (plist-get info :post))
(run-hook-with-args
'gptel-post-response-functions
(marker-position start-marker) (marker-position tracking-marker)))
(with-current-buffer gptel-buffer
- (mapc (lambda (f) (funcall f (marker-position start-marker)
- (marker-position tracking-marker)))
- (plist-get info :post))
+ (mapc (lambda (f) (funcall f info)) (plist-get info :post))
(run-hook-with-args
'gptel-post-response-functions
(marker-position start-marker) (marker-position tracking-marker))))
@@ -1187,16 +1183,12 @@ Perform UI updates and run post-response hooks."
(string-trim (gptel--to-string error-msg)))))
(if-let* ((gptel-window (get-buffer-window gptel-buffer 'visible)))
(with-selected-window gptel-window
- (mapc (lambda (f) (funcall f (marker-position start-marker)
- (marker-position tracking-marker)))
- (plist-get info :post))
+ (mapc (lambda (f) (funcall f info)) (plist-get info :post))
(run-hook-with-args
'gptel-post-response-functions
(marker-position start-marker) (marker-position tracking-marker)))
(with-current-buffer gptel-buffer
- (mapc (lambda (f) (funcall f (marker-position start-marker)
- (marker-position tracking-marker)))
- (plist-get info :post))
+ (mapc (lambda (f) (funcall f info)) (plist-get info :post))
(run-hook-with-args
'gptel-post-response-functions
(marker-position start-marker) (marker-position tracking-marker))))
@@ -1214,16 +1206,12 @@ Perform UI updates and run post-response hooks."
start-marker)))
(if-let* ((gptel-window (get-buffer-window gptel-buffer 'visible)))
(with-selected-window gptel-window
- (mapc (lambda (f) (funcall f (marker-position start-marker)
- (marker-position tracking-marker)))
- (plist-get info :post))
+ (mapc (lambda (f) (funcall f info)) (plist-get info :post))
(run-hook-with-args
'gptel-post-response-functions
(marker-position start-marker) (marker-position tracking-marker)))
(with-current-buffer gptel-buffer
- (mapc (lambda (f) (funcall f (marker-position start-marker)
- (marker-position tracking-marker)))
- (plist-get info :post))
+ (mapc (lambda (f) (funcall f info)) (plist-get info :post))
(run-hook-with-args
'gptel-post-response-functions
(marker-position start-marker) (marker-position tracking-marker))))