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
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
import { readFile, realpath } from "node:fs/promises";
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
import { join, sep } from "node:path";
|
|
4
|
+
import { pathToFileURL } from "node:url";
|
|
5
|
+
import { PluginError, summarizePlugin, } from "./store.js";
|
|
6
|
+
const require = createRequire(import.meta.url);
|
|
7
|
+
function targetId(target) {
|
|
8
|
+
return typeof target === "string" ? target : target.id;
|
|
9
|
+
}
|
|
10
|
+
async function unwrap(response) {
|
|
11
|
+
const envelope = await response;
|
|
12
|
+
if (envelope.ok)
|
|
13
|
+
return envelope.data;
|
|
14
|
+
throw Object.assign(new Error(envelope.error.message), { code: envelope.error.code });
|
|
15
|
+
}
|
|
16
|
+
export class PluginHost {
|
|
17
|
+
#store;
|
|
18
|
+
#workspace;
|
|
19
|
+
#requestTarget;
|
|
20
|
+
#listTargets;
|
|
21
|
+
#log;
|
|
22
|
+
#active = new Map();
|
|
23
|
+
#revision = 0;
|
|
24
|
+
#synced = new Map();
|
|
25
|
+
#pageResults = new Map();
|
|
26
|
+
#queue = Promise.resolve();
|
|
27
|
+
constructor(options) {
|
|
28
|
+
this.#store = options.store;
|
|
29
|
+
this.#workspace = options.workspace;
|
|
30
|
+
this.#requestTarget = options.requestTarget;
|
|
31
|
+
this.#listTargets = options.listTargets;
|
|
32
|
+
this.#log = options.log;
|
|
33
|
+
}
|
|
34
|
+
#serial(operation) {
|
|
35
|
+
const result = this.#queue.then(operation, operation);
|
|
36
|
+
this.#queue = result.catch(() => undefined);
|
|
37
|
+
return result;
|
|
38
|
+
}
|
|
39
|
+
start() {
|
|
40
|
+
return this.#serial(async () => {
|
|
41
|
+
for (const plugin of await this.#store.list()) {
|
|
42
|
+
if (plugin.enabled)
|
|
43
|
+
await this.#activate(plugin);
|
|
44
|
+
}
|
|
45
|
+
await this.#resync();
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
close() {
|
|
49
|
+
return this.#serial(async () => {
|
|
50
|
+
for (const id of [...this.#active.keys()])
|
|
51
|
+
await this.#deactivate(id);
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
async #activate(plugin) {
|
|
55
|
+
const runtime = { handlers: new Map(), disposers: [], pages: {}, error: plugin.error };
|
|
56
|
+
this.#active.set(plugin.id, runtime);
|
|
57
|
+
const manifest = plugin.manifest;
|
|
58
|
+
if (!manifest)
|
|
59
|
+
return;
|
|
60
|
+
const requestTarget = this.#requestTarget;
|
|
61
|
+
const workspace = this.#workspace;
|
|
62
|
+
try {
|
|
63
|
+
const directory = await realpath(plugin.path);
|
|
64
|
+
if (manifest.node) {
|
|
65
|
+
for (const key of Object.keys(require.cache)) {
|
|
66
|
+
if (key.startsWith(`${directory}${sep}`) && !key.includes(`${sep}node_modules${sep}`)) {
|
|
67
|
+
delete require.cache[key];
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
const module = await import(pathToFileURL(join(directory, manifest.node)).href);
|
|
71
|
+
if (typeof module.activate !== "function") {
|
|
72
|
+
throw new Error(`${manifest.node} must export an activate(plugin) function.`);
|
|
73
|
+
}
|
|
74
|
+
await module.activate({
|
|
75
|
+
id: plugin.id,
|
|
76
|
+
version: manifest.version,
|
|
77
|
+
dir: directory,
|
|
78
|
+
workspace: {
|
|
79
|
+
rootDir: workspace.rootDir,
|
|
80
|
+
status: (target) => workspace.statusForTarget(target),
|
|
81
|
+
prepareAssetFiles: (target, assetIds) => workspace.prepareAssetFiles(target, assetIds),
|
|
82
|
+
prepareAssetResources: (target, requests) => workspace.prepareAssetResources(target, requests),
|
|
83
|
+
},
|
|
84
|
+
targets: () => this.#listTargets().filter((target) => target.connected),
|
|
85
|
+
handle: (name, handler) => { runtime.handlers.set(name, handler); },
|
|
86
|
+
onDispose: (dispose) => { runtime.disposers.push(dispose); },
|
|
87
|
+
callPage: (target, name, params = {}, timeoutMs = 15000) => unwrap(requestTarget(targetId(target), `plugin:${plugin.id}:${name}`, params, timeoutMs)),
|
|
88
|
+
rpc: (target, method, params = {}, timeoutMs = 15000) => unwrap(requestTarget(targetId(target), method, params, timeoutMs)),
|
|
89
|
+
log: (message) => this.#log(`[plugin ${plugin.id}] ${message}`),
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
for (const kind of ["editor", "launch"]) {
|
|
93
|
+
const file = manifest.page?.[kind];
|
|
94
|
+
if (file) {
|
|
95
|
+
runtime.pages[kind] = {
|
|
96
|
+
version: manifest.version,
|
|
97
|
+
code: await readFile(join(directory, file), "utf8"),
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
catch (error) {
|
|
103
|
+
await this.#dispose(plugin.id, runtime);
|
|
104
|
+
runtime.pages = {};
|
|
105
|
+
runtime.error = error instanceof Error ? error.message : String(error);
|
|
106
|
+
this.#log(`plugin ${plugin.id} failed to activate: ${runtime.error}`);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
async #dispose(id, runtime) {
|
|
110
|
+
runtime.handlers.clear();
|
|
111
|
+
for (const dispose of runtime.disposers.splice(0).reverse()) {
|
|
112
|
+
try {
|
|
113
|
+
await dispose();
|
|
114
|
+
}
|
|
115
|
+
catch (error) {
|
|
116
|
+
this.#log(`plugin ${id} dispose failed: ${String(error)}`);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
async #deactivate(id) {
|
|
121
|
+
const runtime = this.#active.get(id);
|
|
122
|
+
if (!runtime)
|
|
123
|
+
return;
|
|
124
|
+
this.#active.delete(id);
|
|
125
|
+
await this.#dispose(id, runtime);
|
|
126
|
+
}
|
|
127
|
+
async #restart(id) {
|
|
128
|
+
await this.#deactivate(id);
|
|
129
|
+
const plugin = await this.#store.get(id);
|
|
130
|
+
if (plugin.enabled)
|
|
131
|
+
await this.#activate(plugin);
|
|
132
|
+
await this.#resync();
|
|
133
|
+
}
|
|
134
|
+
async #syncTarget(info) {
|
|
135
|
+
if (!info.connected || !info.ready || (info.kind !== "editor" && info.kind !== "launch"))
|
|
136
|
+
return;
|
|
137
|
+
const signature = `${info.clientId}:${this.#revision}`;
|
|
138
|
+
if (this.#synced.get(info.id) === signature)
|
|
139
|
+
return;
|
|
140
|
+
this.#synced.set(info.id, signature);
|
|
141
|
+
const kind = info.kind;
|
|
142
|
+
const plugins = [...this.#active].flatMap(([id, runtime]) => {
|
|
143
|
+
const page = runtime.pages[kind];
|
|
144
|
+
return page ? [{ id, version: page.version, code: page.code }] : [];
|
|
145
|
+
});
|
|
146
|
+
const response = await this.#requestTarget(info.id, "bridge:syncPlugins", { plugins }, 20000);
|
|
147
|
+
if (this.#synced.get(info.id) !== signature)
|
|
148
|
+
return;
|
|
149
|
+
this.#pageResults.set(info.id, response.ok
|
|
150
|
+
? response.data.plugins
|
|
151
|
+
: plugins.map((plugin) => ({ id: plugin.id, ok: false, error: response.error.message })));
|
|
152
|
+
}
|
|
153
|
+
async #resync() {
|
|
154
|
+
this.#revision += 1;
|
|
155
|
+
await Promise.all(this.#listTargets().map((info) => this.#syncTarget(info)));
|
|
156
|
+
}
|
|
157
|
+
handleTarget(info) {
|
|
158
|
+
void this.#syncTarget(info);
|
|
159
|
+
}
|
|
160
|
+
forgetTarget(info) {
|
|
161
|
+
this.#synced.delete(info.id);
|
|
162
|
+
this.#pageResults.delete(info.id);
|
|
163
|
+
}
|
|
164
|
+
#status(plugin, catalog) {
|
|
165
|
+
const summary = summarizePlugin(plugin, catalog);
|
|
166
|
+
const runtime = this.#active.get(plugin.id);
|
|
167
|
+
const pages = [];
|
|
168
|
+
for (const [target, results] of this.#pageResults) {
|
|
169
|
+
const result = results.find((item) => item.id === plugin.id);
|
|
170
|
+
if (result)
|
|
171
|
+
pages.push({ target, ok: result.ok, error: result.error || null });
|
|
172
|
+
}
|
|
173
|
+
if (!runtime)
|
|
174
|
+
return { ...summary, pages };
|
|
175
|
+
return {
|
|
176
|
+
...summary,
|
|
177
|
+
state: runtime.error ? "error" : "active",
|
|
178
|
+
error: runtime.error,
|
|
179
|
+
pages,
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
async list(options = {}) {
|
|
183
|
+
const [installed, catalog, available] = await Promise.all([
|
|
184
|
+
this.#store.list(),
|
|
185
|
+
this.#store.catalog(options),
|
|
186
|
+
this.#store.available(options),
|
|
187
|
+
]);
|
|
188
|
+
return {
|
|
189
|
+
plugins: installed.map((plugin) => this.#status(plugin, catalog)),
|
|
190
|
+
available,
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
async get(id) {
|
|
194
|
+
const [plugin, catalog] = await Promise.all([
|
|
195
|
+
this.#store.get(id),
|
|
196
|
+
this.#store.catalog(),
|
|
197
|
+
]);
|
|
198
|
+
return this.#status(plugin, catalog);
|
|
199
|
+
}
|
|
200
|
+
add(source) {
|
|
201
|
+
return this.#serial(async () => {
|
|
202
|
+
const plugin = await this.#store.add(source);
|
|
203
|
+
await this.#restart(plugin.id);
|
|
204
|
+
return this.get(plugin.id);
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
remove(id) {
|
|
208
|
+
return this.#serial(async () => {
|
|
209
|
+
await this.#store.remove(id);
|
|
210
|
+
await this.#deactivate(id);
|
|
211
|
+
await this.#resync();
|
|
212
|
+
return { id, removed: true };
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
setEnabled(id, enabled) {
|
|
216
|
+
return this.#serial(async () => {
|
|
217
|
+
await this.#store.setEnabled(id, enabled);
|
|
218
|
+
await this.#restart(id);
|
|
219
|
+
return this.get(id);
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
update(id) {
|
|
223
|
+
return this.#serial(async () => {
|
|
224
|
+
const { previousVersion } = await this.#store.update(id);
|
|
225
|
+
await this.#restart(id);
|
|
226
|
+
return { ...(await this.get(id)), previousVersion };
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
updateAll() {
|
|
230
|
+
return this.#serial(async () => {
|
|
231
|
+
const { updated, skipped } = await this.#store.updateAll();
|
|
232
|
+
const results = [];
|
|
233
|
+
for (const item of updated) {
|
|
234
|
+
await this.#restart(item.id);
|
|
235
|
+
results.push({ ...(await this.get(item.id)), previousVersion: item.previousVersion });
|
|
236
|
+
}
|
|
237
|
+
return { updated: results, skipped };
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
reload(id) {
|
|
241
|
+
return this.#serial(async () => {
|
|
242
|
+
await this.#restart(id);
|
|
243
|
+
return this.get(id);
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
async callAction(id, action, params, target) {
|
|
247
|
+
const runtime = this.#active.get(id);
|
|
248
|
+
if (!runtime) {
|
|
249
|
+
await this.#store.get(id);
|
|
250
|
+
throw new PluginError("PLUGIN_DISABLED", `Plugin ${id} is disabled. Run pcbridge plugin enable ${id}.`);
|
|
251
|
+
}
|
|
252
|
+
if (runtime.error)
|
|
253
|
+
throw new PluginError("PLUGIN_ERROR", runtime.error);
|
|
254
|
+
const handler = runtime.handlers.get(action);
|
|
255
|
+
if (!handler) {
|
|
256
|
+
throw new PluginError("PLUGIN_ACTION_NOT_FOUND", `Plugin ${id} has no action ${action}. Available: ${[...runtime.handlers.keys()].join(", ") || "none"}.`);
|
|
257
|
+
}
|
|
258
|
+
return (await handler(params, { target }) ?? null);
|
|
259
|
+
}
|
|
260
|
+
}
|