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
117 lines (116 loc) • 5.69 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/.
/**
* The `connections-and-harnesses` health check: the table of connections, harnesses and the work
* that runs on them (`../targets.ts`), with what only health asks for added to
* it. Whether a connection's endpoint answers is one bounded `GET /models`,
* once per distinct endpoint; a harness's version is its own `--version`, one
* process per harness. Both are probes: with `--no-probe` neither the endpoints
* are contacted nor any harness run.
*/
import { spawnSync } from "node:child_process";
import { ambiguousDefaultAdvice, loadConfig } from "../../core/config/config.js";
import { defaultWhich, probeVersion } from "../../integrations/agent/detect.js";
import { resolveTarget } from "../../integrations/agent/resolution.js";
import { probeEndpointOnce } from "../../llm/client.js";
import { targetRows } from "../targets.js";
const VERSION_TIMEOUT_MS = 5_000;
/** Each connection's reachability, asked once per distinct endpoint. */
async function probeConnections(config, probe) {
const cache = new Map();
const entries = await Promise.all(Object.entries(config.connections ?? {}).map(async ([name, connection]) => [name, await probeEndpointOnce(connection, cache, probe)]));
return new Map(entries);
}
/** Each installed harness's `--version`, its first line; none when it will not say. */
function harnessVersions(config, which, spawn) {
const versions = new Map();
for (const name of Object.keys(config.harnesses ?? {})) {
try {
const target = resolveTarget({ config, layers: [{ harness: name }] });
if (target.kind !== "harness" || !which(target.bin))
continue;
const result = probeVersion(target.bin, VERSION_TIMEOUT_MS, spawn);
versions.set(name, {
...(result.version ? { version: result.version.split("\n")[0] } : {}),
...(result.error ? { error: result.error } : {}),
});
}
catch (error) {
versions.set(name, { error: error instanceof Error ? error.message : String(error) });
}
}
return versions;
}
/** What is wrong in the table, one phrase each. A key variable's name is never quoted. */
function problems(rows, ambiguous) {
// No default among several connections or harnesses is the config's problem, said once whatever work it leaves
// without a place to run (work may still find one: model work falls to a harness).
const found = ambiguous ? [ambiguous] : [];
// A problem that several kinds of work share is said once.
const shared = new Map();
for (const row of rows) {
if (row.type === "connection") {
if (row.key === "env-unset")
found.push(`connection "${row.name}": the variable holding its key is not set`);
if (row.reachable === false) {
found.push(`connection "${row.name}" is not reachable${row.error ? ` (${row.error})` : ""}`);
}
}
else if (row.type === "harness") {
if (!row.installed)
found.push(`harness "${row.name}" (${row.platform}) was not found on PATH`);
else if (row.error)
found.push(`harness "${row.name}" could not be probed (${row.error})`);
}
else if (row.problem) {
const problem = row.problem.replace(/\.$/, "");
if (problem !== ambiguous)
shared.set(problem, [...(shared.get(problem) ?? []), row.process]);
}
}
for (const [problem, processes] of shared)
found.push(`${processes.join(", ")}: ${problem}`);
return found;
}
/** The check for a table. `unknown` when nothing is configured: there is nothing to be wrong. */
export function connectionsAndHarnessesCheck(rows, probed, ambiguous) {
const connections = rows.filter((row) => row.type === "connection").length;
const harnesses = rows.filter((row) => row.type === "harness").length;
const base = {
name: "connections-and-harnesses",
kind: "deterministic",
confidence: "high",
evidence: { rows },
};
if (connections + harnesses === 0) {
return {
...base,
status: "unknown",
reason: "off",
message: "No connection or harness is configured: model features and agentic work are off. Run `akm setup`.",
};
}
const found = problems(rows, ambiguous);
const counts = `${connections} connection${connections === 1 ? "" : "s"} and ${harnesses} harness${harnesses === 1 ? "" : "es"}`;
const notProbed = connections > 0 && !probed ? " Reachability was not probed." : "";
return {
...base,
status: found.length > 0 ? "warn" : "pass",
message: found.length > 0 ? `${counts}: ${found.join("; ")}.${notProbed}` : `${counts}, all ready.${notProbed}`,
};
}
export async function runConnectionsAndHarnessesCheck(deps = {}) {
const config = deps.loadConfig?.() ?? loadConfig();
const which = deps.which ?? defaultWhich;
const reachability = deps.probeReachable ? await probeConnections(config, deps.probeReachable) : undefined;
const versions = reachability ? harnessVersions(config, which, deps.spawnSync ?? spawnSync) : undefined;
const rows = targetRows({
config,
env: deps.env,
which,
reachability,
versions,
});
return connectionsAndHarnessesCheck(rows, reachability !== undefined, ambiguousDefaultAdvice(config));
}