@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
138 lines • 5.9 kB
JavaScript
import { join, resolve } from "node:path";
import { diagnostic } from "./diagnostics.js";
import { buildPluginEnv, DEFAULT_HOOK_PATH, directoryExists, fileExists, isRecord, MANIFEST_PATH, pluginSource, readJsonFile, resolveContainedPath, sourceForPath, } from "./plugin-manifest.js";
import { validateHookHandlerSafety } from "./safety.js";
import { parseHookConfig } from "./schema.js";
export function loadPluginHookManifest(options) {
const pluginRoot = resolve(options.pluginRoot);
const manifestPath = join(pluginRoot, MANIFEST_PATH);
const env = buildPluginEnv(pluginRoot, options.dataRoot);
const discoveredAt = options.discoveredAt ?? "pre-session";
const diagnostics = [];
const drafts = [];
if (!directoryExists(pluginRoot)) {
const source = pluginSource({
discoveredAt,
displayOrder: options.displayOrder,
env,
manifestPath,
pluginRoot,
sourcePath: manifestPath,
});
diagnostics.push(diagnostic({ code: "invalid_root", message: `Plugin root does not exist: ${pluginRoot}`, path: "$.pluginRoot" }, source));
return emptyResult(diagnostics);
}
if (fileExists(manifestPath)) {
const manifest = readJsonFile(manifestPath, manifestPath, sourceForPath(options, env, manifestPath));
if (manifest.ok) {
drafts.push(...readManifestHookDrafts(manifest.value, options, env, manifestPath, diagnostics));
}
else {
diagnostics.push(manifest.diagnostic);
}
}
const defaultPath = resolveContainedPath(pluginRoot, DEFAULT_HOOK_PATH);
if (options.includeDefaultHooks !== false && defaultPath.ok && fileExists(defaultPath.path)) {
const source = sourceForPath(options, env, defaultPath.path);
const config = readJsonFile(defaultPath.path, manifestPath, source);
if (config.ok) {
drafts.push({ config: config.value, key: defaultPath.path });
}
else {
diagnostics.push(config.diagnostic);
}
}
return parseDrafts(drafts, options, env, manifestPath, diagnostics);
}
export function selectHookCommandForPlatform(handler, platform = process.platform) {
if (platform === "win32" && handler.config.commandWindows !== undefined) {
return handler.config.commandWindows;
}
return handler.config.command;
}
function readManifestHookDrafts(manifest, options, env, manifestPath, diagnostics) {
if (!isRecord(manifest)) {
diagnostics.push(diagnostic({ code: "invalid_root", message: "Plugin manifest must be an object.", path: "$" }, sourceForPath(options, env, manifestPath)));
return [];
}
const hooks = manifest.hooks;
if (hooks === undefined) {
return [];
}
return normalizeHookDrafts(hooks, "$.hooks", options, env, manifestPath, diagnostics);
}
function normalizeHookDrafts(value, path, options, env, manifestPath, diagnostics) {
if (typeof value === "string") {
const resolved = resolveContainedPath(options.pluginRoot, value);
if (!resolved.ok) {
diagnostics.push(diagnostic({ code: "invalid_root", message: resolved.message, path }, sourceForPath(options, env, manifestPath)));
return [];
}
if (!fileExists(resolved.path)) {
diagnostics.push(diagnostic({ code: "invalid_root", message: `Plugin hook file does not exist: ${resolved.path}`, path }, sourceForPath(options, env, resolved.path)));
return [];
}
const source = sourceForPath(options, env, resolved.path);
const parsed = readJsonFile(resolved.path, manifestPath, source);
if (parsed.ok) {
return [{ config: parsed.value, key: resolved.path }];
}
diagnostics.push(parsed.diagnostic);
return [];
}
if (Array.isArray(value)) {
const drafts = [];
for (const [index, item] of value.entries()) {
drafts.push(...normalizeHookDrafts(item, `${path}[${index}]`, options, env, manifestPath, diagnostics));
}
return drafts;
}
if (isRecord(value)) {
return [{ config: value, key: `${manifestPath}#${inlineKeyPath(path)}` }];
}
diagnostics.push(diagnostic({ code: "invalid_root", message: "Plugin manifest hooks must be a path or hook object.", path }, sourceForPath(options, env, manifestPath)));
return [];
}
function parseDrafts(drafts, options, env, manifestPath, diagnostics) {
const sources = [];
const executableHandlers = [];
const parseDiagnostics = [];
for (const [index, draft] of drafts.entries()) {
const source = pluginSource({
discoveredAt: options.discoveredAt ?? "pre-session",
displayOrder: options.displayOrder + index,
env,
manifestPath,
pluginRoot: resolve(options.pluginRoot),
sourcePath: draft.key,
});
sources.push(source);
const parsed = parseHookConfig(draft.config, source);
for (const handler of parsed.executableHandlers) {
const safetyDiagnostics = validateHookHandlerSafety(handler);
if (safetyDiagnostics.length === 0) {
executableHandlers.push(handler);
}
else {
parseDiagnostics.push(...safetyDiagnostics);
}
}
parseDiagnostics.push(...parsed.diagnostics);
}
return {
diagnostics: [...diagnostics, ...parseDiagnostics],
parsed: { diagnostics: parseDiagnostics, executableHandlers },
sources,
};
}
function emptyResult(diagnostics) {
return {
diagnostics,
parsed: { diagnostics, executableHandlers: [] },
sources: [],
};
}
function inlineKeyPath(path) {
return path.replace(/^\$\.?/, "");
}
//# sourceMappingURL=plugin-loader.js.map