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

172 lines (171 loc) • 7.79 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/. /** * CLI surface for `akm proposal extract` (see * src/commands/proposal/proposal-cli.ts for its registration). * * Examples: * akm proposal extract --type claude --session-id <id> * akm proposal extract --type claude --since 24h * akm proposal extract --type opencode --since 7d --dry-run * akm proposal extract --type codex --since 24h * akm proposal extract --auto # iterate all available harnesses * akm proposal extract --type claude --location /custom/path --session-id <id> * * Output is the AkmExtractResult JSON envelope (or an aggregated one when * `--auto` runs multiple harnesses). */ import { getStringArg, PROJECT_ARG, parseTimeoutFlags, SCOPE_ARGS, scopeFromArgs } from "../../cli/parse-args.js"; import { RETIRED_ENGINE_ARG, SELECTION_ARGS, selectionFromArgs } from "../../cli/selection.js"; import { defineJsonCommand, failResult, output } from "../../cli/shared.js"; import { loadConfig } from "../../core/config/config.js"; import { UsageError } from "../../core/errors.js"; import { resolveProject } from "../../core/projects.js"; import { getAvailableHarnesses } from "../../integrations/session-logs/index.js"; import { akmExtract, resolveStandaloneExtractPlan } from "./extract.js"; export const extractCommand = defineJsonCommand({ meta: { name: "extract", description: "Extract durable insights from native session files (claude, codex, opencode) and queue them as proposals.", }, args: { sessions: { type: "string", description: "Session source: claude, codex or opencode. Required unless --auto.", }, type: { type: "string", description: "Deprecated alias of --sessions.", }, "session-id": { type: "string", description: "Process only this session ID. When absent, discover sessions via --since.", }, location: { type: "string", description: "Override the harness's default session-discovery location.", }, since: { type: "string", description: "Discovery cutoff. ISO timestamp or duration (24h, 7d, 30m). Default 24h.", }, auto: { type: "boolean", description: "Iterate every available harness with default --since. Mutually exclusive with --sessions.", default: false, }, "dry-run": { type: "boolean", description: "Show candidates without queuing proposals.", default: false, }, force: { type: "boolean", description: "Re-process sessions already extracted with no new events", default: false, }, timeout: { type: "string", description: "Per-session timeout as a duration (30s, 2m) or milliseconds" }, "timeout-ms": { type: "string", description: "Deprecated: use `--timeout`. Per-session LLM timeout in ms. Default: the engine's own timeout.", }, ...SELECTION_ARGS, ...RETIRED_ENGINE_ARG, ...SCOPE_ARGS, ...PROJECT_ARG, strategy: { type: "string", description: "Improve strategy supplying extract behavior.", }, }, async run({ args }) { const sessionsFlag = getStringArg(args, "sessions"); const typeFlag = getStringArg(args, "type"); if (sessionsFlag !== undefined && typeFlag !== undefined) { throw new UsageError("--sessions and --type are the same selector. Pass only --sessions.", "INVALID_FLAG_VALUE"); } const type = sessionsFlag ?? typeFlag ?? ""; const sessionId = getStringArg(args, "session-id") ?? ""; const location = getStringArg(args, "location") ?? ""; const since = getStringArg(args, "since") ?? ""; const auto = args.auto === true; const dryRun = args["dry-run"] === true; const force = args.force === true; const strategy = getStringArg(args, "strategy"); const legacyTimeoutMs = typeof args["timeout-ms"] === "string" && args["timeout-ms"] !== "" ? Number.parseInt(args["timeout-ms"], 10) : undefined; if (legacyTimeoutMs !== undefined && (!Number.isFinite(legacyTimeoutMs) || legacyTimeoutMs <= 0)) { throw new UsageError(`--timeout-ms must be a positive integer (got "${args["timeout-ms"]}").`, "INVALID_FLAG_VALUE"); } const timeoutMs = parseTimeoutFlags(args.timeout, legacyTimeoutMs); if (auto && type) { throw new UsageError("--auto and --sessions are mutually exclusive. Pick one.", "INVALID_FLAG_VALUE"); } if (!auto && !type) { throw new UsageError("--sessions is required (or pass --auto to try every available harness).", "MISSING_REQUIRED_ARGUMENT"); } const config = loadConfig(); const resolvedPlan = resolveStandaloneExtractPlan(config, { ...selectionFromArgs(args), ...(strategy ? { strategy } : {}), ...(timeoutMs !== undefined ? { timeoutMs } : {}), }); // The project is chosen per session from its own directory; `--project` names one for every session. const projectName = getStringArg(args, "project"); resolveProject(config, { project: projectName }); const scope = scopeFromArgs(args); const commonOptions = Object.freeze({ ...(projectName !== undefined ? { project: projectName } : {}), ...(Object.keys(scope).length > 0 ? { scope } : {}), ...(sessionId ? { sessionId } : {}), ...(location ? { location } : {}), ...(since ? { since } : {}), dryRun, force, config, resolvedPlan, }); if (auto) { const harnesses = getAvailableHarnesses(); if (harnesses.length === 0) { output("extract", { schemaVersion: 1, ok: false, shape: "extract-auto-result", warnings: ["no available harnesses found on this machine"], results: [], }); return; } const results = []; for (const h of harnesses) { const result = await akmExtract({ type: h.name, ...commonOptions }); results.push(result); } const ok = results.every((r) => r.ok); const totalProposals = results.reduce((sum, r) => sum + r.proposals.length, 0); output("extract", { schemaVersion: 1, ok, shape: "extract-auto-result", dryRun, harnessesProcessed: results.length, totalProposals, results, }); // Signal failure to callers/schedulers when every harness failed. output() // only renders; without this a scheduled run exits 0 on a total failure // and the breakage is invisible to exit-code monitoring. process.exitCode // (not process.exit) lets stdout flush before the process exits. if (!ok) failResult("HARNESS_SESSIONS_UNAVAILABLE"); return; } const result = await akmExtract({ type, ...commonOptions }); output("extract", result); if (!result.ok) failResult("HARNESS_SESSIONS_UNAVAILABLE"); }, });