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

105 lines (104 loc) • 5.05 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/. import { getExactStringArg, getStringArg, parseNonNegativeIntFlag, parsePositiveIntFlag, parseTimeoutFlags, } from "../../cli/parse-args.js"; import { RETIRED_ENGINE_ARG, SELECTION_ARGS, selectionFromArgs } from "../../cli/selection.js"; import { defineGroupCommand, defineJsonCommand, failResult, output } from "../../cli/shared.js"; import { loadConfig } from "../../core/config/config.js"; import { agentFailureCode } from "../../core/errors.js"; import { isVerbose, setVerbose, warn, warnVerbose } from "../../core/warn.js"; import { retryDispatch } from "../../execution/retry.js"; import { lookupBundleRefReadonly } from "../../indexer/indexer.js"; import { ensureWorkingFolder } from "../env/injection.js"; import { dispatchPreparedCommandInvocation, inspectPreparedCommandInvocation, prepareCommandInvocation, } from "./command-execution.js"; export const commandRunCommand = defineJsonCommand({ meta: { name: "run", description: "Resolve a stored command through its bundle adapter, apply portable arguments, and execute one fresh session", }, args: { ref: { type: "positional", description: "Command asset ref (for example commands/review or team//commands/review)", required: true, }, arguments: { type: "string", description: "Exact string substituted for each literal $ARGUMENTS occurrence (no tokenization or trimming)", }, agent: { type: "string", description: "Override the command's agent selector with a portable agent ref or native harness selector", }, ...SELECTION_ARGS, ...RETIRED_ENGINE_ARG, timeout: { type: "string", description: "Timeout as a duration (30s, 2m) or milliseconds", }, "timeout-ms": { type: "string", description: "Deprecated: use `--timeout`. Override the execution timeout in milliseconds", }, "max-retries": { type: "string", hidden: true, description: "Run again up to N times when the command fails" }, cwd: { type: "string", description: "Override the execution workspace/current working directory" }, "dry-run": { type: "boolean", default: false, description: "Authorize and lower the command, then print safe diagnostics without dispatching or writing state", }, }, async run({ args }) { if (args.verbose === true) setVerbose(true); const timeoutMs = parseTimeoutFlags(getExactStringArg(args, "timeout"), parsePositiveIntFlag(getExactStringArg(args, "timeout-ms"), "--timeout-ms")); const current = {}; const agent = getStringArg(args, "agent"); const workspace = getStringArg(args, "cwd"); if (agent !== undefined) current.agent = agent; Object.assign(current, selectionFromArgs(args)); if (timeoutMs !== undefined) current.timeout = timeoutMs; if (workspace !== undefined) current.workspace = workspace; const argumentInput = getExactStringArg(args, "arguments"); const dryRun = args["dry-run"] === true; if (workspace !== undefined && !dryRun) ensureWorkingFolder(workspace); const prepared = await prepareCommandInvocation({ action: { ref: args.ref, ...(argumentInput === undefined ? {} : { arguments: argumentInput }), }, config: loadConfig(), current: current, ...(dryRun ? { sourceLookup: lookupBundleRefReadonly } : {}), }); const diagnostics = dryRun || isVerbose() ? inspectPreparedCommandInvocation(prepared) : undefined; if (dryRun) { output("command-dry-run", diagnostics); return; } if (diagnostics) { warnVerbose(`[akm:command] diagnostics ${JSON.stringify({ engine: diagnostics.engine, provenance: diagnostics.provenance, notices: diagnostics.notices, })}`); } const result = await retryDispatch(parseNonNegativeIntFlag(getExactStringArg(args, "max-retries"), "--max-retries") ?? 0, () => dispatchPreparedCommandInvocation(prepared)); for (const message of result.warnings ?? []) warn(message); output("agent-result", result); if (!result.ok) failResult(agentFailureCode(result.reason)); }, }); export const commandCommand = defineGroupCommand({ meta: { name: "command", description: "Deprecated, removed in 0.11: use `akm run <ref>`. Run reusable agent prompt templates through AKM's common execution resolver", }, subCommands: { run: commandRunCommand }, });