Skip to content

Commit 29d9c5d

Browse files
committed
Support openai login via /login
1 parent 1afe938 commit 29d9c5d

File tree

5 files changed

+55
-18
lines changed

5 files changed

+55
-18
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## Unreleased
44

5+
- Support more providers login via `/login`.
6+
- openai
7+
58
## 0.46.0
69

710
- Remove the need to pass `requestId` on prompt messages.

src/eca/config.clj

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,11 @@
109109
(io/file (get-property "user.home") ".config"))]
110110
(io/file xdg-config-home "eca")))
111111

112+
(defn global-config-file ^File []
113+
(io/file (global-config-dir) "config.json"))
114+
112115
(defn ^:private config-from-global-file* []
113-
(let [config-file (io/file (global-config-dir) "config.json")]
116+
(let [config-file (global-config-file)]
114117
(when (.exists config-file)
115118
(safe-read-json-string (slurp config-file) (var *global-config-error*)))))
116119

@@ -190,25 +193,28 @@
190193
:else m*))]
191194
(normalize-map [] m)))
192195

196+
(def ^:private eca-config-normalization-rules
197+
{:kebab-case
198+
[[:providers]]
199+
:stringfy
200+
[[:providers]
201+
[:providers :ANY :models]
202+
[:toolCall :approval :allow]
203+
[:toolCall :approval :allow :ANY :argsMatchers]
204+
[:toolCall :approval :ask]
205+
[:toolCall :approval :ask :ANY :argsMatchers]
206+
[:toolCall :approval :deny]
207+
[:toolCall :approval :deny :ANY :argsMatchers]
208+
[:customTools]
209+
[:customTools :ANY :schema :properties]
210+
[:mcpServers]]})
211+
193212
(defn all [db]
194213
(let [initialization-config @initialization-config*
195214
pure-config? (:pureConfig initialization-config)]
196215
(deep-merge initial-config
197216
(normalize-fields
198-
{:kebab-case
199-
[[:providers]]
200-
:stringfy
201-
[[:providers]
202-
[:providers :ANY :models]
203-
[:toolCall :approval :allow]
204-
[:toolCall :approval :allow :ANY :argsMatchers]
205-
[:toolCall :approval :ask]
206-
[:toolCall :approval :ask :ANY :argsMatchers]
207-
[:toolCall :approval :deny]
208-
[:toolCall :approval :deny :ANY :argsMatchers]
209-
[:customTools]
210-
[:customTools :ANY :schema :properties]
211-
[:mcpServers]]}
217+
eca-config-normalization-rules
212218
(deep-merge initialization-config
213219
(when-not pure-config? (config-from-envvar))
214220
(when-not pure-config? (config-from-global-file))
@@ -285,3 +291,12 @@
285291
(when (seq config-to-notify)
286292
(swap! db* update :last-config-notified shared/deep-merge config-to-notify)
287293
(messenger/config-updated messenger config-to-notify))))
294+
295+
(defn update-global-config! [config]
296+
(let [global-config-file (global-config-file)
297+
current-config (normalize-fields eca-config-normalization-rules (config-from-global-file))
298+
new-config (deep-merge current-config
299+
(normalize-fields eca-config-normalization-rules config))
300+
new-config-json (json/generate-string new-config {:pretty true})]
301+
(io/make-parents global-config-file)
302+
(spit global-config-file new-config-json)))

src/eca/db.clj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
;; cacheable, bump cache when changing
3232
:chats {}
3333
:auth {"anthropic" {}
34-
"github-copilot" {}}})
34+
"github-copilot" {}
35+
"openai" {}}})
3536

3637
(defonce db* (atom initial-db))
3738

src/eca/features/login.clj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
(ns eca.features.login
22
(:require
33
[clojure.string :as string]
4+
[eca.config :as config]
45
[eca.db :as db]
56
[eca.messenger :as messenger]
67
[eca.models :as models]))
@@ -73,10 +74,10 @@
7374
(catch Exception e
7475
(on-error (.getMessage e)))))
7576

76-
(defn login-done! [{:keys [chat-id db* config messenger provider send-msg!]} & [silent]]
77+
(defn login-done! [{:keys [chat-id db* messenger provider send-msg!]} & [silent]]
7778
(db/update-global-cache! @db*)
7879
(models/sync-models! db*
79-
config
80+
(config/all @db*) ;; force get updated config
8081
(fn [new-models]
8182
(messenger/config-updated
8283
messenger

src/eca/llm_providers/openai.clj

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
[cheshire.core :as json]
44
[clojure.java.io :as io]
55
[clojure.string :as string]
6+
[eca.config :as config]
7+
[eca.features.login :as f.login]
68
[eca.llm-util :as llm-util]
79
[eca.logger :as logger]
810
[hato.client :as http]))
@@ -194,3 +196,18 @@
194196
:api-key api-key
195197
:on-error on-error
196198
:on-response on-response-fn})))
199+
200+
(defmethod f.login/login-step ["openai" :login/start] [{:keys [db* chat-id provider send-msg!]}]
201+
(swap! db* assoc-in [:chats chat-id :login-provider] provider)
202+
(swap! db* assoc-in [:auth provider] {:step :login/waiting-api-key})
203+
(send-msg! "Paste your API Key"))
204+
205+
(defmethod f.login/login-step ["openai" :login/waiting-api-key] [{:keys [input db* provider send-msg!] :as ctx}]
206+
(if (string/starts-with? input "sk-")
207+
(do (swap! db* assoc-in [:auth provider] {:step :login/done
208+
:type :auth/token
209+
:api-key input})
210+
(config/update-global-config! {:providers {"openai" {:key input}}})
211+
(send-msg! (str "API key saved in " (.getCanonicalPath (config/global-config-file))))
212+
(f.login/login-done! ctx))
213+
(send-msg! (format "Invalid API key '%s'" input))))

0 commit comments

Comments
 (0)