playcanvas-agent-bridge-cli 0.6.1 → 0.7.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 +148 -31
- package/README.zh-CN.md +137 -33
- package/dist/cli.js +514 -95
- package/dist/config.js +23 -1
- package/dist/daemon/server.js +336 -28
- package/dist/daemon/target-registry.js +3 -0
- package/dist/plugins/host.js +260 -0
- package/dist/plugins/store.js +567 -0
- package/dist/processing/manager.js +265 -0
- package/dist/processing/tools.js +97 -0
- package/dist/snippets.js +27 -0
- package/dist/updates.js +475 -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 +59 -2
- package/extension/main.js +770 -275
- package/extension/manifest.json +60 -10
- package/extension/popup.html +115 -3
- package/extension/popup.js +398 -14
- package/extension/service-worker.js +221 -4
- package/package.json +18 -18
- package/plugins/index.json +14 -0
- package/plugins/tinyapp-builder/node/builder.js +589 -0
- package/plugins/tinyapp-builder/node/index.js +30 -0
- package/plugins/tinyapp-builder/package.json +13 -0
- package/plugins/tinyapp-builder/page/editor.js +249 -0
- package/plugins/tinyapp-builder/pcbridge-plugin.json +28 -0
- package/skills/claude/pcbridge-plugin-authoring/SKILL.md +220 -0
- package/skills/claude/playcanvas-agent-bridge-cli/SKILL.md +99 -43
- package/skills/codex/pcbridge-plugin-authoring/SKILL.md +220 -0
- package/skills/codex/pcbridge-plugin-authoring/agents/openai.yaml +4 -0
- package/skills/codex/playcanvas-agent-bridge-cli/SKILL.md +89 -62
- package/skills/cursor/pcbridge-plugin-authoring.mdc +221 -0
- package/skills/cursor/playcanvas-agent-bridge-cli.mdc +69 -33
- package/skills/windsurf/pcbridge-plugin-authoring.md +215 -0
- package/skills/windsurf/playcanvas-agent-bridge-cli.md +68 -33
- package/dist/builder/manager.js +0 -564
package/dist/config.js
CHANGED
|
@@ -4,7 +4,10 @@ import { existsSync } from "node:fs";
|
|
|
4
4
|
import { homedir } from "node:os";
|
|
5
5
|
import { dirname, join, resolve } from "node:path";
|
|
6
6
|
import { fileURLToPath } from "node:url";
|
|
7
|
-
export const VERSION = "0.
|
|
7
|
+
export const VERSION = "0.7.0";
|
|
8
|
+
export const PACKAGE_NAME = "playcanvas-agent-bridge-cli";
|
|
9
|
+
export const DEFAULT_PCBRIDGE_REPOSITORY = "Q-My99/playcanvas-agent-bridge-cli";
|
|
10
|
+
export const DEFAULT_PCBRIDGE_REF = "main";
|
|
8
11
|
export const DEFAULT_HOST = "127.0.0.1";
|
|
9
12
|
export const DEFAULT_PORT = 17329;
|
|
10
13
|
export const DEFAULT_FRONTEND_PORT = 3487;
|
|
@@ -14,6 +17,25 @@ export const CONFIG_DIR = process.env.PCBRIDGE_CONFIG_DIR
|
|
|
14
17
|
: join(homedir(), ".pcbridge");
|
|
15
18
|
export const SESSION_FILE = join(CONFIG_DIR, "session.json");
|
|
16
19
|
export const EXTENSION_INSTALL_DIR = join(CONFIG_DIR, "extension");
|
|
20
|
+
export const UPDATE_STATE_FILE = join(CONFIG_DIR, "update-state.json");
|
|
21
|
+
export const INSTALLED_SKILLS_FILE = join(CONFIG_DIR, "installed-skills.json");
|
|
22
|
+
export function parseSemver(version) {
|
|
23
|
+
const match = /^(\d+)\.(\d+)\.(\d+)(?:[-+].*)?$/.exec(version.trim());
|
|
24
|
+
if (!match)
|
|
25
|
+
return null;
|
|
26
|
+
return [Number(match[1]), Number(match[2]), Number(match[3])];
|
|
27
|
+
}
|
|
28
|
+
export function compareSemver(left, right) {
|
|
29
|
+
const a = parseSemver(left);
|
|
30
|
+
const b = parseSemver(right);
|
|
31
|
+
if (!a || !b)
|
|
32
|
+
return left.localeCompare(right);
|
|
33
|
+
for (let index = 0; index < 3; index += 1) {
|
|
34
|
+
if (a[index] !== b[index])
|
|
35
|
+
return a[index] - b[index];
|
|
36
|
+
}
|
|
37
|
+
return 0;
|
|
38
|
+
}
|
|
17
39
|
export async function readOrCreateSession() {
|
|
18
40
|
await mkdir(CONFIG_DIR, { recursive: true });
|
|
19
41
|
try {
|
package/dist/daemon/server.js
CHANGED
|
@@ -1,17 +1,26 @@
|
|
|
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";
|
|
6
8
|
import { TargetRegistry } from "./target-registry.js";
|
|
7
9
|
import { WorkspaceManager } from "../workspace/manager.js";
|
|
8
|
-
import {
|
|
10
|
+
import { PluginHost } from "../plugins/host.js";
|
|
11
|
+
import { parsePluginSource, PluginStore } from "../plugins/store.js";
|
|
12
|
+
import { executeOneClickUpdate, formatHumanUpdateHint, getUpdateReport, readGeneratedExtensionVersion, UPDATE_CACHE_TTL_MS, } from "../updates.js";
|
|
9
13
|
import { fail, normalizeError, ok, } from "../shared/protocol.js";
|
|
10
14
|
function readBody(req) {
|
|
11
15
|
return new Promise((resolve, reject) => {
|
|
12
16
|
let text = "";
|
|
13
17
|
req.setEncoding("utf8");
|
|
14
18
|
req.on("data", (chunk) => {
|
|
19
|
+
if (text.length + chunk.length > (req.url?.startsWith("/processing/") ? 1024 * 1024 : 128 * 1024 * 1024)) {
|
|
20
|
+
reject(new Error("Request body exceeds limit"));
|
|
21
|
+
req.destroy();
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
15
24
|
text += chunk;
|
|
16
25
|
});
|
|
17
26
|
req.on("end", () => {
|
|
@@ -43,17 +52,40 @@ export function createDaemonServer(options) {
|
|
|
43
52
|
const host = options.host || DEFAULT_HOST;
|
|
44
53
|
const port = Number(options.port ?? DEFAULT_PORT);
|
|
45
54
|
let actualPort = port;
|
|
55
|
+
let restartRequested = false;
|
|
56
|
+
const configDir = options.frontendRootDir || CONFIG_DIR;
|
|
46
57
|
const registry = new TargetRegistry();
|
|
47
58
|
const pending = new Map();
|
|
59
|
+
const autoReloadedClients = new Set();
|
|
48
60
|
const log = options.log || (() => undefined);
|
|
49
61
|
const frontend = createFrontendServer({
|
|
50
62
|
host,
|
|
51
63
|
port: options.frontendPort ?? DEFAULT_FRONTEND_PORT,
|
|
52
|
-
rootDir:
|
|
64
|
+
rootDir: configDir,
|
|
53
65
|
});
|
|
54
66
|
function requireToken(req) {
|
|
55
67
|
return req.headers["x-pcbridge-token"] === options.token;
|
|
56
68
|
}
|
|
69
|
+
function connectedExtensionVersions() {
|
|
70
|
+
return [
|
|
71
|
+
...new Set(registry
|
|
72
|
+
.list()
|
|
73
|
+
.filter((item) => item.connected && typeof item.extensionVersion === "string" && item.extensionVersion)
|
|
74
|
+
.map((item) => item.extensionVersion)),
|
|
75
|
+
];
|
|
76
|
+
}
|
|
77
|
+
async function reloadConnectedExtensions(reloadTabs = true) {
|
|
78
|
+
const connected = registry.list().find((item) => item.connected);
|
|
79
|
+
if (!connected)
|
|
80
|
+
return false;
|
|
81
|
+
const response = await sendToTarget({
|
|
82
|
+
target: connected.id,
|
|
83
|
+
method: "bridge:reloadExtension",
|
|
84
|
+
params: { reloadTabs },
|
|
85
|
+
timeoutMs: 5000,
|
|
86
|
+
});
|
|
87
|
+
return response.ok;
|
|
88
|
+
}
|
|
57
89
|
async function sendToTarget(request) {
|
|
58
90
|
if (!request.method) {
|
|
59
91
|
return fail("INVALID_REQUEST", "RPC method is required.");
|
|
@@ -97,6 +129,7 @@ export function createDaemonServer(options) {
|
|
|
97
129
|
}
|
|
98
130
|
const workspace = new WorkspaceManager({
|
|
99
131
|
rootDir: options.workspaceRoot || process.cwd(),
|
|
132
|
+
settingsPath: join(configDir, "workspace-settings.json"),
|
|
100
133
|
requestTarget: (target, method, params = {}, timeoutMs = 15000) => sendToTarget({
|
|
101
134
|
target,
|
|
102
135
|
method,
|
|
@@ -104,15 +137,24 @@ export function createDaemonServer(options) {
|
|
|
104
137
|
timeoutMs,
|
|
105
138
|
}),
|
|
106
139
|
});
|
|
107
|
-
const
|
|
108
|
-
|
|
140
|
+
const pluginStore = new PluginStore(join(configDir, "plugins"), {
|
|
141
|
+
fetch: options.fetch,
|
|
142
|
+
});
|
|
143
|
+
const plugins = new PluginHost({
|
|
144
|
+
store: pluginStore,
|
|
145
|
+
workspace,
|
|
109
146
|
requestTarget: (target, method, params = {}, timeoutMs = 15000) => sendToTarget({
|
|
110
147
|
target,
|
|
111
148
|
method,
|
|
112
149
|
params,
|
|
113
150
|
timeoutMs,
|
|
114
151
|
}),
|
|
115
|
-
|
|
152
|
+
listTargets: () => registry.list(),
|
|
153
|
+
log,
|
|
154
|
+
});
|
|
155
|
+
const processing = new ProcessingManager(target => {
|
|
156
|
+
const status = workspace.statusForTarget(target);
|
|
157
|
+
return isJsonObject(status) && !status.branchConflict && typeof status.projectDirectory === "string" ? status.projectDirectory : null;
|
|
116
158
|
});
|
|
117
159
|
let resolveClosed;
|
|
118
160
|
const closed = new Promise((resolvePromise) => {
|
|
@@ -159,6 +201,97 @@ export function createDaemonServer(options) {
|
|
|
159
201
|
});
|
|
160
202
|
return;
|
|
161
203
|
}
|
|
204
|
+
if (url.pathname === "/restart" && req.method === "POST") {
|
|
205
|
+
if (!requireToken(req)) {
|
|
206
|
+
writeJson(res, 403, fail("BAD_TOKEN", "Invalid pcbridge token."));
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
restartRequested = true;
|
|
210
|
+
writeJson(res, 200, ok({
|
|
211
|
+
service: "pcbridge-daemon",
|
|
212
|
+
restarting: true,
|
|
213
|
+
version: VERSION,
|
|
214
|
+
workspaceRoot: workspace.rootDir,
|
|
215
|
+
}));
|
|
216
|
+
res.once("finish", () => {
|
|
217
|
+
setImmediate(() => {
|
|
218
|
+
void closeDaemon().catch((error) => {
|
|
219
|
+
log(`daemon restart shutdown failed: ${String(error)}`);
|
|
220
|
+
});
|
|
221
|
+
});
|
|
222
|
+
});
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
if (url.pathname === "/extension/reload" && req.method === "POST") {
|
|
226
|
+
if (!requireToken(req)) {
|
|
227
|
+
writeJson(res, 403, fail("BAD_TOKEN", "Invalid pcbridge token."));
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
const body = await readBody(req);
|
|
231
|
+
const data = isJsonObject(body) ? body : {};
|
|
232
|
+
const reloadTabs = data.reloadTabs !== false;
|
|
233
|
+
const reloaded = await reloadConnectedExtensions(reloadTabs);
|
|
234
|
+
writeJson(res, 200, ok({ reloaded, reloadTabs }));
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
if (url.pathname === "/updates" && req.method === "GET") {
|
|
238
|
+
if (!requireToken(req)) {
|
|
239
|
+
writeJson(res, 403, fail("BAD_TOKEN", "Invalid pcbridge token."));
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
const refresh = url.searchParams.get("refresh") === "1";
|
|
243
|
+
const report = await getUpdateReport({
|
|
244
|
+
refresh,
|
|
245
|
+
allowNetwork: true,
|
|
246
|
+
configDir,
|
|
247
|
+
store: pluginStore,
|
|
248
|
+
daemonVersion: VERSION,
|
|
249
|
+
connectedExtensionVersions: connectedExtensionVersions(),
|
|
250
|
+
fetch: options.fetch,
|
|
251
|
+
});
|
|
252
|
+
writeJson(res, 200, ok(report));
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
if (url.pathname === "/update" && req.method === "POST") {
|
|
256
|
+
if (!requireToken(req)) {
|
|
257
|
+
writeJson(res, 403, fail("BAD_TOKEN", "Invalid pcbridge token."));
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
const body = await readBody(req);
|
|
261
|
+
const data = isJsonObject(body) ? body : {};
|
|
262
|
+
try {
|
|
263
|
+
const result = await executeOneClickUpdate({
|
|
264
|
+
configDir,
|
|
265
|
+
store: pluginStore,
|
|
266
|
+
includePlugins: Boolean(data.includePlugins),
|
|
267
|
+
reloadTabs: data.reloadTabs !== false,
|
|
268
|
+
daemonVersion: VERSION,
|
|
269
|
+
connectedExtensionVersions: connectedExtensionVersions(),
|
|
270
|
+
updatePlugins: () => plugins.updateAll(),
|
|
271
|
+
fetch: options.fetch,
|
|
272
|
+
});
|
|
273
|
+
const shouldRestart = result.packageUpdated || result.version !== VERSION;
|
|
274
|
+
if (shouldRestart) {
|
|
275
|
+
restartRequested = true;
|
|
276
|
+
result.daemonRestartTriggered = true;
|
|
277
|
+
}
|
|
278
|
+
writeJson(res, 200, ok(result));
|
|
279
|
+
if (shouldRestart) {
|
|
280
|
+
res.once("finish", () => {
|
|
281
|
+
setImmediate(() => {
|
|
282
|
+
void closeDaemon().catch((error) => {
|
|
283
|
+
log(`daemon update restart failed: ${String(error)}`);
|
|
284
|
+
});
|
|
285
|
+
});
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
catch (error) {
|
|
290
|
+
const code = error.code;
|
|
291
|
+
writeJson(res, 400, fail(typeof code === "string" ? code : "UPDATE_FAILED", error instanceof Error ? error.message : String(error)));
|
|
292
|
+
}
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
162
295
|
if (url.pathname === "/frontend/status" && req.method === "GET") {
|
|
163
296
|
if (!requireToken(req)) {
|
|
164
297
|
writeJson(res, 403, fail("BAD_TOKEN", "Invalid pcbridge token."));
|
|
@@ -181,7 +314,20 @@ export function createDaemonServer(options) {
|
|
|
181
314
|
return;
|
|
182
315
|
}
|
|
183
316
|
const tabId = Number(url.searchParams.get("tabId"));
|
|
317
|
+
const refresh = url.searchParams.get("refresh") === "1";
|
|
184
318
|
const target = Number.isFinite(tabId) ? registry.getByTabId(tabId) : undefined;
|
|
319
|
+
const [frontendStatus, updates] = await Promise.all([
|
|
320
|
+
frontend.status(),
|
|
321
|
+
getUpdateReport({
|
|
322
|
+
refresh,
|
|
323
|
+
allowNetwork: refresh,
|
|
324
|
+
configDir,
|
|
325
|
+
store: pluginStore,
|
|
326
|
+
daemonVersion: VERSION,
|
|
327
|
+
connectedExtensionVersions: connectedExtensionVersions(),
|
|
328
|
+
fetch: options.fetch,
|
|
329
|
+
}),
|
|
330
|
+
]);
|
|
185
331
|
writeJson(res, 200, ok({
|
|
186
332
|
daemon: {
|
|
187
333
|
version: VERSION,
|
|
@@ -189,13 +335,42 @@ export function createDaemonServer(options) {
|
|
|
189
335
|
port: actualPort,
|
|
190
336
|
workspaceRoot: workspace.rootDir,
|
|
191
337
|
targetCount: registry.list().filter((item) => item.connected).length,
|
|
338
|
+
eagerMaxMb: workspace.getSettings().eagerMaxMb,
|
|
192
339
|
},
|
|
193
340
|
target: target || null,
|
|
194
341
|
workspace: workspace.statusForTarget(target),
|
|
195
|
-
frontend:
|
|
342
|
+
frontend: frontendStatus,
|
|
343
|
+
updates: updates,
|
|
196
344
|
}));
|
|
197
345
|
return;
|
|
198
346
|
}
|
|
347
|
+
if (url.pathname === "/workspace/settings" && req.method === "GET") {
|
|
348
|
+
if (!requireToken(req)) {
|
|
349
|
+
writeJson(res, 403, fail("BAD_TOKEN", "Invalid pcbridge token."));
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
writeJson(res, 200, ok(workspace.getSettings()));
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
if (url.pathname === "/workspace/settings" && req.method === "POST") {
|
|
356
|
+
if (!requireToken(req)) {
|
|
357
|
+
writeJson(res, 403, fail("BAD_TOKEN", "Invalid pcbridge token."));
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
const body = await readBody(req);
|
|
361
|
+
if (!isJsonObject(body) || body.eagerMaxMb === undefined) {
|
|
362
|
+
writeJson(res, 400, fail("INVALID_REQUEST", "workspace settings requires eagerMaxMb (1..10)."));
|
|
363
|
+
return;
|
|
364
|
+
}
|
|
365
|
+
const rawMb = Number(body.eagerMaxMb);
|
|
366
|
+
if (!Number.isFinite(rawMb) || rawMb < 1 || rawMb > 10) {
|
|
367
|
+
writeJson(res, 400, fail("INVALID_REQUEST", "eagerMaxMb must be between 1 and 10."));
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
const updated = await workspace.updateSettings({ eagerMaxMb: rawMb });
|
|
371
|
+
writeJson(res, 200, ok(updated));
|
|
372
|
+
return;
|
|
373
|
+
}
|
|
199
374
|
if (url.pathname === "/workspace/status" && req.method === "GET") {
|
|
200
375
|
if (!requireToken(req)) {
|
|
201
376
|
writeJson(res, 403, fail("BAD_TOKEN", "Invalid pcbridge token."));
|
|
@@ -209,6 +384,30 @@ export function createDaemonServer(options) {
|
|
|
209
384
|
writeJson(res, 200, ok(workspace.statusForTarget(resolved.target.info)));
|
|
210
385
|
return;
|
|
211
386
|
}
|
|
387
|
+
const processingMatch = url.pathname.match(/^\/processing\/(capabilities|begin|chunk|run|status|read|cancel|finish)$/);
|
|
388
|
+
if (processingMatch && req.method === "POST") {
|
|
389
|
+
if (!requireToken(req)) {
|
|
390
|
+
writeJson(res, 403, fail("BAD_TOKEN", "Invalid pcbridge token."));
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
const body = await readBody(req);
|
|
394
|
+
if (!isJsonObject(body)) {
|
|
395
|
+
writeJson(res, 400, fail("INVALID_REQUEST", "Object required"));
|
|
396
|
+
return;
|
|
397
|
+
}
|
|
398
|
+
const target = registry.resolve(typeof body.target === "string" ? body.target : "current");
|
|
399
|
+
if (!target.ok) {
|
|
400
|
+
writeJson(res, 400, fail(target.code, target.message));
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
403
|
+
try {
|
|
404
|
+
writeJson(res, 200, ok(await processing.request(target.target.info, processingMatch[1], body)));
|
|
405
|
+
}
|
|
406
|
+
catch (error) {
|
|
407
|
+
writeJson(res, 400, fail("PROCESSING_ERROR", String(error)));
|
|
408
|
+
}
|
|
409
|
+
return;
|
|
410
|
+
}
|
|
212
411
|
if (url.pathname === "/workspace/sync" && req.method === "POST") {
|
|
213
412
|
if (!requireToken(req)) {
|
|
214
413
|
writeJson(res, 403, fail("BAD_TOKEN", "Invalid pcbridge token."));
|
|
@@ -254,39 +453,70 @@ export function createDaemonServer(options) {
|
|
|
254
453
|
}
|
|
255
454
|
return;
|
|
256
455
|
}
|
|
257
|
-
if (url.pathname === "/
|
|
456
|
+
if (url.pathname === "/plugins" && req.method === "GET") {
|
|
258
457
|
if (!requireToken(req)) {
|
|
259
458
|
writeJson(res, 403, fail("BAD_TOKEN", "Invalid pcbridge token."));
|
|
260
459
|
return;
|
|
261
460
|
}
|
|
262
|
-
const
|
|
263
|
-
|
|
264
|
-
|
|
461
|
+
const refresh = url.searchParams.get("refresh") === "1";
|
|
462
|
+
writeJson(res, 200, ok(await plugins.list({ refresh })));
|
|
463
|
+
return;
|
|
464
|
+
}
|
|
465
|
+
if (url.pathname === "/plugins/update-all" && req.method === "POST") {
|
|
466
|
+
if (!requireToken(req)) {
|
|
467
|
+
writeJson(res, 403, fail("BAD_TOKEN", "Invalid pcbridge token."));
|
|
265
468
|
return;
|
|
266
469
|
}
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
470
|
+
try {
|
|
471
|
+
writeJson(res, 200, ok(await plugins.updateAll()));
|
|
472
|
+
}
|
|
473
|
+
catch (error) {
|
|
474
|
+
const code = error.code;
|
|
475
|
+
writeJson(res, 400, fail(typeof code === "string" ? code : "PLUGIN_ERROR", error instanceof Error ? error.message : String(error)));
|
|
272
476
|
}
|
|
273
|
-
const suffix = typeof body.suffix === "string" ? body.suffix : undefined;
|
|
274
|
-
const prefix = typeof body.prefix === "string" ? body.prefix : undefined;
|
|
275
|
-
writeJson(res, 202, ok(builder.start(resolved.target.info, body.templateAssetId, { suffix, prefix })));
|
|
276
477
|
return;
|
|
277
478
|
}
|
|
278
|
-
const
|
|
279
|
-
if (
|
|
479
|
+
const pluginMatch = url.pathname.match(/^\/plugins\/(?:(add)|([a-z0-9-]+)\/(enable|disable|remove|update|reload|actions\/([\w.-]+)))$/);
|
|
480
|
+
if (pluginMatch && req.method === "POST") {
|
|
280
481
|
if (!requireToken(req)) {
|
|
281
482
|
writeJson(res, 403, fail("BAD_TOKEN", "Invalid pcbridge token."));
|
|
282
483
|
return;
|
|
283
484
|
}
|
|
284
|
-
const
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
485
|
+
const body = await readBody(req);
|
|
486
|
+
const data = isJsonObject(body) ? body : {};
|
|
487
|
+
const [, add, id, operation, action] = pluginMatch;
|
|
488
|
+
try {
|
|
489
|
+
let result;
|
|
490
|
+
if (add) {
|
|
491
|
+
if (typeof data.source !== "string") {
|
|
492
|
+
writeJson(res, 400, fail("INVALID_REQUEST", "plugin add requires source."));
|
|
493
|
+
return;
|
|
494
|
+
}
|
|
495
|
+
result = await plugins.add(parsePluginSource(data.source, data.link === true));
|
|
496
|
+
}
|
|
497
|
+
else if (action) {
|
|
498
|
+
let target = null;
|
|
499
|
+
const resolved = registry.resolve(typeof data.target === "string" ? data.target : "current");
|
|
500
|
+
if (resolved.ok)
|
|
501
|
+
target = resolved.target.info;
|
|
502
|
+
else if (typeof data.target === "string") {
|
|
503
|
+
writeJson(res, 400, fail(resolved.code, resolved.message));
|
|
504
|
+
return;
|
|
505
|
+
}
|
|
506
|
+
result = await plugins.callAction(id, action, isJsonObject(data.params) ? data.params : {}, target);
|
|
507
|
+
}
|
|
508
|
+
else if (operation === "enable" || operation === "disable") {
|
|
509
|
+
result = await plugins.setEnabled(id, operation === "enable");
|
|
510
|
+
}
|
|
511
|
+
else {
|
|
512
|
+
result = await plugins[operation](id);
|
|
513
|
+
}
|
|
514
|
+
writeJson(res, 200, ok(result));
|
|
515
|
+
}
|
|
516
|
+
catch (error) {
|
|
517
|
+
const code = error.code;
|
|
518
|
+
writeJson(res, 400, fail(typeof code === "string" ? code : "PLUGIN_ERROR", error instanceof Error ? error.message : String(error)));
|
|
288
519
|
}
|
|
289
|
-
writeJson(res, 200, ok(job));
|
|
290
520
|
return;
|
|
291
521
|
}
|
|
292
522
|
if (url.pathname === "/rpc" && req.method === "POST") {
|
|
@@ -345,11 +575,45 @@ export function createDaemonServer(options) {
|
|
|
345
575
|
log(`connected ${result.info.id} project=${result.info.projectId || "unknown"} ` +
|
|
346
576
|
`scene=${result.info.sceneId || "unknown"}`);
|
|
347
577
|
}
|
|
578
|
+
if (result.info.extensionVersion &&
|
|
579
|
+
result.info.extensionVersion !== VERSION &&
|
|
580
|
+
!autoReloadedClients.has(`${result.info.clientId}:${VERSION}`)) {
|
|
581
|
+
void readGeneratedExtensionVersion(join(configDir, "extension"))
|
|
582
|
+
.then((diskExtVersion) => {
|
|
583
|
+
if (diskExtVersion === VERSION) {
|
|
584
|
+
autoReloadedClients.add(`${result.info.clientId}:${VERSION}`);
|
|
585
|
+
log(`reloading stale Chrome extension (${result.info.extensionVersion} -> ${VERSION}) for ${result.info.id}`);
|
|
586
|
+
return sendToTarget({
|
|
587
|
+
target: result.info.id,
|
|
588
|
+
method: "bridge:reloadExtension",
|
|
589
|
+
params: { reloadTabs: true },
|
|
590
|
+
timeoutMs: 5000,
|
|
591
|
+
});
|
|
592
|
+
}
|
|
593
|
+
return null;
|
|
594
|
+
})
|
|
595
|
+
.catch(() => undefined);
|
|
596
|
+
}
|
|
348
597
|
if (result.connected || result.changed) {
|
|
349
598
|
void workspace.handleTarget(result.info).catch((error) => {
|
|
350
599
|
log(`workspace initialization failed for ${result.info.id}: ${String(error)}`);
|
|
351
600
|
});
|
|
352
601
|
}
|
|
602
|
+
plugins.handleTarget(result.info);
|
|
603
|
+
return;
|
|
604
|
+
}
|
|
605
|
+
if (message.type === "workspace:delta" && message.delta && typeof message.delta === "object") {
|
|
606
|
+
const selector = Number.isInteger(message.tabId)
|
|
607
|
+
? `tab:${message.tabId}`
|
|
608
|
+
: message.clientId
|
|
609
|
+
? `client:${message.clientId}`
|
|
610
|
+
: "";
|
|
611
|
+
const resolved = selector ? registry.resolve(selector) : null;
|
|
612
|
+
if (resolved && resolved.ok && resolved.target.ws === ws) {
|
|
613
|
+
void workspace.handleDelta(resolved.target.info, message.delta).catch((error) => {
|
|
614
|
+
log(`workspace delta failed for ${resolved.target.info.id}: ${String(error)}`);
|
|
615
|
+
});
|
|
616
|
+
}
|
|
353
617
|
return;
|
|
354
618
|
}
|
|
355
619
|
if (message.type === "response" && message.id) {
|
|
@@ -394,24 +658,52 @@ export function createDaemonServer(options) {
|
|
|
394
658
|
}
|
|
395
659
|
else
|
|
396
660
|
workspace.markDisconnected(info);
|
|
661
|
+
plugins.forgetTarget(info);
|
|
397
662
|
log(`disconnected ${info.id} project=${info.projectId || "unknown"} ` +
|
|
398
663
|
`scene=${info.sceneId || "unknown"}`);
|
|
399
664
|
}
|
|
400
665
|
});
|
|
401
666
|
});
|
|
667
|
+
let updateCheckTimer = null;
|
|
668
|
+
let lastLoggedUpdateHint = null;
|
|
669
|
+
async function runBackgroundUpdateCheck(refresh) {
|
|
670
|
+
try {
|
|
671
|
+
const report = await getUpdateReport({
|
|
672
|
+
refresh,
|
|
673
|
+
allowNetwork: true,
|
|
674
|
+
configDir,
|
|
675
|
+
store: pluginStore,
|
|
676
|
+
daemonVersion: VERSION,
|
|
677
|
+
connectedExtensionVersions: connectedExtensionVersions(),
|
|
678
|
+
fetch: options.fetch,
|
|
679
|
+
});
|
|
680
|
+
const hint = formatHumanUpdateHint(report);
|
|
681
|
+
if (hint && hint !== lastLoggedUpdateHint) {
|
|
682
|
+
lastLoggedUpdateHint = hint;
|
|
683
|
+
log(hint.replace(/^\[pcbridge\]\s*/, ""));
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
catch {
|
|
687
|
+
// Background update checks must never disrupt the daemon.
|
|
688
|
+
}
|
|
689
|
+
}
|
|
402
690
|
function closeDaemon() {
|
|
403
691
|
if (closePromise)
|
|
404
692
|
return closePromise;
|
|
405
693
|
closePromise = (async () => {
|
|
406
694
|
clearInterval(pingTimer);
|
|
695
|
+
if (updateCheckTimer) {
|
|
696
|
+
clearInterval(updateCheckTimer);
|
|
697
|
+
updateCheckTimer = null;
|
|
698
|
+
}
|
|
407
699
|
for (const waiter of pending.values()) {
|
|
408
700
|
clearTimeout(waiter.timer);
|
|
409
701
|
waiter.resolve(fail("DAEMON_STOPPING", "pcbridge daemon is stopping."));
|
|
410
702
|
}
|
|
411
703
|
pending.clear();
|
|
412
|
-
const
|
|
704
|
+
const pluginCleanup = await Promise.allSettled([plugins.close(), processing.close()]);
|
|
413
705
|
const cleanupResults = [
|
|
414
|
-
...
|
|
706
|
+
...pluginCleanup,
|
|
415
707
|
...await Promise.allSettled([
|
|
416
708
|
workspace.close(),
|
|
417
709
|
frontend.close(),
|
|
@@ -448,10 +740,13 @@ export function createDaemonServer(options) {
|
|
|
448
740
|
get port() {
|
|
449
741
|
return actualPort;
|
|
450
742
|
},
|
|
743
|
+
get restartRequested() {
|
|
744
|
+
return restartRequested;
|
|
745
|
+
},
|
|
451
746
|
registry,
|
|
452
747
|
frontend,
|
|
453
748
|
workspace,
|
|
454
|
-
|
|
749
|
+
plugins,
|
|
455
750
|
closed,
|
|
456
751
|
listen: async () => {
|
|
457
752
|
await new Promise((resolveListen, reject) => {
|
|
@@ -470,6 +765,19 @@ export function createDaemonServer(options) {
|
|
|
470
765
|
catch (error) {
|
|
471
766
|
log(`frontend server unavailable: ${String(error)}`);
|
|
472
767
|
}
|
|
768
|
+
try {
|
|
769
|
+
await plugins.start();
|
|
770
|
+
}
|
|
771
|
+
catch (error) {
|
|
772
|
+
log(`plugins unavailable: ${String(error)}`);
|
|
773
|
+
}
|
|
774
|
+
if (options.enableBackgroundUpdateCheck) {
|
|
775
|
+
void runBackgroundUpdateCheck(false);
|
|
776
|
+
updateCheckTimer = setInterval(() => {
|
|
777
|
+
void runBackgroundUpdateCheck(true);
|
|
778
|
+
}, UPDATE_CACHE_TTL_MS);
|
|
779
|
+
updateCheckTimer.unref();
|
|
780
|
+
}
|
|
473
781
|
},
|
|
474
782
|
close: closeDaemon,
|
|
475
783
|
};
|
|
@@ -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,
|