dsh-agent-control-center 0.1.0 → 0.3.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/CHANGELOG.md +116 -0
- package/README.md +217 -83
- package/README.zh.md +159 -58
- package/client.js +7660 -2005
- package/cordis.patch.yml +5 -0
- package/docs/01-CONTRACT.md +3 -2
- package/docs/05-ACCEPTANCE-EVIDENCE.md +81 -0
- package/docs/06-UX-OPTIMIZATION.md +57 -0
- package/docs/07-GUIDE.md +327 -0
- package/docs/08-COMPATIBILITY.md +202 -0
- package/docs/09-ARCHITECTURE.md +333 -0
- package/docs/ROADMAP.md +448 -0
- package/index.js +67 -2
- package/lib/core/cache.js +277 -0
- package/lib/core/compat.js +877 -0
- package/lib/core/errors.js +14 -1
- package/lib/core/http.js +22 -0
- package/lib/core/index.js +53 -2
- package/lib/core/paths.js +6 -0
- package/lib/core/registry.js +21 -5
- package/lib/core/util.js +4 -1
- package/lib/modules/a-agents.js +104 -4
- package/lib/modules/b-tools.js +169 -26
- package/lib/modules/f-diagnostics.js +109 -17
- package/lib/modules/g-templates.js +18 -0
- package/lib/modules/i-recovery.js +84 -23
- package/lib/modules/j-instruction.js +624 -0
- package/lib/modules/k-export.js +540 -0
- package/package.json +1 -1
- package/scripts/release.mjs +81 -0
- package/scripts/verify.mjs +12 -0
- package/src/client/app.js +684 -118
- package/src/client/i18n/agents.js +96 -0
- package/src/client/i18n/common.js +36 -0
- package/src/client/i18n/diagnostics.js +52 -0
- package/src/client/i18n/export.js +174 -0
- package/src/client/i18n/instruction.js +236 -0
- package/src/client/i18n/meta.js +72 -0
- package/src/client/i18n/overview.js +38 -0
- package/src/client/i18n/recovery.js +66 -0
- package/src/client/i18n/sessions.js +64 -0
- package/src/client/i18n/templates.js +88 -0
- package/src/client/i18n/tools.js +96 -0
- package/src/client/i18n/usage.js +42 -0
- package/src/client/kernel.js +327 -40
- package/src/client/pages/agents.js +637 -354
- package/src/client/pages/diagnostics.js +526 -177
- package/src/client/pages/export.js +664 -0
- package/src/client/pages/instruction.js +648 -0
- package/src/client/pages/meta.js +553 -193
- package/src/client/pages/overview.js +255 -34
- package/src/client/pages/recovery.js +332 -131
- package/src/client/pages/sessions.js +470 -307
- package/src/client/pages/templates.js +691 -381
- package/src/client/pages/tools.js +559 -206
- package/src/client/pages/usage.js +235 -57
|
@@ -0,0 +1,877 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Host compatibility layer. One verdict decides how far the plugin may go on
|
|
3
|
+
* the host it is running on: the DSH version (parsed, compared, bounded by the
|
|
4
|
+
* supported and the measured windows), the capabilities every feature module
|
|
5
|
+
* needs, and an explicit read-only fallback when the version cannot be proven.
|
|
6
|
+
*
|
|
7
|
+
* The layer never throws on a host fact: an unparsable version, an absent
|
|
8
|
+
* service, or a missing manifest each degrade the verdict with a readable
|
|
9
|
+
* reason. `evaluate` is pure and cheap, so `/health`, diagnostics, and the
|
|
10
|
+
* write gate can all reuse the same answer instead of agreeing separately.
|
|
11
|
+
*
|
|
12
|
+
* @module dsh-agent-control-center/core/compat
|
|
13
|
+
*/
|
|
14
|
+
import { readFile } from 'node:fs/promises'
|
|
15
|
+
import { createRequire } from 'node:module'
|
|
16
|
+
import path from 'node:path'
|
|
17
|
+
|
|
18
|
+
/* ------------------------------------------------------------------------- */
|
|
19
|
+
/* Version windows */
|
|
20
|
+
/* ------------------------------------------------------------------------- */
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Lowest DSH version the compatibility layer reasons about. It is deliberately
|
|
24
|
+
* lower than the declared `engines.dsh` floor (DECLARED_MIN): an installation
|
|
25
|
+
* that bypassed the host's own requirement check (version exemption, source
|
|
26
|
+
* run) still gets a verdict instead of an assumption.
|
|
27
|
+
*/
|
|
28
|
+
export const MIN_SUPPORTED = '0.2.0'
|
|
29
|
+
|
|
30
|
+
/** DSH versions the plugin has actually been exercised against. */
|
|
31
|
+
export const TESTED = Object.freeze(['0.2.2-rc.5'])
|
|
32
|
+
|
|
33
|
+
/** Highest measured DSH version; versions above it run degraded, never blocked. */
|
|
34
|
+
export const MAX_TESTED = TESTED.reduce((highest, version) =>
|
|
35
|
+
compareVersions(version, highest) > 0 ? version : highest,
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
/** Lower bound declared in `package.json` (`dsh.compatibility.dsh` / `engines.dsh`). */
|
|
39
|
+
export const DECLARED_MIN = '0.2.1-rc.1'
|
|
40
|
+
|
|
41
|
+
/** Exclusive upper bound declared in `package.json`; this major-line is unmeasured. */
|
|
42
|
+
export const DECLARED_MAX = '0.3.0'
|
|
43
|
+
|
|
44
|
+
/** Capabilities reported by {@link createCaps}; one namespace, no method table here. */
|
|
45
|
+
export const CAPABILITY_NAMES = Object.freeze([
|
|
46
|
+
'fs',
|
|
47
|
+
'sessionQuery',
|
|
48
|
+
'sessionPersistence',
|
|
49
|
+
'sessionProjections',
|
|
50
|
+
'workspaceRegistry',
|
|
51
|
+
'workspaceController',
|
|
52
|
+
'workspaceFiles',
|
|
53
|
+
'tools',
|
|
54
|
+
'settings',
|
|
55
|
+
'credentials',
|
|
56
|
+
'configEditor',
|
|
57
|
+
'agentPresets',
|
|
58
|
+
'agents',
|
|
59
|
+
'tokenMeter',
|
|
60
|
+
'workingDirectory',
|
|
61
|
+
'pluginManager',
|
|
62
|
+
'storage',
|
|
63
|
+
'webServer',
|
|
64
|
+
'connection',
|
|
65
|
+
'commands',
|
|
66
|
+
'systemPrompt',
|
|
67
|
+
])
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* @typedef {object} ModuleRequirement
|
|
71
|
+
* @property {ReadonlyArray<string>} required Capabilities the module uses.
|
|
72
|
+
* @property {ReadonlyArray<string>} blocking Subset of `required`; absence blocks the module.
|
|
73
|
+
* @property {ReadonlyArray<string>} hostServices Host services the module reads but `caps` does not cover.
|
|
74
|
+
* @property {string} minVersion Lowest DSH version where the requirement set is known to hold.
|
|
75
|
+
* @property {string} note Why the split between blocking and degradation-only exists.
|
|
76
|
+
*/
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Build one frozen requirement record.
|
|
80
|
+
* @param {{
|
|
81
|
+
* required: string[],
|
|
82
|
+
* blocking: string[],
|
|
83
|
+
* hostServices?: string[],
|
|
84
|
+
* minVersion?: string,
|
|
85
|
+
* note: string,
|
|
86
|
+
* }} spec
|
|
87
|
+
* @returns {ModuleRequirement}
|
|
88
|
+
*/
|
|
89
|
+
function requirement(spec) {
|
|
90
|
+
return Object.freeze({
|
|
91
|
+
required: Object.freeze(spec.required),
|
|
92
|
+
blocking: Object.freeze(spec.blocking),
|
|
93
|
+
hostServices: Object.freeze(spec.hostServices ?? []),
|
|
94
|
+
minVersion: spec.minVersion ?? MIN_SUPPORTED,
|
|
95
|
+
note: spec.note,
|
|
96
|
+
})
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Per-module host dependency matrix. `required` is what the module needs to be
|
|
101
|
+
* fully useful; `blocking` is the subset whose absence removes the module's
|
|
102
|
+
* core function. A requirement outside `blocking` still degrades the module,
|
|
103
|
+
* matching how each module already handles the service being absent.
|
|
104
|
+
* Versions are uniforms at {@link MIN_SUPPORTED}: no earlier host was measured,
|
|
105
|
+
* so a higher per-module floor would be an invented fact.
|
|
106
|
+
*/
|
|
107
|
+
export const MODULE_REQUIREMENTS = Object.freeze({
|
|
108
|
+
agents: requirement({
|
|
109
|
+
required: ['fs', 'sessionQuery'],
|
|
110
|
+
blocking: ['fs'],
|
|
111
|
+
hostServices: ['sandboxPolicy'],
|
|
112
|
+
note: '写入 AGENTS.md 走宿主 fs 与会话 sandboxPolicy;缺 sessionQuery 只失去按会话派生文件',
|
|
113
|
+
}),
|
|
114
|
+
tools: requirement({
|
|
115
|
+
required: ['tools', 'sessionQuery'],
|
|
116
|
+
blocking: ['tools'],
|
|
117
|
+
note: '工具清单来自 tools.definitions;sessionQuery 只用于用量佐证',
|
|
118
|
+
}),
|
|
119
|
+
sessions: requirement({
|
|
120
|
+
required: ['sessionQuery', 'workspaceRegistry'],
|
|
121
|
+
blocking: ['sessionQuery'],
|
|
122
|
+
note: '归档只读 workspaceRegistry.archivedSessionIds,缺失时归档按钮降级',
|
|
123
|
+
}),
|
|
124
|
+
usage: requirement({
|
|
125
|
+
required: ['sessionQuery', 'sessionPersistence'],
|
|
126
|
+
blocking: ['sessionQuery'],
|
|
127
|
+
note: '实时事件只覆盖本次运行,缺 sessionPersistence 时历史回放与重算降级',
|
|
128
|
+
}),
|
|
129
|
+
meta: requirement({
|
|
130
|
+
required: ['agents', 'agentPresets'],
|
|
131
|
+
blocking: ['agents'],
|
|
132
|
+
note: '缺 agentPresets 时只剩活跃 agent 信息',
|
|
133
|
+
}),
|
|
134
|
+
diagnostics: requirement({
|
|
135
|
+
required: ['fs', 'settings'],
|
|
136
|
+
blocking: [],
|
|
137
|
+
note: '诊断本身是自证模块:任何缺失只减少检查项,不阻断',
|
|
138
|
+
}),
|
|
139
|
+
templates: requirement({
|
|
140
|
+
required: ['fs'],
|
|
141
|
+
blocking: [],
|
|
142
|
+
hostServices: ['sandboxPolicy'],
|
|
143
|
+
note: '模板应用复用 AGENTS.md 保存流程;无 sandboxPolicy 时只读预览',
|
|
144
|
+
}),
|
|
145
|
+
overview: requirement({
|
|
146
|
+
required: [],
|
|
147
|
+
blocking: [],
|
|
148
|
+
note: '总览是纯聚合视图,依赖各模块自身状态',
|
|
149
|
+
}),
|
|
150
|
+
recovery: requirement({
|
|
151
|
+
required: ['sessionQuery', 'sessionPersistence'],
|
|
152
|
+
blocking: ['sessionQuery'],
|
|
153
|
+
note: '中断扫描需要 sessionQuery;无 sessionPersistence 时只看本次运行',
|
|
154
|
+
}),
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
/* ------------------------------------------------------------------------- */
|
|
158
|
+
/* Semantic versions (the subset a DSH version string uses) */
|
|
159
|
+
/* ------------------------------------------------------------------------- */
|
|
160
|
+
|
|
161
|
+
const VERSION_PATTERN =
|
|
162
|
+
/^[vV]?(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?$/
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* @typedef {object} ParsedVersion
|
|
166
|
+
* @property {number} major
|
|
167
|
+
* @property {number} minor
|
|
168
|
+
* @property {number} patch
|
|
169
|
+
* @property {ReadonlyArray<string | number>} prerelease Empty for a release.
|
|
170
|
+
* @property {ReadonlyArray<string>} build Ignored by comparison, kept for display.
|
|
171
|
+
* @property {string} version Canonical `major.minor.patch[-prerelease]`, build dropped.
|
|
172
|
+
* @property {string} raw The trimmed input.
|
|
173
|
+
*/
|
|
174
|
+
|
|
175
|
+
/** @param {string} text @returns {boolean} */
|
|
176
|
+
function hasLeadingZero(text) {
|
|
177
|
+
return text.length > 1 && text.startsWith('0')
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Parse a DSH version string. Accepts a leading `v`, ignores build metadata,
|
|
182
|
+
* and rejects anything outside a strict `major.minor.patch[-prerelease]` form
|
|
183
|
+
* (missing segments, leading zeros, empty identifiers).
|
|
184
|
+
* @param {unknown} text
|
|
185
|
+
* @returns {ParsedVersion | null} `null` when the text is not a version.
|
|
186
|
+
*/
|
|
187
|
+
export function parseVersion(text) {
|
|
188
|
+
if (typeof text !== 'string') return null
|
|
189
|
+
const raw = text.trim()
|
|
190
|
+
const match = VERSION_PATTERN.exec(raw)
|
|
191
|
+
if (match === null) return null
|
|
192
|
+
const [, major, minor, patch, prereleaseText, buildText] = match
|
|
193
|
+
if (hasLeadingZero(major) || hasLeadingZero(minor) || hasLeadingZero(patch)) return null
|
|
194
|
+
const prerelease = prereleaseText === undefined ? [] : prereleaseText.split('.')
|
|
195
|
+
for (const identifier of prerelease) {
|
|
196
|
+
if (hasLeadingZero(identifier)) return null
|
|
197
|
+
}
|
|
198
|
+
const parsedPrerelease = Object.freeze(
|
|
199
|
+
prerelease.map((identifier) => (/^\d+$/.test(identifier) ? Number(identifier) : identifier)),
|
|
200
|
+
)
|
|
201
|
+
const canonical = `${Number(major)}.${Number(minor)}.${Number(patch)}`
|
|
202
|
+
return Object.freeze({
|
|
203
|
+
major: Number(major),
|
|
204
|
+
minor: Number(minor),
|
|
205
|
+
patch: Number(patch),
|
|
206
|
+
prerelease: parsedPrerelease,
|
|
207
|
+
build: Object.freeze(buildText === undefined ? [] : buildText.split('.')),
|
|
208
|
+
version: parsedPrerelease.length === 0 ? canonical : `${canonical}-${parsedPrerelease.join('.')}`,
|
|
209
|
+
raw,
|
|
210
|
+
})
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Coerce a version input into a parsed record. Objects are re-parsed through
|
|
215
|
+
* their `version` text rather than trusted field by field.
|
|
216
|
+
* @param {unknown} value
|
|
217
|
+
* @returns {ParsedVersion | null}
|
|
218
|
+
*/
|
|
219
|
+
function coerceVersion(value) {
|
|
220
|
+
if (typeof value === 'string') return parseVersion(value)
|
|
221
|
+
if (value !== null && typeof value === 'object') {
|
|
222
|
+
const embedded = /** @type {{ version?: unknown }} */ (value).version
|
|
223
|
+
if (typeof embedded === 'string') return parseVersion(embedded)
|
|
224
|
+
}
|
|
225
|
+
return null
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Compare two versions with semver precedence: numeric fields first, then
|
|
230
|
+
* prerelease identifiers (numeric before alphanumeric, shorter prefix first),
|
|
231
|
+
* with build metadata ignored. Accepts strings and {@link ParsedVersion}
|
|
232
|
+
* records.
|
|
233
|
+
* @param {unknown} a
|
|
234
|
+
* @param {unknown} b
|
|
235
|
+
* @returns {-1 | 0 | 1 | null} `null` when either side cannot be parsed
|
|
236
|
+
* (incomparable); a caller must treat that as "unknown", never as "older".
|
|
237
|
+
*/
|
|
238
|
+
export function compareVersions(a, b) {
|
|
239
|
+
const left = coerceVersion(a)
|
|
240
|
+
const right = coerceVersion(b)
|
|
241
|
+
if (left === null || right === null) return null
|
|
242
|
+
for (const field of /** @type {const} */ (['major', 'minor', 'patch'])) {
|
|
243
|
+
if (left[field] !== right[field]) return left[field] < right[field] ? -1 : 1
|
|
244
|
+
}
|
|
245
|
+
if (left.prerelease.length === 0 && right.prerelease.length === 0) return 0
|
|
246
|
+
if (left.prerelease.length === 0) return 1
|
|
247
|
+
if (right.prerelease.length === 0) return -1
|
|
248
|
+
const length = Math.max(left.prerelease.length, right.prerelease.length)
|
|
249
|
+
for (let index = 0; index < length; index += 1) {
|
|
250
|
+
const leftId = left.prerelease[index]
|
|
251
|
+
const rightId = right.prerelease[index]
|
|
252
|
+
if (leftId === undefined) return -1
|
|
253
|
+
if (rightId === undefined) return 1
|
|
254
|
+
if (leftId === rightId) continue
|
|
255
|
+
const leftNumeric = typeof leftId === 'number'
|
|
256
|
+
const rightNumeric = typeof rightId === 'number'
|
|
257
|
+
if (leftNumeric && rightNumeric) return leftId < rightId ? -1 : 1
|
|
258
|
+
if (leftNumeric) return -1
|
|
259
|
+
if (rightNumeric) return 1
|
|
260
|
+
return /** @type {string} */ (leftId) < /** @type {string} */ (rightId) ? -1 : 1
|
|
261
|
+
}
|
|
262
|
+
return 0
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/* ------------------------------------------------------------------------- */
|
|
266
|
+
/* Capability normalization */
|
|
267
|
+
/* ------------------------------------------------------------------------- */
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* @typedef {object} CapabilityEntry
|
|
271
|
+
* @property {boolean} present
|
|
272
|
+
* @property {string[]} methods Methods found by the probe.
|
|
273
|
+
* @property {string} [detail] Probe diagnostic, e.g. the list of missing methods.
|
|
274
|
+
*/
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Recognize one capability value from any of the representations in use:
|
|
278
|
+
* `{present, methods, detail}` (the caps table), a boolean, an array of method
|
|
279
|
+
* names, `{available}` (module-level probes), or a bare service value.
|
|
280
|
+
* @param {unknown} value
|
|
281
|
+
* @returns {CapabilityEntry}
|
|
282
|
+
*/
|
|
283
|
+
function describeCapability(value) {
|
|
284
|
+
if (value === null || value === undefined) return { present: false, methods: [] }
|
|
285
|
+
if (typeof value === 'boolean') return { present: value, methods: [] }
|
|
286
|
+
if (typeof value === 'function') return { present: true, methods: [] }
|
|
287
|
+
if (Array.isArray(value)) {
|
|
288
|
+
return { present: value.length > 0, methods: value.filter((entry) => typeof entry === 'string') }
|
|
289
|
+
}
|
|
290
|
+
if (typeof value === 'object') {
|
|
291
|
+
const candidate = /** @type {Record<string, unknown>} */ (value)
|
|
292
|
+
const methods = Array.isArray(candidate.methods)
|
|
293
|
+
? candidate.methods.filter((entry) => typeof entry === 'string')
|
|
294
|
+
: []
|
|
295
|
+
if (typeof candidate.present === 'boolean') {
|
|
296
|
+
return {
|
|
297
|
+
present: candidate.present,
|
|
298
|
+
methods,
|
|
299
|
+
...(typeof candidate.detail === 'string' ? { detail: candidate.detail } : {}),
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
if (typeof candidate.available === 'boolean') return { present: candidate.available, methods }
|
|
303
|
+
return { present: true, methods }
|
|
304
|
+
}
|
|
305
|
+
return { present: false, methods: [] }
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Normalize a capability source into `{ name: {present, methods} }`. Accepted
|
|
310
|
+
* sources: the caps table snapshot, a caps instance (anything with `get`), an
|
|
311
|
+
* array of present names, a `Map`, or a plain object of any value accepted by
|
|
312
|
+
* {@link describeCapability}. Entries not reported stay absent; names outside
|
|
313
|
+
* {@link CAPABILITY_NAMES} are kept so module-level host services can ride
|
|
314
|
+
* along.
|
|
315
|
+
* @param {unknown} input
|
|
316
|
+
* @returns {Record<string, CapabilityEntry>}
|
|
317
|
+
*/
|
|
318
|
+
export function normalizeCapabilities(input) {
|
|
319
|
+
/** @type {Record<string, CapabilityEntry>} */
|
|
320
|
+
const out = {}
|
|
321
|
+
if (input === null || input === undefined) return out
|
|
322
|
+
if (typeof input === 'object' && typeof (/** @type {{ get?: unknown }} */ (input).get) === 'function') {
|
|
323
|
+
const source = /** @type {{ get(name: string): unknown }} */ (input)
|
|
324
|
+
for (const name of CAPABILITY_NAMES) {
|
|
325
|
+
let value
|
|
326
|
+
try {
|
|
327
|
+
value = source.get(name)
|
|
328
|
+
} catch {
|
|
329
|
+
value = undefined
|
|
330
|
+
}
|
|
331
|
+
out[name] = describeCapability(value)
|
|
332
|
+
}
|
|
333
|
+
return out
|
|
334
|
+
}
|
|
335
|
+
if (Array.isArray(input)) {
|
|
336
|
+
for (const name of input) {
|
|
337
|
+
if (typeof name === 'string') out[name] = { present: true, methods: [] }
|
|
338
|
+
}
|
|
339
|
+
return out
|
|
340
|
+
}
|
|
341
|
+
if (input instanceof Map) {
|
|
342
|
+
for (const [name, value] of input) {
|
|
343
|
+
if (typeof name === 'string') out[name] = describeCapability(value)
|
|
344
|
+
}
|
|
345
|
+
return out
|
|
346
|
+
}
|
|
347
|
+
if (typeof input === 'object') {
|
|
348
|
+
for (const [name, value] of Object.entries(input)) out[name] = describeCapability(value)
|
|
349
|
+
}
|
|
350
|
+
return out
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Check one host service without touching `caps` (which knows only its own
|
|
355
|
+
* table). An absent or throwing service is reported absent, never thrown.
|
|
356
|
+
* @param {{ get?: (name: string) => unknown } | undefined} ctx
|
|
357
|
+
* @param {string} name
|
|
358
|
+
* @returns {boolean}
|
|
359
|
+
*/
|
|
360
|
+
export function hasHostService(ctx, name) {
|
|
361
|
+
if (ctx === undefined || ctx === null || typeof ctx.get !== 'function') return false
|
|
362
|
+
try {
|
|
363
|
+
const value = ctx.get(name)
|
|
364
|
+
return value !== undefined && value !== null
|
|
365
|
+
} catch {
|
|
366
|
+
return false
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/**
|
|
371
|
+
* Probe every host service the matrix names but `caps` does not cover (today:
|
|
372
|
+
* `sandboxPolicy`). Callers pass the result to {@link evaluate} as
|
|
373
|
+
* `hostServices`.
|
|
374
|
+
* @param {{ get?: (name: string) => unknown } | undefined} ctx
|
|
375
|
+
* @param {Readonly<Record<string, ModuleRequirement>>} [requirements]
|
|
376
|
+
* @returns {Record<string, boolean>}
|
|
377
|
+
*/
|
|
378
|
+
export function probeHostServices(ctx, requirements = MODULE_REQUIREMENTS) {
|
|
379
|
+
/** @type {Record<string, boolean>} */
|
|
380
|
+
const out = {}
|
|
381
|
+
for (const spec of Object.values(requirements)) {
|
|
382
|
+
for (const name of spec.hostServices) out[name] = hasHostService(ctx, name)
|
|
383
|
+
}
|
|
384
|
+
return out
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/* ------------------------------------------------------------------------- */
|
|
388
|
+
/* Verdict */
|
|
389
|
+
/* ------------------------------------------------------------------------- */
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* @typedef {object} Reason
|
|
393
|
+
* @property {string} code Stable machine code (`version-unknown`, `modules-blocked`, …).
|
|
394
|
+
* @property {'info' | 'warn' | 'error'} severity
|
|
395
|
+
* @property {string} text Readable, user-facing explanation.
|
|
396
|
+
*/
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* @typedef {object} ModuleVerdict
|
|
400
|
+
* @property {string} id Module id, as in the plugin manifest.
|
|
401
|
+
* @property {'full' | 'degraded' | 'blocked'} state
|
|
402
|
+
* @property {{ required: ReadonlyArray<string>, blocking: ReadonlyArray<string> }} requirement
|
|
403
|
+
* @property {string[]} missing Required capabilities the host did not report.
|
|
404
|
+
* @property {string[]} missingBlocking Blocking capabilities among `missing`.
|
|
405
|
+
* @property {string[]} missingHostServices Host services reported absent.
|
|
406
|
+
* @property {string} minVersion
|
|
407
|
+
* @property {boolean | null} versionOk `false` when the host version is below the module floor; `null` when unknown.
|
|
408
|
+
*/
|
|
409
|
+
|
|
410
|
+
/**
|
|
411
|
+
* Read a version argument: a string, a `{version, source}` record (as returned
|
|
412
|
+
* by {@link detectVersion}), a {@link ParsedVersion}, or nothing.
|
|
413
|
+
* @param {unknown} value
|
|
414
|
+
* @returns {{ text: string | null, source: string | null }}
|
|
415
|
+
*/
|
|
416
|
+
function readVersionArgument(value) {
|
|
417
|
+
if (value === null || value === undefined) return { text: null, source: null }
|
|
418
|
+
if (typeof value === 'string') return { text: value, source: null }
|
|
419
|
+
if (typeof value === 'object') {
|
|
420
|
+
const candidate = /** @type {{ version?: unknown, source?: unknown }} */ (value)
|
|
421
|
+
return {
|
|
422
|
+
text: typeof candidate.version === 'string' ? candidate.version : null,
|
|
423
|
+
source: typeof candidate.source === 'string' ? candidate.source : null,
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
return { text: null, source: null }
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* @typedef {object} Verdict
|
|
431
|
+
* @property {'full' | 'degraded' | 'unsupported'} level
|
|
432
|
+
* @property {string | null} version Canonical version, or the raw unparsable text.
|
|
433
|
+
* @property {string | null} versionSource Source name from {@link detectVersion}.
|
|
434
|
+
* @property {boolean} versionKnown
|
|
435
|
+
* @property {boolean} tested True only for a version in {@link TESTED}.
|
|
436
|
+
* @property {boolean} readOnly Withhold writes: the version is unproven or out of the declared range.
|
|
437
|
+
* @property {ModuleVerdict[]} modules One entry per module in the matrix order.
|
|
438
|
+
* @property {Reason[]} reasons Empty only for a full verdict.
|
|
439
|
+
* @property {{ total: number, present: string[], missing: string[] }} capabilities
|
|
440
|
+
* @property {{ minSupported: string, maxTested: string, declaredMin: string, declaredMax: string, tested: ReadonlyArray<string> }} limits
|
|
441
|
+
*/
|
|
442
|
+
|
|
443
|
+
/**
|
|
444
|
+
* Evaluate the host against the compatibility matrix. Pure and cheap: every
|
|
445
|
+
* call re-reads the capability answer, so boot order and late-mounted services
|
|
446
|
+
* are handled by re-invoking it from `/health`.
|
|
447
|
+
* @param {{
|
|
448
|
+
* version?: unknown,
|
|
449
|
+
* versionSource?: string,
|
|
450
|
+
* capabilities?: unknown,
|
|
451
|
+
* hostServices?: Record<string, boolean>,
|
|
452
|
+
* requirements?: Readonly<Record<string, ModuleRequirement>>,
|
|
453
|
+
* }} [options]
|
|
454
|
+
* @returns {Verdict}
|
|
455
|
+
*/
|
|
456
|
+
export function evaluate(options = {}) {
|
|
457
|
+
const requirements = options.requirements ?? MODULE_REQUIREMENTS
|
|
458
|
+
const versionArgument = readVersionArgument(options.version)
|
|
459
|
+
const parsed = coerceVersion(versionArgument.text)
|
|
460
|
+
const versionKnown = parsed !== null
|
|
461
|
+
const versionText = parsed !== null ? parsed.version : versionArgument.text
|
|
462
|
+
const source = typeof options.versionSource === 'string' ? options.versionSource : versionArgument.source
|
|
463
|
+
const capabilities = normalizeCapabilities(options.capabilities)
|
|
464
|
+
const hostServices = options.hostServices ?? {}
|
|
465
|
+
/** @type {Reason[]} */
|
|
466
|
+
const reasons = []
|
|
467
|
+
/** @param {string} code @param {Reason['severity']} severity @param {string} text */
|
|
468
|
+
const addReason = (code, severity, text) => {
|
|
469
|
+
reasons.push({ code, severity, text })
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
const presentCapabilities = CAPABILITY_NAMES.filter((name) => capabilities[name]?.present === true)
|
|
473
|
+
const missingCapabilities = CAPABILITY_NAMES.filter((name) => capabilities[name]?.present !== true)
|
|
474
|
+
const tested = versionKnown && TESTED.some((version) => compareVersions(parsed, version) === 0)
|
|
475
|
+
|
|
476
|
+
let readOnly = false
|
|
477
|
+
let belowMinimum = false
|
|
478
|
+
if (!versionKnown) {
|
|
479
|
+
readOnly = true
|
|
480
|
+
addReason(
|
|
481
|
+
'version-unknown',
|
|
482
|
+
'warn',
|
|
483
|
+
versionText === null
|
|
484
|
+
? '宿主 DSH 版本未知:按只读降级运行,写入类操作默认关闭'
|
|
485
|
+
: `无法解析宿主版本「${versionText}」:按只读降级运行,写入类操作默认关闭`,
|
|
486
|
+
)
|
|
487
|
+
} else if (compareVersions(parsed, MIN_SUPPORTED) < 0) {
|
|
488
|
+
belowMinimum = true
|
|
489
|
+
readOnly = true
|
|
490
|
+
addReason('version-below-minimum', 'error', `宿主版本 ${parsed.version} 低于最低支持版本 ${MIN_SUPPORTED}:插件不可用`)
|
|
491
|
+
} else {
|
|
492
|
+
if (compareVersions(parsed, DECLARED_MIN) < 0) {
|
|
493
|
+
addReason(
|
|
494
|
+
'version-below-declared',
|
|
495
|
+
'warn',
|
|
496
|
+
`宿主版本 ${parsed.version} 低于声明下限 ${DECLARED_MIN}(仅经由宿主版本豁免时才可能出现)`,
|
|
497
|
+
)
|
|
498
|
+
}
|
|
499
|
+
if (compareVersions(parsed, DECLARED_MAX) >= 0) {
|
|
500
|
+
readOnly = true
|
|
501
|
+
addReason(
|
|
502
|
+
'version-above-declared',
|
|
503
|
+
'warn',
|
|
504
|
+
`宿主版本 ${parsed.version} 超出声明的支持范围(< ${DECLARED_MAX}):按只读降级运行,请先确认上游 API 变化`,
|
|
505
|
+
)
|
|
506
|
+
} else if (compareVersions(parsed, MAX_TESTED) > 0) {
|
|
507
|
+
addReason(
|
|
508
|
+
'version-above-tested',
|
|
509
|
+
'warn',
|
|
510
|
+
`宿主版本 ${parsed.version} 高于实测上限 ${MAX_TESTED}:按降级运行,写入前请先运行诊断`,
|
|
511
|
+
)
|
|
512
|
+
} else if (!tested) {
|
|
513
|
+
addReason(
|
|
514
|
+
'version-untested',
|
|
515
|
+
'info',
|
|
516
|
+
`宿主版本 ${parsed.version} 在支持范围内但未实测(实测:${TESTED.join('、')}):按降级运行`,
|
|
517
|
+
)
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
if (presentCapabilities.length === 0) {
|
|
522
|
+
addReason('capabilities-empty', 'error', '宿主未提供任何已探测能力:插件整体不可用(只读)')
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
/** @type {ModuleVerdict[]} */
|
|
526
|
+
const modules = []
|
|
527
|
+
for (const [id, spec] of Object.entries(requirements)) {
|
|
528
|
+
const missing = spec.required.filter((name) => capabilities[name]?.present !== true)
|
|
529
|
+
const missingBlocking = spec.blocking.filter((name) => capabilities[name]?.present !== true)
|
|
530
|
+
const missingHostServices = spec.hostServices.filter((name) => hostServices[name] === false)
|
|
531
|
+
const versionOk = versionKnown ? compareVersions(parsed, spec.minVersion) >= 0 : null
|
|
532
|
+
const state =
|
|
533
|
+
versionOk === false || missingBlocking.length > 0
|
|
534
|
+
? 'blocked'
|
|
535
|
+
: missing.length > 0 || missingHostServices.length > 0
|
|
536
|
+
? 'degraded'
|
|
537
|
+
: 'full'
|
|
538
|
+
modules.push({
|
|
539
|
+
id,
|
|
540
|
+
state,
|
|
541
|
+
requirement: { required: spec.required, blocking: spec.blocking },
|
|
542
|
+
missing,
|
|
543
|
+
missingBlocking,
|
|
544
|
+
missingHostServices,
|
|
545
|
+
minVersion: spec.minVersion,
|
|
546
|
+
versionOk,
|
|
547
|
+
})
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
const blocked = modules.filter((module) => module.state === 'blocked')
|
|
551
|
+
const degraded = modules.filter((module) => module.state === 'degraded')
|
|
552
|
+
if (blocked.length > 0) {
|
|
553
|
+
const causes = blocked.map((module) =>
|
|
554
|
+
module.missingBlocking.length > 0
|
|
555
|
+
? `${module.id}(缺少 ${module.missingBlocking.join('、')})`
|
|
556
|
+
: `${module.id}(版本 < ${module.minVersion})`,
|
|
557
|
+
)
|
|
558
|
+
addReason('modules-blocked', 'warn', `模块不可用:${causes.join(';')}`)
|
|
559
|
+
}
|
|
560
|
+
if (degraded.length > 0) {
|
|
561
|
+
addReason('modules-degraded', 'info', `模块降级:${degraded.map((module) => module.id).join('、')}`)
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
/** @type {Verdict['level']} */
|
|
565
|
+
let level
|
|
566
|
+
if (belowMinimum) level = 'unsupported'
|
|
567
|
+
else if (presentCapabilities.length === 0) level = 'unsupported'
|
|
568
|
+
else if (modules.length > 0 && blocked.length === modules.length) level = 'unsupported'
|
|
569
|
+
else if (!tested || readOnly || blocked.length > 0 || degraded.length > 0) level = 'degraded'
|
|
570
|
+
else level = 'full'
|
|
571
|
+
if (level === 'unsupported') readOnly = true
|
|
572
|
+
|
|
573
|
+
return {
|
|
574
|
+
level,
|
|
575
|
+
version: versionText,
|
|
576
|
+
versionSource: source,
|
|
577
|
+
versionKnown,
|
|
578
|
+
tested,
|
|
579
|
+
readOnly,
|
|
580
|
+
modules,
|
|
581
|
+
reasons,
|
|
582
|
+
capabilities: {
|
|
583
|
+
total: CAPABILITY_NAMES.length,
|
|
584
|
+
present: presentCapabilities,
|
|
585
|
+
missing: missingCapabilities,
|
|
586
|
+
},
|
|
587
|
+
limits: {
|
|
588
|
+
minSupported: MIN_SUPPORTED,
|
|
589
|
+
maxTested: MAX_TESTED,
|
|
590
|
+
declaredMin: DECLARED_MIN,
|
|
591
|
+
declaredMax: DECLARED_MAX,
|
|
592
|
+
tested: TESTED,
|
|
593
|
+
},
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
/* ------------------------------------------------------------------------- */
|
|
598
|
+
/* Version detection */
|
|
599
|
+
/* ------------------------------------------------------------------------- */
|
|
600
|
+
|
|
601
|
+
/** Where {@link detectVersion} found the running version, in probe order. */
|
|
602
|
+
export const VERSION_SOURCE = Object.freeze({
|
|
603
|
+
/** `ctx.get('profileContext').installAnchor` — the running DSH package manifest. */
|
|
604
|
+
PROFILE_CONTEXT: 'profileContext',
|
|
605
|
+
/** `process.argv[1]` resolved to the `@deepseek-ai/dsh` package manifest. */
|
|
606
|
+
PROCESS_ENTRY: 'process-entry',
|
|
607
|
+
/** `DSH_INSTALL_DIR/package.json` or its bundled `node_modules/@deepseek-ai/dsh`. */
|
|
608
|
+
INSTALL_DIR: 'DSH_INSTALL_DIR',
|
|
609
|
+
/** Module-resolution of `@deepseek-ai/dsh/package.json` from this plugin. */
|
|
610
|
+
MODULE_RESOLUTION: 'module-resolution',
|
|
611
|
+
/** No source produced a version. */
|
|
612
|
+
UNKNOWN: 'unknown',
|
|
613
|
+
})
|
|
614
|
+
|
|
615
|
+
/**
|
|
616
|
+
* @typedef {object} VersionAttempt
|
|
617
|
+
* @property {string} source
|
|
618
|
+
* @property {string | null} path Candidate manifest path.
|
|
619
|
+
* @property {string | null} version Version read there, when valid.
|
|
620
|
+
* @property {string | null} error Why the candidate produced no version.
|
|
621
|
+
*/
|
|
622
|
+
|
|
623
|
+
/**
|
|
624
|
+
* @typedef {object} VersionDetection
|
|
625
|
+
* @property {string | null} version Manifest version at the first successful source.
|
|
626
|
+
* @property {string} source One of {@link VERSION_SOURCE}.
|
|
627
|
+
* @property {string | null} path Manifest the version came from.
|
|
628
|
+
* @property {VersionAttempt[]} attempts Every probe, in order, with its outcome.
|
|
629
|
+
* @property {string[]} warnings Human-readable notes for diagnostics.
|
|
630
|
+
*/
|
|
631
|
+
|
|
632
|
+
/** @param {unknown} error @returns {string} */
|
|
633
|
+
function errorText(error) {
|
|
634
|
+
const code = /** @type {{ code?: unknown, message?: unknown }} */ (error)?.code
|
|
635
|
+
if (typeof code === 'string') return code
|
|
636
|
+
const message = /** @type {{ message?: unknown }} */ (error)?.message
|
|
637
|
+
return typeof message === 'string' ? message.slice(0, 120) : String(error).slice(0, 120)
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
/** Package names that carry the running DSH version (CLI or a lockstep @deepseek-ai/dsh-*). */
|
|
641
|
+
const DSH_PACKAGE_NAME = /^(@deepseek-ai\/)?dsh($|[-/])/
|
|
642
|
+
|
|
643
|
+
/**
|
|
644
|
+
* Parse one manifest and return its version, optionally refusing a manifest
|
|
645
|
+
* whose `name` is not a DSH package (a heuristic path may point at an unrelated
|
|
646
|
+
* project root).
|
|
647
|
+
* @param {string} text
|
|
648
|
+
* @param {boolean} strictName
|
|
649
|
+
* @returns {{ version: string } | { error: string }}
|
|
650
|
+
*/
|
|
651
|
+
function readManifest(text, strictName) {
|
|
652
|
+
let parsed
|
|
653
|
+
try {
|
|
654
|
+
parsed = JSON.parse(text)
|
|
655
|
+
} catch {
|
|
656
|
+
return { error: '清单不是合法 JSON' }
|
|
657
|
+
}
|
|
658
|
+
if (parsed === null || typeof parsed !== 'object') return { error: '清单不是 JSON 对象' }
|
|
659
|
+
const manifest = /** @type {{ name?: unknown, version?: unknown }} */ (parsed)
|
|
660
|
+
if (strictName && !(typeof manifest.name === 'string' && DSH_PACKAGE_NAME.test(manifest.name))) {
|
|
661
|
+
return { error: `清单 name「${typeof manifest.name === 'string' ? manifest.name : '缺失'}」不是 DSH 包` }
|
|
662
|
+
}
|
|
663
|
+
if (typeof manifest.version !== 'string') return { error: '清单缺少 version 字段' }
|
|
664
|
+
if (parseVersion(manifest.version) === null) return { error: `清单 version「${manifest.version}」不是合法版本` }
|
|
665
|
+
return { version: manifest.version }
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
/**
|
|
669
|
+
* Resolve the running DSH version from the host, in probe order: the
|
|
670
|
+
* `profileContext` host service, the process entry (`argv[1]`), the
|
|
671
|
+
* `DSH_INSTALL_DIR` environment variable, then module resolution. Never
|
|
672
|
+
* throws: every failure becomes an entry in `attempts` and the caller gets
|
|
673
|
+
* `version: null` with `source: 'unknown'`.
|
|
674
|
+
* @param {{
|
|
675
|
+
* ctx?: { get?: (name: string) => unknown },
|
|
676
|
+
* env?: Record<string, string | undefined>,
|
|
677
|
+
* argv?: ReadonlyArray<string>,
|
|
678
|
+
* readText?: (target: string) => string | Promise<string>,
|
|
679
|
+
* }} [options]
|
|
680
|
+
* @returns {Promise<VersionDetection>}
|
|
681
|
+
*/
|
|
682
|
+
export async function detectVersion(options = {}) {
|
|
683
|
+
const ctx = options.ctx
|
|
684
|
+
const env = options.env ?? process.env
|
|
685
|
+
const argv = options.argv ?? process.argv
|
|
686
|
+
const readText = options.readText ?? (async (target) => readFile(target, 'utf8'))
|
|
687
|
+
/** @type {VersionAttempt[]} */
|
|
688
|
+
const attempts = []
|
|
689
|
+
/** @type {Array<{ source: string, path: string, strictName: boolean }>} */
|
|
690
|
+
const candidates = []
|
|
691
|
+
/** @param {string} source @param {string} path @param {boolean} strictName */
|
|
692
|
+
const addCandidate = (source, path, strictName) => {
|
|
693
|
+
if (candidates.some((candidate) => candidate.path === path)) return
|
|
694
|
+
candidates.push({ source, path, strictName })
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
let profileContext
|
|
698
|
+
try {
|
|
699
|
+
profileContext = typeof ctx?.get === 'function' ? ctx.get('profileContext') : undefined
|
|
700
|
+
} catch (error) {
|
|
701
|
+
attempts.push({ source: VERSION_SOURCE.PROFILE_CONTEXT, path: null, version: null, error: errorText(error) })
|
|
702
|
+
profileContext = undefined
|
|
703
|
+
}
|
|
704
|
+
const installAnchor = /** @type {{ installAnchor?: unknown }} */ (profileContext)?.installAnchor
|
|
705
|
+
if (typeof installAnchor === 'string' && installAnchor.length > 0) {
|
|
706
|
+
// The host hands over its own running manifest; no name heuristic applies.
|
|
707
|
+
addCandidate(VERSION_SOURCE.PROFILE_CONTEXT, installAnchor, false)
|
|
708
|
+
} else if (profileContext === undefined) {
|
|
709
|
+
attempts.push({
|
|
710
|
+
source: VERSION_SOURCE.PROFILE_CONTEXT,
|
|
711
|
+
path: null,
|
|
712
|
+
version: null,
|
|
713
|
+
error: typeof ctx?.get === 'function' ? '宿主未挂载 profileContext' : 'ctx 不可用',
|
|
714
|
+
})
|
|
715
|
+
} else {
|
|
716
|
+
attempts.push({
|
|
717
|
+
source: VERSION_SOURCE.PROFILE_CONTEXT,
|
|
718
|
+
path: null,
|
|
719
|
+
version: null,
|
|
720
|
+
error: 'profileContext 没有 installAnchor',
|
|
721
|
+
})
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
const entry = Array.isArray(argv) && typeof argv[1] === 'string' ? argv[1] : ''
|
|
725
|
+
if (entry.length > 0 && path.isAbsolute(entry)) {
|
|
726
|
+
const entryDir = path.dirname(entry)
|
|
727
|
+
addCandidate(VERSION_SOURCE.PROCESS_ENTRY, path.resolve(entryDir, '..', 'package.json'), true)
|
|
728
|
+
addCandidate(
|
|
729
|
+
VERSION_SOURCE.PROCESS_ENTRY,
|
|
730
|
+
path.resolve(entryDir, '..', 'node_modules', '@deepseek-ai', 'dsh', 'package.json'),
|
|
731
|
+
true,
|
|
732
|
+
)
|
|
733
|
+
} else {
|
|
734
|
+
attempts.push({
|
|
735
|
+
source: VERSION_SOURCE.PROCESS_ENTRY,
|
|
736
|
+
path: null,
|
|
737
|
+
version: null,
|
|
738
|
+
error: entry.length === 0 ? 'argv 没有可用的入口路径' : '入口路径不是绝对路径',
|
|
739
|
+
})
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
const installDir = typeof env.DSH_INSTALL_DIR === 'string' ? env.DSH_INSTALL_DIR : ''
|
|
743
|
+
if (installDir.length > 0) {
|
|
744
|
+
// The install root manifest may be a private manifest without a name or
|
|
745
|
+
// version; the bundled DSH package is the authoritative one.
|
|
746
|
+
addCandidate(VERSION_SOURCE.INSTALL_DIR, path.join(installDir, 'package.json'), false)
|
|
747
|
+
addCandidate(
|
|
748
|
+
VERSION_SOURCE.INSTALL_DIR,
|
|
749
|
+
path.join(installDir, 'node_modules', '@deepseek-ai', 'dsh', 'package.json'),
|
|
750
|
+
true,
|
|
751
|
+
)
|
|
752
|
+
} else {
|
|
753
|
+
attempts.push({
|
|
754
|
+
source: VERSION_SOURCE.INSTALL_DIR,
|
|
755
|
+
path: null,
|
|
756
|
+
version: null,
|
|
757
|
+
error: '环境变量 DSH_INSTALL_DIR 未设置',
|
|
758
|
+
})
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
try {
|
|
762
|
+
const require = createRequire(import.meta.url)
|
|
763
|
+
for (const specifier of ['@deepseek-ai/dsh/package.json', '@deepseek-ai/dsh-app-boot/package.json']) {
|
|
764
|
+
try {
|
|
765
|
+
addCandidate(VERSION_SOURCE.MODULE_RESOLUTION, require.resolve(specifier), false)
|
|
766
|
+
} catch (error) {
|
|
767
|
+
attempts.push({ source: VERSION_SOURCE.MODULE_RESOLUTION, path: specifier, version: null, error: errorText(error) })
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
} catch (error) {
|
|
771
|
+
attempts.push({ source: VERSION_SOURCE.MODULE_RESOLUTION, path: null, version: null, error: errorText(error) })
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
/** @type {{ version: string, source: string, path: string } | null} */
|
|
775
|
+
let found = null
|
|
776
|
+
for (const candidate of candidates) {
|
|
777
|
+
let text
|
|
778
|
+
try {
|
|
779
|
+
text = await readText(candidate.path)
|
|
780
|
+
} catch (error) {
|
|
781
|
+
attempts.push({ source: candidate.source, path: candidate.path, version: null, error: errorText(error) })
|
|
782
|
+
continue
|
|
783
|
+
}
|
|
784
|
+
const manifest = readManifest(String(text), candidate.strictName)
|
|
785
|
+
if ('error' in manifest) {
|
|
786
|
+
attempts.push({ source: candidate.source, path: candidate.path, version: null, error: manifest.error })
|
|
787
|
+
continue
|
|
788
|
+
}
|
|
789
|
+
attempts.push({ source: candidate.source, path: candidate.path, version: manifest.version, error: null })
|
|
790
|
+
found = { version: manifest.version, source: candidate.source, path: candidate.path }
|
|
791
|
+
break
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
/** Probe order, so a later unread candidate never reads as an earlier failure. */
|
|
795
|
+
const sourceOrder = Object.values(VERSION_SOURCE)
|
|
796
|
+
attempts.sort((left, right) => sourceOrder.indexOf(left.source) - sourceOrder.indexOf(right.source))
|
|
797
|
+
|
|
798
|
+
/** @type {string[]} */
|
|
799
|
+
const warnings = []
|
|
800
|
+
if (found === null) warnings.push('未找到可读的宿主版本清单:兼容层按未知版本(只读)处理')
|
|
801
|
+
return {
|
|
802
|
+
version: found === null ? null : found.version,
|
|
803
|
+
source: found === null ? VERSION_SOURCE.UNKNOWN : found.source,
|
|
804
|
+
path: found === null ? null : found.path,
|
|
805
|
+
attempts,
|
|
806
|
+
warnings,
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
/* ------------------------------------------------------------------------- */
|
|
811
|
+
/* Monitor */
|
|
812
|
+
/* ------------------------------------------------------------------------- */
|
|
813
|
+
|
|
814
|
+
/**
|
|
815
|
+
* Wire the layer into the plugin: detect the version once, then answer with a
|
|
816
|
+
* fresh {@link evaluate} on every read (capabilities mount after activation,
|
|
817
|
+
* so a verdict computed at boot must not stick).
|
|
818
|
+
* @param {{
|
|
819
|
+
* ctx?: { get?: (name: string) => unknown },
|
|
820
|
+
* caps: { snapshot?: () => unknown },
|
|
821
|
+
* requirements?: Readonly<Record<string, ModuleRequirement>>,
|
|
822
|
+
* log?: (message: string) => void,
|
|
823
|
+
* env?: Record<string, string | undefined>,
|
|
824
|
+
* argv?: ReadonlyArray<string>,
|
|
825
|
+
* readText?: (target: string) => string | Promise<string>,
|
|
826
|
+
* }} options
|
|
827
|
+
* @returns {{
|
|
828
|
+
* ensure: () => Promise<VersionDetection>,
|
|
829
|
+
* current: () => Verdict,
|
|
830
|
+
* }}
|
|
831
|
+
*/
|
|
832
|
+
export function createCompatibility(options) {
|
|
833
|
+
const { ctx, caps, requirements = MODULE_REQUIREMENTS } = options
|
|
834
|
+
const log = options.log ?? (() => {})
|
|
835
|
+
/** @type {VersionDetection | null} */
|
|
836
|
+
let detection = null
|
|
837
|
+
/** @type {Promise<VersionDetection> | null} */
|
|
838
|
+
let pending = null
|
|
839
|
+
|
|
840
|
+
async function ensure() {
|
|
841
|
+
if (detection !== null) return detection
|
|
842
|
+
if (pending === null) {
|
|
843
|
+
pending = detectVersion({ ctx, env: options.env, argv: options.argv, readText: options.readText })
|
|
844
|
+
.then((value) => {
|
|
845
|
+
detection = value
|
|
846
|
+
if (value.version === null) log('host version unknown: compatibility runs read-only')
|
|
847
|
+
else log(`host version ${value.version} (source: ${value.source})`)
|
|
848
|
+
return value
|
|
849
|
+
})
|
|
850
|
+
.catch((error) => {
|
|
851
|
+
detection = {
|
|
852
|
+
version: null,
|
|
853
|
+
source: VERSION_SOURCE.UNKNOWN,
|
|
854
|
+
path: null,
|
|
855
|
+
attempts: [],
|
|
856
|
+
warnings: [`版本探测失败:${errorText(error)}`],
|
|
857
|
+
}
|
|
858
|
+
log(`host version detection failed: ${errorText(error)}`)
|
|
859
|
+
return detection
|
|
860
|
+
})
|
|
861
|
+
}
|
|
862
|
+
return pending
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
function current() {
|
|
866
|
+
const capabilities = typeof caps?.snapshot === 'function' ? caps.snapshot() : caps
|
|
867
|
+
return evaluate({
|
|
868
|
+
version: detection === null ? null : detection,
|
|
869
|
+
versionSource: detection === null ? undefined : detection.source,
|
|
870
|
+
capabilities,
|
|
871
|
+
hostServices: probeHostServices(ctx, requirements),
|
|
872
|
+
requirements,
|
|
873
|
+
})
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
return { ensure, current }
|
|
877
|
+
}
|