@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.
Files changed (76) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +178 -3
  3. package/cordis.patch.yml +9 -0
  4. package/icons/folder-zip-line.svg +1 -0
  5. package/lib/ball.js +597 -0
  6. package/lib/client.js +2934 -0
  7. package/lib/host.js +51745 -0
  8. package/lib/migrate-helper.js +35346 -0
  9. package/lib/progress-main.cjs +79 -0
  10. package/lib/progress-window.ps1 +114 -0
  11. package/lib/progress.html +126 -0
  12. package/lib/publish-to-github/SKILL.md +132 -0
  13. package/package.json +77 -4
  14. package/src/ball-entry.js +5 -0
  15. package/src/ball-host.js +99 -0
  16. package/src/ball-settings.js +352 -0
  17. package/src/ball.js +418 -0
  18. package/src/channel.js +12 -0
  19. package/src/client-plugin.js +58 -0
  20. package/src/client-rpc.js +14 -0
  21. package/src/client.js +5 -0
  22. package/src/core/discovery.js +192 -0
  23. package/src/core/dspack.js +93 -0
  24. package/src/core/extension-files.js +102 -0
  25. package/src/core/extension-install.js +295 -0
  26. package/src/core/extension-patch.js +369 -0
  27. package/src/core/extension-skills.js +164 -0
  28. package/src/core/extension-validate.js +127 -0
  29. package/src/core/extensions.js +207 -0
  30. package/src/core/home-store.js +32 -0
  31. package/src/core/host.js +149 -0
  32. package/src/core/index.js +59 -0
  33. package/src/core/inspect.js +162 -0
  34. package/src/core/install.js +775 -0
  35. package/src/core/managed-install.js +84 -0
  36. package/src/core/manifest.js +717 -0
  37. package/src/core/market.js +268 -0
  38. package/src/core/mcp-composition.js +189 -0
  39. package/src/core/mcp-install.js +112 -0
  40. package/src/core/mcp-transport.js +116 -0
  41. package/src/core/pack.js +371 -0
  42. package/src/core/plugin-metadata.js +355 -0
  43. package/src/core/registry.js +316 -0
  44. package/src/core/repo.js +339 -0
  45. package/src/core/scan.js +78 -0
  46. package/src/core/security.js +101 -0
  47. package/src/core/special.js +50 -0
  48. package/src/core/tar.js +103 -0
  49. package/src/core/update-config.js +234 -0
  50. package/src/core/update.js +792 -0
  51. package/src/core/vendored.js +715 -0
  52. package/src/core/workspace.js +95 -0
  53. package/src/endpoints.js +455 -0
  54. package/src/ensure-manager.js +186 -0
  55. package/src/host-node.js +625 -0
  56. package/src/host.js +17 -0
  57. package/src/index.d.ts +7 -0
  58. package/src/index.js +39 -0
  59. package/src/junction.js +142 -0
  60. package/src/migrate-helper.js +225 -0
  61. package/src/migrate.js +56 -0
  62. package/src/packaged.js +9 -0
  63. package/src/profiles.js +106 -0
  64. package/src/progress-main.cjs +79 -0
  65. package/src/progress-window.ps1 +114 -0
  66. package/src/progress.html +126 -0
  67. package/src/progress.js +132 -0
  68. package/src/rpc.js +157 -0
  69. package/src/runtime.js +43 -0
  70. package/src/section-gate.js +50 -0
  71. package/src/settings-nav-icon.js +188 -0
  72. package/src/settings.js +1611 -0
  73. package/src/skills/index.js +90 -0
  74. package/src/skills/publish-to-github/SKILL.md +132 -0
  75. package/src/tasks.js +145 -0
  76. package/src/v6-dialogs.js +312 -0
@@ -0,0 +1,192 @@
1
+ // Profile 发现:经典路径 + DSH 启动器实例(双源),以及精确 dshVersion 枚举。
2
+ // 主机无关:平台差异(homedir / %APPDATA% / XDG)通过 Host.env/homedir/joinPath 抽象。
3
+
4
+ const SKIP_DIRS = new Set(['node_modules', '__temp__']);
5
+
6
+ /** 判断 profile 目录名是否跳过(点目录 / 脚手架 / 依赖)。 */
7
+ export function skipProfileDir(name) {
8
+ return typeof name !== 'string' || name.startsWith('.') || SKIP_DIRS.has(name);
9
+ }
10
+
11
+ /** 启动器 config.json 候选位置(Windows / Linux / macOS)。 */
12
+ export function launcherConfigCandidates(host) {
13
+ const c = [];
14
+ const appData = host.env('APPDATA');
15
+ if (appData) c.push(host.joinPath(appData, 'in.dsh-plug.dsh-launcher', 'config.json'));
16
+ const xdg = host.env('XDG_CONFIG_HOME') || host.joinPath(host.homedir(), '.config');
17
+ c.push(host.joinPath(xdg, 'in.dsh-plug.dsh-launcher', 'config.json'));
18
+ c.push(host.joinPath(xdg, 'dsh-launcher', 'config.json'));
19
+ return c;
20
+ }
21
+
22
+ async function readJson(host, p) {
23
+ const raw = await host.readTextFile(p);
24
+ if (raw == null) return null;
25
+ try {
26
+ return JSON.parse(raw);
27
+ } catch {
28
+ return null;
29
+ }
30
+ }
31
+
32
+ async function findLauncherConfig(host, override) {
33
+ if (override !== undefined) return override || null; // 空串/null 显式禁用探测
34
+ for (const p of launcherConfigCandidates(host)) {
35
+ if ((await host.stat(p)) != null) return p;
36
+ }
37
+ return null;
38
+ }
39
+
40
+ async function readLauncherConfig(host, override) {
41
+ const p = await findLauncherConfig(host, override);
42
+ if (!p) return null;
43
+ return await readJson(host, p);
44
+ }
45
+
46
+ /**
47
+ * 发现全部 profile(经典 + 启动器,按目录去重)。
48
+ * @param {Host} host
49
+ * @param {object} opts { home?, launcherConfig? } 测试可注入;launcherConfig 为 '' 表示禁用。
50
+ * @returns {{profiles: Array<{name,dir,source,home}>, roots: Array<{source,root,home}>}}
51
+ * source ∈ 'classic' | 'launcher';home 为启动器实例名(classic 为 null)。
52
+ */
53
+ export async function discoverProfiles(host, opts = {}) {
54
+ const home = opts.home || host.homedir();
55
+ const roots = [];
56
+ const profiles = [];
57
+ const seen = new Set(); // 去重(绝对目录)
58
+
59
+ const collect = async (rootDir, source, homeName) => {
60
+ roots.push({ source, root: rootDir, home: homeName ?? null });
61
+ const entries = await host.readdir(rootDir);
62
+ if (!Array.isArray(entries)) return;
63
+ const dirs = entries.filter((e) => e.type === 'dir' && !skipProfileDir(e.name));
64
+ dirs.sort((a, b) => a.name.localeCompare(b.name));
65
+ for (const e of dirs) {
66
+ if (seen.has(e.abs)) continue;
67
+ seen.add(e.abs);
68
+ profiles.push({ name: e.name, dir: e.abs, source, home: homeName ?? null });
69
+ }
70
+ };
71
+
72
+ // 1) 经典 ~/.dsh/profiles
73
+ await collect(host.joinPath(home, '.dsh', 'profiles'), 'classic', null);
74
+
75
+ // 2) 启动器 homes[](config.json 是权威注册表)
76
+ const cfg = await readLauncherConfig(host, opts.launcherConfig);
77
+ for (const h of cfg?.homes ?? []) {
78
+ if (!h?.path) continue;
79
+ await collect(host.joinPath(h.path, 'profiles'), 'launcher', h.name || h.id || h.path);
80
+ }
81
+
82
+ return { profiles, roots };
83
+ }
84
+
85
+ /**
86
+ * 发现全部 DSH_HOME 实例(经典默认 home + 启动器 homes[])。
87
+ * dshhome 导出 / 安装以「整个 home」为单元,故需要 home 而非 profile 粒度。
88
+ * @param {Host} host
89
+ * @param {object} opts { home?, launcherConfig? }
90
+ * @returns {Array<{name, dir, source}>} source ∈ 'classic' | 'launcher'
91
+ */
92
+ export async function discoverHomes(host, opts = {}) {
93
+ const defaultHome = opts.home || host.homedir();
94
+ const homes = [];
95
+ const seen = new Set();
96
+
97
+ const add = (dir, name, source) => {
98
+ const abs = host.resolvePath(dir);
99
+ if (seen.has(abs)) return;
100
+ seen.add(abs);
101
+ homes.push({ name, dir: abs, source });
102
+ };
103
+
104
+ // 1) 经典 ~/.dsh(默认单实例)
105
+ add(host.joinPath(defaultHome, '.dsh'), 'default', 'classic');
106
+
107
+ // 2) 启动器 homes[](config.json 是权威注册表)
108
+ const cfg = await readLauncherConfig(host, opts.launcherConfig);
109
+ for (const h of cfg?.homes ?? []) {
110
+ if (!h?.path) continue;
111
+ add(h.path, h.name || h.id || h.path, 'launcher');
112
+ }
113
+
114
+ return homes;
115
+ }
116
+
117
+
118
+ /** 仅在目标 HOME 或整合包版本能确定本机 DSH 安装时,返回解析内置 bundle 的 package.json。 */
119
+ export async function findLauncherInstallAnchor(host, home, version) {
120
+ const config = await readLauncherConfig(host);
121
+ if (!Array.isArray(config?.versions)) return undefined;
122
+ const key = path => {
123
+ const abs = host.resolvePath(path);
124
+ return abs.includes('\\') ? abs.toLowerCase() : abs;
125
+ };
126
+ const target = key(home);
127
+ const homes = new Set((Array.isArray(config.homes) ? config.homes : [])
128
+ .filter(row => typeof row?.path === 'string' && row.path && key(row.path) === target)
129
+ .map(row => row.id));
130
+ const active = new Set((Array.isArray(config.instances) ? config.instances : [])
131
+ .filter(row => row && homes.has(row.home_id)).map(row => row.version_id));
132
+ if (!active.size && !version) return undefined;
133
+ const matches = config.versions.filter(row => row && !row.wsl && typeof row.dir === 'string' && row.dir &&
134
+ (!active.size || active.has(row.id)) && (!version || row.version === version));
135
+ const dirs = [...new Set(matches.map(row => host.resolvePath(row.dir)))];
136
+ if (dirs.length !== 1) return undefined;
137
+ for (const location of ['apps/cli/package.json', 'node_modules/@deepseek-ai/dsh/package.json']) {
138
+ const anchor = host.joinPath(dirs[0], location);
139
+ const pkg = await readJson(host, anchor);
140
+ if (pkg?.name === '@deepseek-ai/dsh' && (!version || pkg.version === version)) return anchor;
141
+ }
142
+ return undefined;
143
+ }
144
+
145
+ /** 枚举启动器已安装的 DSH 版本(精确版本号),降序。 */
146
+ export async function listInstalledDshVersions(host, opts = {}) {
147
+ const cfg = await readLauncherConfig(host, opts.launcherConfig);
148
+ const versions = (cfg?.versions ?? []).map((v) => v?.version).filter((v) => typeof v === 'string' && v);
149
+ return sortVersionsDesc(versions);
150
+ }
151
+
152
+ /**
153
+ * 解析 profile 输入:路径优先(手动选择目录),其次按名字匹配已发现 profile。
154
+ * @returns {{name,dir,source,home}|null}
155
+ */
156
+ export async function resolveProfileInput(host, input, opts = {}) {
157
+ if (!input) return null;
158
+ const abs = host.resolvePath(input);
159
+ const st = await host.stat(abs);
160
+ if (st?.isDirectory) {
161
+ return { name: host.basename(abs), dir: abs, source: 'custom', home: null };
162
+ }
163
+ const { profiles } = await discoverProfiles(host, opts);
164
+ const hits = profiles.filter((p) => p.name === input);
165
+ return hits.length ? hits[0] : null;
166
+ }
167
+
168
+ /* ------------------- 版本比较(用于 dshVersion 精确钉定) ------------------- */
169
+
170
+ function parseVersion(v) {
171
+ const m = String(v).match(/^(\d+)\.(\d+)\.(\d+)(?:-([A-Za-z]+)\.(\d+))?/);
172
+ if (!m) return { num: [0, 0, 0], pre: null };
173
+ return { num: [+m[1], +m[2], +m[3]], pre: m[4] ? +m[5] : null };
174
+ }
175
+
176
+ /** 比较 a、b:a>b 返回正,相等返回 0。正式版 > 预发布。 */
177
+ export function compareVersions(a, b) {
178
+ const pa = parseVersion(a);
179
+ const pb = parseVersion(b);
180
+ for (let i = 0; i < 3; i++) {
181
+ if (pa.num[i] !== pb.num[i]) return pa.num[i] - pb.num[i];
182
+ }
183
+ if (pa.pre === null && pb.pre === null) return 0;
184
+ if (pa.pre === null) return 1;
185
+ if (pb.pre === null) return -1;
186
+ return pa.pre - pb.pre;
187
+ }
188
+
189
+ /** 降序排序版本号。 */
190
+ export function sortVersionsDesc(versions) {
191
+ return [...versions].sort((a, b) => compareVersions(b, a));
192
+ }
@@ -0,0 +1,93 @@
1
+ import { validatePluginAssets } from './plugin-metadata.js';
2
+ import { validateExtensions } from './extension-validate.js';
3
+ // .dspack 容器 = 标准 ZIP(无自定义魔数头,压缩软件可直接打开)。
4
+ // 格式识别由根 dspack.json(容器标记)+ 根 manifest.json(manifestVersion/type)共同承担,
5
+ // 见 DSH-PackForge/specs/pack-structure/v4.md(统一覆盖 profile 与 dshhome)。
6
+ import { zipSync, unzipSync } from 'fflate';
7
+
8
+ const encoder = new TextEncoder();
9
+ const decoder = new TextDecoder();
10
+
11
+ /** 容器标记固定 format 值。 */
12
+ export const DSPACK_FORMAT = 'dspack';
13
+
14
+ /** 容器版本:统一为 4(pack-structure v4,覆盖 profile / dshhome 两种形态)。 */
15
+ export const DSPACK_CONTAINER_VERSION = 4;
16
+
17
+ /** 生成根标记文件 dspack.json 的内容。 */
18
+ export function dspackMarker(version) {
19
+ return { format: DSPACK_FORMAT, version };
20
+ }
21
+
22
+ /**
23
+ * 打包:entries 为 { rel: Uint8Array }(rel 用 '/' 分隔)→ 标准 ZIP 字节。
24
+ * 纯 ZIP,不关心标记;调用方(pack.js)负责把 dspack.json / manifest.json 放进 entries。
25
+ * 整包 sha256 覆盖全部 ZIP 字节。
26
+ */
27
+ export function buildDspack(entries) {
28
+ return zipSync(entries);
29
+ }
30
+
31
+ /**
32
+ * 解包:标准 ZIP 字节 → { entries, marker }。
33
+ * 非 ZIP(或空内容)抛错;marker 为根 dspack.json(format 必须是 "dspack"),
34
+ * 缺标记时 marker = null(兼容旧包 / 测试包,靠 manifest 识别)。
35
+ * @returns {{ entries: Record<string, Uint8Array>, marker: {format:string,version:number}|null }}
36
+ */
37
+ export function parseDspack(bytes) {
38
+ if (!(bytes instanceof Uint8Array) || bytes.length === 0) {
39
+ throw new Error('不是有效的 .dspack 文件(空内容)');
40
+ }
41
+ let entries;
42
+ try {
43
+ entries = unzipSync(bytes);
44
+ } catch {
45
+ throw new Error('不是有效的 .dspack 文件(无法按 ZIP 解压)');
46
+ }
47
+ const marker = parseMarker(entries);
48
+ let manifest;
49
+ if (entries['manifest.json']) {
50
+ try {
51
+ manifest = JSON.parse(decodeText(entries['manifest.json']));
52
+ } catch {
53
+ throw new Error('manifest.json 不是有效 JSON');
54
+ }
55
+ }
56
+ if (marker && (!Number.isInteger(marker.version) || marker.version < 1 || marker.version > DSPACK_CONTAINER_VERSION)) {
57
+ throw new Error('不支持的 dspack.json 容器版本');
58
+ }
59
+ if ((manifest?.manifestVersion === 6) !== (marker?.version === 4)) {
60
+ throw new Error('manifest v6 必须与容器 v4 配套');
61
+ }
62
+ if (manifest?.manifestVersion === 6) {
63
+ validatePluginAssets(manifest, entries);
64
+ validateExtensions(manifest, entries);
65
+ }
66
+ return { entries, marker };
67
+ }
68
+
69
+ /** 读根 dspack.json:缺失 → null;format 非 "dspack" → 拒载(外来 ZIP / 损坏)。 */
70
+ function parseMarker(entries) {
71
+ const raw = entries['dspack.json'];
72
+ if (!raw) return null;
73
+ let m;
74
+ try {
75
+ m = JSON.parse(decodeText(raw));
76
+ } catch {
77
+ throw new Error('dspack.json 不是有效 JSON');
78
+ }
79
+ if (!m || typeof m !== 'object' || Array.isArray(m) || m.format !== DSPACK_FORMAT) {
80
+ throw new Error('dspack.json 不是有效的容器标记(format 必须为 "dspack")');
81
+ }
82
+ return m;
83
+ }
84
+
85
+ /** 文本 → UTF-8 字节(不落盘用)。 */
86
+ export function encodeText(s) {
87
+ return encoder.encode(s);
88
+ }
89
+
90
+ /** UTF-8 字节 → 文本。 */
91
+ export function decodeText(u8) {
92
+ return decoder.decode(u8);
93
+ }
@@ -0,0 +1,102 @@
1
+ import { isExcluded } from './security.js';
2
+
3
+ const invalidSegment = /[\\:\x00-\x1f<>|?*"]|[. ]$/;
4
+ const reserved = /^(?:con|prn|aux|nul|com[1-9]|lpt[1-9])(?:\.|$)/i;
5
+ const forbidden = new Set(['.git', '.dsh-pack', 'node_modules', '.credentials.yaml', 'settings.yaml']);
6
+
7
+ export function relativePath(path) {
8
+ if (typeof path !== 'string' || !path || path.length > 1024) throw new Error('扩展资源路径无效');
9
+ for (const part of path.split('/')) {
10
+ if (!part || part === '.' || part === '..' || invalidSegment.test(part) || reserved.test(part) ||
11
+ forbidden.has(part.toLowerCase()) || isExcluded(part.toLowerCase())) {
12
+ throw new Error('扩展资源包含禁止的路径片段');
13
+ }
14
+ }
15
+ return path;
16
+ }
17
+
18
+ export function segment(id) {
19
+ if (relativePath(id).includes('/')) throw new Error('扩展资源标识不能包含路径分隔符');
20
+ return id;
21
+ }
22
+
23
+ export function localized(value, fallback) {
24
+ if (value == null) return fallback;
25
+ if (typeof value === 'string') return value;
26
+ if (!value || Array.isArray(value) || typeof value !== 'object' || !Object.entries(value).every(
27
+ ([key, text]) => /^[a-zA-Z0-9_-]+$/.test(key) && typeof text === 'string',
28
+ )) throw new Error('名称和描述必须是文本或语言映射');
29
+ return value;
30
+ }
31
+
32
+ export function expandTemplate(template, profile) {
33
+ if (typeof template !== 'string') throw new Error('paths 需要字符串路径');
34
+ let path = template.replace(/^\.\//, '');
35
+ if (path.startsWith('<profile>/')) path = 'profiles/' + path;
36
+ if (path.includes('<profile>')) {
37
+ segment(profile);
38
+ path = path.split('/').map(part => part === '<profile>' ? profile : part).join('/');
39
+ }
40
+ return relativePath(path.replace(/\/$/, ''));
41
+ }
42
+
43
+ // 读取每级目录项而非仅检查最终拼接路径,避免沿符号链接离开宿主给定的根。
44
+ export async function safePath(host, root, rel) {
45
+ relativePath(rel);
46
+ let dir = root;
47
+ for (const part of rel.split('/')) {
48
+ const entries = await host.readdir(dir);
49
+ const entry = entries?.find(e => e.name.toLowerCase() === part.toLowerCase());
50
+ if (entry?.type === 'symlink') throw new Error('扩展资源路径包含符号链接:' + rel);
51
+ dir = host.joinPath(dir, part);
52
+ }
53
+ return dir;
54
+ }
55
+
56
+ export async function readTree(host, root, options = {}) {
57
+ const files = new Map();
58
+ async function visit(dir, prefix) {
59
+ const list = await host.readdir(dir);
60
+ if (!list) throw new Error('无法读取扩展资源目录');
61
+ if (!list.length && options.emptyDirs) options.emptyDirs.push(prefix.replace(/\/$/, ''));
62
+ for (const entry of list) {
63
+ const path = prefix + entry.name;
64
+ if (entry.name === '.git' || entry.name === '.dsh-skill.json' || isExcluded(path.toLowerCase())) continue;
65
+ relativePath(path);
66
+ if (entry.type === 'symlink') {
67
+ if (options.rejectLinks) throw new Error('远程技能含符号链接:' + path);
68
+ continue;
69
+ }
70
+ if (entry.type === 'dir') await visit(entry.abs, path + '/');
71
+ if (entry.type === 'file') {
72
+ const data = await host.readFile(entry.abs);
73
+ if (!data) throw new Error('读取资源失败:' + path);
74
+ files.set(path, data);
75
+ }
76
+ }
77
+ }
78
+ await visit(root, '');
79
+ return files;
80
+ }
81
+
82
+ export async function digestFiles(host, files) {
83
+ const digests = [];
84
+ for (const path of [...files.keys()].sort()) digests.push([path, await host.sha256(files.get(path))]);
85
+ return host.sha256(new TextEncoder().encode(JSON.stringify(digests)));
86
+ }
87
+
88
+ export async function readJson(host, path) {
89
+ const raw = await host.readTextFile(path);
90
+ if (raw == null) return null;
91
+ try { return JSON.parse(raw); } catch { throw new Error('JSON 格式错误:' + path); }
92
+ }
93
+
94
+ export function protectedTarget(rel) {
95
+ const lower = rel.toLowerCase();
96
+ const parts = lower.split('/');
97
+ const rootEntry = parts[0] === 'profiles' ? parts[2] : parts[0];
98
+ return ['cordis.patch.yml','package.json','pnpm-workspace.yaml','pnpm-lock.yaml'].includes(rootEntry) ||
99
+ parts.includes('.dsh-skill.json') || parts[0] === 'attachments' ||
100
+ parts[0] === 'skills' || parts[0] === '.agent-presets' ||
101
+ ['provider.json','mcp.json','path.json','skill.json','plugins.json'].includes(lower);
102
+ }