akm-cli
Version:
akm (Agent Knowledge Manager) — a portable, local-first capability library for AI agents. Discover, load, share, and improve reusable skills, scripts, workflows, and knowledge across any shell-capable coding agent, including Claude Code, OpenCode, and Cur
215 lines (214 loc) • 8.84 kB
JavaScript
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
/**
* `akm bundle` command group: the bundle-lifecycle commands.
* - `create` (src/commands/sources/init.ts), output shape `bundle-create`
* - `add` (src/commands/sources/add-cli.ts keeps its exported helpers)
* - `list`, `remove`, `update`
* - `show <name>` — a single-bundle detail view of the `list` payload,
* output shape `bundle-show`.
*
* `add`/`list`/`remove`/`update` output the bare `SourceEntry`/`RemoveResponse`/
* `UpdateResponse` shapes.
*/
import { defineGroupCommand, defineJsonCommand, output } from "../../cli/shared.js";
import { renameBundle } from "../../core/bundle-rename.js";
import { NotFoundError, TransientError, UsageError } from "../../core/errors.js";
import { appendEvent } from "../../core/events.js";
import { stderr } from "../../core/stdio.js";
import { warn } from "../../core/warn.js";
import { akmTasksSync } from "../tasks/tasks.js";
import { addCommand } from "./add-cli.js";
import { akmInit } from "./init.js";
import { akmListSources, akmRemove, akmUpdate } from "./installed-stashes.js";
const VALID_SOURCE_KINDS = new Set(["filesystem", "git", "npm", "website", "location"]);
function parseKindFilter(raw) {
if (!raw)
return undefined;
const kinds = raw.split(",").map((s) => s.trim());
for (const k of kinds) {
if (!VALID_SOURCE_KINDS.has(k)) {
throw new UsageError(`Invalid --kind value: "${k}". Expected one of: filesystem, git, npm, website, location`);
}
}
return kinds;
}
const createCommand = defineJsonCommand({
meta: {
name: "create",
description: "Initialize akm's working bundle directory and persist it in config",
},
args: {
dir: { type: "string", description: "Custom bundle directory path (default: ~/akm)" },
"set-default": {
type: "boolean",
description: "Make --dir the default bundle",
default: false,
},
},
async run({ args }) {
const result = await akmInit({
dir: args.dir,
setDefault: args["set-default"],
});
output("bundle-create", result);
},
});
const listCommand = defineJsonCommand({
meta: { name: "list", description: "List configured bundles and their resolved source state" },
args: {
kind: {
type: "string",
description: "Filter by source provider (filesystem, git, npm, website, location). Comma-separated.",
},
},
async run({ args }) {
const kind = parseKindFilter(args.kind);
const result = await akmListSources({ kind });
output("list", result);
},
});
const showCommand = defineJsonCommand({
meta: { name: "show", description: "Show detail for a single configured bundle" },
args: {
name: { type: "positional", description: "Bundle name (as reported by `akm bundle list`)", required: true },
},
async run({ args }) {
const { sources } = await akmListSources();
const entry = sources.find((s) => s.name === args.name);
if (!entry) {
throw new NotFoundError(`No configured bundle named "${args.name}"`, "SOURCE_NOT_FOUND");
}
output("bundle-show", entry);
},
});
const removeCommand = defineJsonCommand({
meta: { name: "remove", description: "Remove a bundle by id, ref, path, URL, or name" },
args: {
target: { type: "positional", description: "Bundle to remove (id, ref, path, URL, or name)", required: true },
yes: { type: "boolean", alias: "y", description: "Skip confirmation prompt", default: false },
},
async run({ args }) {
const { confirmDestructive } = await import("../../cli/confirm.js");
const confirmed = await confirmDestructive(`Remove bundle "${args.target}"? This cannot be undone.`, {
yes: args.yes === true,
});
if (!confirmed) {
stderr().write("Aborted.\n");
return;
}
const result = await akmRemove({ target: args.target });
appendEvent({
eventType: "remove",
metadata: {
target: args.target,
ref: typeof result.removed?.ref === "string" ? result.removed.ref : null,
id: typeof result.removed?.id === "string" ? result.removed.id : null,
},
});
output("remove", result);
},
});
const updateCommand = defineJsonCommand({
meta: {
name: "update",
description: "Refresh one or all configured bundles and reconcile their index. Git refresh is explicit; schedule this command for automatic updates.",
},
args: {
target: { type: "positional", description: "Bundle to update (id or ref)", required: false },
all: { type: "boolean", description: "Update all configured bundles and report each outcome", default: false },
force: { type: "boolean", description: "Force fresh download even if version is unchanged", default: false },
"skip-if-locked": {
type: "boolean",
description: "Exit successfully when index/state database contention shows another akm operation is active",
default: false,
},
// Deprecated no-op, accepted through 0.10 so scripts that pass it do not
// fail with UNKNOWN_FLAG. Installing a bundle is the trust decision: an
// update no longer scans env files for process-hijacking keys.
"allow-dangerous-env-keys": {
type: "boolean",
description: "Deprecated, no effect (accepted through 0.10)",
default: false,
},
// Deprecated no-op, accepted through 0.10 so scripts that pass it do not
// fail with UNKNOWN_FLAG. Pruning old install directories of a read-only
// bundle no longer asks: they are a re-fetchable cache.
yes: {
type: "boolean",
alias: "y",
description: "Deprecated, no effect (accepted through 0.10)",
default: false,
},
},
async run({ args }) {
let result;
try {
result = await akmUpdate({
target: args.target,
all: args.all,
force: args.force,
});
}
catch (error) {
const skippable = isSkippableBundleUpdateLock(error);
if (!args["skip-if-locked"] || !skippable)
throw error;
warn(`[bundle update] ${error.message}; skipping (--skip-if-locked)`);
output("update", { ok: true, skipped: { reason: "lock-held", code: error.code } });
return;
}
appendEvent({
eventType: "update",
metadata: {
target: args.target ?? null,
all: args.all === true,
force: args.force === true,
processed: Array.isArray(result.processed)
? result.processed.length
: 0,
},
});
output("update", result);
},
});
const renameCommand = defineJsonCommand({
meta: {
name: "rename",
description: "Rename a configured bundle's key everywhere akm persists it (config, lock, index, and its own proposals/task history). Content inside the bundle that still spells the old ref is reported, not rewritten.",
},
args: {
old: { type: "positional", description: "Current bundle name", required: true },
new: { type: "positional", description: "New bundle name (must be a legal, unused slug)", required: true },
"dry-run": {
type: "boolean",
description: "Show the rename plan without writing anything",
default: false,
},
},
async run({ args }) {
const result = await renameBundle(args.old, args.new, { dryRun: args["dry-run"] }, {
syncTasks: (newId, backend) => akmTasksSync({ backend }, newId),
});
output("bundle-rename", result);
},
});
export function isSkippableBundleUpdateLock(error) {
return error instanceof TransientError && ["INDEX_DB_CONTENDED", "STATE_DB_CONTENDED"].includes(error.code);
}
export const bundleCommand = defineGroupCommand({
meta: {
name: "bundle",
description: "Create, add to, inspect, and manage your bundles (working bundle + configured sources)",
},
subCommands: {
create: createCommand,
add: addCommand,
list: listCommand,
show: showCommand,
remove: removeCommand,
update: updateCommand,
rename: renameCommand,
},
});