import { createAnthropic } from "@ai-sdk/anthropic";
import { createDeepSeek } from "@ai-sdk/deepseek";
import { createGoogleGenerativeAI } from "@ai-sdk/google";
import { createMistral } from "@ai-sdk/mistral";
import { createOpenAI } from "@ai-sdk/openai";

//#region src/utils/AI/aiSdk.ts
/**
* Supported AI SDK providers
*/
let AIProvider = /* @__PURE__ */ function(AIProvider$1) {
	AIProvider$1["OPENAI"] = "openai";
	AIProvider$1["ANTHROPIC"] = "anthropic";
	AIProvider$1["MISTRAL"] = "mistral";
	AIProvider$1["DEEPSEEK"] = "deepseek";
	AIProvider$1["GEMINI"] = "gemini";
	return AIProvider$1;
}({});
const getAPIKey = (res, accessType, aiOptions) => {
	const defaultApiKey = process.env.OPENAI_API_KEY;
	if (accessType.includes("public")) return aiOptions?.apiKey ?? defaultApiKey;
	if (accessType.includes("apiKey") && aiOptions?.apiKey) return aiOptions?.apiKey;
	if (accessType.includes("registered_user") && res.locals.user) return aiOptions?.apiKey ?? defaultApiKey;
	if (accessType.includes("premium_user") && res.locals.user) return aiOptions?.apiKey ?? defaultApiKey;
};
const getModel = (provider, userApiKey, userModel, defaultModel) => {
	const fallBackModel = defaultModel ?? "chatgpt-4o-latest";
	switch (provider) {
		case AIProvider.OPENAI:
			defaultModel = "chatgpt-4o-latest";
			break;
		case AIProvider.ANTHROPIC:
			defaultModel = "claude-3-haiku-20240307";
			break;
		case AIProvider.MISTRAL:
			defaultModel = "mistral-large-latest";
			break;
		case AIProvider.DEEPSEEK:
			defaultModel = "deepseek-coder";
			break;
		case AIProvider.GEMINI:
			defaultModel = "gemini-1.5-pro";
			break;
	}
	if (Boolean(userApiKey) && Boolean(userModel)) return userModel;
	if (userModel) throw new Error("The user should use his own API key to use a custom model");
	return fallBackModel;
};
const DEFAULT_PROVIDER = AIProvider.OPENAI;
const DEFAULT_TEMPERATURE = 1;
/**
* Get AI model configuration based on the selected provider and options
* This function handles the configuration for different AI providers
*
* @param options Configuration options including provider, API keys, models and temperature
* @returns Configured AI model ready to use with generateText
*/
const getAIConfig = async (res, options) => {
	const { userOptions, defaultOptions, accessType = ["registered_user"] } = options;
	const aiOptions = {
		provider: DEFAULT_PROVIDER,
		temperature: DEFAULT_TEMPERATURE,
		...defaultOptions,
		...userOptions
	};
	const apiKey = getAPIKey(res, accessType, aiOptions);
	if (!apiKey) throw new Error(`API key for ${aiOptions.provider} is missing`);
	const selectedModel = getModel(aiOptions.provider, apiKey, aiOptions.model, defaultOptions?.model);
	const protectedOptions = {
		...aiOptions,
		apiKey,
		model: selectedModel
	};
	let languageModel;
	switch (protectedOptions.provider) {
		case AIProvider.OPENAI:
			languageModel = createOpenAI({ apiKey })(selectedModel);
			break;
		case AIProvider.ANTHROPIC:
			languageModel = createAnthropic({ apiKey })(selectedModel);
			break;
		case AIProvider.MISTRAL:
			languageModel = createMistral({ apiKey })(selectedModel);
			break;
		case AIProvider.DEEPSEEK:
			languageModel = createDeepSeek({ apiKey })(selectedModel);
			break;
		case AIProvider.GEMINI:
			languageModel = createGoogleGenerativeAI({ apiKey })(selectedModel);
			break;
		default: throw new Error(`Provider ${protectedOptions.provider} not supported`);
	}
	return {
		model: languageModel,
		temperature: protectedOptions.temperature
	};
};

//#endregion
export { AIProvider, getAIConfig };
//# sourceMappingURL=aiSdk.mjs.map