const require_logger = require("./logger-DuG3amVa.cjs");
const require_fetch = require("./fetch-Hdu2Q-w3.cjs");
const require_cache = require("./cache-JYoIjHBA.cjs");
const require_util = require("./util-yQtHUDgC.cjs");
const require_billing = require("./billing-BEOsKbij.cjs");
const require_openai = require("./openai-D1Fttk6l.cjs");
//#region src/providers/openai/embedding.ts
var OpenAiEmbeddingProvider = class extends require_openai.OpenAiGenericProvider {
	constructor(modelName, options = {}) {
		super(modelName, options);
	}
	getBillingModelName() {
		return this.modelName;
	}
	async callEmbeddingApi(text) {
		if (this.requiresApiKey() && !this.getApiKey()) return { error: this.getMissingApiKeyErrorMessage() };
		if (typeof text !== "string") return { error: `Invalid input type for embedding API. Expected string, got ${typeof text}. Input: ${JSON.stringify(text)}` };
		const body = {
			input: text,
			model: this.modelName,
			...this.config.passthrough || {}
		};
		let data;
		let status;
		let statusText;
		let deleteFromCache;
		let cached = false;
		let latencyMs;
		try {
			const apiKey = this.getApiKey();
			const response = await require_cache.fetchWithCache(`${this.getApiUrl()}/embeddings`, {
				method: "POST",
				headers: {
					"Content-Type": "application/json",
					...apiKey ? { Authorization: `Bearer ${apiKey}` } : {},
					...this.getOpenAiRequestHeaders()
				},
				body: JSON.stringify(body)
			}, require_fetch.getRequestTimeoutMs(), "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)}` };
		} catch (err) {
			require_logger.logger.error(`API call error: ${String(err)}`);
			await deleteFromCache?.();
			return { error: `API call error: ${String(err)}` };
		}
		try {
			const embedding = data?.data?.[0]?.embedding;
			if (!embedding) return { error: "No embedding found in OpenAI embeddings API response" };
			return {
				embedding,
				latencyMs,
				tokenUsage: require_util.getTokenUsage(data, cached),
				cost: require_billing.calculateOpenAIUsageCost(this.getBillingModelName(), this.config, data.usage, { cachedResponse: cached })
			};
		} catch (err) {
			require_logger.logger.error(`Response parsing error: ${String(err)}`);
			await deleteFromCache?.();
			return { error: `API error: ${String(err)}: ${JSON.stringify(data)}` };
		}
	}
};
//#endregion
Object.defineProperty(exports, "OpenAiEmbeddingProvider", {
	enumerable: true,
	get: function() {
		return OpenAiEmbeddingProvider;
	}
});

//# sourceMappingURL=embedding-B-5e24ct.cjs.map