Skip to content

Commit a8524fd

Browse files
committed
gptel: Fix overlay handling in tool call confirmation cleanup
* gptel.el (gptel--accept-tool-calls, gptel--reject-tool-calls): When cleaning up the tool call confirmation overlays, set the buffer to the overlay buffer. Otherwise this creates issues when the tool call changes the active Emacs buffer, which happpens often when an LLM is controlling Emacs.
1 parent c89d61d commit a8524fd

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

gptel.el

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1878,15 +1878,16 @@ NAME and ARG-VALUES are the name and arguments for the call."
18781878
(apply (gptel-tool-function tool-spec) arg-values)
18791879
(error (mapconcat #'gptel--to-string errdata " ")))))
18801880
(funcall process-tool-result result))))
1881-
(when (overlayp ov)
1882-
(when-let* ((preview-handles (overlay-get ov 'previews)))
1883-
(dolist (func-to-handle preview-handles)
1884-
(when (car func-to-handle) (apply func-to-handle))))
1885-
(when-let* ((prompt-ov (overlay-get ov 'prompt))
1886-
((overlay-buffer prompt-ov))
1887-
(inhibit-read-only t))
1888-
(delete-region (overlay-start prompt-ov)
1889-
(overlay-end prompt-ov)))
1881+
(when (and (overlayp ov) (overlay-buffer ov))
1882+
(with-current-buffer (overlay-buffer ov)
1883+
(when-let* ((preview-handles (overlay-get ov 'previews)))
1884+
(dolist (func-to-handle preview-handles)
1885+
(when (car func-to-handle) (apply func-to-handle))))
1886+
(when-let* ((prompt-ov (overlay-get ov 'prompt))
1887+
((overlay-buffer prompt-ov))
1888+
(inhibit-read-only t))
1889+
(delete-region (overlay-start prompt-ov)
1890+
(overlay-end prompt-ov))))
18901891
(delete-overlay ov)))
18911892

18921893
(defun gptel--reject-tool-calls (&optional _response ov)
@@ -1896,15 +1897,16 @@ NAME and ARG-VALUES are the name and arguments for the call."
18961897
(gptel--update-status " Tools cancelled" 'error)
18971898
(message (substitute-command-keys
18981899
"Tool calls canceled. \\[gptel-menu] to continue them!"))
1899-
(when (overlayp ov)
1900-
(when-let* ((preview-handles (overlay-get ov 'previews)))
1901-
(dolist (func-to-handle preview-handles)
1902-
(when (car func-to-handle) (apply func-to-handle))))
1903-
(when-let* ((prompt-ov (overlay-get ov 'prompt))
1904-
((overlay-buffer prompt-ov))
1905-
(inhibit-read-only t))
1906-
(delete-region (overlay-start prompt-ov)
1907-
(overlay-end prompt-ov)))
1900+
(when (and (overlayp ov) (overlay-buffer ov))
1901+
(with-current-buffer (overlay-buffer ov)
1902+
(when-let* ((preview-handles (overlay-get ov 'previews)))
1903+
(dolist (func-to-handle preview-handles)
1904+
(when (car func-to-handle) (apply func-to-handle))))
1905+
(when-let* ((prompt-ov (overlay-get ov 'prompt))
1906+
((overlay-buffer prompt-ov))
1907+
(inhibit-read-only t))
1908+
(delete-region (overlay-start prompt-ov)
1909+
(overlay-end prompt-ov))))
19081910
(delete-overlay ov)))
19091911

19101912
(defun gptel--dispatch-tool-calls (choice)

0 commit comments

Comments
 (0)