@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,589 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 一个 Loader 行的 config 分层视图。
|
|
3
|
+
*
|
|
4
|
+
* 快照只保留「生效值」(`entry.configEntries`),看不到这个值来自哪一层、
|
|
5
|
+
* 哪一层又没写。禁用预演要回答的正是后者:上游组合包声明了什么、下游覆盖
|
|
6
|
+
* 改成了什么,以及上游声明的键在生效配置里还在不在。
|
|
7
|
+
*
|
|
8
|
+
* 三条硬规则:
|
|
9
|
+
* 1. **层存在与文件可读分开**:内置组合包、缺 `pluginPackages`、manifest 没有
|
|
10
|
+
* patch 声明,都会让文件读不到;这一层仍然出现在覆盖链里,标 `readable:false`
|
|
11
|
+
* 与原因,不能据此断言「它没改 config」。
|
|
12
|
+
* 2. **只比较、不解释**:诊断只说「生效配置里没有这个键」「值与某层声明不同」。
|
|
13
|
+
* patch 的合并语义不是本插件的公开契约,涉及原因的措辞一律标推断。
|
|
14
|
+
* 3. **只读一个目标行**:文件按 `path + mtime + size` 缓存,最多 32 份;
|
|
15
|
+
* 不返回整份 patch,只返回目标行的 config 子树(有长度上限)。
|
|
16
|
+
* @module dsh-plugin-console/host/config-layers
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { isAbsolute, join } from 'node:path'
|
|
20
|
+
import { fileURLToPath, pathToFileURL } from 'node:url'
|
|
21
|
+
import { configText } from './entries.js'
|
|
22
|
+
import { readJsonFile, readTextFile, statFile } from './fs.js'
|
|
23
|
+
import { scanPatchConfig } from './patch-config.js'
|
|
24
|
+
|
|
25
|
+
/** 覆盖链最多展示的层数(含生效层)。 */
|
|
26
|
+
const MAX_LAYERS = 12
|
|
27
|
+
/** 单层 config 原文的展示上限。 */
|
|
28
|
+
const MAX_RAW_TEXT = 6000
|
|
29
|
+
/** patch 文本缓存条数。 */
|
|
30
|
+
const MAX_CACHE = 32
|
|
31
|
+
|
|
32
|
+
/** path → `{mtimeMs, size, text}`;只用于同一路径反复读取(bundle patch 很少变)。 */
|
|
33
|
+
const patchTextCache = new Map()
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* 读一个 patch 文件,按指纹缓存。
|
|
37
|
+
* @param {string} file - 绝对路径。
|
|
38
|
+
* @returns {Promise<string | null>} 文件内容或 null。
|
|
39
|
+
*/
|
|
40
|
+
async function readPatchText(file) {
|
|
41
|
+
const stat = await statFile(file)
|
|
42
|
+
if (stat === null) return null
|
|
43
|
+
const hit = patchTextCache.get(file)
|
|
44
|
+
if (hit !== undefined && hit.mtimeMs === stat.mtimeMs && hit.size === stat.size) return hit.text
|
|
45
|
+
const text = await readTextFile(file)
|
|
46
|
+
if (text === null) return null
|
|
47
|
+
if (patchTextCache.size >= MAX_CACHE) {
|
|
48
|
+
const oldest = patchTextCache.keys().next().value
|
|
49
|
+
patchTextCache.delete(oldest)
|
|
50
|
+
}
|
|
51
|
+
patchTextCache.set(file, { mtimeMs: stat.mtimeMs, size: stat.size, text })
|
|
52
|
+
return text
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** 把 `file://`、绝对路径与相对路径统一成绝对路径。 */
|
|
56
|
+
function resolvePath(file, baseDir) {
|
|
57
|
+
if (typeof file !== 'string' || file === '') return undefined
|
|
58
|
+
if (file.startsWith('file://')) {
|
|
59
|
+
try {
|
|
60
|
+
return fileURLToPath(file)
|
|
61
|
+
} catch {
|
|
62
|
+
return undefined
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
if (isAbsolute(file)) return file
|
|
66
|
+
return baseDir === undefined ? undefined : join(baseDir, file)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** 一个路径字符串的 file URL;profile.dir 传给 packageOf 时需要目录 URL。 */
|
|
70
|
+
function dirUrl(dir) {
|
|
71
|
+
if (typeof dir !== 'string' || dir === '') return undefined
|
|
72
|
+
return pathToFileURL(dir.endsWith('/') || dir.endsWith('\\') ? dir : dir + '/').href
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** 组合包是否插入或覆盖了目标行。 */
|
|
76
|
+
function bundleTouch(bundle, rowId) {
|
|
77
|
+
const inserted = (bundle.rows ?? []).some((row) => row?.rowId === rowId)
|
|
78
|
+
const overridden = (bundle.overrides ?? []).some((id) => id === rowId)
|
|
79
|
+
return { inserted, overridden }
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* 读一个组合包的 patch 文件。
|
|
84
|
+
*
|
|
85
|
+
* 解析顺序:`pluginPackages.packageOf` → `<profile>/node_modules/<name>` 兜底 →
|
|
86
|
+
* manifest 的 `dsh.bundle.patch`(数组只读第一个)→ `cordis.patch.yml`。
|
|
87
|
+
* 任何一步失败都返回原因,不抛错。
|
|
88
|
+
* @param {object} ctx - host 插件上下文。
|
|
89
|
+
* @param {object} snapshot - 快照(只用 profile.dir)。
|
|
90
|
+
* @param {object} bundle - `pluginManager.listBundles()` 的一条记录。
|
|
91
|
+
* @returns {Promise<{readable: boolean, reason?: string, file?: string, note?: string}>} 读取结果。
|
|
92
|
+
*/
|
|
93
|
+
async function readBundlePatch(ctx, snapshot, bundle) {
|
|
94
|
+
const profileDir = snapshot.profile?.dir
|
|
95
|
+
if (bundle.availability !== undefined && bundle.availability !== 'profile') {
|
|
96
|
+
return { readable: false, reason: '内置组合包没有可读的 patch 文件' }
|
|
97
|
+
}
|
|
98
|
+
const packages = ctx.get('pluginPackages')
|
|
99
|
+
let pkgDir
|
|
100
|
+
let manifest
|
|
101
|
+
if (packages !== undefined && typeof packages.packageOf === 'function') {
|
|
102
|
+
try {
|
|
103
|
+
const pkg = packages.packageOf(bundle.name, dirUrl(profileDir))
|
|
104
|
+
if (typeof pkg?.dir === 'string') pkgDir = pkg.dir
|
|
105
|
+
manifest = pkg?.manifest
|
|
106
|
+
} catch {
|
|
107
|
+
// 解析失败不算异常:下面还有 node_modules 兜底。
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
if (pkgDir === undefined && typeof profileDir === 'string') {
|
|
111
|
+
const candidate = join(profileDir, 'node_modules', ...String(bundle.name).split('/'))
|
|
112
|
+
const json = await readJsonFile(join(candidate, 'package.json'))
|
|
113
|
+
if (json !== null) {
|
|
114
|
+
pkgDir = candidate
|
|
115
|
+
manifest = json
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
if (pkgDir === undefined) {
|
|
119
|
+
return { readable: false, reason: '无法解析组合包目录(pluginPackages 缺席或没有返回 dir)' }
|
|
120
|
+
}
|
|
121
|
+
manifest ??= await readJsonFile(join(pkgDir, 'package.json'))
|
|
122
|
+
const declared = manifest?.dsh?.bundle?.patch
|
|
123
|
+
let rel = 'cordis.patch.yml'
|
|
124
|
+
let note
|
|
125
|
+
if (Array.isArray(declared)) {
|
|
126
|
+
const first = declared.find((item) => typeof item === 'string')
|
|
127
|
+
if (first !== undefined) rel = first
|
|
128
|
+
note = '声明了多个 patch 文件,本页只读取第一个'
|
|
129
|
+
} else if (typeof declared === 'string') {
|
|
130
|
+
rel = declared
|
|
131
|
+
}
|
|
132
|
+
const file = resolvePath(rel, pkgDir)
|
|
133
|
+
if (file === undefined) return { readable: false, reason: `无法解析 patch 路径(${rel})` }
|
|
134
|
+
const text = await readPatchText(file)
|
|
135
|
+
if (text === null) return { readable: false, reason: `patch 文件读取失败:${file}` }
|
|
136
|
+
return { readable: true, file, ...(note === undefined ? {} : { note }) }
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** 把若干次出现合并成一层视图;同层内后面的出现覆盖前面的键。 */
|
|
140
|
+
function mergeOccurrences(occurrences) {
|
|
141
|
+
const keys = []
|
|
142
|
+
const configKeys = []
|
|
143
|
+
const values = {}
|
|
144
|
+
const parsedValues = {}
|
|
145
|
+
const rows = []
|
|
146
|
+
let parsed = true
|
|
147
|
+
let complete = true
|
|
148
|
+
let truncated = false
|
|
149
|
+
const rawParts = []
|
|
150
|
+
for (const occurrence of occurrences) {
|
|
151
|
+
for (const key of occurrence.keys) if (!keys.includes(key)) keys.push(key)
|
|
152
|
+
const row = {
|
|
153
|
+
line: occurrence.line,
|
|
154
|
+
keys: [...occurrence.keys],
|
|
155
|
+
configKeys: [...(occurrence.configKeys ?? [])],
|
|
156
|
+
hasConfig: occurrence.hasConfig === true,
|
|
157
|
+
parsed: occurrence.parsed !== false,
|
|
158
|
+
...(occurrence.reason === undefined ? {} : { reason: occurrence.reason }),
|
|
159
|
+
rawText: typeof occurrence.configRaw === 'string' ? occurrence.configRaw : '',
|
|
160
|
+
}
|
|
161
|
+
rows.push(row)
|
|
162
|
+
if (occurrence.hasConfig !== true) continue
|
|
163
|
+
if (occurrence.parsed === false) {
|
|
164
|
+
parsed = false
|
|
165
|
+
complete = false
|
|
166
|
+
}
|
|
167
|
+
if (occurrence.truncated === true) truncated = true
|
|
168
|
+
for (const key of occurrence.configKeys ?? []) {
|
|
169
|
+
if (!configKeys.includes(key)) configKeys.push(key)
|
|
170
|
+
if (occurrence.parsed !== false && occurrence.config !== undefined && typeof occurrence.config === 'object') {
|
|
171
|
+
values[key] = configText(occurrence.config[key])
|
|
172
|
+
parsedValues[key] = occurrence.config[key]
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
if (typeof occurrence.configRaw === 'string' && occurrence.configRaw !== '') rawParts.push(occurrence.configRaw)
|
|
176
|
+
}
|
|
177
|
+
const rawText = rawParts.join('\n---\n')
|
|
178
|
+
return {
|
|
179
|
+
keys,
|
|
180
|
+
configKeys,
|
|
181
|
+
values,
|
|
182
|
+
parsedValues,
|
|
183
|
+
rows,
|
|
184
|
+
parsed,
|
|
185
|
+
complete,
|
|
186
|
+
truncated: truncated || rawText.length > MAX_RAW_TEXT,
|
|
187
|
+
rawText: rawText.length > MAX_RAW_TEXT ? `${rawText.slice(0, MAX_RAW_TEXT)}…(已截断)` : rawText,
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** 一条层记录的基础字段。 */
|
|
192
|
+
function baseLayer(fields) {
|
|
193
|
+
return {
|
|
194
|
+
layerId: fields.layerId,
|
|
195
|
+
label: fields.label,
|
|
196
|
+
kind: fields.kind,
|
|
197
|
+
order: fields.order,
|
|
198
|
+
present: true,
|
|
199
|
+
readable: true,
|
|
200
|
+
keys: [],
|
|
201
|
+
configKeys: [],
|
|
202
|
+
values: {},
|
|
203
|
+
parsedValues: {},
|
|
204
|
+
rows: [],
|
|
205
|
+
parsed: true,
|
|
206
|
+
complete: true,
|
|
207
|
+
truncated: false,
|
|
208
|
+
rawText: '',
|
|
209
|
+
...fields,
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** 从一段 patch 文本里建层;没有目标行时返回 null。 */
|
|
214
|
+
function layerFromPatchText({ text, rowId, layerId, label, kind, order, file, version, availability, note }) {
|
|
215
|
+
const { rows } = scanPatchConfig(text)
|
|
216
|
+
const occurrences = rows.filter((row) => row.id === rowId)
|
|
217
|
+
if (occurrences.length === 0) return null
|
|
218
|
+
const merged = mergeOccurrences(occurrences)
|
|
219
|
+
return baseLayer({
|
|
220
|
+
layerId,
|
|
221
|
+
label,
|
|
222
|
+
kind,
|
|
223
|
+
order,
|
|
224
|
+
file,
|
|
225
|
+
...(version === undefined ? {} : { version }),
|
|
226
|
+
...(availability === undefined ? {} : { availability }),
|
|
227
|
+
...(note === undefined ? {} : { note }),
|
|
228
|
+
...merged,
|
|
229
|
+
})
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** 有效配置里被快照截断的值:不能参与等值比较。 */
|
|
233
|
+
function isClipped(text) {
|
|
234
|
+
return typeof text === 'string' && text.includes('…(已截断,共 ')
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/** 深比较:对象键顺序无关,数组顺序敏感。 */
|
|
238
|
+
function deepEqual(left, right) {
|
|
239
|
+
if (left === right) return true
|
|
240
|
+
if (Array.isArray(left) || Array.isArray(right)) {
|
|
241
|
+
if (!Array.isArray(left) || !Array.isArray(right) || left.length !== right.length) return false
|
|
242
|
+
return left.every((item, index) => deepEqual(item, right[index]))
|
|
243
|
+
}
|
|
244
|
+
if (left === null || right === null || typeof left !== 'object' || typeof right !== 'object') return false
|
|
245
|
+
const leftKeys = Object.keys(left)
|
|
246
|
+
const rightKeys = Object.keys(right)
|
|
247
|
+
if (leftKeys.length !== rightKeys.length) return false
|
|
248
|
+
return leftKeys.every((key) => Object.hasOwn(right, key) && deepEqual(left[key], right[key]))
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/** 层声明的值与生效值是否一致。 */
|
|
252
|
+
function valueMatches(layerValue, effectiveText) {
|
|
253
|
+
if (typeof layerValue === 'string') return layerValue === effectiveText
|
|
254
|
+
if (typeof effectiveText !== 'string') return false
|
|
255
|
+
try {
|
|
256
|
+
return deepEqual(layerValue, JSON.parse(effectiveText))
|
|
257
|
+
} catch {
|
|
258
|
+
return false
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/** 诊断项;`id` 只用于 React key 与自检定位。 */
|
|
263
|
+
function diagnostic(kind, severity, evidence, message, extra = {}) {
|
|
264
|
+
const suffix = extra.key ?? extra.layerId ?? ''
|
|
265
|
+
return { id: `${kind}:${suffix}`, kind, severity, evidence, message, ...extra }
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* 比较各层声明与生效配置。
|
|
270
|
+
*
|
|
271
|
+
* 只产出四类可辩护的结论:某个键在生效配置里有没有、值与某层是否相同、
|
|
272
|
+
* 同一键被几层声明、以及「后层覆盖了 config 却没重写上游的 inject」这个
|
|
273
|
+
* **带条件**的推断(措辞里明确写「若运行时按整份替换」)。
|
|
274
|
+
* @param {Array<object>} layers - 覆盖链,含 `effective` 层。
|
|
275
|
+
* @returns {Array<object>} 诊断列表。
|
|
276
|
+
*/
|
|
277
|
+
export function diffConfigLayers(layers) {
|
|
278
|
+
const effective = layers.find((layer) => layer.kind === 'effective')
|
|
279
|
+
if (effective === undefined) return []
|
|
280
|
+
const diagnostics = []
|
|
281
|
+
const effectiveKeys = new Set(effective.configKeys ?? [])
|
|
282
|
+
const declaring = new Map()
|
|
283
|
+
for (const layer of layers) {
|
|
284
|
+
if (layer.kind === 'effective') continue
|
|
285
|
+
if (layer.readable !== true || layer.complete !== true) continue
|
|
286
|
+
for (const key of layer.configKeys ?? []) {
|
|
287
|
+
const list = declaring.get(key) ?? []
|
|
288
|
+
list.push({ layer, value: layer.parsedValues?.[key] })
|
|
289
|
+
declaring.set(key, list)
|
|
290
|
+
}
|
|
291
|
+
if (layer.configKeys?.length === 0 && layer.rows.some((row) => row.hasConfig === true)) {
|
|
292
|
+
diagnostics.push(diagnostic(
|
|
293
|
+
'empty-config-override',
|
|
294
|
+
'warn',
|
|
295
|
+
'file',
|
|
296
|
+
`层「${layer.label}」覆盖了 config,但没有写出任何键;本页不推断这是否会清空早层配置。`,
|
|
297
|
+
{ layerId: layer.layerId },
|
|
298
|
+
))
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
for (const [key, list] of declaring) {
|
|
303
|
+
const declaredBy = [...new Set(list.map((item) => item.layer.label))]
|
|
304
|
+
if (!effectiveKeys.has(key)) {
|
|
305
|
+
if (key === 'inject') {
|
|
306
|
+
diagnostics.push(diagnostic(
|
|
307
|
+
'upstream-inject-risk',
|
|
308
|
+
'warn',
|
|
309
|
+
'runtime',
|
|
310
|
+
`上游层「${declaredBy.join('、')}」声明了 config.inject,但当前生效配置里没有这个键。若上游更新了 inject 而覆盖层没有同步重写,二者会失配;本页不推断合并语义。`,
|
|
311
|
+
{ key },
|
|
312
|
+
))
|
|
313
|
+
} else {
|
|
314
|
+
diagnostics.push(diagnostic(
|
|
315
|
+
'missing-in-effective',
|
|
316
|
+
'info',
|
|
317
|
+
'runtime',
|
|
318
|
+
`层「${declaredBy.join('、')}」声明了 config.${key},但当前生效配置里没有这个键。若后续层按整份 config 替换,早层声明会失效;本页不推断合并语义。`,
|
|
319
|
+
{ key },
|
|
320
|
+
))
|
|
321
|
+
}
|
|
322
|
+
continue
|
|
323
|
+
}
|
|
324
|
+
if (list.length > 1) {
|
|
325
|
+
diagnostics.push(diagnostic(
|
|
326
|
+
'duplicate-key',
|
|
327
|
+
'info',
|
|
328
|
+
'file',
|
|
329
|
+
`config.${key} 被 ${list.length} 层声明;生效值只能反映最后一次结果。`,
|
|
330
|
+
{ key },
|
|
331
|
+
))
|
|
332
|
+
}
|
|
333
|
+
const effectiveText = effective.values?.[key]
|
|
334
|
+
if (isClipped(effectiveText)) {
|
|
335
|
+
diagnostics.push(diagnostic(
|
|
336
|
+
'value-not-compared',
|
|
337
|
+
'info',
|
|
338
|
+
'runtime',
|
|
339
|
+
`config.${key} 的生效值已被快照截断,无法与层声明做等值比较。`,
|
|
340
|
+
{ key },
|
|
341
|
+
))
|
|
342
|
+
continue
|
|
343
|
+
}
|
|
344
|
+
const mismatched = list.filter((item) => !valueMatches(item.value, effectiveText))
|
|
345
|
+
if (mismatched.length === 0) continue
|
|
346
|
+
if (key === 'inject') {
|
|
347
|
+
const laterWithout = layers.filter((layer) => (
|
|
348
|
+
layer.kind !== 'effective'
|
|
349
|
+
&& layer.readable === true
|
|
350
|
+
&& layer.order > list[0].layer.order
|
|
351
|
+
&& (layer.configKeys ?? []).length > 0
|
|
352
|
+
&& !(layer.configKeys ?? []).includes('inject')
|
|
353
|
+
))
|
|
354
|
+
diagnostics.push(diagnostic(
|
|
355
|
+
'upstream-drift',
|
|
356
|
+
'warn',
|
|
357
|
+
'inference',
|
|
358
|
+
`上游层声明的 config.inject 与生效值不同${laterWithout.length === 0
|
|
359
|
+
? ''
|
|
360
|
+
: `;后层「${laterWithout.map((layer) => layer.label).join('、')}」覆盖了 config,但没有重写 inject`}。若运行时按整份替换,说明覆盖层没有跟随上游更新。`,
|
|
361
|
+
{ key, laterOverrideWithoutKey: ['inject'] },
|
|
362
|
+
))
|
|
363
|
+
continue
|
|
364
|
+
}
|
|
365
|
+
diagnostics.push(diagnostic(
|
|
366
|
+
'value-differs',
|
|
367
|
+
'info',
|
|
368
|
+
'runtime',
|
|
369
|
+
`config.${key} 在层「${mismatched.map((item) => item.layer.label).join('、')}」与生效值中不同;生效值以后层为准(运行时事实)。`,
|
|
370
|
+
{ key },
|
|
371
|
+
))
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
for (const key of effectiveKeys) {
|
|
375
|
+
if (declaring.has(key)) continue
|
|
376
|
+
diagnostics.push(diagnostic(
|
|
377
|
+
'only-runtime',
|
|
378
|
+
'info',
|
|
379
|
+
'runtime',
|
|
380
|
+
`config.${key} 只出现在生效配置里,文件层没有声明(可能来自插件默认值或运行时补全)。`,
|
|
381
|
+
{ key },
|
|
382
|
+
))
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
return diagnostics
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/** 用覆盖者名单兜底:pluginManager 不可用时仍要知道这一行来自哪个组合包。 */
|
|
389
|
+
function fallbackBundleLayer(entry, order) {
|
|
390
|
+
if (typeof entry.layer !== 'string' || entry.layer === 'mode' || entry.layer === 'runtime') return []
|
|
391
|
+
return [baseLayer({
|
|
392
|
+
layerId: `bundle:${entry.layer}`,
|
|
393
|
+
label: entry.layer,
|
|
394
|
+
kind: 'bundle',
|
|
395
|
+
order,
|
|
396
|
+
readable: false,
|
|
397
|
+
reason: 'pluginManager 不可用,无法读取组合包清单与 patch 文件',
|
|
398
|
+
})]
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* 汇总一个目标的 config 覆盖链。
|
|
403
|
+
* @param {object} ctx - host 插件上下文。
|
|
404
|
+
* @param {object} snapshot - 当前快照。
|
|
405
|
+
* @param {string} rowId - 目标行 id。
|
|
406
|
+
* @returns {Promise<object>} `{available:true, layers, diagnostics}` 或 `{available:false, reason}`。
|
|
407
|
+
*/
|
|
408
|
+
export async function collectConfigLayers(ctx, snapshot, rowId) {
|
|
409
|
+
const entries = Array.isArray(snapshot?.entries) ? snapshot.entries : []
|
|
410
|
+
const entry = entries.find((candidate) => candidate.rowId === rowId)
|
|
411
|
+
?? entries.find((candidate) => candidate.entryId === rowId)
|
|
412
|
+
if (entry === undefined) return { available: false, reason: '找不到这个插件条目' }
|
|
413
|
+
if (entry.modeOnly === true) {
|
|
414
|
+
return { available: false, reason: '这个条目只由模式挂载,没有自己的 Loader 行与 patch config' }
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
const profile = ctx.get('profileContext')
|
|
418
|
+
const pluginManager = ctx.get('pluginManager')
|
|
419
|
+
let bundles
|
|
420
|
+
if (pluginManager !== undefined && typeof pluginManager.listBundles === 'function') {
|
|
421
|
+
try {
|
|
422
|
+
const listed = await pluginManager.listBundles()
|
|
423
|
+
if (Array.isArray(listed)) bundles = listed
|
|
424
|
+
} catch {
|
|
425
|
+
bundles = undefined
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
const orderNames = Array.isArray(snapshot.profile?.bundles) && snapshot.profile.bundles.length > 0
|
|
430
|
+
? snapshot.profile.bundles
|
|
431
|
+
: (bundles ?? []).map((bundle) => bundle.name)
|
|
432
|
+
const layers = []
|
|
433
|
+
const touched = new Set()
|
|
434
|
+
for (const [index, name] of orderNames.entries()) {
|
|
435
|
+
const bundle = bundles?.find((candidate) => candidate.name === name)
|
|
436
|
+
if (bundle === undefined) {
|
|
437
|
+
if (name === entry.layer && !touched.has(name)) {
|
|
438
|
+
layers.push(...fallbackBundleLayer(entry, index))
|
|
439
|
+
touched.add(name)
|
|
440
|
+
}
|
|
441
|
+
continue
|
|
442
|
+
}
|
|
443
|
+
const touch = bundleTouch(bundle, entry.rowId)
|
|
444
|
+
if (!touch.inserted && !touch.overridden) continue
|
|
445
|
+
touched.add(name)
|
|
446
|
+
const read = await readBundlePatch(ctx, snapshot, bundle)
|
|
447
|
+
if (read.readable !== true) {
|
|
448
|
+
layers.push(baseLayer({
|
|
449
|
+
layerId: `bundle:${name}`,
|
|
450
|
+
label: name,
|
|
451
|
+
kind: 'bundle',
|
|
452
|
+
order: index,
|
|
453
|
+
version: bundle.version,
|
|
454
|
+
availability: bundle.availability,
|
|
455
|
+
readable: false,
|
|
456
|
+
reason: read.reason ?? 'patch 文件不可读',
|
|
457
|
+
}))
|
|
458
|
+
continue
|
|
459
|
+
}
|
|
460
|
+
const text = await readPatchText(read.file)
|
|
461
|
+
if (text === null) {
|
|
462
|
+
layers.push(baseLayer({
|
|
463
|
+
layerId: `bundle:${name}`,
|
|
464
|
+
label: name,
|
|
465
|
+
kind: 'bundle',
|
|
466
|
+
order: index,
|
|
467
|
+
version: bundle.version,
|
|
468
|
+
availability: bundle.availability,
|
|
469
|
+
readable: false,
|
|
470
|
+
reason: `patch 文件读取失败:${read.file}`,
|
|
471
|
+
}))
|
|
472
|
+
continue
|
|
473
|
+
}
|
|
474
|
+
const fromText = layerFromPatchText({
|
|
475
|
+
text,
|
|
476
|
+
rowId: entry.rowId,
|
|
477
|
+
layerId: `bundle:${name}`,
|
|
478
|
+
label: name,
|
|
479
|
+
kind: 'bundle',
|
|
480
|
+
order: index,
|
|
481
|
+
file: read.file,
|
|
482
|
+
version: bundle.version,
|
|
483
|
+
availability: bundle.availability,
|
|
484
|
+
note: read.note,
|
|
485
|
+
})
|
|
486
|
+
if (fromText === null) {
|
|
487
|
+
layers.push(baseLayer({
|
|
488
|
+
layerId: `bundle:${name}`,
|
|
489
|
+
label: name,
|
|
490
|
+
kind: 'bundle',
|
|
491
|
+
order: index,
|
|
492
|
+
version: bundle.version,
|
|
493
|
+
availability: bundle.availability,
|
|
494
|
+
file: read.file,
|
|
495
|
+
note: 'patch 文件里没有找到这一行的 id',
|
|
496
|
+
}))
|
|
497
|
+
} else {
|
|
498
|
+
layers.push(fromText)
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
if (layers.length === 0) layers.push(...fallbackBundleLayer(entry, orderNames.length))
|
|
502
|
+
|
|
503
|
+
const userFiles = []
|
|
504
|
+
if (typeof profile?.dir === 'string') {
|
|
505
|
+
userFiles.push({ layerId: 'profile', label: 'profile patch', file: resolvePath(profile.patchPath ?? 'cordis.patch.yml', profile.dir) })
|
|
506
|
+
}
|
|
507
|
+
if (typeof profile?.home === 'string') {
|
|
508
|
+
userFiles.push({ layerId: 'home', label: 'home patch', file: resolvePath('cordis.patch.yml', profile.home) })
|
|
509
|
+
}
|
|
510
|
+
let order = layers.length
|
|
511
|
+
for (const item of userFiles) {
|
|
512
|
+
if (item.file === undefined) continue
|
|
513
|
+
const text = await readPatchText(item.file)
|
|
514
|
+
if (text === null) continue
|
|
515
|
+
const layer = layerFromPatchText({
|
|
516
|
+
text,
|
|
517
|
+
rowId: entry.rowId,
|
|
518
|
+
layerId: item.layerId,
|
|
519
|
+
label: item.label,
|
|
520
|
+
kind: item.layerId,
|
|
521
|
+
order,
|
|
522
|
+
file: item.file,
|
|
523
|
+
})
|
|
524
|
+
if (layer !== null) layers.push(layer)
|
|
525
|
+
order++
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
const overlays = Array.isArray(profile?.overlays) ? profile.overlays : []
|
|
529
|
+
for (const [index, patch] of overlays.entries()) {
|
|
530
|
+
if (patch === null || typeof patch !== 'object') continue
|
|
531
|
+
const row = Array.isArray(patch.insert)
|
|
532
|
+
? patch.insert.find((item) => item?.id === entry.rowId)
|
|
533
|
+
: patch.id === entry.rowId ? patch : undefined
|
|
534
|
+
if (row === undefined) continue
|
|
535
|
+
const values = {}
|
|
536
|
+
const parsedValues = {}
|
|
537
|
+
const configKeys = row.config !== null && typeof row.config === 'object' && !Array.isArray(row.config) ? Object.keys(row.config) : []
|
|
538
|
+
for (const key of configKeys) {
|
|
539
|
+
values[key] = configText(row.config[key])
|
|
540
|
+
parsedValues[key] = row.config[key]
|
|
541
|
+
}
|
|
542
|
+
layers.push(baseLayer({
|
|
543
|
+
layerId: `overlay:${index}`,
|
|
544
|
+
label: overlays.length > 1 ? `启动覆盖层 ${index + 1}` : '启动覆盖层',
|
|
545
|
+
kind: 'overlay',
|
|
546
|
+
order: order++,
|
|
547
|
+
keys: Object.keys(row).filter((key) => key !== 'id'),
|
|
548
|
+
configKeys,
|
|
549
|
+
values,
|
|
550
|
+
parsedValues,
|
|
551
|
+
readable: true,
|
|
552
|
+
rows: [{ line: null, keys: Object.keys(row).filter((key) => key !== 'id'), configKeys, hasConfig: configKeys.length > 0, parsed: true, rawText: '' }],
|
|
553
|
+
}))
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
const effectiveValues = {}
|
|
557
|
+
const effectiveRaw = {}
|
|
558
|
+
for (const row of Array.isArray(entry.configEntries) ? entry.configEntries : []) {
|
|
559
|
+
effectiveValues[row.key] = row.value
|
|
560
|
+
}
|
|
561
|
+
const effectiveConfigKeys = Array.isArray(entry.configEntries) ? entry.configEntries.map((row) => row.key) : []
|
|
562
|
+
layers.push(baseLayer({
|
|
563
|
+
layerId: 'effective',
|
|
564
|
+
label: '生效配置(运行时)',
|
|
565
|
+
kind: 'effective',
|
|
566
|
+
order: order++,
|
|
567
|
+
keys: ['config'],
|
|
568
|
+
configKeys: effectiveConfigKeys,
|
|
569
|
+
values: effectiveValues,
|
|
570
|
+
parsedValues: effectiveRaw,
|
|
571
|
+
readable: true,
|
|
572
|
+
rows: [],
|
|
573
|
+
note: '来自 Loader 的合并结果;本页不推断各层如何合并',
|
|
574
|
+
}))
|
|
575
|
+
|
|
576
|
+
const kept = layers.length <= MAX_LAYERS
|
|
577
|
+
? layers
|
|
578
|
+
: [...layers.slice(0, MAX_LAYERS - 1), layers[layers.length - 1]]
|
|
579
|
+
const diagnostics = diffConfigLayers(kept)
|
|
580
|
+
if (kept.length < layers.length) {
|
|
581
|
+
diagnostics.push(diagnostic(
|
|
582
|
+
'layers-truncated',
|
|
583
|
+
'info',
|
|
584
|
+
'unknown',
|
|
585
|
+
`覆盖链共 ${layers.length} 层,本页只展示前 ${MAX_LAYERS - 1} 层与生效层。`,
|
|
586
|
+
))
|
|
587
|
+
}
|
|
588
|
+
return { available: true, layers: kept, diagnostics }
|
|
589
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* host 半的常量镜像。
|
|
3
|
+
*
|
|
4
|
+
* 这里只有「读代码的人需要知道、但运行时不一定拿得到」的东西:Cordis 的状态
|
|
5
|
+
* 枚举是跨包 const enum(没有运行时对象可 import),路由路径必须是字面量。
|
|
6
|
+
* @module dsh-plugin-console/host/constants
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/** Fiber 状态常量镜像:cordis 的 FiberState 是跨包 const enum。 */
|
|
10
|
+
export const FIBER_STATE = {
|
|
11
|
+
PENDING: 0,
|
|
12
|
+
LOADING: 1,
|
|
13
|
+
ACTIVE: 2,
|
|
14
|
+
FAILED: 3,
|
|
15
|
+
DISPOSED: 4,
|
|
16
|
+
UNLOADING: 5,
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Fiber 状态到公共阶段词汇的映射;已销毁折叠为 null。 */
|
|
20
|
+
export const FIBER_PHASE = {
|
|
21
|
+
[FIBER_STATE.PENDING]: 'pending',
|
|
22
|
+
[FIBER_STATE.LOADING]: 'loading',
|
|
23
|
+
[FIBER_STATE.ACTIVE]: 'active',
|
|
24
|
+
[FIBER_STATE.FAILED]: 'failed',
|
|
25
|
+
[FIBER_STATE.UNLOADING]: 'unloading',
|
|
26
|
+
[FIBER_STATE.DISPOSED]: null,
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** 预测装配路由路径。 */
|
|
30
|
+
export const PREVIEW_PATH = '/api/plugin-console/preview'
|
|
31
|
+
|
|
32
|
+
/** 快照路由路径。 */
|
|
33
|
+
export const SNAPSHOT_PATH = '/api/plugin-console/snapshot'
|
|
34
|
+
|
|
35
|
+
/** 模板列表路由。 */
|
|
36
|
+
export const TEMPLATES_PATH = '/api/plugin-console/templates'
|
|
37
|
+
|
|
38
|
+
/** 单模板路由(含 revision 列表)。 */
|
|
39
|
+
export const TEMPLATE_PATH = '/api/plugin-console/template'
|
|
40
|
+
|
|
41
|
+
/** 把模板应用到一个会话。 */
|
|
42
|
+
export const APPLY_PATH = '/api/plugin-console/apply'
|
|
43
|
+
|
|
44
|
+
/** 每会话的启用开关:查询与切换。 */
|
|
45
|
+
export const ACTIVATION_PATH = '/api/plugin-console/activation'
|
|
46
|
+
|
|
47
|
+
/** 单会话的实际请求摘要(读 session log,按会话懒取)。 */
|
|
48
|
+
export const SESSION_INFLUENCE_PATH = '/api/plugin-console/session-influence'
|
|
49
|
+
|
|
50
|
+
/** 最近一段时间的工具活动(读 session log,按时间窗与会话数限制)。 */
|
|
51
|
+
export const ACTIVITY_PATH = '/api/plugin-console/activity'
|
|
52
|
+
|
|
53
|
+
/** 控制前的影响预览。 */
|
|
54
|
+
export const IMPACT_PATH = '/api/plugin-console/impact'
|
|
55
|
+
|
|
56
|
+
/** 禁用效果预演:影响面 + config 分层 + 关系(只读)。 */
|
|
57
|
+
export const DISABLE_PREVIEW_PATH = '/api/plugin-console/disable-preview'
|
|
58
|
+
|
|
59
|
+
/** 执行一次控制(全局启停 / 会话掩码)。 */
|
|
60
|
+
export const CONTROL_PATH = '/api/plugin-console/control'
|
|
61
|
+
|
|
62
|
+
/** 最近的控制审计。 */
|
|
63
|
+
export const OPERATIONS_PATH = '/api/plugin-console/operations'
|
|
64
|
+
|
|
65
|
+
/** 清理模板数据:按工作区清绑定,或清空全部并删除存储文件。 */
|
|
66
|
+
export const CLEANUP_PATH = '/api/plugin-console/cleanup'
|
|
67
|
+
|
|
68
|
+
/** 失败 fiber 的诊断读取上限,避免单个挂起的 fiber 拖住整次快照。 */
|
|
69
|
+
export const DIAGNOSTIC_TIMEOUT_MS = 250
|