diff --git a/emacs/.emacs.d/config/init-ai.el b/emacs/.emacs.d/config/init-ai.el index 9fd39b5..0c879c9 100644 --- a/emacs/.emacs.d/config/init-ai.el +++ b/emacs/.emacs.d/config/init-ai.el @@ -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