feat: US-004 - Preload ONNX model during boot sequence

This commit is contained in:
2026-02-15 17:58:41 +00:00
parent aa1774320a
commit 91f8dac261
2 changed files with 31 additions and 0 deletions
+26
View File
@@ -0,0 +1,26 @@
import { pipeline, type FeatureExtractionPipeline } from '@xenova/transformers'
let extractor: FeatureExtractionPipeline | null = null
let loading = false
export async function initModel(): Promise<void> {
if (extractor || loading) return
loading = true
try {
extractor = await pipeline('feature-extraction', 'Xenova/all-MiniLM-L6-v2') as FeatureExtractionPipeline
} catch {
// Silently swallow — model unavailable, semantic search won't activate
} finally {
loading = false
}
}
export async function embedQuery(text: string): Promise<number[]> {
if (!extractor) throw new Error('Model not loaded')
const output = await extractor(text, { pooling: 'mean', normalize: true })
return Array.from(output.data as Float32Array)
}
export function isModelReady(): boolean {
return extractor !== null
}