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
210 lines (209 loc) • 9.76 kB
JavaScript
// 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/.
/**
* `akm migrate apply`'s key step. akm reads a key from a `$VAR` reference or
* from `credentials.json`, nowhere else. The keys a config still holds in any
* other form are read once, filed in `credentials.json` for the block they
* belong to (a connection, or the `embedding` service) and the
* endpoint that block names, and removed from the document:
*
* - an engine's `apiKeyFile`, a literal `apiKey` or a `${secrets/...}` one,
* which the conversion to a connection cannot carry;
* - the same literal or secret-store `apiKey` on a connection or `embedding` that the file already says in the current form.
*
* A key that cannot be read, whose block names no endpoint to file it for, whose
* engine wrote it for another endpoint than the connection now names (the file's
* current-form entries overlay the converted engine), or whose slot
* `credentials.json` already holds a different key or endpoint for, is not moved. It stays in the file: on the block that held
* it, or, for an engine's `apiKey`, put back on the connection as `apiKey`
* exactly as it was, so the rewrite never leaves it in the backups alone (akm
* ignores a key held there, with a warning, until it is moved). An engine's
* `apiKeyFile` is the exception: it names a file, not a key, and a connection
* cannot hold it (an unowned key is sent as a request field), so a note names
* the path. A note names the key's place and its entry, never its value.
*/
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { isRecord } from "../common.js";
import { sameEndpoint } from "../endpoint-url.js";
import { redactUrlCredentials } from "../redaction.js";
import { isSecretRefToken } from "../ref-token.js";
import { slotStanding } from "./credentials.js";
import { isEnvReference } from "./schema/primitives.js";
/** An `apiKeyFile`'s key: the file's contents less one trailing newline, `~` expanded as it always was. */
function readKeyFile(file) {
try {
const expanded = file.startsWith("~") ? path.join(os.homedir(), file.slice(1)) : file;
return fs.readFileSync(expanded, "utf8").replace(/\r?\n$/, "") || undefined;
}
catch {
return undefined;
}
}
/** A key held in the config itself: the literal, or the secret it references. */
function heldValue(value, options) {
if (!isSecretRefToken(value))
return value;
try {
return options.resolveSecretRef?.(value) || undefined;
}
catch {
return undefined;
}
}
/** The keys of the converted document that are not `$VAR` references. */
function documentCandidates(config, options) {
const blocks = [];
if (isRecord(config.connections)) {
for (const [name, block] of Object.entries(config.connections)) {
if (isRecord(block))
blocks.push({ name, slot: { connection: name }, from: `connections.${name}.apiKey`, block });
}
}
if (isRecord(config.embedding)) {
blocks.push({
name: "embedding",
slot: { service: "embedding" },
from: "embedding.apiKey",
block: config.embedding,
});
}
return blocks.flatMap(({ name, slot, from, block }) => {
const endpoint = typeof block.endpoint === "string" ? block.endpoint : undefined;
const found = [];
const apiKey = block.apiKey;
if (typeof apiKey === "string" && !isEnvReference(apiKey)) {
found.push({
name,
slot,
endpoint,
from,
read: () => heldValue(apiKey, options),
source: isSecretRefToken(apiKey) ? "the secret store reference" : "the literal key",
remove: () => {
delete block.apiKey;
},
stays: "in the file",
});
}
// A key file an earlier run could not read is still named here, so a later run can move its key.
const keyFile = block.apiKeyFile;
if (typeof keyFile === "string" && "connection" in slot) {
found.push({
name,
slot,
endpoint,
from: from.replace(/apiKey$/, "apiKeyFile"),
read: () => readKeyFile(keyFile),
source: `its key file ${keyFile}`,
remove: () => {
delete block.apiKeyFile;
},
stays: "in the file",
});
}
return found;
});
}
/** The endpoint the document gives the block that `slot` stands for, if it gives one. */
export function documentEndpoint(config, slot) {
const block = "connection" in slot
? isRecord(config.connections)
? config.connections[slot.connection]
: undefined
: config.embedding;
return isRecord(block) && typeof block.endpoint === "string" ? block.endpoint : undefined;
}
/** The keys the conversion could not carry from an engine, read from the original document. */
function engineCandidates(original, converted, config, options) {
const engines = isRecord(original.engines) ? original.engines : {};
return converted.keys.flatMap(({ engine, connection, field }) => {
const source = isRecord(engines[engine]) ? engines[engine] : {};
const held = source[field];
if (typeof held !== "string")
return [];
const owner = typeof source.endpoint === "string" ? source.endpoint : undefined;
if (field === "apiKeyFile") {
return [
{
name: connection,
slot: { connection },
endpoint: documentEndpoint(config, { connection }),
owner,
from: `engines.${engine}.${field}`,
read: () => readKeyFile(held),
source: `its key file ${held}`,
restore: () => {
const block = isRecord(config.connections) ? config.connections[connection] : undefined;
if (isRecord(block))
block.apiKeyFile = held;
},
stays: "on the connection as apiKeyFile",
},
];
}
return [
{
name: connection,
slot: { connection },
endpoint: documentEndpoint(config, { connection }),
owner,
from: `engines.${engine}.${field}`,
read: () => heldValue(held, options),
source: isSecretRefToken(held) ? `the secret store reference ${held}` : "the literal key",
restore: () => {
const block = isRecord(config.connections) ? config.connections[connection] : undefined;
if (isRecord(block))
block.apiKey = held;
},
stays: "on the connection as apiKey",
},
];
});
}
export function planKeyMoves(original, converted, options = {}) {
const config = structuredClone(converted.config);
const planned = new Map();
const moves = [];
const notes = [];
for (const candidate of [
...engineCandidates(original, converted, config, options),
...documentCandidates(config, options),
]) {
const stays = ` It stays ${candidate.stays}.`;
const { owner } = candidate;
if (owner !== undefined && candidate.endpoint !== undefined && !sameEndpoint(owner, candidate.endpoint)) {
candidate.restore?.();
notes.push(`${candidate.from}: this key was written for ${redactUrlCredentials(owner)}, and the file now gives "${candidate.name}" ${redactUrlCredentials(candidate.endpoint)}, so no key moves and it is not sent there.${stays} Store a key for the new endpoint with \`akm setup\`.`);
continue;
}
const value = candidate.read();
if (value === undefined) {
candidate.restore?.();
notes.push(`${candidate.from}: ${candidate.source} could not be read, so no key moves and this is still to do.${stays} A later \`akm migrate apply\` moves it once it can be read.`);
continue;
}
const { endpoint } = candidate;
if (endpoint === undefined) {
candidate.restore?.();
notes.push(`${candidate.from}: the block names no endpoint of its own, so its key cannot be filed for one and no key moves.${stays} Store it for the endpoint that serves it with \`akm setup\`.`);
continue;
}
const slotKey = JSON.stringify(candidate.slot);
const earlier = planned.get(slotKey);
const standing = slotStanding(candidate.slot, value, endpoint, earlier && { key: earlier.value, endpoint: earlier.endpoint });
if (standing === "different") {
candidate.restore?.();
notes.push(`${candidate.from}: credentials.json already holds a different key or endpoint for "${candidate.name}"; it is not replaced.${stays}`);
continue;
}
const move = { slot: candidate.slot, endpoint, name: candidate.name, value, from: candidate.from };
planned.set(slotKey, move);
moves.push(move);
notes.push(`${candidate.from}: its key moves to credentials.json for "${candidate.name}" at ${redactUrlCredentials(endpoint)}.`);
candidate.remove?.();
}
return { config, moves, notes };
}