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
102 lines (101 loc) • 5.01 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/.
/**
* Shared helpers for the concrete `BundleAdapter` implementations under
* `src/core/adapter/adapters/`.
*
* ── Base checks ──
*
* The base asset checks (unquoted-colon, missing-updated, stale-path, missing-ref,
* redacted-content) have one implementation, `runBaseChecks`
* (`src/core/lint/base-linter.ts`), and one decision on whether a rule applies to a
* file, `ruleApplies` (`core/asset/lint-text.ts`). {@link createBaseChecker} is an
* adapter's way in: it hands `runBaseChecks` the file's text and the bundle's scope and
* returns what it finds. `validate` never writes, so it never asks for a fix.
*/
import { createHash } from "node:crypto";
import path from "node:path";
import { parseFrontmatter } from "../../asset/frontmatter.js";
import { toPosix } from "../../common.js";
import { loadConfig } from "../../config/config.js";
import { runBaseChecks } from "../../lint/base-linter.js";
import { lintScope } from "../../lint/scope.js";
import { rootOf } from "../types.js";
// ── Small pure helpers, reused across the concrete adapters ──────────────────
/** Content hash feeding `IndexDocument.hash` (incrementality/fingerprints, `types.ts` doc comment). */
export function hashContent(content) {
return createHash("sha256").update(content, "utf8").digest("hex");
}
/** Trim a possibly-non-string frontmatter value down to a non-empty string or `undefined` (mirrors `core/common.ts#asNonEmptyString`, duplicated locally to avoid importing a cycle-participant module for a 4-line helper). */
export function nonEmptyString(value) {
if (typeof value !== "string")
return undefined;
const trimmed = value.trim();
return trimmed.length > 0 ? trimmed : undefined;
}
/** Read a frontmatter `tags:` array down to its non-empty string entries, or `undefined` when there are none (mirrors `output/renderers.ts#readFrontmatterTags`). */
export function readTags(value) {
if (!Array.isArray(value))
return undefined;
const tags = value.filter((tag) => typeof tag === "string" && tag.trim().length > 0);
return tags.length > 0 ? tags : undefined;
}
/** Reserved files (case-insensitive) every concrete adapter recognizes but never indexes as a concept. */
export const RESERVED_FILES = new Set(["index.md", "log.md"]);
/** The result of `parseFrontmatter(raw)`, with the text it parsed. */
export function parseForValidate(raw) {
const { data, content, frontmatter, bodyStartLine } = parseFrontmatter(raw);
return { raw, data, content, frontmatter, bodyStartLine };
}
/** What a context with no way to read recorded moves offers. */
const noMoves = () => [];
/** The base checks for the files of the bundle at `bundleRoot`, among the bundles and with the settings `ctx` names (the settings are read once, here, only when `ctx` carries none). The scope (other bundles, moved refs, stale-path roots) is read once. */
export function createBaseChecker(bundleRoot, ctx) {
const scope = lintScope(bundleRoot, ctx.config ?? loadConfig(), ctx.sources ?? [], ctx.readMoves ?? noMoves);
const rulesFor = (relPath, skip = []) => ({
relPath,
skip: [...skip, ...scope.bundleSkip(relPath)],
});
return {
rulesFor,
check: (relPath, parsed, skip) => {
const { skip: lintSkip } = rulesFor(relPath, skip);
return runBaseChecks({
filePath: path.join(bundleRoot, relPath),
relPath,
raw: parsed.raw,
data: parsed.data,
body: parsed.content,
frontmatter: parsed.frontmatter,
bodyStartLine: parsed.bodyStartLine,
fix: false,
stashRoot: scope.stashRoot,
stalePathRoots: scope.stalePathRoots,
extraStashRoots: scope.extraStashRoots,
bundleRoots: scope.bundleRoots,
moves: scope.moves,
lintSkip,
});
},
};
}
/**
* Shared per-change loop: for every non-delete change with readable content,
* parse frontmatter and run the base checks. Adapters with extra per-type checks
* (e.g. `okf`'s missing-type / OKF-link warnings) run their own loop and call
* {@link createBaseChecker} directly instead.
*/
export async function validateChangesWithBaseChecks(c, changes, ctx) {
const diagnostics = [];
const { check } = createBaseChecker(rootOf(c), ctx);
for (const change of changes) {
if (change.op === "delete")
continue;
const raw = change.after ?? (await ctx.readFile(change.path));
if (typeof raw !== "string")
continue;
diagnostics.push(...check(toPosix(change.path), parseForValidate(raw)));
}
return diagnostics;
}