import { i as normalizeProviderId } from "./provider-id-Dq06Bcx6.js";
//#region src/agents/openai-codex-routing.ts
const OPENAI_PROVIDER_ID = "openai";
const OPENAI_CODEX_PROVIDER_ID = "openai-codex";
function isOfficialOpenAIBaseUrl(baseUrl) {
	if (typeof baseUrl !== "string" || !baseUrl.trim()) return true;
	try {
		const url = new URL(baseUrl.trim());
		return url.protocol === "https:" && url.hostname.toLowerCase() === "api.openai.com" && (url.pathname === "" || url.pathname === "/" || url.pathname === "/v1" || url.pathname === "/v1/");
	} catch {
		return false;
	}
}
function openAIProviderUsesCustomBaseUrl(config) {
	return !isOfficialOpenAIBaseUrl(config?.models?.providers?.openai?.baseUrl);
}
function hasProviderConfig(config, provider) {
	return Boolean(config?.models?.providers?.[provider]);
}
function isOpenAIProvider(provider) {
	const normalized = normalizeProviderId(provider ?? "");
	return normalized === "openai" || normalized === "openai-codex";
}
function isOpenAICodexProvider(provider) {
	return normalizeProviderId(provider ?? "") === OPENAI_CODEX_PROVIDER_ID;
}
function openAIProviderUsesCodexRuntimeByDefault(params) {
	return isOpenAIProvider(params.provider) && !openAIProviderUsesCustomBaseUrl(params.config);
}
function parseModelRefProvider(value) {
	if (typeof value !== "string") return;
	const slashIndex = value.trim().indexOf("/");
	if (slashIndex <= 0) return;
	return normalizeProviderId(value.trim().slice(0, slashIndex));
}
function modelSelectionShouldEnsureCodexPlugin(params) {
	const provider = parseModelRefProvider(params.model);
	if (provider === "openai-codex") return true;
	return provider === "openai" && !openAIProviderUsesCustomBaseUrl(params.config);
}
function listOpenAIAuthProfileProvidersForAgentRuntime(params) {
	if (!isOpenAIProvider(params.provider)) return [params.provider];
	return openAIProviderUsesCodexRuntimeByDefault({
		provider: params.provider,
		config: params.config
	}) ? [OPENAI_CODEX_PROVIDER_ID, OPENAI_PROVIDER_ID] : [OPENAI_PROVIDER_ID];
}
function resolveOpenAIRuntimeProvider(params) {
	return isOpenAIProvider(params.provider) ? OPENAI_PROVIDER_ID : params.provider;
}
function resolveSelectedOpenAIRuntimeProvider(params) {
	return isOpenAIProvider(params.provider) ? OPENAI_PROVIDER_ID : params.provider;
}
function resolveContextConfigProviderForRuntime(params) {
	if (!isOpenAIProvider(params.provider)) return params.provider;
	if (params.runtimeId === "codex" && !hasProviderConfig(params.config, "openai") && hasProviderConfig(params.config, "openai-codex")) return OPENAI_CODEX_PROVIDER_ID;
	return OPENAI_PROVIDER_ID;
}
//#endregion
export { listOpenAIAuthProfileProvidersForAgentRuntime as a, parseModelRefProvider as c, resolveSelectedOpenAIRuntimeProvider as d, isOpenAIProvider as i, resolveContextConfigProviderForRuntime as l, OPENAI_PROVIDER_ID as n, modelSelectionShouldEnsureCodexPlugin as o, isOpenAICodexProvider as r, openAIProviderUsesCodexRuntimeByDefault as s, OPENAI_CODEX_PROVIDER_ID as t, resolveOpenAIRuntimeProvider as u };
