import { i as logger_default } from "./logger-CATn0kWr.js";
import { f as REQUEST_TIMEOUT_MS } from "./fetch-BhuDaSn5.js";
import { a as fetchWithCache } from "./cache-C2ESUUfo.js";
import { t as OpenAiGenericProvider } from "./openai-BH2FsThj.js";
import { c as getTokenUsage } from "./util-DUayzR6E.js";

//#region src/providers/openai/embedding.ts
var OpenAiEmbeddingProvider = class extends 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 body = {
			input: text,
			model: this.modelName
		};
		let data;
		let status;
		let statusText;
		let deleteFromCache;
		let cached = false;
		let latencyMs;
		try {
			const response = await 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(body)
			}, 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)}` };
		} catch (err) {
			logger_default.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)
			};
		} catch (err) {
			logger_default.error(`Response parsing error: ${String(err)}`);
			await deleteFromCache?.();
			return { error: `API error: ${String(err)}: ${JSON.stringify(data)}` };
		}
	}
};

//#endregion
export { OpenAiEmbeddingProvider as t };
//# sourceMappingURL=embedding-Bo_7FjBa.js.map