@mzzsfy/dsh-usage-dash 0.6.0 → 0.7.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/README.md CHANGED
@@ -10,8 +10,8 @@
10
10
  - 汇总卡六张:Tokens 用量(服务商总口径)、会话数量、请求数量、最常用模型、平均缓存命中率、活跃天数(恒按天口径,不随视图切换);配置定价规则后 Tokens 卡头部行右侧显示估算费用
11
11
  - 活动热力图:GitHub 风格周列×星期行,26 周窗口,五档色阶,悬停明细;仅按天视图展示
12
12
  - 缓存命中率曲线:日粒度命中率 + 右侧副轴,并叠加平均生成速度曲线与首 token 延迟曲线(各自单独颜色,读数走悬停;仅含配对数据的槽参与,整图无数据不绘制),悬停显示当前时段命中率、平均生成速度、首 token 延迟与 token 明细(配置定价规则后附当前时段估算费用行,受「费用显示」开关;三粒度趋势图通用)
13
- - 模型 donut 与列表:按 token 前 5 模型占比环形图(中心为总量),列表含命中率与占比;每模型右侧两行:第一行占比%·token 数,第二行估算费用·TTFT·生成速度(语言中立,点分隔;费用需配置定价规则,TTFT/速度无配对数据不显示),悬停联动
14
- - 三粒度堆叠柱状趋势图(数据量大时裁最旧并提示);图例可点击切换显隐:点击单选(仅显示该项,左轴刻度按可见项归一)/再点恢复全部,Ctrl+点击多选,隐藏最后一项无效
13
+ - 模型 donut 与列表:按 token 前 5 模型占比环形图(中心为总量);每行名称两行展示(厂商/模型名,其余折叠为「其他」),右侧三排:第一排 token 数·占比,第二排输入输出占比构成(缓存/输入/输出 三段各占该模型 token 总量,缓存=读+写,三段合计 100%,无四桶数据不显示),第三排估算费用·TTFT·生成速度(费用需配置定价规则,TTFT/速度无配对数据不显示),悬停明细含输入/缓存读/缓存写/输出四桶 token 拆分并联动
14
+ - 三粒度堆叠柱状趋势图(数据量大时裁最旧并提示);图例可点击切换显隐:点击单选(仅显示该项,左轴刻度按可见项归一)/再点恢复全部,Ctrl+点击多选,隐藏最后一项无效;悬停明细按当前时段数据展示,零用量模型不显示,全零时段不显示悬浮框
15
15
  - 回扫状态行默认隐藏:首次启用自动回扫历史会话,运行中显示进度(常显);右上角为折叠箭头与刷新图标,折叠层内展开扫描异常日志块(逐条:时间/类型/明细,计数即明细条数,上限 200 条超限丢最旧,重建时清空)与「重建」入口(二次确认 3 秒后清库重扫),采集错误常显;工具栏分主区与右侧操作区,窄宽度时挡位组在主区内换行
16
16
  - 会话底栏接管:替换官方信息行为增强版,偏好卡三个开关默认全开——精确缓存命中率(两位小数)、会话 Token 明细(总/命中缓存/未命中缓存/输出)、费用显示(底栏费用项、趋势 tooltip 费用行与回合费用芯片);三开关全关时与官方逐字节一致
17
17
  - 回合费用芯片:经官方 `conversation.chat.assistant-actions` 槽注入动作行(复制与分支图标之间,官方赞/踩与上下文跳转同排),每轮对话结束后显示该轮估算费用(悬停 title 带 token 摘要与估算口径;显隐节奏随官方动作行——最新一轮常显,历史轮悬停显现;受「费用显示」开关;官方用量芯片弹窗已有 token 明细,芯片只承载费用)。旧宿主无该插槽时告警禁用
@@ -19,7 +19,7 @@
19
19
  - 双语:跟随宿主语言设置(设置 → 通用 → 语言)即时切换 en/zh
20
20
  - 数据 API 守卫:POST 同源校验 + JSON content-type(与 dsh-usage-panel 同构),局域网远程访问可用
21
21
 
22
- 口径:token 总量 = 未缓存输入 + 输出 + 缓存读 + 缓存写;命中率 = 缓存读 / (缓存读 + 未缓存输入 + 缓存写);桶按 host 本地时区。平均生成速度 = decode 配对分子 ÷ 解码时长,两者均取官方吞吐口径(与官方 session-stats 投影同构):解码时长为该步首 token 时刻(首个产出 token 的 attempt 流,回落 message 自带流)到 usage 汇报时刻,不含首 token 前的排队与提示处理等待;首 token 延迟(TTFT)= 首 token 时刻 − `step/start`(起点不随 `llm/retry-started` 重置,即含失败尝试时间)。首 token 时刻在 chunk token 样本上不可得,由后续 usage 报告(message 为主)补发零桶 timing 增量行承载(decodeTokens 与时长同源配对,不重复计 token);存量旧行(时长为旧全时长口径)速度分子回落输出 token,聚合随新数据自然收敛,重建(重扫)可全量按新口径重建。保留策略:天桶永久,小时桶固定 15 天,分钟桶默认 7 天且上限 7 天(设置项 `minuteRetentionDays`,0 = 禁用分钟桶)。
22
+ 口径:token 总量 = 未缓存输入 + 输出 + 缓存读 + 缓存写;命中率 = 缓存读 / (缓存读 + 未缓存输入 + 缓存写);桶按 host 本地时区。聚合响应 `models` 数组每模型带 `inputTokens`/`outputTokens`/`cacheReadTokens`/`cacheWriteTokens` 四桶拆分(与定价四桶同口径),模型列表据此展示输入输出对比。平均生成速度 = decode 配对分子 ÷ 解码时长,两者均取官方吞吐口径(与官方 session-stats 投影同构):解码时长为该步首 token 时刻(首个产出 token 的 attempt 流,回落 message 自带流)到 usage 汇报时刻,不含首 token 前的排队与提示处理等待;首 token 延迟(TTFT)= 首 token 时刻 − `step/start`(起点不随 `llm/retry-started` 重置,即含失败尝试时间)。首 token 时刻在 chunk token 样本上不可得,由后续 usage 报告(message 为主)补发零桶 timing 增量行承载(decodeTokens 与时长同源配对,不重复计 token);存量旧行(时长为旧全时长口径)速度分子回落输出 token,聚合随新数据自然收敛,重建(重扫)可全量按新口径重建。保留策略:天桶永久,小时桶固定 15 天,分钟桶默认 7 天且上限 7 天(设置项 `minuteRetentionDays`,0 = 禁用分钟桶)。
23
23
 
24
24
  写入模型:样本先同步合并进内存 pending,按 2 秒周期批量落盘(单布局存储域每次持久化写都全量重发布 unit 文档,合并把每样本 3 次写降为每脏行 1 次,回扫万级样本写放大降约 99%);查询前自动 flush 保证读己之写;flush 失败的行留 pending 下轮重试并经异常日志可观测,崩溃丢失窗口 = flush 周期,统计可由会话重扫重建。
25
25
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mzzsfy/dsh-usage-dash",
3
3
  "description": "用量统计面板:天/小时/分钟三粒度 token 与请求统计,26 周活跃热力图,会话底栏信息接管与官方动作行回合费用芯片,多条件分时段定价规则与费用估算;复刻自 github.com/HaoyueQin/dsh-usage-statistics-panel,感谢原作者 HaoyueQin",
4
- "version": "0.6.0",
4
+ "version": "0.7.0",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
7
7
  "exports": {
package/src/client.js CHANGED
@@ -204,6 +204,9 @@ const MESSAGES_ZH = {
204
204
  dailyTrend: '按天 Token 趋势',
205
205
  trendLimited: '仅显示最近 {n} 天',
206
206
  modelUsage: '模型用量',
207
+ modelCache: '缓存',
208
+ modelInput: '输入',
209
+ modelOutput: '输出',
207
210
  other: '其他',
208
211
  total: '总用量',
209
212
  percent: '占比',
@@ -344,6 +347,9 @@ const MESSAGES_EN = {
344
347
  dailyTrend: 'Daily token trend',
345
348
  trendLimited: 'Showing only the last {n} days',
346
349
  modelUsage: 'Model usage',
350
+ modelCache: 'Cache',
351
+ modelInput: 'Input',
352
+ modelOutput: 'Output',
347
353
  other: 'Other',
348
354
  total: 'Total',
349
355
  percent: 'Share',
@@ -561,10 +567,12 @@ const topWithOther = (ranked) => {
561
567
  const models = ranked.slice(0, GROUP_TOP_COUNT)
562
568
  if (ranked.length > GROUP_TOP_COUNT) {
563
569
  const rest = ranked.slice(GROUP_TOP_COUNT)
570
+ // 四桶与费用同源条件挂载:输入条目无该字段(旧形数据/未配价)时哨兵同样不带,防零值假数据
564
571
  models.push({
565
572
  model: OTHER_MODEL,
566
573
  tokens: rest.reduce((sum, item) => sum + item.tokens, 0),
567
- cost: rest.reduce((sum, item) => sum + (item.cost ?? 0), 0),
574
+ ...(rest.some(hasTokenBuckets) ? { ...bucketSumsOf(rest) } : {}),
575
+ ...(rest.some((item) => item.cost !== undefined) ? { cost: rest.reduce((sum, item) => sum + (item.cost ?? 0), 0) } : {}),
568
576
  items: rest,
569
577
  })
570
578
  }
@@ -820,7 +828,7 @@ function ttftTipText(ttft, t) {
820
828
  return ttft === undefined ? TOOLTIP_MISSING : formatDuration(ttft, t)
821
829
  }
822
830
 
823
- // 语言中立短时长:60 秒内一位小数秒,以上整秒折分秒(模型列表第二行用,禁本地化)
831
+ // 语言中立短时长:60 秒内一位小数秒,以上整秒折分秒(模型列表 meta 行用,禁本地化)
824
832
  function formatDurationShort(ms) {
825
833
  const seconds = ms / MS_PER_SECOND
826
834
  if (seconds < DURATION_MINUTE_SECONDS) return `${Math.round(seconds * NUMBER_ONE_DECIMAL) / NUMBER_ONE_DECIMAL}s`
@@ -834,6 +842,39 @@ function modelTtftText(ttft) {
834
842
  return `TTFT ${formatDurationShort(ttft)}`
835
843
  }
836
844
 
845
+ // 模型条目四桶齐备判定:生产端同源恒齐,旧形数据缺桶即视为不可渲染
846
+ const hasTokenBuckets = (item) => TOKEN_BUCKET_KEYS.every((key) => typeof item?.[key] === 'number')
847
+
848
+ // 逐桶求和:按共享桶键列表产出四桶聚合形态
849
+ const bucketSumsOf = (items) => Object.fromEntries(TOKEN_BUCKET_KEYS.map((key) => [
850
+ key,
851
+ items.reduce((sum, item) => sum + (item[key] ?? 0), 0),
852
+ ]))
853
+
854
+ // 模型输入输出占比构成:缓存/输入/输出 三段各占该模型 token 总量百分比(缓存=读+写,
855
+ // 三段合计恒 100%);四桶缺失(旧数据)或零总量为空串
856
+ function modelIoPercentText(item, t) {
857
+ if (!hasTokenBuckets(item) || !(item.tokens > 0)) return ''
858
+ const share = (value) => formatPercent((value / item.tokens) * PERCENT_SCALE)
859
+ return `${t('modelCache')} ${share(item.cacheReadTokens + item.cacheWriteTokens)} · ${t('modelInput')} ${share(item.inputTokens)} · ${t('modelOutput')} ${share(item.outputTokens)}`
860
+ }
861
+
862
+ // 趋势 tooltip 内容门:全零槽(无任何用量)不渲染 tooltip
863
+ const hasTipContent = (hoverSlot) => Boolean(hoverSlot) && hoverSlot.total > 0
864
+
865
+ // 趋势 tooltip 模型行数据:主行 = 可见且当前时段有用量的模型;OTHER 子行 = OTHER 可见
866
+ // 且当前时段有用量的其余模型明细,按 tokens 降序由本函数保证
867
+ const tipModelEntries = (hoverSlot, visibleSet) => {
868
+ if (!hasTipContent(hoverSlot)) return { main: [], other: [] }
869
+ const main = Object.entries(hoverSlot.byModel)
870
+ .filter(([model, tokens]) => visibleSet.has(model) && tokens > 0)
871
+ .map(([model, tokens]) => ({ model, tokens }))
872
+ const other = visibleSet.has(OTHER_MODEL)
873
+ ? Object.entries(hoverSlot.otherByModel ?? {}).filter(([, tokens]) => tokens > 0).sort((a, b) => b[1] - a[1])
874
+ : []
875
+ return { main, other }
876
+ }
877
+
837
878
  // 热力图:窗口固定 26 周,与所选范围无关
838
879
  const HEAT_WEEKS = 26
839
880
  const HEAT_ROW_COUNT = 7
@@ -1316,6 +1357,9 @@ const BUCKET_PRICE_KEYS = [
1316
1357
  { tokens: 'cacheWriteTokens', price: 'cacheWrite' },
1317
1358
  ]
1318
1359
 
1360
+ // 展示侧四桶键单一来源:模型分组求和与齐备判定共用,禁止各处手工复制键名
1361
+ const TOKEN_BUCKET_KEYS = BUCKET_PRICE_KEYS.map(({ tokens }) => tokens)
1362
+
1319
1363
  const toFiniteNumber = (value) => (Number.isFinite(value) ? value : 0)
1320
1364
 
1321
1365
  // 费用 = Σ(桶 token × 桶单价) / 每百万;缺桶或非法值按 0,原始浮点不圆整(展示层负责)
@@ -1383,15 +1427,20 @@ const TURN_COST_CHIP_ORDER = 20 + 10
1383
1427
  // messageId 反查:节点表为 chat 节点仓库(values() 可枚举,Map/仓库两态兼容);
1384
1428
  // 回合级用量优先取回合位置数据(location.turn.data.get('turn-tail')).tokenUsage(官方 tokenUsage 聚合,
1385
1429
  // 分页窗口缺 turn/start 时缺席),回退视图节点 data.closing.usage(末步用量采样,输入侧已含缓存,计费口径同源)
1386
- // 索引缓存:每份节点表快照只全量扫描一次建 messageId→turn-tail 索引(WeakMap 随快照释放),
1387
- // 长会话流式期间多芯片各自全量扫描是 O(消息数×节点数) 放大,索引后单快照 O(节点数)
1430
+ // 索引缓存:键取节点集数组身份 —— 官方 chat store 实例身份跨回合恒定,values() 返回的缓存数组
1431
+ // 随节点集更替换新引用:未变时全芯片共享一次扫描,新增回合节点经 values() 换引用自动失效重建
1432
+ // (按 store 实例为键会让新节点永远查不到旧索引,表现为回合费用芯片刷新后才出现);
1433
+ // Map/迭代器形态(测试兼容)仍按容器为键
1388
1434
  const TURN_USAGE_INDEX_CACHE = new WeakMap()
1389
1435
 
1390
1436
  function turnUsageIndexOf(nodes) {
1391
- let index = TURN_USAGE_INDEX_CACHE.get(nodes)
1437
+ const source = nodes && typeof nodes.values === 'function' ? nodes.values() : nodes
1438
+ const snapshotForm = Array.isArray(source)
1439
+ const cacheKey = snapshotForm ? source : nodes
1440
+ let index = TURN_USAGE_INDEX_CACHE.get(cacheKey)
1392
1441
  if (index !== undefined) return index
1393
1442
  index = new Map()
1394
- const list = nodes && typeof nodes.values === 'function' ? [...nodes.values()] : nodes
1443
+ const list = snapshotForm ? source : typeof source?.[Symbol.iterator] === 'function' ? [...source] : source
1395
1444
  if (Array.isArray(list)) {
1396
1445
  for (const node of list) {
1397
1446
  try {
@@ -1402,13 +1451,13 @@ function turnUsageIndexOf(nodes) {
1402
1451
  } catch { /* 单节点形状残缺跳过 */ }
1403
1452
  }
1404
1453
  }
1405
- TURN_USAGE_INDEX_CACHE.set(nodes, index)
1454
+ TURN_USAGE_INDEX_CACHE.set(cacheKey, index)
1406
1455
  return index
1407
1456
  }
1408
1457
 
1409
1458
  function turnTokenUsageOfMessage(nodes, messageId) {
1410
1459
  // 非对象(含 null/undefined)直接 null:WeakMap 键要求对象,展开校验收进索引构建,
1411
- // 每快照只物化一次节点序列
1460
+ // 每份节点集只物化一次节点序列
1412
1461
  if (nodes === null || typeof nodes !== 'object') return null
1413
1462
  const node = turnUsageIndexOf(nodes).get(messageId)
1414
1463
  if (node === undefined) return null
@@ -1934,11 +1983,11 @@ body[data-ds-dark-theme] .ud-panel{--ud-chart-1:color-mix(in srgb,#0576ff 65%,wh
1934
1983
  .ud-heat-l5{fill:var(--dsw-heat-5);background:var(--dsw-heat-5)}
1935
1984
  .ud-heat-legend{display:inline-flex;align-items:center;gap:${HEAT_GAP}px;margin-left:auto;flex:none;font-size:11px;color:var(--dsw-alias-label-secondary)}
1936
1985
  .ud-heat-legend i{display:inline-block;width:${HEAT_BASE}px;height:${HEAT_BASE}px;border-radius:${HEAT_RX}px;flex:none}
1937
- .ud-tip{position:fixed;z-index:${TIP_Z_INDEX};visibility:hidden;pointer-events:none;white-space:nowrap;border:1px solid var(--dsw-alias-border-l2);border-radius:8px;background:var(--dsw-alias-bg-overlay);box-shadow:0 4px 12px var(--dsw-alias-bg-mask-2);color:var(--dsw-alias-label-primary);font-size:13px;padding:8px 10px}
1938
- .ud-tip-title{font-weight:600}
1939
- .ud-tip-row{display:flex;align-items:center;gap:6px;font-size:12px;color:var(--dsw-alias-label-secondary)}
1940
- .ud-tip-row--sub{padding-left:12px;color:var(--dsw-alias-label-tertiary);font-size:11px}
1941
- .ud-tip-breakdown{display:flex;flex-direction:column;gap:2px;border-top:1px solid var(--dsw-alias-border-l1);margin-top:6px;padding-top:6px}
1986
+ .ud-tip{position:fixed;z-index:${TIP_Z_INDEX};visibility:hidden;pointer-events:none;white-space:nowrap;border:1px solid var(--dsw-alias-border-l2);border-radius:10px;background:var(--dsw-alias-bg-overlay);box-shadow:0 1px 2px var(--dsw-alias-bg-mask-2),0 6px 20px var(--dsw-alias-bg-mask-2);color:var(--dsw-alias-label-primary);font-size:13px;padding:10px 12px;font-variant-numeric:tabular-nums}
1987
+ .ud-tip-title{font-weight:600;margin-bottom:6px}
1988
+ .ud-tip-row{display:flex;align-items:center;gap:8px;font-size:12px;line-height:1.6;color:var(--dsw-alias-label-secondary)}
1989
+ .ud-tip-row--sub{padding-left:14px;color:var(--dsw-alias-label-tertiary);font-size:11px}
1990
+ .ud-tip-breakdown{display:flex;flex-direction:column;gap:3px;border-top:1px solid var(--dsw-alias-border-l1);margin-top:8px;padding-top:8px}
1942
1991
  .ud-axis-rate{fill:var(--dsw-alias-label-tertiary);font-size:11px;font-variant-numeric:tabular-nums}
1943
1992
  .ud-bar{transform-box:fill-box;transform-origin:center}
1944
1993
  .ud-bar-hit{fill:transparent;pointer-events:all}
@@ -1958,17 +2007,17 @@ body[data-ds-dark-theme] .ud-panel{--ud-chart-1:color-mix(in srgb,#0576ff 65%,wh
1958
2007
  .ud-donut-center{font-size:18px;font-weight:600;fill:var(--dsw-alias-label-primary)}
1959
2008
  .ud-donut-label{font-size:11px;fill:var(--dsw-alias-label-tertiary)}
1960
2009
  .ud-models{flex:1 1 260px;min-width:240px;display:flex;flex-direction:column}
1961
- .ud-model-row{display:flex;align-items:center;gap:8px;min-height:44px;padding:2px 4px;border-bottom:1px solid var(--dsw-alias-border-l1)}
2010
+ .ud-model-row{display:flex;align-items:center;gap:8px;min-height:48px;padding:2px 4px;border-bottom:1px solid var(--dsw-alias-border-l1)}
1962
2011
  .ud-model-row--expand{cursor:pointer}
1963
2012
  .ud-model-row--expand:hover{background:var(--dsw-alias-interactive-bg-hover)}
1964
2013
  .ud-model-swatch{width:10px;height:10px;border-radius:2px;flex:none}
1965
2014
  .ud-model-id{display:flex;flex-direction:column;gap:1px;min-width:0;flex:1}
1966
- .ud-model-name{font-size:13px;color:var(--dsw-alias-label-primary);white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
1967
- .ud-model-provider{font-size:11px;color:var(--dsw-alias-label-tertiary)}
2015
+ .ud-model-name{font-size:14px;font-weight:500;color:var(--dsw-alias-label-primary);white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
2016
+ .ud-model-provider{font-size:12px;color:var(--dsw-alias-label-tertiary);white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
1968
2017
  .ud-model-values{display:flex;flex-direction:column;align-items:flex-end;gap:1px;font-variant-numeric:tabular-nums;flex:none}
1969
2018
  .ud-model-tokens{font-size:12px;color:var(--dsw-alias-label-secondary)}
1970
- .ud-model-pct{font-size:11px;color:var(--dsw-alias-label-tertiary);white-space:nowrap}
1971
- .ud-model-cost{font-size:11px;color:var(--dsw-alias-label-tertiary);font-variant-numeric:tabular-nums}
2019
+ .ud-model-io{font-size:11px;color:var(--dsw-alias-label-secondary);white-space:nowrap}
2020
+ .ud-model-meta{font-size:11px;color:var(--dsw-alias-label-tertiary);white-space:nowrap}
1972
2021
  .ud-model-toggle{border:none;background:transparent;color:var(--dsw-alias-label-tertiary);cursor:pointer;padding:2px 6px;font-size:14px;line-height:1;transition:transform .2s ease}
1973
2022
  .ud-model-toggle[aria-expanded="true"]{transform:rotate(90deg)}
1974
2023
  .ud-model-toggle:focus-visible{outline:1px solid var(--dsw-alias-state-business-primary);border-radius:4px}
@@ -2178,9 +2227,7 @@ body[data-ds-dark-theme] .ud-panel{--ud-chart-1:color-mix(in srgb,#0576ff 65%,wh
2178
2227
  const hoverTtftPoint = hoverSlot ? ttftPoints.find((point) => point.day === hoverSlot.day) : null
2179
2228
  const pick = (index) => (event) => setHover({ index, anchor: pointerAt(event) })
2180
2229
  const clear = () => setHover(null)
2181
- const otherEntries = hoverSlot
2182
- ? Object.entries(hoverSlot.otherByModel ?? {}).sort((a, b) => b[1] - a[1])
2183
- : []
2230
+ const tipEntries = hasTipContent(hoverSlot) ? tipModelEntries(hoverSlot, visibleSet) : null
2184
2231
  return h('div', { className: 'ud-section' },
2185
2232
  h('div', { className: 'ud-section-head' },
2186
2233
  h('span', { className: 'ud-section-title' }, title),
@@ -2249,15 +2296,15 @@ body[data-ds-dark-theme] .ud-panel{--ud-chart-1:color-mix(in srgb,#0576ff 65%,wh
2249
2296
  onItem: (key, ctrl) => setVisibleKeys(legendToggle(visibleKeys, key, ctrl)),
2250
2297
  }),
2251
2298
  h(ChartTip, { anchor: hover ? hover.anchor : null, panelRef },
2252
- hoverSlot
2299
+ tipEntries
2253
2300
  ? [
2254
2301
  h('div', { key: 'title', className: 'ud-tip-title' }, slotLabelFor ? slotLabelFor(hoverSlot.day) : hoverSlot.day),
2255
2302
  h('div', { key: 'total', className: 'ud-tip-row' }, `${t('total')}: ${formatTokens(hoverSlot.total)}`),
2256
- ...legendModels.filter((item) => visibleSet.has(item.model)).map((item) => h('div', { key: `m-${item.model}`, className: 'ud-tip-row' },
2257
- h('i', { className: 'ud-legend-swatch', style: { background: colorFor(item.model) } }),
2258
- `${item.model === OTHER_MODEL ? t('other') : item.model}: ${formatTokens(hoverSlot.byModel[item.model] ?? 0)}`)),
2259
- ...(visibleSet.has(OTHER_MODEL) ? otherEntries.map(([model, tokens]) => h('div', { key: `om-${model}`, className: 'ud-tip-row ud-tip-row--sub' },
2260
- `${model}: ${formatTokens(tokens)}`)) : []),
2303
+ ...tipEntries.main.map((row) => h('div', { key: `m-${row.model}`, className: 'ud-tip-row' },
2304
+ h('i', { className: 'ud-legend-swatch', style: { background: colorFor(row.model) } }),
2305
+ `${row.model === OTHER_MODEL ? t('other') : row.model}: ${formatTokens(row.tokens)}`)),
2306
+ ...tipEntries.other.map(([model, tokens]) => h('div', { key: `om-${model}`, className: 'ud-tip-row ud-tip-row--sub' },
2307
+ `${model}: ${formatTokens(tokens)}`)),
2261
2308
  showRate ? h('div', { key: 'rate', className: 'ud-tip-row' }, `${t('cacheHitRate')}: ${cacheRateText(hoverSlot.cacheHit, hoverSlot.cacheMiss)}`) : null,
2262
2309
  showSpeed ? h('div', { key: 'speed', className: 'ud-tip-row' }, `${t('avgSpeed')}: ${speedTipText(hoverSlot.speed)}`) : null,
2263
2310
  showTtft ? h('div', { key: 'ttft', className: 'ud-tip-row' }, `${t('ttftLegend')}: ${ttftTipText(hoverSlot.ttft, t)}`) : null,
@@ -2301,15 +2348,17 @@ body[data-ds-dark-theme] .ud-panel{--ud-chart-1:color-mix(in srgb,#0576ff 65%,wh
2301
2348
  observer.disconnect()
2302
2349
  }
2303
2350
  }, [place])
2351
+ // 空内容(无子节点或空数组)不显示悬浮盒,防零尺寸守卫下出现纯背景空壳
2352
+ const show = Boolean(anchor && children != null && (!Array.isArray(children) || children.length > 0))
2304
2353
  const element = h('div', {
2305
2354
  className: 'ud-tip',
2306
2355
  ref: tipRef,
2307
2356
  style: {
2308
2357
  left: pos ? pos.left : 0,
2309
2358
  top: pos ? pos.top : 0,
2310
- visibility: pos && anchor ? 'visible' : 'hidden',
2359
+ visibility: show && pos ? 'visible' : 'hidden',
2311
2360
  },
2312
- }, anchor ? children : null)
2361
+ }, show ? children : null)
2313
2362
  return createPortal && typeof document !== 'undefined' && document.body
2314
2363
  ? createPortal(element, document.body)
2315
2364
  : element
@@ -2422,6 +2471,7 @@ body[data-ds-dark-theme] .ud-panel{--ud-chart-1:color-mix(in srgb,#0576ff 65%,wh
2422
2471
  const isOther = item.model === OTHER_MODEL
2423
2472
  const speedText = modelSpeedText(item.speed)
2424
2473
  const ttftText = modelTtftText(item.ttft)
2474
+ const ioText = modelIoPercentText(item, t)
2425
2475
  const metaParts = [
2426
2476
  item.cost !== undefined ? `≈ ${formatCost(item.cost, costCurrency)}` : null,
2427
2477
  ttftText,
@@ -2436,8 +2486,12 @@ body[data-ds-dark-theme] .ud-panel{--ud-chart-1:color-mix(in srgb,#0576ff 65%,wh
2436
2486
  },
2437
2487
  h('i', { className: 'ud-model-swatch', style: { background: colorFor(item.model) } }),
2438
2488
  h('div', { className: 'ud-model-id' },
2439
- h('span', { className: 'ud-model-name' }, displayName(item.model)),
2440
- isOther ? null : h('span', { className: 'ud-model-provider' }, providerOf(item.model))),
2489
+ isOther
2490
+ ? h('span', { className: 'ud-model-name' }, displayName(item.model))
2491
+ : [
2492
+ h('span', { className: 'ud-model-provider' }, providerOf(item.model)),
2493
+ h('span', { className: 'ud-model-name' }, modelNameOf(item.model)),
2494
+ ]),
2441
2495
  isOther
2442
2496
  ? h('button', {
2443
2497
  className: 'ud-model-toggle', 'aria-expanded': expandedOther, 'aria-label': t('other'),
@@ -2450,7 +2504,8 @@ body[data-ds-dark-theme] .ud-panel{--ud-chart-1:color-mix(in srgb,#0576ff 65%,wh
2450
2504
  h('div', { className: 'ud-model-values' },
2451
2505
  h('span', { className: 'ud-model-tokens' },
2452
2506
  `${formatTokens(item.tokens)} (${formatPercent((item.tokens / total) * PERCENT_SCALE)})`),
2453
- h('span', { className: 'ud-model-pct' }, metaParts.join(' · ')))),
2507
+ ioText ? h('span', { className: 'ud-model-io' }, ioText) : null,
2508
+ h('span', { className: 'ud-model-meta' }, metaParts.join(' · ')))),
2454
2509
  isOther
2455
2510
  ? h('div', { className: cx('ud-model-other', expandedOther && 'ud-model-other--open') },
2456
2511
  h('div', { className: 'ud-model-other-list' },
@@ -2469,6 +2524,14 @@ body[data-ds-dark-theme] .ud-panel{--ud-chart-1:color-mix(in srgb,#0576ff 65%,wh
2469
2524
  h('div', { key: 'title', className: 'ud-tip-title' }, displayName(tipSegment.model)),
2470
2525
  h('div', { key: 'tokens', className: 'ud-tip-row' }, `${t('total')}: ${formatTokens(tipSegment.tokens)}`),
2471
2526
  h('div', { key: 'pct', className: 'ud-tip-row' }, `${t('percent')}: ${formatPercent(tipSegment.percent)}`),
2527
+ hasTokenBuckets(tipSegment)
2528
+ ? [
2529
+ h('div', { key: 'input', className: 'ud-tip-row' }, `${t('priceInput')}: ${formatTokens(tipSegment.inputTokens)}`),
2530
+ h('div', { key: 'cacheRead', className: 'ud-tip-row' }, `${t('priceCacheRead')}: ${formatTokens(tipSegment.cacheReadTokens)}`),
2531
+ h('div', { key: 'cacheWrite', className: 'ud-tip-row' }, `${t('priceCacheWrite')}: ${formatTokens(tipSegment.cacheWriteTokens)}`),
2532
+ h('div', { key: 'output', className: 'ud-tip-row' }, `${t('priceOutput')}: ${formatTokens(tipSegment.outputTokens)}`),
2533
+ ]
2534
+ : null,
2472
2535
  tipSegment.model === OTHER_MODEL && (tipSegment.items ?? []).length > 0
2473
2536
  ? h('div', { key: 'breakdown', className: 'ud-tip-breakdown' },
2474
2537
  tipSegment.items.map((detail) => h('div', { key: detail.model, className: 'ud-tip-row ud-tip-row--sub' },
package/src/query.js CHANGED
@@ -1,249 +1,254 @@
1
- // 用量聚合纯函数:store 行形状仅作数据约定,零宿主依赖。
2
- // 桶串本地时区推导,同粒度字典序即时间序;daily 零值槽全枚举,超槽数保最新丢最旧。
3
-
4
- import { costOf, matchPrice } from './pricing.js'
5
-
6
- export const MAX_SLOTS = 2000
7
-
8
- const PAD_WIDTH = 2
9
- const PERCENT_SCALE = 100
10
- const MS_PER_SECOND = 1000
11
-
12
- const GRANULARITY_DAILY = 'D'
13
- const GRANULARITY_HOURLY = 'H'
14
- const GRANULARITY_MINUTE = 'M'
15
-
16
- const DAY_KEY_PATTERN = /^\d{4}-\d{2}-\d{2}$/
17
- const HOUR_KEY_PATTERN = /^\d{4}-\d{2}-\d{2}T\d{2}$/
18
- const MINUTE_KEY_PATTERN = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}$/
19
-
20
- // 桶串前缀宽:D 段定位日槽,M 行截取父 H 桶
21
- const DAY_KEY_WIDTH = 'YYYY-MM-DD'.length
22
- const HOUR_KEY_WIDTH = 'YYYY-MM-DDTHH'.length
23
-
24
- const pad = (value) => String(value).padStart(PAD_WIDTH, '0')
25
- const formatDate = (date) => `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`
26
- const formatHour = (date) => `${formatDate(date)}T${pad(date.getHours())}`
27
- const formatMinute = (date) => `${formatHour(date)}:${pad(date.getMinutes())}`
28
-
29
- const nextDay = (date) => {
30
- const next = new Date(date)
31
- next.setDate(next.getDate() + 1)
32
- return next
33
- }
34
- const nextHour = (date) => {
35
- const next = new Date(date)
36
- next.setHours(next.getHours() + 1)
37
- return next
38
- }
39
- const nextMinute = (date) => {
40
- const next = new Date(date)
41
- next.setMinutes(next.getMinutes() + MINUTE_STEP_MINUTES)
42
- return next
43
- }
44
-
45
- // 分钟桶粒度:枚举与桶键共用同一步长,from 必须对齐桶边界
46
- const MINUTE_STEP_MINUTES = 10
47
-
48
- // pattern 锚定桶串形态,suffix 补全为本地时区可解析日期串,format 回读校验分量合法性
49
- const BUCKET_FORMS = {
50
- [GRANULARITY_DAILY]: { pattern: DAY_KEY_PATTERN, suffix: 'T00:00:00', format: formatDate, step: nextDay },
51
- [GRANULARITY_HOURLY]: { pattern: HOUR_KEY_PATTERN, suffix: ':00:00', format: formatHour, step: nextHour },
52
- [GRANULARITY_MINUTE]: { pattern: MINUTE_KEY_PATTERN, suffix: ':00', format: formatMinute, step: nextMinute, align: MINUTE_STEP_MINUTES },
53
- }
54
-
55
- const parseBucketKey = (key, form) => {
56
- if (typeof key !== 'string' || !form.pattern.test(key)) return null
57
- const parsed = new Date(`${key}${form.suffix}`)
58
- return Number.isNaN(parsed.getTime()) || form.format(parsed) !== key ? null : parsed
59
- }
60
-
61
- const enumerateBucketKeys = (form) => (from, to) => {
62
- const start = parseBucketKey(from, form)
63
- const end = parseBucketKey(to, form)
64
- if (!start || !end) return []
65
- if (form.align && start.getMinutes() % form.align !== 0) return []
66
- const keys = []
67
- for (let cursor = start, key = form.format(cursor); key <= to; key = form.format(cursor)) {
68
- keys.push(key)
69
- cursor = form.step(cursor)
70
- }
71
- return keys
72
- }
73
-
74
- export const daysInRange = enumerateBucketKeys(BUCKET_FORMS[GRANULARITY_DAILY])
75
- export const hourKeysInRange = enumerateBucketKeys(BUCKET_FORMS[GRANULARITY_HOURLY])
76
- export const minuteKeysInRange = enumerateBucketKeys(BUCKET_FORMS[GRANULARITY_MINUTE])
77
-
78
- const emptySlot = (day) => ({
79
- day,
80
- total: 0,
81
- byModel: {},
82
- byProvider: {},
83
- requests: 0,
84
- turns: 0,
85
- cacheHit: 0,
86
- cacheMiss: 0,
87
- })
88
-
89
- const addRowToSlot = (slot, row, tokens) => {
90
- slot.total += tokens
91
- slot.requests += row.requests
92
- slot.turns += row.turns
93
- slot.cacheHit += row.cacheReadTokens
94
- slot.cacheMiss += row.inputTokens + row.cacheWriteTokens
95
- }
96
-
97
- const percentOf = (part, total) => (total === 0 ? 0 : (part / total) * PERCENT_SCALE)
98
-
99
- const rowTokens = (row) => row.inputTokens + row.outputTokens + row.cacheReadTokens + row.cacheWriteTokens
100
-
101
- // 速度配对分子:decode 口径取 decodeTokens;存量旧格式行(带时长无 decodeTokens)
102
- // 回落 outputTokens,聚合随新数据自然收敛
103
- const speedTokensOf = (row) => (row.durationMs ? row.decodeTokens ?? row.outputTokens : 0)
104
-
105
- export function aggregateRange(rows, g, from, to) {
106
- const form = BUCKET_FORMS[g]
107
- const slots = enumerateBucketKeys(form)(from, to).map((key) => emptySlot(key))
108
- const slotByKey = new Map(slots.map((slot) => [slot.day, slot]))
109
- const modelTotals = new Map()
110
- const providerTotals = new Map()
111
- const activeBuckets = new Set()
112
- // 槽级配对:桶串 速度对 {decodeTokens, durationMs} 与首字对 {ttftMs, ttftSteps},
113
- // 与模型级同口径(仅带配对数据的行计入)
114
- const slotSpeeds = new Map()
115
- const slotTtfts = new Map()
116
- for (const row of rows) {
117
- const slot = slotByKey.get(row.bucket)
118
- // 桶串未落在枚举序列(如改粒度前的历史残行)不可归属,跳过防崩
119
- if (!slot) continue
120
- const tokens = rowTokens(row)
121
- addRowToSlot(slot, row, tokens)
122
- // timing 行( token + decode 配对)不参与归因,但仍进配对聚合
123
- if (tokens > 0) {
124
- activeBuckets.add(row.bucket)
125
- slot.byModel[row.model] = (slot.byModel[row.model] ?? 0) + tokens
126
- slot.byProvider[row.provider] = (slot.byProvider[row.provider] ?? 0) + tokens
127
- }
128
- if (row.durationMs) {
129
- const pair = slotSpeeds.get(row.bucket) ?? { decodeTokens: 0, durationMs: 0 }
130
- pair.decodeTokens += speedTokensOf(row)
131
- pair.durationMs += row.durationMs
132
- slotSpeeds.set(row.bucket, pair)
133
- }
134
- if (row.ttftSteps > 0) {
135
- const pair = slotTtfts.get(row.bucket) ?? { ttftMs: 0, ttftSteps: 0 }
136
- pair.ttftMs += row.ttftMs ?? 0
137
- pair.ttftSteps += row.ttftSteps
138
- slotTtfts.set(row.bucket, pair)
139
- }
140
- const modelTotal = modelTotals.get(row.model)
141
- if (modelTotal) {
142
- modelTotal.tokens += tokens
143
- modelTotal.speedDurationMs += row.durationMs ?? 0
144
- modelTotal.speedOutputTokens += speedTokensOf(row)
145
- modelTotal.ttftMs += row.ttftMs ?? 0
146
- modelTotal.ttftSteps += row.ttftSteps ?? 0
147
- } else {
148
- modelTotals.set(row.model, {
149
- provider: row.provider,
150
- tokens,
151
- speedDurationMs: row.durationMs ?? 0,
152
- speedOutputTokens: speedTokensOf(row),
153
- ttftMs: row.ttftMs ?? 0,
154
- ttftSteps: row.ttftSteps ?? 0,
155
- })
156
- }
157
- providerTotals.set(row.provider, (providerTotals.get(row.provider) ?? 0) + tokens)
158
- }
159
- // 槽级 speed/ttft 条件挂:无配对数据的槽不挂字段(存量槽形契约不变)
160
- for (const slot of slots) {
161
- const speedPair = slotSpeeds.get(slot.day)
162
- if (speedPair && speedPair.durationMs > 0) slot.speed = speedPair.decodeTokens / (speedPair.durationMs / MS_PER_SECOND)
163
- const ttftPair = slotTtfts.get(slot.day)
164
- if (ttftPair && ttftPair.ttftSteps > 0) slot.ttft = ttftPair.ttftMs / ttftPair.ttftSteps
165
- }
166
- const totals = { tokens: 0, requests: 0, turns: 0, cacheHit: 0, cacheMiss: 0 }
167
- for (const slot of slots) {
168
- totals.tokens += slot.total
169
- totals.requests += slot.requests
170
- totals.turns += slot.turns
171
- totals.cacheHit += slot.cacheHit
172
- totals.cacheMiss += slot.cacheMiss
173
- }
174
- // speed = decode 配对口径(decodeTokens ÷ 时长秒);ttft = 首 token 延迟
175
- // 加权平均(毫秒);仅配对数据存在的条目挂字段,无数据条目不挂;
176
- // 纯 timing 行可能产生 0-token 条目,列表保持只含 token 行(存量契约)
177
- const models = [...modelTotals.entries()]
178
- .filter(([, agg]) => agg.tokens > 0)
179
- .map(([model, agg]) => ({
180
- model,
181
- provider: agg.provider,
182
- tokens: agg.tokens,
183
- percent: percentOf(agg.tokens, totals.tokens),
184
- ...(agg.speedDurationMs > 0 ? { speed: agg.speedOutputTokens / (agg.speedDurationMs / MS_PER_SECOND) } : {}),
185
- ...(agg.ttftSteps > 0 ? { ttft: agg.ttftMs / agg.ttftSteps } : {}),
186
- }))
187
- .sort((a, b) => b.tokens - a.tokens)
188
- const providers = [...providerTotals.entries()]
189
- .filter(([, tokens]) => tokens > 0)
190
- .map(([provider, tokens]) => ({ provider, tokens, percent: percentOf(tokens, totals.tokens) }))
191
- .sort((a, b) => b.tokens - a.tokens)
192
- const truncated = slots.length > MAX_SLOTS
193
- const daily = truncated ? slots.slice(-MAX_SLOTS) : slots
194
- const top = models[0]
195
- const result = {
196
- from,
197
- to,
198
- tokens: totals.tokens,
199
- requests: totals.requests,
200
- turns: totals.turns,
201
- cacheHit: totals.cacheHit,
202
- cacheMiss: totals.cacheMiss,
203
- activeDays: activeBuckets.size,
204
- topModel: top?.model ?? '',
205
- topProvider: top?.provider ?? '',
206
- daily,
207
- models,
208
- providers,
209
- }
210
- if (truncated) result.truncated = true
211
- return result
212
- }
213
-
214
- // 聚合计价的槽定位:D 折叠到日槽,H/M 即本槽;计价一律取行所属 H 桶起点
215
- const COST_SLOT_KEYS = {
216
- [GRANULARITY_DAILY]: (bucket) => bucket.slice(0, DAY_KEY_WIDTH),
217
- [GRANULARITY_HOURLY]: (bucket) => bucket,
218
- [GRANULARITY_MINUTE]: (bucket) => bucket,
219
- }
220
-
221
- // 聚合计价:以可见槽为唯一口径,cost 行按 H 桶起点匹配价格后累加;
222
- // unpriced = 有 token 而未命中价的去重 H 桶数,被截断丢弃的行整体不参与。
223
- // 纯函数返回新 result,不修改入参;调用方不调用则响应无 cost/unpriced 字段
224
- export function attachCosts(result, costRows, granularity, rules) {
225
- const slotKeyOf = COST_SLOT_KEYS[granularity]
226
- const slotOfDay = new Map(result.daily.map((slot) => [slot.day, slot]))
227
- const slotCosts = new Map()
228
- const modelCosts = new Map(result.models.map((entry) => [entry.model, 0]))
229
- const unpricedHours = new Set()
230
- for (const row of costRows) {
231
- const slot = slotOfDay.get(slotKeyOf(row.bucket))
232
- if (!slot || rowTokens(row) === 0) continue
233
- const hourKey = row.bucket.slice(0, HOUR_KEY_WIDTH)
234
- const date = parseBucketKey(hourKey, BUCKET_FORMS[GRANULARITY_HOURLY])
235
- if (!date) continue
236
- const price = matchPrice(rules, row.model, date)
237
- if (!price) {
238
- unpricedHours.add(hourKey)
239
- continue
240
- }
241
- const cost = costOf(price, row)
242
- slotCosts.set(slot.day, (slotCosts.get(slot.day) ?? 0) + cost)
243
- if (modelCosts.has(row.model)) modelCosts.set(row.model, modelCosts.get(row.model) + cost)
244
- }
245
- const daily = result.daily.map((slot) => ({ ...slot, cost: slotCosts.get(slot.day) ?? 0 }))
246
- const cost = daily.reduce((sum, slot) => sum + slot.cost, 0)
247
- const models = result.models.map((entry) => ({ ...entry, cost: modelCosts.get(entry.model) }))
248
- return { ...result, daily, models, cost, unpriced: unpricedHours.size }
249
- }
1
+ import { costOf, matchPrice } from './pricing.js'
2
+
3
+ export const MAX_SLOTS = 2000
4
+
5
+ const PAD_WIDTH = 2
6
+ const PERCENT_SCALE = 100
7
+ const MS_PER_SECOND = 1000
8
+
9
+ const GRANULARITY_DAILY = 'D'
10
+ const GRANULARITY_HOURLY = 'H'
11
+ const GRANULARITY_MINUTE = 'M'
12
+
13
+ const DAY_KEY_PATTERN = /^\d{4}-\d{2}-\d{2}$/
14
+ const HOUR_KEY_PATTERN = /^\d{4}-\d{2}-\d{2}T\d{2}$/
15
+ const MINUTE_KEY_PATTERN = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}$/
16
+
17
+ // 桶串前缀宽:D 段定位日桶,M 行截取父 H 桶
18
+ const DAY_KEY_WIDTH = 'YYYY-MM-DD'.length
19
+ const HOUR_KEY_WIDTH = 'YYYY-MM-DDTHH'.length
20
+
21
+ const pad = (value) => String(value).padStart(PAD_WIDTH, '0')
22
+ const formatDate = (date) => `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`
23
+ const formatHour = (date) => `${formatDate(date)}T${pad(date.getHours())}`
24
+ const formatMinute = (date) => `${formatHour(date)}:${pad(date.getMinutes())}`
25
+
26
+ const nextDay = (date) => {
27
+ const next = new Date(date)
28
+ next.setDate(next.getDate() + 1)
29
+ return next
30
+ }
31
+ const nextHour = (date) => {
32
+ const next = new Date(date)
33
+ next.setHours(next.getHours() + 1)
34
+ return next
35
+ }
36
+ const nextMinute = (date) => {
37
+ const next = new Date(date)
38
+ next.setMinutes(next.getMinutes() + MINUTE_STEP_MINUTES)
39
+ return next
40
+ }
41
+
42
+ // 分钟桶粒度:枚举与桶键共用同一步长,from 必须对齐桶边界
43
+ const MINUTE_STEP_MINUTES = 10
44
+
45
+ // pattern 锚定桶串外形,suffix 补全为本地时区可解析日期串,format 回读校验分量合法性
46
+ const BUCKET_FORMS = {
47
+ [GRANULARITY_DAILY]: { pattern: DAY_KEY_PATTERN, suffix: 'T00:00:00', format: formatDate, step: nextDay },
48
+ [GRANULARITY_HOURLY]: { pattern: HOUR_KEY_PATTERN, suffix: ':00:00', format: formatHour, step: nextHour },
49
+ [GRANULARITY_MINUTE]: { pattern: MINUTE_KEY_PATTERN, suffix: ':00', format: formatMinute, step: nextMinute, align: MINUTE_STEP_MINUTES },
50
+ }
51
+
52
+ const parseBucketKey = (key, form) => {
53
+ if (typeof key !== 'string' || !form.pattern.test(key)) return null
54
+ const parsed = new Date(`${key}${form.suffix}`)
55
+ return Number.isNaN(parsed.getTime()) || form.format(parsed) !== key ? null : parsed
56
+ }
57
+
58
+ const enumerateBucketKeys = (form) => (from, to) => {
59
+ const start = parseBucketKey(from, form)
60
+ const end = parseBucketKey(to, form)
61
+ if (!start || !end) return []
62
+ if (form.align && start.getMinutes() % form.align !== 0) return []
63
+ const keys = []
64
+ for (let cursor = start, key = form.format(cursor); key <= to; key = form.format(cursor)) {
65
+ keys.push(key)
66
+ cursor = form.step(cursor)
67
+ }
68
+ return keys
69
+ }
70
+
71
+ export const daysInRange = enumerateBucketKeys(BUCKET_FORMS[GRANULARITY_DAILY])
72
+ export const hourKeysInRange = enumerateBucketKeys(BUCKET_FORMS[GRANULARITY_HOURLY])
73
+ export const minuteKeysInRange = enumerateBucketKeys(BUCKET_FORMS[GRANULARITY_MINUTE])
74
+
75
+ const emptySlot = (day) => ({
76
+ day,
77
+ total: 0,
78
+ byModel: {},
79
+ byProvider: {},
80
+ requests: 0,
81
+ turns: 0,
82
+ cacheHit: 0,
83
+ cacheMiss: 0,
84
+ })
85
+
86
+ const addRowToSlot = (slot, row, tokens) => {
87
+ slot.total += tokens
88
+ slot.requests += row.requests
89
+ slot.turns += row.turns
90
+ slot.cacheHit += row.cacheReadTokens
91
+ slot.cacheMiss += row.inputTokens + row.cacheWriteTokens
92
+ }
93
+
94
+ const percentOf = (part, total) => (total === 0 ? 0 : (part / total) * PERCENT_SCALE)
95
+
96
+ const rowTokens = (row) => row.inputTokens + row.outputTokens + row.cacheReadTokens + row.cacheWriteTokens
97
+
98
+ // 速度配对分子:decode 口径取 decodeTokens;存量旧格式行(带时长无 decodeTokens)
99
+ // 回落 outputTokens,聚合随新数据自然收敛
100
+ const speedTokensOf = (row) => (row.durationMs ? row.decodeTokens ?? row.outputTokens : 0)
101
+
102
+ export function aggregateRange(rows, g, from, to) {
103
+ const form = BUCKET_FORMS[g]
104
+ const slots = enumerateBucketKeys(form)(from, to).map((key) => emptySlot(key))
105
+ const slotByKey = new Map(slots.map((slot) => [slot.day, slot]))
106
+ const modelTotals = new Map()
107
+ const providerTotals = new Map()
108
+ const activeBuckets = new Set()
109
+ // 槽级配对:桶串 速度对 {decodeTokens, durationMs} 与首字对 {ttftMs, ttftSteps},
110
+ const slotSpeeds = new Map()
111
+ const slotTtfts = new Map()
112
+ for (const row of rows) {
113
+ const slot = slotByKey.get(row.bucket)
114
+ // 桶串未落在枚举序列(如改粒度前的历史残行)不可归属,跳过防崩
115
+ if (!slot) continue
116
+ const tokens = rowTokens(row)
117
+ addRowToSlot(slot, row, tokens)
118
+ // 纯 timing 行(零 token 桶 + decode 配对)不参与归属,但仍进配对聚合
119
+ if (tokens > 0) {
120
+ activeBuckets.add(row.bucket)
121
+ slot.byModel[row.model] = (slot.byModel[row.model] ?? 0) + tokens
122
+ slot.byProvider[row.provider] = (slot.byProvider[row.provider] ?? 0) + tokens
123
+ }
124
+ if (row.durationMs) {
125
+ const pair = slotSpeeds.get(row.bucket) ?? { decodeTokens: 0, durationMs: 0 }
126
+ pair.decodeTokens += speedTokensOf(row)
127
+ pair.durationMs += row.durationMs
128
+ slotSpeeds.set(row.bucket, pair)
129
+ }
130
+ if (row.ttftSteps > 0) {
131
+ const pair = slotTtfts.get(row.bucket) ?? { ttftMs: 0, ttftSteps: 0 }
132
+ pair.ttftMs += row.ttftMs ?? 0
133
+ pair.ttftSteps += row.ttftSteps
134
+ slotTtfts.set(row.bucket, pair)
135
+ }
136
+ const modelTotal = modelTotals.get(row.model)
137
+ if (modelTotal) {
138
+ modelTotal.tokens += tokens
139
+ modelTotal.inputTokens += row.inputTokens
140
+ modelTotal.outputTokens += row.outputTokens
141
+ modelTotal.cacheReadTokens += row.cacheReadTokens
142
+ modelTotal.cacheWriteTokens += row.cacheWriteTokens
143
+ modelTotal.speedDurationMs += row.durationMs ?? 0
144
+ modelTotal.speedOutputTokens += speedTokensOf(row)
145
+ modelTotal.ttftMs += row.ttftMs ?? 0
146
+ modelTotal.ttftSteps += row.ttftSteps ?? 0
147
+ } else {
148
+ modelTotals.set(row.model, {
149
+ provider: row.provider,
150
+ tokens,
151
+ inputTokens: row.inputTokens,
152
+ outputTokens: row.outputTokens,
153
+ cacheReadTokens: row.cacheReadTokens,
154
+ cacheWriteTokens: row.cacheWriteTokens,
155
+ speedDurationMs: row.durationMs ?? 0,
156
+ speedOutputTokens: speedTokensOf(row),
157
+ ttftMs: row.ttftMs ?? 0,
158
+ ttftSteps: row.ttftSteps ?? 0,
159
+ })
160
+ }
161
+ providerTotals.set(row.provider, (providerTotals.get(row.provider) ?? 0) + tokens)
162
+ }
163
+ for (const slot of slots) {
164
+ const speedPair = slotSpeeds.get(slot.day)
165
+ if (speedPair && speedPair.durationMs > 0) slot.speed = speedPair.decodeTokens / (speedPair.durationMs / MS_PER_SECOND)
166
+ const ttftPair = slotTtfts.get(slot.day)
167
+ if (ttftPair && ttftPair.ttftSteps > 0) slot.ttft = ttftPair.ttftMs / ttftPair.ttftSteps
168
+ }
169
+ const totals = { tokens: 0, requests: 0, turns: 0, cacheHit: 0, cacheMiss: 0 }
170
+ for (const slot of slots) {
171
+ totals.tokens += slot.total
172
+ totals.requests += slot.requests
173
+ totals.turns += slot.turns
174
+ totals.cacheHit += slot.cacheHit
175
+ totals.cacheMiss += slot.cacheMiss
176
+ }
177
+ // speed = decode 配对口径(decodeTokens ÷ 时长秒);ttft = 首 token 延迟
178
+ // 加权平均(毫秒);仅配对数据存在的条目挂字段,无数据条目不挂
179
+ // timing 行可能产生 0-token 条目,列表保持只含 token 行(存量契约)
180
+ const models = [...modelTotals.entries()]
181
+ .filter(([, agg]) => agg.tokens > 0)
182
+ .map(([model, agg]) => ({
183
+ model,
184
+ provider: agg.provider,
185
+ tokens: agg.tokens,
186
+ inputTokens: agg.inputTokens,
187
+ outputTokens: agg.outputTokens,
188
+ cacheReadTokens: agg.cacheReadTokens,
189
+ cacheWriteTokens: agg.cacheWriteTokens,
190
+ percent: percentOf(agg.tokens, totals.tokens),
191
+ ...(agg.speedDurationMs > 0 ? { speed: agg.speedOutputTokens / (agg.speedDurationMs / MS_PER_SECOND) } : {}),
192
+ ...(agg.ttftSteps > 0 ? { ttft: agg.ttftMs / agg.ttftSteps } : {}),
193
+ }))
194
+ .sort((a, b) => b.tokens - a.tokens)
195
+ const providers = [...providerTotals.entries()]
196
+ .filter(([, tokens]) => tokens > 0)
197
+ .map(([provider, tokens]) => ({ provider, tokens, percent: percentOf(tokens, totals.tokens) }))
198
+ .sort((a, b) => b.tokens - a.tokens)
199
+ const truncated = slots.length > MAX_SLOTS
200
+ const daily = truncated ? slots.slice(-MAX_SLOTS) : slots
201
+ const top = models[0]
202
+ const result = {
203
+ from,
204
+ to,
205
+ tokens: totals.tokens,
206
+ requests: totals.requests,
207
+ turns: totals.turns,
208
+ cacheHit: totals.cacheHit,
209
+ cacheMiss: totals.cacheMiss,
210
+ activeDays: activeBuckets.size,
211
+ topModel: top?.model ?? '',
212
+ topProvider: top?.provider ?? '',
213
+ daily,
214
+ models,
215
+ providers,
216
+ }
217
+ if (truncated) result.truncated = true
218
+ return result
219
+ }
220
+
221
+ // 聚合计价的槽定位:D 折叠到日槽,H/M 即本槽,计价一律取行所属 H 槽起点
222
+ const COST_SLOT_KEYS = {
223
+ [GRANULARITY_DAILY]: (bucket) => bucket.slice(0, DAY_KEY_WIDTH),
224
+ [GRANULARITY_HOURLY]: (bucket) => bucket,
225
+ [GRANULARITY_MINUTE]: (bucket) => bucket,
226
+ }
227
+
228
+ // 聚合计价:以可见槽为唯一口径,cost 行按 H 槽起点匹配价格后累加;
229
+ export function attachCosts(result, costRows, granularity, rules) {
230
+ const slotKeyOf = COST_SLOT_KEYS[granularity]
231
+ const slotOfDay = new Map(result.daily.map((slot) => [slot.day, slot]))
232
+ const slotCosts = new Map()
233
+ const modelCosts = new Map(result.models.map((entry) => [entry.model, 0]))
234
+ const unpricedHours = new Set()
235
+ for (const row of costRows) {
236
+ const slot = slotOfDay.get(slotKeyOf(row.bucket))
237
+ if (!slot || rowTokens(row) === 0) continue
238
+ const hourKey = row.bucket.slice(0, HOUR_KEY_WIDTH)
239
+ const date = parseBucketKey(hourKey, BUCKET_FORMS[GRANULARITY_HOURLY])
240
+ if (!date) continue
241
+ const price = matchPrice(rules, row.model, date)
242
+ if (!price) {
243
+ unpricedHours.add(hourKey)
244
+ continue
245
+ }
246
+ const cost = costOf(price, row)
247
+ slotCosts.set(slot.day, (slotCosts.get(slot.day) ?? 0) + cost)
248
+ if (modelCosts.has(row.model)) modelCosts.set(row.model, modelCosts.get(row.model) + cost)
249
+ }
250
+ const daily = result.daily.map((slot) => ({ ...slot, cost: slotCosts.get(slot.day) ?? 0 }))
251
+ const cost = daily.reduce((sum, slot) => sum + slot.cost, 0)
252
+ const models = result.models.map((entry) => ({ ...entry, cost: modelCosts.get(entry.model) }))
253
+ return { ...result, daily, models, cost, unpriced: unpricedHours.size }
254
+ }
@@ -67,6 +67,9 @@ const {
67
67
  matchPrice,
68
68
  maxSlotsFor,
69
69
  minuteTickLabel,
70
+ modelIoPercentText,
71
+ hasTipContent,
72
+ tipModelEntries,
70
73
  modelSegmentLabel,
71
74
  modelSpeedText,
72
75
  modelNameOf,
@@ -810,6 +813,56 @@ test('donutSegments 全零 total 回落下限防除零', () => {
810
813
  assert.equal(segments[0].percent, 0)
811
814
  })
812
815
 
816
+ test('groupStats 哨兵折叠四桶求和,top 模型四桶透传', () => {
817
+ // Given 超 top 上限模型条目带四桶 When 分组 Then 哨兵四桶为 rest 求和,top 条目原字段保留
818
+ const stats = {
819
+ models: [
820
+ { model: 'p/m1', tokens: 500, inputTokens: 300, outputTokens: 200, cacheReadTokens: 0, cacheWriteTokens: 0 },
821
+ { model: 'p/m2', tokens: 400, inputTokens: 100, outputTokens: 300, cacheReadTokens: 0, cacheWriteTokens: 0 },
822
+ { model: 'p/m3', tokens: 300, inputTokens: 0, outputTokens: 100, cacheReadTokens: 150, cacheWriteTokens: 50 },
823
+ { model: 'p/m4', tokens: 200, inputTokens: 80, outputTokens: 120, cacheReadTokens: 0, cacheWriteTokens: 0 },
824
+ { model: 'p/m5', tokens: 100, inputTokens: 60, outputTokens: 40, cacheReadTokens: 0, cacheWriteTokens: 0 },
825
+ { model: 'p/m6', tokens: 50, inputTokens: 10, outputTokens: 30, cacheReadTokens: 5, cacheWriteTokens: 5 },
826
+ ],
827
+ daily: [],
828
+ }
829
+ const grouped = groupStats(stats)
830
+ assert.equal(grouped.models[0].inputTokens, 300)
831
+ assert.equal(grouped.models[0].outputTokens, 200)
832
+ assert.deepEqual(grouped.models[5], {
833
+ model: OTHER_MODEL,
834
+ tokens: 50,
835
+ inputTokens: 10,
836
+ outputTokens: 30,
837
+ cacheReadTokens: 5,
838
+ cacheWriteTokens: 5,
839
+ items: [stats.models[5]],
840
+ })
841
+ })
842
+
843
+ test('groupStats 哨兵费用同源:无 cost 输入不挂 cost,有 cost 求和', () => {
844
+ // Given rest 条目无 cost When 折叠 Then 哨兵无 cost 字段;Given rest 带 cost When 折叠 Then 哨兵求和
845
+ const bare = groupStats({ models: [
846
+ { model: 'p/m1', tokens: 500 },
847
+ { model: 'p/m2', tokens: 400 },
848
+ { model: 'p/m3', tokens: 300 },
849
+ { model: 'p/m4', tokens: 200 },
850
+ { model: 'p/m5', tokens: 100 },
851
+ { model: 'p/m6', tokens: 50 },
852
+ ], daily: [] })
853
+ assert.equal('cost' in bare.models[5], false)
854
+ const priced = groupStats({ models: [
855
+ { model: 'p/m1', tokens: 500, cost: 1 },
856
+ { model: 'p/m2', tokens: 400, cost: 2 },
857
+ { model: 'p/m3', tokens: 300, cost: undefined },
858
+ { model: 'p/m4', tokens: 200, cost: 4 },
859
+ { model: 'p/m5', tokens: 100, cost: 8 },
860
+ { model: 'p/m6', tokens: 50, cost: 16 },
861
+ { model: 'p/m7', tokens: 25, cost: 32 },
862
+ ], daily: [] })
863
+ assert.equal(priced.models[5].cost, 16 + 32)
864
+ })
865
+
813
866
  test('groupStats 哨兵保留其他模型明细供展开与提示', () => {
814
867
  const stats = {
815
868
  models: [
@@ -824,6 +877,8 @@ test('groupStats 哨兵保留其他模型明细供展开与提示', () => {
824
877
  }
825
878
  const grouped = groupStats(stats)
826
879
  assert.deepEqual(grouped.models[5].items, [{ model: 'p/m6', tokens: 50 }])
880
+ // Given 旧形条目无四桶 When 折叠 Then 哨兵不合成零值四桶,与 top 条目降级形态一致
881
+ assert.equal('inputTokens' in grouped.models[5], false)
827
882
  })
828
883
 
829
884
  test('groupStats 逐日保留其他明细映射供 tooltip', () => {
@@ -856,6 +911,45 @@ test('模型速度文本:官方吞吐口径格式化,无速度为空串', () =>
856
911
  assert.equal(modelSpeedText(undefined), '')
857
912
  })
858
913
 
914
+ test('模型输入输出占比构成:缓存/输入/输出 三段各占该模型 token 总量', () => {
915
+ // Given 四桶齐备的模型条目 When 格式化 Then 三段占比相对 tokens 总量,缓存=读+写,合计 100%,顺序缓存/输入/输出
916
+ const zhT = createTranslator(MESSAGES_ZH)
917
+ const enT = createTranslator(MESSAGES_EN)
918
+ const item = { tokens: 1000, inputTokens: 31, cacheReadTokens: 954, cacheWriteTokens: 10, outputTokens: 5 }
919
+ assert.equal(modelIoPercentText(item, zhT), '缓存 96.4% · 输入 3.1% · 输出 0.5%')
920
+ assert.equal(modelIoPercentText(item, enT), 'Cache 96.4% · Input 3.1% · Output 0.5%')
921
+ })
922
+
923
+ test('趋势 tooltip 模型行:仅含可见且当前时段有用量的模型', () => {
924
+ // Given 槽内 byModel 含零值与非零值,OTHER 明细含零值与无序多元素 When 取行数据 Then 主行与子行均过滤零用量,子行按 tokens 降序,OTHER 不可见时子行为空
925
+ const hoverSlot = {
926
+ total: 300,
927
+ byModel: { 'p/a': 200, 'p/b': 0, [OTHER_MODEL]: 100 },
928
+ otherByModel: { 'p/y': 0, 'p/x': 20, 'p/z': 50 },
929
+ }
930
+ const visible = new Set(['p/a', 'p/b', OTHER_MODEL])
931
+ const rows = tipModelEntries(hoverSlot, visible)
932
+ assert.deepEqual(rows.main, [{ model: 'p/a', tokens: 200 }, { model: OTHER_MODEL, tokens: 100 }])
933
+ assert.deepEqual(rows.other, [['p/z', 50], ['p/x', 20]])
934
+ const hiddenOther = tipModelEntries(hoverSlot, new Set(['p/a']))
935
+ assert.deepEqual(hiddenOther.main, [{ model: 'p/a', tokens: 200 }])
936
+ assert.deepEqual(hiddenOther.other, [])
937
+ })
938
+
939
+ test('趋势 tooltip 内容门:仅 total>0 的槽渲染', () => {
940
+ // Given 全零槽/常规槽 When 判定 Then 全零槽无内容,常规槽有内容,null 槽无内容
941
+ assert.equal(hasTipContent(null), false)
942
+ assert.equal(hasTipContent({ total: 0 }), false)
943
+ assert.equal(hasTipContent({ total: 5 }), true)
944
+ })
945
+
946
+ test('模型输入输出占比构成:缺四桶或零总量为空串', () => {
947
+ const zhT = createTranslator(MESSAGES_ZH)
948
+ assert.equal(modelIoPercentText({ tokens: 100 }, zhT), '')
949
+ assert.equal(modelIoPercentText(undefined, zhT), '')
950
+ assert.equal(modelIoPercentText({ tokens: 0, inputTokens: 0, cacheReadTokens: 0, cacheWriteTokens: 0, outputTokens: 0 }, zhT), '')
951
+ })
952
+
859
953
  test('模型首字文本:语言中立短时长,无 ttft 为空串', () => {
860
954
  assert.equal(modelTtftText(200), 'TTFT 0.2s')
861
955
  assert.equal(modelTtftText(162000), 'TTFT 2m42s')
@@ -117,8 +117,26 @@ test('D 粒度聚合总量排序与占比正确', () => {
117
117
  assert.equal(out.topModel, 'deepseek/deepseek-chat')
118
118
  assert.equal(out.topProvider, 'deepseek')
119
119
  assert.deepEqual(out.models, [
120
- { model: 'deepseek/deepseek-chat', provider: 'deepseek', tokens: 350, percent: (350 / 410) * 100 },
121
- { model: 'deepseek-chat', provider: 'default', tokens: 60, percent: (60 / 410) * 100 },
120
+ {
121
+ model: 'deepseek/deepseek-chat',
122
+ provider: 'deepseek',
123
+ tokens: 350,
124
+ percent: (350 / 410) * 100,
125
+ inputTokens: 100,
126
+ outputTokens: 50,
127
+ cacheReadTokens: 200,
128
+ cacheWriteTokens: 0,
129
+ },
130
+ {
131
+ model: 'deepseek-chat',
132
+ provider: 'default',
133
+ tokens: 60,
134
+ percent: (60 / 410) * 100,
135
+ inputTokens: 10,
136
+ outputTokens: 20,
137
+ cacheReadTokens: 0,
138
+ cacheWriteTokens: 30,
139
+ },
122
140
  ])
123
141
  assert.deepEqual(out.providers, [
124
142
  { provider: 'deepseek', tokens: 350, percent: (350 / 410) * 100 },
@@ -351,6 +369,38 @@ test('ttft 模型级聚合:加权平均,无 ttft 行不参与,无数据不挂字
351
369
  assert.equal('ttft' in m2, false)
352
370
  })
353
371
 
372
+ test('模型级四桶聚合:models 条目带输入输出与缓存拆分,按该模型行累加', () => {
373
+ // Given 两模型多行四桶用量 When D 粒度聚合 Then models 每项四桶为该模型行累加,排序与占比不变
374
+ const rows = [
375
+ makeRow({ bucket: '2020-01-01', model: 'm1', provider: 'p1', inputTokens: 100, outputTokens: 50, cacheReadTokens: 200, cacheWriteTokens: 10 }),
376
+ makeRow({ bucket: '2020-01-02', model: 'm1', provider: 'p1', inputTokens: 30, outputTokens: 70 }),
377
+ makeRow({ bucket: '2020-01-01', model: 'm2', provider: 'p2', inputTokens: 5, outputTokens: 25, cacheWriteTokens: 8 }),
378
+ ]
379
+ const out = aggregateRange(rows, 'D', '2020-01-01', '2020-01-02')
380
+ assert.deepEqual(out.models, [
381
+ {
382
+ model: 'm1',
383
+ provider: 'p1',
384
+ tokens: 460,
385
+ percent: (460 / 498) * 100,
386
+ inputTokens: 130,
387
+ outputTokens: 120,
388
+ cacheReadTokens: 200,
389
+ cacheWriteTokens: 10,
390
+ },
391
+ {
392
+ model: 'm2',
393
+ provider: 'p2',
394
+ tokens: 38,
395
+ percent: (38 / 498) * 100,
396
+ inputTokens: 5,
397
+ outputTokens: 25,
398
+ cacheReadTokens: 0,
399
+ cacheWriteTokens: 8,
400
+ },
401
+ ])
402
+ })
403
+
354
404
  test('槽级 ttft 聚合:同槽配对,无 ttft 槽不挂字段,attachCosts 保留', () => {
355
405
  const rows = [
356
406
  makeRow({ bucket: '2020-01-01', model: 'm1', provider: 'p1', outputTokens: 20, ttftMs: 2000, ttftSteps: 1 }),
@@ -373,7 +423,7 @@ test('attachCosts H 槽按桶起点计价并归集 totals 与 models', () => {
373
423
  assert.equal(out.daily[1].cost, 2)
374
424
  assert.equal(out.cost, 2)
375
425
  assert.equal(out.unpriced, 0)
376
- assert.deepEqual(out.models, [{ model: 'm1', provider: 'p1', tokens: 1500000, percent: 100, cost: 2 }])
426
+ assert.deepEqual(out.models, [{ model: 'm1', provider: 'p1', tokens: 1500000, inputTokens: 1000000, outputTokens: 500000, cacheReadTokens: 0, cacheWriteTokens: 0, percent: 100, cost: 2 }])
377
427
  assert.equal(out.from, '2020-01-01T00')
378
428
  assert.equal(out.tokens, 1500000)
379
429
  // 纯函数:入参 result 不被修改
@@ -151,6 +151,22 @@ test('反查:容器换引用(快照更替)后新节点可见', () => {
151
151
  assert.equal(turnTokenUsageOfMessage(oldNodes, 'm-1'), oldUsage)
152
152
  })
153
153
 
154
+ test('反查:仓库身份恒定而节点集更替时新增回合可见', () => {
155
+ // Given 官方 chat 仓库语义:store 实例身份跨回合恒定,values() 返回的节点集数组随 upsert 换新引用
156
+ const usage1 = { uncachedInputTokens: 1, outputTokens: 1, totalTokens: 2 }
157
+ const usage2 = { uncachedInputTokens: 7, outputTokens: 7, totalTokens: 14 }
158
+ let snapshot = [tailNode('m-1', usage1)]
159
+ const store = { values: () => snapshot }
160
+ assert.equal(turnUsageSourceOfMessage(store, 'm-1'), usage1)
161
+ // When 第二回合 turn-tail 节点入库(同一 store,节点集数组换引用)
162
+ snapshot = [...snapshot, tailNode('m-2', usage2)]
163
+ // Then 新回合反查命中,不依赖页面刷新重建索引
164
+ assert.equal(turnUsageSourceOfMessage(store, 'm-2'), usage2)
165
+ assert.equal(turnTokenUsageOfMessage(store, 'm-2'), usage2)
166
+ // 旧回合反查不受重建影响
167
+ assert.equal(turnUsageSourceOfMessage(store, 'm-1'), usage1)
168
+ })
169
+
154
170
  test('计价模型键:双全拼两段,仅 model 用裸名,双缺回退全通配键', () => {
155
171
  assert.equal(turnModelOf({ routes: [{ provider: 'p', model: 'm' }] }), 'p/m')
156
172
  assert.equal(turnModelOf({ routes: [{ model: 'm' }] }), 'm')