#!/usr/bin/env node
import "./chunk-CzxHAdEc.js";
import { w as getEnvString } from "./logger-CjS9Mbik.js";
import "./fetch-Brl8CXgW.js";
import { t as invariant } from "./invariant-CzGQ4hYY.js";
import "./cloud-CJaIuNs9.js";
import "./types-atpq0XCc.js";
import "./cache-B1ROlJSH.js";
import "./util-BuhznZrR.js";
import "./esm-CNEnghzm.js";
import "./pythonUtils-Dxv9iWbf.js";
import { t as OpenAiChatCompletionProvider } from "./chat-DuguS88k.js";
import "./transform-CKc83iY4.js";
import "./openai-DE75mwa1.js";
import "./util-C53zmGL1.js";
import { t as OpenAiEmbeddingProvider } from "./embedding-loH6vzmf.js";
import { t as OpenAiCompletionProvider } from "./completion-NKlL4rxl.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-BJAmIFBB.js.map