/* Worker for suite-ai.js: loads the sentence-embedding model and embeds text off the page's main thread, so a slow or software-only GPU can never freeze the page. Messages: {id, op: "load", device} and {id, op: "embed", texts}. */ import { pipeline } from "https://cdn.jsdelivr.net/npm/@huggingface/transformers@4.3.0"; const MODEL = "Xenova/all-MiniLM-L6-v2"; let extractor = null; self.onmessage = async ({ data: m }) => { try { if (m.op === "load") { const opts = { dtype: m.device === "webgpu" ? "fp32" : "q8", device: m.device, progress_callback: (e) => { if (e.status === "progress" && e.total) self.postMessage({ id: m.id, progress: `Downloading ${e.file}: ${Math.round(e.loaded / e.total * 100)}%` }); } }; extractor = await pipeline("feature-extraction", MODEL, opts); await extractor("warm-up", { pooling: "mean", normalize: true }); self.postMessage({ id: m.id, ok: true }); } else if (m.op === "embed") { const out = await extractor(m.texts, { pooling: "mean", normalize: true }); const data = Float32Array.from(out.data); self.postMessage({ id: m.id, ok: true, dims: out.dims, data }, [data.buffer]); } } catch (e) { self.postMessage({ id: m.id, error: String(e && e.message || e) }); } };