dsh-usage-overview 0.0.0-stage → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/usage.js ADDED
@@ -0,0 +1,425 @@
1
+ import { readdir, readFile, stat } from 'node:fs/promises';
2
+ import { join } from 'node:path';
3
+
4
+ import { foldSessionLog } from './log-fold.js';
5
+ import { PANEL_ID, USAGE_SCHEMA_VERSION, cacheHitRate } from './protocol.js';
6
+
7
+ function emptyTotals() {
8
+ return {
9
+ uncachedInputTokens: 0,
10
+ cacheReadTokens: 0,
11
+ cacheWriteTokens: 0,
12
+ outputTokens: 0,
13
+ };
14
+ }
15
+
16
+ function addTotals(target, source) {
17
+ if (typeof source !== 'object' || source === null) return;
18
+ for (const key of Object.keys(target)) {
19
+ const value = source[key];
20
+ if (typeof value === 'number' && Number.isFinite(value) && value > 0) target[key] += value;
21
+ }
22
+ }
23
+
24
+ function totalOf(totals) {
25
+ if (typeof totals !== 'object' || totals === null) return 0;
26
+ let sum = 0;
27
+ for (const value of Object.values(totals)) {
28
+ if (typeof value === 'number' && Number.isFinite(value)) sum += value;
29
+ }
30
+ return sum;
31
+ }
32
+
33
+ async function readJsonQuietly(path) {
34
+ try {
35
+ const text = await readFile(path, 'utf8');
36
+ return JSON.parse(text);
37
+ } catch {
38
+ return undefined;
39
+ }
40
+ }
41
+
42
+ function foldProjectionCache(document) {
43
+ const rows = document?.record?.rows;
44
+ if (typeof rows !== 'object' || rows === null) return undefined;
45
+
46
+ const identity = document?.record?.identity;
47
+ const createdAt = typeof identity?.createdAt === 'number' ? identity.createdAt : undefined;
48
+ const cwd = typeof identity?.cwd === 'string' ? identity.cwd : undefined;
49
+
50
+ const usage = rows.tokenUsage?.val;
51
+ const totals = emptyTotals();
52
+ if (typeof usage?.totals === 'object' && usage.totals !== null) addTotals(totals, usage.totals);
53
+
54
+ const selection = rows.modelSelection?.val?.lastUsed;
55
+ const provider = typeof selection?.provider === 'string' ? selection.provider : undefined;
56
+ const model = typeof selection?.model === 'string' ? selection.model : undefined;
57
+
58
+ const stats = rows.sessionStats?.val;
59
+ const sessionStats =
60
+ typeof stats === 'object' && stats !== null
61
+ ? {
62
+ turns: typeof stats.turns === 'number' ? stats.turns : 0,
63
+ steps: typeof stats.steps === 'number' ? stats.steps : 0,
64
+ llmMs: typeof stats.llmMs === 'number' ? stats.llmMs : 0,
65
+ toolMs: typeof stats.toolMs === 'number' ? stats.toolMs : 0,
66
+ }
67
+ : undefined;
68
+
69
+ const title = rows.title?.val;
70
+ const lastPromptAt = rows.sessionListMetadata?.val?.lastPromptAt;
71
+
72
+ return {
73
+ createdAt,
74
+ cwd,
75
+ provider,
76
+ model,
77
+ totals,
78
+ sessionStats,
79
+ title: typeof title === 'string' && title.length > 0 ? title : undefined,
80
+ lastActivityAt: typeof lastPromptAt === 'number' ? lastPromptAt : createdAt,
81
+ };
82
+ }
83
+
84
+ export async function collectSessions(home) {
85
+ const byId = new Map();
86
+
87
+ const indexPath = join(home, 'sessions');
88
+ let workspaceDirs = [];
89
+ try {
90
+ workspaceDirs = await readdir(indexPath, { withFileTypes: true });
91
+ } catch {
92
+ workspaceDirs = [];
93
+ }
94
+
95
+ for (const workspaceDir of workspaceDirs) {
96
+ if (!workspaceDir.isDirectory()) continue;
97
+ const workspacePath = join(indexPath, workspaceDir.name);
98
+ let sessionDirs = [];
99
+ try {
100
+ sessionDirs = await readdir(workspacePath, { withFileTypes: true });
101
+ } catch {
102
+ continue;
103
+ }
104
+ for (const sessionDir of sessionDirs) {
105
+ if (!sessionDir.isDirectory()) continue;
106
+ const sessionId = sessionDir.name;
107
+ if (!sessionId.startsWith('session-')) continue;
108
+ const absolute = join(workspacePath, sessionId);
109
+ let createdAt;
110
+ try {
111
+ const info = await stat(absolute);
112
+ createdAt = info.birthtimeMs > 0 ? info.birthtimeMs : info.mtimeMs;
113
+ } catch {
114
+ createdAt = undefined;
115
+ }
116
+ byId.set(sessionId, {
117
+ id: sessionId,
118
+ createdAt,
119
+ totals: emptyTotals(),
120
+ byDay: new Map(),
121
+ byModel: new Map(),
122
+ observed: false,
123
+ logPath: join(absolute, 'session.v4.jsonl.zstd'),
124
+ });
125
+ }
126
+ }
127
+
128
+ for (const [sessionId, record] of byId) {
129
+ const folded = foldSessionLog(record.logPath);
130
+ if (folded === undefined) continue;
131
+ byId.set(sessionId, {
132
+ ...record,
133
+ createdAt: folded.firstAt ?? record.createdAt,
134
+ totals: folded.totals,
135
+ byDay: folded.byDay,
136
+ byModel: folded.byModel,
137
+ wallMs: folded.wallMs,
138
+ turns: folded.turns,
139
+ steps: folded.steps,
140
+ lastActivityAt: folded.lastAt ?? record.createdAt,
141
+ observed: true,
142
+ });
143
+ }
144
+
145
+ const cachePath = join(home, 'storages', 'session_projcache', 'sessions');
146
+ let cacheFiles = [];
147
+ try {
148
+ cacheFiles = await readdir(cachePath);
149
+ } catch {
150
+ cacheFiles = [];
151
+ }
152
+
153
+ for (const fileName of cacheFiles) {
154
+ if (!fileName.endsWith('.json')) continue;
155
+ const sessionId = fileName.slice(0, -'.json'.length);
156
+ const folded = foldProjectionCache(await readJsonQuietly(join(cachePath, fileName)));
157
+ if (folded === undefined) continue;
158
+
159
+ const existing = byId.get(sessionId);
160
+ const hasLogFigures = existing !== undefined && totalOf(existing.totals) > 0;
161
+ byId.set(sessionId, {
162
+ id: sessionId,
163
+ createdAt: existing?.createdAt ?? folded.createdAt,
164
+ cwd: folded.cwd,
165
+ provider: folded.provider,
166
+ model: folded.model,
167
+ totals: hasLogFigures ? existing.totals : folded.totals,
168
+ byDay: existing?.byDay ?? new Map(),
169
+ byModel: existing?.byModel ?? new Map(),
170
+ sessionStats: folded.sessionStats,
171
+ title: folded.title,
172
+ wallMs: existing?.wallMs,
173
+ turns: existing?.turns,
174
+ steps: existing?.steps,
175
+ lastActivityAt: existing?.lastActivityAt ?? folded.lastActivityAt ?? folded.createdAt,
176
+ observed: true,
177
+ logPath: existing?.logPath,
178
+ });
179
+ }
180
+
181
+ return [...byId.values()];
182
+ }
183
+
184
+ export function mergeLive(sessions, live) {
185
+ if (live.size === 0) return sessions;
186
+ return sessions.map((session) => {
187
+ const running = live.get(session.id);
188
+ if (running === undefined) return session;
189
+ return {
190
+ ...session,
191
+ createdAt: session.createdAt ?? running.createdAt,
192
+ provider: running.provider ?? session.provider,
193
+ model: running.model ?? session.model,
194
+ totals: totalOf(running.totals) > 0 ? running.totals : session.totals,
195
+ sessionStats: running.sessionStats ?? session.sessionStats,
196
+ lastActivityAt: running.lastActivityAt ?? session.lastActivityAt,
197
+ live: true,
198
+ };
199
+ });
200
+ }
201
+
202
+ export function buildUsageDocument(sessions, options) {
203
+ const { now, rangeDays, providerLabels, liveIds } = options;
204
+
205
+ const windowStart = rangeDays === null ? null : now - rangeDays * 86400000;
206
+
207
+ const dayInWindow = (key) => {
208
+ if (windowStart === null) return true;
209
+ const [year, month, day] = key.split('-').map(Number);
210
+ return new Date(year, month - 1, day).getTime() >= windowStart;
211
+ };
212
+
213
+ const scoped = sessions.filter((session) => {
214
+ if (windowStart === null) return true;
215
+ if (session.byDay !== undefined && session.byDay.size > 0) {
216
+ for (const key of session.byDay.keys()) if (dayInWindow(key)) return true;
217
+ return false;
218
+ }
219
+ const stamp = session.lastActivityAt ?? session.createdAt;
220
+ if (typeof stamp !== 'number') return true;
221
+ return stamp >= windowStart;
222
+ });
223
+
224
+ const totals = emptyTotals();
225
+ let peakSessionTokens = 0;
226
+ let longestSessionMs = 0;
227
+ let turns = 0;
228
+ let steps = 0;
229
+
230
+ for (const session of scoped) {
231
+ if (session.byDay !== undefined && session.byDay.size > 0) {
232
+ let sessionTotal = 0;
233
+ for (const [key, day] of session.byDay) {
234
+ if (!dayInWindow(key)) continue;
235
+ addTotals(totals, day.totals);
236
+ sessionTotal += totalOf(day.totals);
237
+ }
238
+ if (sessionTotal > peakSessionTokens) peakSessionTokens = sessionTotal;
239
+ } else {
240
+ addTotals(totals, session.totals);
241
+ const sessionTotal = totalOf(session.totals);
242
+ if (sessionTotal > peakSessionTokens) peakSessionTokens = sessionTotal;
243
+ }
244
+ const span = session.wallMs ?? session.sessionStats?.llmMs ?? 0;
245
+ if (span > longestSessionMs) longestSessionMs = span;
246
+ turns += session.turns ?? session.sessionStats?.turns ?? 0;
247
+ steps += session.steps ?? session.sessionStats?.steps ?? 0;
248
+ }
249
+
250
+ const dayMap = new Map();
251
+ const ensureDay = (key) => {
252
+ let entry = dayMap.get(key);
253
+ if (entry === undefined) {
254
+ entry = { day: key, totals: emptyTotals(), sessions: 0, byModel: new Map(), seen: new Set() };
255
+ dayMap.set(key, entry);
256
+ }
257
+ return entry;
258
+ };
259
+
260
+ for (const session of scoped) {
261
+ if (session.byDay !== undefined && session.byDay.size > 0) {
262
+ for (const [key, day] of session.byDay) {
263
+ if (!dayInWindow(key)) continue;
264
+ const entry = ensureDay(key);
265
+ addTotals(entry.totals, day.totals);
266
+ if (!entry.seen.has(session.id)) {
267
+ entry.seen.add(session.id);
268
+ entry.sessions += 1;
269
+ }
270
+ for (const route of day.byRoute.values()) {
271
+ const routeKey = `${route.provider}/${route.model}`;
272
+ entry.byModel.set(routeKey, (entry.byModel.get(routeKey) ?? 0) + route.total);
273
+ }
274
+ }
275
+ continue;
276
+ }
277
+ const stamp = session.lastActivityAt ?? session.createdAt;
278
+ if (typeof stamp !== 'number' || !Number.isFinite(stamp)) continue;
279
+ const key = localDayKey(stamp);
280
+ if (!dayInWindow(key)) continue;
281
+ const sessionTotal = totalOf(session.totals);
282
+ if (sessionTotal <= 0) continue;
283
+ const entry = ensureDay(key);
284
+ entry.sessions += 1;
285
+ addTotals(entry.totals, session.totals);
286
+ const seriesKey = session.model ?? 'unknown';
287
+ entry.byModel.set(seriesKey, (entry.byModel.get(seriesKey) ?? 0) + sessionTotal);
288
+ }
289
+
290
+ const days = [...dayMap.values()]
291
+ .sort((a, b) => a.day.localeCompare(b.day))
292
+ .map((entry) => ({
293
+ day: entry.day,
294
+ total: totalOf(entry.totals),
295
+ totals: entry.totals,
296
+ sessions: entry.sessions,
297
+ byModel: Object.fromEntries(entry.byModel),
298
+ }));
299
+
300
+ const modelMap = new Map();
301
+ const ensureModel = (provider, model) => {
302
+ const key = `${provider}\u0000${model}`;
303
+ let entry = modelMap.get(key);
304
+ if (entry === undefined) {
305
+ entry = {
306
+ model,
307
+ provider,
308
+ source: providerLabels[provider] ?? provider,
309
+ totals: emptyTotals(),
310
+ sessions: 0,
311
+ calls: 0,
312
+ turns: 0,
313
+ seen: new Set(),
314
+ turnIds: new Set(),
315
+ };
316
+ modelMap.set(key, entry);
317
+ }
318
+ return entry;
319
+ };
320
+
321
+ for (const session of scoped) {
322
+ if (session.byDay !== undefined && session.byDay.size > 0) {
323
+ for (const [key, day] of session.byDay) {
324
+ if (!dayInWindow(key)) continue;
325
+ for (const route of day.byRoute.values()) {
326
+ const entry = ensureModel(route.provider, route.model);
327
+ addTotals(entry.totals, route.totals);
328
+ entry.calls += route.samples;
329
+ for (const turn of route.turns) entry.turnIds.add(`${session.id}:${String(turn)}`);
330
+ if (!entry.seen.has(session.id)) {
331
+ entry.seen.add(session.id);
332
+ entry.sessions += 1;
333
+ }
334
+ }
335
+ }
336
+ continue;
337
+ }
338
+ const entry = ensureModel(session.provider ?? 'unknown', session.model ?? 'unknown');
339
+ addTotals(entry.totals, session.totals);
340
+ entry.sessions += 1;
341
+ }
342
+
343
+ const models = [...modelMap.values()]
344
+ .map(({ seen, turnIds, ...entry }) => ({ ...entry, turns: turnIds.size, total: totalOf(entry.totals) }))
345
+ .sort((a, b) => b.total - a.total);
346
+
347
+ const activeDays = [...new Set(days.filter((day) => day.total > 0).map((day) => day.day))].sort();
348
+ const { current: currentStreak, longest: longestStreak } = streaks(activeDays, now);
349
+
350
+ const grandTotal = totalOf(totals);
351
+
352
+ const peakDay = days.reduce(
353
+ (best, day) => (day.total > best.total ? { total: day.total, day: day.day } : best),
354
+ { total: 0, day: null },
355
+ );
356
+
357
+ return {
358
+ schemaVersion: USAGE_SCHEMA_VERSION,
359
+ generatedAt: now,
360
+ panelId: PANEL_ID,
361
+ scope: {
362
+ rangeDays,
363
+ windowStart,
364
+ sessionCount: scoped.length,
365
+ totalSessionCount: sessions.length,
366
+ liveSessionCount: liveIds.size,
367
+ },
368
+ totals: {
369
+ ...totals,
370
+ total: grandTotal,
371
+ peakDayTokens: peakDay.total,
372
+ peakDay: peakDay.day,
373
+ peakSessionTokens,
374
+ longestSessionMs,
375
+ turns,
376
+ steps,
377
+ currentStreak,
378
+ longestStreak,
379
+ cacheHitRate: cacheHitRate(totals),
380
+ cacheReadTokens: totals.cacheReadTokens,
381
+ cacheWriteTokens: totals.cacheWriteTokens,
382
+ uncachedInputTokens: totals.uncachedInputTokens,
383
+ },
384
+ days,
385
+ models,
386
+ };
387
+ }
388
+
389
+ function localDayKey(ms) {
390
+ const date = new Date(ms);
391
+ const month = String(date.getMonth() + 1).padStart(2, '0');
392
+ const day = String(date.getDate()).padStart(2, '0');
393
+ return `${String(date.getFullYear())}-${month}-${day}`;
394
+ }
395
+
396
+ function streaks(activeDays, now) {
397
+ if (activeDays.length === 0) return { current: 0, longest: 0 };
398
+
399
+ const asDay = (key) => {
400
+ const [year, month, day] = key.split('-').map(Number);
401
+ return Date.UTC(year, month - 1, day);
402
+ };
403
+
404
+ let longest = 1;
405
+ let run = 1;
406
+ for (let index = 1; index < activeDays.length; index += 1) {
407
+ const gap = asDay(activeDays[index]) - asDay(activeDays[index - 1]);
408
+ run = gap === 86400000 ? run + 1 : 1;
409
+ if (run > longest) longest = run;
410
+ }
411
+
412
+ const today = asDay(localDayKey(now));
413
+ const last = asDay(activeDays[activeDays.length - 1]);
414
+ const behind = Math.round((today - last) / 86400000);
415
+ let current = 0;
416
+ if (behind === 0 || behind === 1) {
417
+ current = 1;
418
+ for (let index = activeDays.length - 1; index > 0; index -= 1) {
419
+ if (asDay(activeDays[index]) - asDay(activeDays[index - 1]) !== 86400000) break;
420
+ current += 1;
421
+ }
422
+ }
423
+
424
+ return { current, longest };
425
+ }
package/package.json CHANGED
@@ -1,6 +1,46 @@
1
1
  {
2
2
  "name": "dsh-usage-overview",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "displayName": "DSH Usage Overview",
4
+ "description": "A usage overview for DeepSeek Harness: token totals, activity heatmap, daily token and cache trends, and per-model attribution.",
5
+ "version": "1.0.0",
6
+ "author": "w-callan-w",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/w-callan-w/dsh-usage-overview.git"
10
+ },
11
+ "keywords": [
12
+ "dsh-plugin",
13
+ "deepseek-harness",
14
+ "deepseek",
15
+ "usage",
16
+ "overview",
17
+ "statistics",
18
+ "tokens",
19
+ "analytics"
20
+ ],
21
+ "type": "module",
22
+ "main": "lib/index.js",
23
+ "exports": {
24
+ ".": "./lib/index.js",
25
+ "./client": "./lib/client.js",
26
+ "./cordis.patch.yml": "./cordis.patch.yml",
27
+ "./package.json": "./package.json"
28
+ },
29
+ "files": [
30
+ "lib",
31
+ "cordis.patch.yml",
32
+ "README.md",
33
+ "README.zh-CN.md"
34
+ ],
35
+ "license": "MIT",
36
+ "dsh": {
37
+ "manifestVersion": 1,
38
+ "bundle": {
39
+ "patch": "./cordis.patch.yml"
40
+ },
41
+ "client": {
42
+ "platform": "web",
43
+ "immediately": true
44
+ }
45
+ }
46
+ }