@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,200 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 预测首请求里的 `skill-catalog` 消息。
|
|
3
|
+
*
|
|
4
|
+
* 它同样不进系统提示词:`@deepseek-ai/dsh-tool-skill` 在 `agent/pre-step` 里向
|
|
5
|
+
* `ctx.skills` 要一份目录快照,再渲染成一条 user 消息追加到请求末尾。目录内容随
|
|
6
|
+
* **工作区**变化(实测同一台机器两个工作区分别是 10 与 27 个 skill),所以这条消息
|
|
7
|
+
* 是「预演必须带工作区条件」的直接原因。
|
|
8
|
+
*
|
|
9
|
+
* 渲染格式抄自已安装版本 `0.2.2-rc.1` 的 `@deepseek-ai/dsh-tool-skill/lib/index.js`,
|
|
10
|
+
* 逐字正确性由两份日志 fixture 保证。
|
|
11
|
+
* @module dsh-plugin-console/host/preview-skills
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { CATALOG_DEFAULT_MAX_LENGTH, catalogDescription, escapeAttr, escapeText, renderCatalogMessage } from './catalog.js'
|
|
15
|
+
import { hostModuleResolver } from './preview-modules.js'
|
|
16
|
+
|
|
17
|
+
/** 目录消息的 pre-step 所有者;是否启用见 `preview-modules.js` 的判定口径。 */
|
|
18
|
+
const MODULE = '@deepseek-ai/dsh-tool-skill'
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* 模式 scope 里的 skill 提供方。
|
|
22
|
+
*
|
|
23
|
+
* 宿主层的 `skill-filesystem` 在 web/desktop profile 里同样是 disabled 的;
|
|
24
|
+
* 拿不到模式 scope 时读不到它的提供方,目录会是空的——那不是「没有 skill」。
|
|
25
|
+
*/
|
|
26
|
+
const SKILL_PROVIDER = '@deepseek-ai/dsh-skill-filesystem'
|
|
27
|
+
|
|
28
|
+
/** 目录快照的上限:cwd 扫描可能真的去读磁盘,预演不该被它拖住。 */
|
|
29
|
+
const SNAPSHOT_TIMEOUT_MS = 3000
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* 预测一条首请求里的 `skill-catalog` 消息。
|
|
33
|
+
* @param {object} ctx - host 插件上下文。
|
|
34
|
+
* @param {object} options - 条件与装配结果。
|
|
35
|
+
* @param {string} options.workspacePath - 工作区路径(cwd)。
|
|
36
|
+
* @param {symbol | undefined} options.scopeKey - 预设 scope 标识。
|
|
37
|
+
* @param {Array<{name: string}>} options.tools - 装配出的工具 schema。
|
|
38
|
+
* @param {object} [options.modules] - `resolveModules()` 的结果;省略时只按宿主行判定。
|
|
39
|
+
* @returns {Promise<object>} `{ available, text, entries, complete, confidence, reason? }`。
|
|
40
|
+
*/
|
|
41
|
+
export async function previewSkills(ctx, options) {
|
|
42
|
+
const unavailable = (reason) => ({ available: false, reason, text: '', entries: [], complete: false })
|
|
43
|
+
const devices = options.modules ?? hostModuleResolver(ctx)
|
|
44
|
+
const device = devices.get(MODULE)
|
|
45
|
+
if (!device.available) return unavailable(device.reason)
|
|
46
|
+
// 目录只随这份确切注册一起出现:工具被掩码或同名覆盖挡掉时,模型根本看不到目录。
|
|
47
|
+
if (!(options.tools ?? []).some((tool) => tool?.name === 'skill')) return unavailable('本次装配里看不到 skill 工具')
|
|
48
|
+
if (typeof options.workspacePath !== 'string' || options.workspacePath.length === 0) return unavailable('未选择工作区')
|
|
49
|
+
// 宿主层没有提供方时,目录完全来自模式 scope;没有 scope 就只能读到空目录。
|
|
50
|
+
if (options.scopeKey === undefined && devices.onlyFromPreset(SKILL_PROVIDER)) {
|
|
51
|
+
return unavailable('没有取得模式 scope:读不到该模式注册的 skill 提供方')
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const maxLength = typeof device.config?.catalogDescriptionMaxLength === 'number' && Number.isInteger(device.config.catalogDescriptionMaxLength) && device.config.catalogDescriptionMaxLength >= 3
|
|
55
|
+
? device.config.catalogDescriptionMaxLength
|
|
56
|
+
: CATALOG_DEFAULT_MAX_LENGTH
|
|
57
|
+
|
|
58
|
+
const skills = ctx.get('skills')
|
|
59
|
+
if (typeof skills?.snapshot !== 'function') return unavailable('skills 服务不可用')
|
|
60
|
+
|
|
61
|
+
let snapshot
|
|
62
|
+
try {
|
|
63
|
+
snapshot = await skills.snapshot({
|
|
64
|
+
cwd: options.workspacePath,
|
|
65
|
+
...(options.scopeKey === undefined ? {} : { scope: options.scopeKey }),
|
|
66
|
+
signal: AbortSignal.timeout(SNAPSHOT_TIMEOUT_MS),
|
|
67
|
+
})
|
|
68
|
+
} catch (error) {
|
|
69
|
+
return unavailable(`skill 目录快照失败:${error instanceof Error ? error.message : String(error)}`)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// 快照不完整**不等于没有目录**:真实的 DSH 仍然会发一份目录,并在帧里附一句
|
|
73
|
+
// 「有些 skill 没能确认」。早先这里直接判成不可用,结果是「预演没有、真实请求有」——
|
|
74
|
+
// 比给一份带警告的目录更误导。缺少的那份提示文案与 DSH 逐字一致。
|
|
75
|
+
const incomplete = snapshot?.complete !== true
|
|
76
|
+
const entries = (snapshot?.skills ?? [])
|
|
77
|
+
.filter((skill) => skill?.invocation?.modelInvocable === true)
|
|
78
|
+
.map((skill) => ({
|
|
79
|
+
name: skill.name,
|
|
80
|
+
description: catalogDescription(skill.description, maxLength),
|
|
81
|
+
source: skill.source,
|
|
82
|
+
provider: skill.provider,
|
|
83
|
+
}))
|
|
84
|
+
if (entries.length === 0 && !incomplete) {
|
|
85
|
+
return { available: true, text: '', entries: [], complete: true, confidence: 'derived' }
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return {
|
|
89
|
+
available: true,
|
|
90
|
+
confidence: 'derived',
|
|
91
|
+
complete: !incomplete,
|
|
92
|
+
incomplete,
|
|
93
|
+
entries,
|
|
94
|
+
text: renderCatalogMessage(entries, incomplete),
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* 一条 skill 的资源提示。
|
|
100
|
+
*
|
|
101
|
+
* 三种资源基座(目录 / URL / 不透明)加的句子不同;没有基座时说明由 provider 托管。
|
|
102
|
+
* @param {object} skill - 已加载的 skill。
|
|
103
|
+
* @returns {string[]} 提示行。
|
|
104
|
+
*/
|
|
105
|
+
function renderResourceHint(skill) {
|
|
106
|
+
const base = skill.resourceBase
|
|
107
|
+
if (base === undefined) {
|
|
108
|
+
const provider = escapeText(skill.provider ?? '')
|
|
109
|
+
return [`Resources for this skill are managed by provider "${provider}".`, 'Load referenced resources only as needed.']
|
|
110
|
+
}
|
|
111
|
+
if (base.kind === 'directory') {
|
|
112
|
+
const path = escapeText(base.path)
|
|
113
|
+
return [`Base directory for this skill: ${path}`, 'Resolve relative paths mentioned by this skill against the base directory before using them. Load referenced resources only as needed.']
|
|
114
|
+
}
|
|
115
|
+
if (base.kind === 'url') {
|
|
116
|
+
const url = escapeText(base.url)
|
|
117
|
+
return [`Base URL for this skill: ${url}`, 'Resolve relative URLs mentioned by this skill against the base URL before using them. Load referenced resources only as needed.']
|
|
118
|
+
}
|
|
119
|
+
const description = escapeText(base.description ?? '')
|
|
120
|
+
return [`Resources for this skill: ${description}`, 'Load referenced resources only as needed.']
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* `<skill_content>` 块:`skill` 工具结果与用户 `/name` 注入共用同一份渲染。
|
|
125
|
+
*
|
|
126
|
+
* 正文原样嵌入(skill 是可信的本地内容),名字走转义后的属性。
|
|
127
|
+
* @param {object} skill - 已加载的 skill。
|
|
128
|
+
* @returns {string} 模型可见文本。
|
|
129
|
+
*/
|
|
130
|
+
function renderSkillContent(skill) {
|
|
131
|
+
const name = escapeAttr(skill.name)
|
|
132
|
+
return [
|
|
133
|
+
`<skill_content name="${name}">`,
|
|
134
|
+
'<skill_resources>',
|
|
135
|
+
...renderResourceHint(skill),
|
|
136
|
+
'</skill_resources>',
|
|
137
|
+
'',
|
|
138
|
+
'<skill_instructions>',
|
|
139
|
+
skill.content ?? '',
|
|
140
|
+
'</skill_instructions>',
|
|
141
|
+
'</skill_content>',
|
|
142
|
+
].join('\n')
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** 用户消息里的 `/name` 手势:与 DSH 的公共 skill 名文法一致。 */
|
|
146
|
+
const SKILL_GESTURE = /(^|\s)\/([a-z0-9]+(?:-[a-z0-9]+)*)(?=\s|$)/g
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* 从首条消息草稿里取 `/name` 手势,按首次出现顺序去重。
|
|
150
|
+
* @param {string} text - 草稿正文。
|
|
151
|
+
* @returns {string[]} 候选 skill 名。
|
|
152
|
+
*/
|
|
153
|
+
export function invokedSkillNames(text) {
|
|
154
|
+
const names = []
|
|
155
|
+
for (const match of String(text ?? '').matchAll(SKILL_GESTURE)) {
|
|
156
|
+
const name = match[2]
|
|
157
|
+
if (name !== undefined && !names.includes(name)) names.push(name)
|
|
158
|
+
}
|
|
159
|
+
return names
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* 预测一条 `/name` 带出来的 `skill-invocation` 消息。
|
|
164
|
+
* @param {object} ctx - host 插件上下文。
|
|
165
|
+
* @param {object} options - 条件。
|
|
166
|
+
* @param {string} options.name - skill 名。
|
|
167
|
+
* @param {string} options.workspacePath - 工作区路径。
|
|
168
|
+
* @param {symbol | undefined} options.scopeKey - 预设 scope。
|
|
169
|
+
* @param {object} [options.modules] - `resolveModules()` 的结果;省略时只按宿主行判定。
|
|
170
|
+
* @returns {Promise<object>} `{ available, text, skill, reason? }`。
|
|
171
|
+
*/
|
|
172
|
+
export async function previewSkillInvocation(ctx, options) {
|
|
173
|
+
const device = (options.modules ?? hostModuleResolver(ctx)).get(MODULE)
|
|
174
|
+
if (!device.available) return { available: false, reason: device.reason }
|
|
175
|
+
if (typeof options.workspacePath !== 'string' || options.workspacePath.length === 0) {
|
|
176
|
+
return { available: false, reason: '未选择工作区' }
|
|
177
|
+
}
|
|
178
|
+
const skills = ctx.get('skills')
|
|
179
|
+
if (typeof skills?.get !== 'function') return { available: false, reason: 'skills 服务不可用' }
|
|
180
|
+
let skill
|
|
181
|
+
try {
|
|
182
|
+
skill = await skills.get(options.name, {
|
|
183
|
+
cwd: options.workspacePath,
|
|
184
|
+
...(options.scopeKey === undefined ? {} : { scope: options.scopeKey }),
|
|
185
|
+
signal: AbortSignal.timeout(SNAPSHOT_TIMEOUT_MS),
|
|
186
|
+
})
|
|
187
|
+
} catch (error) {
|
|
188
|
+
return { available: false, reason: `skill 读取失败:${error instanceof Error ? error.message : String(error)}` }
|
|
189
|
+
}
|
|
190
|
+
if (skill === undefined) return { available: false, reason: `当前工作区没有 skill「${options.name}」` }
|
|
191
|
+
// 只有用户可调用的 skill 才会被 `/name` 展开;模型可调用但用户不可调用的名字只能走工具。
|
|
192
|
+
if (skill.invocation?.userInvocable !== true) {
|
|
193
|
+
return { available: false, reason: `skill「${options.name}」不允许用户调用` }
|
|
194
|
+
}
|
|
195
|
+
return {
|
|
196
|
+
available: true,
|
|
197
|
+
text: renderSkillContent(skill),
|
|
198
|
+
skill: { name: skill.name, provider: skill.provider, resourceBase: skill.resourceBase },
|
|
199
|
+
}
|
|
200
|
+
}
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 提示词注册表的读取。
|
|
3
|
+
*
|
|
4
|
+
* 名字与顺序以注册表为准(`systemPrompt.layers`),不靠源码猜;源码只用来回答
|
|
5
|
+
* 「谁写的」。文本则优先取 `assemble()` —— 它给出的是模型真正收到的东西,
|
|
6
|
+
* 包含 `text` 是函数时求值后的结果。
|
|
7
|
+
* @module dsh-plugin-console/host/prompt
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* 读取提示词注册表里此刻真正存在的区块与上下文。
|
|
12
|
+
* @param {object} ctx - host 插件上下文。
|
|
13
|
+
* @param {symbol} [scope] - agent scope;省略表示全局层。
|
|
14
|
+
* @returns {Promise<{sections: Array<object>, contexts: Array<object>, tools?: Array<object>} | undefined>} 注册内容。
|
|
15
|
+
*/
|
|
16
|
+
export function readPromptRegistry(ctx, scope) {
|
|
17
|
+
const systemPrompt = ctx.get('systemPrompt')
|
|
18
|
+
if (systemPrompt === undefined) return undefined
|
|
19
|
+
|
|
20
|
+
// 优先用 assemble:它给出的是模型真正会收到的东西,
|
|
21
|
+
// 包含 `text` 是函数时求值后的结果、以及按 toolOrder 排好的完整工具 schema。
|
|
22
|
+
// assemble 给文本与工具 schema(函数形态的 text 已求值),但它不返回 order;
|
|
23
|
+
// 注册表给 order、interpolate、complete。两者按 name 合并。
|
|
24
|
+
const registry = readLayerRegistry(systemPrompt)
|
|
25
|
+
if (typeof systemPrompt.assemble !== 'function') return registry
|
|
26
|
+
|
|
27
|
+
return Promise.resolve(systemPrompt.assemble({ ...(scope === undefined ? {} : { scope }) }))
|
|
28
|
+
.then((result) => {
|
|
29
|
+
const normalized = normalizeAssembly(result)
|
|
30
|
+
if (normalized === undefined) return registry
|
|
31
|
+
// 同一个名字可以既注册成 section 又注册成 context(harness:source 就是这样),
|
|
32
|
+
// 两张表必须分开索引,否则 context 的 order 会盖掉 section 的。
|
|
33
|
+
const indexOf = (rows) => new Map((rows ?? []).map((row) => [row.name, row]))
|
|
34
|
+
const sectionMeta = indexOf(registry?.sections)
|
|
35
|
+
const contextMeta = indexOf(registry?.contexts)
|
|
36
|
+
/** 把注册表里的静态字段补回 assemble 的行上。 */
|
|
37
|
+
const merge = (row, meta) => {
|
|
38
|
+
if (meta === undefined) return row
|
|
39
|
+
return {
|
|
40
|
+
...row,
|
|
41
|
+
...(meta.order === undefined ? {} : { order: meta.order }),
|
|
42
|
+
...(meta.interpolate === undefined ? {} : { interpolate: meta.interpolate }),
|
|
43
|
+
...(meta.provider === true ? { provider: true } : {}),
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return {
|
|
47
|
+
sections: normalized.sections.map((row) => merge(row, sectionMeta.get(row.name))),
|
|
48
|
+
contexts: normalized.contexts.map((row) => merge(row, contextMeta.get(row.name))),
|
|
49
|
+
tools: normalized.tools,
|
|
50
|
+
}
|
|
51
|
+
})
|
|
52
|
+
.catch(() => registry)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* 把 assemble 的结果整理成可传输的形状。
|
|
57
|
+
*
|
|
58
|
+
* 与直接读注册表的区别:`text` 字段可能是函数(运行时才知道内容),
|
|
59
|
+
* assemble 已经把它求值成字符串,因此这里拿到的是**渲染前的完整文本**,
|
|
60
|
+
* 而不是 160 字的预览。
|
|
61
|
+
* @param {object} assembly - `systemPrompt.assemble()` 的结果。
|
|
62
|
+
* @returns {{sections: Array<object>, contexts: Array<object>, tools: Array<object>} | undefined} 归一化结果。
|
|
63
|
+
*/
|
|
64
|
+
export function normalizeAssembly(assembly) {
|
|
65
|
+
if (assembly === undefined || assembly === null) return undefined
|
|
66
|
+
const text = (value) => (typeof value === 'string' ? value : undefined)
|
|
67
|
+
return {
|
|
68
|
+
sections: (assembly.sections ?? []).map((section) => ({
|
|
69
|
+
name: section.name,
|
|
70
|
+
text: text(section.text),
|
|
71
|
+
length: typeof section.text === 'string' ? section.text.length : undefined,
|
|
72
|
+
interpolate: section.interpolate === false ? false : undefined,
|
|
73
|
+
})),
|
|
74
|
+
contexts: (assembly.contexts ?? []).map((context) => ({
|
|
75
|
+
name: context.name,
|
|
76
|
+
text: text(context.text),
|
|
77
|
+
length: typeof context.text === 'string' ? context.text.length : undefined,
|
|
78
|
+
required: context.required === true ? true : undefined,
|
|
79
|
+
})),
|
|
80
|
+
tools: (assembly.tools ?? []).map((tool) => ({
|
|
81
|
+
name: tool.name,
|
|
82
|
+
description: text(tool.description),
|
|
83
|
+
parameters: tool.parameters,
|
|
84
|
+
deferLoading: tool.deferLoading === true ? true : undefined,
|
|
85
|
+
})),
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* 回退路径:直接读 `layers.global` 的注册表。
|
|
91
|
+
*
|
|
92
|
+
* 只在 `assemble` 不可用或抛错时使用。这条路径拿不到函数形态的 `text`
|
|
93
|
+
* (那是运行时才求值的),所以文本会缺失——这是降级,不是常态。
|
|
94
|
+
* @param {object} systemPrompt - systemPrompt 服务。
|
|
95
|
+
* @returns {{sections: Array<object>, contexts: Array<object>} | undefined} 注册内容。
|
|
96
|
+
*/
|
|
97
|
+
export function readLayerRegistry(systemPrompt) {
|
|
98
|
+
const layers = systemPrompt?.layers
|
|
99
|
+
if (layers === undefined || layers.global === undefined) return undefined
|
|
100
|
+
|
|
101
|
+
const rowsOf = (table) => {
|
|
102
|
+
const out = []
|
|
103
|
+
if (table === undefined || typeof table.entries !== 'function') return out
|
|
104
|
+
for (const [name, value] of table.entries()) {
|
|
105
|
+
const literal = typeof value?.text === 'string' ? value.text : undefined
|
|
106
|
+
out.push({
|
|
107
|
+
name,
|
|
108
|
+
order: typeof value?.order === 'number' ? value.order : undefined,
|
|
109
|
+
text: literal,
|
|
110
|
+
length: literal === undefined ? undefined : literal.length,
|
|
111
|
+
provider: typeof value?.text === 'function' ? true : undefined,
|
|
112
|
+
})
|
|
113
|
+
}
|
|
114
|
+
return out
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return {
|
|
118
|
+
sections: rowsOf(layers.global.sections),
|
|
119
|
+
contexts: rowsOf(layers.global.contexts),
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* 逐层枚举一个 scope 链,找出全局提示词段/上下文的同名替换与 scope 追加。
|
|
125
|
+
*
|
|
126
|
+
* `chainLayers(scope)` 返回的是「已存在的 scope 覆盖层」,按由远到近排列,不含全局层。
|
|
127
|
+
* 同名条目在更近的层里再次出现时,近者胜——所以这里用 Map 记最后出现的那个层,
|
|
128
|
+
* 而不是第一次出现的层。
|
|
129
|
+
*
|
|
130
|
+
* 这是**名字级**事实:只说明「这个名字被 scope 层重新注册了」,不说明是谁注册的;
|
|
131
|
+
* owner 由调用方用现有源码扫描索引回填。
|
|
132
|
+
* @param {object} ctx - host 插件上下文。
|
|
133
|
+
* @param {symbol | string | undefined} scopeKey - `systemPrompt.layers` 的 scope 标识。
|
|
134
|
+
* @returns {{layerCount: number, sections: object, contexts: object} | undefined} 覆盖摘要;读不到时 undefined。
|
|
135
|
+
*/
|
|
136
|
+
export function readScopePromptOverrides(ctx, scopeKey) {
|
|
137
|
+
if (scopeKey === undefined || scopeKey === null) return undefined
|
|
138
|
+
const layers = ctx.get('systemPrompt')?.layers
|
|
139
|
+
if (layers === undefined || layers.global === undefined || typeof layers.chainLayers !== 'function') return undefined
|
|
140
|
+
|
|
141
|
+
let chain
|
|
142
|
+
try {
|
|
143
|
+
chain = layers.chainLayers(scopeKey)
|
|
144
|
+
} catch {
|
|
145
|
+
return undefined
|
|
146
|
+
}
|
|
147
|
+
if (!Array.isArray(chain)) return undefined
|
|
148
|
+
|
|
149
|
+
const rowsOf = (layer, table) => {
|
|
150
|
+
const entries = layer?.[table]
|
|
151
|
+
if (entries === undefined || typeof entries.entries !== 'function') return []
|
|
152
|
+
return [...entries.entries()]
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** 一张注册表(sections 或 contexts)的替换 / 追加摘要。 */
|
|
156
|
+
const summarize = (table) => {
|
|
157
|
+
const globalNames = new Set(rowsOf(layers.global, table).map(([name]) => name))
|
|
158
|
+
const overridden = new Map()
|
|
159
|
+
const added = new Map()
|
|
160
|
+
const perLayer = []
|
|
161
|
+
chain.forEach((layer, index) => {
|
|
162
|
+
const rows = rowsOf(layer, table)
|
|
163
|
+
perLayer.push({ index: index + 1, names: rows.map(([name]) => name) })
|
|
164
|
+
for (const [name, value] of rows) {
|
|
165
|
+
const row = {
|
|
166
|
+
name,
|
|
167
|
+
layer: index + 1,
|
|
168
|
+
...(typeof value?.order === 'number' ? { order: value.order } : {}),
|
|
169
|
+
...(value?.complete === true ? { complete: true } : {}),
|
|
170
|
+
}
|
|
171
|
+
// 由远到近遍历,后写的覆盖先写的:更近的层才是最终生效者。
|
|
172
|
+
if (globalNames.has(name)) overridden.set(name, row)
|
|
173
|
+
else added.set(name, row)
|
|
174
|
+
}
|
|
175
|
+
})
|
|
176
|
+
return { overridden: [...overridden.values()], added: [...added.values()], layers: perLayer }
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
try {
|
|
180
|
+
return {
|
|
181
|
+
layerCount: chain.length,
|
|
182
|
+
sections: summarize('sections'),
|
|
183
|
+
contexts: summarize('contexts'),
|
|
184
|
+
}
|
|
185
|
+
} catch {
|
|
186
|
+
return undefined
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* 读出某个 scope 链上「独占型」提示词段的名字(`complete: true` 的那一段)。
|
|
192
|
+
*
|
|
193
|
+
* 独占段一旦生效,`assemble` 会把 sections 收窄成只有它一段——别的段(包括
|
|
194
|
+
* 追加段落)都不会出现在模型看到的内容里。所以:
|
|
195
|
+
* - 覆盖它时必须带着 `complete` 注册回去,否则独占语义消失;
|
|
196
|
+
* - 追加段落想让它生效,只能并进这一段里,不能再单独注册一段。
|
|
197
|
+
* @param {object} ctx - 带 scope 的上下文(agent.ctx)。
|
|
198
|
+
* @param {Function} scopeKeyOf - scope 标识解析器(避免本模块反向依赖 fiber)。
|
|
199
|
+
* @returns {string | undefined} 独占段名字;没有则 undefined。
|
|
200
|
+
*/
|
|
201
|
+
export function completeSectionOf(ctx, scopeKeyOf) {
|
|
202
|
+
const layers = ctx?.systemPrompt?.layers
|
|
203
|
+
if (layers === undefined || typeof layers.merge !== 'function') return undefined
|
|
204
|
+
try {
|
|
205
|
+
// 必须作为方法调用:merge 内部用 this.chainLayers。
|
|
206
|
+
for (const [name, section] of layers.merge(scopeKeyOf(ctx), (layer) => layer.sections)) {
|
|
207
|
+
if (section?.complete === true) return name
|
|
208
|
+
}
|
|
209
|
+
} catch {
|
|
210
|
+
// 读不到就当没有独占段
|
|
211
|
+
}
|
|
212
|
+
return undefined
|
|
213
|
+
}
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 把一份提示词模板 recipe 叠加到**预演结果**上。
|
|
3
|
+
*
|
|
4
|
+
* 这一层只做纯投影:不注册 section/context、不写 session log、不创建 Agent。
|
|
5
|
+
* 真实会话的同一份语义由 `templates/materialize.js` + `templates/message-overlay.js`
|
|
6
|
+
* 落地;两边的条目过滤与目录帧共用 `catalog.js`,避免预演与实测漂移。
|
|
7
|
+
* @module dsh-plugin-console/host/recipe-overlay
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { applyCatalogOverrides, renderCatalogMessage } from './catalog.js'
|
|
11
|
+
import { RUNTIME_CONTEXT_PREFIX } from './preview-contexts.js'
|
|
12
|
+
|
|
13
|
+
/** 追加段落的名字与位置,与物化时保持一致(DSH 最高段 order 是 10200)。 */
|
|
14
|
+
const APPEND_SECTION = 'template:extra'
|
|
15
|
+
const APPEND_ORDER = 20000
|
|
16
|
+
|
|
17
|
+
/** 把 `template` 加进依据列表,不重复。 */
|
|
18
|
+
function withTemplateBasis(message) {
|
|
19
|
+
const basis = Array.isArray(message.basis) ? message.basis : []
|
|
20
|
+
return basis.includes('template') ? basis : [...basis, 'template']
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** 一条被模板改写的预览消息。 */
|
|
24
|
+
function overriddenMessage(message, text, extra = {}) {
|
|
25
|
+
return {
|
|
26
|
+
...message,
|
|
27
|
+
text,
|
|
28
|
+
length: text.length,
|
|
29
|
+
overridden: true,
|
|
30
|
+
basis: withTemplateBasis(message),
|
|
31
|
+
...extra,
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* 叠加 system 段、runtime context、工具可见集,并重算统计。
|
|
37
|
+
* @param {object} assembly - `previewAssembly()` 的结果。
|
|
38
|
+
* @param {object} recipe - 提示词模板配方。
|
|
39
|
+
* @returns {object} 叠加后的装配结果。
|
|
40
|
+
*/
|
|
41
|
+
export function applyRecipeToAssembly(assembly, recipe) {
|
|
42
|
+
const prompt = recipe?.prompt ?? {}
|
|
43
|
+
const sectionOverrides = prompt.sectionOverrides ?? {}
|
|
44
|
+
const applied = { sections: [], contexts: [], tools: [], appended: false }
|
|
45
|
+
|
|
46
|
+
const sections = (assembly.sections ?? []).map((section) => {
|
|
47
|
+
const text = sectionOverrides[section.name]
|
|
48
|
+
if (typeof text !== 'string') return section
|
|
49
|
+
applied.sections.push(section.name)
|
|
50
|
+
return {
|
|
51
|
+
...section,
|
|
52
|
+
text,
|
|
53
|
+
rendered: text,
|
|
54
|
+
length: text.length,
|
|
55
|
+
interpolate: false,
|
|
56
|
+
unresolved: [],
|
|
57
|
+
overridden: true,
|
|
58
|
+
}
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
// 独占提示词下追加段落无处安放;客户端算 recipe 时已经折进那一整段。
|
|
62
|
+
if (typeof prompt.appendText === 'string' && prompt.appendText.length > 0 && assembly.completeSection === null) {
|
|
63
|
+
const text = prompt.appendText
|
|
64
|
+
sections.push({
|
|
65
|
+
index: sections.length,
|
|
66
|
+
name: APPEND_SECTION,
|
|
67
|
+
order: APPEND_ORDER,
|
|
68
|
+
text,
|
|
69
|
+
rendered: text,
|
|
70
|
+
length: text.length,
|
|
71
|
+
interpolate: false,
|
|
72
|
+
unresolved: [],
|
|
73
|
+
overridden: true,
|
|
74
|
+
template: true,
|
|
75
|
+
})
|
|
76
|
+
applied.appended = true
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const contexts = (assembly.contexts ?? []).map((context) => {
|
|
80
|
+
const text = recipe?.contexts?.[context.name]
|
|
81
|
+
if (typeof text !== 'string') return context
|
|
82
|
+
applied.contexts.push(context.name)
|
|
83
|
+
return { ...context, text, length: text.length, interpolate: false, overridden: true }
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
const disabled = new Set(Array.isArray(recipe?.tools?.disabled) ? recipe.tools.disabled : [])
|
|
87
|
+
const tools = (assembly.tools ?? []).filter((tool) => {
|
|
88
|
+
if (typeof tool?.name !== 'string' || !disabled.has(tool.name)) return true
|
|
89
|
+
applied.tools.push(tool.name)
|
|
90
|
+
return false
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
const visible = new Set(tools.map((tool) => tool.name))
|
|
94
|
+
const toolSet = assembly.toolSet === undefined ? undefined : {
|
|
95
|
+
...assembly.toolSet,
|
|
96
|
+
layers: Object.fromEntries(
|
|
97
|
+
Object.entries(assembly.toolSet.layers ?? {}).map(([layer, names]) => [layer, (names ?? []).filter((name) => visible.has(name))]),
|
|
98
|
+
),
|
|
99
|
+
layerByName: Object.fromEntries(
|
|
100
|
+
Object.entries(assembly.toolSet.layerByName ?? {}).filter(([name]) => visible.has(name)),
|
|
101
|
+
),
|
|
102
|
+
counts: Object.fromEntries(
|
|
103
|
+
Object.entries(assembly.toolSet.layers ?? {}).map(([layer, names]) => [layer, (names ?? []).filter((name) => visible.has(name)).length]),
|
|
104
|
+
),
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const systemText = sections.map((section) => section.rendered ?? section.text ?? '').filter((text) => text.length > 0).join('\n\n')
|
|
108
|
+
const stats = {
|
|
109
|
+
...(assembly.stats ?? {}),
|
|
110
|
+
sections: sections.length,
|
|
111
|
+
sectionChars: sections.reduce((sum, section) => sum + (section.length ?? 0), 0),
|
|
112
|
+
contexts: contexts.length,
|
|
113
|
+
contextChars: contexts.reduce((sum, context) => sum + (context.length ?? 0), 0),
|
|
114
|
+
tools: tools.length,
|
|
115
|
+
toolSchemaChars: JSON.stringify(tools).length,
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return {
|
|
119
|
+
...assembly,
|
|
120
|
+
sections,
|
|
121
|
+
contexts,
|
|
122
|
+
tools,
|
|
123
|
+
...(toolSet === undefined ? {} : { toolSet }),
|
|
124
|
+
systemText,
|
|
125
|
+
stats,
|
|
126
|
+
applied,
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* 叠加预览消息列表。
|
|
132
|
+
*
|
|
133
|
+
* 预览消息是本插件自己的形状(`sourceKind / text / parts / length`),所以这里
|
|
134
|
+
* 不处理 DSH 的 `content` 数组;真实请求的对应逻辑在 `templates/message-overlay.js`。
|
|
135
|
+
* @param {Array<object>} messages - `previewMessages()` 的结果。
|
|
136
|
+
* @param {Array<object>} skipped - 已跳过的消息原因。
|
|
137
|
+
* @param {object} recipe - 提示词配方。
|
|
138
|
+
* @param {object} assembly - 叠加后的装配结果(用于判断 required context)。
|
|
139
|
+
* @returns {{messages: Array<object>, skipped: Array<object>, applied: object}} 叠加结果。
|
|
140
|
+
*/
|
|
141
|
+
export function overlayPreviewMessages(messages, skipped, recipe, assembly) {
|
|
142
|
+
const out = []
|
|
143
|
+
const skippedOut = Array.isArray(skipped) ? [...skipped] : []
|
|
144
|
+
const applied = { messages: [], catalogEntries: [], skipped: [] }
|
|
145
|
+
|
|
146
|
+
for (const message of Array.isArray(messages) ? messages : []) {
|
|
147
|
+
const kind = message.sourceKind
|
|
148
|
+
if (kind === 'runtime-context') {
|
|
149
|
+
const hidden = recipe?.messages?.['runtime-context']?.hidden === true
|
|
150
|
+
if (!hidden) {
|
|
151
|
+
out.push(message)
|
|
152
|
+
continue
|
|
153
|
+
}
|
|
154
|
+
// suppressRuntimeContext 只压可选上下文;required 的仍会进入请求。
|
|
155
|
+
const required = new Set((assembly?.contexts ?? []).filter((context) => context.required === true).map((context) => context.name))
|
|
156
|
+
const parts = (message.parts ?? []).filter((part) => required.has(part.name))
|
|
157
|
+
if (parts.length === 0) {
|
|
158
|
+
skippedOut.push({ sourceKind: 'runtime-context', reason: '模板隐藏' })
|
|
159
|
+
applied.skipped.push('runtime-context')
|
|
160
|
+
continue
|
|
161
|
+
}
|
|
162
|
+
const body = parts.map((part) => part.text).filter((text) => typeof text === 'string' && text.length > 0).join('\n\n')
|
|
163
|
+
const text = body.length === 0 ? '' : RUNTIME_CONTEXT_PREFIX + '\n\n' + body
|
|
164
|
+
out.push(overriddenMessage(message, text, { parts }))
|
|
165
|
+
applied.messages.push('runtime-context')
|
|
166
|
+
continue
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
if (kind === 'agent-instructions' || kind === 'time-context') {
|
|
170
|
+
const spec = recipe?.messages?.[kind]
|
|
171
|
+
if (spec?.hidden === true || spec?.text === '') {
|
|
172
|
+
skippedOut.push({ sourceKind: kind, reason: '模板隐藏' })
|
|
173
|
+
applied.skipped.push(kind)
|
|
174
|
+
continue
|
|
175
|
+
}
|
|
176
|
+
if (typeof spec?.text === 'string') {
|
|
177
|
+
out.push(overriddenMessage(message, spec.text))
|
|
178
|
+
applied.messages.push(kind)
|
|
179
|
+
continue
|
|
180
|
+
}
|
|
181
|
+
out.push(message)
|
|
182
|
+
continue
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
if (kind === 'skill-catalog') {
|
|
186
|
+
const spec = recipe?.messages?.['skill-catalog']
|
|
187
|
+
if (spec?.hidden === true) {
|
|
188
|
+
skippedOut.push({ sourceKind: kind, reason: '模板隐藏' })
|
|
189
|
+
applied.skipped.push(kind)
|
|
190
|
+
continue
|
|
191
|
+
}
|
|
192
|
+
const hasEntryOverrides = Array.isArray(spec?.hiddenEntries) || (spec?.descriptions !== null && typeof spec?.descriptions === 'object')
|
|
193
|
+
if (!hasEntryOverrides) {
|
|
194
|
+
out.push(message)
|
|
195
|
+
continue
|
|
196
|
+
}
|
|
197
|
+
const entries = applyCatalogOverrides(message.parts ?? [], spec)
|
|
198
|
+
const text = renderCatalogMessage(entries, message.incomplete === true)
|
|
199
|
+
out.push(overriddenMessage(message, text, { parts: entries }))
|
|
200
|
+
applied.messages.push(kind)
|
|
201
|
+
applied.catalogEntries = entries.map((entry) => entry.name)
|
|
202
|
+
continue
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
out.push(message)
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
return { messages: out, skipped: skippedOut, applied }
|
|
209
|
+
}
|