@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.
- package/LICENSE +23 -0
- package/README.md +95 -3
- package/cordis.patch.yml +5 -0
- package/icon.svg +203 -0
- package/lib/client.activity.js +222 -0
- package/lib/client.apply.js +171 -0
- package/lib/client.changes.js +206 -0
- package/lib/client.cleanup.js +270 -0
- package/lib/client.control.js +247 -0
- package/lib/client.dialog.js +77 -0
- package/lib/client.disable-preview.js +904 -0
- package/lib/client.draft.js +280 -0
- package/lib/client.editor-messages.js +421 -0
- package/lib/client.editor-store.js +266 -0
- package/lib/client.editor.js +433 -0
- package/lib/client.effects.js +115 -0
- package/lib/client.impact-tree.js +487 -0
- package/lib/client.influence.js +361 -0
- package/lib/client.js +246 -0
- package/lib/client.page.js +646 -0
- package/lib/client.plugin-filters.js +62 -0
- package/lib/client.plugins.js +506 -0
- package/lib/client.preview-messages.js +506 -0
- package/lib/client.preview-raw.js +132 -0
- package/lib/client.preview-store.js +128 -0
- package/lib/client.preview-tools.js +194 -0
- package/lib/client.preview.js +448 -0
- package/lib/client.relation-graph.js +364 -0
- package/lib/client.schema.js +327 -0
- package/lib/client.search.js +409 -0
- package/lib/client.session-detail.js +503 -0
- package/lib/client.sessions.js +495 -0
- package/lib/client.shared.js +562 -0
- package/lib/client.store.js +452 -0
- package/lib/client.styles.js +589 -0
- package/lib/client.template-card.js +240 -0
- package/lib/client.tools.js +366 -0
- package/lib/client.workspace-picker.js +204 -0
- package/lib/host/activity.js +252 -0
- package/lib/host/assembly-preview.js +244 -0
- package/lib/host/attribution.js +341 -0
- package/lib/host/catalog.js +133 -0
- package/lib/host/categorize.js +40 -0
- package/lib/host/config-layers.js +589 -0
- package/lib/host/constants.js +69 -0
- package/lib/host/control.js +159 -0
- package/lib/host/disable-preview.js +651 -0
- package/lib/host/entries.js +289 -0
- package/lib/host/fiber.js +86 -0
- package/lib/host/fs.js +99 -0
- package/lib/host/health.js +178 -0
- package/lib/host/http.js +65 -0
- package/lib/host/impact.js +138 -0
- package/lib/host/injection-owners.js +60 -0
- package/lib/host/layers.js +132 -0
- package/lib/host/masks.js +145 -0
- package/lib/host/modes.js +96 -0
- package/lib/host/names.js +44 -0
- package/lib/host/patch-config.js +554 -0
- package/lib/host/patch-scan.js +103 -0
- package/lib/host/preview-conditions.js +281 -0
- package/lib/host/preview-contexts.js +174 -0
- package/lib/host/preview-envelope.js +31 -0
- package/lib/host/preview-instructions.js +450 -0
- package/lib/host/preview-messages.js +284 -0
- package/lib/host/preview-modules.js +233 -0
- package/lib/host/preview-skills.js +200 -0
- package/lib/host/prompt.js +213 -0
- package/lib/host/recipe-overlay.js +209 -0
- package/lib/host/request-preview.js +114 -0
- package/lib/host/routes/activation.js +96 -0
- package/lib/host/routes/activity.js +22 -0
- package/lib/host/routes/apply.js +93 -0
- package/lib/host/routes/cleanup.js +62 -0
- package/lib/host/routes/control.js +22 -0
- package/lib/host/routes/disable-preview.js +19 -0
- package/lib/host/routes/impact.js +19 -0
- package/lib/host/routes/operations.js +22 -0
- package/lib/host/routes/preview.js +58 -0
- package/lib/host/routes/session-influence.js +23 -0
- package/lib/host/routes/snapshot.js +21 -0
- package/lib/host/routes/template.js +54 -0
- package/lib/host/routes/templates.js +28 -0
- package/lib/host/scope-impact.js +125 -0
- package/lib/host/session-events.js +39 -0
- package/lib/host/session-influence.js +467 -0
- package/lib/host/sessions.js +293 -0
- package/lib/host/snapshot.js +218 -0
- package/lib/host/source-scan.js +225 -0
- package/lib/host/templates/cleanup.js +219 -0
- package/lib/host/templates/inheritance.js +144 -0
- package/lib/host/templates/materialize.js +317 -0
- package/lib/host/templates/message-overlay.js +184 -0
- package/lib/host/templates/repository.js +323 -0
- package/lib/host/templates/store.js +131 -0
- package/lib/host/tool-set.js +150 -0
- package/lib/host/tools.js +129 -0
- package/lib/index.js +106 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +124 -6
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 把一份 recipe 物化到 agent 的 scope 上,并保证它能被撤销。
|
|
3
|
+
*
|
|
4
|
+
* 三个作用点里只用了两个:注册期(scoped section + restrict)。装配期 waterfall
|
|
5
|
+
* 留给下一阶段——先保证「注册的东西能撤销」这条底线。
|
|
6
|
+
*
|
|
7
|
+
* 撤销顺序在这里是有意义的:必须先把旧句柄 await 掉再注册新的,因为同一个
|
|
8
|
+
* scope 里同名段落不允许重复注册,而 Cordis 的 effect 撤销是异步的。
|
|
9
|
+
* @module dsh-plugin-console/host/templates/materialize
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { completeSectionOf } from '../prompt.js'
|
|
13
|
+
import { scopeKeyOf } from '../fiber.js'
|
|
14
|
+
import { readRestrictableNames } from '../tools.js'
|
|
15
|
+
import { previewAssembly } from '../assembly-preview.js'
|
|
16
|
+
import { hasMessageOverrides, overlayStepMessages } from './message-overlay.js'
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* 已物化的句柄,按会话记。
|
|
20
|
+
*
|
|
21
|
+
* 必须挂在进程级而不是插件级:物化的注册本身写在 agent 的 scope 上,
|
|
22
|
+
* 插件被重新加载时那些注册还活着,而模块内的 Map 会被清空。清空之后就
|
|
23
|
+
* 找不到上一次的句柄去撤销,同一个 scope 里再注册同名段落必然失败。
|
|
24
|
+
*/
|
|
25
|
+
const APPLIED_KEY = Symbol.for('dsh-plugin-console.applied')
|
|
26
|
+
export const appliedHandles = globalThis[APPLIED_KEY] ?? (globalThis[APPLIED_KEY] = new Map())
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 追加段落的名字与位置。
|
|
30
|
+
*
|
|
31
|
+
* 必须排在 DSH 已知段顺序之后:`deployment:persona-suffix` 是 10200,
|
|
32
|
+
* 所以用 20000,确保「追加」真的是整份提示词的最后一段。
|
|
33
|
+
*/
|
|
34
|
+
const APPEND_SECTION = 'template:extra'
|
|
35
|
+
const APPEND_ORDER = 20000
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* agent scope 链上的 context 注册表。
|
|
39
|
+
*
|
|
40
|
+
* 覆盖 runtime-context 的干净落点是 scoped 同名 context:注册时沿用原来的 order /
|
|
41
|
+
* required,`interpolate:false` 直接使用 recipe 里已经渲染好的文字。
|
|
42
|
+
* @param {object} agent - 目标 agent。
|
|
43
|
+
* @returns {Map<string, object> | undefined} name → 注册项。
|
|
44
|
+
*/
|
|
45
|
+
function contextRegistrationsOf(agent) {
|
|
46
|
+
const layers = agent?.ctx?.systemPrompt?.layers
|
|
47
|
+
if (layers === undefined || typeof layers.merge !== 'function') return undefined
|
|
48
|
+
try {
|
|
49
|
+
return new Map(layers.merge(scopeKeyOf(agent.ctx), (layer) => layer.contexts))
|
|
50
|
+
} catch {
|
|
51
|
+
return undefined
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** recipe 里真正配了覆盖的消息类型;用于 `applied` 与监听器安装判定。 */
|
|
56
|
+
function configuredMessageKinds(recipe) {
|
|
57
|
+
const messages = recipe?.messages
|
|
58
|
+
if (messages === null || typeof messages !== 'object') return []
|
|
59
|
+
return ['agent-instructions', 'skill-catalog', 'time-context'].filter((kind) => {
|
|
60
|
+
const spec = messages[kind]
|
|
61
|
+
if (spec === null || typeof spec !== 'object') return false
|
|
62
|
+
if (spec.hidden === true || typeof spec.text === 'string') return true
|
|
63
|
+
return Array.isArray(spec.hiddenEntries) || (spec.descriptions !== null && typeof spec.descriptions === 'object')
|
|
64
|
+
})
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* 把一个模板 recipe 应用到 agent 上。
|
|
69
|
+
* @param {object} agent - 目标 agent。
|
|
70
|
+
* @param {object} recipe - 配方。
|
|
71
|
+
* @param {Map<string, number>} sectionOrder - 同名覆盖沿用的 order。
|
|
72
|
+
* @returns {{applied: object, errors: string[], dispose: Function}} 物化句柄。
|
|
73
|
+
*/
|
|
74
|
+
export function materializeRecipe(agent, recipe, sectionOrder) {
|
|
75
|
+
const disposers = []
|
|
76
|
+
const prompt = recipe?.prompt ?? {}
|
|
77
|
+
const applied = {
|
|
78
|
+
sections: [],
|
|
79
|
+
overrides: [],
|
|
80
|
+
deniedTools: [],
|
|
81
|
+
skippedTools: [],
|
|
82
|
+
foldedAppend: null,
|
|
83
|
+
contextOverrides: [],
|
|
84
|
+
skippedContexts: [],
|
|
85
|
+
suppressedRuntimeContext: false,
|
|
86
|
+
messageKinds: [],
|
|
87
|
+
}
|
|
88
|
+
const errors = []
|
|
89
|
+
// 这个 agent 所在模式是不是「独占型」提示词。是的话,同名覆盖要带上 complete,
|
|
90
|
+
// 追加段落也只能并进那一段——见函数体里的两处注释。
|
|
91
|
+
const completeName = completeSectionOf(agent.ctx, scopeKeyOf)
|
|
92
|
+
applied.completeSection = completeName ?? null
|
|
93
|
+
|
|
94
|
+
/** 覆盖某一段。`complete` 只在它本来就是独占段时传。 */
|
|
95
|
+
const overrideSection = (name, text) => {
|
|
96
|
+
const section = {
|
|
97
|
+
name,
|
|
98
|
+
order: sectionOrder.get(name) ?? APPEND_ORDER,
|
|
99
|
+
text: () => text,
|
|
100
|
+
interpolate: false,
|
|
101
|
+
}
|
|
102
|
+
if (name === completeName) section.complete = true
|
|
103
|
+
disposers.push(agent.ctx.systemPrompt.section(section))
|
|
104
|
+
applied.overrides.push(name)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// 追加段落:独占型提示词下追加段落无处安放——独占段会把别的段全丢弃,所以
|
|
108
|
+
// 只能并进那一段。并进去要基于它当前的文本,而这里只有 recipe(存的是差异),
|
|
109
|
+
// 因此这件事由客户端在算 recipe 时折进去;host 这边只记录发生了什么。
|
|
110
|
+
if (typeof prompt.appendText === 'string' && prompt.appendText.length > 0) {
|
|
111
|
+
if (completeName !== undefined) {
|
|
112
|
+
applied.foldedAppend = completeName
|
|
113
|
+
} else {
|
|
114
|
+
try {
|
|
115
|
+
disposers.push(agent.ctx.systemPrompt.section({
|
|
116
|
+
name: APPEND_SECTION,
|
|
117
|
+
order: APPEND_ORDER,
|
|
118
|
+
text: () => prompt.appendText,
|
|
119
|
+
interpolate: false,
|
|
120
|
+
}))
|
|
121
|
+
applied.sections.push(APPEND_SECTION)
|
|
122
|
+
} catch (error) {
|
|
123
|
+
errors.push('追加段落失败:' + (error instanceof Error ? error.message : String(error)))
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// 同名覆盖:空字符串 = 隐藏该段
|
|
129
|
+
for (const [name, text] of Object.entries(prompt.sectionOverrides ?? {})) {
|
|
130
|
+
if (typeof text !== 'string') continue
|
|
131
|
+
try {
|
|
132
|
+
overrideSection(name, text)
|
|
133
|
+
} catch (error) {
|
|
134
|
+
errors.push('改写 ' + name + ' 失败:' + (error instanceof Error ? error.message : String(error)))
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// runtime-context 逐段覆盖:scoped 同名 context 会遮蔽外层注册。
|
|
139
|
+
// 名字在当前 agent scope 里不存在时不凭空新增——那会把「本没有这一段」变成「有」。
|
|
140
|
+
const contextTexts = recipe?.contexts
|
|
141
|
+
if (contextTexts !== null && typeof contextTexts === 'object') {
|
|
142
|
+
const entries = Object.entries(contextTexts).filter(([, text]) => typeof text === 'string')
|
|
143
|
+
if (entries.length > 0 && typeof agent.ctx.systemPrompt?.context !== 'function') {
|
|
144
|
+
errors.push('systemPrompt.context 不可用,runtime-context 覆盖未安装')
|
|
145
|
+
} else if (entries.length > 0) {
|
|
146
|
+
const registry = contextRegistrationsOf(agent)
|
|
147
|
+
for (const [name, text] of entries) {
|
|
148
|
+
const entry = registry?.get(name)
|
|
149
|
+
if (entry === undefined) {
|
|
150
|
+
applied.skippedContexts.push(name)
|
|
151
|
+
continue
|
|
152
|
+
}
|
|
153
|
+
const order = typeof entry.order === 'number'
|
|
154
|
+
? entry.order
|
|
155
|
+
: agent.ctx.systemPrompt.getContextOrder?.(name)
|
|
156
|
+
if (!Number.isFinite(order)) {
|
|
157
|
+
applied.skippedContexts.push(name)
|
|
158
|
+
continue
|
|
159
|
+
}
|
|
160
|
+
try {
|
|
161
|
+
disposers.push(agent.ctx.systemPrompt.context({
|
|
162
|
+
name,
|
|
163
|
+
order,
|
|
164
|
+
...(entry.required === true ? { required: true } : {}),
|
|
165
|
+
text: () => text,
|
|
166
|
+
interpolate: false,
|
|
167
|
+
}))
|
|
168
|
+
applied.contextOverrides.push(name)
|
|
169
|
+
} catch (error) {
|
|
170
|
+
applied.skippedContexts.push(name)
|
|
171
|
+
errors.push('改写上下文 ' + name + ' 失败:' + (error instanceof Error ? error.message : String(error)))
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
if (recipe?.messages?.['runtime-context']?.hidden === true) {
|
|
178
|
+
if (typeof agent.ctx.systemPrompt?.suppressRuntimeContext === 'function') {
|
|
179
|
+
try {
|
|
180
|
+
disposers.push(agent.ctx.systemPrompt.suppressRuntimeContext())
|
|
181
|
+
applied.suppressedRuntimeContext = true
|
|
182
|
+
} catch (error) {
|
|
183
|
+
errors.push('隐藏 runtime-context 失败:' + (error instanceof Error ? error.message : String(error)))
|
|
184
|
+
}
|
|
185
|
+
} else {
|
|
186
|
+
errors.push('systemPrompt.suppressRuntimeContext 不可用,runtime-context 未隐藏')
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// pre-step 消息改写:只在真的有覆盖时注册,避免每个会话多一个空监听器。
|
|
191
|
+
// prepend 让它在内置注入(instructions / catalog / time)之后拿到最终列表。
|
|
192
|
+
if (hasMessageOverrides(recipe)) {
|
|
193
|
+
applied.messageKinds = configuredMessageKinds(recipe)
|
|
194
|
+
if (typeof agent.ctx.on !== 'function') {
|
|
195
|
+
errors.push('agent 上下文没有 on(),消息改写未安装')
|
|
196
|
+
} else {
|
|
197
|
+
try {
|
|
198
|
+
disposers.push(agent.ctx.on('agent/pre-step', async (payload, next) => {
|
|
199
|
+
const decision = await next()
|
|
200
|
+
if (decision?.kind === 'reject' || payload?.signal?.aborted === true) return decision
|
|
201
|
+
try {
|
|
202
|
+
const result = overlayStepMessages(decision.messages ?? [], recipe)
|
|
203
|
+
return { ...decision, messages: result.messages }
|
|
204
|
+
} catch (error) {
|
|
205
|
+
agent.ctx.logger?.warn?.('plugin-console: 消息改写失败:' + (error instanceof Error ? error.message : String(error)))
|
|
206
|
+
return decision
|
|
207
|
+
}
|
|
208
|
+
}, { prepend: true }))
|
|
209
|
+
} catch (error) {
|
|
210
|
+
errors.push('安装消息改写失败:' + (error instanceof Error ? error.message : String(error)))
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// 工具:用 deny 黑名单而不是 allow 白名单——晚到的 MCP 工具不会被误挡。
|
|
216
|
+
// restrict() 对「当前 scope 不认识的全局工具名」直接抛错,一个名字不认识
|
|
217
|
+
// 就整批失效;所以先逐个探明哪些名字真的可禁,只提交认识的那批。
|
|
218
|
+
const disabled = Array.isArray(recipe?.tools?.disabled)
|
|
219
|
+
? recipe.tools.disabled.filter((name) => typeof name === 'string')
|
|
220
|
+
: []
|
|
221
|
+
if (disabled.length > 0 && typeof agent.ctx.tools?.restrict === 'function') {
|
|
222
|
+
let deny = disabled
|
|
223
|
+
const known = readRestrictableNames(agent.ctx)
|
|
224
|
+
if (known !== undefined) {
|
|
225
|
+
deny = disabled.filter((name) => known.has(name))
|
|
226
|
+
applied.skippedTools = disabled.filter((name) => !known.has(name))
|
|
227
|
+
}
|
|
228
|
+
if (deny.length > 0) {
|
|
229
|
+
try {
|
|
230
|
+
disposers.push(agent.ctx.tools.restrict({ deny }))
|
|
231
|
+
applied.deniedTools = deny
|
|
232
|
+
} catch (error) {
|
|
233
|
+
errors.push('禁用工具失败:' + (error instanceof Error ? error.message : String(error)))
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
return {
|
|
239
|
+
applied,
|
|
240
|
+
errors,
|
|
241
|
+
/**
|
|
242
|
+
* 撤销这次物化。
|
|
243
|
+
*
|
|
244
|
+
* 必须 await:Cordis 的 effect 撤销是异步的(清理阶段可能要等),
|
|
245
|
+
* 不 await 就立刻重新注册同名段落,注册表里那一段还在,会直接抛错。
|
|
246
|
+
* @returns {Promise<void>} 全部撤销完成。
|
|
247
|
+
*/
|
|
248
|
+
async dispose() {
|
|
249
|
+
for (const dispose of disposers.reverse()) {
|
|
250
|
+
try {
|
|
251
|
+
await dispose()
|
|
252
|
+
} catch {
|
|
253
|
+
// 单个撤销失败不影响其余
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
},
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* 预构建拿 order 表:同名覆盖要沿用原段位置。
|
|
262
|
+
* @param {object} ctx - host 插件上下文。
|
|
263
|
+
* @param {string | undefined} presetId - 该会话的模式。
|
|
264
|
+
* @returns {Promise<Map<string, number>>} 段落名 → order。
|
|
265
|
+
*/
|
|
266
|
+
export async function sectionOrderOf(ctx, presetId) {
|
|
267
|
+
const base = await previewAssembly(ctx, { presetId })
|
|
268
|
+
return new Map(base.sections.map((section, index) => [section.name, index * 100]))
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* 把一个 recipe 物化到指定会话,并接管它的生命周期。
|
|
273
|
+
*
|
|
274
|
+
* 抽出来是因为两条路径都要用:应用模板(`/apply`)和打开开关(`/activation`)。
|
|
275
|
+
* 两者语义完全一样——「把这份 recipe 注册到这个 agent 的 scope 上,并保证可撤销」。
|
|
276
|
+
* @param {object} ctx - host 插件上下文。
|
|
277
|
+
* @param {string} sessionId - 目标会话。
|
|
278
|
+
* @param {object} recipe - 配方。
|
|
279
|
+
* @param {string | undefined} presetId - 该会话的模式(决定 order 表)。
|
|
280
|
+
* @returns {Promise<object>} `{ applied, live, applied0?, errors?, reason? }`。
|
|
281
|
+
*/
|
|
282
|
+
export async function applyRecipeToSession(ctx, sessionId, recipe, presetId) {
|
|
283
|
+
const live = ctx.get('agents')?.get?.(sessionId)
|
|
284
|
+
if (live === undefined) {
|
|
285
|
+
return { applied: false, live: false, reason: '这个会话当前没有活的 agent,物化未发生;启动会话后可以再试' }
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
const previous = appliedHandles.get(sessionId)
|
|
289
|
+
if (previous !== undefined) {
|
|
290
|
+
appliedHandles.delete(sessionId)
|
|
291
|
+
try { await previous() } catch { /* 旧句柄失效不该挡住新的一次 */ }
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
const order = await sectionOrderOf(ctx, presetId)
|
|
295
|
+
const handle = materializeRecipe(live, recipe, order)
|
|
296
|
+
appliedHandles.set(sessionId, handle.dispose)
|
|
297
|
+
// 撤销挂在 agent 的 scope 上:会话一结束,注册过的段与工具限制一起消失。
|
|
298
|
+
live.ctx.effect(() => () => {
|
|
299
|
+
appliedHandles.delete(sessionId)
|
|
300
|
+
void handle.dispose()
|
|
301
|
+
}, 'plugin-console: template materialization')
|
|
302
|
+
|
|
303
|
+
return { applied: true, live: true, applied0: handle.applied, errors: handle.errors }
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* 撤掉某个会话的物化。
|
|
308
|
+
* @param {string} sessionId - 目标会话。
|
|
309
|
+
* @returns {Promise<boolean>} 是否真的撤掉了一份注册。
|
|
310
|
+
*/
|
|
311
|
+
export async function clearRecipeForSession(sessionId) {
|
|
312
|
+
const handle = appliedHandles.get(sessionId)
|
|
313
|
+
if (handle === undefined) return false
|
|
314
|
+
appliedHandles.delete(sessionId)
|
|
315
|
+
try { await handle() } catch { /* 已失效的句柄不算失败 */ }
|
|
316
|
+
return true
|
|
317
|
+
}
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 真实请求里的 user-role 消息改写。
|
|
3
|
+
*
|
|
4
|
+
* `agent-instructions`、`skill-catalog`、`time-context` 都由 DSH 的 pre-step 监听器
|
|
5
|
+
* 注入;本插件在 agent scope 上注册一个 `prepend` 监听器,等 `next()` 完成后改
|
|
6
|
+
* **最终**消息列表。关键约束:
|
|
7
|
+
*
|
|
8
|
+
* - 只替换 `content`,保留 `id` 与 `source`。`agent-instructions` 靠
|
|
9
|
+
* `source.baseline/baselineIdentity` 判断 baseline 是否已可见;`skill-catalog`
|
|
10
|
+
* 靠 `source.entries` 计算 digest。改掉它们会让 DSH 每步重新注入/重新发布。
|
|
11
|
+
* - 任何异常都由调用方吞掉:一个坏 recipe 不能让每次请求都失败。
|
|
12
|
+
* @module dsh-plugin-console/host/templates/message-overlay
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { applyCatalogOverrides, renderCatalogMessage, renderCatalogUpdate } from '../catalog.js'
|
|
16
|
+
|
|
17
|
+
/** 真正需要 pre-step 监听器的消息类型;runtime-context 由 suppress 处理。 */
|
|
18
|
+
const OVERLAY_KINDS = ['agent-instructions', 'skill-catalog', 'time-context']
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* 这份 recipe 是否需要 pre-step 消息改写。
|
|
22
|
+
* @param {object} recipe - 提示词配方。
|
|
23
|
+
* @returns {boolean} 是否需要注册监听器。
|
|
24
|
+
*/
|
|
25
|
+
export function hasMessageOverrides(recipe) {
|
|
26
|
+
const messages = recipe?.messages
|
|
27
|
+
if (messages === null || typeof messages !== 'object') return false
|
|
28
|
+
// runtime-context 的 hidden 由 systemPrompt.suppressRuntimeContext() 处理,
|
|
29
|
+
// 不需要在这里改消息;把它排除,避免注册一个什么都不做的监听器。
|
|
30
|
+
return OVERLAY_KINDS.some((kind) => {
|
|
31
|
+
const spec = messages[kind]
|
|
32
|
+
if (spec === null || typeof spec !== 'object') return false
|
|
33
|
+
if (spec.hidden === true || typeof spec.text === 'string') return true
|
|
34
|
+
return Array.isArray(spec.hiddenEntries) || (spec.descriptions !== null && typeof spec.descriptions === 'object')
|
|
35
|
+
})
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** 一条消息的正文(只取 text 块);没有时为空串。 */
|
|
39
|
+
function textOf(message) {
|
|
40
|
+
const blocks = Array.isArray(message?.content) ? message.content : []
|
|
41
|
+
return blocks
|
|
42
|
+
.filter((block) => block?.type === 'text' && typeof block.text === 'string')
|
|
43
|
+
.map((block) => block.text)
|
|
44
|
+
.join('\n\n')
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** 用一段文本替换消息正文;保留 id / source。 */
|
|
48
|
+
function withText(message, text) {
|
|
49
|
+
return {
|
|
50
|
+
...message,
|
|
51
|
+
content: [{ type: 'text', text }],
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* time-context 的 `source.sections` 是会话页读的逐段事实;正文改了它也一起改,
|
|
57
|
+
* 否则「原文」和「分段」会互相矛盾。
|
|
58
|
+
*/
|
|
59
|
+
function withTimeSource(message, text) {
|
|
60
|
+
const sections = message?.source?.sections
|
|
61
|
+
if (!Array.isArray(sections)) return message
|
|
62
|
+
return {
|
|
63
|
+
...message,
|
|
64
|
+
source: {
|
|
65
|
+
...message.source,
|
|
66
|
+
sections: sections.map((section, index) => index === 0 ? { ...section, text } : section),
|
|
67
|
+
},
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* 读取目录消息里的条目;不可读时返回 undefined(不改这条消息)。
|
|
73
|
+
* @param {object} source - 消息的 source。
|
|
74
|
+
* @returns {Array<{name: string, description: string}> | undefined} 条目。
|
|
75
|
+
*/
|
|
76
|
+
function catalogEntriesOf(source) {
|
|
77
|
+
if (!Array.isArray(source?.entries)) return undefined
|
|
78
|
+
const entries = []
|
|
79
|
+
for (const entry of source.entries) {
|
|
80
|
+
if (entry === null || typeof entry !== 'object') return undefined
|
|
81
|
+
if (typeof entry.name !== 'string' || entry.name.length === 0 || typeof entry.description !== 'string') return undefined
|
|
82
|
+
entries.push({ name: entry.name, description: entry.description })
|
|
83
|
+
}
|
|
84
|
+
return entries
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* 按 recipe 改写一份 pre-step 决策里的消息。
|
|
89
|
+
*
|
|
90
|
+
* @param {Array<object>} messages - `decision.messages`。
|
|
91
|
+
* @param {object} recipe - 提示词配方。
|
|
92
|
+
* @returns {{messages: Array<object>, applied: {messages: string[], skipped: string[]}}} 改写结果。
|
|
93
|
+
*/
|
|
94
|
+
export function overlayStepMessages(messages, recipe) {
|
|
95
|
+
const instructions = recipe?.messages?.['agent-instructions']
|
|
96
|
+
const catalog = recipe?.messages?.['skill-catalog']
|
|
97
|
+
const time = recipe?.messages?.['time-context']
|
|
98
|
+
const instructionsHidden = instructions?.hidden === true || instructions?.text === ''
|
|
99
|
+
const instructionsText = typeof instructions?.text === 'string' ? instructions.text : undefined
|
|
100
|
+
const timeHidden = time?.hidden === true || time?.text === ''
|
|
101
|
+
const timeText = typeof time?.text === 'string' ? time.text : undefined
|
|
102
|
+
const catalogHidden = catalog?.hidden === true
|
|
103
|
+
const hasCatalogOverrides = Array.isArray(catalog?.hiddenEntries) || (catalog?.descriptions !== null && typeof catalog?.descriptions === 'object')
|
|
104
|
+
|
|
105
|
+
if (!instructionsHidden && instructionsText === undefined && !timeHidden && timeText === undefined && !catalogHidden && !hasCatalogOverrides) {
|
|
106
|
+
return { messages, applied: { messages: [], skipped: [] } }
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const out = []
|
|
110
|
+
const applied = { messages: [], skipped: [] }
|
|
111
|
+
// 只改 baseline 那条;后续的“指令变更”消息承载的是文件变化,继续保留。
|
|
112
|
+
const hasBaseline = (Array.isArray(messages) ? messages : []).some((message) => message?.source?.kind === 'agent-instructions' && message?.source?.baseline === true)
|
|
113
|
+
let instructionsRewritten = false
|
|
114
|
+
|
|
115
|
+
for (const message of Array.isArray(messages) ? messages : []) {
|
|
116
|
+
const kind = message?.source?.kind
|
|
117
|
+
|
|
118
|
+
if (kind === 'agent-instructions') {
|
|
119
|
+
const target = message.source.baseline === true || (!hasBaseline && !instructionsRewritten)
|
|
120
|
+
if (!target) {
|
|
121
|
+
out.push(message)
|
|
122
|
+
continue
|
|
123
|
+
}
|
|
124
|
+
instructionsRewritten = true
|
|
125
|
+
if (instructionsHidden) {
|
|
126
|
+
applied.skipped.push(kind)
|
|
127
|
+
continue
|
|
128
|
+
}
|
|
129
|
+
if (instructionsText !== undefined) {
|
|
130
|
+
out.push(withText(message, instructionsText))
|
|
131
|
+
applied.messages.push(kind)
|
|
132
|
+
continue
|
|
133
|
+
}
|
|
134
|
+
out.push(message)
|
|
135
|
+
continue
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (kind === 'time-context') {
|
|
139
|
+
if (timeHidden) {
|
|
140
|
+
applied.skipped.push(kind)
|
|
141
|
+
continue
|
|
142
|
+
}
|
|
143
|
+
if (timeText !== undefined) {
|
|
144
|
+
out.push(withTimeSource(withText(message, timeText), timeText))
|
|
145
|
+
applied.messages.push(kind)
|
|
146
|
+
continue
|
|
147
|
+
}
|
|
148
|
+
out.push(message)
|
|
149
|
+
continue
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
if (kind === 'skill-catalog') {
|
|
153
|
+
if (catalogHidden) {
|
|
154
|
+
applied.skipped.push(kind)
|
|
155
|
+
continue
|
|
156
|
+
}
|
|
157
|
+
if (!hasCatalogOverrides) {
|
|
158
|
+
out.push(message)
|
|
159
|
+
continue
|
|
160
|
+
}
|
|
161
|
+
const entries = catalogEntriesOf(message.source)
|
|
162
|
+
if (entries === undefined) {
|
|
163
|
+
// 外部写入的种子可能只有 kind;不猜条目,保持原样。
|
|
164
|
+
out.push(message)
|
|
165
|
+
applied.skipped.push(kind + ':unreadable')
|
|
166
|
+
continue
|
|
167
|
+
}
|
|
168
|
+
const next = applyCatalogOverrides(entries, catalog)
|
|
169
|
+
const text = message.source.update === true
|
|
170
|
+
? renderCatalogUpdate(next, message.source.incomplete === true)
|
|
171
|
+
: renderCatalogMessage(next, message.source.incomplete === true)
|
|
172
|
+
out.push(withText(message, text))
|
|
173
|
+
applied.messages.push(kind)
|
|
174
|
+
continue
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
out.push(message)
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
return { messages: out, applied }
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** 导出给自检:消息正文的读取口径与改写保持一致。 */
|
|
184
|
+
export { textOf }
|