export class OpenRouterEmbeddingProvider {
    id = 'openrouter-embedding';
    model;
    dimensions;
    apiKey;
    baseUrl;
    batchSize;
    constructor(config) {
        this.model = config.model;
        this.dimensions = config.dimensions ?? 1536;
        this.apiKey = config.apiKey;
        this.baseUrl = config.baseUrl ?? 'https://openrouter.ai/api/v1';
        this.batchSize = config.batchSize ?? 64;
    }
    async embed(texts) {
        const results = [];
        for (let i = 0; i < texts.length; i += this.batchSize) {
            const batch = texts.slice(i, i + this.batchSize);
            const batchResults = await this.callEmbeddingApi(batch);
            results.push(...batchResults);
        }
        return results;
    }
    async embedQuery(query) {
        const [result] = await this.embed([query]);
        if (!result) {
            throw new Error('Embedding returned no results');
        }
        return result;
    }
    async callEmbeddingApi(texts) {
        const response = await fetch(`${this.baseUrl}/embeddings`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                Authorization: `Bearer ${this.apiKey}`,
            },
            body: JSON.stringify({
                model: this.model,
                input: texts,
                dimensions: this.dimensions,
            }),
        });
        if (!response.ok) {
            const errorText = await response.text();
            throw new Error(`Embedding API error (${response.status}): ${errorText}`);
        }
        const data = (await response.json());
        return data.data.sort((a, b) => a.index - b.index).map((d) => d.embedding);
    }
}
//# sourceMappingURL=embedding.js.map