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
67 lines (66 loc) • 3.31 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 { isRecord } from "../common.js";
import { warnOnce } from "../warn.js";
import { ConnectionSchema } from "./schema/connections.js";
import { HarnessSchema, harnessNameProblem } from "./schema/harnesses.js";
import { engineName } from "./schema/primitives.js";
/** The first thing a schema found wrong with an entry, as a sentence fragment. */
function firstIssue(error) {
const issue = error.issues[0];
const where = issue?.path.join(".");
return `${where ? `${where}: ` : ""}${issue?.message ?? "it is not valid"}`;
}
/** Why an entry cannot have this name, if it cannot: the rule every connection and harness name follows. */
function nameProblem(name) {
const parsed = engineName.safeParse(name);
return parsed.success ? undefined : `its name breaks the naming rule (${firstIssue(parsed.error)})`;
}
/** Why a connection entry cannot be used, if it cannot: its name breaks the rule, or its own schema rejects it. */
function connectionProblem(name, entry) {
const parsed = ConnectionSchema.safeParse(entry);
return nameProblem(name) ?? (parsed.success ? undefined : firstIssue(parsed.error));
}
/** Why a harness entry cannot be used, if it cannot: as for a connection, or it names no platform and is not one. */
function harnessProblem(name, entry) {
const parsed = HarnessSchema.safeParse(entry);
return nameProblem(name) ?? (parsed.success ? harnessNameProblem(name, parsed.data) : firstIssue(parsed.error));
}
/** The connections and harnesses of `raw` that their own schemas reject, in file order. */
export function findUnusableEntries(raw) {
const unusable = [];
const check = (entries, kind, problem) => {
if (!isRecord(entries))
return;
for (const [name, entry] of Object.entries(entries)) {
const why = problem(name, entry);
if (why !== undefined)
unusable.push({ kind, name, why });
}
};
check(raw.connections, "connection", connectionProblem);
check(raw.harnesses, "harness", harnessProblem);
return unusable;
}
/**
* A connection or harness its own schema rejects (no endpoint, an endpoint that
* is not a URL, a model that is not a name, a harness akm cannot run, a name that
* breaks the naming rule) is of no
* use, but it is not a reason to refuse the rest of the config or to disable the
* entries beside it: each is dropped from what is read, with one warning naming
* it and the first thing wrong. The file keeps it, and a write does not remove it.
*/
export function dropUnusableEntries(raw, sourcePath) {
const unusable = findUnusableEntries(raw);
if (unusable.length === 0)
return raw;
const out = { ...raw };
for (const { kind, name, why } of unusable) {
warnOnce(`config:unusable:${kind}:${sourcePath ?? "inline"}:${name}`, `Ignoring ${kind} "${name}"${sourcePath ? ` in ${sourcePath}` : ""}: ${why}.`);
const holder = kind === "connection" ? "connections" : "harnesses";
const { [name]: _dropped, ...kept } = out[holder];
out[holder] = kept;
}
return out;
}