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

80 lines (79 loc) • 4.25 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/. import path from "node:path"; import { resolveSourceEntries } from "../indexer/search/search-source.js"; import { conceptIdFromTypeName, displayRef } from "./asset/resolve-ref.js"; import { UsageError } from "./errors.js"; import { prepareWriteTargetForMutation, resolveWorkingStashTarget, resolveWriteTarget, } from "./write-source.js"; function sameTarget(left, right) { if (left.path !== undefined && right.path !== undefined) { return path.resolve(left.path) === path.resolve(right.path); } return left.path === undefined && right.path === undefined && left.name === right.name; } function targetForCanonicalSource(config, source, options) { const target = source.registryId ? resolveWriteTarget(config, source.bundleId, options) : resolveWorkingStashTarget(config, options); if (source.unresolved || !sameTarget(target.source, { name: source.bundleId, path: source.path })) { throw new UsageError(`Bundle "${source.bundleId}" no longer resolves to its indexed source root.`); } return target; } /** Resolve the canonical bundle id currently assigned to a write target. */ export function canonicalBundleIdForTarget(config, target) { return (resolveSourceEntries(undefined, config).find((source) => !source.unresolved && sameTarget(target.source, { name: source.bundleId, path: source.path }))?.bundleId ?? target.source.name); } /** Resolve a canonical bundle qualifier to its writable target without preparing a Git boundary. */ export function resolveBundleWriteTarget(config, bundleId, options = {}) { const canonical = resolveSourceEntries(undefined, config).find((source) => source.bundleId === bundleId); if (!canonical) { throw new UsageError(`Bundle "${bundleId}" is not configured.`, "INVALID_FLAG_VALUE"); } const target = targetForCanonicalSource(config, canonical, options); return { ...target, source: { ...target.source, name: canonical.bundleId } }; } function resolveExplicitMutationTarget(config, explicitTarget, options) { try { return resolveWriteTarget(config, explicitTarget, options); } catch (error) { try { return resolveBundleWriteTarget(config, explicitTarget, options); } catch { throw error; } } } /** * Reconcile a qualified mutation ref with the explicit target (`--bundle`, or * the `options.flag` the caller's command spells), then resolve the write * destination. */ export function resolveMutationTarget(config, ref, explicitTarget, options = {}) { const flag = options.flag ?? "--bundle"; const writeOptions = { requireWritable: options.requireWritable, flag }; const qualifiedTarget = ref.origin ? resolveBundleWriteTarget(config, ref.origin, writeOptions) : undefined; const explicitResolved = explicitTarget ? resolveExplicitMutationTarget(config, explicitTarget, writeOptions) : undefined; if (qualifiedTarget && explicitResolved && !sameTarget(qualifiedTarget.source, explicitResolved.source)) { throw new UsageError(`Qualified ref bundle "${ref.origin}" conflicts with ${flag} "${explicitTarget}".`, "INVALID_FLAG_VALUE", `Drop ${flag} or select the same bundle.`); } let target = qualifiedTarget ?? explicitResolved ?? resolveWriteTarget(config, undefined, writeOptions); const bundleId = ref.origin ?? canonicalBundleIdForTarget(config, target); // The write is an asset of the ref's type: the bundle's adapter must write that type. target = prepareWriteTargetForMutation({ ...target, source: { ...target.source, name: bundleId } }, { assetType: ref.type }); const stableRef = { ...ref, origin: bundleId }; const defaultBundle = defaultBundleForTarget(config); return { target, ref: stableRef, displayRef: displayRef({ conceptId: conceptIdFromTypeName(stableRef.type, stableRef.name), bundleId: stableRef.origin }, defaultBundle), }; } export function defaultBundleForTarget(config) { return config.defaultBundle; }