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
97 lines (96 loc) • 4.41 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/.
/**
* The one substitution token akm hands to a process or a provider: `${ref}`,
* where `ref` is an akm ref (`[bundle//]conceptId`). A ref to a secret stands
* for the secret's value (what the owning file is decides that, not the ref's
* folder); any other asset ref stands for the asset's absolute path. A `${…}`
* body that is not an akm ref (`${HOME}`, `${PATH//:/ }`, `${v//-/_}`,
* `${x/a/b}`: not led by a stash folder and not qualified by an installed
* bundle) is plain text and is left alone.
*
* Kept free of the commands layer so config credentials and env values share
* this one function.
*/
import { stashDirFor, stashDirNames } from "./asset/asset-placement.js";
import { parseBundleRef } from "./asset/asset-ref.js";
export const REF_TOKEN_RE = /\$\{([^}\s]+)\}/g;
/**
* Does a `${…}` body parse as an akm ref (no export fragment) that either
* leads with a stash folder (bundle-qualified or not) or is bundle-qualified
* with a bundle `isBundleInstalled` accepts? The folder only tells a ref from
* shell text; it names no type. Anything else stays text, so shell
* syntax (`${v//-/_}`, `${x/a/b}`) is never read as a ref. A qualified opaque body
* follows the core ref grammar and the owner walk, whatever adapter owns it, but
* only for an installed bundle; with no predicate no bundle is installed.
*/
function isRefBody(body, isBundleInstalled) {
try {
const ref = parseBundleRef(body);
if (ref.fragment !== undefined)
return false;
if (stashDirNames().includes(ref.conceptId.split("/", 1)[0] ?? "") && ref.conceptId.includes("/"))
return true;
return ref.bundle !== undefined && isBundleInstalled?.(ref.bundle) === true;
}
catch {
return false;
}
}
/** Is the whole string exactly one `${ref}` token? */
export function isRefToken(value) {
const body = /^\$\{([^}\s]+)\}$/.exec(value)?.[1];
return body !== undefined && isRefBody(body);
}
/**
* Is the whole string exactly one `${ref}` token written in the documented
* credential form, `${secrets/<name>}`? A credential in config is read before any
* bundle is, so this is a check of the form, not of what the ref resolves to.
*/
export function isSecretRefToken(value) {
const body = /^\$\{([^}\s]+)\}$/.exec(value)?.[1];
return (body !== undefined && isRefBody(body) && parseBundleRef(body).conceptId.startsWith(`${stashDirFor("secret")}/`));
}
/** `<secret ref>:<VAR>`, as `--secret` and `secret:` take it; undefined when it is not that shape. */
export function parseSecretSpec(spec) {
const colon = spec.lastIndexOf(":");
const variable = spec.slice(colon + 1);
if (colon <= 0 || !/^[A-Za-z_][A-Za-z0-9_]*$/.test(variable))
return undefined;
return { ref: spec.slice(0, colon), variable };
}
/**
* Replace every `${ref}` token in each value. Keys are never touched, and
* tokens embedded in larger strings are all substituted. `isBundleInstalled`
* lets a bundle-qualified body that is not led by a native type be a ref when
* its bundle is installed. An unresolved ref is
* collected in `missing` and its token left in place. Resolved secret values
* must never be logged or printed by callers.
*/
export function substituteRefTokens(values, resolve, isBundleInstalled) {
const missing = [];
const secretValues = new Set();
const cache = new Map();
const out = {};
for (const [key, value] of Object.entries(values)) {
out[key] = value.replace(REF_TOKEN_RE, (token, body) => {
if (!isRefBody(body, isBundleInstalled))
return token;
if (!cache.has(body))
cache.set(body, resolve(body));
const resolved = cache.get(body);
if (resolved === undefined) {
if (!missing.includes(body))
missing.push(body);
return token;
}
if (resolved.kind === "path")
return resolved.path;
if (resolved.value)
secretValues.add(resolved.value);
return resolved.value;
});
}
return { values: out, missing, secretValues: [...secretValues] };
}