import { join } from "node:path";
import { FlagEmbedding, EmbeddingModel } from "fastembed";
import { getConfig } from "../config.js";
import { createLogger } from "../logger.js";
const log = createLogger("embedding");
class FastembedEmbedProvider {
  model = null;
  modelPromise = null;
  modelId;
  dim;
  constructor() {
    this.modelId = "BGE-small-en-v1.5";
    this.dim = 384;
  }
  async warmup() {
    await this.getModel();
  }
  getModel() {
    if (this.model) return Promise.resolve(this.model);
    if (this.modelPromise) return this.modelPromise;
    log.info("Loading embedding model", { modelId: this.modelId });
    const cacheDir = join(getConfig().dataDir, "fastembed");
    this.modelPromise = FlagEmbedding.init({
      model: EmbeddingModel.BGESmallENV15,
      cacheDir
    }).then((m) => {
      this.model = m;
      log.info("Embedding model ready", { modelId: this.modelId, dim: this.dim });
      return m;
    }).catch((err) => {
      this.modelPromise = null;
      throw err;
    });
    return this.modelPromise;
  }
  async embed(texts) {
    if (texts.length === 0) return [];
    const model = await this.getModel();
    const out = [];
    for await (const batch of model.embed(texts, texts.length)) {
      for (const vec of batch) {
        out.push(Float32Array.from(vec));
      }
    }
    return out;
  }
}
export {
  FastembedEmbedProvider
};
//# sourceMappingURL=fastembed-provider.js.map