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

63 lines (62 loc) • 3.11 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/. /** * The registry of akm's model judges (#1306). A judge registers once, with the * schema of everything it reads, the schema of its verdict and a schema version. * `akm judge <name>` and akm's own pipeline both call a judge through its entry * (`run`), so the command and the pipeline cannot drift. A judge failure is an * outcome of its own (`error`, `unparseable`), never a verdict. */ import { pairJudge } from "../commands/improve/consolidate/pair-pass.js"; import { memoryTagsJudge } from "../commands/improve/memory-enrichment.js"; import { lessonQualityJudge, reflectQualityJudge } from "../commands/improve/stage.js"; import { judgmentJudge } from "../commands/proposal/drain.js"; import { ConfigError, UsageError } from "../core/errors.js"; import { validateJsonSchemaSubset } from "../core/json-schema.js"; import { noticeSet } from "../execution/notices.js"; // biome-ignore lint/suspicious/noExplicitAny: the registry holds judges of different input and verdict types. const judges = new Map(); // biome-ignore lint/suspicious/noExplicitAny: see above. export function registerJudge(judge) { judges.set(judge.name, judge); } export function getJudge(name) { const judge = judges.get(name); if (!judge) { throw new UsageError(`Unknown judge "${name}". Registered judges: ${[...judges.keys()].join(", ")}.`, "INVALID_FLAG_VALUE"); } return judge; } export function listJudges() { return [...judges.values()]; } /** One judge call from outside the pipeline: check the input, pick the runner, run, and wrap the result with the runner it ran on. */ export async function runJudge(name, input, config, selection, call = {}) { const judge = getJudge(name); const errors = validateJsonSchemaSubset(input, judge.inputSchema); if (errors.length > 0) { throw new UsageError(`Input for judge ${name} is invalid: ${errors.join("; ")}`, "INVALID_JSON_ARGUMENT"); } const runner = judge.defaultRunner(config, selection); if (!runner) { throw new ConfigError(`Judge ${name} has no connection or harness to run on. Pass --connection or --harness, or run \`akm setup\`.`, "LLM_NOT_CONFIGURED"); } const notices = noticeSet(call.onNotices); const result = await judge.run(input, { ...call, runner, onNotices: notices.add }); return { judge: name, schemaVersion: judge.schemaVersion, runner: { kind: runner.kind, name: runner.name, ...(runner.kind === "harness" ? { platform: runner.platform } : {}), model: runner.model ?? null, }, outcome: result.outcome, ...(result.outcome === "verdict" ? { verdict: result.verdict } : { reason: result.reason }), ...notices.fields(), }; } for (const judge of [reflectQualityJudge, lessonQualityJudge, memoryTagsJudge, pairJudge, judgmentJudge]) registerJudge(judge);