naisys
Version:
NAISYS - Autonomous AI agent runner with built-in context management and cost tracking
60 lines • 2.9 kB
JavaScript
import { getPlatformConfig } from "../services/runtime/shellPlatform.js";
export function createShellCommand(shellWrapper, inputMode, pagedOutputBuffer) {
const platformConfig = getPlatformConfig();
const isShellSuspended = () => shellWrapper.isShellSuspended();
const isSecureContinuation = () => shellWrapper.isSecureContinuation();
const getCommandElapsedTimeString = () => shellWrapper.getCommandElapsedTimeString();
const getCurrentCommandName = () => shellWrapper.getCurrentCommandName();
async function handleCommand(input) {
const cmdParams = input.split(" ");
let response;
let label;
if (!isShellSuspended()) {
if (["nano", "vi", "vim"].includes(cmdParams[0])) {
// Route user to context friendly edit commands that can read/write the entire file in one go
// Having EOF in quotes is important as it prevents the shell from replacing $variables with bash values
throw `${cmdParams[0]} not supported. Use \`cat\` to read a file and \`cat > filename << 'EOF'\` to write a file`;
}
if (cmdParams[0] == "lynx" && cmdParams[1] != "--dump") {
throw `Interactive mode with lynx is not supported. Use --dump with lynx to view a website`;
}
if (cmdParams[0] == "exit") {
if (inputMode.isLLM()) {
throw "Use 'ns-session compact/complete' to finish the session";
}
// Only the debug user is allowed to exit the shell
else if (inputMode.isDebug()) {
await shellWrapper.terminate();
return { exitApp: true };
}
}
label = input.trim();
response = await shellWrapper.executeCommand(input);
}
// Else shell is suspended, continue
else {
// Secure continuations carry a secret (password / API key) — label by
// the running command so neither the source line nor the ns-more
// prefix exposes it. Non-secure continuations use the typed input.
label = isSecureContinuation() ? getCurrentCommandName() : input.trim();
response = await shellWrapper.continueCommand({
kind: "input",
text: input,
});
}
response = pagedOutputBuffer.setContent(label, response);
if (response.endsWith(": command not found") ||
response.includes("is not recognized")) {
response += "\n" + platformConfig.invalidCommandMessage;
}
return { response, exitApp: false };
}
return {
isShellSuspended,
isSecureContinuation,
getCommandElapsedTimeString,
getCurrentCommandName,
handleCommand,
};
}
//# sourceMappingURL=shellCommand.js.map