-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.js
More file actions
260 lines (218 loc) · 8.48 KB
/
storage.js
File metadata and controls
260 lines (218 loc) · 8.48 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/**
* LinkedIn AI Assistant — IndexedDB Storage Module
* Stores structured context data locally for fast retrieval and better AI suggestions.
*
* Stores: contacts, conversations, interactions (comments/replies), profiles
* Avoids: UI elements, duplicated messages, irrelevant page data
*/
const Storage = (() => {
"use strict";
const DB_NAME = "lai_assistant";
const DB_VERSION = 1;
let _db = null;
// ── Open / Initialize DB ─────────────────────────────
function open() {
if (_db) return Promise.resolve(_db);
return new Promise((resolve, reject) => {
const req = indexedDB.open(DB_NAME, DB_VERSION);
req.onupgradeneeded = (e) => {
const db = e.target.result;
// Contacts — people we've interacted with
if (!db.objectStoreNames.contains("contacts")) {
const s = db.createObjectStore("contacts", { keyPath: "id" });
s.createIndex("name", "name", { unique: false });
s.createIndex("updatedAt", "updatedAt", { unique: false });
}
// Conversations — DM thread snapshots
if (!db.objectStoreNames.contains("conversations")) {
const s = db.createObjectStore("conversations", { keyPath: "id" });
s.createIndex("contactId", "contactId", { unique: false });
s.createIndex("updatedAt", "updatedAt", { unique: false });
}
// Interactions — comment / reply contexts
if (!db.objectStoreNames.contains("interactions")) {
const s = db.createObjectStore("interactions", { keyPath: "id" });
s.createIndex("type", "type", { unique: false });
s.createIndex("createdAt", "createdAt", { unique: false });
}
};
req.onsuccess = (e) => {
_db = e.target.result;
resolve(_db);
};
req.onerror = (e) => {
console.warn("[LAI Storage] IndexedDB open failed:", e.target.error);
reject(e.target.error);
};
});
}
// ── Generic helpers ───────────────────────────────────
async function _tx(storeName, mode) {
const db = await open();
return db.transaction(storeName, mode).objectStore(storeName);
}
function _req(idbRequest) {
return new Promise((resolve, reject) => {
idbRequest.onsuccess = () => resolve(idbRequest.result);
idbRequest.onerror = () => reject(idbRequest.error);
});
}
// ── Contacts ─────────────────────────────────────────
/**
* Save or update a contact profile.
* @param {{ name, headline, company, role, about, profileUrl }} data
*/
async function saveContact(data) {
if (!data || !data.name) return;
const id = _contactId(data.name);
const store = await _tx("contacts", "readwrite");
const existing = await _req(store.get(id));
const record = {
id,
name: data.name,
headline: data.headline || "",
company: data.company || "",
role: data.role || "",
about: (data.about || "").slice(0, 500),
profileUrl: data.profileUrl || "",
updatedAt: Date.now(),
...(existing ? { createdAt: existing.createdAt } : { createdAt: Date.now() }),
};
const writeStore = await _tx("contacts", "readwrite");
await _req(writeStore.put(record));
return record;
}
async function getContact(name) {
if (!name) return null;
const store = await _tx("contacts", "readonly");
return _req(store.get(_contactId(name)));
}
function _contactId(name) {
return "c_" + (name || "").toLowerCase().replace(/[^a-z0-9]/g, "_").slice(0, 60);
}
// ── Conversations ────────────────────────────────────
/**
* Save a DM conversation snapshot.
* @param {{ contactName, messages: [{sender, text, timestamp?}], lastSenderIsMe }} data
*/
async function saveConversation(data) {
if (!data || !data.contactName) return;
const id = "conv_" + _contactId(data.contactName);
// Keep last 50 messages for full conversation context across sessions
const messages = (data.messages || []).slice(-50).map((m) => ({
sender: m.sender || "Unknown",
text: (m.text || "").slice(0, 1000),
timestamp: m.timestamp || null,
}));
const store = await _tx("conversations", "readwrite");
const existing = await _req(store.get(id));
const record = {
id,
contactId: _contactId(data.contactName),
contactName: data.contactName,
messages,
lastSenderIsMe: !!data.lastSenderIsMe,
messageCount: messages.length,
updatedAt: Date.now(),
...(existing ? { createdAt: existing.createdAt } : { createdAt: Date.now() }),
};
const writeStore = await _tx("conversations", "readwrite");
await _req(writeStore.put(record));
return record;
}
async function getConversation(contactName) {
if (!contactName) return null;
const id = "conv_" + _contactId(contactName);
const store = await _tx("conversations", "readonly");
return _req(store.get(id));
}
// ── Interactions (comments / replies) ────────────────
/**
* Save a comment/reply interaction context.
* @param {{ type: "comment"|"reply"|"child-reply", postText, postAuthor, commentText, commentAuthor, parentCommentText?, parentCommentAuthor? }} data
*/
async function saveInteraction(data) {
if (!data || !data.type) return;
const id = "int_" + Date.now() + "_" + Math.random().toString(36).slice(2, 7);
const record = {
id,
type: data.type,
postText: (data.postText || "").slice(0, 1000),
postAuthor: data.postAuthor || "",
commentText: (data.commentText || "").slice(0, 500),
commentAuthor: data.commentAuthor || "",
parentCommentText: (data.parentCommentText || "").slice(0, 500),
parentCommentAuthor: data.parentCommentAuthor || "",
createdAt: Date.now(),
};
const store = await _tx("interactions", "readwrite");
await _req(store.put(record));
// Prune old interactions — keep last 100
await _pruneInteractions();
return record;
}
async function getRecentInteractions(type, limit) {
const store = await _tx("interactions", "readonly");
const index = store.index("createdAt");
const results = [];
const count = limit || 10;
return new Promise((resolve, reject) => {
const req = index.openCursor(null, "prev");
req.onsuccess = (e) => {
const cursor = e.target.result;
if (cursor && results.length < count) {
if (!type || cursor.value.type === type) {
results.push(cursor.value);
}
cursor.continue();
} else {
resolve(results);
}
};
req.onerror = () => reject(req.error);
});
}
async function _pruneInteractions() {
const store = await _tx("interactions", "readonly");
const index = store.index("createdAt");
const allKeys = await _req(index.getAllKeys());
if (allKeys.length > 100) {
const toDelete = allKeys.slice(0, allKeys.length - 100);
const writeStore = await _tx("interactions", "readwrite");
for (const key of toDelete) {
writeStore.delete(key);
}
}
}
// ── Utility ──────────────────────────────────────────
async function getStats() {
const contacts = await _tx("contacts", "readonly");
const convos = await _tx("conversations", "readonly");
const ints = await _tx("interactions", "readonly");
return {
contacts: await _req(contacts.count()),
conversations: await _req(convos.count()),
interactions: await _req(ints.count()),
};
}
async function clearAll() {
const db = await open();
const tx = db.transaction(["contacts", "conversations", "interactions"], "readwrite");
tx.objectStore("contacts").clear();
tx.objectStore("conversations").clear();
tx.objectStore("interactions").clear();
return new Promise((resolve) => { tx.oncomplete = resolve; });
}
// ── Public API ───────────────────────────────────────
return {
open,
saveContact,
getContact,
saveConversation,
getConversation,
saveInteraction,
getRecentInteractions,
getStats,
clearAll,
};
})();