@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,95 @@
|
|
|
1
|
+
// 工作区配置(.dshpkcfg):导出工作区的本地快照(规范见 specs/workspace-config/v1.md)。
|
|
2
|
+
// 供 GUI / CLI / AI 工具 / DSH 插件共用的读取、保存与「按配置导出」入口,避免各写一份 readTextFile + JSON.parse。
|
|
3
|
+
import { packProfile, packHome } from './pack.js';
|
|
4
|
+
import { exportRepo } from './repo.js';
|
|
5
|
+
|
|
6
|
+
/** .dshpkcfg 已知字段白名单(规范 v1 r2:公共 + profile + dshhome 形态 + 兼容性/内嵌旋钮)。 */
|
|
7
|
+
export const WORKSPACE_KEYS = [
|
|
8
|
+
'selection', 'plugins', 'profilePlugins',
|
|
9
|
+
'name', 'version', 'displayName', 'description', 'author', 'icon', 'dshVersion', 'out',
|
|
10
|
+
'exportContent', 'profileName', 'mode', 'content', 'defaultProfile',
|
|
11
|
+
'dshVersions', 'launchers', 'vendor',
|
|
12
|
+
];
|
|
13
|
+
|
|
14
|
+
/** 读取某个目录下的 .dshpkcfg;不存在/非法 → null。 */
|
|
15
|
+
export async function loadWorkspaceConfig(host, dir) {
|
|
16
|
+
if (!dir) return null;
|
|
17
|
+
const raw = await host.readTextFile(host.joinPath(dir, '.dshpkcfg'));
|
|
18
|
+
if (!raw) return null;
|
|
19
|
+
try { return JSON.parse(raw); } catch { return null; }
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* 保存工作区配置到目录下的 .dshpkcfg(规范 v1:UTF-8、单 JSON 对象、2 空格缩进、结尾换行)。
|
|
24
|
+
* 只落白名单字段、去 null/undefined;空串保留(规范「空串 = 未填写」)。
|
|
25
|
+
* @returns {Promise<string>} 写入的绝对路径
|
|
26
|
+
*/
|
|
27
|
+
export async function saveWorkspaceConfig(host, dir, config) {
|
|
28
|
+
if (!dir) throw new Error('缺少保存目录');
|
|
29
|
+
const out = {};
|
|
30
|
+
for (const k of WORKSPACE_KEYS) {
|
|
31
|
+
const v = config?.[k];
|
|
32
|
+
if (v === undefined || v === null) continue;
|
|
33
|
+
out[k] = v;
|
|
34
|
+
}
|
|
35
|
+
const file = host.joinPath(dir, '.dshpkcfg');
|
|
36
|
+
await host.writeTextFile(file, JSON.stringify(out, null, 2) + '\n');
|
|
37
|
+
return file;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* 按工作区配置导出:config 打底,overrides 覆盖;按 mode 分流 dspack / repo。
|
|
42
|
+
* @param {Host} host
|
|
43
|
+
* @param {{name:string,dir:string}} profile
|
|
44
|
+
* @param {object} overrides 显式覆盖(CLI flag / AI 工具参数 / GUI 表单);undefined/null 视为「未设置」不覆盖
|
|
45
|
+
* @returns packProfile 或 exportRepo 的结果
|
|
46
|
+
*/
|
|
47
|
+
export async function exportFromWorkspace(host, profile, overrides = {}) {
|
|
48
|
+
const cfg = (await loadWorkspaceConfig(host, profile.dir)) ?? {};
|
|
49
|
+
const opts = { ...cfg };
|
|
50
|
+
for (const [k, v] of Object.entries(overrides)) {
|
|
51
|
+
if (v !== undefined && v !== null) opts[k] = v;
|
|
52
|
+
}
|
|
53
|
+
// exportContent({skill,preset,instruction})→ homeInclude 前缀(profile 形态:勾选即导出上一级目录内容;
|
|
54
|
+
// data 仅 dshhome 形态,这里忽略)。
|
|
55
|
+
const ec = opts.exportContent;
|
|
56
|
+
if (ec && typeof ec === 'object') {
|
|
57
|
+
const include = [];
|
|
58
|
+
if (ec.skill === true) include.push('skills/');
|
|
59
|
+
if (ec.preset === true) include.push('.agent-presets/');
|
|
60
|
+
if (ec.instruction === true) include.push('AGENTS.md');
|
|
61
|
+
if (include.length) opts.homeInclude = include;
|
|
62
|
+
}
|
|
63
|
+
if (opts.mode === 'repo') {
|
|
64
|
+
// repo 形态下 force 等价 replaceRelease(覆盖同版本 release 产物)
|
|
65
|
+
if (opts.force === true) opts.replaceRelease = true;
|
|
66
|
+
return exportRepo(host, profile, opts);
|
|
67
|
+
}
|
|
68
|
+
return packProfile(host, profile, opts);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* 按工作区配置导出 DSH_HOME(dshhome):config 打底、overrides 覆盖;把 exportContent 布尔开关映射为 exclude 前缀。
|
|
73
|
+
* @param {Host} host
|
|
74
|
+
* @param {{name:string,dir:string}} home
|
|
75
|
+
* @param {object} overrides 显式覆盖(CLI flag / AI 参数 / Remote 请求)
|
|
76
|
+
* @returns packHome 的结果
|
|
77
|
+
*/
|
|
78
|
+
export async function exportHomeFromWorkspace(host, home, overrides = {}) {
|
|
79
|
+
const cfg = (await loadWorkspaceConfig(host, home.dir)) ?? {};
|
|
80
|
+
const opts = { ...cfg };
|
|
81
|
+
for (const [k, v] of Object.entries(overrides)) {
|
|
82
|
+
if (v !== undefined && v !== null) opts[k] = v;
|
|
83
|
+
}
|
|
84
|
+
// exportContent({skill,preset,instruction,data})→ exclude 前缀(未勾选即排除)
|
|
85
|
+
const ec = opts.exportContent;
|
|
86
|
+
if (ec && typeof ec === 'object') {
|
|
87
|
+
const excludes = [];
|
|
88
|
+
if (ec.skill === false) excludes.push('skills/');
|
|
89
|
+
if (ec.preset === false) excludes.push('.agent-presets/');
|
|
90
|
+
if (ec.instruction === false) excludes.push('AGENTS.md');
|
|
91
|
+
if (ec.data === false) excludes.push('data/');
|
|
92
|
+
if (excludes.length) opts.exclude = excludes;
|
|
93
|
+
}
|
|
94
|
+
return packHome(host, home, opts);
|
|
95
|
+
}
|
package/src/endpoints.js
ADDED
|
@@ -0,0 +1,455 @@
|
|
|
1
|
+
// 宿主 endpoint 层:UI(rpc.call)与 AI 工具(薄壳)共用的唯一业务入口。
|
|
2
|
+
//
|
|
3
|
+
// 每个 handler 收 { ctx, runtime, payload, signal, peer },返回纯值;
|
|
4
|
+
// 抛错由 rpc.js 统一转成 { ok:false, error }。core 的 .dspack 格式逻辑经 Host 注入调用。
|
|
5
|
+
import {
|
|
6
|
+
exportChoices,
|
|
7
|
+
resolveProfileInput,
|
|
8
|
+
exportFromWorkspace,
|
|
9
|
+
exportHomeFromWorkspace,
|
|
10
|
+
loadWorkspaceConfig,
|
|
11
|
+
saveWorkspaceConfig,
|
|
12
|
+
inspectPack,
|
|
13
|
+
installPack,
|
|
14
|
+
resolvePackSource,
|
|
15
|
+
readMarketIndex,
|
|
16
|
+
fetchMarketPackDetail,
|
|
17
|
+
normalizeLaunchers,
|
|
18
|
+
judgeLaunchers,
|
|
19
|
+
listProfileDependencies,
|
|
20
|
+
listInstalledDshVersions,
|
|
21
|
+
DEFAULT_MARKET_INDEX,
|
|
22
|
+
} from './core/index.js';
|
|
23
|
+
import path from 'node:path';
|
|
24
|
+
import os from 'node:os';
|
|
25
|
+
import fsp from 'node:fs/promises';
|
|
26
|
+
import { createRequire } from 'node:module';
|
|
27
|
+
import { getHost } from './host.js';
|
|
28
|
+
// r2Badges / pickR2Fields 是市场 r2 透传新导出(core/index.js 桶文件未列,直连模块导入)
|
|
29
|
+
import { r2Badges, fetchLaunchersRegistry } from './core/market.js';
|
|
30
|
+
|
|
31
|
+
// launchers/registry 端点缓存({at, value};TTL 1h,进程内共享)
|
|
32
|
+
let launchersRegistryCache = null;
|
|
33
|
+
import { listProfiles, createProfile, deleteProfile, readState, writeState, resolveActiveName } from './profiles.js';
|
|
34
|
+
import { switchProfile } from './junction.js';
|
|
35
|
+
import { CHANNEL, PROFILE_NAME_RE } from './channel.js';
|
|
36
|
+
import { ACTIVE_NAME } from './runtime.js';
|
|
37
|
+
import { checkManagerInProfile, ensureManagerInProfile } from './ensure-manager.js';
|
|
38
|
+
import * as tasks from './tasks.js';
|
|
39
|
+
|
|
40
|
+
const need = (v, msg) => {
|
|
41
|
+
if (v == null || v === '') throw new Error(msg);
|
|
42
|
+
return String(v).trim();
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
// 插件自身元数据:作者/仓库/首页/版本号等,供 About 页与检查更新使用。
|
|
46
|
+
const require = createRequire(import.meta.url);
|
|
47
|
+
const { name: MANAGER, version: PACKAGE_VERSION } = require('../package.json');
|
|
48
|
+
const NPM_REGISTRY = 'https://registry.npmjs.org';
|
|
49
|
+
const NPM_URL = `https://www.npmjs.com/package/${MANAGER}`;
|
|
50
|
+
|
|
51
|
+
// 新建 profile 的官方基线 bundle(web 模板)。这是「模板型 bundle」:只进 dsh.profile.bundles、
|
|
52
|
+
// 不进 dependencies —— boot 时由 DSH 安装目录(installAnchor)fallback 兜底解析(见 core/install.js
|
|
53
|
+
// reconcileProfile 的「模板型 bundle」注释)。来源:@deepseek-ai/dsh-app-boot 的 PROFILE_TEMPLATES.web
|
|
54
|
+
// = base + web-app;缺了 web-app,切过去就没有 UI。
|
|
55
|
+
const BASELINE_BUNDLES = ['@deepseek-ai/dsh-base', '@deepseek-ai/dsh-web-app'];
|
|
56
|
+
|
|
57
|
+
// 简版 semver 比较(去 prerelease 后缀,逐段比数值):>0 表示 a 更新。
|
|
58
|
+
function cmpVersion(a, b) {
|
|
59
|
+
const nums = (v) => String(v).split('-')[0].split('.').map((n) => parseInt(n, 10) || 0);
|
|
60
|
+
const A = nums(a);
|
|
61
|
+
const B = nums(b);
|
|
62
|
+
for (let i = 0; i < Math.max(A.length, B.length); i += 1) {
|
|
63
|
+
const d = (A[i] || 0) - (B[i] || 0);
|
|
64
|
+
if (d !== 0) return d;
|
|
65
|
+
}
|
|
66
|
+
return 0;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// 导出/工作区配置共用的目标解析:默认当前激活 profile;名字优先在桌面 home(与切换列表同一套)里匹配,
|
|
70
|
+
// 匹配不到再当路径/全局名兜底(CLI / AI 工具导出任意目录用)。
|
|
71
|
+
async function resolveExportProfile(runtime, host, want) {
|
|
72
|
+
const profiles = await listProfiles(runtime);
|
|
73
|
+
const active = profiles.find((p) => p.active) ?? profiles[0] ?? null;
|
|
74
|
+
let profile = null;
|
|
75
|
+
if (want) {
|
|
76
|
+
profile = profiles.find((p) => p.name === want) ?? (await resolveProfileInput(host, want));
|
|
77
|
+
} else {
|
|
78
|
+
profile = active;
|
|
79
|
+
}
|
|
80
|
+
if (!profile) {
|
|
81
|
+
const names = profiles.map((p) => p.name).join(', ');
|
|
82
|
+
throw new Error(`找不到 profile「${want ?? ''}」。可用:${names || '(无)'}`);
|
|
83
|
+
}
|
|
84
|
+
return profile;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async function exportTarget(runtime,host,payload) {
|
|
88
|
+
if (payload?.type === 'dshhome') {
|
|
89
|
+
const dir = need(runtime?.home,'当前桌面端没有可导出的 DSH_HOME');
|
|
90
|
+
return {name:path.basename(dir),dir};
|
|
91
|
+
}
|
|
92
|
+
return resolveExportProfile(runtime,host,payload?.profile);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function installOptions(runtime, payload, ctx) {
|
|
96
|
+
const snapshot = ctx?.get?.('launchEnvironment');
|
|
97
|
+
return {
|
|
98
|
+
source: need(payload?.source, '缺少整合包位置'),
|
|
99
|
+
name: payload?.name,
|
|
100
|
+
profilesRoot: payload?.profilesRoot ?? runtime?.profilesDir,
|
|
101
|
+
home: runtime?.home,
|
|
102
|
+
credentialCwd: runtime?.credentialCwd,
|
|
103
|
+
installAnchor: runtime?.installAnchor, overlayPaths: runtime?.overlayPaths,
|
|
104
|
+
credentialEnvironment: snapshot?.getFrom ? ref =>
|
|
105
|
+
snapshot.getFrom(ref,['process'])?.value || snapshot.getFrom(ref,['project-env','user-env'])?.value : undefined,
|
|
106
|
+
update: payload?.update === true,
|
|
107
|
+
listResourcesOnly: payload?.listResourcesOnly === true,
|
|
108
|
+
decisions: payload?.decisions, credentials: payload?.credentials, confirmedCredentials: payload?.confirmedCredentials,
|
|
109
|
+
resolutions: payload?.resolutions, previewToken: payload?.previewToken,
|
|
110
|
+
previousSource: payload?.previousSource, previousSha256: payload?.previousSha256,
|
|
111
|
+
force: payload?.force === true,
|
|
112
|
+
dryRun: payload?.dryRun === true,
|
|
113
|
+
recoverInterrupted: payload?.recoverInterrupted === true,
|
|
114
|
+
noInstall: payload?.noInstall === true,
|
|
115
|
+
// 市场安装时透传索引里的 sha256/size,交给 installPack 做完整性校验(防篡改/坏档)。
|
|
116
|
+
expectedSha256: payload?.expectedSha256 || undefined,
|
|
117
|
+
expectedSize: payload?.expectedSize,
|
|
118
|
+
// v5 r2 launchers 判定:本插件运行在官方桌面端内,版本取 DSH_VERSION(无则按「版本未知」
|
|
119
|
+
// 轻提示放行);launcherId 默认不传(core 缺省 'official-desktop'),预留 payload 覆盖口。
|
|
120
|
+
launcherId: payload?.launcherId || undefined,
|
|
121
|
+
launcherVersion: process.env.DSH_VERSION ?? null,
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export const ENDPOINTS = {
|
|
126
|
+
'runtime/get': async ({ runtime }) => ({
|
|
127
|
+
...runtime,
|
|
128
|
+
platform: process.platform,
|
|
129
|
+
node: process.version,
|
|
130
|
+
dshVersion: process.env.DSH_VERSION ?? null,
|
|
131
|
+
// 本机已装 DSH 版本(dshVersions 多选建议来源,workspace-config v1 r2)
|
|
132
|
+
installedDshVersions: await listInstalledDshVersions(getHost()),
|
|
133
|
+
rpc: CHANNEL,
|
|
134
|
+
}),
|
|
135
|
+
|
|
136
|
+
'config/get': async ({ runtime }) => (await readState(runtime.home)).config ?? {},
|
|
137
|
+
|
|
138
|
+
'config/set': async ({ runtime, payload }) => {
|
|
139
|
+
const s = await readState(runtime.home);
|
|
140
|
+
s.config = { ...(s.config ?? {}), ...(payload ?? {}) };
|
|
141
|
+
await writeState(runtime.home, s);
|
|
142
|
+
getHost().setProxy(s.config.proxy); // 代理立即生效,无需重启
|
|
143
|
+
return s.config;
|
|
144
|
+
},
|
|
145
|
+
|
|
146
|
+
'profile/list': async ({ runtime }) => ({ profiles: await listProfiles(runtime) }),
|
|
147
|
+
|
|
148
|
+
'profile/create': async ({ runtime, payload }) => {
|
|
149
|
+
const name = need(payload?.name, '缺少 profile 名');
|
|
150
|
+
if (!PROFILE_NAME_RE.test(name)) throw new Error('profile 名只能含小写字母、数字、连字符,如 aaa-bb-c');
|
|
151
|
+
// 同步预检重名(真正写盘/迁装放进任务里异步跑)。
|
|
152
|
+
const dir = path.join(runtime.profilesDir, name);
|
|
153
|
+
if (await fsp.stat(dir).then(() => true, () => false)) {
|
|
154
|
+
throw new Error(`profile「${name}」已存在`);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// 空整合包也要有官方基线 bundle(base + web-app,缺了就没 UI)+ 迁装管理器(+fflate),
|
|
158
|
+
// 否则切过去就是单程票、且 switch 会因缺 package.json 抛错。与 install/export 一致走任务中心,
|
|
159
|
+
// 关面板不丢进度。
|
|
160
|
+
const id = tasks.createTask({ kind: 'create', title: `创建 ${name}`, home: runtime?.home });
|
|
161
|
+
tasks.enqueue(async () => {
|
|
162
|
+
const progress = tasks.progressBridge(id);
|
|
163
|
+
try {
|
|
164
|
+
progress('init', '创建目录与 package.json(含官方基线 bundle)');
|
|
165
|
+
const target = await createProfile(runtime.home, name);
|
|
166
|
+
await fsp.writeFile(path.join(target, 'package.json'), `${JSON.stringify({
|
|
167
|
+
name: `dsh-profile-${name}`,
|
|
168
|
+
private: true,
|
|
169
|
+
dependencies: {},
|
|
170
|
+
dsh: { profile: { bundles: [...BASELINE_BUNDLES] } },
|
|
171
|
+
}, null, 2)}\n`);
|
|
172
|
+
progress('manager', '迁装管理器(基本插件)');
|
|
173
|
+
await ensureManagerInProfile(runtime, name);
|
|
174
|
+
tasks.finish(id, { ok: true, title: `创建 ${name}`, result: { name, dir: target, managerInstalled: true } });
|
|
175
|
+
} catch (e) {
|
|
176
|
+
// 建到一半失败:清掉目录,不留残废 profile。
|
|
177
|
+
await fsp.rm(dir, { recursive: true, force: true }).catch(() => {});
|
|
178
|
+
tasks.finish(id, { ok: false, error: String(e?.message ?? e) });
|
|
179
|
+
}
|
|
180
|
+
}).catch(() => {});
|
|
181
|
+
return { taskId: id };
|
|
182
|
+
},
|
|
183
|
+
|
|
184
|
+
'profile/delete': async ({ runtime, payload }) => {
|
|
185
|
+
const name = need(payload?.name, '缺少 profile 名');
|
|
186
|
+
await deleteProfile(runtime.home, name);
|
|
187
|
+
return { name };
|
|
188
|
+
},
|
|
189
|
+
|
|
190
|
+
'profile/switch': async ({ runtime, payload }) => {
|
|
191
|
+
const name = need(payload?.name, '缺少 profile 名');
|
|
192
|
+
return await switchProfile(runtime, name, {
|
|
193
|
+
swapSkills: payload?.swapSkills !== false,
|
|
194
|
+
managerSource: payload?.managerSource === 'copy' ? 'copy' : 'npm',
|
|
195
|
+
});
|
|
196
|
+
},
|
|
197
|
+
|
|
198
|
+
// 切换前的只读预检:确认弹窗要显示的 from/to、目标是否已装管理器、是否首次切换。
|
|
199
|
+
// 不安装、不改状态 —— 真正的安装/换指由 profile/switch 在用户确认后执行。
|
|
200
|
+
'profile/switch-check': async ({ runtime, payload }) => {
|
|
201
|
+
const name = need(payload?.name, '缺少 profile 名');
|
|
202
|
+
const profiles = await listProfiles(runtime);
|
|
203
|
+
if (!profiles.some((p) => p.name === name)) throw new Error(`profile「${name}」不存在`);
|
|
204
|
+
const hasManager = await checkManagerInProfile(runtime, name);
|
|
205
|
+
const activeName = await resolveActiveName(runtime);
|
|
206
|
+
const desktop = path.join(runtime.profilesDir, ACTIVE_NAME);
|
|
207
|
+
let firstTime = true;
|
|
208
|
+
try {
|
|
209
|
+
const st = await fsp.lstat(desktop);
|
|
210
|
+
firstTime = !st.isSymbolicLink(); // 还是真实目录 → 首次切换(要移动默认目录)
|
|
211
|
+
} catch { /* desktop 不存在 → 视为首次 */ }
|
|
212
|
+
return { from: activeName ?? 'default', to: name, hasManager, firstTime };
|
|
213
|
+
},
|
|
214
|
+
|
|
215
|
+
'profile/open-dir': async ({ runtime, payload }) => {
|
|
216
|
+
const host = getHost();
|
|
217
|
+
const profiles = await listProfiles(runtime);
|
|
218
|
+
const name = payload?.name;
|
|
219
|
+
const target = name
|
|
220
|
+
? profiles.find((p) => p.name === name)
|
|
221
|
+
: (profiles.find((p) => p.active) ?? profiles[0]);
|
|
222
|
+
if (!target) throw new Error(`找不到 profile「${name ?? ''}」`);
|
|
223
|
+
const cmd = process.platform === 'win32' ? 'explorer'
|
|
224
|
+
: process.platform === 'darwin' ? 'open' : 'xdg-open';
|
|
225
|
+
const r = await host.exec(cmd, [target.dir]);
|
|
226
|
+
if (r.error) throw new Error(`打开目录失败:${r.error}`);
|
|
227
|
+
return { name: target.name, dir: target.dir };
|
|
228
|
+
},
|
|
229
|
+
|
|
230
|
+
'pack/export': async ({ runtime, payload }) => {
|
|
231
|
+
const host = getHost();
|
|
232
|
+
// 输入校验(profile 解析)在此同步做,出错立即返回;真正的导出进任务中心异步跑。
|
|
233
|
+
const profile = await exportTarget(runtime,host,payload);
|
|
234
|
+
|
|
235
|
+
const whole = payload?.type === 'dshhome';
|
|
236
|
+
if (whole && payload?.mode === 'repo') throw new Error('整机导出支持 .dspack 格式');
|
|
237
|
+
const id = tasks.createTask({ kind: 'export', title: `导出 ${profile.name}`, home: runtime?.home });
|
|
238
|
+
const overrides = {
|
|
239
|
+
out: payload?.out,
|
|
240
|
+
name: payload?.name,
|
|
241
|
+
version: payload?.version,
|
|
242
|
+
displayName: payload?.displayName,
|
|
243
|
+
description: payload?.description,
|
|
244
|
+
author: payload?.author,
|
|
245
|
+
icon: payload?.icon,
|
|
246
|
+
dshVersion: payload?.dshVersion,
|
|
247
|
+
profileName: payload?.profileName,
|
|
248
|
+
exportContent: payload?.exportContent,
|
|
249
|
+
selection: payload?.selection,
|
|
250
|
+
plugins: payload?.plugins,
|
|
251
|
+
profilePlugins: payload?.profilePlugins,
|
|
252
|
+
defaultProfile: payload?.defaultProfile,
|
|
253
|
+
// v5 r2 兼容性字段(UI「兼容性」组):dshVersions 枚举集 + launchers 兼容声明(透传 buildManifest)
|
|
254
|
+
dshVersions:
|
|
255
|
+
Array.isArray(payload?.dshVersions) && payload.dshVersions.length ? payload.dshVersions : undefined,
|
|
256
|
+
launchers:
|
|
257
|
+
payload?.launchers && typeof payload.launchers === 'object' && !Array.isArray(payload.launchers) && Object.keys(payload.launchers).length
|
|
258
|
+
? payload.launchers
|
|
259
|
+
: undefined,
|
|
260
|
+
// v5 r2:vendoring 档位(workspace-config v1 r2):auto(默认)/ off / full
|
|
261
|
+
vendor: typeof payload?.vendor === 'string' && ['auto', 'off', 'full'].includes(payload.vendor) ? payload.vendor : undefined,
|
|
262
|
+
// v5 r2:UI 依赖清单手动勾选的内嵌项 { 坐标: reason }(transient,不进 .dshpkcfg 白名单)
|
|
263
|
+
vendorCoords:
|
|
264
|
+
payload?.vendorCoords && typeof payload.vendorCoords === 'object' && Object.keys(payload.vendorCoords).length
|
|
265
|
+
? payload.vendorCoords
|
|
266
|
+
: undefined,
|
|
267
|
+
force: payload?.force === true,
|
|
268
|
+
mode: payload?.mode,
|
|
269
|
+
content: payload?.content,
|
|
270
|
+
onProgress: tasks.progressBridge(id),
|
|
271
|
+
onOutput: tasks.logSink(id),
|
|
272
|
+
};
|
|
273
|
+
tasks.enqueue(async () => {
|
|
274
|
+
try {
|
|
275
|
+
const r = whole ? await exportHomeFromWorkspace(host,profile,overrides) : await exportFromWorkspace(host, profile, overrides);
|
|
276
|
+
tasks.finish(id, {
|
|
277
|
+
ok: true,
|
|
278
|
+
title: `导出 ${r.manifest?.name ?? profile.name}`,
|
|
279
|
+
result: r.output
|
|
280
|
+
? { mode: 'dspack', output: r.output, sha256: r.sha256, size: r.size, name: r.manifest?.name, version: r.manifest?.version }
|
|
281
|
+
: { mode: 'repo', dir: r.dir, name: r.manifest?.name, version: r.manifest?.version },
|
|
282
|
+
});
|
|
283
|
+
} catch (e) {
|
|
284
|
+
tasks.finish(id, { ok: false, error: String(e?.message ?? e) });
|
|
285
|
+
}
|
|
286
|
+
}).catch(() => {});
|
|
287
|
+
return { taskId: id };
|
|
288
|
+
},
|
|
289
|
+
|
|
290
|
+
// v5 r2:列出 profile 的依赖清单(UI「内嵌依赖」勾选列表数据源;无网络请求,纯本地)。
|
|
291
|
+
// kind ∈ npm(registry 精确版本)/ git(github sha)/ vendored(装过 vendored 包,可直接复用 tarball)。
|
|
292
|
+
'pack/export-choices': async ({ runtime, payload }) => {
|
|
293
|
+
const host = getHost();
|
|
294
|
+
const whole = payload?.type === 'dshhome';
|
|
295
|
+
const target = await exportTarget(runtime,host,payload);
|
|
296
|
+
return exportChoices(host, {profile:whole ? null : target,home:{dir:runtime.home},plugins:payload?.plugins,profilePlugins:payload?.profilePlugins});
|
|
297
|
+
},
|
|
298
|
+
|
|
299
|
+
'pack/dependencies': async ({ runtime, payload }) => {
|
|
300
|
+
const host = getHost();
|
|
301
|
+
const profile = await exportTarget(runtime,host,payload);
|
|
302
|
+
let dirs = [profile.dir];
|
|
303
|
+
if (payload?.type === 'dshhome') {
|
|
304
|
+
dirs = (await host.readdir(host.joinPath(profile.dir,'profiles')) || [])
|
|
305
|
+
.filter(entry=>entry.type === 'dir' && !['web','headless'].includes(entry.name)).map(entry=>entry.abs);
|
|
306
|
+
}
|
|
307
|
+
const deps = await listProfileDependencies(host,dirs);
|
|
308
|
+
return { profile: profile.name, dir: profile.dir, deps };
|
|
309
|
+
},
|
|
310
|
+
|
|
311
|
+
// 读取某个 profile 的工作区配置(.dshpkcfg);不存在/非法 → config: null。
|
|
312
|
+
'pack/config-load': async ({ runtime, payload }) => {
|
|
313
|
+
const host = getHost();
|
|
314
|
+
const profile = await exportTarget(runtime,host,payload);
|
|
315
|
+
const config = await loadWorkspaceConfig(host, profile.dir);
|
|
316
|
+
return { profile: profile.name, dir: profile.dir, config };
|
|
317
|
+
},
|
|
318
|
+
|
|
319
|
+
// 保存某个 profile 的工作区配置(.dshpkcfg);只落白名单字段、去 null/undefined。
|
|
320
|
+
'pack/config-save': async ({ runtime, payload }) => {
|
|
321
|
+
const host = getHost();
|
|
322
|
+
const profile = await exportTarget(runtime,host,payload);
|
|
323
|
+
const path = await saveWorkspaceConfig(host, profile.dir, payload ?? {});
|
|
324
|
+
return { profile: profile.name, dir: profile.dir, path };
|
|
325
|
+
},
|
|
326
|
+
|
|
327
|
+
'pack/view': async ({ payload }) => {
|
|
328
|
+
const host = getHost();
|
|
329
|
+
const src = need(payload?.source, '缺少 .dspack 路径或 URL');
|
|
330
|
+
const { path: p, tempDir } = await resolvePackSource(host, src);
|
|
331
|
+
try {
|
|
332
|
+
const r = await inspectPack(host, p);
|
|
333
|
+
return {
|
|
334
|
+
valid: r.valid,
|
|
335
|
+
name: r.manifest?.name ?? '',
|
|
336
|
+
version: r.manifest?.version ?? '',
|
|
337
|
+
sha256: r.sha256,
|
|
338
|
+
size: r.size,
|
|
339
|
+
totalEntries: r.totalEntries,
|
|
340
|
+
validation: r.validation ?? [],
|
|
341
|
+
// v5 r2(v3 §8.4 判定表):安装前 launchers 判定——警告放行、不硬拒。UI 据 warn 级
|
|
342
|
+
// (supported:false)弹确认对话框、info 级(版本不足 / 白名单未含本启动器)轻提示。
|
|
343
|
+
// 本插件注入官方桌面端(selfId=official-desktop);版本自报取 DSH_VERSION,
|
|
344
|
+
// 缺省按「版本未知」轻提示放行(与 pack/install 的判定参数一致,所见即所装)。
|
|
345
|
+
launchersWarnings: judgeLaunchers(
|
|
346
|
+
normalizeLaunchers(r.manifest?.launchers),
|
|
347
|
+
'official-desktop',
|
|
348
|
+
process.env.DSH_VERSION ?? null,
|
|
349
|
+
),
|
|
350
|
+
};
|
|
351
|
+
} finally {
|
|
352
|
+
if (tempDir) await host.rm(tempDir, { recursive: true, force: true }).catch(() => {});
|
|
353
|
+
}
|
|
354
|
+
},
|
|
355
|
+
|
|
356
|
+
'pack/preview-install': async ({ ctx, runtime, payload }) =>
|
|
357
|
+
installPack(getHost(), { ...installOptions(runtime, payload, ctx), dryRun: true }),
|
|
358
|
+
|
|
359
|
+
'pack/install': async ({ ctx, runtime, payload }) => {
|
|
360
|
+
const host = getHost();
|
|
361
|
+
need(payload?.source, '缺少 .dspack 路径或 URL');
|
|
362
|
+
|
|
363
|
+
const id = tasks.createTask({ kind: 'install', title: '安装整合包', home: runtime?.home });
|
|
364
|
+
tasks.enqueue(async () => {
|
|
365
|
+
try {
|
|
366
|
+
const log = tasks.logSink(id);
|
|
367
|
+
const r = await installPack(host, {
|
|
368
|
+
...installOptions(runtime, payload, ctx),
|
|
369
|
+
onProgress: tasks.progressBridge(id),
|
|
370
|
+
onOutput: log,
|
|
371
|
+
});
|
|
372
|
+
for (const item of r.items || []) {
|
|
373
|
+
if (item.notice) log('\n' + item.key + ' ' + item.notice + '\n');
|
|
374
|
+
}
|
|
375
|
+
tasks.finish(id, {
|
|
376
|
+
ok: true,
|
|
377
|
+
title: `安装 ${r.profileName}`,
|
|
378
|
+
result: { profileName: r.profileName, dir: r.dir, dryRun: r.dryRun === true, installed: r.installed === true, filesDownloaded: r.filesDownloaded ?? 0, launchers: r.launchers ?? null },
|
|
379
|
+
});
|
|
380
|
+
} catch (e) {
|
|
381
|
+
tasks.finish(id, { ok: false, error: String(e?.message ?? e) });
|
|
382
|
+
}
|
|
383
|
+
}).catch(() => {});
|
|
384
|
+
return { taskId: id };
|
|
385
|
+
},
|
|
386
|
+
|
|
387
|
+
'pack/market': async ({ payload }) => {
|
|
388
|
+
const host = getHost();
|
|
389
|
+
const index = await readMarketIndex(host, payload?.indexPath ?? DEFAULT_MARKET_INDEX);
|
|
390
|
+
return { packs: index?.packs ?? [], error: index?.error ?? null };
|
|
391
|
+
},
|
|
392
|
+
|
|
393
|
+
// 市场详情(v5 r2):懒加载 packs/<id>/manifest.json + README(fetchMarketPackDetail)。
|
|
394
|
+
// 中心索引不平铺 launchers / vendored / dshVersions(index 契约 §6.5),完整内容只有懒加载
|
|
395
|
+
// manifest 里有——r2 = pickR2Fields(manifest) 原文透传,badges = r2Badges(manifest) 结构化
|
|
396
|
+
// 徽标(文案由 UI 层按 kind 做 i18n),UI 详情弹窗直接渲染。
|
|
397
|
+
'pack/market-detail': async ({ payload }) => {
|
|
398
|
+
const host = getHost();
|
|
399
|
+
const entry = payload?.pack;
|
|
400
|
+
if (!entry || typeof entry !== 'object') throw new Error('缺少市场条目');
|
|
401
|
+
const d = await fetchMarketPackDetail(host, payload?.indexPath ?? DEFAULT_MARKET_INDEX, entry);
|
|
402
|
+
return { manifest: d.manifest, icons:d.icons, readme: d.readme, dir: d.dir, r2: d.r2, badges: r2Badges(d.manifest) };
|
|
403
|
+
},
|
|
404
|
+
|
|
405
|
+
'task/list': async () => ({ tasks: tasks.list() }),
|
|
406
|
+
|
|
407
|
+
// 启动器注册表(机器可读版,specs/launcher-registry.md 头部):「兼容性」编辑器渲染认领 ID +
|
|
408
|
+
// 显示名。带 1h 内存缓存;拉取失败 / 结构非法回落内置清单(fetchLaunchersRegistry 内置纪律)。
|
|
409
|
+
'launchers/registry': async () => {
|
|
410
|
+
const now = Date.now();
|
|
411
|
+
if (launchersRegistryCache && now - launchersRegistryCache.at < 60 * 60 * 1000) {
|
|
412
|
+
return launchersRegistryCache.value;
|
|
413
|
+
}
|
|
414
|
+
const value = await fetchLaunchersRegistry(getHost());
|
|
415
|
+
launchersRegistryCache = { at: now, value };
|
|
416
|
+
return value;
|
|
417
|
+
},
|
|
418
|
+
|
|
419
|
+
// About 页「检查更新」:拉 registry 最新版本并与本地比较(网络 I/O 走 host,与 ensure-manager 同款)。
|
|
420
|
+
'plugin/check-update': async () => {
|
|
421
|
+
const host = getHost();
|
|
422
|
+
const esc = MANAGER.replace('/', '%2F');
|
|
423
|
+
const tmp = await fsp.mkdtemp(path.join(os.tmpdir(), 'dspack-update-'));
|
|
424
|
+
try {
|
|
425
|
+
const metaPath = path.join(tmp, 'meta.json');
|
|
426
|
+
await host.download(`${NPM_REGISTRY}/${esc}/latest`, metaPath);
|
|
427
|
+
const text = await host.readTextFile(metaPath);
|
|
428
|
+
if (!text) throw new Error(`无法读取 ${MANAGER} 的 NPM 元数据`);
|
|
429
|
+
const latest = JSON.parse(text)?.version;
|
|
430
|
+
if (!latest) throw new Error(`${MANAGER} 尚未发布到 NPM`);
|
|
431
|
+
return {
|
|
432
|
+
current: PACKAGE_VERSION,
|
|
433
|
+
latest,
|
|
434
|
+
outdated: cmpVersion(latest, PACKAGE_VERSION) > 0,
|
|
435
|
+
npmUrl: NPM_URL,
|
|
436
|
+
};
|
|
437
|
+
} finally {
|
|
438
|
+
await fsp.rm(tmp, { recursive: true, force: true }).catch(() => {});
|
|
439
|
+
}
|
|
440
|
+
},
|
|
441
|
+
|
|
442
|
+
// About 页链接(作者/仓库/求 Star):用系统默认浏览器打开 http/https 链接。
|
|
443
|
+
'plugin/open-url': async ({ payload }) => {
|
|
444
|
+
const url = need(payload?.url, '缺少 URL');
|
|
445
|
+
if (!/^https?:\/\//i.test(url)) throw new Error('仅支持 http/https 链接');
|
|
446
|
+
const host = getHost();
|
|
447
|
+
const cmd = process.platform === 'win32' ? 'explorer'
|
|
448
|
+
: process.platform === 'darwin' ? 'open' : 'xdg-open';
|
|
449
|
+
const r = await host.exec(cmd, [url]);
|
|
450
|
+
if (r.error) throw new Error(`打开链接失败:${r.error}`);
|
|
451
|
+
return { opened: true };
|
|
452
|
+
},
|
|
453
|
+
|
|
454
|
+
'task/get': async ({ payload }) => tasks.get(payload?.id),
|
|
455
|
+
};
|