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

101 lines (100 loc) • 5.28 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 { UsageError } from "../../core/errors.js"; import { warn } from "../../core/warn.js"; import { isPortableExecutionAgentSelector } from "../../execution/source.js"; import { executeInteractiveAgentInvocation } from "../../integrations/agent/runner-dispatch.js"; import { executeCommandInvocation, } from "../command/command-execution.js"; import { ensureWorkingFolder } from "../env/injection.js"; function canonicalCurrent(options) { const current = {}; if (options.agentRef !== undefined) current.agent = options.agentRef; if (options.selection?.connection !== undefined) current.connection = options.selection.connection; if (options.selection?.harness !== undefined) current.harness = options.selection.harness; if (options.selection?.engine !== undefined) current.engine = options.selection.engine; if (options.selection?.model !== undefined) current.model = options.selection.model; if (Object.hasOwn(options.selection ?? {}, "inference")) current.inference = options.selection?.inference; if (options.selection?.outputSchema !== undefined) current.outputSchema = options.selection.outputSchema; if (options.timeoutMs !== undefined) current.timeout = options.timeoutMs; if (options.cwd !== undefined) current.workspace = options.cwd; return current; } function rejectInvalidAgentRef(agentRef) { if (agentRef === undefined || isPortableExecutionAgentSelector(agentRef)) return; throw new UsageError(`agent expects an agent asset ref under agents/...; received ${JSON.stringify(agentRef)}.`, "INVALID_FLAG_VALUE"); } /** * Dispatch a `--prompt` / `--prompt-stdin` task through the canonical command * path. The prompt is a person's free text, not a template: it is sent to the * agent verbatim (`inlineContentMode: "literal"`), so prose containing `}}` * from compact JSON, a `$VAR`, a shell snippet, or an `@path` reaches the * agent instead of being rejected as an unsupported template construct. */ async function delegateCanonicalCommand(options, seams, action) { const execute = seams.executeCommand ?? executeCommandInvocation; const result = await execute({ action, inlineContentMode: "literal", config: options.agentConfig, current: canonicalCurrent(options), }, { ...(options.signal ? { signal: options.signal } : {}), ...(options.env ? { env: options.env } : {}), ...(options.baseEnv ? { baseEnv: options.baseEnv } : {}), }); for (const message of result.warnings ?? []) warn(message); return result; } export async function akmAgentDispatch(options, seams = {}) { if (!options.agentConfig) throw new UsageError("agent requires a valid config with a harness or connection.", "MISSING_REQUIRED_ARGUMENT"); rejectInvalidAgentRef(options.agentRef); if (options.cwd) ensureWorkingFolder(options.cwd); if (options.prompt !== undefined) { if (options.prompt.trim() === "") { throw new UsageError("The prompt is empty, so there is no turn to send.", "MISSING_REQUIRED_ARGUMENT", "Pass text with --prompt, or on stdin with --prompt-stdin. Without a prompt the harness is started for a person."); } return delegateCanonicalCommand(options, seams, { content: options.prompt }); } // No prompt: the harness is started for a person. Only where and which model ride along; the rest needs a task. const { connection, harness, engine, model, ...needsTask } = options.selection ?? {}; if (options.agentRef !== undefined || Object.keys(needsTask).length > 0) { throw new UsageError("An agent persona, inference setting or output schema applies to a task: pass --prompt or --prompt-stdin. Without a prompt only the harness (and a model) is started.", "MISSING_REQUIRED_ARGUMENT", "Pass --prompt or --prompt-stdin, or drop the agent ref and the other settings to start the harness interactively."); } const executeInteractive = seams.executeInteractive ?? executeInteractiveAgentInvocation; const execution = await executeInteractive({ config: options.agentConfig, ...(typeof connection === "string" ? { connection } : {}), ...(typeof harness === "string" ? { harness } : {}), ...(typeof engine === "string" ? { engine } : {}), ...(typeof model === "string" ? { model } : {}), ...(options.timeoutMs !== undefined ? { timeoutMs: options.timeoutMs } : {}), ...(options.cwd !== undefined ? { cwd: options.cwd } : {}), }); const result = execution.result; return { schemaVersion: 2, ok: result.ok, shape: "agent-result", engine: execution.engine, exitCode: result.exitCode, stdout: result.stdout ?? "", stderr: result.stderr ?? "", durationMs: result.durationMs, ...(result.error !== undefined ? { error: result.error } : {}), ...(result.reason !== undefined ? { reason: result.reason } : {}), }; }