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

342 lines (341 loc) • 15.6 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/. /** * The base of the adapters of a bundle kept by a secret manager (`secret-tool`, * `azure-key-vault`): such a bundle holds secrets only, as the entries * `secrets/<name>` (the path a secret file has in a directory bundle), and the * adapter reaches them through the manager's command-line client. An entry is * no file: it has no `file`, and its index document has no `path`. * * `SecretManagerAdapterBase` implements every `BundleAdapter` member once; a * subclass supplies the four primitives that talk to the manager. */ import { spawnSync } from "node:child_process"; import { parseBundleRef } from "../asset/asset-ref.js"; import { compareCodePoints } from "../common.js"; import { assetNotFound, ConfigError, UsageError } from "../errors.js"; import { presentationFor } from "../type-presentation.js"; import { hashContent } from "./adapters/shared.js"; const SECRETS_DIR = "secrets"; /** The entry of the secret `name`. */ function secretEntryPath(name) { return `${SECRETS_DIR}/${name}`; } /** The secret an entry names, or undefined for an entry outside `secrets/`. */ function secretNameOf(entry) { const prefix = `${SECRETS_DIR}/`; return entry.startsWith(prefix) && entry.length > prefix.length ? entry.slice(prefix.length) : undefined; } /** The secret `conceptId` names. The facade reads the asset before it changes it, so an id outside `secrets/` is never asked of an adapter. */ function requireSecretName(bundle, conceptId) { const name = secretNameOf(conceptId); if (name === undefined) throw assetNotFound(`${bundle}//${conceptId}`); return name; } /** Whether `name` is a secret's name as a concept id: the one `secrets/<name>` that parses as a ref and reads back as itself. */ export function isConceptName(name) { if (name === "" || name.endsWith("/")) return false; try { const parsed = parseBundleRef(`b//${secretEntryPath(name)}`); return parsed.conceptId === secretEntryPath(name) && parsed.fragment === undefined; } catch { return false; } } /** The secret `name` as an entry, whose value `read` returns, at most once and only when asked. */ function secretEntry(name, read, locator) { let content; const value = () => { content ??= read(); return content; }; return { key: secretEntryPath(name), content: value, bytes: () => Buffer.from(value()), frontmatter: () => null, ...(locator !== undefined ? { locator } : {}), }; } /** One winner per name: the lowest id by code point. */ function classify(items) { const byName = new Map(); const invalid = []; for (const item of items) { if (!isConceptName(item.name)) invalid.push(item); else byName.set(item.name, [...(byName.get(item.name) ?? []), item]); } const winners = new Map(); const shadowed = []; for (const [name, held] of byName) { const ranked = [...held].sort((left, right) => compareCodePoints(left.id, right.id)); winners.set(name, ranked[0]); if (ranked.length > 1) shadowed.push({ name, winner: ranked[0], ignored: ranked.slice(1) }); } return { winners, shadowed, invalid }; } export class SecretManagerAdapterBase { addressedBy = "location"; writes = ["secret"]; /** A secret is deleted, never retired: nothing recoverable should keep its value. */ retires = []; /** Whether the adapter unlocks its store with the bundle's `credential` (a keyring's password); one that does not refuses it. */ takesCredential = false; /** The item that holds `name`, or undefined. By default the winner of one listing; a store can ask for it directly (a vault by name, a keyring by the `locator` the index recorded). */ findSecret(location, name, _locator) { return this.store(location).winners.get(name); } /** Refuse a name the store cannot hold, before anything is asked of it. */ requireStorable(_name) { } /** How `akm show` renders a secret: the type's own renderer. */ rendererFor(type) { return presentationFor(type).renderer; } /** The stores read so far this command, by location: a listing is one request, and a write drops it. */ stores = new Map(); /** The password of each store whose bundle has a `credential`, by location, as the bundle last gave it. */ credentials = new Map(); /** Forget what was read: the next request reads the store again. The CLI calls it per command, a server per request. */ clearCache() { this.stores.clear(); this.credentials.clear(); } /** The password that unlocks the store at `location`, when its bundle has a `credential`: read only when called. */ credentialOf(location) { return this.credentials.get(location); } store(location) { let held = this.stores.get(location); if (!held) { held = classify(this.listSecrets(location)); this.stores.set(location, held); } return held; } /** Where the adapter finds the bundle: its configured `location`. */ locationOf(b) { if (b.location) { if (b.credential && !this.takesCredential) { throw new ConfigError(`Bundle "${b.id}" has a \`credential\`, which the ${this.id} adapter does not take: remove it.`, "INVALID_CONFIG_FILE"); } if (b.credential) this.credentials.set(b.location, b.credential); else this.credentials.delete(b.location); return b.location; } throw new ConfigError(`Bundle "${b.id}" uses the ${this.id} adapter, which reads ${this.what}: set the bundle's \`location\` to its name.`, "INVALID_CONFIG_FILE"); } /** The name a new asset gives a secret. The facade checks `writes` first, so another type is never asked of the adapter. */ secretNameFor(asset) { if (asset.type !== "secret") { throw new Error(`Adapter "${this.id}" does not write ${asset.type} assets; the asset facade checks \`writes\` first.`); } if (!isConceptName(asset.name)) { throw new UsageError(`"${asset.name}" is not a name a secret can have: it must read back as the ref "secrets/${asset.name}".`, "INVALID_FLAG_VALUE"); } this.requireStorable(asset.name); return asset.name; } /** What a write did to the secret `name` (its `content` after a store, and the entry of the item it was stored in): no file changed. */ change(name, content, stored) { return { conceptId: secretEntryPath(name), changed: [], ...(content !== undefined ? { content } : {}), ...(content !== undefined && stored ? { entry: secretEntry(name, () => content.toString(), stored.locator) } : {}), }; } /** Run a write on the store: whatever was read of it is stale afterwards, whether the write worked or not. */ writing(location, write) { try { return write(); } finally { this.stores.delete(location); } } exists(b) { try { return this.locate(this.locationOf(b)); } catch { return false; } } /** List the bundle's secrets. A store that cannot be reached throws `BUNDLE_UNREACHABLE`: the index keeps its rows. */ list(b) { const held = this.store(this.locationOf(b)); return { assets: [...held.winners.values()].map(({ name, read, locator }) => ({ conceptId: secretEntryPath(name), entry: secretEntry(name, read, locator), })), complete: true, findings: this.findings(b, held), }; } read(b, conceptId, locator) { const name = secretNameOf(conceptId); if (name === undefined) return undefined; const found = this.findSecret(this.locationOf(b), name, locator); return found === undefined ? undefined : { conceptId: secretEntryPath(name), entry: secretEntry(name, found.read, found.locator) }; } /** A secret is described from its path alone: its name, and nothing of its value. */ recognize(b, entry) { const name = secretNameOf(entry.key); if (name === undefined) return null; return { ref: `${b.id}//${entry.key}`, bundle: b.id, conceptId: entry.key, // An entry is its name and where the store holds it: an unchanged listing rewrites no row. hash: hashContent(entry.locator === undefined ? entry.key : `${entry.key}\n${JSON.stringify(entry.locator)}`), adapterId: this.id, type: "secret", name: name.split("/").pop(), ...(entry.locator !== undefined ? { locator: entry.locator } : {}), }; } /** What is wrong with the store as listed: a name that is not a valid concept name is not listed, and items sharing a name leave one winner. */ findings(b, held) { return [ ...held.invalid.map(({ name, id }) => ({ ref: `${b.id}//${secretEntryPath(name)}`, issue: "invalid-name", detail: `The entry "${name}" (${id}) is not a name a secret can have, so it is not listed. Rename it in ${this.what}.`, fixed: false, })), ...held.shadowed.map(({ name, winner, ignored }) => ({ ref: `${b.id}//${secretEntryPath(name)}`, issue: "shadowed-spelling", detail: `"${secretEntryPath(name)}" is held by more than one entry; ${winner.id} is used and ${ignored.map((item) => item.id).join(", ")} ignored. Remove the ignored ${ignored.length === 1 ? "entry" : "entries"}.`, fixed: false, })), ]; } /** Nothing is checked live: lint never lists the store. What `akm index` found (`findings`) the facade adds from the index. */ async validate(_b, _edits, _ctx) { return []; } place(_b, asset) { return { conceptId: secretEntryPath(this.secretNameFor(asset)) }; } create(b, asset, options = {}) { const name = this.secretNameFor(asset); const location = this.locationOf(b); const existing = this.findSecret(location, name, options.locator); if (existing && !options.replace) { throw new Error(`Adapter "${this.id}" will not create "${b.id}//${secretEntryPath(name)}" over an existing secret without \`replace\`; the asset facade checks \`exclusive\` first.`); } const stored = this.writing(location, () => this.storeSecret(location, name, Buffer.from(asset.content), existing)); return this.change(name, asset.content, stored); } update(b, conceptId, content, options = {}) { const location = this.locationOf(b); const name = requireSecretName(b.id, conceptId); const existing = this.findSecret(location, name, options.locator); if (!existing) throw assetNotFound(`${b.id}//${conceptId}`); const stored = this.writing(location, () => this.storeSecret(location, name, Buffer.from(content), existing)); return this.change(name, content, stored); } delete(b, conceptId, options = {}) { const location = this.locationOf(b); const name = requireSecretName(b.id, conceptId); const existing = this.findSecret(location, name, options.locator); if (!existing) throw assetNotFound(`${b.id}//${conceptId}`); this.writing(location, () => this.deleteSecret(location, existing)); return [this.change(name)]; } packageFiles(_b, _conceptId) { return this.refusePackages(); } mirrorPackage(_b, _asset, _files, _options) { return this.refusePackages(); } refusePackages() { throw new Error(`Adapter "${this.id}" keeps no packages; the asset facade checks \`writes\` first.`); } retire(_b, _conceptId, _note) { return this.refuseRetire(); } retired(_b) { return []; } restore(_b, _handle, _fallback) { return this.refuseRetire(); } forget(_b, _handle) { return this.refuseRetire(); } expireRetired(_b, _before) { return this.refuseRetire(); } refuseRetire() { throw new Error(`Adapter "${this.id}" does not retire assets; the asset facade checks \`retires\` first.`); } } /** Wait `ms` without returning to the event loop: adapters answer synchronously. */ export function sleepSync(ms) { Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms); } /** How long one client call is given: a client that hangs fails the command, not akm. */ const CLIENT_TIMEOUT_MS = 60_000; /** * Run a client and return what it printed and how it exited. `input` is * written to its stdin, which is how a value reaches a client: never as an * argument, where other processes could read it. */ export function runClient(client, args, input, timeoutMs = CLIENT_TIMEOUT_MS) { // The environment as it is now: Bun 1.3 finds the program on the PATH the process started with unless it is given. const result = spawnSync(client.command, [...args], { encoding: "utf8", input, env: process.env, timeout: timeoutMs, killSignal: "SIGKILL", }); if (result.error) { const missing = result.error.code === "ENOENT"; if (result.error.code === "ETIMEDOUT") { throw new ConfigError(`"${client.command}" did not answer within ${timeoutMs / 1000} s.`, "BUNDLE_UNREACHABLE"); } throw new ConfigError(missing ? `"${client.command}" is not installed.` : `"${client.command}" could not be run: ${result.error.message}`, "BUNDLE_UNREACHABLE", missing ? `Install ${client.install}.` : undefined); } return { status: result.status ?? 1, stdout: result.stdout, stderr: result.stderr }; } /** What a client's message says when it could not reach the store or was not let in: a vault or keyring that is not there, no session bus, no login. */ const UNREACHABLE = /az login|AADSTS|AuthorizationFailed|Forbidden|VaultNotFound|ServiceUnknown|Failed to connect|Cannot autolaunch|No such (secret )?collection|Unknown object|org\.freedesktop\.DBus\.Error/i; /** * The error for a request a client failed, with the client's own message. A store that cannot be reached or * refuses the account is `BUNDLE_UNREACHABLE`; any other failure is a plain error naming the operation. */ export function clientFailure(client, action, result) { const reason = result.stderr.trim() || `exit status ${result.status}`; const message = `${client.command} could not ${action}: ${reason}`; return UNREACHABLE.test(reason) ? new ConfigError(message, "BUNDLE_UNREACHABLE") : new Error(message); } /** Run a client request that must succeed, and return what it printed. */ export function requireClient(client, action, args, input, timeoutMs) { const result = runClient(client, args, input, timeoutMs); if (result.status !== 0) throw clientFailure(client, action, result); return result.stdout; }