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
71 lines (70 loc) • 3.82 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 { createHash } from "node:crypto";
import { parseBuiltinCommandAction } from "../../../commands/command/builtin-action.js";
import { prepareCommandInvocation } from "../../../commands/command/command-execution.js";
import { PORTABLE_ARGUMENTS_PLACEHOLDER } from "../../../commands/command/portable-template.js";
import { captureFrozenDirectoryIdentity } from "../../../execution/directory-identity.js";
import { canonicalResolvedExecutionRequest, } from "../../../execution/resolved-request.js";
import { fallbackAnnouncement } from "../../../integrations/agent/engine-fallback.js";
import { buildExecution } from "../../../integrations/agent/execution.js";
import { freezeEnvironment, workflowExecutionSource } from "../environment.js";
import { gitIdentity } from "../identity.js";
import { durableRequest, executionUnitValues, targetConcurrency, } from "../step-values.js";
function inlineWorkflowCommandAction(action, commandMode) {
if (commandMode !== "portable-template")
return action;
const parsed = parseBuiltinCommandAction(action);
if (parsed.kind !== "inline")
return action;
return { content: parsed.content.split(PORTABLE_ARGUMENTS_PLACEHOLDER).join(parsed.arguments ?? "") };
}
export async function commandDispatch(source, baseUnit, action, context) {
const prepared = await prepareCommandInvocation({
action: inlineWorkflowCommandAction(action, source.commandMode),
config: context.config,
...(context.plan.defaults
? { invocationDefaults: executionUnitValues(context.plan.defaults, context.asset.sourcePath) }
: {}),
...(source.commandMode === "literal" || source.commandMode === "portable-template"
? { inlineContentMode: "literal" }
: {}),
current: executionUnitValues(source.unit, context.asset.sourcePath),
sourceLoader: (ref, kind) => workflowExecutionSource(ref, kind, context),
});
return commandResult(source, baseUnit, prepared, context);
}
export function commandResult(source, baseUnit, prepared, context, literals = []) {
const request = durableRequest(prepared.request);
const lowered = buildExecution(request, prepared.runner);
const cwdIdentity = captureFrozenDirectoryIdentity(context.asset.sourcePath);
const runner = lowered.runner;
const unit = {
...baseUnit,
engine: request.engine.name,
...(request.model ? { model: request.model.resolved } : {}),
...(Object.hasOwn(request.runtime, "timeoutMs") ? { timeoutMs: request.runtime.timeoutMs } : {}),
...(request.inference ? { llm: request.inference } : {}),
...(request.outputSchema ? { output: request.outputSchema } : {}),
};
const environment = Object.freeze([...literals, ...freezeEnvironment(source, undefined, context)]);
const target = Object.freeze({
kind: "command",
ref: request.command.source?.ref ?? null,
contentHash: createHash("sha256").update(request.command.content).digest("hex"),
request: JSON.parse(canonicalResolvedExecutionRequest(request)),
runner,
...(targetConcurrency(runner, context.config) ? { concurrency: targetConcurrency(runner, context.config) } : {}),
cwdIdentity,
...gitIdentity(baseUnit, cwdIdentity.realRoot),
});
const engineAnnouncement = fallbackAnnouncement(prepared.fallbackEngineName, request.engine.name);
return {
target,
environment,
unit,
instructions: request.command.content,
...(engineAnnouncement ? { engineAnnouncement } : {}),
};
}