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
101 lines (100 loc) • 3.95 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/.
/**
* Flexible asset resolution system.
*
* Provides a rich FileContext built once per file during walking, plus a
* matcher/renderer registry that decouples asset classification from rendering.
*/
import fs from "node:fs";
import path from "node:path";
import { parseFrontmatter } from "../../asset/frontmatter.js";
import { toPosix } from "../../common.js";
// ── FileContext ──────────────────────────────────────────────────────────────
/**
* Build a FileContext from a stash root and an absolute file path.
*
* Path-derived fields are computed eagerly. The content, frontmatter, and
* stat getters use lazy caching so the file is only read from disk when
* (and if) a matcher or renderer actually needs it.
*/
export function buildFileContext(stashRoot, absPath) {
const relPath = toPosix(path.relative(stashRoot, absPath));
const ext = path.extname(absPath).toLowerCase();
const fileName = path.basename(absPath);
const parentDirAbs = path.dirname(absPath);
const parentDir = path.basename(parentDirAbs);
// Compute ancestor directory segments from the POSIX relPath's directory portion.
// For "scripts/azure/deploy/run.sh" the dir portion is "scripts/azure/deploy"
// which splits into ["scripts", "azure", "deploy"].
const relDir = toPosix(path.dirname(relPath));
const ancestorDirs = relDir === "." ? [] : relDir.split("/").filter((seg) => seg.length > 0);
// Lazy caches
let cachedContent;
let cachedBytes;
let cachedFrontmatter;
let frontmatterComputed = false;
let cachedStat;
return {
absPath,
file: absPath,
relPath,
key: relPath,
ext,
fileName,
parentDir,
parentDirAbs,
ancestorDirs,
stashRoot,
content() {
if (cachedContent === undefined) {
cachedContent = fs.readFileSync(absPath, "utf8");
}
return cachedContent;
},
bytes() {
if (cachedBytes === undefined) {
cachedBytes = fs.readFileSync(absPath);
}
return cachedBytes;
},
frontmatter() {
if (!frontmatterComputed) {
const raw = this.content();
const parsed = parseFrontmatter(raw);
cachedFrontmatter = Object.keys(parsed.data).length > 0 ? parsed.data : null;
frontmatterComputed = true;
}
return cachedFrontmatter ?? null;
},
stat() {
if (cachedStat === undefined) {
cachedStat = fs.statSync(absPath);
}
return cachedStat;
},
modified() {
return this.stat().mtime;
},
mode() {
return this.stat().mode & 0o777;
},
};
}
// ── MatchResult ───────────────────────────────────────────────
/**
* The `FileContext` of an entry a bundle's adapter lists. A directory entry is one already. Any other entry has no
* file: its path fields are the ones the index keys it under (`<root>/<relPath>`), and its content is the entry's.
*/
export function fileContextOfEntry(root, entry) {
if ("absPath" in entry)
return entry;
return {
...buildFileContext(root, path.join(root, entry.key)),
content: () => entry.content(),
bytes: () => entry.bytes(),
frontmatter: () => entry.frontmatter(),
...(entry.locator !== undefined ? { locator: entry.locator } : {}),
};
}