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
83 lines (82 loc) • 4.13 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/.
import path from "node:path";
import { displayRef } from "../../core/asset/resolve-ref.js";
import { presentationFor } from "../../core/type-presentation.js";
import { getRenderer } from "../../output/renderer-registry.js";
import { getDerivedForParent, getItemRefById } from "../../storage/repositories/index-entries-repository.js";
import { extractDirTagsFromName, extractTagsFromPath } from "../passes/metadata.js";
import { attachSearchHitAttribution } from "./search-attribution.js";
export function searchHitLibrary(library, source, filePath, bundleId, configuredDefault) {
if (typeof library !== "string") {
return { entryStashDir: source?.path, defaultBundleId: library.bundleId };
}
const sourcePath = source?.path;
return {
entryStashDir: sourcePath ?? (filePath === "" ? undefined : library),
defaultBundleId: configuredDefault ??
(sourcePath !== undefined && path.resolve(sourcePath) === path.resolve(library) ? bundleId : undefined),
};
}
/**
* Let the hit's type renderer enrich it, then — for a memory, when a DB is
* open — surface its derived child ({@link surfaceDerivedMemory}).
*/
export async function enrichSearchHit(hit, ctx) {
const rendererName = presentationFor(ctx.type).renderer;
if (rendererName && ctx.stashDir !== undefined) {
const renderer = await getRenderer(rendererName);
renderer?.enrichSearchHit?.(hit, ctx.stashDir);
}
if (ctx.type === "memory" && ctx.db)
surfaceDerivedMemory(hit, ctx.db, ctx);
}
/**
* Phase 5A / Advantage D5 — when a parent memory has a `.derived` child
* indexed (the LLM-distilled lesson surface), fill missing parent metadata
* from the derived child's description / tags AND set `expandTo` to the
* derived child's ref so callers can fetch it via `akm show <ref>`.
*
* The parent ref is preserved on the hit — only the surface text is
* filled, so links and provenance still point at the canonical parent.
*/
function surfaceDerivedMemory(hit, db, ctx) {
// Never recurse: a `.derived` hit is itself the child surface; leaving
// it untouched also avoids `<parent>.derived.derived` chains.
if (hit.name.toLowerCase().endsWith(".derived"))
return;
// INTERNAL lookup key into `getDerivedForParent`: the `derived_from` column
// stores the parent's conceptId.
const derived = getDerivedForParent(db, ctx.conceptId, ctx.bundleId);
if (!derived || (ctx.eligible && !ctx.eligible(derived)))
return;
// Fields fall back independently. Preserve authored qualifications and tags
// that add information beyond the generated filename/directory tokens.
const surfaceFields = [];
let surfaceDescription;
if ((!hit.description?.trim() || ctx.descriptionSource === "filename") && derived.entry.description?.trim()) {
hit.description = derived.entry.description;
surfaceDescription = derived.entry.description;
surfaceFields.push("description");
}
const generatedTags = new Set([...extractTagsFromPath(`${hit.name}.md`, "."), ...extractDirTagsFromName(hit.name)]);
if (!hit.tags?.some((tag) => tag.trim() && !generatedTags.has(tag.trim().toLowerCase())) &&
derived.entry.tags?.some((tag) => tag.trim())) {
hit.tags = derived.entry.tags;
surfaceFields.push("tags");
}
// Retain the parent's bundle qualification so expansion resolves the same child.
hit.expandTo = displayRef({ conceptId: derived.conceptId, bundleId: ctx.bundleId }, ctx.defaultBundleId);
const childRef = getItemRefById(db, derived.id);
if (childRef && surfaceFields.length > 0) {
attachSearchHitAttribution(hit, {
memoryInference: {
exposure: "surface",
childRef,
surfaceFields,
...(surfaceDescription ? { surfaceDescription } : {}),
},
});
}
}