playcanvas-agent-bridge-cli 0.2.2 → 0.4.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 +218 -52
- package/README.zh-CN.md +210 -51
- package/dist/cli.js +723 -92
- package/dist/config.js +6 -2
- package/dist/daemon/server.js +181 -19
- package/dist/daemon/target-registry.js +61 -3
- package/dist/frontend/server.js +191 -0
- package/dist/frontend/store.js +369 -0
- package/dist/shared/protocol.js +3 -1
- package/dist/snippets.js +764 -30
- package/dist/workspace/manager.js +536 -0
- package/extension/icons/concepts/pcbridge-bridge.png +0 -0
- package/extension/icons/concepts/pcbridge-cube.png +0 -0
- package/extension/icons/concepts/pcbridge-p-active.png +0 -0
- package/extension/icons/concepts/pcbridge-p-arch.png +0 -0
- package/extension/icons/concepts/pcbridge-p-bridge.png +0 -0
- package/extension/icons/concepts/pcbridge-p-emblem.png +0 -0
- package/extension/icons/concepts/pcbridge-signal.png +0 -0
- package/extension/icons/icon-128.png +0 -0
- package/extension/icons/icon-16.png +0 -0
- package/extension/icons/icon-32.png +0 -0
- package/extension/icons/icon-48.png +0 -0
- package/extension/isolated.js +214 -43
- package/extension/main.js +619 -32
- package/extension/manifest.json +29 -7
- package/extension/popup.html +188 -24
- package/extension/popup.js +199 -10
- package/extension/service-worker.js +268 -17
- package/package.json +4 -1
- package/skills/claude/playcanvas-agent-bridge-cli/SKILL.md +97 -24
- package/skills/codex/playcanvas-agent-bridge-cli/SKILL.md +167 -52
- package/skills/codex/playcanvas-agent-bridge-cli/agents/openai.yaml +2 -2
- package/skills/cursor/playcanvas-agent-bridge-cli.mdc +63 -23
- package/skills/windsurf/playcanvas-agent-bridge-cli.md +61 -23
package/extension/isolated.js
CHANGED
|
@@ -15,10 +15,60 @@
|
|
|
15
15
|
|
|
16
16
|
let ws = null;
|
|
17
17
|
let reconnectTimer = null;
|
|
18
|
+
let heartbeatTimer = null;
|
|
19
|
+
let stopped = false;
|
|
20
|
+
let connecting = false;
|
|
21
|
+
let reconnectAttempt = 0;
|
|
22
|
+
let connectionState = "unknown";
|
|
23
|
+
let lastTargetSignature = null;
|
|
18
24
|
let config = DEFAULT_CONFIG;
|
|
19
25
|
let tabInfo = {};
|
|
26
|
+
let extensionVersion = null;
|
|
20
27
|
const pending = new Map();
|
|
21
28
|
|
|
29
|
+
function isExtensionContextInvalidated(error) {
|
|
30
|
+
return String((error && error.message) || error)
|
|
31
|
+
.toLowerCase()
|
|
32
|
+
.includes("extension context invalidated");
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function stopBridge() {
|
|
36
|
+
if (stopped) return;
|
|
37
|
+
stopped = true;
|
|
38
|
+
if (reconnectTimer) {
|
|
39
|
+
clearTimeout(reconnectTimer);
|
|
40
|
+
reconnectTimer = null;
|
|
41
|
+
}
|
|
42
|
+
if (heartbeatTimer) {
|
|
43
|
+
clearInterval(heartbeatTimer);
|
|
44
|
+
heartbeatTimer = null;
|
|
45
|
+
}
|
|
46
|
+
for (const waiter of pending.values()) {
|
|
47
|
+
clearTimeout(waiter.timer);
|
|
48
|
+
waiter.reject(new Error("Extension context invalidated."));
|
|
49
|
+
}
|
|
50
|
+
pending.clear();
|
|
51
|
+
if (ws) {
|
|
52
|
+
try {
|
|
53
|
+
ws.close();
|
|
54
|
+
} catch {
|
|
55
|
+
// The extension context is already gone.
|
|
56
|
+
}
|
|
57
|
+
ws = null;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function getExtensionVersion() {
|
|
62
|
+
if (extensionVersion) return extensionVersion;
|
|
63
|
+
try {
|
|
64
|
+
extensionVersion = chrome.runtime.getManifest().version;
|
|
65
|
+
return extensionVersion;
|
|
66
|
+
} catch (error) {
|
|
67
|
+
if (isExtensionContextInvalidated(error)) stopBridge();
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
22
72
|
function requestId() {
|
|
23
73
|
if (crypto && crypto.randomUUID) return crypto.randomUUID();
|
|
24
74
|
return "req-" + Date.now().toString(36) + "-" + Math.random().toString(36).slice(2);
|
|
@@ -26,21 +76,40 @@
|
|
|
26
76
|
|
|
27
77
|
function serializeError(error) {
|
|
28
78
|
if (error && typeof error === "object" && error.message) {
|
|
29
|
-
|
|
79
|
+
const serialized = {
|
|
30
80
|
code: error.code || "EXTENSION_ERROR",
|
|
31
81
|
message: String(error.message)
|
|
32
82
|
};
|
|
83
|
+
if (error.details !== undefined) serialized.details = error.details;
|
|
84
|
+
return serialized;
|
|
33
85
|
}
|
|
34
86
|
return { code: "EXTENSION_ERROR", message: String(error) };
|
|
35
87
|
}
|
|
36
88
|
|
|
37
89
|
function requestRuntime(message) {
|
|
38
90
|
return new Promise((resolve) => {
|
|
91
|
+
if (stopped) {
|
|
92
|
+
resolve(null);
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
39
95
|
try {
|
|
40
96
|
chrome.runtime.sendMessage(message, (response) => {
|
|
97
|
+
try {
|
|
98
|
+
const lastError = chrome.runtime.lastError;
|
|
99
|
+
if (lastError) {
|
|
100
|
+
if (isExtensionContextInvalidated(lastError)) stopBridge();
|
|
101
|
+
resolve(null);
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
} catch (error) {
|
|
105
|
+
if (isExtensionContextInvalidated(error)) stopBridge();
|
|
106
|
+
resolve(null);
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
41
109
|
resolve(response || null);
|
|
42
110
|
});
|
|
43
|
-
} catch {
|
|
111
|
+
} catch (error) {
|
|
112
|
+
if (isExtensionContextInvalidated(error)) stopBridge();
|
|
44
113
|
resolve(null);
|
|
45
114
|
}
|
|
46
115
|
});
|
|
@@ -76,6 +145,13 @@
|
|
|
76
145
|
const message = event.data;
|
|
77
146
|
if (!message || message.channel !== CHANNEL || message.side !== "main") return;
|
|
78
147
|
|
|
148
|
+
if (message.type === "ready") {
|
|
149
|
+
sendTargetUpdate().catch((error) => {
|
|
150
|
+
if (isExtensionContextInvalidated(error)) stopBridge();
|
|
151
|
+
});
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
|
|
79
155
|
if (message.type === "response" && message.id) {
|
|
80
156
|
const waiter = pending.get(message.id);
|
|
81
157
|
if (!waiter) return;
|
|
@@ -111,7 +187,7 @@
|
|
|
111
187
|
clientId,
|
|
112
188
|
tabId: tabInfo.tabId,
|
|
113
189
|
windowId: tabInfo.windowId,
|
|
114
|
-
extensionVersion:
|
|
190
|
+
extensionVersion: getExtensionVersion(),
|
|
115
191
|
...(response.data || {}),
|
|
116
192
|
url: (response.data && response.data.url) || location.href,
|
|
117
193
|
title: (response.data && response.data.title) || document.title || "",
|
|
@@ -122,7 +198,7 @@
|
|
|
122
198
|
clientId,
|
|
123
199
|
tabId: tabInfo.tabId,
|
|
124
200
|
windowId: tabInfo.windowId,
|
|
125
|
-
extensionVersion:
|
|
201
|
+
extensionVersion: getExtensionVersion(),
|
|
126
202
|
url: location.href,
|
|
127
203
|
title: document.title || "",
|
|
128
204
|
ready: false
|
|
@@ -130,29 +206,80 @@
|
|
|
130
206
|
}
|
|
131
207
|
}
|
|
132
208
|
|
|
133
|
-
async function sendTargetUpdate() {
|
|
134
|
-
if (!ws || ws.readyState !== WebSocket.OPEN) return;
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
209
|
+
async function sendTargetUpdate(force = false) {
|
|
210
|
+
if (stopped || !ws || ws.readyState !== WebSocket.OPEN) return;
|
|
211
|
+
try {
|
|
212
|
+
const target = await describeTarget();
|
|
213
|
+
const signature = JSON.stringify(target);
|
|
214
|
+
if (!force && signature === lastTargetSignature) return;
|
|
215
|
+
if (target.projectId) {
|
|
216
|
+
await requestRuntime({
|
|
217
|
+
type: "pcbridge:rememberProjectContext",
|
|
218
|
+
url: target.url,
|
|
219
|
+
projectId: target.projectId,
|
|
220
|
+
sceneId: target.sceneId
|
|
221
|
+
});
|
|
222
|
+
if (stopped) return;
|
|
223
|
+
}
|
|
224
|
+
lastTargetSignature = signature;
|
|
225
|
+
ws.send(
|
|
226
|
+
JSON.stringify({
|
|
227
|
+
type: "target:update",
|
|
228
|
+
target
|
|
229
|
+
})
|
|
230
|
+
);
|
|
231
|
+
} catch (error) {
|
|
232
|
+
if (isExtensionContextInvalidated(error)) stopBridge();
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function setConnectionState(next) {
|
|
237
|
+
if (connectionState === next) return;
|
|
238
|
+
connectionState = next;
|
|
239
|
+
if (next === "connected") {
|
|
240
|
+
console.info("[pcbridge] connected to local daemon");
|
|
241
|
+
} else if (next === "disconnected") {
|
|
242
|
+
console.info("[pcbridge] disconnected from local daemon; waiting to reconnect");
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
async function probeDaemon() {
|
|
247
|
+
const response = await requestRuntime({ type: "pcbridge:probeDaemon" });
|
|
248
|
+
return Boolean(response && response.reachable);
|
|
141
249
|
}
|
|
142
250
|
|
|
143
251
|
function scheduleReconnect() {
|
|
252
|
+
if (stopped) return;
|
|
144
253
|
if (reconnectTimer) return;
|
|
254
|
+
const delays = [1000, 2000, 5000, 10000, 30000];
|
|
255
|
+
const delay = delays[Math.min(reconnectAttempt, delays.length - 1)];
|
|
256
|
+
reconnectAttempt += 1;
|
|
145
257
|
reconnectTimer = setTimeout(() => {
|
|
146
258
|
reconnectTimer = null;
|
|
147
|
-
connect();
|
|
148
|
-
},
|
|
259
|
+
void connect();
|
|
260
|
+
}, delay);
|
|
149
261
|
}
|
|
150
262
|
|
|
151
|
-
function connect() {
|
|
263
|
+
async function connect() {
|
|
264
|
+
if (stopped) return;
|
|
265
|
+
if (connecting) return;
|
|
152
266
|
if (ws && (ws.readyState === WebSocket.OPEN || ws.readyState === WebSocket.CONNECTING)) {
|
|
153
267
|
return;
|
|
154
268
|
}
|
|
155
269
|
|
|
270
|
+
connecting = true;
|
|
271
|
+
const reachable = await probeDaemon();
|
|
272
|
+
if (stopped) {
|
|
273
|
+
connecting = false;
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
if (!reachable) {
|
|
277
|
+
connecting = false;
|
|
278
|
+
setConnectionState("disconnected");
|
|
279
|
+
scheduleReconnect();
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
|
|
156
283
|
const url =
|
|
157
284
|
`ws://${config.host}:${config.port}/extension?` +
|
|
158
285
|
`token=${encodeURIComponent(config.token || "")}&clientId=${encodeURIComponent(clientId)}`;
|
|
@@ -160,16 +287,22 @@
|
|
|
160
287
|
try {
|
|
161
288
|
ws = new WebSocket(url);
|
|
162
289
|
} catch {
|
|
290
|
+
connecting = false;
|
|
163
291
|
scheduleReconnect();
|
|
164
292
|
return;
|
|
165
293
|
}
|
|
294
|
+
const socket = ws;
|
|
295
|
+
connecting = false;
|
|
166
296
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
297
|
+
socket.addEventListener("open", () => {
|
|
298
|
+
reconnectAttempt = 0;
|
|
299
|
+
setConnectionState("connected");
|
|
300
|
+
sendTargetUpdate(true).catch((error) => {
|
|
301
|
+
if (isExtensionContextInvalidated(error)) stopBridge();
|
|
302
|
+
});
|
|
170
303
|
});
|
|
171
304
|
|
|
172
|
-
|
|
305
|
+
socket.addEventListener("message", async (event) => {
|
|
173
306
|
let message;
|
|
174
307
|
try {
|
|
175
308
|
message = JSON.parse(event.data);
|
|
@@ -180,35 +313,55 @@
|
|
|
180
313
|
if (!message || message.type !== "request" || !message.id) return;
|
|
181
314
|
|
|
182
315
|
try {
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
})
|
|
192
|
-
|
|
316
|
+
let response;
|
|
317
|
+
if (message.method === "bridge:focusTarget") {
|
|
318
|
+
const data = await requestRuntime({ type: "pcbridge:focusCurrentTab" });
|
|
319
|
+
if (!data || !data.ok) {
|
|
320
|
+
throw new Error((data && data.error) || "Could not focus the PlayCanvas tab.");
|
|
321
|
+
}
|
|
322
|
+
response = { data, meta: {} };
|
|
323
|
+
} else {
|
|
324
|
+
response = await callMain(message.method, message.params || {}, message.timeoutMs);
|
|
325
|
+
}
|
|
326
|
+
if (!stopped && ws && ws.readyState === WebSocket.OPEN) {
|
|
327
|
+
ws.send(
|
|
328
|
+
JSON.stringify({
|
|
329
|
+
type: "response",
|
|
330
|
+
id: message.id,
|
|
331
|
+
ok: true,
|
|
332
|
+
data: response.data,
|
|
333
|
+
meta: response.meta || {}
|
|
334
|
+
})
|
|
335
|
+
);
|
|
336
|
+
}
|
|
193
337
|
} catch (error) {
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
338
|
+
if (isExtensionContextInvalidated(error)) {
|
|
339
|
+
stopBridge();
|
|
340
|
+
return;
|
|
341
|
+
}
|
|
342
|
+
if (!stopped && ws && ws.readyState === WebSocket.OPEN) {
|
|
343
|
+
ws.send(
|
|
344
|
+
JSON.stringify({
|
|
345
|
+
type: "response",
|
|
346
|
+
id: message.id,
|
|
347
|
+
ok: false,
|
|
348
|
+
error: serializeError(error)
|
|
349
|
+
})
|
|
350
|
+
);
|
|
351
|
+
}
|
|
202
352
|
}
|
|
203
353
|
});
|
|
204
354
|
|
|
205
|
-
|
|
355
|
+
socket.addEventListener("close", () => {
|
|
356
|
+
if (ws === socket) ws = null;
|
|
357
|
+
lastTargetSignature = null;
|
|
358
|
+
setConnectionState("disconnected");
|
|
206
359
|
scheduleReconnect();
|
|
207
360
|
});
|
|
208
361
|
|
|
209
|
-
|
|
362
|
+
socket.addEventListener("error", () => {
|
|
210
363
|
try {
|
|
211
|
-
|
|
364
|
+
socket.close();
|
|
212
365
|
} catch {
|
|
213
366
|
scheduleReconnect();
|
|
214
367
|
}
|
|
@@ -216,11 +369,29 @@
|
|
|
216
369
|
}
|
|
217
370
|
|
|
218
371
|
async function start() {
|
|
372
|
+
const frontendPreference = await requestRuntime({
|
|
373
|
+
type: "pcbridge:applyFrontendPreference",
|
|
374
|
+
url: location.href
|
|
375
|
+
});
|
|
376
|
+
if (frontendPreference && frontendPreference.redirected) {
|
|
377
|
+
stopBridge();
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
219
380
|
config = await loadConfig();
|
|
381
|
+
if (stopped) return;
|
|
382
|
+
extensionVersion = getExtensionVersion();
|
|
383
|
+
if (stopped) return;
|
|
220
384
|
tabInfo = await getTabInfo();
|
|
221
|
-
|
|
222
|
-
|
|
385
|
+
if (stopped) return;
|
|
386
|
+
void connect();
|
|
387
|
+
heartbeatTimer = setInterval(() => {
|
|
388
|
+
sendTargetUpdate().catch((error) => {
|
|
389
|
+
if (isExtensionContextInvalidated(error)) stopBridge();
|
|
390
|
+
});
|
|
391
|
+
}, 2000);
|
|
223
392
|
}
|
|
224
393
|
|
|
225
|
-
start()
|
|
394
|
+
start().catch((error) => {
|
|
395
|
+
if (isExtensionContextInvalidated(error)) stopBridge();
|
|
396
|
+
});
|
|
226
397
|
})();
|