@lingda-ai/dsh-plugin-ledger 0.1.5 → 0.1.7
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/docs/technical.md +38 -2
- package/lib/client.js +189 -75
- package/lib/client.src.js +72 -18
- package/lib/panel-view.js +114 -57
- package/package.json +1 -1
package/docs/technical.md
CHANGED
|
@@ -242,17 +242,34 @@ token 只是 `measures` 里的一种单位。计划中的第二个来源(Trae
|
|
|
242
242
|
|
|
243
243
|
---
|
|
244
244
|
|
|
245
|
+
### 8.1 文案口径(改面板前必读)
|
|
246
|
+
|
|
247
|
+
面板的所有文案都对应过一次真实误读,别再改回去:
|
|
248
|
+
|
|
249
|
+
| 写法 | 为什么不能写成另一个 |
|
|
250
|
+
|---|---|
|
|
251
|
+
| `9 次调用` | `9 条` 紧挨金额会被读成 9 元 |
|
|
252
|
+
| `少花 97%` | `占应花 97%` 里的"应花"是内部口径,用户不知道分母是什么 |
|
|
253
|
+
| `几乎全命中` | 命中率 ≥99.5% 时 `占输入 100%` 零信息量;0% 时整条后缀不画 |
|
|
254
|
+
| `3 条费率未知,本次未计价` | 一条都算不出来时不存在"下限",不能写"合计偏低" |
|
|
255
|
+
| `—`(来源行) | 会话整体未计价时,各来源的 0 元不是"真花 0",是"分不出来" |
|
|
256
|
+
| `工具结果 1,208 字符` | 光秃秃的 `↑1,208` 没人知道箭头指什么 |
|
|
257
|
+
| `本次 180,000 tok` | 没有主语的 `180,000 tok` 会被当成累计上下文 |
|
|
258
|
+
| `共 5 个有金额来源` | 过滤掉零金额来源后必须说明,否则像渲染 bug |
|
|
259
|
+
|
|
245
260
|
## 9. 验证手段
|
|
246
261
|
|
|
247
262
|
| 手段 | 覆盖 | 产出 |
|
|
248
263
|
|---|---|---|
|
|
249
|
-
| `node --test test/*.test.mjs` |
|
|
264
|
+
| `node --test test/*.test.mjs` | 206 例断言 | 计价、归因、预算回滞、幂等、身份、卸载残留、面板 SSR、客户端产物形态 |
|
|
250
265
|
| `node scripts/build-client.mjs` | 产物形态 | 从源码重建 `lib/client.js`(零 import/export 的 loader 包) |
|
|
251
266
|
| `scripts/bench.mjs --gate` | 热路径 + 内存 | 上表数字 |
|
|
252
267
|
| `scripts/smoke-registry.mjs` | 插件装配契约 | 假 ctx 下工具注册/提供不抛 |
|
|
253
268
|
| `scripts/smoke-dsh.mjs` | 宿主契约 | 右栏 tab、seat、事件形状 |
|
|
254
269
|
| `scripts/smoke-panel.mjs` | 面板链路 | PANEL-1..10:落盘 → 读回口径一致 |
|
|
255
|
-
| `scripts/render-panel.mjs` |
|
|
270
|
+
| `scripts/render-panel.mjs` | 视觉(单状态) | 真实 `renderPayload()` 输出,三列宽度 |
|
|
271
|
+
| `scripts/render-states.mjs` | 视觉(状态矩阵) | 7 个状态并排:常规/空态/费率未知/费率被拒/展开/预算/300px |
|
|
272
|
+
| `test/client-lifecycle.test.mjs` | 客户端生命周期 | 登记可逆、重复 apply、无 slots 降级、登记抛错不冒泡 |
|
|
256
273
|
|
|
257
274
|
**SKIP 不等于通过**:冒烟脚本找不到 DSH 运行时用退出码 2 表示 SKIP,`prepublish.sh` 第 6 步把 SKIP 判 FAIL。
|
|
258
275
|
这条是为了避免"换台机器 CI 照样全绿,但契约一条没验"。
|
|
@@ -347,6 +364,25 @@ Desktop 宿主启动阶段直接停住(用户看到"以安全模式重启")
|
|
|
347
364
|
- `test/client-bundle.test.mjs` 在 `node:vm` 里用假 `window.__ModuleLoader__` 加载产物,断言形态;
|
|
348
365
|
- `scripts/ci.sh` 校验产物与源同步(重建后 `git diff` 必须为空)。
|
|
349
366
|
|
|
367
|
+
**12.6 客户端插件必须导出 `inject`,而且它和 `dsh.client.inject` 不是一回事。**
|
|
368
|
+
修完 12.5 之后再重启,宿主换了个死法:`cannot get property "sidebarRightTabs" without inject`。
|
|
369
|
+
|
|
370
|
+
原因:cordis 的 `ctx` 是代理,**访问未声明依赖的服务属性会当场抛错**(不是返回 undefined),
|
|
371
|
+
而这个抛错发生在 `apply()` 里,等于让宿主启动失败。`package.json` 的 `dsh.client.inject` 说的是
|
|
372
|
+
"先加载哪几个客户端包",`exports.inject` 说的是"这个插件依赖哪些服务"——前者写对了并不能免掉后者。
|
|
373
|
+
|
|
374
|
+
正确写法(对齐 `dsh-bloub-mood` / `dsh-client-ui-skill-explorer` 的产物):
|
|
375
|
+
|
|
376
|
+
```js
|
|
377
|
+
exports.inject = ['sidebarRightTabs'] // 硬依赖,缺了就别 apply
|
|
378
|
+
ctx.get('slots') // 可选服务:不受 inject 约束的通道
|
|
379
|
+
```
|
|
380
|
+
|
|
381
|
+
教训与 12.5 同源:**客户端半边的契约没有一条经过真机验证,而每一条的失败方式都是"宿主起不来"。**
|
|
382
|
+
所以本轮把 apply 里所有未证实契约(tab 登记、close handler、seat 挂载、卸载)都包了 try/catch,
|
|
383
|
+
默认姿态是"退化成一个没有面板的插件",而不是"把宿主带走";同时新增反证测试:
|
|
384
|
+
把 `inject` 去掉,`apply` 必须抛 `without inject`——这条测试的存在意义就是让这个错在单测里炸。
|
|
385
|
+
|
|
350
386
|
### 工程侧的一条硬约束
|
|
351
387
|
|
|
352
388
|
`dsh plugin --profile <p> add file:<path>` 是 pnpm 的 `file:` 依赖:**文件硬链接、目录复制**。
|
package/lib/client.js
CHANGED
|
@@ -220,7 +220,7 @@ window.__ModuleLoader__.load({
|
|
|
220
220
|
padding: '8px 10px',
|
|
221
221
|
background: '#fdf6e7',
|
|
222
222
|
borderLeft: `2px solid ${WARN}`,
|
|
223
|
-
fontSize:
|
|
223
|
+
fontSize: 11,
|
|
224
224
|
lineHeight: 1.65,
|
|
225
225
|
color: '#8a5a00',
|
|
226
226
|
},
|
|
@@ -231,7 +231,7 @@ window.__ModuleLoader__.load({
|
|
|
231
231
|
|
|
232
232
|
const INK = '#1f2733'
|
|
233
233
|
const INK2 = '#5a6472'
|
|
234
|
-
const INK3 = '#
|
|
234
|
+
const INK3 = '#6f7a89'
|
|
235
235
|
|
|
236
236
|
/**
|
|
237
237
|
* 时间一律按**本地时区**显示。
|
|
@@ -275,6 +275,18 @@ window.__ModuleLoader__.load({
|
|
|
275
275
|
return typeof value === 'number' && Number.isFinite(value) ? value.toLocaleString('en-US') : '—'
|
|
276
276
|
}
|
|
277
277
|
|
|
278
|
+
/**
|
|
279
|
+
* 明细行专用的紧凑写法:180,000 → 180k。
|
|
280
|
+
*
|
|
281
|
+
* 这一行还要放轮次、工具体量、模型名,380px 下必然是抢空间的;
|
|
282
|
+
* 金额在右边单独对齐,token 只要量级可读就够,不需要个位精度。
|
|
283
|
+
*/
|
|
284
|
+
function formatTokensCompact(value) {
|
|
285
|
+
if (typeof value !== 'number' || !Number.isFinite(value)) return '—'
|
|
286
|
+
if (value < 10_000) return value.toLocaleString('en-US')
|
|
287
|
+
return `${(value / 1000).toFixed(value >= 100_000 ? 0 : 1)}k`
|
|
288
|
+
}
|
|
289
|
+
|
|
278
290
|
/**
|
|
279
291
|
* 来源桶的显示名。三个保留 key 有业务含义,直接显示原始字符串会让人以为坏了。
|
|
280
292
|
* @param {string} key
|
|
@@ -370,7 +382,7 @@ window.__ModuleLoader__.load({
|
|
|
370
382
|
h(
|
|
371
383
|
'p',
|
|
372
384
|
{ style: { margin: '0 14px 12px', fontSize: 11.5, lineHeight: 1.7, color: INK3 } },
|
|
373
|
-
'
|
|
385
|
+
'常见原因:快照文件正在写入、Host 侧没开 panel.enabled、或工作区文件读取权限不足。点「刷新」可以重试。',
|
|
374
386
|
),
|
|
375
387
|
)
|
|
376
388
|
}
|
|
@@ -400,12 +412,22 @@ window.__ModuleLoader__.load({
|
|
|
400
412
|
}
|
|
401
413
|
}
|
|
402
414
|
|
|
403
|
-
/**
|
|
415
|
+
/**
|
|
416
|
+
* 副标题:调用次数 + 未归因次数。
|
|
417
|
+
*
|
|
418
|
+
* 三个坑都踩过:
|
|
419
|
+
* - 「9 条」在金额旁边会被读成 9 元 → 写「9 次调用」;
|
|
420
|
+
* - 「未归因 9」没单位,且全部未归因时这行等于什么都没说
|
|
421
|
+
* → 全部未归因时直接点明原因「来源未配置」,那是用户能动手改的;
|
|
422
|
+
* - 未归因统计的是**条目数**,不是金额未知,两者不能混成一句话。
|
|
423
|
+
*/
|
|
404
424
|
function summaryOf(totals, list) {
|
|
405
425
|
const entries = totals.entries ?? 0
|
|
406
426
|
const unattributed = list.find((item) => item?.plugin === 'unattributed')
|
|
407
427
|
const lost = typeof unattributed?.entries === 'number' && unattributed.entries > 0 ? unattributed.entries : null
|
|
408
|
-
|
|
428
|
+
if (lost === null) return `${entries} 次调用`
|
|
429
|
+
if (lost === entries) return `${entries} 次调用 · 来源未配置`
|
|
430
|
+
return `${entries} 次调用 · ${lost} 次未归因`
|
|
409
431
|
}
|
|
410
432
|
|
|
411
433
|
/** "还能问几次":Host 已按均价算好,这里只加一句人话,面板不做除法以外的推断 */
|
|
@@ -416,9 +438,18 @@ window.__ModuleLoader__.load({
|
|
|
416
438
|
return left === 0 ? ' · 余量不足一问' : ` · 还可约 ${left} 次`
|
|
417
439
|
}
|
|
418
440
|
|
|
419
|
-
/**
|
|
441
|
+
/**
|
|
442
|
+
* 命中率是"输入里走了缓存的比例",分母不含缓存写。
|
|
443
|
+
*
|
|
444
|
+
* 两个退化:
|
|
445
|
+
* - ≥99.5% 时「占输入 100%」没有任何信息量,写成「几乎全命中」;
|
|
446
|
+
* - 0% 时不写(那只是"这次没缓存",不是一条值得占版面的读数)。
|
|
447
|
+
*/
|
|
420
448
|
function hitRateOf(rate) {
|
|
421
|
-
|
|
449
|
+
if (typeof rate !== 'number' || !Number.isFinite(rate)) return ''
|
|
450
|
+
if (rate >= 0.995) return ' · 几乎全命中'
|
|
451
|
+
const pct = Math.round(rate * 100)
|
|
452
|
+
return pct === 0 ? '' : ` · 占输入 ${pct}%`
|
|
422
453
|
}
|
|
423
454
|
|
|
424
455
|
/**
|
|
@@ -439,7 +470,7 @@ window.__ModuleLoader__.load({
|
|
|
439
470
|
'div',
|
|
440
471
|
{ style: { marginTop: 8 } },
|
|
441
472
|
h('div', { style: { fontSize: 11, fontWeight: 600, color: INK2 } }, title),
|
|
442
|
-
h('div', { style: { fontSize:
|
|
473
|
+
h('div', { style: { fontSize: 11, lineHeight: 1.65, color: INK3, marginTop: 2 } }, detail),
|
|
443
474
|
)
|
|
444
475
|
|
|
445
476
|
return h(
|
|
@@ -457,7 +488,7 @@ window.__ModuleLoader__.load({
|
|
|
457
488
|
'发一条消息,这里就开始记账。只读观测,不改你的请求。',
|
|
458
489
|
),
|
|
459
490
|
h('div', { style: { height: 1, background: '#e8ebef', margin: '12px 0 10px' } }),
|
|
460
|
-
h('div', { style: { fontSize:
|
|
491
|
+
h('div', { style: { fontSize: 11, color: INK3, letterSpacing: 0.3 } }, '会记什么'),
|
|
461
492
|
item('每步花多少', '未命中输入 / 缓存命中 / 输出 分开计价,不用平均价套总数'),
|
|
462
493
|
item('谁花的', '按同轮上一步的工具调用归因,跨轮不硬猜'),
|
|
463
494
|
item('还能问几次', '按预算与均价估算;费率未知的条目不进均价'),
|
|
@@ -469,7 +500,7 @@ window.__ModuleLoader__.load({
|
|
|
469
500
|
padding: '8px 10px',
|
|
470
501
|
background: '#f6f7f9',
|
|
471
502
|
borderLeft: `2px solid ${INK3}`,
|
|
472
|
-
fontSize:
|
|
503
|
+
fontSize: 11,
|
|
473
504
|
lineHeight: 1.7,
|
|
474
505
|
color: INK2,
|
|
475
506
|
},
|
|
@@ -481,7 +512,7 @@ window.__ModuleLoader__.load({
|
|
|
481
512
|
),
|
|
482
513
|
h(
|
|
483
514
|
'div',
|
|
484
|
-
{ style: { padding: '4px 14px 12px', fontSize:
|
|
515
|
+
{ style: { padding: '4px 14px 12px', fontSize: 11, color: note.warn ? WARN : INK3 } },
|
|
485
516
|
note.text,
|
|
486
517
|
),
|
|
487
518
|
)
|
|
@@ -497,7 +528,12 @@ window.__ModuleLoader__.load({
|
|
|
497
528
|
const cost = totals.cost ?? {}
|
|
498
529
|
const tokens = totals.tokens ?? null
|
|
499
530
|
const parts = cost.parts ?? null
|
|
500
|
-
const
|
|
531
|
+
const rawList = Array.isArray(view.sources) && view.sources.length > 0 ? view.sources : view.topSources ?? []
|
|
532
|
+
// 金额为 0 且没有条目的来源行没有信息量(多见于"注册了工具但没产生成本"的插件),
|
|
533
|
+
// 但它们会让面板出现一排 ¥0.0000,读起来像"这些插件花过钱"
|
|
534
|
+
const list = rawList.filter((source) => !(source?.amount === 0 && (source?.entries ?? 0) === 0))
|
|
535
|
+
// 整个会话都算不出钱时,各来源的 0 元不是"真花了 0",而是"分不出来" —— 一律显示 —
|
|
536
|
+
const moneyKnown = typeof cost.amount === 'number' && Number.isFinite(cost.amount)
|
|
501
537
|
const recent = Array.isArray(view.recent) ? view.recent.filter((item) => item !== null) : []
|
|
502
538
|
const others = Array.isArray(view.overview)
|
|
503
539
|
? view.overview.filter((row) => row !== null && row?.sessionId !== undefined && row.sessionId !== sessionId)
|
|
@@ -531,8 +567,11 @@ window.__ModuleLoader__.load({
|
|
|
531
567
|
unknownCount > 0
|
|
532
568
|
? h(
|
|
533
569
|
'div',
|
|
534
|
-
{ style: { marginTop: 4, fontSize:
|
|
535
|
-
|
|
570
|
+
{ style: { marginTop: 4, fontSize: 11, lineHeight: 1.6, color: WARN } },
|
|
571
|
+
// "偏低"只在真有一个下限时成立;一条都算不出来时,说"未计价"才是实话
|
|
572
|
+
moneyKnown
|
|
573
|
+
? `其中 ${unknownCount} 条费率未知,合计偏低`
|
|
574
|
+
: `${unknownCount} 条费率未知,本次未计价`,
|
|
536
575
|
)
|
|
537
576
|
: null,
|
|
538
577
|
// 收益行:账本里唯一能严格算出的"省了多少钱"(两个单价都在价表里,差额确定)。
|
|
@@ -541,10 +580,10 @@ window.__ModuleLoader__.load({
|
|
|
541
580
|
? null
|
|
542
581
|
: h(
|
|
543
582
|
'div',
|
|
544
|
-
{ style: { marginTop: 4, fontSize:
|
|
583
|
+
{ style: { marginTop: 4, fontSize: 11, lineHeight: 1.6, color: GOOD, fontVariantNumeric: 'tabular-nums' } },
|
|
545
584
|
`缓存命中已省 ${formatAmount(view.totals.saved.amount, view.totals.saved.currency)}${
|
|
546
585
|
typeof view.totals.savedShare === 'number'
|
|
547
|
-
? ` ·
|
|
586
|
+
? ` · 少花 ${formatShare(view.totals.savedShare)}`
|
|
548
587
|
: ''
|
|
549
588
|
}`,
|
|
550
589
|
),
|
|
@@ -613,8 +652,14 @@ window.__ModuleLoader__.load({
|
|
|
613
652
|
{ style: { display: 'flex', alignItems: 'baseline', gap: 6 } },
|
|
614
653
|
h(
|
|
615
654
|
'span',
|
|
616
|
-
{ style: { fontSize:
|
|
617
|
-
|
|
655
|
+
{ style: { fontSize: 11, color: INK3 } },
|
|
656
|
+
// 计数用"有金额的来源",并在过滤掉零金额来源时说清楚,
|
|
657
|
+
// 免得"共 8 个来源"下面只画 5 行被当成渲染 bug
|
|
658
|
+
view.sourceCount === null || view.sourceCount === undefined
|
|
659
|
+
? ''
|
|
660
|
+
: list.length === (view.sourceCount ?? list.length)
|
|
661
|
+
? `共 ${view.sourceCount} 个来源`
|
|
662
|
+
: `共 ${list.length} 个有金额来源`,
|
|
618
663
|
),
|
|
619
664
|
// 只在前 6 条放不下时才给按钮:按钮一出来就说明"你少看了几条"
|
|
620
665
|
list.length > SOURCE_HEAD
|
|
@@ -623,7 +668,7 @@ window.__ModuleLoader__.load({
|
|
|
623
668
|
{
|
|
624
669
|
type: 'button',
|
|
625
670
|
onClick: options.onToggleSources,
|
|
626
|
-
style: { border: 'none', background: 'none', padding: 0, cursor: 'pointer', fontSize:
|
|
671
|
+
style: { border: 'none', background: 'none', padding: 0, cursor: 'pointer', fontSize: 11, color: '#2f7d64' },
|
|
627
672
|
},
|
|
628
673
|
expanded ? '收起' : `展开全部 ${list.length}`,
|
|
629
674
|
)
|
|
@@ -631,7 +676,11 @@ window.__ModuleLoader__.load({
|
|
|
631
676
|
),
|
|
632
677
|
),
|
|
633
678
|
list.length === 0
|
|
634
|
-
? h(
|
|
679
|
+
? h(
|
|
680
|
+
'p',
|
|
681
|
+
{ style: { margin: 0, fontSize: 11.5, color: INK3 } },
|
|
682
|
+
rawList.length > 0 ? '有工具调用,但都没有产生可计的金额' : '还没有可归因的来源',
|
|
683
|
+
)
|
|
635
684
|
: (expanded ? list : list.slice(0, SOURCE_HEAD)).map((source, index) =>
|
|
636
685
|
h(
|
|
637
686
|
'div',
|
|
@@ -647,7 +696,9 @@ window.__ModuleLoader__.load({
|
|
|
647
696
|
h(
|
|
648
697
|
'span',
|
|
649
698
|
{ style: { fontSize: 12, color: INK, fontVariantNumeric: 'tabular-nums', whiteSpace: 'nowrap' } },
|
|
650
|
-
|
|
699
|
+
moneyKnown
|
|
700
|
+
? `${formatAmount(source.amount, cost.currency)} ${formatShare(source.share)}`
|
|
701
|
+
: '—',
|
|
651
702
|
),
|
|
652
703
|
),
|
|
653
704
|
),
|
|
@@ -668,7 +719,7 @@ window.__ModuleLoader__.load({
|
|
|
668
719
|
? null
|
|
669
720
|
: h(
|
|
670
721
|
'span',
|
|
671
|
-
{ style: { fontSize:
|
|
722
|
+
{ style: { fontSize: 11, color: INK3, fontVariantNumeric: 'tabular-nums', whiteSpace: 'nowrap' } },
|
|
672
723
|
`最贵一步 ${formatAmount(peak.amount, cost.currency)}`,
|
|
673
724
|
),
|
|
674
725
|
),
|
|
@@ -683,10 +734,10 @@ window.__ModuleLoader__.load({
|
|
|
683
734
|
'span',
|
|
684
735
|
{ style: { fontSize: 11.5, color: INK2, fontVariantNumeric: 'tabular-nums' } },
|
|
685
736
|
formatClock(item.at),
|
|
686
|
-
h('span', { style: { fontSize:
|
|
737
|
+
h('span', { style: { fontSize: 11, color: INK3, marginLeft: 6 } }, item.kind === 'compaction' ? '压缩' : '模型'),
|
|
687
738
|
// 轮次:治理时要能定位到"哪一问最贵",时间不够用
|
|
688
739
|
typeof item.turn === 'number'
|
|
689
|
-
? h('span', { style: { fontSize:
|
|
740
|
+
? h('span', { style: { fontSize: 11, color: INK3, marginLeft: 6 } }, `T${item.turn}·S${item.step ?? '—'}`)
|
|
690
741
|
: null,
|
|
691
742
|
),
|
|
692
743
|
h(
|
|
@@ -697,35 +748,11 @@ window.__ModuleLoader__.load({
|
|
|
697
748
|
),
|
|
698
749
|
h(
|
|
699
750
|
'div',
|
|
700
|
-
{ style: { fontSize:
|
|
701
|
-
//
|
|
702
|
-
//
|
|
703
|
-
//
|
|
704
|
-
|
|
705
|
-
? null
|
|
706
|
-
: h(
|
|
707
|
-
'span',
|
|
708
|
-
{ style: { color: GOOD, marginRight: 6, fontVariantNumeric: 'tabular-nums' } },
|
|
709
|
-
// 省下的量记在"压缩后的第一条调用"上;这条只有前值,就别画一个悬空的箭头
|
|
710
|
-
`压缩前 ${formatContextTokens(item.contextBeforeTokens)}`,
|
|
711
|
-
),
|
|
712
|
-
item.contextSavedTokens === null || item.contextSavedTokens === undefined
|
|
713
|
-
? null
|
|
714
|
-
: h(
|
|
715
|
-
'span',
|
|
716
|
-
{ style: { color: GOOD, marginRight: 6, fontVariantNumeric: 'tabular-nums' } },
|
|
717
|
-
`↓省 ${formatContextTokens(item.contextSavedTokens)}`,
|
|
718
|
-
),
|
|
719
|
-
item.contextChars === null || item.contextChars === undefined
|
|
720
|
-
? null
|
|
721
|
-
: h(
|
|
722
|
-
'span',
|
|
723
|
-
{ style: { color: item.contextChars >= 10_000 ? WARN : INK3, marginRight: 6 } },
|
|
724
|
-
`↑${formatChars(item.contextChars)}`,
|
|
725
|
-
),
|
|
726
|
-
routeTextOf(item),
|
|
727
|
-
tokensTotal(item.tokens) === null ? '' : ` · ${formatTokens(tokensTotal(item.tokens))} tok`,
|
|
728
|
-
item.plugin === null ? '' : ` · ${sourceLabel(item.plugin)}`,
|
|
751
|
+
{ style: { fontSize: 11, color: INK3, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' } },
|
|
752
|
+
// 第二行的优先级 = "为什么这一步贵":压缩收益 > 本次 token 量 > 工具体量 > 路由 > 归属。
|
|
753
|
+
// 380px 下这一行必定会被省略号截断,所以要让它从右边开始丢信息,
|
|
754
|
+
// 而不是从左边(左边留住的是解释,右边留住的是标识)。
|
|
755
|
+
...detailPartsOf(React, item, cost),
|
|
729
756
|
),
|
|
730
757
|
),
|
|
731
758
|
),
|
|
@@ -761,20 +788,50 @@ window.__ModuleLoader__.load({
|
|
|
761
788
|
// 页脚
|
|
762
789
|
h(
|
|
763
790
|
'div',
|
|
764
|
-
{ style: { padding: '6px 14px 12px', fontSize:
|
|
765
|
-
|
|
791
|
+
{ style: { padding: '6px 14px 12px', fontSize: 11, color: INK3 } },
|
|
792
|
+
`快照 ${formatLocalTime(view.generatedAt)}`,
|
|
766
793
|
),
|
|
767
794
|
h(
|
|
768
795
|
'div',
|
|
769
796
|
{
|
|
770
797
|
// 费率脚注:自定义生效用正文色、无效用警示色,绝不与"内置价"混成一色
|
|
771
|
-
style: { padding: '0 14px 12px', fontSize:
|
|
798
|
+
style: { padding: '0 14px 12px', fontSize: 11, color: note.warn ? WARN : INK3 },
|
|
772
799
|
},
|
|
773
800
|
note.text,
|
|
774
801
|
),
|
|
775
802
|
)
|
|
776
803
|
}
|
|
777
804
|
|
|
805
|
+
/**
|
|
806
|
+
* 明细第二行的片段,用「 · 」连接。
|
|
807
|
+
*
|
|
808
|
+
* 抽成纯函数是为了让"谁被截断"这件事可测:片段顺序固定,
|
|
809
|
+
* 极少数的压缩条目也只是在最前面多两段,不会影响后面的相对顺序。
|
|
810
|
+
* @param {any} React
|
|
811
|
+
* @param {object} item - compactEntry 产出的一条明细
|
|
812
|
+
* @param {object} cost - totals.cost(取币种与可读性标记)
|
|
813
|
+
*/
|
|
814
|
+
function detailPartsOf(React, item, cost) {
|
|
815
|
+
const h = React.createElement
|
|
816
|
+
const parts = []
|
|
817
|
+
if (item.contextBeforeTokens !== null && item.contextBeforeTokens !== undefined) {
|
|
818
|
+
parts.push(h('span', { style: { color: GOOD, fontVariantNumeric: 'tabular-nums' } }, `压缩前 ${formatContextTokens(item.contextBeforeTokens)}`))
|
|
819
|
+
}
|
|
820
|
+
if (item.contextSavedTokens !== null && item.contextSavedTokens !== undefined) {
|
|
821
|
+
parts.push(h('span', { style: { color: GOOD, fontVariantNumeric: 'tabular-nums' } }, `↓省 ${formatContextTokens(item.contextSavedTokens)}`))
|
|
822
|
+
}
|
|
823
|
+
const total = tokensTotal(item.tokens)
|
|
824
|
+
// 「本次」两个字不能省:没有主语的 180k tok 会被读成累计上下文
|
|
825
|
+
if (total !== null) parts.push(`本次 ${formatTokensCompact(total)} tok`)
|
|
826
|
+
if (item.contextChars !== null && item.contextChars !== undefined) {
|
|
827
|
+
parts.push(h('span', { style: { color: item.contextChars >= 10_000 ? WARN : INK3 } }, `工具结果 ${formatChars(item.contextChars)}`))
|
|
828
|
+
}
|
|
829
|
+
const route = routeTextOf(item)
|
|
830
|
+
if (route !== '') parts.push(route)
|
|
831
|
+
if (item.plugin !== null && item.plugin !== undefined) parts.push(sourceLabel(item.plugin))
|
|
832
|
+
return parts.flatMap((part, index) => (index === 0 ? [part] : [' · ', part]))
|
|
833
|
+
}
|
|
834
|
+
|
|
778
835
|
function rowOf(React, label, value, hint) {
|
|
779
836
|
const h = React.createElement
|
|
780
837
|
return h(
|
|
@@ -784,7 +841,7 @@ window.__ModuleLoader__.load({
|
|
|
784
841
|
'span',
|
|
785
842
|
{ style: { fontSize: 12, color: INK2 } },
|
|
786
843
|
label,
|
|
787
|
-
hint === undefined ? null : h('span', { style: { fontSize:
|
|
844
|
+
hint === undefined ? null : h('span', { style: { fontSize: 11, color: INK3, marginLeft: 4 } }, hint),
|
|
788
845
|
),
|
|
789
846
|
h('span', { style: { fontSize: 12, color: INK, fontVariantNumeric: 'tabular-nums' } }, value),
|
|
790
847
|
)
|
|
@@ -844,6 +901,38 @@ window.__ModuleLoader__.load({
|
|
|
844
901
|
/** tab 类型判别串,`openTab('ledger.cost', …)` 用的就是它。 */
|
|
845
902
|
const LEDGER_KIND = 'ledger.cost'
|
|
846
903
|
|
|
904
|
+
/**
|
|
905
|
+
* 硬依赖:没有右栏 tab 服务,这个插件什么都不用做。
|
|
906
|
+
*
|
|
907
|
+
* 这条**必须声明**。cordis 的 ctx 是代理:访问未声明的服务属性会直接抛
|
|
908
|
+
* `cannot get property "sidebarRightTabs" without inject`,而且是在 apply 里抛,
|
|
909
|
+
* 等同于让宿主启动失败(0.1.5 重启即崩就是这么来的——之前只写了 package.json 的
|
|
910
|
+
* dsh.client.inject,那是"先加载哪个包",不是"这个插件依赖哪个服务",两回事)。
|
|
911
|
+
*/
|
|
912
|
+
const inject = ['sidebarRightTabs']
|
|
913
|
+
|
|
914
|
+
/**
|
|
915
|
+
* 取可选服务。不能写成 `ctx.xxx`——没在 inject 里声明的服务,属性访问会抛。
|
|
916
|
+
* 宿主提供了 `ctx.get(name)` 这条不受 inject 约束的通道;Node 侧单测的假 ctx
|
|
917
|
+
* 没有 get,退回普通属性读。
|
|
918
|
+
* @param {any} ctx
|
|
919
|
+
* @param {string} name
|
|
920
|
+
*/
|
|
921
|
+
function optionalService(ctx, name) {
|
|
922
|
+
if (typeof ctx?.get === 'function') {
|
|
923
|
+
try {
|
|
924
|
+
return ctx.get(name) ?? null
|
|
925
|
+
} catch {
|
|
926
|
+
return null
|
|
927
|
+
}
|
|
928
|
+
}
|
|
929
|
+
try {
|
|
930
|
+
return ctx?.[name] ?? null
|
|
931
|
+
} catch {
|
|
932
|
+
return null
|
|
933
|
+
}
|
|
934
|
+
}
|
|
935
|
+
|
|
847
936
|
/** 面板用到的 seat 名,抽出来避免字符串散落。 */
|
|
848
937
|
const SEAT_BODY = 'sidebar.right.pane.tab'
|
|
849
938
|
const SEAT_TITLE = 'sidebar.right.pane.tab.title'
|
|
@@ -889,7 +978,7 @@ window.__ModuleLoader__.load({
|
|
|
889
978
|
function createFetchView(ctx) {
|
|
890
979
|
return async function fetchView(sessionId, opts = {}) {
|
|
891
980
|
if (typeof sessionId !== 'string' || sessionId.length === 0) return null
|
|
892
|
-
const files = ctx
|
|
981
|
+
const files = optionalService(ctx, 'remote')?.workspaceFiles
|
|
893
982
|
if (!files || typeof files.stat !== 'function' || typeof files.read !== 'function') {
|
|
894
983
|
return { error: '宿主未提供 workspaceFiles 通道' }
|
|
895
984
|
}
|
|
@@ -980,16 +1069,30 @@ window.__ModuleLoader__.load({
|
|
|
980
1069
|
keepMounted: true,
|
|
981
1070
|
}
|
|
982
1071
|
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
1072
|
+
// 客户端契约里除 sidebarRightTabs 之外的部分都没被真机证实过。
|
|
1073
|
+
// 这是一个 UI 插件:任何一处契约偏差都应该退化成"没有面板",而不是让宿主起不来。
|
|
1074
|
+
let disposeTab = null
|
|
1075
|
+
try {
|
|
1076
|
+
disposeTab = tabs.register(definition)
|
|
1077
|
+
} catch (error) {
|
|
1078
|
+
console.warn('[ledger] 右栏 tab 登记失败,面板不出现(宿主其它功能不受影响):', error)
|
|
1079
|
+
return
|
|
1080
|
+
}
|
|
1081
|
+
|
|
1082
|
+
const sidebarRight = optionalService(ctx, 'sidebarRight')
|
|
1083
|
+
let disposeClose = null
|
|
1084
|
+
if (typeof sidebarRight?.registerCloseHandler === 'function') {
|
|
1085
|
+
try {
|
|
1086
|
+
disposeClose = sidebarRight.registerCloseHandler(LEDGER_KIND, () => {})
|
|
1087
|
+
} catch (error) {
|
|
1088
|
+
console.warn('[ledger] registerCloseHandler 失败:', error)
|
|
1089
|
+
}
|
|
1090
|
+
}
|
|
988
1091
|
|
|
989
1092
|
// 正文与标题:宿主把 seat 声明在 sidebar-right 里,先 inject 再 register
|
|
990
1093
|
let disposeBody = null
|
|
991
1094
|
let disposeTitle = null
|
|
992
|
-
const slots = ctx
|
|
1095
|
+
const slots = optionalService(ctx, 'slots')
|
|
993
1096
|
if (typeof slots?.inject === 'function' && typeof slots.register === 'function') {
|
|
994
1097
|
let React = null
|
|
995
1098
|
try {
|
|
@@ -998,21 +1101,29 @@ window.__ModuleLoader__.load({
|
|
|
998
1101
|
React = null // 宿主没给 React:保持无正文的降级态
|
|
999
1102
|
}
|
|
1000
1103
|
if (React !== null) {
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
slots.
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
slots.
|
|
1007
|
-
|
|
1104
|
+
try {
|
|
1105
|
+
const fetchView = createFetchView(ctx)
|
|
1106
|
+
disposeBody = slots.inject(SEAT_BODY, () =>
|
|
1107
|
+
slots.register({ name: SEAT_BODY, key: LEDGER_ID }, makeLedgerBody(React, fetchView)),
|
|
1108
|
+
)
|
|
1109
|
+
disposeTitle = slots.inject(SEAT_TITLE, () =>
|
|
1110
|
+
slots.register({ name: SEAT_TITLE, key: LEDGER_ID }, makeLedgerTitle(React)),
|
|
1111
|
+
)
|
|
1112
|
+
} catch (error) {
|
|
1113
|
+
// seat 名对不上、权限不足、host 版本差异:只丢面板正文,tab 仍在
|
|
1114
|
+
console.warn('[ledger] 面板正文挂载失败(只剩 tab 标题):', error)
|
|
1115
|
+
}
|
|
1008
1116
|
}
|
|
1009
1117
|
}
|
|
1010
1118
|
|
|
1011
1119
|
const cleanup = () => {
|
|
1012
|
-
disposeTitle
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1120
|
+
for (const dispose of [disposeTitle, disposeBody, disposeClose, disposeTab]) {
|
|
1121
|
+
try {
|
|
1122
|
+
dispose?.()
|
|
1123
|
+
} catch {
|
|
1124
|
+
// 卸载阶段的异常同样不能冒泡
|
|
1125
|
+
}
|
|
1126
|
+
}
|
|
1016
1127
|
}
|
|
1017
1128
|
if (typeof ctx.effect === 'function') ctx.effect(() => cleanup)
|
|
1018
1129
|
else ctx.on('dispose', cleanup)
|
|
@@ -1039,6 +1150,9 @@ window.__ModuleLoader__.load({
|
|
|
1039
1150
|
__setReactLoader(() => require("react"));
|
|
1040
1151
|
|
|
1041
1152
|
exports.apply = apply;
|
|
1153
|
+
// 硬依赖必须跟着产物一起导出:cordis 靠它判断"什么时候允许 apply",
|
|
1154
|
+
// 漏了就会在 apply 里抛 cannot get property "..." without inject,把宿主启动带崩。
|
|
1155
|
+
exports.inject = inject;
|
|
1042
1156
|
return module.exports;
|
|
1043
1157
|
},
|
|
1044
1158
|
});
|
package/lib/client.src.js
CHANGED
|
@@ -43,6 +43,38 @@ const LEDGER_ID = '@lingda-ai/ledger'
|
|
|
43
43
|
/** tab 类型判别串,`openTab('ledger.cost', …)` 用的就是它。 */
|
|
44
44
|
const LEDGER_KIND = 'ledger.cost'
|
|
45
45
|
|
|
46
|
+
/**
|
|
47
|
+
* 硬依赖:没有右栏 tab 服务,这个插件什么都不用做。
|
|
48
|
+
*
|
|
49
|
+
* 这条**必须声明**。cordis 的 ctx 是代理:访问未声明的服务属性会直接抛
|
|
50
|
+
* `cannot get property "sidebarRightTabs" without inject`,而且是在 apply 里抛,
|
|
51
|
+
* 等同于让宿主启动失败(0.1.5 重启即崩就是这么来的——之前只写了 package.json 的
|
|
52
|
+
* dsh.client.inject,那是"先加载哪个包",不是"这个插件依赖哪个服务",两回事)。
|
|
53
|
+
*/
|
|
54
|
+
export const inject = ['sidebarRightTabs']
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* 取可选服务。不能写成 `ctx.xxx`——没在 inject 里声明的服务,属性访问会抛。
|
|
58
|
+
* 宿主提供了 `ctx.get(name)` 这条不受 inject 约束的通道;Node 侧单测的假 ctx
|
|
59
|
+
* 没有 get,退回普通属性读。
|
|
60
|
+
* @param {any} ctx
|
|
61
|
+
* @param {string} name
|
|
62
|
+
*/
|
|
63
|
+
function optionalService(ctx, name) {
|
|
64
|
+
if (typeof ctx?.get === 'function') {
|
|
65
|
+
try {
|
|
66
|
+
return ctx.get(name) ?? null
|
|
67
|
+
} catch {
|
|
68
|
+
return null
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
try {
|
|
72
|
+
return ctx?.[name] ?? null
|
|
73
|
+
} catch {
|
|
74
|
+
return null
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
46
78
|
/** 面板用到的 seat 名,抽出来避免字符串散落。 */
|
|
47
79
|
const SEAT_BODY = 'sidebar.right.pane.tab'
|
|
48
80
|
const SEAT_TITLE = 'sidebar.right.pane.tab.title'
|
|
@@ -102,7 +134,7 @@ import {
|
|
|
102
134
|
export function createFetchView(ctx) {
|
|
103
135
|
return async function fetchView(sessionId, opts = {}) {
|
|
104
136
|
if (typeof sessionId !== 'string' || sessionId.length === 0) return null
|
|
105
|
-
const files = ctx
|
|
137
|
+
const files = optionalService(ctx, 'remote')?.workspaceFiles
|
|
106
138
|
if (!files || typeof files.stat !== 'function' || typeof files.read !== 'function') {
|
|
107
139
|
return { error: '宿主未提供 workspaceFiles 通道' }
|
|
108
140
|
}
|
|
@@ -193,16 +225,30 @@ export async function apply(ctx) {
|
|
|
193
225
|
keepMounted: true,
|
|
194
226
|
}
|
|
195
227
|
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
228
|
+
// 客户端契约里除 sidebarRightTabs 之外的部分都没被真机证实过。
|
|
229
|
+
// 这是一个 UI 插件:任何一处契约偏差都应该退化成"没有面板",而不是让宿主起不来。
|
|
230
|
+
let disposeTab = null
|
|
231
|
+
try {
|
|
232
|
+
disposeTab = tabs.register(definition)
|
|
233
|
+
} catch (error) {
|
|
234
|
+
console.warn('[ledger] 右栏 tab 登记失败,面板不出现(宿主其它功能不受影响):', error)
|
|
235
|
+
return
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
const sidebarRight = optionalService(ctx, 'sidebarRight')
|
|
239
|
+
let disposeClose = null
|
|
240
|
+
if (typeof sidebarRight?.registerCloseHandler === 'function') {
|
|
241
|
+
try {
|
|
242
|
+
disposeClose = sidebarRight.registerCloseHandler(LEDGER_KIND, () => {})
|
|
243
|
+
} catch (error) {
|
|
244
|
+
console.warn('[ledger] registerCloseHandler 失败:', error)
|
|
245
|
+
}
|
|
246
|
+
}
|
|
201
247
|
|
|
202
248
|
// 正文与标题:宿主把 seat 声明在 sidebar-right 里,先 inject 再 register
|
|
203
249
|
let disposeBody = null
|
|
204
250
|
let disposeTitle = null
|
|
205
|
-
const slots = ctx
|
|
251
|
+
const slots = optionalService(ctx, 'slots')
|
|
206
252
|
if (typeof slots?.inject === 'function' && typeof slots.register === 'function') {
|
|
207
253
|
let React = null
|
|
208
254
|
try {
|
|
@@ -211,21 +257,29 @@ export async function apply(ctx) {
|
|
|
211
257
|
React = null // 宿主没给 React:保持无正文的降级态
|
|
212
258
|
}
|
|
213
259
|
if (React !== null) {
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
slots.
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
slots.
|
|
220
|
-
|
|
260
|
+
try {
|
|
261
|
+
const fetchView = createFetchView(ctx)
|
|
262
|
+
disposeBody = slots.inject(SEAT_BODY, () =>
|
|
263
|
+
slots.register({ name: SEAT_BODY, key: LEDGER_ID }, makeLedgerBody(React, fetchView)),
|
|
264
|
+
)
|
|
265
|
+
disposeTitle = slots.inject(SEAT_TITLE, () =>
|
|
266
|
+
slots.register({ name: SEAT_TITLE, key: LEDGER_ID }, makeLedgerTitle(React)),
|
|
267
|
+
)
|
|
268
|
+
} catch (error) {
|
|
269
|
+
// seat 名对不上、权限不足、host 版本差异:只丢面板正文,tab 仍在
|
|
270
|
+
console.warn('[ledger] 面板正文挂载失败(只剩 tab 标题):', error)
|
|
271
|
+
}
|
|
221
272
|
}
|
|
222
273
|
}
|
|
223
274
|
|
|
224
275
|
const cleanup = () => {
|
|
225
|
-
disposeTitle
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
276
|
+
for (const dispose of [disposeTitle, disposeBody, disposeClose, disposeTab]) {
|
|
277
|
+
try {
|
|
278
|
+
dispose?.()
|
|
279
|
+
} catch {
|
|
280
|
+
// 卸载阶段的异常同样不能冒泡
|
|
281
|
+
}
|
|
282
|
+
}
|
|
229
283
|
}
|
|
230
284
|
if (typeof ctx.effect === 'function') ctx.effect(() => cleanup)
|
|
231
285
|
else ctx.on('dispose', cleanup)
|
package/lib/panel-view.js
CHANGED
|
@@ -137,7 +137,7 @@ function pricingBanner(React, note) {
|
|
|
137
137
|
padding: '8px 10px',
|
|
138
138
|
background: '#fdf6e7',
|
|
139
139
|
borderLeft: `2px solid ${WARN}`,
|
|
140
|
-
fontSize:
|
|
140
|
+
fontSize: 11,
|
|
141
141
|
lineHeight: 1.65,
|
|
142
142
|
color: '#8a5a00',
|
|
143
143
|
},
|
|
@@ -148,7 +148,7 @@ function pricingBanner(React, note) {
|
|
|
148
148
|
|
|
149
149
|
const INK = '#1f2733'
|
|
150
150
|
const INK2 = '#5a6472'
|
|
151
|
-
const INK3 = '#
|
|
151
|
+
const INK3 = '#6f7a89'
|
|
152
152
|
|
|
153
153
|
/**
|
|
154
154
|
* 时间一律按**本地时区**显示。
|
|
@@ -192,6 +192,18 @@ function formatTokens(value) {
|
|
|
192
192
|
return typeof value === 'number' && Number.isFinite(value) ? value.toLocaleString('en-US') : '—'
|
|
193
193
|
}
|
|
194
194
|
|
|
195
|
+
/**
|
|
196
|
+
* 明细行专用的紧凑写法:180,000 → 180k。
|
|
197
|
+
*
|
|
198
|
+
* 这一行还要放轮次、工具体量、模型名,380px 下必然是抢空间的;
|
|
199
|
+
* 金额在右边单独对齐,token 只要量级可读就够,不需要个位精度。
|
|
200
|
+
*/
|
|
201
|
+
function formatTokensCompact(value) {
|
|
202
|
+
if (typeof value !== 'number' || !Number.isFinite(value)) return '—'
|
|
203
|
+
if (value < 10_000) return value.toLocaleString('en-US')
|
|
204
|
+
return `${(value / 1000).toFixed(value >= 100_000 ? 0 : 1)}k`
|
|
205
|
+
}
|
|
206
|
+
|
|
195
207
|
/**
|
|
196
208
|
* 来源桶的显示名。三个保留 key 有业务含义,直接显示原始字符串会让人以为坏了。
|
|
197
209
|
* @param {string} key
|
|
@@ -287,7 +299,7 @@ function makeLedgerBody(React, fetchView) {
|
|
|
287
299
|
h(
|
|
288
300
|
'p',
|
|
289
301
|
{ style: { margin: '0 14px 12px', fontSize: 11.5, lineHeight: 1.7, color: INK3 } },
|
|
290
|
-
'
|
|
302
|
+
'常见原因:快照文件正在写入、Host 侧没开 panel.enabled、或工作区文件读取权限不足。点「刷新」可以重试。',
|
|
291
303
|
),
|
|
292
304
|
)
|
|
293
305
|
}
|
|
@@ -317,12 +329,22 @@ function makeLedgerBody(React, fetchView) {
|
|
|
317
329
|
}
|
|
318
330
|
}
|
|
319
331
|
|
|
320
|
-
/**
|
|
332
|
+
/**
|
|
333
|
+
* 副标题:调用次数 + 未归因次数。
|
|
334
|
+
*
|
|
335
|
+
* 三个坑都踩过:
|
|
336
|
+
* - 「9 条」在金额旁边会被读成 9 元 → 写「9 次调用」;
|
|
337
|
+
* - 「未归因 9」没单位,且全部未归因时这行等于什么都没说
|
|
338
|
+
* → 全部未归因时直接点明原因「来源未配置」,那是用户能动手改的;
|
|
339
|
+
* - 未归因统计的是**条目数**,不是金额未知,两者不能混成一句话。
|
|
340
|
+
*/
|
|
321
341
|
function summaryOf(totals, list) {
|
|
322
342
|
const entries = totals.entries ?? 0
|
|
323
343
|
const unattributed = list.find((item) => item?.plugin === 'unattributed')
|
|
324
344
|
const lost = typeof unattributed?.entries === 'number' && unattributed.entries > 0 ? unattributed.entries : null
|
|
325
|
-
|
|
345
|
+
if (lost === null) return `${entries} 次调用`
|
|
346
|
+
if (lost === entries) return `${entries} 次调用 · 来源未配置`
|
|
347
|
+
return `${entries} 次调用 · ${lost} 次未归因`
|
|
326
348
|
}
|
|
327
349
|
|
|
328
350
|
/** "还能问几次":Host 已按均价算好,这里只加一句人话,面板不做除法以外的推断 */
|
|
@@ -333,9 +355,18 @@ function callsLeftOf(budget) {
|
|
|
333
355
|
return left === 0 ? ' · 余量不足一问' : ` · 还可约 ${left} 次`
|
|
334
356
|
}
|
|
335
357
|
|
|
336
|
-
/**
|
|
358
|
+
/**
|
|
359
|
+
* 命中率是"输入里走了缓存的比例",分母不含缓存写。
|
|
360
|
+
*
|
|
361
|
+
* 两个退化:
|
|
362
|
+
* - ≥99.5% 时「占输入 100%」没有任何信息量,写成「几乎全命中」;
|
|
363
|
+
* - 0% 时不写(那只是"这次没缓存",不是一条值得占版面的读数)。
|
|
364
|
+
*/
|
|
337
365
|
function hitRateOf(rate) {
|
|
338
|
-
|
|
366
|
+
if (typeof rate !== 'number' || !Number.isFinite(rate)) return ''
|
|
367
|
+
if (rate >= 0.995) return ' · 几乎全命中'
|
|
368
|
+
const pct = Math.round(rate * 100)
|
|
369
|
+
return pct === 0 ? '' : ` · 占输入 ${pct}%`
|
|
339
370
|
}
|
|
340
371
|
|
|
341
372
|
/**
|
|
@@ -356,7 +387,7 @@ function renderEmptyState(React, view, sessionId, head, options = {}) {
|
|
|
356
387
|
'div',
|
|
357
388
|
{ style: { marginTop: 8 } },
|
|
358
389
|
h('div', { style: { fontSize: 11, fontWeight: 600, color: INK2 } }, title),
|
|
359
|
-
h('div', { style: { fontSize:
|
|
390
|
+
h('div', { style: { fontSize: 11, lineHeight: 1.65, color: INK3, marginTop: 2 } }, detail),
|
|
360
391
|
)
|
|
361
392
|
|
|
362
393
|
return h(
|
|
@@ -374,7 +405,7 @@ function renderEmptyState(React, view, sessionId, head, options = {}) {
|
|
|
374
405
|
'发一条消息,这里就开始记账。只读观测,不改你的请求。',
|
|
375
406
|
),
|
|
376
407
|
h('div', { style: { height: 1, background: '#e8ebef', margin: '12px 0 10px' } }),
|
|
377
|
-
h('div', { style: { fontSize:
|
|
408
|
+
h('div', { style: { fontSize: 11, color: INK3, letterSpacing: 0.3 } }, '会记什么'),
|
|
378
409
|
item('每步花多少', '未命中输入 / 缓存命中 / 输出 分开计价,不用平均价套总数'),
|
|
379
410
|
item('谁花的', '按同轮上一步的工具调用归因,跨轮不硬猜'),
|
|
380
411
|
item('还能问几次', '按预算与均价估算;费率未知的条目不进均价'),
|
|
@@ -386,7 +417,7 @@ function renderEmptyState(React, view, sessionId, head, options = {}) {
|
|
|
386
417
|
padding: '8px 10px',
|
|
387
418
|
background: '#f6f7f9',
|
|
388
419
|
borderLeft: `2px solid ${INK3}`,
|
|
389
|
-
fontSize:
|
|
420
|
+
fontSize: 11,
|
|
390
421
|
lineHeight: 1.7,
|
|
391
422
|
color: INK2,
|
|
392
423
|
},
|
|
@@ -398,7 +429,7 @@ function renderEmptyState(React, view, sessionId, head, options = {}) {
|
|
|
398
429
|
),
|
|
399
430
|
h(
|
|
400
431
|
'div',
|
|
401
|
-
{ style: { padding: '4px 14px 12px', fontSize:
|
|
432
|
+
{ style: { padding: '4px 14px 12px', fontSize: 11, color: note.warn ? WARN : INK3 } },
|
|
402
433
|
note.text,
|
|
403
434
|
),
|
|
404
435
|
)
|
|
@@ -414,7 +445,12 @@ function renderPayload(React, view, sessionId, head, options = {}) {
|
|
|
414
445
|
const cost = totals.cost ?? {}
|
|
415
446
|
const tokens = totals.tokens ?? null
|
|
416
447
|
const parts = cost.parts ?? null
|
|
417
|
-
const
|
|
448
|
+
const rawList = Array.isArray(view.sources) && view.sources.length > 0 ? view.sources : view.topSources ?? []
|
|
449
|
+
// 金额为 0 且没有条目的来源行没有信息量(多见于"注册了工具但没产生成本"的插件),
|
|
450
|
+
// 但它们会让面板出现一排 ¥0.0000,读起来像"这些插件花过钱"
|
|
451
|
+
const list = rawList.filter((source) => !(source?.amount === 0 && (source?.entries ?? 0) === 0))
|
|
452
|
+
// 整个会话都算不出钱时,各来源的 0 元不是"真花了 0",而是"分不出来" —— 一律显示 —
|
|
453
|
+
const moneyKnown = typeof cost.amount === 'number' && Number.isFinite(cost.amount)
|
|
418
454
|
const recent = Array.isArray(view.recent) ? view.recent.filter((item) => item !== null) : []
|
|
419
455
|
const others = Array.isArray(view.overview)
|
|
420
456
|
? view.overview.filter((row) => row !== null && row?.sessionId !== undefined && row.sessionId !== sessionId)
|
|
@@ -448,8 +484,11 @@ function renderPayload(React, view, sessionId, head, options = {}) {
|
|
|
448
484
|
unknownCount > 0
|
|
449
485
|
? h(
|
|
450
486
|
'div',
|
|
451
|
-
{ style: { marginTop: 4, fontSize:
|
|
452
|
-
|
|
487
|
+
{ style: { marginTop: 4, fontSize: 11, lineHeight: 1.6, color: WARN } },
|
|
488
|
+
// "偏低"只在真有一个下限时成立;一条都算不出来时,说"未计价"才是实话
|
|
489
|
+
moneyKnown
|
|
490
|
+
? `其中 ${unknownCount} 条费率未知,合计偏低`
|
|
491
|
+
: `${unknownCount} 条费率未知,本次未计价`,
|
|
453
492
|
)
|
|
454
493
|
: null,
|
|
455
494
|
// 收益行:账本里唯一能严格算出的"省了多少钱"(两个单价都在价表里,差额确定)。
|
|
@@ -458,10 +497,10 @@ function renderPayload(React, view, sessionId, head, options = {}) {
|
|
|
458
497
|
? null
|
|
459
498
|
: h(
|
|
460
499
|
'div',
|
|
461
|
-
{ style: { marginTop: 4, fontSize:
|
|
500
|
+
{ style: { marginTop: 4, fontSize: 11, lineHeight: 1.6, color: GOOD, fontVariantNumeric: 'tabular-nums' } },
|
|
462
501
|
`缓存命中已省 ${formatAmount(view.totals.saved.amount, view.totals.saved.currency)}${
|
|
463
502
|
typeof view.totals.savedShare === 'number'
|
|
464
|
-
? ` ·
|
|
503
|
+
? ` · 少花 ${formatShare(view.totals.savedShare)}`
|
|
465
504
|
: ''
|
|
466
505
|
}`,
|
|
467
506
|
),
|
|
@@ -530,8 +569,14 @@ function renderPayload(React, view, sessionId, head, options = {}) {
|
|
|
530
569
|
{ style: { display: 'flex', alignItems: 'baseline', gap: 6 } },
|
|
531
570
|
h(
|
|
532
571
|
'span',
|
|
533
|
-
{ style: { fontSize:
|
|
534
|
-
|
|
572
|
+
{ style: { fontSize: 11, color: INK3 } },
|
|
573
|
+
// 计数用"有金额的来源",并在过滤掉零金额来源时说清楚,
|
|
574
|
+
// 免得"共 8 个来源"下面只画 5 行被当成渲染 bug
|
|
575
|
+
view.sourceCount === null || view.sourceCount === undefined
|
|
576
|
+
? ''
|
|
577
|
+
: list.length === (view.sourceCount ?? list.length)
|
|
578
|
+
? `共 ${view.sourceCount} 个来源`
|
|
579
|
+
: `共 ${list.length} 个有金额来源`,
|
|
535
580
|
),
|
|
536
581
|
// 只在前 6 条放不下时才给按钮:按钮一出来就说明"你少看了几条"
|
|
537
582
|
list.length > SOURCE_HEAD
|
|
@@ -540,7 +585,7 @@ function renderPayload(React, view, sessionId, head, options = {}) {
|
|
|
540
585
|
{
|
|
541
586
|
type: 'button',
|
|
542
587
|
onClick: options.onToggleSources,
|
|
543
|
-
style: { border: 'none', background: 'none', padding: 0, cursor: 'pointer', fontSize:
|
|
588
|
+
style: { border: 'none', background: 'none', padding: 0, cursor: 'pointer', fontSize: 11, color: '#2f7d64' },
|
|
544
589
|
},
|
|
545
590
|
expanded ? '收起' : `展开全部 ${list.length}`,
|
|
546
591
|
)
|
|
@@ -548,7 +593,11 @@ function renderPayload(React, view, sessionId, head, options = {}) {
|
|
|
548
593
|
),
|
|
549
594
|
),
|
|
550
595
|
list.length === 0
|
|
551
|
-
? h(
|
|
596
|
+
? h(
|
|
597
|
+
'p',
|
|
598
|
+
{ style: { margin: 0, fontSize: 11.5, color: INK3 } },
|
|
599
|
+
rawList.length > 0 ? '有工具调用,但都没有产生可计的金额' : '还没有可归因的来源',
|
|
600
|
+
)
|
|
552
601
|
: (expanded ? list : list.slice(0, SOURCE_HEAD)).map((source, index) =>
|
|
553
602
|
h(
|
|
554
603
|
'div',
|
|
@@ -564,7 +613,9 @@ function renderPayload(React, view, sessionId, head, options = {}) {
|
|
|
564
613
|
h(
|
|
565
614
|
'span',
|
|
566
615
|
{ style: { fontSize: 12, color: INK, fontVariantNumeric: 'tabular-nums', whiteSpace: 'nowrap' } },
|
|
567
|
-
|
|
616
|
+
moneyKnown
|
|
617
|
+
? `${formatAmount(source.amount, cost.currency)} ${formatShare(source.share)}`
|
|
618
|
+
: '—',
|
|
568
619
|
),
|
|
569
620
|
),
|
|
570
621
|
),
|
|
@@ -585,7 +636,7 @@ function renderPayload(React, view, sessionId, head, options = {}) {
|
|
|
585
636
|
? null
|
|
586
637
|
: h(
|
|
587
638
|
'span',
|
|
588
|
-
{ style: { fontSize:
|
|
639
|
+
{ style: { fontSize: 11, color: INK3, fontVariantNumeric: 'tabular-nums', whiteSpace: 'nowrap' } },
|
|
589
640
|
`最贵一步 ${formatAmount(peak.amount, cost.currency)}`,
|
|
590
641
|
),
|
|
591
642
|
),
|
|
@@ -600,10 +651,10 @@ function renderPayload(React, view, sessionId, head, options = {}) {
|
|
|
600
651
|
'span',
|
|
601
652
|
{ style: { fontSize: 11.5, color: INK2, fontVariantNumeric: 'tabular-nums' } },
|
|
602
653
|
formatClock(item.at),
|
|
603
|
-
h('span', { style: { fontSize:
|
|
654
|
+
h('span', { style: { fontSize: 11, color: INK3, marginLeft: 6 } }, item.kind === 'compaction' ? '压缩' : '模型'),
|
|
604
655
|
// 轮次:治理时要能定位到"哪一问最贵",时间不够用
|
|
605
656
|
typeof item.turn === 'number'
|
|
606
|
-
? h('span', { style: { fontSize:
|
|
657
|
+
? h('span', { style: { fontSize: 11, color: INK3, marginLeft: 6 } }, `T${item.turn}·S${item.step ?? '—'}`)
|
|
607
658
|
: null,
|
|
608
659
|
),
|
|
609
660
|
h(
|
|
@@ -614,35 +665,11 @@ function renderPayload(React, view, sessionId, head, options = {}) {
|
|
|
614
665
|
),
|
|
615
666
|
h(
|
|
616
667
|
'div',
|
|
617
|
-
{ style: { fontSize:
|
|
618
|
-
//
|
|
619
|
-
//
|
|
620
|
-
//
|
|
621
|
-
|
|
622
|
-
? null
|
|
623
|
-
: h(
|
|
624
|
-
'span',
|
|
625
|
-
{ style: { color: GOOD, marginRight: 6, fontVariantNumeric: 'tabular-nums' } },
|
|
626
|
-
// 省下的量记在"压缩后的第一条调用"上;这条只有前值,就别画一个悬空的箭头
|
|
627
|
-
`压缩前 ${formatContextTokens(item.contextBeforeTokens)}`,
|
|
628
|
-
),
|
|
629
|
-
item.contextSavedTokens === null || item.contextSavedTokens === undefined
|
|
630
|
-
? null
|
|
631
|
-
: h(
|
|
632
|
-
'span',
|
|
633
|
-
{ style: { color: GOOD, marginRight: 6, fontVariantNumeric: 'tabular-nums' } },
|
|
634
|
-
`↓省 ${formatContextTokens(item.contextSavedTokens)}`,
|
|
635
|
-
),
|
|
636
|
-
item.contextChars === null || item.contextChars === undefined
|
|
637
|
-
? null
|
|
638
|
-
: h(
|
|
639
|
-
'span',
|
|
640
|
-
{ style: { color: item.contextChars >= 10_000 ? WARN : INK3, marginRight: 6 } },
|
|
641
|
-
`↑${formatChars(item.contextChars)}`,
|
|
642
|
-
),
|
|
643
|
-
routeTextOf(item),
|
|
644
|
-
tokensTotal(item.tokens) === null ? '' : ` · ${formatTokens(tokensTotal(item.tokens))} tok`,
|
|
645
|
-
item.plugin === null ? '' : ` · ${sourceLabel(item.plugin)}`,
|
|
668
|
+
{ style: { fontSize: 11, color: INK3, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' } },
|
|
669
|
+
// 第二行的优先级 = "为什么这一步贵":压缩收益 > 本次 token 量 > 工具体量 > 路由 > 归属。
|
|
670
|
+
// 380px 下这一行必定会被省略号截断,所以要让它从右边开始丢信息,
|
|
671
|
+
// 而不是从左边(左边留住的是解释,右边留住的是标识)。
|
|
672
|
+
...detailPartsOf(React, item, cost),
|
|
646
673
|
),
|
|
647
674
|
),
|
|
648
675
|
),
|
|
@@ -678,20 +705,50 @@ function renderPayload(React, view, sessionId, head, options = {}) {
|
|
|
678
705
|
// 页脚
|
|
679
706
|
h(
|
|
680
707
|
'div',
|
|
681
|
-
{ style: { padding: '6px 14px 12px', fontSize:
|
|
682
|
-
|
|
708
|
+
{ style: { padding: '6px 14px 12px', fontSize: 11, color: INK3 } },
|
|
709
|
+
`快照 ${formatLocalTime(view.generatedAt)}`,
|
|
683
710
|
),
|
|
684
711
|
h(
|
|
685
712
|
'div',
|
|
686
713
|
{
|
|
687
714
|
// 费率脚注:自定义生效用正文色、无效用警示色,绝不与"内置价"混成一色
|
|
688
|
-
style: { padding: '0 14px 12px', fontSize:
|
|
715
|
+
style: { padding: '0 14px 12px', fontSize: 11, color: note.warn ? WARN : INK3 },
|
|
689
716
|
},
|
|
690
717
|
note.text,
|
|
691
718
|
),
|
|
692
719
|
)
|
|
693
720
|
}
|
|
694
721
|
|
|
722
|
+
/**
|
|
723
|
+
* 明细第二行的片段,用「 · 」连接。
|
|
724
|
+
*
|
|
725
|
+
* 抽成纯函数是为了让"谁被截断"这件事可测:片段顺序固定,
|
|
726
|
+
* 极少数的压缩条目也只是在最前面多两段,不会影响后面的相对顺序。
|
|
727
|
+
* @param {any} React
|
|
728
|
+
* @param {object} item - compactEntry 产出的一条明细
|
|
729
|
+
* @param {object} cost - totals.cost(取币种与可读性标记)
|
|
730
|
+
*/
|
|
731
|
+
function detailPartsOf(React, item, cost) {
|
|
732
|
+
const h = React.createElement
|
|
733
|
+
const parts = []
|
|
734
|
+
if (item.contextBeforeTokens !== null && item.contextBeforeTokens !== undefined) {
|
|
735
|
+
parts.push(h('span', { style: { color: GOOD, fontVariantNumeric: 'tabular-nums' } }, `压缩前 ${formatContextTokens(item.contextBeforeTokens)}`))
|
|
736
|
+
}
|
|
737
|
+
if (item.contextSavedTokens !== null && item.contextSavedTokens !== undefined) {
|
|
738
|
+
parts.push(h('span', { style: { color: GOOD, fontVariantNumeric: 'tabular-nums' } }, `↓省 ${formatContextTokens(item.contextSavedTokens)}`))
|
|
739
|
+
}
|
|
740
|
+
const total = tokensTotal(item.tokens)
|
|
741
|
+
// 「本次」两个字不能省:没有主语的 180k tok 会被读成累计上下文
|
|
742
|
+
if (total !== null) parts.push(`本次 ${formatTokensCompact(total)} tok`)
|
|
743
|
+
if (item.contextChars !== null && item.contextChars !== undefined) {
|
|
744
|
+
parts.push(h('span', { style: { color: item.contextChars >= 10_000 ? WARN : INK3 } }, `工具结果 ${formatChars(item.contextChars)}`))
|
|
745
|
+
}
|
|
746
|
+
const route = routeTextOf(item)
|
|
747
|
+
if (route !== '') parts.push(route)
|
|
748
|
+
if (item.plugin !== null && item.plugin !== undefined) parts.push(sourceLabel(item.plugin))
|
|
749
|
+
return parts.flatMap((part, index) => (index === 0 ? [part] : [' · ', part]))
|
|
750
|
+
}
|
|
751
|
+
|
|
695
752
|
function rowOf(React, label, value, hint) {
|
|
696
753
|
const h = React.createElement
|
|
697
754
|
return h(
|
|
@@ -701,7 +758,7 @@ function rowOf(React, label, value, hint) {
|
|
|
701
758
|
'span',
|
|
702
759
|
{ style: { fontSize: 12, color: INK2 } },
|
|
703
760
|
label,
|
|
704
|
-
hint === undefined ? null : h('span', { style: { fontSize:
|
|
761
|
+
hint === undefined ? null : h('span', { style: { fontSize: 11, color: INK3, marginLeft: 4 } }, hint),
|
|
705
762
|
),
|
|
706
763
|
h('span', { style: { fontSize: 12, color: INK, fontVariantNumeric: 'tabular-nums' } }, value),
|
|
707
764
|
)
|