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
51 lines (50 loc) • 2.27 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/.
/**
* How an asset the facade found relates to the index: the path the index keys it under, whether an indexed row is
* that asset's, and the row the asset's adapter gives without the index.
*/
import path from "node:path";
import { indexAsset, indexedPathOf } from "../../core/assets/type.js";
import { safeRealpath } from "../../core/common.js";
import { deriveEntryProvenance } from "../installations.js";
/**
* The index entry of an asset whose bundle has no row for it (a file added outside akm): the asset's adapter recognizes
* it once and, with `persist`, the row is recorded, so the next ask finds it in the index. Undefined when the adapter
* does not recognize it. Without `persist` (a read-only lookup) the entry is served and the index left as it is; it has
* no links, usage or indexed fragments.
*/
export function entryOnMiss(asset, source, persist) {
const bundleId = asset.bundle;
try {
const document = indexAsset(asset, { persist, onMiss: true });
const adapterId = document.adapterId;
const { itemRef, conceptId } = deriveEntryProvenance({ bundleId, adapterId }, document.type, document.name, document.conceptId);
return {
filePath: indexedPathOf(asset),
stashDir: source.path,
type: document.type,
name: document.name,
adapterId,
document,
itemRef,
bundleId,
conceptId,
};
}
catch {
// An unreadable file is not found from files.
return undefined;
}
}
/** Whether `indexedPath` is where the index keys `asset`: the same path, and the same file behind it. */
export function indexedPathMatchesOwner(indexedPath, asset) {
const owner = indexedPathOf(asset);
// An asset no file holds is indexed under no path.
if (owner === "" || indexedPath === "")
return owner === indexedPath;
if (path.resolve(indexedPath) !== path.resolve(owner))
return false;
return safeRealpath(indexedPath) === path.resolve(safeRealpath(owner));
}