naisys
Version:
NAISYS - Autonomous AI agent runner with built-in context management and cost tracking
85 lines • 3.36 kB
JavaScript
/**
* Registry pattern for NAISYS commands.
* Each service exports its command metadata and handler function.
*/
import { helpCmd } from "./commandDefs.js";
export var NextCommandAction;
(function (NextCommandAction) {
NextCommandAction[NextCommandAction["Continue"] = 0] = "Continue";
NextCommandAction[NextCommandAction["CompactSession"] = 1] = "CompactSession";
NextCommandAction[NextCommandAction["ExitApplication"] = 2] = "ExitApplication";
NextCommandAction[NextCommandAction["SessionComplete"] = 3] = "SessionComplete";
})(NextCommandAction || (NextCommandAction = {}));
export function noWait() {
return { kind: "none" };
}
export function timedWait(seconds) {
return seconds > 0 ? { kind: "timed", seconds } : noWait();
}
export function indefiniteWait() {
return { kind: "indefinite" };
}
export function isTimedWait(wait) {
return wait?.kind === "timed";
}
/**
* Creates a command registry from an array of registrable commands.
* The registry provides O(1) lookup by command name.
*/
export function createCommandRegistry(inputMode, commands) {
const registry = new Map();
// Add built-in ns-help command
const helpCommand = {
command: helpCmd,
handleCommand: () => {
const allCommands = Array.from(new Set(registry.values())).sort((a, b) => a.command.name.localeCompare(b.command.name));
const mainCommands = allCommands.filter((c) => !c.command.isDebug);
const debugCommands = allCommands.filter((c) => c.command.isDebug);
const formatTable = (cmds) => {
const rows = [
...cmds.map((c) => {
const aliases = c.command.aliases?.length
? ` (${c.command.aliases.join(", ")})`
: "";
return [c.command.name + aliases, c.command.description || ""];
}),
];
const colWidths = rows[0].map((_, i) => Math.max(...rows.map((r) => r[i].length)));
return rows
.map((row) => row.map((cell, i) => cell.padEnd(colWidths[i])).join(" "))
.join("\n");
};
let output = "Commands:\n" + formatTable(mainCommands);
if (inputMode.isDebug() && debugCommands.length > 0) {
output +=
"\n\nDebug commands: (Not visible to LLM)\n" +
formatTable(debugCommands);
}
return output;
},
};
registry.set(helpCommand.command.name, helpCommand);
for (const command of commands) {
if (registry.has(command.command.name)) {
throw new Error(`Duplicate command registration: ${command.command.name}`);
}
registry.set(command.command.name, command);
for (const alias of command.command.aliases ?? []) {
if (registry.has(alias)) {
throw new Error(`Duplicate command registration: ${alias}`);
}
registry.set(alias, command);
}
}
function get(commandName) {
return registry.get(commandName);
}
function has(commandName) {
return registry.has(commandName);
}
return {
get,
has,
};
}
//# sourceMappingURL=commandRegistry.js.map