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
64 lines (63 loc) • 3.13 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-cli/api` — the supported in-process recall entry point.
* See docs/reference/api.md for this and the separate local workspace contract.
*
* Each export runs the same code path as its CLI command and computes the
* document the CLI would print, in-process: no child process, no write to
* stdout/stderr, no `process.chdir`, no change to the host's `process.env`,
* and no process-level output-mode, quiet or verbose state left changed.
*/
import { renderOutput } from "./cli/shared.js";
import { runCurate } from "./commands/read/search-cli.js";
import { loadConfig } from "./core/config/config.js";
import { UsageError } from "./core/errors.js";
import { normalizeInclude } from "./core/include.js";
import { withQuiet } from "./core/warn.js";
import { installLlmUsagePersistenceIfAbsent } from "./llm/usage-persist.js";
import { resolveOutputMode } from "./output/context.js";
import { stdoutText } from "./output/stdout.js";
/** `ScopeFilters` as the `key=value` tokens of repeated `--filter`/`--prefer` flags. */
function scopeFlagTokens(scope) {
return scope === undefined
? undefined
: Object.entries(scope).flatMap(([key, value]) => [value ?? []].flat().map((v) => `${key}=${v}`));
}
/**
* Returns exactly the stdout text of
* `akm --detail agent -q curate <query> [--limit N] [--type T] --format <format>`
* including its trailing newline. Rejects with the error the CLI
* would fail with: its message, and its error code on the `code` property.
*/
export async function curate(query, options = {}) {
const format = options.format ?? "json";
if (format !== "text" && format !== "json") {
throw new UsageError(`Invalid value for --format: ${format}. Expected one of: json|text`, "INVALID_FORMAT_VALUE");
}
return withQuiet(async () => {
// Same startup order as the CLI: config first (an invalid config fails
// the call before anything runs), then the output mode from argv + config.
const mode = resolveOutputMode(["--detail", "agent", "--format", format], loadConfig().output ?? {});
const disposeLlmUsageSink = installLlmUsagePersistenceIfAbsent();
try {
const curated = await runCurate({
query,
type: options.type,
limit: options.limit === undefined ? undefined : String(options.limit),
from: options.source,
belief: options.belief,
filters: scopeFlagTokens(options.filters),
prefer: scopeFlagTokens(options.prefer),
include: scopeFlagTokens(normalizeInclude(options.include)),
cwd: options.cwd,
project: options.project?.trim() || undefined,
}, mode);
return stdoutText(renderOutput("curate", curated, { ...mode, format }));
}
finally {
disposeLlmUsageSink();
}
});
}