-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
346 lines (307 loc) · 12.1 KB
/
Copy pathbackground.js
File metadata and controls
346 lines (307 loc) · 12.1 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// background.js - Service worker for Bookmark Semantic Search
// Uses transformers.js (local bundle) to run all-MiniLM-L6-v2 in the browser.
import { pipeline, env } from "./lib/transformers.min.js";
// ── Dev config ────────────────────────────────────────────────────────────────
// Set DEBUG = true to enable the debug panel in the popup.
const DEBUG = false;
// MV3 service workers don't support URL.createObjectURL or SharedArrayBuffer,
// so we must disable threading and SIMD-threaded WASM. Instead we point
// transformers.js at our locally bundled ort-wasm-simd.wasm.
env.allowLocalModels = false;
env.useBrowserCache = true;
// Disable all threading - service workers can't spawn Worker threads
env.backends.onnx.wasm.numThreads = 1;
// Point ONNX runtime at our locally bundled WASM files (no CDN, no Workers)
const WASM_BASE = chrome.runtime.getURL("lib/");
env.backends.onnx.wasm.wasmPaths = WASM_BASE;
// ── State ─────────────────────────────────────────────────────────────────────
let state = "idle"; // idle | loading | indexing | ready | error
let errorMessage = "";
let embedder = null;
let indexedBookmarks = [];
let progress = 0;
let bookmarkCount = 0;
// ── Debug log ─────────────────────────────────────────────────────────────────
const debugLog = [];
function dbg(level, ...args) {
const entry = {
ts: new Date().toISOString().slice(11, 23),
level, // 'info' | 'warn' | 'error'
msg: args
.map((a) => {
try {
return typeof a === "object" ? JSON.stringify(a) : String(a);
} catch {
return "[unserializable]";
}
})
.join(" "),
};
debugLog.push(entry);
if (debugLog.length > 200) debugLog.shift();
console[level === "error" ? "error" : level === "warn" ? "warn" : "log"](
`[Findmark ${entry.ts}]`,
...args,
);
}
// ── Cosine similarity ─────────────────────────────────────────────────────────
function cosineSimilarity(a, b) {
let dot = 0,
normA = 0,
normB = 0;
for (let i = 0; i < a.length; i++) {
dot += a[i] * b[i];
normA += a[i] * a[i];
normB += b[i] * b[i];
}
return dot / (Math.sqrt(normA) * Math.sqrt(normB));
}
// ── Flatten Chrome bookmark tree ──────────────────────────────────────────────
function flattenBookmarks(nodes, results = []) {
for (const node of nodes) {
if (node.url) {
results.push({
id: node.id,
title: node.title || "Untitled",
url: node.url,
});
}
if (node.children) flattenBookmarks(node.children, results);
}
return results;
}
// ── Storage helpers ───────────────────────────────────────────────────────────
// Quantize embeddings to int8 (4x smaller: 384 bytes vs 1536 per bookmark).
// 5000 bookmarks ≈ 1.9 MB quantized vs 7.5 MB raw float32.
const STORAGE_KEY = "bookmark_index_v2";
function quantizeEmbedding(f32) {
let max = 0;
for (let i = 0; i < f32.length; i++) {
const abs = Math.abs(f32[i]);
if (abs > max) max = abs;
}
const scale = max > 0 ? 127 / max : 1;
const i8 = new Int8Array(f32.length);
for (let i = 0; i < f32.length; i++) i8[i] = Math.round(f32[i] * scale);
return { i8: Array.from(i8), scale };
}
function dequantizeEmbedding({ i8, scale }) {
const f32 = new Float32Array(i8.length);
const inv = 1 / scale;
for (let i = 0; i < i8.length; i++) f32[i] = i8[i] * inv;
return f32;
}
async function saveIndex(bookmarks) {
const serializable = bookmarks.map((b) => {
const { i8, scale } = quantizeEmbedding(b.embedding);
return { id: b.id, title: b.title, url: b.url, i8, scale };
});
await chrome.storage.local.set({ [STORAGE_KEY]: serializable });
dbg("info", `Saved ${bookmarks.length} bookmarks to storage`);
}
async function loadIndex() {
const result = await chrome.storage.local.get(STORAGE_KEY);
const data = result[STORAGE_KEY];
if (!data) return null;
return data.map((b) => ({
id: b.id,
title: b.title,
url: b.url,
embedding: dequantizeEmbedding({ i8: b.i8, scale: b.scale }),
}));
}
async function clearIndex() {
await chrome.storage.local.remove(STORAGE_KEY);
dbg("info", "Cleared storage index");
}
// ── Embed text ────────────────────────────────────────────────────────────────
async function embed(text) {
const output = await embedder(text, { pooling: "mean", normalize: true });
return output.data;
}
// ── Main init flow ────────────────────────────────────────────────────────────
// Lazily loads the embedding pipeline — only called when we actually need to
// embed something (stale/missing cache, forced reindex, or first search call).
// Safe to call multiple times: no-ops if embedder is already loaded.
async function ensureEmbedder() {
if (embedder) return;
dbg("info", "Loading pipeline: Xenova/all-MiniLM-L6-v2");
dbg(
"info",
"env.allowLocalModels =",
env.allowLocalModels,
"| env.useBrowserCache =",
env.useBrowserCache,
);
embedder = await pipeline("feature-extraction", "Xenova/all-MiniLM-L6-v2", {
progress_callback: (p) => {
dbg(
"info",
`Model progress: status=${p.status} file=${p.file || ""} loaded=${p.loaded || 0} total=${p.total || 0}`,
);
if (
p.status === "downloading" ||
p.status === "loading" ||
p.status === "progress"
) {
progress = Math.min(
60,
Math.round(((p.loaded || 0) / (p.total || 1)) * 60),
);
}
if (p.status === "done") {
progress = 62;
dbg("info", "File done:", p.file);
}
if (p.status === "ready") {
progress = 65;
dbg("info", "Pipeline ready");
}
},
});
dbg("info", "Pipeline loaded successfully");
progress = 65;
}
async function initialize(forceReindex = false) {
if (state === "loading" || state === "indexing") {
dbg("info", "initialize() called but already in state:", state);
return;
}
try {
state = "loading";
progress = 5;
dbg("info", "Starting initialization. forceReindex =", forceReindex);
// Check the cache BEFORE loading the model — the fingerprint check is just
// storage reads + string comparisons and doesn't need the embedder at all.
// On a cache-hit cold start the popup reaches "ready" instantly, with the
// model only loaded lazily when the user actually fires a search.
if (!forceReindex) {
dbg("info", "Checking for cached index...");
const cached = await loadIndex();
if (cached && cached.length > 0) {
dbg("info", `Found cached index with ${cached.length} bookmarks`);
const tree = await chrome.bookmarks.getTree();
const current = flattenBookmarks(tree);
dbg("info", `Current bookmark count: ${current.length}`);
const fingerprint = (bms) =>
bms
.map((b) => `${b.id}:${b.title}:${b.url}`)
.sort()
.join("|");
if (fingerprint(current) === fingerprint(cached)) {
dbg("info", "Cache is fresh - skipping reindex, embedder deferred");
indexedBookmarks = cached;
bookmarkCount = cached.length;
state = "ready";
progress = 100;
return; // embedder stays null until first search()
} else {
dbg("info", "Cache is stale - reindexing");
}
} else {
dbg("info", "No cached index found - indexing fresh");
}
}
// Cache miss or forced reindex — now we actually need the model.
await ensureEmbedder();
await buildIndex();
} catch (err) {
state = "error";
errorMessage = err.message || "Failed to initialize.";
dbg("error", "Initialization failed:", err.message, err.stack || "");
}
}
async function buildIndex() {
state = "indexing";
dbg("info", "Building index...");
const tree = await chrome.bookmarks.getTree();
const bookmarks = flattenBookmarks(tree);
bookmarkCount = bookmarks.length;
dbg("info", `Found ${bookmarks.length} bookmarks to embed`);
const embedded = [];
const BATCH_SIZE = 8;
for (let i = 0; i < bookmarks.length; i += BATCH_SIZE) {
const batch = bookmarks.slice(i, i + BATCH_SIZE);
await Promise.all(
batch.map(async (bm) => {
const text = `${bm.title} ${bm.url}`.slice(0, 512);
try {
const embedding = await embed(text);
embedded.push({ ...bm, embedding });
} catch (e) {
dbg("warn", `Failed to embed bookmark "${bm.title}": ${e.message}`);
}
}),
);
// Progress: 65% → 95% during indexing
progress = 65 + Math.round(((i + BATCH_SIZE) / bookmarks.length) * 30);
}
dbg("info", `Embedded ${embedded.length} / ${bookmarks.length} bookmarks`);
indexedBookmarks = embedded;
await saveIndex(embedded);
state = "ready";
progress = 100;
dbg("info", "Index build complete");
}
// ── Search ────────────────────────────────────────────────────────────────────
async function search(query, topK = 8) {
if (state !== "ready") throw new Error("Not ready");
// Lazily load the embedder on first search after a cache-hit cold start.
// ensureEmbedder() is a no-op if the model is already loaded.
await ensureEmbedder();
dbg("info", `Searching: "${query}"`);
const queryEmbedding = await embed(query);
const scored = indexedBookmarks.map((bm) => ({
id: bm.id,
title: bm.title,
url: bm.url,
score: cosineSimilarity(queryEmbedding, bm.embedding),
}));
scored.sort((a, b) => b.score - a.score);
const results = scored.slice(0, topK).filter((r) => r.score > 0.15);
dbg("info", `Search returned ${results.length} results`);
return results;
}
// ── Message handler ───────────────────────────────────────────────────────────
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
(async () => {
try {
if (message.type === "INIT") {
if (state === "ready") {
sendResponse({ status: "ready", count: bookmarkCount });
} else {
initialize();
sendResponse({ status: state, progress, count: bookmarkCount });
}
} else if (message.type === "STATUS") {
sendResponse({
status: state,
progress,
count: bookmarkCount,
message: errorMessage,
});
} else if (message.type === "SEARCH") {
const results = await search(message.query);
sendResponse({ results });
} else if (message.type === "REINDEX") {
await clearIndex();
indexedBookmarks = [];
state = "idle";
progress = 0;
initialize(true);
sendResponse({ status: "started" });
} else if (message.type === "GET_DEBUG") {
sendResponse({ logs: debugLog, debug: DEBUG });
}
} catch (err) {
dbg("error", "Message handler error:", err.message);
sendResponse({ error: err.message });
}
})();
return true;
});
// Auto-init when service worker starts
dbg("info", "Service worker started");
dbg("info", "WASM base path:", WASM_BASE);
dbg("info", "numThreads:", env.backends.onnx.wasm.numThreads);
initialize();