@arcadia-eternity/content-sdk 4.13.0 → 4.15.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 +16 -5
- package/dist/appimage-sdk.d.ts +2 -0
- package/dist/appimage-sdk.js +110 -0
- package/dist/author-api.d.ts +6 -1
- package/dist/author-api.js +27 -3
- package/dist/author-environment-install.d.ts +14 -0
- package/dist/author-environment-install.js +104 -0
- package/dist/author-environment-source.d.ts +162 -0
- package/dist/author-environment-source.js +226 -0
- package/dist/author-install.d.ts +22 -0
- package/dist/author-install.js +289 -0
- package/dist/author-reference.d.ts +1 -1
- package/dist/author-reference.js +23 -4
- package/dist/cli.js +56 -23
- package/dist/command-discovery.d.ts +3 -0
- package/dist/command-discovery.js +122 -0
- package/dist/content-query.js +4 -1
- package/dist/content-workspace.d.ts +2 -138
- package/dist/content-workspace.js +6 -6
- package/dist/debug-environment-install.d.ts +15 -0
- package/dist/debug-environment-install.js +130 -0
- package/dist/debug-environments.d.ts +135 -0
- package/dist/debug-environments.js +48 -0
- package/dist/demo.js +22 -8
- package/dist/desktop-cli.d.ts +3 -0
- package/dist/desktop-cli.js +72 -0
- package/dist/desktop-client.d.ts +71 -0
- package/dist/desktop-client.js +162 -0
- package/dist/environment-demo.d.ts +8 -0
- package/dist/environment-demo.js +52 -0
- package/dist/environment-rules.js +38 -19
- package/dist/environment.d.ts +12 -8
- package/dist/environment.js +107 -58
- package/dist/init.d.ts +1 -0
- package/dist/init.js +125 -0
- package/dist/jobs.d.ts +3 -1
- package/dist/jobs.js +34 -15
- package/dist/lib/presentation.js +12 -2
- package/dist/managed-projects.d.ts +5 -1
- package/dist/managed-projects.js +3 -3
- package/dist/mcp-contracts.d.ts +28 -1
- package/dist/mcp-contracts.js +24 -1
- package/dist/mcp.js +21 -7
- package/dist/official-environment-packages.d.ts +6 -0
- package/dist/official-environment-packages.js +19 -0
- package/dist/official-environments.d.ts +7 -0
- package/dist/official-environments.js +25 -0
- package/dist/pack-project.d.ts +1 -0
- package/dist/pack-project.js +66 -0
- package/dist/package-updates.d.ts +2 -1
- package/dist/package-updates.js +30 -17
- package/dist/prepared-document.d.ts +1 -0
- package/dist/prepared-document.js +5 -13
- package/dist/preview-control.js +24 -1
- package/dist/preview.d.ts +1 -0
- package/dist/preview.js +6 -4
- package/dist/project.d.ts +1 -0
- package/dist/project.js +57 -18
- package/dist/template/README.md +8 -8
- package/dist/template/package.json +2 -2
- package/dist/template/skills/arcadia-content-authoring/SKILL.md +15 -2
- package/dist/template/skills/arcadia-content-authoring/references/tool-workflows.md +25 -1
- package/dist/testing.d.ts +3 -4
- package/dist/testing.js +22 -8
- package/dist/workspace-cli.js +6 -4
- package/dist/workspace.d.ts +1 -0
- package/dist/workspace.js +66 -15
- package/package.json +10 -5
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { constants } from 'node:fs';
|
|
3
|
+
import { mkdir, open, rm, writeFile } from 'node:fs/promises';
|
|
4
|
+
import { createRequire } from 'node:module';
|
|
5
|
+
import { dirname, isAbsolute, join } from 'node:path';
|
|
6
|
+
import { Readable } from 'node:stream';
|
|
7
|
+
import { isDeepStrictEqual } from 'node:util';
|
|
8
|
+
import { satisfies, valid, validRange } from 'semver';
|
|
9
|
+
import YAML from 'yaml';
|
|
10
|
+
import { environmentManifestDocument } from './debug-environments.js';
|
|
11
|
+
import { assertEnvironmentRequirements, validateEnvironmentRuleDeclaration } from './environment-rules.js';
|
|
12
|
+
import { matchesArchiveIntegrity, tarFiles } from './managed-sources.js';
|
|
13
|
+
import { teamDocument } from './project-teams.js';
|
|
14
|
+
import { isRecord } from './types.js';
|
|
15
|
+
import { contentError } from './workspace-materials.js';
|
|
16
|
+
const npmName = /^(?:@[a-z0-9_.-]+\/)?[a-z0-9][a-z0-9_.-]*$/;
|
|
17
|
+
const candidate = /^file:candidates\/[a-zA-Z0-9_.-]+\.tgz$/;
|
|
18
|
+
const maxBytes = 256 * 1024 * 1024;
|
|
19
|
+
const sdkVersion = createRequire(import.meta.url)('../package.json').version;
|
|
20
|
+
function exactPackage(name, version) {
|
|
21
|
+
if (typeof name !== 'string' ||
|
|
22
|
+
name.length > 214 ||
|
|
23
|
+
!npmName.test(name) ||
|
|
24
|
+
typeof version !== 'string' ||
|
|
25
|
+
!/^\d/.test(version) ||
|
|
26
|
+
version.trim() !== version ||
|
|
27
|
+
!valid(version))
|
|
28
|
+
throw contentError('ENVIRONMENT_PACKAGE_ID_INVALID', '环境包需要 npm 包名与完整版本号');
|
|
29
|
+
return { kind: 'npm', name, version };
|
|
30
|
+
}
|
|
31
|
+
export function parseAuthorEnvironmentNpmSpec(spec) {
|
|
32
|
+
const index = typeof spec === 'string' ? spec.lastIndexOf('@') : -1;
|
|
33
|
+
if (index < 1)
|
|
34
|
+
throw contentError('ENVIRONMENT_PACKAGE_ID_INVALID', '请指定带完整版本的 npm 环境包');
|
|
35
|
+
return exactPackage(spec.slice(0, index), spec.slice(index + 1));
|
|
36
|
+
}
|
|
37
|
+
export function parseAuthorEnvironmentSource(value) {
|
|
38
|
+
if (isRecord(value)) {
|
|
39
|
+
if (value.kind === 'npm')
|
|
40
|
+
return exactPackage(value.name, value.version);
|
|
41
|
+
if (value.kind === 'archive' &&
|
|
42
|
+
typeof value.file === 'string' &&
|
|
43
|
+
value.file.length > 0 &&
|
|
44
|
+
/\.tgz$/i.test(value.file))
|
|
45
|
+
return { kind: 'archive', file: value.file };
|
|
46
|
+
}
|
|
47
|
+
throw contentError('ENVIRONMENT_PACKAGE_SOURCE_INVALID', '请选择 npm 环境包及完整版本,或环境包 .tgz 文件');
|
|
48
|
+
}
|
|
49
|
+
async function readArchive(stream, signal) {
|
|
50
|
+
const chunks = [];
|
|
51
|
+
let size = 0;
|
|
52
|
+
for await (const chunk of stream) {
|
|
53
|
+
signal.throwIfAborted();
|
|
54
|
+
size += chunk.length;
|
|
55
|
+
if (size > maxBytes)
|
|
56
|
+
throw contentError('SOURCE_LIMIT');
|
|
57
|
+
chunks.push(chunk);
|
|
58
|
+
}
|
|
59
|
+
return Buffer.concat(chunks, size);
|
|
60
|
+
}
|
|
61
|
+
async function archiveBytes(source, options, signal) {
|
|
62
|
+
if (source.kind === 'archive') {
|
|
63
|
+
if (!isAbsolute(source.file))
|
|
64
|
+
throw contentError('SOURCE_PATH_INVALID');
|
|
65
|
+
const file = await open(source.file, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
|
|
66
|
+
try {
|
|
67
|
+
const info = await file.stat();
|
|
68
|
+
if (!info.isFile() || info.size > maxBytes)
|
|
69
|
+
throw contentError('SOURCE_ARCHIVE_INVALID');
|
|
70
|
+
return await readArchive(file.createReadStream({ autoClose: false, signal }), signal);
|
|
71
|
+
}
|
|
72
|
+
finally {
|
|
73
|
+
await file.close();
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
const { tarball } = (await import('pacote')).default;
|
|
77
|
+
const cache = options.cache ?? process.env.npm_config_cache;
|
|
78
|
+
return tarball.stream(source.name + '@' + source.version, stream => readArchive(stream, signal), {
|
|
79
|
+
...(cache && { cache: join(cache, '_cacache') }),
|
|
80
|
+
offline: options.offline ?? process.env.npm_config_offline === 'true',
|
|
81
|
+
preferOffline: true,
|
|
82
|
+
retry: { retries: 0 },
|
|
83
|
+
signal,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
function archiveLock(bytes, packages, files, retained) {
|
|
87
|
+
const lock = JSON.parse(bytes.toString());
|
|
88
|
+
if (!isRecord(lock) ||
|
|
89
|
+
![2, 3].includes(Number(lock.lockfileVersion)) ||
|
|
90
|
+
!isRecord(lock.packages) ||
|
|
91
|
+
!isRecord(lock.packages['']) ||
|
|
92
|
+
!isDeepStrictEqual(lock.packages[''].dependencies ?? {}, packages))
|
|
93
|
+
throw contentError('INSTALL_LOCK_INVALID');
|
|
94
|
+
for (const [name, spec] of Object.entries(packages)) {
|
|
95
|
+
const pin = lock.packages['node_modules/' + name];
|
|
96
|
+
if (!isRecord(pin) ||
|
|
97
|
+
typeof pin.version !== 'string' ||
|
|
98
|
+
(candidate.test(spec) ? pin.resolved !== spec : !satisfies(pin.version, spec)))
|
|
99
|
+
throw contentError('INSTALL_LOCK_INVALID');
|
|
100
|
+
}
|
|
101
|
+
for (const [location, entry] of Object.entries(lock.packages)) {
|
|
102
|
+
if (!location)
|
|
103
|
+
continue;
|
|
104
|
+
if (!/^(?:node_modules\/(?:@[a-z0-9_.-]+\/)?[a-z0-9][a-z0-9_.-]*\/)*node_modules\/(?:@[a-z0-9_.-]+\/)?[a-z0-9][a-z0-9_.-]*$/.test(location) ||
|
|
105
|
+
!isRecord(entry) ||
|
|
106
|
+
entry.link ||
|
|
107
|
+
typeof entry.resolved !== 'string' ||
|
|
108
|
+
typeof entry.integrity !== 'string')
|
|
109
|
+
throw contentError('INSTALL_LOCK_INVALID');
|
|
110
|
+
if (candidate.test(entry.resolved)) {
|
|
111
|
+
const bytes = retainCandidate(entry.resolved, files, retained);
|
|
112
|
+
if (!matchesArchiveIntegrity(bytes, entry.integrity))
|
|
113
|
+
throw contentError('SOURCE_INTEGRITY_MISMATCH');
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
const url = URL.parse(entry.resolved);
|
|
117
|
+
if (!url || url.protocol !== 'https:' || url.username || url.password)
|
|
118
|
+
throw contentError('INSTALL_LOCK_INVALID');
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
function retainCandidate(spec, files, retained) {
|
|
123
|
+
const path = spec.slice(5);
|
|
124
|
+
const bytes = files.get('package/' + path);
|
|
125
|
+
if (!bytes)
|
|
126
|
+
throw contentError('ENVIRONMENT_SOURCE_MISSING', '环境包缺少依赖归档 ' + path);
|
|
127
|
+
retained.set(path, bytes);
|
|
128
|
+
return bytes;
|
|
129
|
+
}
|
|
130
|
+
/** Capture the exact archive once. Only data fields and portable project inputs reach the new directory. */
|
|
131
|
+
export async function acquireAuthorEnvironment(source, destination, options = {}) {
|
|
132
|
+
source = parseAuthorEnvironmentSource(source);
|
|
133
|
+
if (!isAbsolute(destination))
|
|
134
|
+
throw contentError('SOURCE_PATH_INVALID');
|
|
135
|
+
const signal = AbortSignal.any([AbortSignal.timeout(120_000), ...(options.signal ? [options.signal] : [])]);
|
|
136
|
+
signal.throwIfAborted();
|
|
137
|
+
const bytes = await archiveBytes(source, options, signal);
|
|
138
|
+
if (bytes.length < 2 || bytes.readUInt16LE(0) !== 0x8b1f)
|
|
139
|
+
throw contentError('SOURCE_ARCHIVE_INVALID');
|
|
140
|
+
const entries = await tarFiles(Readable.from([bytes]), {
|
|
141
|
+
signal,
|
|
142
|
+
timeoutMs: 120_000,
|
|
143
|
+
maxFiles: 50000,
|
|
144
|
+
maxBytes,
|
|
145
|
+
rejectPrivateFiles: true,
|
|
146
|
+
});
|
|
147
|
+
if (entries.some(entry => !entry.name.startsWith('package/')))
|
|
148
|
+
throw contentError('ENVIRONMENT_PACKAGE_INVALID');
|
|
149
|
+
const files = new Map(entries.map(entry => [entry.name, entry.bytes]));
|
|
150
|
+
const raw = files.get('package/package.json');
|
|
151
|
+
if (!raw || raw.length > 1024 * 1024)
|
|
152
|
+
throw contentError('ENVIRONMENT_PACKAGE_INVALID');
|
|
153
|
+
const manifest = JSON.parse(raw.toString());
|
|
154
|
+
if (!isRecord(manifest) || manifest.arcadia || !isRecord(manifest.arcadiaEnvironment))
|
|
155
|
+
throw contentError('ENVIRONMENT_PACKAGE_REQUIRED', '请选择带有 arcadiaEnvironment 声明的环境包');
|
|
156
|
+
const { name, version } = exactPackage(manifest.name, manifest.version);
|
|
157
|
+
if (source.kind === 'npm' && (name !== source.name || version !== source.version))
|
|
158
|
+
throw contentError('SOURCE_PACKAGE_IDENTITY_MISMATCH');
|
|
159
|
+
const document = environmentManifestDocument(manifest);
|
|
160
|
+
if (document.sources.length)
|
|
161
|
+
throw contentError('ENVIRONMENT_SOURCE_NOT_PORTABLE', '请将本机来源打包为可安装的依赖');
|
|
162
|
+
assertEnvironmentRequirements(document.requires);
|
|
163
|
+
validateEnvironmentRuleDeclaration(document.rules);
|
|
164
|
+
const { packages: dependencies, ...arcadiaEnvironment } = document;
|
|
165
|
+
const range = isRecord(manifest.engines) ? manifest.engines.arcadia : undefined;
|
|
166
|
+
if (range !== undefined &&
|
|
167
|
+
(typeof range !== 'string' || !validRange(range) || !satisfies(options.clientVersion ?? sdkVersion, range)))
|
|
168
|
+
throw contentError('ARCADIA_VERSION_INCOMPATIBLE');
|
|
169
|
+
const retained = new Map();
|
|
170
|
+
for (const [dep, spec] of Object.entries(dependencies)) {
|
|
171
|
+
if (candidate.test(spec))
|
|
172
|
+
retainCandidate(spec, files, retained);
|
|
173
|
+
else if (!validRange(spec))
|
|
174
|
+
throw contentError('INSTALL_VERSION_INVALID', '无效的依赖版本范围 ' + dep);
|
|
175
|
+
}
|
|
176
|
+
const lock = files.get('package/npm-shrinkwrap.json') ?? files.get('package/package-lock.json');
|
|
177
|
+
if (lock) {
|
|
178
|
+
if (lock.length > 16 * 1024 * 1024)
|
|
179
|
+
throw contentError('SOURCE_LIMIT');
|
|
180
|
+
archiveLock(lock, dependencies, files, retained);
|
|
181
|
+
retained.set('package-lock.json', lock);
|
|
182
|
+
}
|
|
183
|
+
for (const [key, data] of files) {
|
|
184
|
+
if (/^package\/LICENSE(?:\.(?:md|txt))?$/i.test(key))
|
|
185
|
+
retained.set(key.slice('package/'.length), data);
|
|
186
|
+
if (!/^package\/debug\/teams\/[^/]+\.yaml$/.test(key))
|
|
187
|
+
continue;
|
|
188
|
+
if (data.length > 1024 * 1024)
|
|
189
|
+
throw contentError('SOURCE_LIMIT');
|
|
190
|
+
const team = teamDocument(YAML.parse(data.toString()));
|
|
191
|
+
const path = 'debug/teams/' + team.id + '.yaml';
|
|
192
|
+
if (key !== 'package/' + path)
|
|
193
|
+
throw contentError('TEAM_INVALID');
|
|
194
|
+
retained.set(path, data);
|
|
195
|
+
}
|
|
196
|
+
const recipe = {
|
|
197
|
+
name,
|
|
198
|
+
version,
|
|
199
|
+
private: true,
|
|
200
|
+
...(typeof manifest.license === 'string' && { license: manifest.license }),
|
|
201
|
+
...(typeof manifest.description === 'string' && { description: manifest.description }),
|
|
202
|
+
...(range !== undefined && { engines: { arcadia: range } }),
|
|
203
|
+
dependencies,
|
|
204
|
+
arcadiaEnvironment,
|
|
205
|
+
};
|
|
206
|
+
retained.set('package.json', Buffer.from(JSON.stringify(recipe, null, 2) + '\n'));
|
|
207
|
+
await mkdir(destination, { mode: 0o700 });
|
|
208
|
+
try {
|
|
209
|
+
for (const [path, data] of retained) {
|
|
210
|
+
signal.throwIfAborted();
|
|
211
|
+
await mkdir(dirname(join(destination, path)), { recursive: true });
|
|
212
|
+
await writeFile(join(destination, path), data, { flag: 'wx', mode: 0o600 });
|
|
213
|
+
}
|
|
214
|
+
return {
|
|
215
|
+
directory: destination,
|
|
216
|
+
name,
|
|
217
|
+
version,
|
|
218
|
+
document,
|
|
219
|
+
integrity: 'sha512-' + createHash('sha512').update(bytes).digest('base64'),
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
catch (error) {
|
|
223
|
+
await rm(destination, { recursive: true, force: true });
|
|
224
|
+
throw error;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { type AuthorWorkspace } from './workspace.js';
|
|
2
|
+
export interface AuthorInstallation {
|
|
3
|
+
directory: string;
|
|
4
|
+
manifestVersion: string;
|
|
5
|
+
installationDigest: string;
|
|
6
|
+
packages: Array<{
|
|
7
|
+
name: string;
|
|
8
|
+
version: string;
|
|
9
|
+
}>;
|
|
10
|
+
}
|
|
11
|
+
/** Resolves only the immutable consumer matching the project's current dependency declarations. */
|
|
12
|
+
export declare function findAuthorInstallation(root: string, input: unknown, materialRoot?: string): Promise<AuthorInstallation | undefined>;
|
|
13
|
+
/** Stage the locked dependencies alongside a newly packed author candidate. */
|
|
14
|
+
export declare function stageInstalledDependencies(receipt: AuthorInstallation, declared: unknown, installation: string, dependencies: Record<string, string>, sources: string[], environmentLock?: string): Promise<{
|
|
15
|
+
lock: string;
|
|
16
|
+
localOverrides: Record<string, string>;
|
|
17
|
+
}>;
|
|
18
|
+
export declare function installAuthorDependencies(workspace: AuthorWorkspace, { expectedVersion, offline, signal, }: {
|
|
19
|
+
expectedVersion: string;
|
|
20
|
+
offline?: boolean;
|
|
21
|
+
signal?: AbortSignal;
|
|
22
|
+
}): Promise<AuthorInstallation>;
|
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
import { sha256Hex, sha256StreamHex } from '@arcadia-eternity/const';
|
|
2
|
+
import { inspectNpmProject, resolveNpmInvocation } from '@arcadia-eternity/pack-loader/node';
|
|
3
|
+
import { execFile } from 'node:child_process';
|
|
4
|
+
import { constants, createReadStream } from 'node:fs';
|
|
5
|
+
import { copyFile, lstat, mkdir, mkdtemp, readdir, readFile, realpath, rm, writeFile } from 'node:fs/promises';
|
|
6
|
+
import { join } from 'node:path';
|
|
7
|
+
import { promisify } from 'node:util';
|
|
8
|
+
import { validRange } from 'semver';
|
|
9
|
+
import writeFileAtomic from 'write-file-atomic';
|
|
10
|
+
import { z } from 'zod';
|
|
11
|
+
import { stageEnvironmentDependencies } from './debug-environment-install.js';
|
|
12
|
+
import { checkLock, installationEnvironment } from './managed-projects.js';
|
|
13
|
+
import { withSourceLock } from './source-lock.js';
|
|
14
|
+
import { errorCode, isRecord } from './types.js';
|
|
15
|
+
import { WorkspaceError } from './workspace.js';
|
|
16
|
+
import { materialFiles, readJson } from './workspace-materials.js';
|
|
17
|
+
const exec = promisify(execFile);
|
|
18
|
+
const namePattern = /^(?:@[a-z0-9_.-]+\/)?[a-z0-9][a-z0-9_.-]*$/;
|
|
19
|
+
const localSpec = /^file:candidates\/[a-zA-Z0-9_.-]+\.tgz$/;
|
|
20
|
+
const hex = z.string().regex(/^[a-f0-9]{64}$/);
|
|
21
|
+
const receiptSchema = z.object({
|
|
22
|
+
generation: z.string().regex(/^install-[a-zA-Z0-9]{6}$/),
|
|
23
|
+
declarations: hex,
|
|
24
|
+
lockVersion: hex.nullable(),
|
|
25
|
+
archives: z.record(z.string().regex(localSpec), hex),
|
|
26
|
+
manifestVersion: hex,
|
|
27
|
+
installationDigest: z.string(),
|
|
28
|
+
packages: z.array(z.object({ name: z.string(), version: z.string() })),
|
|
29
|
+
});
|
|
30
|
+
function dependenciesOf(input) {
|
|
31
|
+
if (input === undefined)
|
|
32
|
+
return {};
|
|
33
|
+
if (!isRecord(input) || Object.keys(input).length > 200)
|
|
34
|
+
throw new WorkspaceError('INSTALL_DEPENDENCY_INVALID', 'dependencies 需要有效的依赖声明', 'package.json');
|
|
35
|
+
const result = {};
|
|
36
|
+
for (const [name, spec] of Object.entries(input).sort(([a], [b]) => a.localeCompare(b))) {
|
|
37
|
+
if (!namePattern.test(name) ||
|
|
38
|
+
name.length > 214 ||
|
|
39
|
+
name === 'node_modules' ||
|
|
40
|
+
typeof spec !== 'string' ||
|
|
41
|
+
!spec.trim() ||
|
|
42
|
+
spec.length > 256 ||
|
|
43
|
+
(!validRange(spec) && !localSpec.test(spec)))
|
|
44
|
+
throw new WorkspaceError('INSTALL_DEPENDENCY_INVALID', `不支持的内容依赖:${name}`, 'package.json');
|
|
45
|
+
result[name] = spec;
|
|
46
|
+
}
|
|
47
|
+
return result;
|
|
48
|
+
}
|
|
49
|
+
async function canonicalDirectory(path, create = false) {
|
|
50
|
+
if (create)
|
|
51
|
+
await mkdir(path, { mode: 0o700 }).catch(error => {
|
|
52
|
+
if (errorCode(error) !== 'EEXIST')
|
|
53
|
+
throw error;
|
|
54
|
+
});
|
|
55
|
+
if ((await realpath(path)) !== path || !(await lstat(path)).isDirectory())
|
|
56
|
+
throw new WorkspaceError('MATERIAL_PATH_ESCAPE', '安装目录必须是项目内的普通目录');
|
|
57
|
+
return path;
|
|
58
|
+
}
|
|
59
|
+
async function storeDirectory(root, create = false) {
|
|
60
|
+
const state = await canonicalDirectory(join(root, '.arcadia'), create);
|
|
61
|
+
return canonicalDirectory(join(state, 'dependencies'), create);
|
|
62
|
+
}
|
|
63
|
+
async function projectLock(root) {
|
|
64
|
+
const path = join(root, 'package-lock.json');
|
|
65
|
+
try {
|
|
66
|
+
const info = await lstat(path);
|
|
67
|
+
if (!info.isFile() || info.size > 16 * 1024 * 1024)
|
|
68
|
+
throw new WorkspaceError('INSTALL_LOCK_INVALID', '锁文件无效');
|
|
69
|
+
return await readFile(path, 'utf8');
|
|
70
|
+
}
|
|
71
|
+
catch (error) {
|
|
72
|
+
if (errorCode(error) !== 'ENOENT')
|
|
73
|
+
throw error;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
async function archiveVersions(root, specs) {
|
|
77
|
+
const versions = {};
|
|
78
|
+
for (const spec of new Set(specs)) {
|
|
79
|
+
if (!localSpec.test(spec))
|
|
80
|
+
continue;
|
|
81
|
+
const path = join(root, spec.slice(5));
|
|
82
|
+
try {
|
|
83
|
+
const info = await lstat(path);
|
|
84
|
+
if (!info.isFile() || (await realpath(path)) !== path)
|
|
85
|
+
throw new WorkspaceError('MATERIAL_PATH_ESCAPE', '本地归档必须是 candidates 下的普通文件');
|
|
86
|
+
if (info.size > 256 * 1024 * 1024)
|
|
87
|
+
throw new WorkspaceError('MATERIAL_LIMIT', '本地候选归档过大');
|
|
88
|
+
versions[spec] = await sha256StreamHex(createReadStream(path));
|
|
89
|
+
}
|
|
90
|
+
catch (error) {
|
|
91
|
+
if (errorCode(error) !== 'ENOENT')
|
|
92
|
+
throw error;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return versions;
|
|
96
|
+
}
|
|
97
|
+
function result(store, receipt) {
|
|
98
|
+
return {
|
|
99
|
+
directory: join(store, receipt.generation),
|
|
100
|
+
manifestVersion: receipt.manifestVersion,
|
|
101
|
+
installationDigest: receipt.installationDigest,
|
|
102
|
+
packages: receipt.packages,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
/** Resolves only the immutable consumer matching the project's current dependency declarations. */
|
|
106
|
+
export async function findAuthorInstallation(root, input, materialRoot = root) {
|
|
107
|
+
root = await realpath(root);
|
|
108
|
+
materialRoot = await realpath(materialRoot);
|
|
109
|
+
try {
|
|
110
|
+
const store = await storeDirectory(root);
|
|
111
|
+
const receipt = receiptSchema.parse(await readJson(join(store, 'active.json')));
|
|
112
|
+
const dependencies = dependenciesOf(input);
|
|
113
|
+
if (receipt.declarations !== sha256Hex(JSON.stringify(dependencies)))
|
|
114
|
+
return;
|
|
115
|
+
const lock = await projectLock(materialRoot);
|
|
116
|
+
if (receipt.lockVersion !== (lock === undefined ? null : sha256Hex(lock)))
|
|
117
|
+
return;
|
|
118
|
+
const archives = await archiveVersions(materialRoot, Object.keys(receipt.archives));
|
|
119
|
+
if (Object.entries(archives).some(([spec, version]) => receipt.archives[spec] !== version))
|
|
120
|
+
return;
|
|
121
|
+
const installed = result(store, receipt);
|
|
122
|
+
await canonicalDirectory(installed.directory);
|
|
123
|
+
await canonicalDirectory(join(installed.directory, 'node_modules'));
|
|
124
|
+
await canonicalDirectory(join(store, 'npm-cache'));
|
|
125
|
+
return installed;
|
|
126
|
+
}
|
|
127
|
+
catch (error) {
|
|
128
|
+
if (errorCode(error) !== 'ENOENT')
|
|
129
|
+
throw error;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
/** Stage the locked dependencies alongside a newly packed author candidate. */
|
|
133
|
+
export async function stageInstalledDependencies(receipt, declared, installation, dependencies, sources, environmentLock) {
|
|
134
|
+
const manifest = await readJson(join(receipt.directory, 'package.json'));
|
|
135
|
+
const installed = dependenciesOf(manifest.dependencies);
|
|
136
|
+
const requested = dependenciesOf(declared);
|
|
137
|
+
// npm resolves file: dependencies relative to the package declaring them. Reference
|
|
138
|
+
// the consumer's direct dependency so an author archive can reuse the installed bytes.
|
|
139
|
+
const localOverrides = {};
|
|
140
|
+
for (const [name, spec] of Object.entries(installed)) {
|
|
141
|
+
if (localSpec.test(spec))
|
|
142
|
+
localOverrides[name] = '$' + name;
|
|
143
|
+
if (!sources.includes(name) && (!Object.hasOwn(dependencies, name) || dependencies[name] === requested[name]))
|
|
144
|
+
dependencies[name] = spec;
|
|
145
|
+
}
|
|
146
|
+
const candidates = await canonicalDirectory(join(receipt.directory, 'candidates'));
|
|
147
|
+
for (const entry of await readdir(candidates, { withFileTypes: true })) {
|
|
148
|
+
if (!entry.isFile() || !localSpec.test('file:candidates/' + entry.name))
|
|
149
|
+
throw new WorkspaceError('MATERIAL_PATH_ESCAPE', '已安装候选归档无效');
|
|
150
|
+
const target = join(installation, 'candidates', entry.name);
|
|
151
|
+
const source = join(candidates, entry.name);
|
|
152
|
+
try {
|
|
153
|
+
await copyFile(source, target, constants.COPYFILE_EXCL);
|
|
154
|
+
}
|
|
155
|
+
catch (error) {
|
|
156
|
+
if (errorCode(error) !== 'EEXIST' ||
|
|
157
|
+
(await sha256StreamHex(createReadStream(source))) !== (await sha256StreamHex(createReadStream(target))))
|
|
158
|
+
throw error;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
const lock = await projectLock(receipt.directory);
|
|
162
|
+
if (!lock)
|
|
163
|
+
throw new WorkspaceError('INSTALL_LOCK_INVALID', '已安装的资料包缺少锁文件');
|
|
164
|
+
if (!environmentLock)
|
|
165
|
+
return { lock, localOverrides };
|
|
166
|
+
const previous = JSON.parse(environmentLock);
|
|
167
|
+
const durable = JSON.parse(lock);
|
|
168
|
+
checkLock(previous, true);
|
|
169
|
+
for (const [location, pin] of Object.entries(durable.packages)) {
|
|
170
|
+
if (!location)
|
|
171
|
+
continue;
|
|
172
|
+
const name = location.slice('node_modules/'.length);
|
|
173
|
+
if (!sources.includes(name) && dependencies[name] === installed[name])
|
|
174
|
+
previous.packages[location] = pin;
|
|
175
|
+
}
|
|
176
|
+
return { lock: JSON.stringify(previous), localOverrides };
|
|
177
|
+
}
|
|
178
|
+
export async function installAuthorDependencies(workspace, { expectedVersion, offline = false, signal, }) {
|
|
179
|
+
signal?.throwIfAborted();
|
|
180
|
+
await workspace.assertWritable();
|
|
181
|
+
const source = await workspace.read('package.json');
|
|
182
|
+
const conflict = () => new WorkspaceError('REVISION_CONFLICT', '安装期间依赖声明或材料已改变,请重新读取', 'package.json');
|
|
183
|
+
if (source.version !== expectedVersion)
|
|
184
|
+
throw conflict();
|
|
185
|
+
const dependencies = dependenciesOf(JSON.parse(source.text).dependencies);
|
|
186
|
+
const root = workspace.root;
|
|
187
|
+
const current = await findAuthorInstallation(root, dependencies);
|
|
188
|
+
if (current)
|
|
189
|
+
return { ...current, manifestVersion: source.version };
|
|
190
|
+
const store = await storeDirectory(root, true);
|
|
191
|
+
const directory = await mkdtemp(join(store, 'install-'));
|
|
192
|
+
let committed = false;
|
|
193
|
+
try {
|
|
194
|
+
const lock = await projectLock(root);
|
|
195
|
+
const archiveSpecs = new Set(Object.values(dependencies));
|
|
196
|
+
const lockedPackages = lock && JSON.parse(lock).packages;
|
|
197
|
+
if (isRecord(lockedPackages))
|
|
198
|
+
for (const pin of Object.values(lockedPackages)) {
|
|
199
|
+
if (isRecord(pin) && typeof pin.resolved === 'string' && localSpec.test(pin.resolved))
|
|
200
|
+
archiveSpecs.add(pin.resolved);
|
|
201
|
+
}
|
|
202
|
+
const archives = await archiveVersions(root, archiveSpecs);
|
|
203
|
+
const specs = { ...dependencies };
|
|
204
|
+
await mkdir(join(directory, 'candidates'));
|
|
205
|
+
const { lockText } = await stageEnvironmentDependencies({
|
|
206
|
+
materialRoot: root,
|
|
207
|
+
sourceRoot: root,
|
|
208
|
+
installation: directory,
|
|
209
|
+
dependencies: specs,
|
|
210
|
+
// Use an npm archive for the base so captured dependencies can also be reused when debugging a pack.
|
|
211
|
+
hasSources: true,
|
|
212
|
+
lockText: lock,
|
|
213
|
+
sourceLocked: lock !== undefined,
|
|
214
|
+
});
|
|
215
|
+
const captured = await archiveVersions(directory, Object.keys(archives));
|
|
216
|
+
if (JSON.stringify(captured) !== JSON.stringify(archives))
|
|
217
|
+
throw conflict();
|
|
218
|
+
await writeFile(join(directory, 'package.json'), JSON.stringify({ name: 'arcadia-author-dependencies', version: '1.0.0', private: true, dependencies: specs }));
|
|
219
|
+
if (lockText)
|
|
220
|
+
await writeFile(join(directory, 'package-lock.json'), lockText);
|
|
221
|
+
const cache = await canonicalDirectory(join(store, 'npm-cache'), true);
|
|
222
|
+
const config = join(directory, 'empty.npmrc');
|
|
223
|
+
const env = installationEnvironment(cache, config);
|
|
224
|
+
const timeout = AbortSignal.timeout(120_000);
|
|
225
|
+
const activeSignal = signal ? AbortSignal.any([timeout, signal]) : timeout;
|
|
226
|
+
for (const args of [['install', '--package-lock-only', '--lockfile-version=3'], ['ci']]) {
|
|
227
|
+
const npm = await resolveNpmInvocation([
|
|
228
|
+
...args,
|
|
229
|
+
'--workspaces=false',
|
|
230
|
+
'--ignore-scripts',
|
|
231
|
+
'--no-bin-links',
|
|
232
|
+
'--omit=dev',
|
|
233
|
+
'--no-audit',
|
|
234
|
+
'--no-fund',
|
|
235
|
+
...(offline ? ['--offline'] : []),
|
|
236
|
+
]);
|
|
237
|
+
try {
|
|
238
|
+
await exec(npm.file, npm.args, {
|
|
239
|
+
cwd: directory,
|
|
240
|
+
env,
|
|
241
|
+
signal: activeSignal,
|
|
242
|
+
maxBuffer: 2 * 1024 * 1024,
|
|
243
|
+
timeout: 120_000,
|
|
244
|
+
windowsHide: true,
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
catch (error) {
|
|
248
|
+
activeSignal.throwIfAborted();
|
|
249
|
+
const code = isRecord(error) && typeof error.stderr === 'string'
|
|
250
|
+
? /^npm (?:error|ERR!) code ([A-Z][A-Z0-9_]{1,40})$/m.exec(error.stderr)?.[1]
|
|
251
|
+
: undefined;
|
|
252
|
+
throw new WorkspaceError('NPM_INSTALL_FAILED', `npm ${args[0]} 未能安装声明的资料包${offline ? '(离线模式)' : ''}${code ? ':' + code : ''}`);
|
|
253
|
+
}
|
|
254
|
+
if (args[0] === 'install')
|
|
255
|
+
checkLock(await readJson(join(directory, 'package-lock.json'), 16 * 1024 * 1024), true);
|
|
256
|
+
}
|
|
257
|
+
await mkdir(join(directory, 'node_modules'), { recursive: true });
|
|
258
|
+
await materialFiles(directory, { signal: activeSignal });
|
|
259
|
+
const installed = await inspectNpmProject(directory);
|
|
260
|
+
const receipt = {
|
|
261
|
+
generation: directory.slice(store.length + 1),
|
|
262
|
+
declarations: sha256Hex(JSON.stringify(dependencies)),
|
|
263
|
+
lockVersion: lock === undefined ? null : sha256Hex(lock),
|
|
264
|
+
archives,
|
|
265
|
+
manifestVersion: source.version,
|
|
266
|
+
installationDigest: installed.digest,
|
|
267
|
+
packages: installed.packages.map(({ name, version }) => ({ name, version })),
|
|
268
|
+
};
|
|
269
|
+
return await withSourceLock(root, source.file, async () => {
|
|
270
|
+
await workspace.assertWritable();
|
|
271
|
+
if ((await workspace.read(source.file)).version !== source.version || (await projectLock(root)) !== lock)
|
|
272
|
+
throw conflict();
|
|
273
|
+
const latest = await archiveVersions(root, archiveSpecs);
|
|
274
|
+
if (JSON.stringify(latest) !== JSON.stringify(archives))
|
|
275
|
+
throw conflict();
|
|
276
|
+
const existing = await findAuthorInstallation(root, dependencies);
|
|
277
|
+
if (existing)
|
|
278
|
+
return { ...existing, manifestVersion: source.version };
|
|
279
|
+
activeSignal.throwIfAborted();
|
|
280
|
+
await writeFileAtomic(join(store, 'active.json'), JSON.stringify(receipt), { mode: 0o600 });
|
|
281
|
+
committed = true;
|
|
282
|
+
return result(store, receipt);
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
finally {
|
|
286
|
+
if (!committed)
|
|
287
|
+
await rm(directory, { recursive: true, force: true });
|
|
288
|
+
}
|
|
289
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export declare const referenceTopics: readonly ['species', 'skills', 'marks', 'team', 'environment', 'descriptions', 'effects', 'git', 'release'];
|
|
1
|
+
export declare const referenceTopics: readonly ['authoring', 'species', 'skills', 'marks', 'team', 'environment', 'descriptions', 'effects', 'git', 'release'];
|
|
2
2
|
/** Discover installed SDK contracts, without importing a content project's executable effects. */
|
|
3
3
|
export declare function authorReference({ topic }?: {
|
|
4
4
|
topic?: string;
|
package/dist/author-reference.js
CHANGED
|
@@ -6,6 +6,7 @@ import { EnvironmentDocumentSchema } from './debug-environments.js';
|
|
|
6
6
|
import { WorkspaceError } from './workspace.js';
|
|
7
7
|
import { errorCode } from './types.js';
|
|
8
8
|
export const referenceTopics = [
|
|
9
|
+
'authoring',
|
|
9
10
|
'species',
|
|
10
11
|
'skills',
|
|
11
12
|
'marks',
|
|
@@ -40,6 +41,15 @@ async function template(file) {
|
|
|
40
41
|
export async function authorReference({ topic } = {}) {
|
|
41
42
|
if (!topic || !referenceTopics.includes(topic))
|
|
42
43
|
throw new WorkspaceError('REFERENCE_UNKNOWN', '请选择有效的作者参考主题');
|
|
44
|
+
if (topic === 'authoring') {
|
|
45
|
+
const source = 'skills/arcadia-content-authoring/SKILL.md';
|
|
46
|
+
const instructions = await template(source).catch(error => {
|
|
47
|
+
if (errorCode(error) !== 'ENOENT')
|
|
48
|
+
throw error;
|
|
49
|
+
return readFile(new URL('../../../docs/guides/ugc-authoring-agent/SKILL.md', import.meta.url), 'utf8');
|
|
50
|
+
});
|
|
51
|
+
return { topic, title: 'Create and debug a pack or environment', source, instructions };
|
|
52
|
+
}
|
|
43
53
|
if (topic === 'git' || topic === 'release') {
|
|
44
54
|
const source = `skills/arcadia-content-${topic}/SKILL.md`;
|
|
45
55
|
return {
|
|
@@ -84,20 +94,29 @@ export async function authorReference({ topic } = {}) {
|
|
|
84
94
|
if (topic === 'environment') {
|
|
85
95
|
return {
|
|
86
96
|
topic,
|
|
87
|
-
title: '
|
|
97
|
+
title: 'Standalone and named battle environments',
|
|
88
98
|
source: '@arcadia-eternity/content-sdk/debug-environments',
|
|
89
99
|
stage: 'source',
|
|
90
100
|
schema: toJsonSchemaCompat(EnvironmentDocumentSchema),
|
|
91
101
|
examples: [
|
|
92
102
|
{
|
|
93
103
|
language: 'json',
|
|
94
|
-
text: JSON.stringify({
|
|
104
|
+
text: JSON.stringify({
|
|
105
|
+
schemaVersion: 2,
|
|
106
|
+
id: 'singles-cup',
|
|
107
|
+
name: '单精灵杯',
|
|
108
|
+
packages: {},
|
|
109
|
+
sources: [],
|
|
110
|
+
rules: { schemaVersion: 1, team: { maxSize: 1 } },
|
|
111
|
+
requires: {},
|
|
112
|
+
}),
|
|
95
113
|
},
|
|
96
114
|
],
|
|
97
115
|
notes: [
|
|
98
116
|
'default is built in and cannot be saved as a named environment. Names must be nonblank; source package names must be unique.',
|
|
99
|
-
'
|
|
100
|
-
'
|
|
117
|
+
'In a standalone project, package.json holds arcadiaEnvironment and npm dependencies. environment_list returns the editable document and its version. Save it with environment_save using that version.',
|
|
118
|
+
'In a content pack, environment_save with expectedVersion:null creates a named debug environment. Select its ID in preview_start.',
|
|
119
|
+
'Use registry versions for shared packages. sources contains locally mapped package names. After a rules change, prepare a fresh preview. Pass its jobId and a saved teamId to team_validate. A false valid field includes the rule diagnostic.',
|
|
101
120
|
],
|
|
102
121
|
};
|
|
103
122
|
}
|