-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelfos-backend.el
46 lines (34 loc) · 1.37 KB
/
delfos-backend.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
;;; delfos-backend.el --- Description -*- lexical-binding: t; -*-
;;
;; Copyright (C) 2023 namespace
;;
;; Author: namespace <src.namespace@gmail.com>
;; Maintainer: namespace <src.namespace@gmail.com>
;;; Commentary:
;; This file provides an interface for creating chatbot backends.
;;; Code:
(require 'cl-lib)
(require 'secrets)
(require 'f)
(defvar rc-path (expand-file-name ".delfosrc" user-emacs-directory))
(make-directory user-emacs-directory t)
(cl-defstruct delfos-backend complete-chat extract-answer models default-model)
(defun delfos-backend-prompt-secret (label &optional prompt)
(let ((api-key (read-from-minibuffer (or prompt (format "Enter secret (%s): " label)))))
(condition-case _err
(secrets-delete-item "login" (format "delfos-%s" label))
(secrets-create-item "login" (format "delfos-%s" label) api-key)
(error (with-temp-file rc-path (insert api-key))))
api-key))
(defun delfos-backend-get-or-prompt-secret (label &optional prompt)
(let ((secret (delfos-backend-get-secret label)))
(if secret
secret
(delfos-backend-prompt-secret label prompt))))
(defun delfos-backend-get-secret (label)
(condition-case _err
(secrets-get-secret "login" (format "delfos-%s" label))
(error (condition-case _err (f-read-text rc-path)
(error nil)))))
(provide 'delfos-backend)
;;; delfos-backend.el ends here