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

113 lines (112 loc) • 5.33 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/. import { warnOnce } from "../core/warn.js"; import { cloneExecutionJsonObject } from "./json.js"; export const EXECUTION_SOURCE_SCHEMA_VERSION = 1; /** Current internal adapter identifiers are lowercase kebab-case registry keys. */ export const EXECUTION_ADAPTER_ID_PATTERN = /^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/; /** What an asset that still says `engine:` is told, once. */ export function warnRetiredEngineKey() { warnOnce("asset-engine-key-retired", "`engine` is retired; use `connection:` or `harness:`."); } /** Those settings as one layer's defaults. A setting that is absent says nothing; `null` is a real value. */ export function executionDefaultsOf(settings) { if (!settings) return {}; return { ...(settings.connection !== undefined ? { connection: settings.connection } : {}), ...(settings.harness !== undefined ? { harness: settings.harness } : {}), ...(settings.engine !== undefined ? { engine: settings.engine } : {}), ...(settings.model !== undefined ? { model: settings.model } : {}), ...(settings.llm !== undefined ? { inference: cloneExecutionJsonObject(settings.llm, "inference") } : {}), ...(settings.timeoutMs !== undefined ? { timeout: settings.timeoutMs } : {}), }; } function requireRecord(value, path) { if (value === null || typeof value !== "object" || Array.isArray(value)) { throw new TypeError(`${path} must be an object`); } return value; } /** A selector or layer identifier that may cross the durable execution boundary. */ export function requireStableExecutionSelector(value, path) { if (typeof value !== "string" || value.length === 0 || value.trim() !== value) { throw new TypeError(`${path} must be a non-empty stable identifier`); } return value; } export function isPortableExecutionAgentSelector(value) { return /^(?:[^/]+\/\/)?agents\/[^/]/.test(value); } export function executionPersonaMatchesSelector(selector, personaRef) { return selector.includes("//") ? selector === personaRef : personaRef.endsWith(`//${selector}`); } const IDENTITY_KEYS = ["ref", "bundle", "adapter", "file", "hash"]; function identityFrom(value, path) { const record = requireRecord(value, path); const out = {}; for (const key of IDENTITY_KEYS) { const field = record[key]; if (typeof field !== "string" || field.length === 0) throw new TypeError(`${path}.${key} must be a non-empty string`); out[key] = field; } return Object.freeze(out); } /** Copy and freeze an identity produced by an adapter renderer. */ export function createExecutionSourceIdentity(input) { return identityFrom(input, "execution source identity"); } /** Read a source identity back from a frozen request. */ export function decodeExecutionSourceIdentity(value, path = "execution source identity") { return identityFrom(value, path); } /** Type-check the common frontmatter defaults; unknown keys are ignored. */ export function cloneUnresolvedExecutionDefaults(input, path = "execution source defaults") { const record = requireRecord(input, path); const present = (key) => Object.hasOwn(record, key) && record[key] !== undefined; const out = {}; for (const key of ["agent", "connection", "harness", "engine", "model", "workspace"]) { if (!present(key)) continue; const value = record[key]; if (value !== null && typeof value !== "string") throw new TypeError(`${path}.${key} must be a string or null`); out[key] = value; } if (present("timeout")) { const timeout = record.timeout; if (timeout !== null && typeof timeout !== "string" && typeof timeout !== "number") { throw new TypeError(`${path}.timeout must be a string, number, or null`); } out.timeout = timeout; } for (const key of ["inference", "outputSchema"]) { if (!present(key)) continue; out[key] = record[key] === null ? null : cloneExecutionJsonObject(record[key], `${path}.${key}`); } if (present("environment")) { const environment = record.environment === null ? null : cloneExecutionJsonObject(record.environment, `${path}.environment`); if (environment !== null && Object.values(environment).some((value) => typeof value !== "string")) { throw new TypeError(`${path}.environment values must be strings`); } out.environment = environment; } return Object.freeze(out); } export function createAdapterRenderedExecutionSource(input) { const { kind, content } = input; if (kind !== "command" && kind !== "persona") throw new TypeError("execution source kind is invalid"); if (typeof content !== "string") throw new TypeError("execution source content must be a string"); return Object.freeze({ schemaVersion: EXECUTION_SOURCE_SCHEMA_VERSION, kind, content, defaults: cloneUnresolvedExecutionDefaults(input.defaults ?? {}), identity: createExecutionSourceIdentity(input.identity), }); }