UNPKG

@bestdefense/bd-agent

Version:

An AI-powered coding assistant CLI that connects to AWS Bedrock

140 lines 4.96 kB
"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.HistoryManager = void 0; const fs = __importStar(require("fs")); const path = __importStar(require("path")); const os = __importStar(require("os")); class HistoryManager { historyFile; history = []; currentIndex = -1; maxHistorySize = 1000; tempInput = ''; constructor() { // Store history in the same directory as config const configDir = path.join(os.homedir(), '.bd-agent'); if (!fs.existsSync(configDir)) { fs.mkdirSync(configDir, { recursive: true }); } this.historyFile = path.join(configDir, 'history.json'); this.loadHistory(); } loadHistory() { try { if (fs.existsSync(this.historyFile)) { const data = fs.readFileSync(this.historyFile, 'utf-8'); const parsed = JSON.parse(data); this.history = Array.isArray(parsed) ? parsed : []; // Start at the end of history this.currentIndex = this.history.length; } } catch (error) { console.error('Failed to load history:', error); this.history = []; this.currentIndex = 0; } } saveHistory() { try { // Keep only the most recent entries if (this.history.length > this.maxHistorySize) { this.history = this.history.slice(-this.maxHistorySize); } fs.writeFileSync(this.historyFile, JSON.stringify(this.history, null, 2)); } catch (error) { console.error('Failed to save history:', error); } } addToHistory(command) { // Don't add empty commands or duplicates of the last command if (!command.trim() || (this.history.length > 0 && this.history[this.history.length - 1] === command)) { return; } // Don't add exit/quit commands to history if (['exit', 'quit'].includes(command.toLowerCase().trim())) { return; } this.history.push(command); this.currentIndex = this.history.length; this.tempInput = ''; this.saveHistory(); } getPreviousCommand(currentInput) { // Save current input if we're at the end of history if (this.currentIndex === this.history.length && currentInput) { this.tempInput = currentInput; } if (this.currentIndex > 0) { this.currentIndex--; return this.history[this.currentIndex]; } return null; } getNextCommand() { if (this.currentIndex < this.history.length - 1) { this.currentIndex++; return this.history[this.currentIndex]; } else if (this.currentIndex === this.history.length - 1) { this.currentIndex = this.history.length; return this.tempInput; } return null; } resetPosition() { this.currentIndex = this.history.length; this.tempInput = ''; } getHistory(limit) { if (limit) { return this.history.slice(-limit); } return [...this.history]; } clearHistory() { this.history = []; this.currentIndex = 0; this.tempInput = ''; this.saveHistory(); } searchHistory(query) { return this.history.filter(cmd => cmd.toLowerCase().includes(query.toLowerCase())); } } exports.HistoryManager = HistoryManager; //# sourceMappingURL=history-manager.js.map