playcanvas-agent-bridge-cli 0.6.1 → 0.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +79 -23
- package/README.zh-CN.md +71 -25
- package/dist/cli.js +132 -8
- package/dist/config.js +1 -1
- package/dist/daemon/server.js +79 -1
- package/dist/daemon/target-registry.js +3 -0
- package/dist/processing/manager.js +265 -0
- package/dist/processing/tools.js +97 -0
- package/dist/snippets.js +27 -0
- package/dist/workspace/json-sync.js +821 -0
- package/dist/workspace/manager.js +1973 -326
- package/extension/icons/icon-128.png +0 -0
- package/extension/icons/icon-16.png +0 -0
- package/extension/icons/icon-256.png +0 -0
- package/extension/icons/icon-32.png +0 -0
- package/extension/icons/icon-48.png +0 -0
- package/extension/isolated.js +48 -0
- package/extension/main.js +708 -39
- package/extension/manifest.json +60 -10
- package/extension/popup.html +33 -2
- package/extension/popup.js +142 -11
- package/extension/service-worker.js +124 -1
- package/package.json +19 -18
- package/skills/claude/playcanvas-agent-bridge-cli/SKILL.md +76 -26
- package/skills/codex/playcanvas-agent-bridge-cli/SKILL.md +59 -44
- package/skills/cursor/playcanvas-agent-bridge-cli.mdc +57 -22
- package/skills/windsurf/playcanvas-agent-bridge-cli.md +56 -22
package/dist/daemon/server.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
import { ProcessingManager } from "../processing/manager.js";
|
|
1
2
|
import http from "node:http";
|
|
2
3
|
import { randomUUID } from "node:crypto";
|
|
4
|
+
import { join } from "node:path";
|
|
3
5
|
import { WebSocketServer } from "ws";
|
|
4
6
|
import { CONFIG_DIR, DEFAULT_FRONTEND_PORT, DEFAULT_HOST, DEFAULT_PORT, VERSION, } from "../config.js";
|
|
5
7
|
import { createFrontendServer, } from "../frontend/server.js";
|
|
@@ -12,6 +14,11 @@ function readBody(req) {
|
|
|
12
14
|
let text = "";
|
|
13
15
|
req.setEncoding("utf8");
|
|
14
16
|
req.on("data", (chunk) => {
|
|
17
|
+
if (text.length + chunk.length > (req.url?.startsWith("/processing/") ? 1024 * 1024 : 128 * 1024 * 1024)) {
|
|
18
|
+
reject(new Error("Request body exceeds limit"));
|
|
19
|
+
req.destroy();
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
15
22
|
text += chunk;
|
|
16
23
|
});
|
|
17
24
|
req.on("end", () => {
|
|
@@ -97,6 +104,7 @@ export function createDaemonServer(options) {
|
|
|
97
104
|
}
|
|
98
105
|
const workspace = new WorkspaceManager({
|
|
99
106
|
rootDir: options.workspaceRoot || process.cwd(),
|
|
107
|
+
settingsPath: join(options.frontendRootDir || CONFIG_DIR, "workspace-settings.json"),
|
|
100
108
|
requestTarget: (target, method, params = {}, timeoutMs = 15000) => sendToTarget({
|
|
101
109
|
target,
|
|
102
110
|
method,
|
|
@@ -114,6 +122,10 @@ export function createDaemonServer(options) {
|
|
|
114
122
|
}),
|
|
115
123
|
workspace,
|
|
116
124
|
});
|
|
125
|
+
const processing = new ProcessingManager(target => {
|
|
126
|
+
const status = workspace.statusForTarget(target);
|
|
127
|
+
return isJsonObject(status) && !status.branchConflict && typeof status.projectDirectory === "string" ? status.projectDirectory : null;
|
|
128
|
+
});
|
|
117
129
|
let resolveClosed;
|
|
118
130
|
const closed = new Promise((resolvePromise) => {
|
|
119
131
|
resolveClosed = resolvePromise;
|
|
@@ -189,6 +201,7 @@ export function createDaemonServer(options) {
|
|
|
189
201
|
port: actualPort,
|
|
190
202
|
workspaceRoot: workspace.rootDir,
|
|
191
203
|
targetCount: registry.list().filter((item) => item.connected).length,
|
|
204
|
+
eagerMaxMb: workspace.getSettings().eagerMaxMb,
|
|
192
205
|
},
|
|
193
206
|
target: target || null,
|
|
194
207
|
workspace: workspace.statusForTarget(target),
|
|
@@ -196,6 +209,33 @@ export function createDaemonServer(options) {
|
|
|
196
209
|
}));
|
|
197
210
|
return;
|
|
198
211
|
}
|
|
212
|
+
if (url.pathname === "/workspace/settings" && req.method === "GET") {
|
|
213
|
+
if (!requireToken(req)) {
|
|
214
|
+
writeJson(res, 403, fail("BAD_TOKEN", "Invalid pcbridge token."));
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
writeJson(res, 200, ok(workspace.getSettings()));
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
if (url.pathname === "/workspace/settings" && req.method === "POST") {
|
|
221
|
+
if (!requireToken(req)) {
|
|
222
|
+
writeJson(res, 403, fail("BAD_TOKEN", "Invalid pcbridge token."));
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
const body = await readBody(req);
|
|
226
|
+
if (!isJsonObject(body) || body.eagerMaxMb === undefined) {
|
|
227
|
+
writeJson(res, 400, fail("INVALID_REQUEST", "workspace settings requires eagerMaxMb (1..10)."));
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
const rawMb = Number(body.eagerMaxMb);
|
|
231
|
+
if (!Number.isFinite(rawMb) || rawMb < 1 || rawMb > 10) {
|
|
232
|
+
writeJson(res, 400, fail("INVALID_REQUEST", "eagerMaxMb must be between 1 and 10."));
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
const updated = await workspace.updateSettings({ eagerMaxMb: rawMb });
|
|
236
|
+
writeJson(res, 200, ok(updated));
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
199
239
|
if (url.pathname === "/workspace/status" && req.method === "GET") {
|
|
200
240
|
if (!requireToken(req)) {
|
|
201
241
|
writeJson(res, 403, fail("BAD_TOKEN", "Invalid pcbridge token."));
|
|
@@ -209,6 +249,30 @@ export function createDaemonServer(options) {
|
|
|
209
249
|
writeJson(res, 200, ok(workspace.statusForTarget(resolved.target.info)));
|
|
210
250
|
return;
|
|
211
251
|
}
|
|
252
|
+
const processingMatch = url.pathname.match(/^\/processing\/(capabilities|begin|chunk|run|status|read|cancel|finish)$/);
|
|
253
|
+
if (processingMatch && req.method === "POST") {
|
|
254
|
+
if (!requireToken(req)) {
|
|
255
|
+
writeJson(res, 403, fail("BAD_TOKEN", "Invalid pcbridge token."));
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
const body = await readBody(req);
|
|
259
|
+
if (!isJsonObject(body)) {
|
|
260
|
+
writeJson(res, 400, fail("INVALID_REQUEST", "Object required"));
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
const target = registry.resolve(typeof body.target === "string" ? body.target : "current");
|
|
264
|
+
if (!target.ok) {
|
|
265
|
+
writeJson(res, 400, fail(target.code, target.message));
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
try {
|
|
269
|
+
writeJson(res, 200, ok(await processing.request(target.target.info, processingMatch[1], body)));
|
|
270
|
+
}
|
|
271
|
+
catch (error) {
|
|
272
|
+
writeJson(res, 400, fail("PROCESSING_ERROR", String(error)));
|
|
273
|
+
}
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
212
276
|
if (url.pathname === "/workspace/sync" && req.method === "POST") {
|
|
213
277
|
if (!requireToken(req)) {
|
|
214
278
|
writeJson(res, 403, fail("BAD_TOKEN", "Invalid pcbridge token."));
|
|
@@ -352,6 +416,20 @@ export function createDaemonServer(options) {
|
|
|
352
416
|
}
|
|
353
417
|
return;
|
|
354
418
|
}
|
|
419
|
+
if (message.type === "workspace:delta" && message.delta && typeof message.delta === "object") {
|
|
420
|
+
const selector = Number.isInteger(message.tabId)
|
|
421
|
+
? `tab:${message.tabId}`
|
|
422
|
+
: message.clientId
|
|
423
|
+
? `client:${message.clientId}`
|
|
424
|
+
: "";
|
|
425
|
+
const resolved = selector ? registry.resolve(selector) : null;
|
|
426
|
+
if (resolved && resolved.ok && resolved.target.ws === ws) {
|
|
427
|
+
void workspace.handleDelta(resolved.target.info, message.delta).catch((error) => {
|
|
428
|
+
log(`workspace delta failed for ${resolved.target.info.id}: ${String(error)}`);
|
|
429
|
+
});
|
|
430
|
+
}
|
|
431
|
+
return;
|
|
432
|
+
}
|
|
355
433
|
if (message.type === "response" && message.id) {
|
|
356
434
|
const waiter = pending.get(message.id);
|
|
357
435
|
if (!waiter)
|
|
@@ -409,7 +487,7 @@ export function createDaemonServer(options) {
|
|
|
409
487
|
waiter.resolve(fail("DAEMON_STOPPING", "pcbridge daemon is stopping."));
|
|
410
488
|
}
|
|
411
489
|
pending.clear();
|
|
412
|
-
const builderCleanup = await Promise.allSettled([builder.close()]);
|
|
490
|
+
const builderCleanup = await Promise.allSettled([builder.close(), processing.close()]);
|
|
413
491
|
const cleanupResults = [
|
|
414
492
|
...builderCleanup,
|
|
415
493
|
...await Promise.allSettled([
|
|
@@ -13,6 +13,9 @@ export class TargetRegistry {
|
|
|
13
13
|
tabId: info.tabId,
|
|
14
14
|
windowId: info.windowId,
|
|
15
15
|
kind,
|
|
16
|
+
platform: info.platform,
|
|
17
|
+
bridgeProtocolVersion: info.bridgeProtocolVersion,
|
|
18
|
+
processingProtocolVersion: info.processingProtocolVersion,
|
|
16
19
|
url: info.url || existing?.url || "",
|
|
17
20
|
title: info.title || existing?.title,
|
|
18
21
|
projectId: info.projectId || existing?.projectId,
|
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
+
import { mkdir, mkdtemp, open, readFile, readdir, realpath, rm, stat, writeFile } from "node:fs/promises";
|
|
3
|
+
import { createReadStream } from "node:fs";
|
|
4
|
+
import { spawn } from "node:child_process";
|
|
5
|
+
import { dirname, join, relative, sep } from "node:path";
|
|
6
|
+
import { createRequire } from "node:module";
|
|
7
|
+
import { activeTools, MEMORY_BYTES, RECIPE, TOOLS_ROOT } from "./tools.js";
|
|
8
|
+
export const CHUNK_BYTES = 256 * 1024;
|
|
9
|
+
async function digest(path) {
|
|
10
|
+
const h = createHash("md5");
|
|
11
|
+
for await (const chunk of createReadStream(path))
|
|
12
|
+
h.update(chunk);
|
|
13
|
+
return h.digest("hex");
|
|
14
|
+
}
|
|
15
|
+
export class ProcessingManager {
|
|
16
|
+
projectDirectory;
|
|
17
|
+
tasks = new Map();
|
|
18
|
+
timer;
|
|
19
|
+
constructor(projectDirectory) {
|
|
20
|
+
this.projectDirectory = projectDirectory;
|
|
21
|
+
this.timer = setInterval(() => {
|
|
22
|
+
for (const task of this.tasks.values())
|
|
23
|
+
if (Date.now() - task.touched > 90000)
|
|
24
|
+
void this.remove(task);
|
|
25
|
+
if (this.tasks.size)
|
|
26
|
+
void readFile(join(TOOLS_ROOT, "settings.json"), "utf8").then(text => {
|
|
27
|
+
if (JSON.parse(text).enabled !== true)
|
|
28
|
+
for (const task of this.tasks.values())
|
|
29
|
+
void this.remove(task);
|
|
30
|
+
}).catch(() => { for (const task of this.tasks.values())
|
|
31
|
+
void this.remove(task); });
|
|
32
|
+
}, 10000);
|
|
33
|
+
this.timer.unref();
|
|
34
|
+
}
|
|
35
|
+
async capabilities() {
|
|
36
|
+
const config = JSON.parse(await readFile(join(TOOLS_ROOT, "settings.json"), "utf8").catch(() => '{}'));
|
|
37
|
+
const memoryBytes = Number(config.memoryBytes || MEMORY_BYTES);
|
|
38
|
+
const timeoutMs = Number(config.timeoutMs || 600000);
|
|
39
|
+
if (!Number.isSafeInteger(memoryBytes) || memoryBytes < 64 * 1024 * 1024 || memoryBytes > MEMORY_BYTES ||
|
|
40
|
+
!Number.isSafeInteger(timeoutMs) || timeoutMs < 1000 || timeoutMs > 600000)
|
|
41
|
+
throw new Error("INVALID_PROCESSING_LIMITS");
|
|
42
|
+
const base = { bridgeProtocolVersion: 1, processingProtocolVersion: 1, recipeVersion: RECIPE,
|
|
43
|
+
targetEngine: "1.77.0", memoryBudgetBytes: memoryBytes, timeoutMs, enabled: config.enabled === true,
|
|
44
|
+
toolRelease: null, toolFingerprints: [], busy: this.tasks.size > 0, available: false, formats: { texture: [], model: [] } };
|
|
45
|
+
if (!base.enabled)
|
|
46
|
+
return { ...base, reason: "LOCAL_DISABLED" };
|
|
47
|
+
try {
|
|
48
|
+
const { manifest } = await activeTools();
|
|
49
|
+
return { ...base, available: !base.busy, formats: manifest.formats, toolRelease: manifest.release,
|
|
50
|
+
toolFingerprints: Object.values(manifest.binaries).map(spec => spec.sha256), reason: base.busy ? "LOCAL_BUSY" : null };
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
return { ...base, reason: "TOOLS_UNAVAILABLE" };
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
requireTask(target, id) {
|
|
57
|
+
const task = typeof id === "string" ? this.tasks.get(id) : null;
|
|
58
|
+
if (!task || task.targetId !== target.id || task.clientId !== target.clientId || task.branchId !== target.branchId)
|
|
59
|
+
throw new Error("PROCESSING_TARGET_MISMATCH");
|
|
60
|
+
task.touched = Date.now();
|
|
61
|
+
return task;
|
|
62
|
+
}
|
|
63
|
+
async request(target, action, body) {
|
|
64
|
+
if (target.kind !== "editor" || target.platform !== "selfhost")
|
|
65
|
+
throw new Error("SELFHOST_EDITOR_REQUIRED");
|
|
66
|
+
if (target.processingProtocolVersion !== 1)
|
|
67
|
+
throw new Error("PROCESSING_EDITOR_UNSUPPORTED");
|
|
68
|
+
if (action === "capabilities")
|
|
69
|
+
return this.capabilities();
|
|
70
|
+
if (action === "begin") {
|
|
71
|
+
const caps = await this.capabilities();
|
|
72
|
+
if (!caps.available || this.tasks.size)
|
|
73
|
+
throw new Error("LOCAL_UNAVAILABLE");
|
|
74
|
+
const source = body.source;
|
|
75
|
+
if (!source || !/^[^/\\\x00-\x1f]{1,200}$/.test(source.filename) || source.filename === "." || source.filename === ".." ||
|
|
76
|
+
!Number.isSafeInteger(source.size) || source.size < 1 || source.size > caps.memoryBudgetBytes / 2 ||
|
|
77
|
+
!/^[a-f0-9]{32}$/.test(source.hash) || !["model", "texture"].includes(body.kind) ||
|
|
78
|
+
typeof body.jobId !== "string" || body.jobId.length > 100)
|
|
79
|
+
throw new Error("INVALID_PROCESSING_TASK");
|
|
80
|
+
// Only copy the allowlisted fields; server leases and signed URLs never enter a task.
|
|
81
|
+
const project = this.projectDirectory(target);
|
|
82
|
+
if (!project)
|
|
83
|
+
throw new Error("WORKSPACE_NOT_READY");
|
|
84
|
+
const root = join(project, "tmp", "processing");
|
|
85
|
+
await mkdir(root, { recursive: true });
|
|
86
|
+
if (!(await realpath(root)).startsWith((await realpath(project)) + sep))
|
|
87
|
+
throw new Error("PROCESSING_PATH_ESCAPE");
|
|
88
|
+
for (const name of await readdir(root)) {
|
|
89
|
+
if (!name.startsWith("job-"))
|
|
90
|
+
continue;
|
|
91
|
+
const stale = join(root, name);
|
|
92
|
+
if (Date.now() - (await stat(stale)).mtimeMs > 7 * 86400000)
|
|
93
|
+
await rm(stale, { recursive: true, force: true });
|
|
94
|
+
}
|
|
95
|
+
const directory = await mkdtemp(join(root, "job-"));
|
|
96
|
+
if (this.tasks.size) {
|
|
97
|
+
await rm(directory, { recursive: true });
|
|
98
|
+
throw new Error("LOCAL_BUSY");
|
|
99
|
+
}
|
|
100
|
+
const task = { id: randomUUID(), targetId: target.id, clientId: target.clientId, branchId: target.branchId, jobId: body.jobId,
|
|
101
|
+
directory, source: { filename: source.filename, size: source.size, hash: source.hash }, offset: 0,
|
|
102
|
+
state: "receiving", phase: "download", progress: 0, touched: Date.now(), files: {}, busy: false, memoryBytes: caps.memoryBudgetBytes, timeoutMs: caps.timeoutMs,
|
|
103
|
+
job: { filename: source.filename, kind: body.kind, options: body.options || {} } };
|
|
104
|
+
this.tasks.set(task.id, task);
|
|
105
|
+
await writeFile(join(directory, "source"), Buffer.alloc(0));
|
|
106
|
+
return { transferId: task.id, chunkBytes: CHUNK_BYTES };
|
|
107
|
+
}
|
|
108
|
+
const task = this.requireTask(target, body.transferId);
|
|
109
|
+
if (action === "cancel" || action === "finish") {
|
|
110
|
+
await this.remove(task);
|
|
111
|
+
return { removed: true };
|
|
112
|
+
}
|
|
113
|
+
if (action === "status")
|
|
114
|
+
return { state: task.state, phase: task.phase, progress: task.progress, error: task.error,
|
|
115
|
+
manifest: task.manifest, files: Object.fromEntries(Object.entries(task.files).map(([id, { path, ...file }]) => [id, file])) };
|
|
116
|
+
if (task.busy)
|
|
117
|
+
throw new Error("PROCESSING_TRANSFER_BUSY");
|
|
118
|
+
task.busy = true;
|
|
119
|
+
try {
|
|
120
|
+
if (action === "chunk") {
|
|
121
|
+
if (task.state !== "receiving" || !Number.isSafeInteger(body.offset) || body.offset < 0 ||
|
|
122
|
+
typeof body.data !== "string" || body.data.length > Math.ceil(CHUNK_BYTES / 3) * 4 || !/^[A-Za-z0-9+/]*={0,2}$/.test(body.data))
|
|
123
|
+
throw new Error("INVALID_PROCESSING_CHUNK");
|
|
124
|
+
const bytes = Buffer.from(body.data, "base64");
|
|
125
|
+
if (!bytes.length || bytes.length > CHUNK_BYTES || bytes.toString("base64") !== body.data || body.offset + bytes.length > task.source.size)
|
|
126
|
+
throw new Error("INVALID_PROCESSING_CHUNK");
|
|
127
|
+
const file = await open(join(task.directory, "source"), "r+");
|
|
128
|
+
try {
|
|
129
|
+
if (body.offset < task.offset) {
|
|
130
|
+
const old = Buffer.alloc(bytes.length);
|
|
131
|
+
await file.read(old, 0, old.length, body.offset);
|
|
132
|
+
if (body.offset + bytes.length > task.offset || !old.equals(bytes))
|
|
133
|
+
throw new Error("PROCESSING_CHUNK_CONFLICT");
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
if (body.offset !== task.offset)
|
|
137
|
+
throw new Error("PROCESSING_CHUNK_ORDER");
|
|
138
|
+
await file.write(bytes, 0, bytes.length, task.offset);
|
|
139
|
+
task.offset += bytes.length;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
finally {
|
|
143
|
+
await file.close();
|
|
144
|
+
}
|
|
145
|
+
return { offset: task.offset };
|
|
146
|
+
}
|
|
147
|
+
if (action === "run") {
|
|
148
|
+
if (task.state !== "receiving" || task.offset !== task.source.size || await digest(join(task.directory, "source")) !== task.source.hash)
|
|
149
|
+
throw new Error("PROCESSING_SOURCE_INTEGRITY");
|
|
150
|
+
task.state = "running";
|
|
151
|
+
void this.convert(task).catch(error => { task.state = "failed"; task.error = String(error).slice(0, 2000); });
|
|
152
|
+
return { state: task.state };
|
|
153
|
+
}
|
|
154
|
+
if (action === "read") {
|
|
155
|
+
const file = task.files[body.fileId];
|
|
156
|
+
if (task.state !== "computed" || !file || !Number.isSafeInteger(body.offset) || body.offset < 0 || body.offset >= file.size)
|
|
157
|
+
throw new Error("INVALID_PROCESSING_READ");
|
|
158
|
+
const bytes = Buffer.alloc(Math.min(CHUNK_BYTES, file.size - body.offset));
|
|
159
|
+
const handle = await open(file.path, "r");
|
|
160
|
+
try {
|
|
161
|
+
await handle.read(bytes, 0, bytes.length, body.offset);
|
|
162
|
+
}
|
|
163
|
+
finally {
|
|
164
|
+
await handle.close();
|
|
165
|
+
}
|
|
166
|
+
return { offset: body.offset, data: bytes.toString("base64") };
|
|
167
|
+
}
|
|
168
|
+
throw new Error("UNKNOWN_PROCESSING_ACTION");
|
|
169
|
+
}
|
|
170
|
+
finally {
|
|
171
|
+
task.busy = false;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
async convert(task) {
|
|
175
|
+
const { directory, manifest } = await activeTools();
|
|
176
|
+
const config = { job: task.job, input: join(task.directory, "source"), directory: task.directory,
|
|
177
|
+
memoryBytes: task.memoryBytes, executable: join(directory, manifest.binaries.modelConverter.path),
|
|
178
|
+
textureExecutable: join(directory, manifest.binaries.textureConverter.path),
|
|
179
|
+
catalogPath: join(directory, manifest.catalog.path), includeThumbnails: true };
|
|
180
|
+
const configPath = join(task.directory, "input.json");
|
|
181
|
+
await writeFile(configPath, JSON.stringify(config));
|
|
182
|
+
const require = createRequire(import.meta.url);
|
|
183
|
+
const sharpModules = dirname(dirname(dirname(require.resolve("sharp"))));
|
|
184
|
+
if (!this.tasks.has(task.id))
|
|
185
|
+
return;
|
|
186
|
+
const proc = spawn(process.execPath, [join(directory, manifest.runner.path), configPath], {
|
|
187
|
+
cwd: task.directory, detached: process.platform !== "win32",
|
|
188
|
+
env: { PATH: process.env.PATH || "/usr/bin:/bin", NODE_PATH: sharpModules,
|
|
189
|
+
IMPORT_WORKER_MEMORY_BYTES: String(task.memoryBytes) }, stdio: ["ignore", "pipe", "pipe"]
|
|
190
|
+
});
|
|
191
|
+
task.process = proc;
|
|
192
|
+
let errorOutput = "", pending = "";
|
|
193
|
+
proc.stderr?.on("data", chunk => { errorOutput = (errorOutput + chunk).slice(-2000); });
|
|
194
|
+
proc.stdout?.on("data", chunk => {
|
|
195
|
+
pending = (pending + chunk).slice(-8000);
|
|
196
|
+
const lines = pending.split("\n");
|
|
197
|
+
pending = lines.pop() || "";
|
|
198
|
+
for (const line of lines)
|
|
199
|
+
try {
|
|
200
|
+
const progress = JSON.parse(line);
|
|
201
|
+
if (Number.isFinite(progress.progress))
|
|
202
|
+
task.progress = Math.min(.9, Math.max(0, progress.progress));
|
|
203
|
+
if (typeof progress.phase === "string")
|
|
204
|
+
task.phase = progress.phase.slice(0, 80);
|
|
205
|
+
}
|
|
206
|
+
catch { /* native diagnostics are not protocol messages */ }
|
|
207
|
+
});
|
|
208
|
+
const timeout = setTimeout(() => this.kill(task), task.timeoutMs);
|
|
209
|
+
try {
|
|
210
|
+
const code = await new Promise((resolveExit, reject) => { proc.once("error", reject); proc.once("exit", resolveExit); });
|
|
211
|
+
if (code !== 0)
|
|
212
|
+
throw new Error(`CONVERTER_FAILED: ${errorOutput}`);
|
|
213
|
+
}
|
|
214
|
+
finally {
|
|
215
|
+
clearTimeout(timeout);
|
|
216
|
+
task.process = undefined;
|
|
217
|
+
}
|
|
218
|
+
if (!this.tasks.has(task.id))
|
|
219
|
+
return;
|
|
220
|
+
const resultPath = join(task.directory, "result.json");
|
|
221
|
+
if ((await stat(resultPath)).size > 32 * 1024 * 1024)
|
|
222
|
+
throw new Error("PROCESSING_MANIFEST_TOO_LARGE");
|
|
223
|
+
const result = JSON.parse(await readFile(resultPath, "utf8"));
|
|
224
|
+
if (!Array.isArray(result.artifacts) || result.artifacts.length > 10000)
|
|
225
|
+
throw new Error("PROCESSING_ARTIFACT_LIMIT");
|
|
226
|
+
let total = 0;
|
|
227
|
+
const addFile = async (artifact, artifactId, role, variant) => {
|
|
228
|
+
const path = await realpath(artifact.localFile);
|
|
229
|
+
if (relative(task.directory, path).startsWith("..") || !path.startsWith(task.directory + sep))
|
|
230
|
+
throw new Error("PROCESSING_OUTPUT_ESCAPE");
|
|
231
|
+
const size = (await stat(path)).size;
|
|
232
|
+
total += size;
|
|
233
|
+
if (size > 256 * 1024 * 1024 || total > MEMORY_BYTES * 2)
|
|
234
|
+
throw new Error("PROCESSING_OUTPUT_BUDGET");
|
|
235
|
+
const id = randomUUID();
|
|
236
|
+
task.files[id] = { path, size, hash: await digest(path), filename: artifact.filename, mime: artifact.mime || "application/octet-stream", role, artifactId, artifactType: artifact.type, variant };
|
|
237
|
+
delete artifact.localFile;
|
|
238
|
+
artifact.fileId = id;
|
|
239
|
+
};
|
|
240
|
+
for (const [i, artifact] of result.artifacts.entries()) {
|
|
241
|
+
if (artifact.localFile)
|
|
242
|
+
await addFile(artifact, String(i), "derived");
|
|
243
|
+
for (const [variant, thumb] of Object.entries(artifact.thumbnails || {}))
|
|
244
|
+
await addFile(thumb, String(i), "thumbnail", variant);
|
|
245
|
+
}
|
|
246
|
+
task.manifest = result;
|
|
247
|
+
task.state = "computed";
|
|
248
|
+
task.phase = "upload";
|
|
249
|
+
task.progress = .9;
|
|
250
|
+
}
|
|
251
|
+
kill(task) {
|
|
252
|
+
if (!task.process?.pid)
|
|
253
|
+
return;
|
|
254
|
+
try {
|
|
255
|
+
process.kill(process.platform === "win32" ? task.process.pid : -task.process.pid, "SIGKILL");
|
|
256
|
+
}
|
|
257
|
+
catch { /* already exited */ }
|
|
258
|
+
}
|
|
259
|
+
async remove(task) {
|
|
260
|
+
this.tasks.delete(task.id);
|
|
261
|
+
this.kill(task);
|
|
262
|
+
await rm(task.directory, { recursive: true, force: true });
|
|
263
|
+
}
|
|
264
|
+
async close() { clearInterval(this.timer); await Promise.all([...this.tasks.values()].map(task => this.remove(task))); }
|
|
265
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { mkdir, readFile, writeFile, mkdtemp, rm, rename, lstat, chmod } from "node:fs/promises";
|
|
3
|
+
import { join, resolve, dirname } from "node:path";
|
|
4
|
+
import { x } from "tar";
|
|
5
|
+
import { CONFIG_DIR } from "../config.js";
|
|
6
|
+
export const TOOLS_ROOT = join(CONFIG_DIR, "asset-tools");
|
|
7
|
+
export const RECIPE = "asset-tools-v7";
|
|
8
|
+
export const MEMORY_BYTES = 1024 * 1024 * 1024;
|
|
9
|
+
export const hash = (data, algorithm = "sha256") => createHash(algorithm).update(data).digest("hex");
|
|
10
|
+
export async function verifyTools(directory) {
|
|
11
|
+
const manifest = JSON.parse(await readFile(join(directory, "release.json"), "utf8"));
|
|
12
|
+
if (manifest.schemaVersion !== 1 || manifest.platform !== process.platform || manifest.arch !== process.arch ||
|
|
13
|
+
manifest.recipeVersion !== RECIPE || manifest.targetEngine !== "1.77.0")
|
|
14
|
+
throw new Error("TOOLS_PLATFORM_OR_RECIPE_MISMATCH");
|
|
15
|
+
for (const spec of [manifest.runner, manifest.catalog, ...Object.values(manifest.binaries)]) {
|
|
16
|
+
if (!/^(bin|runner|schema)\/[a-zA-Z0-9._-]+$/.test(spec.path))
|
|
17
|
+
throw new Error("TOOLS_INVALID_PATH");
|
|
18
|
+
const path = join(directory, spec.path);
|
|
19
|
+
const info = await lstat(path);
|
|
20
|
+
if (!info.isFile() || info.isSymbolicLink() || info.size !== spec.size || spec.size > 256 * 1024 * 1024 ||
|
|
21
|
+
hash(await readFile(path)) !== spec.sha256)
|
|
22
|
+
throw new Error("TOOLS_INTEGRITY_MISMATCH");
|
|
23
|
+
}
|
|
24
|
+
return manifest;
|
|
25
|
+
}
|
|
26
|
+
async function readInput(source, maxBytes) {
|
|
27
|
+
if (!/^https:\/\//.test(source)) {
|
|
28
|
+
const info = await lstat(source);
|
|
29
|
+
if (!info.isFile() || info.size > maxBytes)
|
|
30
|
+
throw new Error("TOOLS_DOWNLOAD_TOO_LARGE");
|
|
31
|
+
return readFile(source);
|
|
32
|
+
}
|
|
33
|
+
const response = await fetch(source, { signal: AbortSignal.timeout(120000), redirect: "follow" });
|
|
34
|
+
if (!response.url.startsWith("https://") || !response.ok || !response.body)
|
|
35
|
+
throw new Error(`TOOLS_DOWNLOAD_HTTP_${response.status}`);
|
|
36
|
+
const chunks = [];
|
|
37
|
+
let size = 0;
|
|
38
|
+
for await (const chunk of response.body) {
|
|
39
|
+
size += chunk.length;
|
|
40
|
+
if (size > maxBytes)
|
|
41
|
+
throw new Error("TOOLS_DOWNLOAD_TOO_LARGE");
|
|
42
|
+
chunks.push(chunk);
|
|
43
|
+
}
|
|
44
|
+
return Buffer.concat(chunks);
|
|
45
|
+
}
|
|
46
|
+
// Installation is an explicit local CLI action. Browser requests cannot install executable code.
|
|
47
|
+
export async function installTools(source) {
|
|
48
|
+
const manifestBytes = await readInput(source, 1024 * 1024);
|
|
49
|
+
const manifest = JSON.parse(manifestBytes.toString());
|
|
50
|
+
if (!/^[a-zA-Z0-9._-]{1,160}$/.test(manifest.release) || !/^[a-zA-Z0-9._-]+\.tar\.gz$/.test(manifest.archive.name))
|
|
51
|
+
throw new Error("TOOLS_INVALID_MANIFEST");
|
|
52
|
+
const archiveSource = /^https:\/\//.test(source) ? manifest.archive.url : resolve(dirname(source), manifest.archive.name);
|
|
53
|
+
const archive = await readInput(archiveSource, 256 * 1024 * 1024);
|
|
54
|
+
if (archive.length !== manifest.archive.size || hash(archive) !== manifest.archive.sha256)
|
|
55
|
+
throw new Error("TOOLS_ARCHIVE_INTEGRITY_MISMATCH");
|
|
56
|
+
await mkdir(TOOLS_ROOT, { recursive: true });
|
|
57
|
+
const staging = await mkdtemp(join(TOOLS_ROOT, ".install-"));
|
|
58
|
+
try {
|
|
59
|
+
const archivePath = join(staging, "package.tgz");
|
|
60
|
+
await writeFile(archivePath, archive);
|
|
61
|
+
let expanded = 0;
|
|
62
|
+
await x({ file: archivePath, cwd: staging, strict: true, filter: (path, entry) => {
|
|
63
|
+
if (!/^(?:bin|runner|schema)\/?$|^(?:bin|runner|schema)\/[a-zA-Z0-9._-]+$|^THIRD-PARTY-LICENSES.txt$/.test(path) ||
|
|
64
|
+
!("type" in entry) || !["File", "Directory"].includes(entry.type))
|
|
65
|
+
throw new Error("TOOLS_UNSAFE_ARCHIVE");
|
|
66
|
+
expanded += entry.size;
|
|
67
|
+
if (expanded > 512 * 1024 * 1024)
|
|
68
|
+
throw new Error("TOOLS_ARCHIVE_TOO_LARGE");
|
|
69
|
+
return true;
|
|
70
|
+
} });
|
|
71
|
+
await writeFile(join(staging, "release.json"), manifestBytes);
|
|
72
|
+
await verifyTools(staging);
|
|
73
|
+
for (const spec of Object.values(manifest.binaries))
|
|
74
|
+
await chmod(join(staging, spec.path), 0o755);
|
|
75
|
+
await rm(archivePath);
|
|
76
|
+
const destination = join(TOOLS_ROOT, `${manifest.release}-${manifest.archive.sha256.slice(0, 16)}`);
|
|
77
|
+
try {
|
|
78
|
+
await rename(staging, destination);
|
|
79
|
+
}
|
|
80
|
+
catch (error) {
|
|
81
|
+
if (!["EEXIST", "ENOTEMPTY"].includes(error.code || ""))
|
|
82
|
+
throw error;
|
|
83
|
+
await verifyTools(destination);
|
|
84
|
+
}
|
|
85
|
+
await writeFile(join(TOOLS_ROOT, "active.json"), JSON.stringify({ directory: destination }));
|
|
86
|
+
return { release: manifest.release, directory: destination };
|
|
87
|
+
}
|
|
88
|
+
finally {
|
|
89
|
+
await rm(staging, { recursive: true, force: true });
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
export async function activeTools() {
|
|
93
|
+
const active = JSON.parse(await readFile(join(TOOLS_ROOT, "active.json"), "utf8"));
|
|
94
|
+
if (dirname(active.directory) !== TOOLS_ROOT)
|
|
95
|
+
throw new Error("TOOLS_INVALID_ACTIVE_PATH");
|
|
96
|
+
return { directory: active.directory, manifest: await verifyTools(active.directory) };
|
|
97
|
+
}
|
package/dist/snippets.js
CHANGED
|
@@ -1619,6 +1619,33 @@ return {
|
|
|
1619
1619
|
};
|
|
1620
1620
|
`;
|
|
1621
1621
|
}
|
|
1622
|
+
export function sceneManageSnippet() {
|
|
1623
|
+
return `
|
|
1624
|
+
const args = command.args || {};
|
|
1625
|
+
const list = () => new Promise((resolve, reject) => editor.call("scenes:list", resolve, reject));
|
|
1626
|
+
if (args.action === "list") return await list();
|
|
1627
|
+
if (args.action === "create") {
|
|
1628
|
+
if (!editor.call("permissions:write")) throw new Error("Scene creation requires write access.");
|
|
1629
|
+
return await new Promise((resolve, reject) => editor.call("scenes:new", args.name, resolve, reject));
|
|
1630
|
+
}
|
|
1631
|
+
const scenes = await list();
|
|
1632
|
+
const scene = scenes.find(item => String(item.id) === String(args.id) || String(item.uniqueId) === String(args.id));
|
|
1633
|
+
if (!scene) throw new Error("Scene not found in the current project: " + args.id);
|
|
1634
|
+
await new Promise((resolve, reject) => editor.call("scenes:get", String(scene.id), (error, data) => error ? reject(error) : resolve(data)));
|
|
1635
|
+
if (String(editor.api.globals.realtime.scenes.current?.uniqueId) === String(scene.uniqueId)) return scene;
|
|
1636
|
+
await new Promise((resolve, reject) => {
|
|
1637
|
+
const event = editor.on("scene:load", (id, uniqueId) => {
|
|
1638
|
+
if (String(uniqueId) !== String(scene.uniqueId)) return;
|
|
1639
|
+
clearTimeout(timer);
|
|
1640
|
+
event.unbind();
|
|
1641
|
+
resolve();
|
|
1642
|
+
});
|
|
1643
|
+
const timer = setTimeout(() => { event.unbind(); reject(new Error("Scene load timed out.")); }, 50000);
|
|
1644
|
+
editor.call("scene:load", scene.uniqueId);
|
|
1645
|
+
});
|
|
1646
|
+
return scene;
|
|
1647
|
+
`;
|
|
1648
|
+
}
|
|
1622
1649
|
export function sceneSettingsGetSnippet() {
|
|
1623
1650
|
return `
|
|
1624
1651
|
return editor.api.globals.settings.scene.json();
|