|
| 1 | +// Pure reverse search over prompt history already loaded for one workspace. |
| 2 | +// It never reads storage or runs a model/process. Entries remain complete for |
| 3 | +// acceptance; only the examination and terminal display are bounded. |
| 4 | +import { HISTORY_CAP } from "../core/history_store.js"; |
| 5 | +import { sliceVisible, visibleWidth } from "./text.js"; |
| 6 | + |
| 7 | +export const HISTORY_SEARCH_BOUNDS = { |
| 8 | + entryBytes: 16 * 1024, |
| 9 | + totalBytes: 1024 * 1024, |
| 10 | + queryBytes: 256, |
| 11 | + displayChars: 512, |
| 12 | +} as const; |
| 13 | + |
| 14 | +export interface HistorySearchState { |
| 15 | + readonly entries: readonly string[]; |
| 16 | + readonly priorValue: string; |
| 17 | + readonly priorCursor: number; |
| 18 | + readonly query: string; |
| 19 | + readonly matches: readonly number[]; |
| 20 | + readonly selected: number; |
| 21 | + readonly skippedOversize: number; |
| 22 | + readonly skippedBudget: number; |
| 23 | + readonly disabled: boolean; |
| 24 | +} |
| 25 | + |
| 26 | +function search(state: HistorySearchState, query: string): HistorySearchState { |
| 27 | + const matches: number[] = []; |
| 28 | + let examined = 0; |
| 29 | + let skippedOversize = 0; |
| 30 | + let skippedBudget = 0; |
| 31 | + const countRemainder = (through: number): void => { |
| 32 | + for (let i = through; i >= 0; i--) { |
| 33 | + if (state.entries[i]!.length > HISTORY_SEARCH_BOUNDS.entryBytes) skippedOversize++; |
| 34 | + else skippedBudget++; |
| 35 | + } |
| 36 | + }; |
| 37 | + for (let index = state.entries.length - 1; index >= 0; index--) { |
| 38 | + const entry = state.entries[index]!; |
| 39 | + // Check the JS length first so a hostile giant entry is never traversed |
| 40 | + // just to calculate its byte size. Shorter Unicode entries get a byte cap. |
| 41 | + if (entry.length > HISTORY_SEARCH_BOUNDS.entryBytes) { skippedOversize++; continue; } |
| 42 | + if (examined + entry.length > HISTORY_SEARCH_BOUNDS.totalBytes) { |
| 43 | + countRemainder(index); |
| 44 | + break; |
| 45 | + } |
| 46 | + const bytes = Buffer.byteLength(entry, "utf8"); |
| 47 | + if (bytes > HISTORY_SEARCH_BOUNDS.entryBytes) { skippedOversize++; continue; } |
| 48 | + if (examined + bytes > HISTORY_SEARCH_BOUNDS.totalBytes) { |
| 49 | + countRemainder(index); |
| 50 | + break; |
| 51 | + } |
| 52 | + examined += bytes; |
| 53 | + if (entry.includes(query)) matches.push(index); |
| 54 | + } |
| 55 | + return { ...state, query, matches, selected: 0, skippedOversize, skippedBudget }; |
| 56 | +} |
| 57 | + |
| 58 | +/** Empty query selects the newest examinable entry, then Ctrl+R walks older. */ |
| 59 | +export function openHistorySearch(entries: readonly string[], priorValue: string, priorCursor: number, disabled = false): HistorySearchState { |
| 60 | + return search({ entries: disabled ? [] : entries.slice(-HISTORY_CAP), priorValue, priorCursor, |
| 61 | + query: "", matches: [], selected: 0, skippedOversize: 0, skippedBudget: 0, disabled }, ""); |
| 62 | +} |
| 63 | + |
| 64 | +export function typeHistoryQuery(state: HistorySearchState, input: string): HistorySearchState { |
| 65 | + let query = state.query; |
| 66 | + for (const ch of input) { |
| 67 | + if (Buffer.byteLength(query + ch, "utf8") > HISTORY_SEARCH_BOUNDS.queryBytes) break; |
| 68 | + query += ch; |
| 69 | + } |
| 70 | + return search(state, query); |
| 71 | +} |
| 72 | + |
| 73 | +export function backspaceHistoryQuery(state: HistorySearchState): HistorySearchState { |
| 74 | + return search(state, [...state.query].slice(0, -1).join("")); |
| 75 | +} |
| 76 | + |
| 77 | +/** Stops at the oldest match; it does not wrap invisibly. */ |
| 78 | +export function olderHistoryMatch(state: HistorySearchState): HistorySearchState { |
| 79 | + return { ...state, selected: Math.min(state.selected + 1, Math.max(0, state.matches.length - 1)) }; |
| 80 | +} |
| 81 | + |
| 82 | +export function selectedHistoryMatch(state: HistorySearchState): string | null { |
| 83 | + const index = state.matches[state.selected]; |
| 84 | + return index === undefined ? null : state.entries[index] ?? null; |
| 85 | +} |
| 86 | + |
| 87 | +function safeDisplay(value: string): string { |
| 88 | + return value.replace(/\r\n|\r|\n/g, "⏎").replace(/[\x00-\x1f\x7f]/g, "�"); |
| 89 | +} |
| 90 | + |
| 91 | +function matchPreview(value: string, width: number): string { |
| 92 | + const chars = [...safeDisplay(value)]; |
| 93 | + const capped = chars.slice(0, HISTORY_SEARCH_BOUNDS.displayChars).join(""); |
| 94 | + if (chars.length <= HISTORY_SEARCH_BOUNDS.displayChars && visibleWidth(capped) <= width) return capped; |
| 95 | + const marker = "… [clipped]"; |
| 96 | + return sliceVisible(capped, Math.max(0, width - visibleWidth(marker))) + marker; |
| 97 | +} |
| 98 | + |
| 99 | +/** At most two terminal rows, with an explicit clipped/skipped indication. */ |
| 100 | +export function renderHistorySearch(state: HistorySearchState, cols: number, rows: number): string[] { |
| 101 | + if (rows < 2) return []; |
| 102 | + const width = Math.max(1, cols - 1); |
| 103 | + const query = safeDisplay(state.query) || "(newest)"; |
| 104 | + const selected = selectedHistoryMatch(state); |
| 105 | + const status = state.disabled ? "history disabled" : selected === null ? "no match" : `${state.selected + 1}/${state.matches.length}`; |
| 106 | + const skipped = state.skippedOversize + state.skippedBudget; |
| 107 | + const note = skipped ? `skipped ${skipped} (${state.skippedOversize} oversized, ${state.skippedBudget} budget) · ` : ""; |
| 108 | + const header = ` Ctrl+R ${note}${query} · ${status}`; |
| 109 | + if (rows === 2) { |
| 110 | + const compact = `R ${sliceVisible(query, Math.max(1, Math.floor(width / 4)))} ${status}${skipped ? ` skip ${skipped}` : ""} · `; |
| 111 | + if (selected === null) return [sliceVisible(compact + "no match", width)]; |
| 112 | + return [sliceVisible(compact + matchPreview(selected, Math.max(0, width - visibleWidth(compact))), width)]; |
| 113 | + } |
| 114 | + if (selected === null) return [sliceVisible(header, width), sliceVisible(" No matching prompts · Esc restores draft", width)]; |
| 115 | + const shown = `> ${matchPreview(selected, Math.max(0, width - 2))}`; |
| 116 | + return [sliceVisible(header, width), sliceVisible(shown, width)]; |
| 117 | +} |
0 commit comments