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
122 lines (121 loc) • 6.08 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/.
/**
* `akm models` — the one place to see and change which model answers each alias
* (`fast`, `balanced`, `reasoning`, and any the user adds) on a connection.
*
* akm models [--connection NAME] the picks, then what the endpoint lists
* akm models set <alias> <model> [--connection NAME] change one
*
* Connections only: a harness's alias map is `harnesses.<name>.models`, edited
* with `akm config set`.
*/
import { defineGroupCommand, defineJsonCommand, GLOBAL_OUTPUT_ARGS, output } from "../cli/shared.js";
import { defaultConnection, loadConfig, mutateConfig, } from "../core/config/config.js";
import { resolveApiKey } from "../core/config/credentials.js";
import { entryModel, entryWithModel } from "../core/config/model-aliases.js";
import { ConfigError, UsageError } from "../core/errors.js";
import { redactUrlCredentials } from "../core/redaction.js";
import { warn } from "../core/warn.js";
import { selectionNotConfigured } from "../integrations/agent/resolution.js";
import { listEndpointModels } from "../llm/model-list.js";
const CONNECTION_ARG = {
connection: {
type: "string",
description: "The connection to show or change (default: the default connection)",
},
};
/** The connection to show or change: the one named, else the default. */
function targetConnection(config, name) {
if (name !== undefined) {
const connection = config.connections?.[name];
if (connection)
return { name, connection };
throw selectionNotConfigured(config, "connection", name);
}
const found = defaultConnection(config);
if (found)
return found;
throw new ConfigError("No connection is configured, so there are no model aliases to show or change. Run `akm setup` to add one.", "LLM_NOT_CONFIGURED");
}
/** The models the connection's endpoint lists, or none when it cannot say. */
async function availableModels(name, connection, probe) {
if (!probe)
return [];
const apiKey = resolveApiKey(connection.apiKey, { connection: name }, connection.endpoint);
const listed = await listEndpointModels({ endpoint: connection.endpoint, ...(apiKey ? { apiKey } : {}) });
if (listed.ok)
return listed.models;
warn(`Could not list models from ${redactUrlCredentials(connection.endpoint)} (${listed.error}).`);
return [];
}
/** Probing is the default, so only the opt-out is declared; citty parses `--no-probe` as `probe: false` (see `setup`'s flag in `cli.ts`). */
const PROBE_ARG = {
"no-probe": {
type: "boolean",
description: "Do not ask the endpoint which models it lists.",
},
};
const probes = (args) => args.probe !== false;
export const modelsCommand = defineGroupCommand({
meta: {
name: "models",
description: "Show which model answers each alias on a connection, then the models its endpoint lists; `models set` changes one",
},
// The group runs the list itself, so it declares the output flags a leaf command would get from `defineJsonCommand`.
args: { ...GLOBAL_OUTPUT_ARGS, ...CONNECTION_ARG, ...PROBE_ARG },
// `akm models` is itself the list, so the bare group is not a usage error here.
async defaultRun({ args }) {
const { name, connection } = targetConnection(loadConfig(), args.connection);
const aliases = Object.fromEntries(Object.entries(connection.models ?? {}).map(([alias, entry]) => [alias, entryModel(entry)]));
output("models", { connection: name, aliases, available: await availableModels(name, connection, probes(args)) });
},
subCommands: {
set: defineJsonCommand({
meta: {
name: "set",
description: "Make a model answer an alias on a connection, e.g. `akm models set reasoning glm-5`; an alias with request fields keeps them",
},
args: {
alias: { type: "positional", required: true, description: "Alias: fast, balanced, reasoning, or your own" },
model: { type: "positional", required: true, description: "The model's name on the endpoint" },
...CONNECTION_ARG,
...PROBE_ARG,
},
async run({ args }) {
const alias = args.alias.trim();
const model = args.model.trim();
if (!alias || !model) {
throw new UsageError("Name an alias and a model: `akm models set <alias> <model>`.", "MISSING_REQUIRED_ARGUMENT");
}
const { name, connection } = targetConnection(loadConfig(), args.connection);
const available = await availableModels(name, connection, probes(args));
if (available.length > 0 && !available.includes(model)) {
warn(`"${model}" is not among the models ${name} lists; setting it anyway.`);
}
const before = connection.models?.[alias];
mutateConfig((current) => {
const target = targetConnection(current, name);
const held = target.connection.models?.[alias];
return {
...current,
connections: {
...current.connections,
[name]: {
...target.connection,
models: { ...target.connection.models, [alias]: entryWithModel(held, model) },
},
},
};
});
output("models-set", {
connection: name,
alias,
model,
previous: before === undefined ? null : entryModel(before),
});
},
}),
},
});