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.68 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/.
import { isWithin, safeRealpath } from "./common.js";
import { expandProjectPath, NO_PROJECT } from "./config/schema/projects.js";
import { UsageError } from "./errors.js";
import { IncludeSchema } from "./include.js";
/** The project whose configured path most specifically contains `dir`, with that path as configured. */
export function projectForDirectory(config, dir) {
let best;
for (const [name, project] of Object.entries(config.projects ?? {})) {
for (const configured of project.paths ?? []) {
const root = expandProjectPath(configured);
if (!isWithin(dir, root))
continue;
const length = safeRealpath(root).length;
if (!best || length > best.length)
best = { name, path: configured, length };
}
}
return best && { name: best.name, path: best.path };
}
function toResolved(name, project, matchedPath) {
const { write = {} } = project;
return {
name,
...(matchedPath !== undefined ? { path: matchedPath } : {}),
...(project.from !== undefined ? { from: project.from } : {}),
...(project.filters !== undefined ? { filters: project.filters } : {}),
...(project.include !== undefined ? { include: IncludeSchema.strip().parse(project.include) } : {}),
prefer: project.prefer ?? { project: name },
...(project.rules !== undefined ? { rules: project.rules } : {}),
write: {
...(write.bundle !== undefined ? { bundle: write.bundle } : {}),
...(write.user !== undefined ? { user: write.user } : {}),
...(write.agent !== undefined ? { agent: write.agent } : {}),
...(write.channel !== undefined ? { channel: write.channel } : {}),
metadata: { project: name, ...write.metadata },
},
};
}
/**
* The project a call runs under: `project` (`--project`), else the one whose
* path most specifically contains `cwd` (default: the process's), else none.
* `none` selects no project; an unknown name is a usage error, because ignoring
* it would silently drop an isolating filter.
*/
export function resolveProject(config, options = {}) {
const { project: selected } = options;
if (selected === NO_PROJECT)
return undefined;
if (selected !== undefined) {
const project = Object.hasOwn(config.projects ?? {}, selected) ? config.projects?.[selected] : undefined;
if (!project) {
const known = Object.keys(config.projects ?? {});
throw new UsageError(`Unknown project "${selected}". ${known.length > 0 ? `Configured projects: ${known.join(", ")}.` : "No projects are configured."}`, "INVALID_FLAG_VALUE", `Add it under \`projects\` in your config, or pass --project ${NO_PROJECT} to run without one.`);
}
return toResolved(selected, project);
}
if (!config.projects)
return undefined;
const match = projectForDirectory(config, options.cwd ?? process.cwd());
const project = match && config.projects[match.name];
return match && project ? toResolved(match.name, project, match.path) : undefined;
}
export function projectView(project) {
return {
name: project.name,
...(project.path !== undefined ? { path: project.path } : {}),
...(project.from !== undefined ? { from: project.from } : {}),
...(project.filters !== undefined ? { filters: project.filters } : {}),
...(project.include !== undefined ? { include: project.include } : {}),
prefer: project.prefer,
};
}
function scopeText(scope) {
return Object.entries(scope)
.map(([key, value]) => `${key}=${[value ?? []]
.flat()
.map((v) => v || '""')
.join(",")}`)
.join(" ");
}
/** `text` opened by the project line when a project applied: `Project akm (~/code/akm): prefer project=akm`. */
export function withProjectLine(text, view) {
return view ? `${formatProjectLine(view)}\n${text}` : text;
}
function formatProjectLine(view) {
const parts = [
view.from !== undefined ? `from ${[view.from].flat().join(",")}` : "",
view.include ? `include ${scopeText(view.include)}` : "",
view.filters ? `filter ${scopeText(view.filters)}` : "",
view.prefer ? `prefer ${scopeText(view.prefer)}` : "",
].filter(Boolean);
return `Project ${view.name}${view.path ? ` (${view.path})` : ""}${parts.length > 0 ? `: ${parts.join("; ")}` : ""}`;
}