UNPKG

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

56 lines (55 loc) • 2.43 kB
// 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/. import { makeBundleRef, parseBundleRef } from "../core/asset/asset-ref.js"; import { findAsset } from "../core/assets/read.js"; import { indexedPathOf } from "../core/assets/type.js"; import { bundleAt } from "../core/bundle-at-dir.js"; import { safeRealpath } from "../core/common.js"; import { lookupBundleRef } from "./indexer.js"; import { resolveSourceEntries } from "./search/search-source.js"; async function resolveViaIndex(ref) { try { const entry = await lookupBundleRef(ref); return entry?.filePath ?? null; } catch { return null; } } /** * The asset `ref` names on disk: in the first of the sources, in order (the named directory's bundle first), that holds * it; with `honorOrigin: false` the ref's own bundle is not required. */ export function findAssetOnDisk(ref, options = {}) { const parsed = parseBundleRef(ref); const allSources = resolveSourceEntries(options.stashDir); const { writableBundleIds } = options; const sources = writableBundleIds ? allSources.filter((source) => writableBundleIds.has(source.bundleId)) : allSources; const bundle = options.honorOrigin !== false ? parsed.bundle : undefined; const held = options.stashDir === undefined ? undefined : bundleAt(options.stashDir); return sources .filter((source) => bundle === undefined || source.bundleId === bundle) .map((source) => findAsset(makeBundleRef(source.bundleId, parsed.conceptId), held)) .find((found) => found !== undefined); } /** The file of the asset `ref` names on disk; null when none holds it, or no file does (a keyring's or vault's). */ function resolveViaDisk(ref, options) { const found = findAssetOnDisk(ref, options); const file = found && indexedPathOf(found); return file ? safeRealpath(file) : null; } export async function resolveAssetPath(ref, options = {}) { const parsed = parseBundleRef(ref); const mode = options.mode ?? "index-first"; if (mode !== "disk-only") { const indexed = await resolveViaIndex(parsed); if (indexed) return indexed; if (mode === "index-only") return null; } return resolveViaDisk(ref, options); }