playcanvas-agent-bridge-cli 0.2.1 → 0.2.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.
@@ -15,6 +15,7 @@ export class TargetRegistry {
15
15
  projectId: info.projectId || existing?.projectId,
16
16
  sceneId: info.sceneId || existing?.sceneId,
17
17
  branchId: info.branchId || existing?.branchId,
18
+ extensionVersion: info.extensionVersion || existing?.extensionVersion,
18
19
  ready: Boolean(info.ready),
19
20
  connected: true,
20
21
  lastSeen: now,
package/dist/snippets.js CHANGED
@@ -86,14 +86,6 @@ async function resolveFolder(assets, args) {
86
86
  return { folder: null, created: [] };
87
87
  }
88
88
 
89
- function bytesFromBase64(base64) {
90
- const binary = atob(base64);
91
- const bytes = new Uint8Array(binary.length);
92
- for (let index = 0; index < binary.length; index += 1) {
93
- bytes[index] = binary.charCodeAt(index);
94
- }
95
- return bytes;
96
- }
97
89
  `;
98
90
  export function entityListSnippet() {
99
91
  return `
@@ -108,6 +100,9 @@ if (args.name) {
108
100
  if (args.component) {
109
101
  items = items.filter((entity) => Boolean(entity.get("components." + args.component)));
110
102
  }
103
+ if (args.tag) {
104
+ items = items.filter((entity) => (entity.get("tags") || []).includes(args.tag));
105
+ }
111
106
  const total = items.length;
112
107
  const offset = Math.max(0, Number(args.offset || 0));
113
108
  const limit = Math.max(1, Math.min(Number(args.limit || 50), 500));
@@ -161,6 +156,50 @@ if (!entity) {
161
156
  return readEntity(entity);
162
157
  `;
163
158
  }
159
+ export function entityCreateManySnippet() {
160
+ return `
161
+ ${entityReader}
162
+ const args = command.args || {};
163
+ const entitiesApi = editor.api.globals.entities;
164
+ const definitions = Array.isArray(args.entities) && args.entities.length ? args.entities : [];
165
+ if (!definitions.length) {
166
+ throw new Error("entities must be a non-empty array.");
167
+ }
168
+ const created = [];
169
+ for (const definition of definitions) {
170
+ const data = { ...(definition.entity || definition) };
171
+ const parentId = definition.parent || data.parent;
172
+ if (!parentId) {
173
+ data.parent = entitiesApi.root;
174
+ } else if (typeof parentId === "string") {
175
+ const parent = entitiesApi.get(parentId);
176
+ if (!parent) {
177
+ throw new Error("Parent entity not found: " + parentId);
178
+ }
179
+ data.parent = parent;
180
+ }
181
+ data.position = data.position || [0, 0, 0];
182
+ data.rotation = data.rotation || [0, 0, 0];
183
+ data.scale = data.scale || [1, 1, 1];
184
+ data.enabled = data.enabled !== false;
185
+ data.components = data.components || {};
186
+ data.children = data.children || [];
187
+ data.tags = data.tags || [];
188
+ const entity = entitiesApi.create(data, { history: true, select: false });
189
+ if (!entity) {
190
+ throw new Error("PlayCanvas did not return a created entity.");
191
+ }
192
+ created.push(entity);
193
+ }
194
+ if (created.length) {
195
+ editor.api.globals.selection.set(created, { history: true });
196
+ }
197
+ return {
198
+ affected: created.length,
199
+ entities: created.map(readEntity)
200
+ };
201
+ `;
202
+ }
164
203
  export function entityPatchSnippet() {
165
204
  return `
166
205
  ${entityReader}
@@ -175,6 +214,84 @@ for (const change of args.sets || []) {
175
214
  return readEntity(entity);
176
215
  `;
177
216
  }
217
+ export function entityPatchManySnippet() {
218
+ return `
219
+ ${entityReader}
220
+ const args = command.args || {};
221
+ const entities = editor.api.globals.entities;
222
+ const edits = Array.isArray(args.edits) ? args.edits : [];
223
+ if (!edits.length) {
224
+ throw new Error("edits must be a non-empty array.");
225
+ }
226
+ const changed = new Map();
227
+ const missing = [];
228
+ for (const edit of edits) {
229
+ const entity = entities.get(edit.id);
230
+ if (!entity) {
231
+ missing.push(edit.id);
232
+ continue;
233
+ }
234
+ entity.set(edit.path, edit.value);
235
+ changed.set(edit.id, entity);
236
+ }
237
+ return {
238
+ affected: changed.size,
239
+ entities: Array.from(changed.values()).map(readEntity),
240
+ missing
241
+ };
242
+ `;
243
+ }
244
+ export function entityDuplicateSnippet() {
245
+ return `
246
+ ${entityReader}
247
+ const args = command.args || {};
248
+ const entitiesApi = editor.api.globals.entities;
249
+ const ids = Array.isArray(args.ids) && args.ids.length ? args.ids : [args.id];
250
+ const entities = ids.map((id) => entitiesApi.get(id)).filter(Boolean);
251
+ const missing = ids.filter((id) => !entitiesApi.get(id));
252
+ if (!entities.length) {
253
+ return { affected: 0, duplicated: [], missing };
254
+ }
255
+ const duplicatedResult = await entitiesApi.duplicate(entities);
256
+ const duplicated = Array.isArray(duplicatedResult)
257
+ ? duplicatedResult.filter(Boolean)
258
+ : duplicatedResult
259
+ ? [duplicatedResult]
260
+ : [];
261
+ return {
262
+ affected: duplicated.length,
263
+ duplicated: duplicated.map(readEntity),
264
+ missing
265
+ };
266
+ `;
267
+ }
268
+ export function entityReparentSnippet() {
269
+ return `
270
+ ${entityReader}
271
+ const args = command.args || {};
272
+ const entities = editor.api.globals.entities;
273
+ const entity = entities.get(args.id);
274
+ if (!entity) {
275
+ throw new Error("Entity not found: " + args.id);
276
+ }
277
+ const parent = entities.get(args.parent);
278
+ if (!parent) {
279
+ throw new Error("Parent entity not found: " + args.parent);
280
+ }
281
+ if (typeof entity.reparent === "function") {
282
+ entity.reparent(parent, args.index === undefined || args.index === null ? undefined : Number(args.index), {
283
+ history: true,
284
+ preserveTransform: args.preserveTransform !== false
285
+ });
286
+ } else {
287
+ await entities.reparent([{ entity, parent, index: args.index }], {
288
+ history: true,
289
+ preserveTransform: args.preserveTransform !== false
290
+ });
291
+ }
292
+ return readEntity(entity);
293
+ `;
294
+ }
178
295
  export function entityDeleteSnippet() {
179
296
  return `
180
297
  const args = command.args || {};
@@ -188,16 +305,29 @@ for (const id of ids) {
188
305
  else missing.push(id);
189
306
  }
190
307
  if (!found.length) {
191
- return { affected: 0, deleted: [], missing };
308
+ return { affected: 0, deleted: [], missing, fallback: null };
309
+ }
310
+ const deleted = found.map((entity) => ({
311
+ resource_id: entity.get("resource_id"),
312
+ name: entity.get("name")
313
+ }));
314
+ let fallback = null;
315
+ try {
316
+ await entities.delete(found);
317
+ } catch (error) {
318
+ fallback = error instanceof Error ? error.message : String(error);
319
+ for (const item of deleted) {
320
+ const entity = entities.get(item.resource_id);
321
+ if (entity) {
322
+ entities.remove(entity);
323
+ }
324
+ }
192
325
  }
193
- await entities.delete(found, { history: true });
194
326
  return {
195
327
  affected: found.length,
196
- deleted: found.map((entity) => ({
197
- resource_id: entity.get("resource_id"),
198
- name: entity.get("name")
199
- })),
200
- missing
328
+ deleted,
329
+ missing,
330
+ fallback
201
331
  };
202
332
  `;
203
333
  }
@@ -216,6 +346,31 @@ entity.addComponent(args.component, args.data || {});
216
346
  return readEntity(entity);
217
347
  `;
218
348
  }
349
+ export function entityAddComponentsSnippet() {
350
+ return `
351
+ ${entityReader}
352
+ const args = command.args || {};
353
+ const entity = editor.api.globals.entities.get(args.id);
354
+ if (!entity) {
355
+ throw new Error("Entity not found: " + args.id);
356
+ }
357
+ const components = args.components || {};
358
+ if (!components || typeof components !== "object" || Array.isArray(components)) {
359
+ throw new Error("components must be a JSON object.");
360
+ }
361
+ for (const [name, data] of Object.entries(components)) {
362
+ if (entity.get("components." + name)) {
363
+ entity.set("components." + name, {
364
+ ...(entity.get("components." + name) || {}),
365
+ ...(data || {})
366
+ });
367
+ } else {
368
+ entity.addComponent(name, data || {});
369
+ }
370
+ }
371
+ return readEntity(entity);
372
+ `;
373
+ }
219
374
  export function entityRemoveComponentSnippet() {
220
375
  return `
221
376
  ${entityReader}
@@ -234,6 +389,32 @@ if (entity.get("components." + args.component)) {
234
389
  return { affected: 0, entity: readEntity(entity) };
235
390
  `;
236
391
  }
392
+ export function entityRemoveComponentsSnippet() {
393
+ return `
394
+ ${entityReader}
395
+ const args = command.args || {};
396
+ const entity = editor.api.globals.entities.get(args.id);
397
+ if (!entity) {
398
+ throw new Error("Entity not found: " + args.id);
399
+ }
400
+ const components = Array.isArray(args.components) ? args.components : [];
401
+ if (!components.length) {
402
+ throw new Error("components must be a non-empty array.");
403
+ }
404
+ const removed = [];
405
+ for (const component of components) {
406
+ if (entity.get("components." + component)) {
407
+ entity.removeComponent(component);
408
+ removed.push(component);
409
+ }
410
+ }
411
+ return {
412
+ affected: removed.length,
413
+ removed,
414
+ entity: readEntity(entity)
415
+ };
416
+ `;
417
+ }
237
418
  export function entitySetMaterialSnippet() {
238
419
  return `
239
420
  ${entityReader}
@@ -335,6 +516,9 @@ if (args.name) {
335
516
  const needle = String(args.name).toLowerCase();
336
517
  items = items.filter((asset) => String(asset.get("name") || "").toLowerCase().includes(needle));
337
518
  }
519
+ if (args.tag) {
520
+ items = items.filter((asset) => (asset.get("tags") || []).includes(args.tag));
521
+ }
338
522
  const total = items.length;
339
523
  const offset = Math.max(0, Number(args.offset || 0));
340
524
  const limit = Math.max(1, Math.min(Number(args.limit || 50), 500));
@@ -359,6 +543,68 @@ if (!asset) {
359
543
  return args.full ? asset.json() : readAsset(asset);
360
544
  `;
361
545
  }
546
+ export function assetCreateManySnippet() {
547
+ return `
548
+ ${assetReader}
549
+ const args = command.args || {};
550
+ const assetsApi = editor.api.globals.assets;
551
+ const entitiesApi = editor.api.globals.entities;
552
+ const defs = Array.isArray(args.assets) && args.assets.length ? args.assets : [];
553
+ if (!defs.length) {
554
+ throw new Error("assets must be a non-empty array.");
555
+ }
556
+ const created = [];
557
+ for (const definition of defs) {
558
+ const type = definition.type;
559
+ const options = { ...(definition.options || {}) };
560
+ if (options.folder !== undefined && options.folder !== null && options.folder !== "") {
561
+ const folder = assetsApi.get(Number(options.folder));
562
+ if (!folder || folder.get("type") !== "folder") {
563
+ throw new Error("Folder asset not found: " + options.folder);
564
+ }
565
+ options.folder = folder;
566
+ }
567
+ let templateSourceEntity = null;
568
+ if (type === "template") {
569
+ const entity = entitiesApi.get(options.entity);
570
+ if (!entity) {
571
+ throw new Error("Template source entity not found: " + options.entity);
572
+ }
573
+ templateSourceEntity = entity;
574
+ options.entity = entity;
575
+ }
576
+
577
+ let asset = null;
578
+ if (type === "material" && options.data && options.data.name && !options.name) {
579
+ options.name = options.data.name;
580
+ }
581
+ if (type === "css") asset = await assetsApi.createCss(options);
582
+ else if (type === "folder") asset = await assetsApi.createFolder(options);
583
+ else if (type === "html") asset = await assetsApi.createHtml(options);
584
+ else if (type === "material") asset = await assetsApi.createMaterial(options);
585
+ else if (type === "script") asset = await assetsApi.createScript(options);
586
+ else if (type === "shader") asset = await assetsApi.createShader(options);
587
+ else if (type === "template") {
588
+ asset = await assetsApi.createTemplate(options);
589
+ if (!asset && templateSourceEntity) {
590
+ const templateId = templateSourceEntity.get("template_id");
591
+ asset = templateId ? assetsApi.get(Number(templateId)) : null;
592
+ }
593
+ }
594
+ else if (type === "text") asset = await assetsApi.createText(options);
595
+ else throw new Error("Unsupported asset type: " + type);
596
+
597
+ if (!asset) {
598
+ throw new Error("Failed to create asset of type: " + type);
599
+ }
600
+ created.push(asset);
601
+ }
602
+ return {
603
+ affected: created.length,
604
+ assets: created.map(readAsset)
605
+ };
606
+ `;
607
+ }
362
608
  export function assetFolderEnsureSnippet() {
363
609
  return `
364
610
  ${assetReader}
@@ -375,35 +621,95 @@ return {
375
621
  };
376
622
  `;
377
623
  }
378
- export function assetUploadSnippet() {
624
+ export function assetInstantiateTemplateSnippet() {
379
625
  return `
626
+ ${entityReader}
380
627
  ${assetReader}
381
- ${assetFolderHelpers}
382
628
  const args = command.args || {};
383
- if (!args.base64) {
384
- throw new Error("base64 file content is required.");
629
+ const assetsApi = editor.api.globals.assets;
630
+ const ids = Array.isArray(args.ids) && args.ids.length ? args.ids : [args.id];
631
+ const templates = [];
632
+ const missing = [];
633
+ for (const id of ids) {
634
+ const asset = assetsApi.get(Number(id));
635
+ if (!asset) {
636
+ missing.push(id);
637
+ continue;
638
+ }
639
+ if (asset.get("type") !== "template") {
640
+ throw new Error("Asset is not a template: " + id);
641
+ }
642
+ templates.push(asset);
385
643
  }
386
- if (!args.name) {
387
- throw new Error("name is required.");
644
+ if (!templates.length) {
645
+ return { affected: 0, entities: [], missing };
388
646
  }
389
- if (!args.type) {
390
- throw new Error("type is required.");
647
+ const entities = await assetsApi.instantiateTemplates(templates);
648
+ return {
649
+ affected: entities.length,
650
+ entities: entities.map(readEntity),
651
+ templates: templates.map(readAsset),
652
+ missing
653
+ };
654
+ `;
391
655
  }
392
- const assets = editor.api.globals.assets;
393
- const folderResult = await resolveFolder(assets, args);
394
- const blob = new Blob([bytesFromBase64(args.base64)], {
395
- type: args.mime || "application/octet-stream"
396
- });
397
- const asset = await assets.upload({
398
- name: args.name,
399
- type: args.type,
656
+ export function templateCreateSnippet() {
657
+ return `
658
+ ${entityReader}
659
+ ${assetReader}
660
+ ${assetFolderHelpers}
661
+ const args = command.args || {};
662
+ const assetsApi = editor.api.globals.assets;
663
+ const entitiesApi = editor.api.globals.entities;
664
+ const id = args.entityId || args.id;
665
+ if (!id) {
666
+ throw new Error("entityId is required.");
667
+ }
668
+ const entity = entitiesApi.get(id);
669
+ if (!entity) {
670
+ throw new Error("Template source entity not found: " + id);
671
+ }
672
+ const folderResult = await resolveFolder(assetsApi, args);
673
+ const name = args.name || entity.get("name") || "Template";
674
+ const beforeTemplateIds = new Set(
675
+ assetsApi
676
+ .list()
677
+ .filter((asset) => asset.get("type") === "template")
678
+ .map((asset) => asset.get("id"))
679
+ );
680
+
681
+ let asset = await assetsApi.createTemplate({
682
+ name,
683
+ entity,
400
684
  folder: folderResult.folder || undefined,
401
- filename: args.filename || args.name,
402
- file: blob,
403
685
  preload: args.preload !== false
404
- }, null);
686
+ });
687
+
688
+ if (!asset) {
689
+ const templateId = entity.get("template_id");
690
+ asset = templateId ? assetsApi.get(Number(templateId)) : null;
691
+ }
692
+
693
+ if (!asset) {
694
+ const createdTemplates = assetsApi
695
+ .list()
696
+ .filter((candidate) =>
697
+ candidate.get("type") === "template" &&
698
+ !beforeTemplateIds.has(candidate.get("id")) &&
699
+ candidate.get("name") === name
700
+ );
701
+ asset = createdTemplates[createdTemplates.length - 1] || null;
702
+ }
703
+
704
+ if (!asset) {
705
+ throw new Error("Failed to create template asset.");
706
+ }
707
+
405
708
  return {
406
- asset: readAsset(asset),
709
+ template: readAsset(asset),
710
+ sourceEntity: readEntity(entity),
711
+ templateId: entity.get("template_id") || asset.get("id"),
712
+ templateEntityIds: entity.get("template_ent_ids") || null,
407
713
  createdFolders: folderResult.created.map(readAsset)
408
714
  };
409
715
  `;
@@ -473,6 +779,30 @@ return {
473
779
  };
474
780
  `;
475
781
  }
782
+ export function materialPatchSnippet() {
783
+ return `
784
+ ${assetReader}
785
+ const args = command.args || {};
786
+ const asset = editor.api.globals.assets.get(Number(args.assetId));
787
+ if (!asset) {
788
+ throw new Error("Asset not found: " + args.assetId);
789
+ }
790
+ if (asset.get("type") !== "material") {
791
+ throw new Error("Asset is not a material: " + args.assetId);
792
+ }
793
+ const data = args.data || {};
794
+ for (const [key, value] of Object.entries(data)) {
795
+ asset.set("data." + key, value);
796
+ }
797
+ for (const change of args.sets || []) {
798
+ asset.set("data." + change.path, change.value);
799
+ }
800
+ return {
801
+ asset: readAsset(asset),
802
+ data: asset.get("data") || {}
803
+ };
804
+ `;
805
+ }
476
806
  export function scriptCreateSnippet() {
477
807
  return `
478
808
  ${assetReader}
@@ -555,6 +885,91 @@ return {
555
885
  };
556
886
  `;
557
887
  }
888
+ export function sceneSettingsGetSnippet() {
889
+ return `
890
+ return editor.api.globals.settings.scene.json();
891
+ `;
892
+ }
893
+ export function sceneSettingsPatchSnippet() {
894
+ return `
895
+ const args = command.args || {};
896
+ const settings = args.settings || {};
897
+ const scene = editor.api.globals.settings.scene;
898
+ function iterate(value, prefix) {
899
+ for (const [key, child] of Object.entries(value || {})) {
900
+ const path = prefix ? prefix + "." + key : key;
901
+ if (child && typeof child === "object" && !Array.isArray(child)) {
902
+ iterate(child, path);
903
+ } else {
904
+ scene.set(path, child);
905
+ }
906
+ }
907
+ }
908
+ iterate(settings, "");
909
+ for (const change of args.sets || []) {
910
+ scene.set(change.path, change.value);
911
+ }
912
+ return scene.json();
913
+ `;
914
+ }
915
+ export function storeSearchSnippet() {
916
+ return `
917
+ const args = command.args || {};
918
+ const params = new URLSearchParams();
919
+ if (args.search) params.set("search", String(args.search));
920
+ params.set("regexp", "true");
921
+ if (args.order) params.set("order", args.order === "desc" ? "-1" : "1");
922
+ if (args.skip !== undefined && args.skip !== null) params.set("skip", String(args.skip));
923
+ if (args.limit !== undefined && args.limit !== null) params.set("limit", String(args.limit));
924
+ const response = await fetch("/api/store?" + params.toString());
925
+ const body = await response.json().catch(() => ({}));
926
+ if (!response.ok || body.error) {
927
+ throw new Error(body.error || "Failed to search PlayCanvas store.");
928
+ }
929
+ return body;
930
+ `;
931
+ }
932
+ export function storeGetSnippet() {
933
+ return `
934
+ const args = command.args || {};
935
+ if (!args.id) {
936
+ throw new Error("id is required.");
937
+ }
938
+ const response = await fetch("/api/store/" + encodeURIComponent(args.id));
939
+ const body = await response.json().catch(() => ({}));
940
+ if (!response.ok || body.error) {
941
+ throw new Error(body.error || "Failed to get PlayCanvas store asset.");
942
+ }
943
+ return body;
944
+ `;
945
+ }
946
+ export function storeDownloadSnippet() {
947
+ return `
948
+ const args = command.args || {};
949
+ if (!args.id || !args.name || !args.license) {
950
+ throw new Error("id, name, and license are required.");
951
+ }
952
+ const response = await fetch("/api/store/" + encodeURIComponent(args.id) + "/clone", {
953
+ method: "POST",
954
+ headers: { "Content-Type": "application/json" },
955
+ body: JSON.stringify({
956
+ scope: {
957
+ type: "project",
958
+ id: window.config?.project?.id
959
+ },
960
+ name: args.name,
961
+ store: "playcanvas",
962
+ targetFolderId: args.folderId === undefined ? null : Number(args.folderId),
963
+ license: args.license
964
+ })
965
+ });
966
+ const body = await response.json().catch(() => ({}));
967
+ if (!response.ok || body.error) {
968
+ throw new Error(body.error || "Failed to download PlayCanvas store asset.");
969
+ }
970
+ return body;
971
+ `;
972
+ }
558
973
  export function viewportCaptureSnippet() {
559
974
  return `
560
975
  const args = command.args || {};
@@ -111,6 +111,7 @@
111
111
  clientId,
112
112
  tabId: tabInfo.tabId,
113
113
  windowId: tabInfo.windowId,
114
+ extensionVersion: chrome.runtime.getManifest().version,
114
115
  ...(response.data || {}),
115
116
  url: (response.data && response.data.url) || location.href,
116
117
  title: (response.data && response.data.title) || document.title || "",
@@ -121,6 +122,7 @@
121
122
  clientId,
122
123
  tabId: tabInfo.tabId,
123
124
  windowId: tabInfo.windowId,
125
+ extensionVersion: chrome.runtime.getManifest().version,
124
126
  url: location.href,
125
127
  title: document.title || "",
126
128
  ready: false