From 017c4b0b7d51b180f1504003568459be0814ae11 Mon Sep 17 00:00:00 2001 From: Joseph Ferano Date: Mon, 11 May 2026 13:19:52 +0700 Subject: [PATCH] emacs: guard against missing gptel keys --- .config/emacs/init.org | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/.config/emacs/init.org b/.config/emacs/init.org index 9162df0..fa0abf1 100644 --- a/.config/emacs/init.org +++ b/.config/emacs/init.org @@ -2177,19 +2177,26 @@ start it, so go through all existing buffers that match the mode and belong to t #+end_src ** AI #+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))) -(setq gptel-model 'claude-sonnet-4-6) -(setq gptel-backend (gptel-get-backend "Claude")) +(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-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 . "### ") (org-mode . "* ")