naisys
Version:
NAISYS - Autonomous AI agent runner with built-in context management and cost tracking
45 lines • 1.49 kB
JavaScript
/**
* Utility for listening to ESC key presses during async operations.
* Uses raw mode stdin to capture key events without requiring Enter.
*/
const ESC_KEY = "\x1b";
const CTRL_C_KEY = "\x03";
export function createEscKeyListener() {
function start(onEsc) {
// Store original raw mode state
const wasRaw = process.stdin.isRaw;
// Handler for keypress data
const onData = (data) => {
const key = data.toString();
if (key === ESC_KEY) {
onEsc();
}
else if (key === CTRL_C_KEY) {
// readline is paused during LLM queries, so raw mode must forward
// Ctrl+C back to the process-level shutdown handler.
process.emit("SIGINT", "SIGINT");
}
};
// Enable raw mode to get individual keystrokes
if (process.stdin.isTTY) {
process.stdin.setRawMode(true);
process.stdin.resume();
process.stdin.on("data", onData);
}
// Return cleanup function
return () => {
if (process.stdin.isTTY) {
process.stdin.off("data", onData);
// Restore original raw mode state
if (!wasRaw) {
process.stdin.setRawMode(false);
}
process.stdin.pause();
}
};
}
return {
start,
};
}
//# sourceMappingURL=escKeyListener.js.map