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
161 lines (160 loc) • 7.54 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/.
import { sortExecutionJson } from "./json.js";
import { decodeExecutionSourceIdentity, } from "./source.js";
export const RESOLVED_EXECUTION_SCHEMA_VERSION = 2;
const COMMAND_KEYS = ["template", "argumentInput", "content", "source"];
const PERSONA_KEYS = ["content", "source"];
const TARGET_KEYS = ["name", "kind", "platform"];
const MODEL_KEYS = ["model", "alias"];
const RUNTIME_KEYS = ["timeoutMs", "workspace", "environment"];
const IDENTITY_KEYS = ["ref", "bundle", "adapter", "file", "hash"];
const OPTIONAL_REQUEST_KEYS = ["conversation", "agent", "persona", "model", "inference", "outputSchema"];
/** Copy the own, defined fields named by `keys`; omitted stays omitted, `null` stays `null`. */
function pick(value, keys) {
const record = value;
const out = {};
for (const key of keys) {
if (Object.hasOwn(record, key) && record[key] !== undefined)
out[key] = record[key];
}
return out;
}
function requireRecord(value, path) {
if (value === null || typeof value !== "object" || Array.isArray(value)) {
throw new TypeError(`${path} must be an object`);
}
return value;
}
export function createResolvedCommand(input) {
if (typeof input.content !== "string")
throw new TypeError("resolved command content must be a string");
return Object.freeze({
template: input.source.content,
...(input.argumentInput !== undefined ? { argumentInput: input.argumentInput } : {}),
content: input.content,
source: decodeExecutionSourceIdentity(input.source.identity),
});
}
export function createInlineResolvedCommand(input) {
if (typeof input.template !== "string" || typeof input.content !== "string") {
throw new TypeError("inline command template and content must be strings");
}
return Object.freeze({
template: input.template,
...(input.argumentInput !== undefined ? { argumentInput: input.argumentInput } : {}),
content: input.content,
source: null,
});
}
export function createResolvedPersona(source) {
return Object.freeze({
content: source.content,
source: decodeExecutionSourceIdentity(source.identity),
});
}
/** Freeze one request without normalizing away optional-field presence. */
export function createResolvedExecutionRequest(input) {
if (!input.command || !input.target || !input.runtime || typeof input.modelWork !== "boolean") {
throw new TypeError("resolved execution request requires command, target, modelWork, and runtime");
}
const out = {
schemaVersion: RESOLVED_EXECUTION_SCHEMA_VERSION,
command: input.command,
target: input.target,
modelWork: input.modelWork,
runtime: input.runtime,
...pick(input, OPTIONAL_REQUEST_KEYS),
};
return Object.freeze(out);
}
/**
* Rehydrate a frozen JSON request after workflow resume/replay. Tolerant of
* unknown keys; refuses a request of any other schema version: an earlier
* release's names an engine and cannot run now.
*/
export function decodeResolvedExecutionRequest(value) {
const input = requireRecord(value, "resolved execution request");
const version = input.schemaVersion;
if (version !== RESOLVED_EXECUTION_SCHEMA_VERSION) {
throw new TypeError(typeof version === "number" && version > RESOLVED_EXECUTION_SCHEMA_VERSION
? `resolved execution request schemaVersion ${version} was written by a newer akm; upgrade akm to run it`
: `unsupported resolved execution schemaVersion: ${String(version)}`);
}
const command = requireRecord(input.command, "command");
if (typeof command.template !== "string" || typeof command.content !== "string") {
throw new TypeError("command template and content must be strings");
}
const target = requireRecord(input.target, "target");
if (typeof target.name !== "string" || (target.kind !== "connection" && target.kind !== "harness")) {
throw new TypeError("target.name must be a string and target.kind must be connection or harness");
}
if (typeof input.modelWork !== "boolean")
throw new TypeError("modelWork must be a boolean");
const request = {
command: Object.freeze({
...pick(command, COMMAND_KEYS),
source: command.source === null ? null : decodeExecutionSourceIdentity(command.source, "command.source"),
}),
target: Object.freeze(pick(target, TARGET_KEYS)),
modelWork: input.modelWork,
runtime: Object.freeze(pick(requireRecord(input.runtime, "runtime"), RUNTIME_KEYS)),
};
if (Array.isArray(input.conversation)) {
request.conversation = Object.freeze(input.conversation.map((message, index) => Object.freeze(pick(requireRecord(message, `conversation[${index}]`), ["role", "content"]))));
}
if (Object.hasOwn(input, "agent") && input.agent !== undefined)
request.agent = input.agent;
if (Object.hasOwn(input, "persona") && input.persona !== undefined) {
if (input.persona === null)
request.persona = null;
else {
const persona = requireRecord(input.persona, "persona");
request.persona = Object.freeze({
...pick(persona, PERSONA_KEYS),
source: decodeExecutionSourceIdentity(persona.source, "persona.source"),
});
}
}
if (Object.hasOwn(input, "model") && input.model !== undefined) {
request.model = input.model === null ? null : Object.freeze(pick(requireRecord(input.model, "model"), MODEL_KEYS));
}
for (const key of ["inference", "outputSchema"]) {
if (Object.hasOwn(input, key) && input[key] !== undefined)
request[key] = input[key];
}
return createResolvedExecutionRequest(request);
}
/** Stable bytes for freeze hashes and entrypoint-equivalence projections. */
export function canonicalResolvedExecutionRequest(request) {
const wire = {
schemaVersion: RESOLVED_EXECUTION_SCHEMA_VERSION,
command: {
...pick(request.command, COMMAND_KEYS),
source: request.command.source === null ? null : pick(request.command.source, IDENTITY_KEYS),
},
target: pick(request.target, TARGET_KEYS),
modelWork: request.modelWork,
runtime: pick(request.runtime, RUNTIME_KEYS),
};
if (request.conversation !== undefined) {
wire.conversation = request.conversation.map((message) => ({ role: message.role, content: message.content }));
}
if (Object.hasOwn(request, "agent") && request.agent !== undefined)
wire.agent = request.agent;
if (Object.hasOwn(request, "persona") && request.persona !== undefined) {
wire.persona =
request.persona === null
? null
: { ...pick(request.persona, PERSONA_KEYS), source: pick(request.persona.source, IDENTITY_KEYS) };
}
if (Object.hasOwn(request, "model") && request.model !== undefined) {
wire.model = request.model === null ? null : pick(request.model, MODEL_KEYS);
}
for (const key of ["inference", "outputSchema"]) {
if (Object.hasOwn(request, key) && request[key] !== undefined)
wire[key] = request[key];
}
return `${JSON.stringify(sortExecutionJson(wire))}\n`;
}