@cheemsup/dsh-plugin-console 0.0.0-stage → 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.
Files changed (101) hide show
  1. package/LICENSE +23 -0
  2. package/README.md +95 -3
  3. package/cordis.patch.yml +5 -0
  4. package/icon.svg +203 -0
  5. package/lib/client.activity.js +222 -0
  6. package/lib/client.apply.js +171 -0
  7. package/lib/client.changes.js +206 -0
  8. package/lib/client.cleanup.js +270 -0
  9. package/lib/client.control.js +247 -0
  10. package/lib/client.dialog.js +77 -0
  11. package/lib/client.disable-preview.js +904 -0
  12. package/lib/client.draft.js +280 -0
  13. package/lib/client.editor-messages.js +421 -0
  14. package/lib/client.editor-store.js +266 -0
  15. package/lib/client.editor.js +433 -0
  16. package/lib/client.effects.js +115 -0
  17. package/lib/client.impact-tree.js +487 -0
  18. package/lib/client.influence.js +361 -0
  19. package/lib/client.js +246 -0
  20. package/lib/client.page.js +646 -0
  21. package/lib/client.plugin-filters.js +62 -0
  22. package/lib/client.plugins.js +506 -0
  23. package/lib/client.preview-messages.js +506 -0
  24. package/lib/client.preview-raw.js +132 -0
  25. package/lib/client.preview-store.js +128 -0
  26. package/lib/client.preview-tools.js +194 -0
  27. package/lib/client.preview.js +448 -0
  28. package/lib/client.relation-graph.js +364 -0
  29. package/lib/client.schema.js +327 -0
  30. package/lib/client.search.js +409 -0
  31. package/lib/client.session-detail.js +503 -0
  32. package/lib/client.sessions.js +495 -0
  33. package/lib/client.shared.js +562 -0
  34. package/lib/client.store.js +452 -0
  35. package/lib/client.styles.js +589 -0
  36. package/lib/client.template-card.js +240 -0
  37. package/lib/client.tools.js +366 -0
  38. package/lib/client.workspace-picker.js +204 -0
  39. package/lib/host/activity.js +252 -0
  40. package/lib/host/assembly-preview.js +244 -0
  41. package/lib/host/attribution.js +341 -0
  42. package/lib/host/catalog.js +133 -0
  43. package/lib/host/categorize.js +40 -0
  44. package/lib/host/config-layers.js +589 -0
  45. package/lib/host/constants.js +69 -0
  46. package/lib/host/control.js +159 -0
  47. package/lib/host/disable-preview.js +651 -0
  48. package/lib/host/entries.js +289 -0
  49. package/lib/host/fiber.js +86 -0
  50. package/lib/host/fs.js +99 -0
  51. package/lib/host/health.js +178 -0
  52. package/lib/host/http.js +65 -0
  53. package/lib/host/impact.js +138 -0
  54. package/lib/host/injection-owners.js +60 -0
  55. package/lib/host/layers.js +132 -0
  56. package/lib/host/masks.js +145 -0
  57. package/lib/host/modes.js +96 -0
  58. package/lib/host/names.js +44 -0
  59. package/lib/host/patch-config.js +554 -0
  60. package/lib/host/patch-scan.js +103 -0
  61. package/lib/host/preview-conditions.js +281 -0
  62. package/lib/host/preview-contexts.js +174 -0
  63. package/lib/host/preview-envelope.js +31 -0
  64. package/lib/host/preview-instructions.js +450 -0
  65. package/lib/host/preview-messages.js +284 -0
  66. package/lib/host/preview-modules.js +233 -0
  67. package/lib/host/preview-skills.js +200 -0
  68. package/lib/host/prompt.js +213 -0
  69. package/lib/host/recipe-overlay.js +209 -0
  70. package/lib/host/request-preview.js +114 -0
  71. package/lib/host/routes/activation.js +96 -0
  72. package/lib/host/routes/activity.js +22 -0
  73. package/lib/host/routes/apply.js +93 -0
  74. package/lib/host/routes/cleanup.js +62 -0
  75. package/lib/host/routes/control.js +22 -0
  76. package/lib/host/routes/disable-preview.js +19 -0
  77. package/lib/host/routes/impact.js +19 -0
  78. package/lib/host/routes/operations.js +22 -0
  79. package/lib/host/routes/preview.js +58 -0
  80. package/lib/host/routes/session-influence.js +23 -0
  81. package/lib/host/routes/snapshot.js +21 -0
  82. package/lib/host/routes/template.js +54 -0
  83. package/lib/host/routes/templates.js +28 -0
  84. package/lib/host/scope-impact.js +125 -0
  85. package/lib/host/session-events.js +39 -0
  86. package/lib/host/session-influence.js +467 -0
  87. package/lib/host/sessions.js +293 -0
  88. package/lib/host/snapshot.js +218 -0
  89. package/lib/host/source-scan.js +225 -0
  90. package/lib/host/templates/cleanup.js +219 -0
  91. package/lib/host/templates/inheritance.js +144 -0
  92. package/lib/host/templates/materialize.js +317 -0
  93. package/lib/host/templates/message-overlay.js +184 -0
  94. package/lib/host/templates/repository.js +323 -0
  95. package/lib/host/templates/store.js +131 -0
  96. package/lib/host/tool-set.js +150 -0
  97. package/lib/host/tools.js +129 -0
  98. package/lib/index.js +106 -0
  99. package/locale/en.json +6 -0
  100. package/locale/zh.json +6 -0
  101. package/package.json +124 -6
@@ -0,0 +1,562 @@
1
+ /** 插件控制台 client 半的共享工具箱:路由常量、归一/格式化、请求与原子节点。 */
2
+
3
+ window.__ModuleLoader__.load({
4
+ id: '@cheemsup/dsh-plugin-console',
5
+ chunk: 'client.shared.js',
6
+ factory: (require) => {
7
+ const React = require('react')
8
+ const { createElement: h, useEffect, useState } = React
9
+
10
+ /** 侧边栏入口与主面板共享的 id。 */
11
+ const PANEL_ID = 'plugin-console'
12
+ /** 快照路由,相对于应用 base。 */
13
+ const SNAPSHOT_ROUTE = 'api/plugin-console/snapshot'
14
+ /** 预测装配路由。 */
15
+ const PREVIEW_ROUTE = 'api/plugin-console/preview'
16
+ /** 提示词模板列表路由。 */
17
+ const TEMPLATES_ROUTE = 'api/plugin-console/templates'
18
+ /** 单提示词模板路由。 */
19
+ const TEMPLATE_ROUTE = 'api/plugin-console/template'
20
+ /** 把提示词模板物化到一个会话的路由。 */
21
+ const APPLY_ROUTE = 'api/plugin-console/apply'
22
+ /** 每会话启用开关路由。 */
23
+ const ACTIVATION_ROUTE = 'api/plugin-console/activation'
24
+ /** 清除提示词定制模板快照数据路由:按工作区清绑定,或清空全部。 */
25
+ const CLEANUP_ROUTE = 'api/plugin-console/cleanup'
26
+ /** 单会话实际请求摘要路由(按会话懒读取)。 */
27
+ const SESSION_INFLUENCE_ROUTE = 'api/plugin-console/session-influence'
28
+ const ACTIVITY_ROUTE = 'api/plugin-console/activity'
29
+ const IMPACT_ROUTE = 'api/plugin-console/impact'
30
+ /** 禁用效果预演:影响面 + 逐层 config + 诊断(只读)。 */
31
+ const DISABLE_PREVIEW_ROUTE = 'api/plugin-console/disable-preview'
32
+ const CONTROL_ROUTE = 'api/plugin-console/control'
33
+ const OPERATIONS_ROUTE = 'api/plugin-console/operations'
34
+
35
+ /** 内置模式的本地化名;自定义模式回退到它自己声明的 name,再回退到 id。 */
36
+ const BUILTIN_MODE_LABELS = {
37
+ standard: '标准模式',
38
+ ptc: 'PTC 模式',
39
+ minimal: '极简模式',
40
+ cordis: '创造模式',
41
+ }
42
+
43
+ /** 面板外观:跟随 DSH、固定明亮、固定夜间。 */
44
+ const THEMES = ['follow', 'light', 'dark']
45
+ /** 外观偏好的本地存储键;只作用于本面板,不写宿主存储域。 */
46
+ const THEME_STORAGE_KEY = 'dsh-plugin-console/theme'
47
+
48
+ /**
49
+ * 把任意存储值归一成受支持的外观。
50
+ *
51
+ * 旧版本、手改存储或私密窗口都可能给到别的值;退回「跟随 DSH」而不是报错,
52
+ * 因为外观只是显示偏好,不值得让面板失去颜色。
53
+ * @param {unknown} value - 存储里的原始值。
54
+ * @returns {'follow' | 'light' | 'dark'} 受支持的外观。
55
+ */
56
+ function normalizeTheme(value) {
57
+ return THEMES.includes(value) ? value : 'follow'
58
+ }
59
+
60
+ /** 读一次已保存的外观;存储不可用(私密窗口、禁 cookie)时退回跟随。 */
61
+ function storedTheme() {
62
+ try {
63
+ if (typeof window === 'undefined' || window.localStorage === undefined) return 'follow'
64
+ return normalizeTheme(window.localStorage.getItem(THEME_STORAGE_KEY))
65
+ } catch {
66
+ return 'follow'
67
+ }
68
+ }
69
+
70
+ /**
71
+ * 外观偏好:读取、切换并写回本地存储。
72
+ *
73
+ * 存储写入失败只影响「下次是否记得」,不影响本次切换,所以吞掉异常。
74
+ * @returns {[string, Function]} `[theme, setTheme]`。
75
+ */
76
+ function useThemePreference() {
77
+ const [theme, setTheme] = useState(storedTheme)
78
+ useEffect(() => {
79
+ try {
80
+ if (typeof window === 'undefined' || window.localStorage === undefined) return
81
+ window.localStorage.setItem(THEME_STORAGE_KEY, normalizeTheme(theme))
82
+ } catch {
83
+ // 存储不可用:本次切换仍然生效,只是不会记住。
84
+ }
85
+ }, [theme])
86
+ return [theme, setTheme]
87
+ }
88
+
89
+ /** 插件能力类别:面向用户的问法,而不是包的命名。 */
90
+ const CATEGORY_LABELS = {
91
+ tool: '工具',
92
+ ui: '界面',
93
+ model: '模型',
94
+ session: '会话与时序',
95
+ safety: '安全与权限',
96
+ transport: '传输与网关',
97
+ extend: '扩展能力',
98
+ client: '客户端内核',
99
+ infra: '宿主基础设施',
100
+ structure: '结构行',
101
+ other: '其他',
102
+ }
103
+
104
+ /** 短名:优先本地化标题,其次去掉 scope 与前缀的模块名。 */
105
+ function shortName(entry) {
106
+ if (entry.moduleName && entry.moduleName.startsWith('cordis:')) {
107
+ return entry.moduleName === 'cordis:include' ? '包含根(include)' : `分组(${entry.moduleName.slice(7)})`
108
+ }
109
+ if (entry.title) return entry.title
110
+ const raw = entry.packageName ?? entry.moduleName ?? entry.entryId
111
+ return String(raw)
112
+ .replace(/^@[^/]+\//, '')
113
+ .replace(/^dsh-(client-ui-|client-|host-|api-|tool-)?/, '')
114
+ .replace(/^cordis-plugin-/, '')
115
+ }
116
+
117
+ /** 结构行(cordis:include 等)不是插件,不参与状态与依赖展示。 */
118
+ function isStructural(entry) {
119
+ return typeof entry.moduleName === 'string' && entry.moduleName.startsWith('cordis:')
120
+ }
121
+
122
+ /** 焦点在输入类元素里时,全局快捷键不该抢走按键。 */
123
+ function isTypingTarget(target) {
124
+ if (target === null || target === undefined) return false
125
+ const tag = target.tagName
126
+ return tag === 'INPUT' || tag === 'TEXTAREA' || tag === 'SELECT' || target.isContentEditable === true
127
+ }
128
+
129
+ /** 模式的展示名。 */
130
+ function modeTitle(mode) {
131
+ return BUILTIN_MODE_LABELS[mode.id] ?? mode.name ?? mode.id
132
+ }
133
+
134
+ /** 模式行里的模块短名。 */
135
+ function shortModule(moduleName) {
136
+ return String(moduleName)
137
+ .replace(/^@[^/]+\//, '')
138
+ .replace(/^dsh-(client-ui-|client-|host-|api-|tool-)?/, '')
139
+ .replace(/^cordis:/, 'cordis:')
140
+ .replace(/^cordis-plugin-/, '')
141
+ }
142
+
143
+ /** 层级归属的展示名。 */
144
+ function layerLabel(layer) {
145
+ if (!layer) return 'profile'
146
+ if (layer === 'cordis') return 'cordis 结构行'
147
+ if (layer === 'runtime') return '运行时挂载'
148
+ if (layer === 'mode') return '模式挂载'
149
+ return layer.replace(/^@[^/]+\//, '')
150
+ }
151
+
152
+ /**
153
+ * 一次 JSON 请求。
154
+ *
155
+ * 只用于提示词模板路由:它们返回 5xx 时也带 JSON body,所以先读 body 再判状态,
156
+ * 这样错误信息能原样透出而不是变成「HTTP 500」。
157
+ * @param {string} route - 相对路由。
158
+ * @param {object} [init] - fetch 选项。
159
+ * @returns {Promise<object>} 解析后的 JSON。
160
+ */
161
+ async function fetchJson(route, init) {
162
+ const response = await fetch(new URL(route, document.baseURI).href, {
163
+ credentials: 'same-origin',
164
+ ...init,
165
+ headers: { 'content-type': 'application/json', ...(init?.headers ?? {}) },
166
+ })
167
+ const data = await response.json()
168
+ if (!response.ok || data.error !== undefined) throw new Error(data.error ?? 'HTTP ' + response.status)
169
+ return data
170
+ }
171
+
172
+ /** 一次快照读取。 */
173
+ async function fetchSnapshot() {
174
+ const url = new URL(SNAPSHOT_ROUTE, document.baseURI)
175
+ const response = await fetch(url.href, { credentials: 'same-origin', headers: { accept: 'application/json' } })
176
+ if (!response.ok) throw new Error(`快照读取失败(HTTP ${response.status})`)
177
+ return await response.json()
178
+ }
179
+
180
+ /** 详情里的一行事实;`title` 是字段名的一句话解释,悬停即见。 */
181
+ function fact(label, value, title) {
182
+ if (value === undefined || value === null || value === '') return null
183
+ return h(React.Fragment, { key: label }, h('dt', { title }, label), h('dd', null, value))
184
+ }
185
+
186
+ /** 依赖列表:键 + 若干条目。 */
187
+ function depList(items) {
188
+ if (!items.length) return h('div', { className: 'ptDepValue' }, '(无)')
189
+ return h('ul', { className: 'ptDepList' }, items.map((item) => h('li', { key: item.key }, item.node)))
190
+ }
191
+
192
+ /** 依赖里的运行时条目 id 压缩成短名。 */
193
+ function shortId(entryId, byId) {
194
+ const entry = byId.get(entryId)
195
+ return entry ? shortName(entry) : entryId
196
+ }
197
+
198
+ /** 判断一个带 owners 的行是否通过当前来源筛选。 */
199
+ function passesOwner(row, toolOwner) {
200
+ if (toolOwner === null) return true
201
+ const owners = row.owners ?? []
202
+ if (toolOwner === '__none') return owners.length === 0
203
+ return owners.includes(toolOwner)
204
+ }
205
+
206
+ /**
207
+ * 一个可切换的筛选胶囊。
208
+ * @param {string} label - 显示文本。
209
+ * @param {boolean} active - 是否选中。
210
+ * @param {Function} onClick - 点击回调。
211
+ * @returns {object} React 节点。
212
+ */
213
+ function chip2(label, active, onClick) {
214
+ return h(
215
+ 'button',
216
+ { key: label, type: 'button', className: 'ptChip', 'aria-pressed': active ? 'true' : 'false', onClick },
217
+ label,
218
+ )
219
+ }
220
+
221
+ function formatBytes(bytes) {
222
+ if (!bytes) return '0B'
223
+ if (bytes < 1024) return `${bytes}B`
224
+ return `${(bytes / 1024).toFixed(1)}KB`
225
+ }
226
+
227
+ /**
228
+ * 主标签按钮。
229
+ *
230
+ * 数字是这一栏的总量,让「值不值得点进去」在点之前就能判断;
231
+ * 左右方向键在同一组标签之间移动并切换,和原生 tablist 的预期一致。
232
+ * @param {string} id - 视图 id。
233
+ * @param {string} label - 显示名。
234
+ * @param {string} current - 当前视图 id。
235
+ * @param {Function} set - 切换视图。
236
+ * @param {number} [count] - 可选的计数。
237
+ * @param {object} [opts] - `{ prefix, controls }`:次级标签用另一套 id 前缀,避免与主标签撞 id。
238
+ * @returns {object} React 节点。
239
+ */
240
+ function tabButton(id, label, current, set, count, opts) {
241
+ const selected = current === id
242
+ const domId = (opts?.prefix ?? 'pt-tab-') + id
243
+ const controls = opts?.controls === undefined ? (opts?.prefix === undefined ? 'pt-panel' : undefined) : opts.controls
244
+ return h(
245
+ 'button',
246
+ {
247
+ type: 'button',
248
+ className: 'ptTab',
249
+ role: 'tab',
250
+ id: domId,
251
+ 'aria-selected': selected ? 'true' : 'false',
252
+ 'aria-controls': controls,
253
+ tabIndex: selected ? 0 : -1,
254
+ onClick: () => set(id),
255
+ onKeyDown: (event) => {
256
+ if (!['ArrowLeft', 'ArrowRight', 'Home', 'End'].includes(event.key)) return
257
+ const buttons = [...event.currentTarget.parentElement.querySelectorAll('.ptTab')]
258
+ if (!buttons.length) return
259
+ const at = buttons.indexOf(event.currentTarget)
260
+ const next = event.key === 'Home'
261
+ ? 0
262
+ : event.key === 'End'
263
+ ? buttons.length - 1
264
+ : (at + (event.key === 'ArrowRight' ? 1 : -1) + buttons.length) % buttons.length
265
+ event.preventDefault()
266
+ buttons[next].focus()
267
+ buttons[next].click()
268
+ },
269
+ },
270
+ label,
271
+ count !== undefined && count !== null ? h('span', { className: 'ptTabCount' }, String(count)) : null,
272
+ )
273
+ }
274
+
275
+ /** 会话 id 略缩:保留可辨识的前后段。 */
276
+ function formatSessionId(id) {
277
+ const text = String(id)
278
+ if (text.length <= 32) return text
279
+ return text.slice(0, 18) + '…' + text.slice(-6)
280
+ }
281
+
282
+ /**
283
+ * 相对时间:刚刚 / N 分钟前 / N 小时前 / N 天前 / 日期。
284
+ * @param {number} timestamp - 毫秒时间戳。
285
+ * @returns {string} 可读的相对时间。
286
+ */
287
+ function formatAge(timestamp) {
288
+ const delta = Date.now() - timestamp
289
+ if (!Number.isFinite(delta) || delta < 0) return formatTime(new Date(timestamp).toISOString())
290
+ const minutes = Math.floor(delta / 60000)
291
+ if (minutes < 1) return '刚刚'
292
+ if (minutes < 60) return minutes + ' 分钟前'
293
+ const hours = Math.floor(minutes / 60)
294
+ if (hours < 24) return hours + ' 小时前'
295
+ const days = Math.floor(hours / 24)
296
+ if (days < 30) return days + ' 天前'
297
+ return formatTime(new Date(timestamp).toISOString())
298
+ }
299
+
300
+ /** 时间戳的本地化展示;解析失败时原样返回。 */
301
+ function formatTime(iso) {
302
+ const date = new Date(iso)
303
+ if (Number.isNaN(date.getTime())) return iso
304
+ return date.toLocaleString()
305
+ }
306
+
307
+ /**
308
+ * 一个带标题的下拉筛选。
309
+ *
310
+ * 之前每个筛选维度都是一排胶囊:类别 10 个、来源 5 个、状态 4 个,
311
+ * 210 个插件的页面因此先被筛选条占掉三行。下拉把「当前选了什么」和
312
+ * 「有哪些可选项」分开,默认只占一个控件的宽度。
313
+ * @param {string} label - 控件前的说明文字。
314
+ * @param {string | null} value - 当前值;null 归一成空串(= 全部)。
315
+ * @param {Array<{value: string, label: string, disabled?: boolean}>} options - 选项。
316
+ * @param {Function} onChange - 值变化回调,收到原始字符串。
317
+ * @param {object} [opts] - `{ title, disabled }`。
318
+ * @returns {object} React 节点。
319
+ */
320
+ function selectField(label, value, options, onChange, opts) {
321
+ return h(
322
+ 'label',
323
+ { className: 'ptField', title: opts?.title },
324
+ label ? h('span', { className: 'ptFieldLabel' }, label) : null,
325
+ h(
326
+ 'select',
327
+ {
328
+ className: 'ptSelect',
329
+ value: value ?? '',
330
+ disabled: opts?.disabled === true,
331
+ 'aria-label': label ?? opts?.title,
332
+ onChange: (event) => onChange(event.target.value),
333
+ },
334
+ options.filter(Boolean).map((option) =>
335
+ h('option', { key: option.value, value: option.value, disabled: option.disabled === true }, option.label),
336
+ ),
337
+ ),
338
+ )
339
+ }
340
+
341
+ /**
342
+ * 无边框的文字按钮,用于「展开全部」「重命名」「返回」这类次要动作。
343
+ * @param {string} label - 显示文本。
344
+ * @param {Function} onClick - 点击回调。
345
+ * @param {object} [opts] - `{ title, tone, disabled }`。
346
+ * @returns {object} React 节点。
347
+ */
348
+ function textButton(label, onClick, opts) {
349
+ return h(
350
+ 'button',
351
+ {
352
+ type: 'button',
353
+ className: 'ptTextBtn',
354
+ 'data-tone': opts?.tone,
355
+ title: opts?.title,
356
+ disabled: opts?.disabled === true,
357
+ onClick,
358
+ },
359
+ label,
360
+ )
361
+ }
362
+
363
+ /**
364
+ * 一个开关。用于「每会话提示词模板是否生效」与「物化后自动发送」。
365
+ *
366
+ * 用 `role="switch"` 而不是把状态塞进文案:文案会跟着状态变长变短,
367
+ * 按钮宽度一跳,整行都在晃。开关的视觉状态与 `aria-checked` 保持一致。
368
+ * @param {boolean} checked - 是否开启。
369
+ * @param {string} label - 开关后的说明文字。
370
+ * @param {Function} onClick - 点击回调。
371
+ * @param {object} [opts] - `{ title, disabled }`。
372
+ * @returns {object} React 节点。
373
+ */
374
+ function switchButton(checked, label, onClick, opts) {
375
+ return h(
376
+ 'button',
377
+ {
378
+ type: 'button',
379
+ className: 'ptSwitch',
380
+ role: 'switch',
381
+ 'aria-checked': checked ? 'true' : 'false',
382
+ title: opts?.title,
383
+ disabled: opts?.disabled === true,
384
+ onClick,
385
+ },
386
+ h('span', { className: 'ptSwitchTrack', 'aria-hidden': 'true' }, h('span', { className: 'ptSwitchKnob' })),
387
+ label ? h('span', { className: 'ptSwitchLabel' }, label) : null,
388
+ )
389
+ }
390
+
391
+ /**
392
+ * 一行用「·」分隔的次要信息。比一排彩色标签安静,也更省横向空间。
393
+ * @param {Array<object | string | null | undefined>} parts - 片段。
394
+ * @param {boolean} [wrap] - 允许在窄屏折行(页内摘要用);行内元信息保持单行。
395
+ * @returns {object} React 节点。
396
+ */
397
+ function inlineMeta(parts, wrap) {
398
+ const visible = parts.filter((part) => part !== null && part !== undefined && part !== false)
399
+ return h(
400
+ 'span',
401
+ { className: wrap ? 'ptMeta ptMetaWrap' : 'ptMeta' },
402
+ visible.map((part, index) =>
403
+ h(
404
+ React.Fragment,
405
+ { key: index },
406
+ index === 0 ? null : h('span', { className: 'ptMetaSep' }, '·'),
407
+ part,
408
+ ),
409
+ ),
410
+ )
411
+ }
412
+
413
+ /**
414
+ * 一行末尾的读数簇。
415
+ *
416
+ * 数量、长度、序号与时间统一靠右,且不再用胶囊包数字:左边只回答「这是什么」,
417
+ * 右边回答「多少」。空簇返回 null,不占一行。
418
+ * @param {...object} children - 读数节点。
419
+ * @returns {object | null} 右簇。
420
+ */
421
+ function rowEnd(...children) {
422
+ const visible = children.filter((child) => child !== null && child !== undefined && child !== false)
423
+ return visible.length === 0 ? null : h('span', { className: 'ptEnd' }, visible)
424
+ }
425
+
426
+ /** 结果行:「显示 37 / 210」。unit 用于补「个会话」这类量词。 */
427
+ function resultText(shown, total, unit) {
428
+ return h('span', { className: 'ptResult' }, '显示 ', h('b', null, String(shown)), ' / ' + String(total) + (unit ?? ''))
429
+ }
430
+
431
+ /** 一次性展开或收起 `<details>`:details 的开关归浏览器,用 DOM 改比造受控状态更合适。 */
432
+ function setDetailsOpen(nodes, open) {
433
+ for (const node of nodes) {
434
+ if (open) node.setAttribute('open', '')
435
+ else node.removeAttribute('open')
436
+ }
437
+ }
438
+
439
+ /**
440
+ * 当前面板里的可折叠块。
441
+ *
442
+ * 默认返回所有 `ptFoldable`;传 selector 可以只作用于某一组(例如请求结构里的
443
+ * `details.ptRawBlock`),避免「折叠全部」误伤同页其它折叠块。
444
+ * @param {string} [selector] - CSS 选择器。
445
+ * @returns {NodeList | Array} 可折叠节点。
446
+ */
447
+ function foldNodes(selector = '.ptBody details.ptFoldable') {
448
+ if (typeof document === 'undefined' || typeof document.querySelectorAll !== 'function') return []
449
+ return document.querySelectorAll(selector)
450
+ }
451
+
452
+ /** 让可点的非按钮容器响应键盘:Enter / Space 等价于点击。 */
453
+ function activateOnKey(event, action) {
454
+ if (event.key !== 'Enter' && event.key !== ' ') return
455
+ event.preventDefault()
456
+ action()
457
+ }
458
+
459
+ /**
460
+ * 在折叠集合里切换一个 key。
461
+ *
462
+ * 集合是 state,必须换新引用才会触发重渲染;就地 add/delete 不会。
463
+ * @param {Function} setter - 该集合的 setState。
464
+ * @param {string} key - 要切换的 key。
465
+ */
466
+ function toggleIn(setter, key) {
467
+ setter((previous) => {
468
+ const next = new Set(previous)
469
+ if (next.has(key)) next.delete(key)
470
+ else next.add(key)
471
+ return next
472
+ })
473
+ }
474
+
475
+ /**
476
+ * 一次性折叠或展开所有分组。
477
+ * @param {Function} setter - 折叠集合的 setState。
478
+ * @param {Array<string>} keys - 所有分组 key。
479
+ * @param {boolean} collapse - true 表示全部折叠。
480
+ */
481
+ function setAll(setter, keys, collapse) {
482
+ setter(() => new Set(collapse ? keys : []))
483
+ }
484
+
485
+ /**
486
+ * 浏览器时区。
487
+ *
488
+ * 模型看到的 time-context 用它解释未限定时区的时间;拿不到就不上报,
489
+ * 由宿主退回服务端时区并在结果里标「推测」。预演页与定制页都要用同一份。
490
+ * @returns {string | undefined} IANA 时区名。
491
+ */
492
+ function browserTimeZone() {
493
+ try {
494
+ return Intl.DateTimeFormat().resolvedOptions().timeZone || undefined
495
+ } catch {
496
+ return undefined
497
+ }
498
+ }
499
+
500
+ return function createShared() {
501
+ // 视图壳只需要这一份原子集合;各视图 chunk 共用同一个对象。
502
+ // 顶层导出与它大部分重合,只额外带上路由常量与模式标签,所以先建这份再展开。
503
+ const viewHelpers = {
504
+ h,
505
+ React,
506
+ shortName,
507
+ isStructural,
508
+ isTypingTarget,
509
+ modeTitle,
510
+ shortModule,
511
+ layerLabel,
512
+ fetchJson,
513
+ fetchSnapshot,
514
+ ACTIVITY_ROUTE,
515
+ IMPACT_ROUTE,
516
+ DISABLE_PREVIEW_ROUTE,
517
+ CONTROL_ROUTE,
518
+ OPERATIONS_ROUTE,
519
+ fact,
520
+ depList,
521
+ shortId,
522
+ passesOwner,
523
+ chip2,
524
+ selectField,
525
+ textButton,
526
+ switchButton,
527
+ inlineMeta,
528
+ resultText,
529
+ rowEnd,
530
+ setDetailsOpen,
531
+ foldNodes,
532
+ formatBytes,
533
+ tabButton,
534
+ formatSessionId,
535
+ formatAge,
536
+ formatTime,
537
+ activateOnKey,
538
+ toggleIn,
539
+ setAll,
540
+ browserTimeZone,
541
+ useThemePreference,
542
+ normalizeTheme,
543
+ THEMES,
544
+ CATEGORY_LABELS,
545
+ }
546
+ return {
547
+ ...viewHelpers,
548
+ PANEL_ID,
549
+ SNAPSHOT_ROUTE,
550
+ PREVIEW_ROUTE,
551
+ TEMPLATES_ROUTE,
552
+ TEMPLATE_ROUTE,
553
+ APPLY_ROUTE,
554
+ ACTIVATION_ROUTE,
555
+ CLEANUP_ROUTE,
556
+ SESSION_INFLUENCE_ROUTE,
557
+ BUILTIN_MODE_LABELS,
558
+ helpers: viewHelpers,
559
+ }
560
+ }
561
+ },
562
+ })