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

114 lines (113 loc) • 4.97 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/. /** Readers shared by the workspace operations: the read-only state database, the stash directory, per-section isolation and caps. */ import fs from "node:fs"; import { isDeepStrictEqual } from "node:util"; import { resolveStashDir } from "../core/common.js"; import { loadConfig, sanitizeConfigForWrite } from "../core/config/config.js"; import { getConfigValueSource } from "../core/config/config-extends.js"; import { bundleAtDir } from "../core/config/config-sources.js"; import { UsageError } from "../core/errors.js"; import { getStateDbPath } from "../core/state-db.js"; import { resolveWorkingStashTarget } from "../core/write-source.js"; import { readUsableIndex } from "../indexer/index-readiness.js"; import { implicitStashDir } from "../indexer/search/search-source.js"; import { getEntryIdByFilePath, getItemRefById } from "../storage/repositories/index-entries-repository.js"; export { withReadonlyStateDb } from "../core/state-db.js"; export function stateDbUnavailable() { // asset-io: state return { available: false, reason: fs.existsSync(getStateDbPath()) ? "needs-migration" : "absent" }; } export function tryStashDir() { try { return resolveStashDir(); } catch { return null; } } /** `section` for a synchronous read, where the caller needs a fallback value (a read inside an open database). */ export function attempt(errors, name, read, fallback) { try { return read(); } catch (error) { errors[name] = error instanceof Error ? error.message : String(error); return fallback; } } /** Run each section of a composite response on its own: one failing store degrades one section, not the page. */ export async function section(errors, name, fn) { try { return await fn(); } catch (error) { errors[name] = error instanceof Error ? error.message : String(error); return null; } } const DEFAULT_LIST_LIMIT = 50; const MAX_LIST_LIMIT = 200; /** A list size from a caller: the default when absent or not a positive integer, never above the cap. */ export function clampLimit(limit, fallback = DEFAULT_LIST_LIMIT) { if (typeof limit !== "number" || !Number.isInteger(limit) || limit < 1) return fallback; return Math.min(limit, MAX_LIST_LIMIT); } /** Most characters of either side a detail view returns; a larger file is cut and flagged `truncated`. */ export const DIFF_SIDE_MAX_CHARS = 512 * 1024; export function capSide(text) { if (text === null || text.length <= DIFF_SIDE_MAX_CHARS) return { text, truncated: false }; return { text: text.slice(0, DIFF_SIDE_MAX_CHARS), truncated: true }; } /** * The working stash is the directory akm reads besides the configured bundles (`implicitStashDir`). * It is described only when no configured bundle already covers that directory. */ export function workingStashDir(config) { try { const dir = implicitStashDir(config); return dir && !bundleAtDir(config, dir) ? dir : undefined; } catch { return undefined; } } /** Refuse a write that the config sanitizer would turn into a silent credential removal. */ export function preserveUnrelatedConfig(config = loadConfig()) { if (!isDeepStrictEqual(sanitizeConfigForWrite(config, { quiet: true }), config)) { throw new UsageError("AKM's config sanitizer would remove unrelated credentials. Remove registry URL credentials using the CLI before changing bundle configuration in the UI."); } } export function configSource(path) { // `getConfigValueSource` takes a dotted path; a key that contains a dot cannot be told apart from two keys. if (path.some((segment) => segment.includes("."))) return null; try { const source = getConfigValueSource(path.join(".")); return source === "local" ? "file" : source; } catch { return null; } } /** Where an automatic write goes (no defaultWriteTarget): AKM_BUNDLE_DIR first, then the default bundle. */ export function automaticWriteTarget(config) { try { const { source } = resolveWorkingStashTarget(config, { requireWritable: false }); const path = source.path ?? null; return { name: source.name, path, workingStash: path !== null && path === workingStashDir(config) }; } catch { return null; // No AKM_BUNDLE_DIR and no usable default bundle: writes need an explicit target. } } /** The durable `bundle//conceptId` of the indexed asset at `filePath`, the key feedback rows use. */ export function durableRefForPath(filePath) { return readUsableIndex((db) => { const id = getEntryIdByFilePath(db, filePath); return id === undefined ? null : getItemRefById(db, id); }, null); }