@botpress/adk-cli
Version:
Command-line interface for the Botpress Agent Development Kit (ADK)
68 lines (66 loc) • 1.67 kB
JavaScript
// @bun
// src/utils/prompt.ts
import readline from "readline";
var ESC = "\x1B";
var CTRL_C = "\x03";
async function promptApproval(question, output = process.stderr) {
output.write(question);
const input = process.stdin;
if (!input.isTTY) {
output.write(`
`);
return "no";
}
return new Promise((resolve) => {
const finish = (choice, echo) => {
input.setRawMode(false);
input.pause();
input.removeListener("data", onData);
output.write(`${echo}
`);
resolve(choice);
};
const onData = (key) => {
switch (key) {
case "y":
case "Y":
return finish("yes", "y");
case "n":
case "N":
case "\r":
case `
`:
return finish("no", "n");
case ESC:
return finish("cancel", "esc");
case CTRL_C:
return finish("cancel", "^C");
default:
return;
}
};
input.setRawMode(true);
input.resume();
input.setEncoding("utf8");
input.on("data", onData);
});
}
async function promptYesNo(question, output = process.stderr, options) {
const rl = readline.createInterface({ input: process.stdin, output });
return new Promise((resolve) => {
rl.on("SIGINT", () => {
rl.close();
resolve(false);
});
rl.question(question, (answer) => {
rl.close();
const normalized = answer.trim().toLowerCase();
if (options?.defaultYes) {
resolve(normalized === "" || normalized === "y" || normalized === "yes");
} else {
resolve(normalized === "y" || normalized === "yes");
}
});
});
}
export { promptApproval, promptYesNo };