|
14 | 14 |
|
15 | 15 | (def ^:private logger-tag "[DB]") |
16 | 16 |
|
17 | | -(def version 1) |
| 17 | +(def version 2) |
18 | 18 |
|
19 | 19 | (defonce initial-db |
20 | 20 | {:client-info {} |
21 | 21 | :workspace-folders [] |
22 | 22 | :client-capabilities {} |
23 | | - :chats {} |
24 | 23 | :chat-behaviors ["agent" "plan"] |
25 | 24 | :models {} |
26 | | - :auth {} |
27 | | - :mcp-clients {}}) |
| 25 | + :mcp-clients {} |
| 26 | + |
| 27 | + ;; cacheable, bump cache when changing |
| 28 | + :chats {} |
| 29 | + :auth {}}) |
28 | 30 |
|
29 | 31 | (defonce db* (atom initial-db)) |
30 | 32 |
|
|
57 | 59 | key (.encodeToString encoder digest)] |
58 | 60 | (subs key 0 (min 8 (count key))))) |
59 | 61 |
|
60 | | -(defn ^:private transit-global-db-file [workspaces] |
| 62 | +(defn ^:private transit-global-by-workspaces-db-file [workspaces] |
61 | 63 | (io/file (global-cache-dir) (workspaces-hash workspaces) "db.transit.json")) |
62 | 64 |
|
| 65 | +(defn ^:private transit-global-db-file [] |
| 66 | + (io/file (global-cache-dir) "db.transit.json")) |
| 67 | + |
63 | 68 | (defn ^:private read-cache [cache-file] |
64 | 69 | (try |
65 | 70 | (logger/logging-task |
|
85 | 90 | (catch Throwable e |
86 | 91 | (logger/error logger-tag (str "Could not upsert db cache to " cache-file) e)))) |
87 | 92 |
|
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))] |
90 | 100 | (when (= version (:version cache)) |
91 | 101 | cache))) |
92 | 102 |
|
93 | 103 | (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))) |
98 | 108 |
|
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]) |
101 | 111 | (update :chats (fn [chats] |
102 | 112 | (into {} |
103 | 113 | (map (fn [[k v]] |
104 | 114 | [k (dissoc v :tool-calls)])) |
105 | 115 | chats))))) |
106 | 116 |
|
| 117 | +(defn ^:private normalize-db-for-global-write [db] |
| 118 | + (select-keys db [:auth])) |
| 119 | + |
107 | 120 | (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) |
109 | 127 | (assoc :version version) |
110 | | - (upsert-cache! (transit-global-db-file (:workspace-folders db))))) |
| 128 | + (upsert-cache! (transit-global-db-file)))) |
0 commit comments