dsh-usage-prism 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +142 -2
- package/client.js +1080 -0
- package/cordis.patch.yml +5 -0
- package/docs/DETAIL.md +1 -0
- package/docs/preview.png +0 -0
- package/icon.svg +6 -0
- package/index.js +271 -0
- package/lib/fold.js +512 -0
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +61 -4
package/lib/fold.js
ADDED
|
@@ -0,0 +1,512 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 会话事件 → 用量汇总的纯折叠。不读盘、不取当前时间,只吃事件数组,
|
|
3
|
+
* 所以测试可以喂进固定的事件、也拿真实日志逐项对账。
|
|
4
|
+
*
|
|
5
|
+
* 记账口径跟着 dsh 自己的 `@deepseek-ai/dsh-session-stats` 与 `tokenUsage` 投影走,
|
|
6
|
+
* 不自己发明:一个步骤的模型时间 = `assistant/message` 落盘 − `step/start`,
|
|
7
|
+
* 首 token = 首个非空增量块,生成 = 落盘 − 首 token,工具 = `tool/result` − `tool/call`。
|
|
8
|
+
* 这三段相加恰好等于投影里的 llm + tool,没有第四桶。
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/** 本地日历日 key(YYYY-MM-DD)。不折时区:「本日」就是这台机器的今天。 */
|
|
12
|
+
export function dayKey(ms) {
|
|
13
|
+
const d = new Date(ms)
|
|
14
|
+
const pad = (n) => String(n).padStart(2, '0')
|
|
15
|
+
return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* 折叠口径的版本。改了这里的记账规则——哪怕字段没变、只是算法变了——就把它加一。
|
|
20
|
+
* Host 拿它给磁盘摘要的键做前缀;不加的话旧摘要会以「会话没变化」的身份被复用,
|
|
21
|
+
* 面板显示的就是上一版口径的数字,还得等会话再长一点才自愈。
|
|
22
|
+
*/
|
|
23
|
+
export const FOLD_VERSION = 1
|
|
24
|
+
|
|
25
|
+
/** 本地小时 key(YYYY-MM-DDTHH),只给「本日」那 24 格用。 */
|
|
26
|
+
export function hourKey(ms) {
|
|
27
|
+
return `${dayKey(ms)}T${String(new Date(ms).getHours()).padStart(2, '0')}`
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function count(value) {
|
|
31
|
+
return typeof value === 'number' && Number.isFinite(value) && value >= 0 ? value : 0
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** 一个增量块是否算「模型真的吐了东西」(`isTokenDelta`)。 */
|
|
35
|
+
function isTokenDelta(chunk) {
|
|
36
|
+
if (chunk === null || typeof chunk !== 'object') return false
|
|
37
|
+
switch (chunk.type) {
|
|
38
|
+
case 'text-delta':
|
|
39
|
+
case 'reasoning-delta':
|
|
40
|
+
return chunk.text !== ''
|
|
41
|
+
case 'tool-call-delta':
|
|
42
|
+
return chunk.argumentsDelta !== '' || chunk.name !== undefined
|
|
43
|
+
default:
|
|
44
|
+
return false
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** 打包块里第一个满足条件的片段的时刻(`firstRunMemberTime`)。 */
|
|
49
|
+
function firstRunMemberTime(run, predicate) {
|
|
50
|
+
const fragments = run.type === 'tool-call-chunks' ? run.args : run.texts
|
|
51
|
+
if (!Array.isArray(fragments)) return undefined
|
|
52
|
+
let time = run.time0
|
|
53
|
+
for (let i = 0; i < fragments.length; i += 1) {
|
|
54
|
+
if (i > 0) time += run.dt?.[i - 1] ?? 0
|
|
55
|
+
if (predicate(fragments[i])) return time
|
|
56
|
+
}
|
|
57
|
+
return undefined
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** 一条流里首个非空输出的时刻(`assistantStreamFirstTokenTime`)。 */
|
|
61
|
+
export function firstTokenTime(stream) {
|
|
62
|
+
if (!Array.isArray(stream)) return undefined
|
|
63
|
+
for (const record of stream) {
|
|
64
|
+
if (record === null || typeof record !== 'object') continue
|
|
65
|
+
let time
|
|
66
|
+
if (record.type === 'chunk') time = isTokenDelta(record.chunk) ? record.time : undefined
|
|
67
|
+
else if (record.type === 'tool-call-chunks' && record.name !== undefined) time = record.time0
|
|
68
|
+
else time = firstRunMemberTime(record, (fragment) => fragment !== '')
|
|
69
|
+
if (time !== undefined) return time
|
|
70
|
+
}
|
|
71
|
+
return undefined
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* 一条事件携带的用量样本:优先 `data.usage`,否则流里最后一个 `usage` 块
|
|
76
|
+
* (`tokenUsage` 投影对 `assistant/attempt` 就是这么取的)。
|
|
77
|
+
*/
|
|
78
|
+
export function usageOf(event) {
|
|
79
|
+
const direct = event?.data?.usage
|
|
80
|
+
if (direct !== null && typeof direct === 'object') return direct
|
|
81
|
+
const stream = event?.data?.stream
|
|
82
|
+
if (!Array.isArray(stream)) return undefined
|
|
83
|
+
for (let i = stream.length - 1; i >= 0; i -= 1) {
|
|
84
|
+
const record = stream[i]
|
|
85
|
+
const chunk = record?.type === 'chunk' ? record.chunk : undefined
|
|
86
|
+
if (chunk?.type === 'usage' && chunk.usage !== null && typeof chunk.usage === 'object') return chunk.usage
|
|
87
|
+
}
|
|
88
|
+
return undefined
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function emptyTotals() {
|
|
92
|
+
return { tokens: 0, inTok: 0, outTok: 0, cacheRead: 0, turns: 0, steps: 0, tools: 0, waitMs: 0, genMs: 0, toolMs: 0 }
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function addInto(target, source) {
|
|
96
|
+
target.tokens += source.tokens
|
|
97
|
+
target.inTok += source.inTok
|
|
98
|
+
target.outTok += source.outTok
|
|
99
|
+
target.cacheRead += source.cacheRead
|
|
100
|
+
target.turns += source.turns
|
|
101
|
+
target.steps += source.steps
|
|
102
|
+
target.tools += source.tools
|
|
103
|
+
target.waitMs += source.waitMs
|
|
104
|
+
target.genMs += source.genMs
|
|
105
|
+
target.toolMs += source.toolMs
|
|
106
|
+
return target
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** 一个(天|小时)× 模型的计数行。样本数组只在天这一层留。 */
|
|
110
|
+
function modelRow(map, key) {
|
|
111
|
+
let row = map.get(key)
|
|
112
|
+
if (row === undefined) {
|
|
113
|
+
row = { tokens: 0, inTok: 0, outTok: 0, cacheRead: 0, n: 0, tools: 0, ttft: [], tps: [] }
|
|
114
|
+
map.set(key, row)
|
|
115
|
+
}
|
|
116
|
+
return row
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** 减一行计数(`modelRow()` 出来的行和 `bucket()` 出来的行都吃这个)。 */
|
|
120
|
+
function subtractInto(target, row) {
|
|
121
|
+
target.tokens -= row.tokens
|
|
122
|
+
target.inTok -= row.inTok
|
|
123
|
+
target.outTok -= row.outTok
|
|
124
|
+
target.cacheRead -= row.cacheRead
|
|
125
|
+
return target
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** 减完归零的桶就删掉,别在按天 / 按小时的表里留空行。 */
|
|
129
|
+
function isZeroBucket(row) {
|
|
130
|
+
return (
|
|
131
|
+
row.tokens === 0 &&
|
|
132
|
+
row.inTok === 0 &&
|
|
133
|
+
row.outTok === 0 &&
|
|
134
|
+
row.cacheRead === 0 &&
|
|
135
|
+
row.turns === 0 &&
|
|
136
|
+
row.steps === 0 &&
|
|
137
|
+
row.tools === 0 &&
|
|
138
|
+
row.waitMs === 0 &&
|
|
139
|
+
row.genMs === 0 &&
|
|
140
|
+
row.toolMs === 0
|
|
141
|
+
)
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** 同一个 (turn, step) 前后两次样本的模型不同时,旧模型的记账减完会归零。 */
|
|
145
|
+
function isZeroModel(row) {
|
|
146
|
+
return row.tokens === 0 && row.inTok === 0 && row.outTok === 0 && row.cacheRead === 0 && row.n === 0
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** 天|小时的计数行。 */
|
|
150
|
+
function bucket(map, key) {
|
|
151
|
+
let row = map.get(key)
|
|
152
|
+
if (row === undefined) {
|
|
153
|
+
row = { ...emptyTotals(), models: new Map() }
|
|
154
|
+
map.set(key, row)
|
|
155
|
+
}
|
|
156
|
+
return row
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function sourceKey(source) {
|
|
160
|
+
const provider = typeof source?.provider === 'string' ? source.provider : '—'
|
|
161
|
+
const model = typeof source?.model === 'string' ? source.model : '—'
|
|
162
|
+
return `${provider}/${model}`
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* 折一个会话的全部事件。
|
|
167
|
+
* @param sessionId - 会话 id,只用于「这个范围有几个会话」去重。
|
|
168
|
+
* @param events - 原始事件数组(seq 升序)。
|
|
169
|
+
* @returns 会话摘要:按天、按小时、按模型的计数与样本。
|
|
170
|
+
*/
|
|
171
|
+
export function foldSession(sessionId, events) {
|
|
172
|
+
const days = new Map()
|
|
173
|
+
const hours = new Map()
|
|
174
|
+
const pendingCalls = new Map()
|
|
175
|
+
let open = null
|
|
176
|
+
let currentModel = '—/—'
|
|
177
|
+
let lastSample = null
|
|
178
|
+
let lastTurn = null
|
|
179
|
+
|
|
180
|
+
for (const event of events) {
|
|
181
|
+
if (event === null || typeof event !== 'object') continue
|
|
182
|
+
switch (event.type) {
|
|
183
|
+
case 'request/context': {
|
|
184
|
+
const key = sourceKey(event.data)
|
|
185
|
+
if (key !== '—/—') currentModel = key
|
|
186
|
+
break
|
|
187
|
+
}
|
|
188
|
+
case 'request/header': {
|
|
189
|
+
const key = sourceKey(event.data?.header?.config)
|
|
190
|
+
if (key !== '—/—') currentModel = key
|
|
191
|
+
break
|
|
192
|
+
}
|
|
193
|
+
case 'step/start':
|
|
194
|
+
open = { turn: event.data?.turn, step: event.data?.step, start: event.time, first: null }
|
|
195
|
+
break
|
|
196
|
+
case 'llm/retry-started':
|
|
197
|
+
// 重试结束「同名样本」的替换窗口(同 tokenUsage 投影)。
|
|
198
|
+
if (lastSample !== null && lastSample.turn === event.data?.turn && lastSample.step === event.data?.step) {
|
|
199
|
+
lastSample = null
|
|
200
|
+
}
|
|
201
|
+
break
|
|
202
|
+
case 'assistant/attempt': {
|
|
203
|
+
const sample = usageOf(event)
|
|
204
|
+
if (sample !== undefined) recordUsage(event, sample, currentModel)
|
|
205
|
+
if (open === null) break
|
|
206
|
+
if (open.turn !== event.data?.turn || open.step !== event.data?.step) break
|
|
207
|
+
const first = firstTokenTime(event.data?.stream)
|
|
208
|
+
if (open.first === null && first !== undefined) open.first = first
|
|
209
|
+
break
|
|
210
|
+
}
|
|
211
|
+
case 'assistant/message': {
|
|
212
|
+
// 用量无条件记账(`tokenUsage` 投影就是这么加的);耗时才要求对上 step 边界。
|
|
213
|
+
const time = event.time
|
|
214
|
+
const key = sourceKey(event.data?.message?.source)
|
|
215
|
+
if (key !== '—/—') currentModel = key
|
|
216
|
+
const sample = usageOf(event)
|
|
217
|
+
if (sample !== undefined) recordUsage(event, sample, currentModel, key)
|
|
218
|
+
|
|
219
|
+
const matched = open !== null && open.turn === event.data?.turn && open.step === event.data?.step
|
|
220
|
+
if (!matched) break
|
|
221
|
+
const day = bucket(days, dayKey(time))
|
|
222
|
+
const hour = bucket(hours, hourKey(time))
|
|
223
|
+
const first = open.first ?? firstTokenTime(event.data?.stream)
|
|
224
|
+
if (first !== undefined) {
|
|
225
|
+
const ttft = Math.max(0, first - open.start)
|
|
226
|
+
day.waitMs += ttft
|
|
227
|
+
hour.waitMs += ttft
|
|
228
|
+
const dayModel = modelRow(day.models, key)
|
|
229
|
+
dayModel.n += 1
|
|
230
|
+
dayModel.ttft.push(ttft)
|
|
231
|
+
const outTokens = event.data?.usage?.outputTokens
|
|
232
|
+
if (typeof outTokens === 'number' && Number.isFinite(outTokens) && outTokens >= 0) {
|
|
233
|
+
const decode = Math.max(0, time - first)
|
|
234
|
+
day.genMs += decode
|
|
235
|
+
hour.genMs += decode
|
|
236
|
+
// t/s 的样本只收真吐过 token 的那一次:0 会把中位数一路往下拽。
|
|
237
|
+
if (decode > 0 && outTokens > 0) dayModel.tps.push((outTokens * 1000) / decode)
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
open = null
|
|
241
|
+
break
|
|
242
|
+
}
|
|
243
|
+
case 'tool/call': {
|
|
244
|
+
const callId = event.data?.callId
|
|
245
|
+
if (typeof callId === 'string') pendingCalls.set(callId, event.time)
|
|
246
|
+
break
|
|
247
|
+
}
|
|
248
|
+
case 'tool/result': {
|
|
249
|
+
const callId = event.data?.message?.source?.callId
|
|
250
|
+
if (typeof callId !== 'string' || !pendingCalls.has(callId)) break
|
|
251
|
+
const spent = Math.max(0, event.time - pendingCalls.get(callId))
|
|
252
|
+
pendingCalls.delete(callId)
|
|
253
|
+
for (const target of [bucket(days, dayKey(event.time)), bucket(hours, hourKey(event.time))]) {
|
|
254
|
+
target.toolMs += spent
|
|
255
|
+
target.tools += 1
|
|
256
|
+
}
|
|
257
|
+
break
|
|
258
|
+
}
|
|
259
|
+
case 'turn/end':
|
|
260
|
+
// 没落地的工具调用属于取消或失败的轮,丢掉,别跨天挂着(同 projection)。
|
|
261
|
+
pendingCalls.clear()
|
|
262
|
+
break
|
|
263
|
+
case 'step/end': {
|
|
264
|
+
// 步骤每个 step/end 记一次,轮次只在这一轮的第一个 step/end 记(同 sessionStats)。
|
|
265
|
+
// 步骤数不是「组装出消息的次数」:取消 / 失败 / max-tokens 的步骤也各落一个 step/end。
|
|
266
|
+
const newTurn = lastTurn !== event.data?.turn
|
|
267
|
+
lastTurn = event.data?.turn
|
|
268
|
+
for (const target of [bucket(days, dayKey(event.time)), bucket(hours, hourKey(event.time))]) {
|
|
269
|
+
if (newTurn) target.turns += 1
|
|
270
|
+
target.steps += 1
|
|
271
|
+
}
|
|
272
|
+
open = null
|
|
273
|
+
break
|
|
274
|
+
}
|
|
275
|
+
default:
|
|
276
|
+
break
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
return { id: sessionId, days, hours }
|
|
281
|
+
|
|
282
|
+
/** 一条用量样本按 (turn, step) 后写覆盖前写;覆盖时先把上一次的贡献扣掉。 */
|
|
283
|
+
function recordUsage(event, usage, fallbackKey, explicitKey) {
|
|
284
|
+
const turn = event.data?.turn
|
|
285
|
+
const step = event.data?.step
|
|
286
|
+
const key = explicitKey !== undefined && explicitKey !== '—/—' ? explicitKey : fallbackKey
|
|
287
|
+
const uncached = count(usage?.inputTokens)
|
|
288
|
+
const outTok = count(usage?.outputTokens)
|
|
289
|
+
const cacheRead = count(usage?.cacheReadTokens)
|
|
290
|
+
const cacheWrite = count(usage?.cacheWriteTokens)
|
|
291
|
+
// 输入侧记「模型实际吃进去的量」:未缓存 + 缓存读 + 缓存写。
|
|
292
|
+
// 只记未缓存会把命中率 98~99% 的会话少算几十倍。
|
|
293
|
+
const inTok = uncached + cacheRead + cacheWrite
|
|
294
|
+
const same = lastSample !== null && lastSample.turn === turn && lastSample.step === step
|
|
295
|
+
if (same) dropContribution(lastSample)
|
|
296
|
+
const contribution = {
|
|
297
|
+
turn,
|
|
298
|
+
step,
|
|
299
|
+
day: dayKey(event.time),
|
|
300
|
+
hour: hourKey(event.time),
|
|
301
|
+
key,
|
|
302
|
+
tokens: inTok + outTok,
|
|
303
|
+
inTok,
|
|
304
|
+
outTok,
|
|
305
|
+
cacheRead,
|
|
306
|
+
}
|
|
307
|
+
addContribution(contribution)
|
|
308
|
+
lastSample = contribution
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
function addContribution(row) {
|
|
312
|
+
const day = bucket(days, row.day)
|
|
313
|
+
day.tokens += row.tokens
|
|
314
|
+
day.inTok += row.inTok
|
|
315
|
+
day.outTok += row.outTok
|
|
316
|
+
day.cacheRead += row.cacheRead
|
|
317
|
+
const dayModel = modelRow(day.models, row.key)
|
|
318
|
+
dayModel.tokens += row.tokens
|
|
319
|
+
dayModel.inTok += row.inTok
|
|
320
|
+
dayModel.outTok += row.outTok
|
|
321
|
+
dayModel.cacheRead += row.cacheRead
|
|
322
|
+
const hour = bucket(hours, row.hour)
|
|
323
|
+
hour.tokens += row.tokens
|
|
324
|
+
hour.inTok += row.inTok
|
|
325
|
+
hour.outTok += row.outTok
|
|
326
|
+
hour.cacheRead += row.cacheRead
|
|
327
|
+
const hourModel = modelRow(hour.models, row.key)
|
|
328
|
+
hourModel.tokens += row.tokens
|
|
329
|
+
hourModel.inTok += row.inTok
|
|
330
|
+
hourModel.outTok += row.outTok
|
|
331
|
+
hourModel.cacheRead += row.cacheRead
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
function dropContribution(row) {
|
|
335
|
+
// 只用 get:为了扣一笔而新建一个桶,扣完再删,纯属绕路。
|
|
336
|
+
const day = days.get(row.day)
|
|
337
|
+
if (day !== undefined) {
|
|
338
|
+
subtractInto(day, row)
|
|
339
|
+
const dayModel = day.models.get(row.key)
|
|
340
|
+
if (dayModel !== undefined) {
|
|
341
|
+
subtractInto(dayModel, row)
|
|
342
|
+
if (isZeroModel(dayModel)) day.models.delete(row.key)
|
|
343
|
+
}
|
|
344
|
+
if (isZeroBucket(day)) days.delete(row.day)
|
|
345
|
+
}
|
|
346
|
+
const hour = hours.get(row.hour)
|
|
347
|
+
if (hour !== undefined) {
|
|
348
|
+
subtractInto(hour, row)
|
|
349
|
+
const hourModel = hour.models.get(row.key)
|
|
350
|
+
if (hourModel !== undefined) {
|
|
351
|
+
subtractInto(hourModel, row)
|
|
352
|
+
if (isZeroModel(hourModel)) hour.models.delete(row.key)
|
|
353
|
+
}
|
|
354
|
+
if (isZeroBucket(hour)) hours.delete(row.hour)
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
* 摘要 ↔ 纯 JSON。`foldSession()` 的返回值里有两层 Map,直接 `JSON.stringify` 会变成
|
|
361
|
+
* `{}`,所以显式转成数组;Host 用它把摘要写进本地缓存,重启后不必再解压日志。
|
|
362
|
+
*/
|
|
363
|
+
function encodeBucket(row) {
|
|
364
|
+
return { ...row, models: [...row.models].map(([key, model]) => [key, model]) }
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
function decodeBucket(row) {
|
|
368
|
+
const { models, ...rest } = row
|
|
369
|
+
return {
|
|
370
|
+
...rest,
|
|
371
|
+
models: new Map((models ?? []).map(([key, model]) => [key, { ...model, ttft: model.ttft ?? [], tps: model.tps ?? [] }])),
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
export function encodeSummary(summary) {
|
|
376
|
+
return {
|
|
377
|
+
id: summary.id,
|
|
378
|
+
days: [...summary.days].map(([key, row]) => [key, encodeBucket(row)]),
|
|
379
|
+
hours: [...summary.hours].map(([key, row]) => [key, encodeBucket(row)]),
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
export function decodeSummary(payload) {
|
|
384
|
+
return {
|
|
385
|
+
id: payload.id,
|
|
386
|
+
days: new Map((payload.days ?? []).map(([key, row]) => [key, decodeBucket(row)])),
|
|
387
|
+
hours: new Map((payload.hours ?? []).map(([key, row]) => [key, decodeBucket(row)])),
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/** 中位数;没有样本给 null(前端据此显示「—」)。 */
|
|
392
|
+
export function median(samples) {
|
|
393
|
+
if (!Array.isArray(samples) || samples.length === 0) return null
|
|
394
|
+
const sorted = [...samples].sort((a, b) => a - b)
|
|
395
|
+
const mid = sorted.length >> 1
|
|
396
|
+
return sorted.length % 2 === 1 ? sorted[mid] : (sorted[mid - 1] + sorted[mid]) / 2
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
function shareRows(models, limit = 3) {
|
|
400
|
+
const rows = []
|
|
401
|
+
for (const [key, row] of models) rows.push({ key, tokens: row.tokens })
|
|
402
|
+
rows.sort((a, b) => b.tokens - a.tokens)
|
|
403
|
+
return { rows: rows.slice(0, limit), otherTokens: rows.slice(limit).reduce((sum, row) => sum + row.tokens, 0) }
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
function publicBucket(row, keyName, key, sessions) {
|
|
407
|
+
const shares = shareRows(row.models)
|
|
408
|
+
return {
|
|
409
|
+
[keyName]: key,
|
|
410
|
+
tokens: row.tokens,
|
|
411
|
+
inTok: row.inTok,
|
|
412
|
+
outTok: row.outTok,
|
|
413
|
+
cacheRead: row.cacheRead,
|
|
414
|
+
sessions,
|
|
415
|
+
turns: row.turns,
|
|
416
|
+
steps: row.steps,
|
|
417
|
+
tools: row.tools,
|
|
418
|
+
waitMs: row.waitMs,
|
|
419
|
+
genMs: row.genMs,
|
|
420
|
+
toolMs: row.toolMs,
|
|
421
|
+
models: shares.rows,
|
|
422
|
+
otherTokens: shares.otherTokens,
|
|
423
|
+
// 前 3 名之外还有多少,悬浮卡用来说「其他 N 个」(不然卡片各行加起来 ≠ 格子)。
|
|
424
|
+
modelCount: row.models.size,
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/**
|
|
429
|
+
* 把一串会话摘要折成某段日期上的快照(两端都含;日期是 YYYY-MM-DD,字符串比较即可)。
|
|
430
|
+
* @param summaries - `foldSession()` 的返回值数组。
|
|
431
|
+
* @param range - `{ from, to }`,本地日期字符串。
|
|
432
|
+
*/
|
|
433
|
+
export function aggregate(summaries, range) {
|
|
434
|
+
const { from, to } = range
|
|
435
|
+
const days = new Map()
|
|
436
|
+
const hours = new Map()
|
|
437
|
+
const models = new Map()
|
|
438
|
+
const sessionsPerDay = new Map()
|
|
439
|
+
const totals = { ...emptyTotals(), sessions: 0 }
|
|
440
|
+
|
|
441
|
+
for (const summary of summaries) {
|
|
442
|
+
let inRange = false
|
|
443
|
+
for (const [key, row] of summary.days) {
|
|
444
|
+
if (key < from || key > to) continue
|
|
445
|
+
inRange = true
|
|
446
|
+
sessionsPerDay.set(key, (sessionsPerDay.get(key) ?? 0) + 1)
|
|
447
|
+
addInto(bucket(days, key), row)
|
|
448
|
+
for (const [modelKey, model] of row.models) {
|
|
449
|
+
const dayModel = modelRow(bucket(days, key).models, modelKey)
|
|
450
|
+
dayModel.tokens += model.tokens
|
|
451
|
+
dayModel.inTok += model.inTok
|
|
452
|
+
dayModel.outTok += model.outTok
|
|
453
|
+
dayModel.cacheRead += model.cacheRead
|
|
454
|
+
dayModel.n += model.n
|
|
455
|
+
dayModel.tools += model.tools
|
|
456
|
+
const into = modelRow(models, modelKey)
|
|
457
|
+
into.tokens += model.tokens
|
|
458
|
+
into.inTok += model.inTok
|
|
459
|
+
into.outTok += model.outTok
|
|
460
|
+
into.cacheRead += model.cacheRead
|
|
461
|
+
into.n += model.n
|
|
462
|
+
into.tools += model.tools
|
|
463
|
+
for (const sample of model.ttft) into.ttft.push(sample)
|
|
464
|
+
for (const sample of model.tps) into.tps.push(sample)
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
if (inRange) totals.sessions += 1
|
|
468
|
+
|
|
469
|
+
for (const [key, row] of summary.hours) {
|
|
470
|
+
const day = key.slice(0, 10)
|
|
471
|
+
if (day < from || day > to) continue
|
|
472
|
+
addInto(bucket(hours, key), row)
|
|
473
|
+
for (const [modelKey, model] of row.models) {
|
|
474
|
+
const hourModel = modelRow(bucket(hours, key).models, modelKey)
|
|
475
|
+
hourModel.tokens += model.tokens
|
|
476
|
+
hourModel.n += model.n
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
const dayRows = [...days.keys()].sort().map((key) => {
|
|
482
|
+
const row = days.get(key)
|
|
483
|
+
addInto(totals, row)
|
|
484
|
+
return publicBucket(row, 'day', key, sessionsPerDay.get(key) ?? 0)
|
|
485
|
+
})
|
|
486
|
+
|
|
487
|
+
const hourRows = [...hours.keys()].sort().map((key) => publicBucket(hours.get(key), 'hour', key, 0))
|
|
488
|
+
|
|
489
|
+
const modelRows = [...models.keys()]
|
|
490
|
+
.map((key) => {
|
|
491
|
+
const row = models.get(key)
|
|
492
|
+
const slash = key.indexOf('/')
|
|
493
|
+
const sampled = row.inTok
|
|
494
|
+
return {
|
|
495
|
+
key,
|
|
496
|
+
provider: slash < 0 ? key : key.slice(0, slash),
|
|
497
|
+
model: slash < 0 ? '' : key.slice(slash + 1),
|
|
498
|
+
n: row.n,
|
|
499
|
+
tokens: row.tokens,
|
|
500
|
+
inTok: row.inTok,
|
|
501
|
+
outTok: row.outTok,
|
|
502
|
+
cacheRead: row.cacheRead,
|
|
503
|
+
cacheRate: sampled > 0 ? row.cacheRead / sampled : null,
|
|
504
|
+
ttftMs: median(row.ttft),
|
|
505
|
+
tps: median(row.tps),
|
|
506
|
+
tools: row.tools,
|
|
507
|
+
}
|
|
508
|
+
})
|
|
509
|
+
.sort((a, b) => b.tokens - a.tokens)
|
|
510
|
+
|
|
511
|
+
return { totals, days: dayRows, hours: hourRows, models: modelRows }
|
|
512
|
+
}
|
package/locale/en.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"meta": {
|
|
3
|
+
"title": "Usage",
|
|
4
|
+
"description": "A usage panel in the sidebar: token usage, where time went (waiting, generating, tools) and per-model performance for today, this week, this month or this year. Open it from the Usage icon in the sidebar. Display only: no cost conversion and no budgets. It only reads local session logs — no network, no session changes. Sessions forked from another session are counted once per copy, so cross-session totals can run high.",
|
|
5
|
+
"detail": "docs/DETAIL.md"
|
|
6
|
+
}
|
|
7
|
+
}
|
package/locale/zh.json
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,63 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-usage-prism",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "侧边栏用量面板:按本日 / 本周 / 本月 / 本年把 token 用量、时间去向与逐模型性能拆开来看。只读本机会话日志。",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "shizukunatsuki",
|
|
8
|
+
"homepage": "https://github.com/shizukunatsuki/dsh-usage-prism#readme",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/shizukunatsuki/dsh-usage-prism.git"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/shizukunatsuki/dsh-usage-prism/issues"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"dsh",
|
|
18
|
+
"dsh-plugin",
|
|
19
|
+
"deepseek-harness",
|
|
20
|
+
"plugin",
|
|
21
|
+
"bundle",
|
|
22
|
+
"usage",
|
|
23
|
+
"tokens",
|
|
24
|
+
"sidebar"
|
|
25
|
+
],
|
|
26
|
+
"main": "index.js",
|
|
27
|
+
"icon": "./icon.svg",
|
|
28
|
+
"exports": {
|
|
29
|
+
".": "./index.js",
|
|
30
|
+
"./client": "./client.js",
|
|
31
|
+
"./locale/*.json": "./locale/*.json",
|
|
32
|
+
"./icon": "./icon.svg",
|
|
33
|
+
"./icon.svg": "./icon.svg",
|
|
34
|
+
"./package.json": "./package.json"
|
|
35
|
+
},
|
|
36
|
+
"files": [
|
|
37
|
+
"index.js",
|
|
38
|
+
"client.js",
|
|
39
|
+
"lib/*.js",
|
|
40
|
+
"locale/*.json",
|
|
41
|
+
"docs/DETAIL.md",
|
|
42
|
+
"docs/preview.png",
|
|
43
|
+
"icon.svg",
|
|
44
|
+
"cordis.patch.yml"
|
|
45
|
+
],
|
|
46
|
+
"engines": {
|
|
47
|
+
"dsh": "^0.2.3-alpha.3"
|
|
48
|
+
},
|
|
49
|
+
"scripts": {
|
|
50
|
+
"test": "node --test test/*.test.mjs",
|
|
51
|
+
"test:conformance": "node test/conformance.mjs",
|
|
52
|
+
"demo": "node demo/build.mjs"
|
|
53
|
+
},
|
|
54
|
+
"dsh": {
|
|
55
|
+
"bundle": {
|
|
56
|
+
"patch": "./cordis.patch.yml"
|
|
57
|
+
},
|
|
58
|
+
"client": {
|
|
59
|
+
"platform": "web",
|
|
60
|
+
"immediately": true
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|