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
114 lines (113 loc) • 5.72 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/.
/**
* Setup steps 1 and 2: which agent programs to enable as harnesses, which of
* them agent work runs on, and whether akm's own model calls (improve, judging,
* extraction) may run through it too.
*/
import * as p from "../../cli/clack.js";
import { defaultConnection, defaultHarness } from "../../core/config/config.js";
import { warn } from "../../core/warn.js";
import { getHarness } from "../../integrations/harnesses/index.js";
import { prompt } from "../prompt.js";
import { harnessEntries } from "../setup-plan.js";
import { runsReadOnly, unrestrictedModelWorkNote } from "../summary.js";
function readOnlyNote(platform) {
return runsReadOnly(platform)
? "model calls are limited to reading and searching"
: "model calls cannot be restricted to read-only";
}
function configuredPlatforms(current) {
return [...new Set(Object.entries(current.harnesses ?? {}).map(([name, harness]) => harness.platform ?? name))];
}
/** The platform agent work runs on in the config, if any. */
function currentDefaultPlatform(current) {
const found = defaultHarness(current);
return found ? (found.harness.platform ?? found.name) : undefined;
}
/** The harness agent work runs on once `answer` is applied: the entry the config will hold, with its own `bin`. */
function selectedHarness(current, answer) {
const harnesses = answer ? harnessEntries(current.harnesses, answer.platforms, answer.default) : current.harnesses;
return defaultHarness({ harnesses });
}
export async function stepHarnesses(input) {
const { current, installed, flags } = input;
const configured = configuredPlatforms(current);
const installedPlatforms = installed.map((harness) => harness.platform);
let answer;
if (flags.harness) {
for (const platform of flags.harness) {
if (!installedPlatforms.includes(platform)) {
warn(`[akm setup] ${platform} was not found on PATH; enabling it anyway.`);
}
}
answer = { platforms: [...new Set([...configured, ...flags.harness])], default: flags.harness[0] };
}
else if (!input.interactive) {
if (configured.length === 0 && installedPlatforms.length > 0) {
answer = { platforms: installedPlatforms, default: installedPlatforms[0] };
}
}
else {
answer = await askHarnesses(current, installed, configured);
}
// `modelWork` is whether the default harness may take akm's model calls, once that has been decided.
const result = (modelCallsOnHarness, modelWork) => ({
...(answer ? { answer } : {}),
modelCallsOnHarness,
...(modelWork !== undefined ? { modelWork } : {}),
});
// Model calls can go to a harness only when one is enabled and no connection exists or is being added.
const selected = selectedHarness(current, answer);
if (selected === undefined || defaultConnection(current) !== undefined || flags.endpoint !== undefined) {
return result(false);
}
const defaultPlatform = selected.harness.platform ?? selected.name;
const readOnly = runsReadOnly(defaultPlatform);
// A saved `modelWork: false` is the user's choice: only the question's "yes" changes it, never a default.
const declined = selected.harness.modelWork === false;
if (!input.interactive)
return declined ? result(false, false) : result(readOnly, readOnly);
const yes = await prompt(() => p.confirm({
message: `Use ${defaultPlatform} for akm's own model calls too (improve, judging, extraction)? ${readOnly ? "Model calls are limited to reading and searching." : unrestrictedModelWorkNote(defaultPlatform)}`,
initialValue: !declined,
}));
// Said again once it is chosen, so it stays in the transcript beside the answer.
if (yes && !readOnly)
p.log.warn(unrestrictedModelWorkNote(defaultPlatform));
return result(yes, yes);
}
async function askHarnesses(current, installed, configured) {
const installedPlatforms = installed.map((harness) => harness.platform);
const candidates = [...new Set([...installedPlatforms, ...configured])];
if (candidates.length === 0) {
p.log.info("No agent programs found on PATH. Agent work runs on akm's own runtime over a connection (set one up next).");
return undefined;
}
const selected = await prompt(() => p.multiselect({
message: "Which agent programs should akm use? (agent work runs on them)",
options: candidates.map((platform) => ({
value: platform,
label: getHarness(platform)?.displayName ?? platform,
hint: `${installed.find((harness) => harness.platform === platform)?.path ?? "not found on PATH"}; ${readOnlyNote(platform)}`,
})),
initialValues: configured.length > 0 ? [...configured] : installedPlatforms,
required: false,
}));
if (selected.length === 0)
return { platforms: [] };
const wanted = currentDefaultPlatform(current);
const initialValue = wanted && selected.includes(wanted) ? wanted : selected[0];
const chosen = selected.length === 1
? selected[0]
: await prompt(() => p.select({
message: "Which one should agent work run on by default?",
options: selected.map((platform) => ({
value: platform,
label: getHarness(platform)?.displayName ?? platform,
})),
initialValue,
}));
return { platforms: selected, default: chosen };
}