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

104 lines (103 loc) • 4.75 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/. /** * `callStructured()` — one model call on any runner kind. * * It resolves the call once (the runner's target, model and timeout, the call's * own settings), lowers it for the runner's transport, dispatches it, and * returns the reply text or the failure. A provider's failure (transport error, * timeout, abort, a harness that exits non-zero) is a failed outcome, the same * for every caller; only a configuration failure throws, because it is not the * provider's and no fallback can repair it. * * What stays per-caller: reading the reply (the structured loop, `llm/structured.ts`), * and what a failed outcome means for that caller. */ import { ConfigError } from "../core/errors.js"; import { buildExecution, currentLayer, resolveExecution } from "../integrations/agent/execution.js"; import { runExecution } from "../integrations/agent/runner-dispatch.js"; function own(value, key) { return value !== undefined && Object.hasOwn(value, key); } /** * @internal Exact request-to-cascade projection, exported for presence-semantics contracts. */ export function resolveStructuredCurrent(current, request) { const out = current ? { ...current } : {}; const requestHasInference = own(request, "temperature") || own(request, "maxTokens") || own(request, "enableThinking"); const baseInference = current?.inference && typeof current.inference === "object" && !Array.isArray(current.inference) ? { ...current.inference } : {}; const inference = { ...baseInference }; if (own(request, "temperature") && request?.temperature !== undefined) inference.temperature = request.temperature; if (own(request, "maxTokens") && request?.maxTokens !== undefined) inference.maxTokens = request.maxTokens; if (own(request, "enableThinking") && request?.enableThinking !== undefined) { inference.enableThinking = request.enableThinking; } if (Object.keys(inference).length > 0 || requestHasInference) out.inference = inference; else if (current?.inference === null) out.inference = null; if (own(request, "responseSchema") && request?.responseSchema !== undefined) { out.outputSchema = request.responseSchema; } if (own(request, "timeoutMs")) out.timeout = request?.timeoutMs ?? null; return Object.keys(out).length > 0 ? out : undefined; } function requireTerminalUserMessage(messages) { const terminal = messages.at(-1); if (!terminal || terminal.role !== "user") { throw new TypeError("callStructured messages must end with the terminal user command"); } return { content: terminal.content, conversation: messages.slice(0, -1).map((message) => ({ role: message.role, content: message.content })), }; } /** Timeout and abort come from the dispatch's own reason, whatever the runner's kind. */ function failed(error, result) { const reason = result?.reason; return { ok: false, reason: reason === "timeout" || reason === "aborted" ? reason : "error", error, ...(result ? { result } : {}), }; } /** A dispatch's result as the reply it produced or the failure it reports. */ export function outcomeOf(result) { return result.ok ? { ok: true, raw: result.stdout } : failed(result.error ?? result.stderr ?? result.reason ?? "dispatch failed", result); } export async function callStructured(opts) { const { runner, messages, request } = opts; try { const terminal = requireTerminalUserMessage(messages); // Every structured call is unattended model work, so it runs under the model-work tool policy. const prepared = resolveExecution({ content: terminal.content, conversation: terminal.conversation, runner, layers: [currentLayer(resolveStructuredCurrent(opts.current, request) ?? {})], modelWork: true, }); const lowered = buildExecution(prepared.request, prepared.runner); opts.onNotices?.(lowered.notices); const result = await runExecution(lowered, { ...(request?.chat ? { chat: request.chat } : {}), ...(request?.onRetryAttempt ? { onRetryAttempt: request.onRetryAttempt } : {}), ...(request?.signal ? { signal: request.signal } : {}), }); return outcomeOf(result); } catch (err) { if (err instanceof ConfigError) throw err; return failed(err instanceof Error ? err.message : String(err)); } }