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

81 lines (80 loc) • 4.44 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 { isPlainObject } from "./config/deep-merge.js"; import { warnOnce } from "./warn.js"; /** * The portable settings a request's inference may carry and an endpoint's * request translates: `temperature`, `maxTokens`, `reasoningEffort` (an asset's * `effort:` is the same setting) and `enableThinking`, plus `extraParams`, whose * entries are inlined as request fields. `contextLength` is a connection's own * key, not a per-call setting. */ export const PORTABLE_INFERENCE_KEYS = [ "temperature", "maxTokens", "reasoningEffort", "enableThinking", "extraParams", ]; /** Whether a request's fields turn thinking on or off, in either of the two spellings an endpoint reads; `undefined` when they say nothing. */ export function thinkingSetting(request) { const kwargs = request.chat_template_kwargs; const nested = typeof kwargs === "object" && kwargs !== null ? kwargs.enable_thinking : undefined; const value = request.enable_thinking ?? nested; return typeof value === "boolean" ? value : undefined; } /** The wire fields for `inference`, in the order the request lists them. */ export function inferenceWireFields(inference) { if (!inference) return {}; return { ...(inference.temperature !== undefined ? { temperature: inference.temperature } : {}), ...(inference.maxTokens !== undefined ? { max_tokens: inference.maxTokens } : {}), ...inference.extraParams, // Which of the two thinking fields an endpoint honors is a fact about it (and any gateway in // front of it), not about akm's wording: send both whenever thinking is set. ...(inference.enableThinking !== undefined ? { chat_template_kwargs: { enable_thinking: inference.enableThinking }, enable_thinking: inference.enableThinking, } : {}), ...(inference.reasoningEffort !== undefined ? { reasoning_effort: inference.reasoningEffort } : {}), }; } /** * `over` laid on `base`, one level deep: a field that is an object on both sides * (`chat_template_kwargs`) is merged key by key, so a call's `enableThinking` * does not erase the `thinking_budget` beside it; any other field of `over` wins. */ export function mergeRequestFields(base, over) { const merged = { ...base }; for (const [key, value] of Object.entries(over)) { const was = merged[key]; merged[key] = isPlainObject(was) && isPlainObject(value) ? { ...was, ...value } : value; } return merged; } /** * The fields of a request that are not the user's to set: the model akm resolved, the conversation and tool * state the transport builds, whether the reply streams, and the response format the caller asks for. Whichever * way one is written (a connection or model entry's request fields, or an `extraParams` of a call, a process or * a workflow step) it is ignored for that field, on both transports. */ export const OWNED_REQUEST_FIELDS = ["model", "messages", "tools", "stream", "response_format"]; function withoutOwnedFields(request) { const written = OWNED_REQUEST_FIELDS.filter((field) => Object.hasOwn(request, field)); if (written.length === 0) return request; warnOnce(`request-fields:owned:${written.join(",")}`, `Request field${written.length === 1 ? "" : "s"} ${written.map((field) => `"${field}"`).join(", ")} in a connection's or model entry's request fields, or in an extraParams, ${written.length === 1 ? "is" : "are"} ignored: akm sets the model, the conversation, the tools, streaming and the response format itself.`); return Object.fromEntries(Object.entries(request).filter(([field]) => !OWNED_REQUEST_FIELDS.includes(field))); } /** * The fields a request carries: `request` as written, with the translation of `inference` laid on it, less the * fields akm owns, which are taken out of the merged result so no layer can bring one back. The one place * portable settings meet an endpoint's own spelling, and where both transports take the user's fields from. */ export function effectiveRequestFields(request, inference) { return withoutOwnedFields(mergeRequestFields(request ?? {}, inferenceWireFields(inference ?? undefined))); }