|
171 | 171 | (* output-tokens output-token-cost))))))) |
172 | 172 |
|
173 | 173 | (defn usage-sumary [chat-id full-model db] |
174 | | - (let [last-input-tokens (or (get-in db [:chats chat-id :last-input-tokens]) 0) |
175 | | - last-output-tokens (or (get-in db [:chats chat-id :last-output-tokens]) 0) |
176 | | - last-input-cache-creation-tokens (or (get-in db [:chats chat-id :last-input-cache-creation-tokens]) 0) |
177 | | - last-input-cache-read-tokens (or (get-in db [:chats chat-id :last-input-cache-read-tokens]) 0) |
178 | | - total-input-tokens (or (get-in db [:chats chat-id :total-input-tokens]) 0) |
179 | | - total-input-cache-creation-tokens (or (get-in db [:chats chat-id :total-input-cache-creation-tokens]) 0) |
180 | | - total-input-cache-read-tokens (or (get-in db [:chats chat-id :total-input-cache-read-tokens]) 0) |
181 | | - total-output-tokens (or (get-in db [:chats chat-id :total-output-tokens]) 0) |
| 174 | + (let [last-input-tokens (or (get-in db [:chats chat-id :usage :last-input-tokens]) 0) |
| 175 | + last-output-tokens (or (get-in db [:chats chat-id :usage :last-output-tokens]) 0) |
| 176 | + last-input-cache-creation-tokens (or (get-in db [:chats chat-id :usage :last-input-cache-creation-tokens]) 0) |
| 177 | + last-input-cache-read-tokens (or (get-in db [:chats chat-id :usage :last-input-cache-read-tokens]) 0) |
| 178 | + total-input-tokens (or (get-in db [:chats chat-id :usage :total-input-tokens]) 0) |
| 179 | + total-input-cache-creation-tokens (or (get-in db [:chats chat-id :usage :total-input-cache-creation-tokens]) 0) |
| 180 | + total-input-cache-read-tokens (or (get-in db [:chats chat-id :usage :total-input-cache-read-tokens]) 0) |
| 181 | + total-output-tokens (or (get-in db [:chats chat-id :usage :total-output-tokens]) 0) |
182 | 182 | model-capabilities (get-in db [:models full-model])] |
183 | 183 | (assoc-some {:session-tokens (+ last-input-tokens |
184 | 184 | last-input-cache-read-tokens |
|
198 | 198 | full-model |
199 | 199 | {:keys [chat-id db*]}] |
200 | 200 | (when (and output-tokens input-tokens) |
201 | | - (swap! db* assoc-in [:chats chat-id :last-input-tokens] input-tokens) |
202 | | - (swap! db* assoc-in [:chats chat-id :last-output-tokens] output-tokens) |
203 | | - (swap! db* update-in [:chats chat-id :total-input-tokens] (fnil + 0) input-tokens) |
204 | | - (swap! db* update-in [:chats chat-id :total-output-tokens] (fnil + 0) output-tokens) |
| 201 | + (swap! db* assoc-in [:chats chat-id :usage :last-input-tokens] input-tokens) |
| 202 | + (swap! db* assoc-in [:chats chat-id :usage :last-output-tokens] output-tokens) |
| 203 | + (swap! db* update-in [:chats chat-id :usage :total-input-tokens] (fnil + 0) input-tokens) |
| 204 | + (swap! db* update-in [:chats chat-id :usage :total-output-tokens] (fnil + 0) output-tokens) |
205 | 205 | (when input-cache-creation-tokens |
206 | | - (swap! db* assoc-in [:chats chat-id :last-input-cache-creation-tokens] input-cache-creation-tokens) |
207 | | - (swap! db* update-in [:chats chat-id :total-input-cache-creation-tokens] (fnil + 0) input-cache-creation-tokens)) |
| 206 | + (swap! db* assoc-in [:chats chat-id :usage :last-input-cache-creation-tokens] input-cache-creation-tokens) |
| 207 | + (swap! db* update-in [:chats chat-id :usage :total-input-cache-creation-tokens] (fnil + 0) input-cache-creation-tokens)) |
208 | 208 | (when input-cache-read-tokens |
209 | | - (swap! db* assoc-in [:chats chat-id :last-input-cache-read-tokens] input-cache-read-tokens) |
210 | | - (swap! db* update-in [:chats chat-id :total-input-cache-read-tokens] (fnil + 0) input-cache-read-tokens)) |
| 209 | + (swap! db* assoc-in [:chats chat-id :usage :last-input-cache-read-tokens] input-cache-read-tokens) |
| 210 | + (swap! db* update-in [:chats chat-id :usage :total-input-cache-read-tokens] (fnil + 0) input-cache-read-tokens)) |
211 | 211 | (usage-sumary chat-id full-model @db*))) |
212 | 212 |
|
213 | 213 | (defn map->camel-cased-map [m] |
|
326 | 326 | (get-in db [:chats chat-id :last-summary]))}]}]))) |
327 | 327 |
|
328 | 328 | ;; Zero chat usage |
329 | | - (swap! db* assoc-in [:chats chat-id :last-input-tokens] nil) |
330 | | - (swap! db* assoc-in [:chats chat-id :last-output-tokens] nil) |
331 | | - (swap! db* assoc-in [:chats chat-id :last-input-cache-creation-tokens] nil) |
332 | | - (swap! db* assoc-in [:chats chat-id :last-input-cache-read-tokens] nil) |
333 | | - (swap! db* assoc-in [:chats chat-id :total-input-tokens] nil) |
334 | | - (swap! db* assoc-in [:chats chat-id :total-output-tokens] nil) |
335 | | - (swap! db* assoc-in [:chats chat-id :total-input-cache-creation-tokens] nil) |
336 | | - (swap! db* assoc-in [:chats chat-id :total-input-cache-read-tokens] nil) |
| 329 | + (swap! db* update-in [:chats chat-id] dissoc :usage) |
337 | 330 | (messenger/chat-content-received |
338 | 331 | messenger |
339 | 332 | {:chat-id chat-id |
|
0 commit comments