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) • 2.63 kB
JavaScript
import { warn } from "../core/warn.js";
import { deleteEntriesByBundle } from "../storage/repositories/index-entries-repository.js";
import { getMeta, setMeta } from "../storage/repositories/index-meta-repository.js";
/** The `index_meta` key holding the last index run's {@link SemanticSearchRecord}. */
export const SEMANTIC_SEARCH_META_KEY = "semanticSearch";
/** The `index_meta` key holding the {@link IncompleteIndexSource} list of the last index run. */
export const INCOMPLETE_SOURCES_META_KEY = "incompleteSources";
/**
* The sources the last index run could not scan, whose last-known-good rows the index still serves. Undefined when
* no run recorded the list (an index built before it was recorded).
*/
export function readIncompleteSources(db) {
const raw = getMeta(db, INCOMPLETE_SOURCES_META_KEY);
return raw ? JSON.parse(raw) : undefined;
}
/** The last index run's {@link SemanticSearchRecord} in an open index, or undefined when no run recorded one. */
export function readSemanticSearchRecord(db) {
const raw = getMeta(db, SEMANTIC_SEARCH_META_KEY);
return raw ? JSON.parse(raw) : undefined;
}
export function recordSourceOwners(db, sources) {
const owners = sources.map(({ bundleId, path }) => ({ bundleId, sourceRoot: path }));
setMeta(db, "sourceOwners", JSON.stringify(owners));
}
export function readSourceOwners(db) {
const raw = getMeta(db, "sourceOwners");
if (!raw)
return [];
try {
const parsed = JSON.parse(raw);
if (!Array.isArray(parsed) ||
parsed.some((owner) => !owner || typeof owner !== "object" || typeof owner.bundleId !== "string")) {
warn("index_meta sourceOwners value is invalid — treating as empty");
return [];
}
return parsed;
}
catch {
warn("index_meta sourceOwners value is corrupt JSON — treating as empty");
return [];
}
}
export function applyRemovedSources(db, sources, previousOwners, isIncremental) {
const currentBundles = new Set(sources.map((source) => source.bundleId));
for (const { bundleId } of previousOwners) {
if (!currentBundles.has(bundleId))
deleteEntriesByBundle(db, bundleId);
}
// A full run re-drains every configured source, so any other bundle's rows
// are stale even when no stored owner names them.
if (isIncremental)
return;
const indexed = db.prepare("SELECT DISTINCT bundle_id AS bundleId FROM entries").all();
for (const { bundleId } of indexed) {
if (!currentBundles.has(bundleId))
deleteEntriesByBundle(db, bundleId);
}
}