@rbbtsn0w/adg 0.3.0 → 0.4.0-beta.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.
@@ -0,0 +1,454 @@
1
+ import { existsSync } from "node:fs";
2
+ import { resolve } from "node:path";
3
+ import { adaptPlugin } from "../commands/adapt.js";
4
+ import { addPlugins } from "../commands/install.js";
5
+ import { validatePlugin } from "../commands/validate.js";
6
+ import { listPlugins } from "../commands/list.js";
7
+ import { importSkills } from "../commands/import.js";
8
+ import { linkPlugins } from "../commands/link.js";
9
+ import { unlinkPlugins } from "../commands/unlink.js";
10
+ import { syncPlugins } from "../commands/sync.js";
11
+ import { removePlugin } from "../commands/remove.js";
12
+ import { migrateLayout } from "../commands/migrate.js";
13
+ import { pluginStatus } from "../commands/status.js";
14
+ import { marketplaceList, marketplaceRemove, marketplaceSync, updatePlugins } from "../commands/marketplace.js";
15
+ import { initScaffold } from "../commands/init.js";
16
+ import { confirmFullInstall, selectComponentsInteractive } from "../commands/select-components.js";
17
+ import { selectPluginsInteractive } from "../commands/select-plugins.js";
18
+ import { selectTargetsInteractive } from "../commands/select-agents.js";
19
+ import { selectScopeInteractive, selectUpdateScopeInteractive } from "../commands/select-scope.js";
20
+ import { globalPluginsDir, projectPluginsDir, lockPath } from "../paths.js";
21
+ import { getAgent } from "../agents/index.js";
22
+ import { ui } from "../render/ui.js";
23
+ import { renderAgentReport, renderMarketplaceList, renderPluginList, renderStatus, renderUpdateReport, } from "../render/plugins.js";
24
+ import { MARKETPLACE_USAGE, PLUGIN_ALIASES, PLUGIN_COMMANDS, SCOPE, fail, parseVerb, renderPluginsHelp, renderVerbHelp, requireSingleTarget, resolveComponents, resolveScopeDir, resolveTargets, scopeInfo, scopeOf, wantsHelp, } from "./index.js";
25
+ /**
26
+ * Resolve which scope(s) `plugins update` should refresh, mirroring
27
+ * `adg skills update`: an explicit --dir is a single ad-hoc location; otherwise
28
+ * --project/--global (or both) win, a terminal prompts for project/global/both,
29
+ * and a non-interactive run defaults to project when a project lock exists.
30
+ */
31
+ async function resolveUpdateScopes(values) {
32
+ if (typeof values.dir === "string") {
33
+ const dir = resolve(values.dir);
34
+ return [{ dir, agentScope: "project", label: dir }];
35
+ }
36
+ const project = { dir: projectPluginsDir(), agentScope: "project", label: "project" };
37
+ const global = { dir: globalPluginsDir(), agentScope: "user", label: "global" };
38
+ let choice;
39
+ if (values.global && values.project)
40
+ choice = "both";
41
+ else if (values.global)
42
+ choice = "global";
43
+ else if (values.project)
44
+ choice = "project";
45
+ else if (process.stdin.isTTY)
46
+ choice = await selectUpdateScopeInteractive();
47
+ else
48
+ choice = existsSync(lockPath(projectPluginsDir())) ? "project" : "global";
49
+ if (choice === "both") {
50
+ return [{ ...project, heading: "Project plugins" }, { ...global, heading: "Global plugins" }];
51
+ }
52
+ return [choice === "global" ? global : project];
53
+ }
54
+ /** Render an update/upgrade result: per-source changes, then per-agent re-sync. */
55
+ function printUpdateReport(result) {
56
+ for (const line of renderUpdateReport(result))
57
+ console.log(line);
58
+ for (const line of renderAgentReport(result.agents, "re-synced"))
59
+ console.log(line);
60
+ }
61
+ /**
62
+ * Top-level dispatcher for `adg plugins <verb>`: render help, resolve aliases,
63
+ * look the verb up in the command table, then hand off to the per-verb handler
64
+ * (or the marketplace sub-dispatcher). Kept free of `spawnSync` so the whole
65
+ * dispatch surface is unit-testable.
66
+ */
67
+ export async function runPlugins(rawVerb, rest) {
68
+ // `adg plugins` (no verb) or an explicit help request → the L1 overview.
69
+ if (rawVerb === undefined || rawVerb === "-h" || rawVerb === "--help" || rawVerb === "help") {
70
+ console.log(renderPluginsHelp());
71
+ return;
72
+ }
73
+ const verb = PLUGIN_ALIASES[rawVerb] ?? rawVerb;
74
+ const cmd = PLUGIN_COMMANDS[verb];
75
+ if (!cmd) {
76
+ console.error(`${ui.err("error:")} unknown plugins subcommand: ${rawVerb}\n`);
77
+ console.error(renderPluginsHelp());
78
+ process.exit(1);
79
+ }
80
+ // `adg plugins <verb> -h` → just this command's help. (marketplace handles
81
+ // its own sub-help, so let it through.)
82
+ if (!cmd.delegated && wantsHelp(rest)) {
83
+ console.log(renderVerbHelp(verb));
84
+ return;
85
+ }
86
+ if (verb === "marketplace")
87
+ return runMarketplace(rest);
88
+ return runPluginsVerb(verb, rest, cmd);
89
+ }
90
+ async function runPluginsVerb(verb, rest, cmd) {
91
+ switch (verb) {
92
+ case "init": {
93
+ const { values, positionals } = parseVerb(verb, cmd.flags, rest);
94
+ const name = positionals[0];
95
+ if (!name)
96
+ fail("plugins init requires a <name>");
97
+ const dir = values.dir ? resolve(values.dir) : resolve(process.cwd(), "plugins");
98
+ const type = (values.type ?? "plugin");
99
+ if (type !== "plugin" && type !== "marketplace" && type !== "all") {
100
+ fail(`invalid --type "${values.type}" (expected plugin|marketplace|all)`);
101
+ }
102
+ const res = initScaffold({ name, dir, type, description: values.description, author: values.author, skill: values.skill?.[0] });
103
+ console.log(`${ui.ok(`created ${type}`)} at ${ui.name(res.pluginDir)}`);
104
+ for (const f of res.created)
105
+ console.log(ui.meta(` + ${f}`));
106
+ return;
107
+ }
108
+ case "adapt": {
109
+ const { values, positionals } = parseVerb(verb, cmd.flags, rest);
110
+ const pluginDir = resolve(positionals[0] ?? process.cwd());
111
+ for (const r of adaptPlugin(pluginDir, resolveTargets(values.target))) {
112
+ console.log(`${ui.ok("adapted")} ${ui.name(r.target)} ${ui.meta(`-> ${r.file}`)}`);
113
+ }
114
+ return;
115
+ }
116
+ case "validate": {
117
+ const { positionals } = parseVerb(verb, cmd.flags, rest);
118
+ const pluginDir = resolve(positionals[0] ?? process.cwd());
119
+ const res = validatePlugin(pluginDir);
120
+ if (res.ok) {
121
+ console.log(`${ui.ok("ok:")} ${ui.name(pluginDir)} is a valid ADG plugin`);
122
+ }
123
+ else {
124
+ console.error(`${ui.err("invalid:")} ${ui.name(pluginDir)}`);
125
+ for (const i of res.issues)
126
+ console.error(ui.warn(` - ${i}`));
127
+ process.exit(1);
128
+ }
129
+ return;
130
+ }
131
+ case "add": {
132
+ const { values, positionals } = parseVerb(verb, cmd.flags, rest);
133
+ const spec = positionals[0];
134
+ if (!spec)
135
+ fail("plugins add requires a <plugin-dir | owner/repo[@ref] | github-url>");
136
+ const tty = process.stdin.isTTY;
137
+ // Scope: honor an explicit --dir/--global/--project, else ask in a terminal
138
+ // (defaulting to project non-interactively).
139
+ let global = Boolean(values.global);
140
+ if (!values.dir && values.global === undefined && values.project === undefined && tty) {
141
+ global = await selectScopeInteractive();
142
+ }
143
+ const pluginsDir = values.dir
144
+ ? resolve(values.dir)
145
+ : global
146
+ ? globalPluginsDir()
147
+ : projectPluginsDir();
148
+ // A source may hold one plugin or a whole marketplace. addPlugins discovers
149
+ // all of them; in a terminal the user picks scope, then plugins, then agents,
150
+ // then (unless --only/--skill narrow it) chooses what to install per plugin.
151
+ // --all / --plugin / --path / --only / --skill narrow non-interactively.
152
+ const only = resolveComponents(values.only);
153
+ const skillsSubset = values.skill && values.skill.length > 0 ? values.skill : undefined;
154
+ const narrowed = only !== undefined || skillsSubset !== undefined;
155
+ const { order, installed, converted, agents } = await addPlugins({
156
+ spec,
157
+ pluginsDir,
158
+ ref: values.ref,
159
+ sparse: values.sparse,
160
+ path: values.path,
161
+ all: values.all,
162
+ plugins: values.plugin,
163
+ only,
164
+ skillsSubset,
165
+ withDeps: !values["no-deps"],
166
+ marketplaceName: values["marketplace-name"],
167
+ targets: values.target !== undefined ? resolveTargets(values.target) : undefined,
168
+ selectPlugins: tty ? selectPluginsInteractive : undefined,
169
+ selectTargets: tty && values.target === undefined ? selectTargetsInteractive : undefined,
170
+ confirmFull: tty && !narrowed ? confirmFullInstall : undefined,
171
+ selectComponents: tty && !narrowed ? selectComponentsInteractive : undefined,
172
+ // Make the install actually usable in the chosen agents, not just stored.
173
+ activate: true,
174
+ scope: global ? "user" : "project",
175
+ });
176
+ for (const name of converted)
177
+ console.log(ui.meta(`converted native manifest -> .agents/.plugin.json: ${name}`));
178
+ if (order.length > 1)
179
+ console.log(ui.meta(`install order: ${order.join(" -> ")}`));
180
+ for (const res of installed) {
181
+ console.log(`${ui.ok("added")} ${ui.name(res.name)} ${ui.meta(`-> ${res.installedTo}`)}`);
182
+ console.log(ui.meta(` folderHash: ${res.folderHash}`));
183
+ for (const f of res.adapted)
184
+ console.log(ui.meta(` adapted: ${f}`));
185
+ }
186
+ for (const line of renderAgentReport(agents, "enabled"))
187
+ console.log(line);
188
+ return;
189
+ }
190
+ case "import-skills": {
191
+ const { values, positionals } = parseVerb(verb, cmd.flags, rest);
192
+ const dir = positionals[0];
193
+ if (!dir)
194
+ fail("plugins import-skills requires a <skills-dir>");
195
+ if (!values.as)
196
+ fail("plugins import-skills requires --as <plugin-name>");
197
+ const res = importSkills({
198
+ skillsDir: resolve(dir),
199
+ as: values.as,
200
+ prefix: values.prefix,
201
+ pluginsDir: resolveScopeDir(values),
202
+ description: values.description,
203
+ });
204
+ console.log(`${ui.ok("imported skills into")} ${ui.name(res.name)} ${ui.meta(`-> ${res.installedTo}`)}`);
205
+ return;
206
+ }
207
+ case "link": {
208
+ const { values, positionals } = parseVerb(verb, cmd.flags, rest);
209
+ const targets = values.target === "all"
210
+ ? resolveTargets("all")
211
+ : [requireSingleTarget(values.target, "link")];
212
+ const names = positionals.length > 0 ? positionals : undefined;
213
+ for (const target of targets) {
214
+ const res = linkPlugins({ pluginsDir: resolveScopeDir(values), target, global: Boolean(values.global), names });
215
+ for (const a of res.actions) {
216
+ console.log(`${ui.ok("linked")} ${ui.name(a.name)} ${ui.meta(`[${res.target}]`)}${a.linkedTo ? ui.meta(` -> ${a.linkedTo}`) : ""}`);
217
+ for (const f of a.adapted)
218
+ console.log(ui.meta(` adapted: ${f}`));
219
+ }
220
+ if (res.cliSkipped) {
221
+ console.log(ui.warn(`note: \`${target}\` CLI not found — manifests were generated, but nothing was enabled in ${target}.`));
222
+ }
223
+ }
224
+ return;
225
+ }
226
+ case "unlink": {
227
+ const { values, positionals } = parseVerb(verb, cmd.flags, rest);
228
+ const targets = values.target === "all"
229
+ ? resolveTargets("all")
230
+ : [requireSingleTarget(values.target, "unlink")];
231
+ const names = positionals.length > 0 ? positionals : undefined;
232
+ for (const target of targets) {
233
+ const res = unlinkPlugins({ pluginsDir: resolveScopeDir(values), target, global: Boolean(values.global), names });
234
+ for (const name of res.unlinked)
235
+ console.log(`${ui.ok("unlinked")} ${ui.name(name)} ${ui.meta(`[${res.target}]`)}`);
236
+ if (res.cliSkipped) {
237
+ console.log(ui.warn(`note: \`${target}\` CLI not found — nothing was unlinked from ${target}.`));
238
+ }
239
+ else if (res.unlinked.length === 0) {
240
+ if (targets.length === 1) {
241
+ console.log(ui.meta(`nothing to unlink from ${target}`));
242
+ }
243
+ }
244
+ }
245
+ return;
246
+ }
247
+ case "sync": {
248
+ const { values, positionals } = parseVerb(verb, cmd.flags, rest);
249
+ const targets = values.target === "all"
250
+ ? resolveTargets("all")
251
+ : [requireSingleTarget(values.target, "sync")];
252
+ const names = positionals.length > 0 ? positionals : undefined;
253
+ for (const target of targets) {
254
+ const res = syncPlugins({ pluginsDir: resolveScopeDir(values), target, global: Boolean(values.global), names });
255
+ for (const a of res.actions) {
256
+ const tail = a.synced ? ui.meta(` -> ${res.target}`) : "";
257
+ console.log(`${ui.ok("synced")} ${ui.name(a.name)} ${ui.meta(`[${res.target}]`)}${tail}`);
258
+ }
259
+ if (res.cliSkipped) {
260
+ console.log(ui.warn(`note: \`${target}\` CLI not found — manifests were regenerated, but nothing was re-synced in ${target}.`));
261
+ }
262
+ }
263
+ return;
264
+ }
265
+ case "update": {
266
+ const { values, positionals } = parseVerb(verb, cmd.flags, rest);
267
+ const source = positionals[0];
268
+ for (const sc of await resolveUpdateScopes(values)) {
269
+ if (sc.heading)
270
+ console.log(`${ui.name(sc.heading)}`);
271
+ try {
272
+ const result = await updatePlugins({
273
+ pluginsDir: sc.dir,
274
+ source,
275
+ all: values.all,
276
+ activate: true,
277
+ agentScope: sc.agentScope,
278
+ scope: { label: sc.label, globalDir: globalPluginsDir() },
279
+ });
280
+ printUpdateReport(result);
281
+ }
282
+ catch (err) {
283
+ console.error(`${ui.err("error:")} ${err instanceof Error ? err.message : String(err)}`);
284
+ }
285
+ }
286
+ return;
287
+ }
288
+ case "remove": {
289
+ const { values, positionals } = parseVerb(verb, cmd.flags, rest);
290
+ const name = positionals[0];
291
+ if (!name)
292
+ fail("plugins remove requires a <name>");
293
+ const res = removePlugin({
294
+ pluginsDir: resolveScopeDir(values),
295
+ name,
296
+ force: values.force,
297
+ deactivate: true,
298
+ scope: scopeOf(values),
299
+ });
300
+ if (res.removedDir)
301
+ console.log(`${ui.ok("removed")} ${ui.name(res.name)} ${ui.meta(`-> ${res.removedDir}`)}`);
302
+ else
303
+ console.log(`${ui.ok("removed")} ${ui.name(res.name)} ${ui.meta("(no directory on disk)")}`);
304
+ for (const link of res.unlinked)
305
+ console.log(ui.meta(` unlinked: ${link}`));
306
+ for (const r of res.agents ?? []) {
307
+ const display = getAgent(r.agent)?.displayName ?? r.agent;
308
+ if (r.affected.length > 0)
309
+ console.log(ui.meta(` disabled in ${display}`));
310
+ // CLI absent → the plugin may still be enabled there; surface the precise
311
+ // repair so a store-deleted plugin can't silently linger in that agent.
312
+ else if (r.skipped) {
313
+ console.log(ui.warn(` ${r.agent} CLI not found — if enabled there, run \`adg plugins unlink --target ${r.agent} ${res.name}\``));
314
+ }
315
+ }
316
+ if (!res.removedFromLock && !res.removedDir) {
317
+ console.log(ui.warn(` ${res.name} was not recorded in the lock`));
318
+ }
319
+ return;
320
+ }
321
+ case "list": {
322
+ const { values } = parseVerb(verb, cmd.flags, rest);
323
+ const pluginsDir = resolveScopeDir(values);
324
+ const plugins = listPlugins(pluginsDir);
325
+ for (const line of renderPluginList(plugins, pluginsDir, { verbose: values.verbose })) {
326
+ console.log(line);
327
+ }
328
+ return;
329
+ }
330
+ case "status": {
331
+ const { values } = parseVerb(verb, cmd.flags, rest);
332
+ const targets = values.target !== undefined ? resolveTargets(values.target) : undefined;
333
+ const statuses = pluginStatus({ pluginsDir: resolveScopeDir(values), scope: scopeOf(values), targets });
334
+ for (const line of renderStatus(statuses))
335
+ console.log(line);
336
+ return;
337
+ }
338
+ case "migrate": {
339
+ const { values } = parseVerb(verb, cmd.flags, rest);
340
+ const res = migrateLayout(resolveScopeDir(values));
341
+ for (const m of res.moved)
342
+ console.log(`${ui.ok("moved")} ${ui.name(m.name)}: ${ui.meta(`${m.from} -> ${m.to}`)}`);
343
+ for (const m of res.missing)
344
+ console.error(ui.warn(` ! missing directory for locked plugin: ${m}`));
345
+ if (res.moved.length === 0)
346
+ console.log(ui.meta(`nothing to migrate (${res.unchanged.length} already in place)`));
347
+ return;
348
+ }
349
+ // A verb present in PLUGIN_COMMANDS but missing here (other than `marketplace`,
350
+ // routed before this point) is a wiring bug, not user error — fail loudly
351
+ // rather than silently no-op.
352
+ default:
353
+ fail(`unhandled plugins verb: ${verb}`);
354
+ }
355
+ }
356
+ async function runMarketplace(args) {
357
+ const [sub, ...rest] = args;
358
+ // `marketplace <sub> -h` → the marketplace help (it documents every sub + flags).
359
+ if (sub !== undefined && wantsHelp(rest)) {
360
+ console.log(MARKETPLACE_USAGE);
361
+ return;
362
+ }
363
+ switch (sub) {
364
+ case undefined:
365
+ case "-h":
366
+ case "--help":
367
+ case "help":
368
+ console.log(MARKETPLACE_USAGE);
369
+ return;
370
+ case "list": {
371
+ const { values } = parseVerb("marketplace", ["verbose", ...SCOPE], rest);
372
+ const dir = resolveScopeDir(values);
373
+ const groups = marketplaceList({ pluginsDir: dir });
374
+ // Verbose: drill each plugin down to its components (reuses `plugins list -v`).
375
+ const byName = values.verbose ? new Map(listPlugins(dir).map((p) => [p.name, p])) : undefined;
376
+ for (const line of renderMarketplaceList(groups, byName))
377
+ console.log(line);
378
+ return;
379
+ }
380
+ case "upgrade": {
381
+ // Deprecated: a thin alias for `adg plugins update`. It runs the exact same
382
+ // update path (re-fetch + refresh, with the richer changed/unchanged/deleted
383
+ // report) so there is no second implementation to drift.
384
+ console.error(ui.warn("note: `marketplace upgrade` is a deprecated alias for `adg plugins update`."));
385
+ const { values, positionals } = parseVerb("marketplace", ["all", "target", ...SCOPE], rest);
386
+ // Mirror `plugins update`'s error handling: report a failed re-fetch and
387
+ // exit cleanly rather than throwing to the top-level catch, so the two
388
+ // alias paths behave identically.
389
+ try {
390
+ const result = await updatePlugins({
391
+ pluginsDir: resolveScopeDir(values),
392
+ scope: scopeInfo(values),
393
+ activate: true,
394
+ agentScope: scopeOf(values),
395
+ source: positionals[0],
396
+ all: values.all,
397
+ targets: values.target !== undefined ? resolveTargets(values.target) : undefined,
398
+ });
399
+ printUpdateReport(result);
400
+ }
401
+ catch (err) {
402
+ console.error(`${ui.err("error:")} ${err instanceof Error ? err.message : String(err)}`);
403
+ }
404
+ return;
405
+ }
406
+ case "remove":
407
+ case "rm": {
408
+ const { values, positionals } = parseVerb("marketplace", ["force", ...SCOPE], rest);
409
+ const source = positionals[0];
410
+ if (!source)
411
+ fail("marketplace remove requires a <source>");
412
+ const res = marketplaceRemove({
413
+ pluginsDir: resolveScopeDir(values),
414
+ scope: scopeInfo(values),
415
+ agentScope: scopeOf(values),
416
+ source,
417
+ force: values.force,
418
+ deactivate: true,
419
+ });
420
+ console.log(`${ui.ok("removed")} ${res.removed.length} plugin(s) from ${ui.name(res.source)}: ${res.removed.join(", ")}`);
421
+ return;
422
+ }
423
+ case "sync": {
424
+ const { values, positionals } = parseVerb("marketplace", ["target", ...SCOPE], rest);
425
+ const source = positionals[0];
426
+ if (!source)
427
+ fail("marketplace sync requires a <source>");
428
+ const targets = values.target === "all"
429
+ ? resolveTargets("all")
430
+ : [requireSingleTarget(values.target, "marketplace sync")];
431
+ for (const target of targets) {
432
+ const res = marketplaceSync({
433
+ pluginsDir: resolveScopeDir(values),
434
+ scope: scopeInfo(values),
435
+ source,
436
+ target,
437
+ global: Boolean(values.global),
438
+ });
439
+ for (const a of res.actions) {
440
+ console.log(`${ui.ok("synced")} ${ui.name(a.name)} ${ui.meta(`[${res.target}]`)}${a.synced ? ui.meta(` -> ${res.target}`) : ""}`);
441
+ }
442
+ if (res.cliSkipped) {
443
+ console.log(ui.warn(`note: \`${target}\` CLI not found — manifests were regenerated, but nothing was re-synced in ${target}.`));
444
+ }
445
+ }
446
+ return;
447
+ }
448
+ default: {
449
+ console.error(`${ui.err("error:")} unknown marketplace subcommand: ${sub}\n`);
450
+ console.error(MARKETPLACE_USAGE);
451
+ process.exit(1);
452
+ }
453
+ }
454
+ }