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

150 lines (149 loc) • 8.88 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/. /** * `models.json`, and an engine's own `model`, folded into the `models` of the connections * and harnesses the engines became. See `convert-engines.ts`. */ import { getHarness } from "../../integrations/harnesses/index.js"; import { isRecord } from "../common.js"; import { str, wireFieldsOf } from "./convert-engines-shared.js"; import { modelForEveryAlias } from "./model-aliases.js"; const allAliases = (model) => model === undefined ? undefined : modelForEveryAlias(model); /** An overlay entry as a model entry: the bare name, or `{ model, ...fields }` when it sets more. */ function modelEntry(model, fields) { return Object.keys(fields).length > 0 ? { model, ...fields } : model; } // ── models.json folds into the models of what it named ─────────────────────── /** The models of `entry`, made on it when it has none yet. */ function modelsOf(entry) { entry.models ??= {}; return entry.models; } const hasModel = (entry, alias) => isRecord(entry) && isRecord(entry.models) && alias in entry.models; /** * The overlay's rows: one per alias and column, the runtime's column first (it is the weakest, naming OpenCode's own * models), then a connection's own, then `*`, which fills what is left. The flat shorthand is the `*` column. */ function overlayRows(modelsOverlay) { const overlay = isRecord(modelsOverlay) && isRecord(modelsOverlay.aliases) ? modelsOverlay.aliases : {}; const rows = Object.entries(overlay).flatMap(([alias, columns]) => { if (!isRecord(columns)) return []; const flat = Object.fromEntries(Object.entries(columns).filter(([k]) => k === "model" || k === "inference" || k === "engine")); const perColumn = Object.entries(columns).filter(([k]) => !(k in flat)); return [...perColumn, ...(Object.keys(flat).length > 0 ? [["*", flat]] : [])].map(([column, profile]) => ({ alias, column, profile, })); }); const rank = (column) => (column === "opencode-sdk" ? 0 : column === "*" ? 2 : 1); return rows.sort((a, b) => rank(a.column) - rank(b.column)); } /** * `models.json`: an alias is set by, nearest first, the config's own `models`, models.json's column for that * connection or harness, models.json's `*` column (and the flat shorthand, which is that), the engine's own model. * The exception is a harness engine's own model: it ran when a request named none, so it stands over every column. */ export function foldModelsOverlay(modelsOverlay, fold) { const { conversion, merged, written, inherited, ownModels, renamed } = fold; const { notes } = conversion; const mergedConnections = merged.connections; const mergedHarnesses = merged.harnesses; const inheritedConnections = inherited.connections; const inheritedHarnesses = inherited.harnesses; const writtenConnections = written.connections; const writtenHarnesses = written.harnesses; /** Whether the config itself, the file or its base, already names a model for the alias. */ const configNames = (own, base, name, alias) => hasModel(own[name], alias) || hasModel(base[name], alias); const platformOf = (name) => String((mergedHarnesses[name] ?? inheritedHarnesses[name])?.platform ?? name); /** The column of models.json that named a harness: an `opencode-sdk` engine's was `opencode-sdk`, not its platform's. */ const columnOf = (name) => (renamed.sdkHarness.includes(name) ? "opencode-sdk" : platformOf(name)); /** Harnesses whose engine had a `model` of its own, which names every alias and so stands over the column (noted once each). */ const notedOwn = new Set(); let folded = false; for (const { alias, column, profile } of overlayRows(modelsOverlay)) { const p = typeof profile === "string" ? { model: profile } : isRecord(profile) ? profile : {}; const { effort, ...portable } = (isRecord(p.inference) ? p.inference : {}); const where = `models.json ${alias}.${column === "*" ? "(all)" : column}`; const wire = wireFieldsOf({ ...portable, reasoningEffort: portable.reasoningEffort ?? effort }, where, notes); if (p.engine !== undefined) { notes.push(`${where}: an engine indirection is not carried over.`); continue; } const candidates = column === "*" ? [...new Set([...Object.keys(mergedConnections), ...Object.keys(inheritedConnections)])] : column === "opencode-sdk" ? [...renamed.runtime] : renamed.connection(column) !== undefined ? [renamed.connection(column)] : column in writtenConnections || column in inheritedConnections ? [column] : []; const harnessNamed = [...new Set([...Object.keys(mergedHarnesses), ...Object.keys(inheritedHarnesses)])].filter((name) => columnOf(name) === column); if (candidates.length === 0 && harnessNamed.length === 0) { notes.push(`${where}: names no configured connection or harness, so it is not carried over.`); continue; } // The config's own choice stands, and `*` leaves what an explicit column already set. const connectionNames = candidates.filter((name) => !configNames(writtenConnections, inheritedConnections, name, alias) && !(column === "*" && hasModel(mergedConnections[name], alias))); const unnamed = harnessNamed.filter((name) => !configNames(writtenHarnesses, inheritedHarnesses, name, alias)); // The model a harness engine named is what ran when a request named none, and a harness runs its first model then, // so it stands over the column, which would put the column's models first. const harnessNames = unnamed.filter((name) => !ownModels.harnesses.has(name)); for (const name of unnamed.filter((n) => ownModels.harnesses.has(n) && !notedOwn.has(`${column}:${n}`))) { notedOwn.add(`${column}:${name}`); notes.push(`models.json (${column === "*" ? "all" : column}): harness "${name}" has a model of its own, which names every alias, so the column is not carried over.`); } if (connectionNames.length === 0 && harnessNames.length === 0) { // Only the config's own choice is worth a note; an explicit column that came first is the overlay's own. const keptByConfig = candidates.some((name) => configNames(writtenConnections, inheritedConnections, name, alias)) || harnessNamed.some((name) => configNames(writtenHarnesses, inheritedHarnesses, name, alias)); if (keptByConfig) notes.push(`${where}: the config already names a model for it, so it is not carried over.`); continue; } for (const name of connectionNames) { const current = mergedConnections[name]?.models; const existing = current?.[alias]; const model = str(p.model) ?? str(isRecord(existing) ? existing.model : existing) ?? ownModels.connections.get(name); if (model === undefined) continue; // A connection only the base defines gets a partial entry here; the base completes it. mergedConnections[name] ??= {}; modelsOf(mergedConnections[name])[alias] = modelEntry(model, wire); folded = true; } for (const name of harnessNames) { const model = str(p.model) ?? str(getHarness(platformOf(name))?.models?.[alias]); if (model === undefined) continue; mergedHarnesses[name] ??= {}; modelsOf(mergedHarnesses[name])[alias] = model; folded = true; if (Object.keys(wire).length > 0) notes.push(`${where}: a harness takes the model only; the inference settings are not carried over.`); } } if (folded) conversion.changed = true; } /** An engine's `model`: every alias nothing else named is that model, on the entry the engine became. */ export function foldEngineModels(merged, ownModels) { for (const [kind, entries] of [ ["connections", merged.connections], ["harnesses", merged.harnesses], ]) { for (const [name, model] of ownModels[kind]) { const entry = entries[name]; if (!isRecord(entry)) continue; for (const [alias, value] of Object.entries(allAliases(model) ?? {})) { if (!hasModel(entry, alias)) modelsOf(entry)[alias] = value; } } } }