gptel: Add pre-response-hook
* gptel.el (gptel--insert-response, gptel-pre-response-hook): New user option `gptel-pre-response-hook' that runs before the response is inserted into the buffer. This can be used to prepare the buffer in some user-specified way for the response. * gptel-curl.el (gptel-curl--stream-filter): Run `gptel-pre-response-hook' before inserting streaming responses.
This commit is contained in:
parent
c20fba8247
commit
e0a7898645
2 changed files with 15 additions and 1 deletions
|
@ -236,7 +236,12 @@ See `gptel--url-get-response' for details."
|
||||||
(current-buffer))
|
(current-buffer))
|
||||||
'((display-buffer-reuse-window
|
'((display-buffer-reuse-window
|
||||||
display-buffer-pop-up-window)
|
display-buffer-pop-up-window)
|
||||||
(reusable-frames . visible)))))
|
(reusable-frames . visible))))
|
||||||
|
;; Run pre-response hook
|
||||||
|
(when (and (equal (plist-get proc-info :http-status) "200")
|
||||||
|
gptel-pre-response-hook)
|
||||||
|
(with-current-buffer (marker-buffer (plist-get proc-info :position))
|
||||||
|
(run-hooks 'gptel-pre-response-hook))))
|
||||||
|
|
||||||
(when-let ((http-msg (plist-get proc-info :status))
|
(when-let ((http-msg (plist-get proc-info :status))
|
||||||
(http-status (plist-get proc-info :http-status)))
|
(http-status (plist-get proc-info :http-status)))
|
||||||
|
|
9
gptel.el
9
gptel.el
|
@ -134,6 +134,14 @@ return the transformed string."
|
||||||
:group 'gptel
|
:group 'gptel
|
||||||
:type 'hook)
|
:type 'hook)
|
||||||
|
|
||||||
|
(defcustom gptel-pre-response-hook nil
|
||||||
|
"Hook run before inserting ChatGPT's response into the current buffer.
|
||||||
|
|
||||||
|
This hook is called in the buffer from which the prompt was sent
|
||||||
|
to ChatGPT. Note: this hook only runs if the request succeeds."
|
||||||
|
:group 'gptel
|
||||||
|
:type 'hook)
|
||||||
|
|
||||||
(defcustom gptel-post-response-hook nil
|
(defcustom gptel-post-response-hook nil
|
||||||
"Hook run after inserting ChatGPT's response into the current buffer.
|
"Hook run after inserting ChatGPT's response into the current buffer.
|
||||||
|
|
||||||
|
@ -569,6 +577,7 @@ See `gptel--url-get-response' for details."
|
||||||
(put-text-property 0 (length response) 'gptel 'response response)
|
(put-text-property 0 (length response) 'gptel 'response response)
|
||||||
(with-current-buffer (marker-buffer start-marker)
|
(with-current-buffer (marker-buffer start-marker)
|
||||||
(goto-char start-marker)
|
(goto-char start-marker)
|
||||||
|
(run-hooks 'gptel-pre-response-hook)
|
||||||
(unless (or (bobp) (plist-get info :in-place))
|
(unless (or (bobp) (plist-get info :in-place))
|
||||||
(insert "\n\n"))
|
(insert "\n\n"))
|
||||||
(let ((p (point)))
|
(let ((p (point)))
|
||||||
|
|
Loading…
Add table
Reference in a new issue