Skip to content

Commit 3c66a5b

Browse files
committed
Add global cache for auth
1 parent fbae096 commit 3c66a5b

File tree

3 files changed

+36
-16
lines changed

3 files changed

+36
-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+
- Fix copilot requiring login in different workspaces.
6+
57
## 0.34.1
68

79
- Fix proxy exception. #73

src/eca/db.clj

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,19 @@
1414

1515
(def ^:private logger-tag "[DB]")
1616

17-
(def version 1)
17+
(def version 2)
1818

1919
(defonce initial-db
2020
{:client-info {}
2121
:workspace-folders []
2222
:client-capabilities {}
23-
:chats {}
2423
:chat-behaviors ["agent" "plan"]
2524
:models {}
26-
:auth {}
27-
:mcp-clients {}})
25+
:mcp-clients {}
26+
27+
;; cacheable, bump cache when changing
28+
:chats {}
29+
:auth {}})
2830

2931
(defonce db* (atom initial-db))
3032

@@ -57,9 +59,12 @@
5759
key (.encodeToString encoder digest)]
5860
(subs key 0 (min 8 (count key)))))
5961

60-
(defn ^:private transit-global-db-file [workspaces]
62+
(defn ^:private transit-global-by-workspaces-db-file [workspaces]
6163
(io/file (global-cache-dir) (workspaces-hash workspaces) "db.transit.json"))
6264

65+
(defn ^:private transit-global-db-file []
66+
(io/file (global-cache-dir) "db.transit.json"))
67+
6368
(defn ^:private read-cache [cache-file]
6469
(try
6570
(logger/logging-task
@@ -85,26 +90,39 @@
8590
(catch Throwable e
8691
(logger/error logger-tag (str "Could not upsert db cache to " cache-file) e))))
8792

88-
(defn ^:private read-workspaces-cache [workspaces]
89-
(let [cache (read-cache (transit-global-db-file workspaces))]
93+
(defn ^:private read-global-cache []
94+
(let [cache (read-cache (transit-global-db-file))]
95+
(when (= version (:version cache))
96+
cache)))
97+
98+
(defn ^:private read-global-by-workspaces-cache [workspaces]
99+
(let [cache (read-cache (transit-global-by-workspaces-db-file workspaces))]
90100
(when (= version (:version cache))
91101
cache)))
92102

93103
(defn load-db-from-cache! [db*]
94-
(when-let [global-cache (read-workspaces-cache (:workspace-folders @db*))]
95-
(swap! db* (fn [state-db]
96-
(merge state-db
97-
(select-keys global-cache [:chats :auth]))))))
104+
(when-let [global-cache (read-global-cache)]
105+
(swap! db* shared/deep-merge global-cache))
106+
(when-let [global-by-workspace-cache (read-global-by-workspaces-cache (:workspace-folders @db*))]
107+
(swap! db* shared/deep-merge global-by-workspace-cache)))
98108

99-
(defn ^:private normalize-db-for-write [db]
100-
(-> (select-keys db [:chats :auth])
109+
(defn ^:private normalize-db-for-workspace-write [db]
110+
(-> (select-keys db [:chats])
101111
(update :chats (fn [chats]
102112
(into {}
103113
(map (fn [[k v]]
104114
[k (dissoc v :tool-calls)]))
105115
chats)))))
106116

117+
(defn ^:private normalize-db-for-global-write [db]
118+
(select-keys db [:auth]))
119+
107120
(defn update-workspaces-cache! [db]
108-
(-> (normalize-db-for-write db)
121+
(-> (normalize-db-for-workspace-write db)
122+
(assoc :version version)
123+
(upsert-cache! (transit-global-by-workspaces-db-file (:workspace-folders db)))))
124+
125+
(defn update-global-cache! [db]
126+
(-> (normalize-db-for-global-write db)
109127
(assoc :version version)
110-
(upsert-cache! (transit-global-db-file (:workspace-folders db)))))
128+
(upsert-cache! (transit-global-db-file))))

src/eca/features/login.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
:role "system"
4343
:content {:type :progress
4444
:state :finished}}))))
45-
(db/update-workspaces-cache! @db*)))
45+
(db/update-global-cache! @db*)))
4646

4747
(defn renew-auth! [provider db*]
4848
(case provider

0 commit comments

Comments
 (0)