@cheemsup/dsh-plugin-console 0.1.1 → 0.2.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.
@@ -0,0 +1,904 @@
1
+ /**
2
+ * 「插件禁用预演」页:被禁用的插件、连锁反应、依赖关系图、配置与覆盖。
3
+ *
4
+ * 页面正文只回答两个问题:被禁用的是谁;禁用后失去了哪些工具与提示词(含下游连带贡献),
5
+ * 以及哪些下游插件会跟着不可用。依赖图与配置分层收在后面的独立区块里。
6
+ *
7
+ * 数据只来自两处:快照(插件、服务、工具、会话)与 `disable-preview` 路由
8
+ * (级联影响 + 逐层 config + 诊断)。页面本身**不执行任何写操作**,也不猜测
9
+ * 合并语义——「生效配置里没有」与「值与某层不同」是事实,原因属于推断。
10
+ *
11
+ * 关系图用固定几何的树布局(`client.impact-tree.js`)。图下不再附等价的
12
+ * 文字明细:下游插件已由「受影响的下游插件」逐行给出,再列一遍只是同源
13
+ * 信息的第二次搬运。
14
+ * @module dsh-plugin-console/client-disable-preview
15
+ */
16
+
17
+ window.__ModuleLoader__.load({
18
+ id: '@cheemsup/dsh-plugin-console',
19
+ chunk: 'client.disable-preview.js',
20
+ factory: (require) => {
21
+ const React = require('react')
22
+ const { createElement: h, Fragment, useCallback, useRef, useState } = React
23
+
24
+ return function create(deps) {
25
+ const {
26
+ DISABLE_PREVIEW_ROUTE,
27
+ fact,
28
+ fetchJson,
29
+ formatSessionId,
30
+ relationGraph,
31
+ rowEnd,
32
+ search,
33
+ shortName,
34
+ textButton,
35
+ } = deps
36
+
37
+ /** 证据级别 → 界面说法;与 README 的「数据可信度」保持同一套词。 */
38
+ const EVIDENCE_LABELS = {
39
+ runtime: '运行时事实',
40
+ file: '文件事实',
41
+ source: '源码扫描',
42
+ inference: '推断',
43
+ unknown: '未知',
44
+ }
45
+
46
+ /** 诊断的严重度 → 标签文案。 */
47
+ const SEVERITY_LABELS = { warn: '警告', info: '提示' }
48
+
49
+ /** 因果路径的读法;起点、服务、终点各是链条里的哪种节点。 */
50
+ const PATH_HINT = '影响传导链:起点 → 服务 → 节点1 → 服务 → 节点2 → … → 终点'
51
+
52
+ /** 服务影响标题:每行是什么,以及声明值的边界。 */
53
+ const SERVICE_IMPACT_HINT = '条目:“服务名 提供者 · 声明依赖该服务的插件数”。依赖来自 fiber.inject 声明,包含已禁用与已 pending 的条目,不等于实际会停的数量'
54
+
55
+ /** 逐层 config 的读法:列的是每层声明值与读取状态,不是合并结果。 */
56
+ const CONFIG_LAYERS_HINT = '按覆盖链顺序显示每一层写出的 config 内容'
57
+
58
+ /** 一个状态标签;`tone` 走 `.ptTag` 的四档。 */
59
+ function tag(text, tone, title) {
60
+ return h('span', { className: 'ptTag', 'data-tone': tone, title }, text)
61
+ }
62
+
63
+ /** 顶层区块下的子块:左侧竖线 + 降一级标题,避免把每个子问题都升成顶级标题。 */
64
+ function subBlock(title, children, hint) {
65
+ return h(
66
+ 'div',
67
+ { className: 'ptSubBlocks' },
68
+ h('p', { className: 'ptSubTitle' }, hint === undefined ? title : h('span', { title: hint }, title)),
69
+ children,
70
+ )
71
+ }
72
+
73
+ /**
74
+ * 一级标题 + 降一级内容。
75
+ *
76
+ * 与 `subBlock` 的区别是标题不缩进、字号与顶层区块一致;用于区块内部
77
+ * 还带两层结构的答案(如「会失去的提示词与消息内容」)。
78
+ */
79
+ function firstLevelBlock(title, children) {
80
+ return h(
81
+ 'div',
82
+ { className: 'ptSection' },
83
+ h('p', { className: 'ptSectionTitle' }, title),
84
+ h('div', { className: 'ptSubBlocks' }, children),
85
+ )
86
+ }
87
+
88
+ /**
89
+ * 禁用预演的状态。
90
+ *
91
+ * 每次请求带一个 ticket:快速切换目标时只有最后一次响应能落进状态,
92
+ * 避免旧目标的影响面盖住新目标。失败不自动重试,交给页面上的「重试」。
93
+ * @returns {object} `{rowId, data, status, error, choose, reload}`。
94
+ */
95
+ function useDisablePreviewStore() {
96
+ const [rowId, setRowId] = useState(null)
97
+ const [data, setData] = useState(null)
98
+ const [status, setStatus] = useState('idle')
99
+ const [error, setError] = useState(null)
100
+ // 制图区两种视图:默认看禁用影响,切换后看以目标为中心的完整依赖链。
101
+ const [graphMode, setGraphMode] = useState('impact')
102
+ // 图缩放只影响显示尺寸,不重新布局;切换目标或视图时保留当前比例。
103
+ const [graphZoom, setGraphZoom] = useState(1)
104
+ // 插件选择器:展开时输入框用于筛选,关闭时显示当前选择。
105
+ const [targetQuery, setTargetQuery] = useState('')
106
+ const [targetOpen, setTargetOpen] = useState(false)
107
+ const [targetIndex, setTargetIndex] = useState(-1)
108
+ const ticket = useRef(0)
109
+ const current = useRef(null)
110
+
111
+ const load = useCallback(async (target) => {
112
+ const id = typeof target === 'string' ? target : current.current
113
+ if (typeof id !== 'string' || id === '') return
114
+ current.current = id
115
+ const mine = ++ticket.current
116
+ setStatus('loading')
117
+ setError(null)
118
+ try {
119
+ const encoded = typeof encodeURIComponent === 'function' ? encodeURIComponent(id) : id
120
+ const next = await fetchJson(DISABLE_PREVIEW_ROUTE + '?id=' + encoded)
121
+ if (mine !== ticket.current) return
122
+ setData(next)
123
+ setStatus('ready')
124
+ } catch (cause) {
125
+ if (mine !== ticket.current) return
126
+ setError(cause instanceof Error ? cause.message : String(cause))
127
+ setStatus('error')
128
+ }
129
+ }, [fetchJson])
130
+
131
+ /** 选择目标;传空表示回到未选择状态。 */
132
+ const choose = useCallback((id) => {
133
+ if (typeof id !== 'string' || id === '') {
134
+ current.current = null
135
+ ticket.current++
136
+ setRowId(null)
137
+ setData(null)
138
+ setStatus('idle')
139
+ setError(null)
140
+ return
141
+ }
142
+ current.current = id
143
+ setRowId(id)
144
+ setData(null)
145
+ void load(id)
146
+ }, [load])
147
+
148
+ const reload = useCallback(() => {
149
+ void load(current.current)
150
+ }, [load])
151
+
152
+ return {
153
+ rowId,
154
+ data,
155
+ status,
156
+ error,
157
+ choose,
158
+ reload,
159
+ graphMode,
160
+ setGraphMode,
161
+ graphZoom,
162
+ setGraphZoom,
163
+ targetPicker: {
164
+ query: targetQuery,
165
+ open: targetOpen,
166
+ index: targetIndex,
167
+ setQuery: setTargetQuery,
168
+ setOpen: setTargetOpen,
169
+ setIndex: setTargetIndex,
170
+ },
171
+ }
172
+ }
173
+
174
+ /** 可输入选择器在缺省状态下的空实现:视图被单独渲染(自检)时也能工作。 */
175
+ const EMPTY_TARGET_PICKER = {
176
+ query: '',
177
+ open: false,
178
+ index: -1,
179
+ setQuery: () => {},
180
+ setOpen: () => {},
181
+ setIndex: () => {},
182
+ }
183
+
184
+ /**
185
+ * 页头工具条:可输入的插件选择器 + 重新加载。
186
+ *
187
+ * 输入框同时是列表的筛选条件:聚焦时展开完整列表,输入即过滤;
188
+ * 关闭时输入框显示当前选中的插件名。
189
+ * @param {object} vc - 视图上下文。
190
+ * @returns {object} React 节点。
191
+ */
192
+ function renderDisablePreviewFilters(vc) {
193
+ const { entries, disablePreview } = vc
194
+ const picker = disablePreview.targetPicker ?? EMPTY_TARGET_PICKER
195
+ const chosen = disablePreview.rowId ?? ''
196
+ const all = (entries ?? [])
197
+ .slice()
198
+ .sort((left, right) => shortName(left).localeCompare(shortName(right), 'zh-CN'))
199
+ .map((entry) => ({
200
+ value: entry.rowId,
201
+ label: shortName(entry) + (entry.isGroup ? '(分组)' : entry.modeOnly ? '(模式专属)' : ''),
202
+ search: `${shortName(entry)} ${entry.rowId ?? ''} ${entry.moduleName ?? ''}`.toLowerCase(),
203
+ }))
204
+ const needle = picker.query.trim().toLowerCase()
205
+ const items = needle === ''
206
+ ? [{ value: '', label: '选择插件…', search: '' }, ...all]
207
+ : all.filter((item) => item.search.includes(needle))
208
+ const selected = all.find((item) => item.value === chosen)
209
+ const active = items.length === 0 ? -1 : Math.min(picker.index, items.length - 1)
210
+ /** 收起列表并清空筛选词;输入框随后显示当前选中项。 */
211
+ const close = () => {
212
+ picker.setOpen(false)
213
+ picker.setQuery('')
214
+ picker.setIndex(-1)
215
+ }
216
+ const choose = (value) => {
217
+ close()
218
+ disablePreview.choose(value)
219
+ }
220
+ const onKeyDown = (event) => {
221
+ if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
222
+ event.preventDefault()
223
+ picker.setOpen(true)
224
+ if (items.length === 0) {
225
+ picker.setIndex(-1)
226
+ return
227
+ }
228
+ const step = event.key === 'ArrowDown' ? 1 : -1
229
+ const start = picker.index
230
+ picker.setIndex(start < 0
231
+ ? (step > 0 ? 0 : items.length - 1)
232
+ : (start + step + items.length) % items.length)
233
+ } else if (event.key === 'Enter' && picker.open === true && active >= 0) {
234
+ event.preventDefault()
235
+ choose(items[active].value)
236
+ } else if (event.key === 'Escape') {
237
+ close()
238
+ }
239
+ }
240
+ return h(
241
+ Fragment,
242
+ null,
243
+ h(
244
+ 'div',
245
+ { className: 'ptField' },
246
+ h('span', { className: 'ptFieldLabel' }, '选择禁用插件'),
247
+ h(
248
+ 'div',
249
+ { className: 'ptCombo' },
250
+ h('input', {
251
+ type: 'text',
252
+ className: 'ptSelect ptComboInput',
253
+ placeholder: '选择插件…',
254
+ value: picker.open === true ? picker.query : (selected?.label ?? ''),
255
+ role: 'combobox',
256
+ 'aria-label': '选择禁用插件',
257
+ 'aria-expanded': picker.open === true,
258
+ 'aria-controls': 'pt-disable-target-options',
259
+ 'aria-autocomplete': 'list',
260
+ 'aria-activedescendant': picker.open === true && active >= 0 ? `pt-disable-target-option-${active}` : undefined,
261
+ onFocus: () => {
262
+ picker.setOpen(true)
263
+ picker.setQuery('')
264
+ picker.setIndex(-1)
265
+ },
266
+ onChange: (event) => {
267
+ picker.setOpen(true)
268
+ picker.setQuery(event.target.value)
269
+ picker.setIndex(0)
270
+ },
271
+ // 已经聚焦时再点一次也要能重新展开(例如刚按过 Esc)。
272
+ onClick: () => {
273
+ if (picker.open === true) return
274
+ picker.setOpen(true)
275
+ picker.setQuery('')
276
+ picker.setIndex(-1)
277
+ },
278
+ onKeyDown,
279
+ onBlur: () => picker.setOpen(false),
280
+ }),
281
+ picker.open === true
282
+ ? h(
283
+ 'div',
284
+ { id: 'pt-disable-target-options', className: 'ptComboList', role: 'listbox' },
285
+ items.length === 0
286
+ ? h('p', { className: 'ptComboEmpty' }, '没有匹配的插件')
287
+ : items.map((item, index) => h(
288
+ 'button',
289
+ {
290
+ key: item.value === '' ? '__none' : item.value,
291
+ id: `pt-disable-target-option-${index}`,
292
+ type: 'button',
293
+ className: 'ptComboOption',
294
+ role: 'option',
295
+ 'data-active': index === active,
296
+ 'aria-selected': item.value === chosen,
297
+ // 按下时不抢输入框焦点,click 才能正常命中。
298
+ onMouseDown: (event) => event.preventDefault(),
299
+ onClick: () => choose(item.value),
300
+ },
301
+ item.label,
302
+ )),
303
+ )
304
+ : null,
305
+ ),
306
+ ),
307
+ textButton('重新加载', () => disablePreview.reload(), {
308
+ disabled: disablePreview.rowId === null || disablePreview.status === 'loading',
309
+ title: '按当前快照重新计算',
310
+ }),
311
+ )
312
+ }
313
+
314
+ /** 「连锁反应」的摘要:只回答失去多少、波及谁,明细放在下面的子块里。 */
315
+ function renderReactionSummary(data) {
316
+ const { scale } = data
317
+ return h(
318
+ 'dl',
319
+ { className: 'ptFacts' },
320
+ fact('失去工具', scale.lostTools === 0 ? '无' : `${scale.lostTools}${scale.lostToolsViaChain > 0 ? `(其中链路 ${scale.lostToolsViaChain})` : ''}`),
321
+ fact('失去提示词与上下文', scale.lostSections + scale.lostContexts === 0 ? '无' : `段 ${scale.lostSections} · 上下文 ${scale.lostContexts}`),
322
+ fact('受影响的下游插件', scale.blockedEntries === 0 ? '无' : `${scale.blockedEntries} 个 · 最深第 ${scale.chainDepth} 层`),
323
+ fact('影响范围', `模式 ${scale.modes} · 活会话 ${scale.liveSessions} · 冷会话 ${scale.coldSessionCount}`),
324
+ fact('生效', data.hmr === true ? 'HMR 可用,可能立即生效' : '未检测到 HMR;可能需要重启'),
325
+ )
326
+ }
327
+
328
+ /**
329
+ * 一组名字:失去 / 共享 / 未归属 / 不在全局注册表各一段。
330
+ *
331
+ * 「不在全局注册表」与「未归属」不是一回事:前者是名字在源码里扫得到、
332
+ * 当前全局注册表却没有(多为会话内或条件注册);后者是注册表里有这一项、
333
+ * 但扫描不到 owner。两者都不并入「失去」,但原因必须分开写。
334
+ */
335
+ function renderNameGroup(title, bucket, options) {
336
+ const rows = bucket.rows ?? []
337
+ const query = options.query ?? ''
338
+ // 搜索时没有命中的分组整块省略:留一行「没有注册项」只会把命中项推远。
339
+ if (rows.length === 0 && search.normalizeQuery(query) !== '') return null
340
+ const allSections = [
341
+ { key: 'lost', label: '失去' },
342
+ { key: 'shared', label: '共享' },
343
+ { key: 'unknown', label: '未归属' },
344
+ { key: 'registryMissing', label: '不在全局注册表' },
345
+ ]
346
+ // 没有这一类行时不显示计数,避免每个分组都多出一段「… 0」。
347
+ const sections = allSections.filter((group) => (
348
+ group.key !== 'registryMissing' || rows.some((row) => row.registryMissing === true)
349
+ ))
350
+ const counts = sections.map((group) => rows.filter((row) => row[group.key] === true).length)
351
+ const nodes = []
352
+ for (const group of sections) {
353
+ const list = rows.filter((row) => row[group.key] === true)
354
+ if (list.length === 0) continue
355
+ nodes.push(
356
+ h('p', { key: group.key, className: 'ptSub', style: { margin: 0 } }, `${group.label}(${list.length})`),
357
+ h(
358
+ 'ul',
359
+ { key: group.key + ':list', className: 'ptDepList' },
360
+ list.map((row) => h(
361
+ 'li',
362
+ { key: group.key + ':' + row.name },
363
+ options.onOpen === undefined
364
+ ? h('span', { className: 'ptDepKey' }, search.markQuery(row.name, query))
365
+ : h(
366
+ 'button',
367
+ {
368
+ type: 'button',
369
+ className: 'ptTag ptLink',
370
+ title: options.openTitle,
371
+ onClick: () => options.onOpen(row.name),
372
+ },
373
+ search.markQuery(row.name, query),
374
+ ),
375
+ options.extra === undefined ? null : options.extra(row),
376
+ rowEnd(options.end === undefined ? null : options.end(row)),
377
+ )),
378
+ ),
379
+ )
380
+ }
381
+ const summary = sections.map((group, index) => `${group.label} ${counts[index]}`).join(' · ')
382
+ return h(
383
+ 'div',
384
+ null,
385
+ h('p', { className: 'ptSub', style: { margin: 0 } }, `${title}(${summary})`),
386
+ nodes.length === 0
387
+ ? h('p', { className: 'ptSub', style: { margin: 0 } }, '没有注册项')
388
+ : nodes,
389
+ )
390
+ }
391
+
392
+ /** 覆盖链:按真实应用顺序列出层,并可展开逐层 config。 */
393
+ function renderLayersBody(config) {
394
+ if (config?.available !== true) {
395
+ return h('p', { className: 'ptSub', style: { margin: 0 } }, config?.reason ?? '配置分层不可用')
396
+ }
397
+ return h(
398
+ Fragment,
399
+ null,
400
+ h('p', { className: 'ptSub', style: { margin: 0 } }, `共 ${config.layers.length} 层,按 bundle → profile → home → 启动覆盖 → 生效排列`),
401
+ h(
402
+ 'div',
403
+ { className: 'ptFlow' },
404
+ config.layers.map((layer) => h(
405
+ 'div',
406
+ { key: layer.layerId, className: 'ptFlowStep', 'data-tone': layer.readable === true ? undefined : 'muted' },
407
+ h('span', { className: 'ptFlowKey' }, layer.kind === 'effective' ? '生效' : layer.kind),
408
+ h('span', { className: 'ptFlowValue' }, layer.label + (layer.version ? `@${layer.version}` : '')),
409
+ layer.readable === true ? null : tag('不可读', 'warn', layer.reason),
410
+ rowEnd((layer.configKeys ?? []).length === 0 ? null : `config ${layer.configKeys.length}`),
411
+ )),
412
+ ),
413
+ h(
414
+ 'details',
415
+ { className: 'ptFold ptFoldable' },
416
+ h(
417
+ 'summary',
418
+ null,
419
+ h('span', { title: CONFIG_LAYERS_HINT }, '逐层 config'),
420
+ rowEnd(`${config.layers.filter((layer) => layer.readable === true).length} 层可读`),
421
+ ),
422
+ h(
423
+ 'div',
424
+ { className: 'ptConfigLayers' },
425
+ config.layers.map((layer) => h(
426
+ 'div',
427
+ { key: layer.layerId, className: 'ptConfigLayer' },
428
+ h(
429
+ 'p',
430
+ { className: 'ptSubTitle ptConfigLayerHead' },
431
+ h('span', null, `${layer.label} · `),
432
+ h('span', { className: 'ptConfigKind' }, layer.kind),
433
+ ),
434
+ h(
435
+ 'div',
436
+ { className: 'ptConfigLayerBody' },
437
+ layer.readable !== true
438
+ ? h('p', { className: 'ptSub', style: { margin: 0 } }, layer.reason ?? '文件不可读')
439
+ : (layer.configKeys ?? []).length === 0
440
+ ? h('p', { className: 'ptSub', style: { margin: 0 } }, layer.note ?? '这一层没有写出 config 键')
441
+ : h(
442
+ 'ul',
443
+ { className: 'ptConfigList' },
444
+ layer.configKeys.map((key) => h(
445
+ 'li',
446
+ { key },
447
+ h('code', { className: 'ptConfigKey' }, key),
448
+ h('span', { className: 'ptConfigEquals' }, '='),
449
+ h('code', { className: 'ptConfigValue' }, layer.values?.[key] ?? '(未解析)'),
450
+ )),
451
+ ),
452
+ layer.parsed === false
453
+ ? h('p', { className: 'ptSub', style: { margin: 0 } }, '未解析:' + (layer.reason ?? '不支持的 YAML 写法') + (layer.rawText ? ';原文见下' : ''))
454
+ : null,
455
+ layer.parsed === false && layer.rawText
456
+ ? h('pre', { className: 'ptConfigRaw' }, layer.rawText)
457
+ : null,
458
+ ),
459
+ )),
460
+ ),
461
+ ),
462
+ config.diagnostics.some((row) => row.kind === 'layers-truncated')
463
+ ? h('p', { className: 'ptHint', style: { margin: 0 } }, '覆盖链过长,只展示了前若干层与生效层。')
464
+ : null,
465
+ )
466
+ }
467
+
468
+ /** 以 config 键为轴,逐层对照。 */
469
+ function renderConfigKeysBody(config, matches) {
470
+ if (config?.available !== true) return null
471
+ const keys = new Set()
472
+ for (const layer of config.layers) {
473
+ for (const key of layer.configKeys ?? []) keys.add(key)
474
+ }
475
+ const visible = [...keys].filter((key) => (
476
+ matches(key)
477
+ || config.layers.some((layer) => (
478
+ (layer.configKeys ?? []).includes(key)
479
+ && (matches(layer.label) || matches(layer.values?.[key] ?? ''))
480
+ ))
481
+ ))
482
+ if (keys.size === 0 || visible.length === 0) return null
483
+ const effective = config.layers.find((layer) => layer.kind === 'effective')
484
+ return h(
485
+ Fragment,
486
+ null,
487
+ h('p', { className: 'ptSub', style: { margin: 0 } }, `共 ${keys.size} 个键,当前显示 ${visible.length} 个`),
488
+ visible.sort().map((key) => {
489
+ const declaring = config.layers
490
+ .filter((layer) => layer.kind !== 'effective' && layer.readable === true && (layer.configKeys ?? []).includes(key))
491
+ .map((layer) => ({ layer, value: layer.values?.[key] }))
492
+ const diagnostics = config.diagnostics.filter((row) => row.key === key)
493
+ return h(
494
+ 'details',
495
+ { key, className: 'ptFold ptFoldable' },
496
+ h(
497
+ 'summary',
498
+ null,
499
+ h('code', { className: 'ptConfigKey' }, key),
500
+ diagnostics.some((row) => row.severity === 'warn') ? tag('差异', 'warn') : null,
501
+ rowEnd(`${declaring.length} 层声明`),
502
+ ),
503
+ h(
504
+ 'ul',
505
+ { className: 'ptDepList' },
506
+ declaring.map((item) => h(
507
+ 'li',
508
+ { key: item.layer.layerId },
509
+ h('span', { className: 'ptDepKey' }, item.layer.label),
510
+ h('code', { className: 'ptConfigValue' }, item.value ?? '(未解析)'),
511
+ )),
512
+ h(
513
+ 'li',
514
+ { key: 'effective' },
515
+ h('span', { className: 'ptDepKey' }, '生效(运行时)'),
516
+ effective?.configKeys?.includes(key) === true
517
+ ? h('code', { className: 'ptConfigValue' }, effective.values?.[key] ?? '')
518
+ : h('span', { className: 'ptDepValue ptMissing' }, '(生效配置里没有这个键)'),
519
+ ),
520
+ ),
521
+ diagnostics.map((row) => h('p', { key: row.id, className: row.severity === 'warn' ? 'ptError' : 'ptSub', style: { margin: 0 } }, row.message)),
522
+ )
523
+ }),
524
+ )
525
+ }
526
+
527
+ /** 诊断列表;调用方只在有诊断时渲染这个子块。 */
528
+ function renderDiagnosticsBody(diagnostics) {
529
+ return h(
530
+ 'ul',
531
+ { className: 'ptDepList' },
532
+ diagnostics.map((row) => h(
533
+ 'li',
534
+ { key: row.id },
535
+ tag(SEVERITY_LABELS[row.severity] ?? row.severity, row.severity === 'warn' ? 'warn' : undefined),
536
+ h('span', { className: 'ptDepValue' }, row.message),
537
+ rowEnd(EVIDENCE_LABELS[row.evidence] ?? row.evidence),
538
+ )),
539
+ )
540
+ }
541
+
542
+ /** 受影响的下游插件与完整因果路径。 */
543
+ function renderChainBody(chain, vc) {
544
+ const rows = chain?.blocked ?? []
545
+ const openPlugin = typeof vc.gotoPlugin === 'function' ? vc.gotoPlugin : () => {}
546
+ const searching = search.normalizeQuery(vc.query) !== ''
547
+ if (rows.length === 0) {
548
+ return h('p', { className: 'ptSub', style: { margin: 0 } }, searching
549
+ ? '没有匹配的下游插件'
550
+ : '没有其他插件会因为这次禁用而失去必需服务')
551
+ }
552
+ return h(
553
+ Fragment,
554
+ null,
555
+ h(
556
+ 'ul',
557
+ { className: 'ptDepList' },
558
+ rows.map((row) => h(
559
+ 'li',
560
+ { key: row.entryId },
561
+ h(
562
+ 'button',
563
+ {
564
+ type: 'button',
565
+ className: 'ptTag ptLink',
566
+ title: '在插件页展开 ' + row.title,
567
+ onClick: () => openPlugin(row.rowId),
568
+ },
569
+ search.markQuery(row.title, vc.query),
570
+ ),
571
+ tag(row.alreadyPending ? '已 pending' : '将不可用', 'warn'),
572
+ h(
573
+ 'span',
574
+ { className: 'ptDepValue', title: PATH_HINT },
575
+ row.path.map((part) => (part.kind === 'service' ? `〔${part.name}〕` : part.title)).join(' → '),
576
+ ),
577
+ rowEnd(`第 ${row.distance} 层`),
578
+ )),
579
+ ),
580
+ )
581
+ }
582
+
583
+ /**
584
+ * 服务行的提供者状态。
585
+ *
586
+ * `soleInSet` 是「全部声明提供者都不可用」,不是「只有一个提供者」;
587
+ * 唯一提供者也可能来自链路里被带停的其他插件,所以不能一律写「本插件」。
588
+ */
589
+ function serviceProviderState(row, targetRowId) {
590
+ const providers = Array.isArray(row.providers) ? row.providers : []
591
+ if (row.soleInSet === true) {
592
+ if (providers.length === 1) {
593
+ const provider = providers[0]
594
+ const isTarget = provider.rowId === targetRowId
595
+ return {
596
+ text: isTarget ? '本插件是唯一提供者' : `唯一提供者:${provider.title ?? provider.rowId ?? '未知插件'}`,
597
+ tag: isTarget ? '唯一提供者' : '链路内唯一提供',
598
+ tone: 'warn',
599
+ }
600
+ }
601
+ return { text: `提供者全部不可用(${providers.length} 个)`, tag: '提供者全失', tone: 'warn' }
602
+ }
603
+ if (providers.length === 0) return { text: '未登记提供者', tag: null, tone: undefined }
604
+ return { text: `提供者 ${providers.length} 个(仍有可用)`, tag: null, tone: undefined }
605
+ }
606
+
607
+ /** 服务影响:机制细节,默认收起,避免挤占「受影响的下游插件」正文。 */
608
+ function renderServiceDetails(services, targetRowId, query) {
609
+ const rows = (services.rows ?? []).filter((row) => row.soleInSet || (row.consumers ?? []).length > 0)
610
+ const searching = search.normalizeQuery(query) !== ''
611
+ return h(
612
+ 'details',
613
+ { className: 'ptFold ptFoldable' },
614
+ h(
615
+ 'summary',
616
+ null,
617
+ h('span', { title: SERVICE_IMPACT_HINT }, '服务影响'),
618
+ rowEnd(`提供者全失 ${services.soleProviders.length} · pending ${services.pending.length}`),
619
+ ),
620
+ rows.length === 0
621
+ ? h('p', { className: 'ptSub', style: { margin: 0 } }, searching ? '没有匹配的服务' : '这个条目没有提供任何服务')
622
+ : h(
623
+ 'ul',
624
+ { className: 'ptDepList' },
625
+ rows.map((row) => {
626
+ const state = serviceProviderState(row, targetRowId)
627
+ const consumers = (row.consumers ?? []).length
628
+ return h(
629
+ 'li',
630
+ { key: row.name },
631
+ h('span', { className: 'ptDepKey' }, search.markQuery(row.name, query)),
632
+ h('span', { className: 'ptDepValue' }, `${state.text} · 声明依赖的插件 ${consumers} 个`),
633
+ state.tag === null ? null : tag(state.tag, state.tone),
634
+ )
635
+ }),
636
+ ),
637
+ services.pending.length === 0
638
+ ? null
639
+ : h(
640
+ Fragment,
641
+ null,
642
+ h('p', { className: 'ptSub', style: { margin: 0 } }, `会停在 pending(${services.pending.length})`),
643
+ h(
644
+ 'ul',
645
+ { className: 'ptDepList' },
646
+ services.pending.map((row) => h(
647
+ 'li',
648
+ { key: row.rowId },
649
+ h('span', { className: 'ptDepKey' }, search.markQuery(row.title, query)),
650
+ h('span', { className: 'ptDepValue' }, `需要 ${row.services.join('、')}`),
651
+ tag(row.alreadyPending ? '已 pending' : '将停在 pending', 'warn'),
652
+ )),
653
+ ),
654
+ ),
655
+ )
656
+ }
657
+
658
+ /** 模式与会话的一句话摘要,放在下游子块里,不再单开一节。 */
659
+ function renderScopeLine(data) {
660
+ const modes = (data.modes ?? []).map((mode) => mode.name).join('、') || '无'
661
+ const live = (data.liveSessions ?? []).map((row) => formatSessionId(row.sessionId)).join('、') || '无'
662
+ return h(
663
+ 'p',
664
+ { className: 'ptSub', style: { margin: 0 } },
665
+ `受影响模式:${modes} · 活会话:${live} · 冷会话:${data.coldSessionCount}`,
666
+ )
667
+ }
668
+
669
+ /** 运行时消息注入(整条消息,推断),作为「会失去的提示词与消息内容」里的一个分组。 */
670
+ function renderInjectionGroup(injections, query) {
671
+ // 搜索没有命中时整组省略;没有注入数据(available=false)仍保留说明。
672
+ if (injections.rows.length === 0 && search.normalizeQuery(query) !== '') return null
673
+ return h(
674
+ 'div',
675
+ null,
676
+ h('p', { className: 'ptSub', style: { margin: 0 } }, `运行时消息注入(整条消息 · 推断 ${injections.rows.length})`),
677
+ injections.rows.length === 0
678
+ ? h('p', { className: 'ptSub', style: { margin: 0 } }, injections.note)
679
+ : h(
680
+ 'ul',
681
+ { className: 'ptDepList' },
682
+ injections.rows.map((row) => h(
683
+ 'li',
684
+ { key: row.sourceKind },
685
+ h('span', { className: 'ptDepKey' }, search.markQuery(row.label, query)),
686
+ h('span', { className: 'ptDepValue' }, row.sourceKind),
687
+ tag('推断', undefined, '归属按包名映射,不是运行时实测'),
688
+ )),
689
+ ),
690
+ )
691
+ }
692
+
693
+ /** 证据与边界:按页面区块逐条标注来源与限制,折叠在页尾。 */
694
+ function renderEvidence(data) {
695
+ const notes = Array.isArray(data.notes) ? data.notes : []
696
+ return h(
697
+ 'details',
698
+ { className: 'ptFold ptFoldable' },
699
+ h('summary', null, h('span', null, '证据与边界'), rowEnd(`${notes.length} 项`)),
700
+ h(
701
+ 'ul',
702
+ { className: 'ptDepList' },
703
+ notes.map((note) => h(
704
+ 'li',
705
+ { key: note.id },
706
+ h('span', { className: 'ptDepKey' }, note.area),
707
+ h('span', { className: 'ptDepValue' }, note.text),
708
+ )),
709
+ ),
710
+ )
711
+ }
712
+
713
+ /** 空态:还没选目标。 */
714
+ function renderEmpty() {
715
+ return h(
716
+ 'div',
717
+ { className: 'ptEmpty' },
718
+ '选择一个插件查看禁用影响。',
719
+ h('p', { className: 'ptHint', style: { margin: '8px 0 0' } }, '在上方选择插件,或在插件详情里点「预演禁用」。本页只读,不会执行禁用。'),
720
+ )
721
+ }
722
+
723
+ /**
724
+ * 页面正文。
725
+ * @param {object} vc - 视图上下文。
726
+ * @returns {object} React 节点。
727
+ */
728
+ function renderDisablePreviewView(vc) {
729
+ const { disablePreview, query, gotoTool, gotoSection } = vc
730
+ if (disablePreview.rowId === null) return renderEmpty()
731
+ if (disablePreview.status === 'error') {
732
+ return h(
733
+ 'div',
734
+ { className: 'ptNotice' },
735
+ h('p', { className: 'ptError' }, disablePreview.error),
736
+ textButton('重试', () => disablePreview.reload()),
737
+ )
738
+ }
739
+ if (disablePreview.data === null) {
740
+ return h('div', { 'aria-busy': 'true' }, Array.from({ length: 6 }, (_value, index) => h('div', { key: index, className: 'ptSkeleton' })))
741
+ }
742
+ const data = disablePreview.data
743
+ if (data.found !== true) {
744
+ return h('div', { className: 'ptNotice' }, h('p', { className: 'ptError' }, data.reason ?? '找不到这个插件条目'))
745
+ }
746
+ const matches = (value) => search.matchesQuery(query, [value])
747
+ const searching = search.normalizeQuery(query) !== ''
748
+ const config = data.config
749
+ const diagnostics = (config?.diagnostics ?? []).filter((row) => query === '' || matches(row.message) || matches(row.kind))
750
+ // 没有可对照的键、没有诊断时整块省略:空标题比空内容更占注意力。
751
+ const keysBody = renderConfigKeysBody(config, matches)
752
+ const diagnosticsBody = diagnostics.length === 0 ? null : renderDiagnosticsBody(diagnostics)
753
+ const tools = data.tools.rows.filter((row) => query === '' || matches(row.name))
754
+ const sections = data.prompt.sections.rows.filter((row) => query === '' || matches(row.name))
755
+ const contexts = data.prompt.contexts.rows.filter((row) => query === '' || matches(row.name))
756
+ const services = {
757
+ ...data.services,
758
+ rows: (data.services.rows ?? []).filter((row) => query === '' || matches(row.name)),
759
+ pending: (data.services.pending ?? []).filter((row) => query === '' || matches(row.title) || matches(row.services.join(' '))),
760
+ }
761
+ const injections = {
762
+ ...data.injections,
763
+ rows: (data.injections.rows ?? []).filter((row) => query === '' || matches(row.label) || matches(row.sourceKind)),
764
+ }
765
+ // 页头搜索也筛下游插件链:这里只留下命中的行,图仍作为结构总览保留。
766
+ const chainRows = (data.chain?.blocked ?? []).filter((row) => !searching || search.matchesQuery(query, [
767
+ row.title,
768
+ row.rowId,
769
+ row.moduleName,
770
+ row.causeService,
771
+ ...(row.missingServices ?? []),
772
+ ...(row.path ?? []).flatMap((part) => [part.name, part.title]),
773
+ ]))
774
+ const chainTotal = data.chain?.blocked?.length ?? 0
775
+ const chain = { ...data.chain, blocked: chainRows }
776
+ const promptRows = tools.length + sections.length
777
+ const messageRows = contexts.length + injections.rows.length
778
+ const nothingMatched = searching
779
+ && promptRows + messageRows + services.rows.length + services.pending.length
780
+ + chainRows.length + diagnostics.length === 0
781
+ && keysBody === null
782
+ return h(
783
+ Fragment,
784
+ null,
785
+ h(
786
+ 'div',
787
+ { className: 'ptSection' },
788
+ h('p', { className: 'ptSectionTitle' }, '预禁用的插件信息'),
789
+ h(
790
+ 'dl',
791
+ { className: 'ptFacts' },
792
+ fact('插件', data.target.title),
793
+ fact('组合行 id', h('code', null, data.target.rowId)),
794
+ data.target.entryId !== data.target.rowId ? fact('运行时 id', h('code', null, data.target.entryId)) : null,
795
+ fact('模块', data.target.moduleName ? h('code', null, data.target.moduleName) : null),
796
+ fact('层', data.target.layer),
797
+ data.target.isGroup ? fact('分组', `子行 ${data.descendants.length} 个`) : null,
798
+ ),
799
+ ),
800
+ nothingMatched
801
+ ? search.renderQueryEmpty({
802
+ noun: '影响项、配置键或诊断',
803
+ onClear: typeof vc.setQuery === 'function' ? () => vc.setQuery('') : undefined,
804
+ })
805
+ : null,
806
+ h(
807
+ 'div',
808
+ { className: 'ptSection' },
809
+ h('p', { className: 'ptSectionTitle' }, '连锁反应'),
810
+ renderReactionSummary(data),
811
+ firstLevelBlock(
812
+ '会失去的提示词与消息内容',
813
+ h(
814
+ Fragment,
815
+ null,
816
+ searching && promptRows === 0 ? null : h(
817
+ 'div',
818
+ { className: 'ptLayer' },
819
+ h('p', { className: 'ptSubTitle' }, '系统提示词(模型收到的 system message)'),
820
+ h(
821
+ 'div',
822
+ { className: 'ptSubSection' },
823
+ renderNameGroup('工具', { ...data.tools, rows: tools }, {
824
+ query,
825
+ onOpen: typeof gotoTool === 'function' ? gotoTool : undefined,
826
+ openTitle: '在「工具」页查看',
827
+ extra: (row) => (row.shared
828
+ ? tag('共享', undefined, row.owners.join('、'))
829
+ : row.chainOnly ? tag('链路', 'warn', '由依赖链连带失去') : null),
830
+ end: (row) => (row.unknown ? '未归属' : row.scopes.includes('global') ? '全局' : `${row.scopes.length} 个范围`),
831
+ }),
832
+ renderNameGroup('提示词段', { ...data.prompt.sections, rows: sections }, {
833
+ query,
834
+ onOpen: typeof gotoSection === 'function' ? gotoSection : undefined,
835
+ openTitle: '在「上下文预演」页查看该段落',
836
+ extra: (row) => (row.shared
837
+ ? tag('共享', undefined, row.owners.join('、'))
838
+ : row.chainOnly ? tag('链路', 'warn', '由依赖链连带失去') : null),
839
+ end: (row) => (row.registryMissing
840
+ ? h('span', { className: 'ptDepValue', title: row.reason }, '可能仅会话内注册')
841
+ : row.unknown ? h('span', { className: 'ptDepValue', title: row.reason }, '未归属') : null),
842
+ }),
843
+ ),
844
+ ),
845
+ searching && messageRows === 0 ? null : h(
846
+ 'div',
847
+ { className: 'ptLayer' },
848
+ h('p', { className: 'ptSubTitle' }, '会话消息(首请求里的 user-role 消息)'),
849
+ h(
850
+ 'div',
851
+ { className: 'ptSubSection' },
852
+ renderNameGroup('运行时上下文段', { ...data.prompt.contexts, rows: contexts }, {
853
+ query,
854
+ onOpen: typeof gotoSection === 'function' ? gotoSection : undefined,
855
+ openTitle: '在「上下文预演」页查看该上下文',
856
+ extra: (row) => (row.shared
857
+ ? tag('共享', undefined, row.owners.join('、'))
858
+ : row.chainOnly ? tag('链路', 'warn', '由依赖链连带失去') : null),
859
+ end: (row) => (row.registryMissing
860
+ ? h('span', { className: 'ptDepValue', title: row.reason }, '可能仅会话内注册')
861
+ : row.unknown ? h('span', { className: 'ptDepValue', title: row.reason }, '未归属') : null),
862
+ }),
863
+ renderInjectionGroup(injections, query),
864
+ ),
865
+ ),
866
+ ),
867
+ ),
868
+ subBlock(
869
+ `受影响的下游插件(${searching && chainRows.length !== chainTotal ? `${chainRows.length} / ${chainTotal}` : chainTotal} 个)`,
870
+ h(
871
+ Fragment,
872
+ null,
873
+ renderServiceDetails(services, data.target.rowId, query),
874
+ renderChainBody(chain, vc),
875
+ (data.modes?.length ?? 0) + (data.liveSessions?.length ?? 0) + (data.coldSessionCount ?? 0) > 0
876
+ ? renderScopeLine(data)
877
+ : null,
878
+ ),
879
+ data.chain?.note,
880
+ ),
881
+ ),
882
+ relationGraph.renderRelations(vc, data),
883
+ h(
884
+ 'div',
885
+ { className: 'ptSection' },
886
+ h('p', { className: 'ptSectionTitle' }, '配置与覆盖'),
887
+ config?.available !== true
888
+ ? h('p', { className: 'ptSub', style: { margin: 0 } }, config?.reason ?? '配置分层不可用')
889
+ : h(
890
+ Fragment,
891
+ null,
892
+ subBlock('覆盖链', renderLayersBody(config)),
893
+ keysBody === null ? null : subBlock('配置逐层对照', keysBody),
894
+ diagnosticsBody === null ? null : subBlock(`配置诊断(${diagnostics.length})`, diagnosticsBody),
895
+ ),
896
+ ),
897
+ renderEvidence(data),
898
+ )
899
+ }
900
+
901
+ return { useDisablePreviewStore, renderDisablePreviewFilters, renderDisablePreviewView }
902
+ }
903
+ },
904
+ })