@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
152 lines • 5.52 kB
JavaScript
import { readFile } from "node:fs/promises";
import { diagnostic } from "./diagnostics.js";
import { parseHookConfig } from "./schema.js";
export function loadHookConfigSources(options) {
const executableHandlers = [];
const diagnostics = [];
let displayOrder = 0;
for (const candidate of createSourceCandidates(options)) {
const source = {
discoveredAt: candidate.discoveredAt,
displayOrder,
scope: candidate.scope,
sourcePath: candidate.sourcePath,
};
const loaded = loadCandidate(candidate, source, options.fileSystem);
if (!loaded) {
continue;
}
displayOrder += 1;
executableHandlers.push(...loaded.executableHandlers);
diagnostics.push(...loaded.diagnostics);
if (source.discoveredAt === "runtime") {
diagnostics.push(...runtimeSessionStartDiagnostics(loaded.executableHandlers, source));
}
}
return { executableHandlers, diagnostics };
}
/** Read files asynchronously, retaining the synchronous parser's source order and diagnostics. */
export async function loadHookConfigSourcesAsync(options) {
const files = new Map();
const candidates = createSourceCandidates(options);
await Promise.all(candidates.map(async (candidate) => {
if (candidate.kind !== "file")
return;
try {
files.set(candidate.sourcePath, { text: await readFile(candidate.sourcePath, "utf8") });
}
catch (error) {
files.set(candidate.sourcePath, error.code === "ENOENT" ? {} : { error });
}
}));
return loadHookConfigSources({
...options,
fileSystem: {
readTextFile(path) {
const file = files.get(path);
if (file?.error !== undefined)
throw file.error;
return file?.text;
},
},
});
}
function createSourceCandidates(options) {
const candidates = [];
if (options.globalSettingsHooks !== undefined) {
candidates.push({
discoveredAt: "pre-session",
hooks: options.globalSettingsHooks,
kind: "inline",
scope: "global",
sourcePath: "<global-settings-hooks>",
});
}
candidates.push(fileSource("global", options.globalHooksPath ?? `${options.agentDir}/hooks.json`, "pre-session"));
candidates.push(...fileSources("global", options.globalHookSourcePaths, "pre-session"));
if (options.projectSettingsHooks !== undefined) {
candidates.push({
discoveredAt: "pre-session",
hooks: options.projectSettingsHooks,
kind: "inline",
scope: "project",
sourcePath: "<project-settings-hooks>",
});
}
candidates.push(fileSource("project", options.projectHooksPath ?? `${options.cwd}/.senpi/hooks.json`, "pre-session"));
candidates.push(...fileSources("project", options.projectHookSourcePaths, "pre-session"));
candidates.push(...fileSources("plugin", options.preSessionHookSourcePaths, "pre-session"));
candidates.push(...fileSources("runtime", options.runtimeHookSourcePaths, "runtime"));
return candidates;
}
function fileSources(scope, paths, discoveredAt) {
if (!paths) {
return [];
}
return paths.map((path) => fileSource(scope, path, discoveredAt));
}
function fileSource(scope, sourcePath, discoveredAt) {
return { discoveredAt, kind: "file", scope, sourcePath };
}
function loadCandidate(candidate, source, fileSystem) {
if (candidate.kind === "inline") {
return parseHookConfig({ hooks: candidate.hooks }, source);
}
const text = readSourceText(candidate.sourcePath, source, fileSystem);
if (text === undefined) {
return undefined;
}
if (typeof text !== "string") {
return text;
}
try {
const parsed = JSON.parse(text);
return parseHookConfig(parsed, source);
}
catch (error) {
const message = error instanceof Error ? error.message : "Unknown JSON parse failure.";
return {
diagnostics: [
diagnostic({
code: "invalid_root",
message: `Hook source JSON is malformed: ${message}`,
path: "$",
}, source),
],
executableHandlers: [],
};
}
}
function readSourceText(sourcePath, source, fileSystem) {
try {
return fileSystem.readTextFile(sourcePath);
}
catch (error) {
const message = error instanceof Error ? error.message : "Unknown file read failure.";
return {
diagnostics: [
diagnostic({
code: "invalid_root",
message: `Hook source could not be read: ${message}`,
path: "$",
}, source),
],
executableHandlers: [],
};
}
}
function runtimeSessionStartDiagnostics(handlers, source) {
if (!handlers.some((handler) => handler.event === "SessionStart")) {
return [];
}
return [
diagnostic({
code: "unsupported_event",
event: "SessionStart",
message: "Runtime SessionStart hooks are loaded for reload or the next session only.",
path: "hooks.SessionStart",
severity: "warning",
}, source),
];
}
//# sourceMappingURL=config-loader.js.map