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

67 lines (66 loc) • 3.61 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 { canonicalResolvedExecutionRequest, decodeResolvedExecutionRequest, } from "../../execution/resolved-request.js"; import { defaultLlmEngineConcurrency } from "../concurrency-policy.js"; import { DEFAULT_EXEC_TIMEOUT_MS } from "../resource-limits.js"; /** The unit's authored settings plus its source span and argv, before any target resolution. */ export function baseUnitOf(source) { return { ...(source.unit ? structuredClone(source.unit) : {}), source: { ...source.source }, ...(source.exec ? { exec: { ...source.exec } } : {}), }; } /** Resolve an exec's timeout: unit `timeout:` → document `defaults.timeout` → the default. */ export function freezeExecSpec(source, exec, context) { const declared = Object.hasOwn(source.unit ?? {}, "timeoutMs") ? source.unit?.timeoutMs : context.plan.defaults && Object.hasOwn(context.plan.defaults, "timeoutMs") ? context.plan.defaults.timeoutMs : undefined; return { ...exec, command: exec.command, timeoutMs: declared === undefined ? DEFAULT_EXEC_TIMEOUT_MS : declared, }; } export function targetConcurrency(runner, config) { if (runner.kind === "llm") { const configured = typeof runner.engine === "string" ? config.engines?.[runner.engine] : undefined; return defaultLlmEngineConcurrency(runner.connection.endpoint, configured?.kind === "llm" ? configured.concurrency : undefined); } if (runner.kind !== "sdk" || !runner.fallbackConnection) return undefined; const selected = typeof runner.engine === "string" ? config.engines?.[runner.engine] : undefined; const fallbackName = selected?.kind === "agent" ? selected.llmEngine : undefined; const fallback = fallbackName ? config.engines?.[fallbackName] : undefined; return defaultLlmEngineConcurrency(runner.fallbackConnection.endpoint, fallback?.kind === "llm" ? fallback.concurrency : undefined); } /** The request as it may be persisted: its live runtime environment removed. */ export function durableRequest(request) { const wire = JSON.parse(canonicalResolvedExecutionRequest(request)); const runtime = { ...wire.runtime }; delete runtime.environment; wire.runtime = runtime; return decodeResolvedExecutionRequest(wire); } export function executionUnitValues(unit, workspace) { return Object.freeze({ ...(unit && Object.hasOwn(unit, "engine") ? { engine: unit.engine } : {}), ...(unit && Object.hasOwn(unit, "model") ? { model: unit.model } : {}), ...(unit && Object.hasOwn(unit, "llm") ? { inference: unit.llm } : {}), ...(unit && Object.hasOwn(unit, "timeoutMs") ? { timeout: unit.timeoutMs } : {}), ...(unit && "output" in unit && Object.hasOwn(unit, "output") ? { outputSchema: unit.output } : {}), workspace, }); } /** Step ids declared BEFORE `stepId` — the same ordering `map.over` and `inputs:` rely on. */ export function earlierStepIds(plan, stepId) { const index = plan.steps.findIndex((step) => step.stepId === stepId); return new Set(index < 0 ? [] : plan.steps.slice(0, index).map((step) => step.stepId)); } /** THIS workflow's own declared param names — never an outer composing task's. */ export function declaredParamNames(plan) { return new Set(plan.paramSchemas ? Object.keys(plan.paramSchemas) : (plan.params ?? [])); }