@cheemsup/dsh-plugin-console 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +85 -2
- package/cordis.patch.yml +5 -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 +242 -0
- package/lib/client.dialog.js +77 -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.influence.js +361 -0
- package/lib/client.js +216 -0
- package/lib/client.page.js +584 -0
- package/lib/client.plugin-filters.js +62 -0
- package/lib/client.plugins.js +504 -0
- package/lib/client.preview-messages.js +495 -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 +447 -0
- package/lib/client.schema.js +327 -0
- package/lib/client.search.js +408 -0
- package/lib/client.session-detail.js +503 -0
- package/lib/client.sessions.js +495 -0
- package/lib/client.shared.js +559 -0
- package/lib/client.store.js +452 -0
- package/lib/client.styles.js +524 -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 +307 -0
- package/lib/host/catalog.js +133 -0
- package/lib/host/categorize.js +40 -0
- package/lib/host/constants.js +66 -0
- package/lib/host/control.js +159 -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 +132 -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-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/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 +475 -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 +103 -0
- package/package.json +111 -4
package/lib/host/fs.js
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 只读文件访问。
|
|
3
|
+
*
|
|
4
|
+
* 快照里的每一次文件读取都是**可选的**:读不到就降级,不让整份快照失败。
|
|
5
|
+
* 因此这里的函数全部返回 `null` 而不是抛错。文件读取只在快照时发生,
|
|
6
|
+
* 不参与模块加载期。
|
|
7
|
+
* @module dsh-plugin-console/host/fs
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* 读一个文本文件;任何失败都返回 null。
|
|
12
|
+
* @param {string} file - 绝对路径。
|
|
13
|
+
* @returns {Promise<string | null>} 文件内容或 null。
|
|
14
|
+
*/
|
|
15
|
+
export async function readTextFile(file) {
|
|
16
|
+
try {
|
|
17
|
+
const { readFile } = await import('node:fs/promises')
|
|
18
|
+
return await readFile(file, 'utf8')
|
|
19
|
+
} catch {
|
|
20
|
+
return null
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* 读一个 JSON 文件;解析失败同样返回 null。
|
|
26
|
+
* @param {string} file - 绝对路径。
|
|
27
|
+
* @returns {Promise<any | null>} 解析结果或 null。
|
|
28
|
+
*/
|
|
29
|
+
export async function readJsonFile(file) {
|
|
30
|
+
const text = await readTextFile(file)
|
|
31
|
+
if (text === null) return null
|
|
32
|
+
try {
|
|
33
|
+
return JSON.parse(text)
|
|
34
|
+
} catch {
|
|
35
|
+
return null
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* 列一个目录的文件名;失败返回空数组。
|
|
41
|
+
* @param {string} dir - 绝对路径。
|
|
42
|
+
* @returns {Promise<string[]>} 文件名列表。
|
|
43
|
+
*/
|
|
44
|
+
export async function listDir(dir) {
|
|
45
|
+
try {
|
|
46
|
+
const { readdir } = await import('node:fs/promises')
|
|
47
|
+
return await readdir(dir)
|
|
48
|
+
} catch {
|
|
49
|
+
return []
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* 列一个目录的条目(带类型);失败返回空数组。
|
|
55
|
+
* @param {string} dir - 绝对路径。
|
|
56
|
+
* @returns {Promise<Array<{name: string, isDirectory: boolean, isFile: boolean}>>} 条目列表。
|
|
57
|
+
*/
|
|
58
|
+
export async function listDirDetailed(dir) {
|
|
59
|
+
try {
|
|
60
|
+
const { readdir } = await import('node:fs/promises')
|
|
61
|
+
const entries = await readdir(dir, { withFileTypes: true })
|
|
62
|
+
return entries.map((entry) => ({
|
|
63
|
+
name: entry.name,
|
|
64
|
+
isDirectory: entry.isDirectory(),
|
|
65
|
+
isFile: entry.isFile(),
|
|
66
|
+
}))
|
|
67
|
+
} catch {
|
|
68
|
+
return []
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* 判断一个路径是不是目录;任何失败都返回 false。
|
|
74
|
+
* @param {string} dir - 绝对路径。
|
|
75
|
+
* @returns {Promise<boolean>} 是否是目录。
|
|
76
|
+
*/
|
|
77
|
+
export async function isDirectory(dir) {
|
|
78
|
+
try {
|
|
79
|
+
const { stat } = await import('node:fs/promises')
|
|
80
|
+
return (await stat(dir)).isDirectory()
|
|
81
|
+
} catch {
|
|
82
|
+
return false
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* 读一个文件的元信息;失败返回 null。
|
|
88
|
+
* @param {string} file - 绝对路径。
|
|
89
|
+
* @returns {Promise<{mtimeMs: number, size: number} | null>} 元信息或 null。
|
|
90
|
+
*/
|
|
91
|
+
export async function statFile(file) {
|
|
92
|
+
try {
|
|
93
|
+
const { stat } = await import('node:fs/promises')
|
|
94
|
+
const value = await stat(file)
|
|
95
|
+
return { mtimeMs: value.mtimeMs, size: value.size }
|
|
96
|
+
} catch {
|
|
97
|
+
return null
|
|
98
|
+
}
|
|
99
|
+
}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 健康摘要与「需要关注」列表。
|
|
3
|
+
*
|
|
4
|
+
* 这一层不读新的数据源,只把快照里已经采到的事实压成两个用户问题:
|
|
5
|
+
* 1. 现在有多少东西不正常?(summary)
|
|
6
|
+
* 2. 具体是哪几项,按什么顺序看?(attention)
|
|
7
|
+
*
|
|
8
|
+
* 判断只依赖运行时事实与已有的归属索引,因此同一个快照反复计算得到同一份结果。
|
|
9
|
+
* 活动、错误率、延迟等需要读 session log 的指标留给后续阶段,不在这里推断。
|
|
10
|
+
* @module dsh-plugin-console/host/health
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/** 严重度排序:数字越小越靠前。 */
|
|
14
|
+
const SEVERITY_RANK = { critical: 0, warning: 1, info: 2 }
|
|
15
|
+
|
|
16
|
+
/** 同类问题的展示顺序:先故障,再缺依赖,最后是归属。 */
|
|
17
|
+
const KIND_RANK = {
|
|
18
|
+
failed: 0,
|
|
19
|
+
'missing-service': 1,
|
|
20
|
+
'unattributed-tools': 2,
|
|
21
|
+
'unattributed-sections': 3,
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** 单次最多返回多少条关注项;summary 仍保留全量计数。 */
|
|
25
|
+
const MAX_ATTENTION = 40
|
|
26
|
+
|
|
27
|
+
/** 结构行(cordis:include 等)不是插件,不参与健康统计。 */
|
|
28
|
+
function isStructural(entry) {
|
|
29
|
+
return typeof entry?.moduleName === 'string' && entry.moduleName.startsWith('cordis:')
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** 插件在界面上的名字:本地化标题优先,其次包名,最后模块名。 */
|
|
33
|
+
function displayName(entry) {
|
|
34
|
+
if (typeof entry?.title === 'string' && entry.title.length > 0) return entry.title
|
|
35
|
+
if (typeof entry?.packageName === 'string' && entry.packageName.length > 0) return entry.packageName
|
|
36
|
+
if (typeof entry?.moduleName === 'string' && entry.moduleName.length > 0) return entry.moduleName
|
|
37
|
+
return entry?.entryId ?? '未知插件'
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* 一个条目缺了哪些必需服务。
|
|
42
|
+
* @param {object} entry - 已采集的 Loader 条目。
|
|
43
|
+
* @param {object} services - 服务名 → `{ providers, consumers }`。
|
|
44
|
+
* @returns {string[]} 没有提供者的服务名。
|
|
45
|
+
*/
|
|
46
|
+
function missingServicesOf(entry, services) {
|
|
47
|
+
return (entry?.requires ?? []).filter((name) => (services?.[name]?.providers ?? []).length === 0)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** 把名字列表压成一句短摘要,避免关注项被长列表撑开。 */
|
|
51
|
+
function sampleNames(names, limit = 5) {
|
|
52
|
+
if (names.length === 0) return '(无)'
|
|
53
|
+
const head = names.slice(0, limit).join('、')
|
|
54
|
+
return names.length > limit ? `${head} 等 ${names.length} 个` : head
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* 计算健康摘要与关注项。
|
|
59
|
+
* @param {object} input - 快照里的相关事实。
|
|
60
|
+
* @param {Array<object>} [input.entries] - 全部条目(含结构行与模式合成条目)。
|
|
61
|
+
* @param {object} [input.services] - 服务名 → 提供者/消费者。
|
|
62
|
+
* @param {object} [input.tools] - 至少包含 `attributed`。
|
|
63
|
+
* @param {object} [input.prompt] - 至少包含 `sections` / `contexts`。
|
|
64
|
+
* @returns {{summary: object, attention: Array<object>}} 摘要与按严重度排序的关注项。
|
|
65
|
+
*/
|
|
66
|
+
export function computeHealth(input = {}) {
|
|
67
|
+
const entries = Array.isArray(input.entries) ? input.entries : []
|
|
68
|
+
const services = input.services ?? {}
|
|
69
|
+
const attributedTools = Array.isArray(input.tools?.attributed) ? input.tools.attributed : []
|
|
70
|
+
const promptSections = Array.isArray(input.prompt?.sections) ? input.prompt.sections : []
|
|
71
|
+
const promptContexts = Array.isArray(input.prompt?.contexts) ? input.prompt.contexts : []
|
|
72
|
+
const plugins = entries.filter((entry) => !isStructural(entry))
|
|
73
|
+
|
|
74
|
+
const attention = []
|
|
75
|
+
let conditional = 0
|
|
76
|
+
let overridden = 0
|
|
77
|
+
let missingServices = 0
|
|
78
|
+
|
|
79
|
+
for (const entry of plugins) {
|
|
80
|
+
if (entry.phase === 'failed') {
|
|
81
|
+
attention.push({
|
|
82
|
+
id: 'failed:' + entry.entryId,
|
|
83
|
+
severity: 'critical',
|
|
84
|
+
kind: 'failed',
|
|
85
|
+
title: displayName(entry) + ' 启动失败',
|
|
86
|
+
detail: typeof entry.diagnostic === 'string' && entry.diagnostic.length > 0
|
|
87
|
+
? entry.diagnostic
|
|
88
|
+
: 'fiber 阶段为 failed;诊断未返回。',
|
|
89
|
+
entryId: entry.entryId,
|
|
90
|
+
rowId: entry.rowId,
|
|
91
|
+
moduleName: entry.moduleName,
|
|
92
|
+
})
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const missing = entry.enabled === true ? missingServicesOf(entry, services) : []
|
|
96
|
+
if (missing.length > 0) {
|
|
97
|
+
missingServices++
|
|
98
|
+
attention.push({
|
|
99
|
+
id: 'missing-service:' + entry.entryId,
|
|
100
|
+
severity: 'warning',
|
|
101
|
+
kind: 'missing-service',
|
|
102
|
+
title: displayName(entry) + ' 缺少服务',
|
|
103
|
+
detail: '没有提供者:' + missing.join('、'),
|
|
104
|
+
entryId: entry.entryId,
|
|
105
|
+
rowId: entry.rowId,
|
|
106
|
+
moduleName: entry.moduleName,
|
|
107
|
+
services: missing,
|
|
108
|
+
})
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (entry.disabledExpr !== undefined && entry.enabled !== true) {
|
|
112
|
+
// 条件禁用是声明语义,不一定是问题;只进 summary,不占用关注项。
|
|
113
|
+
conditional++
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if ((entry.overriddenBy ?? []).length > 0) overridden++
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const unattributedTools = attributedTools
|
|
120
|
+
.filter((tool) => (tool.owners ?? []).length === 0)
|
|
121
|
+
.map((tool) => tool.name)
|
|
122
|
+
const unattributedSections = [...promptSections, ...promptContexts]
|
|
123
|
+
.filter((row) => (row.owners ?? []).length === 0)
|
|
124
|
+
.map((row) => row.name)
|
|
125
|
+
|
|
126
|
+
if (unattributedTools.length > 0) {
|
|
127
|
+
attention.push({
|
|
128
|
+
id: 'unattributed-tools',
|
|
129
|
+
severity: 'info',
|
|
130
|
+
kind: 'unattributed-tools',
|
|
131
|
+
title: `${unattributedTools.length} 个工具没有归属`,
|
|
132
|
+
detail: '无法从源码字面量或 MCP 规则确定注册者:' + sampleNames(unattributedTools),
|
|
133
|
+
names: unattributedTools,
|
|
134
|
+
})
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if (unattributedSections.length > 0) {
|
|
138
|
+
attention.push({
|
|
139
|
+
id: 'unattributed-sections',
|
|
140
|
+
severity: 'info',
|
|
141
|
+
kind: 'unattributed-sections',
|
|
142
|
+
title: `${unattributedSections.length} 个提示词段没有归属`,
|
|
143
|
+
detail: '无法从源码字面量确定注册者:' + sampleNames(unattributedSections),
|
|
144
|
+
names: unattributedSections,
|
|
145
|
+
})
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
attention.sort((left, right) => {
|
|
149
|
+
const severity = (SEVERITY_RANK[left.severity] ?? 9) - (SEVERITY_RANK[right.severity] ?? 9)
|
|
150
|
+
if (severity !== 0) return severity
|
|
151
|
+
const kind = (KIND_RANK[left.kind] ?? 9) - (KIND_RANK[right.kind] ?? 9)
|
|
152
|
+
if (kind !== 0) return kind
|
|
153
|
+
return String(left.title).localeCompare(String(right.title))
|
|
154
|
+
})
|
|
155
|
+
|
|
156
|
+
const countBySeverity = (severity) => attention.filter((item) => item.severity === severity).length
|
|
157
|
+
const summary = {
|
|
158
|
+
entries: plugins.length,
|
|
159
|
+
active: plugins.filter((entry) => entry.phase === 'active').length,
|
|
160
|
+
pending: plugins.filter((entry) => entry.phase === 'pending').length,
|
|
161
|
+
failed: plugins.filter((entry) => entry.phase === 'failed').length,
|
|
162
|
+
disabled: plugins.filter((entry) => entry.enabled !== true).length,
|
|
163
|
+
conditional,
|
|
164
|
+
missingServices,
|
|
165
|
+
overridden,
|
|
166
|
+
modeOnly: plugins.filter((entry) => entry.modeOnly === true).length,
|
|
167
|
+
unattributedTools: unattributedTools.length,
|
|
168
|
+
unattributedSections: unattributedSections.length,
|
|
169
|
+
totalTools: attributedTools.length,
|
|
170
|
+
totalSections: promptSections.length + promptContexts.length,
|
|
171
|
+
attention: attention.length,
|
|
172
|
+
critical: countBySeverity('critical'),
|
|
173
|
+
warning: countBySeverity('warning'),
|
|
174
|
+
info: countBySeverity('info'),
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return { summary, attention: attention.slice(0, MAX_ATTENTION) }
|
|
178
|
+
}
|
package/lib/host/http.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 路由层的公共 HTTP 小工具。
|
|
3
|
+
*
|
|
4
|
+
* 每个路由只做三件事:解析参数 → 调一个服务函数 → 序列化结果。
|
|
5
|
+
* 三件里的第一件和第三件在这里,第二件在各自的模块里。
|
|
6
|
+
* @module dsh-plugin-console/host/http
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/** 成功响应:200,JSON,不缓存。 */
|
|
10
|
+
export function jsonOk(body) {
|
|
11
|
+
return new Response(JSON.stringify(body), {
|
|
12
|
+
status: 200,
|
|
13
|
+
headers: {
|
|
14
|
+
'content-type': 'application/json; charset=utf-8',
|
|
15
|
+
'cache-control': 'no-store',
|
|
16
|
+
},
|
|
17
|
+
})
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** 客户端错误:4xx,带一句可读的原因。 */
|
|
21
|
+
export function jsonError(status, message) {
|
|
22
|
+
return new Response(JSON.stringify({ error: message }), {
|
|
23
|
+
status,
|
|
24
|
+
headers: { 'content-type': 'application/json; charset=utf-8' },
|
|
25
|
+
})
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 把一次路由执行包起来:任何抛出都变成 500 + `{error}`。
|
|
30
|
+
*
|
|
31
|
+
* 路由里不写 try/catch 的理由:这段是唯一需要它的地方,重复六遍只会淹没业务。
|
|
32
|
+
* @param {Function} handler - 真正的实现。
|
|
33
|
+
* @returns {Function} fetch 处理函数。
|
|
34
|
+
*/
|
|
35
|
+
export function jsonRoute(handler) {
|
|
36
|
+
return async (request) => {
|
|
37
|
+
try {
|
|
38
|
+
return await handler(request)
|
|
39
|
+
} catch (error) {
|
|
40
|
+
const message = error instanceof Error ? error.message : String(error)
|
|
41
|
+
return jsonError(500, message)
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* 读 JSON 请求体;空体或坏 JSON 一律当 `{}`。
|
|
48
|
+
* @param {Request} request - 请求。
|
|
49
|
+
* @returns {Promise<any>} 请求体。
|
|
50
|
+
*/
|
|
51
|
+
export async function readJsonBody(request) {
|
|
52
|
+
const body = await request?.json?.().catch(() => ({}))
|
|
53
|
+
return body ?? {}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* 读 Query 参数。
|
|
58
|
+
* @param {Request} request - 请求。
|
|
59
|
+
* @param {string} name - 参数名。
|
|
60
|
+
* @returns {string | undefined} 参数值。
|
|
61
|
+
*/
|
|
62
|
+
export function searchParam(request, name) {
|
|
63
|
+
const value = new URL(request.url).searchParams.get(name)
|
|
64
|
+
return typeof value === 'string' ? value : undefined
|
|
65
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 影响预览:禁用/掩码一个插件会发生什么。
|
|
3
|
+
*
|
|
4
|
+
* 全部基于快照里已有的事实:工具与提示词段的归属、服务供需、模式行、活会话
|
|
5
|
+
* 的运行时工具集。不调用任何写操作,也不读 session log。
|
|
6
|
+
* @module dsh-plugin-console/host/impact
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { readSnapshot } from './snapshot.js'
|
|
10
|
+
|
|
11
|
+
/** 取一个条目的展示名。 */
|
|
12
|
+
function labelOf(entry) {
|
|
13
|
+
return entry?.title || entry?.packageName || entry?.moduleName || entry?.entryId || '未知插件'
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** 从一个名字的所有者里判断是否会真的消失。 */
|
|
17
|
+
function ownership(owners) {
|
|
18
|
+
return {
|
|
19
|
+
owners,
|
|
20
|
+
shared: owners.length > 1,
|
|
21
|
+
lost: owners.length <= 1,
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* 计算一个目标条目的影响面。
|
|
27
|
+
* @param {object} snapshot - 完整快照。
|
|
28
|
+
* @param {string} rowId - 目标行 id 或运行时 id。
|
|
29
|
+
* @returns {object} 影响面;找不到时 `{found:false}`。
|
|
30
|
+
*/
|
|
31
|
+
export function computeImpact(snapshot, rowId) {
|
|
32
|
+
const entries = Array.isArray(snapshot?.entries) ? snapshot.entries : []
|
|
33
|
+
const entry = entries.find((candidate) => candidate.rowId === rowId)
|
|
34
|
+
?? entries.find((candidate) => candidate.entryId === rowId)
|
|
35
|
+
if (entry === undefined) return { found: false, reason: '找不到这个插件条目' }
|
|
36
|
+
|
|
37
|
+
const toolRows = []
|
|
38
|
+
for (const name of entry.registrations?.tools ?? []) {
|
|
39
|
+
const tool = (snapshot?.tools?.attributed ?? []).find((candidate) => candidate.name === name)
|
|
40
|
+
const owners = tool?.owners ?? []
|
|
41
|
+
toolRows.push({ name, ...ownership(owners) })
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const promptRows = []
|
|
45
|
+
for (const name of [...(entry.registrations?.sections ?? []), ...(entry.registrations?.contexts ?? [])]) {
|
|
46
|
+
const row = [...(snapshot?.prompt?.sections ?? []), ...(snapshot?.prompt?.contexts ?? [])]
|
|
47
|
+
.find((candidate) => candidate.name === name)
|
|
48
|
+
const owners = row?.owners ?? []
|
|
49
|
+
promptRows.push({ name, ...ownership(owners) })
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const services = (entry.provides ?? []).map((name) => {
|
|
53
|
+
const record = snapshot?.services?.[name] ?? {}
|
|
54
|
+
const providers = record.providers ?? []
|
|
55
|
+
const consumers = record.consumers ?? []
|
|
56
|
+
return {
|
|
57
|
+
name,
|
|
58
|
+
providers,
|
|
59
|
+
consumers,
|
|
60
|
+
soleProvider: providers.length <= 1,
|
|
61
|
+
affectedConsumers: providers.length <= 1 ? consumers : [],
|
|
62
|
+
}
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
const modes = (snapshot?.modes ?? [])
|
|
66
|
+
.filter((mode) => (mode.rows ?? []).some((row) => row.entryId === entry.rowId || row.moduleName === entry.moduleName))
|
|
67
|
+
.map((mode) => ({ id: mode.id, name: mode.name ?? mode.id }))
|
|
68
|
+
const modeIds = new Set(modes.map((mode) => mode.id))
|
|
69
|
+
|
|
70
|
+
const liveSessions = (snapshot?.tools?.liveAgents ?? [])
|
|
71
|
+
.filter((agent) => modeIds.has(agent.preset) || (agent.tools ?? []).some((name) => (entry.registrations?.tools ?? []).includes(name)))
|
|
72
|
+
.map((agent) => ({ sessionId: agent.sessionId, preset: agent.preset }))
|
|
73
|
+
|
|
74
|
+
const allSessions = [
|
|
75
|
+
...(snapshot?.workspaces ?? []).flatMap((workspace) => workspace.sessions ?? []),
|
|
76
|
+
...(snapshot?.ungroupedSessions ?? []),
|
|
77
|
+
]
|
|
78
|
+
const seenSessions = new Set()
|
|
79
|
+
let coldSessionCount = 0
|
|
80
|
+
for (const session of allSessions) {
|
|
81
|
+
if (typeof session?.id !== 'string' || seenSessions.has(session.id)) continue
|
|
82
|
+
seenSessions.add(session.id)
|
|
83
|
+
if (session.live === true) continue
|
|
84
|
+
if (modeIds.has(session.preset)) coldSessionCount++
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return {
|
|
88
|
+
found: true,
|
|
89
|
+
rowId: entry.rowId,
|
|
90
|
+
entryId: entry.entryId,
|
|
91
|
+
moduleName: entry.moduleName,
|
|
92
|
+
title: labelOf(entry),
|
|
93
|
+
enabled: entry.enabled === true,
|
|
94
|
+
phase: entry.phase ?? null,
|
|
95
|
+
tools: {
|
|
96
|
+
owned: toolRows.map((row) => row.name),
|
|
97
|
+
lost: toolRows.filter((row) => row.lost).map((row) => row.name),
|
|
98
|
+
shared: toolRows.filter((row) => row.shared).map((row) => row.name),
|
|
99
|
+
rows: toolRows,
|
|
100
|
+
},
|
|
101
|
+
sections: {
|
|
102
|
+
owned: promptRows.map((row) => row.name),
|
|
103
|
+
lost: promptRows.filter((row) => row.lost).map((row) => row.name),
|
|
104
|
+
shared: promptRows.filter((row) => row.shared).map((row) => row.name),
|
|
105
|
+
rows: promptRows,
|
|
106
|
+
},
|
|
107
|
+
services: {
|
|
108
|
+
rows: services,
|
|
109
|
+
soleProviders: services.filter((service) => service.soleProvider).map((service) => service.name),
|
|
110
|
+
affectedConsumers: [...new Set(services.flatMap((service) => service.affectedConsumers))],
|
|
111
|
+
},
|
|
112
|
+
modes,
|
|
113
|
+
liveSessions,
|
|
114
|
+
coldSessionCount,
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* 读取一个目标的影响预览。
|
|
120
|
+
* @param {object} ctx - host 插件上下文。
|
|
121
|
+
* @param {string} rowId - 目标行 id/运行时 id。
|
|
122
|
+
* @returns {Promise<object>} 影响面 + 快照时间与 HMR 提示。
|
|
123
|
+
*/
|
|
124
|
+
export async function readImpact(ctx, rowId) {
|
|
125
|
+
const snapshot = await readSnapshot(ctx, {})
|
|
126
|
+
return {
|
|
127
|
+
...computeImpact(snapshot, rowId),
|
|
128
|
+
generatedAt: snapshot.generatedAt,
|
|
129
|
+
// HMR 是否可用决定全局变更是否需要重启;未知时返回 false,界面按「可能需要重启」写。
|
|
130
|
+
hmr: ctx.get('hmr') !== undefined,
|
|
131
|
+
}
|
|
132
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 组合包层与用户 patch 层的归因。
|
|
3
|
+
*
|
|
4
|
+
* 一份快照要回答「这一行是哪来的」,三个来源必须分开记:
|
|
5
|
+
* 1. profile 声明的 bundle 顺序(决定层级归因的优先级);
|
|
6
|
+
* 2. 每个可用 bundle 插入了哪些行、覆盖了哪些行;
|
|
7
|
+
* 3. 用户自己在 profile / home 两层 `cordis.patch.yml` 里插入或覆盖的行。
|
|
8
|
+
* @module dsh-plugin-console/host/layers
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { readJsonFile, readTextFile } from './fs.js'
|
|
12
|
+
import { scanPatchRows } from './patch-scan.js'
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* 采集组合包层。
|
|
16
|
+
*
|
|
17
|
+
* `rowOwner` 只记**首个**插入该行的 bundle(后插入的只是覆盖),
|
|
18
|
+
* `rowOverriders` 记所有覆盖它的 bundle,顺序即应用顺序。
|
|
19
|
+
* @param {object} profile - `profileContext` 服务。
|
|
20
|
+
* @param {object} pluginManager - `pluginManager` 服务。
|
|
21
|
+
* @returns {Promise<{bundleOrder: string[], profileManifest: object | null, rowOwner: Map, rowOverriders: Map, layerList: Array, enabledBundles: string[]}>} 层信息。
|
|
22
|
+
*/
|
|
23
|
+
export async function collectBundles(profile, pluginManager) {
|
|
24
|
+
// —— 1. profile 声明的 bundle 顺序:决定层级归因的优先级 ——
|
|
25
|
+
let bundleOrder = []
|
|
26
|
+
let profileManifest = null
|
|
27
|
+
if (profile?.dir) {
|
|
28
|
+
const manifest = await readJsonFile(`${profile.dir}/package.json`)
|
|
29
|
+
if (manifest !== null) {
|
|
30
|
+
profileManifest = manifest
|
|
31
|
+
bundleOrder = manifest?.dsh?.profile?.bundles ?? []
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// —— 2. 每个可用 bundle 插入了哪些行(用于层级归因) ——
|
|
36
|
+
/** @type {Map<string, {bundle: string, version?: string}>} 行 id → 首个插入它的 bundle */
|
|
37
|
+
const rowOwner = new Map()
|
|
38
|
+
/** @type {Map<string, string[]>} 行 id → 覆盖它的 bundle 列表 */
|
|
39
|
+
const rowOverriders = new Map()
|
|
40
|
+
/** @type {Array<{name: string, version?: string, kind: string}>} */
|
|
41
|
+
const layerList = []
|
|
42
|
+
|
|
43
|
+
let bundles = []
|
|
44
|
+
if (pluginManager && typeof pluginManager.listBundles === 'function') {
|
|
45
|
+
try {
|
|
46
|
+
bundles = await pluginManager.listBundles()
|
|
47
|
+
} catch {
|
|
48
|
+
bundles = []
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const enabledBundles = bundleOrder.length > 0
|
|
53
|
+
? bundleOrder
|
|
54
|
+
: bundles.filter((bundle) => bundle.enabled).map((bundle) => bundle.name)
|
|
55
|
+
|
|
56
|
+
for (const bundleName of enabledBundles) {
|
|
57
|
+
const bundle = bundles.find((candidate) => candidate.name === bundleName)
|
|
58
|
+
if (!bundle) continue
|
|
59
|
+
const kind = bundle.availability === 'profile' ? 'package' : 'builtin'
|
|
60
|
+
layerList.push({ name: bundle.name, version: bundle.version, kind })
|
|
61
|
+
for (const row of bundle.rows ?? []) {
|
|
62
|
+
if (!row?.rowId) continue
|
|
63
|
+
if (!rowOwner.has(row.rowId)) rowOwner.set(row.rowId, { bundle: bundle.name, version: bundle.version })
|
|
64
|
+
}
|
|
65
|
+
for (const rowId of bundle.overrides ?? []) {
|
|
66
|
+
const list = rowOverriders.get(rowId) ?? []
|
|
67
|
+
list.push(bundle.name)
|
|
68
|
+
rowOverriders.set(rowId, list)
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return { bundleOrder, profileManifest, rowOwner, rowOverriders, layerList, enabledBundles }
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* 采集用户自己的 patch 层:profile、home 与启动覆盖层。
|
|
77
|
+
*
|
|
78
|
+
* 三层在 DSH 里的应用顺序是 profile → home → overlays
|
|
79
|
+
* (`readProfilePatches`),所以归因数组也按这个顺序追加;
|
|
80
|
+
* 界面上的 `→` 才等于真实的优先级。文件只读不写。
|
|
81
|
+
* @param {object} profile - `profileContext` 服务。
|
|
82
|
+
* @returns {Promise<{userInserted: Set<string>, userOverridden: Set<string>, userOverrides: Map<string, Array<{layer: string, index?: number, file: string | null, keys: string[]}>>, userLayers: Array<object>}>} 用户层信息。
|
|
83
|
+
*/
|
|
84
|
+
export async function collectUserLayers(profile) {
|
|
85
|
+
const userInserted = new Set()
|
|
86
|
+
const userOverridden = new Set()
|
|
87
|
+
/** @type {Map<string, Array<{layer: string, index?: number, file: string | null, keys: string[]}>>} */
|
|
88
|
+
const userOverrides = new Map()
|
|
89
|
+
const userLayers = []
|
|
90
|
+
|
|
91
|
+
/** 一个 id 可能同时被 profile 与 home 改过;保留每一次,顺序即优先级。 */
|
|
92
|
+
const attribute = (id, info) => {
|
|
93
|
+
userOverridden.add(id)
|
|
94
|
+
const list = userOverrides.get(id) ?? []
|
|
95
|
+
list.push(info)
|
|
96
|
+
userOverrides.set(id, list)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const files = []
|
|
100
|
+
if (profile?.dir) files.push({ layer: 'profile', dir: profile.dir, file: profile.patchPath ?? `${profile.dir}/cordis.patch.yml` })
|
|
101
|
+
if (profile?.home) files.push({ layer: 'home', dir: profile.home, file: `${profile.home}/cordis.patch.yml` })
|
|
102
|
+
for (const { layer, dir, file } of files) {
|
|
103
|
+
const raw = await readTextFile(file)
|
|
104
|
+
if (!raw) continue
|
|
105
|
+
const { inserted, overridden } = scanPatchRows(raw)
|
|
106
|
+
for (const id of inserted.keys()) userInserted.add(id)
|
|
107
|
+
for (const [id, info] of overridden) attribute(id, { layer, file, keys: info.keys })
|
|
108
|
+
userLayers.push({ name: layer, dir, rows: inserted.size, overrides: overridden.size })
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// 启动覆盖层(`--patch`、遥测开关等)已经是解析好的 PatchOptions,
|
|
112
|
+
// 不再读文件;它们应用在 profile / home 之上,所以排在归因最后。
|
|
113
|
+
const overlays = Array.isArray(profile?.overlays) ? profile.overlays : []
|
|
114
|
+
for (const [index, patch] of overlays.entries()) {
|
|
115
|
+
if (patch === null || typeof patch !== 'object') continue
|
|
116
|
+
if (Array.isArray(patch.insert)) {
|
|
117
|
+
for (const row of patch.insert) {
|
|
118
|
+
if (typeof row?.id === 'string') userInserted.add(row.id)
|
|
119
|
+
}
|
|
120
|
+
continue
|
|
121
|
+
}
|
|
122
|
+
if (typeof patch.id !== 'string') continue
|
|
123
|
+
attribute(patch.id, {
|
|
124
|
+
layer: 'overlay',
|
|
125
|
+
...(overlays.length > 1 ? { index } : {}),
|
|
126
|
+
file: null,
|
|
127
|
+
keys: Object.keys(patch).filter((key) => key !== 'id'),
|
|
128
|
+
})
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return { userInserted, userOverridden, userOverrides, userLayers }
|
|
132
|
+
}
|