import { r as formatErrorMessage } from "./errors-CGMew-1w.js";
import { s as normalizeOptionalString } from "./string-coerce-CIT1-v_a.js";
import { n as ensureAuthProfileStore } from "./store-DLavseDR.js";
import { n as listProfilesForProvider } from "./profiles-g4Nk53sC.js";
import "./model-auth-markers-CvwYPdtC.js";
import { t as buildProviderReplayFamilyHooks } from "./provider-model-shared-DkmyZ3nD.js";
import { n as fetchMinimaxUsage } from "./provider-usage.fetch.zai-BxLPbOkm.js";
import "./text-runtime-Drz6LusC.js";
import "./error-runtime-_1NmlXar.js";
import { t as createProviderApiKeyAuthMethod } from "./provider-api-key-auth-BdQOqMrP.js";
import { t as buildOauthProviderAuthResult } from "./provider-auth-result-BisJtSt9.js";
import "./provider-auth-CO56d927.js";
import "./provider-auth-api-key-Dw5uHi0O.js";
import { t as buildProviderStreamFamilyHooks } from "./provider-stream-BCWKnfEA.js";
import "./provider-stream-family-BM1vkZBV.js";
import "./provider-usage-CaQjkq44.js";
import { o as isMiniMaxModernModelId, t as MINIMAX_DEFAULT_MODEL_ID } from "./provider-models-CVbMiJ8s.js";
import { n as buildMinimaxProvider, t as buildMinimaxPortalProvider } from "./provider-catalog-BATkZWDB.js";
import { n as applyMinimaxApiConfigCn, t as applyMinimaxApiConfig } from "./onboard-CNBhV0u8.js";
import "./api-C7KmfGC1.js";
//#region extensions/minimax/provider-registration.ts
const API_PROVIDER_ID = "minimax";
const PORTAL_PROVIDER_ID = "minimax-portal";
const PROVIDER_LABEL = "MiniMax";
const DEFAULT_MODEL = MINIMAX_DEFAULT_MODEL_ID;
const DEFAULT_BASE_URL_CN = "https://api.minimaxi.com/anthropic";
const DEFAULT_BASE_URL_GLOBAL = "https://api.minimax.io/anthropic";
const MINIMAX_USAGE_ENV_VAR_KEYS = [
	"MINIMAX_OAUTH_TOKEN",
	"MINIMAX_CODE_PLAN_KEY",
	"MINIMAX_CODING_API_KEY",
	"MINIMAX_API_KEY"
];
const HYBRID_ANTHROPIC_OPENAI_REPLAY_HOOKS = buildProviderReplayFamilyHooks({
	family: "hybrid-anthropic-openai",
	anthropicModelDropThinkingBlocks: true
});
const MINIMAX_FAST_MODE_STREAM_HOOKS = buildProviderStreamFamilyHooks("minimax-fast-mode");
function resolveMinimaxReasoningOutputMode() {
	return "native";
}
function getDefaultBaseUrl(region) {
	return region === "cn" ? DEFAULT_BASE_URL_CN : DEFAULT_BASE_URL_GLOBAL;
}
function apiModelRef(modelId) {
	return `${API_PROVIDER_ID}/${modelId}`;
}
function portalModelRef(modelId) {
	return `${PORTAL_PROVIDER_ID}/${modelId}`;
}
function buildPortalProviderCatalog(params) {
	return {
		...buildMinimaxPortalProvider(),
		baseUrl: params.baseUrl,
		apiKey: params.apiKey
	};
}
function resolveApiCatalog(ctx) {
	const apiKey = ctx.resolveProviderApiKey(API_PROVIDER_ID).apiKey;
	if (!apiKey) return null;
	return { provider: {
		...buildMinimaxProvider(ctx.env),
		apiKey
	} };
}
function resolvePortalCatalog(ctx) {
	const explicitProvider = ctx.config.models?.providers?.[PORTAL_PROVIDER_ID];
	const envApiKey = ctx.resolveProviderApiKey(PORTAL_PROVIDER_ID).apiKey;
	const hasProfiles = listProfilesForProvider(ensureAuthProfileStore(ctx.agentDir, { allowKeychainPrompt: false }), PORTAL_PROVIDER_ID).length > 0;
	const explicitApiKey = normalizeOptionalString(explicitProvider?.apiKey);
	const apiKey = envApiKey ?? explicitApiKey ?? (hasProfiles ? "minimax-oauth" : void 0);
	if (!apiKey) return null;
	return { provider: buildPortalProviderCatalog({
		baseUrl: normalizeOptionalString(explicitProvider?.baseUrl) || buildMinimaxPortalProvider(ctx.env).baseUrl,
		apiKey
	}) };
}
function createOAuthHandler(region) {
	const defaultBaseUrl = getDefaultBaseUrl(region);
	const regionLabel = region === "cn" ? "CN" : "Global";
	return async (ctx) => {
		const progress = ctx.prompter.progress(`Starting MiniMax OAuth (${regionLabel})…`);
		try {
			const { loginMiniMaxPortalOAuth } = await import("./extensions/minimax/oauth.runtime.js");
			const result = await loginMiniMaxPortalOAuth({
				openUrl: ctx.openUrl,
				note: ctx.prompter.note,
				progress,
				region
			});
			progress.stop("MiniMax OAuth complete");
			if (result.notification_message) await ctx.prompter.note(result.notification_message, "MiniMax OAuth");
			const baseUrl = result.resourceUrl || defaultBaseUrl;
			return buildOauthProviderAuthResult({
				providerId: PORTAL_PROVIDER_ID,
				defaultModel: portalModelRef(DEFAULT_MODEL),
				access: result.access,
				refresh: result.refresh,
				expires: result.expires,
				configPatch: {
					models: { providers: { [PORTAL_PROVIDER_ID]: {
						baseUrl,
						models: []
					} } },
					agents: { defaults: { models: {
						[portalModelRef("MiniMax-M2.7")]: { alias: "minimax-m2.7" },
						[portalModelRef("MiniMax-M2.7-highspeed")]: { alias: "minimax-m2.7-highspeed" }
					} } }
				},
				notes: [
					"MiniMax OAuth tokens auto-refresh. Re-run login if refresh fails or access is revoked.",
					`Base URL defaults to ${defaultBaseUrl}. Override models.providers.${PORTAL_PROVIDER_ID}.baseUrl if needed.`,
					...result.notification_message ? [result.notification_message] : []
				]
			});
		} catch (err) {
			const errorMsg = formatErrorMessage(err);
			progress.stop(`MiniMax OAuth failed: ${errorMsg}`);
			await ctx.prompter.note("If OAuth fails, verify your MiniMax account has portal access and try again.", "MiniMax OAuth");
			throw err;
		}
	};
}
function registerMinimaxProviders(api) {
	api.registerProvider({
		id: API_PROVIDER_ID,
		label: PROVIDER_LABEL,
		hookAliases: ["minimax-cn"],
		docsPath: "/providers/minimax",
		envVars: ["MINIMAX_API_KEY"],
		auth: [createProviderApiKeyAuthMethod({
			providerId: API_PROVIDER_ID,
			methodId: "api-global",
			label: "MiniMax API key (Global)",
			hint: "Global endpoint - api.minimax.io",
			optionKey: "minimaxApiKey",
			flagName: "--minimax-api-key",
			envVar: "MINIMAX_API_KEY",
			promptMessage: "Enter MiniMax API key (sk-api- or sk-cp-)\nhttps://platform.minimax.io/user-center/basic-information/interface-key",
			profileId: "minimax:global",
			allowProfile: false,
			defaultModel: apiModelRef(DEFAULT_MODEL),
			expectedProviders: ["minimax"],
			applyConfig: (cfg) => applyMinimaxApiConfig(cfg),
			wizard: {
				choiceId: "minimax-global-api",
				choiceLabel: "MiniMax API key (Global)",
				choiceHint: "Global endpoint - api.minimax.io",
				groupId: "minimax",
				groupLabel: "MiniMax",
				groupHint: "M2.7 (recommended)"
			}
		}), createProviderApiKeyAuthMethod({
			providerId: API_PROVIDER_ID,
			methodId: "api-cn",
			label: "MiniMax API key (CN)",
			hint: "CN endpoint - api.minimaxi.com",
			optionKey: "minimaxApiKey",
			flagName: "--minimax-api-key",
			envVar: "MINIMAX_API_KEY",
			promptMessage: "Enter MiniMax CN API key (sk-api- or sk-cp-)\nhttps://platform.minimaxi.com/user-center/basic-information/interface-key",
			profileId: "minimax:cn",
			allowProfile: false,
			defaultModel: apiModelRef(DEFAULT_MODEL),
			expectedProviders: ["minimax", "minimax-cn"],
			applyConfig: (cfg) => applyMinimaxApiConfigCn(cfg),
			wizard: {
				choiceId: "minimax-cn-api",
				choiceLabel: "MiniMax API key (CN)",
				choiceHint: "CN endpoint - api.minimaxi.com",
				groupId: "minimax",
				groupLabel: "MiniMax",
				groupHint: "M2.7 (recommended)"
			}
		})],
		catalog: {
			order: "simple",
			run: async (ctx) => resolveApiCatalog(ctx)
		},
		resolveUsageAuth: async (ctx) => {
			const portalOauth = await ctx.resolveOAuthToken({ provider: PORTAL_PROVIDER_ID });
			if (portalOauth) return portalOauth;
			const apiKey = ctx.resolveApiKeyFromConfigAndStore({
				providerIds: [API_PROVIDER_ID, PORTAL_PROVIDER_ID],
				envDirect: MINIMAX_USAGE_ENV_VAR_KEYS.map((name) => ctx.env[name])
			});
			return apiKey ? { token: apiKey } : null;
		},
		...HYBRID_ANTHROPIC_OPENAI_REPLAY_HOOKS,
		...MINIMAX_FAST_MODE_STREAM_HOOKS,
		resolveReasoningOutputMode: () => resolveMinimaxReasoningOutputMode(),
		isModernModelRef: ({ modelId }) => isMiniMaxModernModelId(modelId),
		fetchUsageSnapshot: async (ctx) => await fetchMinimaxUsage(ctx.token, ctx.timeoutMs, ctx.fetchFn)
	});
	api.registerProvider({
		id: PORTAL_PROVIDER_ID,
		label: PROVIDER_LABEL,
		hookAliases: ["minimax-portal-cn"],
		docsPath: "/providers/minimax",
		envVars: ["MINIMAX_OAUTH_TOKEN", "MINIMAX_API_KEY"],
		catalog: { run: async (ctx) => resolvePortalCatalog(ctx) },
		auth: [{
			id: "oauth",
			label: "MiniMax OAuth (Global)",
			hint: "Global endpoint - api.minimax.io",
			kind: "device_code",
			wizard: {
				choiceId: "minimax-global-oauth",
				choiceLabel: "MiniMax OAuth (Global)",
				choiceHint: "Global endpoint - api.minimax.io",
				groupId: "minimax",
				groupLabel: "MiniMax",
				groupHint: "M2.7 (recommended)"
			},
			run: createOAuthHandler("global")
		}, {
			id: "oauth-cn",
			label: "MiniMax OAuth (CN)",
			hint: "CN endpoint - api.minimaxi.com",
			kind: "device_code",
			wizard: {
				choiceId: "minimax-cn-oauth",
				choiceLabel: "MiniMax OAuth (CN)",
				choiceHint: "CN endpoint - api.minimaxi.com",
				groupId: "minimax",
				groupLabel: "MiniMax",
				groupHint: "M2.7 (recommended)"
			},
			run: createOAuthHandler("cn")
		}],
		...HYBRID_ANTHROPIC_OPENAI_REPLAY_HOOKS,
		...MINIMAX_FAST_MODE_STREAM_HOOKS,
		resolveReasoningOutputMode: () => resolveMinimaxReasoningOutputMode(),
		isModernModelRef: ({ modelId }) => isMiniMaxModernModelId(modelId)
	});
}
//#endregion
export { registerMinimaxProviders as t };
