UNPKG

naisys

Version:

NAISYS - Autonomous AI agent runner with built-in context management and cost tracking

112 lines 4.61 kB
import { ADMIN_USERNAME } from "@naisys/common"; import { cmdCmd, contextCmd, exitCmd, pauseCmd, talkCmd, toolCmd, } from "./commandDefs.js"; import { NextCommandAction } from "./commandRegistry.js"; export function createDebugCommands(contextManager, output, inputMode, systemMessage, agentManager, localUserId) { function roleToString(role) { switch (role) { case "assistant": return "LLM/Assistant"; case "user": return "NAISYS/User"; case "system": return "NAISYS/System"; default: return "Unknown"; } } function printContext() { let content = `------ System ------`; content += `\n${systemMessage}`; contextManager.getCombinedMessages().forEach((message) => { content += `\n\n------ ${roleToString(message.role)} ------`; if (message.cachePoint) { content += `\n[-- CACHE POINT --]`; } content += `\n${message.content}`; }); return content; } const nsContext = { command: contextCmd, handleCommand: () => { output.comment("#####################"); output.comment(printContext()); output.comment("#####################"); return Promise.resolve(""); }, }; let firstTalkMessage = true; const nsTalk = { command: talkCmd, handleCommand: (cmdArgs) => { if (inputMode.isLLM()) { return { content: "Message sent!" }; } else if (inputMode.isDebug()) { inputMode.setLLM(); // Dont say specifically mail/chat was used for admin message so agent can choose from available reply methods (mail/chat/comment) contextManager.append(`Message from ${ADMIN_USERNAME}: ${cmdArgs}`); if (firstTalkMessage) { contextManager.append(`Reply with: ns-chat send ${ADMIN_USERNAME} "<message>"`); firstTalkMessage = false; } inputMode.setDebug(); return { content: "", nextCommandResponse: { nextCommandAction: NextCommandAction.Continue, triggerLlm: true, }, }; } return { content: "" }; }, }; const nsPause = { command: pauseCmd, handleCommand: (cmdArgs) => { const agent = agentManager.runningAgents.find((a) => a.agentUserId === localUserId); if (!agent) { return "Agent not running"; } const arg = cmdArgs.trim().toLowerCase(); const next = arg === "on" ? true : arg === "off" ? false : !agent.isPaused(); const changed = agent.setPaused(next); return changed ? `Session ${next ? "paused" : "resumed"}` : `Session already ${next ? "paused" : "resumed"}`; }, }; // Stub handler — the ns-cmd / ! prefix is intercepted in commandHandler so // the rest of the line runs in LLM mode. This handler only fires when the // user enters the bare command with no args. const nsCmd = { command: cmdCmd, handleCommand: () => { return "Usage: ns-cmd <command> (or !<command>) — runs the command as if the LLM had typed it."; }, }; const nsTool = { command: toolCmd, handleCommand: () => "ns-tool is a virtual command — it appears in logs when the LLM uses a tool (e.g. computer use). It has no runtime behavior and is not invoked directly.", }; const nsExit = { command: exitCmd, handleCommand: async (cmdArgs) => { if (cmdArgs.trim() === "all") { const otherAgents = agentManager.runningAgents.filter((a) => a.agentUserId !== localUserId); output.comment(`Stopping agents: ${otherAgents.map((a) => a.agentUsername).join(", ")}...`); await agentManager.stopAll("exit all", localUserId); output.comment(`Stopped ${otherAgents.length} agent(s)`); } return { content: "", nextCommandResponse: { nextCommandAction: NextCommandAction.ExitApplication, }, }; }, }; return [nsContext, nsTalk, nsPause, nsCmd, nsTool, nsExit]; } //# sourceMappingURL=debugCommand.js.map