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
340 lines (339 loc) • 17.9 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 effective config the owner can read, and the validated changes they can make to it. */
import fs from "node:fs";
import { isDeepStrictEqual } from "node:util";
import { usageReportEnabled } from "../cli/usage-report.js";
import { assertUnsettable, listConfig, setConfigValue, unsetConfigValue } from "../commands/config-cli.js";
import { isRecord } from "../core/common.js";
import { defaultConnection, inheritedConnectionsOf, loadConfig, mutateConfig, resetConfigCache, resolveSecret, } from "../core/config/config.js";
import { parseConfigText, readConfigText } from "../core/config/config-io.js";
import { storedCredential, writeCredential } from "../core/config/credentials.js";
import { dropUnusableProjects, NO_PROJECT } from "../core/config/schema/projects.js";
import { UsageError } from "../core/errors.js";
import { getConfigPath } from "../core/paths.js";
import { redactUrlCredentials } from "../core/redaction.js";
import { hasLocalEmbeddings, noLocalEmbeddingsReason } from "../llm/embedders/onnx-runtime.js";
import { parseSetupFlags } from "../setup/setup-flags.js";
import { applySetupAnswers } from "../setup/setup-plan.js";
import { storeSecretResolver } from "../sources/snapshot-fetchers/secret-seam.js";
import { automaticWriteTarget, configSource, preserveUnrelatedConfig } from "./readers.js";
import { isSecretEnvName, redactForUi } from "./redact.js";
/** Names that would reach Object.prototype when used as a key. */
const UNSAFE_NAMES = new Set(["__proto__", "constructor", "prototype"]);
/** Flatten to dotted leaf paths. Arrays and empty objects are leaves, matching how config layers merge. */
function configLeaves(value, prefix, out) {
if (value && typeof value === "object" && !Array.isArray(value) && Object.keys(value).length > 0) {
for (const [key, child] of Object.entries(value))
configLeaves(child, [...prefix, key], out);
return;
}
out.push({ path: prefix, value });
}
/**
* What akm says about the malformed parts of `projects` in this install's config file, in its own words, and the
* names of the projects that file holds (the others are only inherited).
*/
function localProjects(configPath) {
const warnings = [];
let local = [];
let raw = {};
try {
const text = readConfigText(configPath);
if (text !== undefined) {
const file = parseConfigText(text, configPath);
if (isRecord(file.projects))
raw = file.projects;
const kept = dropUnusableProjects(file, configPath, (_key, message) => warnings.push(message)).projects;
local = isRecord(kept) ? Object.keys(kept) : [];
}
}
catch {
// An unreadable file fails the read above, not this one.
}
return { warnings, local, raw };
}
const redactEndpoint = (endpoint) => endpoint === undefined ? null : redactUrlCredentials(endpoint);
export function get() {
const live = loadConfig();
const effective = redactForUi(listConfig(live));
// The report switch is read from the config file by the report itself; show that answer, not the loaded config's.
effective.usage = { ...(isRecord(effective.usage) ? effective.usage : {}), report: usageReportEnabled() };
// Projects hold no credentials; redaction would turn a value such as `filters.env` into "[redacted]" and a save would write that back.
if (live.projects)
effective.projects = live.projects;
const leaves = [];
configLeaves(effective, [], leaves);
const entries = leaves.map(({ path, value }) => ({
path: path.join("."),
value,
source: configSource(path),
}));
const named = (entries) => Object.entries((entries ?? {})).map(([name, entry]) => ({
name,
...entry,
}));
const env = Object.keys(process.env)
.filter((name) => name.startsWith("AKM_"))
.sort()
.map((name) => ({ name, value: isSecretEnvName(name) ? "[redacted]" : (process.env[name] ?? "") }));
const configPath = getConfigPath();
const projects = localProjects(configPath);
return {
path: configPath,
projectWarnings: projects.warnings,
localProjects: projects.local,
// asset-io: config
exists: fs.existsSync(configPath),
config: effective,
// What the embedding settings need beyond the config: whether this platform has the built-in embedder, and the
// endpoint the stored embedding key belongs to (never the key).
localEmbeddings: hasLocalEmbeddings(),
embeddingKeyEndpoint: redactEndpoint(storedCredential({ service: "embedding" })?.endpoint),
automaticWriteTarget: automaticWriteTarget(live),
entries,
connections: named(effective.connections).map((connection) => ({
...connection,
// Only an `extends` base defines it: it cannot be removed here, and a local save overrides it.
inherited: inheritedConnectionsOf(live).has(connection.name),
// The configured keys that name it, so a reader can say what depends on it.
usedBy: connectionReferences(live, connection.name),
// `file` (this install's config.json), or `extends:<ref>` naming the config this one extends that defines it.
source: configSource(["connections", connection.name]),
})),
// The connection named `default`, else the only one, else none: the rule every consumer without a `connection` follows.
defaultConnection: defaultConnection(live)?.name ?? null,
harnesses: named(effective.harnesses),
env,
};
}
/**
* The `experimental.web` settings `akm-web` starts with. `secret` is the resolved value of the configured reference
* (`undefined` when none is set); a reference that cannot be read throws an error naming it, never the value. The
* `AKM_WEB_SECRET` override and the secret's length are the server's to decide. With `resolveSecret: false` the
* reference is not read and `secret` is omitted, so an override does not depend on the configured reference.
*/
export function webSettings(options = {}) {
const web = loadConfig().experimental?.web;
let secret;
if (web?.secret !== undefined && options.resolveSecret !== false) {
try {
secret = resolveSecret(web.secret, storeSecretResolver.resolveSecret);
}
catch {
throw new UsageError(`experimental.web.secret ${web.secret} could not be read.`);
}
if (!secret)
throw new UsageError(`experimental.web.secret ${web.secret} could not be read.`);
}
return {
...(web?.host !== undefined ? { host: web.host } : {}),
...(web?.port !== undefined ? { port: web.port } : {}),
...(web?.origin !== undefined ? { origin: web.origin } : {}),
allowedHosts: web?.allowedHosts ?? [],
frameParents: web?.frameParents ?? [],
...(secret !== undefined ? { secret } : {}),
};
}
/**
* Set and unset config keys through the same validated path as `akm config set` / `unset`: each value is coerced
* and checked against the schema, literal API keys are refused, and the whole file is validated before one atomic
* write. An unset the CLI refuses (`assertUnsettable`) or skips (no config file) is refused or skipped here too.
*/
export function change(input) {
const set = input.set ?? {};
const unset = input.unset ?? [];
if (typeof set !== "object" || Array.isArray(set))
throw new UsageError("set must be an object of config keys.");
if (!Array.isArray(unset) || unset.some((key) => typeof key !== "string" || !key.trim()))
throw new UsageError("unset must be a list of config keys.");
for (const [key, value] of Object.entries(set)) {
if (!key.trim())
throw new UsageError("Config keys must not be empty.");
if (typeof value !== "string")
throw new UsageError(`The value for ${key} must be a string, as on the command line.`);
}
if (Object.keys(set).length === 0 && unset.length === 0)
return { saved: false };
const result = mutateConfig((current) => {
let updated = current;
for (const [key, value] of Object.entries(set))
updated = setConfigValue(updated, key, value);
for (const key of unset) {
assertUnsettable(key);
updated = unsetConfigValue(updated, key);
}
if (updated !== current)
preserveUnrelatedConfig(current);
return updated;
},
// With no config file there is nothing to unset; a set still has to create it.
{ absentNoop: Object.keys(set).length === 0 });
resetConfigCache();
return { saved: result.written };
}
/**
* Add, replace, rename (set the new name and unset the old) and remove `projects` entries in one atomic write.
* Each entry is a whole project, written as sent (the reader takes a project whole by name); the complete config is
* validated as for `change`, so akm's schema decides what is accepted. Names are not dotted config keys, so a name
* may contain a dot. A project this install's config.json does not hold (inherited, or one akm ignores) cannot be
* unset or renamed here, as `akm config unset` refuses a key it cannot remove.
*/
export function changeProjects(input) {
const set = input.set ?? {};
const unset = input.unset ?? [];
if (!isRecord(set) || Object.values(set).some((project) => !isRecord(project)))
throw new UsageError("set must be an object of project names to project objects.");
if (!Array.isArray(unset) || unset.some((name) => typeof name !== "string"))
throw new UsageError("unset must be a list of project names.");
for (const name of Object.keys(set)) {
if (!name.trim() || name === NO_PROJECT || UNSAFE_NAMES.has(name))
throw new UsageError(`"${name}" cannot name a project.`);
}
if (Object.keys(set).length === 0 && unset.length === 0)
return { saved: false };
const { local: held, raw } = localProjects(getConfigPath());
for (const name of unset) {
if (!held.includes(name))
throw new UsageError(`Project "${name}" is not in ${getConfigPath()}, so it cannot be removed or renamed here.`, "TARGET_NOT_UPDATABLE", "It is inherited from an extended config, or akm ignores it: edit the file that holds it.");
}
// A set is a no-op only when the file already holds exactly that entry, whatever akm makes of the file's own.
const writeProjects = Object.keys(set).filter((name) => !isDeepStrictEqual(raw[name], set[name]));
if (writeProjects.length === 0 && unset.length === 0)
return { saved: false };
const result = mutateConfig((current) => {
const projects = { ...current.projects };
for (const name of unset)
delete projects[name];
Object.assign(projects, set);
preserveUnrelatedConfig(current);
return { ...current, projects };
}, { absentNoop: Object.keys(set).length === 0, writeProjects });
resetConfigCache();
return { saved: result.written };
}
/** Where configured work names a connection: the dotted paths of every `connection` key holding `name`. */
function connectionReferences(value, name, prefix = []) {
if (!isRecord(value))
return [];
return Object.entries(value).flatMap(([key, child]) => {
const path = [...prefix, key];
if (key === "connection" && child === name)
return [path.join(".")];
return prefix.length === 0 && key === "connections" ? [] : connectionReferences(child, name, path);
});
}
/**
* Remove one connection through the same validated, atomic path as `akm config unset connections.<name>`, and say what
* the removal touches: the configured `connection` references to the name (a connection named explicitly is never
* replaced by the default, so each of them fails with "Connection is not configured" until it is changed), whether it was the default and which connection is after, and whether an
* `extends` base still defines it. A connection only a base defines is refused, as `unset` refuses it.
*/
export function removeConnection(input) {
const name = typeof input.name === "string" ? input.name.trim() : "";
if (!name)
throw new UsageError("A connection name is required.", "MISSING_REQUIRED_ARGUMENT");
const before = loadConfig();
if (!Object.hasOwn(before.connections ?? {}, name))
throw new UsageError(`No connection named "${name}".`);
const references = connectionReferences(before, name);
const wasDefault = defaultConnection(before)?.name === name;
const { saved } = change({ unset: [`connections.${name}`] });
const after = loadConfig();
return {
saved,
references,
wasDefault,
defaultConnection: defaultConnection(after)?.name ?? null,
stillInherited: Object.hasOwn(after.connections ?? {}, name),
};
}
/**
* Replace one connection with `entry` as a whole, through the same validated atomic write as `change`. Keys the entry
* no longer has (a removed alias, cleared request options) leave the file, which a dotted `set` of the entry would not
* do. A literal API key is refused; a connection only an `extends` base defines is saved as a local copy that overrides it.
*/
export function saveConnection(input) {
const name = typeof input.name === "string" ? input.name.trim() : "";
if (!name)
throw new UsageError("A connection name is required.", "MISSING_REQUIRED_ARGUMENT");
if (UNSAFE_NAMES.has(name))
throw new UsageError(`"${name}" cannot name a connection.`);
if (!isRecord(input.entry))
throw new UsageError("entry must be a connection object.");
const key = input.entry.apiKey;
if (key !== undefined && (typeof key !== "string" || !key.startsWith("$")))
throw new UsageError(`connections.${name}.apiKey must be an environment-variable reference such as $OPENAI_API_KEY, not the key itself.`);
const result = mutateConfig((current) => {
// Already exactly this: nothing to write.
if (isDeepStrictEqual(current.connections?.[name], input.entry))
return current;
preserveUnrelatedConfig(current);
return { ...current, connections: { ...current.connections, [name]: input.entry } };
});
resetConfigCache();
return { saved: result.written };
}
/**
* Set the embedding model as `akm setup` does: the same block and credential from the same answer
* (`applySetupAnswers`), the endpoint and key variable checked and normalized as the setup flags are. A pasted key is
* stored in `credentials.json` (`services.embedding`) first, so a bad answer fails before the config changes, and is
* never written to config.json. Choosing an embedder turns semantic search on. Returns the block now configured.
*/
export function saveEmbedding(input) {
const answer = embeddingAnswer(input);
const before = loadConfig();
if (answer.kind === "local" && !hasLocalEmbeddings())
throw new UsageError(noLocalEmbeddingsReason());
if (answer.kind === "connection" && defaultConnection(before) === undefined)
throw new UsageError("No default connection serves this model. Add one, or give an endpoint.", "MISSING_REQUIRED_ARGUMENT");
const { patch, credentials } = applySetupAnswers(before, { embedding: answer });
for (const { slot, key, endpoint } of credentials)
writeCredential(slot, key, endpoint);
const result = mutateConfig((current) => {
const { embedding: _held, ...rest } = current;
const next = {
...rest,
semanticSearchMode: "auto",
...(patch.embedding ? { embedding: patch.embedding } : {}),
};
if (isDeepStrictEqual(next, current))
return current;
preserveUnrelatedConfig(current);
return next;
});
resetConfigCache();
return { saved: result.written, embedding: patch.embedding ?? null };
}
/** The answer as `akm setup --embedding … --embedding-endpoint … --embedding-api-key-env …` would take it. */
function embeddingAnswer(input) {
if (!isRecord(input))
throw new UsageError("An embedding answer is required.", "MISSING_REQUIRED_ARGUMENT");
if (input.kind === "local")
return { kind: "local" };
if (input.kind !== "connection" && input.kind !== "endpoint")
throw new UsageError("kind must be local, connection or endpoint.");
const model = typeof input.model === "string" ? input.model.trim() : "";
if (!model)
throw new UsageError("An embedding model is required.", "MISSING_REQUIRED_ARGUMENT");
if (input.kind === "connection")
return { kind: "connection", model };
const { env, pasted } = isRecord(input.key) ? input.key : {};
if (input.key !== undefined && typeof env !== "string" && typeof pasted !== "string")
throw new UsageError("key must name an environment variable ({ env }) or hold the key ({ pasted }).");
const flags = parseSetupFlags({
embedding: model,
embeddingEndpoint: input.endpoint,
embeddingApiKeyEnv: typeof env === "string" ? env : undefined,
});
const key = typeof pasted === "string" ? pasted.trim() : "";
if (typeof pasted === "string" && !key)
throw new UsageError("The embedding key is empty.");
return {
kind: "endpoint",
endpoint: flags.embeddingEndpoint,
model,
...(flags.embeddingApiKeyEnv ? { key: { env: flags.embeddingApiKeyEnv } } : key ? { key: { pasted: key } } : {}),
};
}