Skip to content

Commit 26da476

Browse files
committed
Fix clear messages to reset usage tokens as well
1 parent badb2a0 commit 26da476

4 files changed

Lines changed: 24 additions & 29 deletions

File tree

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 clear messages to reset usage tokens as well.
6+
57
## 0.109.4
68

79
- Fix clear messages to reset last api used as well.

src/eca/features/chat.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1824,7 +1824,7 @@
18241824
(fn [chat]
18251825
(cond-> chat
18261826
messages (-> (assoc :messages [])
1827-
(dissoc :tool-calls :last-api)))))
1827+
(dissoc :tool-calls :last-api :usage)))))
18281828
(db/update-workspaces-cache! @db* metrics)))
18291829

18301830
(defn rollback-chat

src/eca/features/commands.clj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -358,10 +358,10 @@
358358
:chats {chat-id {:title (:title chat)
359359
:messages (concat [{:role "system" :content [{:type :text :text (str "Resuming chat: " selected-chat-id)}]}]
360360
(:messages chat))}}})))
361-
"costs" (let [total-input-tokens (get-in db [:chats chat-id :total-input-tokens] 0)
362-
total-input-cache-creation-tokens (get-in db [:chats chat-id :total-input-cache-creation-tokens] nil)
363-
total-input-cache-read-tokens (get-in db [:chats chat-id :total-input-cache-read-tokens] nil)
364-
total-output-tokens (get-in db [:chats chat-id :total-output-tokens] 0)
361+
"costs" (let [total-input-tokens (get-in db [:chats chat-id :usage :total-input-tokens] 0)
362+
total-input-cache-creation-tokens (get-in db [:chats chat-id :usage :total-input-cache-creation-tokens] nil)
363+
total-input-cache-read-tokens (get-in db [:chats chat-id :usage :total-input-cache-read-tokens] nil)
364+
total-output-tokens (get-in db [:chats chat-id :usage :total-output-tokens] 0)
365365
model-capabilities (get-in db [:models full-model])
366366
text (multi-str (str "Total input tokens: " total-input-tokens)
367367
(when total-input-cache-creation-tokens

src/eca/shared.clj

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,14 @@
171171
(* output-tokens output-token-cost)))))))
172172

173173
(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)
182182
model-capabilities (get-in db [:models full-model])]
183183
(assoc-some {:session-tokens (+ last-input-tokens
184184
last-input-cache-read-tokens
@@ -198,16 +198,16 @@
198198
full-model
199199
{:keys [chat-id db*]}]
200200
(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)
205205
(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))
208208
(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))
211211
(usage-sumary chat-id full-model @db*)))
212212

213213
(defn map->camel-cased-map [m]
@@ -326,14 +326,7 @@
326326
(get-in db [:chats chat-id :last-summary]))}]}])))
327327

328328
;; 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)
337330
(messenger/chat-content-received
338331
messenger
339332
{:chat-id chat-id

0 commit comments

Comments
 (0)