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

141 lines (140 loc) • 6.73 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 { defineCommand } from "citty"; import { findCittyTopLevelCommandIndex } from "../../cli/invocation.js"; import { defineJsonCommand, GLOBAL_OUTPUT_ARGS, output } from "../../cli/shared.js"; import { assertKnownFlags } from "../../cli/unknown-flags.js"; import { readUsage } from "../../cli/usage.js"; import { CLI_USAGE_SOURCES } from "../../cli/usage-kinds.js"; import { UsageError } from "../../core/errors.js"; import { parseHealthSince } from "../health.js"; import { improveReportCommand } from "../improve/improve-cli.js"; import { doctorMetricsAssetsCommand } from "../read/search-cli.js"; import { METRICS_DESCRIPTION, metricsCommand } from "./metrics-cli.js"; /** * The retained invocations (30 days) that used a Deprecated command or flag, by the task they ran inside (`[none]` * outside a task): how many, and which command or flag, as typed. */ function deprecatedByTask(details) { const tasks = {}; for (const detail of details) { const deprecated = detail.deprecated; if (!deprecated) continue; const command = (detail.invokedCommand ?? detail.command).join(" "); const task = typeof detail.task === "string" ? detail.task : "[none]"; tasks[task] ??= { calls: 0, commands: {}, flags: {} }; const entry = tasks[task]; entry.calls += 1; if (deprecated.command) entry.commands[command] = (entry.commands[command] ?? 0) + 1; for (const flag of deprecated.flags) entry.flags[`${command} --${flag}`] = (entry.flags[`${command} --${flag}`] ?? 0) + 1; } return tasks; } const usageCommand = defineJsonCommand({ meta: { name: "usage", description: "Report/export local CLI attempts, arguments, outcomes and coverage. JSON aggregates have stable keys for overlap deduplication.", }, args: { since: { type: "string", description: "UTC window start: ISO date, epoch ms, or duration such as 30d; aggregates select whole UTC days", }, until: { type: "string", description: "UTC window end (inclusive day)" }, "cli-version": { type: "string", description: "Filter by exact CLI version" }, source: { type: "string", description: "Filter user, agent, task, improve, audit or unknown provenance" }, export: { type: "boolean", default: false, description: "Include retained invocation details as well as durable aggregates", }, }, run({ args }) { if (args.source && !CLI_USAGE_SOURCES.has(args.source)) { throw new UsageError("Source must be user, agent, task, improve, audit or unknown.", "INVALID_FLAG_VALUE"); } const since = args.since ? new Date(parseHealthSince(args.since)).toISOString() : undefined; const until = args.until ? new Date(parseHealthSince(args.until)).toISOString() : undefined; if (since && until && since > until) { throw new UsageError("Usage window start must not follow its end.", "INVALID_FLAG_VALUE"); } const report = readUsage({ ...(since ? { since } : {}), ...(until ? { until } : {}), ...(args["cli-version"] ? { version: args["cli-version"] } : {}), ...(args.source ? { source: args.source } : {}), }); const aggregates = report.aggregates; const commands = {}; const argumentsUsed = {}; for (const row of aggregates) { const dimensions = row.dimensions; const name = dimensions.command.join(" ") || "[root]"; commands[name] ??= { attempts: 0, completed: 0, failures: 0, durationMs: 0, }; const command = commands[name]; command.attempts += Number(row.attempts); command.completed += Number(row.completed); command.failures += Number(row.failures); command.durationMs += Number(row.duration_ms); for (const argument of dimensions.arguments.filter((arg) => arg.explicit)) { const identity = `${name}:${argument.name}`; argumentsUsed[identity] = (argumentsUsed[identity] ?? 0) + Number(row.attempts); } } output("doctor metrics usage", { ...report, details: args.export ? report.details : undefined, deprecatedByTask: deprecatedByTask(report.details), commands: Object.entries(commands).map(([command, row]) => ({ command, ...row, meanDurationMs: row.completed ? row.durationMs / row.completed : null, })), arguments: argumentsUsed, exportContract: "Deduplicate overlapping snapshots by aggregate key; retain the latest snapshot per key, never sum snapshots. Details are subsets of aggregates: deduplicate by invocation id and never add detail counts to aggregate counts.", }); }, }); const METRICS_SUBCOMMANDS = { usage: 1, improve: 1, assets: 1 }; // Retain the old leaf handler and args verbatim; citty selects a child before // running this handler, so existing bare doctor metrics output stays intact. export const doctorMetricsCommand = defineCommand({ ...metricsCommand, meta: { name: "metrics", description: METRICS_DESCRIPTION }, subCommands: { usage: usageCommand, improve: improveReportCommand, assets: doctorMetricsAssetsCommand, }, setup({ rawArgs }) { const index = findCittyTopLevelCommandIndex(rawArgs, metricsCommand.args); if (index < 0 || !(rawArgs[index] in METRICS_SUBCOMMANDS)) { // The bare report remains a leaf for flag validation even though this // wrapper also routes usage; health-only flags must still be rejected. assertKnownFlags(metricsCommand, rawArgs); return; } try { assertKnownFlags({ args: GLOBAL_OUTPUT_ARGS }, rawArgs.slice(0, index)); } catch (error) { if (!(error instanceof UsageError)) throw error; throw new UsageError(`Put ${rawArgs[index]} report options after it: akm doctor metrics ${rawArgs[index]} --since 7d.`, "INVALID_FLAG_VALUE"); } }, run(context) { if (String(context.args._[0]) in METRICS_SUBCOMMANDS) return; return metricsCommand.run?.(context); }, });