Skip to content

Commit d2be7d5

Browse files
committed
Documentate db schema
1 parent 3fb44c9 commit d2be7d5

File tree

1 file changed

+83
-14
lines changed

1 file changed

+83
-14
lines changed

src/eca/db.clj

Lines changed: 83 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,75 @@
1717

1818
(def version 5)
1919

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+
2089
(defonce initial-db
2190
{:client-info {}
2291
:workspace-folders []
@@ -28,7 +97,7 @@
2897
:models {}
2998
:mcp-clients {}
3099

31-
;; cacheable, bump cache when changing
100+
;; cacheable, bump db `version` when changing any below
32101
:chats {}
33102
:auth {"anthropic" {}
34103
"azure" {}
@@ -79,23 +148,23 @@
79148
(defn ^:private read-cache [cache-file metrics]
80149
(try
81150
(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))))
88157
(catch Throwable e
89158
(logger/error logger-tag "Could not load global cache from DB" e))))
90159

91160
(defn ^:private upsert-cache! [cache cache-file metrics]
92161
(try
93162
(metrics/task metrics :db/upsert-cache
94-
(io/make-parents cache-file)
163+
(io/make-parents cache-file)
95164
;; 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))))
99168
(catch Throwable e
100169
(logger/error logger-tag (str "Could not upsert db cache to " cache-file) e))))
101170

@@ -123,9 +192,9 @@
123192
(update :chats (fn [chats]
124193
(into {}
125194
(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)])))
129198
chats)))))
130199

131200
(defn ^:private normalize-db-for-global-write [db]

0 commit comments

Comments
 (0)