dsh-prompt-customizer 0.1.0 → 0.2.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.
@@ -0,0 +1,125 @@
1
+ /** Pure preset / section helpers shared by the client UI and the node tests. */
2
+ import type { Config, Inventory, PresetData } from './types.ts'
3
+
4
+ export interface Section {
5
+ name: string
6
+ order: number
7
+ text?: string
8
+ active: boolean
9
+ replaced: boolean
10
+ }
11
+
12
+ /** The config fields a preset apply must write back to the settings scope. */
13
+ export interface ConfigPatch {
14
+ sections: string[]
15
+ replace: Record<string, string>
16
+ inject: Array<{ name: string; order: number; text: string }>
17
+ tools: { exclude: string[]; include: string[] }
18
+ }
19
+
20
+ /** Merge the inventory sections with the user's inject list, sorted by order. */
21
+ export function mergeSections(inv: Inventory | null, cfg: Config, blockedNames: ReadonlySet<string>): Section[] {
22
+ const map = new Map<string, Section>()
23
+ for (const sec of inv?.sections ?? []) map.set(sec.name, sec)
24
+ for (const item of cfg.inject ?? []) {
25
+ const existing = map.get(item.name)
26
+ if (existing) map.set(item.name, { ...existing, order: item.order })
27
+ else map.set(item.name, {
28
+ name: item.name,
29
+ order: item.order,
30
+ text: item.text || '<动态生成>',
31
+ active: !blockedNames.has(item.name),
32
+ replaced: false,
33
+ })
34
+ }
35
+ return [...map.values()].sort((a, b) => a.order - b.order)
36
+ }
37
+
38
+ /**
39
+ * Resolve a preset's relative order into an absolute ordered list (0..n-1).
40
+ * Sections with no anchor (or whose anchor is absent) come first in preset
41
+ * order; each anchored section is inserted right after its anchor; any
42
+ * remaining sections (cycles) are appended.
43
+ */
44
+ export function resolveOrder(presetOrder: PresetData['order']): Array<{ name: string; order: number; text: string }> {
45
+ const list = presetOrder ?? []
46
+ const afterMap = new Map<string, string | undefined>()
47
+ const textMap = new Map<string, string>()
48
+ for (const sec of list) {
49
+ afterMap.set(sec.name, sec.after)
50
+ textMap.set(sec.name, sec.text)
51
+ }
52
+
53
+ const result: string[] = []
54
+ const placed = new Set<string>()
55
+
56
+ for (const sec of list) {
57
+ const anchor = afterMap.get(sec.name)
58
+ if (!anchor || !afterMap.has(anchor)) { result.push(sec.name); placed.add(sec.name) }
59
+ }
60
+
61
+ let changed = true
62
+ while (changed) {
63
+ changed = false
64
+ for (const sec of list) {
65
+ if (placed.has(sec.name)) continue
66
+ const anchor = afterMap.get(sec.name)
67
+ if (anchor && placed.has(anchor)) {
68
+ result.splice(result.indexOf(anchor) + 1, 0, sec.name)
69
+ placed.add(sec.name)
70
+ changed = true
71
+ }
72
+ }
73
+ }
74
+
75
+ for (const sec of list) {
76
+ if (!placed.has(sec.name)) { result.push(sec.name); placed.add(sec.name) }
77
+ }
78
+
79
+ return result.map((name, i) => ({ name, order: i, text: textMap.get(name) ?? '' }))
80
+ }
81
+
82
+ /**
83
+ * Build a preset snapshot from the current config. The order list is derived
84
+ * from the MERGED section list (not just `cfg.inject`) so that a preset saved
85
+ * after only blocking sections (without reordering) still carries the full
86
+ * ordered set, and thus a meaningful "active list" when applied.
87
+ */
88
+ export function buildPresetData(cfg: Config, merged: Section[]): PresetData {
89
+ const order = merged.map((sec, i) => ({
90
+ name: sec.name,
91
+ after: i > 0 ? merged[i - 1].name : undefined,
92
+ text: sec.text ?? '',
93
+ }))
94
+ return {
95
+ sections: cfg.sections,
96
+ replace: cfg.replace,
97
+ order,
98
+ tools: cfg.tools,
99
+ }
100
+ }
101
+
102
+ /**
103
+ * Compute the config patch when applying a preset:
104
+ * - same-name sections are overridden (the order list drives this at runtime)
105
+ * - preset sections missing from the current prompt are added
106
+ * - current sections NOT in the preset's ordered list are disabled by default
107
+ * - only the preset's ACTIVE sections (in the order list but not in its block
108
+ * list) are unblocked; the preset's own blocked sections stay blocked.
109
+ */
110
+ export function applyPresetData(data: PresetData, cfg: Config, currentNames: ReadonlySet<string>): ConfigPatch {
111
+ const presetOrder = data.order ?? []
112
+ const presetNames = new Set(presetOrder.map((x) => x.name))
113
+ const blocked = new Set(data.sections ?? [])
114
+ const activeNames = new Set([...presetNames].filter((n) => !blocked.has(n)))
115
+
116
+ for (const name of currentNames) if (!presetNames.has(name)) blocked.add(name)
117
+ for (const name of activeNames) blocked.delete(name)
118
+
119
+ return {
120
+ sections: [...blocked],
121
+ replace: { ...(cfg.replace ?? {}), ...(data.replace ?? {}) },
122
+ inject: resolveOrder(presetOrder),
123
+ tools: { exclude: data.tools?.exclude ?? [], include: data.tools?.include ?? [] },
124
+ }
125
+ }
package/README.zh-CN.md DELETED
@@ -1,107 +0,0 @@
1
- # dsh-prompt-customizer
2
-
3
- 一个 [DeepSeek Harness](https://github.com/deepseek-ai/deepseek-harness) (dsh) 插件,让你通过设置面板 UI 控制**系统提示词**和**工具目录**。
4
-
5
- 其他插件注入的提示词段可能会污染你的系统提示词。本插件让你可以按名称**屏蔽**、**替换**、**注入**和**排序**提示词段,并从模型目录中**隐藏工具**——全部实时生效,无需改动其他插件。
6
-
7
- ## 功能特性
8
-
9
- - **提示词段**
10
- - **屏蔽**指定名称的段(从组装后的系统提示词中移除)。
11
- - **替换**某段的文本(编辑时会回显原始文本)。
12
- - **注入**全新的段。
13
- - 通过 **↑/↓ 箭头**或**拖拽**(HTML5)排序。顺序以虚拟的 0 起始下标存储,因此不会出现重复或小数 order。
14
- - **工具**
15
- - **黑名单**(`exclude`):隐藏列出的工具。
16
- - **白名单**(`include`):仅保留列出的工具(优先于 exclude)。
17
- - 只影响面向模型的目录,工具与路由仍正常工作。
18
- - **预设**
19
- - **保存**当前定制为预设(完整快照)。
20
- - **应用**预设:同名段被覆盖、预设独有段被添加、预设名单之外的当前段默认被屏蔽。
21
- - **导出 / 导入**预设为 JSON(完整快照)。
22
- - 预设存储**相对顺序**(每段记录它跟在哪个段之后),因此可跨不同段集合的提示词移植。
23
- - 本地可保存多个预设,同一时间只能激活一个。
24
-
25
- ## 安装
26
-
27
- 从 npm 安装:
28
-
29
- ```bash
30
- dsh plugin add dsh-prompt-customizer
31
- ```
32
-
33
- 或从本地目录安装:
34
-
35
- ```bash
36
- dsh plugin add /path/to/dsh-prompt-customizer
37
- ```
38
-
39
- 安装后,打开 dsh Web UI → **设置** → 侧边栏中的 **提示词定制**。
40
-
41
- ## 使用说明
42
-
43
- ### 提示词 Tab
44
-
45
- 每一行代表一个提示词段,包含:
46
-
47
- - **复选框**:屏蔽 / 取消屏蔽。
48
- - **替换**按钮:编辑该段文本(默认回显原始文本)。
49
- - **↑/↓ 箭头**和**拖拽手柄**:调整顺序。
50
- - `#N` 徽标显示该段的虚拟顺序(0 起始的位置)。
51
-
52
- 底部的 **注入新段** 区域可按名称、顺序和文本添加全新段。
53
-
54
- ### 工具 Tab
55
-
56
- 勾选工具以隐藏(黑名单),或切换到白名单模式仅保留勾选的工具。
57
-
58
- ### 预设 Tab
59
-
60
- - **保存当前为预设**:捕获当前的提示词 / 工具定制。
61
- - **应用**:激活某个预设(覆盖同名段、添加预设独有段、屏蔽预设名单之外的当前段)。
62
- - **导出**:将预设下载为 JSON。
63
- - **导入**:加载预设 JSON 文件(同名预设会被跳过)。
64
-
65
- ## 配置
66
-
67
- 插件配置存储在 dsh 设置文档的 `prompt-customizer` 命名空间下(`~/.dsh/settings.yaml`)。你也可以直接编辑:
68
-
69
- ```yaml
70
- prompt-customizer:
71
- # 按名称屏蔽这些提示词段。
72
- sections:
73
- - plugin:some-plugin
74
- # 按名称替换某段文本(保留原始顺序)。
75
- replace:
76
- system: 'You are a helpful coding assistant.'
77
- # 注入 / 覆盖段(名称 + 顺序 + 文本)。
78
- inject:
79
- - name: tool:read
80
- order: 0
81
- text: ''
82
- # 工具目录过滤。
83
- tools:
84
- exclude: []
85
- include: []
86
- ```
87
-
88
- | 字段 | 类型 | 说明 |
89
- | --- | --- | --- |
90
- | `sections` | `string[]` | 要屏蔽的提示词段名称。 |
91
- | `replace` | `Record<string, string>` | 每个段名称对应的替换文本。 |
92
- | `inject` | `{name, order, text}[]` | 要注入或覆盖的段(order 控制拼接位置)。 |
93
- | `tools.exclude` | `string[]` | 要隐藏的工具名称。 |
94
- | `tools.include` | `string[]` | 非空时仅保留这些工具(优先于 `exclude`)。 |
95
-
96
- ## 开发
97
-
98
- ```bash
99
- npm install
100
- npm run check # typecheck + build
101
- ```
102
-
103
- 浏览器端使用 [tsdown](https://github.com/rolldown/tsdown) 打包为 `client/client.js`(`__ModuleLoader__` factory bundle)。宿主端代码位于 `lib/`。
104
-
105
- ## 许可证
106
-
107
- MIT