@bestdefense/bd-agent
Version:
An AI-powered coding assistant CLI that connects to AWS Bedrock
196 lines • 8.33 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.ReadlineWithHistory = void 0;
const readline = __importStar(require("readline"));
class ReadlineWithHistory {
rl;
historyManager;
currentLine = '';
cursorPosition = 0;
constructor(historyManager) {
this.historyManager = historyManager;
// Create readline interface with custom key handling
this.rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: true,
historySize: 0 // We manage history ourselves
});
// Enable raw mode for better key handling
if (process.stdin.isTTY) {
process.stdin.setRawMode(true);
}
}
async question(prompt) {
return new Promise((resolve) => {
process.stdout.write(prompt);
this.currentLine = '';
this.cursorPosition = 0;
const onData = (key) => {
const char = key.toString();
// Handle special keys
if (key[0] === 3) { // Ctrl+C
process.stdout.write('\n');
process.exit(0);
}
else if (key[0] === 4) { // Ctrl+D
if (this.currentLine.length === 0) {
process.stdout.write('\n');
resolve('exit');
cleanup();
return;
}
}
else if (key[0] === 13) { // Enter
process.stdout.write('\n');
const result = this.currentLine;
this.historyManager.addToHistory(result);
this.historyManager.resetPosition();
cleanup();
resolve(result);
return;
}
else if (key[0] === 127 || key[0] === 8) { // Backspace
if (this.cursorPosition > 0) {
this.currentLine =
this.currentLine.slice(0, this.cursorPosition - 1) +
this.currentLine.slice(this.cursorPosition);
this.cursorPosition--;
this.redrawLine(prompt);
}
}
else if (key[0] === 27) { // Escape sequence
if (key[1] === 91) { // Arrow keys
if (key[2] === 65) { // Up arrow
const previousCommand = this.historyManager.getPreviousCommand(this.currentLine);
if (previousCommand !== null) {
this.currentLine = previousCommand;
this.cursorPosition = this.currentLine.length;
this.redrawLine(prompt);
}
}
else if (key[2] === 66) { // Down arrow
const nextCommand = this.historyManager.getNextCommand();
if (nextCommand !== null) {
this.currentLine = nextCommand;
this.cursorPosition = this.currentLine.length;
this.redrawLine(prompt);
}
}
else if (key[2] === 67) { // Right arrow
if (this.cursorPosition < this.currentLine.length) {
this.cursorPosition++;
this.redrawLine(prompt);
}
}
else if (key[2] === 68) { // Left arrow
if (this.cursorPosition > 0) {
this.cursorPosition--;
this.redrawLine(prompt);
}
}
else if (key[2] === 72) { // Home
this.cursorPosition = 0;
this.redrawLine(prompt);
}
else if (key[2] === 70) { // End
this.cursorPosition = this.currentLine.length;
this.redrawLine(prompt);
}
}
}
else if (key[0] === 1) { // Ctrl+A (Home)
this.cursorPosition = 0;
this.redrawLine(prompt);
}
else if (key[0] === 5) { // Ctrl+E (End)
this.cursorPosition = this.currentLine.length;
this.redrawLine(prompt);
}
else if (key[0] === 11) { // Ctrl+K (Kill to end of line)
this.currentLine = this.currentLine.slice(0, this.cursorPosition);
this.redrawLine(prompt);
}
else if (key[0] === 21) { // Ctrl+U (Kill to beginning of line)
this.currentLine = this.currentLine.slice(this.cursorPosition);
this.cursorPosition = 0;
this.redrawLine(prompt);
}
else if (key[0] >= 32 && key[0] < 127) { // Printable characters
this.currentLine =
this.currentLine.slice(0, this.cursorPosition) +
char +
this.currentLine.slice(this.cursorPosition);
this.cursorPosition++;
this.redrawLine(prompt);
}
};
const cleanup = () => {
process.stdin.removeListener('data', onData);
if (process.stdin.isTTY) {
process.stdin.setRawMode(false);
}
};
process.stdin.on('data', onData);
});
}
redrawLine(prompt) {
// Clear current line
readline.clearLine(process.stdout, 0);
readline.cursorTo(process.stdout, 0);
// Write prompt and current line
process.stdout.write(prompt + this.currentLine);
// Position cursor
readline.cursorTo(process.stdout, prompt.length + this.cursorPosition);
}
close() {
if (process.stdin.isTTY) {
process.stdin.setRawMode(false);
}
this.rl.close();
}
// Fallback method for non-TTY environments (like when piping)
async questionSimple(prompt) {
return new Promise((resolve) => {
this.rl.question(prompt, (answer) => {
this.historyManager.addToHistory(answer);
resolve(answer);
});
});
}
}
exports.ReadlineWithHistory = ReadlineWithHistory;
//# sourceMappingURL=readline-with-history.js.map