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
174 lines (173 loc) • 8.3 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 repeatable `--env <ref>` and `--secret <ref>:<VAR>` flags of `akm run`
* and `akm exec`: what they resolve to, and the one child run `akm exec`
* makes (`runProgram`). Env assets apply in the order given, then secrets over them; a later
* flag wins over an earlier one for the same variable. Resolution keeps every
* invariant of `resolveEnvBinding` (nothing is injected when a ref is missing;
* audit events carry names only).
*/
import fs from "node:fs";
import path from "node:path";
import { getParsedInvocation } from "../../cli/invocation.js";
import { readSecret } from "../../core/env-secret-ref.js";
import { NotFoundError, UsageError } from "../../core/errors.js";
import { appendEvent } from "../../core/events.js";
import { parseSecretSpec } from "../../core/ref-token.js";
import { endBySignal } from "../../core/signal-observers.js";
import { runManagedSubprocess } from "../../core/subprocess.js";
import { withRetries } from "../../execution/retry.js";
import { isDirectory } from "../../sources/providers/provider-utils.js";
import { resolveEnvBinding } from "./env-binding.js";
export const INJECTION_ARGS = {
env: {
type: "string",
description: "Inject every variable of this env asset (`[bundle//]env/<name>`); repeatable",
},
secret: {
type: "string",
description: "Inject a secret's value as a variable: `<secret-ref>:<VAR>`; repeatable",
},
};
const INJECTION_FLAGS = ["--env", "--secret"];
/** Resolve the `--env` and `--secret` flags of `invocation` (this process's own by default). */
export function resolveInjections(invocation = getParsedInvocation()) {
return resolveInjectionRefs({
env: invocation.getAllFlagValues("--env"),
secret: invocation.getAllFlagValues("--secret"),
});
}
/** Resolve env asset refs, in order, then `<secret-ref>:<VAR>` specs over them. */
export function resolveInjectionRefs(refs) {
const values = {};
const secretValues = [];
// Parse every secret spec first, so a malformed one fails before any ref is read or audited.
const secrets = (refs.secret ?? []).map((spec) => {
const parsed = parseSecretSpec(spec);
if (parsed === undefined) {
throw new UsageError(`--secret takes <secret-ref>:<VAR>, for example secrets/deploy-token:GITHUB_TOKEN; got ${JSON.stringify(spec)}.`, "INVALID_FLAG_VALUE");
}
return parsed;
});
for (const ref of refs.env ?? []) {
const binding = resolveEnvBinding(ref);
Object.assign(values, binding.values);
secretValues.push(...binding.secretValues);
}
for (const { ref, variable } of secrets) {
const { ref: secretRef, value } = readSecret(ref);
values[variable] = value;
secretValues.push(value);
appendEvent({ eventType: "secret_access", ref: secretRef, metadata: { var: variable } });
}
return { values, secretValues };
}
/** `rawArgs` without the given flags and their values (`booleanFlags` take none); a `--` tail is kept as it is. */
export function withoutFlags(rawArgs, valueFlags, booleanFlags = []) {
const kept = [];
for (let i = 0; i < rawArgs.length; i++) {
const arg = rawArgs[i];
if (arg === "--")
return [...kept, ...rawArgs.slice(i)];
if (valueFlags.includes(arg))
i++;
else if (!booleanFlags.includes(arg) && !valueFlags.some((flag) => arg.startsWith(`${flag}=`)))
kept.push(arg);
}
return kept;
}
/** `rawArgs` without the injection flags and their values; a `--` tail is kept as it is. */
export function withoutInjectionFlags(rawArgs) {
return withoutFlags(rawArgs, INJECTION_FLAGS);
}
function refuseWorkingFolder(folder, code, where) {
return new UsageError(`--cwd ${folder} cannot be made: ${code ?? "an error"} on ${where}`, "INVALID_FLAG_VALUE");
}
/**
* Refuse a working folder that cannot be made. An existing path must be a folder; otherwise the nearest
* existing ancestor must be a folder this process can write to and enter. Returns the folder, absolute.
*/
export function assertWorkingFolderCanBeMade(cwd) {
const folder = path.resolve(cwd);
// asset-io: user-input --cwd folder
if (fs.existsSync(folder)) {
// asset-io: user-input --cwd folder
if (!fs.statSync(folder).isDirectory())
throw new UsageError("--cwd <path> is not a folder", "INVALID_FLAG_VALUE");
return folder;
}
let ancestor = path.dirname(folder);
// asset-io: user-input --cwd folder
while (!fs.existsSync(ancestor))
ancestor = path.dirname(ancestor);
try {
// asset-io: user-input --cwd folder
if (!fs.statSync(ancestor).isDirectory())
throw Object.assign(new Error("not a folder"), { code: "ENOTDIR" });
fs.accessSync(ancestor, fs.constants.W_OK | fs.constants.X_OK);
}
catch (error) {
throw refuseWorkingFolder(folder, error.code, ancestor);
}
return folder;
}
/** Make `cwd` (and any parents) if missing and return it absolute; a folder that cannot be made is refused ({@link assertWorkingFolderCanBeMade}); a relative one is from the folder akm was started in. */
export function ensureWorkingFolder(cwd) {
const folder = assertWorkingFolderCanBeMade(cwd);
try {
fs.mkdirSync(folder, { recursive: true });
}
catch (error) {
throw refuseWorkingFolder(folder, error.code, path.dirname(folder));
}
return folder;
}
/**
* Run `argv` with `env`, no shell: one program and its arguments. Each try gets the whole
* `timeoutMs`; a failed try (non-zero exit or timeout) is retried `maxRetries` times. A program
* that cannot be started, or a run the caller aborted, is not retried.
*/
export async function runProgram(options) {
const { argv, env, maxRetries = 0 } = options;
const cwd = options.cwd === undefined ? undefined : ensureWorkingFolder(options.cwd);
const childEnv = {};
for (const [name, value] of Object.entries(env))
if (value !== undefined)
childEnv[name] = value;
const result = await withRetries(maxRetries, () => runManagedSubprocess([...argv], {
capture: options.capture,
env: childEnv,
timeoutMs: options.timeoutMs ?? null,
...(cwd !== undefined ? { cwd } : {}),
...(options.signal ? { signal: options.signal } : {}),
}), (r) => r.spawnError === undefined && !r.aborted && (r.timedOut || r.exitCode !== 0));
if (result.spawnError)
throw spawnFailure(argv[0], result.spawnError, options.script);
// A passthrough child's status is akm's: one that died of a signal ends akm by the same signal.
if (!options.capture && result.signal && !result.timedOut && !result.aborted)
endBySignal(result.signal);
return result;
}
/** The error for a program that could not be started; `script` names the script whose command it was. */
export function spawnFailure(program, error, script) {
const code = error.code;
if (code === "ENOENT" && script) {
return new NotFoundError(script.firstLine
? `${script.ref}: its first line ${script.firstLine} names ${program}, which was not found`
: `${script.ref}: ${program}, the program for its extension, was not found`, "FILE_NOT_FOUND", script.firstLine
? `Install '${program}', or correct the script's first line.`
: `Install '${program}' or add its directory to PATH, or add a \`#!\` first line naming the program that runs the script.`);
}
if (code === "ENOENT") {
return new NotFoundError(`Command not found: ${program}`, "FILE_NOT_FOUND", `Install '${program}' or add its directory to PATH.`);
}
if (code === "EACCES") {
if (isDirectory(program)) {
return new NotFoundError(`Command not executable: ${program} is a folder, not a program`, "COMMAND_NOT_EXECUTABLE", "Name the program inside it.");
}
return new NotFoundError(`Command not executable: ${program}`, "COMMAND_NOT_EXECUTABLE");
}
return error;
}