@yugoo-design/dsh-design-studio 0.0.0-stage → 0.1.0-beta.2
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 +202 -0
- package/NOTICE +18 -0
- package/README.md +75 -2
- package/THIRD_PARTY_NOTICES.md +35 -0
- package/lib/index.d.ts +738 -0
- package/lib/index.js +1423 -0
- package/licenses/dsh-distribution.json +54 -0
- package/package.json +35 -4
package/lib/index.js
ADDED
|
@@ -0,0 +1,1423 @@
|
|
|
1
|
+
import { basename, extname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
2
|
+
import { DesignBundleVersion, DesignKitVersion, compileSchema, loadDesignBundle, loadDesignKit } from "@yugoo-design/dsh-design-core";
|
|
3
|
+
import { createHash } from "node:crypto";
|
|
4
|
+
//#region src/types.ts
|
|
5
|
+
/**
|
|
6
|
+
* Thrown by a Bundle source port when a file exists but is not decodable
|
|
7
|
+
* UTF-8 text. The Studio surfaces binary content as an explicit diagnostic
|
|
8
|
+
* instead of lossy-decoding it into a text viewer.
|
|
9
|
+
*/
|
|
10
|
+
var StudioBinaryContentError = class extends Error {
|
|
11
|
+
/** The path whose content is not UTF-8 text. */
|
|
12
|
+
path;
|
|
13
|
+
/** @param path - absolute path of the non-text file. */
|
|
14
|
+
constructor(path) {
|
|
15
|
+
super(`design studio: ${JSON.stringify(path)} is not UTF-8 text`);
|
|
16
|
+
this.name = "StudioBinaryContentError";
|
|
17
|
+
this.path = path;
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
//#endregion
|
|
21
|
+
//#region src/snapshot.ts
|
|
22
|
+
/**
|
|
23
|
+
* Project one Core-compiled Kit into its version-addressed snapshot form.
|
|
24
|
+
* @param kit - compiled Kit from the host catalog authority.
|
|
25
|
+
* @returns the frozen snapshot entry retaining the definition by reference.
|
|
26
|
+
*/
|
|
27
|
+
function snapshotKit(kit) {
|
|
28
|
+
return Object.freeze({
|
|
29
|
+
name: kit.name,
|
|
30
|
+
version: String(kit.version),
|
|
31
|
+
definition: kit,
|
|
32
|
+
bundles: Object.freeze(kit.skills.map((skill) => Object.freeze({
|
|
33
|
+
name: skill.catalogName ?? skill.name,
|
|
34
|
+
version: String(skill.version),
|
|
35
|
+
definition: skill
|
|
36
|
+
})))
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
//#endregion
|
|
40
|
+
//#region src/load.ts
|
|
41
|
+
/** Studio-owned loading of a Kit from its directory source. */
|
|
42
|
+
async function loadStudioKit(fs, kitRoot, options = {}) {
|
|
43
|
+
const root = resolve(kitRoot);
|
|
44
|
+
return await loadDesignKit(fs, root, {
|
|
45
|
+
sourceId: options.sourceId ?? `directory:${root}`,
|
|
46
|
+
...options.bundles ? { resolveBundle: async (pin) => {
|
|
47
|
+
const location = options.bundles.find((entry) => entry.name === pin.name && entry.version === pin.version);
|
|
48
|
+
return location ? loadDesignBundle(fs, location, pin) : void 0;
|
|
49
|
+
} } : {}
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
//#endregion
|
|
53
|
+
//#region src/validate.ts
|
|
54
|
+
/**
|
|
55
|
+
* Explicit Kit/Bundle definition validation for the Studio editing experience.
|
|
56
|
+
*
|
|
57
|
+
* Unlike catalog enumeration (which fails loud on an illegal Kit), validation
|
|
58
|
+
* captures the Design Core loader's known content errors and returns them as
|
|
59
|
+
* bounded, locatable diagnostics so an author can repair a working copy. A valid
|
|
60
|
+
* definition reports an empty diagnostic list; an invalid one never degrades
|
|
61
|
+
* to a silently empty result. This validates Bundle DEFINITIONS, not design
|
|
62
|
+
* state instances (that is the runtime's `validateSkillDesign`).
|
|
63
|
+
*/
|
|
64
|
+
const REQUIRED_BUNDLE_SLOTS = [
|
|
65
|
+
"SKILL.md",
|
|
66
|
+
"skill.yaml",
|
|
67
|
+
"schema.yaml",
|
|
68
|
+
"validation.yaml"
|
|
69
|
+
];
|
|
70
|
+
function errorMessage(error) {
|
|
71
|
+
return error instanceof Error ? error.message : String(error);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Validate one Kit definition at `kitRoot`.
|
|
75
|
+
* @param fs - injected read file-ops port.
|
|
76
|
+
* @param kitRoot - absolute directory holding the Kit.
|
|
77
|
+
* @returns a report; empty diagnostics exactly when the Kit loads.
|
|
78
|
+
*/
|
|
79
|
+
async function validateKit(fs, kitRoot) {
|
|
80
|
+
try {
|
|
81
|
+
await loadStudioKit(fs, kitRoot);
|
|
82
|
+
return {
|
|
83
|
+
valid: true,
|
|
84
|
+
diagnostics: []
|
|
85
|
+
};
|
|
86
|
+
} catch (error) {
|
|
87
|
+
return {
|
|
88
|
+
valid: false,
|
|
89
|
+
diagnostics: [{
|
|
90
|
+
code: "kit.load",
|
|
91
|
+
message: errorMessage(error),
|
|
92
|
+
subject: basename(kitRoot)
|
|
93
|
+
}]
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Validate one Bundle definition inside an existing Kit.
|
|
99
|
+
*
|
|
100
|
+
* Reports missing required slots, then loads the Kit and attributes any loader
|
|
101
|
+
* content error or a missing Mindset-catalog reference to the Bundle.
|
|
102
|
+
* @param fs - injected read file-ops port.
|
|
103
|
+
* @param kitRoot - absolute directory of the containing Kit.
|
|
104
|
+
* @param name - the Bundle id (its `skills/<name>/` directory).
|
|
105
|
+
* @returns a report; empty diagnostics exactly when the Bundle loads as part of the Kit.
|
|
106
|
+
*/
|
|
107
|
+
async function validateBundle(fs, kitRoot, name) {
|
|
108
|
+
const bundleRoot = join(kitRoot, "skills", name);
|
|
109
|
+
const missing = [];
|
|
110
|
+
for (const slot of REQUIRED_BUNDLE_SLOTS) {
|
|
111
|
+
const text = await fs.readText(join(bundleRoot, slot));
|
|
112
|
+
if (text === void 0 || text.trim() === "") missing.push({
|
|
113
|
+
code: "bundle.missing_slot",
|
|
114
|
+
message: `Bundle ${JSON.stringify(name)} is missing required slot ${slot}`,
|
|
115
|
+
subject: name
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
if (missing.length > 0) return {
|
|
119
|
+
valid: false,
|
|
120
|
+
diagnostics: missing
|
|
121
|
+
};
|
|
122
|
+
try {
|
|
123
|
+
if (!(await loadStudioKit(fs, kitRoot)).skills.some((skill) => skill.name === name)) return {
|
|
124
|
+
valid: false,
|
|
125
|
+
diagnostics: [{
|
|
126
|
+
code: "bundle.not_in_catalog",
|
|
127
|
+
message: `Bundle ${JSON.stringify(name)} is not referenced by the Mindset <design_skills> catalog`,
|
|
128
|
+
subject: name
|
|
129
|
+
}]
|
|
130
|
+
};
|
|
131
|
+
return {
|
|
132
|
+
valid: true,
|
|
133
|
+
diagnostics: []
|
|
134
|
+
};
|
|
135
|
+
} catch (error) {
|
|
136
|
+
return {
|
|
137
|
+
valid: false,
|
|
138
|
+
diagnostics: [{
|
|
139
|
+
code: "bundle.load",
|
|
140
|
+
message: errorMessage(error),
|
|
141
|
+
subject: name
|
|
142
|
+
}]
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
//#endregion
|
|
147
|
+
//#region src/panel-preview.ts
|
|
148
|
+
/** Definition-only Panel preview preparation; never writes or runs a Bundle. */
|
|
149
|
+
function failure(code, message) {
|
|
150
|
+
return { diagnostics: [{
|
|
151
|
+
code,
|
|
152
|
+
message
|
|
153
|
+
}] };
|
|
154
|
+
}
|
|
155
|
+
/** Shared finalization over a real Core-compiled Bundle, with optional real Kit context. */
|
|
156
|
+
function finalizeStudioPanelPreview(input) {
|
|
157
|
+
const { compiled, revision, sampleSource } = input;
|
|
158
|
+
const bundle = { ...input.bundle }, kit = input.kit === void 0 ? void 0 : { ...input.kit };
|
|
159
|
+
if (!Number.isSafeInteger(revision) || revision < 0 || typeof sampleSource !== "string") return failure("preview.request", "Select an exact Bundle, text sample JSON and a non-negative editor revision.");
|
|
160
|
+
if ((compiled.catalogName ?? compiled.name) !== bundle.name || String(compiled.version) !== bundle.version) return failure("preview.bundle_pin", "The source does not match the selected Bundle version.");
|
|
161
|
+
const panelYaml = compiled.metadata?.panelYaml;
|
|
162
|
+
if (typeof panelYaml !== "string") return failure("preview.not_declarative", "Only an existing declarative Panel can be previewed.");
|
|
163
|
+
const schema = compiled.metadata?.schemaDocument;
|
|
164
|
+
if (schema === void 0) return failure("preview.definition", "The loader did not produce a declarative Panel and schema.");
|
|
165
|
+
if (new TextEncoder().encode(sampleSource).length > 262144) return failure("preview.size", "Sample JSON is limited to 256 KiB.");
|
|
166
|
+
let sample;
|
|
167
|
+
try {
|
|
168
|
+
sample = JSON.parse(sampleSource);
|
|
169
|
+
} catch {
|
|
170
|
+
return failure("preview.sample_json", "The preview sample must be valid JSON.");
|
|
171
|
+
}
|
|
172
|
+
if (sample === null || typeof sample !== "object" || Array.isArray(sample)) return failure("preview.sample_json", "The preview sample must be a JSON object.");
|
|
173
|
+
try {
|
|
174
|
+
const errors = compileSchema(schema)(sample);
|
|
175
|
+
if (errors.length > 0) return { diagnostics: errors.map((message) => ({
|
|
176
|
+
code: "preview.sample_schema",
|
|
177
|
+
message
|
|
178
|
+
})) };
|
|
179
|
+
return {
|
|
180
|
+
preview: {
|
|
181
|
+
revision,
|
|
182
|
+
...kit === void 0 ? {} : { kit },
|
|
183
|
+
bundle,
|
|
184
|
+
panelYaml,
|
|
185
|
+
sample
|
|
186
|
+
},
|
|
187
|
+
diagnostics: []
|
|
188
|
+
};
|
|
189
|
+
} catch (error) {
|
|
190
|
+
return failure("preview.definition", error instanceof Error ? error.message : String(error));
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Compile one Panel edit against an exact, host-resolved Kit snapshot.
|
|
195
|
+
*
|
|
196
|
+
* The host must supply an immutable/read-confined source port and root for the
|
|
197
|
+
* selected pin, not arbitrary browser paths or a mutable production project.
|
|
198
|
+
* Only panel.yaml is overlaid. All schemas, methods, locales and renderer
|
|
199
|
+
* contracts still come from that snapshot and pass through the runtime loader.
|
|
200
|
+
* The returned sample is fresh JSON and can be edited in a sandboxed preview;
|
|
201
|
+
* this function grants no artifact, filesystem, model or publication authority.
|
|
202
|
+
* A preview consumer must discard a result if its editor revision has changed.
|
|
203
|
+
*/
|
|
204
|
+
async function prepareStudioPanelPreview(source, kitRoot, request, loadOptions = {}) {
|
|
205
|
+
const { panelSource, sampleSource, revision } = request;
|
|
206
|
+
const kitPin = { ...request.kit };
|
|
207
|
+
const bundlePin = { ...request.bundle };
|
|
208
|
+
if (!isAbsolute(kitRoot) || !/^[a-z][a-z0-9_]*$/.test(bundlePin.name) || !Number.isSafeInteger(revision) || revision < 0 || typeof panelSource !== "string" || typeof sampleSource !== "string") return failure("preview.request", "Select an exact Bundle and a non-negative editor revision.");
|
|
209
|
+
if (new TextEncoder().encode(panelSource).length > 1048576 || new TextEncoder().encode(sampleSource).length > 262144) return failure("preview.size", "Panel source is limited to 1 MiB and sample JSON to 256 KiB.");
|
|
210
|
+
let sample;
|
|
211
|
+
try {
|
|
212
|
+
sample = JSON.parse(sampleSource);
|
|
213
|
+
} catch {
|
|
214
|
+
return failure("preview.sample_json", "The preview sample must be valid JSON.");
|
|
215
|
+
}
|
|
216
|
+
if (sample === null || typeof sample !== "object" || Array.isArray(sample)) return failure("preview.sample_json", "The preview sample must be a JSON object.");
|
|
217
|
+
try {
|
|
218
|
+
const base = await loadStudioKit(source, kitRoot, loadOptions);
|
|
219
|
+
if (base.name !== kitPin.name || String(base.version) !== kitPin.version) return failure("preview.kit_pin", "The source does not match the selected Kit version.");
|
|
220
|
+
const bundle = base.skills.find((skill) => (skill.catalogName ?? skill.name) === bundlePin.name);
|
|
221
|
+
if (bundle === void 0 || String(bundle.version) !== bundlePin.version) return failure("preview.bundle_pin", "The source does not match the selected Bundle version.");
|
|
222
|
+
if (typeof bundle.metadata?.panelYaml !== "string") return failure("preview.not_declarative", "Only an existing declarative Panel can be edited in this preview.");
|
|
223
|
+
const panelPath = resolve(bundle.resourceRoot ?? resolve(kitRoot, "skills", bundle.name), "panel.yaml");
|
|
224
|
+
const compiled = (await loadStudioKit({
|
|
225
|
+
readText: (path) => resolve(path) === panelPath ? Promise.resolve(panelSource) : source.readText(path),
|
|
226
|
+
...source.readResource ? { readResource: (path) => resolve(path) === panelPath ? Promise.resolve({
|
|
227
|
+
kind: "text",
|
|
228
|
+
content: panelSource
|
|
229
|
+
}) : source.readResource(path) } : {},
|
|
230
|
+
listDir: (path) => source.listDir(path),
|
|
231
|
+
...source.isDirectory ? { isDirectory: (path) => source.isDirectory(path) } : {},
|
|
232
|
+
...source.realPath ? { realPath: (path) => source.realPath(path) } : {}
|
|
233
|
+
}, kitRoot, loadOptions)).skills.find((skill) => (skill.catalogName ?? skill.name) === bundlePin.name);
|
|
234
|
+
if (compiled === void 0) return failure("preview.bundle_pin", "The source does not match the selected Bundle version.");
|
|
235
|
+
return finalizeStudioPanelPreview({
|
|
236
|
+
compiled,
|
|
237
|
+
revision,
|
|
238
|
+
kit: kitPin,
|
|
239
|
+
bundle: bundlePin,
|
|
240
|
+
sampleSource
|
|
241
|
+
});
|
|
242
|
+
} catch (error) {
|
|
243
|
+
return failure("preview.definition", error instanceof Error ? error.message : String(error));
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
//#endregion
|
|
247
|
+
//#region src/panel-drafts.ts
|
|
248
|
+
/** Host-neutral, versioned Panel working copies; not Kit publication. */
|
|
249
|
+
function checkText$1(panelSource, sampleSource) {
|
|
250
|
+
if (typeof panelSource !== "string" || typeof sampleSource !== "string" || new TextEncoder().encode(panelSource).length > 1048576 || new TextEncoder().encode(sampleSource).length > 262144) throw new Error("Panel source must be text ≤1 MiB; sample JSON text ≤256 KiB.");
|
|
251
|
+
}
|
|
252
|
+
function checkRevision$1(record, expected) {
|
|
253
|
+
if (!Number.isSafeInteger(expected) || record.revision !== expected) throw new Error("Draft changed elsewhere. Reopen the latest revision before saving.");
|
|
254
|
+
}
|
|
255
|
+
function content$1(record) {
|
|
256
|
+
const { id: _id, revision: _revision, ...value } = record;
|
|
257
|
+
return value;
|
|
258
|
+
}
|
|
259
|
+
/** Shared semantics used by host adapters; no filesystem or runtime tool access. */
|
|
260
|
+
var StudioPanelDraftService = class {
|
|
261
|
+
repository;
|
|
262
|
+
sources;
|
|
263
|
+
constructor(repository, sources) {
|
|
264
|
+
this.repository = repository;
|
|
265
|
+
this.sources = sources;
|
|
266
|
+
}
|
|
267
|
+
async list() {
|
|
268
|
+
return (await this.repository.list()).map(({ id, revision, kit, bundle, confirmed }) => ({
|
|
269
|
+
id,
|
|
270
|
+
revision,
|
|
271
|
+
kit,
|
|
272
|
+
bundle,
|
|
273
|
+
confirmed
|
|
274
|
+
}));
|
|
275
|
+
}
|
|
276
|
+
read(id) {
|
|
277
|
+
return this.repository.read(id);
|
|
278
|
+
}
|
|
279
|
+
async create(input) {
|
|
280
|
+
const kitPin = { ...input.kit }, bundlePin = { ...input.bundle };
|
|
281
|
+
if (!/^[a-z][a-z0-9_]*$/.test(bundlePin.name)) throw new Error("Select an exact Bundle identity.");
|
|
282
|
+
const sampleSource = input.sampleSource;
|
|
283
|
+
const snapshot = await this.sources.capture(kitPin);
|
|
284
|
+
return this.createRetained(snapshot, kitPin, bundlePin, sampleSource);
|
|
285
|
+
}
|
|
286
|
+
/** Open only the retained snapshot; imported/template pins need not be installed. */
|
|
287
|
+
async createFromSnapshot(input) {
|
|
288
|
+
const kit = { ...input.kit }, bundle = { ...input.bundle }, sampleSource = input.sampleSource;
|
|
289
|
+
if (!/^[a-z][a-z0-9_]*$/.test(bundle.name)) throw new Error("Select an exact Bundle identity.");
|
|
290
|
+
return this.createRetained(await this.sources.open(input.snapshotId), kit, bundle, sampleSource);
|
|
291
|
+
}
|
|
292
|
+
async createRetained(snapshot, kitPin, bundlePin, sampleSource) {
|
|
293
|
+
const kit = await loadStudioKit(snapshot.fs, snapshot.root, snapshot.loadOptions);
|
|
294
|
+
if (kit.name !== kitPin.name || String(kit.version) !== kitPin.version) throw new Error("Source does not match selected Kit pin.");
|
|
295
|
+
const bundle = kit.skills.find((entry) => entry.name === bundlePin.name && String(entry.version) === bundlePin.version);
|
|
296
|
+
if (!bundle || typeof bundle.metadata?.panelYaml !== "string") throw new Error("Select an installed declarative Bundle version.");
|
|
297
|
+
const panelSource = await snapshot.fs.readText(join(bundle.resourceRoot ?? join(snapshot.root, "skills", bundlePin.name), "panel.yaml"));
|
|
298
|
+
if (panelSource === void 0) throw new Error("Panel source is missing.");
|
|
299
|
+
const panel = bundle.metadata.panelManifest;
|
|
300
|
+
const sample = sampleSource ?? JSON.stringify(panel.initial_values ?? {}, null, 2);
|
|
301
|
+
checkText$1(panelSource, sample);
|
|
302
|
+
return this.repository.create({
|
|
303
|
+
kit: kitPin,
|
|
304
|
+
bundle: bundlePin,
|
|
305
|
+
baseSnapshot: snapshot.id,
|
|
306
|
+
panelSource,
|
|
307
|
+
sampleSource: sample,
|
|
308
|
+
confirmed: false
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
/** Saving incomplete source is allowed; confirmation requires successful compilation. */
|
|
312
|
+
async save(input) {
|
|
313
|
+
const { id, expectedRevision, panelSource, sampleSource } = input;
|
|
314
|
+
checkText$1(panelSource, sampleSource);
|
|
315
|
+
const current = await this.repository.read(id);
|
|
316
|
+
checkRevision$1(current, expectedRevision);
|
|
317
|
+
if (current.confirmed) throw new Error("Confirmed snapshot is locked. Create a new draft to edit it.");
|
|
318
|
+
return this.repository.replace(id, expectedRevision, {
|
|
319
|
+
...content$1(current),
|
|
320
|
+
panelSource,
|
|
321
|
+
sampleSource
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
/** Compile an unsaved editor revision without persisting or touching any project. */
|
|
325
|
+
async preview(input) {
|
|
326
|
+
const { id, expectedRevision, panelSource, sampleSource, revision } = input;
|
|
327
|
+
checkText$1(panelSource, sampleSource);
|
|
328
|
+
const current = await this.repository.read(id);
|
|
329
|
+
checkRevision$1(current, expectedRevision);
|
|
330
|
+
const snapshot = await this.sources.open(current.baseSnapshot);
|
|
331
|
+
return prepareStudioPanelPreview(snapshot.fs, snapshot.root, {
|
|
332
|
+
kit: current.kit,
|
|
333
|
+
bundle: current.bundle,
|
|
334
|
+
revision,
|
|
335
|
+
panelSource,
|
|
336
|
+
sampleSource
|
|
337
|
+
}, snapshot.loadOptions);
|
|
338
|
+
}
|
|
339
|
+
async confirm(input) {
|
|
340
|
+
const { id, expectedRevision } = input;
|
|
341
|
+
const current = await this.repository.read(id);
|
|
342
|
+
checkRevision$1(current, expectedRevision);
|
|
343
|
+
const result = await this.preview({
|
|
344
|
+
...current,
|
|
345
|
+
expectedRevision
|
|
346
|
+
});
|
|
347
|
+
if (!result.preview || result.diagnostics.length) return result;
|
|
348
|
+
const draft = current.confirmed ? current : await this.repository.replace(id, expectedRevision, {
|
|
349
|
+
...content$1(current),
|
|
350
|
+
confirmed: true
|
|
351
|
+
});
|
|
352
|
+
return {
|
|
353
|
+
...result,
|
|
354
|
+
draft
|
|
355
|
+
};
|
|
356
|
+
}
|
|
357
|
+
/** Start another editable revision, sharing the immutable base bytes. */
|
|
358
|
+
async fork(id) {
|
|
359
|
+
const current = await this.repository.read(id);
|
|
360
|
+
return this.repository.create({
|
|
361
|
+
...content$1(current),
|
|
362
|
+
confirmed: false
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
};
|
|
366
|
+
//#endregion
|
|
367
|
+
//#region src/kit-drafts.ts
|
|
368
|
+
/** Kit-owned working-copy edits over retained source; never catalog publication. */
|
|
369
|
+
function checkText(mindset, relations) {
|
|
370
|
+
if (typeof mindset !== "string" || relations !== null && typeof relations !== "string" || new TextEncoder().encode(mindset).length > 1048576 || relations !== null && new TextEncoder().encode(relations).length > 1048576) throw new Error("Kit-owned sources must be UTF-8 text of at most 1 MiB each.");
|
|
371
|
+
}
|
|
372
|
+
function checkRevision(current, expected) {
|
|
373
|
+
if (!Number.isSafeInteger(expected) || current.revision !== expected) throw new Error("Draft changed elsewhere. Reopen the latest revision.");
|
|
374
|
+
}
|
|
375
|
+
function content(record) {
|
|
376
|
+
const { id: _id, revision: _revision, ...value } = record;
|
|
377
|
+
return structuredClone(value);
|
|
378
|
+
}
|
|
379
|
+
function samePins(a, b) {
|
|
380
|
+
const keys = (pins) => pins.map((pin) => `${pin.name}@${pin.version}`).sort();
|
|
381
|
+
return JSON.stringify(keys(a)) === JSON.stringify(keys(b));
|
|
382
|
+
}
|
|
383
|
+
/** Only two Kit-owned files are overlaid; no user-supplied paths or live FS fallback. */
|
|
384
|
+
function overlay(snapshot, record) {
|
|
385
|
+
const root = resolve(snapshot.root);
|
|
386
|
+
const replacements = /* @__PURE__ */ new Map([[resolve(root, "mindset.md"), record.mindsetSource], [resolve(root, "relations.yaml"), record.relationsSource]]);
|
|
387
|
+
return {
|
|
388
|
+
readText: (path) => replacements.has(resolve(path)) ? Promise.resolve(replacements.get(resolve(path)) ?? void 0) : snapshot.fs.readText(path),
|
|
389
|
+
...snapshot.fs.readResource ? { readResource: async (path) => {
|
|
390
|
+
if (!replacements.has(resolve(path))) return snapshot.fs.readResource(path);
|
|
391
|
+
const content = replacements.get(resolve(path));
|
|
392
|
+
return content == null ? void 0 : {
|
|
393
|
+
kind: "text",
|
|
394
|
+
content
|
|
395
|
+
};
|
|
396
|
+
} } : {},
|
|
397
|
+
async listDir(path) {
|
|
398
|
+
if (resolve(path) !== root) return snapshot.fs.listDir(path);
|
|
399
|
+
const entries = new Set(await snapshot.fs.listDir(path));
|
|
400
|
+
entries.add("mindset.md");
|
|
401
|
+
if (record.relationsSource === null) entries.delete("relations.yaml");
|
|
402
|
+
else entries.add("relations.yaml");
|
|
403
|
+
return [...entries].sort();
|
|
404
|
+
},
|
|
405
|
+
...snapshot.fs.isDirectory ? { isDirectory: (path) => replacements.has(resolve(path)) ? Promise.resolve(false) : snapshot.fs.isDirectory(path) } : {},
|
|
406
|
+
...snapshot.fs.realPath ? { realPath: (path) => replacements.has(resolve(path)) ? Promise.resolve(resolve(path)) : snapshot.fs.realPath(path) } : {}
|
|
407
|
+
};
|
|
408
|
+
}
|
|
409
|
+
var StudioKitDraftService = class {
|
|
410
|
+
repository;
|
|
411
|
+
sources;
|
|
412
|
+
constructor(repository, sources) {
|
|
413
|
+
this.repository = repository;
|
|
414
|
+
this.sources = sources;
|
|
415
|
+
}
|
|
416
|
+
async list() {
|
|
417
|
+
return (await this.repository.list()).map(({ id, revision, baseKit, confirmed }) => ({
|
|
418
|
+
id,
|
|
419
|
+
revision,
|
|
420
|
+
baseKit: { ...baseKit },
|
|
421
|
+
confirmed
|
|
422
|
+
}));
|
|
423
|
+
}
|
|
424
|
+
async read(id) {
|
|
425
|
+
return structuredClone(await this.repository.read(id));
|
|
426
|
+
}
|
|
427
|
+
async create(input) {
|
|
428
|
+
const pin = { ...input.kit };
|
|
429
|
+
const snapshot = await this.sources.capture(pin);
|
|
430
|
+
return this.createRetained(snapshot, pin);
|
|
431
|
+
}
|
|
432
|
+
/** Adopt retained, independently validated content; never resolve an installed pin. */
|
|
433
|
+
async createFromSnapshot(input) {
|
|
434
|
+
const pin = { ...input.kit }, snapshotId = input.snapshotId;
|
|
435
|
+
return this.createRetained(await this.sources.open(snapshotId), pin);
|
|
436
|
+
}
|
|
437
|
+
async createRetained(snapshot, pin) {
|
|
438
|
+
const kit = await loadStudioKit(snapshot.fs, snapshot.root, snapshot.loadOptions);
|
|
439
|
+
if (kit.name !== pin.name || String(kit.version) !== pin.version) throw new Error("Source does not match the selected Kit pin.");
|
|
440
|
+
const mindsetSource = await snapshot.fs.readText(resolve(snapshot.root, "mindset.md"));
|
|
441
|
+
if (mindsetSource === void 0) throw new Error("Kit Mindset source is missing.");
|
|
442
|
+
const relationsSource = await snapshot.fs.readText(resolve(snapshot.root, "relations.yaml")) ?? null;
|
|
443
|
+
checkText(mindsetSource, relationsSource);
|
|
444
|
+
return this.repository.create({
|
|
445
|
+
baseKit: pin,
|
|
446
|
+
baseSnapshot: snapshot.id,
|
|
447
|
+
mounts: kit.skills.map((bundle) => ({
|
|
448
|
+
name: bundle.name,
|
|
449
|
+
version: String(bundle.version)
|
|
450
|
+
})),
|
|
451
|
+
mindsetSource,
|
|
452
|
+
relationsSource,
|
|
453
|
+
confirmed: false
|
|
454
|
+
});
|
|
455
|
+
}
|
|
456
|
+
/** Invalid intermediate edits can be retained, but cannot be confirmed. */
|
|
457
|
+
async save(input) {
|
|
458
|
+
const { id, expectedRevision, mindsetSource, relationsSource } = input;
|
|
459
|
+
checkText(mindsetSource, relationsSource);
|
|
460
|
+
const current = await this.read(id);
|
|
461
|
+
checkRevision(current, expectedRevision);
|
|
462
|
+
if (current.confirmed) throw new Error("Confirmed Kit draft is locked. Fork it to continue editing.");
|
|
463
|
+
return this.repository.replace(id, expectedRevision, {
|
|
464
|
+
...content(current),
|
|
465
|
+
mindsetSource,
|
|
466
|
+
relationsSource
|
|
467
|
+
});
|
|
468
|
+
}
|
|
469
|
+
async compile(current) {
|
|
470
|
+
checkText(current.mindsetSource, current.relationsSource);
|
|
471
|
+
const snapshot = await this.sources.open(current.baseSnapshot);
|
|
472
|
+
const base = await loadStudioKit(snapshot.fs, snapshot.root, snapshot.loadOptions);
|
|
473
|
+
if (base.name !== current.baseKit.name || String(base.version) !== current.baseKit.version || !samePins(current.mounts, base.skills.map((bundle) => ({
|
|
474
|
+
name: bundle.name,
|
|
475
|
+
version: String(bundle.version)
|
|
476
|
+
})))) throw new Error("Retained source does not match the working-copy base and mount pins.");
|
|
477
|
+
const fs = overlay(snapshot, current);
|
|
478
|
+
const candidate = await loadStudioKit(fs, snapshot.root, snapshot.loadOptions);
|
|
479
|
+
if (!samePins(current.mounts, candidate.skills.map((bundle) => ({
|
|
480
|
+
name: bundle.name,
|
|
481
|
+
version: String(bundle.version)
|
|
482
|
+
})))) throw new Error("Bundle mounts are read-only in this editor. Use an explicit catalog mount operation.");
|
|
483
|
+
return {
|
|
484
|
+
snapshot,
|
|
485
|
+
fs,
|
|
486
|
+
candidate
|
|
487
|
+
};
|
|
488
|
+
}
|
|
489
|
+
async validate(input) {
|
|
490
|
+
const { id, expectedRevision } = input;
|
|
491
|
+
const current = await this.read(id);
|
|
492
|
+
checkRevision(current, expectedRevision);
|
|
493
|
+
try {
|
|
494
|
+
const { candidate } = await this.compile(current);
|
|
495
|
+
return {
|
|
496
|
+
valid: true,
|
|
497
|
+
diagnostics: [],
|
|
498
|
+
revision: current.revision,
|
|
499
|
+
candidateKit: {
|
|
500
|
+
name: candidate.name,
|
|
501
|
+
version: String(candidate.version)
|
|
502
|
+
}
|
|
503
|
+
};
|
|
504
|
+
} catch (error) {
|
|
505
|
+
return {
|
|
506
|
+
valid: false,
|
|
507
|
+
revision: current.revision,
|
|
508
|
+
diagnostics: [{
|
|
509
|
+
code: "kit_draft.definition",
|
|
510
|
+
message: error instanceof Error ? error.message : String(error)
|
|
511
|
+
}]
|
|
512
|
+
};
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
async preview(input) {
|
|
516
|
+
const { id, expectedRevision, sampleSource } = input;
|
|
517
|
+
const bundle = { ...input.bundle };
|
|
518
|
+
const current = await this.read(id);
|
|
519
|
+
checkRevision(current, expectedRevision);
|
|
520
|
+
try {
|
|
521
|
+
const { fs, snapshot, candidate } = await this.compile(current);
|
|
522
|
+
if (!current.mounts.some((pin) => pin.name === bundle.name && pin.version === bundle.version)) throw new Error("Select an exact mounted Bundle.");
|
|
523
|
+
const mounted = candidate.skills.find((skill) => skill.name === bundle.name && String(skill.version) === bundle.version);
|
|
524
|
+
const panelSource = await fs.readText(resolve(mounted?.resourceRoot ?? resolve(snapshot.root, "skills", bundle.name), "panel.yaml"));
|
|
525
|
+
if (panelSource === void 0) throw new Error("This Bundle has no declarative Panel preview.");
|
|
526
|
+
return prepareStudioPanelPreview(fs, snapshot.root, {
|
|
527
|
+
kit: {
|
|
528
|
+
name: candidate.name,
|
|
529
|
+
version: String(candidate.version)
|
|
530
|
+
},
|
|
531
|
+
bundle,
|
|
532
|
+
revision: current.revision,
|
|
533
|
+
panelSource,
|
|
534
|
+
sampleSource
|
|
535
|
+
}, snapshot.loadOptions);
|
|
536
|
+
} catch (error) {
|
|
537
|
+
return { diagnostics: [{
|
|
538
|
+
code: "kit_draft.preview",
|
|
539
|
+
message: error instanceof Error ? error.message : String(error)
|
|
540
|
+
}] };
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
async confirm(input) {
|
|
544
|
+
const { id, expectedRevision } = input;
|
|
545
|
+
const current = await this.read(id);
|
|
546
|
+
checkRevision(current, expectedRevision);
|
|
547
|
+
const result = await this.validate({
|
|
548
|
+
id,
|
|
549
|
+
expectedRevision
|
|
550
|
+
});
|
|
551
|
+
if (!result.valid) return result;
|
|
552
|
+
const draft = current.confirmed ? current : await this.repository.replace(id, expectedRevision, {
|
|
553
|
+
...content(current),
|
|
554
|
+
confirmed: true
|
|
555
|
+
});
|
|
556
|
+
return {
|
|
557
|
+
...result,
|
|
558
|
+
draft
|
|
559
|
+
};
|
|
560
|
+
}
|
|
561
|
+
async fork(id) {
|
|
562
|
+
return this.repository.create({
|
|
563
|
+
...content(await this.read(id)),
|
|
564
|
+
confirmed: false
|
|
565
|
+
});
|
|
566
|
+
}
|
|
567
|
+
};
|
|
568
|
+
//#endregion
|
|
569
|
+
//#region src/content-archive.ts
|
|
570
|
+
/** Portable regular-file content, not a plugin installer or publication format. */
|
|
571
|
+
const MAX_BYTES = 16777216;
|
|
572
|
+
const MAX_JSON = 33554432;
|
|
573
|
+
const MAX_FILES = 4096;
|
|
574
|
+
const HASH = /^[0-9a-f]{64}$/;
|
|
575
|
+
const sha = (value) => createHash("sha256").update(value).digest("hex");
|
|
576
|
+
const OPAQUE_RESOURCE_EXTENSIONS = /* @__PURE__ */ new Set([
|
|
577
|
+
".bin",
|
|
578
|
+
".png",
|
|
579
|
+
".jpg",
|
|
580
|
+
".jpeg",
|
|
581
|
+
".gif",
|
|
582
|
+
".webp",
|
|
583
|
+
".avif",
|
|
584
|
+
".ico",
|
|
585
|
+
".bmp",
|
|
586
|
+
".pdf",
|
|
587
|
+
".woff",
|
|
588
|
+
".woff2",
|
|
589
|
+
".ttf",
|
|
590
|
+
".otf",
|
|
591
|
+
".eot",
|
|
592
|
+
".wasm"
|
|
593
|
+
]);
|
|
594
|
+
/** Shared by archive capture and virtual import; full bytes and directory entries remain intact. */
|
|
595
|
+
function readStudioArchiveResource(path, bytes) {
|
|
596
|
+
return OPAQUE_RESOURCE_EXTENSIONS.has(extname(path).toLowerCase()) ? {
|
|
597
|
+
kind: "opaque",
|
|
598
|
+
byteLength: bytes.byteLength
|
|
599
|
+
} : {
|
|
600
|
+
kind: "text",
|
|
601
|
+
content: new TextDecoder("utf-8", { fatal: true }).decode(bytes)
|
|
602
|
+
};
|
|
603
|
+
}
|
|
604
|
+
function object(value) {
|
|
605
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) throw Error("Invalid archive object.");
|
|
606
|
+
return value;
|
|
607
|
+
}
|
|
608
|
+
function keys(value, allowed) {
|
|
609
|
+
if (Object.keys(value).length !== allowed.length || allowed.some((key) => !Object.hasOwn(value, key))) throw Error("Unknown or missing archive fields.");
|
|
610
|
+
}
|
|
611
|
+
function text(value, limit) {
|
|
612
|
+
if (typeof value !== "string" || !value.trim() || Buffer.byteLength(value) > limit || /[\u0000-\u001f\u007f]/u.test(value)) throw Error("Invalid archive metadata text.");
|
|
613
|
+
return value;
|
|
614
|
+
}
|
|
615
|
+
function pin(value, kind) {
|
|
616
|
+
const data = object(value);
|
|
617
|
+
keys(data, ["name", "version"]);
|
|
618
|
+
const name = text(data.name, 128), version = text(data.version, 32);
|
|
619
|
+
if (!/^[a-z][a-z0-9_]*$/.test(name)) throw Error("Unsupported archive content identity.");
|
|
620
|
+
safePath(name);
|
|
621
|
+
safePath(version);
|
|
622
|
+
if (kind === "kit") DesignKitVersion(version);
|
|
623
|
+
else DesignBundleVersion(version);
|
|
624
|
+
return Object.freeze({
|
|
625
|
+
name,
|
|
626
|
+
version
|
|
627
|
+
});
|
|
628
|
+
}
|
|
629
|
+
function safePath(value) {
|
|
630
|
+
const path = text(value, 1024);
|
|
631
|
+
if (path !== path.normalize("NFC") || /[\\:*?"<>|]/u.test(path)) throw Error("Unsafe archive path.");
|
|
632
|
+
if (/\.(exe|dll|node|so|dylib|com|msi|sys)$/iu.test(path)) throw Error("Native executable payload propagation is unsupported.");
|
|
633
|
+
for (const part of path.split("/")) if (!part || part === "." || part === ".." || [".git", "node_modules"].includes(part.toLowerCase()) || /[. ]$/u.test(part) || /^(con|prn|aux|nul|com[1-9¹²³]|lpt[1-9¹²³])(?:\.|$)/iu.test(part)) throw Error("Unsafe archive path.");
|
|
634
|
+
return path;
|
|
635
|
+
}
|
|
636
|
+
function canonical(data) {
|
|
637
|
+
return JSON.stringify(data);
|
|
638
|
+
}
|
|
639
|
+
/** Parse a bounded JSON envelope; hashes include metadata and every original byte. */
|
|
640
|
+
function readStudioContentArchive(source) {
|
|
641
|
+
if (typeof source !== "string" || Buffer.byteLength(source) > MAX_JSON) throw Error("Archive JSON exceeds 32 MiB.");
|
|
642
|
+
const raw = object(JSON.parse(source));
|
|
643
|
+
keys(raw, [
|
|
644
|
+
"format",
|
|
645
|
+
"kind",
|
|
646
|
+
"pin",
|
|
647
|
+
"provenance",
|
|
648
|
+
"dependencies",
|
|
649
|
+
"files",
|
|
650
|
+
"sha256"
|
|
651
|
+
]);
|
|
652
|
+
if (raw.format !== "studio-content-archive/v1" || raw.kind !== "kit" && raw.kind !== "bundle") throw Error("Unsupported archive format.");
|
|
653
|
+
const identity = pin(raw.pin, raw.kind), provenance = object(raw.provenance);
|
|
654
|
+
keys(provenance, ["source", "license"]);
|
|
655
|
+
const origin = Object.freeze({
|
|
656
|
+
source: text(provenance.source, 2048),
|
|
657
|
+
license: text(provenance.license, 2048)
|
|
658
|
+
});
|
|
659
|
+
if (!Array.isArray(raw.dependencies) || raw.dependencies.length > MAX_FILES) throw Error("Invalid archive dependencies.");
|
|
660
|
+
const dependencies = raw.dependencies.map((value) => pin(value, "bundle"));
|
|
661
|
+
const names = /* @__PURE__ */ new Set();
|
|
662
|
+
for (const dependency of dependencies) {
|
|
663
|
+
if (names.has(dependency.name) || raw.kind === "bundle" && dependency.name === identity.name) throw Error("Duplicate or self archive dependency.");
|
|
664
|
+
names.add(dependency.name);
|
|
665
|
+
}
|
|
666
|
+
if (!Array.isArray(raw.files) || !raw.files.length || raw.files.length > MAX_FILES) throw Error("Invalid archive file count.");
|
|
667
|
+
let total = 0;
|
|
668
|
+
const paths = /* @__PURE__ */ new Map();
|
|
669
|
+
const prefixes = /* @__PURE__ */ new Map();
|
|
670
|
+
const directories = /* @__PURE__ */ new Set();
|
|
671
|
+
const files = raw.files.map((value) => {
|
|
672
|
+
const entry = object(value);
|
|
673
|
+
keys(entry, [
|
|
674
|
+
"path",
|
|
675
|
+
"base64",
|
|
676
|
+
"bytes",
|
|
677
|
+
"sha256"
|
|
678
|
+
]);
|
|
679
|
+
const path = safePath(entry.path), folded = path.toLowerCase();
|
|
680
|
+
const segments = path.split("/");
|
|
681
|
+
for (let index = 1; index <= segments.length; index++) {
|
|
682
|
+
const prefix = segments.slice(0, index).join("/"), foldedPrefix = prefix.toLowerCase();
|
|
683
|
+
if (prefixes.has(foldedPrefix) && prefixes.get(foldedPrefix) !== prefix) throw Error("Case-conflicting archive path segments.");
|
|
684
|
+
prefixes.set(foldedPrefix, prefix);
|
|
685
|
+
}
|
|
686
|
+
if (paths.has(folded)) throw Error("Duplicate or case-conflicting archive paths.");
|
|
687
|
+
paths.set(folded, path);
|
|
688
|
+
const parts = folded.split("/");
|
|
689
|
+
parts.pop();
|
|
690
|
+
while (parts.length) {
|
|
691
|
+
directories.add(parts.join("/"));
|
|
692
|
+
parts.pop();
|
|
693
|
+
}
|
|
694
|
+
if (!Number.isSafeInteger(entry.bytes) || entry.bytes < 0 || entry.bytes > MAX_BYTES - total) throw Error("Archive content exceeds 16 MiB or has an invalid byte count.");
|
|
695
|
+
if (typeof entry.base64 !== "string" || entry.base64.length > Math.ceil(MAX_BYTES / 3) * 4 || entry.base64.length % 4 || !/^[A-Za-z0-9+/]*={0,2}$/u.test(entry.base64)) throw Error("Invalid archive base64.");
|
|
696
|
+
const bytes = Buffer.from(entry.base64, "base64");
|
|
697
|
+
if (bytes.toString("base64") !== entry.base64 || entry.bytes !== bytes.length || typeof entry.sha256 !== "string" || !HASH.test(entry.sha256) || sha(bytes) !== entry.sha256) throw Error("Archive file integrity mismatch.");
|
|
698
|
+
total += bytes.length;
|
|
699
|
+
if (total > MAX_BYTES) throw Error("Archive content exceeds 16 MiB.");
|
|
700
|
+
return Object.freeze({
|
|
701
|
+
path,
|
|
702
|
+
base64: entry.base64,
|
|
703
|
+
bytes: bytes.length,
|
|
704
|
+
sha256: entry.sha256
|
|
705
|
+
});
|
|
706
|
+
}).sort((a, b) => a.path < b.path ? -1 : a.path > b.path ? 1 : 0);
|
|
707
|
+
if ([...directories].some((path) => paths.has(path))) throw Error("Archive file/directory collision.");
|
|
708
|
+
const data = {
|
|
709
|
+
format: raw.format,
|
|
710
|
+
kind: raw.kind,
|
|
711
|
+
pin: identity,
|
|
712
|
+
provenance: origin,
|
|
713
|
+
dependencies: Object.freeze(dependencies),
|
|
714
|
+
files: Object.freeze(files)
|
|
715
|
+
};
|
|
716
|
+
if (typeof raw.sha256 !== "string" || !HASH.test(raw.sha256) || sha(canonical(data)) !== raw.sha256) throw Error("Archive envelope integrity mismatch.");
|
|
717
|
+
return Object.freeze({
|
|
718
|
+
...data,
|
|
719
|
+
sha256: raw.sha256
|
|
720
|
+
});
|
|
721
|
+
}
|
|
722
|
+
/** Pure virtual source: explicit text reads stay strict; attached byte assets remain opaque. */
|
|
723
|
+
function virtualSource(files, root) {
|
|
724
|
+
const map = new Map(files.map((file) => [file.path, file.base64]));
|
|
725
|
+
const key = (path) => {
|
|
726
|
+
const result = relative(root, resolve(path));
|
|
727
|
+
if (isAbsolute(result) || result === ".." || result.startsWith(`..${sep}`)) throw Error("Archive validation path escapes source.");
|
|
728
|
+
return result.split(sep).join("/");
|
|
729
|
+
};
|
|
730
|
+
const children = (path) => {
|
|
731
|
+
const prefix = key(path);
|
|
732
|
+
const head = prefix ? `${prefix}/` : "";
|
|
733
|
+
return [...new Set([...map.keys()].filter((file) => file.startsWith(head)).map((file) => file.slice(head.length).split("/")[0]))].sort();
|
|
734
|
+
};
|
|
735
|
+
return {
|
|
736
|
+
async readText(path) {
|
|
737
|
+
const content = map.get(key(path));
|
|
738
|
+
return content === void 0 ? void 0 : new TextDecoder("utf-8", { fatal: true }).decode(Buffer.from(content, "base64"));
|
|
739
|
+
},
|
|
740
|
+
async readResource(path) {
|
|
741
|
+
const content = map.get(key(path));
|
|
742
|
+
return content === void 0 ? void 0 : readStudioArchiveResource(path, Buffer.from(content, "base64"));
|
|
743
|
+
},
|
|
744
|
+
async listDir(path) {
|
|
745
|
+
return children(path);
|
|
746
|
+
},
|
|
747
|
+
async isDirectory(path) {
|
|
748
|
+
return children(path).length > 0;
|
|
749
|
+
},
|
|
750
|
+
async realPath(path) {
|
|
751
|
+
key(path);
|
|
752
|
+
return resolve(path);
|
|
753
|
+
}
|
|
754
|
+
};
|
|
755
|
+
}
|
|
756
|
+
const bundlePin = (skill) => ({
|
|
757
|
+
name: skill.catalogName ?? skill.name,
|
|
758
|
+
version: String(skill.version)
|
|
759
|
+
});
|
|
760
|
+
const samePin = (left, right) => left.name === right.name && left.version === right.version;
|
|
761
|
+
const bundlePrefix = (identity) => `bundles/${identity.name}/${identity.version}/`;
|
|
762
|
+
const within = (root, path) => {
|
|
763
|
+
const rel = relative(root, resolve(path));
|
|
764
|
+
return !isAbsolute(rel) && rel !== ".." && !rel.startsWith(`..${sep}`);
|
|
765
|
+
};
|
|
766
|
+
/** All reads dispatch to captured bytes; there is no live filesystem fallback. */
|
|
767
|
+
function sourceView(snapshot) {
|
|
768
|
+
const bundles = snapshot.bundles ?? [];
|
|
769
|
+
const sources = [...bundles.map((bundle) => bundle.snapshot), snapshot].sort((a, b) => b.root.length - a.root.length);
|
|
770
|
+
const owner = (path) => {
|
|
771
|
+
const source = sources.find((source) => within(source.root, path));
|
|
772
|
+
if (!source) throw Error("Archive validation path escapes captured sources.");
|
|
773
|
+
return source;
|
|
774
|
+
};
|
|
775
|
+
return {
|
|
776
|
+
fs: {
|
|
777
|
+
readText: (path) => owner(path).fs.readText(path),
|
|
778
|
+
async readResource(path) {
|
|
779
|
+
const source = owner(path);
|
|
780
|
+
if (typeof source.readBytes === "function") return readStudioArchiveResource(path, await source.readBytes(path));
|
|
781
|
+
if (source.fs.readResource) return source.fs.readResource(path);
|
|
782
|
+
const content = await source.fs.readText(path);
|
|
783
|
+
return content === void 0 ? void 0 : {
|
|
784
|
+
kind: "text",
|
|
785
|
+
content
|
|
786
|
+
};
|
|
787
|
+
},
|
|
788
|
+
listDir: (path) => owner(path).fs.listDir(path),
|
|
789
|
+
async isDirectory(path) {
|
|
790
|
+
const source = owner(path);
|
|
791
|
+
return source.fs.isDirectory ? source.fs.isDirectory(path) : (await source.fs.listDir(path)).length > 0;
|
|
792
|
+
},
|
|
793
|
+
async realPath(path) {
|
|
794
|
+
const source = owner(path);
|
|
795
|
+
return source.fs.realPath ? source.fs.realPath(path) : resolve(path);
|
|
796
|
+
}
|
|
797
|
+
},
|
|
798
|
+
options: {
|
|
799
|
+
sourceId: snapshot.sourceId,
|
|
800
|
+
bundles: bundles.map((bundle) => ({
|
|
801
|
+
name: bundle.name,
|
|
802
|
+
version: bundle.version,
|
|
803
|
+
rootDir: bundle.snapshot.root,
|
|
804
|
+
sourceId: bundle.sourceId
|
|
805
|
+
}))
|
|
806
|
+
}
|
|
807
|
+
};
|
|
808
|
+
}
|
|
809
|
+
function archiveSnapshot(archive) {
|
|
810
|
+
const root = resolve("/studio-content-archive-virtual");
|
|
811
|
+
const sourceId = `archive:untrusted:${archive.sha256}`;
|
|
812
|
+
const independent = archive.kind === "kit" && archive.files.some((file) => file.path === "kit.yaml");
|
|
813
|
+
const filesAt = (files, root) => {
|
|
814
|
+
const fs = virtualSource(files, root);
|
|
815
|
+
return {
|
|
816
|
+
root,
|
|
817
|
+
sourceId,
|
|
818
|
+
fs,
|
|
819
|
+
async listFiles() {
|
|
820
|
+
return files.map((file) => file.path);
|
|
821
|
+
},
|
|
822
|
+
async readBytes(path) {
|
|
823
|
+
const file = files.find((file) => resolve(root, file.path) === resolve(path));
|
|
824
|
+
if (!file) throw Error("Missing archive source file.");
|
|
825
|
+
return Buffer.from(file.base64, "base64");
|
|
826
|
+
}
|
|
827
|
+
};
|
|
828
|
+
};
|
|
829
|
+
if (!independent) return filesAt(archive.files, root);
|
|
830
|
+
const bundleFiles = /* @__PURE__ */ new Set();
|
|
831
|
+
const bundles = archive.dependencies.map((identity) => {
|
|
832
|
+
const prefix = bundlePrefix(identity);
|
|
833
|
+
const files = archive.files.filter((file) => file.path.startsWith(prefix)).map((file) => {
|
|
834
|
+
bundleFiles.add(file.path);
|
|
835
|
+
return Object.freeze({
|
|
836
|
+
...file,
|
|
837
|
+
path: file.path.slice(prefix.length)
|
|
838
|
+
});
|
|
839
|
+
});
|
|
840
|
+
if (!files.length) throw Error(`Archive is missing exact Bundle content: ${identity.name}@${identity.version}`);
|
|
841
|
+
return {
|
|
842
|
+
...identity,
|
|
843
|
+
sourceId,
|
|
844
|
+
snapshot: filesAt(files, resolve(root, prefix))
|
|
845
|
+
};
|
|
846
|
+
});
|
|
847
|
+
if (archive.files.some((file) => file.path.startsWith("bundles/") && !bundleFiles.has(file.path))) throw Error("Archive contains unreferenced Bundle content.");
|
|
848
|
+
return {
|
|
849
|
+
...filesAt(archive.files.filter((file) => !bundleFiles.has(file.path)), root),
|
|
850
|
+
bundles
|
|
851
|
+
};
|
|
852
|
+
}
|
|
853
|
+
/** No writes, extraction, permission restoration, script execution, installation or publication. */
|
|
854
|
+
async function prepareStudioContentImport(source, options = {}) {
|
|
855
|
+
const archive = readStudioContentArchive(source);
|
|
856
|
+
let validation;
|
|
857
|
+
let contents = Object.freeze([]);
|
|
858
|
+
let requiresTrustReview = archive.files.some((file) => /\.(m?js|cjs|[cm]?tsx?|sh|ps1|cmd|bat|py|wasm)$/iu.test(file.path));
|
|
859
|
+
try {
|
|
860
|
+
const snapshot = archiveSnapshot(archive), view = sourceView(snapshot);
|
|
861
|
+
const selected = [];
|
|
862
|
+
if (archive.kind === "kit") {
|
|
863
|
+
const kit = await loadStudioKit(view.fs, snapshot.root, view.options);
|
|
864
|
+
selected.push(...kit.skills);
|
|
865
|
+
if (kit.name !== archive.pin.name || String(kit.version) !== archive.pin.version) throw Error("Archive Kit identity disagrees with source.");
|
|
866
|
+
if (JSON.stringify(kit.skills.map(bundlePin)) !== JSON.stringify(archive.dependencies)) throw Error("Archive Kit member pins disagree with source.");
|
|
867
|
+
if (snapshot.bundles) {
|
|
868
|
+
const kitFiles = archive.files.filter((file) => !file.path.startsWith("bundles/"));
|
|
869
|
+
contents = Object.freeze([Object.freeze({
|
|
870
|
+
kind: "kit",
|
|
871
|
+
pin: archive.pin,
|
|
872
|
+
files: Object.freeze(kitFiles)
|
|
873
|
+
}), ...snapshot.bundles.map((bundle) => Object.freeze({
|
|
874
|
+
kind: "bundle",
|
|
875
|
+
pin: Object.freeze({
|
|
876
|
+
name: bundle.name,
|
|
877
|
+
version: bundle.version
|
|
878
|
+
}),
|
|
879
|
+
files: Object.freeze(archive.files.filter((file) => file.path.startsWith(bundlePrefix(bundle))).map((file) => Object.freeze({
|
|
880
|
+
...file,
|
|
881
|
+
path: file.path.slice(bundlePrefix(bundle).length)
|
|
882
|
+
})))
|
|
883
|
+
}))]);
|
|
884
|
+
} else contents = Object.freeze([Object.freeze({
|
|
885
|
+
kind: "kit",
|
|
886
|
+
pin: archive.pin,
|
|
887
|
+
files: archive.files
|
|
888
|
+
})]);
|
|
889
|
+
} else {
|
|
890
|
+
const bundle = await loadDesignBundle(snapshot.fs, {
|
|
891
|
+
rootDir: snapshot.root,
|
|
892
|
+
sourceId: snapshot.sourceId
|
|
893
|
+
}, archive.pin);
|
|
894
|
+
selected.push(bundle.skill);
|
|
895
|
+
const context = options.validationContext;
|
|
896
|
+
if (archive.dependencies.length && (!context || !isAbsolute(context.root))) throw Error("Independent Bundle dependency validation requires a host-resolved immutable Kit context.");
|
|
897
|
+
if (context) {
|
|
898
|
+
if (!isAbsolute(context.root)) throw Error("Archive context must have a host-resolved absolute root.");
|
|
899
|
+
const contextView = sourceView(context);
|
|
900
|
+
const kit = await loadStudioKit(contextView.fs, context.root, contextView.options);
|
|
901
|
+
if (archive.dependencies.some((dependency) => !kit.skills.some((skill) => samePin(bundlePin(skill), dependency)))) throw Error("Archive Bundle dependency is unresolved in context.");
|
|
902
|
+
const existing = kit.skills.find((skill) => samePin(bundlePin(skill), archive.pin));
|
|
903
|
+
if (existing?.resourceRoot) {
|
|
904
|
+
const local = virtualSource(archive.files, existing.resourceRoot);
|
|
905
|
+
const owns = (path) => within(existing.resourceRoot, path);
|
|
906
|
+
await loadStudioKit({
|
|
907
|
+
readText: (path) => owns(path) ? local.readText(path) : contextView.fs.readText(path),
|
|
908
|
+
readResource: (path) => owns(path) ? local.readResource(path) : contextView.fs.readResource(path),
|
|
909
|
+
listDir: (path) => owns(path) ? local.listDir(path) : contextView.fs.listDir(path),
|
|
910
|
+
async isDirectory(path) {
|
|
911
|
+
return owns(path) ? local.isDirectory(path) : contextView.fs.isDirectory(path);
|
|
912
|
+
},
|
|
913
|
+
async realPath(path) {
|
|
914
|
+
return owns(path) ? resolve(path) : contextView.fs.realPath(path);
|
|
915
|
+
}
|
|
916
|
+
}, context.root, contextView.options);
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
contents = Object.freeze([Object.freeze({
|
|
920
|
+
kind: "bundle",
|
|
921
|
+
pin: archive.pin,
|
|
922
|
+
files: archive.files
|
|
923
|
+
})]);
|
|
924
|
+
}
|
|
925
|
+
requiresTrustReview ||= selected.some((skill) => {
|
|
926
|
+
const manifest = skill.metadata?.skillManifest;
|
|
927
|
+
const rules = skill.metadata?.validationDocument;
|
|
928
|
+
return manifest?.presentation?.mode === "native" || !!rules?.rules?.some((rule) => typeof rule.check?.driver === "string");
|
|
929
|
+
});
|
|
930
|
+
validation = Object.freeze({
|
|
931
|
+
valid: true,
|
|
932
|
+
diagnostics: Object.freeze([])
|
|
933
|
+
});
|
|
934
|
+
} catch (error) {
|
|
935
|
+
contents = Object.freeze([]);
|
|
936
|
+
validation = Object.freeze({
|
|
937
|
+
valid: false,
|
|
938
|
+
diagnostics: Object.freeze([Object.freeze({
|
|
939
|
+
code: "archive.definition",
|
|
940
|
+
message: error instanceof Error ? error.message : String(error),
|
|
941
|
+
subject: archive.pin.name
|
|
942
|
+
})])
|
|
943
|
+
});
|
|
944
|
+
}
|
|
945
|
+
return Object.freeze({
|
|
946
|
+
archive,
|
|
947
|
+
files: archive.files,
|
|
948
|
+
contents,
|
|
949
|
+
executionGranted: false,
|
|
950
|
+
requiresTrustReview,
|
|
951
|
+
validation
|
|
952
|
+
});
|
|
953
|
+
}
|
|
954
|
+
/** Capture only immutable host snapshots, preserving full exact referenced trees. */
|
|
955
|
+
async function exportStudioContentArchive(snapshot, input) {
|
|
956
|
+
const request = JSON.parse(JSON.stringify(input));
|
|
957
|
+
if (!isAbsolute(snapshot.root)) throw Error("Archive source root must be host-resolved absolute.");
|
|
958
|
+
const view = sourceView(snapshot), kit = await loadStudioKit(view.fs, snapshot.root, view.options);
|
|
959
|
+
const selected = request.kind === "kit" ? kit : kit.skills.find((skill) => samePin(bundlePin(skill), request.pin));
|
|
960
|
+
if (!selected || !samePin(request.kind === "kit" ? {
|
|
961
|
+
name: selected.name,
|
|
962
|
+
version: String(selected.version)
|
|
963
|
+
} : bundlePin(selected), request.pin)) throw Error("Export pin disagrees with snapshot.");
|
|
964
|
+
const files = [];
|
|
965
|
+
let total = 0;
|
|
966
|
+
const append = async (source, prefix, selectPrefix = "") => {
|
|
967
|
+
const all = await source.listFiles();
|
|
968
|
+
if (all.length > MAX_FILES) throw Error("Snapshot file count exceeds 4096.");
|
|
969
|
+
for (const path of [...all].sort()) {
|
|
970
|
+
safePath(path);
|
|
971
|
+
if (selectPrefix && !path.startsWith(selectPrefix)) continue;
|
|
972
|
+
const target = prefix + path.slice(selectPrefix.length);
|
|
973
|
+
safePath(target);
|
|
974
|
+
const bytes = await source.readBytes(resolve(source.root, path));
|
|
975
|
+
total += bytes.byteLength;
|
|
976
|
+
if (total > MAX_BYTES || files.length >= MAX_FILES) throw Error("Archive content exceeds 16 MiB or 4096 files.");
|
|
977
|
+
files.push({
|
|
978
|
+
path: target,
|
|
979
|
+
base64: Buffer.from(bytes).toString("base64"),
|
|
980
|
+
bytes: bytes.byteLength,
|
|
981
|
+
sha256: sha(bytes)
|
|
982
|
+
});
|
|
983
|
+
}
|
|
984
|
+
};
|
|
985
|
+
if (snapshot.bundles) {
|
|
986
|
+
const referenced = kit.skills.map((skill) => {
|
|
987
|
+
const identity = bundlePin(skill), matches = snapshot.bundles.filter((bundle) => samePin(bundle, identity));
|
|
988
|
+
if (matches.length !== 1) throw Error("Missing or ambiguous exact Bundle snapshot.");
|
|
989
|
+
return matches[0];
|
|
990
|
+
});
|
|
991
|
+
if (request.kind === "kit") {
|
|
992
|
+
if ((await snapshot.listFiles()).some((path) => path === "bundles" || path.startsWith("bundles/"))) throw Error("Kit-owned files collide with archive Bundle transport paths.");
|
|
993
|
+
await append(snapshot, "");
|
|
994
|
+
for (const bundle of referenced) await append(bundle.snapshot, bundlePrefix(bundle));
|
|
995
|
+
} else await append(referenced.find((bundle) => samePin(bundle, request.pin)).snapshot, "");
|
|
996
|
+
} else await append(snapshot, "", request.kind === "bundle" ? `skills/${selected.name}/` : "");
|
|
997
|
+
files.sort((a, b) => a.path < b.path ? -1 : a.path > b.path ? 1 : 0);
|
|
998
|
+
const data = {
|
|
999
|
+
format: "studio-content-archive/v1",
|
|
1000
|
+
kind: request.kind,
|
|
1001
|
+
pin: request.pin,
|
|
1002
|
+
provenance: request.provenance,
|
|
1003
|
+
dependencies: request.kind === "kit" ? kit.skills.map(bundlePin) : request.dependencies ?? [],
|
|
1004
|
+
files
|
|
1005
|
+
};
|
|
1006
|
+
if (request.kind === "kit" && request.dependencies !== void 0 && JSON.stringify(request.dependencies) !== JSON.stringify(data.dependencies)) throw Error("Explicit Kit pins disagree with source membership.");
|
|
1007
|
+
const serialized = JSON.stringify({
|
|
1008
|
+
...data,
|
|
1009
|
+
sha256: sha(canonical(data))
|
|
1010
|
+
});
|
|
1011
|
+
const prepared = await prepareStudioContentImport(serialized, { validationContext: snapshot });
|
|
1012
|
+
if (!prepared.validation.valid) throw Error(prepared.validation.diagnostics[0].message);
|
|
1013
|
+
return serialized;
|
|
1014
|
+
}
|
|
1015
|
+
//#endregion
|
|
1016
|
+
//#region src/composition.ts
|
|
1017
|
+
function pinKey(name, version) {
|
|
1018
|
+
return `${name}\u0000${version}`;
|
|
1019
|
+
}
|
|
1020
|
+
function comparePins(left, right) {
|
|
1021
|
+
if (left.name !== right.name) return left.name < right.name ? -1 : 1;
|
|
1022
|
+
if (left.version === right.version) return 0;
|
|
1023
|
+
return left.version < right.version ? -1 : 1;
|
|
1024
|
+
}
|
|
1025
|
+
/**
|
|
1026
|
+
* Derive the flat searchable Bundle catalog and reverse dependencies.
|
|
1027
|
+
* @param snapshot - authoritative Core-compiled installed catalog snapshot.
|
|
1028
|
+
* @returns a frozen derived catalog; identical Bundle pins are coalesced.
|
|
1029
|
+
*/
|
|
1030
|
+
function buildStudioCompositionCatalog(snapshot) {
|
|
1031
|
+
const entries = /* @__PURE__ */ new Map();
|
|
1032
|
+
const kits = [];
|
|
1033
|
+
for (const bundle of snapshot.bundles ?? []) {
|
|
1034
|
+
const key = pinKey(bundle.name, bundle.version);
|
|
1035
|
+
if (entries.has(key)) throw new Error(`studio.duplicate_bundle: ${bundle.name}@${bundle.version}`);
|
|
1036
|
+
entries.set(key, {
|
|
1037
|
+
bundle,
|
|
1038
|
+
usedBy: []
|
|
1039
|
+
});
|
|
1040
|
+
}
|
|
1041
|
+
for (const kit of snapshot.kits) {
|
|
1042
|
+
const kitPin = Object.freeze({
|
|
1043
|
+
name: kit.name,
|
|
1044
|
+
version: kit.version
|
|
1045
|
+
});
|
|
1046
|
+
kits.push(kitPin);
|
|
1047
|
+
for (const bundle of kit.bundles) {
|
|
1048
|
+
const key = pinKey(bundle.name, bundle.version);
|
|
1049
|
+
const current = entries.get(key);
|
|
1050
|
+
if (current === void 0 && snapshot.bundles !== void 0) throw new Error(`studio.missing_bundle: ${kit.name}@${kit.version} references ${bundle.name}@${bundle.version}`);
|
|
1051
|
+
if (current === void 0) entries.set(key, {
|
|
1052
|
+
bundle,
|
|
1053
|
+
usedBy: [kitPin]
|
|
1054
|
+
});
|
|
1055
|
+
else current.usedBy.push(kitPin);
|
|
1056
|
+
}
|
|
1057
|
+
}
|
|
1058
|
+
const bundles = [...entries.values()].map(({ bundle, usedBy }) => Object.freeze({
|
|
1059
|
+
name: bundle.name,
|
|
1060
|
+
version: bundle.version,
|
|
1061
|
+
definition: bundle.definition,
|
|
1062
|
+
usedBy: Object.freeze([...usedBy].sort(comparePins))
|
|
1063
|
+
})).sort(comparePins);
|
|
1064
|
+
return Object.freeze({
|
|
1065
|
+
bundles: Object.freeze(bundles),
|
|
1066
|
+
kits: Object.freeze(kits.sort(comparePins))
|
|
1067
|
+
});
|
|
1068
|
+
}
|
|
1069
|
+
/**
|
|
1070
|
+
* Search Bundle name and description in deterministic catalog order.
|
|
1071
|
+
* @param catalog - derived flat catalog.
|
|
1072
|
+
* @param query - case-insensitive trimmed text; empty returns the full catalog.
|
|
1073
|
+
* @returns matching immutable catalog entries.
|
|
1074
|
+
*/
|
|
1075
|
+
function searchStudioBundles(catalog, query) {
|
|
1076
|
+
const normalized = query.trim().toLowerCase();
|
|
1077
|
+
if (normalized === "") return catalog.bundles;
|
|
1078
|
+
return Object.freeze(catalog.bundles.filter((entry) => entry.name.toLowerCase().includes(normalized) || entry.definition.description.toLowerCase().includes(normalized) || Object.values(entry.definition.title ?? {}).some((text) => text.toLowerCase().includes(normalized)) || Object.values(entry.definition.pickerDescription ?? {}).some((text) => text.toLowerCase().includes(normalized))));
|
|
1079
|
+
}
|
|
1080
|
+
//#endregion
|
|
1081
|
+
//#region src/management.ts
|
|
1082
|
+
function utf8ByteLength$1(text) {
|
|
1083
|
+
return new TextEncoder().encode(text).length;
|
|
1084
|
+
}
|
|
1085
|
+
/**
|
|
1086
|
+
* JSON-facing read-only Kit catalog and workspace inspection over the
|
|
1087
|
+
* authoritative Core-compiled catalog. The Studio never mutates installed
|
|
1088
|
+
* content: source files and the Design Core loader remain the only authorities.
|
|
1089
|
+
*/
|
|
1090
|
+
var StudioManagementService = class {
|
|
1091
|
+
#catalog;
|
|
1092
|
+
/** @param catalog - current authoritative installed Kit snapshot provider. */
|
|
1093
|
+
constructor(catalog) {
|
|
1094
|
+
this.#catalog = catalog;
|
|
1095
|
+
}
|
|
1096
|
+
/**
|
|
1097
|
+
* Build a detached, frozen, JSON-only browser view.
|
|
1098
|
+
* @param query - optional Bundle name/description search.
|
|
1099
|
+
* @returns current published Kit pins and the searched Bundle catalog.
|
|
1100
|
+
*/
|
|
1101
|
+
async workspace(query = "") {
|
|
1102
|
+
const snapshot = await this.#catalog.snapshot();
|
|
1103
|
+
const composition = buildStudioCompositionCatalog(snapshot);
|
|
1104
|
+
const normalizedQuery = query.trim();
|
|
1105
|
+
const kits = Object.freeze(composition.kits.map((kit) => {
|
|
1106
|
+
const definition = snapshot.kits.find((entry) => entry.name === kit.name && entry.version === kit.version).definition;
|
|
1107
|
+
return Object.freeze({
|
|
1108
|
+
name: kit.name,
|
|
1109
|
+
version: kit.version,
|
|
1110
|
+
...definition.description === void 0 ? {} : { description: definition.description },
|
|
1111
|
+
...definition.pickerDescription ? { displayDescription: Object.freeze({ ...definition.pickerDescription }) } : {}
|
|
1112
|
+
});
|
|
1113
|
+
}));
|
|
1114
|
+
const bundles = Object.freeze(searchStudioBundles(composition, normalizedQuery).map((bundle) => Object.freeze({
|
|
1115
|
+
name: bundle.name,
|
|
1116
|
+
version: bundle.version,
|
|
1117
|
+
description: bundle.definition.description,
|
|
1118
|
+
...bundle.definition.title ? { title: Object.freeze({ ...bundle.definition.title }) } : {},
|
|
1119
|
+
...bundle.definition.pickerDescription ? { displayDescription: Object.freeze({ ...bundle.definition.pickerDescription }) } : {},
|
|
1120
|
+
usedBy: Object.freeze(bundle.usedBy.map((kit) => Object.freeze({
|
|
1121
|
+
name: kit.name,
|
|
1122
|
+
version: kit.version
|
|
1123
|
+
})))
|
|
1124
|
+
})));
|
|
1125
|
+
return Object.freeze({
|
|
1126
|
+
query: normalizedQuery,
|
|
1127
|
+
kits,
|
|
1128
|
+
bundles
|
|
1129
|
+
});
|
|
1130
|
+
}
|
|
1131
|
+
/**
|
|
1132
|
+
* Read the Kit workspace view model for one exact installed Kit version.
|
|
1133
|
+
*
|
|
1134
|
+
* The view is JSON-only: the complete raw mindset.md source is exposed for
|
|
1135
|
+
* reading, mounts carry exact version pins plus their reverse Kit
|
|
1136
|
+
* references, and resources project only the paths the Core loader compiled
|
|
1137
|
+
* into the version. No Core-compiled definition object crosses into the view.
|
|
1138
|
+
* @param name - the Kit id.
|
|
1139
|
+
* @param version - exact installed Kit version.
|
|
1140
|
+
* @returns the frozen workspace view, or resolution diagnostics.
|
|
1141
|
+
*/
|
|
1142
|
+
async readKitWorkspace(name, version) {
|
|
1143
|
+
const snapshot = await this.#catalog.snapshot();
|
|
1144
|
+
const entry = snapshot.kits.find((kit) => kit.name === name && kit.version === version);
|
|
1145
|
+
if (entry === void 0) {
|
|
1146
|
+
const installed = snapshot.kits.filter((kit) => kit.name === name).map((kit) => kit.version);
|
|
1147
|
+
if (installed.length === 0) return Object.freeze({ diagnostics: Object.freeze([Object.freeze({
|
|
1148
|
+
code: "workspace.kit_missing",
|
|
1149
|
+
message: `Kit ${JSON.stringify(name)} is not installed`,
|
|
1150
|
+
subject: name
|
|
1151
|
+
})]) });
|
|
1152
|
+
return Object.freeze({ diagnostics: Object.freeze([Object.freeze({
|
|
1153
|
+
code: "workspace.version_missing",
|
|
1154
|
+
message: `Kit ${JSON.stringify(name)} version ${JSON.stringify(version)} is not installed; the installed versions are ${installed.map((candidate) => JSON.stringify(candidate)).join(", ")}`,
|
|
1155
|
+
subject: version
|
|
1156
|
+
})]) });
|
|
1157
|
+
}
|
|
1158
|
+
const definition = entry.definition;
|
|
1159
|
+
const mounts = Object.freeze(entry.bundles.map((bundle) => Object.freeze({
|
|
1160
|
+
name: bundle.name,
|
|
1161
|
+
version: bundle.version,
|
|
1162
|
+
description: bundle.definition.description,
|
|
1163
|
+
...bundle.definition.title ? { title: Object.freeze({ ...bundle.definition.title }) } : {},
|
|
1164
|
+
...bundle.definition.pickerDescription ? { displayDescription: Object.freeze({ ...bundle.definition.pickerDescription }) } : {},
|
|
1165
|
+
mountedBy: Object.freeze(snapshot.kits.filter((kit) => kit.bundles.some((candidate) => candidate.name === bundle.name && candidate.version === bundle.version)).map((kit) => Object.freeze({
|
|
1166
|
+
name: kit.name,
|
|
1167
|
+
version: kit.version
|
|
1168
|
+
})))
|
|
1169
|
+
})));
|
|
1170
|
+
const resources = Object.freeze(Object.entries(definition.resources).map(([path, content]) => Object.freeze({
|
|
1171
|
+
path,
|
|
1172
|
+
bytes: utf8ByteLength$1(content)
|
|
1173
|
+
})).sort((left, right) => left.path < right.path ? -1 : left.path > right.path ? 1 : 0));
|
|
1174
|
+
return Object.freeze({
|
|
1175
|
+
workspace: Object.freeze({
|
|
1176
|
+
name: entry.name,
|
|
1177
|
+
version: entry.version,
|
|
1178
|
+
description: definition.description ?? "",
|
|
1179
|
+
...definition.pickerDescription ? { displayDescription: Object.freeze({ ...definition.pickerDescription }) } : {},
|
|
1180
|
+
mindsetSource: definition.mindsetSource,
|
|
1181
|
+
mounts,
|
|
1182
|
+
resources,
|
|
1183
|
+
relationVocabulary: Object.freeze({
|
|
1184
|
+
schemaVersion: 1,
|
|
1185
|
+
pin: definition.relationVocabulary?.revision ?? "legacy-unavailable",
|
|
1186
|
+
relations: Object.freeze((definition.relationVocabulary?.relations ?? []).map((relation) => Object.freeze({
|
|
1187
|
+
type: relation.type,
|
|
1188
|
+
meaning: relation.meaning,
|
|
1189
|
+
direction: relation.direction,
|
|
1190
|
+
label: Object.freeze({ ...relation.label }),
|
|
1191
|
+
description: Object.freeze({ ...relation.description }),
|
|
1192
|
+
...relation.domain ? { domain: Object.freeze([...relation.domain]) } : {},
|
|
1193
|
+
...relation.range ? { range: Object.freeze([...relation.range]) } : {},
|
|
1194
|
+
...relation.inverse ? { inverse: relation.inverse } : {},
|
|
1195
|
+
...relation.cardinality ? { cardinality: Object.freeze({ ...relation.cardinality }) } : {},
|
|
1196
|
+
...relation.reasoning ? { reasoning: Object.freeze({ ...relation.reasoning }) } : {},
|
|
1197
|
+
...relation.evidence ? { evidence: Object.freeze({ ...relation.evidence }) } : {}
|
|
1198
|
+
})))
|
|
1199
|
+
})
|
|
1200
|
+
}),
|
|
1201
|
+
diagnostics: Object.freeze([])
|
|
1202
|
+
});
|
|
1203
|
+
}
|
|
1204
|
+
};
|
|
1205
|
+
//#endregion
|
|
1206
|
+
//#region src/bundle-source.ts
|
|
1207
|
+
/**
|
|
1208
|
+
* Read-only inspection of real Bundle source files.
|
|
1209
|
+
*
|
|
1210
|
+
* The Bundle content authority is the Kit source directory on disk. The read
|
|
1211
|
+
* model exposes the physical file tree, byte-exact text content, and the
|
|
1212
|
+
* loader-compiled manifest/schema/validation summaries with Panel
|
|
1213
|
+
* classification. Identity is reconciled against the host's Core-compiled
|
|
1214
|
+
* catalog by exact Kit and Bundle name+version; an ambiguous physical source
|
|
1215
|
+
* or a catalog/source disagreement fails loud instead of guessing. The Studio
|
|
1216
|
+
* never writes: traversal, `.git`, directories, and binary content are
|
|
1217
|
+
* rejected, and no mutation port exists.
|
|
1218
|
+
*/
|
|
1219
|
+
function diagnostic(code, message, subject) {
|
|
1220
|
+
return Object.freeze([Object.freeze(subject === void 0 ? {
|
|
1221
|
+
code,
|
|
1222
|
+
message
|
|
1223
|
+
} : {
|
|
1224
|
+
code,
|
|
1225
|
+
message,
|
|
1226
|
+
subject
|
|
1227
|
+
})]);
|
|
1228
|
+
}
|
|
1229
|
+
function jsonProject(value) {
|
|
1230
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return void 0;
|
|
1231
|
+
return Object.freeze(JSON.parse(JSON.stringify(value)));
|
|
1232
|
+
}
|
|
1233
|
+
function utf8ByteLength(content) {
|
|
1234
|
+
return new TextEncoder().encode(content).length;
|
|
1235
|
+
}
|
|
1236
|
+
/**
|
|
1237
|
+
* Split one relative file path into safe segments. Rejects absolute paths,
|
|
1238
|
+
* empty/`.`/`..`/`.git` segments, backslashes, and control characters.
|
|
1239
|
+
* @param path - candidate slash-separated relative path.
|
|
1240
|
+
* @returns the segments, or undefined when the shape is unsafe.
|
|
1241
|
+
*/
|
|
1242
|
+
function safeSegments(path) {
|
|
1243
|
+
if (path === "" || path.startsWith("/") || path.endsWith("/") || path.includes("\\") || /[\u0000-\u001f\u007f]/u.test(path)) return;
|
|
1244
|
+
const parts = path.split("/");
|
|
1245
|
+
if (parts.some((part) => part === "" || part === "." || part === ".." || part === ".git")) return void 0;
|
|
1246
|
+
return parts;
|
|
1247
|
+
}
|
|
1248
|
+
function panelOf(manifest) {
|
|
1249
|
+
const presentation = manifest?.presentation;
|
|
1250
|
+
if (typeof presentation !== "object" || presentation === null || Array.isArray(presentation)) return void 0;
|
|
1251
|
+
const { mode, renderer } = presentation;
|
|
1252
|
+
if (mode === "native") return Object.freeze(typeof renderer === "string" ? {
|
|
1253
|
+
kind: "native",
|
|
1254
|
+
renderer
|
|
1255
|
+
} : { kind: "native" });
|
|
1256
|
+
if (mode === "declarative") return Object.freeze({ kind: "declarative" });
|
|
1257
|
+
}
|
|
1258
|
+
function validationOf(skill) {
|
|
1259
|
+
const document = skill.metadata?.validationDocument;
|
|
1260
|
+
if (document === void 0 || !Array.isArray(document.rules)) return void 0;
|
|
1261
|
+
const drivers = /* @__PURE__ */ new Set();
|
|
1262
|
+
for (const rule of document.rules) if (typeof rule.check?.driver === "string") drivers.add(rule.check.driver);
|
|
1263
|
+
return Object.freeze({
|
|
1264
|
+
rules: document.rules.length,
|
|
1265
|
+
drivers: Object.freeze([...drivers].sort())
|
|
1266
|
+
});
|
|
1267
|
+
}
|
|
1268
|
+
/**
|
|
1269
|
+
* Read-only Bundle inspection over real Kit source files, reconciled against
|
|
1270
|
+
* the host's Core-compiled catalog. The injected port owns the physical
|
|
1271
|
+
* filesystem; this service owns resolution, path safety, identity
|
|
1272
|
+
* reconciliation, and the JSON-only read models.
|
|
1273
|
+
*/
|
|
1274
|
+
var StudioBundleSourceService = class {
|
|
1275
|
+
#catalog;
|
|
1276
|
+
#source;
|
|
1277
|
+
/**
|
|
1278
|
+
* @param catalog - current authoritative installed Kit snapshot provider.
|
|
1279
|
+
* @param source - host-supplied read-only physical source operations.
|
|
1280
|
+
*/
|
|
1281
|
+
constructor(catalog, source) {
|
|
1282
|
+
this.#catalog = catalog;
|
|
1283
|
+
this.#source = source;
|
|
1284
|
+
}
|
|
1285
|
+
/**
|
|
1286
|
+
* Read one Bundle's workspace: physical file tree, loader-compiled
|
|
1287
|
+
* manifest/schema/validation summaries, and Panel classification.
|
|
1288
|
+
* @param name - the Bundle id.
|
|
1289
|
+
* @returns the workspace, or resolution/reconciliation diagnostics.
|
|
1290
|
+
*/
|
|
1291
|
+
async readBundleWorkspace(name, version) {
|
|
1292
|
+
const resolved = await this.#resolve(name, version);
|
|
1293
|
+
if ("diagnostics" in resolved) return resolved;
|
|
1294
|
+
const { kitRoot, bundleRoot } = resolved;
|
|
1295
|
+
let skill;
|
|
1296
|
+
let kitName;
|
|
1297
|
+
let kitVersion;
|
|
1298
|
+
try {
|
|
1299
|
+
if (resolved.skill) skill = resolved.skill;
|
|
1300
|
+
else {
|
|
1301
|
+
const kit = await loadStudioKit(this.#source, kitRoot);
|
|
1302
|
+
const found = kit.skills.find((candidate) => candidate.name === name);
|
|
1303
|
+
if (found === void 0) return Object.freeze({ diagnostics: diagnostic("bundle.not_in_catalog", `Bundle ${JSON.stringify(name)} is not referenced by the Mindset <design_skills> catalog`, name) });
|
|
1304
|
+
skill = found;
|
|
1305
|
+
kitName = kit.name;
|
|
1306
|
+
kitVersion = String(kit.version);
|
|
1307
|
+
}
|
|
1308
|
+
} catch (error) {
|
|
1309
|
+
return Object.freeze({ diagnostics: diagnostic("kit.load", error instanceof Error ? error.message : String(error), name) });
|
|
1310
|
+
}
|
|
1311
|
+
const bundleVersion = String(skill.version);
|
|
1312
|
+
const catalogKit = (await this.#catalog.snapshot()).kits.find((kit) => kit.name === kitName && kit.version === kitVersion);
|
|
1313
|
+
if (!resolved.skill && catalogKit === void 0) return Object.freeze({ diagnostics: diagnostic("studio.source_catalog_mismatch", `source Kit ${JSON.stringify(kitName)}@${JSON.stringify(kitVersion)} is not present in the compiled catalog`, name) });
|
|
1314
|
+
if (!resolved.skill && !catalogKit?.bundles.some((bundle) => bundle.name === name && bundle.version === bundleVersion)) return Object.freeze({ diagnostics: diagnostic("studio.source_catalog_mismatch", `source Bundle ${JSON.stringify(name)}@${JSON.stringify(bundleVersion)} disagrees with the compiled catalog`, name) });
|
|
1315
|
+
const files = [];
|
|
1316
|
+
for (const path of await this.#source.listFiles(bundleRoot)) {
|
|
1317
|
+
let content;
|
|
1318
|
+
try {
|
|
1319
|
+
content = await this.#source.readText(join(bundleRoot, path));
|
|
1320
|
+
} catch (error) {
|
|
1321
|
+
if (error instanceof StudioBinaryContentError) continue;
|
|
1322
|
+
throw error;
|
|
1323
|
+
}
|
|
1324
|
+
if (content === void 0) continue;
|
|
1325
|
+
files.push(Object.freeze({
|
|
1326
|
+
path,
|
|
1327
|
+
bytes: utf8ByteLength(content)
|
|
1328
|
+
}));
|
|
1329
|
+
}
|
|
1330
|
+
const manifest = jsonProject(skill.metadata?.skillManifest);
|
|
1331
|
+
const schema = jsonProject(skill.metadata?.schemaDocument);
|
|
1332
|
+
const validation = validationOf(skill);
|
|
1333
|
+
const panel = panelOf(manifest);
|
|
1334
|
+
return Object.freeze({
|
|
1335
|
+
workspace: Object.freeze({
|
|
1336
|
+
name,
|
|
1337
|
+
version: bundleVersion,
|
|
1338
|
+
...kitName === void 0 ? {} : { kit: kitName },
|
|
1339
|
+
description: skill.description,
|
|
1340
|
+
...skill.pickerDescription ? { displayDescription: Object.freeze({ ...skill.pickerDescription }) } : {},
|
|
1341
|
+
files: Object.freeze(files),
|
|
1342
|
+
...manifest === void 0 ? {} : { manifest },
|
|
1343
|
+
...schema === void 0 ? {} : { schema },
|
|
1344
|
+
...validation === void 0 ? {} : { validation },
|
|
1345
|
+
...panel === void 0 ? {} : { panel }
|
|
1346
|
+
}),
|
|
1347
|
+
diagnostics: Object.freeze([])
|
|
1348
|
+
});
|
|
1349
|
+
}
|
|
1350
|
+
/**
|
|
1351
|
+
* Read one complete Bundle file byte-exactly as UTF-8 text.
|
|
1352
|
+
* @param input - Bundle id and Bundle-relative path.
|
|
1353
|
+
* @returns the file content, or resolution diagnostics.
|
|
1354
|
+
*/
|
|
1355
|
+
async readBundleFile(input) {
|
|
1356
|
+
const located = await this.#locate(input.name, input.path, input.version);
|
|
1357
|
+
if ("diagnostics" in located) return located;
|
|
1358
|
+
let content;
|
|
1359
|
+
try {
|
|
1360
|
+
content = await this.#source.readText(located.absolute);
|
|
1361
|
+
} catch (error) {
|
|
1362
|
+
if (error instanceof StudioBinaryContentError) return Object.freeze({ diagnostics: diagnostic("bundle_file.binary", `Bundle ${JSON.stringify(input.name)} file ${JSON.stringify(input.path)} is not UTF-8 text`, input.path) });
|
|
1363
|
+
throw error;
|
|
1364
|
+
}
|
|
1365
|
+
if (content === void 0) return Object.freeze({ diagnostics: diagnostic("bundle_file.missing", `Bundle ${JSON.stringify(input.name)} has no file ${JSON.stringify(input.path)}`, input.path) });
|
|
1366
|
+
return Object.freeze({
|
|
1367
|
+
file: Object.freeze({
|
|
1368
|
+
path: input.path,
|
|
1369
|
+
content
|
|
1370
|
+
}),
|
|
1371
|
+
diagnostics: Object.freeze([])
|
|
1372
|
+
});
|
|
1373
|
+
}
|
|
1374
|
+
/** Resolve one Bundle id to exactly one physical root; same-name multi-root fails loud. */
|
|
1375
|
+
async #resolve(name, version) {
|
|
1376
|
+
if (this.#source.resolveBundleRoot) {
|
|
1377
|
+
const candidates = ((await this.#catalog.snapshot()).bundles ?? []).filter((bundle) => bundle.name === name && (version === void 0 || bundle.version === version));
|
|
1378
|
+
if (candidates.length !== 1) return { diagnostics: diagnostic(candidates.length ? "studio.ambiguous_version" : "bundle.missing", `Select one registered exact version of Bundle ${JSON.stringify(name)}`, name) };
|
|
1379
|
+
const selected = candidates[0];
|
|
1380
|
+
const root = await this.#source.resolveBundleRoot({
|
|
1381
|
+
name,
|
|
1382
|
+
version: selected.version
|
|
1383
|
+
});
|
|
1384
|
+
if (!root) return { diagnostics: diagnostic("bundle.missing", `Missing retained source for ${name}@${selected.version}`, name) };
|
|
1385
|
+
if ((selected.definition.catalogName ?? selected.definition.name) !== name || String(selected.definition.version) !== selected.version) return { diagnostics: diagnostic("studio.source_catalog_mismatch", "Independent Bundle definition disagrees with its exact pin", name) };
|
|
1386
|
+
return {
|
|
1387
|
+
bundleRoot: root,
|
|
1388
|
+
skill: selected.definition
|
|
1389
|
+
};
|
|
1390
|
+
}
|
|
1391
|
+
const matches = [];
|
|
1392
|
+
for (const kitRoot of await this.#source.listKitRoots()) {
|
|
1393
|
+
const root = resolve(kitRoot);
|
|
1394
|
+
const bundleRoot = join(root, "skills", name);
|
|
1395
|
+
if (await this.#source.isDirectory(bundleRoot)) matches.push({
|
|
1396
|
+
kitRoot: root,
|
|
1397
|
+
bundleRoot
|
|
1398
|
+
});
|
|
1399
|
+
}
|
|
1400
|
+
if (matches.length === 0) return { diagnostics: diagnostic("bundle.missing", `no source Bundle named ${JSON.stringify(name)}`, name) };
|
|
1401
|
+
if (matches.length > 1) return { diagnostics: diagnostic("studio.duplicate_source", `Bundle ${JSON.stringify(name)} exists in ${matches.length} configured Kit sources: ${matches.map((match) => JSON.stringify(match.bundleRoot)).join(", ")}`, name) };
|
|
1402
|
+
const match = matches[0];
|
|
1403
|
+
if (version !== void 0) try {
|
|
1404
|
+
const kit = await loadStudioKit(this.#source, match.kitRoot);
|
|
1405
|
+
const skill = kit.skills.find((candidate) => candidate.name === name);
|
|
1406
|
+
if (skill === void 0 || String(skill.version) !== version) return { diagnostics: diagnostic("bundle.version_missing", `Source does not provide Bundle ${JSON.stringify(name)}@${JSON.stringify(version)}`, version) };
|
|
1407
|
+
if (!(await this.#catalog.snapshot()).kits.find((candidate) => candidate.name === kit.name && candidate.version === String(kit.version))?.bundles.some((bundle) => bundle.name === name && bundle.version === version)) return { diagnostics: diagnostic("studio.source_catalog_mismatch", "Legacy source does not match the registered exact Bundle version", name) };
|
|
1408
|
+
} catch (error) {
|
|
1409
|
+
return { diagnostics: diagnostic("kit.load", error instanceof Error ? error.message : String(error), name) };
|
|
1410
|
+
}
|
|
1411
|
+
return match;
|
|
1412
|
+
}
|
|
1413
|
+
/** Resolve and path-check one Bundle file target against the physical file tree. */
|
|
1414
|
+
async #locate(name, path, version) {
|
|
1415
|
+
const resolved = await this.#resolve(name, version);
|
|
1416
|
+
if ("diagnostics" in resolved) return resolved;
|
|
1417
|
+
if (safeSegments(path) === void 0) return { diagnostics: diagnostic("bundle_file.path_forbidden", `path must stay inside the Bundle directory: ${JSON.stringify(path)}`, path) };
|
|
1418
|
+
if (!(await this.#source.listFiles(resolved.bundleRoot)).includes(path)) return { diagnostics: diagnostic("bundle_file.missing", `Bundle ${JSON.stringify(name)} has no regular file ${JSON.stringify(path)}`, path) };
|
|
1419
|
+
return { absolute: join(resolved.bundleRoot, path) };
|
|
1420
|
+
}
|
|
1421
|
+
};
|
|
1422
|
+
//#endregion
|
|
1423
|
+
export { StudioBinaryContentError, StudioBundleSourceService, StudioKitDraftService, StudioManagementService, StudioPanelDraftService, buildStudioCompositionCatalog, exportStudioContentArchive, loadStudioKit, prepareStudioContentImport, prepareStudioPanelPreview, readStudioContentArchive, searchStudioBundles, snapshotKit, validateBundle, validateKit };
|