-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstorage.ts
More file actions
34 lines (32 loc) · 933 Bytes
/
storage.ts
File metadata and controls
34 lines (32 loc) · 933 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { type Chat } from "./types.ts"
const HISTORY_KEY = "llm-cli"
const HISTORY_LENGTH = 30
export const History = {
read(): Chat[] {
const contents = localStorage.getItem(HISTORY_KEY)
if (!contents) return []
const chats: Chat[] = JSON.parse(contents, (key, value) => {
if (
(key === "createdAt" || key === "startedAt") &&
typeof value === "string"
) {
return new Date(value)
}
return value
})
// Backfill id for chats stored before we started writing it. Persisted on
// the next History.write().
for (const chat of chats) {
if (!chat.id) chat.id = crypto.randomUUID()
}
return chats
},
write(history: Chat[]) {
// keep only the most recent N
const truncated = history.slice(-HISTORY_LENGTH)
localStorage.setItem(HISTORY_KEY, JSON.stringify(truncated))
},
clear() {
localStorage.removeItem(HISTORY_KEY)
},
}