@arcadia-eternity/content-sdk 4.6.0 → 4.8.0
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/README.md +2 -2
- package/dist/author-api.d.ts +12 -2
- package/dist/author-api.js +11 -2
- package/dist/author-git.d.ts +63 -0
- package/dist/author-git.js +324 -0
- package/dist/author-reference.d.ts +15 -1
- package/dist/author-reference.js +20 -1
- package/dist/cli.js +5 -4
- package/dist/content-query.d.ts +15 -1
- package/dist/content-query.js +53 -11
- package/dist/content-workspace.d.ts +428 -3
- package/dist/content-workspace.js +249 -54
- package/dist/debug-environments.d.ts +393 -25
- package/dist/debug-environments.js +3 -42
- package/dist/demo.js +1 -1
- package/dist/effect-declarations.d.ts +14 -0
- package/dist/effect-declarations.js +103 -0
- package/dist/environment-host-base.d.ts +175 -0
- package/dist/environment-host-base.js +86 -0
- package/dist/environment-member-transfer.d.ts +5 -0
- package/dist/environment-member-transfer.js +95 -0
- package/dist/environment-rules.d.ts +728 -3
- package/dist/environment-rules.js +158 -74
- package/dist/environment-sharing.d.ts +51 -0
- package/dist/environment-sharing.js +321 -0
- package/dist/environment.d.ts +2 -0
- package/dist/environment.js +32 -9
- package/dist/jobs.d.ts +5 -0
- package/dist/jobs.js +59 -10
- package/dist/learnsets.d.ts +47 -0
- package/dist/learnsets.js +124 -0
- package/dist/lib/content.d.ts +1 -1
- package/dist/lib/content.js +26 -2
- package/dist/lib/presentation.js +5 -11
- package/dist/managed-projects.d.ts +6 -0
- package/dist/managed-projects.js +137 -45
- package/dist/managed-sources.d.ts +17 -0
- package/dist/managed-sources.js +28 -10
- package/dist/mcp-contracts.d.ts +405 -4
- package/dist/mcp-contracts.js +3 -1
- package/dist/mcp.js +23 -1
- package/dist/pack-project.d.ts +2 -1
- package/dist/pack-project.js +40 -5
- package/dist/prepared-document.d.ts +476 -0
- package/dist/prepared-document.js +47 -0
- package/dist/preview.js +5 -5
- package/dist/project.d.ts +3 -1
- package/dist/project.js +47 -4
- package/dist/rule-composition.d.ts +290 -0
- package/dist/rule-composition.js +267 -0
- package/dist/template/.github/scripts/release-payload.mjs +91 -0
- package/dist/template/.github/scripts/release-test-npm.mjs +27 -0
- package/dist/template/.github/scripts/verify-release.mjs +138 -0
- package/dist/template/.github/workflows/release-pack.yml +77 -0
- package/dist/template/README.md +15 -2
- package/dist/template/gitignore-template +7 -0
- package/dist/template/npm-lock-template.json +2319 -0
- package/dist/template/package.json +4 -2
- package/dist/template/skills/arcadia-content-authoring/references/tool-workflows.md +1 -1
- package/dist/template/skills/arcadia-content-git/SKILL.md +14 -0
- package/dist/template/skills/arcadia-content-release/SKILL.md +29 -0
- package/dist/template/test/pack.test.mjs +24 -5
- package/dist/testing.d.ts +1 -0
- package/dist/testing.js +26 -7
- package/dist/workspace-cli.js +2 -2
- package/dist/workspace-materials.d.ts +4 -1
- package/dist/workspace-materials.js +8 -2
- package/dist/workspace.d.ts +11 -0
- package/dist/workspace.js +191 -31
- package/package.json +16 -4
- package/release-payload.mjs +91 -0
- package/release-test-npm.mjs +27 -0
|
@@ -1,29 +1,60 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { sha256Hex, skillParameterCatalogSchema, readElementCatalog, } from '@arcadia-eternity/const';
|
|
2
|
+
import { randomUUID } from 'node:crypto';
|
|
3
|
+
import { isDeepStrictEqual } from 'node:util';
|
|
2
4
|
import { lstat, mkdir, readFile, readdir, realpath, rename, rm, writeFile } from 'node:fs/promises';
|
|
3
5
|
import { join, resolve } from 'node:path';
|
|
4
6
|
import writeFileAtomic from 'write-file-atomic';
|
|
5
7
|
import semver from 'semver';
|
|
6
8
|
import { inspectNpmProject } from '@arcadia-eternity/pack-loader/node';
|
|
7
|
-
import { normalizeEnvironmentDocument, EnvironmentDocumentV2Schema, } from './debug-environments.js';
|
|
9
|
+
import { normalizeEnvironmentDocument, EnvironmentDocumentV2Schema, EnvironmentRulesSchema, EnvironmentRuleDeclarationSchema, } from './debug-environments.js';
|
|
10
|
+
import { z } from 'zod';
|
|
8
11
|
import { withSourceLock } from './source-lock.js';
|
|
9
|
-
import { contentError, materialFiles, writeMaterials } from './workspace-materials.js';
|
|
12
|
+
import { contentError, readJson, materialFiles, writeMaterials } from './workspace-materials.js';
|
|
10
13
|
import { errorCode, isRecord } from './types.js';
|
|
14
|
+
import { compactMaterials, expandSharedMaterials, exportDependencies, } from './environment-sharing.js';
|
|
15
|
+
import { environmentShareMaterials } from './environment-host-base.js';
|
|
16
|
+
export { copyEnvironmentMember } from './environment-member-transfer.js';
|
|
17
|
+
import { parseManifestDraft, preparedDocument } from './prepared-document.js';
|
|
18
|
+
export { readInstalledEnvironmentDocument } from './prepared-document.js';
|
|
11
19
|
export { normalizeEnvironmentDocument } from './debug-environments.js';
|
|
20
|
+
const compositionSchema = z
|
|
21
|
+
.object({
|
|
22
|
+
rules: EnvironmentRulesSchema,
|
|
23
|
+
requiredCapabilities: z.array(z.string()).optional(),
|
|
24
|
+
fields: z.array(z.object({
|
|
25
|
+
path: z.array(z.string()),
|
|
26
|
+
value: z.unknown(),
|
|
27
|
+
sources: z.array(z.string()),
|
|
28
|
+
effectiveSources: z.array(z.string()),
|
|
29
|
+
})),
|
|
30
|
+
ruleSets: z.array(z.object({ id: z.string(), name: z.string(), rules: EnvironmentRuleDeclarationSchema })),
|
|
31
|
+
})
|
|
32
|
+
.strict();
|
|
33
|
+
const contentChoices = z.array(z.object({ id: z.string(), name: z.string().optional() }));
|
|
34
|
+
const contentCatalogSchema = z.object({
|
|
35
|
+
species: contentChoices,
|
|
36
|
+
skills: contentChoices,
|
|
37
|
+
marks: contentChoices,
|
|
38
|
+
stages: contentChoices,
|
|
39
|
+
});
|
|
40
|
+
export const exportSelectionSchema = z
|
|
41
|
+
.object({
|
|
42
|
+
revision: z.string().min(1),
|
|
43
|
+
locations: z.array(z.string().min(1)).max(50000),
|
|
44
|
+
})
|
|
45
|
+
.strict();
|
|
46
|
+
function exportRevision(draft, content, dependencies) {
|
|
47
|
+
return sha256Hex(JSON.stringify([draft, content, dependencies]));
|
|
48
|
+
}
|
|
12
49
|
const uuid = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
|
50
|
+
const builtin120IdPattern = new RegExp('^builtin-120-' + uuid.source.slice(1));
|
|
13
51
|
const envId = /^[a-z0-9][a-z0-9_-]{0,79}$/;
|
|
14
|
-
const revision = (text) => createHash('sha256').update(text).digest('hex');
|
|
15
52
|
const json = (value) => JSON.stringify(value, null, 2) + '\n';
|
|
16
53
|
const checkId = (id, pattern = uuid) => {
|
|
17
54
|
if (typeof id !== 'string' || !pattern.test(id))
|
|
18
55
|
throw contentError('RESOURCE_ID_INVALID');
|
|
19
56
|
return id;
|
|
20
57
|
};
|
|
21
|
-
async function readJson(file, limit = 4 * 1024 * 1024) {
|
|
22
|
-
const info = await lstat(file);
|
|
23
|
-
if (!info.isFile() || info.isSymbolicLink() || info.size > limit)
|
|
24
|
-
throw contentError('WORKSPACE_FILE_INVALID');
|
|
25
|
-
return JSON.parse(await readFile(file, 'utf8'));
|
|
26
|
-
}
|
|
27
58
|
async function regularDirectory(path) {
|
|
28
59
|
await mkdir(path, { recursive: true, mode: 0o700 });
|
|
29
60
|
const info = await lstat(path);
|
|
@@ -55,6 +86,14 @@ async function checkedPreparation(result, document, directory) {
|
|
|
55
86
|
return {
|
|
56
87
|
contentRevision: result.contentRevision,
|
|
57
88
|
packages,
|
|
89
|
+
...(result.contentCatalog !== undefined && { contentCatalog: contentCatalogSchema.parse(result.contentCatalog) }),
|
|
90
|
+
...(result.ruleComposition !== undefined && { ruleComposition: compositionSchema.parse(result.ruleComposition) }),
|
|
91
|
+
...(result.skillParameters !== undefined && {
|
|
92
|
+
skillParameters: skillParameterCatalogSchema.parse(result.skillParameters),
|
|
93
|
+
}),
|
|
94
|
+
...(isRecord(result.elementCatalog) && {
|
|
95
|
+
elementCatalog: readElementCatalog(result.elementCatalog),
|
|
96
|
+
}),
|
|
58
97
|
members: (inspection.members ?? []),
|
|
59
98
|
};
|
|
60
99
|
}
|
|
@@ -73,12 +112,12 @@ function activeProcess(pid) {
|
|
|
73
112
|
throw error;
|
|
74
113
|
}
|
|
75
114
|
}
|
|
76
|
-
/** One owner
|
|
77
|
-
* never a selectable historical version. Runtime loaders capture their own inputs. */
|
|
115
|
+
/** One write owner publishes prepared documents alongside the locked npm materials. */
|
|
78
116
|
class ContentWorkspaceImplementation {
|
|
79
117
|
root;
|
|
80
118
|
bootId = randomUUID();
|
|
81
119
|
workspaceId;
|
|
120
|
+
builtin120EnvironmentId;
|
|
82
121
|
#queue = Promise.resolve();
|
|
83
122
|
#closed = false;
|
|
84
123
|
#controller = new AbortController();
|
|
@@ -121,8 +160,13 @@ class ContentWorkspaceImplementation {
|
|
|
121
160
|
}
|
|
122
161
|
else {
|
|
123
162
|
workspace.workspaceId = randomUUID();
|
|
124
|
-
await writeFileAtomic(file, json({ format: 'arcadia.content-workspace/2', workspaceId: workspace.workspaceId }));
|
|
125
163
|
}
|
|
164
|
+
if (metadata?.builtin120EnvironmentId !== undefined)
|
|
165
|
+
workspace.builtin120EnvironmentId = checkId(metadata.builtin120EnvironmentId, builtin120IdPattern);
|
|
166
|
+
else if (options.includeBuiltin120)
|
|
167
|
+
workspace.builtin120EnvironmentId = 'builtin-120-' + randomUUID();
|
|
168
|
+
if (!metadata || workspace.builtin120EnvironmentId !== metadata.builtin120EnvironmentId)
|
|
169
|
+
await workspace.#saveMetadata();
|
|
126
170
|
// Recover an interrupted directory replacement before exposing projects.
|
|
127
171
|
for (const name of await readdir(join(root, 'staging'))) {
|
|
128
172
|
if (!name.startsWith('replaced-') || !envId.test(name.slice(9)))
|
|
@@ -146,6 +190,13 @@ class ContentWorkspaceImplementation {
|
|
|
146
190
|
throw error;
|
|
147
191
|
}
|
|
148
192
|
}
|
|
193
|
+
async #saveMetadata() {
|
|
194
|
+
await writeFileAtomic(join(this.root, 'workspace.json'), json({
|
|
195
|
+
format: 'arcadia.content-workspace/2',
|
|
196
|
+
workspaceId: this.workspaceId,
|
|
197
|
+
builtin120EnvironmentId: this.builtin120EnvironmentId,
|
|
198
|
+
}));
|
|
199
|
+
}
|
|
149
200
|
async #assertOwner() {
|
|
150
201
|
if (this.#closed)
|
|
151
202
|
throw contentError('WORKSPACE_CLOSED');
|
|
@@ -178,7 +229,7 @@ class ContentWorkspaceImplementation {
|
|
|
178
229
|
return;
|
|
179
230
|
const archive = join(this.root, 'migration-backups');
|
|
180
231
|
await regularDirectory(archive);
|
|
181
|
-
await writeFileAtomic(join(archive, `${id}-${
|
|
232
|
+
await writeFileAtomic(join(archive, `${id}-${sha256Hex(text)}.json`), text);
|
|
182
233
|
}
|
|
183
234
|
#manifest(document, previous = {}) {
|
|
184
235
|
const { packages, ...metadata } = document;
|
|
@@ -201,19 +252,10 @@ class ContentWorkspaceImplementation {
|
|
|
201
252
|
throw contentError('WORKSPACE_PATH_INVALID');
|
|
202
253
|
await readJson(join(directory, 'package.json'));
|
|
203
254
|
const text = await readFile(join(this.#directory(id), 'package.json'), 'utf8');
|
|
204
|
-
const
|
|
205
|
-
if (
|
|
206
|
-
throw contentError('ENVIRONMENT_MANIFEST_INVALID');
|
|
207
|
-
const sources = manifest.arcadiaEnvironment.sources;
|
|
208
|
-
if (!Array.isArray(sources))
|
|
209
|
-
throw contentError('ENVIRONMENT_MANIFEST_INVALID');
|
|
210
|
-
const document = normalizeEnvironmentDocument({
|
|
211
|
-
...manifest.arcadiaEnvironment,
|
|
212
|
-
packages: Object.fromEntries(Object.entries(manifest.dependencies).filter(([name]) => !sources.includes(name))),
|
|
213
|
-
});
|
|
214
|
-
if (document.id !== id)
|
|
255
|
+
const draft = parseManifestDraft(text);
|
|
256
|
+
if (draft.document.id !== id)
|
|
215
257
|
throw contentError('ENVIRONMENT_ID_MISMATCH');
|
|
216
|
-
return
|
|
258
|
+
return draft;
|
|
217
259
|
}
|
|
218
260
|
listDrafts() {
|
|
219
261
|
return this.#exclusive(() => this.#listDrafts());
|
|
@@ -240,10 +282,20 @@ class ContentWorkspaceImplementation {
|
|
|
240
282
|
throw error;
|
|
241
283
|
return null;
|
|
242
284
|
});
|
|
243
|
-
if ((previous === null ? null :
|
|
285
|
+
if ((previous === null ? null : sha256Hex(previous)) !== input.expectedRevision)
|
|
244
286
|
throw contentError('REVISION_CONFLICT');
|
|
245
287
|
await regularDirectory(directory);
|
|
246
288
|
await this.#archiveRemovedDeclarations(document.id);
|
|
289
|
+
if (previous !== null) {
|
|
290
|
+
const receiptFile = join(directory, '.arcadia-prepared.json');
|
|
291
|
+
const receipt = await readJson(receiptFile).catch(error => {
|
|
292
|
+
if (errorCode(error) !== 'ENOENT')
|
|
293
|
+
throw error;
|
|
294
|
+
return undefined;
|
|
295
|
+
});
|
|
296
|
+
if (receipt && !receipt.document && receipt.draftRevision === sha256Hex(previous))
|
|
297
|
+
await writeFileAtomic(receiptFile, json({ ...receipt, document: parseManifestDraft(previous).document }));
|
|
298
|
+
}
|
|
247
299
|
await writeFileAtomic(file, json(this.#manifest(document, previous ? JSON.parse(previous) : {})));
|
|
248
300
|
return this.#draft(document.id);
|
|
249
301
|
}, true);
|
|
@@ -261,21 +313,34 @@ class ContentWorkspaceImplementation {
|
|
|
261
313
|
}
|
|
262
314
|
async #installation(id) {
|
|
263
315
|
const directory = this.#directory(id);
|
|
264
|
-
await readJson(join(directory, 'package-lock.json'), 16 * 1024 * 1024);
|
|
316
|
+
const lock = await readJson(join(directory, 'package-lock.json'), 16 * 1024 * 1024);
|
|
265
317
|
await lstat(join(directory, 'node_modules'));
|
|
266
|
-
const draft = await this.#draft(id);
|
|
267
318
|
const metadata = await readJson(join(directory, '.arcadia-prepared.json'));
|
|
268
319
|
if (metadata.format !== 'arcadia.environment-installation/1' ||
|
|
269
320
|
metadata.installationId !== id ||
|
|
270
321
|
typeof metadata.contentRevision !== 'string')
|
|
271
322
|
throw contentError('ENVIRONMENT_INSTALLATION_INVALID');
|
|
272
|
-
|
|
323
|
+
const document = preparedDocument(metadata, metadata.document === undefined ? await readFile(join(directory, 'package.json'), 'utf8') : '');
|
|
324
|
+
const locked = isRecord(lock.packages) ? lock.packages : {};
|
|
325
|
+
const members = Array.isArray(metadata.members)
|
|
326
|
+
? metadata.members.map(member => {
|
|
327
|
+
const entry = locked['node_modules/' + member.name];
|
|
328
|
+
return isRecord(entry) &&
|
|
329
|
+
typeof entry.resolved === 'string' &&
|
|
330
|
+
/^file:candidates\/[a-zA-Z0-9_.-]+\.tgz$/.test(entry.resolved) &&
|
|
331
|
+
typeof entry.integrity === 'string' &&
|
|
332
|
+
member.name !== '@arcadia-eternity/data-pack-base'
|
|
333
|
+
? { ...member, archiveIntegrity: entry.integrity }
|
|
334
|
+
: member;
|
|
335
|
+
})
|
|
336
|
+
: undefined;
|
|
273
337
|
return {
|
|
274
338
|
...metadata,
|
|
339
|
+
members,
|
|
275
340
|
installationId: id,
|
|
276
341
|
environmentId: id,
|
|
277
342
|
workspaceId: this.workspaceId,
|
|
278
|
-
document
|
|
343
|
+
document,
|
|
279
344
|
directory,
|
|
280
345
|
materialDirectory: `environments/${id}`,
|
|
281
346
|
status: 'ready',
|
|
@@ -322,6 +387,7 @@ class ContentWorkspaceImplementation {
|
|
|
322
387
|
throw contentError('ENVIRONMENT_ID_MISMATCH');
|
|
323
388
|
const source = input.projectDirectory ??
|
|
324
389
|
input.directory ??
|
|
390
|
+
input.seedDirectory ??
|
|
325
391
|
(await this.#options.resolveProject?.(input.environmentId)) ??
|
|
326
392
|
this.#directory(input.environmentId);
|
|
327
393
|
return this.#install(document, source, preparer, input);
|
|
@@ -338,10 +404,14 @@ class ContentWorkspaceImplementation {
|
|
|
338
404
|
let journaled = false;
|
|
339
405
|
try {
|
|
340
406
|
await input.onProgress?.({ stage: 'preparing' });
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
407
|
+
const limits = { ...this.#options, signal };
|
|
408
|
+
let materials = await materialFiles(source, limits);
|
|
409
|
+
if (input.importing) {
|
|
410
|
+
const restored = await environmentShareMaterials(materials, document, limits);
|
|
411
|
+
materials = restored.files;
|
|
412
|
+
document = restored.document;
|
|
413
|
+
}
|
|
414
|
+
await writeMaterials(staging, materials, limits);
|
|
345
415
|
const manifest = await readJson(join(staging, 'package.json'));
|
|
346
416
|
const base = '@arcadia-eternity/data-pack-base';
|
|
347
417
|
if (Object.hasOwn(document.packages, base) || document.sources.includes(base)) {
|
|
@@ -360,10 +430,15 @@ class ContentWorkspaceImplementation {
|
|
|
360
430
|
await writeFile(join(staging, '.arcadia-prepared.json'), json({
|
|
361
431
|
format: 'arcadia.environment-installation/1',
|
|
362
432
|
installationId: document.id,
|
|
363
|
-
|
|
433
|
+
document,
|
|
434
|
+
draftRevision: sha256Hex(text),
|
|
364
435
|
contentRevision: result.contentRevision,
|
|
365
436
|
packages: result.packages,
|
|
366
437
|
members: result.members,
|
|
438
|
+
ruleComposition: result.ruleComposition,
|
|
439
|
+
skillParameters: result.skillParameters,
|
|
440
|
+
contentCatalog: result.contentCatalog,
|
|
441
|
+
elementCatalog: result.elementCatalog,
|
|
367
442
|
createdAt: Date.now(),
|
|
368
443
|
...(input.importId ? { importId: input.importId } : {}),
|
|
369
444
|
}));
|
|
@@ -452,9 +527,6 @@ class ContentWorkspaceImplementation {
|
|
|
452
527
|
...((await this.#options.retentions?.list(id)) ?? []).map(x => ({ kind: 'retention', ...x })),
|
|
453
528
|
];
|
|
454
529
|
}
|
|
455
|
-
references(id) {
|
|
456
|
-
return this.#exclusive(() => this.#references(id));
|
|
457
|
-
}
|
|
458
530
|
clearInstallation(id) {
|
|
459
531
|
return this.#exclusive(async () => {
|
|
460
532
|
const references = await this.#references(id);
|
|
@@ -465,21 +537,78 @@ class ContentWorkspaceImplementation {
|
|
|
465
537
|
await rm(join(directory, '.arcadia-prepared.json'), { force: true });
|
|
466
538
|
}, true);
|
|
467
539
|
}
|
|
468
|
-
async #
|
|
540
|
+
async #exportVersion(id) {
|
|
469
541
|
const draft = await this.#draft(id);
|
|
470
542
|
const installation = await this.#installation(id);
|
|
471
543
|
if (installation.draftRevision !== draft.revision)
|
|
472
544
|
throw contentError('ENVIRONMENT_PREPARATION_REQUIRED', '环境已修改,请先准备环境再导出');
|
|
473
|
-
|
|
545
|
+
return { draft, installation };
|
|
546
|
+
}
|
|
547
|
+
async #shareFiles(id, document) {
|
|
548
|
+
return environmentShareMaterials(await materialFiles(this.#directory(id), this.#options), document);
|
|
549
|
+
}
|
|
550
|
+
previewEnvironmentExport(id) {
|
|
551
|
+
return this.#exclusive(async () => {
|
|
552
|
+
const { draft, installation } = await this.#exportVersion(id);
|
|
553
|
+
const lock = await readJson(join(this.#directory(id), 'package-lock.json'), 16 * 1024 * 1024);
|
|
554
|
+
const hasOnline = Object.values(lock.packages ?? {}).some(entry => isRecord(entry) && typeof entry.resolved === 'string' && entry.resolved.startsWith('https:'));
|
|
555
|
+
const dependencies = hasOnline ? exportDependencies((await this.#shareFiles(id, draft.document)).files) : [];
|
|
556
|
+
return { revision: exportRevision(draft.revision, installation.contentRevision, dependencies), dependencies };
|
|
557
|
+
});
|
|
558
|
+
}
|
|
559
|
+
missingEnvironmentArchives(probe) {
|
|
560
|
+
return this.#exclusive(async () => {
|
|
561
|
+
if (probe?.lock?.path !== 'package-lock.json' ||
|
|
562
|
+
!Array.isArray(probe.archives) ||
|
|
563
|
+
probe.archives.length > (this.#options.maxFiles ?? 50000) ||
|
|
564
|
+
probe.archives.some(ref => !ref?.external))
|
|
565
|
+
throw contentError('ENVIRONMENT_EXPORT_INVALID');
|
|
566
|
+
const missing = new Set();
|
|
567
|
+
await expandSharedMaterials([probe.lock], probe.archives, {
|
|
568
|
+
...this.#options,
|
|
569
|
+
offline: true,
|
|
570
|
+
missing,
|
|
571
|
+
signal: this.#controller.signal,
|
|
572
|
+
environments: join(this.root, 'environments'),
|
|
573
|
+
cache: this.#options.npmCache ?? join(this.root, 'npm-cache'),
|
|
574
|
+
});
|
|
575
|
+
return [...missing];
|
|
576
|
+
});
|
|
577
|
+
}
|
|
578
|
+
async #export(id, mode) {
|
|
579
|
+
const { draft, installation } = await this.#exportVersion(id);
|
|
580
|
+
const { contentRevision } = installation;
|
|
581
|
+
const shared = await this.#shareFiles(id, draft.document);
|
|
582
|
+
let files = shared.files;
|
|
583
|
+
let archives = [];
|
|
584
|
+
if (mode !== 'offline') {
|
|
585
|
+
let locations = [];
|
|
586
|
+
if (mode !== 'share') {
|
|
587
|
+
const selection = exportSelectionSchema.safeParse(mode);
|
|
588
|
+
if (!selection.success)
|
|
589
|
+
throw contentError('ENVIRONMENT_EXPORT_INVALID');
|
|
590
|
+
if (exportRevision(draft.revision, contentRevision, exportDependencies(files)) !== selection.data.revision)
|
|
591
|
+
throw contentError('REVISION_CONFLICT', '环境已修改,请重新导出');
|
|
592
|
+
locations = selection.data.locations;
|
|
593
|
+
}
|
|
594
|
+
const compact = await compactMaterials(files, this.#options, locations);
|
|
595
|
+
files = compact.materials;
|
|
596
|
+
archives = compact.archives;
|
|
597
|
+
}
|
|
474
598
|
return {
|
|
475
|
-
format:
|
|
476
|
-
|
|
477
|
-
|
|
599
|
+
format: shared.usesBase
|
|
600
|
+
? 'arcadia.environment-export/4'
|
|
601
|
+
: archives.length
|
|
602
|
+
? 'arcadia.environment-export/3'
|
|
603
|
+
: 'arcadia.environment-export/2',
|
|
604
|
+
...(archives.length || shared.usesBase ? { archives } : {}),
|
|
605
|
+
document: shared.document,
|
|
606
|
+
contentRevision,
|
|
478
607
|
files: files.map(({ path, bytes }) => ({ path, data: bytes.toString('base64') })),
|
|
479
608
|
};
|
|
480
609
|
}
|
|
481
|
-
exportEnvironment(id) {
|
|
482
|
-
return this.#exclusive(() => this.#export(id));
|
|
610
|
+
exportEnvironment(id, mode = 'share') {
|
|
611
|
+
return this.#exclusive(() => this.#export(id, mode));
|
|
483
612
|
}
|
|
484
613
|
async #snapshot() {
|
|
485
614
|
for (const name of await readdir(join(this.root, 'imports'))) {
|
|
@@ -503,7 +632,13 @@ class ContentWorkspaceImplementation {
|
|
|
503
632
|
const files = await materialFiles(this.#directory(draft.document.id), this.#options);
|
|
504
633
|
installations.push({ version, files: files.map(({ path, bytes }) => ({ path, data: bytes.toString('base64') })) });
|
|
505
634
|
}
|
|
506
|
-
return {
|
|
635
|
+
return {
|
|
636
|
+
format: 'arcadia.workspace-snapshot/2',
|
|
637
|
+
workspaceId: this.workspaceId,
|
|
638
|
+
builtin120EnvironmentId: this.builtin120EnvironmentId,
|
|
639
|
+
drafts,
|
|
640
|
+
installations,
|
|
641
|
+
};
|
|
507
642
|
}
|
|
508
643
|
exportSnapshot() {
|
|
509
644
|
return this.#exclusive(() => this.#snapshot());
|
|
@@ -511,7 +646,7 @@ class ContentWorkspaceImplementation {
|
|
|
511
646
|
captureSnapshot() {
|
|
512
647
|
return this.#exclusive(async () => {
|
|
513
648
|
const snapshot = await this.#snapshot();
|
|
514
|
-
this.#snapshotFingerprint =
|
|
649
|
+
this.#snapshotFingerprint = sha256Hex(json(snapshot));
|
|
515
650
|
const stamp = { workspaceId: this.workspaceId, bootId: this.bootId, generation: ++this.#snapshotGeneration };
|
|
516
651
|
return { snapshot, stamp };
|
|
517
652
|
});
|
|
@@ -525,7 +660,7 @@ class ContentWorkspaceImplementation {
|
|
|
525
660
|
return false;
|
|
526
661
|
const expected = this.#snapshotFingerprint;
|
|
527
662
|
this.#snapshotFingerprint = undefined;
|
|
528
|
-
return
|
|
663
|
+
return sha256Hex(json(await this.#snapshot())) === expected;
|
|
529
664
|
});
|
|
530
665
|
}
|
|
531
666
|
restoreSnapshot(snapshot, { mode }, preparer) {
|
|
@@ -535,6 +670,9 @@ class ContentWorkspaceImplementation {
|
|
|
535
670
|
!Array.isArray(snapshot.drafts) ||
|
|
536
671
|
!Array.isArray(snapshot.installations))
|
|
537
672
|
throw contentError('WORKSPACE_SNAPSHOT_INVALID');
|
|
673
|
+
const builtin120EnvironmentId = snapshot.builtin120EnvironmentId === undefined
|
|
674
|
+
? this.builtin120EnvironmentId
|
|
675
|
+
: checkId(snapshot.builtin120EnvironmentId, builtin120IdPattern);
|
|
538
676
|
if ((await this.#listDrafts()).length)
|
|
539
677
|
throw contentError('WORKSPACE_NOT_EMPTY');
|
|
540
678
|
const drafts = new Map(snapshot.drafts.map(draft => {
|
|
@@ -558,7 +696,10 @@ class ContentWorkspaceImplementation {
|
|
|
558
696
|
const manifest = JSON.parse(manifestText);
|
|
559
697
|
const lock = await readJson(join(staging, 'package-lock.json'));
|
|
560
698
|
await writeFile(join(staging, 'package.json'), json({ ...manifest, dependencies: lock.packages['']?.dependencies ?? {} }));
|
|
561
|
-
const
|
|
699
|
+
const applied = normalizeEnvironmentDocument(entry.version.document);
|
|
700
|
+
if (applied.id !== id)
|
|
701
|
+
throw contentError('WORKSPACE_SNAPSHOT_INVALID');
|
|
702
|
+
const result = await preparer({ directory: staging, draft: applied });
|
|
562
703
|
await inspectNpmProject(staging);
|
|
563
704
|
await writeFile(join(staging, 'package.json'), manifestText);
|
|
564
705
|
await writeFile(join(staging, '.arcadia-prepared.json'), json({
|
|
@@ -566,6 +707,10 @@ class ContentWorkspaceImplementation {
|
|
|
566
707
|
format: 'arcadia.environment-installation/1',
|
|
567
708
|
contentRevision: result.contentRevision,
|
|
568
709
|
packages: result.packages ?? [],
|
|
710
|
+
ruleComposition: result.ruleComposition,
|
|
711
|
+
skillParameters: result.skillParameters,
|
|
712
|
+
contentCatalog: result.contentCatalog,
|
|
713
|
+
elementCatalog: result.elementCatalog,
|
|
569
714
|
}));
|
|
570
715
|
await rename(staging, this.#directory(id));
|
|
571
716
|
}
|
|
@@ -585,13 +730,14 @@ class ContentWorkspaceImplementation {
|
|
|
585
730
|
throw contentError('WORKSPACE_SNAPSHOT_INVALID');
|
|
586
731
|
}
|
|
587
732
|
this.workspaceId = mode === 'restore' ? checkId(snapshot.workspaceId, uuid) : this.workspaceId;
|
|
588
|
-
|
|
733
|
+
this.builtin120EnvironmentId = builtin120EnvironmentId;
|
|
734
|
+
await this.#saveMetadata();
|
|
589
735
|
return { workspaceId: this.workspaceId, installationIds: Object.fromEntries([...ids].map(id => [id, id])) };
|
|
590
736
|
}, true);
|
|
591
737
|
}
|
|
592
738
|
importEnvironment(bundle, input, preparer) {
|
|
593
739
|
return this.#exclusive(async () => {
|
|
594
|
-
if (
|
|
740
|
+
if (!['arcadia.environment-export/2', 'arcadia.environment-export/3', 'arcadia.environment-export/4'].includes(bundle?.format))
|
|
595
741
|
throw contentError('ENVIRONMENT_EXPORT_INVALID');
|
|
596
742
|
const document = normalizeEnvironmentDocument({
|
|
597
743
|
...bundle.document,
|
|
@@ -607,8 +753,30 @@ class ContentWorkspaceImplementation {
|
|
|
607
753
|
const staging = join(this.root, 'staging', `import-${randomUUID()}`);
|
|
608
754
|
await mkdir(staging);
|
|
609
755
|
try {
|
|
610
|
-
|
|
611
|
-
|
|
756
|
+
const files = bundle.format !== 'arcadia.environment-export/2'
|
|
757
|
+
? await expandSharedMaterials(bundle.files, bundle.archives, {
|
|
758
|
+
...this.#options,
|
|
759
|
+
signal: input.signal
|
|
760
|
+
? AbortSignal.any([input.signal, this.#controller.signal])
|
|
761
|
+
: this.#controller.signal,
|
|
762
|
+
offline: this.#options.allowShareDownloads !== true || input.allowDownload === false,
|
|
763
|
+
environments: join(this.root, 'environments'),
|
|
764
|
+
supplied: bundle.suppliedArchives,
|
|
765
|
+
cache: this.#options.npmCache ?? join(this.root, 'npm-cache'),
|
|
766
|
+
})
|
|
767
|
+
: bundle.files;
|
|
768
|
+
await writeMaterials(staging, files, { ...this.#options, signal: input.signal });
|
|
769
|
+
// The recipient owns the captured local archives, including registered ZIP sources.
|
|
770
|
+
const manifest = await readJson(join(staging, 'package.json'));
|
|
771
|
+
const dependencies = manifest.dependencies;
|
|
772
|
+
for (const name of [...document.sources]) {
|
|
773
|
+
const spec = isRecord(dependencies) ? dependencies[name] : undefined;
|
|
774
|
+
if (typeof spec === 'string' && /^file:candidates\/[a-zA-Z0-9_.-]+\.tgz$/.test(spec)) {
|
|
775
|
+
document.packages[name] = spec;
|
|
776
|
+
document.sources = document.sources.filter(source => source !== name);
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
const result = await this.#install(document, staging, preparer, { ...input, importing: true });
|
|
612
780
|
return result;
|
|
613
781
|
}
|
|
614
782
|
finally {
|
|
@@ -808,3 +976,30 @@ async function changeEnvironmentPackage(workspace, input) {
|
|
|
808
976
|
}
|
|
809
977
|
return workspace.saveDraft({ document: normalizeEnvironmentDocument(document), expectedRevision });
|
|
810
978
|
}
|
|
979
|
+
/** Materialize the shipped preset with the host's own ID and bundled base project. */
|
|
980
|
+
export async function prepareBuiltin120Environment(workspace, projectDirectory, rules, validate) {
|
|
981
|
+
const id = workspace.builtin120EnvironmentId;
|
|
982
|
+
if (!id)
|
|
983
|
+
throw contentError('BUILTIN_ENVIRONMENT_ID_MISSING');
|
|
984
|
+
const base = '@arcadia-eternity/data-pack-base';
|
|
985
|
+
const { version } = await readJson(join(projectDirectory, 'node_modules', base, 'package.json'));
|
|
986
|
+
const document = normalizeEnvironmentDocument({
|
|
987
|
+
schemaVersion: 2,
|
|
988
|
+
id,
|
|
989
|
+
name: '120 资质 · 神魔化与二代',
|
|
990
|
+
packages: { [base]: version },
|
|
991
|
+
sources: [],
|
|
992
|
+
rules,
|
|
993
|
+
requires: {},
|
|
994
|
+
});
|
|
995
|
+
const current = await workspace.getDraft(id).catch(error => {
|
|
996
|
+
if (errorCode(error) !== 'ENOENT')
|
|
997
|
+
throw error;
|
|
998
|
+
});
|
|
999
|
+
if (current && isDeepStrictEqual(current.document, document)) {
|
|
1000
|
+
const [installed] = await workspace.listInstallations(id);
|
|
1001
|
+
if (installed?.status === 'ready' && installed.draftRevision === current.revision)
|
|
1002
|
+
return;
|
|
1003
|
+
}
|
|
1004
|
+
await workspace.prepareEnvironment({ environmentId: id, document, expectedRevision: current?.revision ?? null, projectDirectory }, validate);
|
|
1005
|
+
}
|