Skip to content

Commit 0bc293c

Browse files
gptel-gh: Default to saving github-token in a file
1 parent 5304345 commit 0bc293c

File tree

1 file changed

+41
-19
lines changed

1 file changed

+41
-19
lines changed

gptel-gh.el

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -182,14 +182,46 @@
182182
(:constructor gptel--make-gh))
183183
sessionid machineid)
184184

185+
(defcustom gptel-gh-github-token-file (expand-file-name ".cache/copilot-chat/github-token"
186+
user-emacs-directory)
187+
"File where the GitHub token is stored."
188+
:type 'string
189+
:group 'gptel)
190+
191+
(defun gptel--gh-restore-from-file ()
192+
"Restore saved object from gptel-gh-github-token-file."
193+
(when (file-exists-p gptel-gh-github-token-file)
194+
;; We set the coding system to `utf-8-auto-dos' when reading so that
195+
;; files with CR EOL can still be read properly
196+
(let ((coding-system-for-read 'utf-8-auto-dos))
197+
(with-temp-buffer
198+
(set-buffer-multibyte nil)
199+
(insert-file-contents-literally gptel-gh-github-token-file)
200+
(goto-char (point-min))
201+
(read (current-buffer))))))
202+
203+
(defun gptel--gh-save-to-file (obj)
204+
"Save OBJ to gptel--gh-save-to-file."
205+
(let ((print-length nil)
206+
(print-level nil)
207+
(coding-system-for-write 'utf-8-unix))
208+
(make-directory (file-name-directory gptel-gh-github-token-file) t)
209+
(write-region (prin1-to-string obj) nil gptel-gh-github-token-file nil :silent)
210+
obj))
211+
212+
(defvar gptel-gh-github-token-load 'gptel--gh-restore-from-file
213+
"Function to load the current github token.")
214+
215+
(defvar gptel-gh-github-token-save 'gptel--gh-save-to-file
216+
"Function to save the new github token.")
217+
185218
(defconst gptel--gh-auth-common-headers
186219
`(("editor-plugin-version" . "gptel/*")
187220
("editor-version" . ,(concat "emacs/" emacs-version))))
188221

189222
(defconst gptel--gh-client-id "Iv1.b507a08c87ecfe98")
190223

191224
(defvar gptel--gh-token nil)
192-
(defvar gptel--gh-github-token nil)
193225

194226
;; https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_4_(random)
195227
(defun gptel--gh-uuid ()
@@ -210,10 +242,6 @@
210242
(setq hex (nconc hex (list (aref hex-chars (random 16))))))
211243
(apply #'string hex)))
212244

213-
(defun gptel--gh-github-token-save (token)
214-
"Saves the new github token for use."
215-
(setq gptel--gh-github-token token))
216-
217245
(defun gptel-gh-login ()
218246
"Login to GitHub Copilot API.
219247
@@ -244,13 +272,11 @@ If your browser does not open automatically, browse to %s."
244272
:device_code ,device_code
245273
:grant_type "urn:ietf:params:oauth:grant-type:device_code"))
246274
:access_token)
247-
(gptel--gh-github-token-save))
248-
(if (and gptel--gh-github-token
249-
(not (string-empty-p gptel--gh-github-token)))
250-
(progn
251-
(message "New GitHub token: %s" (prin1-to-string gptel--gh-github-token))
252-
(message "Successfully logged in to GitHub Copilot"))
253-
(user-error "Error: You might not have access to GitHub Copilot Chat!"))))
275+
(funcall gptel-gh-github-token-save))
276+
(let ((github-token (funcall gptel-gh-github-token-load)))
277+
(if (and github-token (not (string-empty-p github-token)))
278+
(message "Successfully logged in to GitHub Copilot")
279+
(user-error "Error: You might not have access to GitHub Copilot Chat!")))))
254280

255281
(defun gptel--gh-renew-token ()
256282
"Renew session token."
@@ -259,7 +285,7 @@ If your browser does not open automatically, browse to %s."
259285
"https://api.github.com/copilot_internal/v2/token"
260286
:method 'get
261287
:headers `(("authorization"
262-
. ,(format "token %s" gptel--gh-github-token))
288+
. ,(format "token %s" (funcall gptel-gh-github-token-load)))
263289
,@gptel--gh-auth-common-headers))))
264290
(if (not (plist-get token :token))
265291
(user-error "Error: You might not have access to GitHub Copilot Chat!")
@@ -270,7 +296,7 @@ If your browser does not open automatically, browse to %s."
270296
271297
We first need github authorization (github token).
272298
Then we need a session token."
273-
(unless gptel--gh-github-token
299+
(unless (funcall gptel-gh-github-token-load)
274300
(gptel-gh-login))
275301

276302
(pcase-let (((map :token :expires_at)
@@ -298,8 +324,7 @@ Then we need a session token."
298324
(protocol "https")
299325
(endpoint "/chat/completions")
300326
(stream t)
301-
(models gptel--gh-models)
302-
(github-token-init nil))
327+
(models gptel--gh-models))
303328
"Register a Github Copilot chat backend for gptel with NAME.
304329
305330
Keyword arguments:
@@ -351,9 +376,6 @@ parameters (as plist keys) and values supported by the API. Use
351376
these to set parameters that gptel does not provide user options
352377
for."
353378
(declare (indent 1))
354-
(if (and (not gptel--gh-github-token)
355-
github-token-init)
356-
(gptel--gh-github-token-save (funcall github-token-init)))
357379
(let ((backend (gptel--make-gh
358380
:name name
359381
:host host

0 commit comments

Comments
 (0)