@craft_eco/craft-mcp 0.1.14 → 0.1.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (27) hide show
  1. package/README.md +1 -1
  2. package/dist-server/game-workflow/draft-archive.mjs +53 -91
  3. package/dist-server/game-workflow/draft-uploads.mjs +207 -207
  4. package/dist-server/game-workflow/publications.mjs +4 -2
  5. package/dist-server/mcp/control-panel.mjs +59 -40
  6. package/dist-server/mcp/publication-page.html +86 -0
  7. package/dist-server/mcp/publication-page.mjs +347 -0
  8. package/dist-server/mcp/publication-preview.mjs +49 -0
  9. package/dist-server/mcp/publication-tools.mjs +4 -1
  10. package/dist-server/mcp/publish-panel.mjs +191 -0
  11. package/dist-server/mcp/server.mjs +312 -297
  12. package/dist-server/upstream/craft-aigc.mjs +169 -197
  13. package/dist-server/upstream/publication-transport.mjs +2 -1
  14. package/package.json +1 -1
  15. package/resources/craft-game-maker/SKILL.md +69 -67
  16. package/vendor/agent-cli/dist/src/artifacts/external-game-archive.d.ts +19 -0
  17. package/vendor/agent-cli/dist/src/artifacts/external-game-archive.d.ts.map +1 -0
  18. package/vendor/agent-cli/dist/src/artifacts/external-game-archive.js +107 -0
  19. package/vendor/agent-cli/dist/src/artifacts/external-game-archive.js.map +1 -0
  20. package/vendor/agent-cli/dist/src/artifacts/external-game-upload.d.ts +44 -0
  21. package/vendor/agent-cli/dist/src/artifacts/external-game-upload.d.ts.map +1 -0
  22. package/vendor/agent-cli/dist/src/artifacts/external-game-upload.js +106 -0
  23. package/vendor/agent-cli/dist/src/artifacts/external-game-upload.js.map +1 -0
  24. package/vendor/agent-cli/dist/src/index.d.ts +2 -0
  25. package/vendor/agent-cli/dist/src/index.d.ts.map +1 -1
  26. package/vendor/agent-cli/dist/src/index.js +2 -0
  27. package/vendor/agent-cli/dist/src/index.js.map +1 -1
@@ -0,0 +1,191 @@
1
+ import { randomBytes, randomUUID, createHash } from 'node:crypto';
2
+ import path from 'node:path';
3
+ import { mkdir,writeFile,rm,lstat,realpath } from 'node:fs/promises';
4
+ import { privateDirectory } from '../game-workflow/request-store.mjs';
5
+ import { imageExtension } from '../game-workflow/asset-formats.mjs';
6
+ import { publicationMaterials } from '../game-workflow/publication-materials.mjs';
7
+
8
+ const identifier = /^[a-zA-Z0-9_-]{1,128}$/;
9
+ const failure = (message,status=400)=>Object.assign(new Error(message),{ status });
10
+ function bodyFields(body,keys,required=keys) {
11
+ if (!body || typeof body !== 'object' || Array.isArray(body) || Object.keys(body).some(key=>!keys.includes(key)) ||
12
+ required.some(key=>!Object.hasOwn(body,key))) throw failure('Invalid publication request');
13
+ }
14
+ function selectedId(value) {
15
+ if (typeof value !== 'string' || !identifier.test(value)) throw failure('Invalid publication identity');
16
+ return value;
17
+ }
18
+
19
+ // Both the MCP tools and this local page use the host's original workflow and project locks.
20
+ export function createPublishPanel({ platform,projects,tasks,drafts,publications,checkedTask,mutate,viewTask,
21
+ preview:openPreview,coverDirectory,now=Date.now }) {
22
+ const scopes = new Map(); let origin,closed=false,coverRoot;
23
+ const current = scope=>{
24
+ const session = platform.store.getSession();
25
+ return !closed && now()-scope.created < 60*60*1000 && platform.base === scope.platform && session.loggedIn &&
26
+ session.token === scope.token && Number(session.identity?.userId) === scope.owner;
27
+ };
28
+ async function retire(cap,scope) {
29
+ scopes.delete(cap); clearTimeout(scope.expiry);
30
+ if (scope.cover_directory) { try { await rm(scope.cover_directory,{ recursive:true,force:true }); } catch { /* The private stage can be retried by host cleanup. */ } }
31
+ try { await (await scope.preview)?.close(); } catch { /* A stopped local preview is already unavailable. */ }
32
+ }
33
+ async function scopeFor(cap) {
34
+ const scope = scopes.get(cap);
35
+ if (!scope) throw failure('This local publication page is unavailable. Reopen it from Craft.',404);
36
+ if (!current(scope)) {
37
+ await retire(cap,scope);
38
+ throw failure('Craft account or publication page changed. Reopen the page.',403);
39
+ }
40
+ return scope;
41
+ }
42
+ function requireCurrent(scope) {
43
+ if (!current(scope)) throw failure('Craft account or publication page changed. Reopen the page.',403);
44
+ }
45
+ async function publication(scope,id) {
46
+ if (selectedId(id) !== scope.publication_id) throw failure('Publication does not belong to this page',403);
47
+ const result = await publications.status(scope.name,id,scope.owner);
48
+ if (result.check_task_id !== scope.check_task_id) throw failure('Publication does not match this checked build',403);
49
+ requireCurrent(scope); return result;
50
+ }
51
+ async function preview(scope) {
52
+ if (!openPreview) return;
53
+ const pending=scope.preview ??= Promise.resolve().then(()=>openPreview(scope.check));
54
+ let handle;
55
+ let url;
56
+ try {
57
+ handle=await pending;
58
+ url=new URL(handle.url);
59
+ if (url.protocol !== 'http:' || url.hostname !== '127.0.0.1' || url.origin === origin || url.username || url.password || url.hash) {
60
+ throw failure('Publication preview must use an isolated loopback origin');
61
+ }
62
+ } catch {
63
+ if (scope.preview === pending) delete scope.preview;
64
+ try { await handle?.close(); } catch { /* No usable optional preview remains. */ }
65
+ requireCurrent(scope); scope.preview_unavailable=true;
66
+ return undefined;
67
+ }
68
+ // Authorization failures remain errors; retirement owns the valid preview handle.
69
+ requireCurrent(scope); scope.preview_unavailable=false;
70
+ return url.href;
71
+ }
72
+ const unsubscribe = platform.store.onChange?.(()=>{
73
+ for (const [cap,scope] of scopes) if (!current(scope)) void retire(cap,scope);
74
+ });
75
+ return {
76
+ bindOrigin(value) { origin=value; },
77
+ async open(name,checkId,publicationId) {
78
+ if (closed || !origin) throw failure('Local publication panel is unavailable');
79
+ projects.file(name); selectedId(checkId);
80
+ const account=platform.account(),session=platform.store.getSession();
81
+ const resume=Boolean(publicationId) || await drafts.hasRequest(name,checkId,account.user_id);
82
+ const context=await checkedTask(checkId,name,resume);
83
+ for (const [cap,existing] of scopes) if (!current(existing)) await retire(cap,existing);
84
+ if (scopes.size >= 32) throw failure('Close and reconnect Craft to reopen the local publication panel');
85
+ const scope={ name,check_task_id:checkId,check:context.task,project:context.project,owner:account.user_id,
86
+ platform:platform.base,token:session.token,created:now(),tasks:new Set() };
87
+ if (publicationId) {
88
+ const existing=await publications.status(name,selectedId(publicationId),account.user_id);
89
+ if (existing.check_task_id !== checkId) throw failure('Publication does not match this checked build',403);
90
+ scope.publication_id=publicationId;
91
+ }
92
+ requireCurrent(scope);
93
+ const cap=randomBytes(32).toString('base64url'); scopes.set(cap,scope);
94
+ scope.expiry=setTimeout(()=>{void retire(cap,scope);},60*60*1000);scope.expiry.unref();
95
+ return { status:'ready',url:origin+'/publish/'+cap,name,check_task_id:checkId,
96
+ ...(publicationId ? { publication_id:publicationId } : {}),
97
+ next:'Open this local page to review the frozen game, prepare materials and confirm the exact publication revision. Opening the page does not submit review.' };
98
+ },
99
+ async document(cap) {
100
+ const scope=await scopeFor(cap),runtime_url=await preview(scope);
101
+ scope.document_loaded=true;scope.document_preview_origin=runtime_url ? new URL(runtime_url).origin : undefined;
102
+ return { runtime_url,...(scope.preview_unavailable ? { preview_error:'Local frozen preview is unavailable. Reload this page to retry; saved publication status remains available.' } : {}) };
103
+ },
104
+ async handle(cap,action,method,query,body) {
105
+ const scope=await scopeFor(cap);
106
+ const writing=['cover','prepare','review','submit'].includes(action);
107
+ if (method !== (writing ? 'POST' : 'GET')) throw failure('Publication method is not allowed',405);
108
+ const allowedQuery=action === 'task' ? ['task_id'] : action === 'status' ? ['publication_id'] : [];
109
+ if ([...query.keys()].some(key=>!allowedQuery.includes(key)) || allowedQuery.some(key=>query.getAll(key).length !== 1)) throw failure('Invalid publication query');
110
+ if (action === 'state') {
111
+ const options=await platform.publicationSession(scope.owner)('options');
112
+ const draft=await drafts.status(scope.name,scope.check_task_id,scope.owner);
113
+ const saved=scope.publication_id ? await publication(scope,scope.publication_id) : undefined;
114
+ // The document CSP pins its optional frame origin; a failed preview is retried on reload.
115
+ const runtime_url=scope.document_loaded && !scope.document_preview_origin ? undefined : await preview(scope); requireCurrent(scope);
116
+ const account=platform.account();
117
+ return { name:scope.name,check_task_id:scope.check_task_id,engine:scope.check.result.engine,platform:scope.platform,
118
+ account:{ user_id:account.user_id,nickname:account.nickname },draft,options,publication:saved,
119
+ task_id:scope.task_id,runtime_url,preview_unavailable:scope.preview_unavailable === true,
120
+ ...(scope.preview_unavailable ? { preview_error:'Local frozen preview is unavailable. Reload this page to retry; saved publication status remains available.' } : {}) };
121
+ }
122
+ if (action === 'task') {
123
+ const id=selectedId(query.get('task_id'));
124
+ if (!scope.tasks.has(id)) throw failure('Task does not belong to this page',403);
125
+ const result=viewTask(await tasks.get(id)); requireCurrent(scope); return result;
126
+ }
127
+ if (action === 'status') return publication(scope,query.get('publication_id'));
128
+ if (!writing) throw failure('Unknown publication action',404);
129
+ return mutate(scope.name,async()=>{
130
+ requireCurrent(scope);
131
+ if (action === 'cover') {
132
+ bodyFields(body,['data']);
133
+ if (typeof body.data !== 'string' || body.data.length > Math.ceil(2*1024*1024/3)*4 ||
134
+ !/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(body.data)) throw failure('Cover must be a PNG/JPEG image of at most 2 MiB');
135
+ const data=Buffer.from(body.data,'base64');
136
+ if (!data.length || data.length > 2*1024*1024) throw failure('Cover must be a PNG/JPEG image of at most 2 MiB');
137
+ const extension=imageExtension(data);
138
+ if (!['png','jpg'].includes(extension)) throw failure('Cover must be a PNG or JPEG image');
139
+ if (!coverDirectory) throw failure('Local cover staging is unavailable');
140
+ coverRoot ??= privateDirectory(coverDirectory,projects.root);
141
+ const root=await coverRoot;
142
+ const directory=scope.cover_directory ?? path.join(root,cap);
143
+ await mkdir(directory,{ recursive:true,mode:0o700 });
144
+ if ((await lstat(directory)).isSymbolicLink() || await realpath(directory) !== directory) throw failure('Invalid private cover directory');
145
+ scope.cover_directory=directory;
146
+ const id=randomUUID(),file=id+'.'+extension;
147
+ await writeFile(path.join(directory,file),data,{ flag:'wx',mode:0o600 });
148
+ requireCurrent(scope);
149
+ const old=scope.cover;
150
+ scope.cover={ id,directory,file,sha256:createHash('sha256').update(data).digest('hex'),bytes:data.length,extension };
151
+ if (old) await rm(path.join(old.directory,old.file),{ force:true });
152
+ return { cover_id:id };
153
+ }
154
+ if (action === 'prepare') {
155
+ bodyFields(body,['materials','cover_id'],['materials']); publicationMaterials(body.materials);
156
+ if (body.materials.cover_path !== undefined) throw failure('Choose a cover image on this local page');
157
+ if (body.cover_id !== undefined && (typeof body.cover_id !== 'string' || body.cover_id !== scope.cover?.id)) throw failure('Cover does not belong to this page',403);
158
+ const stagedCover=body.cover_id ? scope.cover : undefined;
159
+ const resume=await drafts.hasRequest(scope.name,scope.check_task_id,scope.owner);
160
+ const context=await checkedTask(scope.check_task_id,scope.name,resume);
161
+ requireCurrent(scope);
162
+ const task=await tasks.start(scope.name,'publish_prepare',async signal=>{
163
+ requireCurrent(scope);
164
+ await drafts.upload(context.task,scope.owner,signal);
165
+ requireCurrent(scope);
166
+ const result=await publications.prepare(context.task,context.project,body.materials,scope.owner,signal,stagedCover);
167
+ requireCurrent(scope); scope.publication_id=result.publication_id; return result;
168
+ },{ owner:scope.owner,platform:scope.platform,check_task_id:scope.check_task_id });
169
+ scope.tasks.add(task.task_id); scope.task_id=task.task_id; return viewTask(task);
170
+ }
171
+ if (action === 'review') {
172
+ bodyFields(body,['publication_id']); await publication(scope,body.publication_id);
173
+ const result=await publications.review(scope.name,body.publication_id,scope.owner);
174
+ requireCurrent(scope); return result;
175
+ }
176
+ bodyFields(body,['publication_id','revision','confirmed']);
177
+ if (typeof body.revision !== 'string' || !/^[0-9a-f]{64}$/.test(body.revision) || body.confirmed !== true) throw failure('Confirm the exact reviewed publication revision');
178
+ await publication(scope,body.publication_id); requireCurrent(scope);
179
+ const task=await tasks.start(scope.name,'publish_submit',signal=>{
180
+ requireCurrent(scope);
181
+ return publications.submit(scope.name,body.publication_id,body.revision,true,scope.owner,signal);
182
+ },{ owner:scope.owner,platform:scope.platform,publication_id:body.publication_id });
183
+ scope.tasks.add(task.task_id); scope.task_id=task.task_id; return viewTask(task);
184
+ });
185
+ },
186
+ async close() {
187
+ closed=true; unsubscribe?.();
188
+ await Promise.allSettled([...scopes].map(([cap,scope])=>retire(cap,scope)));
189
+ },
190
+ };
191
+ }