const require_logger = require('./logger-q_xXORhb.cjs');
const require_fetch = require('./fetch--6dTWCD4.cjs');
const require_cache = require('./cache-BrwLt6g7.cjs');
const require_openai = require('./openai-DLwm54eV.cjs');
const require_util = require('./util-DdYfVi1z.cjs');

//#region src/providers/openai/embedding.ts
var OpenAiEmbeddingProvider = class extends require_openai.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 require_cache.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)
			}, require_fetch.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) {
			require_logger.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: require_util.getTokenUsage(data, cached)
			};
		} catch (err) {
			require_logger.logger_default.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-7FmJs2RM.cjs.map