@dsh-plugin/dsh-pack-plugin 0.0.0-stage → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +178 -3
- package/cordis.patch.yml +9 -0
- package/icons/folder-zip-line.svg +1 -0
- package/lib/ball.js +597 -0
- package/lib/client.js +2934 -0
- package/lib/host.js +51745 -0
- package/lib/migrate-helper.js +35346 -0
- package/lib/progress-main.cjs +79 -0
- package/lib/progress-window.ps1 +114 -0
- package/lib/progress.html +126 -0
- package/lib/publish-to-github/SKILL.md +132 -0
- package/package.json +77 -4
- package/src/ball-entry.js +5 -0
- package/src/ball-host.js +99 -0
- package/src/ball-settings.js +352 -0
- package/src/ball.js +418 -0
- package/src/channel.js +12 -0
- package/src/client-plugin.js +58 -0
- package/src/client-rpc.js +14 -0
- package/src/client.js +5 -0
- package/src/core/discovery.js +192 -0
- package/src/core/dspack.js +93 -0
- package/src/core/extension-files.js +102 -0
- package/src/core/extension-install.js +295 -0
- package/src/core/extension-patch.js +369 -0
- package/src/core/extension-skills.js +164 -0
- package/src/core/extension-validate.js +127 -0
- package/src/core/extensions.js +207 -0
- package/src/core/home-store.js +32 -0
- package/src/core/host.js +149 -0
- package/src/core/index.js +59 -0
- package/src/core/inspect.js +162 -0
- package/src/core/install.js +775 -0
- package/src/core/managed-install.js +84 -0
- package/src/core/manifest.js +717 -0
- package/src/core/market.js +268 -0
- package/src/core/mcp-composition.js +189 -0
- package/src/core/mcp-install.js +112 -0
- package/src/core/mcp-transport.js +116 -0
- package/src/core/pack.js +371 -0
- package/src/core/plugin-metadata.js +355 -0
- package/src/core/registry.js +316 -0
- package/src/core/repo.js +339 -0
- package/src/core/scan.js +78 -0
- package/src/core/security.js +101 -0
- package/src/core/special.js +50 -0
- package/src/core/tar.js +103 -0
- package/src/core/update-config.js +234 -0
- package/src/core/update.js +792 -0
- package/src/core/vendored.js +715 -0
- package/src/core/workspace.js +95 -0
- package/src/endpoints.js +455 -0
- package/src/ensure-manager.js +186 -0
- package/src/host-node.js +625 -0
- package/src/host.js +17 -0
- package/src/index.d.ts +7 -0
- package/src/index.js +39 -0
- package/src/junction.js +142 -0
- package/src/migrate-helper.js +225 -0
- package/src/migrate.js +56 -0
- package/src/packaged.js +9 -0
- package/src/profiles.js +106 -0
- package/src/progress-main.cjs +79 -0
- package/src/progress-window.ps1 +114 -0
- package/src/progress.html +126 -0
- package/src/progress.js +132 -0
- package/src/rpc.js +157 -0
- package/src/runtime.js +43 -0
- package/src/section-gate.js +50 -0
- package/src/settings-nav-icon.js +188 -0
- package/src/settings.js +1611 -0
- package/src/skills/index.js +90 -0
- package/src/skills/publish-to-github/SKILL.md +132 -0
- package/src/tasks.js +145 -0
- package/src/v6-dialogs.js +312 -0
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 市场索引读取(宿主无关:Electron GUI 与 DSH 客户端插件共用)。
|
|
3
|
+
* - 兼容精简索引(schemaVersion 2,`modpacks[]`):条目只含指针 + 展示元数据 + `id`;
|
|
4
|
+
* - 完整 manifest + README 存于 `packs/<owner>.<repo>/`,用 `fetchMarketPackDetail` 懒加载;
|
|
5
|
+
* - 兼容旧式单 `downloadUrl`+`sha256`+`size` 与 `files[]` 指针式;
|
|
6
|
+
* - 从下载地址/版本号自动判别 `.dspack`(v4/v5) 与 `.tgz`(v3) 旧格式。
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { normalizeLaunchers } from './manifest.js';
|
|
10
|
+
|
|
11
|
+
/** 默认市场索引:官方 GitHub Pages 站点(CI 每日刷新扫描 dsh-pack 标签仓库)。 */
|
|
12
|
+
export const DEFAULT_MARKET_INDEX = 'https://dsh-packforge.github.io/dsh-pack-market/index.json';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* 启动器注册表机器可读版本(specs/launcher-registry.md 头部记载,schemaVersion 1)。
|
|
16
|
+
* 第三方(如 DSHL)可直接引用;本插件用于「兼容性」编辑器渲染认领 ID + 显示名。
|
|
17
|
+
*/
|
|
18
|
+
export const DEFAULT_LAUNCHERS_REGISTRY_URL = 'https://dsh-packforge.github.io/dsh-pack-market/launchers.json';
|
|
19
|
+
|
|
20
|
+
/** 内置回落清单(拉取失败 / 结构非法时用;与 specs/launcher-registry.md §1 表同步维护)。 */
|
|
21
|
+
export const BUILTIN_LAUNCHERS = [
|
|
22
|
+
{ id: 'dshl', name: 'DSHL · DeepSeek Harness Launcher' },
|
|
23
|
+
{ id: 'hdsl', name: 'HDSL · Hello DeepSeek Launcher' },
|
|
24
|
+
{ id: 'dsh-packforge-app', name: 'DSH PackForge GUI / dspack CLI' },
|
|
25
|
+
{ id: 'official-desktop', name: 'DeepSeek Harness 官方桌面端' },
|
|
26
|
+
{ id: 'dsh-cli', name: '裸 dsh 命令行' },
|
|
27
|
+
];
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* 解析 launchers.json(schemaVersion 1):`launchers[]: {id, name, url?, support?, desc?}` →
|
|
31
|
+
* `[{id, name}]`。结构非法 / 无有效条目 → null(调用方回落内置清单;消费纪律见规范头部)。
|
|
32
|
+
*/
|
|
33
|
+
export function parseLaunchersRegistry(json) {
|
|
34
|
+
if (!json || typeof json !== 'object' || Array.isArray(json)) return null;
|
|
35
|
+
const list = Array.isArray(json.launchers) ? json.launchers : null;
|
|
36
|
+
if (!list) return null;
|
|
37
|
+
const out = [];
|
|
38
|
+
for (const e of list) {
|
|
39
|
+
if (!e || typeof e !== 'object' || typeof e.id !== 'string' || !/^[a-z0-9]+(-[a-z0-9]+)*$/.test(e.id)) continue;
|
|
40
|
+
out.push({ id: e.id, name: typeof e.name === 'string' && e.name ? e.name : e.id });
|
|
41
|
+
}
|
|
42
|
+
return out.length ? out : null;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* 拉取启动器注册表(机器可读版)。拉取失败 / 解析非法 → 回落内置清单(source: 'builtin')。
|
|
47
|
+
* @returns {Promise<{launchers: Array<{id, name}>, source: 'registry'|'builtin'}>}
|
|
48
|
+
*/
|
|
49
|
+
export async function fetchLaunchersRegistry(host, opts = {}) {
|
|
50
|
+
const url = opts.url || DEFAULT_LAUNCHERS_REGISTRY_URL;
|
|
51
|
+
try {
|
|
52
|
+
const tmp = await host.mkdtemp('dspack-launchers-');
|
|
53
|
+
try {
|
|
54
|
+
const dest = host.joinPath(tmp, 'launchers.json');
|
|
55
|
+
await host.download(url, dest);
|
|
56
|
+
const raw = await host.readTextFile(dest);
|
|
57
|
+
const parsed = parseLaunchersRegistry(raw ? JSON.parse(raw) : null);
|
|
58
|
+
if (parsed) return { launchers: parsed, source: 'registry' };
|
|
59
|
+
} finally {
|
|
60
|
+
await host.rm(tmp, { recursive: true, force: true }).catch(() => {});
|
|
61
|
+
}
|
|
62
|
+
} catch { /* 落入内置回落 */ }
|
|
63
|
+
return { launchers: BUILTIN_LAUNCHERS, source: 'builtin' };
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** 由索引条目的 id/owner/repo 推导懒加载目录 key(`<owner>.<repo>`)。 */
|
|
67
|
+
export function packDirId(entry) {
|
|
68
|
+
if (entry?.id && typeof entry.id === 'string') return entry.id;
|
|
69
|
+
if (entry?.owner && entry?.repo) return `${entry.owner}.${entry.repo}`;
|
|
70
|
+
return '';
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** v5 r2 兼容性字段透传(manifest v5 §12/§13/§14):launchers / vendored / dshVersions
|
|
74
|
+
* 从索引 entry 或懒加载详情 manifest 里「有什么带什么」。中心索引(index 契约 §6.5)不平铺
|
|
75
|
+
* 这三个字段(只带派生标记 launcherRestricted),完整内容在 packs/<id>/manifest.json——
|
|
76
|
+
* 透传只为宽容消费:条目/manifest 里带了就带出,缺失不设键。 */
|
|
77
|
+
export function pickR2Fields(src) {
|
|
78
|
+
const out = {};
|
|
79
|
+
if (!src || typeof src !== 'object') return out;
|
|
80
|
+
if (src.launchers && typeof src.launchers === 'object' && !Array.isArray(src.launchers)) out.launchers = src.launchers;
|
|
81
|
+
if (src.vendored && typeof src.vendored === 'object' && !Array.isArray(src.vendored)) out.vendored = src.vendored;
|
|
82
|
+
if (Array.isArray(src.dshVersions)) {
|
|
83
|
+
const versions = src.dshVersions.filter((v) => typeof v === 'string' && v.trim());
|
|
84
|
+
if (versions.length) out.dshVersions = versions;
|
|
85
|
+
}
|
|
86
|
+
return out;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** r2 字段 → 市场详情展示徽标(结构化条目,文案由 UI 层按 kind 做 i18n):
|
|
90
|
+
* - { kind: 'launcher-require', id, minVersion } 需启动器 <id> ≥ <ver>(简式/全式都归一)
|
|
91
|
+
* - { kind: 'launcher-conflict', id, reason } 声明不支持某启动器(reason 可为空串)
|
|
92
|
+
* - { kind: 'vendored', count } 内嵌 N 个依赖(离线分发)
|
|
93
|
+
* - { kind: 'dsh-versions', versions } 兼容 DSH 版本枚举集
|
|
94
|
+
* 纯支持(supported:true 无 minVersion)不产生徽标;无任何 r2 字段 → 空列表(通用包)。 */
|
|
95
|
+
export function r2Badges(src) {
|
|
96
|
+
const out = [];
|
|
97
|
+
const r2 = pickR2Fields(src);
|
|
98
|
+
for (const [id, e] of Object.entries(normalizeLaunchers(r2.launchers))) {
|
|
99
|
+
if (e.supported === false) out.push({ kind: 'launcher-conflict', id, reason: e.reason ?? '' });
|
|
100
|
+
else if (e.minVersion) out.push({ kind: 'launcher-require', id, minVersion: e.minVersion });
|
|
101
|
+
}
|
|
102
|
+
const count = r2.vendored ? Object.keys(r2.vendored).length : 0;
|
|
103
|
+
if (count) out.push({ kind: 'vendored', count });
|
|
104
|
+
if (r2.dshVersions?.length) out.push({ kind: 'dsh-versions', versions: r2.dshVersions });
|
|
105
|
+
return out;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** 读取本地路径或 http(s) URL 的 index.json,返回归一化后的市场条目列表。
|
|
109
|
+
* 解析异常/缺字段时不抛错,而是带 `error` 说明(packs 为空),便于调用方提示真实原因。 */
|
|
110
|
+
export async function readMarketIndex(host, indexPath) {
|
|
111
|
+
const parsed = parseIndex(await fetchText(host, indexPath));
|
|
112
|
+
const index = parsed.index;
|
|
113
|
+
const raw = Array.isArray(index?.modpacks) ? index.modpacks : Array.isArray(index?.packs) ? index.packs : [];
|
|
114
|
+
return {
|
|
115
|
+
schemaVersion: index?.schemaVersion ?? 1,
|
|
116
|
+
generatedAt: index?.generatedAt ?? null,
|
|
117
|
+
packs: raw.map((e) => normalizeMarketPack(e)).filter(Boolean),
|
|
118
|
+
error: parsed.error,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** 懒加载单个整合包的完整 manifest + README(来自 `packs/<owner>.<repo>/`)。
|
|
123
|
+
* 由 index 路径推导 base:URL 去掉尾段 `index.json`;本地路径去掉文件名。
|
|
124
|
+
* 返回 { manifest, readme, dir, r2 }:manifest 为解析后的对象(失败 null),readme 为原文(失败 ''),
|
|
125
|
+
* r2 = pickR2Fields(manifest)——manifest v5 r2 的 launchers / vendored / dshVersions 有什么带什么。 */
|
|
126
|
+
export async function fetchMarketPackDetail(host, indexPath, entry) {
|
|
127
|
+
const dir = packDirId(entry);
|
|
128
|
+
if (!dir) return { manifest: null, readme: '', dir: '', r2: {} };
|
|
129
|
+
const base = detailBase(indexPath);
|
|
130
|
+
const manifestSrc = `${base}packs/${dir}/manifest.json`;
|
|
131
|
+
const readmeSrc = `${base}packs/${dir}/README.md`;
|
|
132
|
+
|
|
133
|
+
const [rawManifest, readme] = await Promise.all([
|
|
134
|
+
fetchText(host, manifestSrc).catch(() => null),
|
|
135
|
+
fetchText(host, readmeSrc).catch(() => ''),
|
|
136
|
+
]);
|
|
137
|
+
let manifest = null;
|
|
138
|
+
if (rawManifest) {
|
|
139
|
+
try { manifest = JSON.parse(rawManifest); } catch { manifest = null; }
|
|
140
|
+
}
|
|
141
|
+
let icons;
|
|
142
|
+
const units = manifest?.type === 'dshhome' ? Object.values(manifest.profiles || {}) : [manifest];
|
|
143
|
+
const withAssets = units.some(unit=>Object.values(unit?.plugins || {}).some(plugin=>typeof plugin?.icon === 'string' && plugin.icon.startsWith('assets/plugins/')));
|
|
144
|
+
if (withAssets && /^[a-f0-9]{64}$/i.test(manifest.sha256)) {
|
|
145
|
+
const cached = await fetchText(host,base+'packs/'+dir+'/plugin-icons.json').catch(()=>null);
|
|
146
|
+
if (cached) {
|
|
147
|
+
try {
|
|
148
|
+
const cache = JSON.parse(cached);
|
|
149
|
+
if (cache.sha256 === manifest.sha256) icons = cache.icons;
|
|
150
|
+
} catch { /* 历史缓存缺失或格式不符时仍展示插件名称。 */ }
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return {manifest,readme,dir,r2:pickR2Fields(manifest),...(icons ? {icons} : {})};
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** 取文本(本地路径或 http(s) URL):URL 走 host.download 拉到临时文件再读(读完清理);否则当本地路径读。 */
|
|
157
|
+
async function fetchText(host, src) {
|
|
158
|
+
if (!/^https?:\/\//i.test(src)) {
|
|
159
|
+
return (await host.readTextFile(host.resolvePath(src))) ?? '';
|
|
160
|
+
}
|
|
161
|
+
const tmp = await host.mkdtemp('pfx-mkt-');
|
|
162
|
+
try {
|
|
163
|
+
const dest = host.joinPath(tmp, 'detail.json');
|
|
164
|
+
await host.download(src, dest);
|
|
165
|
+
return (await host.readTextFile(dest)) ?? '';
|
|
166
|
+
} finally {
|
|
167
|
+
await host.rm(tmp, { recursive: true, force: true }).catch(() => {});
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** 由 index 路径推导 detail base(去掉 `index.json` 尾段,保留结尾分隔符)。 */
|
|
172
|
+
function detailBase(indexPath) {
|
|
173
|
+
const s = String(indexPath ?? '');
|
|
174
|
+
return s.replace(/index\.json$/i, '');
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** 解析索引 JSON:空内容 / 非 JSON / 缺数组字段时返回带 error 的对象。 */
|
|
178
|
+
function parseIndex(text) {
|
|
179
|
+
const s = String(text ?? '').trim();
|
|
180
|
+
if (!s) return { index: {}, error: '市场索引内容为空' };
|
|
181
|
+
try {
|
|
182
|
+
const index = JSON.parse(s);
|
|
183
|
+
if (!Array.isArray(index?.modpacks) && !Array.isArray(index?.packs)) {
|
|
184
|
+
return { index, error: '市场索引缺少 modpacks[] 或 packs[] 字段' };
|
|
185
|
+
}
|
|
186
|
+
return { index, error: null };
|
|
187
|
+
} catch {
|
|
188
|
+
return { index: {}, error: `市场索引不是有效 JSON(开头:${s.slice(0, 80)})` };
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** 归一化一条市场条目;不可识别返回 null。 */
|
|
193
|
+
export function normalizeMarketPack(entry, locale = 'zh-CN') {
|
|
194
|
+
if (!entry || typeof entry.name !== 'string') return null;
|
|
195
|
+
const urls = collectUrls(entry);
|
|
196
|
+
const manifestVersion = entry.manifestVersion ?? inferManifestVersion(urls);
|
|
197
|
+
return {
|
|
198
|
+
name: entry.name,
|
|
199
|
+
displayName: pickLocale(entry.displayName, locale, entry.name),
|
|
200
|
+
description: pickLocale(entry.description, locale, ''),
|
|
201
|
+
version: entry.version ?? '',
|
|
202
|
+
author: entry.author ?? '',
|
|
203
|
+
icon: pickLocale(entry.icon, locale, ''),
|
|
204
|
+
dshVersion: entry.dshVersion ?? '',
|
|
205
|
+
type: entry.type ?? inferType(manifestVersion),
|
|
206
|
+
profileName: entry.profileName ?? entry.name,
|
|
207
|
+
defaultProfile: entry.defaultProfile ?? '',
|
|
208
|
+
category: entry.category ?? '',
|
|
209
|
+
updatedAt: entry.updatedAt ?? '',
|
|
210
|
+
manifestVersion,
|
|
211
|
+
format: detectFormat(entry, urls, manifestVersion),
|
|
212
|
+
urls,
|
|
213
|
+
downloadUrl: urls[0] ?? '',
|
|
214
|
+
sha256: entry.sha256 ?? entry.files?.[0]?.sha256 ?? '',
|
|
215
|
+
size: entry.size ?? entry.files?.[0]?.size ?? 0,
|
|
216
|
+
id: entry.id ?? '',
|
|
217
|
+
owner: entry.owner ?? '',
|
|
218
|
+
repo: entry.repo ?? '',
|
|
219
|
+
// 精简索引不再平铺这些字段;完整值见 fetchMarketPackDetail 懒加载的 manifest。
|
|
220
|
+
bundles: entry.bundles ?? [],
|
|
221
|
+
dependencies: entry.dependencies ?? {},
|
|
222
|
+
profiles: entry.profiles,
|
|
223
|
+
presets: entry.presets,
|
|
224
|
+
skills: entry.skills,
|
|
225
|
+
// v5 r2(index 契约 §3/§6.5):索引只带派生标记 launcherRestricted(列表廉价过滤用),
|
|
226
|
+
// 完整 launchers / vendored / dshVersions 在懒加载 manifest;此处宽容透传——条目里带了就带出。
|
|
227
|
+
launcherRestricted: entry.launcherRestricted === true,
|
|
228
|
+
...pickR2Fields(entry),
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
function collectUrls(entry) {
|
|
233
|
+
if (Array.isArray(entry.files) && entry.files.length) {
|
|
234
|
+
const out = [];
|
|
235
|
+
for (const f of entry.files) for (const u of f.urls ?? []) out.push(u);
|
|
236
|
+
if (out.length) return out;
|
|
237
|
+
}
|
|
238
|
+
return entry.downloadUrl ? [entry.downloadUrl] : [];
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function detectFormat(entry, urls, manifestVersion) {
|
|
242
|
+
if (urls.some((u) => /\.dspack(\?|#|$)/i.test(u))) return 'dspack';
|
|
243
|
+
if (urls.some((u) => /\.tgz(\?|#|$)/i.test(u))) return 'tgz';
|
|
244
|
+
if ([4, 5, 6].includes(manifestVersion)) return 'dspack';
|
|
245
|
+
if (manifestVersion === 3) return 'tgz';
|
|
246
|
+
return 'unknown';
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/** manifestVersion → type 兜底(未显式声明 type 时)。 */
|
|
250
|
+
function inferType(manifestVersion) {
|
|
251
|
+
return (manifestVersion === 5 || manifestVersion === 6) ? 'dshhome' : 'profile';
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
function inferManifestVersion(urls) {
|
|
255
|
+
if (urls.some((u) => /\.dspack/i.test(u))) return 4;
|
|
256
|
+
if (urls.some((u) => /\.tgz/i.test(u))) return 3;
|
|
257
|
+
return 0;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function pickLocale(map, locale, fallback) {
|
|
261
|
+
if (typeof map === 'string' && map) return map;
|
|
262
|
+
if (map && typeof map === 'object') {
|
|
263
|
+
if (typeof map[locale] === 'string') return map[locale];
|
|
264
|
+
const first = Object.values(map).find((v) => typeof v === 'string');
|
|
265
|
+
if (first) return first;
|
|
266
|
+
}
|
|
267
|
+
return fallback ?? '';
|
|
268
|
+
}
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { parseDocument } from 'yaml';
|
|
2
|
+
import { safePath } from './extension-files.js';
|
|
3
|
+
|
|
4
|
+
const client = '@deepseek-ai/dsh-mcp-client';
|
|
5
|
+
const manager = '@deepseek-ai/dsh-mcp-manager';
|
|
6
|
+
const npmName = /^(?:@[a-z0-9._-]+\/)?[a-z0-9][a-z0-9._-]*$/i;
|
|
7
|
+
const serverName = /^[a-z0-9_-]{1,32}$/i;
|
|
8
|
+
const expression = '\u0000dsh:js';
|
|
9
|
+
const object = value => value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
10
|
+
const dynamic = value => value === expression || (object(value) && Object.hasOwn(value,'__jsExpr'));
|
|
11
|
+
|
|
12
|
+
function parseLayer(raw) {
|
|
13
|
+
if (!raw?.trim()) return [];
|
|
14
|
+
const doc = parseDocument(raw, {customTags:[{tag:'tag:yaml.org,2002:js',resolve:()=>expression}]});
|
|
15
|
+
if (doc.errors.length || doc.warnings.length) throw new Error('补丁无法静态解析');
|
|
16
|
+
const patches = doc.toJS({maxAliasCount:64});
|
|
17
|
+
if (!Array.isArray(patches) || !patches.every(object)) throw new Error('补丁不是配置行数组');
|
|
18
|
+
return patches;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// Node 按锚点逐级查找 node_modules。宿主安装目录优先于 profile,pnpm 的包链接由 Host 读取。
|
|
22
|
+
async function bundleRoot(host, anchors, name) {
|
|
23
|
+
const visited = new Set();
|
|
24
|
+
for (let dir of anchors) {
|
|
25
|
+
while (dir && !visited.has(dir)) {
|
|
26
|
+
visited.add(dir);
|
|
27
|
+
if (host.basename(dir) !== 'node_modules') {
|
|
28
|
+
const root = host.joinPath(dir,'node_modules',...name.split('/'));
|
|
29
|
+
const manifestPath = host.joinPath(root,'package.json');
|
|
30
|
+
const json = await host.readTextFile(manifestPath);
|
|
31
|
+
if (json != null) return {root,manifest:JSON.parse(json)};
|
|
32
|
+
if (await host.stat(manifestPath)) throw new Error('bundle 清单不可读');
|
|
33
|
+
}
|
|
34
|
+
const parent = host.resolvePath(dir,'..');
|
|
35
|
+
if (parent === dir) break;
|
|
36
|
+
dir = parent;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async function readBundle(host, anchors, name) {
|
|
43
|
+
if (!npmName.test(name)) throw new Error('无效的 bundle 名称');
|
|
44
|
+
const pkg = await bundleRoot(host,anchors,name);
|
|
45
|
+
if (!pkg) return null;
|
|
46
|
+
const declared = pkg?.manifest?.dsh?.bundle?.patch;
|
|
47
|
+
const files = typeof declared === 'string' ? [declared] : declared;
|
|
48
|
+
if (!Array.isArray(files)) throw new Error('无法定位 bundle 补丁');
|
|
49
|
+
const result = [];
|
|
50
|
+
for (const file of files) {
|
|
51
|
+
if (typeof file !== 'string') throw new Error('无效的 bundle 补丁路径');
|
|
52
|
+
const relative = file.replace(/^\.\//,'');
|
|
53
|
+
if (!relative || relative.split('/').some(s=>!s || s === '.' || s === '..' || /[\\:\x00-\x1f]/.test(s))) {
|
|
54
|
+
throw new Error('无效的 bundle 补丁路径');
|
|
55
|
+
}
|
|
56
|
+
const raw = await host.readTextFile(await safePath(host,pkg.root,relative));
|
|
57
|
+
if (raw == null) throw new Error('bundle 补丁尚不可读');
|
|
58
|
+
result.push(...parseLayer(raw));
|
|
59
|
+
}
|
|
60
|
+
return result;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// 来源标记附在副本的顶层属性上,随 Cordis 对该行和 config 的实际覆盖一起传播。
|
|
64
|
+
function markLayers(layers, unresolved) {
|
|
65
|
+
const patches = [], groups = new Set(), regrouped = new Set();
|
|
66
|
+
function mark(row, scope, inserted, depth = 0) {
|
|
67
|
+
if (!object(row) || depth > 64) throw new Error('补丁行无法静态解析');
|
|
68
|
+
const result = {};
|
|
69
|
+
if (dynamic(row.id) || dynamic(row.name) || dynamic(row.group) || dynamic(row.insert)) unresolved.add(scope);
|
|
70
|
+
for (const key of ['id','name','disabled','group']) {
|
|
71
|
+
if (Object.hasOwn(row,key)) result[key] = row[key];
|
|
72
|
+
}
|
|
73
|
+
if (inserted) result.rowScope = scope;
|
|
74
|
+
if (row.group === true && typeof row.id === 'string') groups.add(row.id);
|
|
75
|
+
if (Object.hasOwn(row,'insert')) {
|
|
76
|
+
if (!Array.isArray(row.insert)) throw new Error('无效的补丁插入行');
|
|
77
|
+
result.insert = row.insert.map(child => mark(child,scope,true,depth+1));
|
|
78
|
+
}
|
|
79
|
+
if (Object.hasOwn(row,'config')) {
|
|
80
|
+
result.configScope = scope;
|
|
81
|
+
const group = row.group === true || groups.has(row.id);
|
|
82
|
+
if (group && dynamic(row.config)) unresolved.add(scope);
|
|
83
|
+
if (group && Array.isArray(row.config)) {
|
|
84
|
+
result.config = row.config.map(child => mark(child,scope,true,depth+1));
|
|
85
|
+
if (!inserted) {
|
|
86
|
+
const collect = entries => {
|
|
87
|
+
for (const entry of entries) {
|
|
88
|
+
if (entry.id) regrouped.add(entry.id);
|
|
89
|
+
if (entry.group && Array.isArray(entry.config)) collect(entry.config);
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
collect(result.config);
|
|
93
|
+
}
|
|
94
|
+
} else result.config = row.config;
|
|
95
|
+
}
|
|
96
|
+
return result;
|
|
97
|
+
}
|
|
98
|
+
for (const {scope,patches:source} of layers) {
|
|
99
|
+
for (const row of source) {
|
|
100
|
+
// 旧版宿主对覆盖整个 group.config 后的子项保留旧索引,新版会重建。
|
|
101
|
+
if (row.id && regrouped.has(row.id)) unresolved.add(scope+':重编组后的条目标识');
|
|
102
|
+
patches.push(mark(row,scope,false));
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return patches;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function summarize(roots, unresolved) {
|
|
109
|
+
const rows = [], servers = [];
|
|
110
|
+
function visit(nodes, parentEnabled) {
|
|
111
|
+
for (const node of nodes) {
|
|
112
|
+
const {id,name,rowScope:scope,configScope=scope,config} = node;
|
|
113
|
+
const enabled = parentEnabled && node.disabled !== true;
|
|
114
|
+
if (dynamic(id) || dynamic(name) || dynamic(node.group) ||
|
|
115
|
+
((node.group || name === manager || name === client) && dynamic(node.disabled))) unresolved.add(scope);
|
|
116
|
+
if (typeof id === 'string' && !dynamic(id)) rows.push({id,name,scope,configScope,enabled});
|
|
117
|
+
function binding(server, entry, prefix) {
|
|
118
|
+
if (!id || dynamic(id) || typeof server !== 'string' || !serverName.test(server) ||
|
|
119
|
+
!object(entry) || dynamic(entry) || dynamic(entry.enabled)) {
|
|
120
|
+
unresolved.add(configScope);
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
const key = entry.oauth?.credentialKey;
|
|
124
|
+
if (dynamic(entry.oauth) || dynamic(key)) unresolved.add(configScope);
|
|
125
|
+
servers.push({server,owner:prefix+':'+id,scope:configScope,rowScope:scope,
|
|
126
|
+
enabled:enabled && (prefix !== 'manager' || entry.enabled !== false),
|
|
127
|
+
...(typeof key === 'string' && !dynamic(key) ? {oauthKey:key} : {})});
|
|
128
|
+
}
|
|
129
|
+
if (name === manager) {
|
|
130
|
+
if (dynamic(config) || dynamic(config?.servers)) unresolved.add(configScope);
|
|
131
|
+
else if (object(config?.servers)) for (const [server,entry] of Object.entries(config.servers)) binding(server,entry,'manager');
|
|
132
|
+
} else if (name === client) {
|
|
133
|
+
binding(config?.serverName,config,'client');
|
|
134
|
+
}
|
|
135
|
+
if (node.group === true && Array.isArray(config)) visit(config,enabled);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
visit(roots,true);
|
|
139
|
+
return {rows,servers};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** 按 bundle、profile、HOME、显式启动覆盖文件的顺序读取本机 MCP 配置;未安装的依赖保留为待核验项。 */
|
|
143
|
+
export async function readMcpComposition(host, options) {
|
|
144
|
+
const {profileDir,home,installAnchor,overlayPaths = []} = options;
|
|
145
|
+
const layers = [], unresolved = new Set(), unresolvedBundles = [], missingBundles = [];
|
|
146
|
+
const anchors = [];
|
|
147
|
+
if (installAnchor) anchors.push(host.resolvePath(installAnchor,'..'));
|
|
148
|
+
if (profileDir) anchors.push(host.resolvePath(profileDir));
|
|
149
|
+
let bundles = options.bundles;
|
|
150
|
+
if (bundles === undefined && profileDir) {
|
|
151
|
+
const raw = await host.readTextFile(host.joinPath(profileDir,'package.json'));
|
|
152
|
+
if (raw != null) {
|
|
153
|
+
try { bundles = JSON.parse(raw)?.dsh?.profile?.bundles; } catch { unresolved.add('profile'); }
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
if (bundles != null && !Array.isArray(bundles)) unresolved.add('profile');
|
|
157
|
+
for (const name of Array.isArray(bundles) ? bundles : []) {
|
|
158
|
+
try {
|
|
159
|
+
const patches = await readBundle(host,anchors,name);
|
|
160
|
+
if (patches === null) {
|
|
161
|
+
missingBundles.push(name);
|
|
162
|
+
unresolvedBundles.push(name);
|
|
163
|
+
unresolved.add(name);
|
|
164
|
+
} else layers.push({scope:'bundle',patches});
|
|
165
|
+
} catch {
|
|
166
|
+
const label = typeof name === 'string' && npmName.test(name) ? name : 'bundle';
|
|
167
|
+
unresolved.add(label);
|
|
168
|
+
unresolvedBundles.push(label);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
async function layer(scope, path, override, required = false) {
|
|
172
|
+
try {
|
|
173
|
+
const raw = override !== undefined ? override : path ? await host.readTextFile(path) : null;
|
|
174
|
+
if (raw == null && required) { unresolved.add(scope); return; }
|
|
175
|
+
layers.push({scope,patches:parseLayer(raw)});
|
|
176
|
+
} catch { unresolved.add(scope); }
|
|
177
|
+
}
|
|
178
|
+
await layer('profile',profileDir && host.joinPath(profileDir,'cordis.patch.yml'),options.profilePatch);
|
|
179
|
+
await layer('home',home && host.joinPath(home,'cordis.patch.yml'),options.homePatch);
|
|
180
|
+
for (const path of overlayPaths) await layer('overlay',path,undefined,true);
|
|
181
|
+
let entries = [];
|
|
182
|
+
try {
|
|
183
|
+
entries = await host.composePackEntries(markLayers(layers,unresolved));
|
|
184
|
+
} catch {
|
|
185
|
+
unresolved.add('Cordis 配置组合');
|
|
186
|
+
}
|
|
187
|
+
const result = summarize(entries,unresolved);
|
|
188
|
+
return {...result,unresolved:[...unresolved],unresolvedBundles,missingBundles};
|
|
189
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { readMcpComposition } from './mcp-composition.js';
|
|
2
|
+
import { findLauncherInstallAnchor } from './discovery.js';
|
|
3
|
+
import { baselineOwnsMcp, mcpClientInPatch } from './extension-patch.js';
|
|
4
|
+
|
|
5
|
+
const client = '@deepseek-ai/dsh-mcp-client';
|
|
6
|
+
const notice = (item,message) => { item.notice = item.notice ? item.notice+'。'+message : message; };
|
|
7
|
+
|
|
8
|
+
export async function checkMcpInstall(host,opts,actions) {
|
|
9
|
+
const candidates = actions.filter(a=>a.mcp);
|
|
10
|
+
if (!candidates.length) return async () => {};
|
|
11
|
+
const home = opts.credentialHome || opts.home;
|
|
12
|
+
const installAnchor = opts.installAnchor || await findLauncherInstallAnchor(host,home,opts.manifest.dshVersion);
|
|
13
|
+
const whole = opts.manifest.type === 'dshhome';
|
|
14
|
+
const profilesRoot = host.joinPath(home,opts.profilesDir || 'profiles');
|
|
15
|
+
const profiles = new Set(whole ? Object.keys(opts.manifest.profiles) : [opts.profileName]);
|
|
16
|
+
if (whole && candidates.some(a=>a.mcp.scope === null)) {
|
|
17
|
+
for (const entry of await host.readdir(profilesRoot) || []) if (entry.type === 'dir') profiles.add(entry.name);
|
|
18
|
+
}
|
|
19
|
+
const targetProfiles = mcp => mcp.scope === null ? profiles : [mcp.scope];
|
|
20
|
+
const layer = mcp => mcp.scope === null ? 'home' : 'profile';
|
|
21
|
+
const scopesOverlap = (a,b) => a.scope === null || b.scope === null || a.scope === b.scope;
|
|
22
|
+
const sameIdentity = (a,b) => a.server === b.server || a.oauthKey && a.oauthKey === b.oauthKey;
|
|
23
|
+
const chosen = [];
|
|
24
|
+
const priority = action => opts.decisions?.[action.item.key] === 'replace' ? 0 : 1;
|
|
25
|
+
async function composition(name,final) {
|
|
26
|
+
const profileDir = !whole && (opts.credentialProfileDir || !opts.staging && opts.profileDir) || host.joinPath(profilesRoot,name);
|
|
27
|
+
const unit = whole ? opts.manifest.profiles[name] : opts.manifest;
|
|
28
|
+
return readMcpComposition(host,{home,profileDir,installAnchor,overlayPaths:opts.overlayPaths,
|
|
29
|
+
bundles:final ? undefined : unit?.bundles});
|
|
30
|
+
}
|
|
31
|
+
const views = new Map();
|
|
32
|
+
for (const name of profiles) views.set(name,await composition(name,false));
|
|
33
|
+
for (const action of [...candidates].sort((a,b)=>priority(a)-priority(b))) {
|
|
34
|
+
const {mcp,item} = action;
|
|
35
|
+
const choice = opts.decisions?.[item.key];
|
|
36
|
+
const scopes = targetProfiles(mcp);
|
|
37
|
+
let collision = false, foreign = false;
|
|
38
|
+
const unresolved = new Set();
|
|
39
|
+
for (const profile of scopes) {
|
|
40
|
+
const view = views.get(profile);
|
|
41
|
+
for (const reason of view.unresolved) unresolved.add(reason);
|
|
42
|
+
for (const row of view.rows.filter(row=>row.id === mcp.id)) {
|
|
43
|
+
if (row.scope !== layer(mcp) || row.name !== client || row.configScope !== layer(mcp)) foreign = true;
|
|
44
|
+
}
|
|
45
|
+
if (view.servers.some(b=>b.owner === mcp.owner && b.server !== mcp.server && !mcp.owned)) foreign = true;
|
|
46
|
+
for (const binding of view.servers.filter(other=>sameIdentity(mcp,other))) {
|
|
47
|
+
const local = binding.scope === layer(mcp);
|
|
48
|
+
if (local && binding.owner === mcp.owner && binding.rowScope === layer(mcp)) {
|
|
49
|
+
if (!mcp.owned) collision = true;
|
|
50
|
+
} else if (local && mcp.legacy && binding.owner.startsWith('manager:') && binding.server === mcp.server) {
|
|
51
|
+
const owner = binding.owner.slice(8);
|
|
52
|
+
const owned = await baselineOwnsMcp(host,mcp.tree,owner,mcp.server);
|
|
53
|
+
if (!owned) {
|
|
54
|
+
collision = true;
|
|
55
|
+
if (choice === 'replace') mcp.replaceOwner = owner;
|
|
56
|
+
}
|
|
57
|
+
} else foreign = true;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
if (unresolved.size) notice(item,'部分 MCP 配置需在依赖安装后核验:'+[...unresolved].join('、'));
|
|
61
|
+
if (foreign || collision) {
|
|
62
|
+
item.conflict = true;
|
|
63
|
+
item.canReplace = !foreign;
|
|
64
|
+
notice(item,foreign ? '服务名称、OAuth 记录或配置行已由其他来源使用,请改名或跳过' : '目标补丁已有同名 MCP 服务,可保留或替换该项');
|
|
65
|
+
if (foreign && choice === 'replace') throw new Error('无法覆盖其他来源或另一作用域的 MCP:'+mcp.server);
|
|
66
|
+
if (foreign || choice !== 'replace') item.skipped = true;
|
|
67
|
+
}
|
|
68
|
+
if (item.skipped) continue;
|
|
69
|
+
const other = chosen.find(a=>scopesOverlap(mcp,a.mcp) && (mcp.id === a.mcp.id || sameIdentity(mcp,a.mcp)));
|
|
70
|
+
if (other) {
|
|
71
|
+
item.conflict = item.skipped = true;
|
|
72
|
+
notice(item,'包内另一个已选条目使用相同名称或 OAuth 记录');
|
|
73
|
+
if (choice === 'replace' && priority(other) === 0) throw new Error('所选 MCP 条目使用相同名称或 OAuth 记录:'+mcp.server);
|
|
74
|
+
} else chosen.push(action);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return async ({allowUnresolved=false}={}) => {
|
|
78
|
+
const cache = new Map();
|
|
79
|
+
for (const {item,mcp} of chosen) {
|
|
80
|
+
if (item.skipped) continue;
|
|
81
|
+
const local = mcpClientInPatch(await host.readTextFile(mcp.actualPatch),mcp.id);
|
|
82
|
+
if (!local && mcp.owned) {
|
|
83
|
+
item.skipped = true;
|
|
84
|
+
notice(item,'三方更新保留了本地删除,此服务未启用');
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
const expected = local ? {server:local.config?.serverName,oauthKey:local.config?.oauth?.credentialKey,enabled:local.disabled !== true} : mcp;
|
|
88
|
+
if (local && (expected.server !== mcp.server || expected.enabled !== mcp.enabled)) {
|
|
89
|
+
notice(item,'三方更新采用了该客户端的本地配置');
|
|
90
|
+
}
|
|
91
|
+
for (const name of targetProfiles(mcp)) {
|
|
92
|
+
if (!cache.has(name)) cache.set(name,await composition(name,true));
|
|
93
|
+
const view = cache.get(name);
|
|
94
|
+
if (view.unresolved.length) {
|
|
95
|
+
const missing = new Set(view.missingBundles ?? []);
|
|
96
|
+
const pending = view.unresolved.every(id=>missing.has(id) &&
|
|
97
|
+
(allowUnresolved || !installAnchor && id.startsWith('@deepseek-ai/')));
|
|
98
|
+
if (!pending) {
|
|
99
|
+
throw new Error('无法核验 '+name+' 的 MCP 配置:'+view.unresolved.join('、'));
|
|
100
|
+
}
|
|
101
|
+
notice(item,'已写入客户端,尚无法定位的宿主依赖须在实际启动环境核验');
|
|
102
|
+
}
|
|
103
|
+
const rows = view.rows.filter(r=>r.id === mcp.id);
|
|
104
|
+
const names = view.servers.filter(b=>sameIdentity(expected,b));
|
|
105
|
+
if (rows.length !== 1 || rows[0].name !== client || rows[0].scope !== layer(mcp) || rows[0].configScope !== layer(mcp) ||
|
|
106
|
+
names.length !== 1 || names[0].owner !== mcp.owner || names[0].enabled !== expected.enabled) {
|
|
107
|
+
throw new Error('MCP 客户端被其他配置覆盖、同名或未按预期启用:'+mcp.server+'('+name+')');
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
}
|