@lingda-ai/dsh-plugin-replay 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +102 -2
- package/cordis.patch.yml +7 -0
- package/docs/replay-contract.md +81 -0
- package/index.js +146 -0
- package/lib/client.js +682 -0
- package/lib/projector.js +253 -0
- package/lib/rates.js +172 -0
- package/lib/redact.js +58 -0
- package/lib/route.js +196 -0
- package/lib/store.js +439 -0
- package/package.json +72 -4
package/lib/client.js
ADDED
|
@@ -0,0 +1,682 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @lingda-ai/dsh-plugin-replay —— Client 半(右侧栏「黑匣子」tab)
|
|
3
|
+
*
|
|
4
|
+
* 形态:宿主把每个插件的 client.js 原样拼成一个 classic script,
|
|
5
|
+
* 所以这里**不能出现 import / export**,只有一次 window.__ModuleLoader__.load()。
|
|
6
|
+
*
|
|
7
|
+
* 数据通道:同源 `GET /dsh-ops/api`(Host 半只读投影,不落盘到浏览器)。
|
|
8
|
+
* 布局硬约束:右栏最窄 300px 可用;所有数字列固定宽度、长文本省略号。
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
window.__ModuleLoader__.load({
|
|
12
|
+
id: '@lingda-ai/dsh-plugin-replay',
|
|
13
|
+
factory(require) {
|
|
14
|
+
const React = require('react')
|
|
15
|
+
const h = React.createElement
|
|
16
|
+
const { useState, useEffect, useRef, useCallback, useMemo } = React
|
|
17
|
+
|
|
18
|
+
/** seat key 与 tab 类型共用。 */
|
|
19
|
+
const ID = '@lingda-ai/dsh-plugin-replay'
|
|
20
|
+
/** openTab 用的判别串。 */
|
|
21
|
+
const KIND = 'replay.blackbox'
|
|
22
|
+
const API = '/dsh-ops/api'
|
|
23
|
+
|
|
24
|
+
const C = {
|
|
25
|
+
bg: 'transparent',
|
|
26
|
+
card: '#fff',
|
|
27
|
+
line: '#e3e8ef',
|
|
28
|
+
hair: '#eef1f5',
|
|
29
|
+
t1: '#1f2733',
|
|
30
|
+
t2: '#5a6472',
|
|
31
|
+
t3: '#64707f',
|
|
32
|
+
t4: '#8a93a0',
|
|
33
|
+
green: '#2f7d64',
|
|
34
|
+
amber: '#c98a12',
|
|
35
|
+
red: '#b4453c',
|
|
36
|
+
blue: '#2f6feb',
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// #region 文案
|
|
40
|
+
const T = {
|
|
41
|
+
title: '黑匣子',
|
|
42
|
+
guideDesc: '会话回放:每一步做了什么、花了多少、成没成',
|
|
43
|
+
tabTimeline: '时间轴',
|
|
44
|
+
tabHotspot: '热点',
|
|
45
|
+
tabCompare: '对比',
|
|
46
|
+
refresh: '刷新',
|
|
47
|
+
loading: '正在读回放…',
|
|
48
|
+
emptyTitle: '这个会话还没有记录',
|
|
49
|
+
emptyBody: '发一条消息就开始记录。只记结构(动作、金额、成败),不记对话内容。',
|
|
50
|
+
emptyHint: '一直为空?确认插件已在 profile 中启用,且 `~/.dsh/ops/replay` 可写。',
|
|
51
|
+
errTitle: '回放读不出来',
|
|
52
|
+
filterAll: '全部',
|
|
53
|
+
filterCostly: '只看贵的',
|
|
54
|
+
filterFailed: '只看失败',
|
|
55
|
+
secTool: '按工具(谁让它变贵的)',
|
|
56
|
+
secModel: '按模型',
|
|
57
|
+
noCost: '费率未知,本次不计价',
|
|
58
|
+
comparePick: '选两个会话做对比',
|
|
59
|
+
compareNeed: '已选 {n}/2',
|
|
60
|
+
privacy: '不含对话内容:无 prompt / 工具参数 / 返回 / 绝对路径',
|
|
61
|
+
unknown: '未归因',
|
|
62
|
+
}
|
|
63
|
+
// #endregion
|
|
64
|
+
|
|
65
|
+
// #region 格式化
|
|
66
|
+
function money(cost) {
|
|
67
|
+
if (!cost || !Number.isFinite(cost.amount)) return null
|
|
68
|
+
const symbol = cost.currency === 'CNY' ? '¥' : cost.currency === 'USD' ? '$' : ''
|
|
69
|
+
const amount = cost.amount
|
|
70
|
+
const digits = Math.abs(amount) >= 0.1 ? 2 : 4
|
|
71
|
+
return symbol + amount.toFixed(digits)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function tokens(value) {
|
|
75
|
+
if (!Number.isFinite(value) || value <= 0) return null
|
|
76
|
+
if (value >= 1e6) return (value / 1e6).toFixed(2).replace(/\.?0+$/, '') + 'M'
|
|
77
|
+
if (value >= 1e3) return (value / 1e3).toFixed(1).replace(/\.0$/, '') + 'k'
|
|
78
|
+
return String(value)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function duration(ms) {
|
|
82
|
+
if (!Number.isFinite(ms)) return null
|
|
83
|
+
if (ms < 1000) return Math.round(ms) + 'ms'
|
|
84
|
+
if (ms < 60000) return (ms / 1000).toFixed(1) + 's'
|
|
85
|
+
const m = Math.floor(ms / 60000)
|
|
86
|
+
const s = Math.round((ms % 60000) / 1000)
|
|
87
|
+
return m + 'm' + (s > 0 ? s + 's' : '')
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function clockOf(at) {
|
|
91
|
+
if (!Number.isFinite(at)) return '—'
|
|
92
|
+
const d = new Date(at)
|
|
93
|
+
return String(d.getHours()).padStart(2, '0') + ':' + String(d.getMinutes()).padStart(2, '0')
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function coordsOf(row) {
|
|
97
|
+
if (row.turn === undefined || row.step === undefined) return null
|
|
98
|
+
return 'T' + row.turn + '·S' + row.step
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function measText(rows) {
|
|
102
|
+
const parts = []
|
|
103
|
+
for (const row of rows) {
|
|
104
|
+
for (const m of row.meas || []) {
|
|
105
|
+
const label = m.unit === 'token.input' ? '输入'
|
|
106
|
+
: m.unit === 'token.cache_read' ? '缓存'
|
|
107
|
+
: m.unit === 'token.output' ? '输出'
|
|
108
|
+
: m.unit === 'token.cache_write' ? '缓存写' : m.unit
|
|
109
|
+
const value = tokens(m.value)
|
|
110
|
+
if (value) parts.push(label + ' ' + value)
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return parts.join(' · ')
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function opLabel(row) {
|
|
117
|
+
if (row.op === 'model.call') return { text: '模型', tone: 'model' }
|
|
118
|
+
if (row.op === 'tool.call') {
|
|
119
|
+
if (row.ok === false) return { text: '失败', tone: 'err' }
|
|
120
|
+
return { text: '工具', tone: 'tool' }
|
|
121
|
+
}
|
|
122
|
+
if (row.op === 'compaction') return { text: '压缩', tone: 'neutral' }
|
|
123
|
+
if (row.op === 'aggr') return { text: '折叠', tone: 'neutral' }
|
|
124
|
+
return { text: row.op || '事件', tone: 'neutral' }
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// #endregion
|
|
128
|
+
|
|
129
|
+
// #region 取数
|
|
130
|
+
async function getJson(query, signal) {
|
|
131
|
+
const response = await fetch(API + '?' + query, { signal, headers: { accept: 'application/json' } })
|
|
132
|
+
if (!response.ok) throw new Error('HTTP ' + response.status)
|
|
133
|
+
const data = await response.json()
|
|
134
|
+
if (!data || data.ok !== true) throw new Error((data && data.error) || '查询失败')
|
|
135
|
+
return data
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** 会话 id 的来源:tab 的 props 优先,取不到再退回最近一个回放。 */
|
|
139
|
+
function pickSessionId(props) {
|
|
140
|
+
const candidates = [
|
|
141
|
+
props && props.session && props.session.id,
|
|
142
|
+
props && props.sessionId,
|
|
143
|
+
props && props.session && props.session.sessionId,
|
|
144
|
+
props && props.session && props.session.header && props.session.header.id,
|
|
145
|
+
]
|
|
146
|
+
for (const value of candidates) {
|
|
147
|
+
if (typeof value === 'string' && value.length > 0) return value
|
|
148
|
+
}
|
|
149
|
+
return null
|
|
150
|
+
}
|
|
151
|
+
// #endregion
|
|
152
|
+
|
|
153
|
+
// #region 视图
|
|
154
|
+
function Chip(props) {
|
|
155
|
+
const on = props.on
|
|
156
|
+
const style = {
|
|
157
|
+
border: '1px solid ' + (on ? C.t1 : '#dfe4ec'),
|
|
158
|
+
background: on ? C.t1 : '#fff',
|
|
159
|
+
color: on ? '#fff' : C.t2,
|
|
160
|
+
borderRadius: 999,
|
|
161
|
+
padding: '2px 9px',
|
|
162
|
+
fontSize: 11,
|
|
163
|
+
cursor: 'pointer',
|
|
164
|
+
lineHeight: 1.6,
|
|
165
|
+
}
|
|
166
|
+
return h('button', { type: 'button', style, onClick: props.onClick }, props.label)
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function Empty(props) {
|
|
170
|
+
return h('div', { style: { padding: '18px 14px 14px' } },
|
|
171
|
+
h('div', { style: { fontSize: 14, fontWeight: 600, color: C.t1, marginBottom: 6 } }, props.title),
|
|
172
|
+
h('div', { style: { fontSize: 11.5, lineHeight: 1.7, color: C.t2 } }, props.body),
|
|
173
|
+
props.hint
|
|
174
|
+
? h('div', {
|
|
175
|
+
style: {
|
|
176
|
+
marginTop: 12, padding: '8px 10px', background: '#f6f8fb',
|
|
177
|
+
borderLeft: '2px solid ' + C.t3, borderRadius: '0 8px 8px 0',
|
|
178
|
+
fontSize: 11, lineHeight: 1.7, color: C.t2,
|
|
179
|
+
},
|
|
180
|
+
}, props.hint)
|
|
181
|
+
: null,
|
|
182
|
+
)
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function SpanRow(props) {
|
|
186
|
+
const row = props.row
|
|
187
|
+
const op = opLabel(row)
|
|
188
|
+
const isAggr = row.op === 'aggr'
|
|
189
|
+
const amount = money(row.cost)
|
|
190
|
+
const dur = duration(row.durMs)
|
|
191
|
+
const coords = coordsOf(row)
|
|
192
|
+
|
|
193
|
+
const toneBg = op.tone === 'model' ? '#eaf0fb'
|
|
194
|
+
: op.tone === 'tool' ? '#edf6f1'
|
|
195
|
+
: op.tone === 'err' ? '#fbeeec' : '#eef1f5'
|
|
196
|
+
const toneFg = op.tone === 'model' ? '#2f5fb3'
|
|
197
|
+
: op.tone === 'tool' ? '#2f7d64'
|
|
198
|
+
: op.tone === 'err' ? '#b4453c' : C.t2
|
|
199
|
+
|
|
200
|
+
const name = isAggr
|
|
201
|
+
? '已折叠 ' + row.aggr + ' 步'
|
|
202
|
+
: row.op === 'model.call'
|
|
203
|
+
? (row.model || '未知模型')
|
|
204
|
+
: row.op === 'tool.call'
|
|
205
|
+
? (row.tool || '未知工具')
|
|
206
|
+
: row.op === 'compaction' ? '上下文压缩' : '事件'
|
|
207
|
+
|
|
208
|
+
const sub = isAggr
|
|
209
|
+
? measText([row]) || '早期明细已合并'
|
|
210
|
+
: row.op === 'model.call'
|
|
211
|
+
? (measText([row]) || (row.costReason ? T.noCost : ''))
|
|
212
|
+
: row.op === 'tool.call'
|
|
213
|
+
? (row.ok === false ? '错误码 ' + (row.errCode || 'error') : (row.ok === null ? '结果未回' : '完成'))
|
|
214
|
+
: ''
|
|
215
|
+
|
|
216
|
+
const barWidth = isAggr ? 100
|
|
217
|
+
: (row.op === 'model.call' && amount && props.maxAmount > 0)
|
|
218
|
+
? Math.max(6, Math.round((row.cost.amount / props.maxAmount) * 100))
|
|
219
|
+
: 0
|
|
220
|
+
|
|
221
|
+
return h('div', {
|
|
222
|
+
style: {
|
|
223
|
+
display: 'flex', gap: 10, padding: '7px 0',
|
|
224
|
+
borderTop: '1px solid ' + C.hair, alignItems: 'flex-start',
|
|
225
|
+
},
|
|
226
|
+
},
|
|
227
|
+
h('div', { style: { width: 46, flex: 'none', fontSize: 11, lineHeight: 1.5, color: C.t3 } },
|
|
228
|
+
h('b', { style: { display: 'block', color: C.t2, fontWeight: 500, fontVariantNumeric: 'tabular-nums' } }, clockOf(row.at)),
|
|
229
|
+
coords ? coords : null,
|
|
230
|
+
),
|
|
231
|
+
h('div', { style: { flex: 1, minWidth: 0 } },
|
|
232
|
+
h('div', { style: { display: 'flex', alignItems: 'center', gap: 5, fontSize: 12, color: C.t1 } },
|
|
233
|
+
h('span', {
|
|
234
|
+
style: {
|
|
235
|
+
fontSize: 10, padding: '1px 5px', borderRadius: 4,
|
|
236
|
+
background: toneBg, color: toneFg, flex: 'none',
|
|
237
|
+
},
|
|
238
|
+
}, op.text),
|
|
239
|
+
h('span', {
|
|
240
|
+
style: { overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' },
|
|
241
|
+
title: name,
|
|
242
|
+
}, name),
|
|
243
|
+
),
|
|
244
|
+
sub
|
|
245
|
+
? h('div', {
|
|
246
|
+
style: {
|
|
247
|
+
fontSize: 11, color: C.t3, marginTop: 2,
|
|
248
|
+
overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap',
|
|
249
|
+
},
|
|
250
|
+
}, sub)
|
|
251
|
+
: null,
|
|
252
|
+
barWidth > 0
|
|
253
|
+
? h('div', { style: { height: 3, borderRadius: 2, background: '#f0f3f7', marginTop: 5, overflow: 'hidden' } },
|
|
254
|
+
h('i', {
|
|
255
|
+
style: {
|
|
256
|
+
display: 'block', height: '100%', width: barWidth + '%',
|
|
257
|
+
background: op.tone === 'err' ? '#d08a80' : op.tone === 'model' ? '#c9d3e2' : '#dfe5ee',
|
|
258
|
+
borderRadius: 2,
|
|
259
|
+
},
|
|
260
|
+
}))
|
|
261
|
+
: null,
|
|
262
|
+
),
|
|
263
|
+
h('div', {
|
|
264
|
+
style: {
|
|
265
|
+
width: 58, flex: 'none', textAlign: 'right', fontSize: 12,
|
|
266
|
+
color: amount ? C.t1 : C.t3, lineHeight: 1.5, fontVariantNumeric: 'tabular-nums',
|
|
267
|
+
},
|
|
268
|
+
},
|
|
269
|
+
amount || (row.op === 'tool.call' ? '—' : ''),
|
|
270
|
+
h('span', { style: { display: 'block', fontSize: 10.5, color: C.t3 } }, dur || ''),
|
|
271
|
+
),
|
|
272
|
+
)
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
function Timeline(props) {
|
|
276
|
+
const rows = props.rows
|
|
277
|
+
const maxAmount = useMemo(() => {
|
|
278
|
+
let max = 0
|
|
279
|
+
for (const row of rows) {
|
|
280
|
+
if (row.cost && Number.isFinite(row.cost.amount) && row.cost.amount > max) max = row.cost.amount
|
|
281
|
+
}
|
|
282
|
+
return max
|
|
283
|
+
}, [rows])
|
|
284
|
+
|
|
285
|
+
const filtered = useMemo(() => {
|
|
286
|
+
if (props.filter === 'failed') return rows.filter((row) => row.op === 'tool.call' && row.ok === false)
|
|
287
|
+
if (props.filter === 'costly') {
|
|
288
|
+
const threshold = maxAmount * 0.5
|
|
289
|
+
return rows.filter((row) => row.cost && row.cost.amount >= threshold && threshold > 0)
|
|
290
|
+
.concat(rows.filter((row) => row.op === 'aggr'))
|
|
291
|
+
.slice(0, 40)
|
|
292
|
+
}
|
|
293
|
+
return rows
|
|
294
|
+
}, [rows, props.filter, maxAmount])
|
|
295
|
+
|
|
296
|
+
if (filtered.length === 0) {
|
|
297
|
+
return h(Empty, {
|
|
298
|
+
title: props.filter === 'all' ? T.emptyTitle : '这个筛选下没有记录',
|
|
299
|
+
body: props.filter === 'all' ? T.emptyBody : '换个筛选条件;失败项只在工具出错时才有。',
|
|
300
|
+
hint: props.filter === 'all' ? T.emptyHint : null,
|
|
301
|
+
})
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
return h('div', { style: { padding: '2px 14px 6px' } },
|
|
305
|
+
filtered.map((row) => h(SpanRow, { key: row.eid || (row.op + row.at), row, maxAmount })),
|
|
306
|
+
)
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
function BarList(props) {
|
|
310
|
+
const list = props.list
|
|
311
|
+
if (!list || list.length === 0) {
|
|
312
|
+
return h('div', { style: { fontSize: 11.5, color: C.t3, padding: '6px 0' } }, '没有可归因的成本')
|
|
313
|
+
}
|
|
314
|
+
return h('div', null, list.map((item) => h('div', { key: item.key, style: { padding: '5px 0' } },
|
|
315
|
+
h('div', { style: { display: 'flex', justifyContent: 'space-between', gap: 8, fontSize: 12, color: C.t2 } },
|
|
316
|
+
h('span', {
|
|
317
|
+
style: { overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' },
|
|
318
|
+
title: item.key,
|
|
319
|
+
}, item.key),
|
|
320
|
+
h('span', { style: { color: C.t1, whiteSpace: 'nowrap', fontVariantNumeric: 'tabular-nums' } },
|
|
321
|
+
(money(item) || '—') + (item.share === null ? '' : ' · ' + item.share + '%')),
|
|
322
|
+
),
|
|
323
|
+
h('div', { style: { height: 6, borderRadius: 3, background: '#f0f3f7', marginTop: 4, overflow: 'hidden' } },
|
|
324
|
+
h('i', {
|
|
325
|
+
style: {
|
|
326
|
+
display: 'block', height: '100%', borderRadius: 3, background: '#9fb0c9',
|
|
327
|
+
width: Math.max(2, Math.min(100, item.share === null ? 4 : item.share)) + '%',
|
|
328
|
+
},
|
|
329
|
+
})),
|
|
330
|
+
)))
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
function Hotspot(props) {
|
|
334
|
+
const data = props.data
|
|
335
|
+
if (!data) return h('div', { style: { padding: '14px', fontSize: 11.5, color: C.t3 } }, T.loading)
|
|
336
|
+
const totals = data.totals || {}
|
|
337
|
+
const cost = money(totals.cost)
|
|
338
|
+
return h('div', null,
|
|
339
|
+
h('div', { style: { padding: '12px 14px 4px' } },
|
|
340
|
+
h('div', { style: { display: 'flex', alignItems: 'baseline', gap: 8, flexWrap: 'wrap' } },
|
|
341
|
+
h('span', { style: { fontSize: 22, fontWeight: 600, color: C.t1, fontVariantNumeric: 'tabular-nums' } }, cost || '—'),
|
|
342
|
+
h('span', { style: { fontSize: 11, color: C.t3 } },
|
|
343
|
+
(totals.calls || 0) + ' 次调用 · ' + (totals.steps || 0) + ' 步'),
|
|
344
|
+
),
|
|
345
|
+
totals.cost && totals.cost.unknownEntries > 0
|
|
346
|
+
? h('div', { style: { marginTop: 4, fontSize: 11, color: C.amber } },
|
|
347
|
+
'其中 ' + totals.cost.unknownEntries + ' 条费率未知,未计入合计')
|
|
348
|
+
: null,
|
|
349
|
+
),
|
|
350
|
+
h('div', { style: { padding: '0 14px 10px' } },
|
|
351
|
+
h('div', {
|
|
352
|
+
style: {
|
|
353
|
+
display: 'flex', alignItems: 'center', gap: 6, padding: '8px 0 2px',
|
|
354
|
+
borderTop: '1px solid ' + C.hair, fontSize: 12, fontWeight: 600, color: C.t1,
|
|
355
|
+
},
|
|
356
|
+
}, T.secTool, h('span', { style: { marginLeft: 'auto', fontSize: 11, fontWeight: 400, color: C.t3 } }, '成本占比')),
|
|
357
|
+
h(BarList, { list: data.byTool }),
|
|
358
|
+
),
|
|
359
|
+
h('div', { style: { padding: '0 14px 10px' } },
|
|
360
|
+
h('div', {
|
|
361
|
+
style: {
|
|
362
|
+
display: 'flex', alignItems: 'center', gap: 6, padding: '8px 0 2px',
|
|
363
|
+
borderTop: '1px solid ' + C.hair, fontSize: 12, fontWeight: 600, color: C.t1,
|
|
364
|
+
},
|
|
365
|
+
}, T.secModel, h('span', { style: { marginLeft: 'auto', fontSize: 11, fontWeight: 400, color: C.t3 } }, '调用次数')),
|
|
366
|
+
h(BarList, { list: data.byModel }),
|
|
367
|
+
),
|
|
368
|
+
)
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
function CompareRow(props) {
|
|
372
|
+
const item = props.item
|
|
373
|
+
const picked = props.picked
|
|
374
|
+
const cost = money(item.cost)
|
|
375
|
+
return h('div', {
|
|
376
|
+
style: {
|
|
377
|
+
display: 'flex', alignItems: 'center', gap: 8, padding: '7px 0',
|
|
378
|
+
borderTop: '1px solid ' + C.hair, cursor: 'pointer',
|
|
379
|
+
},
|
|
380
|
+
onClick: () => props.onToggle(item.sid),
|
|
381
|
+
},
|
|
382
|
+
h('span', {
|
|
383
|
+
style: {
|
|
384
|
+
width: 14, height: 14, borderRadius: 4, flex: 'none',
|
|
385
|
+
border: '1px solid ' + (picked ? C.t1 : '#cfd6e0'),
|
|
386
|
+
background: picked ? C.t1 : '#fff',
|
|
387
|
+
color: '#fff', fontSize: 10, lineHeight: '12px', textAlign: 'center',
|
|
388
|
+
},
|
|
389
|
+
}, picked ? '✓' : ''),
|
|
390
|
+
h('span', { style: { flex: 1, minWidth: 0 } },
|
|
391
|
+
h('span', {
|
|
392
|
+
style: {
|
|
393
|
+
display: 'block', fontSize: 12, color: C.t1,
|
|
394
|
+
overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap',
|
|
395
|
+
},
|
|
396
|
+
}, item.title || item.sid.slice(0, 12)),
|
|
397
|
+
h('span', { style: { display: 'block', fontSize: 10.5, color: C.t3 } },
|
|
398
|
+
(item.closed ? '已收场' : '进行中') + ' · ' + clockOf(item.lastAt) + (item.steps ? ' · ' + item.steps + ' 步' : '')),
|
|
399
|
+
),
|
|
400
|
+
h('span', { style: { fontSize: 12, color: C.t1, whiteSpace: 'nowrap', fontVariantNumeric: 'tabular-nums' } }, cost || '—'),
|
|
401
|
+
)
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
function Compare(props) {
|
|
405
|
+
const [picked, setPicked] = useState([])
|
|
406
|
+
const [result, setResult] = useState(null)
|
|
407
|
+
const [error, setError] = useState(null)
|
|
408
|
+
const sessions = props.sessions || []
|
|
409
|
+
|
|
410
|
+
const toggle = useCallback((sid) => {
|
|
411
|
+
setResult(null)
|
|
412
|
+
setError(null)
|
|
413
|
+
setPicked((current) => {
|
|
414
|
+
if (current.includes(sid)) return current.filter((id) => id !== sid)
|
|
415
|
+
const next = current.concat(sid)
|
|
416
|
+
return next.length > 2 ? next.slice(-2) : next
|
|
417
|
+
})
|
|
418
|
+
}, [])
|
|
419
|
+
|
|
420
|
+
useEffect(() => {
|
|
421
|
+
if (picked.length !== 2) return undefined
|
|
422
|
+
const controller = new AbortController()
|
|
423
|
+
getJson('compare=' + encodeURIComponent(picked[0]) + ',' + encodeURIComponent(picked[1]), controller.signal)
|
|
424
|
+
.then((data) => setResult(data))
|
|
425
|
+
.catch((cause) => {
|
|
426
|
+
if (cause && cause.name === 'AbortError') return
|
|
427
|
+
setError(cause.message || '对比失败')
|
|
428
|
+
})
|
|
429
|
+
return () => controller.abort()
|
|
430
|
+
}, [picked])
|
|
431
|
+
|
|
432
|
+
const diffText = (value, unit) => {
|
|
433
|
+
if (value === null || value === undefined) return '—'
|
|
434
|
+
const sign = value > 0 ? '+' : ''
|
|
435
|
+
return sign + (unit === 'money' ? value.toFixed(4) : value) + (unit === 'money' ? '' : '')
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
return h('div', { style: { padding: '10px 14px 6px' } },
|
|
439
|
+
h('div', { style: { fontSize: 11.5, color: C.t3, marginBottom: 6 } },
|
|
440
|
+
picked.length === 2 ? '两段会话的差异(仅同工作区可作同条件对照)' : T.comparePick + ' · ' + T.compareNeed.replace('{n}', String(picked.length))),
|
|
441
|
+
sessions.slice(0, 12).map((item) => h(CompareRow, {
|
|
442
|
+
key: item.sid, item, picked: picked.includes(item.sid), onToggle: toggle,
|
|
443
|
+
})),
|
|
444
|
+
error ? h('div', { style: { marginTop: 10, fontSize: 11.5, color: C.red } }, error) : null,
|
|
445
|
+
result
|
|
446
|
+
? h('div', { style: { marginTop: 10, borderTop: '1px solid ' + C.hair, paddingTop: 8 } },
|
|
447
|
+
h('div', { style: { display: 'flex', justifyContent: 'space-between', fontSize: 11, color: C.t3 } },
|
|
448
|
+
h('span', { style: { maxWidth: 150, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' } },
|
|
449
|
+
result.a.title || result.a.sid.slice(0, 10)),
|
|
450
|
+
h('span', { style: { maxWidth: 150, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' } },
|
|
451
|
+
result.b.title || result.b.sid.slice(0, 10)),
|
|
452
|
+
),
|
|
453
|
+
h('div', { style: { display: 'flex', justifyContent: 'space-between', marginTop: 4, fontSize: 14, color: C.t1, fontVariantNumeric: 'tabular-nums' } },
|
|
454
|
+
h('span', null, money(result.a.cost) || '—'),
|
|
455
|
+
h('span', null, money(result.b.cost) || '—'),
|
|
456
|
+
),
|
|
457
|
+
result.sameWorkspace
|
|
458
|
+
? null
|
|
459
|
+
: h('div', { style: { marginTop: 6, fontSize: 10.5, color: C.amber } }, '非同工作区,仅作参考'),
|
|
460
|
+
(result.notes || []).map((note, index) => h('div', {
|
|
461
|
+
key: 'note-' + index,
|
|
462
|
+
style: {
|
|
463
|
+
marginTop: 8, padding: '7px 9px', background: '#f6f8fb',
|
|
464
|
+
borderLeft: '2px solid #9fb0c9', borderRadius: '0 8px 8px 0',
|
|
465
|
+
fontSize: 11, lineHeight: 1.7, color: C.t2,
|
|
466
|
+
},
|
|
467
|
+
}, h('b', { style: { color: C.t1 } }, String(index + 1)), ' ', note.text)),
|
|
468
|
+
)
|
|
469
|
+
: null,
|
|
470
|
+
)
|
|
471
|
+
}
|
|
472
|
+
// #endregion
|
|
473
|
+
|
|
474
|
+
// #region 面板
|
|
475
|
+
function Panel(props) {
|
|
476
|
+
const [sessions, setSessions] = useState([])
|
|
477
|
+
const [sid, setSid] = useState(() => pickSessionId(props))
|
|
478
|
+
const [mode, setMode] = useState('timeline')
|
|
479
|
+
const [filter, setFilter] = useState('all')
|
|
480
|
+
const [timeline, setTimeline] = useState(null)
|
|
481
|
+
const [hotspot, setHotspot] = useState(null)
|
|
482
|
+
const [error, setError] = useState(null)
|
|
483
|
+
const [loading, setLoading] = useState(true)
|
|
484
|
+
const [tick, setTick] = useState(0)
|
|
485
|
+
const alive = useRef(true)
|
|
486
|
+
|
|
487
|
+
useEffect(() => () => { alive.current = false }, [])
|
|
488
|
+
|
|
489
|
+
// 会话列表:用于兜底选一个会话,也给对比页当候选
|
|
490
|
+
useEffect(() => {
|
|
491
|
+
const controller = new AbortController()
|
|
492
|
+
getJson('sessions=1&limit=30', controller.signal)
|
|
493
|
+
.then((data) => {
|
|
494
|
+
if (!alive.current) return
|
|
495
|
+
setSessions(data.sessions || [])
|
|
496
|
+
setSid((current) => current || (data.sessions && data.sessions[0] && data.sessions[0].sid) || null)
|
|
497
|
+
})
|
|
498
|
+
.catch((cause) => {
|
|
499
|
+
if (cause && cause.name === 'AbortError') return
|
|
500
|
+
if (alive.current) setError(cause.message || '会话列表读取失败')
|
|
501
|
+
})
|
|
502
|
+
return () => controller.abort()
|
|
503
|
+
}, [tick])
|
|
504
|
+
|
|
505
|
+
// 时间轴 / 热点:按当前会话拉取
|
|
506
|
+
useEffect(() => {
|
|
507
|
+
if (!sid || mode === 'compare') {
|
|
508
|
+
setLoading(false)
|
|
509
|
+
return undefined
|
|
510
|
+
}
|
|
511
|
+
const controller = new AbortController()
|
|
512
|
+
setLoading(true)
|
|
513
|
+
setError(null)
|
|
514
|
+
const query = mode === 'hotspot'
|
|
515
|
+
? 'hotspot=' + encodeURIComponent(sid)
|
|
516
|
+
: 'replay=' + encodeURIComponent(sid) + '&limit=200'
|
|
517
|
+
getJson(query, controller.signal)
|
|
518
|
+
.then((data) => {
|
|
519
|
+
if (!alive.current) return
|
|
520
|
+
if (mode === 'hotspot') setHotspot(data)
|
|
521
|
+
else setTimeline(data)
|
|
522
|
+
setLoading(false)
|
|
523
|
+
})
|
|
524
|
+
.catch((cause) => {
|
|
525
|
+
if (cause && cause.name === 'AbortError') return
|
|
526
|
+
if (!alive.current) return
|
|
527
|
+
setError(cause.message || '回放读取失败')
|
|
528
|
+
setLoading(false)
|
|
529
|
+
})
|
|
530
|
+
return () => controller.abort()
|
|
531
|
+
}, [sid, mode, tick])
|
|
532
|
+
|
|
533
|
+
const current = sessions.find((item) => item.sid === sid) || null
|
|
534
|
+
const titleOf = (current && (current.title || current.cwdName)) || (sid ? sid.slice(0, 14) : '—')
|
|
535
|
+
|
|
536
|
+
const summaryRows = timeline && timeline.rows ? timeline.rows : []
|
|
537
|
+
const sumCost = useMemo(() => {
|
|
538
|
+
if (timeline && timeline.sum && timeline.sum.cost) return timeline.sum.cost
|
|
539
|
+
if (current && current.cost) return current.cost
|
|
540
|
+
return null
|
|
541
|
+
}, [timeline, current])
|
|
542
|
+
const sumSaved = (timeline && timeline.sum && timeline.sum.saved) || (current && current.saved) || null
|
|
543
|
+
const stepCount = (timeline && timeline.total) || (current && current.steps) || 0
|
|
544
|
+
const failCount = summaryRows.filter((row) => row.op === 'tool.call' && row.ok === false).length
|
|
545
|
+
|
|
546
|
+
const head = h('div', {
|
|
547
|
+
style: {
|
|
548
|
+
display: 'flex', alignItems: 'center', gap: 8, padding: '10px 14px 8px',
|
|
549
|
+
borderBottom: '1px solid ' + C.hair,
|
|
550
|
+
},
|
|
551
|
+
},
|
|
552
|
+
h('span', { style: { fontSize: 13, fontWeight: 600, color: C.t1, whiteSpace: 'nowrap' } }, T.title),
|
|
553
|
+
h('span', {
|
|
554
|
+
style: {
|
|
555
|
+
fontSize: 11, color: C.t4, overflow: 'hidden', textOverflow: 'ellipsis',
|
|
556
|
+
whiteSpace: 'nowrap', maxWidth: 170,
|
|
557
|
+
},
|
|
558
|
+
title: titleOf,
|
|
559
|
+
}, titleOf),
|
|
560
|
+
h('button', {
|
|
561
|
+
type: 'button',
|
|
562
|
+
onClick: () => setTick((n) => n + 1),
|
|
563
|
+
style: {
|
|
564
|
+
marginLeft: 'auto', border: '1px solid #d8dee7', background: '#fff',
|
|
565
|
+
borderRadius: 7, padding: '3px 9px', fontSize: 11.5, color: C.t2, cursor: 'pointer',
|
|
566
|
+
},
|
|
567
|
+
}, T.refresh),
|
|
568
|
+
)
|
|
569
|
+
|
|
570
|
+
const tabs = h('div', { style: { display: 'flex', gap: 6, padding: '8px 14px 6px' } },
|
|
571
|
+
Chip({ label: T.tabTimeline, on: mode === 'timeline', onClick: () => setMode('timeline') }),
|
|
572
|
+
Chip({ label: T.tabHotspot, on: mode === 'hotspot', onClick: () => setMode('hotspot') }),
|
|
573
|
+
Chip({ label: T.tabCompare, on: mode === 'compare', onClick: () => setMode('compare') }),
|
|
574
|
+
)
|
|
575
|
+
|
|
576
|
+
let body
|
|
577
|
+
if (mode === 'compare') {
|
|
578
|
+
body = h(Compare, { sessions })
|
|
579
|
+
} else if (loading && !timeline && !hotspot) {
|
|
580
|
+
body = h('div', { style: { padding: '16px 14px', fontSize: 11.5, color: C.t3 } }, T.loading)
|
|
581
|
+
} else if (error) {
|
|
582
|
+
body = h(Empty, { title: T.errTitle, body: error, hint: '若刚装好,先确认 profile 已启用本插件;文件在 `~/.dsh/ops/replay`。' })
|
|
583
|
+
} else if (mode === 'timeline') {
|
|
584
|
+
body = h('div', null,
|
|
585
|
+
h('div', { style: { padding: '10px 14px 0' } },
|
|
586
|
+
h('div', { style: { display: 'flex', alignItems: 'baseline', gap: 8, flexWrap: 'wrap' } },
|
|
587
|
+
h('span', { style: { fontSize: 22, fontWeight: 600, color: C.t1, fontVariantNumeric: 'tabular-nums' } },
|
|
588
|
+
money(sumCost) || '—'),
|
|
589
|
+
h('span', { style: { fontSize: 11, color: C.t3 } }, stepCount + ' 步' + (failCount > 0 ? ' · ' + failCount + ' 次失败' : '')),
|
|
590
|
+
),
|
|
591
|
+
sumSaved && Number.isFinite(sumSaved.amount) && sumSaved.amount > 0
|
|
592
|
+
? h('div', { style: { marginTop: 4, fontSize: 11, color: C.green, fontVariantNumeric: 'tabular-nums' } },
|
|
593
|
+
'缓存已省 ' + money(sumSaved))
|
|
594
|
+
: null,
|
|
595
|
+
timeline && timeline.sum && timeline.sum.cost && timeline.sum.cost.unknownEntries > 0
|
|
596
|
+
? h('div', { style: { marginTop: 4, fontSize: 11, color: C.amber } },
|
|
597
|
+
timeline.sum.cost.unknownEntries + ' 条费率未知,合计偏低')
|
|
598
|
+
: null,
|
|
599
|
+
),
|
|
600
|
+
h('div', { style: { display: 'flex', gap: 6, padding: '8px 14px 6px' } },
|
|
601
|
+
Chip({ label: T.filterAll, on: filter === 'all', onClick: () => setFilter('all') }),
|
|
602
|
+
Chip({ label: T.filterCostly, on: filter === 'costly', onClick: () => setFilter('costly') }),
|
|
603
|
+
Chip({ label: failCount > 0 ? T.filterFailed + ' ' + failCount : T.filterFailed, on: filter === 'failed', onClick: () => setFilter('failed') }),
|
|
604
|
+
),
|
|
605
|
+
timeline && timeline.truncated
|
|
606
|
+
? h('div', { style: { margin: '0 14px 6px', fontSize: 10.5, color: C.amber } }, '该会话超过记录上限,早期明细已折叠')
|
|
607
|
+
: null,
|
|
608
|
+
h(Timeline, { rows: summaryRows, filter }),
|
|
609
|
+
)
|
|
610
|
+
} else {
|
|
611
|
+
body = h(Hotspot, { data: hotspot })
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
const foot = h('div', {
|
|
615
|
+
style: { padding: '8px 14px 12px', fontSize: 10.5, lineHeight: 1.65, color: C.t4 },
|
|
616
|
+
}, T.privacy, h('br'), '归因只有 exact / none 两档,拿不到确切来源就写「未归因」')
|
|
617
|
+
|
|
618
|
+
return h('div', { style: { background: C.bg, display: 'flex', flexDirection: 'column', minHeight: 0 } }, head, tabs, body, foot)
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
function makeTitle() {
|
|
622
|
+
return function Title() {
|
|
623
|
+
return h('span', { style: { fontSize: 12, fontWeight: 600, color: C.t1 } }, T.title)
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
// #endregion
|
|
627
|
+
|
|
628
|
+
function openPanel(ctx) {
|
|
629
|
+
try {
|
|
630
|
+
if (ctx && typeof ctx.openTab === 'function') ctx.openTab(KIND)
|
|
631
|
+
} catch {
|
|
632
|
+
/* 打不开 tab 不算错误:用户还能从右栏点 */
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
function apply(ctx) {
|
|
637
|
+
const tabs = ctx.get ? ctx.get('sidebarRightTabs') : ctx.sidebarRightTabs
|
|
638
|
+
const slots = ctx.get ? ctx.get('slots') : ctx.slots
|
|
639
|
+
let disposers = []
|
|
640
|
+
|
|
641
|
+
try {
|
|
642
|
+
if (tabs && typeof tabs.register === 'function') {
|
|
643
|
+
disposers.push(tabs.register({
|
|
644
|
+
id: ID,
|
|
645
|
+
kind: KIND,
|
|
646
|
+
title: () => T.title,
|
|
647
|
+
priority: 55,
|
|
648
|
+
guide: { description: T.guideDesc },
|
|
649
|
+
}))
|
|
650
|
+
}
|
|
651
|
+
} catch (error) {
|
|
652
|
+
console.warn('[replay] tab 登记失败:', error)
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
try {
|
|
656
|
+
if (slots && typeof slots.register === 'function') {
|
|
657
|
+
disposers.push(slots.register({ name: 'sidebar.right.pane.tab', key: ID }, Panel))
|
|
658
|
+
disposers.push(slots.register({ name: 'sidebar.right.pane.tab.title', key: ID }, makeTitle()))
|
|
659
|
+
}
|
|
660
|
+
} catch (error) {
|
|
661
|
+
console.warn('[replay] 面板挂载失败:', error)
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
const cleanup = () => {
|
|
665
|
+
for (const dispose of disposers) {
|
|
666
|
+
try {
|
|
667
|
+
dispose && dispose()
|
|
668
|
+
} catch {}
|
|
669
|
+
}
|
|
670
|
+
disposers = []
|
|
671
|
+
}
|
|
672
|
+
if (typeof ctx.effect === 'function') ctx.effect(() => cleanup)
|
|
673
|
+
else if (typeof ctx.on === 'function') ctx.on('dispose', cleanup)
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
return {
|
|
677
|
+
inject: ['sidebarRightTabs'],
|
|
678
|
+
apply,
|
|
679
|
+
__test: { money, tokens, duration, clockOf, coordsOf, opLabel, measText, pickSessionId },
|
|
680
|
+
}
|
|
681
|
+
},
|
|
682
|
+
})
|