@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,234 @@
1
+ import { isMap, isSeq, parseDocument } from 'yaml';
2
+
3
+ const missing = Symbol('缺少成员');
4
+ const encoder = new TextEncoder();
5
+
6
+ function plain(value) {
7
+ return value !== null && value !== missing && typeof value === 'object' && !Array.isArray(value);
8
+ }
9
+
10
+ function itemId(value) {
11
+ if (typeof value === 'string') {
12
+ return ['text', value];
13
+ }
14
+ if (plain(value)) {
15
+ for (const field of ['id', 'name', 'key']) {
16
+ if (typeof value[field] === 'string') {
17
+ return [field, value[field]];
18
+ }
19
+ }
20
+ const keys = Object.keys(value);
21
+ if (keys.length === 1) {
22
+ const op = keys[0];
23
+ const payload = value[op];
24
+ if (Array.isArray(payload) && payload.length === 1 && plain(payload[0])) {
25
+ return [op, itemId(payload[0])];
26
+ }
27
+ if (typeof payload === 'string') {
28
+ return [op, payload];
29
+ }
30
+ return ['operation', op];
31
+ }
32
+ }
33
+ return null;
34
+ }
35
+
36
+ function config(path) {
37
+ return /\.(json|ya?ml)$/i.test(path) && !path.endsWith('/pnpm-lock.yaml');
38
+ }
39
+
40
+ function parse(path, text) {
41
+ if (/\.json$/i.test(path)) {
42
+ return { value: JSON.parse(text) };
43
+ }
44
+ const doc = parseDocument(text, { uniqueKeys: true });
45
+ if (doc.errors.length || doc.warnings.length) {
46
+ return null;
47
+ }
48
+ return { value: doc.toJS({ maxAliasCount: 32 }), doc };
49
+ }
50
+
51
+ // 基线仅保存结构和摘要。键名、数组标识与标量值均不作为明文持久化。
52
+ async function describe(host, value) {
53
+ const hash = (data) => host.sha256(encoder.encode(JSON.stringify(data)));
54
+ if (plain(value)) {
55
+ const fields = {};
56
+ const names = {};
57
+ for (const key of Object.keys(value).sort()) {
58
+ const id = await hash(['key', key]);
59
+ fields[id] = await describe(host, value[key]);
60
+ names[id] = key;
61
+ }
62
+ const digest = await hash(['map', Object.entries(fields).map(([key, v]) => [key, v.hash])]);
63
+ return { hash: digest, fields, names, value, type: 'map' };
64
+ }
65
+ if (Array.isArray(value)) {
66
+ const fields = {};
67
+ const order = [];
68
+ for (const entry of value) {
69
+ const identity = itemId(entry);
70
+ if (identity === null) {
71
+ return { hash: await hash(['value', value]), value };
72
+ }
73
+ const id = await hash(identity);
74
+ if (Object.hasOwn(fields, id)) {
75
+ return { hash: await hash(['value', value]), value };
76
+ }
77
+ fields[id] = await describe(host, entry);
78
+ order.push(id);
79
+ }
80
+ return { type: 'list', fields, order, value, hash: await hash(['list', order.map(id => [id, fields[id].hash])]) };
81
+ }
82
+ return { hash: await hash(['value', value]), value };
83
+ }
84
+
85
+ function baseline(node) {
86
+ const out = { hash: node.hash };
87
+ if (node.type) {
88
+ out.type = node.type;
89
+ out.fields = Object.fromEntries(Object.entries(node.fields).map(([k, child]) => [k, baseline(child)]));
90
+ }
91
+ if (node.order) {
92
+ out.order = node.order;
93
+ }
94
+ return out;
95
+ }
96
+
97
+ /** 为可按成员合并的配置生成摘要树,无法无损解析的文件按普通文件比较。 */
98
+ export async function configBaseline(host, path, text) {
99
+ if (!config(path)) {
100
+ return undefined;
101
+ }
102
+ let parsed;
103
+ try {
104
+ parsed = parse(path, text);
105
+ } catch {
106
+ return undefined;
107
+ }
108
+ if (!parsed || !plain(parsed.value) && !Array.isArray(parsed.value)) {
109
+ return undefined;
110
+ }
111
+ return baseline(await describe(host, parsed.value));
112
+ }
113
+
114
+ function sameOrder(a, b) {
115
+ return a.length === b.length && a.every((v, i) => v === b[i]);
116
+ }
117
+
118
+ // 以选定一侧的顺序为主,另一侧新增的成员仍跟随其前后的原有成员。
119
+ function combinedOrder(primary, secondary) {
120
+ const result = [...primary];
121
+ let prev;
122
+ for (const id of secondary) {
123
+ if (!result.includes(id)) {
124
+ result.splice(prev === undefined ? 0 : result.indexOf(prev) + 1, 0, id);
125
+ }
126
+ prev = id;
127
+ }
128
+ return result;
129
+ }
130
+
131
+ function memberChoice(context, trail, label) {
132
+ const key = JSON.stringify([context.path, ...trail]);
133
+ context.fields.push({key, label: label.join(' / ') || context.path});
134
+ return context.resolutions[key] ?? context.choice;
135
+ }
136
+
137
+ function merge(base, local, upstream, context, trail = [], label = []) {
138
+ if (local?.hash === upstream?.hash || upstream?.hash === base?.hash) {
139
+ return local ? local.value : missing;
140
+ }
141
+ if (local?.hash === base?.hash) {
142
+ return upstream ? upstream.value : missing;
143
+ }
144
+ if (local?.type && local.type === upstream?.type && (!base || local.type === base.type)) {
145
+ const values = new Map();
146
+ for (const id of new Set([...Object.keys(local.fields), ...Object.keys(upstream.fields)])) {
147
+ const entry = local.fields[id] || upstream.fields[id];
148
+ const title = local.type === 'map' ? (local.names[id] ?? upstream.names[id]) :
149
+ (entry.value?.id || entry.value?.name || entry.value?.key || '条目 ' + (Math.max(local.order.indexOf(id), upstream.order.indexOf(id)) + 1));
150
+ const value = merge(base?.fields?.[id], local.fields[id], upstream.fields[id], context, [...trail, id], [...label, title]);
151
+ if (value !== missing) {
152
+ values.set(id, value);
153
+ }
154
+ }
155
+ if (local.type === 'map') {
156
+ return Object.fromEntries([...values].map(([id, val]) => [local.names[id] ?? upstream.names[id], val]));
157
+ }
158
+ const common = new Set((base?.order ?? []).filter(id => local.fields[id] && upstream.fields[id]));
159
+ const before = (base?.order ?? []).filter(id => common.has(id));
160
+ const here = local.order.filter(id => common.has(id));
161
+ const next = upstream.order.filter(id => common.has(id));
162
+ const localMoved = !sameOrder(before, here);
163
+ const upstreamMoved = !sameOrder(before, next);
164
+ const orderConflict = localMoved && upstreamMoved && !sameOrder(here, next);
165
+ const orderChoice = orderConflict ? memberChoice(context, [...trail, '@order'], [...label, '排列顺序']) : null;
166
+ const useUpstream = upstreamMoved && (!localMoved || orderChoice === 'upstream');
167
+ const order = useUpstream ? combinedOrder(upstream.order, local.order) : combinedOrder(local.order, upstream.order);
168
+ return order.filter(id => values.has(id)).map(id => values.get(id));
169
+ }
170
+ const selected = memberChoice(context, trail, label) === 'upstream' ? upstream : local;
171
+ return selected ? selected.value : missing;
172
+ }
173
+
174
+ // 尽量复用本地 YAML 节点,保留未编辑配置的注释、引号和集合样式。
175
+ function editYaml(doc, node, old, value) {
176
+ if (JSON.stringify(old) === JSON.stringify(value)) {
177
+ return node;
178
+ }
179
+ if (isMap(node) && plain(old) && plain(value)) {
180
+ for (const key of Object.keys(old)) {
181
+ if (!Object.hasOwn(value, key)) {
182
+ node.delete(key);
183
+ }
184
+ }
185
+ for (const [key, next] of Object.entries(value)) {
186
+ node.set(key, Object.hasOwn(old, key) ? editYaml(doc, node.get(key, true), old[key], next) : doc.createNode(next));
187
+ }
188
+ return node;
189
+ }
190
+ if (isSeq(node) && Array.isArray(old) && Array.isArray(value)) {
191
+ const byId = new Map(old.map((v, i) => [JSON.stringify(itemId(v)), i]));
192
+ if (!byId.has('null') && byId.size === old.length) {
193
+ node.items = value.map(v => {
194
+ const i = byId.get(JSON.stringify(itemId(v)));
195
+ return i === undefined ? doc.createNode(v) : editYaml(doc, node.items[i], old[i], v);
196
+ });
197
+ return node;
198
+ }
199
+ }
200
+ return doc.createNode(value);
201
+ }
202
+
203
+ /** 比较两侧配置与上次安装的结构摘要,冲突选择仅覆盖发生冲突的成员。 */
204
+ export async function mergeConfig(host, path, baseTree, localText, nextText, choice, resolutions = {}) {
205
+ if (!baseTree || !config(path)) {
206
+ return null;
207
+ }
208
+ let local;
209
+ let next;
210
+ try {
211
+ local = parse(path, localText);
212
+ next = parse(path, nextText);
213
+ } catch {
214
+ return null;
215
+ }
216
+ if (!local || !next) {
217
+ return null;
218
+ }
219
+ const [here, upstream] = await Promise.all([describe(host, local.value), describe(host, next.value)]);
220
+ const fields = [];
221
+ const value = merge(baseTree, here, upstream, { path, choice, resolutions, fields });
222
+ if (value === missing) {
223
+ return null;
224
+ }
225
+ let text;
226
+ if (local.doc) {
227
+ local.doc.contents = editYaml(local.doc, local.doc.contents, local.value, value);
228
+ text = String(local.doc);
229
+ } else {
230
+ const indent = localText.match(/\n([ \t]+)"/)?.[1] ?? 2;
231
+ text = JSON.stringify(value, null, indent) + '\n';
232
+ }
233
+ return { text, conflicts: fields.length, fields };
234
+ }