UNPKG

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

164 lines (163 loc) • 7.88 kB
// 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/. /** * Where akm's work runs, as one table: a row for each configured connection * and harness, and a row for each kind of work naming the connection or * harness, and the model, it resolves to. The work rows are resolved by the same * resolvers the calls use, so the table never disagrees with what a call does. * * Pure but for the key and PATH lookups it reads. What needs the network or a * process (is the endpoint reachable, what does the harness report as its * version) is passed in by the caller that wants it: `akm health` supplies * them, `akm info` does not. */ import { ambiguousDefaultAdvice, defaultConnection, defaultHarness, } from "../core/config/config.js"; import { readCredential } from "../core/config/credentials.js"; import { IMPROVE_ENGINE_PROCESSES } from "../core/config/engine-semantics.js"; import { SHIPPED_ALIAS } from "../core/config/model-aliases.js"; import { envReferenceName } from "../core/config/schema/primitives.js"; import { ConfigError } from "../core/errors.js"; import { redactUrlCredentials } from "../core/redaction.js"; import { defaultWhich } from "../integrations/agent/detect.js"; import { resolveTarget } from "../integrations/agent/resolution.js"; import { DEFAULT_LOCAL_MODEL } from "../llm/embedder.js"; import { effectiveEmbedding } from "../llm/embedders/connection.js"; import { resolveIndexPassExecution } from "../llm/index-passes.js"; import { resolveImproveExecution } from "./improve/execution.js"; function keyState(connection, name, env) { const variable = connection.apiKey === undefined ? undefined : envReferenceName(connection.apiKey); if (variable !== undefined) return env[variable]?.trim() ? "env-set" : "env-unset"; return readCredential({ connection: name }, connection.endpoint) !== undefined ? "credentials" : "none"; } function connectionRows(input) { const { config } = input; const defaultName = defaultConnection(config)?.name; return Object.entries(config.connections ?? {}).map(([name, connection]) => { const reach = input.reachability?.get(name); return { type: "connection", name, default: name === defaultName, endpoint: redactUrlCredentials(connection.endpoint), key: keyState(connection, name, input.env ?? process.env), ...(reach ? { reachable: reach.reachable, ...(reach.error ? { error: reach.error } : {}) } : {}), }; }); } /** A harness's binary, as a call to it would resolve it. */ function harnessBin(config, name, harness) { try { const target = resolveTarget({ config, layers: [{ harness: name }] }); if (target.kind === "harness") return target.bin; } catch { // Falls through: nothing to look up. } return harness.bin ?? harness.platform ?? name; } function harnessRows(input) { const { config } = input; const defaultName = defaultHarness(config)?.name; const which = input.which ?? defaultWhich; return Object.entries(config.harnesses ?? {}).map(([name, harness]) => { const probe = input.versions?.get(name); const bin = harnessBin(config, name, harness); return { type: "harness", name, default: name === defaultName, platform: harness.platform ?? name, bin, installed: Boolean(which(bin)), ...(harness.modelWork === false ? { modelWork: false } : {}), ...probe, }; }); } /** What a resolver says: a runner, nothing (not set up), or what is wrong with the config. */ function attempt(resolve) { try { const runner = resolve(); return runner ? { runner } : {}; } catch (err) { if (err instanceof ConfigError && (err.code === "LLM_NOT_CONFIGURED" || err.code === "AGENT_NOT_CONFIGURED")) { return {}; } return { problem: err instanceof Error ? err.message : String(err) }; } } const sameTarget = (a, b) => a.runner?.kind === b.runner?.kind && a.runner?.name === b.runner?.name && a.runner?.model === b.runner?.model; /** A work row from its base resolution and the variants that may resolve somewhere else. */ function runnerRow(process, base, variants = [], ambiguous) { const overrides = variants .filter(([, variant]) => !sameTarget(variant, base)) .map(([name, variant]) => ({ name, kind: variant.runner?.kind ?? null, target: variant.runner?.name ?? null, model: variant.runner?.model ?? null, })); // Work that resolves to nothing because the config has several entries and no default is not "not set up". const problem = base.problem ?? variants.find(([, variant]) => variant.problem)?.[1].problem ?? (base.runner ? undefined : ambiguous); return { type: "process", process, kind: base.runner?.kind ?? null, target: base.runner?.name ?? null, model: base.runner?.model ?? null, ...(overrides.length > 0 ? { overrides } : {}), ...(problem ? { problem } : {}), }; } function embeddingRow(config, ambiguous) { const off = { type: "process", process: "embedding", kind: null, target: null, model: null }; if (config.semanticSearchMode === "off") return off; // A model with no endpoint of its own is served by the default connection; with none to choose, the built-in embedder runs instead. const wantsDefaultConnection = config.embedding?.model !== undefined && config.embedding.endpoint === undefined; const embedding = effectiveEmbedding(config); if (embedding?.endpoint === undefined) { return { ...off, kind: "local", target: "built-in", model: embedding?.localModel ?? DEFAULT_LOCAL_MODEL, ...(wantsDefaultConnection && ambiguous ? { problem: ambiguous } : {}), }; } const served = config.embedding?.endpoint === undefined; return { ...off, kind: served ? "connection" : "endpoint", target: served ? (defaultConnection(config)?.name ?? null) : redactUrlCredentials(embedding.endpoint), model: embedding.model ?? null, }; } function processRows(config) { const ambiguous = ambiguousDefaultAdvice(config); const improve = (processName, alias, rest = {}) => attempt(() => resolveImproveExecution({ config, processName, alias: SHIPPED_ALIAS[alias], ...rest })?.runner); const gate = config.improve?.qualityGate; return [ runnerRow("improve", improve("improve", "improve"), IMPROVE_ENGINE_PROCESSES.map((name) => [name, improve(name, "improve")]), ambiguous), runnerRow("judges", improve("judge", "judge"), [ ["triage judgment", improve("triage-judgment", "judge", { process: "triage" })], ["quality gate", improve("quality-judge", "judge", gate ? { current: gate } : {})], ], ambiguous), runnerRow("agents", attempt(() => resolveTarget({ config, layers: [{ model: SHIPPED_ALIAS.agent }] })), [], ambiguous), runnerRow("extract", improve("extract", "extract"), [], ambiguous), runnerRow("enrich", improve("remember-enrich", "enrich"), [], ambiguous), runnerRow("index", attempt(() => resolveIndexPassExecution("memory", config).runner), [], // A pass that is switched off is not waiting for a default. config.index?.memory?.enabled === false ? undefined : ambiguous), embeddingRow(config, ambiguous), ]; } export function targetRows(input) { return [...connectionRows(input), ...harnessRows(input), ...processRows(input.config)]; }