@arcadia-eternity/content-sdk 4.3.3 → 4.4.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 +3 -3
- package/bin/arcadia-content-mcp.mjs +2 -0
- package/bin/arcadia-content.mjs +2 -0
- package/dist/asset-inspect.d.ts +30 -0
- package/dist/asset-inspect.js +84 -0
- package/dist/author-api.d.ts +48 -0
- package/dist/author-api.js +128 -0
- package/dist/author-reference.d.ts +61 -0
- package/dist/author-reference.js +130 -0
- package/dist/build-content.d.ts +2 -0
- package/dist/build-content.js +29 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +81 -0
- package/dist/content-query.d.ts +30 -0
- package/dist/content-query.js +326 -0
- package/dist/content-workspace.d.ts +204 -0
- package/dist/content-workspace.js +981 -0
- package/dist/debug-environments.d.ts +96 -0
- package/dist/debug-environments.js +135 -0
- package/dist/demo.d.ts +1 -0
- package/dist/demo.js +18 -0
- package/dist/effect-build.d.ts +3 -0
- package/dist/effect-build.js +44 -0
- package/dist/environment-rules.d.ts +7 -0
- package/dist/environment-rules.js +94 -0
- package/dist/environment.d.ts +48 -0
- package/dist/environment.js +184 -0
- package/dist/job-journal.d.ts +19 -0
- package/dist/job-journal.js +110 -0
- package/dist/jobs.d.ts +103 -0
- package/dist/jobs.js +626 -0
- package/dist/lib/content.d.ts +24 -0
- package/dist/lib/content.js +106 -0
- package/dist/lib/presentation.d.ts +26 -0
- package/dist/lib/presentation.js +252 -0
- package/dist/lib/yaml-sharing.d.ts +16 -0
- package/dist/lib/yaml-sharing.js +130 -0
- package/dist/managed-projects.d.ts +37 -0
- package/dist/managed-projects.js +305 -0
- package/dist/managed-sources.d.ts +28 -0
- package/dist/managed-sources.js +541 -0
- package/dist/mcp-contracts.d.ts +420 -0
- package/dist/mcp-contracts.js +146 -0
- package/dist/mcp.d.ts +2 -0
- package/dist/mcp.js +228 -0
- package/dist/module-path.d.ts +1 -0
- package/dist/module-path.js +8 -0
- package/dist/pack-project.d.ts +10 -0
- package/dist/pack-project.js +178 -0
- package/dist/preview-control.d.ts +17 -0
- package/dist/preview-control.js +273 -0
- package/dist/preview.d.ts +24 -0
- package/dist/preview.js +332 -0
- package/dist/project-teams.d.ts +51 -0
- package/dist/project-teams.js +106 -0
- package/dist/project.d.ts +18 -0
- package/dist/project.js +146 -0
- package/dist/rename.d.ts +2 -0
- package/dist/rename.js +128 -0
- package/dist/setup.d.ts +2 -0
- package/dist/setup.js +117 -0
- package/dist/source-lock.d.ts +9 -0
- package/dist/source-lock.js +117 -0
- package/dist/template/README.md +2 -0
- package/dist/template/docs/presentation.md +3 -3
- package/dist/template/package.json +3 -7
- package/dist/template/test/pack.test.mjs +10 -7
- package/dist/testing.d.ts +58 -0
- package/dist/testing.js +205 -0
- package/dist/types.d.ts +5 -0
- package/dist/types.js +14 -0
- package/dist/workbench-context.d.ts +10 -0
- package/dist/workbench-context.js +27 -0
- package/dist/workspace-cli.d.ts +1 -0
- package/dist/workspace-cli.js +66 -0
- package/dist/workspace-materials.d.ts +19 -0
- package/dist/workspace-materials.js +165 -0
- package/dist/workspace.d.ts +190 -0
- package/dist/workspace.js +726 -0
- package/package.json +34 -27
- package/dist/asset-inspect.mjs +0 -67
- package/dist/author-api.mjs +0 -112
- package/dist/author-reference.mjs +0 -121
- package/dist/build-content.mjs +0 -30
- package/dist/cli.mjs +0 -76
- package/dist/content-query.mjs +0 -283
- package/dist/content-workspace.d.mts +0 -161
- package/dist/content-workspace.mjs +0 -936
- package/dist/debug-environments.mjs +0 -154
- package/dist/demo.mjs +0 -22
- package/dist/effect-build.d.mts +0 -2
- package/dist/effect-build.mjs +0 -46
- package/dist/environment-rules.d.mts +0 -5
- package/dist/environment-rules.mjs +0 -92
- package/dist/environment.mjs +0 -183
- package/dist/job-journal.mjs +0 -94
- package/dist/jobs.mjs +0 -579
- package/dist/lib/content.mjs +0 -95
- package/dist/lib/presentation.mjs +0 -240
- package/dist/lib/yaml-sharing.mjs +0 -101
- package/dist/managed-projects.d.mts +0 -35
- package/dist/managed-projects.mjs +0 -303
- package/dist/managed-sources.d.mts +0 -19
- package/dist/managed-sources.mjs +0 -503
- package/dist/mcp-contracts.mjs +0 -153
- package/dist/mcp.mjs +0 -369
- package/dist/pack-project.mjs +0 -157
- package/dist/preview-control.mjs +0 -260
- package/dist/preview.mjs +0 -313
- package/dist/project-teams.mjs +0 -98
- package/dist/project.mjs +0 -140
- package/dist/rename.mjs +0 -112
- package/dist/setup.mjs +0 -134
- package/dist/source-lock.mjs +0 -103
- package/dist/testing.mjs +0 -204
- package/dist/workbench-context.mjs +0 -18
- package/dist/workspace-cli.mjs +0 -60
- package/dist/workspace-materials.mjs +0 -153
- package/dist/workspace.mjs +0 -684
- /package/dist/{index.d.mts → index.d.ts} +0 -0
- /package/dist/{index.mjs → index.js} +0 -0
package/dist/mcp.js
ADDED
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
3
|
+
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
4
|
+
import { z } from 'zod';
|
|
5
|
+
import { openAuthorSession } from './author-api.js';
|
|
6
|
+
import { diagnostic } from './workspace.js';
|
|
7
|
+
import { identifier, version, record, outputSchemas, jobKind, jobState, referenceTopics } from './mcp-contracts.js';
|
|
8
|
+
import { TeamDocumentSchema } from './project-teams.js';
|
|
9
|
+
import { EnvironmentDocumentSchema } from './debug-environments.js';
|
|
10
|
+
import { isRecord } from './types.js';
|
|
11
|
+
const [directory, ...flags] = process.argv.slice(2);
|
|
12
|
+
let contextFile;
|
|
13
|
+
const options = new Set();
|
|
14
|
+
for (let index = 0; index < flags.length; index++) {
|
|
15
|
+
const flag = flags[index];
|
|
16
|
+
if (flag === '--context-file' && flags[index + 1] && !flags[index + 1].startsWith('--'))
|
|
17
|
+
contextFile = flags[++index];
|
|
18
|
+
else if (['--execute', '--read-only', '--published'].includes(flag))
|
|
19
|
+
options.add(flag);
|
|
20
|
+
else
|
|
21
|
+
options.add('invalid');
|
|
22
|
+
}
|
|
23
|
+
if (!directory || options.has('invalid')) {
|
|
24
|
+
process.stderr.write('Usage: arcadia-content-mcp <project-directory> [--execute] [--read-only] [--context-file <file>] [--published]\n');
|
|
25
|
+
process.exit(1);
|
|
26
|
+
}
|
|
27
|
+
const session = await openAuthorSession(directory, {
|
|
28
|
+
execute: options.has('--execute'),
|
|
29
|
+
readOnly: options.has('--read-only'),
|
|
30
|
+
contextFile,
|
|
31
|
+
published: options.has('--published'),
|
|
32
|
+
});
|
|
33
|
+
const server = new McpServer({ name: 'arcadia-content-authoring', version: '0.1.0' });
|
|
34
|
+
function tool(name, description, inputSchema, operation, transform = input => input, readOnly = false) {
|
|
35
|
+
server.registerTool(name, {
|
|
36
|
+
description,
|
|
37
|
+
inputSchema,
|
|
38
|
+
outputSchema: { result: outputSchemas[operation] },
|
|
39
|
+
annotations: { readOnlyHint: readOnly, ...(readOnly ? { destructiveHint: false, idempotentHint: true } : {}) },
|
|
40
|
+
}, async (input) => {
|
|
41
|
+
try {
|
|
42
|
+
const value = await session.call(operation, transform(input));
|
|
43
|
+
let result = value;
|
|
44
|
+
let image;
|
|
45
|
+
if (operation === 'asset-inspect' && isRecord(value)) {
|
|
46
|
+
const { image: inspectedImage, ...metadata } = value;
|
|
47
|
+
result = metadata;
|
|
48
|
+
if (isRecord(inspectedImage) &&
|
|
49
|
+
inspectedImage.type === 'image' &&
|
|
50
|
+
typeof inspectedImage.mimeType === 'string' &&
|
|
51
|
+
typeof inspectedImage.data === 'string')
|
|
52
|
+
image = { type: 'image', mimeType: inspectedImage.mimeType, data: inspectedImage.data };
|
|
53
|
+
}
|
|
54
|
+
return {
|
|
55
|
+
...(operation === 'save' && isRecord(result) && !result.complete ? { isError: true } : {}),
|
|
56
|
+
content: [
|
|
57
|
+
{ type: 'text', text: JSON.stringify({ result }) },
|
|
58
|
+
...(image ? [image] : []),
|
|
59
|
+
],
|
|
60
|
+
structuredContent: { result },
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
catch (error) {
|
|
64
|
+
return { isError: true, content: [{ type: 'text', text: JSON.stringify({ error: diagnostic(error) }) }] };
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
tool('workspace_describe', 'Read the bound project identity, source revision, SDK and execution capabilities without importing project code.', {}, 'describe', undefined, true);
|
|
69
|
+
tool('workspace_context', 'Read live desktop context: current view, focused object/resource, language, unsaved drafts and external conflicts. No selection is required. Context is not an edit scope or authorization; follow the user request and query related content as needed. Draft values are unsaved and are not source revisions. Disconnected/expired context must not be treated as current. Re-read source versions before writes; external edits are reconciled by the desktop.', {}, 'context', undefined, true);
|
|
70
|
+
tool('content_query', 'Find content by stable ID or localized name. Defaults to concise project summaries; detail:full includes records for editing. Locales paginate individual entries. Dependency/all scope reads declared installed content without executing code; dependencies are read-only and their file paths belong to that dependency, not the bound project. Use file/version/index from full project rows for saves, never a translated name as identity.', {
|
|
71
|
+
kind: z.enum(['content', 'presentations', 'resources', 'assets', 'files', 'locales', 'diagnostics']).optional(),
|
|
72
|
+
search: z.string().max(200).optional(),
|
|
73
|
+
id: z.string().max(200).optional(),
|
|
74
|
+
contentKind: z.enum(['species', 'skills', 'marks', 'behaviors', 'presentations', 'resources']).optional(),
|
|
75
|
+
locale: z.string().max(80).optional(),
|
|
76
|
+
namespace: z.string().max(200).optional(),
|
|
77
|
+
packageName: z.string().max(200).optional(),
|
|
78
|
+
scope: z.enum(['project', 'dependencies', 'all']).optional(),
|
|
79
|
+
detail: z.enum(['summary', 'full']).optional(),
|
|
80
|
+
offset: z.number().int().min(0).optional(),
|
|
81
|
+
limit: z.number().int().min(1).max(100).optional(),
|
|
82
|
+
}, 'query', undefined, true);
|
|
83
|
+
tool('source_read', 'Read one project source file and its current version. Generated directories are excluded except in read-only published-package sessions.', { file: z.string().max(1000) }, 'read', undefined, true);
|
|
84
|
+
if (!session.workspace.readOnly)
|
|
85
|
+
tool('source_apply', 'Save one source file only if its version still matches. Null expects a new file. Re-read on conflict; no automatic merge.', {
|
|
86
|
+
file: z.string().max(1000),
|
|
87
|
+
expectedVersion: z
|
|
88
|
+
.string()
|
|
89
|
+
.regex(/^[a-f0-9]{64}$/)
|
|
90
|
+
.nullable(),
|
|
91
|
+
text: z.string().max(2 * 1024 * 1024),
|
|
92
|
+
}, 'write');
|
|
93
|
+
if (!session.workspace.readOnly)
|
|
94
|
+
tool('content_apply', 'Save reviewed records or name/description entries. create-record uses a new data file and expectedVersion:null; new locale files also use null. Query sources first. Keeps YAML sharing read-only, checks versions and groups edits per file. Inspect complete and every result; partial writes are not rolled back.', {
|
|
95
|
+
changes: z
|
|
96
|
+
.array(z.object({
|
|
97
|
+
kind: z.enum(['record', 'locale', 'create-record']),
|
|
98
|
+
file: z.string().max(1000),
|
|
99
|
+
expectedVersion: z
|
|
100
|
+
.string()
|
|
101
|
+
.regex(/^[a-f0-9]{64}$/)
|
|
102
|
+
.nullable(),
|
|
103
|
+
index: z.number().int().min(0).nullable().optional(),
|
|
104
|
+
id: z.string().optional(),
|
|
105
|
+
record: z.record(z.string(), z.unknown()),
|
|
106
|
+
}))
|
|
107
|
+
.min(1)
|
|
108
|
+
.max(1000),
|
|
109
|
+
}, 'save');
|
|
110
|
+
for (const [name, kind] of [
|
|
111
|
+
['check_start', 'check'],
|
|
112
|
+
['test_start', 'test'],
|
|
113
|
+
['preview_start', 'preview'],
|
|
114
|
+
['package_build', 'pack'],
|
|
115
|
+
]) {
|
|
116
|
+
if (session.workspace.readOnly)
|
|
117
|
+
continue;
|
|
118
|
+
if (kind !== 'check' && !options.has('--execute'))
|
|
119
|
+
continue;
|
|
120
|
+
tool(name, kind === 'preview'
|
|
121
|
+
? 'Build and install a captured npm candidate, then keep a local Node battle host ready for the full client. Poll job_get for preview.url or preview.clientPath. job_cancel stops it; new candidates do not replace existing battles.'
|
|
122
|
+
: `Start ${kind}; poll job_get for actual output and stale-source state.`, {
|
|
123
|
+
...(kind === 'preview'
|
|
124
|
+
? {
|
|
125
|
+
environmentId: z
|
|
126
|
+
.string()
|
|
127
|
+
.regex(/^[a-z0-9][a-z0-9_-]{0,79}$/)
|
|
128
|
+
.optional(),
|
|
129
|
+
}
|
|
130
|
+
: {}),
|
|
131
|
+
sourceRevision: z
|
|
132
|
+
.string()
|
|
133
|
+
.regex(/^[a-f0-9]{64}$/)
|
|
134
|
+
.optional(),
|
|
135
|
+
}, 'start', input => ({ ...input, kind }));
|
|
136
|
+
}
|
|
137
|
+
tool('job_list', 'Discover current and retained project jobs across author sessions. Compact results omit logs. Reading never selects a task or opens a window.', {
|
|
138
|
+
offset: z.number().int().min(0).optional(),
|
|
139
|
+
limit: z.number().int().min(1).max(100).optional(),
|
|
140
|
+
state: jobState.optional(),
|
|
141
|
+
kind: jobKind.optional(),
|
|
142
|
+
}, 'jobs-query', undefined, true);
|
|
143
|
+
tool('job_get', 'Read task status, phase, diagnostics and candidate metadata. Logs are omitted by default. waitMs waits briefly for a change; afterCursor is the previously observed outputCursor. includeOutput returns only a bounded log chunk.', {
|
|
144
|
+
jobId: z.string(),
|
|
145
|
+
waitMs: z.number().int().min(0).max(10000).optional(),
|
|
146
|
+
afterCursor: z.number().int().min(0).optional(),
|
|
147
|
+
includeOutput: z.boolean().optional(),
|
|
148
|
+
limit: z.number().int().min(1).max(65536).optional(),
|
|
149
|
+
}, 'job-query', undefined, true);
|
|
150
|
+
tool('job_output', 'Read an incremental log chunk using UTF-16 code-unit cursors. Pass nextCursor on the next call. truncated reports a lost log prefix; hasMore means more retained output is available.', {
|
|
151
|
+
jobId: z.string(),
|
|
152
|
+
cursor: z.number().int().min(0).optional(),
|
|
153
|
+
limit: z.number().int().min(1).max(65536).optional(),
|
|
154
|
+
}, 'job-output', undefined, true);
|
|
155
|
+
tool('job_cancel', 'Cancel a task and terminate its child process group; source files are retained.', { jobId: z.string() }, 'cancel-query');
|
|
156
|
+
tool('team_list', 'Read saved project test teams and versions. These are not the user’s unsaved team drafts or current battle players.', {}, 'team-list', undefined, true);
|
|
157
|
+
tool('environment_list', 'Read saved named battle environments and versions. Reading or saving an environment does not select it in the desktop.', {}, 'environment-list', undefined, true);
|
|
158
|
+
if (!session.workspace.readOnly) {
|
|
159
|
+
tool('team_save', 'Save a project team against its file version; null creates a new team. Does not overwrite desktop drafts, change selections or replace a running match. Conflicting user drafts remain available for reconciliation.', {
|
|
160
|
+
document: TeamDocumentSchema,
|
|
161
|
+
expectedVersion: version.nullable(),
|
|
162
|
+
}, 'team-save');
|
|
163
|
+
tool('team_remove', 'Delete a saved project team only if its version matches. Existing battles and desktop drafts keep their copied values; automatic tests referencing this team will fail until updated.', {
|
|
164
|
+
id: identifier,
|
|
165
|
+
expectedVersion: version,
|
|
166
|
+
}, 'team-remove');
|
|
167
|
+
tool('environment_save', 'Save a named environment against its version; null creates it. Does not install dependencies or change the user’s selected environment. Prepare a new preview to use the changed configuration.', {
|
|
168
|
+
document: EnvironmentDocumentSchema,
|
|
169
|
+
expectedVersion: version.nullable(),
|
|
170
|
+
}, 'environment-save');
|
|
171
|
+
}
|
|
172
|
+
tool('author_reference', 'Read the actual SDK schemas or focused authoring guidance. Schema notes distinguish normalized runtime definitions from source references. Description guidance covers current supported semantics, not future generic reference protocols.', {
|
|
173
|
+
topic: z.enum(referenceTopics),
|
|
174
|
+
}, 'reference', undefined, true);
|
|
175
|
+
tool('asset_inspect', 'Inspect one declared project resource by ID. Local images up to 2 MiB may be returned as standard image content when includeImage is true. MIME follows the filename; decoding/display is handled by the client. No dimensions or image integrity validation are provided. HTTP resources return metadata only and are never fetched or cached. Audio/SWF are not executed.', {
|
|
176
|
+
id: z.string().min(1).max(200),
|
|
177
|
+
includeImage: z.boolean().optional(),
|
|
178
|
+
}, 'asset-inspect', undefined, true);
|
|
179
|
+
if (!session.workspace.readOnly && options.has('--execute')) {
|
|
180
|
+
const battle = (name, description, schema, operation, readOnly = false) => tool(name, description, { jobId: z.string(), ...schema }, 'battle', input => ({ ...input, operation }), readOnly);
|
|
181
|
+
const identity = { sessionId: z.string() };
|
|
182
|
+
const mutation = { ...identity, expectedRevision: z.number().int().min(0) };
|
|
183
|
+
battle('battle_create', 'Create an isolated automation battle on an existing preview candidate using the latest saved team files and a fixed seed. Team-only edits do not require rebuilding the candidate. Never operates on the human preview battle or unsaved team drafts. At most four automation battles per preview.', {
|
|
184
|
+
teamIds: z.tuple([identifier, identifier]),
|
|
185
|
+
seed: z.string().min(1).max(200),
|
|
186
|
+
}, 'create');
|
|
187
|
+
battle('battle_read', 'Read an automation battle state, revision and available selections. Use returned selections for actions; this does not read or change the human preview battle.', identity, 'read', true);
|
|
188
|
+
battle('battle_events', 'Read bounded automation battle events after a cursor. Expired cursors are explicit errors; read state again to recover.', {
|
|
189
|
+
...identity,
|
|
190
|
+
afterCursor: z.number().int().min(0).optional(),
|
|
191
|
+
limit: z.number().int().min(1).max(100).optional(),
|
|
192
|
+
}, 'events', true);
|
|
193
|
+
battle('battle_action', 'Submit one legal selection to an automation battle at the observed revision. Concurrent/stale actions are rejected. The user’s manual match is unaffected.', {
|
|
194
|
+
...mutation,
|
|
195
|
+
selection: record,
|
|
196
|
+
}, 'action');
|
|
197
|
+
battle('battle_reset', 'Reset only the named automation battle, checking its revision. Omit teams/seed to reuse its team IDs and seed; reset reads their latest saved files. Never resets the human match.', {
|
|
198
|
+
...mutation,
|
|
199
|
+
teamIds: z.tuple([identifier, identifier]).optional(),
|
|
200
|
+
seed: z.string().min(1).max(200).optional(),
|
|
201
|
+
}, 'reset');
|
|
202
|
+
battle('battle_close', 'Release one isolated automation battle after checking its revision. Keeps the preview host and human match running.', mutation, 'close');
|
|
203
|
+
}
|
|
204
|
+
server.registerResource('workspace', 'arcadia://workspace/summary', { mimeType: 'application/json' }, async (uri) => ({
|
|
205
|
+
contents: [{ uri: uri.href, mimeType: 'application/json', text: JSON.stringify(await session.describe()) }],
|
|
206
|
+
}));
|
|
207
|
+
for (const topic of referenceTopics)
|
|
208
|
+
server.registerResource(`reference-${topic}`, `arcadia://authoring/${topic}`, { mimeType: 'application/json' }, async (uri) => ({
|
|
209
|
+
contents: [
|
|
210
|
+
{
|
|
211
|
+
uri: uri.href,
|
|
212
|
+
mimeType: 'application/json',
|
|
213
|
+
text: JSON.stringify(await session.call('reference', { topic })),
|
|
214
|
+
},
|
|
215
|
+
],
|
|
216
|
+
}));
|
|
217
|
+
let closing = false;
|
|
218
|
+
async function close() {
|
|
219
|
+
if (closing)
|
|
220
|
+
return;
|
|
221
|
+
closing = true;
|
|
222
|
+
await session.close();
|
|
223
|
+
await server.close();
|
|
224
|
+
}
|
|
225
|
+
process.once('SIGINT', close);
|
|
226
|
+
process.once('SIGTERM', close);
|
|
227
|
+
process.stdin.once('end', close);
|
|
228
|
+
await server.connect(new StdioServerTransport());
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function siblingModulePath(name: string): string;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { fileURLToPath } from 'node:url';
|
|
2
|
+
const extension = import.meta.url.endsWith('.ts') ? '.ts' : '.js';
|
|
3
|
+
function siblingModuleUrl(name) {
|
|
4
|
+
return new URL(`./${name}${extension}`, import.meta.url).href;
|
|
5
|
+
}
|
|
6
|
+
export function siblingModulePath(name) {
|
|
7
|
+
return fileURLToPath(siblingModuleUrl(name));
|
|
8
|
+
}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { execFile } from 'node:child_process';
|
|
2
|
+
import { statSync } from 'node:fs';
|
|
3
|
+
import { copyFile, lstat, mkdir, mkdtemp, readFile, realpath, rename, rm, writeFile } from 'node:fs/promises';
|
|
4
|
+
import { basename, dirname, isAbsolute, join, relative, resolve } from 'node:path';
|
|
5
|
+
import { promisify } from 'node:util';
|
|
6
|
+
import { compileEffectModule, resolveNpmInvocation } from '@arcadia-eternity/pack-loader/node';
|
|
7
|
+
import { errorCode } from './types.js';
|
|
8
|
+
const exec = promisify(execFile);
|
|
9
|
+
const artifactFile = 'dist/arcadia-effects.json';
|
|
10
|
+
const distributionMetadata = /^(?:package\.json|readme(?:\..+)?|licen[cs]e(?:\..+)?|notice(?:\..+)?)$/i;
|
|
11
|
+
function fail(message) {
|
|
12
|
+
throw new Error(message);
|
|
13
|
+
}
|
|
14
|
+
function packagePath(file) {
|
|
15
|
+
if (typeof file !== 'string' ||
|
|
16
|
+
!file ||
|
|
17
|
+
file.includes('\\') ||
|
|
18
|
+
file.includes('\0') ||
|
|
19
|
+
isAbsolute(file) ||
|
|
20
|
+
file.split('/').some(part => !part || ['.', '..', '.arcadia', '.git', 'node_modules'].includes(part)))
|
|
21
|
+
fail('npm returned an unsafe package path');
|
|
22
|
+
return file;
|
|
23
|
+
}
|
|
24
|
+
async function npm(root, args, cache) {
|
|
25
|
+
const invocation = await resolveNpmInvocation([...args, ...(cache ? ['--cache', resolve(cache)] : [])]);
|
|
26
|
+
return exec(invocation.file, invocation.args, { cwd: root, maxBuffer: 32 * 1024 * 1024, timeout: 120000 });
|
|
27
|
+
}
|
|
28
|
+
async function sourceFile(root, file) {
|
|
29
|
+
const path = join(root, packagePath(file)), canonical = await realpath(path), info = await lstat(path);
|
|
30
|
+
const location = relative(root, canonical);
|
|
31
|
+
if (!info.isFile() ||
|
|
32
|
+
info.isSymbolicLink() ||
|
|
33
|
+
isAbsolute(location) ||
|
|
34
|
+
location === '..' ||
|
|
35
|
+
location.startsWith('../') ||
|
|
36
|
+
location.startsWith('..\\'))
|
|
37
|
+
fail(`Package source must be a regular file inside the project: ${file}`);
|
|
38
|
+
return { path, canonical, info };
|
|
39
|
+
}
|
|
40
|
+
const sameFile = (before, after) => before.size === after.size &&
|
|
41
|
+
before.mtimeMs === after.mtimeMs &&
|
|
42
|
+
before.ctimeMs === after.ctimeMs &&
|
|
43
|
+
before.ino === after.ino;
|
|
44
|
+
async function verifyCapture(sources) {
|
|
45
|
+
for (const source of sources.values()) {
|
|
46
|
+
const after = await lstat(source.path);
|
|
47
|
+
if (!sameFile(source.info, after) || (await realpath(source.path)) !== source.canonical)
|
|
48
|
+
fail(`Package source changed during capture: ${source.path}`);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
async function stagingRoot(root) {
|
|
52
|
+
let directory = root;
|
|
53
|
+
for (const part of ['.arcadia', 'package-staging']) {
|
|
54
|
+
directory = join(directory, part);
|
|
55
|
+
await mkdir(directory, { recursive: true, mode: 0o700 });
|
|
56
|
+
const info = await lstat(directory);
|
|
57
|
+
if (!info.isDirectory() || info.isSymbolicLink() || (await realpath(directory)) !== directory)
|
|
58
|
+
fail('Package staging must be a regular directory inside the author workspace');
|
|
59
|
+
}
|
|
60
|
+
return directory;
|
|
61
|
+
}
|
|
62
|
+
export async function packBuiltProject(directory = process.cwd(), { cache } = {}) {
|
|
63
|
+
const root = await realpath(resolve(directory));
|
|
64
|
+
const manifestText = await readFile(join(root, 'package.json'), 'utf8');
|
|
65
|
+
const manifest = JSON.parse(manifestText);
|
|
66
|
+
const preview = JSON.parse(String((await npm(root, ['pack', '--dry-run', '--ignore-scripts', '--json'], cache)).stdout))[0];
|
|
67
|
+
if (!Array.isArray(preview?.files) || preview.files.length > 50000)
|
|
68
|
+
fail('npm returned an invalid package file list');
|
|
69
|
+
const included = new Set(preview.files.map((file) => packagePath(file.path)));
|
|
70
|
+
if (!included.has('package.json'))
|
|
71
|
+
fail('npm omitted the package manifest');
|
|
72
|
+
let presentation;
|
|
73
|
+
try {
|
|
74
|
+
presentation = JSON.parse(await readFile(join(root, 'dist/presentation.json'), 'utf8'));
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
if (errorCode(error) !== 'ENOENT')
|
|
78
|
+
throw error;
|
|
79
|
+
}
|
|
80
|
+
const required = [];
|
|
81
|
+
const declaredAssets = manifest.arcadia?.assets && typeof manifest.arcadia.assets === 'object' && !Array.isArray(manifest.arcadia.assets)
|
|
82
|
+
? Object.values(manifest.arcadia.assets)
|
|
83
|
+
: [];
|
|
84
|
+
for (const file of [
|
|
85
|
+
manifest.arcadia?.content,
|
|
86
|
+
manifest.arcadia?.presentation,
|
|
87
|
+
manifest.arcadia?.catalog,
|
|
88
|
+
...declaredAssets,
|
|
89
|
+
])
|
|
90
|
+
if (typeof file === 'string')
|
|
91
|
+
required.push(packagePath(file));
|
|
92
|
+
if (manifest.arcadia?.effectArtifact && !manifest.arcadia?.effects)
|
|
93
|
+
required.push(packagePath(manifest.arcadia.effectArtifact));
|
|
94
|
+
if (presentation &&
|
|
95
|
+
(presentation.bindings?.length || presentation.resources?.length || Object.keys(presentation.locales ?? {}).length)) {
|
|
96
|
+
if (manifest.arcadia?.presentation !== 'dist/presentation.json')
|
|
97
|
+
fail('Set arcadia.presentation to dist/presentation.json before packing media/locales');
|
|
98
|
+
required.push('dist/content.json', 'dist/presentation.json', ...(presentation.resources ?? [])
|
|
99
|
+
.filter((resource) => resource.kind === 'file' && typeof resource.source === 'string')
|
|
100
|
+
.map(resource => resource.source));
|
|
101
|
+
}
|
|
102
|
+
for (const file of required)
|
|
103
|
+
if (!included.has(file))
|
|
104
|
+
fail(`npm files omits required presentation file: ${file}`);
|
|
105
|
+
const distributed = new Set(['package.json']);
|
|
106
|
+
for (const file of included)
|
|
107
|
+
if (file.startsWith('dist/') || distributionMetadata.test(file))
|
|
108
|
+
distributed.add(file);
|
|
109
|
+
for (const file of required)
|
|
110
|
+
distributed.add(file);
|
|
111
|
+
// Keep output on the destination filesystem so publishing can be one rename.
|
|
112
|
+
const temporary = await mkdtemp(join(await stagingRoot(root), 'publish-'));
|
|
113
|
+
const payload = join(temporary, 'package'), output = join(temporary, 'output');
|
|
114
|
+
const sources = new Map();
|
|
115
|
+
try {
|
|
116
|
+
await mkdir(payload);
|
|
117
|
+
await mkdir(output);
|
|
118
|
+
const staged = structuredClone(manifest);
|
|
119
|
+
for (const file of distributed) {
|
|
120
|
+
const source = await sourceFile(root, file);
|
|
121
|
+
sources.set(source.path, source);
|
|
122
|
+
await mkdir(dirname(join(payload, file)), { recursive: true });
|
|
123
|
+
await copyFile(source.path, join(payload, file));
|
|
124
|
+
const after = await lstat(source.path);
|
|
125
|
+
if (!sameFile(source.info, after))
|
|
126
|
+
fail(`Package source changed during capture: ${file}`);
|
|
127
|
+
}
|
|
128
|
+
if (typeof manifest.arcadia?.effects === 'string') {
|
|
129
|
+
const effects = await sourceFile(root, manifest.arcadia.effects);
|
|
130
|
+
const artifact = await compileEffectModule(effects.path, root, manifest.name, path => {
|
|
131
|
+
// The fixed compiler calls this before reading each validated dependency.
|
|
132
|
+
const info = statSync(path), previous = sources.get(path);
|
|
133
|
+
if (previous && !sameFile(previous.info, info))
|
|
134
|
+
fail(`Effect source changed during capture: ${path}`);
|
|
135
|
+
if (!previous)
|
|
136
|
+
sources.set(path, { path, canonical: path, info });
|
|
137
|
+
});
|
|
138
|
+
await verifyCapture(sources);
|
|
139
|
+
await mkdir(dirname(join(payload, artifactFile)), { recursive: true });
|
|
140
|
+
await writeFile(join(payload, artifactFile), JSON.stringify(artifact) + '\n');
|
|
141
|
+
const stagedArcadia = staged.arcadia;
|
|
142
|
+
if (!stagedArcadia)
|
|
143
|
+
fail('Package manifest is missing arcadia metadata');
|
|
144
|
+
stagedArcadia.effectArtifact = artifactFile;
|
|
145
|
+
distributed.add(artifactFile);
|
|
146
|
+
}
|
|
147
|
+
else if (typeof manifest.arcadia?.effectArtifact === 'string' && !included.has(manifest.arcadia.effectArtifact)) {
|
|
148
|
+
fail(`npm files omits required effect artifact: ${manifest.arcadia.effectArtifact}`);
|
|
149
|
+
}
|
|
150
|
+
if (staged.arcadia) {
|
|
151
|
+
delete staged.arcadia.effectSources;
|
|
152
|
+
delete staged.arcadia.effects;
|
|
153
|
+
}
|
|
154
|
+
staged.files = [...distributed];
|
|
155
|
+
await writeFile(join(payload, 'package.json'), JSON.stringify(staged, null, 2) + '\n');
|
|
156
|
+
const packed = JSON.parse(String((await npm(payload, ['pack', '--ignore-scripts', '--json', '--pack-destination', output], cache)).stdout))[0];
|
|
157
|
+
if (!packed || typeof packed.filename !== 'string' || !Array.isArray(packed.files))
|
|
158
|
+
fail('npm returned an invalid package receipt');
|
|
159
|
+
packagePath(packed.filename);
|
|
160
|
+
if (basename(packed.filename) !== packed.filename)
|
|
161
|
+
fail('npm returned an unsafe archive filename');
|
|
162
|
+
const actual = new Set(packed.files.map((file) => file.path));
|
|
163
|
+
for (const file of distributed)
|
|
164
|
+
if (!actual.has(file))
|
|
165
|
+
fail(`npm omitted staged package file: ${file}`);
|
|
166
|
+
for (const file of actual)
|
|
167
|
+
if (!distributed.has(file) && !distributionMetadata.test(file))
|
|
168
|
+
fail(`npm included a non-artifact file: ${file}`);
|
|
169
|
+
if ((await readFile(join(root, 'package.json'), 'utf8')) !== manifestText)
|
|
170
|
+
fail('Package manifest changed during packaging');
|
|
171
|
+
await verifyCapture(sources);
|
|
172
|
+
await rename(join(output, packed.filename), join(root, packed.filename));
|
|
173
|
+
return packed;
|
|
174
|
+
}
|
|
175
|
+
finally {
|
|
176
|
+
await rm(temporary, { recursive: true, force: true });
|
|
177
|
+
}
|
|
178
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type ProjectTeam } from './project-teams.js';
|
|
2
|
+
import type { prepareContentPack } from './testing.js';
|
|
3
|
+
type PreviewPrepared = Awaited<ReturnType<typeof prepareContentPack>>;
|
|
4
|
+
/** Independent battles over the captured candidate. Never receives the human preview's host or drafts. */
|
|
5
|
+
export declare class PreviewAutomation {
|
|
6
|
+
#private;
|
|
7
|
+
prepared: PreviewPrepared;
|
|
8
|
+
readTeams: () => Promise<ProjectTeam[]>;
|
|
9
|
+
constructor(prepared: PreviewPrepared, { readTeams }?: {
|
|
10
|
+
readTeams?: () => Promise<ProjectTeam[]>;
|
|
11
|
+
});
|
|
12
|
+
call(operation: string, input?: unknown): Promise<unknown>;
|
|
13
|
+
close(): Promise<void>;
|
|
14
|
+
}
|
|
15
|
+
/** The caller resolves endpoint from its bound project's preview receipt; no arbitrary host or method is accepted. */
|
|
16
|
+
export declare function callPreviewControl(endpoint: string, operation: string, input?: unknown): Promise<unknown>;
|
|
17
|
+
export {};
|