@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,244 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 预测一次「首条消息发出前」的装配。
|
|
3
|
+
*
|
|
4
|
+
* 与 loop 的区别只有一个:loop 用 `agent.ctx` 的 scope,这里用
|
|
5
|
+
* `agentPresets.acquireScope(presetId)` 拿到的 preset scope。两者都是
|
|
6
|
+
* `assemble({ scope })` 的合法输入——scope 决定「沿哪条链收集贡献」,
|
|
7
|
+
* 而装配本身(sections/contexts/tools 的求值)走的是同一段代码。
|
|
8
|
+
*
|
|
9
|
+
* 因此这是**预测**而不是记录:它不产生任何会话事件,也不改变注册表。
|
|
10
|
+
* @module dsh-plugin-console/host/assembly-preview
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { readScopePromptOverrides } from './prompt.js'
|
|
14
|
+
import { classifyPreviewTools } from './tool-set.js'
|
|
15
|
+
import { readToolSchemas } from './tools.js'
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* 找出 scope 链上声明了 `complete` 的那一段。
|
|
19
|
+
*
|
|
20
|
+
* complete 段一旦生效,`assemble` 就把 sections 换成「只有它」——其余段全部
|
|
21
|
+
* 被丢掉,追加段落也不会出现。这不是 bug,是 system-prompt 的既定语义,
|
|
22
|
+
* 但它对定制功能有决定性影响:minimal 模式就是这样,它的整份提示词就是
|
|
23
|
+
* 一个 complete 段。所以这里要把它是谁标出来,前端才能如实提示,
|
|
24
|
+
* 物化时也才知道那个段必须带着 complete 注册回去。
|
|
25
|
+
* @param {object} systemPrompt - systemPrompt 服务。
|
|
26
|
+
* @param {symbol} scopeKey - preset scope 标识。
|
|
27
|
+
* @returns {string | undefined} 独占段名字。
|
|
28
|
+
*/
|
|
29
|
+
function completeSectionName(systemPrompt, scopeKey) {
|
|
30
|
+
const layers = systemPrompt.layers
|
|
31
|
+
if (layers === undefined || typeof layers.merge !== 'function') return undefined
|
|
32
|
+
try {
|
|
33
|
+
// 必须作为方法调用:merge 内部用 this.chainLayers。
|
|
34
|
+
for (const [name, section] of layers.merge(scopeKey, (layer) => layer.sections)) {
|
|
35
|
+
if (section?.complete === true) return name
|
|
36
|
+
}
|
|
37
|
+
} catch {
|
|
38
|
+
// 读不到就当没有 complete 段
|
|
39
|
+
}
|
|
40
|
+
return undefined
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* 从 scope 链上读取段/上下文的 order。
|
|
45
|
+
*
|
|
46
|
+
* `assemble()` 按 order 排好序,但不把 order 带回来;注册表 merge 能给出每个名字
|
|
47
|
+
* 的 order。这里只读取、不改动装配结果,读不到就留空。
|
|
48
|
+
* @param {object} systemPrompt - systemPrompt 服务。
|
|
49
|
+
* @param {symbol | undefined} scopeKey - preset scope 标识。
|
|
50
|
+
* @param {'sections' | 'contexts'} table - 要读的注册表。
|
|
51
|
+
* @returns {Map<string, number>} 名字 → order。
|
|
52
|
+
*/
|
|
53
|
+
function orderOf(systemPrompt, scopeKey, table) {
|
|
54
|
+
const result = new Map()
|
|
55
|
+
const layers = systemPrompt.layers
|
|
56
|
+
if (layers === undefined || typeof layers.merge !== 'function') return result
|
|
57
|
+
try {
|
|
58
|
+
for (const [name, item] of layers.merge(scopeKey, (layer) => layer[table])) {
|
|
59
|
+
if (typeof item?.order === 'number') result.set(name, item.order)
|
|
60
|
+
}
|
|
61
|
+
} catch {
|
|
62
|
+
// 注册表读不到时不影响装配预览
|
|
63
|
+
}
|
|
64
|
+
return result
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* 取得「某个模式」的临时 scope。
|
|
69
|
+
*
|
|
70
|
+
* 单独抽出来是因为一次首请求预演要拿同一份 scope 做两件事:装配(sections / contexts /
|
|
71
|
+
* tools)和 skill 目录快照。两边各自 `acquireScope()` 会挂两个并行 scope,注册表里同名
|
|
72
|
+
* 段落的覆盖顺序也会变得不可预期,所以由编排层取一次、传下去、最后统一释放。
|
|
73
|
+
* @param {object} ctx - host 插件上下文。
|
|
74
|
+
* @param {string} [presetId] - 目标模式;省略则用默认模式。
|
|
75
|
+
* @returns {Promise<object>} `{ presetId, scopeKey, warnings, dispose() }`。
|
|
76
|
+
*/
|
|
77
|
+
export async function acquirePresetScope(ctx, presetId) {
|
|
78
|
+
const presets = ctx.get('agentPresetRegistry') ?? ctx.get('agentPresets')
|
|
79
|
+
const warnings = []
|
|
80
|
+
let resolved = presetId
|
|
81
|
+
let acquired
|
|
82
|
+
let scopeKey
|
|
83
|
+
|
|
84
|
+
if (presets !== undefined && typeof presets.acquireScope === 'function') {
|
|
85
|
+
if (typeof resolved !== 'string') {
|
|
86
|
+
// 默认模式:从 roster 里找 isDefault
|
|
87
|
+
try {
|
|
88
|
+
const inventory = await presets.compositionInventory()
|
|
89
|
+
resolved = inventory.find((mode) => mode.isDefault)?.id ?? inventory[0]?.id
|
|
90
|
+
} catch {
|
|
91
|
+
resolved = undefined
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
if (typeof resolved === 'string') {
|
|
95
|
+
acquired = await presets.acquireScope(resolved)
|
|
96
|
+
scopeKey = acquired.key
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
if (scopeKey === undefined) {
|
|
100
|
+
warnings.push('没有可用的模式预设:本次预演只覆盖全局层,预设贡献缺失')
|
|
101
|
+
}
|
|
102
|
+
return {
|
|
103
|
+
presetId: resolved,
|
|
104
|
+
scopeKey,
|
|
105
|
+
warnings,
|
|
106
|
+
async dispose() {
|
|
107
|
+
if (acquired !== undefined) await acquired[Symbol.asyncDispose]()
|
|
108
|
+
},
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* @param {object} ctx - host 插件上下文。
|
|
114
|
+
* @param {object} options - 选择项。
|
|
115
|
+
* @param {string} [options.presetId] - 目标模式;省略则用默认模式。
|
|
116
|
+
* @param {object} [options.scope] - 已经取得的 scope(`acquirePresetScope()` 的返回值)。
|
|
117
|
+
* 传入时不再自行取得,也不在结束时释放;由调用方统一管理。
|
|
118
|
+
* @param {object} [options.variableOverrides] - 补上装配结果里缺失的插值变量。
|
|
119
|
+
* 只补缺失项:`assemble()` 自己给出的值优先。
|
|
120
|
+
* @returns {Promise<object>} 装配结果与归因。
|
|
121
|
+
*/
|
|
122
|
+
export async function previewAssembly(ctx, options = {}) {
|
|
123
|
+
const systemPrompt = ctx.get('systemPrompt')
|
|
124
|
+
|
|
125
|
+
if (systemPrompt === undefined || typeof systemPrompt.assemble !== 'function') {
|
|
126
|
+
throw new Error('systemPrompt service unavailable, cannot predict assembly')
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const givenScope = options.scope
|
|
130
|
+
const scope = givenScope ?? await acquirePresetScope(ctx, options.presetId)
|
|
131
|
+
const { presetId, scopeKey } = scope
|
|
132
|
+
const warnings = [...scope.warnings]
|
|
133
|
+
|
|
134
|
+
try {
|
|
135
|
+
const assembly = await systemPrompt.assemble(scopeKey === undefined ? {} : { scope: scopeKey })
|
|
136
|
+
const completeName = completeSectionName(systemPrompt, scopeKey)
|
|
137
|
+
const sectionOrder = orderOf(systemPrompt, scopeKey, 'sections')
|
|
138
|
+
const contextOrder = orderOf(systemPrompt, scopeKey, 'contexts')
|
|
139
|
+
|
|
140
|
+
const sections = (assembly.sections ?? []).map((section, index) => ({
|
|
141
|
+
index,
|
|
142
|
+
name: section.name,
|
|
143
|
+
/** DSH 用 order 决定段落在最终提示词里的先后;它才是排序依据。 */
|
|
144
|
+
order: sectionOrder.get(section.name),
|
|
145
|
+
text: typeof section.text === 'string' ? section.text : '',
|
|
146
|
+
length: typeof section.text === 'string' ? section.text.length : 0,
|
|
147
|
+
interpolate: section.interpolate === false ? false : undefined,
|
|
148
|
+
complete: section.name === completeName ? true : undefined,
|
|
149
|
+
}))
|
|
150
|
+
const contexts = (assembly.contexts ?? []).map((context) => ({
|
|
151
|
+
name: context.name,
|
|
152
|
+
order: contextOrder.get(context.name),
|
|
153
|
+
text: typeof context.text === 'string' ? context.text : '',
|
|
154
|
+
length: typeof context.text === 'string' ? context.text.length : 0,
|
|
155
|
+
required: context.required === true ? true : undefined,
|
|
156
|
+
// 预演要按同一套规则插值:`false` 的上下文是字面量,不能被 `{{}}` 改写。
|
|
157
|
+
interpolate: context.interpolate === false ? false : undefined,
|
|
158
|
+
}))
|
|
159
|
+
const toolSchemas = (assembly.tools ?? []).map((tool) => ({
|
|
160
|
+
name: tool.name,
|
|
161
|
+
description: tool.description,
|
|
162
|
+
parameters: tool.parameters,
|
|
163
|
+
deferLoading: tool.deferLoading === true ? true : undefined,
|
|
164
|
+
}))
|
|
165
|
+
// 工具按名字来自哪一层分组:全局视图里就有的名字归全局层,只有 preset scope
|
|
166
|
+
// 才有的归预设层。只看名字、不比定义——装配结果本来就取自 preset scope。
|
|
167
|
+
const toolSet = classifyPreviewTools({
|
|
168
|
+
tools: toolSchemas,
|
|
169
|
+
global: readToolSchemas(ctx),
|
|
170
|
+
preset: scopeKey === undefined ? [] : readToolSchemas(ctx, scopeKey),
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
// 渲染:与 renderPrompt 一致用 `\n\n` 连接,但我们**不让缺变量中断整次预览**。
|
|
174
|
+
//
|
|
175
|
+
// `{{model}}` / `{{provider}}` 由 agent-loop 的 variable provider 提供,取值来自
|
|
176
|
+
// `context.agent.options`。没有 Agent 时它们是 undefined,`interpolate()` 会抛错。
|
|
177
|
+
// 预览的价值在于「看构造」,不该因为一个未选模型就整页失败,所以这里自行插值:
|
|
178
|
+
// 能解析的替换,解析不了的保留字面量并记进 unresolvedVariables,由前端标出来。
|
|
179
|
+
// 变量优先用 `assemble()` 给出的值,调用方只补它缺失的那些(通常是默认模型)。
|
|
180
|
+
const overrides = options.variableOverrides ?? {}
|
|
181
|
+
const variables = { ...overrides, ...(assembly.variables ?? {}) }
|
|
182
|
+
const unresolved = new Set()
|
|
183
|
+
const renderSection = (section) => {
|
|
184
|
+
if (section.interpolate === false) return section.text
|
|
185
|
+
return section.text.replace(/\{\{(\w+)\}\}/g, (whole, key) => {
|
|
186
|
+
const value = variables[key]
|
|
187
|
+
if (typeof value === 'string') return value
|
|
188
|
+
unresolved.add(key)
|
|
189
|
+
return whole
|
|
190
|
+
})
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const renderedSections = sections.map((section) => ({
|
|
194
|
+
...section,
|
|
195
|
+
rendered: renderSection(section),
|
|
196
|
+
unresolved: section.interpolate === false
|
|
197
|
+
? []
|
|
198
|
+
: [...section.text.matchAll(/\{\{(\w+)\}\}/g)]
|
|
199
|
+
.map((match) => match[1])
|
|
200
|
+
.filter((key) => typeof variables[key] !== 'string'),
|
|
201
|
+
}))
|
|
202
|
+
|
|
203
|
+
const systemText = renderedSections
|
|
204
|
+
.map((section) => section.rendered)
|
|
205
|
+
.filter((text) => text.length > 0)
|
|
206
|
+
.join('\n\n')
|
|
207
|
+
|
|
208
|
+
// 逐层枚举 scope 链:哪些全局段被同名替换、哪些段只在这个 scope 出现。
|
|
209
|
+
// 这是名字级事实;owner 由前端用快照里的源码扫描索引回填。
|
|
210
|
+
const scopeOverrides = readScopePromptOverrides(ctx, scopeKey) ?? null
|
|
211
|
+
|
|
212
|
+
return {
|
|
213
|
+
presetId: presetId ?? null,
|
|
214
|
+
scoped: scopeKey !== undefined,
|
|
215
|
+
warnings,
|
|
216
|
+
sections: renderedSections,
|
|
217
|
+
contexts,
|
|
218
|
+
tools: toolSchemas,
|
|
219
|
+
variables,
|
|
220
|
+
/** 插值变量里由调用方补上的键;前端据此说明「这几项不是装配给的」。 */
|
|
221
|
+
predictedVariables: Object.keys(overrides).filter((key) => typeof (assembly.variables ?? {})[key] !== 'string'),
|
|
222
|
+
/** 工具按名字来源分层(全局层 / 预设层 / 未分类);口径见 classifyPreviewTools。 */
|
|
223
|
+
toolSet,
|
|
224
|
+
// 哪个段是 complete。有值时说明这个模式的提示词是「独占」的:
|
|
225
|
+
// assemble 只保留了它一段,追加段落之类不会出现在模型看到的内容里。
|
|
226
|
+
completeSection: completeName ?? null,
|
|
227
|
+
/** scope 层对全局 sections / contexts 的同名替换与追加;无 scope 时为 null。 */
|
|
228
|
+
scopeOverrides,
|
|
229
|
+
stats: {
|
|
230
|
+
sections: renderedSections.length,
|
|
231
|
+
sectionChars: sections.reduce((sum, section) => sum + section.length, 0),
|
|
232
|
+
contexts: contexts.length,
|
|
233
|
+
contextChars: contexts.reduce((sum, context) => sum + context.length, 0),
|
|
234
|
+
tools: toolSchemas.length,
|
|
235
|
+
toolSchemaChars: JSON.stringify(toolSchemas).length,
|
|
236
|
+
},
|
|
237
|
+
systemText,
|
|
238
|
+
unresolvedVariables: [...unresolved],
|
|
239
|
+
}
|
|
240
|
+
} finally {
|
|
241
|
+
// 共享 scope 的生命周期归调用方(首请求预演要跨采集器复用同一份)。
|
|
242
|
+
if (givenScope === undefined) await scope.dispose()
|
|
243
|
+
}
|
|
244
|
+
}
|
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 名字级归属:把注册表里的名字连回「谁注册的」。
|
|
3
|
+
*
|
|
4
|
+
* 运行时只能回答「有哪些工具/区块」,回答不了「谁注册的」——注册表不记 owner。
|
|
5
|
+
* 答案在包源码里(见 `source-scan.js`),这里的职责是把两边按名字连接起来:
|
|
6
|
+
* 完全同名 → 模板前缀 → MCP 工具名规则,三种匹配逐级降级。
|
|
7
|
+
* @module dsh-plugin-console/host/attribution
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { categorize } from './categorize.js'
|
|
11
|
+
import { scanPackageRegistrations } from './source-scan.js'
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 建立「名字 → 注册它的行」索引。
|
|
15
|
+
*
|
|
16
|
+
* 一行可能有多个名字,一个名字也可能来自多行(例如 PTC 与 native 都注册 grep)。
|
|
17
|
+
* @param {Array<object>} entries - 已采集条目。
|
|
18
|
+
* @returns {{entryByRow: Map<string, object>, toolOwners: Map<string, string[]>, sectionOwners: Map<string, string[]>, mcpServerRow: Map<string, string>}} 四张索引。
|
|
19
|
+
*/
|
|
20
|
+
export function buildOwnerIndex(entries) {
|
|
21
|
+
const entryByRow = new Map(entries.map((entry) => [entry.rowId, entry]))
|
|
22
|
+
|
|
23
|
+
/** 名字 → 注册它的条目(一行可能有多个,例如 PTC 与 native 都注册 grep)。 */
|
|
24
|
+
const toolOwners = new Map()
|
|
25
|
+
const sectionOwners = new Map()
|
|
26
|
+
for (const entry of entries) {
|
|
27
|
+
for (const name of entry.registrations?.tools ?? []) {
|
|
28
|
+
const list = toolOwners.get(name) ?? []
|
|
29
|
+
list.push(entry.rowId)
|
|
30
|
+
toolOwners.set(name, list)
|
|
31
|
+
}
|
|
32
|
+
for (const name of entry.registrations?.sections ?? []) {
|
|
33
|
+
const list = sectionOwners.get(name) ?? []
|
|
34
|
+
list.push(entry.rowId)
|
|
35
|
+
sectionOwners.set(name, list)
|
|
36
|
+
}
|
|
37
|
+
for (const name of entry.registrations?.contexts ?? []) {
|
|
38
|
+
const list = sectionOwners.get(name) ?? []
|
|
39
|
+
list.push(entry.rowId)
|
|
40
|
+
sectionOwners.set(name, list)
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** MCP 工具的名字形如 `mcp__<server>__<tool>`,据此归到对应的 mcp-client 行。 */
|
|
45
|
+
const mcpServerRow = new Map()
|
|
46
|
+
for (const entry of entries) {
|
|
47
|
+
if (!entry.moduleName.includes('mcp-client')) continue
|
|
48
|
+
const serverName = entry.mcpServerName
|
|
49
|
+
if (typeof serverName === 'string') mcpServerRow.set(serverName, entry.rowId)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return { entryByRow, toolOwners, sectionOwners, mcpServerRow }
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* 名字级归属。
|
|
57
|
+
*
|
|
58
|
+
* 先找完全同名的来源;找不到再找模板来源(源码里写的是 `mcp:${server}`,
|
|
59
|
+
* 注册表里落成 `mcp:firecrawl`),按 `${}` 之前的前缀匹配。
|
|
60
|
+
* @param {Map<string, string[]>} owners - 名字 → 行 id。
|
|
61
|
+
* @param {string} name - 注册表里的实际名字。
|
|
62
|
+
* @param {Map<string, object>} entryByRow - 行 id → 条目。
|
|
63
|
+
* @returns {{owners: string[], via: string}} 归属结果。
|
|
64
|
+
*/
|
|
65
|
+
export function matchOwners(owners, name, entryByRow) {
|
|
66
|
+
const exact = owners.get(name)
|
|
67
|
+
if (exact !== undefined) return { owners: exact, via: 'source' }
|
|
68
|
+
for (const [pattern, rowIds] of owners) {
|
|
69
|
+
const cut = pattern.indexOf('${')
|
|
70
|
+
if (cut <= 0 || !name.startsWith(pattern.slice(0, cut))) continue
|
|
71
|
+
// 模板里 `${}` 之后的字面尾巴(若有)也要对上,避免 mcp:${a} 吃掉 mcp:xyz
|
|
72
|
+
const tail = pattern.slice(pattern.indexOf('}') + 1)
|
|
73
|
+
if (tail !== '' && !name.endsWith(tail)) continue
|
|
74
|
+
// `mcp:${server}` 这类模板,只归给 serverName 与之相符的那一条
|
|
75
|
+
const narrowed = rowIds.filter((rowId) => {
|
|
76
|
+
const serverName = entryByRow.get(rowId)?.mcpServerName
|
|
77
|
+
if (typeof serverName !== 'string') return rowIds.length === 1
|
|
78
|
+
return name === `${pattern.slice(0, cut)}${serverName}${tail}`
|
|
79
|
+
})
|
|
80
|
+
if (narrowed.length > 0) return { owners: narrowed, via: 'template' }
|
|
81
|
+
return { owners: rowIds, via: 'template' }
|
|
82
|
+
}
|
|
83
|
+
return { owners: [], via: 'unknown' }
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* 把源码扫描得到的注册名展开成注册表里的实际名字。
|
|
88
|
+
*
|
|
89
|
+
* 源码里写的是模板(`mcp:${server}`),注册表里落成具体值(`mcp:firecrawl`)。
|
|
90
|
+
* 直接拿模板当键查注册表永远查不到,每个 MCP 客户端都会显示成同一条「未归属」
|
|
91
|
+
* 的模板名,点开也无从定位。模板规则与 {@link matchOwners} 同向:只按 `${`
|
|
92
|
+
* 前的前缀与 `}` 后的尾巴匹配;条目带 serverName 时收窄到这一个实际名。
|
|
93
|
+
* @param {string} name - 源码里写的注册名。
|
|
94
|
+
* @param {object} entry - 注册它的条目(用于取 mcpServerName)。
|
|
95
|
+
* @param {Map<string, object>} registryNames - 注册表:实际名字 → 行。
|
|
96
|
+
* @returns {string[]} 注册表里的实际名字;匹配不到时保留可读的具体名或原模板名。
|
|
97
|
+
*/
|
|
98
|
+
export function expandRegistrationName(name, entry, registryNames) {
|
|
99
|
+
if (registryNames.has(name)) return [name]
|
|
100
|
+
const cut = name.indexOf('${')
|
|
101
|
+
if (cut <= 0) return [name]
|
|
102
|
+
const close = name.indexOf('}', cut)
|
|
103
|
+
if (close < 0) return [name]
|
|
104
|
+
const prefix = name.slice(0, cut)
|
|
105
|
+
const tail = name.slice(close + 1)
|
|
106
|
+
const serverName = typeof entry?.mcpServerName === 'string' ? entry.mcpServerName : undefined
|
|
107
|
+
const matches = [...registryNames.keys()].filter((actual) => (
|
|
108
|
+
actual.length >= prefix.length + tail.length
|
|
109
|
+
&& actual.startsWith(prefix)
|
|
110
|
+
&& actual.endsWith(tail)
|
|
111
|
+
&& (serverName === undefined || actual === prefix + serverName + tail)
|
|
112
|
+
))
|
|
113
|
+
if (matches.length > 0) return matches
|
|
114
|
+
// 注册表里没有对应项(插件没注册成功)时,至少把 `${server}` 填成具体服务名,
|
|
115
|
+
// 界面才能指出是哪一条;否则每个 MCP 客户端都显示同一个 `mcp:${server}`。
|
|
116
|
+
if (serverName !== undefined && name.slice(cut + 2, close) === 'server') return [prefix + serverName + tail]
|
|
117
|
+
return [name]
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* 归因一个工具名:先按源码名字,再按 MCP 工具名规则。
|
|
122
|
+
* @param {string} toolName - 工具名。
|
|
123
|
+
* @param {{toolOwners: Map<string, string[]>, mcpServerRow: Map<string, string>, entryByRow: Map<string, object>}} index - 名字索引。
|
|
124
|
+
* @returns {{owners: string[], via: string}} 归属结果。
|
|
125
|
+
*/
|
|
126
|
+
export function attributeTool(toolName, index) {
|
|
127
|
+
const direct = matchOwners(index.toolOwners, toolName, index.entryByRow)
|
|
128
|
+
if (direct.owners.length > 0) return direct
|
|
129
|
+
const mcp = toolName.match(/^mcp__(.+?)__/)
|
|
130
|
+
if (mcp !== null) {
|
|
131
|
+
const rowId = index.mcpServerRow.get(mcp[1])
|
|
132
|
+
if (rowId !== undefined) return { owners: [rowId], via: 'mcp-server' }
|
|
133
|
+
return { owners: [], via: 'mcp-unmatched' }
|
|
134
|
+
}
|
|
135
|
+
return { owners: [], via: 'unknown' }
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* 为「只出现在模式声明里的模块」补扫源码,并登记它们注册的名字。
|
|
140
|
+
*
|
|
141
|
+
* 这些插件确实装在这台机器上(present / schedule_create / ask_user_question
|
|
142
|
+
* 都由它们注册),只是没有自己的 Loader 行——模式在会话里挂载它们。包根目录
|
|
143
|
+
* 可以从任一条目的 packageDir 推出(条目都在同一个 node_modules 下)。
|
|
144
|
+
* @param {Array<object>} entries - 全局条目(会被就地补 registry)。
|
|
145
|
+
* @param {Array<object>} modes - 模式列表(会被就地补 registry)。
|
|
146
|
+
* @param {Map<string, string[]>} toolOwners - 工具归属索引(会被就地补)。
|
|
147
|
+
* @param {Map<string, string[]>} sectionOwners - 段落归属索引(会被就地补)。
|
|
148
|
+
* @returns {Promise<string | undefined>} 推测出的包根目录。
|
|
149
|
+
*/
|
|
150
|
+
export async function scanModeOnlyModules(entries, modes, toolOwners, sectionOwners) {
|
|
151
|
+
const sampleDir = entries.find((entry) => typeof entry.packageDir === 'string')?.packageDir
|
|
152
|
+
const packageRoot = typeof sampleDir === 'string'
|
|
153
|
+
? sampleDir.replace(/[\\/][^\\/]+$/, '')
|
|
154
|
+
: undefined
|
|
155
|
+
if (packageRoot === undefined) return undefined
|
|
156
|
+
|
|
157
|
+
const scanned = new Set(entries.map((entry) => entry.moduleName))
|
|
158
|
+
for (const mode of modes) {
|
|
159
|
+
for (const row of mode.rows ?? []) {
|
|
160
|
+
const moduleName = row.moduleName
|
|
161
|
+
if (typeof moduleName !== 'string' || scanned.has(moduleName)) continue
|
|
162
|
+
scanned.add(moduleName)
|
|
163
|
+
const bare = moduleName.replace(/^@[^/]+\//, '')
|
|
164
|
+
const registrations = await scanPackageRegistrations(`${packageRoot}/${bare}`)
|
|
165
|
+
row.registry = registrations.tools.length + registrations.sections.length + registrations.contexts.length > 0
|
|
166
|
+
? registrations
|
|
167
|
+
: undefined
|
|
168
|
+
for (const name of registrations.tools) {
|
|
169
|
+
const list = toolOwners.get(name) ?? []
|
|
170
|
+
if (!list.includes(moduleName)) list.push(moduleName)
|
|
171
|
+
toolOwners.set(name, list)
|
|
172
|
+
}
|
|
173
|
+
for (const name of [...registrations.sections, ...registrations.contexts]) {
|
|
174
|
+
const list = sectionOwners.get(name) ?? []
|
|
175
|
+
if (!list.includes(moduleName)) list.push(moduleName)
|
|
176
|
+
sectionOwners.set(name, list)
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
return packageRoot
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* 把只出现在模式声明里的模块补成「合成条目」。
|
|
185
|
+
*
|
|
186
|
+
* 它们确实装在这台机器上(present / schedule_create / ask_user_question 都由它们注册),
|
|
187
|
+
* 只是没有自己的 Loader 行——模式在会话里挂载它们。不补进来的话,
|
|
188
|
+
* 插件视图里搜不到它们,工具页上的归属标签也点不到落点。
|
|
189
|
+
* @param {Array<object>} entries - 全局条目(用于判断哪些模块已经有行)。
|
|
190
|
+
* @param {Array<object>} modes - 模式列表(行上的 registry 来自 {@link scanModeOnlyModules})。
|
|
191
|
+
* @returns {Array<object>} 合成条目列表。
|
|
192
|
+
*/
|
|
193
|
+
export function buildModeOnlyEntries(entries, modes) {
|
|
194
|
+
const known = new Set(entries.map((entry) => entry.moduleName))
|
|
195
|
+
const covered = new Set()
|
|
196
|
+
for (const mode of modes) {
|
|
197
|
+
for (const row of mode.rows ?? []) {
|
|
198
|
+
const moduleName = row.moduleName
|
|
199
|
+
if (typeof moduleName !== 'string' || known.has(moduleName) || row.registry === undefined) continue
|
|
200
|
+
covered.add(moduleName)
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const modeOnlyEntries = []
|
|
205
|
+
for (const moduleName of covered) {
|
|
206
|
+
const modeIds = modes
|
|
207
|
+
.filter((mode) => (mode.rows ?? []).some((row) => row.moduleName === moduleName))
|
|
208
|
+
.map((mode) => mode.id)
|
|
209
|
+
const registry = modes
|
|
210
|
+
.flatMap((mode) => mode.rows ?? [])
|
|
211
|
+
.find((row) => row.moduleName === moduleName)?.registry
|
|
212
|
+
modeOnlyEntries.push({
|
|
213
|
+
entryId: `mode:${moduleName}`,
|
|
214
|
+
rowId: moduleName,
|
|
215
|
+
depth: 1,
|
|
216
|
+
moduleName,
|
|
217
|
+
packageName: moduleName,
|
|
218
|
+
version: undefined,
|
|
219
|
+
title: undefined,
|
|
220
|
+
description: undefined,
|
|
221
|
+
layer: 'mode',
|
|
222
|
+
layerVersion: undefined,
|
|
223
|
+
overriddenBy: [],
|
|
224
|
+
userInserted: false,
|
|
225
|
+
face: 'host',
|
|
226
|
+
enabled: true,
|
|
227
|
+
inheritedDisabled: false,
|
|
228
|
+
phase: null,
|
|
229
|
+
requires: [],
|
|
230
|
+
provides: [],
|
|
231
|
+
clientInject: [],
|
|
232
|
+
clientExternal: [],
|
|
233
|
+
configKeys: [],
|
|
234
|
+
effects: [],
|
|
235
|
+
registrations: registry,
|
|
236
|
+
category: categorize(moduleName, registry),
|
|
237
|
+
modeOnly: true,
|
|
238
|
+
modeIds,
|
|
239
|
+
})
|
|
240
|
+
}
|
|
241
|
+
return modeOnlyEntries
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* 计算「模式层」工具集:每个模式在 agent scope 上挂的工具。
|
|
246
|
+
*
|
|
247
|
+
* 这一层不能靠「全局视图里没有」来判断,那样会把整层误标成「会话私有」。
|
|
248
|
+
* 这里用实测:从同模式的一个活会话取 `tools − global`,再与其它会话两两比对,
|
|
249
|
+
* 只有真的对不上的部分才算会话差异。
|
|
250
|
+
* @param {Array<object>} modes - 模式列表(`mode.tools` 会被就地写入)。
|
|
251
|
+
* @param {Array<object>} entries - 全局条目(含合成条目)。
|
|
252
|
+
* @param {Array<object>} liveAgents - 活会话的工具快照。
|
|
253
|
+
* @param {string[]} globalTools - 全局可见工具名。
|
|
254
|
+
* @returns {Map<string, object>} presetId → 模式层。
|
|
255
|
+
*/
|
|
256
|
+
export function computePresetLayer(modes, entries, liveAgents, globalTools) {
|
|
257
|
+
const presetLayer = new Map()
|
|
258
|
+
for (const mode of modes) {
|
|
259
|
+
const peers = liveAgents.filter((agent) => agent.preset === mode.id)
|
|
260
|
+
const fromLive = peers.length > 0
|
|
261
|
+
? peers[0].tools.filter((name) => !globalTools.includes(name))
|
|
262
|
+
: undefined
|
|
263
|
+
const fromScan = new Set()
|
|
264
|
+
for (const row of mode.rows ?? []) {
|
|
265
|
+
const entry = entries.find((candidate) => candidate.rowId === row.entryId)
|
|
266
|
+
?? entries.find((candidate) => candidate.moduleName === row.moduleName)
|
|
267
|
+
for (const name of (row.registry ?? entry?.registrations)?.tools ?? []) fromScan.add(name)
|
|
268
|
+
}
|
|
269
|
+
presetLayer.set(mode.id, {
|
|
270
|
+
/** 实测优先;没有活会话时退回源码扫描的结果。 */
|
|
271
|
+
tools: fromLive ?? [...fromScan],
|
|
272
|
+
measured: fromLive !== undefined,
|
|
273
|
+
/** 声明行里注册了、但实测没出现的:被平台条件跳过或被 restrict 挡掉。 */
|
|
274
|
+
absent: fromLive === undefined ? [] : [...fromScan].filter((name) => !fromLive.includes(name)),
|
|
275
|
+
/** 实测出现了、但声明行的注册记录里找不到的:动态注册或扫描盲区。 */
|
|
276
|
+
unlisted: fromLive === undefined ? [] : fromLive.filter((name) => !fromScan.has(name)),
|
|
277
|
+
sessions: peers.map((agent) => agent.sessionId),
|
|
278
|
+
})
|
|
279
|
+
mode.tools = presetLayer.get(mode.id).tools
|
|
280
|
+
}
|
|
281
|
+
return presetLayer
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* 把每个活会话的工具集与「全局 + 模式」对比,得出会话级差异。
|
|
286
|
+
* @param {Array<object>} liveAgents - 活会话的工具快照(会被就地补 extra/missing/hidden)。
|
|
287
|
+
* @param {Map<string, object>} presetLayer - 模式层。
|
|
288
|
+
* @param {string[]} globalTools - 全局可见工具名。
|
|
289
|
+
* @returns {void}
|
|
290
|
+
*/
|
|
291
|
+
export function annotateSessionDiffs(liveAgents, presetLayer, globalTools) {
|
|
292
|
+
for (const agent of liveAgents) {
|
|
293
|
+
const layer = presetLayer.get(agent.preset)
|
|
294
|
+
// 只有「同模式会话之间真的不一样」的那部分才算会话差异。
|
|
295
|
+
agent.extra = layer === undefined ? [] : agent.tools.filter((name) => !globalTools.includes(name) && !layer.tools.includes(name))
|
|
296
|
+
agent.missing = layer === undefined ? [] : layer.tools.filter((name) => !agent.tools.includes(name))
|
|
297
|
+
// 「被 restrict 过滤」必须用 knownNames − visible:同名替换不会让名字消失,
|
|
298
|
+
// 「全局可见 − 会话可见」会把这种替换误报成遮蔽。
|
|
299
|
+
const known = Array.isArray(agent.knownNames) ? agent.knownNames : globalTools
|
|
300
|
+
agent.hidden = known.filter((name) => !agent.tools.includes(name))
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* 同模式会话之间是否真的存在差异——这是回答「是不是每个会话一套」的直接证据。
|
|
306
|
+
* @param {Array<object>} liveAgents - 活会话的工具快照。
|
|
307
|
+
* @returns {Array<{presetId: string, sessions: number, divergent: string[]}>} 差异摘要。
|
|
308
|
+
*/
|
|
309
|
+
export function computePresetDifferences(liveAgents) {
|
|
310
|
+
const presetIds = new Set(liveAgents.map((agent) => agent.preset))
|
|
311
|
+
const presetDifferences = []
|
|
312
|
+
for (const presetId of presetIds) {
|
|
313
|
+
const peers = liveAgents.filter((agent) => agent.preset === presetId)
|
|
314
|
+
if (peers.length < 2) continue
|
|
315
|
+
const first = new Set(peers[0].tools)
|
|
316
|
+
const divergent = peers.filter((agent) => {
|
|
317
|
+
const set = new Set(agent.tools)
|
|
318
|
+
return set.size !== first.size || [...set].some((name) => !first.has(name))
|
|
319
|
+
})
|
|
320
|
+
presetDifferences.push({ presetId, sessions: peers.length, divergent: divergent.map((agent) => agent.sessionId) })
|
|
321
|
+
}
|
|
322
|
+
return presetDifferences
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* MCP 客户端的工具是运行时从 server 拉取后动态注册的,源码里没有字面量,
|
|
327
|
+
* 静态扫描必然为空。改为按 serverName 前缀从工具集里反推,补回 registrations.tools,
|
|
328
|
+
* 让插件详情页也能列出这些工具。
|
|
329
|
+
* @param {Array<object>} entries - 条目(会被就地补 registrations.tools)。
|
|
330
|
+
* @param {string[]} allToolNames - 全局与会话可见集的并集。
|
|
331
|
+
* @returns {void}
|
|
332
|
+
*/
|
|
333
|
+
export function backfillMcpTools(entries, allToolNames) {
|
|
334
|
+
for (const entry of entries) {
|
|
335
|
+
const serverName = entry.mcpServerName
|
|
336
|
+
if (typeof serverName !== 'string') continue
|
|
337
|
+
const owned = allToolNames.filter((name) => name.startsWith(`mcp__${serverName}__`))
|
|
338
|
+
if (owned.length === 0) continue
|
|
339
|
+
entry.registrations = { ...entry.registrations, tools: owned }
|
|
340
|
+
}
|
|
341
|
+
}
|