File size: 6,847 Bytes
82be208
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/**
 * ContextStatsStore - Context window usage stats for the active conversation
 *
 * Combines token usage persisted in message timings metadata with
 * server-originating data: model context size from /props (modelsStore)
 * and live processing state while streaming (chatStore).
 */

import { MessageRole } from '$lib/enums';
// direct imports between stores, not via the barrel, to avoid circular deps
import { agenticStore } from '$lib/stores/agentic/index.svelte';
import { chatStore } from '$lib/stores/chat/index.svelte';
import { conversationsStore } from '$lib/stores/conversations/index.svelte';
import { modelsStore } from '$lib/stores/models/index.svelte';
import { serverStore } from '$lib/stores/server.svelte';
import type {
	ApiProcessingState,
	ChatMessageAgenticTimings,
	ChatMessageTimings,
	DatabaseMessage
} from '$lib/types';

interface LiveStats {
	freshTokens: number;
	promptTokens: number;
	cacheTokens: number;
	outputTokens: number;
}

interface AssistantTimingsSummary {
	lastAgenticLlm: ChatMessageAgenticTimings['llm'] | undefined;
	lastTimings: ChatMessageTimings | undefined;
	cacheTotal: number;
	output: number;
	outputMs: number;
	read: number;
}

/**
 * One forward pass over the messages computing everything the deriveds
 * below need: the last assistant timings (per-turn gauges), the last
 * agentic llm totals (cumulative gauge) and the cumulative sums. During
 * streaming activeMessages churns every chunk, and each of these used to be
 * its own O(n) scan re-run per chunk.
 */
function summarizeAssistantTimings(messages: DatabaseMessage[]): AssistantTimingsSummary {
	let lastAgenticLlm: ChatMessageAgenticTimings['llm'] | undefined;
	let lastTimings: ChatMessageTimings | undefined;
	let read = 0;
	let cacheTotal = 0;
	let output = 0;
	let outputMs = 0;

	for (const m of messages) {
		if (m.role !== MessageRole.ASSISTANT || !m.timings) continue;

		lastTimings = m.timings;

		if (m.timings.agentic?.llm?.predicted_n != null) {
			lastAgenticLlm = m.timings.agentic.llm;
		}

		read += m.timings.prompt_n ?? 0;
		cacheTotal += m.timings.cache_n ?? 0;
		output += m.timings.predicted_n ?? 0;
		outputMs += m.timings.predicted_ms ?? 0;
	}

	return { cacheTotal, lastAgenticLlm, lastTimings, output, outputMs, read };
}

function deriveLiveStats(state: ApiProcessingState | null): LiveStats | null {
	if (!state || (state.status !== 'preparing' && state.status !== 'generating')) {
		return null;
	}

	const promptTokens = state.promptTokens ?? 0;
	const cacheTokens = state.cacheTokens ?? 0;

	return {
		cacheTokens,
		freshTokens: promptTokens,
		outputTokens: state.outputTokensUsed ?? 0,
		promptTokens: promptTokens + cacheTokens
	};
}

class ContextStatsStore {
	// The canonical resolution lives in modelsStore.activeModelId.
	activeModelId = $derived(modelsStore.activeModelId);

	// shared by currentRead/Fresh/Cache/Output and cumulative so a per-chunk
	// churn of activeMessages triggers exactly one scan instead of one per
	// derived
	private assistantTimings = $derived.by(() =>
		summarizeAssistantTimings(conversationsStore.activeMessages as DatabaseMessage[])
	);

	private cumulative = $derived.by(() => {
		const convId = conversationsStore.activeConversation?.id;
		// A running agentic flow stamps llm totals on messages only when it
		// exits, so read its live session totals instead.
		const liveLlm = convId ? agenticStore.getLiveLlmTotals(convId) : null;

		if (liveLlm) {
			const outputMs = liveLlm.predicted_ms;
			const averageTokensPerSecond =
				outputMs > 0 && liveLlm.predicted_n > 0 ? (liveLlm.predicted_n / outputMs) * 1000 : null;

			return {
				averageTokensPerSecond,
				cacheTotal: 0,
				output: liveLlm.predicted_n,
				read: liveLlm.prompt_n
			};
		}

		const { cacheTotal, lastAgenticLlm, output, outputMs, read } = this.assistantTimings;

		// Agentic sessions stamp the same agentic.llm totals onto every
		// assistant message; cache_n is never per-turn so cache_total stays 0.
		if (lastAgenticLlm) {
			const averageTokensPerSecond =
				lastAgenticLlm.predicted_ms > 0 && lastAgenticLlm.predicted_n > 0
					? (lastAgenticLlm.predicted_n / lastAgenticLlm.predicted_ms) * 1000
					: null;

			return {
				averageTokensPerSecond,
				cacheTotal: 0,
				output: lastAgenticLlm.predicted_n ?? 0,
				read: lastAgenticLlm.prompt_n ?? 0
			};
		}

		const averageTokensPerSecond = outputMs > 0 && output > 0 ? (output / outputMs) * 1000 : null;

		return { averageTokensPerSecond, cacheTotal, output, read };
	});

	averageTokensPerSecond = $derived(this.cumulative.averageTokensPerSecond);

	contextTotal = $derived.by(() => {
		void modelsStore.props.cacheVersion;

		return this.activeModelId ? modelsStore.props.getModelContextSize(this.activeModelId) : null;
	});

	private liveStats = $derived(deriveLiveStats(chatStore.processing.activeState));

	currentOutput = $derived.by(() => {
		if (this.liveStats && this.liveStats.outputTokens > 0) return this.liveStats.outputTokens;

		return this.assistantTimings.lastTimings?.predicted_n ?? 0;
	});

	currentRead = $derived.by(() => {
		const timings = this.assistantTimings.lastTimings;

		let read = 0;

		if (timings) {
			read = (timings.prompt_n ?? 0) + (timings.cache_n ?? 0);
		}

		// live.promptTokens is already the combined reading (prompt + cache),
		// so do not also add live.cacheTokens.
		if (this.liveStats && this.liveStats.promptTokens > 0) {
			read = Math.max(read, this.liveStats.promptTokens);
		}

		return read;
	});

	contextUsed = $derived(this.currentRead + this.currentOutput);

	contextAvailable = $derived(
		this.contextTotal !== null ? this.contextTotal - this.contextUsed : null
	);

	contextPercent = $derived.by(() => {
		if (this.contextTotal === null || this.contextTotal <= 0) return null;

		return Math.round((this.contextUsed / this.contextTotal) * 100);
	});

	cumulativeCacheTotal = $derived(this.cumulative.cacheTotal);

	cumulativeOutput = $derived(this.cumulative.output);

	cumulativeRead = $derived(this.cumulative.read);

	currentCache = $derived.by(() => {
		const cached = this.assistantTimings.lastTimings?.cache_n ?? 0;

		if (this.liveStats && this.liveStats.promptTokens > 0) {
			return Math.max(cached, this.liveStats.cacheTokens);
		}

		return cached;
	});

	currentFresh = $derived.by(() => {
		const fresh = this.assistantTimings.lastTimings?.prompt_n ?? 0;

		return Math.max(fresh, this.liveStats?.freshTokens ?? 0);
	});

	isActiveModelLoaded = $derived(
		this.activeModelId !== null &&
			(!serverStore.isRouterMode || modelsStore.isModelLoaded(this.activeModelId))
	);

	isActiveModelLoading = $derived(
		this.activeModelId !== null && modelsStore.status.isOperationInProgress(this.activeModelId)
	);

	kvTotal = $derived(this.currentRead + this.currentOutput);
}

export const contextStatsStore = new ContextStatsStore();