dsh-prompt-customizer 0.4.2 → 0.5.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/README.md +41 -35
- package/client/client.js +1655 -781
- package/client/client.js.map +1 -1
- package/lib/effective.js +2 -2
- package/lib/index.js +139 -100
- package/lib/promotion.js +58 -62
- package/lib/sectionOps.d.mts +35 -0
- package/lib/sectionOps.mjs +140 -0
- package/lib/zh/cordis.mjs +110 -0
- package/lib/zh/core.mjs +47 -0
- package/lib/zh/index.d.mts +8 -0
- package/lib/zh/index.mjs +23 -0
- package/lib/zh/sdk.mjs +22 -0
- package/lib/zh/teams.mjs +19 -0
- package/lib/zh/undo.mjs +18 -0
- package/package.json +9 -10
- package/src/client/Panel.tsx +532 -0
- package/src/client/{PresetsTab.tsx → PresetsPane.tsx} +146 -129
- package/src/client/PreviewPane.tsx +73 -0
- package/src/client/SectionsPane.tsx +331 -0
- package/src/client/ToolsPane.tsx +230 -0
- package/src/client/error-boundary.tsx +48 -0
- package/src/client/index.ts +69 -406
- package/src/client/locales.ts +44 -8
- package/src/client/nav.tsx +284 -0
- package/src/client/shell.tsx +179 -0
- package/src/client/styles.ts +24 -14
- package/src/client/types.ts +1 -4
- package/src/client/PreviewTab.tsx +0 -92
- package/src/client/SectionsTab.tsx +0 -539
- package/src/client/ToolsTab.tsx +0 -293
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SectionsPane — 独立面板的左栏(提示词模式)。
|
|
3
|
+
*
|
|
4
|
+
* 顶部三个阶段 Tab(引导期 / 常驻期 / 压缩受控期,恒定全部可选),与右栏
|
|
5
|
+
* 预览的阶段按钮**联动**:两边切的是同一个阶段,编辑哪个阶段就看哪个阶段的
|
|
6
|
+
* 装配。每个阶段列出该阶段装配里的段:勾选框 = 该阶段是否注入模型(勾上 =
|
|
7
|
+
* 启用),被停用的行半透明原位保留、随时可勾回。行内:↑/↓ 重排、编辑(按
|
|
8
|
+
* 阶段替换文本)、还原、删除(仅自定义段);「+ 注入」展开注入新段表单。
|
|
9
|
+
*
|
|
10
|
+
* 底部固定条:三态过滤(全部 / 已启用 / 已停用)——像宿主能力管理页一样按
|
|
11
|
+
* 勾选状态筛选当前阶段的列表。
|
|
12
|
+
*
|
|
13
|
+
* 「本系统全部提示词」只读池收在列表末尾的折叠区(不分阶段):每行三个小
|
|
14
|
+
* 按钮把该段加入对应阶段(替代旧版的拖拽搬移;运行时动态段加不进,会给出
|
|
15
|
+
* 说明)。
|
|
16
|
+
*
|
|
17
|
+
* 全部阶段状态逻辑来自 lib/sectionOps.mjs(纯函数,node --test 单测直接
|
|
18
|
+
* 覆盖同一份代码)。
|
|
19
|
+
*/
|
|
20
|
+
import { createElement as h, useState, type ChangeEvent, type ReactElement } from 'react'
|
|
21
|
+
import type { Config, Inventory, Phase, PhaseViewKey, Preview } from './types.ts'
|
|
22
|
+
import type { Translate } from './locales.ts'
|
|
23
|
+
import { PART_ORDER } from './presets.ts'
|
|
24
|
+
import { injectPhaseOf, deniedNames, blockPatch, phaseInjectEntries, mergedPhaseInjectEntries, phaseRows } from '../../lib/sectionOps.mjs'
|
|
25
|
+
import { s } from './styles.ts'
|
|
26
|
+
|
|
27
|
+
/** Panel 并行拉取的三阶段装配。 */
|
|
28
|
+
type PhaseViews = Record<PhaseViewKey, Preview | null>
|
|
29
|
+
|
|
30
|
+
/** 屏幕上一条可交互的段行。 */
|
|
31
|
+
interface PartRow {
|
|
32
|
+
name: string
|
|
33
|
+
text: string
|
|
34
|
+
replaced: boolean
|
|
35
|
+
custom: boolean
|
|
36
|
+
/** 本阶段的用户替换文本(空串 = 本阶段没改过文本)—— 三个阶段各一份,互不影响。 */
|
|
37
|
+
override: string
|
|
38
|
+
/** 该阶段自己的屏蔽名单是否屏蔽了此段:勾选框 = 未屏蔽(注入模型)。 */
|
|
39
|
+
blocked: boolean
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** 三态过滤:全部 / 已启用(未屏蔽)/ 已停用(被屏蔽)。 */
|
|
43
|
+
export type TriState = 'all' | 'on' | 'off'
|
|
44
|
+
|
|
45
|
+
/** 编辑态的键:段名之外还要带阶段,否则三个部分里同名的行会同时展开编辑器。 */
|
|
46
|
+
const editKey = (key: PhaseViewKey, name: string): string => `${key}:${name}`
|
|
47
|
+
|
|
48
|
+
export function SectionsPane({ cfg, inv, phases, phase, syncAll, t, write }: {
|
|
49
|
+
cfg: Config
|
|
50
|
+
inv: Inventory | null
|
|
51
|
+
phases: PhaseViews | null
|
|
52
|
+
/** 当前阶段(状态在 Panel 持有,头部阶段按钮统一切换)。 */
|
|
53
|
+
phase: PhaseViewKey
|
|
54
|
+
/** 三态同步(默认关):勾选后屏蔽 / 解除屏蔽与替换文本对三个阶段一起生效(只作用于同名段)。 */
|
|
55
|
+
syncAll?: boolean
|
|
56
|
+
t: Translate
|
|
57
|
+
write: (field: 'sections' | 'sectionsBootstrap' | 'sectionsCompaction' | 'replace' | 'inject' | 'tools', value: unknown) => void
|
|
58
|
+
}): ReactElement {
|
|
59
|
+
const [filter, setFilter] = useState<TriState>('all')
|
|
60
|
+
const [editing, setEditing] = useState<string | null>(null)
|
|
61
|
+
const [draft, setDraft] = useState('')
|
|
62
|
+
// 「注入新段」表单展开态(当前阶段分组内)。
|
|
63
|
+
const [addOpen, setAddOpen] = useState(false)
|
|
64
|
+
// 面板内短提示(例如:动态段加不进某个阶段时说明原因)。
|
|
65
|
+
const [notice, setNotice] = useState<string | null>(null)
|
|
66
|
+
|
|
67
|
+
const rowsOf = (key: PhaseViewKey): PartRow[] => phaseRows(cfg, phases?.[key] ?? null, key)
|
|
68
|
+
|
|
69
|
+
// 屏蔽 / 恢复一个段(blockPatch 只动本阶段自己的名单)。写入严格落在当前
|
|
70
|
+
// 编辑目标的草稿里 —— 预设之间互相独立:在 A 预设里的任何改动对 B 预设与
|
|
71
|
+
// 全局默认零影响。
|
|
72
|
+
const applyBlock = (key: PhaseViewKey, name: string, blocked: boolean): void => {
|
|
73
|
+
const patch = blockPatch(cfg, key, name, blocked)
|
|
74
|
+
for (const [field, value] of Object.entries(patch)) {
|
|
75
|
+
write(field as 'sections' | 'sectionsBootstrap' | 'sectionsCompaction', value)
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
const toggleBlocked = (key: PhaseViewKey, name: string): void => {
|
|
79
|
+
const blocked = !deniedNames(cfg, key).includes(name)
|
|
80
|
+
// 三态同步(可选):一次写全三个阶段 —— 只作用于同名的这一段。
|
|
81
|
+
for (const k of syncAll ? PART_ORDER : [key]) applyBlock(k, name, blocked)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// 该阶段中某名字是否在该阶段的装配输入里(判断加入是「解除屏蔽」还是「凭空注入」)。
|
|
85
|
+
const inBaseOf = (key: PhaseViewKey, name: string): boolean =>
|
|
86
|
+
(phases?.[key]?.baseSections ?? []).some((sec) => sec.name === name)
|
|
87
|
+
|
|
88
|
+
// 持久化一个阶段的有序列表(写回 inject):每行一条注入项、order 为连续整数;
|
|
89
|
+
// 系统行空文本(仅 order 覆盖),custom 行保留文本;其它阶段的注入项原样保留。
|
|
90
|
+
const persistPhase = (key: PhaseViewKey, rows: PartRow[]): void => {
|
|
91
|
+
write('inject', phaseInjectEntries(cfg, key, rows))
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// 上移/下移一格(数组交换后重排整个阶段)。
|
|
95
|
+
const moveRow = (key: PhaseViewKey, index: number, dir: -1 | 1): void => {
|
|
96
|
+
const rows = rowsOf(key)
|
|
97
|
+
const target = index + dir
|
|
98
|
+
if (target < 0 || target >= rows.length) return
|
|
99
|
+
const next = rows.slice()
|
|
100
|
+
const [item] = next.splice(index, 1)
|
|
101
|
+
next.splice(target, 0, item)
|
|
102
|
+
persistPhase(key, next)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// 从该阶段移除一个段:删掉它在本阶段的注入条目,以及跨阶段生效(always)的
|
|
106
|
+
// 条目 —— 预设应用 / 导入产生的自定义段就是 always。随后解除该阶段屏蔽。
|
|
107
|
+
const removeFromPart = (key: PhaseViewKey, name: string): void => {
|
|
108
|
+
const phaseOfItem = injectPhaseOf(key)
|
|
109
|
+
const inject = (cfg.inject ?? []).filter((x) => {
|
|
110
|
+
if (x.name !== name) return true
|
|
111
|
+
const itemPhase = x.phase ?? 'always'
|
|
112
|
+
return !(itemPhase === phaseOfItem || itemPhase === 'always')
|
|
113
|
+
})
|
|
114
|
+
write('inject', inject)
|
|
115
|
+
applyBlock(key, name, false)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// 文本编辑按阶段生效:编辑态的键是「阶段 + 段名」。写入走 persistPhase
|
|
119
|
+
//(本阶段的注入条目带 text),绝不碰全局 replace 字典。
|
|
120
|
+
const startReplace = (key: PhaseViewKey, row: PartRow): void => {
|
|
121
|
+
setEditing(editKey(key, row.name))
|
|
122
|
+
setDraft(row.override || row.text)
|
|
123
|
+
}
|
|
124
|
+
const commitReplace = (key: PhaseViewKey, row: PartRow): void => {
|
|
125
|
+
const patchRow = (r: PartRow): PartRow => (r.name !== row.name ? r : { ...r, override: draft, text: draft })
|
|
126
|
+
if (syncAll) {
|
|
127
|
+
// 三态同步(可选):替换文本一次写全三个阶段 —— 只改各阶段里同名的那
|
|
128
|
+
// 一行。edit 对 inject 是整体替换,必须把三个阶段叠进同一份列表一次写入。
|
|
129
|
+
const rowsByKey = {} as Record<PhaseViewKey, PartRow[]>
|
|
130
|
+
for (const k of PART_ORDER) {
|
|
131
|
+
const rows = rowsOf(k)
|
|
132
|
+
rowsByKey[k] = k === key || rows.some((r) => r.name === row.name) ? rows.map(patchRow) : rows
|
|
133
|
+
}
|
|
134
|
+
write('inject', mergedPhaseInjectEntries(cfg, rowsByKey))
|
|
135
|
+
} else {
|
|
136
|
+
persistPhase(key, rowsOf(key).map(patchRow))
|
|
137
|
+
}
|
|
138
|
+
setEditing(null)
|
|
139
|
+
}
|
|
140
|
+
const restoreReplace = (key: PhaseViewKey, row: PartRow): void => {
|
|
141
|
+
const next = rowsOf(key).map((r) =>
|
|
142
|
+
r.name !== row.name ? r : { ...r, override: '', text: r.custom ? r.text : '' },
|
|
143
|
+
)
|
|
144
|
+
persistPhase(key, next)
|
|
145
|
+
// 遗留的全局替换条目(旧版 UI 的产物):只在本部分还原时一并清除。
|
|
146
|
+
if (!row.custom && Object.hasOwn(cfg.replace ?? {}, row.name)) {
|
|
147
|
+
const rest = { ...(cfg.replace ?? {}) }
|
|
148
|
+
delete rest[row.name]
|
|
149
|
+
write('replace', rest)
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const addSection = (name: string, text: string, phase: Phase): void => {
|
|
154
|
+
const inject = (cfg.inject ?? []).slice()
|
|
155
|
+
inject.push({ name, order: 120 + inject.length, text, phase, custom: true })
|
|
156
|
+
write('inject', inject)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// 从「全部」池把一个段加入某阶段(替代旧版拖拽:池 → 阶段)。该阶段已有的
|
|
160
|
+
// 段只是移到末尾;新段带上池里的原文(注入条目带文本,服务端才会把它真正
|
|
161
|
+
// 建出来)。动态段没有可带的原文,给出说明。
|
|
162
|
+
const addFromPool = (key: PhaseViewKey, name: string, text: string): void => {
|
|
163
|
+
if (text === '' || text.startsWith('<')) {
|
|
164
|
+
setNotice(t('sectionDynamicNoAdd', { name }))
|
|
165
|
+
return
|
|
166
|
+
}
|
|
167
|
+
setNotice(null)
|
|
168
|
+
const rows = rowsOf(key)
|
|
169
|
+
const existing = rows.find((row) => row.name === name)
|
|
170
|
+
const next = existing !== undefined
|
|
171
|
+
? [...rows.filter((row) => row.name !== name), existing]
|
|
172
|
+
: [...rows, {
|
|
173
|
+
name,
|
|
174
|
+
text: '',
|
|
175
|
+
replaced: Object.hasOwn(cfg.replace ?? {}, name),
|
|
176
|
+
custom: false,
|
|
177
|
+
override: text,
|
|
178
|
+
blocked: false,
|
|
179
|
+
}]
|
|
180
|
+
if (existing === undefined && inBaseOf(key, name) && deniedNames(cfg, key).includes(name)) {
|
|
181
|
+
applyBlock(key, name, false)
|
|
182
|
+
}
|
|
183
|
+
persistPhase(key, next)
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const stageLabel = (key: PhaseViewKey): string =>
|
|
187
|
+
key === 'bootstrap' ? t('phaseStageGuide') : key === 'compaction' ? t('phaseStageControlled') : t('phaseStageResident')
|
|
188
|
+
|
|
189
|
+
// 该阶段的「段级定制没进最终提示词」警示(forceSections 关闭或失效时)。
|
|
190
|
+
const partNote = (key: PhaseViewKey): ReactElement | null => {
|
|
191
|
+
const view = phases?.[key]
|
|
192
|
+
if (view === undefined || view === null) return null
|
|
193
|
+
if (cfg.forceSections !== false) return null
|
|
194
|
+
if (view.takenOverBy !== undefined) {
|
|
195
|
+
return h('div', { style: s.noticeWarn }, t('sectionsTakenOver', { name: view.takenOverBy }))
|
|
196
|
+
}
|
|
197
|
+
const lost = view.lostSections
|
|
198
|
+
if (lost !== undefined) {
|
|
199
|
+
return h('div', { style: s.noticeWarn }, t('sectionsLost', { emitted: lost.emitted, survived: lost.survived }))
|
|
200
|
+
}
|
|
201
|
+
return null
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const isEditing = (key: PhaseViewKey, row: PartRow): boolean => editing === editKey(key, row.name)
|
|
205
|
+
|
|
206
|
+
const renderRow = (key: PhaseViewKey, row: PartRow, index: number, total: number): ReactElement | null => {
|
|
207
|
+
if (!rowVisible(row)) return null
|
|
208
|
+
return h('div', {
|
|
209
|
+
key: row.name,
|
|
210
|
+
style: { ...s.row, ...(row.blocked ? s.rowBlocked : {}) },
|
|
211
|
+
}, [
|
|
212
|
+
h('input', {
|
|
213
|
+
type: 'checkbox',
|
|
214
|
+
checked: !row.blocked,
|
|
215
|
+
onChange: () => toggleBlocked(key, row.name),
|
|
216
|
+
title: row.blocked ? t('blockedOn') : t('blockedOff'),
|
|
217
|
+
style: { margin: 0, cursor: 'pointer', flex: 'none' },
|
|
218
|
+
}),
|
|
219
|
+
h('div', { style: s.rowBody }, [
|
|
220
|
+
h('div', { style: s.rowTitle }, [
|
|
221
|
+
h('span', { style: s.code }, row.name),
|
|
222
|
+
h('span', { style: s.orderTag }, '#' + index),
|
|
223
|
+
h('span', { style: row.custom ? s.badgeCustom : s.badgeSystem }, row.custom ? t('manual') : t('system')),
|
|
224
|
+
row.replaced ? h('span', { style: s.badgeReplaced }, t('replaced')) : null,
|
|
225
|
+
row.blocked ? h('span', { style: s.badgeBlocked }, t('blockedOn')) : null,
|
|
226
|
+
]),
|
|
227
|
+
isEditing(key, row)
|
|
228
|
+
? h('div', { style: s.editBox }, [
|
|
229
|
+
h('textarea', { style: s.editInput, value: draft, onChange: (e: ChangeEvent<HTMLTextAreaElement>) => setDraft(e.target.value), rows: 3 }),
|
|
230
|
+
h('div', { style: s.injectRow }, [
|
|
231
|
+
h('button', { style: s.mini, onClick: () => commitReplace(key, row) }, t('save')),
|
|
232
|
+
h('button', { style: s.mini, onClick: () => setDraft('') }, t('clearInput')),
|
|
233
|
+
!row.custom && (row.override || Object.hasOwn(cfg.replace ?? {}, row.name))
|
|
234
|
+
? h('button', { style: s.mini, onClick: () => restoreReplace(key, row) }, t('restore'))
|
|
235
|
+
: null,
|
|
236
|
+
]),
|
|
237
|
+
])
|
|
238
|
+
: h('div', { style: s.preview }, String(row.override || row.text || '').slice(0, 140) || (row.custom ? t('empty') : t('dynamic'))),
|
|
239
|
+
]),
|
|
240
|
+
h('div', { style: s.arrowCol }, [
|
|
241
|
+
h('button', { style: s.arrow, disabled: index === 0, onClick: () => moveRow(key, index, -1), title: t('moveUp') }, '↑'),
|
|
242
|
+
h('button', { style: s.arrow, disabled: index === total - 1, onClick: () => moveRow(key, index, 1), title: t('moveDown') }, '↓'),
|
|
243
|
+
]),
|
|
244
|
+
isEditing(key, row) ? null : h('button', { style: s.mini, onClick: () => startReplace(key, row) }, t('replace')),
|
|
245
|
+
!isEditing(key, row) && !row.custom && (row.override || Object.hasOwn(cfg.replace ?? {}, row.name))
|
|
246
|
+
? h('button', { style: s.mini, onClick: () => restoreReplace(key, row) }, t('restore'))
|
|
247
|
+
: null,
|
|
248
|
+
row.custom ? h('button', { style: s.mini, onClick: () => removeFromPart(key, row.name), title: t('delete') }, t('delete')) : null,
|
|
249
|
+
])
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// 当前阶段的行 + 三态过滤。
|
|
253
|
+
const rows = rowsOf(phase)
|
|
254
|
+
const onCount = rows.filter((row) => !row.blocked).length
|
|
255
|
+
const offCount = rows.length - onCount
|
|
256
|
+
const rowVisible = (row: PartRow): boolean =>
|
|
257
|
+
filter === 'all' || (filter === 'on' ? !row.blocked : row.blocked)
|
|
258
|
+
|
|
259
|
+
const poolSections = inv?.sections ?? []
|
|
260
|
+
|
|
261
|
+
return h('div', { style: s.colLeft }, [
|
|
262
|
+
h('div', { style: s.colScroll }, [
|
|
263
|
+
notice ? h('div', { style: s.noticeWarn }, notice) : null,
|
|
264
|
+
partNote(phase),
|
|
265
|
+
rows.map((row, i) => renderRow(phase, row, i, rows.length)),
|
|
266
|
+
rows.length === 0 ? h('div', { style: s.muted }, t('empty')) : null,
|
|
267
|
+
addOpen
|
|
268
|
+
? h(InjectForm, {
|
|
269
|
+
onAdd: (name, text) => { addSection(name, text, injectPhaseOf(phase)); setAddOpen(false) },
|
|
270
|
+
phaseLabel: stageLabel(phase),
|
|
271
|
+
t,
|
|
272
|
+
})
|
|
273
|
+
: null,
|
|
274
|
+
// 本系统全部提示词:跨预设累积的只读池(折叠区),每行可加入三个阶段。
|
|
275
|
+
h('details', { style: s.injectBox }, [
|
|
276
|
+
h('summary', { style: { ...s.muted, cursor: 'pointer' } },
|
|
277
|
+
`${t('allSectionsTitle')} (${poolSections.length})`),
|
|
278
|
+
h('div', { style: { ...s.muted, marginBottom: 4 } }, t('sectionsFourHint')),
|
|
279
|
+
poolSections.length === 0 ? h('div', { style: s.muted }, t('empty')) : null,
|
|
280
|
+
poolSections.map((sec) => h('div', { key: sec.name, style: { ...s.row, opacity: 0.92 } }, [
|
|
281
|
+
h('div', { style: s.rowBody }, [
|
|
282
|
+
h('div', { style: s.rowTitle }, h('span', { style: s.code }, sec.name)),
|
|
283
|
+
h('div', { style: s.preview }, String(sec.text ?? '').slice(0, 140) || t('dynamic')),
|
|
284
|
+
]),
|
|
285
|
+
...PART_ORDER.map((key) => h('button', {
|
|
286
|
+
key,
|
|
287
|
+
style: s.arrow,
|
|
288
|
+
title: t('poolAddTitle', { phase: stageLabel(key) }),
|
|
289
|
+
onClick: () => addFromPool(key, sec.name, sec.text ?? ''),
|
|
290
|
+
}, t(key === 'bootstrap' ? 'phaseShortGuide' : key === 'active' ? 'phaseShortResident' : 'phaseShortControlled'))),
|
|
291
|
+
])),
|
|
292
|
+
]),
|
|
293
|
+
// 「+ 注入」入口放在滚动区末尾:新段通常追加在当前阶段列表尾部。
|
|
294
|
+
h('div', { style: { ...s.injectRow, marginTop: 8 } }, [
|
|
295
|
+
h('button', { style: s.mini, onClick: () => setAddOpen(!addOpen), title: t('injectNew') },
|
|
296
|
+
addOpen ? `× ${t('clearInput')}` : `+ ${t('injectNew')}`),
|
|
297
|
+
]),
|
|
298
|
+
]),
|
|
299
|
+
// 左栏底部固定条:三态过滤(全部 / 已启用 / 已停用),只统计当前阶段。
|
|
300
|
+
h('div', { style: s.colFoot }, [
|
|
301
|
+
h('div', { style: s.seg }, [
|
|
302
|
+
h('button', { style: filter === 'all' ? s.segBtnActive : s.segBtn, onClick: () => setFilter('all') },
|
|
303
|
+
`${t('filterAll')} ${rows.length}`),
|
|
304
|
+
h('button', { style: filter === 'on' ? s.segBtnActive : s.segBtn, onClick: () => setFilter('on') },
|
|
305
|
+
`${t('filterOn')} ${onCount}`),
|
|
306
|
+
h('button', { style: filter === 'off' ? s.segBtnActive : s.segBtn, onClick: () => setFilter('off') },
|
|
307
|
+
`${t('filterOff')} ${offCount}`),
|
|
308
|
+
]),
|
|
309
|
+
]),
|
|
310
|
+
])
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/** 注入新段表单:「+ 注入」展开,阶段锁定为当前 Tab 的阶段。 */
|
|
314
|
+
function InjectForm({ onAdd, phaseLabel, t }: {
|
|
315
|
+
onAdd: (name: string, text: string) => void
|
|
316
|
+
phaseLabel: string
|
|
317
|
+
t: Translate
|
|
318
|
+
}): ReactElement {
|
|
319
|
+
const [name, setName] = useState('')
|
|
320
|
+
const [text, setText] = useState('')
|
|
321
|
+
const submit = (): void => {
|
|
322
|
+
if (!name.trim()) return
|
|
323
|
+
onAdd(name.trim(), text)
|
|
324
|
+
setName(''); setText('')
|
|
325
|
+
}
|
|
326
|
+
return h('div', { style: s.injectRow }, [
|
|
327
|
+
h('input', { style: { ...s.input, width: '30%' }, placeholder: t('name'), value: name, onChange: (e: ChangeEvent<HTMLInputElement>) => setName(e.target.value) }),
|
|
328
|
+
h('input', { style: { ...s.input, flex: 1 }, placeholder: `${t('text')}(${phaseLabel})`, value: text, onChange: (e: ChangeEvent<HTMLInputElement>) => setText(e.target.value) }),
|
|
329
|
+
h('button', { style: s.mini, onClick: submit }, t('add')),
|
|
330
|
+
])
|
|
331
|
+
}
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ToolsPane — 独立面板的左栏(工具模式)。
|
|
3
|
+
*
|
|
4
|
+
* 顶部三个阶段 Tab(引导期 / 常驻期 / 压缩受控期),与右栏预览的阶段按钮
|
|
5
|
+
* **联动**。每个阶段列出该阶段进入过滤的目录 ∪ 用户加回的工具:勾选框 =
|
|
6
|
+
* 该阶段对模型可见;隐藏后仍在列表可反选。三份名单互不继承。
|
|
7
|
+
*
|
|
8
|
+
* 底部固定条:三态过滤(全部 / 已启用 = 可见 / 已停用 = 隐藏),只统计当前
|
|
9
|
+
* 阶段。
|
|
10
|
+
*
|
|
11
|
+
* 「本系统全部工具」只读池收在折叠区(不分阶段):每行三个小按钮把该工具
|
|
12
|
+
* 加入对应阶段(替代旧版拖拽)——被该阶段默认裁掉、但注册表里仍有的会写进
|
|
13
|
+
* add 名单加回;注册表里根本没有的(别的预设独有)加不进来,界面明确说明。
|
|
14
|
+
*/
|
|
15
|
+
import { createElement as h, useState, type ReactElement } from 'react'
|
|
16
|
+
import type { Config, Inventory, PhaseViewKey, Preview } from './types.ts'
|
|
17
|
+
import type { Translate } from './locales.ts'
|
|
18
|
+
import { PART_ORDER, withPhaseAdd, withPhaseExclude } from './presets.ts'
|
|
19
|
+
import { s } from './styles.ts'
|
|
20
|
+
import type { TriState } from './SectionsPane.tsx'
|
|
21
|
+
|
|
22
|
+
/** Panel 并行拉取的三阶段装配。 */
|
|
23
|
+
type PhaseViews = Record<PhaseViewKey, Preview | null>
|
|
24
|
+
|
|
25
|
+
/** 一次加回操作的反馈(ok = 已写入,warn = 什么都没改并说明原因)。 */
|
|
26
|
+
type Notice = { kind: 'ok' | 'warn'; text: string }
|
|
27
|
+
|
|
28
|
+
export function ToolsPane({ cfg, inv, phases, phase, syncAll, t, write }: {
|
|
29
|
+
cfg: Config
|
|
30
|
+
inv: Inventory | null
|
|
31
|
+
phases: PhaseViews | null
|
|
32
|
+
/** 当前阶段(状态在 Panel 持有,头部阶段按钮统一切换)。 */
|
|
33
|
+
phase: PhaseViewKey
|
|
34
|
+
/** 三态同步(默认关):勾选后隐藏 / 显示与加回对三个阶段一起生效(只作用于同名工具)。 */
|
|
35
|
+
syncAll?: boolean
|
|
36
|
+
t: Translate
|
|
37
|
+
write: (field: 'sections' | 'replace' | 'inject' | 'tools', value: unknown) => void
|
|
38
|
+
}): ReactElement {
|
|
39
|
+
const [filter, setFilter] = useState<TriState>('all')
|
|
40
|
+
const [notice, setNotice] = useState<Notice | null>(null)
|
|
41
|
+
|
|
42
|
+
// 某阶段自己的黑名单 / 加回名单(三份互不继承)。
|
|
43
|
+
const excludeOf = (key: PhaseViewKey): string[] => {
|
|
44
|
+
const tools = cfg.tools ?? {}
|
|
45
|
+
const list = key === 'bootstrap' ? tools.bootstrap?.exclude : key === 'compaction' ? tools.compaction?.exclude : tools.exclude
|
|
46
|
+
return list ?? []
|
|
47
|
+
}
|
|
48
|
+
const addOf = (key: PhaseViewKey): string[] => {
|
|
49
|
+
const tools = cfg.tools ?? {}
|
|
50
|
+
const list = key === 'bootstrap' ? tools.bootstrap?.add : key === 'compaction' ? tools.compaction?.add : tools.add
|
|
51
|
+
return list ?? []
|
|
52
|
+
}
|
|
53
|
+
// 一次写入同时落该阶段的 exclude 与 add(在同一份 tools 上叠完再写一次)。
|
|
54
|
+
const writeLists = (key: PhaseViewKey, exclude: string[], add: string[]): void => {
|
|
55
|
+
write('tools', withPhaseAdd(withPhaseExclude(cfg.tools ?? {}, key, exclude), key, add))
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// 三态同步写入(syncAll 开启时):对三个阶段各自的 exclude / add 名单做同一
|
|
59
|
+
// 变换,叠进一份 tools 后一次写入。变换返回 null = 该阶段无可做的改动,跳过。
|
|
60
|
+
const writeSynced = (
|
|
61
|
+
apply: (key: PhaseViewKey, exclude: string[], add: string[]) => { exclude: string[]; add: string[] } | null,
|
|
62
|
+
): void => {
|
|
63
|
+
let tools = cfg.tools
|
|
64
|
+
for (const k of PART_ORDER) {
|
|
65
|
+
const next = apply(k, excludeOf(k), addOf(k))
|
|
66
|
+
if (next === null) continue
|
|
67
|
+
tools = withPhaseExclude(withPhaseAdd(tools, k, next.add), k, next.exclude)
|
|
68
|
+
}
|
|
69
|
+
write('tools', tools)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// 该阶段的装配目录(进入本插件过滤的工具原文)与该预设注册表(能加回的来源)。
|
|
73
|
+
const catalogOf = (key: PhaseViewKey): string[] => (phases?.[key]?.baseTools ?? []).map((tool) => tool.name)
|
|
74
|
+
const isInCatalog = (key: PhaseViewKey, name: string): boolean => catalogOf(key).includes(name)
|
|
75
|
+
// 注册表三个阶段共享同一 scope,取第一个非空的即可。
|
|
76
|
+
const registry: Set<string> = (() => {
|
|
77
|
+
const list = PART_ORDER.map((key) => phases?.[key]?.registryTools).find((x) => Array.isArray(x) && x.length > 0) ?? []
|
|
78
|
+
return new Set(list)
|
|
79
|
+
})()
|
|
80
|
+
|
|
81
|
+
// 把一个工具放进某阶段(复制语义,绝不改动其它阶段):不在该阶段目录里就
|
|
82
|
+
// 加进 add 名单;hidden 决定它在目标阶段是可见还是隐藏。
|
|
83
|
+
const addToPhase = (key: PhaseViewKey, name: string, hidden: boolean): void => {
|
|
84
|
+
let exclude = excludeOf(key)
|
|
85
|
+
let add = addOf(key)
|
|
86
|
+
if (!isInCatalog(key, name) && !add.includes(name)) add = [...add, name]
|
|
87
|
+
exclude = hidden
|
|
88
|
+
? (exclude.includes(name) ? exclude : [...exclude, name])
|
|
89
|
+
: exclude.filter((x) => x !== name)
|
|
90
|
+
writeLists(key, exclude, add)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// 阶段分组里的行 = 该阶段进入过滤的目录 ∪ 已加回(但尚未出现在装配里)的工具。
|
|
94
|
+
const rowsOf = (key: PhaseViewKey): Array<{ name: string; description: string; hidden: boolean; added: boolean }> => {
|
|
95
|
+
const exclude = excludeOf(key)
|
|
96
|
+
const catalog = (phases?.[key]?.baseTools ?? []).map((tool) => ({
|
|
97
|
+
name: tool.name,
|
|
98
|
+
description: tool.description ?? '',
|
|
99
|
+
hidden: exclude.includes(tool.name),
|
|
100
|
+
added: false,
|
|
101
|
+
}))
|
|
102
|
+
const names = new Set(catalog.map((row) => row.name))
|
|
103
|
+
const added = addOf(key)
|
|
104
|
+
.filter((name) => !names.has(name))
|
|
105
|
+
.map((name) => ({ name, description: '', hidden: false, added: true }))
|
|
106
|
+
return [...catalog, ...added]
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// 勾选 / 取消勾选:只动这一个工具在本阶段的可见性。
|
|
110
|
+
const toggleHide = (key: PhaseViewKey, name: string, currentlyHidden: boolean): void => {
|
|
111
|
+
if (syncAll) {
|
|
112
|
+
// 三态同步:显示 = 各阶段 exclude 移除(绝不顺手把没加回的工具加进 add);
|
|
113
|
+
// 隐藏 = 已加回的撤销加回、原生的进该阶段 exclude。
|
|
114
|
+
writeSynced((_k, exclude, add) => {
|
|
115
|
+
if (currentlyHidden) return { exclude: exclude.filter((x) => x !== name), add }
|
|
116
|
+
if (add.includes(name)) return { exclude, add: add.filter((x) => x !== name) }
|
|
117
|
+
return exclude.includes(name) ? null : { exclude: [...exclude, name], add }
|
|
118
|
+
})
|
|
119
|
+
setNotice(null)
|
|
120
|
+
return
|
|
121
|
+
}
|
|
122
|
+
if (currentlyHidden) {
|
|
123
|
+
addToPhase(key, name, false)
|
|
124
|
+
} else {
|
|
125
|
+
let exclude = excludeOf(key)
|
|
126
|
+
let add = addOf(key)
|
|
127
|
+
if (add.includes(name)) add = add.filter((x) => x !== name)
|
|
128
|
+
else if (!exclude.includes(name)) exclude = [...exclude, name]
|
|
129
|
+
writeLists(key, exclude, add)
|
|
130
|
+
}
|
|
131
|
+
setNotice(null)
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// 从池加入某阶段(替代旧版拖拽:池 → 阶段)。注册表里没有的加不进来。
|
|
135
|
+
const addFromPool = (key: PhaseViewKey, name: string): void => {
|
|
136
|
+
if (!isInCatalog(key, name) && !registry.has(name)) {
|
|
137
|
+
setNotice({ kind: 'warn', text: t('toolNotInRegistry', { name }) })
|
|
138
|
+
return
|
|
139
|
+
}
|
|
140
|
+
if (syncAll) {
|
|
141
|
+
// 三态同步:三个阶段都让它出现(该阶段目录里没有的就写进各自的 add 名单)。
|
|
142
|
+
writeSynced((k, exclude, add) => ({
|
|
143
|
+
exclude: exclude.filter((x) => x !== name),
|
|
144
|
+
add: !isInCatalog(k, name) && !add.includes(name) ? [...add, name] : add,
|
|
145
|
+
}))
|
|
146
|
+
setNotice({ kind: 'ok', text: t('toolSyncShown', { name }) })
|
|
147
|
+
return
|
|
148
|
+
}
|
|
149
|
+
addToPhase(key, name, false)
|
|
150
|
+
setNotice({ kind: 'ok', text: isInCatalog(key, name) ? t('toolShown', { name, to: stageLabel(key) }) : t('toolAdded', { name, phase: stageLabel(key) }) })
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const stageLabel = (key: PhaseViewKey): string =>
|
|
154
|
+
key === 'bootstrap' ? t('phaseStageGuide') : key === 'compaction' ? t('phaseStageControlled') : t('phaseStageResident')
|
|
155
|
+
|
|
156
|
+
// 当前阶段的行 + 三态过滤(已启用 = 对模型可见)。
|
|
157
|
+
const rows = rowsOf(phase)
|
|
158
|
+
const onCount = rows.filter((row) => !row.hidden).length
|
|
159
|
+
const offCount = rows.length - onCount
|
|
160
|
+
const rowVisible = (row: { hidden: boolean }): boolean =>
|
|
161
|
+
filter === 'all' || (filter === 'on' ? !row.hidden : row.hidden)
|
|
162
|
+
|
|
163
|
+
const renderRow = (row: { name: string; description: string; hidden: boolean; added: boolean }): ReactElement | null => {
|
|
164
|
+
if (!rowVisible(row)) return null
|
|
165
|
+
return h('div', {
|
|
166
|
+
key: row.name,
|
|
167
|
+
style: { ...s.row, ...(row.hidden ? s.rowBlocked : {}) },
|
|
168
|
+
title: row.description.slice(0, 120),
|
|
169
|
+
}, [
|
|
170
|
+
h('input', {
|
|
171
|
+
type: 'checkbox',
|
|
172
|
+
checked: !row.hidden,
|
|
173
|
+
onChange: () => toggleHide(phase, row.name, row.hidden),
|
|
174
|
+
title: row.hidden ? t('hiddenOn') : t('hiddenOff'),
|
|
175
|
+
style: { margin: 0, cursor: 'pointer', flex: 'none' },
|
|
176
|
+
}),
|
|
177
|
+
h('div', { style: s.rowBody }, [
|
|
178
|
+
h('div', { style: s.rowTitle }, [
|
|
179
|
+
h('span', { style: s.code }, row.name),
|
|
180
|
+
row.added ? h('span', { style: s.badgeCustom }, t('toolAddedTag')) : null,
|
|
181
|
+
row.hidden ? h('span', { style: s.badgeBlocked }, t('hiddenOn')) : null,
|
|
182
|
+
]),
|
|
183
|
+
row.description !== '' ? h('div', { style: s.preview }, row.description.slice(0, 120)) : null,
|
|
184
|
+
]),
|
|
185
|
+
])
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const allTools = (inv?.tools ?? []).map((tool) => ({
|
|
189
|
+
name: tool.name,
|
|
190
|
+
description: typeof tool === 'string' ? '' : (tool.description ?? ''),
|
|
191
|
+
}))
|
|
192
|
+
|
|
193
|
+
return h('div', { style: s.colLeft }, [
|
|
194
|
+
h('div', { style: s.colScroll }, [
|
|
195
|
+
h('div', { style: s.muted }, t('toolsFourHint')),
|
|
196
|
+
notice ? h('div', { style: notice.kind === 'ok' ? s.noticeOk : s.noticeWarn }, notice.text) : null,
|
|
197
|
+
rows.map(renderRow),
|
|
198
|
+
rows.length === 0 ? h('div', { style: s.muted }, t('empty')) : null,
|
|
199
|
+
// 本系统全部工具:注册表的完整目录,只读池(每行可加入三个阶段)。
|
|
200
|
+
h('details', { style: s.injectBox }, [
|
|
201
|
+
h('summary', { style: { ...s.muted, cursor: 'pointer' } },
|
|
202
|
+
`${t('allToolsTitle')} (${allTools.length})`),
|
|
203
|
+
allTools.length === 0 ? h('div', { style: s.muted }, t('empty')) : null,
|
|
204
|
+
allTools.map((tool) => h('div', { key: tool.name, style: { ...s.row, opacity: 0.92 } }, [
|
|
205
|
+
h('div', { style: s.rowBody }, [
|
|
206
|
+
h('div', { style: s.rowTitle }, h('span', { style: s.code }, tool.name)),
|
|
207
|
+
h('div', { style: s.preview }, tool.description.slice(0, 120)),
|
|
208
|
+
]),
|
|
209
|
+
...PART_ORDER.map((key) => h('button', {
|
|
210
|
+
key,
|
|
211
|
+
style: s.arrow,
|
|
212
|
+
title: t('poolAddTitle', { phase: stageLabel(key) }),
|
|
213
|
+
onClick: () => addFromPool(key, tool.name),
|
|
214
|
+
}, t(key === 'bootstrap' ? 'phaseShortGuide' : key === 'active' ? 'phaseShortResident' : 'phaseShortControlled'))),
|
|
215
|
+
])),
|
|
216
|
+
]),
|
|
217
|
+
]),
|
|
218
|
+
// 左栏底部固定条:三态过滤(全部 / 已启用 = 可见 / 已停用 = 隐藏)。
|
|
219
|
+
h('div', { style: s.colFoot }, [
|
|
220
|
+
h('div', { style: s.seg }, [
|
|
221
|
+
h('button', { style: filter === 'all' ? s.segBtnActive : s.segBtn, onClick: () => setFilter('all') },
|
|
222
|
+
`${t('filterAll')} ${rows.length}`),
|
|
223
|
+
h('button', { style: filter === 'on' ? s.segBtnActive : s.segBtn, onClick: () => setFilter('on') },
|
|
224
|
+
`${t('filterOn')} ${onCount}`),
|
|
225
|
+
h('button', { style: filter === 'off' ? s.segBtnActive : s.segBtn, onClick: () => setFilter('off') },
|
|
226
|
+
`${t('filterOff')} ${offCount}`),
|
|
227
|
+
]),
|
|
228
|
+
]),
|
|
229
|
+
])
|
|
230
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* error-boundary — 把「面板渲染崩溃」关进笼子。
|
|
3
|
+
*
|
|
4
|
+
* 侧边栏入口是 createRoot 挂出来的独立 React 根,React 18 对渲染期异常的
|
|
5
|
+
* 默认处理是卸载整个根——面板里一个组件抛错,导航行按钮会跟着一起消失。
|
|
6
|
+
* 因此面板单独包一层:崩了只收起面板,导航按钮照常留着,真实错误打到控制台。
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { Component, type ErrorInfo, type ReactNode } from 'react'
|
|
10
|
+
|
|
11
|
+
/** 边界属性。 */
|
|
12
|
+
export interface ErrorBoundaryProps {
|
|
13
|
+
/** 出错时打在日志里的名字。 */
|
|
14
|
+
label: string
|
|
15
|
+
/** 崩溃后的替代内容(面板场景传 null,只收面板)。 */
|
|
16
|
+
fallback?: ReactNode
|
|
17
|
+
/** 捕获到错误时的回调(用于顺手把面板关掉)。 */
|
|
18
|
+
onError?: (error: Error) => void
|
|
19
|
+
children: ReactNode
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
interface ErrorBoundaryState {
|
|
23
|
+
error: Error | null
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** 只兜渲染/生命周期异常的错误边界(不兜事件回调与异步)。 */
|
|
27
|
+
export class ErrorBoundary extends Component<ErrorBoundaryProps, ErrorBoundaryState> {
|
|
28
|
+
override state: ErrorBoundaryState = { error: null }
|
|
29
|
+
|
|
30
|
+
static getDerivedStateFromError(error: Error): ErrorBoundaryState {
|
|
31
|
+
return { error }
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
override componentDidCatch(error: Error, info: ErrorInfo): void {
|
|
35
|
+
// 打出组件栈:没有它,崩溃只会表现为「界面少了一块」,无从下手。
|
|
36
|
+
console.error(`[dsh-prompt-customizer] ${this.props.label} 渲染崩溃:`, error, info.componentStack ?? '')
|
|
37
|
+
try {
|
|
38
|
+
this.props.onError?.(error)
|
|
39
|
+
} catch (callbackError) {
|
|
40
|
+
console.error('[dsh-prompt-customizer] 错误边界回调失败:', callbackError)
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
override render(): ReactNode {
|
|
45
|
+
if (this.state.error !== null) return this.props.fallback ?? null
|
|
46
|
+
return this.props.children
|
|
47
|
+
}
|
|
48
|
+
}
|