Default to creating a new buffer when invoking gptel

This commit is contained in:
Jeremy Dormitzer 2024-08-20 10:18:23 -04:00
parent 6898a3e8e5
commit d1147a81c3

View File

@ -6,6 +6,29 @@
(use-package gptel
:commands (gptel-request)
:config
;; Default to creating a new buffer when invoking gptel
(defun gptel-before-advice (name &optional _ initial interactivep)
(interactive
(let* ((backend (default-value 'gptel-backend))
(backend-name
(generate-new-buffer-name (format "*%s*" (gptel-backend-name backend)))))
(list (read-buffer "Create or choose gptel buffer: "
backend-name nil ; DEFAULT and REQUIRE-MATCH
(lambda (b) ; PREDICATE
(buffer-local-value 'gptel-mode
(get-buffer (or (car-safe b) b)))))
(condition-case nil
(gptel--get-api-key
(gptel-backend-key backend))
((error user-error)
(setq gptel-api-key
(read-passwd
(format "%s API key: " backend-name)))))
(and (use-region-p)
(buffer-substring (region-beginning)
(region-end)))
t))))
(advice-add 'gptel :before #'gptel-before-advice)
(defvar gptel-backend-openai (gptel-make-openai "ChatGPT"
:key #'gptel-api-key-from-auth-source
:stream t