"use strict";
// src/orchestrator/rag/OpenAIEmbeddingProvider.ts
// v1.13.0 — Opt-in cloud embedding provider.
//
// Uses the existing `openai` SDK already in the project's dependency
// tree (see src/orchestrator/Brain.ts:5). Activated via
// OPENLIFE_EMBEDDING_PROVIDER=openai. Costs apply.
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.OpenAIEmbeddingProvider = void 0;
const openai_1 = __importDefault(require("openai"));
const EmbeddingProvider_1 = require("./EmbeddingProvider");
const DEFAULT_MODEL = 'text-embedding-3-small';
const DEFAULT_DIM = 1536;
class OpenAIEmbeddingProvider extends EmbeddingProvider_1.AbstractEmbeddingProvider {
    client;
    modelName;
    dimSize;
    constructor(opts) {
        super();
        const apiKey = opts?.apiKey ?? process.env.OPENAI_API_KEY ?? '';
        if (apiKey.trim().length === 0) {
            this.client = null;
        }
        else {
            const clientOpts = { apiKey };
            const baseUrl = (opts?.baseUrl || process.env.OPENAI_BASE_URL || '').trim();
            if (baseUrl)
                clientOpts.baseURL = baseUrl;
            this.client = new openai_1.default(clientOpts);
        }
        this.modelName = opts?.model || process.env.OPENLIFE_OPENAI_EMBED_MODEL || DEFAULT_MODEL;
        this.dimSize = opts?.dim || Number(process.env.OPENLIFE_OPENAI_EMBED_DIM || DEFAULT_DIM);
    }
    name() { return 'openai'; }
    dim() { return this.dimSize; }
    model() { return this.modelName; }
    async embed(text) {
        if (!this.client)
            throw new Error('[openai-embed] OPENAI_API_KEY missing — set it or switch to OPENLIFE_EMBEDDING_PROVIDER=ollama');
        const out = await this.client.embeddings.create({ model: this.modelName, input: text });
        const vec = out.data[0]?.embedding;
        if (!vec)
            throw new Error('[openai-embed] empty response.data[0].embedding');
        return vec;
    }
    async embedBatch(texts) {
        if (!this.client)
            throw new Error('[openai-embed] OPENAI_API_KEY missing');
        if (texts.length === 0)
            return [];
        const out = await this.client.embeddings.create({ model: this.modelName, input: texts });
        return out.data.map(d => d.embedding);
    }
    async isAvailable() {
        return this.client !== null;
    }
}
exports.OpenAIEmbeddingProvider = OpenAIEmbeddingProvider;
