Skip to content

Commit 6e67057

Browse files
committed
Support use API keys even if subscription is logged.
Fixes #175
1 parent 3c94665 commit 6e67057

File tree

4 files changed

+21
-16
lines changed

4 files changed

+21
-16
lines changed

CHANGELOG.md

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

33
## Unreleased
44

5+
- Support use API keys even if subscription is logged. #175
6+
57
## 0.73.4
68

79
- Fix tool call approval ignoring eca tools.

src/eca/llm_api.clj

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,8 @@
8484
provider-config (get-in config [:providers provider])
8585
model-config (get-in provider-config [:models model])
8686
extra-payload (:extraPayload model-config)
87-
api-key (llm-util/provider-api-key provider provider-auth config)
87+
[auth-type api-key] (llm-util/provider-api-key provider provider-auth config)
8888
api-url (llm-util/provider-api-url provider config)
89-
provider-auth-type (:type provider-auth)
9089
callbacks (when-not sync?
9190
{:on-message-received on-message-received
9291
:on-error on-error
@@ -115,7 +114,7 @@
115114
extra-payload)
116115
:api-url api-url
117116
:api-key api-key
118-
:auth-type provider-auth-type}
117+
:auth-type auth-type}
119118
callbacks)
120119

121120
(= "anthropic" provider)
@@ -132,7 +131,7 @@
132131
:extra-payload extra-payload
133132
:api-url api-url
134133
:api-key api-key
135-
:auth-type provider-auth-type}
134+
:auth-type auth-type}
136135
callbacks)
137136

138137
(= "github-copilot" provider)

src/eca/llm_util.clj

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,16 @@
9595
:challenge (-> verifier str->sha256 ->base64 ->base64url (string/replace "=" ""))}))
9696

9797
(defn provider-api-key [provider provider-auth config]
98-
(or (get-in config [:providers (name provider) :key])
99-
(:api-key provider-auth)
100-
(when-let [key-rc (get-in config [:providers (name provider) :keyRc])]
101-
(secrets/get-credential key-rc))
102-
(some-> (get-in config [:providers (name provider) :keyEnv])
103-
config/get-env)))
98+
(or (when-let [key (get-in config [:providers (name provider) :key])]
99+
[:auth/token key])
100+
(when-let [key (:api-key provider-auth)]
101+
[:auth/oauth key])
102+
(when-let [key (some-> (get-in config [:providers (name provider) :keyRc])
103+
(secrets/get-credential))]
104+
[:auth/token key])
105+
(when-let [key (some-> (get-in config [:providers (name provider) :keyEnv])
106+
config/get-env)]
107+
[:auth/token key])))
104108

105109
(defn provider-api-url [provider config]
106110
(or (get-in config [:providers (name provider) :url])

test/eca/llm_util_test.clj

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
{"openai" {:url "https://api.openai.com"
7070
:keyRc "api.openai.com"}}}
7171
result (llm-util/provider-api-key "openai" nil config)]
72-
(is (= "sk-test-from-netrc" result))))
72+
(is (= [:auth/token "sk-test-from-netrc"] result))))
7373
(finally
7474
(.delete temp-file))))))
7575

@@ -87,20 +87,20 @@
8787
{"openai" {:key "sk-explicit-key"
8888
:keyRc "api.openai.com"}}}
8989
result (llm-util/provider-api-key "openai" nil config)]
90-
(is (= "sk-explicit-key" result)))
90+
(is (= [:auth/token "sk-explicit-key"] result)))
9191

9292
;; Test 2: oauth token takes precedence over keyRc
9393
(let [config {:providers
9494
{"openai" {:keyRc "api.openai.com"}}}
9595
provider-auth {:api-key "sk-oauth-token"}
9696
result (llm-util/provider-api-key "openai" provider-auth config)]
97-
(is (= "sk-oauth-token" result)))
97+
(is (= [:auth/oauth "sk-oauth-token"] result)))
9898

9999
;; Test 3: keyRc is used when key and oauth are not available
100100
(let [config {:providers
101101
{"openai" {:keyRc "api.openai.com"}}}
102102
result (llm-util/provider-api-key "openai" nil config)]
103-
(is (= "sk-test-from-netrc" result))))
103+
(is (= [:auth/token "sk-test-from-netrc"] result))))
104104
(finally
105105
(.delete temp-file))))))
106106

@@ -118,13 +118,13 @@
118118
(let [config {:providers
119119
{"anthropic" {:keyRc "[email protected]"}}}
120120
result (llm-util/provider-api-key "anthropic" nil config)]
121-
(is (= "sk-ant-work-key" result)))
121+
(is (= [:auth/token "sk-ant-work-key"] result)))
122122

123123
;; Test with different login
124124
(let [config {:providers
125125
{"anthropic" {:keyRc "[email protected]"}}}
126126
result (llm-util/provider-api-key "anthropic" nil config)]
127-
(is (= "sk-ant-personal-key" result))))
127+
(is (= [:auth/token "sk-ant-personal-key"] result))))
128128
(finally
129129
(.delete temp-file))))))
130130

0 commit comments

Comments
 (0)