Skip to content

Commit 7c9927b

Browse files
committed
Support openrouter login
1 parent 29d9c5d commit 7c9927b

File tree

9 files changed

+53
-12
lines changed

9 files changed

+53
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- Support more providers login via `/login`.
66
- openai
7+
- openrouter
78

89
## 0.46.0
910

docs/models.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,16 @@ Only set this when your provider uses a different path or expects query paramete
157157
```
158158

159159
=== "OpenRouter"
160-
160+
161161
[OpenRouter](https://openrouter.ai) provides access to many models through a unified API:
162+
163+
1. Login via the chat command `/login`.
164+
2. Type 'openrouter' and send it.
165+
3. Specify your Openrouter API key.
166+
4. Inform at least a model, ex: `openai/gpt-5`
167+
5. Done, it should be saved to your global config.
168+
169+
or manually via config:
162170

163171
```javascript
164172
{

src/eca/db.clj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
:chats {}
3333
:auth {"anthropic" {}
3434
"github-copilot" {}
35-
"openai" {}}})
35+
"openai" {}
36+
"openrouter" {}}})
3637

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

src/eca/features/login.clj

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,10 @@
7474
(catch Exception e
7575
(on-error (.getMessage e)))))
7676

77-
(defn login-done! [{:keys [chat-id db* messenger provider send-msg!]} & [silent]]
78-
(db/update-global-cache! @db*)
77+
(defn login-done! [{:keys [chat-id db* messenger provider send-msg!]} & {:keys [silent?]
78+
:or {silent? false}}]
79+
(when (get-in @db* [:auth provider])
80+
(db/update-global-cache! @db*))
7981
(models/sync-models! db*
8082
(config/all @db*) ;; force get updated config
8183
(fn [new-models]
@@ -85,5 +87,5 @@
8587
{:models (sort (keys new-models))}})))
8688
(swap! db* assoc-in [:chats chat-id :login-provider] nil)
8789
(swap! db* assoc-in [:chats chat-id :status] :idle)
88-
(when-not silent
90+
(when-not silent?
8991
(send-msg! (format "Login successful! You can now use the '%s' models." provider))))

src/eca/llm_api.clj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
[eca.llm-providers.ollama :as llm-providers.ollama]
99
[eca.llm-providers.openai :as llm-providers.openai]
1010
[eca.llm-providers.openai-chat :as llm-providers.openai-chat]
11+
[eca.llm-providers.openrouter]
1112
[eca.llm-util :as llm-util]
1213
[eca.logger :as logger]))
1314

src/eca/llm_providers/anthropic.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,4 +343,4 @@
343343
:refresh-token refresh-token
344344
:api-key access-token
345345
:expires-at expires-at})
346-
(f.login/login-done! ctx true)))
346+
(f.login/login-done! ctx :silent? true)))

src/eca/llm_providers/copilot.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,4 @@
7777
{:keys [api-key expires-at]} (oauth-renew-token access-token)]
7878
(swap! db* update-in [:auth provider] merge {:api-key api-key
7979
:expires-at expires-at})
80-
(f.login/login-done! ctx true)))
80+
(f.login/login-done! ctx :silent? true)))

src/eca/llm_providers/openai.clj

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,9 @@
204204

205205
(defmethod f.login/login-step ["openai" :login/waiting-api-key] [{:keys [input db* provider send-msg!] :as ctx}]
206206
(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}}})
207+
(do (config/update-global-config! {:providers {"openai" {:key input}}})
208+
(swap! db* assoc-in [:auth provider] nil)
211209
(send-msg! (str "API key saved in " (.getCanonicalPath (config/global-config-file))))
212-
(f.login/login-done! ctx))
210+
211+
(f.login/login-done! ctx :update-cache? false))
213212
(send-msg! (format "Invalid API key '%s'" input))))
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
(ns eca.llm-providers.openrouter
2+
(:require
3+
[clojure.string :as string]
4+
[eca.config :as config]
5+
[eca.features.login :as f.login]))
6+
7+
(defmethod f.login/login-step ["openrouter" :login/start] [{:keys [db* chat-id provider send-msg!]}]
8+
(swap! db* assoc-in [:chats chat-id :login-provider] provider)
9+
(swap! db* assoc-in [:auth provider] {:step :login/waiting-api-key})
10+
(send-msg! "Paste your API Key"))
11+
12+
(defmethod f.login/login-step ["openrouter" :login/waiting-api-key] [{:keys [input db* provider send-msg!]}]
13+
(swap! db* assoc-in [:auth provider] {:step :login/waiting-models
14+
:api-key input})
15+
(send-msg! "Inform one or more models (separated by `,`):"))
16+
17+
(defmethod f.login/login-step ["openrouter" :login/waiting-models] [{:keys [input db* provider send-msg!] :as ctx}]
18+
(let [api-key (get-in @db* [:auth provider :api-key])]
19+
(config/update-global-config! {:providers {"openrouter" {:api "openai-chat"
20+
:url "https://openrouter.ai/api/v1"
21+
:models (reduce
22+
(fn [models model-str]
23+
(assoc models (string/trim model-str) {}))
24+
{}
25+
(string/split input #","))
26+
:key api-key}}}))
27+
(swap! db* assoc-in [:auth provider] nil)
28+
(send-msg! (format "API key and models saved to %s" (.getCanonicalPath (config/global-config-file))))
29+
(f.login/login-done! ctx))

0 commit comments

Comments
 (0)