Skip to content

Commit 873c13d

Browse files
committed
Fix message stats for combined messages
1 parent 12cd48d commit 873c13d

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

tools/server/webui/src/lib/components/app/chat/ChatMessages/ChatMessages.svelte

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import type {
99
ApiChatCompletionToolCall,
1010
ChatMessageSiblingInfo,
11+
ChatMessageTimings,
1112
DatabaseMessage
1213
} from '$lib/types';
1314
@@ -104,6 +105,51 @@
104105
};
105106
};
106107
108+
const sumTimings = (assistantIds: string[]): ChatMessageTimings | undefined => {
109+
if (assistantIds.length <= 1) return undefined;
110+
111+
let predicted_n_sum = 0;
112+
let predicted_ms_sum = 0;
113+
let prompt_n_sum = 0;
114+
let prompt_ms_sum = 0;
115+
let cache_n_sum = 0;
116+
117+
let hasPredicted = false;
118+
let hasPrompt = false;
119+
let hasCache = false;
120+
121+
for (const id of assistantIds) {
122+
const m = filteredMessages.find((x) => x.id === id);
123+
const t = m?.timings;
124+
if (!t) continue;
125+
126+
if (typeof t.predicted_n === 'number' && typeof t.predicted_ms === 'number') {
127+
predicted_n_sum += t.predicted_n;
128+
predicted_ms_sum += t.predicted_ms;
129+
hasPredicted = true;
130+
}
131+
132+
if (typeof t.prompt_n === 'number' && typeof t.prompt_ms === 'number') {
133+
prompt_n_sum += t.prompt_n;
134+
prompt_ms_sum += t.prompt_ms;
135+
hasPrompt = true;
136+
}
137+
138+
if (typeof t.cache_n === 'number') {
139+
cache_n_sum += t.cache_n;
140+
hasCache = true;
141+
}
142+
}
143+
144+
if (!hasPredicted && !hasPrompt && !hasCache) return undefined;
145+
146+
return {
147+
...(hasPredicted ? { predicted_n: predicted_n_sum, predicted_ms: predicted_ms_sum } : {}),
148+
...(hasPrompt ? { prompt_n: prompt_n_sum, prompt_ms: prompt_ms_sum } : {}),
149+
...(hasCache ? { cache_n: cache_n_sum } : {})
150+
};
151+
};
152+
107153
for (const msg of filteredMessages) {
108154
if (visited.has(msg.id)) continue;
109155
// Don't render tools directly, but keep them for collection; skip marking visited here
@@ -198,11 +244,14 @@
198244
totalSiblings: 1
199245
};
200246
247+
const aggregatedTimings = sumTimings(toolParentIds);
248+
201249
const mergedAssistant: AssistantDisplayMessage = {
202250
...(currentAssistant ?? msg),
203251
content: currentAssistant?.content ?? '',
204252
thinking: thinkingParts.filter(Boolean).join('\n\n'),
205253
toolCalls: toolCallsCombined.length ? JSON.stringify(toolCallsCombined) : '',
254+
...(aggregatedTimings ? { timings: aggregatedTimings } : {}),
206255
_toolParentIds: toolParentIds,
207256
_segments: segments,
208257
_actionTargetId: msg.id,

0 commit comments

Comments
 (0)