emacs: guard against missing gptel keys

This commit is contained in:
Joseph Ferano 2026-05-11 13:19:52 +07:00
parent 77b1b6f999
commit 017c4b0b7d

View File

@ -2177,19 +2177,26 @@ start it, so go through all existing buffers that match the mode and belong to t
#+end_src #+end_src
** AI ** AI
#+begin_src emacs-lisp #+begin_src emacs-lisp
(setq gptel-default-mode #'org-mode)
(setq gptel-api-key (with-temp-buffer
(insert-file-contents (expand-file-name "gptel-gpt-key" user-emacs-directory))
(buffer-string)))
(gptel-make-anthropic "Claude" :stream t :key (with-temp-buffer
(insert-file-contents (expand-file-name "gptel-key" user-emacs-directory))
(buffer-string)))
(gptel-make-openai "ChatGPT" :stream t :key (with-temp-buffer
(insert-file-contents (expand-file-name "gptel-gpt-key" user-emacs-directory))
(buffer-string)))
(defun joe/read-file-or-nil (filename)
"Read FILENAME from emacs directory, return nil if file doesn't exist."
(let ((path (expand-file-name filename user-emacs-directory)))
(when (file-exists-p path)
(with-temp-buffer
(insert-file-contents path)
(string-trim (buffer-string))))))
(setq gptel-default-mode #'org-mode)
(when-let ((gpt-key (joe/read-file-or-nil "gptel-gpt-key")))
(setq gptel-api-key gpt-key))
(when-let ((claude-key (joe/read-file-or-nil "gptel-key")))
(gptel-make-anthropic "Claude" :stream t :key claude-key)
(setq gptel-model 'claude-sonnet-4-6) (setq gptel-model 'claude-sonnet-4-6)
(setq gptel-backend (gptel-get-backend "Claude")) (setq gptel-backend (gptel-get-backend "Claude")))
(when-let ((gpt-key (joe/read-file-or-nil "gptel-gpt-key")))
(gptel-make-openai "ChatGPT" :stream t :key gpt-key))
(setq gptel-prompt-prefix-alist '((markdown-mode . "### ") (setq gptel-prompt-prefix-alist '((markdown-mode . "### ")
(org-mode . "* ") (org-mode . "* ")