@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.
Files changed (101) hide show
  1. package/LICENSE +23 -0
  2. package/README.md +95 -3
  3. package/cordis.patch.yml +5 -0
  4. package/icon.svg +203 -0
  5. package/lib/client.activity.js +222 -0
  6. package/lib/client.apply.js +171 -0
  7. package/lib/client.changes.js +206 -0
  8. package/lib/client.cleanup.js +270 -0
  9. package/lib/client.control.js +247 -0
  10. package/lib/client.dialog.js +77 -0
  11. package/lib/client.disable-preview.js +904 -0
  12. package/lib/client.draft.js +280 -0
  13. package/lib/client.editor-messages.js +421 -0
  14. package/lib/client.editor-store.js +266 -0
  15. package/lib/client.editor.js +433 -0
  16. package/lib/client.effects.js +115 -0
  17. package/lib/client.impact-tree.js +487 -0
  18. package/lib/client.influence.js +361 -0
  19. package/lib/client.js +246 -0
  20. package/lib/client.page.js +646 -0
  21. package/lib/client.plugin-filters.js +62 -0
  22. package/lib/client.plugins.js +506 -0
  23. package/lib/client.preview-messages.js +506 -0
  24. package/lib/client.preview-raw.js +132 -0
  25. package/lib/client.preview-store.js +128 -0
  26. package/lib/client.preview-tools.js +194 -0
  27. package/lib/client.preview.js +448 -0
  28. package/lib/client.relation-graph.js +364 -0
  29. package/lib/client.schema.js +327 -0
  30. package/lib/client.search.js +409 -0
  31. package/lib/client.session-detail.js +503 -0
  32. package/lib/client.sessions.js +495 -0
  33. package/lib/client.shared.js +562 -0
  34. package/lib/client.store.js +452 -0
  35. package/lib/client.styles.js +589 -0
  36. package/lib/client.template-card.js +240 -0
  37. package/lib/client.tools.js +366 -0
  38. package/lib/client.workspace-picker.js +204 -0
  39. package/lib/host/activity.js +252 -0
  40. package/lib/host/assembly-preview.js +244 -0
  41. package/lib/host/attribution.js +341 -0
  42. package/lib/host/catalog.js +133 -0
  43. package/lib/host/categorize.js +40 -0
  44. package/lib/host/config-layers.js +589 -0
  45. package/lib/host/constants.js +69 -0
  46. package/lib/host/control.js +159 -0
  47. package/lib/host/disable-preview.js +651 -0
  48. package/lib/host/entries.js +289 -0
  49. package/lib/host/fiber.js +86 -0
  50. package/lib/host/fs.js +99 -0
  51. package/lib/host/health.js +178 -0
  52. package/lib/host/http.js +65 -0
  53. package/lib/host/impact.js +138 -0
  54. package/lib/host/injection-owners.js +60 -0
  55. package/lib/host/layers.js +132 -0
  56. package/lib/host/masks.js +145 -0
  57. package/lib/host/modes.js +96 -0
  58. package/lib/host/names.js +44 -0
  59. package/lib/host/patch-config.js +554 -0
  60. package/lib/host/patch-scan.js +103 -0
  61. package/lib/host/preview-conditions.js +281 -0
  62. package/lib/host/preview-contexts.js +174 -0
  63. package/lib/host/preview-envelope.js +31 -0
  64. package/lib/host/preview-instructions.js +450 -0
  65. package/lib/host/preview-messages.js +284 -0
  66. package/lib/host/preview-modules.js +233 -0
  67. package/lib/host/preview-skills.js +200 -0
  68. package/lib/host/prompt.js +213 -0
  69. package/lib/host/recipe-overlay.js +209 -0
  70. package/lib/host/request-preview.js +114 -0
  71. package/lib/host/routes/activation.js +96 -0
  72. package/lib/host/routes/activity.js +22 -0
  73. package/lib/host/routes/apply.js +93 -0
  74. package/lib/host/routes/cleanup.js +62 -0
  75. package/lib/host/routes/control.js +22 -0
  76. package/lib/host/routes/disable-preview.js +19 -0
  77. package/lib/host/routes/impact.js +19 -0
  78. package/lib/host/routes/operations.js +22 -0
  79. package/lib/host/routes/preview.js +58 -0
  80. package/lib/host/routes/session-influence.js +23 -0
  81. package/lib/host/routes/snapshot.js +21 -0
  82. package/lib/host/routes/template.js +54 -0
  83. package/lib/host/routes/templates.js +28 -0
  84. package/lib/host/scope-impact.js +125 -0
  85. package/lib/host/session-events.js +39 -0
  86. package/lib/host/session-influence.js +467 -0
  87. package/lib/host/sessions.js +293 -0
  88. package/lib/host/snapshot.js +218 -0
  89. package/lib/host/source-scan.js +225 -0
  90. package/lib/host/templates/cleanup.js +219 -0
  91. package/lib/host/templates/inheritance.js +144 -0
  92. package/lib/host/templates/materialize.js +317 -0
  93. package/lib/host/templates/message-overlay.js +184 -0
  94. package/lib/host/templates/repository.js +323 -0
  95. package/lib/host/templates/store.js +131 -0
  96. package/lib/host/tool-set.js +150 -0
  97. package/lib/host/tools.js +129 -0
  98. package/lib/index.js +106 -0
  99. package/locale/en.json +6 -0
  100. package/locale/zh.json +6 -0
  101. package/package.json +124 -6
@@ -0,0 +1,554 @@
1
+ /**
2
+ * patch 文件的 config 子树扫描与有限 YAML 解析。
3
+ *
4
+ * 为什么不直接用完整 YAML:本插件零依赖,而这里要回答的问题很窄——
5
+ * 「某一层给这一行声明了哪些 config 键、值是什么」。因此只实现 DSH 组合
6
+ * 文件实际出现的块式写法;遇到块标量、锚点、跨行流式值这类不支持的构造,
7
+ * 保留原文并标记 `parsed:false`,**不猜一个看起来合理的值**。
8
+ *
9
+ * 与 `patch-scan.js` 的分工:那边只回答「被谁、改了哪些顶层键」,是快照的
10
+ * 轻量事实;这里回答「改成了什么」,只在禁用预演按需读取一个目标行。
11
+ * 两套扫描器独立演进,`verify-patch-config.mjs` 用同一份样例断言 id、类型与
12
+ * 顶层键名一致,防止语义悄悄分叉。
13
+ * @module dsh-plugin-console/host/patch-config
14
+ */
15
+
16
+ /** 单个 patch 文件最多扫描的行数;超出只保留已读到的行。 */
17
+ const MAX_LINES = 2000
18
+ /** 单个标量的展示上限;超出截断并给行标 truncated。 */
19
+ const MAX_VALUE_TEXT = 2000
20
+ /** config 的最大嵌套深度。 */
21
+ const MAX_DEPTH = 8
22
+ /** 单个 config 子树的节点上限。 */
23
+ const MAX_NODES = 2000
24
+
25
+ /** 解析失败:patch 里出现了本页刻意不支持的写法。 */
26
+ class PatchParseError extends Error {}
27
+
28
+ /**
29
+ * 去掉行尾注释。
30
+ *
31
+ * 只在引号外、且 `#` 前面是空白时截断——`url: a#b` 里的 `#` 是值的一部分。
32
+ * @param {string} raw - 原始行。
33
+ * @returns {string} 去掉注释后的行。
34
+ */
35
+ function stripComment(raw) {
36
+ let quote = null
37
+ for (let index = 0; index < raw.length; index++) {
38
+ const char = raw[index]
39
+ if (quote !== null) {
40
+ if (char === '\\' && quote === '"') {
41
+ index++
42
+ continue
43
+ }
44
+ if (char === quote) quote = null
45
+ continue
46
+ }
47
+ if (char === '"' || char === "'") {
48
+ quote = char
49
+ continue
50
+ }
51
+ if (char === '#' && (index === 0 || /\s/.test(raw[index - 1]))) return raw.slice(0, index)
52
+ }
53
+ return raw
54
+ }
55
+
56
+ /** 去掉键两端的引号;只处理 patch 里常见的写法。 */
57
+ function unquoteKey(text) {
58
+ const trimmed = text.trim()
59
+ if (trimmed.length >= 2 && trimmed.startsWith('"') && trimmed.endsWith('"')) {
60
+ return trimmed.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\')
61
+ }
62
+ if (trimmed.length >= 2 && trimmed.startsWith("'") && trimmed.endsWith("'")) {
63
+ return trimmed.slice(1, -1).replace(/''/g, "'")
64
+ }
65
+ return trimmed
66
+ }
67
+
68
+ /**
69
+ * 切出 `key: value`。
70
+ *
71
+ * 分隔符是引号外的第一个 `:`,且它后面必须是空白或行尾——否则
72
+ * `url: https://x` 会被从协议名处切开。
73
+ * @param {string} text - 已去掉注释的正文(不含列表前缀)。
74
+ * @returns {{key: string, rest: string} | null} 键与剩余文本。
75
+ */
76
+ function splitKey(text) {
77
+ let quote = null
78
+ for (let index = 0; index < text.length; index++) {
79
+ const char = text[index]
80
+ if (quote !== null) {
81
+ if (char === '\\' && quote === '"') {
82
+ index++
83
+ continue
84
+ }
85
+ if (char === quote) quote = null
86
+ continue
87
+ }
88
+ if (char === '"' || char === "'") {
89
+ quote = char
90
+ continue
91
+ }
92
+ if (char !== ':') continue
93
+ const rest = text.slice(index + 1)
94
+ if (rest !== '' && !/^\s/.test(rest)) continue
95
+ const key = unquoteKey(text.slice(0, index))
96
+ if (key === '') return null
97
+ return { key, rest: rest.trim() }
98
+ }
99
+ return null
100
+ }
101
+
102
+ /**
103
+ * 解析一个标量文本。
104
+ *
105
+ * 引号、布尔、null 与数字按 YAML 的常见语义;其余原样当字符串。
106
+ * @param {string} text - 标量文本。
107
+ * @returns {unknown} 解析结果。
108
+ */
109
+ function parseScalarToken(text) {
110
+ const value = text.trim()
111
+ if (value === '' ) return null
112
+ if (value.startsWith('"')) {
113
+ if (!value.endsWith('"') || value.length < 2) throw new PatchParseError('双引号未闭合')
114
+ return value.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\')
115
+ }
116
+ if (value.startsWith("'")) {
117
+ if (!value.endsWith("'") || value.length < 2) throw new PatchParseError('单引号未闭合')
118
+ return value.slice(1, -1).replace(/''/g, "'")
119
+ }
120
+ if (/^(null|~)$/i.test(value)) return null
121
+ if (/^true$/i.test(value)) return true
122
+ if (/^false$/i.test(value)) return false
123
+ if (/^[+-]?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?$/.test(value)) return Number(value)
124
+ return value
125
+ }
126
+
127
+ /**
128
+ * 解析流式值 `[a, b]` / `{a: b}`。
129
+ *
130
+ * 只支持单行、可嵌套的写法;跨行流式值属于不支持的构造。
131
+ * @param {string} text - 以 `[` 或 `{` 开头的文本。
132
+ * @param {number} line - 行号,用于错误信息。
133
+ * @returns {unknown} 解析结果。
134
+ */
135
+ function parseFlow(text, line) {
136
+ let index = 0
137
+ const fail = (why) => {
138
+ throw new PatchParseError(`第 ${line} 行的流式值无法解析:${why}`)
139
+ }
140
+ const skip = () => {
141
+ while (index < text.length && /\s/.test(text[index])) index++
142
+ }
143
+
144
+ /** 读一个带引号的字符串。 */
145
+ const readQuoted = () => {
146
+ const quote = text[index]
147
+ let out = ''
148
+ index++
149
+ while (index < text.length) {
150
+ const char = text[index]
151
+ if (char === '\\' && quote === '"') {
152
+ out += text[index + 1] ?? ''
153
+ index += 2
154
+ continue
155
+ }
156
+ if (char === quote) {
157
+ if (quote === "'" && text[index + 1] === "'") {
158
+ out += "'"
159
+ index += 2
160
+ continue
161
+ }
162
+ index++
163
+ return out
164
+ }
165
+ out += char
166
+ index++
167
+ }
168
+ fail('引号未闭合')
169
+ return ''
170
+ }
171
+
172
+ /** 流式 map 的键:允许引号,也允许到 `:` 为止的裸键。 */
173
+ const readKey = () => {
174
+ skip()
175
+ if (text[index] === '"' || text[index] === "'") return readQuoted()
176
+ const start = index
177
+ while (index < text.length && text[index] !== ':') index++
178
+ const key = text.slice(start, index).trim()
179
+ if (key === '') fail('缺少键名')
180
+ return key
181
+ }
182
+
183
+ const readNode = (depth) => {
184
+ if (depth > MAX_DEPTH) fail('嵌套过深')
185
+ skip()
186
+ const char = text[index]
187
+ if (char === '[') {
188
+ index++
189
+ const list = []
190
+ skip()
191
+ if (text[index] === ']') {
192
+ index++
193
+ return list
194
+ }
195
+ while (true) {
196
+ list.push(readNode(depth + 1))
197
+ skip()
198
+ if (text[index] === ',') {
199
+ index++
200
+ skip()
201
+ if (text[index] === ']') {
202
+ index++
203
+ return list
204
+ }
205
+ continue
206
+ }
207
+ if (text[index] === ']') {
208
+ index++
209
+ return list
210
+ }
211
+ fail('缺少 ]')
212
+ }
213
+ }
214
+ if (char === '{') {
215
+ index++
216
+ const map = {}
217
+ skip()
218
+ if (text[index] === '}') {
219
+ index++
220
+ return map
221
+ }
222
+ while (true) {
223
+ const key = readKey()
224
+ skip()
225
+ if (text[index] !== ':') fail('缺少 :')
226
+ index++
227
+ map[key] = readNode(depth + 1)
228
+ skip()
229
+ if (text[index] === ',') {
230
+ index++
231
+ skip()
232
+ if (text[index] === '}') {
233
+ index++
234
+ return map
235
+ }
236
+ continue
237
+ }
238
+ if (text[index] === '}') {
239
+ index++
240
+ return map
241
+ }
242
+ fail('缺少 }')
243
+ }
244
+ }
245
+ if (char === '"' || char === "'") return readQuoted()
246
+ const start = index
247
+ while (index < text.length && !',]}'.includes(text[index])) index++
248
+ return parseScalarToken(text.slice(start, index))
249
+ }
250
+
251
+ const value = readNode(1)
252
+ skip()
253
+ if (index !== text.length) fail('值后面还有多余内容')
254
+ return value
255
+ }
256
+
257
+ /**
258
+ * 解析一个值文本;不支持的构造直接抛错,由调用方整行降级。
259
+ * @param {string} text - 值文本。
260
+ * @param {number} line - 行号。
261
+ * @param {object} state - 截断/节点计数状态。
262
+ * @returns {unknown} 解析结果。
263
+ */
264
+ function parseValueText(text, line, state) {
265
+ const value = text.trim()
266
+ if (value === '') return null
267
+ if (/^[|>]/.test(value)) throw new PatchParseError(`第 ${line} 行使用了块标量,本页只保留原文`)
268
+ if (/^[&*!]/.test(value)) throw new PatchParseError(`第 ${line} 行使用了锚点、别名或标签,本页只保留原文`)
269
+ if (value.startsWith('[') || value.startsWith('{')) {
270
+ state.nodes++
271
+ if (state.nodes > MAX_NODES) throw new PatchParseError('config 节点过多,已截断')
272
+ return parseFlow(value, line)
273
+ }
274
+ return clipScalar(parseScalarToken(value), state)
275
+ }
276
+
277
+ /** 超长标量截断;只影响展示与比较,解析结果仍然是字符串。 */
278
+ function clipScalar(value, state) {
279
+ if (typeof value !== 'string' || value.length <= MAX_VALUE_TEXT) return value
280
+ state.truncated = true
281
+ return `${value.slice(0, MAX_VALUE_TEXT)}…(已截断,共 ${value.length} 字符)`
282
+ }
283
+
284
+ /**
285
+ * 解析一块缩进文本。
286
+ * @param {Array<{indent: number, text: string, line: number}>} lines - 块内行。
287
+ * @param {number} start - 起始下标。
288
+ * @param {number} indent - 该块的首行缩进。
289
+ * @param {number} depth - 当前深度。
290
+ * @param {object} state - 截断/节点计数状态。
291
+ * @returns {[unknown, number]} 解析值与该块之后的下标。
292
+ */
293
+ function parseBlock(lines, start, indent, depth, state) {
294
+ if (depth > MAX_DEPTH) throw new PatchParseError('config 嵌套过深')
295
+ const first = lines[start]
296
+ if (first.text === '-' || first.text.startsWith('- ')) {
297
+ return parseSequence(lines, start, indent, depth, state)
298
+ }
299
+ return parseMapping(lines, start, indent, depth, state)
300
+ }
301
+
302
+ /** 解析一个块式 map。 */
303
+ function parseMapping(lines, start, indent, depth, state) {
304
+ const map = {}
305
+ let index = start
306
+ while (index < lines.length) {
307
+ const line = lines[index]
308
+ if (line.indent < indent) break
309
+ if (line.indent > indent) throw new PatchParseError(`第 ${line.line} 行的缩进无法归入上一级`)
310
+ if (line.text === '-' || line.text.startsWith('- ')) break
311
+ const kv = splitKey(line.text)
312
+ if (kv === null) throw new PatchParseError(`第 ${line.line} 行不是键值对`)
313
+ state.nodes++
314
+ if (state.nodes > MAX_NODES) throw new PatchParseError('config 节点过多,已截断')
315
+ if (kv.rest === '') {
316
+ if (index + 1 < lines.length && lines[index + 1].indent > indent) {
317
+ const [child, next] = parseBlock(lines, index + 1, lines[index + 1].indent, depth + 1, state)
318
+ map[kv.key] = child
319
+ index = next
320
+ } else {
321
+ map[kv.key] = null
322
+ index++
323
+ }
324
+ continue
325
+ }
326
+ map[kv.key] = parseValueText(kv.rest, line.line, state)
327
+ index++
328
+ if (index < lines.length && lines[index].indent > indent) {
329
+ throw new PatchParseError(`第 ${lines[index].line} 行:标量后面还有子行`)
330
+ }
331
+ }
332
+ return [map, index]
333
+ }
334
+
335
+ /** 解析一个块式序列;序列项可以是标量或 map。 */
336
+ function parseSequence(lines, start, indent, depth, state) {
337
+ const list = []
338
+ let index = start
339
+ while (index < lines.length) {
340
+ const line = lines[index]
341
+ if (line.indent < indent) break
342
+ if (line.indent > indent) throw new PatchParseError(`第 ${line.line} 行的缩进无法归入上一级`)
343
+ if (!(line.text === '-' || line.text.startsWith('- '))) break
344
+ state.nodes++
345
+ if (state.nodes > MAX_NODES) throw new PatchParseError('config 节点过多,已截断')
346
+ const body = line.text === '-' ? '' : line.text.slice(2).trim()
347
+ if (body === '') {
348
+ if (index + 1 < lines.length && lines[index + 1].indent > indent) {
349
+ const [child, next] = parseBlock(lines, index + 1, lines[index + 1].indent, depth + 1, state)
350
+ list.push(child)
351
+ index = next
352
+ } else {
353
+ list.push(null)
354
+ index++
355
+ }
356
+ continue
357
+ }
358
+ const kv = splitKey(body)
359
+ if (kv === null) {
360
+ list.push(parseValueText(body, line.line, state))
361
+ index++
362
+ continue
363
+ }
364
+ const item = {}
365
+ index++
366
+ if (kv.rest === '') {
367
+ if (index < lines.length && lines[index].indent > indent) {
368
+ const [child, next] = parseBlock(lines, index, lines[index].indent, depth + 1, state)
369
+ item[kv.key] = child
370
+ index = next
371
+ } else {
372
+ item[kv.key] = null
373
+ }
374
+ } else {
375
+ item[kv.key] = parseValueText(kv.rest, line.line, state)
376
+ }
377
+ if (index < lines.length && lines[index].indent > indent) {
378
+ const [rest, next] = parseMapping(lines, index, lines[index].indent, depth + 1, state)
379
+ Object.assign(item, rest)
380
+ index = next
381
+ }
382
+ list.push(item)
383
+ }
384
+ return [list, index]
385
+ }
386
+
387
+ /** 从 config 块里按首层缩进抓键名;解析失败时用它回答「声明了哪些键」。 */
388
+ function topLevelKeys(lines, indent) {
389
+ const keys = []
390
+ for (const line of lines) {
391
+ if (line.indent !== indent) continue
392
+ const kv = splitKey(line.text)
393
+ if (kv !== null && !keys.includes(kv.key)) keys.push(kv.key)
394
+ }
395
+ return keys
396
+ }
397
+
398
+ /**
399
+ * 扫描一个 patch 文件的 config 子树。
400
+ *
401
+ * 每个 `- id:` 出现一次就是一条记录;同一 id 在一个文件里出现多次时保留
402
+ * 各自的 occurrence,不做合并——合并是「生效值」的事,覆盖链需要看到每一层。
403
+ * @param {string} text - patch 文件内容。
404
+ * @returns {{rows: Array<object>, diagnostics: string[]}} config 行与文件级诊断。
405
+ */
406
+ export function scanPatchConfig(text) {
407
+ const rawLines = String(text).split(/\r?\n/)
408
+ const diagnostics = []
409
+ if (rawLines.length > MAX_LINES) {
410
+ diagnostics.push(`文件超过 ${MAX_LINES} 行,只扫描前 ${MAX_LINES} 行`)
411
+ }
412
+ const lines = rawLines.slice(0, MAX_LINES)
413
+ /** @type {Array<object>} */
414
+ const rows = []
415
+ const occurrences = new Map()
416
+ let inserting = false
417
+ let current = null
418
+
419
+ /** 开始一条新行记录。 */
420
+ const startRow = (id, kind, line) => {
421
+ const occurrence = (occurrences.get(id) ?? 0) + 1
422
+ occurrences.set(id, occurrence)
423
+ const row = {
424
+ id,
425
+ kind,
426
+ line,
427
+ occurrence,
428
+ keys: [],
429
+ hasConfig: false,
430
+ configKeys: [],
431
+ configRaw: '',
432
+ parsed: true,
433
+ truncated: false,
434
+ }
435
+ rows.push(row)
436
+ return { row, keyIndent: null }
437
+ }
438
+
439
+ for (let index = 0; index < lines.length; index++) {
440
+ const raw = lines[index]
441
+ if (/^\s*\t/.test(raw)) diagnostics.push(`第 ${index + 1} 行含 tab 缩进;本页按原文逐行识别,可能漏键`)
442
+ const cleaned = stripComment(raw)
443
+ if (!cleaned.trim()) continue
444
+ const indent = cleaned.length - cleaned.trimStart().length
445
+ const body = cleaned.trim()
446
+
447
+ if (indent === 0) {
448
+ current = null
449
+ if (/^-?\s*insert\s*:/.test(body)) {
450
+ inserting = true
451
+ continue
452
+ }
453
+ const match = body.match(/^-\s*id\s*:\s*(.+)$/)
454
+ if (match !== null) {
455
+ inserting = false
456
+ current = startRow(unquoteKey(match[1]), 'override', index + 1)
457
+ current.keyIndent = indent + 2
458
+ }
459
+ continue
460
+ }
461
+
462
+ const idMatch = body.match(/^-\s*id\s*:\s*(.+)$/)
463
+ if (idMatch !== null && inserting) {
464
+ current = startRow(unquoteKey(idMatch[1]), 'insert', index + 1)
465
+ current.keyIndent = indent + 2
466
+ continue
467
+ }
468
+ if (idMatch !== null) {
469
+ current = null
470
+ continue
471
+ }
472
+ if (current === null) continue
473
+ current.keyIndent ??= indent
474
+ if (indent < current.keyIndent) {
475
+ current = null
476
+ continue
477
+ }
478
+ if (indent !== current.keyIndent) continue
479
+
480
+ const kv = splitKey(body)
481
+ if (kv === null || kv.key === 'id') continue
482
+ if (!current.row.keys.includes(kv.key)) current.row.keys.push(kv.key)
483
+ if (kv.key !== 'config') continue
484
+
485
+ current.row.hasConfig = true
486
+ // `config: {...}` 与 `config:` 两种写法都要认;后者收集子块。
487
+ if (kv.rest !== '') {
488
+ current.row.configRaw = kv.rest
489
+ const state = { nodes: 0, truncated: false }
490
+ try {
491
+ current.row.config = parseValueText(kv.rest, index + 1, state)
492
+ current.row.configKeys = current.row.config !== null && typeof current.row.config === 'object' && !Array.isArray(current.row.config)
493
+ ? Object.keys(current.row.config)
494
+ : []
495
+ current.row.truncated = state.truncated
496
+ } catch (cause) {
497
+ current.row.parsed = false
498
+ current.row.reason = cause instanceof Error ? cause.message : String(cause)
499
+ }
500
+ continue
501
+ }
502
+
503
+ const blockLines = []
504
+ const rawBlock = []
505
+ let next = index + 1
506
+ for (; next < lines.length; next++) {
507
+ const nested = stripComment(lines[next])
508
+ if (!nested.trim()) {
509
+ rawBlock.push(lines[next])
510
+ continue
511
+ }
512
+ const nestedIndent = nested.length - nested.trimStart().length
513
+ if (nestedIndent <= current.keyIndent) break
514
+ blockLines.push({ indent: nestedIndent, text: nested.trim(), line: next + 1 })
515
+ rawBlock.push(lines[next])
516
+ }
517
+ index = next - 1
518
+ current.row.configRaw = rawBlock.join('\n').trim()
519
+ if (blockLines.length === 0) {
520
+ current.row.config = {}
521
+ current.row.configKeys = []
522
+ current.row.emptyConfig = true
523
+ continue
524
+ }
525
+ const state = { nodes: 0, truncated: false }
526
+ const firstIndent = blockLines[0].indent
527
+ try {
528
+ const [config, after] = parseBlock(blockLines, 0, firstIndent, 1, state)
529
+ if (after < blockLines.length) throw new PatchParseError(`第 ${blockLines[after].line} 行无法归入 config 子树`)
530
+ current.row.config = config
531
+ current.row.configKeys = config !== null && typeof config === 'object' && !Array.isArray(config) ? Object.keys(config) : []
532
+ current.row.truncated = state.truncated
533
+ } catch (cause) {
534
+ current.row.parsed = false
535
+ current.row.reason = cause instanceof Error ? cause.message : String(cause)
536
+ current.row.configKeys = topLevelKeys(blockLines, firstIndent)
537
+ }
538
+ }
539
+
540
+ return { rows, diagnostics }
541
+ }
542
+
543
+ /**
544
+ * 解析一个独立的 YAML 值文本。
545
+ *
546
+ * 只给测试与流式值使用;不承诺完整 YAML 语义,不支持的构造会抛错。
547
+ * @param {string} text - 值文本。
548
+ * @returns {unknown} 解析结果。
549
+ */
550
+ export function parseYamlValue(text) {
551
+ const cleaned = stripComment(String(text)).trim()
552
+ if (cleaned === '') return null
553
+ return parseValueText(cleaned, 0, { nodes: 0, truncated: false })
554
+ }
@@ -0,0 +1,103 @@
1
+ /**
2
+ * profile / home 的 `cordis.patch.yml` 扫描。
3
+ *
4
+ * 完整的 YAML 解析在这里没有收益:patch 的语义只有两种(`insert` 与按
5
+ * `id` 覆盖),而界面需要回答的是「这一行被谁、改了哪些顶层键」。所以
6
+ * 这里只按缩进识别块式 patch,并记下每个 id 的顶层键名;流式 insert
7
+ * 与嵌套 group 不求全——漏报只影响键的展示,不影响覆盖关系本身。
8
+ * @module dsh-plugin-console/host/patch-scan
9
+ */
10
+
11
+ /**
12
+ * 扫描一个 patch 文件,分别给出插入行与覆盖行。
13
+ *
14
+ * 识别的缩进形状(DSH 组合文件实际使用的块式写法):
15
+ * - insert: ← 顶层,进入插入模式
16
+ * - id: added ← 插入行,缩进 > 0
17
+ * name: pkg ← 该行的顶层键
18
+ * - id: existing ← 顶层,覆盖行
19
+ * config: ← 该行的顶层键(嵌套内容不展开)
20
+ *
21
+ * 同一个 id 在一个文件里出现多次时,键取并集,行号取首次出现的位置。
22
+ * @param {string} text - patch 文件内容。
23
+ * @returns {{inserted: Map<string, {keys: string[], line: number}>, overridden: Map<string, {keys: string[], line: number}>}}
24
+ * 插入行与覆盖行;值是该行声明的顶层键与起始行号。
25
+ */
26
+ export function scanPatchRows(text) {
27
+ const inserted = new Map()
28
+ const overridden = new Map()
29
+ let inserting = false
30
+ /** @type {{entry: {keys: string[], line: number}, keyIndent: number} | null} */
31
+ let current = null
32
+ let lineNo = 0
33
+
34
+ /** 同一个 id 重复出现时复用同一条记录,键去重后合并。 */
35
+ const record = (rows, id, line) => {
36
+ let entry = rows.get(id)
37
+ if (entry === undefined) {
38
+ entry = { keys: [], line }
39
+ rows.set(id, entry)
40
+ }
41
+ return entry
42
+ }
43
+
44
+ for (const rawLine of text.split(/\r?\n/)) {
45
+ lineNo++
46
+ // 注释只在行尾截断;引号内的 # 目前不会出现在 id 或键名上。
47
+ const line = rawLine.replace(/#.*$/, '')
48
+ if (!line.trim()) continue
49
+ const indent = line.length - line.trimStart().length
50
+ const body = line.trim()
51
+
52
+ // 顶层的每个新条目都会结束上一个 id 的作用域。
53
+ if (indent === 0) {
54
+ current = null
55
+ inserting = false
56
+ if (/^-\s*insert\s*:/.test(body)) {
57
+ inserting = true
58
+ continue
59
+ }
60
+ const idLine = body.match(/^-\s*id\s*:\s*(.+)$/)
61
+ if (idLine === null) continue
62
+ const id = cleanId(idLine[1])
63
+ if (!id) continue
64
+ current = { entry: record(overridden, id, lineNo), keyIndent: 2 }
65
+ continue
66
+ }
67
+
68
+ const idLine = body.match(/^-\s*id\s*:\s*(.+)$/)
69
+ if (idLine !== null) {
70
+ const id = cleanId(idLine[1])
71
+ current = inserting && id ? { entry: record(inserted, id, lineNo), keyIndent: indent + 2 } : null
72
+ continue
73
+ }
74
+
75
+ if (current === null) continue
76
+ // 缩进回退说明这个行条目结束;顶层键只认它自己那一层。
77
+ if (indent < current.keyIndent) {
78
+ current = null
79
+ continue
80
+ }
81
+ if (indent !== current.keyIndent) continue
82
+ const keyLine = body.match(/^([A-Za-z_][\w-]*)\s*:/)
83
+ if (keyLine === null || keyLine[1] === 'id') continue
84
+ if (!current.entry.keys.includes(keyLine[1])) current.entry.keys.push(keyLine[1])
85
+ }
86
+
87
+ return { inserted, overridden }
88
+ }
89
+
90
+ /**
91
+ * 只要 id 集合的兼容入口。
92
+ * @param {string} text - patch 文件内容。
93
+ * @returns {{inserted: Set<string>, overridden: Set<string>}} 两种 id 集合。
94
+ */
95
+ export function scanPatchIds(text) {
96
+ const { inserted, overridden } = scanPatchRows(text)
97
+ return { inserted: new Set(inserted.keys()), overridden: new Set(overridden.keys()) }
98
+ }
99
+
100
+ /** 去掉 id 两端的引号,其余字符原样保留。 */
101
+ function cleanId(raw) {
102
+ return raw.trim().replace(/^['"]|['"]$/g, '')
103
+ }