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
108 lines (107 loc) • 5.99 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 proposal new` — the asset-authoring entry point. Shares no private helper with the proposal MANAGEMENT
* family (list/show/accept/reject/…, `./proposal-cli.ts`) — its path/name
* helpers come from the shared src/core/asset/asset-create.ts module.
*
* Keeps the inline `runWithJsonErrors` form (branches on the result and sets
* `process.exitCode` conditionally on a failed proposal) rather than
* migrating to `defineJsonCommand`, matching the original contribute-cli.ts
* rationale: `process.exitCode` (not `process.exit()`) so the process still
* exits via natural event-loop drain rather than skipping pending cleanup.
*/
import fs from "node:fs";
import path from "node:path";
import { defineCommand } from "citty";
import { getStringArg, PROJECT_ARG, parsePositiveIntFlag, parseTimeoutFlags, SCOPE_ARGS, scopeFromArgs, } from "../../cli/parse-args.js";
import { RETIRED_ENGINE_ARG, SELECTION_ARGS, selectionFromArgs } from "../../cli/selection.js";
import { failResult, GLOBAL_OUTPUT_ARGS, output, runWithJsonErrors } from "../../cli/shared.js";
import { assertFlatAssetName, combineCreatePath, normalizeCreateSubPath } from "../../core/asset/asset-create.js";
import { loadConfig } from "../../core/config/config.js";
import { agentFailureCode, UsageError } from "../../core/errors.js";
import { resolveProject } from "../../core/projects.js";
import { akmPropose } from "./propose.js";
export const proposeCommand = defineCommand({
meta: {
name: "new",
description: "Ask the configured engine to author a brand-new asset as JSON and queue it as a proposal",
},
// Raw defineCommand: declare the global output flags so their space-separated
// values are consumed rather than shifting the `type` / `name` positionals.
args: {
...GLOBAL_OUTPUT_ARGS,
// Optional in citty so run() is invoked when omitted; we re-validate
// below to surface a structured UsageError (exit 2) instead of citty's
// default help-banner exit-0.
type: { type: "positional", description: "Asset type (skill, command, knowledge, lesson, ...)", required: false },
name: {
type: "positional",
description: "Asset name (flat, no '/'; use --path for a subdirectory)",
required: false,
},
path: {
type: "string",
description: "Subdirectory under the type dir (e.g. 'release')",
},
task: { type: "string", description: "Task description for the agent (what should the asset do?)" },
file: { type: "string", description: "Read the task or prompt text from a UTF-8 file" },
bundle: {
type: "string",
description: "Writable bundle to queue the proposal for (default: project write.bundle, else the working bundle)",
},
...PROJECT_ARG,
...SELECTION_ARGS,
...RETIRED_ENGINE_ARG,
...SCOPE_ARGS,
timeout: {
type: "string",
description: "Timeout as a duration (30s, 2m) or milliseconds",
},
"timeout-ms": {
type: "string",
description: "Deprecated: use `--timeout`. Override the engine timeout in milliseconds",
},
},
async run({ args }) {
await runWithJsonErrors(async () => {
// citty silently shows help and exits 0 when required positionals are
// omitted. Re-validate explicitly so the exit code is 2 (USAGE) and a
// structured JSON error reaches scripted callers.
const taskFromFlag = typeof args.task === "string" ? args.task : undefined;
const fileFromFlag = typeof args.file === "string" ? args.file : undefined;
if (!args.type || !args.name || (!taskFromFlag && !fileFromFlag)) {
throw new UsageError("Usage: akm proposal new <type> <name> (--task '<task>' | --file <path>).", "MISSING_REQUIRED_ARGUMENT", "Provide the asset type, name, and exactly one of --task or --file.");
}
if (taskFromFlag && fileFromFlag) {
throw new UsageError("Pass exactly one of --task or --file.", "INVALID_FLAG_VALUE");
}
// `name` is flat; subdirectory placement is `--path`'s job.
assertFlatAssetName(String(args.name));
const proposedName = combineCreatePath(normalizeCreateSubPath(getStringArg(args, "path")), String(args.name));
// asset-io: user-input --file
const taskText = fileFromFlag ? fs.readFileSync(path.resolve(fileFromFlag), "utf8") : (taskFromFlag ?? "");
const timeoutMs = parseTimeoutFlags(args.timeout, parsePositiveIntFlag(args["timeout-ms"], "--timeout-ms"));
const project = resolveProject(loadConfig(), { project: getStringArg(args, "project") });
const result = await akmPropose({
type: String(args.type),
name: proposedName,
task: taskText,
selection: selectionFromArgs(args),
bundle: getStringArg(args, "bundle") ?? project?.write.bundle,
scope: scopeFromArgs(args, project),
...(project ? { metadata: project.write.metadata } : {}),
...(timeoutMs !== undefined ? { timeoutMs } : {}),
});
output("proposal-new", result);
if (result.ok === false) {
// Same reasoning as agentCommand in contribute-cli.ts: output() already
// ran, this is the last statement in the handler, so process.exitCode +
// return is equivalent without skipping any pending cleanup.
failResult(agentFailureCode(result.reason));
return;
}
});
},
});