|
1 | 1 | (ns eca.features.login |
2 | 2 | (:require |
3 | 3 | [eca.db :as db] |
4 | | - [eca.llm-api :as llm-api] |
| 4 | + [eca.llm-providers.copilot :as llm-providers.copilot] |
5 | 5 | [eca.messenger :as messenger])) |
6 | 6 |
|
7 | 7 | (defn start-login [chat-id provider db*] |
8 | | - (let [{:keys [error-message auth-type] :as result} (llm-api/auth-start {:provider provider})] |
9 | | - (cond |
10 | | - error-message |
11 | | - (do |
12 | | - (swap! db* assoc-in [:chats chat-id :status] :idle) |
13 | | - {:error true |
14 | | - :message error-message}) |
15 | | - |
16 | | - (= :oauth/simple auth-type) |
17 | | - (do |
18 | | - (swap! db* assoc-in [:chats chat-id :login-provider] provider) |
19 | | - (swap! db* assoc-in [:auth provider] {:step :login/waiting-user-confirmation |
20 | | - :device-code (:device-code result)}) |
21 | | - {:message (format "Open your browser at `%s` and authenticate using the code: `%s`\nThen type anything in the chat and send it to continue the authentication." |
22 | | - (:url result) |
23 | | - (:user-code result))})))) |
| 8 | + (case provider |
| 9 | + "github-copilot" |
| 10 | + (let [{:keys [user-code device-code url]} (llm-providers.copilot/oauth-url)] |
| 11 | + (swap! db* assoc-in [:chats chat-id :login-provider] provider) |
| 12 | + (swap! db* assoc-in [:auth provider] {:step :login/waiting-user-confirmation |
| 13 | + :device-code device-code}) |
| 14 | + {:message (format "Open your browser at `%s` and authenticate using the code: `%s`\nThen type anything in the chat and send it to continue the authentication." |
| 15 | + url |
| 16 | + user-code)}))) |
24 | 17 |
|
25 | 18 | (defn continue [{:keys [chat-id request-id]} db* messenger] |
26 | 19 | (let [provider (get-in @db* [:chats chat-id :login-provider]) |
27 | 20 | step (get-in @db* [:auth provider :step])] |
28 | 21 | (case step |
29 | 22 | :login/waiting-user-confirmation |
30 | 23 | (case provider |
31 | | - "github-copilot" (let [{:keys [api-token expires-at error-message]} (llm-api/auth-continue {:provider provider |
32 | | - :db* db*}) |
33 | | - msg (or error-message "Login successful! You can now use the 'github-copilot' models.")] |
34 | | - (when-not error-message |
35 | | - (swap! db* update-in [:auth provider] merge {:step :login/done |
36 | | - :api-token api-token |
37 | | - :expires-at expires-at})) |
| 24 | + "github-copilot" (let [access-token (llm-providers.copilot/oauth-access-token (get-in @db* [:auth provider :device-code])) |
| 25 | + {:keys [api-token expires-at]} (llm-providers.copilot/oauth-renew-token access-token)] |
| 26 | + (swap! db* update-in [:auth provider] merge {:step :login/done |
| 27 | + :access-token access-token |
| 28 | + :api-token api-token |
| 29 | + :expires-at expires-at}) |
38 | 30 | (swap! db* update-in [:chats chat-id :status] :idle) |
39 | 31 | (messenger/chat-content-received |
40 | 32 | messenger |
41 | 33 | {:chat-id chat-id |
42 | 34 | :request-id request-id |
43 | 35 | :role "system" |
44 | 36 | :content {:type :text |
45 | | - :text msg}}) |
| 37 | + :text "Login successful! You can now use the 'github-copilot' models."}}) |
46 | 38 | (messenger/chat-content-received |
47 | 39 | messenger |
48 | 40 | {:chat-id chat-id |
|
51 | 43 | :content {:type :progress |
52 | 44 | :state :finished}})))) |
53 | 45 | (db/update-workspaces-cache! @db*))) |
| 46 | + |
| 47 | +(defn renew-auth! [provider db*] |
| 48 | + (case provider |
| 49 | + "github-copilot" |
| 50 | + (let [access-token (get-in @db* [:auth provider :access-token]) |
| 51 | + {:keys [api-token expires-at]} (llm-providers.copilot/oauth-renew-token access-token)] |
| 52 | + (swap! db* update-in [:auth provider] merge {:api-token api-token |
| 53 | + :expires-at expires-at})))) |
0 commit comments