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

248 lines (247 loc) • 13.4 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/. /** * What `env` and `secret` add to an asset: typed refs, the owner check a command makes on them, a secret's * value, and the write target of a mutation. Finding, listing and writing go through the asset facade * (`src/core/assets/`), so an env file or secret behaves the same whichever adapter holds it. The only value read * here is a secret's, for the callers that inject it ({@link readSecret}); no value is surfaced. */ import path from "node:path"; import { resolveSourceEntries } from "../indexer/search/search-source.js"; import { resolveSourcesForOrigin } from "../registry/origin-resolve.js"; import { assertFlatAssetName, combineCreatePath, normalizeCreateSubPath } from "./asset/asset-create.js"; import { stashDirFor } from "./asset/asset-placement.js"; import { makeBundleRef, parseBundleRef } from "./asset/asset-ref.js"; import { conceptIdFromTypeName, displayRef } from "./asset/resolve-ref.js"; import { listAssets, lookupAsset, readAsset, unindexedHint } from "./assets/read.js"; import { assetType } from "./assets/type.js"; import { fileOfRef, writeAssets } from "./assets/write.js"; import { isWithin, scanEnvKeyNames } from "./common.js"; import { loadConfig } from "./config/config.js"; import { envReferenceName } from "./config/schema/primitives.js"; import { ConfigError, NotFoundError, UsageError } from "./errors.js"; import { resolveMutationTarget } from "./mutation-target.js"; import { formatRefForMessage, requireWriteTargetPath } from "./write-source.js"; /** * The secrets in the type's folder (`secrets/`): the refs the secret commands accept. A file elsewhere that is typed * the same (`team/secrets/token`, which may be another bundle's `secrets/token`) is not listed. A keyring's or vault's * secrets come from the index (`akm index` refreshes them), so listing never reaches, or unlocks, the store. */ function listSecretsInFolder() { const prefix = `${stashDirFor("secret")}/`; return listAssets({ type: "secret" }).filter(({ conceptId }) => conceptId.startsWith(prefix)); } /** * Every listed env asset with its key names (never values or comments), for `commands/health` as well as * `env list`. An env file is an env asset wherever it sits (`x/env/app.env`), as it is for `env path`, `env run` * and `${ref}` tokens. A file inside a bundle nested in another is listed once, as the nested bundle's. * `config` defaults to the real `loadConfig()`. */ export function listEnvsRecursive(config = loadConfig()) { const sources = resolveSourceEntries(undefined, config).flatMap(({ bundleId, path }) => path === undefined ? [] : [{ bundleId, root: path }]); // The folders of a bundle that another bundle is at: what is under them is that bundle's. const nested = new Map(sources.map(({ bundleId, root }) => [ bundleId, sources .map((inner) => path.relative(root, inner.root)) .filter((relative) => relative !== "" && !relative.startsWith("..") && !path.isAbsolute(relative)) .map((relative) => `${relative.split(path.sep).join("/")}/`), ])); return listAssets({ type: "env" }) .filter(({ bundle, conceptId }) => !nested.get(bundle)?.some((folder) => conceptId.startsWith(folder))) .map(({ conceptId, bundle, entry }) => ({ ref: makeEnvRef(conceptId, bundle, config), // asset-io: display the env file's path in a listing ...(entry.file !== undefined ? { path: entry.file } : {}), keys: scanEnvKeyNames(entry.content()), })); } /** Every listed secret, by ref. Its value is not read. */ export function listSecrets() { return listSecretsInFolder().map(({ conceptId, bundle, entry }) => ({ ref: makeSecretRef(conceptId, bundle), // asset-io: display the secret file's path in a listing ...(entry.file !== undefined ? { path: entry.file } : {}), })); } export function parseEnvRef(ref) { return parseTypedRef(ref, "env", "env/<name>"); } export function parseSecretRef(ref) { return parseTypedRef(ref, "secret", "secrets/<name>"); } /** * Parse `ref` as the placement address of a NEW (or canonically placed) asset of * the type the command names: `[bundle//]<type folder>/<name>`. The type is the * command's, not read from the ref. There is no bare-name form: `prod` is a * usage error that also names the ref it probably meant (`env/prod`), as text * only — the suggestion is never resolved or accepted. */ function parseTypedRef(ref, type, form) { const parsed = parseBundleRef(ref); const prefix = `${stashDirFor(type)}/`; if (parsed.fragment !== undefined || !parsed.conceptId.startsWith(prefix) || parsed.conceptId === prefix) { const hint = parsed.fragment === undefined && !parsed.conceptId.includes("/") ? ` Did you mean "${makeBundleRef(parsed.bundle, conceptIdFromTypeName(type, parsed.conceptId))}"?` : ""; throw new UsageError(`Expected ${type === "env" ? "an" : "a"} ${type} ref (${form} or <bundle>//${form}); got "${ref}". Write the full ref.${hint}`, "INVALID_FLAG_VALUE"); } return { type, name: parsed.conceptId.slice(prefix.length), origin: parsed.bundle }; } /** * The asset a read ref of the given type names. The ref is the asset's path inside its bundle, so it may sit * anywhere: what makes it an env or a secret is the asset, not the ref's first folder. */ function ownerOfType(ref, type, form) { const parsed = parseBundleRef(ref); const asset = parsed.fragment === undefined ? resolveRefOwner(ref) : undefined; if (!asset) { // Not found: a typed placement address reads as "not found", anything else as the wrong kind of ref. parseTypedRef(ref, type, form); throw new NotFoundError(`${type === "env" ? "Env" : "Secret"} not found: ${ref}`, "ASSET_NOT_FOUND", unindexedHint(ref)); } if (assetType(asset) !== type) { throw new UsageError(`Expected ${type === "env" ? "an" : "a"} ${type} ref (${form} or <bundle>//${form}); got "${ref}". Write the full ref.`, "INVALID_FLAG_VALUE"); } // The ref as the caller wrote it (`env/app.env`): for output only, which echoes a ref as written, not as akm keeps it. return { written: parsed.conceptId, asset }; } /** The asset any ref names right now (a sensitive env file or secret included), or undefined when no bundle holds it. The lookup behind `${ref}` tokens. */ export function resolveRefOwner(ref, from) { return lookupAsset(ref, from); } /** * A secret's value: its content as its adapter reads it, with surrounding whitespace trimmed, so a hand-written * file's trailing newline never lands in an HTTP header. The one read every consumer uses. */ export function secretValueOf(asset) { return asset.entry.content().trim(); } /** * Is `bundle` the id of an installed bundle in the active source set? The predicate `${ref}` substitution uses to * tell a qualified ref from shell text. A source set that cannot be read (for example `AKM_BUNDLE_DIR` naming a * disabled bundle) answers "not installed", so shell text never raises a config error; a real ref then fails where * it is resolved, with that error. */ export function isInstalledBundle(bundle) { try { return resolveSourcesForOrigin(bundle, resolveSourceEntries(undefined, loadConfig())).length > 0; } catch { return false; } } /** * `config` defaults to the real `loadConfig()`; `listEnvsRecursive` threads its own through so a caller-supplied * config governs ref display as it governs which bundles are listed. */ export function makeEnvRef(conceptId, bundleId, config = loadConfig()) { // `env/name` in the primary bundle, `bundle//env/name` in another. return displayRef({ conceptId, bundleId }, displayDefaultBundle(bundleId, config)); } export function makeSecretRef(conceptId, bundleId) { return displayRef({ conceptId, bundleId }, displayDefaultBundle(bundleId)); } function displayDefaultBundle(bundleId, config = loadConfig()) { if (config.defaultBundle || !bundleId) return config.defaultBundle; const primary = resolveSourceEntries(undefined, config)[0]?.bundleId; return primary === bundleId ? bundleId : undefined; } /** The env asset a ref names right now; `NotFoundError` when no bundle holds it (creating one goes through {@link resolveEnvWriteTarget}). */ export function resolveEnvPath(ref) { return ownerOfType(ref, "env", "env/<name>"); } /** The secret asset a ref names right now. Its content is the value: never print or log it. */ export function resolveSecretPath(ref) { return ownerOfType(ref, "secret", "secrets/<name>"); } /** * Read the secret `ref` names: its trimmed value and its canonical ref in the display form (what an audit event keeps, * never the spelling the caller wrote). * Callers MUST NOT write the value to stdout or any log. */ export function readSecret(ref) { const { asset } = ownerOfType(ref, "secret", "secrets/<name>"); return { ref: makeSecretRef(asset.conceptId, asset.bundle), value: secretValueOf(asset) }; } /** The bundles whose `credential` is being read, so one that names a secret of the store it unlocks fails, not recurses. */ const readingCredential = new Set(); /** * The password a store bundle's `credential` gives: the variable a `$VAR` / `${VAR}` names, or the value of the secret * a `${secrets/<name>}` names. Callers MUST NOT write the value to stdout or any log. */ export function readCredential(bundle, reference) { if (readingCredential.has(bundle)) { throw new ConfigError(`Bundle "${bundle}" has a credential that names a secret of "${bundle}" itself, which the credential is needed to unlock.`, "INVALID_CONFIG_FILE", "Keep the keyring's password in another bundle, or in an environment variable."); } readingCredential.add(bundle); try { const variable = envReferenceName(reference); const value = variable !== undefined ? process.env[variable] : readSecret(reference.slice(2, -1)).value; if (!value) { throw new ConfigError(`The credential of bundle "${bundle}", ${reference}, is empty.`, "INVALID_CONFIG_FILE"); } return value; } finally { readingCredential.delete(bundle); } } /** * Resolve the destination of an env or secret mutation: an explicit * `--bundle`, else `defaultWriteTarget`, else the working bundle, whose adapter * must write the type. `create` enforces a flat ref name and applies `--path` * as the subdirectory. */ function resolveEnvSecretWriteTarget(type, ref, writeTarget, create) { const parsed = type === "env" ? parseEnvRef(ref) : parseSecretRef(ref); if (create) { assertFlatAssetName(parsed.name); parsed.name = combineCreatePath(normalizeCreateSubPath(create.subPath), parsed.name); } const resolved = resolveMutationTarget(loadConfig(), parsed, writeTarget); // The file the name is kept in (a link included) must stay in the type's folder; a store keeps no file to look up. const { source } = resolved.target; const folder = stashDirFor(type); const file = fileOfRef(source, conceptIdFromTypeName(type, parsed.name)); if (file !== undefined && folder !== undefined && !isWithin(file, path.join(requireWriteTargetPath(source), folder))) { throw new UsageError(`${type === "env" ? "Env" : "Secret"} name "${parsed.name}" escapes the ${folder === "env" ? "env" : "secrets"} directory.`, "INVALID_FLAG_VALUE"); } return { type, name: parsed.name, conceptId: conceptIdFromTypeName(type, parsed.name), target: resolved.target, ref: resolved.displayRef, }; } export function resolveEnvWriteTarget(ref, writeTarget, create) { return resolveEnvSecretWriteTarget("env", ref, writeTarget, create); } export function resolveSecretWriteTarget(ref, writeTarget, create) { return resolveEnvSecretWriteTarget("secret", ref, writeTarget, create); } /** The env file or secret a write resolution names, as the target bundle's adapter reads it; undefined when it has none. */ export function findWriteTargetAsset(resolution) { return readAsset(resolution.target.source, resolution.conceptId); } /** * Create, update or delete the env file or secret a resolution names, through the target bundle's adapter, as one * operation committed at the target's boundary. A removal deletes: an env file or secret is never retired. */ export function writeEnvSecret(resolution, operation) { const { type, name, conceptId, target } = resolution; const verb = operation.op === "delete" ? "Remove" : "Update"; return writeAssets(target, { purpose: `${type}-${verb.toLowerCase()}`, message: `${verb} ${formatRefForMessage({ type, name, origin: target.source.name })}`, }, (w) => operation.op === "create" ? w.create({ type, name, content: operation.content }, { exclusive: operation.exclusive }) : operation.op === "update" ? w.update(conceptId, operation.content, { revision: operation.revision }) : w.delete(conceptId, { revision: operation.revision })); }