/**
 * fastembed embedder — in-process text→vector via fastembed-js (bge-small-en-v1.5, 384d).
 * No Ollama, no Python, no daemon (confirmed by spike E0: init ~6.5s one-time model load,
 * ~112ms for 2 strings, dim 384). This is the self-contained OSS-local embedder.
 *
 * The model loads lazily on first `embed()` (the one-time ~6.5s download+load is a startup
 * cost, not per-call) and is cached for the process. `fastembed` is imported dynamically so a
 * host that never selects this embedder pays nothing for the native onnxruntime dependency.
 * Same one-way `up` latch as the Ollama embedder: on failure, degrade to `null` for the session.
 *
 * Imported by: src/rag/backend_factory.ts (the `libsql-fastembed` arm).
 */
import { homedir } from 'node:os';
import { join } from 'node:path';
// Cache the downloaded model under opensquid's home, NOT the process CWD (fastembed's default
// `local_cache/` would otherwise dump model files into the user's project tree).
function modelCacheDir() {
    const home = process.env.OPENSQUID_HOME?.trim();
    return join(home && home.length > 0 ? home : join(homedir(), '.opensquid'), 'models');
}
export function fastembedEmbedder() {
    let model = null;
    let up = true;
    return {
        dim: 384, // bge-small-en-v1.5
        async embed(text) {
            if (!up)
                return null;
            try {
                if (model === null) {
                    const { FlagEmbedding, EmbeddingModel } = (await import('fastembed'));
                    model = await FlagEmbedding.init({
                        model: EmbeddingModel.BGESmallENV15,
                        cacheDir: modelCacheDir(),
                    });
                }
                for await (const batch of model.embed([text])) {
                    const v = batch[0];
                    if (v !== undefined)
                        return Array.from(v);
                }
                return null;
            }
            catch {
                up = false;
                return null;
            }
        },
    };
}
//# sourceMappingURL=fastembed.js.map