|
| 1 | +const STORAGE_KEY = "acpSessionHistory"; |
| 2 | +const MAX_ENTRIES = 30; |
| 3 | + |
| 4 | +export interface ACPHistoryEntry { |
| 5 | + sessionId: string; |
| 6 | + url: string; |
| 7 | + cwd: string; |
| 8 | + agentName: string; |
| 9 | + title: string; |
| 10 | + preview: string; |
| 11 | + createdAt: string; |
| 12 | + updatedAt: string; |
| 13 | +} |
| 14 | + |
| 15 | +type ACPHistoryFilter = { |
| 16 | + url?: string; |
| 17 | +}; |
| 18 | + |
| 19 | +type ACPHistoryRemoveParams = { |
| 20 | + sessionId: string; |
| 21 | + url: string; |
| 22 | +}; |
| 23 | + |
| 24 | +type ACPHistorySaveInput = Partial<ACPHistoryEntry> & |
| 25 | + Pick<ACPHistoryEntry, "sessionId" | "url">; |
| 26 | + |
| 27 | +function normalizeEntry(entry: Partial<ACPHistoryEntry> = {}): ACPHistoryEntry { |
| 28 | + const createdAt = entry.createdAt || new Date().toISOString(); |
| 29 | + const updatedAt = entry.updatedAt || createdAt; |
| 30 | + |
| 31 | + return { |
| 32 | + sessionId: |
| 33 | + typeof entry.sessionId === "string" ? entry.sessionId.trim() : "", |
| 34 | + url: typeof entry.url === "string" ? entry.url.trim() : "", |
| 35 | + cwd: typeof entry.cwd === "string" ? entry.cwd.trim() : "", |
| 36 | + agentName: |
| 37 | + typeof entry.agentName === "string" ? entry.agentName.trim() : "", |
| 38 | + title: typeof entry.title === "string" ? entry.title.trim() : "", |
| 39 | + preview: typeof entry.preview === "string" ? entry.preview.trim() : "", |
| 40 | + createdAt, |
| 41 | + updatedAt, |
| 42 | + }; |
| 43 | +} |
| 44 | + |
| 45 | +function getTimestamp(value: string): number { |
| 46 | + const parsed = Date.parse(value); |
| 47 | + return Number.isNaN(parsed) ? 0 : parsed; |
| 48 | +} |
| 49 | + |
| 50 | +function sortEntries(entries: ACPHistoryEntry[]): ACPHistoryEntry[] { |
| 51 | + return [...entries].sort((a, b) => { |
| 52 | + return getTimestamp(b.updatedAt) - getTimestamp(a.updatedAt); |
| 53 | + }); |
| 54 | +} |
| 55 | + |
| 56 | +function parseEntries(): ACPHistoryEntry[] { |
| 57 | + let entries = null; |
| 58 | + try { |
| 59 | + entries = JSON.parse(localStorage.getItem(STORAGE_KEY) || "[]"); |
| 60 | + } catch { |
| 61 | + entries = []; |
| 62 | + } |
| 63 | + if (!Array.isArray(entries)) return []; |
| 64 | + |
| 65 | + return entries |
| 66 | + .map((entry) => normalizeEntry(entry)) |
| 67 | + .filter((entry) => entry.sessionId && entry.url); |
| 68 | +} |
| 69 | + |
| 70 | +function persist(entries: ACPHistoryEntry[]): void { |
| 71 | + const nextEntries = sortEntries(entries).slice(0, MAX_ENTRIES); |
| 72 | + localStorage.setItem(STORAGE_KEY, JSON.stringify(nextEntries)); |
| 73 | +} |
| 74 | + |
| 75 | +const acpHistory = { |
| 76 | + list(filter: ACPHistoryFilter = {}): ACPHistoryEntry[] { |
| 77 | + return sortEntries(parseEntries()).filter((entry) => { |
| 78 | + if (!filter.url) return true; |
| 79 | + return entry.url === filter.url; |
| 80 | + }); |
| 81 | + }, |
| 82 | + |
| 83 | + save(entry: ACPHistorySaveInput): ACPHistoryEntry | null { |
| 84 | + const normalized = normalizeEntry(entry); |
| 85 | + if (!normalized.sessionId || !normalized.url) return null; |
| 86 | + |
| 87 | + const entries = parseEntries(); |
| 88 | + const index = entries.findIndex((item) => { |
| 89 | + return ( |
| 90 | + item.sessionId === normalized.sessionId && item.url === normalized.url |
| 91 | + ); |
| 92 | + }); |
| 93 | + |
| 94 | + if (index >= 0) { |
| 95 | + const current = entries[index]; |
| 96 | + entries[index] = { |
| 97 | + ...current, |
| 98 | + ...normalized, |
| 99 | + cwd: normalized.cwd || current.cwd, |
| 100 | + agentName: normalized.agentName || current.agentName, |
| 101 | + title: normalized.title || current.title, |
| 102 | + preview: normalized.preview || current.preview, |
| 103 | + createdAt: current.createdAt, |
| 104 | + updatedAt: normalized.updatedAt || new Date().toISOString(), |
| 105 | + }; |
| 106 | + } else { |
| 107 | + entries.unshift({ |
| 108 | + ...normalized, |
| 109 | + createdAt: normalized.createdAt || new Date().toISOString(), |
| 110 | + updatedAt: normalized.updatedAt || new Date().toISOString(), |
| 111 | + }); |
| 112 | + } |
| 113 | + |
| 114 | + persist(entries); |
| 115 | + return normalized; |
| 116 | + }, |
| 117 | + |
| 118 | + remove({ sessionId, url }: ACPHistoryRemoveParams): void { |
| 119 | + if (!sessionId || !url) return; |
| 120 | + |
| 121 | + persist( |
| 122 | + parseEntries().filter((entry) => { |
| 123 | + return !(entry.sessionId === sessionId && entry.url === url); |
| 124 | + }), |
| 125 | + ); |
| 126 | + }, |
| 127 | +}; |
| 128 | + |
| 129 | +export default acpHistory; |
0 commit comments