dsh-soul 0.6.2 → 0.7.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/lib/config.mjs CHANGED
@@ -16,9 +16,12 @@ export const DEFAULT_CONFIG = {
16
16
  bio: '',
17
17
  // 回复风格和语调(v0.2.0 起合并为单一选项)
18
18
  style: 'professional',
19
- // 特质:标题和列表 / 表情符号(default=默认,more=增强,less=减弱)
19
+ // 特质:标题和列表 / 表情符号 / 表格(default=默认,more=增强,less=减弱)
20
20
  headingLists: 'default',
21
21
  emoji: 'default',
22
+ tables: 'default',
23
+ // 回复长度偏好:concise=简洁,normal=适中(不产生任何提示词文案),detailed=详尽
24
+ replyLength: 'normal',
22
25
  language: 'zh',
23
26
  customInstructions: '',
24
27
  // set_persona 工具确认模式:开启后 Agent 的人设修改返回待确认提议而不落盘
@@ -33,9 +36,14 @@ export const DEFAULT_CONFIG = {
33
36
  // 合法的回复风格和语调取值(供工具参数校验)
34
37
  export const STYLE_VALUES = ['professional', 'casual', 'humorous', 'roast', 'efficient']
35
38
 
36
- // 特质合法取值
39
+ // 特质合法取值(标题和列表 / 表情符号 / 表格 共用三档)
37
40
  export const TRAIT_VALUES = ['default', 'more', 'less']
38
41
 
42
+ // 回复长度偏好合法取值。
43
+ // normal(适中)刻意不产生任何提示词文案 —— 与特质的 default 同理,
44
+ // 代表「不额外约束」,保证它作为默认值时现有用户的行为零变化。
45
+ export const REPLY_LENGTH_VALUES = ['concise', 'normal', 'detailed']
46
+
39
47
  // 输出语言合法取值
40
48
  export const LANGUAGE_VALUES = ['zh', 'en']
41
49
 
@@ -65,8 +73,30 @@ const TEXT_FIELDS = ['nickname', 'occupation', 'bio', 'customInstructions']
65
73
 
66
74
  // ==================== 人设预设 ====================
67
75
 
68
- // 人设预设覆盖的配置字段(enabled 为全局开关、不进入预设)
69
- export const PERSONA_FIELDS = ['nickname', 'occupation', 'bio', 'style', 'headingLists', 'emoji', 'language', 'customInstructions']
76
+ // 「关于你」:使用者**本人**的身份信息(昵称 / 职业 / 介绍)。
77
+ //
78
+ // 它与人设是两码事:人设描述 **Agent 怎么说话**,关于你描述 **你在跟谁说话**。
79
+ // 所以任何预设(内置或自建)都不声明、也不应用这三个字段 —— 切换预设只换回复风格,
80
+ // 不该动到你的资料。这条不靠「约定」而靠结构保证:三个字段根本不在 PERSONA_FIELDS
81
+ // 里,应用的取键、匹配的判据、保存的快照、磁盘的归一化全部按白名单走。
82
+ //
83
+ // v0.7.1 起自建预设与内置预设语义一致。此前自建预设是「保存那一刻的完整配置快照」,
84
+ // 含这三个字段,应用时会把它们一并写回(旧数据由 normalizePersonas 迁移剥离)。
85
+ export const PROFILE_FIELDS = ['nickname', 'occupation', 'bio']
86
+
87
+ // 人设预设覆盖的配置字段:**只含 Agent 的人格与回复风格**,不含「关于你」(见上)。
88
+ // - language 在范围内:它决定 Agent 用什么语言作答,属于人格的一部分(自建预设
89
+ // 会保存与还原它;内置预设作为通用人格刻意不声明,见 lib/personas.mjs 约定 3)。
90
+ // - enabled 是全局开关,不进预设 —— 预设不该替你关掉个性化。
91
+ export const PERSONA_FIELDS = ['style', 'headingLists', 'emoji', 'tables', 'replyLength', 'language', 'customInstructions']
92
+
93
+ // 参与 compilePrompt 的字段 = 总开关 + 关于你 + 人设。
94
+ // 刻意与 PERSONA_FIELDS 分开放:**预设能覆盖的字段**与**编译实际读取的字段**不是一回事,
95
+ // 混成一个常量会让「预设范围」的增删意外改变提示词预览的脏检查范围。
96
+ export const PROMPT_INPUT_FIELDS = ['enabled', ...PROFILE_FIELDS, ...PERSONA_FIELDS]
97
+
98
+ // 预设条目里的元数据键:不属于人设维度,不参与比较与匹配(builtin 仅存在于合并视图)
99
+ export const PERSONA_META_KEYS = new Set(['updatedAt', 'builtin'])
70
100
 
71
101
  // 人设预设名称上限
72
102
  export const PERSONA_NAME_MAX = 30
@@ -83,7 +113,13 @@ export function sanitizePersonaName(raw) {
83
113
  return name
84
114
  }
85
115
 
86
- // 归一化磁盘上的预设库:只保留普通对象条目,剔除保留键与非对象条目
116
+ // 归一化磁盘上的预设库:只保留普通对象条目,剔除保留键与非对象条目。
117
+ //
118
+ // 同时按白名单过滤条目字段(v0.7.1 迁移):只留 PERSONA_FIELDS 内的人设字段与
119
+ // PERSONA_META_KEYS 里的元数据。历史版本的自建预设是完整配置快照,带着
120
+ // nickname / occupation / bio —— 它们已不再是预设的一部分,留着会让人误以为
121
+ // 「应用这个预设能恢复我的昵称」。丢弃无损:这些值同时存在于活动配置里
122
+ // (预设只是保存那一刻的拷贝),迁移后的结果会在下一次写入时落盘。
87
123
  export function normalizePersonas(raw) {
88
124
  if (!raw || typeof raw !== 'object' || Array.isArray(raw)) return {}
89
125
  const out = {}
@@ -91,7 +127,11 @@ export function normalizePersonas(raw) {
91
127
  if (FORBIDDEN_PERSONA_NAMES.has(key)) continue
92
128
  const entry = raw[key]
93
129
  if (!entry || typeof entry !== 'object' || Array.isArray(entry)) continue
94
- out[key] = { ...entry }
130
+ const clean = {}
131
+ for (const field of Object.keys(entry)) {
132
+ if (PERSONA_FIELDS.includes(field) || PERSONA_META_KEYS.has(field)) clean[field] = entry[field]
133
+ }
134
+ out[key] = clean
95
135
  }
96
136
  return out
97
137
  }
@@ -127,6 +167,8 @@ export function migrateConfig(raw) {
127
167
  // 特质字段校验:脏数据回退为默认值
128
168
  if (!TRAIT_VALUES.includes(config.headingLists)) config.headingLists = 'default'
129
169
  if (!TRAIT_VALUES.includes(config.emoji)) config.emoji = 'default'
170
+ if (!TRAIT_VALUES.includes(config.tables)) config.tables = DEFAULT_CONFIG.tables
171
+ if (!REPLY_LENGTH_VALUES.includes(config.replyLength)) config.replyLength = DEFAULT_CONFIG.replyLength
130
172
  // 确认模式:非布尔脏数据回退为默认值
131
173
  if (typeof config.requireToolConfirmation !== 'boolean') config.requireToolConfirmation = DEFAULT_CONFIG.requireToolConfirmation
132
174
  // 光轨字段脏数据回退:布尔 / 枚举 / 6 位十六进制颜色
@@ -214,6 +256,8 @@ export function sanitizeConfig(raw) {
214
256
  ['style', STYLE_VALUES, '回复风格和语调'],
215
257
  ['headingLists', TRAIT_VALUES, '特质·标题和列表'],
216
258
  ['emoji', TRAIT_VALUES, '特质·表情符号'],
259
+ ['tables', TRAIT_VALUES, '特质·表格'],
260
+ ['replyLength', REPLY_LENGTH_VALUES, '回复长度偏好'],
217
261
  ['language', LANGUAGE_VALUES, '输出语言'],
218
262
  ['trailSpeed', TRAIL_SPEED_VALUES, '光轨流动速度'],
219
263
  ['trailWidth', TRAIL_WIDTH_VALUES, '光轨光带粗细']
@@ -221,8 +265,12 @@ export function sanitizeConfig(raw) {
221
265
  for (const [key, values, label] of ENUM_CHECKS) {
222
266
  if (!(key in raw)) continue
223
267
  const value = raw[key]
224
- if (typeof value === 'string' && values.includes(value)) {
225
- patch[key] = value
268
+ // 枚举值同样 trim:文本字段(TEXT_FIELDS)本来就 trim,枚举不 trim 会让
269
+ // `/soul set style= humorous`(= 后带空格,解析出 ' humorous')被拒,
270
+ // 而 `bio= 张三` 却能成功 —— 同一命令里两套规则。
271
+ const text = typeof value === 'string' ? value.trim() : ''
272
+ if (values.includes(text)) {
273
+ patch[key] = text
226
274
  } else {
227
275
  errors[key] = `${label}必须是 ${values.join(' / ')} 之一`
228
276
  }
package/lib/injection.mjs CHANGED
@@ -21,10 +21,10 @@
21
21
  // 取 `plugin:dsh-soul` 而非裸名,是为了与 DSH 自带 v3→v4 迁移对第三方插件生成的
22
22
  // `plugin:${plugin}`(producerKind 的回落分支)保持一致:升级前后的历史事件与
23
23
  // 新增事件归属同一个生产者,而不是在同一个会话里出现两个身份。
24
- export const INJECTION_PRODUCER_KIND = 'plugin:dsh-soul'
24
+ const INJECTION_PRODUCER_KIND = 'plugin:dsh-soul'
25
25
 
26
26
  // 快照区块名,与 systemPrompt section 的 name 对应
27
- export const INJECTION_SECTION_NAME = 'soul:persona'
27
+ const INJECTION_SECTION_NAME = 'soul:persona'
28
28
 
29
29
  /**
30
30
  * 构造注入消息的 source:自持 kind + snapshot 形态 + 命名区块。
@@ -0,0 +1,184 @@
1
+ // lib/personas.mjs — dsh-soul 内置人设预设(纯数据 + 纯函数,无任何依赖)
2
+ //
3
+ // 职责:
4
+ // - 内置人设清单(BUILTIN_PERSONAS)
5
+ // - 用户预设库与内置预设的合并(mergePersonas / resolvePersona)
6
+ //
7
+ // 四条设计约定(verify-config.mjs 逐条固化,改动时勿破坏):
8
+ //
9
+ // 1) **预设只描述 Agent 的人格,不含「关于你」**。「关于你」(昵称 / 职业 / 介绍)
10
+ // 是使用者本人的身份信息,「人设」是 Agent 的回复风格 —— 两码事。切换预设应该
11
+ // 只换说话方式,不该动你的资料。这条由 lib/config.mjs 的 PROFILE_FIELDS /
12
+ // PERSONA_FIELDS 结构性保证:三个字段不在 PERSONA_FIELDS 里,所以不止内置预设,
13
+ // 连**自建**预设也不会声明或应用它们(保存走 personaSnapshotOf、应用走
14
+ // pickPersonaValues,两者都只遍历 PERSONA_FIELDS)。v0.7.1 之前自建预设是
15
+ // 「完整配置快照」、会写回这三个字段,旧数据由 normalizePersonas 迁移剥离。
16
+ //
17
+ // 2) **只声明要覆盖的字段**。预设是「部分覆盖」语义:应用时只写入预设实际
18
+ // 声明的键,未声明的保持用户当前值。所以内置预设只写它真正要改变的人格维度。
19
+ //
20
+ // 3) **内置预设一律不声明 `language`**。输出语言在预设范围内(自建预设会保存
21
+ // 与还原它),但内置预设是给任何语言用户共用的通用人格:写死 zh 会让英文用户
22
+ // 应用一次就被强行切回中文。省略即「不动它」。
23
+ //
24
+ // 4) **内置预设不进磁盘**。它们只存在于本文件,磁盘上的 `config.personas`
25
+ // 仅存用户自己保存的预设。好处有三:升级时内置内容自动更新(不会被用户
26
+ // 的旧副本覆盖)、用户无法删除或篡改内置项、`$DSH_HOME/soul-config.json`
27
+ // 保持干净。写入路径(保存 / 删除)对内置名一律拒绝,因此不存在「删了又
28
+ // 回来」的困惑状态。
29
+ //
30
+ // 名称冲突规则:**内置优先**。磁盘上若残留同名条目(手改文件或历史数据),
31
+ // 合并时会被内置内容遮蔽,且该名字在 UI 上不可删除——不变量是「内置名永远指向
32
+ // 内置内容」。保存路径同样拒绝内置名,用户想微调就换个名字存。
33
+
34
+ import { PERSONA_FIELDS, PERSONA_META_KEYS } from './config.mjs'
35
+
36
+ // ==================== 内置人设 ====================
37
+ //
38
+ // 每条只写与默认值不同的字段。默认值见 lib/config.mjs 的 DEFAULT_CONFIG:
39
+ // style=professional / headingLists=default / emoji=default / tables=default
40
+ // / replyLength=normal(normal 与 default 均不产生任何提示词文案)
41
+ //
42
+ // customInstructions 是内置人设的主要载体:枚举字段只能表达「更啰嗦 / 更简洁」
43
+ // 这类粗粒度倾向,人格化的行为差异要靠自由文本。写法上只描述**行为规则**
44
+ // (先问什么、先给什么、按什么顺序展开),避免与 style 档位的文案重复。
45
+
46
+ export const BUILTIN_PERSONAS = {
47
+ '苏格拉底式提问者': {
48
+ style: 'professional',
49
+ headingLists: 'less',
50
+ customInstructions:
51
+ '用提问推进对话,而不是直接给答案。' +
52
+ '先用自己的话复述你对问题的理解,确认无误后再提出 1-2 个能暴露关键假设的追问;' +
53
+ '追问要具体、指向可验证的事实,不要问「你确定吗」这类空泛问题。' +
54
+ '只有当用户明确要求结论,或已经追问过两轮时,才给出答案——此时要一并说明结论依赖哪些前提。'
55
+ },
56
+ '极简主义者': {
57
+ style: 'efficient',
58
+ headingLists: 'less',
59
+ emoji: 'less',
60
+ tables: 'less',
61
+ replyLength: 'concise',
62
+ customInstructions:
63
+ '只给结论和必要依据,砍掉一切铺垫、客套与重复。' +
64
+ '能用一句话说清就不要用三句;不主动扩展话题,也不追加未被问及的注意事项。' +
65
+ '若信息不足以回答,只问一个最关键的问题,不要罗列一串待确认项。'
66
+ },
67
+ '资深架构师': {
68
+ style: 'professional',
69
+ headingLists: 'more',
70
+ tables: 'more',
71
+ replyLength: 'detailed',
72
+ customInstructions:
73
+ '以系统设计视角回答:先说清约束、假设与规模量级,再给方案。' +
74
+ '至少对比两条技术路线,用表格列出权衡维度(复杂度、可维护性、性能、迁移成本、团队门槛)。' +
75
+ '主动指出边界条件、失败模式与长期演进风险;' +
76
+ '涉及取舍时给出明确推荐并说明理由,不要以「都可以,看需求」收尾。'
77
+ },
78
+ '教学型讲解者': {
79
+ style: 'casual',
80
+ headingLists: 'more',
81
+ replyLength: 'detailed',
82
+ customInstructions:
83
+ '按「是什么 → 为什么 → 怎么用」的顺序讲解,先建立直觉再引入术语。' +
84
+ '开头用一个具体例子或类比,结尾用一句话总结关键点。' +
85
+ '遇到专业名词先解释含义,不要假设用户已掌握前置知识;' +
86
+ '但每个概念只讲一遍,不要为了周全而反复换说法重复同一件事。'
87
+ },
88
+ '严格代码审阅者': {
89
+ style: 'professional',
90
+ headingLists: 'more',
91
+ tables: 'more',
92
+ emoji: 'less',
93
+ replyLength: 'detailed',
94
+ customInstructions:
95
+ '按代码审阅者的标准挑问题,按严重度分组:正确性 / 安全 / 性能 / 可维护性 / 风格。' +
96
+ '每条指出具体位置、触发条件与修复方向,不要只说「这里可能有问题」。' +
97
+ '优先报告会导致错误结果、数据损坏或安全风险的问题。' +
98
+ '没有发现问题时,明确说明检查过哪些方面,不要为凑数编造无关痛痒的意见。'
99
+ },
100
+ '头脑风暴伙伴': {
101
+ style: 'casual',
102
+ headingLists: 'more',
103
+ customInstructions:
104
+ '先发散再收敛:一次性给出若干方向明显不同的方案,而不是同一方案的变体。' +
105
+ '每个方案用一句话说清核心思路与主要代价。' +
106
+ '发散阶段不要否定任何想法;给出方案后再标出你个人最看好的一个及理由。' +
107
+ '避免只给显而易见的选项,至少包含一个不常规但有可行性的思路。'
108
+ },
109
+ '自驱型协作者': {
110
+ style: 'professional',
111
+ headingLists: 'more',
112
+ emoji: 'more',
113
+ customInstructions:
114
+ '先自己动手再开口:把能读的读了、能查的查了、能验证的验证掉——读文件、看上下文、检索资料都算;' +
115
+ '确实卡住才提问,并把已排查的范围与还缺什么一并说明,目标是带着方案回来而不是带着问题。' +
116
+ '给出明确判断:比较方案时指明你更推荐哪一条及理由,不以「都可以,看需求」收尾;有不同意见就直接讲,摆出分歧点与依据。' +
117
+ '以可核实的依据为准:涉及事实、接口、参数、版本时先查证再断言,查不到就明说「不确定」,不用听起来合理的说法填空。' +
118
+ '实质优先于形式:直接从结论或第一步行动开始,不复述用户已经说清的需求,也不用「好问题」「我很乐意帮忙」这类开场垫话——' +
119
+ '礼貌体现在措辞上,不体现在铺垫上。' +
120
+ '区分两类动作:读文件、检索、整理这类内部动作大胆做;发消息、提交、发布、删除这类对外动作先确认。不发送未经整理的回复。'
121
+ }
122
+ }
123
+
124
+ // ==================== 合并与查询 ====================
125
+
126
+ // 预设实际声明的人设字段名(只保留 PERSONA_FIELDS 白名单内的键)。
127
+ // 白名单同时过滤掉元数据与「关于你」——后者连历史数据里残留的也不会被算作声明,
128
+ // 于是匹配对昵称 / 职业 / 介绍天然免疫(见文件头约定 1)。
129
+ export function declaredPersonaKeys(entry) {
130
+ return Object.keys(entry || {}).filter(
131
+ (key) => !PERSONA_META_KEYS.has(key) && PERSONA_FIELDS.includes(key)
132
+ )
133
+ }
134
+
135
+ // 预设是否与当前活动配置匹配(★ 标记的判据)。
136
+ //
137
+ // 预设是「部分覆盖」语义——应用时只写它声明过的字段,所以判定也必须只比较
138
+ // 声明过的字段。若按 PERSONA_FIELDS 全量比较,内置预设会永远匹配不上:它们
139
+ // 刻意只声明自己真正要改变的人格维度,没声明的含义是「不动它」,而不是「要求
140
+ // 它是默认值」。
141
+ //
142
+ // 一个字段都没声明的条目视为不匹配——否则它会匹配任何配置。
143
+ export function personaMatches(entry, config) {
144
+ const keys = declaredPersonaKeys(entry)
145
+ if (keys.length === 0) return false
146
+ return keys.every((key) => entry[key] === config[key])
147
+ }
148
+
149
+ // 内置人设名(按定义顺序返回;UI 列表用它把内置项排在前面的展示区)
150
+ export function builtinPersonaNames() {
151
+ return Object.keys(BUILTIN_PERSONAS)
152
+ }
153
+
154
+ // 是否为内置人设名(保存与删除路径的准入判据)
155
+ export function isBuiltinPersona(name) {
156
+ return typeof name === 'string' && Object.prototype.hasOwnProperty.call(BUILTIN_PERSONAS, name)
157
+ }
158
+
159
+ // 合并用户预设库与内置预设,返回给前端的展示视图。
160
+ // 内置条目额外带 `builtin: true` —— 前端据此渲染「内置」标记并隐藏删除按钮;
161
+ // 用户预设不带该字段。内置优先,同名用户条目被遮蔽(见文件头约定 3)。
162
+ export function mergePersonas(userPersonas) {
163
+ const out = {}
164
+ for (const name of builtinPersonaNames()) {
165
+ out[name] = { ...BUILTIN_PERSONAS[name], builtin: true }
166
+ }
167
+ if (userPersonas && typeof userPersonas === 'object' && !Array.isArray(userPersonas)) {
168
+ for (const name of Object.keys(userPersonas)) {
169
+ if (isBuiltinPersona(name)) continue
170
+ out[name] = { ...userPersonas[name] }
171
+ }
172
+ }
173
+ return out
174
+ }
175
+
176
+ // 按名称取预设条目(先内置后用户),不存在返回 null。
177
+ export function resolvePersona(name, userPersonas) {
178
+ if (typeof name !== 'string') return null
179
+ if (isBuiltinPersona(name)) return { ...BUILTIN_PERSONAS[name] }
180
+ if (userPersonas && Object.prototype.hasOwnProperty.call(userPersonas, name)) {
181
+ return { ...userPersonas[name] }
182
+ }
183
+ return null
184
+ }
package/lib/store.mjs ADDED
@@ -0,0 +1,125 @@
1
+ // lib/store.mjs — dsh-soul 配置持久化(文件 IO,无 cordis 依赖)
2
+ //
3
+ // 职责:
4
+ // - 读取配置文件,并把「文件不存在」与「文件存在但读不出来 / 不是合法 JSON」区分开
5
+ // - 配置文件的原子写入(临时文件 + rename)
6
+ // - 损坏文件的备份
7
+ //
8
+ // 本模块只负责如实报告结果,不替调用方决定如何回退;可被 scripts/verify-store.mjs
9
+ // 独立加载并针对真实文件系统测试。
10
+
11
+ import { readFile, writeFile, mkdir, rename, copyFile } from 'node:fs/promises'
12
+ import { dirname } from 'node:path'
13
+
14
+ // 损坏配置的备份路径(与配置文件同目录)
15
+ export function corruptBackupPath(filePath) {
16
+ return `${filePath}.corrupt`
17
+ }
18
+
19
+ // 把损坏的配置文件另存为 <file>.corrupt,供用户找回内容。
20
+ // 备份失败返回 null 而不抛错 —— 备份是尽力而为,不能因为备份失败就让读取路径也失败。
21
+ async function backupCorruptFile(filePath) {
22
+ const target = corruptBackupPath(filePath)
23
+ try {
24
+ await copyFile(filePath, target)
25
+ return target
26
+ } catch {
27
+ return null
28
+ }
29
+ }
30
+
31
+ // 把不可用的配置文件**移开**(而非复制),备份到 <file>.corrupt,返回备份路径。
32
+ //
33
+ // 与 backupCorruptFile 的差别只有一个,但这个差别很关键:移开之后原路径不再存在,
34
+ // 读取随即回到「首次运行」的正常分支。这是「重置」这类**刻意丢弃**动作需要的语义——
35
+ // 只复制而保留原文件的话,下一次读取仍会判定为损坏,于是重置依然被拒绝,
36
+ // 用户除手删文件外没有任何出路(这正是本函数存在的原因)。
37
+ //
38
+ // 目标已存在时会被 rename 覆盖(Node 在 Windows 用 MOVEFILE_REPLACE_EXISTING)。
39
+ // 文件本就不存在 / 移不动时返回 null —— 调用方据此判断是否真的腾出了位置。
40
+ export async function moveAsideConfigFile(filePath) {
41
+ const target = corruptBackupPath(filePath)
42
+ try {
43
+ await rename(filePath, target)
44
+ return target
45
+ } catch {
46
+ return null
47
+ }
48
+ }
49
+
50
+ // 读取并解析配置文件,返回 { value, exists, failure }:
51
+ //
52
+ // | 情况 | value | exists | failure |
53
+ // | ------------------------ | ----- | ------ | -------------------------------- |
54
+ // | 文件不存在(首次运行) | null | false | null(不算错误) |
55
+ // | 读到且是合法 JSON | 解析值| true | null |
56
+ // | 存在但读不出来(权限等) | null | true | { code:'unreadable' } |
57
+ // | 存在但不是合法 JSON | null | true | { code:'invalid-json', backupPath} |
58
+ //
59
+ // failure 是「磁盘上的配置不可用」的如实报告 —— 调用方据此决定是否回退默认值,
60
+ // 以及是否拒绝后续写入。这里刻意不吞掉失败:静默回退会让调用方以为读到了一份
61
+ // 空配置,进而在下一次写入时把用户原有的配置整体覆盖掉。
62
+ export async function readConfigFile(filePath) {
63
+ let text
64
+ try {
65
+ text = await readFile(filePath, 'utf8')
66
+ } catch (err) {
67
+ if (err && err.code === 'ENOENT') {
68
+ return { value: null, exists: false, failure: null }
69
+ }
70
+ return {
71
+ value: null,
72
+ exists: true,
73
+ failure: {
74
+ code: 'unreadable',
75
+ message: `配置文件读取失败(${(err && err.code) || 'unknown'}):${(err && err.message) || err}`,
76
+ backupPath: null
77
+ }
78
+ }
79
+ }
80
+
81
+ try {
82
+ return { value: JSON.parse(text), exists: true, failure: null }
83
+ } catch (err) {
84
+ return {
85
+ value: null,
86
+ exists: true,
87
+ failure: {
88
+ code: 'invalid-json',
89
+ message: `配置文件不是合法的 JSON(${(err && err.message) || err})`,
90
+ backupPath: await backupCorruptFile(filePath)
91
+ }
92
+ }
93
+ }
94
+ }
95
+
96
+ // 把读取失败翻译成用户可读的说明(含损坏文件的备份位置)
97
+ export function describeConfigFailure(failure) {
98
+ if (!failure) return null
99
+ const backup = failure.backupPath ? `(原文件已备份到 ${failure.backupPath})` : ''
100
+ return `${failure.message}${backup}`
101
+ }
102
+
103
+ // 在这份失败之上能否写入:返回 null 表示可以写,返回字符串表示应拒绝并原样展示该原因。
104
+ //
105
+ // 判定下沉在这里(而不是散在调用方),是为了让「损坏的配置绝不会被静默覆盖」这条
106
+ // 规则只有一处实现 —— 写路径有 HTTP 保存 / 斜杠命令 / 工具 / 服务四条。
107
+ export function configWriteRefusal(failure, filePath) {
108
+ if (!failure) return null
109
+ return `${describeConfigFailure(failure)}。为避免覆盖,本次保存已拒绝;请修复或删除 ${filePath} 后重试。`
110
+ }
111
+
112
+ // 原子写入配置:先写同目录的临时文件,再 rename 覆盖。
113
+ //
114
+ // 同目录 rename 是原子替换(Windows 的 MoveFileEx 亦带 REPLACE_EXISTING),
115
+ // 因此文件要么是旧的完整内容、要么是新的完整内容。直接 writeFile 在写到一半
116
+ // 进程被杀时会留下**截断的 JSON**,整份配置随即不可解析。
117
+ //
118
+ // 临时文件名固定(不带随机后缀):写入统一经由 index.mjs 的写队列串行执行,
119
+ // 不存在并发同名临时文件;固定名还能让中断残留的 .tmp 在下次写入时被覆盖。
120
+ export async function writeConfigFile(filePath, config) {
121
+ await mkdir(dirname(filePath), { recursive: true })
122
+ const tmpPath = `${filePath}.tmp`
123
+ await writeFile(tmpPath, JSON.stringify(config, null, 2), 'utf8')
124
+ await rename(tmpPath, filePath)
125
+ }
package/package.json CHANGED
@@ -1,7 +1,8 @@
1
1
  {
2
2
  "name": "dsh-soul",
3
- "version": "0.6.2",
4
- "description": "为 DeepSeek Harness 添加个性化设置",
3
+ "version": "0.7.1",
4
+ "description": "为 DeepSeek Harness 添加个性化设置。",
5
+ "icon": "./assets/icon.svg",
5
6
  "keywords": [
6
7
  "dsh",
7
8
  "dsh-plugin",
@@ -17,15 +18,25 @@
17
18
  "type": "module",
18
19
  "main": "index.mjs",
19
20
  "scripts": {
20
- "verify": "node scripts/verify-config.mjs && node scripts/verify-compat.mjs && node scripts/verify-trail.mjs",
21
+ "verify": "node scripts/verify-config.mjs && node scripts/verify-store.mjs && node scripts/verify-client.mjs && node scripts/verify-e2e-prompt.mjs && node scripts/verify-live-prompt.mjs && node scripts/verify-compat.mjs && node scripts/verify-trail.mjs && node scripts/verify-nav-icon.mjs",
22
+ "verify:strict": "node scripts/verify-config.mjs --strict && node scripts/verify-store.mjs --strict && node scripts/verify-client.mjs --strict && node scripts/verify-e2e-prompt.mjs --strict && node scripts/verify-live-prompt.mjs --strict && node scripts/verify-compat.mjs --strict && node scripts/verify-trail.mjs --strict && node scripts/verify-nav-icon.mjs --strict",
23
+ "verify:store": "node scripts/verify-store.mjs",
24
+ "verify:client": "node scripts/verify-client.mjs",
25
+ "verify:e2e": "node scripts/verify-e2e-prompt.mjs",
26
+ "verify:host": "node scripts/verify-live-prompt.mjs",
21
27
  "verify:compat": "node scripts/verify-compat.mjs",
22
- "verify:trail": "node scripts/verify-trail.mjs"
28
+ "verify:trail": "node scripts/verify-trail.mjs",
29
+ "verify:nav-icon": "node scripts/verify-nav-icon.mjs"
23
30
  },
24
31
  "license": "MIT",
25
32
  "repository": {
26
33
  "type": "git",
27
34
  "url": "git+https://github.com/Aliuyanfeng/dsh-soul.git"
28
35
  },
36
+ "homepage": "https://github.com/Aliuyanfeng/dsh-soul#readme",
37
+ "bugs": {
38
+ "url": "https://github.com/Aliuyanfeng/dsh-soul/issues"
39
+ },
29
40
  "peerDependencies": {
30
41
  "@deepseek-ai/cordis": "^4.0.1 || ^4.0.5-alpha.1",
31
42
  "@deepseek-ai/dsh-llm": ">=0.1.1-rc.2 <0.3.0-0",
@@ -55,6 +66,7 @@
55
66
  "index.mjs",
56
67
  "lib",
57
68
  "client",
69
+ "assets",
58
70
  "cordis.patch.yml",
59
71
  "scripts",
60
72
  "README.md",
@@ -0,0 +1,73 @@
1
+ // scripts/lib/skip-report.mjs — 「跳过」的统一语义
2
+ //
3
+ // 为什么需要它:verify-trail / verify-nav-icon 在没有可用浏览器时、verify-e2e /
4
+ // verify-live-prompt / verify-compat 在定位不到 DSH 时都会**跳过**并返回 0。它们串在
5
+ // `npm run verify` 的 `&&` 链里,于是最终输出看起来一片绿 —— 实际有几十项断言根本没跑。
6
+ // 这是最误导人的一种报告:它把「没有验证」显示成了「验证通过」。
7
+ //
8
+ // 约定:
9
+ // 1. 跳过时必须显式打印「有 N 项断言本次未执行」,而不是静默退出 0;
10
+ // 2. 传 `--strict` 时跳过按失败处理(rc=1),供「本机本该有浏览器 / 本该有 DSH」的场景;
11
+ // 3. 断言数量由各脚本自己声明(`EXPECTED_ASSERTIONS`),并在真正跑完时用
12
+ // `assertCount` 校验 —— 脚本加了断言却忘了更新数字时,跑得起来的那一次会失败,
13
+ // 从而保证跳过提示里的数字不会悄悄变成谎话。
14
+ // 4. 只有**整脚本**跳过用 `skipExit`;脚本内部个别断言无条件执行用 `skipNote`,
15
+ // 由调用方把缺口计入声明数、并据返回值判定(strict 下这种缺口也算失败)。
16
+
17
+ /** 是否处于 --strict(跳过即失败)。 */
18
+ export function strictMode() {
19
+ return process.argv.includes('--strict')
20
+ }
21
+
22
+ /**
23
+ * 跳过并以统一格式说明「本次没有验证什么」。
24
+ * @param {string} tool 脚本名(用于报告)
25
+ * @param {number} assertions 本次未执行的断言数
26
+ * @param {string} reason 跳过原因(要能指导用户怎么补跑)
27
+ * @param {string} [hint] 补跑方式
28
+ */
29
+ export function skipExit(tool, assertions, reason, hint) {
30
+ console.log('')
31
+ console.log(` ⚠ 跳过 ${tool}:${reason}`)
32
+ console.log(` → 本次有 ${assertions} 项断言**未执行**。跳过不等于通过。`)
33
+ if (hint) console.log(` → 补跑:${hint}`)
34
+ if (strictMode()) {
35
+ console.log('结果:跳过(--strict 模式下按失败处理)。')
36
+ process.exit(1)
37
+ }
38
+ console.log('结果:跳过(不算通过)。加 --strict 可让跳过直接失败。')
39
+ process.exit(0)
40
+ }
41
+
42
+ /**
43
+ * 校验实际跑出的断言数与脚本声明的数量一致。
44
+ * @returns {boolean} 一致为 true;不一致时已打印说明,调用方应判失败。
45
+ */
46
+ export function assertCount(tool, actual, declared) {
47
+ if (actual === declared) return true
48
+ console.log('')
49
+ console.log(` ✗ ${tool} 的断言数与脚本声明不一致:实际 ${actual} 项,声明 ${declared} 项。`)
50
+ console.log(' 本次运行并不可信;请把脚本里的 EXPECTED_ASSERTIONS 更新为实际值后重跑')
51
+ console.log(' (跳过提示与 --strict 的判定都依赖这个数字)。')
52
+ return false
53
+ }
54
+
55
+ /**
56
+ * 报告脚本**内部**某一项断言因平台 / 环境能力缺失而无法执行。
57
+ *
58
+ * 与 `skipExit` 的分工:`skipExit` 是整个脚本都不跑(直接结束进程,用于「没有浏览器 /
59
+ * 定位不到 DSH」这类整脚本级跳过);本函数用于「其余断言照常跑完,只有其中一项没条件
60
+ * 执行」的情形 —— 调用方把它计入声明数,并据返回值决定是否判失败。
61
+ *
62
+ * @param {string} tool 脚本名(用于报告)
63
+ * @param {number} assertions 本次未执行的断言数
64
+ * @param {string} reason 为什么这一项跑不了
65
+ * @param {string} [hint] 什么条件下能补跑
66
+ * @returns {boolean} 非 strict 时 true(可接受);strict 时 false(调用方应判失败)
67
+ */
68
+ export function skipNote(tool, assertions, reason, hint) {
69
+ console.log(` ⚠ 跳过 ${tool}:${reason}`)
70
+ console.log(` → 本次有 ${assertions} 项断言**未执行**。跳过不等于通过。`)
71
+ if (hint) console.log(` → 补跑:${hint}`)
72
+ return !strictMode()
73
+ }