| 1 | import fs from "fs"; |
| 2 | import path from "path"; |
| 3 | |
| 4 | import { env } from "../env.js"; |
| 5 | |
| 6 | const HISTORY_FILE = path.join(env.continueHome, "input_history.json"); |
| 7 | const MAX_HISTORY_SIZE = 1000; |
| 8 | |
| 9 | export interface InputHistoryEntry { |
| 10 | text: string; |
| 11 | timestamp: number; |
| 12 | } |
| 13 | |
| 14 | export class InputHistory { |
| 15 | private history: InputHistoryEntry[] = []; |
| 16 | private currentIndex: number = -1; |
| 17 | private originalInput: string = ""; |
| 18 | |
| 19 | constructor() { |
| 20 | this.loadHistory(); |
| 21 | } |
| 22 | |
| 23 | private loadHistory(): void { |
| 24 | try { |
| 25 | const dir = path.dirname(HISTORY_FILE); |
| 26 | if (!fs.existsSync(dir)) { |
| 27 | fs.mkdirSync(dir, { recursive: true }); |
| 28 | } |
| 29 | |
| 30 | if (fs.existsSync(HISTORY_FILE)) { |
| 31 | const data = fs.readFileSync(HISTORY_FILE, "utf8"); |
| 32 | this.history = JSON.parse(data); |
| 33 | } |
| 34 | } catch (error) { |
| 35 | console.error("Failed to load input history:", error); |
| 36 | this.history = []; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | private saveHistory(): void { |
| 41 | try { |
| 42 | const dir = path.dirname(HISTORY_FILE); |
| 43 | if (!fs.existsSync(dir)) { |
| 44 | fs.mkdirSync(dir, { recursive: true }); |
| 45 | } |
| 46 | |
| 47 | fs.writeFileSync(HISTORY_FILE, JSON.stringify(this.history, null, 2)); |
| 48 | } catch (error) { |
| 49 | console.error("Failed to save input history:", error); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | addEntry(text: string): void { |
| 54 | if (!text.trim()) return; |
| 55 | |
| 56 | // Remove duplicate if it exists |
| 57 | this.history = this.history.filter((entry) => entry.text !== text); |
| 58 | |
| 59 | // Add new entry at the beginning |
| 60 | this.history.unshift({ |
| 61 | text, |
| 62 | timestamp: Date.now(), |
| 63 | }); |
| 64 | |
| 65 | // Limit history size |
| 66 | if (this.history.length > MAX_HISTORY_SIZE) { |
| 67 | this.history = this.history.slice(0, MAX_HISTORY_SIZE); |
| 68 | } |
| 69 | |
| 70 | this.saveHistory(); |
| 71 | this.resetNavigation(); |
| 72 | } |
| 73 | |
| 74 | navigateUp(currentInput: string): string | null { |
| 75 | if (this.history.length === 0) return null; |
| 76 | |
| 77 | // Store original input on first navigation |
| 78 | if (this.currentIndex === -1) { |
| 79 | this.originalInput = currentInput; |
| 80 | this.currentIndex = 0; |
| 81 | } else if (this.currentIndex < this.history.length - 1) { |
| 82 | this.currentIndex++; |
| 83 | } |
| 84 | |
| 85 | return this.history[this.currentIndex]?.text || null; |
| 86 | } |
| 87 | |
| 88 | navigateDown(): string | null { |
| 89 | if (this.currentIndex === -1) return null; |
| 90 | |
| 91 | if (this.currentIndex > 0) { |
| 92 | this.currentIndex--; |
| 93 | return this.history[this.currentIndex]?.text || null; |
| 94 | } else { |
| 95 | // Return to original input |
| 96 | const originalInput = this.originalInput; |
| 97 | this.resetNavigation(); |
| 98 | return originalInput; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | resetNavigation(): void { |
| 103 | this.currentIndex = -1; |
| 104 | this.originalInput = ""; |
| 105 | } |
| 106 | |
| 107 | getHistory(): InputHistoryEntry[] { |
| 108 | return [...this.history]; |
| 109 | } |
| 110 | |
| 111 | clearHistory(): void { |
| 112 | this.history = []; |
| 113 | this.resetNavigation(); |
| 114 | this.saveHistory(); |
| 115 | } |
| 116 | } |