naisys
Version:
NAISYS - Autonomous AI agent runner with built-in context management and cost tracking
50 lines • 1.97 kB
JavaScript
import chalk from "chalk";
import { getSharedReadline } from "./sharedReadline.js";
/**
* Prompt the operator for y/n confirmation.
* When the console is not available, resolves to defaultAccept immediately.
*/
export function getConfirmation(output, prompt, options) {
const defaultAccept = options?.defaultAccept ?? false;
const nonInteractiveAccept = options?.nonInteractiveAccept ?? defaultAccept;
const timeoutSeconds = options?.timeoutSeconds;
return new Promise((resolve) => {
const rl = output.isConsoleEnabled() ? getSharedReadline() : null;
if (!rl) {
const reason = !output.isConsoleEnabled()
? "console disabled"
: "no interactive terminal";
output.comment(prompt +
(nonInteractiveAccept ? "<auto-approved>" : `<denied: ${reason}>`));
resolve(nonInteractiveAccept);
return;
}
const controller = new AbortController();
let timeout;
if (timeoutSeconds && timeoutSeconds > 0) {
timeout = setTimeout(() => {
controller.abort();
try {
rl.pause();
}
catch {
// readline may already be closed after abort
}
resolve(defaultAccept);
}, timeoutSeconds * 1000);
}
const timeoutHint = timeoutSeconds && timeoutSeconds > 0 ? ` (${timeoutSeconds}s)` : "";
rl.question(chalk.greenBright(`${prompt}${timeoutHint} `), { signal: controller.signal }, (answer) => {
clearTimeout(timeout);
rl.pause();
const trimmed = answer.trim().toLowerCase();
if (trimmed === "") {
resolve(defaultAccept);
}
else {
resolve(trimmed === "y" || trimmed === "yes");
}
});
});
}
//# sourceMappingURL=confirmation.js.map