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
59 lines (58 loc) • 2.86 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 flags a start of a version 5 task takes, beside its input flags: `--env`,
* `--secret`, `--cwd`, `--timeout`, `--max-retries`, `--inherit`, `--resume` and
* `--skip-if-locked`. `akm run <task>` and `akm task run <task>` (the scheduler's
* form) both read a start through `parseStartFlags`.
*/
import path from "node:path";
import { createParsedInvocation } from "../../cli/invocation.js";
import { parseNonNegativeIntFlag } from "../../cli/parse-args.js";
import { withoutFlags } from "../../commands/env/injection.js";
import { TASK_RUN_VALUE_FLAGS } from "../task-run-reserved-flags.js";
const VALUE_FLAGS = ["--env", "--secret", "--cwd", "--timeout", "--max-retries", "--inherit"];
const BOOLEAN_FLAGS = ["--resume", "--skip-if-locked"];
/** The start flags in `rawArgs`, and the rest, which are the task id and its input flags. */
export function parseStartFlags(rawArgs) {
const invocation = createParsedInvocation(["", "", ...rawArgs]);
const env = invocation.getAllFlagValues("--env");
const secret = invocation.getAllFlagValues("--secret");
const inherit = invocation
.getAllFlagValues("--inherit")
.flatMap((list) => list.split(/[,\s]+/))
.filter(Boolean);
const cwd = invocation.getFlagValue("--cwd");
const timeout = invocation.getFlagValue("--timeout");
const maxRetries = parseNonNegativeIntFlag(invocation.getFlagValue("--max-retries"), "--max-retries");
return {
flags: {
...(env.length > 0 ? { env } : {}),
...(secret.length > 0 ? { secret } : {}),
...(inherit.length > 0 ? { inherit } : {}),
...(cwd !== undefined ? { cwd: path.resolve(cwd) } : {}),
...(timeout !== undefined ? { timeout } : {}),
...(maxRetries !== undefined ? { maxRetries } : {}),
},
resume: invocation.hasFlag("--resume"),
skipIfLocked: invocation.hasFlag("--skip-if-locked"),
rest: withoutFlags(rawArgs, VALUE_FLAGS, BOOLEAN_FLAGS),
};
}
/**
* The task a command's tokens name: the first positional once the start flags and the values of the task run's value
* flags are set aside. A start flag before it takes its value from the next token, which citty, not knowing the flag,
* reads as the task.
*/
export function taskIdInStart(rawArgs) {
const { rest } = parseStartFlags(rawArgs);
for (let index = 0; index < rest.length; index += 1) {
const token = rest[index];
if (!token.startsWith("-"))
return token;
if (!token.includes("=") && TASK_RUN_VALUE_FLAGS.includes(token.slice(2)))
index += 1;
}
return undefined;
}