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
59 lines (58 loc) • 3.37 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/.
/**
* Links to assets that were renamed or moved on disk: which assets of a bundle still link to the old ref, with the
* new ref, and the rewrite of exactly those links. Both are `akm lint`'s `missing-ref` pass: the list is its
* findings that carry a new ref, the rewrite is its `fixMovedRefs` fix, so they never disagree with the CLI.
*/
import path from "node:path";
import { akmLint } from "../commands/lint/index.js";
import { indexFiles } from "../core/assets/files.js";
import { adapterWrites } from "../core/assets/gate.js";
import { loadConfig } from "../core/config/config.js";
import { UsageError } from "../core/errors.js";
import { inspectWriteTarget } from "../core/write-source.js";
import { resolveSourceEntries } from "../indexer/search/search-source.js";
import { interactive } from "./lease.js";
/** The akm bundles the sweep covers: lint reads other adapters' bundles through their own validators. */
function akmBundles(config, bundle) {
return resolveSourceEntries(undefined, config).flatMap((source) => {
const { bundleId: name, path: root, registryId, unresolved, writable } = source;
if (!registryId || root === undefined || unresolved || (bundle !== undefined && name !== bundle))
return [];
return adapterWrites(name, "knowledge") ? [{ name, root, writable }] : [];
});
}
/** Per bundle, the links that name an old ref of an asset that moved: where, what is written, and where it points now. */
export async function movedLinks(input = {}) {
const config = loadConfig();
const bundles = [];
for (const { name, root, writable } of akmBundles(config, input.bundle || undefined)) {
const found = await akmLint({ dir: root, rule: "missing-ref", config });
bundles.push({
bundle: name,
writable: writable === true,
writeBlocker: inspectWriteTarget(config, name).blocker,
links: found.flagged.flatMap((issue) => issue.movedFrom !== undefined && issue.movedTo !== undefined
? [{ ref: issue.ref ?? issue.file, file: issue.file, from: issue.movedFrom, to: issue.movedTo }]
: []),
});
}
return { bundles, total: bundles.reduce((sum, bundle) => sum + bundle.links.length, 0) };
}
/** Rewrite the links to moved refs in one bundle, and nothing else. A read-only bundle is refused. */
export async function updateMovedLinks(input) {
const config = loadConfig();
const name = typeof input.bundle === "string" ? input.bundle.trim() : "";
const found = akmBundles(config, name || undefined)[0];
if (!name || !found)
throw new UsageError(`"${name}" is not a bundle of akm assets.`, "INVALID_FLAG_VALUE");
return interactive("ui-moved-links", async () => {
const result = await akmLint({ dir: found.root, fixMovedRefs: true, config });
const fixed = result.fixed.filter((issue) => issue.movedTo !== undefined);
const files = [...new Set(fixed.map((issue) => issue.file))];
await indexFiles(name, files.map((file) => path.join(found.root, file)));
return { bundle: name, rewritten: fixed.length, files };
});
}