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

71 lines (70 loc) • 4.27 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 fs from "node:fs"; import { parseBundleRef } from "../../core/asset/asset-ref.js"; import { loadConfig } from "../../core/config/config.js"; import { readUsableIndex } from "../../indexer/index-readiness.js"; import { applyEntryFilters } from "../../indexer/search/db-search.js"; import { resolveSourceEntries } from "../../indexer/search/search-source.js"; import { getEntriesByIds, getEntryIdByFilePath, getItemRefById, } from "../../storage/repositories/index-entries-repository.js"; import { getMeta } from "../../storage/repositories/index-meta-repository.js"; import { getAllEntriesForEmbedding, getNeighborsByEntryId, hasStoredEmbedding, } from "../../storage/repositories/index-vec-repository.js"; import { akmShowUnified } from "./show.js"; const RELATED_LIMIT = 20; /** * The indexed assets whose stored embeddings are nearest to those of the asset at `ref`, with no query embedding and * no network. Search's own entry filter keeps the matches to enabled bundles, and scope `bundle` to the asset's bundle; * protected and generated assets are never listed. */ export async function akmRelated(ref, scope = "all") { const unavailable = (note) => ({ available: false, note, matches: [] }); const shown = await akmShowUnified({ ref, readOnly: true }); if (shown.type === "env" || shown.type === "secret") return unavailable("This asset has no comparable indexed content."); const file = shown.path; return readUsableIndex((db) => { const id = file ? getEntryIdByFilePath(db, file) : undefined; const bundle = id === undefined ? undefined : parseBundleRef(getItemRefById(db, id) ?? "").bundle; if (id === undefined || !bundle || !hasStoredEmbedding(db, id)) return unavailable("No stored embedding for this asset. Run AKM indexing with embeddings enabled; browsing here never calls a provider."); if (getAllEntriesForEmbedding(db, [id], getMeta(db, "embeddingFingerprint")).length) return unavailable("This asset's embedding belongs to an older model. Refresh the index before comparing it."); const sources = resolveSourceEntries(undefined, loadConfig()).filter((source) => !source.unresolved); const filterOptions = { db, sources, restrictToSources: true, includeProposed: false, beliefFilter: "all", }; const neighbours = getNeighborsByEntryId(db, id, RELATED_LIMIT, undefined, (ids) => { const rows = getEntriesByIds(db, ids).filter((row) => row.id !== id && row.itemRef && !["secret", "env", "session"].includes(row.entry.type) && (scope === "all" || row.bundleId === bundle) && // asset-io: base a neighbour whose indexed file is gone is left out; the facade has no existence read for a row's file, and one listing of 2,400 assets (72 ms) costs far more than stats of a few candidates (4.6 ms for 2,400) fs.existsSync(row.filePath)); return new Set(applyEntryFilters(rows, filterOptions).map((row) => row.id)); }); const rows = new Map(getEntriesByIds(db, neighbours.map((match) => match.id)).map((row) => [row.id, row])); const matches = neighbours.flatMap((match) => { const row = rows.get(match.id); if (!row) return []; return [ { ref: row.itemRef, title: row.entry.toc?.find((heading) => heading.level === 1)?.text || row.entry.name, type: row.entry.type, cosine: Math.max(-1, Math.min(1, 1 - match.distance ** 2 / 2)), }, ]; }); return { available: true, matches, note: `Up to ${RELATED_LIMIT} nearest stored embeddings. Scores describe indexed content; refresh the index after external edits.`, }; }, unavailable("No index is available. Build the AKM index to compare stored embeddings.")); }