dsh-soul 0.3.3 → 0.4.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.
- package/README.md +4 -1
- package/README_EN.md +4 -1
- package/client/index.mjs +290 -145
- package/index.mjs +199 -198
- package/package.json +1 -1
package/index.mjs
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
// index.mjs — dsh-soul host 入口
|
|
2
2
|
//
|
|
3
|
-
//
|
|
4
|
-
// -
|
|
5
|
-
// -
|
|
6
|
-
// -
|
|
3
|
+
// 提示词结构:
|
|
4
|
+
// - 身份(自定义指令 → [角色设定])
|
|
5
|
+
// - 用户背景(「关于你」→ [用户背景])
|
|
6
|
+
// - 行为(风格/特质/输出语言 → [回复风格])
|
|
7
|
+
// - 执行规则([执行规则])
|
|
8
|
+
// 全部固定文案按 config.language 提供中英两套(PROMPT_TEXT 文案表)。
|
|
7
9
|
//
|
|
8
10
|
// 功能:
|
|
9
11
|
// - 支持自定义 Agent 回复风格和语调
|
|
@@ -12,6 +14,7 @@
|
|
|
12
14
|
// - 提供 HTTP API 供客户端调用
|
|
13
15
|
// - 注册斜杠命令 /soul 查看当前配置
|
|
14
16
|
// - 注入系统提示词到 Agent
|
|
17
|
+
// - 配置写入统一走写队列,返回 changed 供调用方跳过无变化刷新
|
|
15
18
|
|
|
16
19
|
import { readFile, writeFile, mkdir } from 'node:fs/promises'
|
|
17
20
|
import { join } from 'node:path'
|
|
@@ -33,132 +36,125 @@ function configPath() {
|
|
|
33
36
|
return join(process.env.DSH_HOME || join(homedir(), '.dsh'), 'soul-config.json')
|
|
34
37
|
}
|
|
35
38
|
|
|
36
|
-
// ====================
|
|
37
|
-
|
|
38
|
-
//
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
39
|
+
// ==================== 提示词文案与构建器 ====================
|
|
40
|
+
|
|
41
|
+
// 提示词固定文案表:所有进入 system prompt / 会话注入 / 工具返回的文本,
|
|
42
|
+
// 按输出语言(config.language)提供中英两套,键集完全对齐。
|
|
43
|
+
const PROMPT_TEXT = {
|
|
44
|
+
zh: {
|
|
45
|
+
roleHeader: '[角色设定]',
|
|
46
|
+
profileHeader: '[用户背景]',
|
|
47
|
+
profileDisclaimer: '以下信息描述的是与你对话的用户本人,不是你的身份;不要把用户的职业、技术背景或自述当成你自己的设定。',
|
|
48
|
+
profileNickname: (n) => `- 昵称:${n}(回复时使用这个称呼)`,
|
|
49
|
+
profileOccupation: (o) => `- 职业:${o}`,
|
|
50
|
+
profileBio: (b) => `- 用户自述:${b}`,
|
|
51
|
+
styleHeader: '[回复风格]',
|
|
52
|
+
styleLabel: '回复风格和语调:',
|
|
53
|
+
styles: {
|
|
54
|
+
professional: '专业严谨、注重逻辑和准确性、正式礼貌',
|
|
55
|
+
casual: '轻松自然、像朋友一样交流、中性客观',
|
|
56
|
+
humorous: '幽默风趣、适当使用比喻和玩笑、口语化',
|
|
57
|
+
roast: '吐槽达人、犀利幽默、爱调侃但无恶意、口语化',
|
|
58
|
+
efficient: '高效干练、直击要点、简洁明了、不绕弯子'
|
|
59
|
+
},
|
|
60
|
+
headingLists: {
|
|
61
|
+
more: '采用清晰的格式和列表结构组织回答,善用标题和列表',
|
|
62
|
+
less: '使用更多段落文本,减少标题和列表的使用'
|
|
63
|
+
},
|
|
64
|
+
emoji: {
|
|
65
|
+
more: '在回复中使用较多表情符号',
|
|
66
|
+
less: '尽量减少使用表情符号'
|
|
67
|
+
},
|
|
68
|
+
replyLanguage: {
|
|
69
|
+
zh: '必须使用简体中文回复',
|
|
70
|
+
en: '必须使用英语(English)回复'
|
|
71
|
+
},
|
|
72
|
+
joiner: ',',
|
|
73
|
+
rulesHeader: '[执行规则]',
|
|
74
|
+
rules: '你必须在每一条回复中严格遵守以上角色设定和回复风格;涉及用户背景时,用它来理解用户、贴合用户的需求和水平作答,而不是把它当作你自己的身份。包括但不限于:代码解释、技术问答、日常闲聊、知识科普等所有场景。违反此规则视为失败。',
|
|
75
|
+
injectUpdatedHeader: '[dsh-soul 个性化配置已更新]',
|
|
76
|
+
injectUpdatedBody: [
|
|
77
|
+
'以下是当前最新的个性化配置快照(含昵称、回复风格和语调、特质、输出语言等)。',
|
|
78
|
+
'从现在开始必须按照此配置回复,不要继续使用旧的昵称、角色、风格、语调、特质或输出语言。'
|
|
79
|
+
].join('\n'),
|
|
80
|
+
injectDisabled: [
|
|
81
|
+
'[dsh-soul 个性化配置已关闭]',
|
|
82
|
+
'从现在开始不要使用 dsh-soul 之前注入的昵称、角色、回复风格、语调、特质或输出语言配置。',
|
|
83
|
+
'请恢复使用 Agent 的默认行为。'
|
|
84
|
+
].join('\n'),
|
|
85
|
+
toolUpdated: '已更新个性化设置',
|
|
86
|
+
toolNoChanges: '没有要更改的设置',
|
|
87
|
+
toolValidationFailed: '配置校验失败:'
|
|
47
88
|
},
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
89
|
+
en: {
|
|
90
|
+
roleHeader: '[Role]',
|
|
91
|
+
profileHeader: '[User profile]',
|
|
92
|
+
profileDisclaimer: 'The information below describes the user you are talking to — it is NOT your own identity; do not treat the user\'s occupation, background or self-description as your persona.',
|
|
93
|
+
profileNickname: (n) => `- Nickname: ${n} (address the user by this name in replies)`,
|
|
94
|
+
profileOccupation: (o) => `- Occupation: ${o}`,
|
|
95
|
+
profileBio: (b) => `- Self-description: ${b}`,
|
|
96
|
+
styleHeader: '[Reply style]',
|
|
97
|
+
styleLabel: 'Reply style & tone: ',
|
|
98
|
+
styles: {
|
|
99
|
+
professional: 'Rigorous and professional, focused on logic and accuracy, formal and courteous',
|
|
100
|
+
casual: 'Relaxed and natural, chatting like a friend, neutral and objective',
|
|
101
|
+
humorous: 'Humorous, apt with analogies and jokes, colloquial',
|
|
102
|
+
roast: 'Playful roaster, sharp and witty but never mean, colloquial',
|
|
103
|
+
efficient: 'Efficient and straight to the point, concise, no beating around the bush'
|
|
104
|
+
},
|
|
105
|
+
headingLists: {
|
|
106
|
+
more: 'Organize answers with clear formatting, headings and lists',
|
|
107
|
+
less: 'Use more paragraph text; minimize headings and lists'
|
|
108
|
+
},
|
|
109
|
+
emoji: {
|
|
110
|
+
more: 'Use emojis fairly often in replies',
|
|
111
|
+
less: 'Keep emoji usage to a minimum'
|
|
112
|
+
},
|
|
113
|
+
replyLanguage: {
|
|
114
|
+
zh: '必须使用简体中文回复',
|
|
115
|
+
en: 'You must reply in English.'
|
|
116
|
+
},
|
|
117
|
+
joiner: '; ',
|
|
118
|
+
rulesHeader: '[Execution rules]',
|
|
119
|
+
rules: 'You must strictly follow the role setup and reply style above in EVERY reply; when user background is involved, use it to understand the user and tailor your answers to their needs and level — never treat it as your own identity. This covers all scenarios including code explanation, technical Q&A, casual chat and knowledge sharing. Violating this rule counts as failure.',
|
|
120
|
+
injectUpdatedHeader: '[dsh-soul] Personalization config updated',
|
|
121
|
+
injectUpdatedBody: [
|
|
122
|
+
'Below is the latest personalization snapshot (nickname, reply style & tone, traits, output language, etc.).',
|
|
123
|
+
'From now on you must reply according to this config; do not keep using the previous nickname, role, style, tone, traits or output language.'
|
|
124
|
+
].join('\n'),
|
|
125
|
+
injectDisabled: [
|
|
126
|
+
'[dsh-soul] Personalization config disabled',
|
|
127
|
+
'Stop using any nickname, role, reply style, tone, traits or output language previously injected by dsh-soul.',
|
|
128
|
+
'Return to your default behavior.'
|
|
129
|
+
].join('\n'),
|
|
130
|
+
toolUpdated: 'Personalization settings updated',
|
|
131
|
+
toolNoChanges: 'No settings to change',
|
|
132
|
+
toolValidationFailed: 'Config validation failed: '
|
|
59
133
|
}
|
|
60
134
|
}
|
|
61
135
|
|
|
62
|
-
//
|
|
63
|
-
function
|
|
64
|
-
|
|
65
|
-
if (config.nickname) {
|
|
66
|
-
lines.push(`- 昵称:${config.nickname}(回复时使用这个称呼)`)
|
|
67
|
-
}
|
|
68
|
-
if (config.occupation) {
|
|
69
|
-
lines.push(`- 职业:${config.occupation}`)
|
|
70
|
-
}
|
|
71
|
-
if (config.bio) {
|
|
72
|
-
lines.push(`- 用户自述:${config.bio}`)
|
|
73
|
-
}
|
|
74
|
-
return lines.join('\n')
|
|
136
|
+
// 按配置选择文案表(未知语言回退中文)
|
|
137
|
+
function promptTextOf(config) {
|
|
138
|
+
return PROMPT_TEXT[config && config.language === 'en' ? 'en' : 'zh']
|
|
75
139
|
}
|
|
76
140
|
|
|
77
|
-
//
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
roast: '吐槽达人、犀利幽默、爱调侃但无恶意、口语化',
|
|
85
|
-
efficient: '高效干练、直击要点、简洁明了、不绕弯子'
|
|
86
|
-
},
|
|
87
|
-
|
|
88
|
-
// 行为规则
|
|
89
|
-
rules: [
|
|
90
|
-
'在回答任何问题时,都必须体现上述身份特征',
|
|
91
|
-
'禁止使用标准AI助手的中性语气',
|
|
92
|
-
'保持角色一致性,不要跳出设定'
|
|
93
|
-
],
|
|
94
|
-
|
|
95
|
-
// 回复语言指令(同时决定 /soul 命令输出语言)
|
|
96
|
-
languages: {
|
|
97
|
-
zh: '必须使用简体中文回复',
|
|
98
|
-
en: '必须使用英语(English)回复'
|
|
99
|
-
},
|
|
100
|
-
|
|
101
|
-
// 特质:标题和列表的使用程度
|
|
102
|
-
headingLists: {
|
|
103
|
-
more: '采用清晰的格式和列表结构组织回答,善用标题和列表',
|
|
104
|
-
less: '使用更多段落文本,减少标题和列表的使用'
|
|
105
|
-
},
|
|
106
|
-
|
|
107
|
-
// 特质:表情符号的使用程度
|
|
108
|
-
emoji: {
|
|
109
|
-
more: '在回复中使用较多表情符号',
|
|
110
|
-
less: '尽量减少使用表情符号'
|
|
111
|
-
},
|
|
112
|
-
|
|
113
|
-
// 构建行为描述
|
|
114
|
-
build(config) {
|
|
115
|
-
const parts = []
|
|
116
|
-
|
|
117
|
-
// 回复风格和语调
|
|
118
|
-
if (config.style && this.styles[config.style]) {
|
|
119
|
-
parts.push(`回复风格和语调:${this.styles[config.style]}`)
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
// 特质:标题和列表
|
|
123
|
-
if (config.headingLists && this.headingLists[config.headingLists]) {
|
|
124
|
-
parts.push(this.headingLists[config.headingLists])
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
// 特质:表情符号
|
|
128
|
-
if (config.emoji && this.emoji[config.emoji]) {
|
|
129
|
-
parts.push(this.emoji[config.emoji])
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
// 回复语言
|
|
133
|
-
if (config.language && this.languages[config.language]) {
|
|
134
|
-
parts.push(this.languages[config.language])
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
return parts.join(',')
|
|
138
|
-
}
|
|
141
|
+
// 用户背景块(「关于你」):描述对话用户本人,不属于 Agent 的角色设定
|
|
142
|
+
function buildUserProfile(config, T) {
|
|
143
|
+
const lines = []
|
|
144
|
+
if (config.nickname) lines.push(T.profileNickname(config.nickname))
|
|
145
|
+
if (config.occupation) lines.push(T.profileOccupation(config.occupation))
|
|
146
|
+
if (config.bio) lines.push(T.profileBio(config.bio))
|
|
147
|
+
return lines.join('\n')
|
|
139
148
|
}
|
|
140
149
|
|
|
141
|
-
//
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
},
|
|
150
|
-
|
|
151
|
-
// 构建风格描述
|
|
152
|
-
build(config) {
|
|
153
|
-
const parts = []
|
|
154
|
-
|
|
155
|
-
// 根据风格选择格式
|
|
156
|
-
if (config.style === 'casual') {
|
|
157
|
-
parts.push('使用轻松的语气')
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
return parts.join(',')
|
|
161
|
-
}
|
|
150
|
+
// 行为块:回复风格和语调 + 特质 + 输出语言
|
|
151
|
+
function buildBehavior(config, T) {
|
|
152
|
+
const parts = []
|
|
153
|
+
if (config.style && T.styles[config.style]) parts.push(`${T.styleLabel}${T.styles[config.style]}`)
|
|
154
|
+
if (config.headingLists && T.headingLists[config.headingLists]) parts.push(T.headingLists[config.headingLists])
|
|
155
|
+
if (config.emoji && T.emoji[config.emoji]) parts.push(T.emoji[config.emoji])
|
|
156
|
+
if (config.language && T.replyLanguage[config.language]) parts.push(T.replyLanguage[config.language])
|
|
157
|
+
return parts.join(T.joiner)
|
|
162
158
|
}
|
|
163
159
|
|
|
164
160
|
// ==================== 配置管理 ====================
|
|
@@ -215,13 +211,27 @@ function enqueueConfigWrite(task) {
|
|
|
215
211
|
|
|
216
212
|
// 在写队列中执行一次配置更新:mutate 收到当前配置的浅拷贝,返回新配置对象。
|
|
217
213
|
// 注意 mutate 必须在队列任务内完成全部读取与合并,不要在队列外提前读取配置。
|
|
214
|
+
// 返回 { config, changed }:changed 为实际发生变化的字段名数组,
|
|
215
|
+
// 调用方在其为空时应跳过提示词刷新与会话注入(配置没有变化)。
|
|
218
216
|
async function commitConfig(mutate) {
|
|
219
217
|
return enqueueConfigWrite(async () => {
|
|
220
|
-
const
|
|
221
|
-
|
|
218
|
+
const current = await loadConfig()
|
|
219
|
+
const next = mutate({ ...current })
|
|
220
|
+
const changed = diffKeys(current, next)
|
|
221
|
+
const config = await saveConfig(next)
|
|
222
|
+
return { config, changed }
|
|
222
223
|
})
|
|
223
224
|
}
|
|
224
225
|
|
|
226
|
+
// 比较两份配置的差异字段名(浅比较;soul 配置为扁平对象)
|
|
227
|
+
function diffKeys(a, b) {
|
|
228
|
+
const changed = []
|
|
229
|
+
for (const key of new Set([...Object.keys(a), ...Object.keys(b)])) {
|
|
230
|
+
if (a[key] !== b[key]) changed.push(key)
|
|
231
|
+
}
|
|
232
|
+
return changed
|
|
233
|
+
}
|
|
234
|
+
|
|
225
235
|
// 将字段级校验错误汇总为一行文本(HTTP 错误信息 / 工具返回消息共用)
|
|
226
236
|
function formatFieldErrors(errors) {
|
|
227
237
|
return Object.entries(errors)
|
|
@@ -231,46 +241,43 @@ function formatFieldErrors(errors) {
|
|
|
231
241
|
|
|
232
242
|
// ==================== 提示词编译器 ====================
|
|
233
243
|
|
|
234
|
-
// Prompt Compiler
|
|
244
|
+
// Prompt Compiler:将身份(自定义指令)、用户背景与行为(风格/特质/语言)
|
|
245
|
+
// 按 config.language 对应的文案表编译为 system prompt。
|
|
235
246
|
function compilePrompt(config) {
|
|
236
247
|
if (!config.enabled) {
|
|
237
248
|
return ''
|
|
238
249
|
}
|
|
239
|
-
|
|
240
|
-
const
|
|
241
|
-
const
|
|
242
|
-
const
|
|
243
|
-
const
|
|
244
|
-
|
|
250
|
+
|
|
251
|
+
const T = promptTextOf(config)
|
|
252
|
+
const identity = (config.customInstructions || '').trim()
|
|
253
|
+
const profile = buildUserProfile(config, T)
|
|
254
|
+
const behavior = buildBehavior(config, T)
|
|
255
|
+
|
|
245
256
|
// 如果没有任何配置,返回空
|
|
246
257
|
if (!identity && !behavior && !profile) {
|
|
247
258
|
return ''
|
|
248
259
|
}
|
|
249
|
-
|
|
250
|
-
// 编译提示词
|
|
260
|
+
|
|
251
261
|
const parts = []
|
|
252
|
-
|
|
262
|
+
|
|
253
263
|
// 身份层
|
|
254
264
|
if (identity) {
|
|
255
|
-
parts.push(
|
|
265
|
+
parts.push(`${T.roleHeader} ${identity}`)
|
|
256
266
|
}
|
|
257
|
-
|
|
258
|
-
//
|
|
267
|
+
|
|
268
|
+
// 用户背景块:明确声明描述的是用户本人,防止模型把用户信息当成自己的身份
|
|
259
269
|
if (profile) {
|
|
260
|
-
parts.push(
|
|
261
|
-
'[用户背景] 以下信息描述的是与你对话的用户本人,不是你的身份;不要把用户的职业、技术背景或自述当成你自己的设定。',
|
|
262
|
-
profile
|
|
263
|
-
].join('\n'))
|
|
270
|
+
parts.push(`${T.profileHeader} ${T.profileDisclaimer}\n${profile}`)
|
|
264
271
|
}
|
|
265
|
-
|
|
272
|
+
|
|
266
273
|
// 行为层
|
|
267
274
|
if (behavior) {
|
|
268
|
-
parts.push(
|
|
275
|
+
parts.push(`${T.styleHeader} ${behavior}`)
|
|
269
276
|
}
|
|
270
|
-
|
|
277
|
+
|
|
271
278
|
// 执行规则
|
|
272
|
-
parts.push(`
|
|
273
|
-
|
|
279
|
+
parts.push(`${T.rulesHeader} ${T.rules}`)
|
|
280
|
+
|
|
274
281
|
return parts.join('\n')
|
|
275
282
|
}
|
|
276
283
|
|
|
@@ -285,26 +292,16 @@ function injectPromptToAllAgents(ctx, config) {
|
|
|
285
292
|
return
|
|
286
293
|
}
|
|
287
294
|
|
|
295
|
+
const T = promptTextOf(config)
|
|
288
296
|
const prompt = compilePrompt(config)
|
|
289
|
-
const
|
|
290
|
-
'[dsh-soul 个性化配置已关闭]',
|
|
291
|
-
'从现在开始不要使用 dsh-soul 之前注入的昵称、角色、回复风格、语调、特质或输出语言配置。',
|
|
292
|
-
'请恢复使用 Agent 的默认行为。'
|
|
293
|
-
].join('\n')
|
|
294
|
-
const snapshotText = prompt || disabledText
|
|
297
|
+
const snapshotText = prompt || T.injectDisabled
|
|
295
298
|
|
|
296
299
|
for (const agent of agents.list()) {
|
|
297
300
|
try {
|
|
298
301
|
agent.inject(createUserMessage({
|
|
299
302
|
content: [{
|
|
300
303
|
type: 'text',
|
|
301
|
-
text: prompt ?
|
|
302
|
-
'[dsh-soul 个性化配置已更新]',
|
|
303
|
-
'以下是当前最新的个性化配置快照(含昵称、回复风格和语调、特质、输出语言等)。',
|
|
304
|
-
'从现在开始必须按照此配置回复,不要继续使用旧的昵称、角色、风格、语调、特质或输出语言。',
|
|
305
|
-
'',
|
|
306
|
-
prompt
|
|
307
|
-
].join('\n') : snapshotText
|
|
304
|
+
text: prompt ? `${T.injectUpdatedHeader}\n${T.injectUpdatedBody}\n\n${prompt}` : snapshotText
|
|
308
305
|
}],
|
|
309
306
|
source: {
|
|
310
307
|
kind: 'plugin',
|
|
@@ -387,12 +384,14 @@ function registerRoutes(ctx) {
|
|
|
387
384
|
}
|
|
388
385
|
|
|
389
386
|
// 写队列内「读—改—写」,只合并通过校验的字段(未知字段不落盘)
|
|
390
|
-
const updated = await commitConfig(current => ({ ...current, ...patch }))
|
|
391
|
-
|
|
392
|
-
//
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
387
|
+
const { config: updated, changed } = await commitConfig(current => ({ ...current, ...patch }))
|
|
388
|
+
|
|
389
|
+
// 配置有实际变化才更新系统提示词并注入会话,避免无变化保存堆积注入消息
|
|
390
|
+
if (changed.length > 0) {
|
|
391
|
+
refreshPromptAndInject(ctx, updated)
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
send(200, { ok: true, config: updated, changed })
|
|
396
395
|
} else {
|
|
397
396
|
send(405, { ok: false, error: 'Method not allowed' })
|
|
398
397
|
}
|
|
@@ -435,12 +434,14 @@ function registerRoutes(ctx) {
|
|
|
435
434
|
}
|
|
436
435
|
|
|
437
436
|
try {
|
|
438
|
-
const config = await commitConfig(() => ({ ...DEFAULT_CONFIG }))
|
|
437
|
+
const { config, changed } = await commitConfig(() => ({ ...DEFAULT_CONFIG }))
|
|
439
438
|
|
|
440
|
-
//
|
|
441
|
-
|
|
439
|
+
// 配置有实际变化才更新系统提示词并注入会话
|
|
440
|
+
if (changed.length > 0) {
|
|
441
|
+
refreshPromptAndInject(ctx, config)
|
|
442
|
+
}
|
|
442
443
|
|
|
443
|
-
send(200, { ok: true, config })
|
|
444
|
+
send(200, { ok: true, config, changed })
|
|
444
445
|
} catch (err) {
|
|
445
446
|
send(500, { ok: false, error: String(err.message || err) })
|
|
446
447
|
}
|
|
@@ -560,20 +561,17 @@ function registerCommands(ctx) {
|
|
|
560
561
|
text: `${t.showTitle}\n\n${t.statusLabel}${t.colon}${status}\n${t.nicknameLabel}${t.colon}${nickname}\n${t.occupationLabel}${t.colon}${occupation}\n${t.bioLabel}${t.colon}${bio}\n${t.styleLabel}${t.colon}${styleName}\n${t.traitsLabel}${t.colon}${t.headingListsLabel}=${hlName},${t.emojiLabel}=${emojiName}\n${t.instructionsLabel}${t.colon}${instructions}\n\n${t.help}`
|
|
561
562
|
}
|
|
562
563
|
} else if (args === 'reset') {
|
|
563
|
-
const updated = await commitConfig(() => ({ ...DEFAULT_CONFIG }))
|
|
564
|
-
|
|
565
|
-
refreshPromptAndInject(ctx, updated)
|
|
564
|
+
const { config: updated, changed } = await commitConfig(() => ({ ...DEFAULT_CONFIG }))
|
|
565
|
+
if (changed.length > 0) refreshPromptAndInject(ctx, updated)
|
|
566
566
|
return { kind: 'success', text: t.resetDone }
|
|
567
567
|
} else if (args === 'enable') {
|
|
568
|
-
//
|
|
569
|
-
const updated = await commitConfig(c => ({ ...c, enabled: true }))
|
|
570
|
-
|
|
571
|
-
refreshPromptAndInject(ctx, updated)
|
|
568
|
+
// 写队列内「读—改—写」,不在队列外改写内存缓存对象;无变化时跳过注入
|
|
569
|
+
const { config: updated, changed } = await commitConfig(c => ({ ...c, enabled: true }))
|
|
570
|
+
if (changed.length > 0) refreshPromptAndInject(ctx, updated)
|
|
572
571
|
return { kind: 'success', text: t.enableDone }
|
|
573
572
|
} else if (args === 'disable') {
|
|
574
|
-
const updated = await commitConfig(c => ({ ...c, enabled: false }))
|
|
575
|
-
|
|
576
|
-
refreshPromptAndInject(ctx, updated)
|
|
573
|
+
const { config: updated, changed } = await commitConfig(c => ({ ...c, enabled: false }))
|
|
574
|
+
if (changed.length > 0) refreshPromptAndInject(ctx, updated)
|
|
577
575
|
return { kind: 'success', text: t.disableDone }
|
|
578
576
|
} else {
|
|
579
577
|
// 设置昵称(保留原始大小写,不做 toLowerCase);与 HTTP 保存共用同一套校验
|
|
@@ -581,9 +579,8 @@ function registerCommands(ctx) {
|
|
|
581
579
|
if (errors.nickname) {
|
|
582
580
|
return { kind: 'error', text: t.failed(errors.nickname) }
|
|
583
581
|
}
|
|
584
|
-
const updated = await commitConfig(c => ({ ...c, nickname: patch.nickname }))
|
|
585
|
-
|
|
586
|
-
refreshPromptAndInject(ctx, updated)
|
|
582
|
+
const { config: updated, changed } = await commitConfig(c => ({ ...c, nickname: patch.nickname }))
|
|
583
|
+
if (changed.length > 0) refreshPromptAndInject(ctx, updated)
|
|
587
584
|
return { kind: 'success', text: t.nicknameDone(patch.nickname) }
|
|
588
585
|
}
|
|
589
586
|
} catch (err) {
|
|
@@ -712,32 +709,34 @@ function registerTools(ctx) {
|
|
|
712
709
|
// 与 HTTP 保存共用同一套校验(白名单 / 类型 / 长度 / 枚举)
|
|
713
710
|
const { patch, errors } = sanitizeConfig(args)
|
|
714
711
|
if (Object.keys(errors).length > 0) {
|
|
715
|
-
|
|
712
|
+
const T0 = promptTextOf(await loadConfig())
|
|
713
|
+
return { ok: false, message: T0.toolValidationFailed + formatFieldErrors(errors) }
|
|
716
714
|
}
|
|
717
715
|
if (Object.keys(patch).length === 0) {
|
|
718
|
-
|
|
716
|
+
const T0 = promptTextOf(await loadConfig())
|
|
717
|
+
return { ok: true, message: T0.toolNoChanges }
|
|
719
718
|
}
|
|
720
719
|
|
|
721
|
-
//
|
|
722
|
-
|
|
723
|
-
const updated = await commitConfig((current) => {
|
|
720
|
+
// 写队列内合并,避免与其他写入方(Web UI、/soul 命令)竞态;
|
|
721
|
+
// changed 由队列任务统一 diff 得出
|
|
722
|
+
const { config: updated, changed } = await commitConfig((current) => {
|
|
724
723
|
const next = { ...current }
|
|
725
|
-
changes = {}
|
|
726
724
|
for (const key of Object.keys(patch)) {
|
|
727
|
-
|
|
728
|
-
next[key] = patch[key]
|
|
729
|
-
changes[key] = patch[key]
|
|
730
|
-
}
|
|
725
|
+
next[key] = patch[key]
|
|
731
726
|
}
|
|
732
727
|
return next
|
|
733
728
|
})
|
|
734
729
|
|
|
735
|
-
if (
|
|
736
|
-
return { ok: true, message:
|
|
730
|
+
if (changed.length === 0) {
|
|
731
|
+
return { ok: true, message: promptTextOf(updated).toolNoChanges }
|
|
737
732
|
}
|
|
738
733
|
|
|
739
734
|
refreshPromptAndInject(ctx, updated)
|
|
740
|
-
return {
|
|
735
|
+
return {
|
|
736
|
+
ok: true,
|
|
737
|
+
message: promptTextOf(updated).toolUpdated,
|
|
738
|
+
changes: Object.fromEntries(changed.map(key => [key, updated[key]]))
|
|
739
|
+
}
|
|
741
740
|
} catch (err) {
|
|
742
741
|
return { ok: false, message: String(err.message || err) }
|
|
743
742
|
}
|
|
@@ -761,19 +760,21 @@ export async function apply(ctx) {
|
|
|
761
760
|
return await loadConfig()
|
|
762
761
|
},
|
|
763
762
|
async updateConfig(newConfig) {
|
|
764
|
-
// 与 HTTP
|
|
763
|
+
// 与 HTTP 保存共用同一套校验;失败时抛错给服务调用方;返回更新后的配置
|
|
765
764
|
const { patch, errors } = sanitizeConfig(newConfig)
|
|
766
765
|
if (Object.keys(errors).length > 0) {
|
|
767
766
|
throw new Error('配置校验失败:' + formatFieldErrors(errors))
|
|
768
767
|
}
|
|
769
|
-
|
|
768
|
+
const { config } = await commitConfig(current => ({ ...current, ...patch }))
|
|
769
|
+
return config
|
|
770
770
|
},
|
|
771
771
|
async getSystemPrompt() {
|
|
772
772
|
const config = await loadConfig()
|
|
773
773
|
return compilePrompt(config)
|
|
774
774
|
},
|
|
775
775
|
async resetConfig() {
|
|
776
|
-
|
|
776
|
+
const { config } = await commitConfig(() => ({ ...DEFAULT_CONFIG }))
|
|
777
|
+
return config
|
|
777
778
|
}
|
|
778
779
|
})
|
|
779
780
|
|