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

161 lines (160 loc) • 7.68 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 { UsageError } from "../core/errors.js"; import { EXECUTION_MAX_TIMEOUT_MS } from "../execution/limits.js"; // findCittyTopLevelCommand(Index) + CittyArg(s)DefinitionForScan moved to // ./invocation (argv normalization); re-exported here so existing // importers (tests/tasks-embedded.test.ts, commands/read/show.ts) are unaffected. export { findCittyTopLevelCommand, findCittyTopLevelCommandIndex, } from "./invocation.js"; /** * Return true when `args._[0]` is a member of `validSet`. * * Citty exposes unknown subcommands as `args._[0]` (the first positional). * Several top-level commands (config, vault, wiki, workflow, tasks) need to * detect whether a recognised subcommand was supplied so they can show a help * banner rather than an unhelpful "unknown command" error. * * @param args Parsed citty argument object (must have an `_` array). * @param validSet The set of recognised subcommand names for this command. */ export function hasSubcommand(args, validSet) { const command = Array.isArray(args._) ? args._[0] : undefined; return typeof command === "string" && validSet.has(command); } // ── Numeric flag parsing ───────────────────────────────────────────────────── /** * Parse a `--limit`-style flag value into a positive integer. * * Returns `undefined` when `raw` is `undefined` or empty (flag not supplied). * Throws `UsageError` when the raw value is present but not a valid positive * integer so the caller gets a structured, machine-readable error response. * * @param raw The raw string value from citty (may be undefined). * @param flagName The flag name to include in the error message (e.g. `"--limit"`). */ export function parsePositiveIntFlag(raw, flagName = "--limit") { if (raw === undefined) return undefined; const trimmed = raw.trim(); if (!trimmed) return undefined; // Strict digits: parseInt stops at the first non-digit, so "10x" silently // became 10, "3.5" became 3, and "5 apples" became 5 — accepted rather than // rejected as invalid. if (!/^\d+$/.test(trimmed)) { throw new UsageError(`Invalid ${flagName} value: "${raw}". Must be a positive integer.`, "INVALID_FLAG_VALUE"); } const parsed = parseInt(trimmed, 10); if (parsed <= 0) { throw new UsageError(`Invalid ${flagName} value: "${raw}". Must be a positive integer.`, "INVALID_FLAG_VALUE"); } return parsed; } /** * Parse a `--max-retries`-style flag value into a whole number, 0 or more, as `max-retries:` is read in a task file. * Returns `undefined` when the flag was not supplied. */ export function parseNonNegativeIntFlag(raw, flagName) { if (raw === undefined) return undefined; const trimmed = raw.trim(); if (!trimmed) return undefined; if (!/^\d+$/.test(trimmed) || !Number.isSafeInteger(Number(trimmed))) { throw new UsageError(`Invalid ${flagName} value: "${raw}". Must be a whole number, 0 or more.`, "INVALID_FLAG_VALUE"); } return Number(trimmed); } /** * The one timeout grammar, for `--timeout` flags and asset `timeout:` fields: bare N * is milliseconds, or N with `ms`, `s`, `m`, `h` or `d`. `--since`/`--expires` use the * shared duration grammar in core/time. */ const TIMEOUT_UNITS = { ms: 1, s: 1000, m: 60_000, h: 3_600_000, d: 86_400_000, }; /** A timeout in the `--timeout` grammar as milliseconds, or null when it is not in the grammar. Zero is read as zero. */ export function timeoutMilliseconds(raw) { const match = /^(\d+)(ms|s|m|h|d)?$/.exec(raw.trim()); if (!match) return null; const milliseconds = Number(match[1]) * TIMEOUT_UNITS[match[2] ?? "ms"]; return Number.isSafeInteger(milliseconds) ? milliseconds : null; } /** Parse one `--timeout` value to positive milliseconds, or throw a usage error. */ export function parseTimeoutValue(raw, flagName = "--timeout") { const parsed = timeoutMilliseconds(raw); if (parsed === null || parsed <= 0) { throw new UsageError(`Invalid ${flagName} value: "${raw}". Expected a positive number: bare N is milliseconds, or N with ms, s, m, h or d (30s, 2m, 1h, 7d).`, "INVALID_FLAG_VALUE"); } if (parsed > EXECUTION_MAX_TIMEOUT_MS) { throw new UsageError(`Invalid ${flagName} value: "${raw}". A timeout is at most ${EXECUTION_MAX_TIMEOUT_MS} milliseconds (24 days).`, "INVALID_FLAG_VALUE"); } return parsed; } /** Resolve the duration alias against an already-parsed legacy millisecond flag. */ export function parseTimeoutFlags(timeout, timeoutMs) { if (timeout === undefined) return timeoutMs; const parsed = parseTimeoutValue(timeout); if (timeoutMs !== undefined && timeoutMs !== parsed) { throw new UsageError("--timeout and --timeout-ms specify conflicting budgets.", "INVALID_FLAG_VALUE"); } return parsed; } // ── String flag parsing ────────────────────────────────────────────────────── /** * Extract a string value from a parsed citty argument object by key. * * Returns the trimmed string when present and non-empty, or `undefined` * otherwise. Eliminates the repeated * `typeof args.X === "string" && args.X.trim() ? args.X.trim() : undefined` * pattern throughout the CLI command handlers. * * @param args The citty argument object (typed as unknown for flexibility). * @param key The argument key to look up. */ export function getStringArg(args, key) { const val = args[key]; if (typeof val !== "string") return undefined; const trimmed = val.trim(); return trimmed || undefined; } /** A string argument exactly as given: an empty or blank one is kept, so the command that needs text can refuse it. */ export function getExactStringArg(args, key) { const val = args[key]; return typeof val === "string" ? val : undefined; } /** `--project`: the configured project to use instead of the one the working directory selects (`none`: no project). */ export const PROJECT_ARG = { project: { type: "string", description: "Configured project to use instead of the one the working directory selects (`none`: no project)", }, }; /** The scope flags `remember` takes, for the proposal commands that stamp a scope on what they queue. */ export const SCOPE_ARGS = { user: { type: "string", description: "Scope the proposal to a user id (persisted as `scope_user` frontmatter)" }, agent: { type: "string", description: "Scope the proposal to an agent id (persisted as `scope_agent` frontmatter)" }, run: { type: "string", description: "Scope the proposal to a run id (persisted as `scope_run` frontmatter)" }, channel: { type: "string", description: "Scope the proposal to a channel name (persisted as `scope_channel` frontmatter)", }, }; /** The scope the {@link SCOPE_ARGS} flags name, each absent one filled from the project's `write`; empty when none is given. */ export function scopeFromArgs(args, project) { const scope = {}; for (const key of ["user", "agent", "run", "channel"]) { const value = getStringArg(args, key) ?? (key === "run" ? undefined : project?.write[key]); if (value) scope[key] = value; } return scope; }