playcanvas-agent-bridge-cli 0.3.0 → 0.5.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.
Binary file
Binary file
Binary file
Binary file
@@ -17,6 +17,10 @@
17
17
  let reconnectTimer = null;
18
18
  let heartbeatTimer = null;
19
19
  let stopped = false;
20
+ let connecting = false;
21
+ let reconnectAttempt = 0;
22
+ let connectionState = "unknown";
23
+ let lastTargetSignature = null;
20
24
  let config = DEFAULT_CONFIG;
21
25
  let tabInfo = {};
22
26
  let extensionVersion = null;
@@ -72,10 +76,12 @@
72
76
 
73
77
  function serializeError(error) {
74
78
  if (error && typeof error === "object" && error.message) {
75
- return {
79
+ const serialized = {
76
80
  code: error.code || "EXTENSION_ERROR",
77
81
  message: String(error.message)
78
82
  };
83
+ if (error.details !== undefined) serialized.details = error.details;
84
+ return serialized;
79
85
  }
80
86
  return { code: "EXTENSION_ERROR", message: String(error) };
81
87
  }
@@ -134,11 +140,68 @@
134
140
  });
135
141
  }
136
142
 
143
+ async function callDaemon(path, method, body) {
144
+ const allowed =
145
+ (path === "/builder/jobs" && method === "POST") ||
146
+ (/^\/builder\/jobs\/[^/]+$/.test(path) && method === "GET");
147
+ if (!allowed) throw new Error("Unsupported pcbridge browser request.");
148
+ const payload = body && typeof body === "object" ? { ...body } : {};
149
+ if (method === "POST" && !payload.target && Number.isInteger(tabInfo.tabId)) {
150
+ payload.target = "tab:" + tabInfo.tabId;
151
+ }
152
+ const response = await requestRuntime({
153
+ type: "pcbridge:daemonRequest",
154
+ path,
155
+ method,
156
+ body: payload
157
+ });
158
+ if (!response) throw new Error("pcbridge extension background service is unavailable.");
159
+ if (!response.ok) {
160
+ const error = new Error(
161
+ (response.error && response.error.message) || "pcbridge daemon request failed."
162
+ );
163
+ if (response.error && response.error.code) error.code = response.error.code;
164
+ if (response.error && response.error.details !== undefined) {
165
+ error.details = response.error.details;
166
+ }
167
+ throw error;
168
+ }
169
+ return response.data;
170
+ }
171
+
172
+ async function handleDaemonRequest(message) {
173
+ try {
174
+ const data = await callDaemon(message.path, message.method || "GET", message.body || {});
175
+ window.postMessage({
176
+ channel: CHANNEL,
177
+ side: "isolated",
178
+ type: "daemon-response",
179
+ id: message.id,
180
+ ok: true,
181
+ data
182
+ }, "*");
183
+ } catch (error) {
184
+ window.postMessage({
185
+ channel: CHANNEL,
186
+ side: "isolated",
187
+ type: "daemon-response",
188
+ id: message.id,
189
+ ok: false,
190
+ error: serializeError(error)
191
+ }, "*");
192
+ }
193
+ }
194
+
137
195
  window.addEventListener("message", (event) => {
138
196
  if (event.source !== window) return;
139
197
  const message = event.data;
140
198
  if (!message || message.channel !== CHANNEL || message.side !== "main") return;
141
199
 
200
+ if (message.type === "daemon-request" && message.id) {
201
+ void handleDaemonRequest(message);
202
+ return;
203
+ }
204
+
142
205
  if (message.type === "ready") {
143
206
  sendTargetUpdate().catch((error) => {
144
207
  if (isExtensionContextInvalidated(error)) stopBridge();
@@ -200,13 +263,26 @@
200
263
  }
201
264
  }
202
265
 
203
- async function sendTargetUpdate() {
266
+ async function sendTargetUpdate(force = false) {
204
267
  if (stopped || !ws || ws.readyState !== WebSocket.OPEN) return;
205
268
  try {
269
+ const target = await describeTarget();
270
+ const signature = JSON.stringify(target);
271
+ if (!force && signature === lastTargetSignature) return;
272
+ if (target.projectId) {
273
+ await requestRuntime({
274
+ type: "pcbridge:rememberProjectContext",
275
+ url: target.url,
276
+ projectId: target.projectId,
277
+ sceneId: target.sceneId
278
+ });
279
+ if (stopped) return;
280
+ }
281
+ lastTargetSignature = signature;
206
282
  ws.send(
207
283
  JSON.stringify({
208
284
  type: "target:update",
209
- target: await describeTarget()
285
+ target
210
286
  })
211
287
  );
212
288
  } catch (error) {
@@ -214,21 +290,53 @@
214
290
  }
215
291
  }
216
292
 
293
+ function setConnectionState(next) {
294
+ if (connectionState === next) return;
295
+ connectionState = next;
296
+ if (next === "connected") {
297
+ console.info("[pcbridge] connected to local daemon");
298
+ } else if (next === "disconnected") {
299
+ console.info("[pcbridge] disconnected from local daemon; waiting to reconnect");
300
+ }
301
+ }
302
+
303
+ async function probeDaemon() {
304
+ const response = await requestRuntime({ type: "pcbridge:probeDaemon" });
305
+ return Boolean(response && response.reachable);
306
+ }
307
+
217
308
  function scheduleReconnect() {
218
309
  if (stopped) return;
219
310
  if (reconnectTimer) return;
311
+ const delays = [1000, 2000, 5000, 10000, 30000];
312
+ const delay = delays[Math.min(reconnectAttempt, delays.length - 1)];
313
+ reconnectAttempt += 1;
220
314
  reconnectTimer = setTimeout(() => {
221
315
  reconnectTimer = null;
222
- connect();
223
- }, 1000);
316
+ void connect();
317
+ }, delay);
224
318
  }
225
319
 
226
- function connect() {
320
+ async function connect() {
227
321
  if (stopped) return;
322
+ if (connecting) return;
228
323
  if (ws && (ws.readyState === WebSocket.OPEN || ws.readyState === WebSocket.CONNECTING)) {
229
324
  return;
230
325
  }
231
326
 
327
+ connecting = true;
328
+ const reachable = await probeDaemon();
329
+ if (stopped) {
330
+ connecting = false;
331
+ return;
332
+ }
333
+ if (!reachable) {
334
+ connecting = false;
335
+ setConnectionState("disconnected");
336
+ scheduleReconnect();
337
+ return;
338
+ }
339
+
232
340
  const url =
233
341
  `ws://${config.host}:${config.port}/extension?` +
234
342
  `token=${encodeURIComponent(config.token || "")}&clientId=${encodeURIComponent(clientId)}`;
@@ -236,18 +344,22 @@
236
344
  try {
237
345
  ws = new WebSocket(url);
238
346
  } catch {
347
+ connecting = false;
239
348
  scheduleReconnect();
240
349
  return;
241
350
  }
351
+ const socket = ws;
352
+ connecting = false;
242
353
 
243
- ws.addEventListener("open", () => {
244
- console.info("[pcbridge] connected to local daemon");
245
- sendTargetUpdate().catch((error) => {
354
+ socket.addEventListener("open", () => {
355
+ reconnectAttempt = 0;
356
+ setConnectionState("connected");
357
+ sendTargetUpdate(true).catch((error) => {
246
358
  if (isExtensionContextInvalidated(error)) stopBridge();
247
359
  });
248
360
  });
249
361
 
250
- ws.addEventListener("message", async (event) => {
362
+ socket.addEventListener("message", async (event) => {
251
363
  let message;
252
364
  try {
253
365
  message = JSON.parse(event.data);
@@ -258,7 +370,16 @@
258
370
  if (!message || message.type !== "request" || !message.id) return;
259
371
 
260
372
  try {
261
- const response = await callMain(message.method, message.params || {}, message.timeoutMs);
373
+ let response;
374
+ if (message.method === "bridge:focusTarget") {
375
+ const data = await requestRuntime({ type: "pcbridge:focusCurrentTab" });
376
+ if (!data || !data.ok) {
377
+ throw new Error((data && data.error) || "Could not focus the PlayCanvas tab.");
378
+ }
379
+ response = { data, meta: {} };
380
+ } else {
381
+ response = await callMain(message.method, message.params || {}, message.timeoutMs);
382
+ }
262
383
  if (!stopped && ws && ws.readyState === WebSocket.OPEN) {
263
384
  ws.send(
264
385
  JSON.stringify({
@@ -288,13 +409,16 @@
288
409
  }
289
410
  });
290
411
 
291
- ws.addEventListener("close", () => {
412
+ socket.addEventListener("close", () => {
413
+ if (ws === socket) ws = null;
414
+ lastTargetSignature = null;
415
+ setConnectionState("disconnected");
292
416
  scheduleReconnect();
293
417
  });
294
418
 
295
- ws.addEventListener("error", () => {
419
+ socket.addEventListener("error", () => {
296
420
  try {
297
- ws.close();
421
+ socket.close();
298
422
  } catch {
299
423
  scheduleReconnect();
300
424
  }
@@ -302,13 +426,21 @@
302
426
  }
303
427
 
304
428
  async function start() {
429
+ const frontendPreference = await requestRuntime({
430
+ type: "pcbridge:applyFrontendPreference",
431
+ url: location.href
432
+ });
433
+ if (frontendPreference && frontendPreference.redirected) {
434
+ stopBridge();
435
+ return;
436
+ }
305
437
  config = await loadConfig();
306
438
  if (stopped) return;
307
439
  extensionVersion = getExtensionVersion();
308
440
  if (stopped) return;
309
441
  tabInfo = await getTabInfo();
310
442
  if (stopped) return;
311
- connect();
443
+ void connect();
312
444
  heartbeatTimer = setInterval(() => {
313
445
  sendTargetUpdate().catch((error) => {
314
446
  if (isExtensionContextInvalidated(error)) stopBridge();