import { a as normalizeLowercaseStringOrEmpty } from "./string-coerce-mnp54Vah.js";
import "./defaults-mDjiWzE5.js";
import { a as normalizeModelCompat } from "./provider-model-compat-7OQP6Y82.js";
import "./string-coerce-runtime-DzE-BRXv.js";
import { f as cloneFirstTemplateModel, p as matchesExactOrPrefix, u as normalizeProviderId } from "./provider-model-shared-irgBN2qC.js";
import { t as createProviderApiKeyAuthMethod } from "./provider-api-key-auth-CZmnjfTR.js";
import "./provider-auth-api-key-DlSaCoeF.js";
import { l as findCatalogTemplate } from "./provider-catalog-shared-DHq7WOSR.js";
import { i as isOpenAICodexBaseUrl, r as isOpenAIApiBaseUrl } from "./base-url-D93CVaWY.js";
import { a as OPENAI_DEFAULT_MODEL, c as applyOpenAIConfig } from "./default-models-BBgP8hnH.js";
import { n as buildOpenAISyntheticCatalogEntry, t as buildOpenAIResponsesProviderHooks } from "./shared-DhuF4OPZ.js";
import { n as OPENAI_API_KEY_LABEL, t as OPENAI_ACCOUNT_WIZARD_GROUP } from "./auth-choice-copy-CI2uePXw.js";
import { r as resolveUnifiedOpenAIThinkingProfile } from "./thinking-policy-D2pce6l8.js";
import { n as buildOpenAICodexProviderHooks, t as buildOpenAIChatGPTAuthMethods } from "./openai-chatgpt-provider-CgTEUzSb.js";
//#region extensions/openai/openai-provider.ts
const PROVIDER_ID = "openai";
const OPENAI_CHAT_LATEST_MODEL_ID = "chat-latest";
const OPENAI_GPT_55_MODEL_ID = "gpt-5.5";
const OPENAI_GPT_55_PRO_MODEL_ID = "gpt-5.5-pro";
const OPENAI_GPT_54_MODEL_ID = "gpt-5.4";
const OPENAI_GPT_54_PRO_MODEL_ID = "gpt-5.4-pro";
const OPENAI_GPT_54_MINI_MODEL_ID = "gpt-5.4-mini";
const OPENAI_GPT_54_NANO_MODEL_ID = "gpt-5.4-nano";
const OPENAI_GPT_55_CONTEXT_WINDOW = 1e6;
const OPENAI_GPT_55_CONTEXT_TOKENS = 272e3;
const OPENAI_GPT_55_PRO_CONTEXT_TOKENS = 1e6;
const OPENAI_GPT_54_CONTEXT_TOKENS = 105e4;
const OPENAI_GPT_54_PRO_CONTEXT_TOKENS = 105e4;
const OPENAI_GPT_54_MINI_CONTEXT_TOKENS = 4e5;
const OPENAI_GPT_54_NANO_CONTEXT_TOKENS = 4e5;
const OPENAI_GPT_54_MAX_TOKENS = 128e3;
const OPENAI_CHAT_LATEST_COST = {
	input: 5,
	output: 30,
	cacheRead: .5,
	cacheWrite: 0
};
const OPENAI_GPT_55_COST = {
	input: 5,
	output: 30,
	cacheRead: .5,
	cacheWrite: 0
};
const OPENAI_GPT_55_PRO_COST = {
	input: 30,
	output: 180,
	cacheRead: 0,
	cacheWrite: 0
};
const OPENAI_GPT_54_COST = {
	input: 2.5,
	output: 15,
	cacheRead: .25,
	cacheWrite: 0
};
const OPENAI_GPT_54_PRO_COST = {
	input: 30,
	output: 180,
	cacheRead: 0,
	cacheWrite: 0
};
const OPENAI_GPT_54_MINI_COST = {
	input: .75,
	output: 4.5,
	cacheRead: .075,
	cacheWrite: 0
};
const OPENAI_GPT_54_NANO_COST = {
	input: .2,
	output: 1.25,
	cacheRead: .02,
	cacheWrite: 0
};
const OPENAI_GPT_55_PRO_TEMPLATE_MODEL_IDS = [OPENAI_GPT_54_PRO_MODEL_ID, OPENAI_GPT_54_MODEL_ID];
const OPENAI_GPT_55_MEDIA_INPUT = { image: {
	maxSidePx: 6e3,
	preferredSidePx: 2048,
	tokenMode: "detail"
} };
const OPENAI_GPT_54_TEMPLATE_MODEL_IDS = [OPENAI_GPT_55_MODEL_ID];
const OPENAI_GPT_54_PRO_TEMPLATE_MODEL_IDS = [OPENAI_GPT_55_PRO_MODEL_ID];
const OPENAI_GPT_54_MINI_TEMPLATE_MODEL_IDS = ["gpt-5-mini"];
const OPENAI_GPT_54_NANO_TEMPLATE_MODEL_IDS = ["gpt-5-nano", "gpt-5-mini"];
const OPENAI_CHAT_LATEST_TEMPLATE_MODEL_IDS = [OPENAI_GPT_55_MODEL_ID, OPENAI_GPT_54_MODEL_ID];
const OPENAI_MODERN_MODEL_IDS = [
	OPENAI_CHAT_LATEST_MODEL_ID,
	OPENAI_GPT_55_MODEL_ID,
	OPENAI_GPT_55_PRO_MODEL_ID,
	OPENAI_GPT_54_MODEL_ID,
	OPENAI_GPT_54_PRO_MODEL_ID,
	OPENAI_GPT_54_MINI_MODEL_ID,
	OPENAI_GPT_54_NANO_MODEL_ID
];
function shouldUseOpenAIResponsesTransport(params) {
	if (params.api !== "openai-completions") return false;
	if (normalizeProviderId(params.provider) === PROVIDER_ID) return !params.baseUrl || isOpenAIApiBaseUrl(params.baseUrl);
	return typeof params.baseUrl === "string" && isOpenAIApiBaseUrl(params.baseUrl);
}
function isOpenAIProvider(provider) {
	return normalizeProviderId(provider ?? "") === PROVIDER_ID;
}
function normalizeOpenAITransport(model) {
	if (!shouldUseOpenAIResponsesTransport({
		provider: model.provider,
		api: model.api,
		baseUrl: model.baseUrl
	})) return model;
	return {
		...model,
		api: "openai-responses"
	};
}
function shouldUseCodexResponsesHooks(params) {
	if (params.api === "openai-chatgpt-responses") return true;
	return typeof params.baseUrl === "string" && isOpenAICodexBaseUrl(params.baseUrl);
}
function resolveConfiguredAuthTransport(ctx) {
	if (ctx.authProfileMode === "oauth" || ctx.authProfileMode === "token") return "codex";
	if (ctx.authProfileMode === "api_key" || ctx.authProfileMode === "aws-sdk") return "responses";
	const authMode = ctx.providerConfig?.auth;
	if (authMode === "oauth" || authMode === "token") return "codex";
	if (authMode === "api-key") return "responses";
	const auth = ctx.config?.auth;
	const profiles = auth?.profiles ?? {};
	const orderedProfileIds = auth?.order?.[PROVIDER_ID] ?? [];
	for (const profileId of orderedProfileIds) {
		const mode = profiles[profileId]?.mode;
		if (mode === "oauth" || mode === "token") return "codex";
		if (mode === "api_key") return "responses";
	}
	const providerModes = Object.values(profiles).filter((profile) => normalizeProviderId(profile.provider) === PROVIDER_ID).map((profile) => profile.mode);
	if (providerModes.some((mode) => mode === "oauth" || mode === "token")) return "codex";
	if (providerModes.includes("api_key")) return "responses";
}
function shouldResolveDynamicModelThroughCodex(ctx) {
	if (shouldUseCodexResponsesHooks({
		provider: ctx.provider,
		api: ctx.providerConfig?.api,
		baseUrl: ctx.providerConfig?.baseUrl
	})) return true;
	if (ctx.providerConfig?.baseUrl && !isOpenAIApiBaseUrl(ctx.providerConfig.baseUrl)) return false;
	return resolveConfiguredAuthTransport(ctx) === "codex";
}
function resolveOpenAIGptForwardCompatModel(ctx) {
	const trimmedModelId = ctx.modelId.trim();
	const lower = normalizeLowercaseStringOrEmpty(trimmedModelId);
	let templateIds;
	let patch;
	if (lower === OPENAI_CHAT_LATEST_MODEL_ID) {
		templateIds = OPENAI_CHAT_LATEST_TEMPLATE_MODEL_IDS;
		patch = {
			api: "openai-responses",
			provider: PROVIDER_ID,
			baseUrl: "https://api.openai.com/v1",
			reasoning: false,
			input: ["text", "image"],
			cost: OPENAI_CHAT_LATEST_COST,
			contextWindow: 4e5,
			maxTokens: OPENAI_GPT_54_MAX_TOKENS
		};
	} else if (lower === OPENAI_GPT_55_MODEL_ID) {
		templateIds = [OPENAI_GPT_55_MODEL_ID, OPENAI_GPT_54_MODEL_ID];
		patch = {
			api: "openai-responses",
			provider: PROVIDER_ID,
			baseUrl: "https://api.openai.com/v1",
			reasoning: true,
			input: ["text", "image"],
			mediaInput: OPENAI_GPT_55_MEDIA_INPUT,
			cost: OPENAI_GPT_55_COST,
			contextWindow: OPENAI_GPT_55_CONTEXT_WINDOW,
			contextTokens: OPENAI_GPT_55_CONTEXT_TOKENS,
			maxTokens: OPENAI_GPT_54_MAX_TOKENS
		};
	} else if (lower === OPENAI_GPT_55_PRO_MODEL_ID) {
		templateIds = OPENAI_GPT_55_PRO_TEMPLATE_MODEL_IDS;
		patch = {
			api: "openai-responses",
			provider: PROVIDER_ID,
			baseUrl: "https://api.openai.com/v1",
			reasoning: true,
			input: ["text", "image"],
			cost: OPENAI_GPT_55_PRO_COST,
			contextWindow: OPENAI_GPT_55_PRO_CONTEXT_TOKENS,
			maxTokens: OPENAI_GPT_54_MAX_TOKENS
		};
	} else if (lower === OPENAI_GPT_54_MODEL_ID) {
		templateIds = OPENAI_GPT_54_TEMPLATE_MODEL_IDS;
		patch = {
			api: "openai-responses",
			provider: PROVIDER_ID,
			baseUrl: "https://api.openai.com/v1",
			reasoning: true,
			input: ["text", "image"],
			cost: OPENAI_GPT_54_COST,
			contextWindow: OPENAI_GPT_54_CONTEXT_TOKENS,
			maxTokens: OPENAI_GPT_54_MAX_TOKENS
		};
	} else if (lower === OPENAI_GPT_54_PRO_MODEL_ID) {
		templateIds = OPENAI_GPT_54_PRO_TEMPLATE_MODEL_IDS;
		patch = {
			api: "openai-responses",
			provider: PROVIDER_ID,
			baseUrl: "https://api.openai.com/v1",
			reasoning: true,
			input: ["text", "image"],
			cost: OPENAI_GPT_54_PRO_COST,
			contextWindow: OPENAI_GPT_54_PRO_CONTEXT_TOKENS,
			maxTokens: OPENAI_GPT_54_MAX_TOKENS
		};
	} else if (lower === OPENAI_GPT_54_MINI_MODEL_ID) {
		templateIds = OPENAI_GPT_54_MINI_TEMPLATE_MODEL_IDS;
		patch = {
			api: "openai-responses",
			provider: PROVIDER_ID,
			baseUrl: "https://api.openai.com/v1",
			reasoning: true,
			input: ["text", "image"],
			cost: OPENAI_GPT_54_MINI_COST,
			contextWindow: OPENAI_GPT_54_MINI_CONTEXT_TOKENS,
			maxTokens: OPENAI_GPT_54_MAX_TOKENS
		};
	} else if (lower === OPENAI_GPT_54_NANO_MODEL_ID) {
		templateIds = OPENAI_GPT_54_NANO_TEMPLATE_MODEL_IDS;
		patch = {
			api: "openai-responses",
			provider: PROVIDER_ID,
			baseUrl: "https://api.openai.com/v1",
			reasoning: true,
			input: ["text", "image"],
			cost: OPENAI_GPT_54_NANO_COST,
			contextWindow: OPENAI_GPT_54_NANO_CONTEXT_TOKENS,
			maxTokens: OPENAI_GPT_54_MAX_TOKENS
		};
	} else return;
	return cloneFirstTemplateModel({
		providerId: PROVIDER_ID,
		modelId: trimmedModelId,
		templateIds,
		ctx,
		patch
	}) ?? normalizeModelCompat({
		id: trimmedModelId,
		name: trimmedModelId,
		...patch,
		cost: patch.cost ?? {
			input: 0,
			output: 0,
			cacheRead: 0,
			cacheWrite: 0
		},
		contextWindow: patch.contextWindow ?? 2e5,
		maxTokens: patch.maxTokens ?? 2e5
	});
}
function buildOpenAIProvider() {
	const codexHooks = buildOpenAICodexProviderHooks();
	const codexResponsesHooks = buildOpenAIResponsesProviderHooks();
	const responsesHooks = buildOpenAIResponsesProviderHooks({ transport: "sse" });
	return {
		id: PROVIDER_ID,
		label: "OpenAI",
		hookAliases: ["azure-openai", "azure-openai-responses"],
		docsPath: "/providers/models",
		envVars: ["OPENAI_API_KEY"],
		auth: [...buildOpenAIChatGPTAuthMethods(), createProviderApiKeyAuthMethod({
			providerId: PROVIDER_ID,
			methodId: "api-key",
			label: OPENAI_API_KEY_LABEL,
			hint: "Use your OpenAI API key directly",
			optionKey: "openaiApiKey",
			flagName: "--openai-api-key",
			envVar: "OPENAI_API_KEY",
			promptMessage: "Enter OpenAI API key",
			profileId: "openai:api-key",
			defaultModel: OPENAI_DEFAULT_MODEL,
			expectedProviders: ["openai"],
			applyConfig: (cfg) => applyOpenAIConfig(cfg),
			wizard: {
				choiceId: "openai-api-key",
				choiceLabel: OPENAI_API_KEY_LABEL,
				choiceHint: "Use your OpenAI API key directly",
				assistantPriority: 5,
				...OPENAI_ACCOUNT_WIZARD_GROUP
			}
		})],
		resolveDynamicModel: (ctx) => shouldResolveDynamicModelThroughCodex(ctx) ? codexHooks.resolveDynamicModel?.(ctx) : resolveOpenAIGptForwardCompatModel(ctx),
		preferRuntimeResolvedModel: (ctx) => codexHooks.preferRuntimeResolvedModel?.(ctx) ?? false,
		normalizeResolvedModel: (ctx) => {
			if (!isOpenAIProvider(ctx.provider)) return;
			if (shouldUseCodexResponsesHooks({
				provider: ctx.provider,
				api: ctx.model.api,
				baseUrl: ctx.model.baseUrl
			})) return codexHooks.normalizeResolvedModel?.(ctx);
			return normalizeOpenAITransport(ctx.model);
		},
		normalizeTransport: (ctx) => {
			if (shouldUseCodexResponsesHooks(ctx)) return codexHooks.normalizeTransport?.(ctx);
			return shouldUseOpenAIResponsesTransport(ctx) ? {
				api: "openai-responses",
				baseUrl: ctx.baseUrl
			} : void 0;
		},
		...responsesHooks,
		prepareExtraParams: (ctx) => {
			const providerConfig = ctx.config?.models?.providers?.[PROVIDER_ID];
			return (shouldUseCodexResponsesHooks({
				provider: ctx.provider,
				api: ctx.model?.api,
				baseUrl: ctx.model?.baseUrl
			}) || normalizeProviderId(ctx.provider) === PROVIDER_ID && (!providerConfig?.baseUrl || isOpenAIApiBaseUrl(providerConfig.baseUrl)) && resolveConfiguredAuthTransport({
				config: ctx.config,
				providerConfig
			}) === "codex" ? codexResponsesHooks : responsesHooks).prepareExtraParams?.(ctx);
		},
		resolveUsageAuth: codexHooks.resolveUsageAuth,
		fetchUsageSnapshot: codexHooks.fetchUsageSnapshot,
		refreshOAuth: codexHooks.refreshOAuth,
		buildMissingAuthMessage: (ctx) => {
			if (normalizeProviderId(ctx.provider) !== PROVIDER_ID) return;
			if (ctx.listProfileIds(PROVIDER_ID).length === 0) return;
			return "No API key found for provider \"openai\". You are authenticated with OpenAI ChatGPT/Codex OAuth. Use openai/gpt-5.5 with the ChatGPT/Codex OAuth profile, or set OPENAI_API_KEY for direct OpenAI API access.";
		},
		matchesContextOverflowError: ({ errorMessage }) => /content_filter.*(?:prompt|input).*(?:too long|exceed)/i.test(errorMessage),
		resolveReasoningOutputMode: () => "native",
		resolveThinkingProfile: ({ provider, modelId }) => normalizeProviderId(provider) === PROVIDER_ID ? resolveUnifiedOpenAIThinkingProfile(modelId) : null,
		isModernModelRef: ({ modelId }) => matchesExactOrPrefix(modelId, OPENAI_MODERN_MODEL_IDS),
		augmentModelCatalog: (ctx) => {
			const openAiGpt55ProTemplate = findCatalogTemplate({
				entries: ctx.entries,
				providerId: PROVIDER_ID,
				templateIds: OPENAI_GPT_55_PRO_TEMPLATE_MODEL_IDS
			});
			const openAiGpt54Template = findCatalogTemplate({
				entries: ctx.entries,
				providerId: PROVIDER_ID,
				templateIds: OPENAI_GPT_54_TEMPLATE_MODEL_IDS
			});
			const openAiGpt54ProTemplate = findCatalogTemplate({
				entries: ctx.entries,
				providerId: PROVIDER_ID,
				templateIds: OPENAI_GPT_54_PRO_TEMPLATE_MODEL_IDS
			});
			const openAiGpt54MiniTemplate = findCatalogTemplate({
				entries: ctx.entries,
				providerId: PROVIDER_ID,
				templateIds: OPENAI_GPT_54_MINI_TEMPLATE_MODEL_IDS
			});
			const openAiGpt54NanoTemplate = findCatalogTemplate({
				entries: ctx.entries,
				providerId: PROVIDER_ID,
				templateIds: OPENAI_GPT_54_NANO_TEMPLATE_MODEL_IDS
			});
			return [
				buildOpenAISyntheticCatalogEntry(openAiGpt55ProTemplate, {
					id: OPENAI_GPT_55_PRO_MODEL_ID,
					reasoning: true,
					input: ["text", "image"],
					contextWindow: OPENAI_GPT_55_PRO_CONTEXT_TOKENS
				}),
				buildOpenAISyntheticCatalogEntry(openAiGpt54Template, {
					id: OPENAI_GPT_54_MODEL_ID,
					reasoning: true,
					input: ["text", "image"],
					contextWindow: OPENAI_GPT_54_CONTEXT_TOKENS
				}),
				buildOpenAISyntheticCatalogEntry(openAiGpt54ProTemplate, {
					id: OPENAI_GPT_54_PRO_MODEL_ID,
					reasoning: true,
					input: ["text", "image"],
					contextWindow: OPENAI_GPT_54_PRO_CONTEXT_TOKENS
				}),
				buildOpenAISyntheticCatalogEntry(openAiGpt54MiniTemplate, {
					id: OPENAI_GPT_54_MINI_MODEL_ID,
					reasoning: true,
					input: ["text", "image"],
					contextWindow: OPENAI_GPT_54_MINI_CONTEXT_TOKENS
				}),
				buildOpenAISyntheticCatalogEntry(openAiGpt54NanoTemplate, {
					id: OPENAI_GPT_54_NANO_MODEL_ID,
					reasoning: true,
					input: ["text", "image"],
					contextWindow: OPENAI_GPT_54_NANO_CONTEXT_TOKENS
				})
			].filter((entry) => entry !== void 0);
		}
	};
}
/** @deprecated Use buildOpenAIProvider; OpenAI Codex is now an OpenAI auth/transport mode. */
function buildOpenAICodexProviderPlugin() {
	return buildOpenAIProvider();
}
//#endregion
export { buildOpenAIProvider as n, buildOpenAICodexProviderPlugin as t };
