import "./chunk-BT06e5E1.js";
import { b as getEnvString } from "./logger-BudIH0qK.js";
import { t as invariant } from "./invariant-BWiDH1ss.js";
import "./esm-C5zIToiC.js";
import "./pythonUtils-BiVLWZ6g.js";
import "./types-Cg21S-St.js";
import "./util-BaEBV99n.js";
import "./fetch-D70iPJNm.js";
import "./cache-BNI_7DxT.js";
import "./genaiTracer-C6LxD3Ij.js";
import { t as OpenAiChatCompletionProvider } from "./chat-F6Hy65LQ.js";
import "./transform-kOrXspJM.js";
import "./openai-0a7Nnw31.js";
import "./util-CnHd9JAv.js";
import { n as OpenAiEmbeddingProvider, t as OpenAiCompletionProvider } from "./completion-DAe6fo0x.js";

//#region src/providers/cloudflare-ai.ts
function getCloudflareApiConfig(config, env) {
	const apiTokenCandidate = config?.apiKey || (config?.apiKeyEnvar ? getEnvString(config.apiKeyEnvar) || env?.[config.apiKeyEnvar] : void 0) || env?.CLOUDFLARE_API_KEY || getEnvString("CLOUDFLARE_API_KEY");
	invariant(apiTokenCandidate, "Cloudflare API token required. Supply it via config apiKey or apiKeyEnvar, or the CLOUDFLARE_API_KEY environment variable");
	const accountIdCandidate = config?.accountId || (config?.accountIdEnvar ? getEnvString(config.accountIdEnvar) || env?.[config.accountIdEnvar] : void 0) || env?.CLOUDFLARE_ACCOUNT_ID || getEnvString("CLOUDFLARE_ACCOUNT_ID");
	invariant(accountIdCandidate, "Cloudflare account ID required. Supply it via config accountId or accountIdEnvar, or the CLOUDFLARE_ACCOUNT_ID environment variable");
	return {
		apiToken: apiTokenCandidate,
		accountId: accountIdCandidate
	};
}
function getApiBaseUrl(config, env) {
	if (config?.apiBaseUrl) return config.apiBaseUrl;
	const { accountId } = getCloudflareApiConfig(config, env);
	return `https://api.cloudflare.com/client/v4/accounts/${accountId}/ai/v1`;
}
function getPassthroughConfig(config) {
	const { accountId: _accountId, accountIdEnvar: _accountIdEnvar, apiKey: _apiKey, apiKeyEnvar: _apiKeyEnvar, apiBaseUrl: _apiBaseUrl, ...passthrough } = config || {};
	return passthrough;
}
var CloudflareAiChatCompletionProvider = class extends OpenAiChatCompletionProvider {
	cloudflareConfig;
	modelType = "chat";
	constructor(modelName, providerOptions) {
		const apiBaseUrl = getApiBaseUrl(providerOptions.config, providerOptions.env);
		const passthrough = getPassthroughConfig(providerOptions.config);
		const config = {
			...providerOptions.config,
			apiKeyEnvar: "CLOUDFLARE_API_KEY",
			apiBaseUrl,
			passthrough
		};
		super(modelName, {
			...providerOptions,
			config
		});
		this.cloudflareConfig = providerOptions.config || {};
	}
	id() {
		return `cloudflare-ai:${this.modelType}:${this.modelName}`;
	}
	toString() {
		return `[Cloudflare AI ${this.modelType} Provider ${this.modelName}]`;
	}
	getApiKey() {
		const { apiToken } = getCloudflareApiConfig(this.cloudflareConfig, this.env);
		return apiToken;
	}
	toJSON() {
		return {
			provider: "cloudflare-ai",
			model: this.modelName,
			modelType: this.modelType,
			config: {
				...this.config,
				...this.getApiKey() && { apiKey: void 0 }
			}
		};
	}
};
var CloudflareAiCompletionProvider = class extends OpenAiCompletionProvider {
	cloudflareConfig;
	modelType = "completion";
	constructor(modelName, providerOptions) {
		const apiBaseUrl = getApiBaseUrl(providerOptions.config, providerOptions.env);
		const passthrough = getPassthroughConfig(providerOptions.config);
		const config = {
			...providerOptions.config,
			apiKeyEnvar: "CLOUDFLARE_API_KEY",
			apiBaseUrl,
			passthrough
		};
		super(modelName, {
			...providerOptions,
			config
		});
		this.cloudflareConfig = providerOptions.config || {};
	}
	id() {
		return `cloudflare-ai:${this.modelType}:${this.modelName}`;
	}
	toString() {
		return `[Cloudflare AI ${this.modelType} Provider ${this.modelName}]`;
	}
	getApiKey() {
		const { apiToken } = getCloudflareApiConfig(this.cloudflareConfig, this.env);
		return apiToken;
	}
	toJSON() {
		return {
			provider: "cloudflare-ai",
			model: this.modelName,
			modelType: this.modelType,
			config: {
				...this.config,
				...this.getApiKey() && { apiKey: void 0 }
			}
		};
	}
};
var CloudflareAiEmbeddingProvider = class extends OpenAiEmbeddingProvider {
	cloudflareConfig;
	modelType = "embedding";
	constructor(modelName, providerOptions) {
		const apiBaseUrl = getApiBaseUrl(providerOptions.config, providerOptions.env);
		const passthrough = getPassthroughConfig(providerOptions.config);
		const config = {
			...providerOptions.config,
			apiKeyEnvar: "CLOUDFLARE_API_KEY",
			apiBaseUrl,
			passthrough
		};
		super(modelName, {
			...providerOptions,
			config
		});
		this.cloudflareConfig = providerOptions.config || {};
	}
	id() {
		return `cloudflare-ai:${this.modelType}:${this.modelName}`;
	}
	toString() {
		return `[Cloudflare AI ${this.modelType} Provider ${this.modelName}]`;
	}
	getApiKey() {
		const { apiToken } = getCloudflareApiConfig(this.cloudflareConfig, this.env);
		return apiToken;
	}
	toJSON() {
		return {
			provider: "cloudflare-ai",
			model: this.modelName,
			modelType: this.modelType,
			config: {
				...this.config,
				...this.getApiKey() && { apiKey: void 0 }
			}
		};
	}
};
function createCloudflareAiProvider(providerPath, options = {}) {
	const splits = providerPath.split(":");
	const modelType = splits[1];
	const modelName = splits.slice(2).join(":");
	invariant(modelName, "Model name is required");
	switch (modelType) {
		case "chat": return new CloudflareAiChatCompletionProvider(modelName, options);
		case "completion": return new CloudflareAiCompletionProvider(modelName, options);
		case "embedding":
		case "embeddings": return new CloudflareAiEmbeddingProvider(modelName, options);
		default: throw new Error(`Unknown Cloudflare AI model type: ${modelType}`);
	}
}

//#endregion
export { createCloudflareAiProvider };
//# sourceMappingURL=cloudflare-ai-DKW7fIWs.js.map