"use strict";
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 cache_1 = require("../../cache");
const logger_1 = __importDefault(require("../../logger"));
const textOverflow_1 = require("../embedding/textOverflow");
const shared_1 = require("../shared");
const _1 = require(".");
const util_1 = require("./util");
class OpenAiEmbeddingProvider extends _1.OpenAiGenericProvider {
    async callEmbeddingApi(text) {
        if (this.requiresApiKey() && !this.getApiKey()) {
            return {
                error: `API key is not set. Set the ${this.config.apiKeyEnvar || 'OPENAI_API_KEY'} environment variable or add \`apiKey\` to the provider config.`,
            };
        }
        if (typeof text !== 'string') {
            return {
                error: `Invalid input type for embedding API. Expected string, got ${typeof text}. Input: ${JSON.stringify(text)}`,
            };
        }
        const prepared = (0, textOverflow_1.prepareEmbeddingInput)(text, this.config);
        const expectedCount = Array.isArray(prepared.input) ? prepared.input.length : 1;
        const metadata = (0, textOverflow_1.buildEmbeddingOverflowMetadata)(prepared);
        let data;
        let status;
        let statusText;
        let deleteFromCache;
        let cached = false;
        let latencyMs;
        try {
            const response = await (0, cache_1.fetchWithCache)(`${this.getApiUrl()}/embeddings`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    Authorization: `Bearer ${this.getApiKey()}`,
                    ...(this.getOrganization() ? { 'OpenAI-Organization': this.getOrganization() } : {}),
                    ...this.config.headers,
                },
                body: JSON.stringify({
                    input: prepared.input,
                    model: this.modelName,
                }),
            }, shared_1.REQUEST_TIMEOUT_MS, 'json', false, this.config.maxRetries);
            ({ data, cached, status, statusText, latencyMs, deleteFromCache } = response);
            if (status && (status < 200 || status >= 300)) {
                return {
                    error: `API error: ${status} ${statusText || 'Unknown error'}\n${typeof data === 'string' ? data : JSON.stringify(data)}`,
                    ...(metadata ? { metadata } : {}),
                };
            }
        }
        catch (err) {
            logger_1.default.error(`API call error: ${String(err)}`);
            await deleteFromCache?.();
            return {
                error: `API call error: ${String(err)}`,
                ...(metadata ? { metadata } : {}),
            };
        }
        const aggregated = (0, textOverflow_1.aggregateEmbeddingsFromResponse)(data, expectedCount, prepared.strategy);
        if (aggregated.error || !aggregated.embedding) {
            logger_1.default.error(`Response parsing error: ${aggregated.error}`);
            await deleteFromCache?.();
            return {
                error: aggregated.error ?? 'No embedding found in OpenAI embeddings API response',
                ...(metadata ? { metadata } : {}),
            };
        }
        return {
            embedding: aggregated.embedding,
            latencyMs,
            tokenUsage: (0, util_1.getTokenUsage)(data, cached),
            ...(metadata ? { metadata } : {}),
        };
    }
}
exports.OpenAiEmbeddingProvider = OpenAiEmbeddingProvider;
//# sourceMappingURL=embedding.js.map