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
79 lines (78 loc) • 3.67 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 { materializeLlmConnection } from "./engine-resolution.js";
/** The symbolic LLM use behind one llm runner; nothing is read here. */
export function llmUseFromRunner(runner) {
return {
engine: runner.engine,
connection: runner.connection,
...(runner.credential ? { credential: runner.credential } : {}),
...(runner.apiKeyFile ? { apiKeyFile: runner.apiKeyFile } : {}),
...(runner.apiKeySecretRef ? { apiKeySecretRef: runner.apiKeySecretRef } : {}),
timeoutMs: runner.timeoutMs ?? null,
};
}
/** The symbolic LLM use behind an sdk runner's provider fallback, when it has one. */
export function sdkFallbackUseFromRunner(runner) {
if (!runner.fallbackConnection)
return undefined;
return {
engine: runner.engine,
connection: runner.fallbackConnection,
...(runner.fallbackCredential ? { credential: runner.fallbackCredential } : {}),
...(runner.fallbackApiKeyFile ? { apiKeyFile: runner.fallbackApiKeyFile } : {}),
...(runner.fallbackApiKeySecretRef ? { apiKeySecretRef: runner.fallbackApiKeySecretRef } : {}),
timeoutMs: runner.fallbackTimeoutMs !== undefined ? runner.fallbackTimeoutMs : (runner.fallbackConnection.timeoutMs ?? null),
};
}
/** Read the current credential and inject it: the dispatch boundary for one llm runner. */
export function materializeLlmRunnerConnection(runner, envSource = process.env) {
return materializeLlmConnection(llmUseFromRunner(runner), envSource);
}
/** The same for an sdk runner's provider fallback, when it has one. */
export function materializeSdkFallbackConnection(runner, envSource = process.env) {
const use = sdkFallbackUseFromRunner(runner);
return use ? materializeLlmConnection(use, envSource) : undefined;
}
function isRecord(value) {
return value !== null && typeof value === "object" && !Array.isArray(value);
}
/**
* Read a journaled runner back. Tolerant: unknown keys are kept and missing
* profile defaults are filled in; only a shape nothing could dispatch is refused.
*/
export function decodeFrozenRunnerSpec(value) {
if (!isRecord(value) || typeof value.engine !== "string") {
throw new TypeError("a frozen runner must be an object with an engine name");
}
if (value.kind === "llm") {
if (!isRecord(value.connection) || typeof value.connection.endpoint !== "string") {
throw new TypeError("a frozen llm runner must carry connection.endpoint");
}
return value;
}
if (value.kind !== "agent" && value.kind !== "sdk") {
throw new TypeError("a frozen runner's kind must be llm, agent, or sdk");
}
const profile = value.profile;
if (!isRecord(profile) || typeof profile.name !== "string" || typeof profile.bin !== "string") {
throw new TypeError("a frozen agent runner must carry profile.name and profile.bin");
}
return {
...value,
profile: { args: [], envPassthrough: [], stdio: "captured", parseOutput: "text", ...profile },
};
}
export function runnerIsLlm(runner) {
return runner.kind === "llm";
}
/**
* The LLM connection behind a runner, whatever its kind: an llm runner's own,
* an sdk runner's provider fallback, none for an agent CLI.
*/
export function runnerLlmConnection(runner) {
if (runner.kind === "llm")
return runner.connection;
return runner.kind === "sdk" ? runner.fallbackConnection : undefined;
}