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

140 lines (139 loc) • 5.53 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 { closeDatabase, openExistingDatabase } from "../../../storage/repositories/index-connection.js"; import { getEntryById, getEntryIdByFilePath } from "../../../storage/repositories/index-entries-repository.js"; import { getNeighborsByEntryId } from "../../../storage/repositories/index-vec-repository.js"; import { stripFrontmatterBody } from "../content-hash.js"; import { indexedContent } from "../indexed-asset.js"; import { PAIR_NEIGHBOR_FETCH_K } from "./pair-pass.js"; /** Words per shingle. */ export const COVERAGE_SHINGLE_WORDS = 5; /** Share of a memory's distinct shingles one knowledge doc must hold for the memory to count as covered. */ export const COVERAGE_MIN_CONTAINMENT = 0.3; const WORD = /[\p{L}\p{N}]+/gu; /** The distinct lower-cased word n-grams of `text`; empty when it has fewer than {@link COVERAGE_SHINGLE_WORDS} words. */ export function wordShingles(text) { const words = text.toLowerCase().match(WORD) ?? []; const shingles = new Set(); for (let i = 0; i + COVERAGE_SHINGLE_WORDS <= words.length; i++) { shingles.add(words.slice(i, i + COVERAGE_SHINGLE_WORDS).join(" ")); } return shingles; } /** The share (0..1) of `memory`'s shingles that also occur in `doc`; 0 when the memory has none. */ export function shingleContainment(memory, doc) { if (memory.size === 0) return 0; const docShingles = wordShingles(doc); let shared = 0; for (const shingle of memory) if (docShingles.has(shingle)) shared++; return shared / memory.size; } /** * The {@link PAIR_NEIGHBOR_FETCH_K} knowledge docs in `bundleId` nearest to the * memory at `filePath`, nearest first. A memory the index does not know has no * stored vector and so no neighbours. */ function knowledgeNeighbours(db, bundleId, filePath) { const entryId = getEntryIdByFilePath(db, filePath); if (entryId === undefined) return []; const out = []; for (const hit of getNeighborsByEntryId(db, entryId, PAIR_NEIGHBOR_FETCH_K, { type: "knowledge", bundleId })) { const neighbour = getEntryById(db, hit.id); if (!neighbour) continue; const raw = indexedContent(neighbour.bundleId, neighbour.conceptId); if (raw === undefined) continue; // the index outlived the asset out.push({ ref: neighbour.conceptId, description: neighbour.entry.description ?? "", body: stripFrontmatterBody(raw), }); } return out; } /** * The best-covering knowledge doc among the {@link PAIR_NEIGHBOR_FETCH_K} * knowledge docs in `bundleId` nearest to the memory. `filePath` is the * memory's indexed file; a memory the index does not know has no stored * vector and so no candidates. */ export function findCoveringKnowledge(db, bundleId, filePath, body) { const shingles = wordShingles(body); if (shingles.size === 0) return undefined; let best; for (const neighbour of knowledgeNeighbours(db, bundleId, filePath)) { const containment = shingleContainment(shingles, neighbour.body); if (containment >= COVERAGE_MIN_CONTAINMENT && (best === undefined || containment > best.containment)) { best = { ref: neighbour.ref, containment }; } } return best; } /** Knowledge docs a reviewer is shown for a promotion, nearest first. */ export const NEIGHBOUR_NOTE_COUNT = 5; export const NEIGHBOUR_EXCERPT_CHARS = 300; /** * The knowledge notes nearest to the memory at `memoryPath`, for the drain's * judge to compare a promotion against: the nearest {@link NEIGHBOUR_NOTE_COUNT} * of the same candidates the coverage gate reads. The memory's bundle is the one * the index recorded for it. Empty when the index has no vector for the memory * or cannot be opened; never throws. */ export function nearestKnowledgeNotes(memoryPath) { let db; try { db = openExistingDatabase(); const entryId = getEntryIdByFilePath(db, memoryPath); const bundleId = entryId === undefined ? undefined : getEntryById(db, entryId)?.bundleId; if (bundleId === undefined) return []; return knowledgeNeighbours(db, bundleId, memoryPath) .slice(0, NEIGHBOUR_NOTE_COUNT) .map((n) => ({ ref: n.ref, description: n.description, excerpt: n.body.length > NEIGHBOUR_EXCERPT_CHARS ? `${n.body.slice(0, NEIGHBOUR_EXCERPT_CHARS)}...` : n.body, })); } catch { return []; } finally { if (db) closeDatabase(db); } } /** * The gate for one run, holding its own read handle on `index.db` for the * promotions that run emits; `undefined` when there is no bundle or no index * to look in. A lookup that throws counts as "not known to be covered". */ export function openKnowledgeCoverage(bundleId) { if (!bundleId) return undefined; let db; try { db = openExistingDatabase(); } catch { return undefined; } return { find: (filePath, body) => { try { return findCoveringKnowledge(db, bundleId, filePath, body); } catch { return undefined; } }, close: () => closeDatabase(db), }; }