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

260 lines (259 loc) • 12.5 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/. /** * Run a built execution: the one place credentials are read. Model work on a * connection goes to `chatCompletion`, a harness to `runAgent` (the harness * builder turns the request into argv), and agent work on a connection to * akm's own runtime (`agentRuntime`). The key of the connection a run used is * redacted from its result. */ import fs from "node:fs"; import os from "node:os"; import path from "node:path"; import { resolveApiKey } from "../../core/config/credentials.js"; import { UsageError } from "../../core/errors.js"; import { collectInferredSecretValues, collectSensitiveValues, redactSensitiveText } from "../../core/redaction.js"; import { chatCompletion, LlmCallError } from "../../llm/client.js"; import { emitLlmUsage } from "../../llm/usage-telemetry.js"; import { modelFromArgs } from "../harnesses/harness.js"; import { harnessProfile, runtimeProfile } from "./profiles.js"; import { mergeLayerInference, resolveTarget } from "./resolution.js"; import { agentRuntime } from "./runtime.js"; import { runAgent } from "./spawn.js"; /** * The value a dispatch must never echo back: the key of the connection it runs on. * A harness is the user's own program and holds no key of akm's, so there is none. * A request's own `env` is configuration; a caller's secret values are redacted * through its own sensitive set. */ export function collectDispatchSensitiveValues(runner, envSource = process.env) { // The child inherits akm's environment, so a reply that echoes a credential-named variable's value is scrubbed // of it, as the connection's own key is. const inherited = collectInferredSecretValues(envSource); if (runner.kind !== "connection") return collectSensitiveValues(inherited); return collectSensitiveValues([ resolveApiKey(runner.apiKey, { connection: runner.name }, runner.endpoint, envSource, true, runner.storedKey !== false), ...inherited, ]); } function redactResult(result, sensitiveValues) { return { ...result, stdout: redactSensitiveText(result.stdout, sensitiveValues), stderr: redactSensitiveText(result.stderr, sensitiveValues), ...(result.error !== undefined ? { error: redactSensitiveText(result.error, sensitiveValues) } : {}), }; } function llmFailureReason(error) { if (!(error instanceof LlmCallError)) return "spawn_failed"; switch (error.code) { case "aborted": return "aborted"; case "timeout": return "timeout"; case "rate_limited": return "llm_rate_limit"; case "parse_error": case "provider_html_error": return "parse_error"; default: return "spawn_failed"; } } const USAGE_ERROR_CODES = { timeout: "timeout", aborted: "aborted", parse_error: "parse_error", llm_rate_limit: "rate_limited", }; /** * The model an agent or runtime dispatch ran: the request's, else the one a * harness's own `args` select, which its command carries when the request names none. */ function dispatchedModel({ request, runner }) { return request.model?.model ?? (runner.kind === "harness" ? modelFromArgs(runner.args) : undefined); } /** * One usage record for an agent or runtime dispatch, through the same sink and * ambient `withLlmStage` attribution as the LLM transport's per-HTTP-attempt * records: the model it ran, and tokens when the runner reported them. */ function recordDispatchUsage(execution, result) { const { inputTokens, outputTokens, reasoningTokens } = result.usage ?? {}; const reported = [inputTokens, outputTokens, reasoningTokens].filter((count) => count !== undefined); const model = dispatchedModel(execution); emitLlmUsage({ outcome: result.ok ? "success" : "error", modelSource: "configured", ...(model ? { model } : {}), durationMs: result.durationMs, ...(inputTokens !== undefined ? { promptTokens: inputTokens } : {}), ...(outputTokens !== undefined ? { completionTokens: outputTokens } : {}), ...(reasoningTokens !== undefined ? { reasoningTokens } : {}), ...(reported.length > 0 ? { totalTokens: reported.reduce((sum, count) => sum + count, 0) } : {}), ...(result.ok ? {} : { errorCode: (result.reason && USAGE_ERROR_CODES[result.reason]) ?? "unknown_error" }), }); } /** * The chat connection a connection runner stands for: its key read now, and the * portable settings of the runner's choosing layers and of this call's request * (the request's nearer), which the client translates. The one place a * connection's key is read for a call. */ export function materializeConnection(runner, inference, envSource = process.env) { const apiKey = resolveApiKey(runner.apiKey, { connection: runner.name }, runner.endpoint, envSource, false, runner.storedKey !== false); const settings = mergeLayerInference([ { inference: runner.inference }, { inference: inference }, ]); return { endpoint: runner.endpoint, model: runner.model, request: runner.request, ...(settings ? { inference: settings } : {}), timeoutMs: runner.timeoutMs, ...(runner.contextLength !== undefined ? { contextLength: runner.contextLength } : {}), ...(apiKey ? { apiKey } : {}), }; } /** `llm` is given for a direct chat; without it a connection runs akm's internal runtime. */ async function dispatchRunner({ runner, profile, inference, confined }, opts, seams, llm) { const secrets = collectDispatchSensitiveValues(runner); let result; if (runner.kind === "harness") { const harnessRunProfile = profile ?? harnessProfile(runner); const spawn = (spawnOpts) => (seams.runAgent ?? runAgent)(harnessRunProfile, spawnOpts); result = await (confined ? inScratchDirectory(opts, spawn) : spawn(opts)); } else { const connection = materializeConnection(runner, inference); if (connection.apiKey) secrets.push(connection.apiKey); result = llm ? await llm(connection, opts) : await (seams.runSdk ?? agentRuntime.run)(profile ?? runtimeProfile(runner), opts, connection); } return redactResult(result, collectSensitiveValues(secrets)); } /** * Run `work` in a scratch working directory that akm creates for it and removes * after, so the edit that model work on a CLI harness cannot be denied never * reaches the stash. This function owns the directory's lifecycle: nothing else, * the test tooling included, removes an `akm-model-work-*` directory. */ async function inScratchDirectory(opts, work) { const scratch = fs.mkdtempSync(path.join(os.tmpdir(), "akm-model-work-")); try { return await work({ ...opts, cwd: scratch }); } finally { fs.rmSync(scratch, { recursive: true, force: true }); } } /** Whether this execution is model work that runs an agent (not a direct chat). */ function isConfinedAgentWork(execution) { return execution.request.modelWork && execution.messages === undefined; } /** * An agent's reply is its stdout, and its exit code does not say whether it gave one: opencode exits 0 with none at its * step limit, and a harness can end a finished turn with an empty reply. A run that ends with no answer has failed. * OpenCode 1's turn that ends unfinished, on a refused tool call, is reported by its harness (`runAgent`). */ function agentAnswer(runner, result) { if (result.stdout.trim() !== "") return result; return { ...result, ok: false, reason: "parse_error", error: `"${runner.name}" returned no answer.` }; } /** * Run a built execution. Credentials are read here, once per call, and never * returned. Model work on a harness runs confined, in a scratch working * directory. An agent run, on a harness or akm's own runtime, must end with an * answer: one that stops with none has failed with `parse_error`. */ export async function runExecution(execution, options = {}) { const confined = isConfinedAgentWork(execution); const opts = { ...execution.options }; if (options.signal) opts.signal = options.signal; if (options.baseEnv) opts.baseEnv = { ...options.baseEnv }; if (options.env) opts.env = { ...execution.options.env, ...options.env }; // Model work is akm's own traffic: its reads and its agents' commands are the improve pipeline's, never the // user's demand. A caller's source (a task, say) wins, as does one the unit's own `env:` binds. const eventSource = options.eventSource ?? (execution.request.modelWork && execution.options.env?.AKM_EVENT_SOURCE === undefined ? "improve" : undefined); if (eventSource !== undefined) { opts.env = { ...opts.env, AKM_EVENT_SOURCE: eventSource }; } const chat = options.chat ?? chatCompletion; const llm = execution.messages === undefined ? undefined : async (connection, callOpts) => { const started = Date.now(); try { const stdout = await chat(connection, [...(execution.messages ?? [])], { ...execution.chatOptions, ...(Object.hasOwn(callOpts, "timeoutMs") ? { timeoutMs: callOpts.timeoutMs } : {}), ...(callOpts.signal ? { signal: callOpts.signal } : {}), ...(options.onRetryAttempt ? { onRetryAttempt: options.onRetryAttempt } : {}), }); return { ok: true, exitCode: 0, stdout, stderr: "", durationMs: Date.now() - started }; } catch (error) { // Returned rather than thrown so the redaction below covers provider bodies. return { ok: false, exitCode: null, stdout: "", stderr: "", durationMs: Date.now() - started, error: error instanceof Error ? error.message : String(error), reason: llmFailureReason(error), ...(error instanceof LlmCallError ? { llmErrorCode: error.code } : {}), }; } }; let result = await dispatchRunner({ runner: execution.runner, ...(execution.profile ? { profile: execution.profile } : {}), inference: execution.request.inference, confined, }, opts, options, llm); // A direct chat is no agent: its callers judge its reply. if (llm === undefined && result.ok) result = agentAnswer(execution.runner, result); // The LLM transport records each HTTP attempt itself. if (llm === undefined) recordDispatchUsage(execution, result); return result; } /** * `akm agent` with no prompt: start the harness for a person to use, with the terminal * inherited and nothing to run (no `-p`, no empty turn). A connection has nothing * interactive to start, so it is refused rather than billed for an empty turn. */ export async function executeInteractiveAgentInvocation(input, seams = {}) { const runner = resolveTarget({ config: input.config, layers: [ ...(input.connection === undefined ? [] : [{ connection: input.connection }]), ...(input.harness === undefined ? [] : [{ harness: input.harness }]), ...(input.engine === undefined ? [] : [{ engine: input.engine }]), ...(input.model === undefined ? [] : [{ model: input.model }]), ], }); if (runner.kind === "connection") { throw new UsageError(`"${runner.name}" is a connection, which has nothing to start interactively: pass --prompt, or name a harness.`, "MISSING_REQUIRED_ARGUMENT", "Pass --prompt to run a task on the connection, or name a harness with --harness to start it interactively."); } const opts = { stdio: "interactive", timeoutMs: input.timeoutMs ?? runner.timeoutMs, ...(input.cwd ? { cwd: input.cwd } : runner.workspace ? { cwd: runner.workspace } : {}), dispatch: { ...(runner.model ? { model: runner.model } : {}), ...(runner.agent ? { agent: runner.agent } : {}) }, }; return { engine: runner.name, result: await dispatchRunner({ runner }, opts, seams) }; }