"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AzureEmbeddingProvider = void 0;
const cache_1 = require("../../cache");
const logger_1 = __importDefault(require("../../logger"));
const invariant_1 = __importDefault(require("../../util/invariant"));
const textOverflow_1 = require("../embedding/textOverflow");
const shared_1 = require("../shared");
const defaults_1 = require("./defaults");
const generic_1 = require("./generic");
class AzureEmbeddingProvider extends generic_1.AzureGenericProvider {
    async callEmbeddingApi(text) {
        await this.ensureInitialized();
        (0, invariant_1.default)(this.authHeaders, 'auth headers are not initialized');
        if (!this.getApiBaseUrl()) {
            throw new Error('Azure API host must be set.');
        }
        const prepared = (0, textOverflow_1.prepareEmbeddingInput)(text, this.config);
        const expectedCount = Array.isArray(prepared.input) ? prepared.input.length : 1;
        let data;
        let cached = false;
        try {
            ({ data, cached } = await this.callEmbeddingApiRaw(prepared.input));
        }
        catch (err) {
            return {
                error: `API call error: ${String(err)}`,
                tokenUsage: {
                    total: 0,
                    prompt: 0,
                    completion: 0,
                },
            };
        }
        logger_1.default.debug(`\tAzure API response (embeddings): ${JSON.stringify(data)}`);
        const aggregated = (0, textOverflow_1.aggregateEmbeddingsFromResponse)(data, expectedCount, prepared.strategy);
        const metadata = (0, textOverflow_1.buildEmbeddingOverflowMetadata)(prepared);
        const tokenUsage = cached
            ? { cached: data.usage?.total_tokens, total: data.usage?.total_tokens }
            : {
                total: data?.usage?.total_tokens,
                prompt: data?.usage?.prompt_tokens,
                completion: data?.usage?.completion_tokens,
            };
        if (aggregated.error || !aggregated.embedding) {
            return {
                error: `API response error: ${aggregated.error ?? 'No embedding returned'}: ${JSON.stringify(data)}`,
                tokenUsage,
                ...(metadata ? { metadata } : {}),
            };
        }
        return {
            embedding: aggregated.embedding,
            tokenUsage,
            ...(metadata ? { metadata } : {}),
        };
    }
    async callEmbeddingApiRaw(input) {
        const body = {
            input,
            model: this.deploymentName,
        };
        return (await (0, cache_1.fetchWithCache)(`${this.getApiBaseUrl()}/openai/deployments/${this.deploymentName}/embeddings?api-version=${this.config.apiVersion || defaults_1.DEFAULT_AZURE_API_VERSION}`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                ...this.authHeaders,
                ...this.config.headers,
            },
            body: JSON.stringify(body),
        }, shared_1.REQUEST_TIMEOUT_MS));
    }
}
exports.AzureEmbeddingProvider = AzureEmbeddingProvider;
//# sourceMappingURL=embedding.js.map