|
17 | 17 |
|
18 | 18 | (def version 5) |
19 | 19 |
|
| 20 | +(def ^:private _db-spec |
| 21 | + "Used for documentation only" |
| 22 | + {:client-info {:name :string |
| 23 | + :version :string} |
| 24 | + :workspace-folders [{:name :string :uri :string}] |
| 25 | + :client-capabilities {:code-assistant {:editor {:diagnostics :boolean}}} |
| 26 | + :config-hash :string |
| 27 | + :providers-config-hash :string |
| 28 | + :last-config-notified ::any-map |
| 29 | + :stopping :boolean |
| 30 | + :models {"<model-name>" {:web-search :boolean |
| 31 | + :tools :boolean |
| 32 | + :reason? :boolean |
| 33 | + :image-input? :boolean |
| 34 | + :max-output-tokens :number |
| 35 | + :model-name :string ;; real model name used for requests |
| 36 | + :limit {:context :number :output :number} |
| 37 | + :input-token-cost (or :number nil) |
| 38 | + :output-token-cost (or :number nil) |
| 39 | + :input-cache-read-token-cost (or :number nil) |
| 40 | + :input-cache-creation-token-cost (or :number nil)}} |
| 41 | + :mcp-clients {"<client-id>" {:client :McpSyncClient |
| 42 | + :status (or :starting :running :failed :stopping :stopped) |
| 43 | + :version :string |
| 44 | + :tools [{:name :string |
| 45 | + :description :string |
| 46 | + :parameters ::any-map}] |
| 47 | + :prompts [{:name :string |
| 48 | + :description :string |
| 49 | + :arguments [{:name :string |
| 50 | + :description :string |
| 51 | + :required :boolean}]}] |
| 52 | + :resources [{:uri :string |
| 53 | + :name :string |
| 54 | + :description :string |
| 55 | + :mime-type :string}]}} |
| 56 | + :chats {"<chat-id>" {:id :string |
| 57 | + :title (or :string nil) |
| 58 | + :status (or :idle :running :stopping :login) |
| 59 | + :created-at :number |
| 60 | + :login-provider :string |
| 61 | + :messages [::chat-message] |
| 62 | + :tool-calls {"<tool-call-id>" |
| 63 | + {:status (or :initial :preparing :check-approval :waiting-approval |
| 64 | + :execution-approved :executing :rejected :cleanup |
| 65 | + :completed :stopping) |
| 66 | + :name :string |
| 67 | + :full-name :string |
| 68 | + :server :string |
| 69 | + :origin (or :native :mcp) |
| 70 | + :arguments ::any-map |
| 71 | + :decision-reason {:code :keyword :text :string} |
| 72 | + :approved?* :promise |
| 73 | + :future-cleanup-complete?* :promise |
| 74 | + :start-time :long |
| 75 | + :future :future |
| 76 | + :resources :map}}}} |
| 77 | + :auth {"<provider-name>" {:step (or :login/start :login/waiting-login-method |
| 78 | + :login/waiting-provider-code :login/waiting-api-key |
| 79 | + :login/waiting-user-confirmation :login/done :login/renew-token) |
| 80 | + :type (or :auth/token :auth/oauth nil) |
| 81 | + :mode (or :manual :console :max nil) |
| 82 | + :api-key :string |
| 83 | + :access-token :string |
| 84 | + :refresh-token :string |
| 85 | + :expires-at :long |
| 86 | + :verifier :string |
| 87 | + :device-code :string}}}) |
| 88 | + |
20 | 89 | (defonce initial-db |
21 | 90 | {:client-info {} |
22 | 91 | :workspace-folders [] |
|
28 | 97 | :models {} |
29 | 98 | :mcp-clients {} |
30 | 99 |
|
31 | | - ;; cacheable, bump cache when changing |
| 100 | + ;; cacheable, bump db `version` when changing any below |
32 | 101 | :chats {} |
33 | 102 | :auth {"anthropic" {} |
34 | 103 | "azure" {} |
|
79 | 148 | (defn ^:private read-cache [cache-file metrics] |
80 | 149 | (try |
81 | 150 | (metrics/task metrics :db/read-cache |
82 | | - (if (fs/exists? cache-file) |
83 | | - (let [cache (with-open [is (io/input-stream cache-file)] |
84 | | - (transit/read (transit/reader is :json)))] |
85 | | - (when (= version (:version cache)) |
86 | | - cache)) |
87 | | - (logger/info logger-tag (str "No existing DB cache found for " cache-file)))) |
| 151 | + (if (fs/exists? cache-file) |
| 152 | + (let [cache (with-open [is (io/input-stream cache-file)] |
| 153 | + (transit/read (transit/reader is :json)))] |
| 154 | + (when (= version (:version cache)) |
| 155 | + cache)) |
| 156 | + (logger/info logger-tag (str "No existing DB cache found for " cache-file)))) |
88 | 157 | (catch Throwable e |
89 | 158 | (logger/error logger-tag "Could not load global cache from DB" e)))) |
90 | 159 |
|
91 | 160 | (defn ^:private upsert-cache! [cache cache-file metrics] |
92 | 161 | (try |
93 | 162 | (metrics/task metrics :db/upsert-cache |
94 | | - (io/make-parents cache-file) |
| 163 | + (io/make-parents cache-file) |
95 | 164 | ;; https://github.com/cognitect/transit-clj/issues/43 |
96 | | - (with-open [os ^OutputStream (no-flush-output-stream (io/output-stream cache-file))] |
97 | | - (let [writer (transit/writer os :json)] |
98 | | - (transit/write writer cache)))) |
| 165 | + (with-open [os ^OutputStream (no-flush-output-stream (io/output-stream cache-file))] |
| 166 | + (let [writer (transit/writer os :json)] |
| 167 | + (transit/write writer cache)))) |
99 | 168 | (catch Throwable e |
100 | 169 | (logger/error logger-tag (str "Could not upsert db cache to " cache-file) e)))) |
101 | 170 |
|
|
123 | 192 | (update :chats (fn [chats] |
124 | 193 | (into {} |
125 | 194 | (comp |
126 | | - (filter #(seq (:messages (second %)))) |
127 | | - (map (fn [[k v]] |
128 | | - [k (dissoc v :tool-calls)]))) |
| 195 | + (filter #(seq (:messages (second %)))) |
| 196 | + (map (fn [[k v]] |
| 197 | + [k (dissoc v :tool-calls)]))) |
129 | 198 | chats))))) |
130 | 199 |
|
131 | 200 | (defn ^:private normalize-db-for-global-write [db] |
|
0 commit comments