@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
91 lines • 3.66 kB
JavaScript
const RULE_SUBCOMMANDS = ["list", "show", "paths", "status"];
export function registerSlashCommands(pi, engine) {
pi.registerCommand("rules", {
description: "Inspect loaded pi-rules.",
getArgumentCompletions: (prefix) => {
const completions = RULE_SUBCOMMANDS.filter((subcommand) => subcommand.startsWith(prefix)).map((subcommand) => ({
value: subcommand,
label: subcommand,
}));
return completions.length > 0 ? completions : null;
},
handler: async (args, ctx) => {
const tokens = args.trim().length === 0 ? [] : args.trim().split(/\s+/);
const subcommand = tokens[0] ?? "";
const loaded = engine.loadStaticRules(ctx.cwd);
if (subcommand === "" || subcommand === "status") {
notify(ctx, buildSummaryText(loaded.rules, loaded.diagnostics));
return;
}
if (subcommand === "list") {
notify(ctx, formatRuleList(loaded.rules));
return;
}
if (subcommand === "show") {
const id = tokens[1];
if (id === undefined) {
notify(ctx, "Rule ID is required", "error");
return;
}
const rule = findRuleById(loaded.rules, id);
if (rule === null) {
notify(ctx, `Rule not found: ${id}`, "error");
return;
}
notify(ctx, rule.body);
return;
}
if (subcommand === "paths") {
notify(ctx, loaded.rules.map((rule) => rule.path).join("\n"));
return;
}
notify(ctx, `Unknown /rules subcommand: ${subcommand}`, "error");
},
});
pi.registerCommand("reload-rules", {
description: "Reload pi-rules for the current session.",
handler: async (_args, ctx) => {
engine.resetSession(ctx.cwd);
const loaded = engine.loadStaticRules(ctx.cwd);
notify(ctx, buildReloadText(loaded.rules, loaded.diagnostics));
},
});
}
function notify(ctx, message, severity = "info") {
ctx.ui.notify(message, severity);
}
function buildSummaryText(rules, diagnostics) {
return appendDiagnostics(`pi-rules: ${rules.length} rules from ${countSources(rules)} sources`, diagnostics);
}
function buildReloadText(rules, diagnostics) {
return appendDiagnostics(`Reloaded ${rules.length} rules from ${countSources(rules)} sources`, diagnostics);
}
function appendDiagnostics(text, diagnostics) {
return diagnostics.length === 0 ? text : `${text}, ${diagnostics.length} diagnostics`;
}
function countSources(rules) {
return new Set(rules.map((rule) => rule.source)).size;
}
function formatRuleList(rules) {
return rules
.map((rule) => `${rule.relativePath} [${rule.source}, ${formatMatchReason(rule.matchReason)}]`)
.join("\n");
}
function formatMatchReason(matchReason) {
if (typeof matchReason === "string") {
return matchReason;
}
if (matchReason.kind === "no-match") {
return matchReason.kind;
}
return `${matchReason.kind}:${matchReason.pattern}`;
}
function findRuleById(rules, id) {
const exactMatch = rules.find((rule) => rule.relativePath === id);
if (exactMatch !== undefined) {
return exactMatch;
}
const suffixMatches = rules.filter((rule) => rule.relativePath.endsWith(id));
return suffixMatches.length === 1 ? (suffixMatches[0] ?? null) : null;
}
//# sourceMappingURL=commands.js.map