gptel: add custom gptel-update-destination

README: Mention `gptel-update-destination` in README.

gptel.el (gptel-update-destination, gptel--update-status,
gptel-send, gptel--insert-response): New option
`gptel-update-destination` to control how gptel's status messages
are shown.  `gptel--update-status` replaces
`gptel--update-header-line`.  Replace calls to this function
elsewhere in gptel.el.

gptel-curl.el (gptel-abort, gptel-curl--stream-cleanup,
gptel-curl--stream-insert-response): Use `gptel--update-status` in
place of `gptel--update-header-line`.

gptel-transient.el (gptel--suffix-send): Use
`gptel--update-status` in place of `gptel--update-header-line`.
This commit is contained in:
Mark Stuart 2023-11-02 08:46:32 +00:00 committed by Karthik Chikmagalur
parent 1a554785e8
commit 4775ade6e0
4 changed files with 29 additions and 9 deletions

View file

@ -344,6 +344,7 @@ Other Emacs clients for LLMs prescribe the format of the interaction (a comint s
| =gptel-default-mode= | Major mode for dedicated chat buffers. |
| =gptel-prompt-prefix-alist= | Text inserted before queries. |
| =gptel-response-prefix-alist= | Text inserted before responses. |
| =gptel-update-destination= | Display status messages in headerline (default) or minibuffer |
|-----------------------------+----------------------------------------|
** COMMENT Will you add feature X?

View file

@ -155,7 +155,7 @@ the response is inserted into the current buffer after point."
(delete-process proc)
(kill-buffer (process-buffer proc))
(with-current-buffer buf
(when gptel-mode (gptel--update-header-line " Ready" 'success)))
(when gptel-mode (gptel--update-status " Ready" 'success)))
(message "Stopped gptel request in buffer %S" (buffer-name buf)))
(message "No gptel request associated with buffer %S" (buffer-name buf))))
@ -185,7 +185,7 @@ PROCESS and _STATUS are process parameters."
(when gptel-mode (save-excursion (goto-char tracking-marker)
(insert "\n\n" (gptel-prompt-prefix-string)))))
(with-current-buffer gptel-buffer
(when gptel-mode (gptel--update-header-line " Ready" 'success))))
(when gptel-mode (gptel--update-status " Ready" 'success))))
;; Or Capture error message
(with-current-buffer proc-buf
(goto-char (point-max))
@ -210,7 +210,7 @@ PROCESS and _STATUS are process parameters."
(t (message "ChatGPT error (%s): Could not parse HTTP response." http-msg)))))
(with-current-buffer gptel-buffer
(when gptel-mode
(gptel--update-header-line
(gptel--update-status
(format " Response Error: %s" http-msg) 'error))))
(with-current-buffer gptel-buffer
(run-hooks 'gptel-post-response-hook)))
@ -229,7 +229,7 @@ See `gptel--url-get-response' for details."
(with-current-buffer (marker-buffer start-marker)
(save-excursion
(unless tracking-marker
(gptel--update-header-line " Typing..." 'success)
(gptel--update-status " Typing..." 'success)
(goto-char start-marker)
(unless (or (bobp) (plist-get info :in-place))
(insert "\n\n")

View file

@ -418,7 +418,7 @@ will get progressively longer!"
(gptel--at-word-end (point)))))))
(with-current-buffer buffer
(setq gptel-backend backend)
(gptel--update-header-line " Waiting..." 'warning)
(gptel--update-status " Waiting..." 'warning)
(setq position (point)))
(setq output-to-other-buffer-p t))
((setq gptel-buffer-name
@ -449,7 +449,7 @@ will get progressively longer!"
(insert reduced-prompt))
(setq position (point))
(when gptel-mode
(gptel--update-header-line " Waiting..." 'warning))))))
(gptel--update-status " Waiting..." 'warning))))))
(when in-place
(setq prompt (gptel--create-prompt (point)))

View file

@ -372,6 +372,15 @@ To set the model for a chat session interactively call
(const :tag "GPT 4" "gpt-4")
(const :tag "GPT 4 turbo (preview)" "gpt-4-1106-preview")))
(defcustom gptel-update-destination "headerline"
"Where update messages appear."
:local t
:safe #'gptel--always
:group 'gptel
:type '(choice
(const :tag "Headerline" "headerline")
(const :tag "Minibufer" "minibuffer")))
(defcustom gptel-temperature 1.0
"\"Temperature\" of ChatGPT response.
@ -621,6 +630,16 @@ opening the file."
(propertize msg 'face face))
(force-mode-line-update)))
(defun gptel--update-status (msg face)
"Update status MSG in FACE."
(when gptel-mode
(if (string= gptel-update-destination "modeline")
(message (propertize msg 'face face))
(and (consp header-line-format)
(setf (nth 1 header-line-format)
(propertize msg 'face face))
(force-mode-line-update)))))
(cl-defun gptel-request
(&optional prompt &key callback
(buffer (current-buffer))
@ -758,7 +777,7 @@ instead."
(list :prompt full-prompt
:buffer gptel-buffer
:position response-pt)))
(gptel--update-header-line " Waiting..." 'warning)))
(gptel--update-status " Waiting..." 'warning)))
(defun gptel--insert-response (response info)
"Insert RESPONSE from ChatGPT into the gptel buffer.
@ -802,8 +821,8 @@ See `gptel--url-get-response' for details."
(insert response)
(pulse-momentary-highlight-region p (point)))
(when gptel-mode (insert "\n\n" (gptel-prompt-prefix-string))))
(when gptel-mode (gptel--update-header-line " Ready" 'success))))
(gptel--update-header-line
(when gptel-mode (gptel--update-status " Ready" 'success))))
(gptel--update-status
(format " Response Error: %s" status-str) 'error)
(message "ChatGPT response error: (%s) %s"
status-str (plist-get info :error)))