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
84 lines (83 loc) • 4.07 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's read-only tools for agent work, in harness-neutral form (#1244): the
* `akm` capability. A harness adapts each one to its own tool API; the tool runs
* in akm's process and returns what `akm --detail agent <command> --format json`
* would print.
*/
import { renderOutput } from "../../cli/shared.js";
import { akmSearch } from "../../commands/read/search.js";
import { runCurate } from "../../commands/read/search-cli.js";
import { akmShowUnified } from "../../commands/read/show.js";
import { loadConfig } from "../../core/config/config.js";
import { withQuiet } from "../../core/warn.js";
import { resolveUsageEventSource } from "../../indexer/usage/usage-events.js";
import { resolveOutputMode } from "../../output/context.js";
import { stdoutText } from "../../output/stdout.js";
/** The usage-event source a tool call records: the run's own, validated; the default only for a run with none. */
const sourceOf = (context) => context?.eventSource === undefined ? undefined : resolveUsageEventSource({ AKM_EVENT_SOURCE: context.eventSource });
const str = (value) => typeof value === "string" && value.trim() !== "" ? value.trim() : undefined;
const positiveInt = (value) => typeof value === "number" && Number.isInteger(value) && value > 0 ? value : undefined;
/** Run one read command the way the CLI does with `--detail agent --format json`, quietly. */
function asCli(command, compute) {
return withQuiet(async () => {
const mode = resolveOutputMode(["--detail", "agent", "--format", "json"], loadConfig().output ?? {});
return stdoutText(renderOutput(command, await compute(mode), { ...mode, format: "json" }));
});
}
function required(input, key) {
const value = str(input[key]);
if (!value)
throw new Error(`"${key}" is required`);
return value;
}
export const AKM_READ_TOOLS = [
{
name: "akm_curate",
description: "Find the akm assets (skills, knowledge, memories, workflows) that best fit a task. Read one with akm_show before relying on it.",
inputSchema: {
type: "object",
properties: {
query: { type: "string", description: "The task or question" },
type: { type: "string", description: "Optional asset type filter" },
limit: { type: "integer", minimum: 1 },
},
required: ["query"],
},
run: (input, context) => asCli("curate", (mode) => runCurate({
query: required(input, "query"),
type: str(input.type),
limit: positiveInt(input.limit) === undefined ? undefined : String(input.limit),
eventSource: sourceOf(context),
// Model work is not a place the user works: no project's defaults apply.
project: "none",
}, mode)),
},
{
name: "akm_search",
description: "Search akm assets by name or keyword.",
inputSchema: {
type: "object",
properties: {
query: { type: "string" },
type: { type: "string", description: "Optional asset type filter" },
limit: { type: "integer", minimum: 1 },
},
required: ["query"],
},
run: (input, context) => asCli("search", () => akmSearch({
query: required(input, "query"),
type: str(input.type),
limit: positiveInt(input.limit),
eventSource: sourceOf(context),
})),
},
{
name: "akm_show",
description: "Read one akm asset by its ref, as returned by akm_curate or akm_search.",
inputSchema: { type: "object", properties: { ref: { type: "string" } }, required: ["ref"] },
run: (input, context) => asCli("show", () => akmShowUnified({ ref: required(input, "ref"), eventSource: sourceOf(context) })),
},
];