#!/usr/bin/env node
import { c as logger } from "./logger-4ntIRWmf.js";
import { C as getRequestTimeoutMs } from "./fetch-gOBR1GF3.js";
import { r as fetchWithCache } from "./cache-ByuR9dhw.js";
import { u as getTokenUsage } from "./util-C5qwNYly.js";
import { n as calculateOpenAIUsageCost } from "./billing-D-k_rN9I.js";
import { t as OpenAiGenericProvider } from "./openai-D4nC8Z-O.js";
//#region src/providers/openai/embedding.ts
var OpenAiEmbeddingProvider = class extends 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 fetchWithCache(`${this.getApiUrl()}/embeddings`, {
				method: "POST",
				headers: {
					"Content-Type": "application/json",
					...apiKey ? { Authorization: `Bearer ${apiKey}` } : {},
					...this.getOpenAiRequestHeaders()
				},
				body: JSON.stringify(body)
			}, 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) {
			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: getTokenUsage(data, cached),
				cost: calculateOpenAIUsageCost(this.getBillingModelName(), this.config, data.usage, { cachedResponse: cached })
			};
		} catch (err) {
			logger.error(`Response parsing error: ${String(err)}`);
			await deleteFromCache?.();
			return { error: `API error: ${String(err)}: ${JSON.stringify(data)}` };
		}
	}
};
//#endregion
export { OpenAiEmbeddingProvider as t };

//# sourceMappingURL=embedding-DngVNcyg.js.map