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
68 lines (67 loc) • 3.11 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/.
/**
* The one corrective loop for a model call whose reply must have a shape:
* dispatch, parse, and, when the parser refuses the reply, one more dispatch that
* carries what was wrong. Whoever dispatches decides how the correction reaches
* the model (appended to the prompt, or as the next turns of the conversation);
* whoever parses decides what a good reply is.
*
* Only a refused reply is corrected. A dispatch that fails, in transport or by
* timeout, ends the loop with that failure as the dispatch reported it: the
* transports own their retries, and a command is never run twice to fix its
* output (`attempts: 1`).
*/
import { parseEmbeddedJsonResponse } from "../core/parse.js";
/** The reply was refused by the parser, as against the dispatch failing. */
export function isRefused(result) {
return !result.ok && "errors" in result;
}
/** The default instruction for a refused reply. */
export function correctionText(refusal) {
if ((refusal.reason ?? "parse_error") === "parse_error") {
return "Your previous response contained no parseable JSON. Respond with ONLY a JSON value that matches the requested schema — no prose, no code fences.";
}
return `Your previous JSON response failed validation:\n- ${refusal.errors.join("\n- ")}\nRespond again with ONLY a corrected JSON value.`;
}
export async function structuredLoop(options) {
const attempts = Math.max(1, options.attempts ?? 2);
const correct = options.correct ?? correctionText;
let correction;
for (let attempt = 1;; attempt += 1) {
const dispatched = await options.dispatch(correction);
if (!dispatched.ok)
return { ...dispatched, attempts: attempt };
const raw = dispatched.raw;
const parsed = options.parse(raw);
if (parsed.ok)
return { ...dispatched, value: parsed.value, raw, attempts: attempt };
if (attempt >= attempts) {
return {
...dispatched,
ok: false,
reason: parsed.reason ?? "parse_error",
errors: parsed.errors,
raw,
attempts: attempt,
};
}
correction = { text: correct(parsed), reply: raw };
}
}
/**
* A parser for a reply that is JSON, perhaps in prose or code fences: the JSON
* found in it, held to `validate`. A reply with no JSON in it is a `parse_error`;
* JSON that fails `validate` is a `validation_error`.
*/
export function jsonParser(validate) {
return (raw) => {
const candidate = parseEmbeddedJsonResponse(raw);
if (candidate === undefined) {
return { ok: false, errors: ["no JSON structure found in response"], reason: "parse_error" };
}
const verdict = validate(candidate);
return verdict.ok ? verdict : { ...verdict, reason: verdict.reason ?? "validation_error" };
};
}