import { ensureAuthProfileStore, resolveAuthProfileOrder } from "../agents/auth-profiles.js";
import { resolveEnvApiKey } from "../agents/model-auth.js";
import { formatApiKeyPreview, normalizeApiKeyInput, validateApiKeyInput, } from "./auth-choice.api-key.js";
import { applyDefaultModelChoice } from "./auth-choice.default-model.js";
import { applyGoogleGeminiModelDefault, GOOGLE_GEMINI_DEFAULT_MODEL, } from "./google-gemini-model-default.js";
import { applyAuthProfileConfig, applyCloudflareAiGatewayConfig, applyCloudflareAiGatewayProviderConfig, applyCustomProviderConfig, applyKimiCodingConfig, applyKimiCodingProviderConfig, applyMoonshotConfig, applyMoonshotCnConfig, applyMoonshotProviderConfig, applyMoonshotCnProviderConfig, applyOpencodeZenConfig, applyOpencodeZenProviderConfig, applyOpenrouterConfig, applyOpenrouterProviderConfig, applySyntheticConfig, applySyntheticProviderConfig, applyVeniceConfig, applyVeniceProviderConfig, applyVercelAiGatewayConfig, applyVercelAiGatewayProviderConfig, applyXiaomiConfig, applyXiaomiProviderConfig, applyZaiConfig, CLOUDFLARE_AI_GATEWAY_DEFAULT_MODEL_REF, KIMI_CODING_MODEL_REF, MOONSHOT_DEFAULT_MODEL_REF, OPENROUTER_DEFAULT_MODEL_REF, SYNTHETIC_DEFAULT_MODEL_REF, VENICE_DEFAULT_MODEL_REF, VERCEL_AI_GATEWAY_DEFAULT_MODEL_REF, XIAOMI_DEFAULT_MODEL_REF, setCloudflareAiGatewayConfig, setGeminiApiKey, setKimiCodingApiKey, setMoonshotApiKey, setOpencodeZenApiKey, setOpenrouterApiKey, setSyntheticApiKey, setVeniceApiKey, setVercelAiGatewayApiKey, setXiaomiApiKey, setZaiApiKey, ZAI_DEFAULT_MODEL_REF, } from "./onboard-auth.js";
import { discoverOpenAICompatibleModels, buildDiscoveredModelOptions, } from "../agents/openai-models-discovery.js";
import { applyEphoneConfig, applyEphoneProviderConfig, EPHONE_BASE_URL, EPHONE_DEFAULT_MODEL_ID, EPHONE_MODELS, setEphoneApiKey, applySiliconflowConfig, applySiliconflowProviderConfig, applyDashscopeConfig, applyDashscopeProviderConfig, applyDashscopeCodingPlanConfig, applyDashscopeCodingPlanProviderConfig, applyDeepseekConfig, applyDeepseekProviderConfig, SILICONFLOW_BASE_URL, SILICONFLOW_DEFAULT_MODEL_REF, DASHSCOPE_BASE_URL, DASHSCOPE_DEFAULT_MODEL_REF, DASHSCOPE_CODING_PLAN_BASE_URL, DASHSCOPE_CODING_PLAN_DEFAULT_MODEL_ID, DASHSCOPE_CODING_PLAN_MODELS, DEEPSEEK_BASE_URL, DEEPSEEK_DEFAULT_MODEL_REF, setSiliconflowApiKey, setDashscopeApiKey, setDashscopeCodingPlanApiKey, setMoonshotCodingPlanApiKey, setDeepseekApiKey, applyMoonshotCodingPlanConfig, applyMoonshotCodingPlanProviderConfig, MOONSHOT_BASE_URL, MOONSHOT_CN_BASE_URL, MOONSHOT_CODING_PLAN_DEFAULT_MODEL_ID, } from "./onboard-auth.js";
import { OPENCODE_ZEN_DEFAULT_MODEL } from "./opencode-zen-model-default.js";
import { XIAOMI_API_BASE_URL } from "../agents/models-config.providers.js";
const ZAI_BASE_URL = "https://open.bigmodel.cn/api/paas/v4";
export async function applyAuthChoiceApiProviders(params) {
    let nextConfig = params.config;
    let agentModelOverride;
    const noteAgentModel = async (model) => {
        if (!params.agentId) {
            return;
        }
        await params.prompter.note(`Default model set to ${model} for agent "${params.agentId}".`, "Model configured");
    };
    let authChoice = params.authChoice;
    if (authChoice === "apiKey" &&
        params.opts?.tokenProvider &&
        params.opts.tokenProvider !== "anthropic" &&
        params.opts.tokenProvider !== "openai") {
        if (params.opts.tokenProvider === "openrouter") {
            authChoice = "openrouter-api-key";
        }
        else if (params.opts.tokenProvider === "vercel-ai-gateway") {
            authChoice = "ai-gateway-api-key";
        }
        else if (params.opts.tokenProvider === "cloudflare-ai-gateway") {
            authChoice = "cloudflare-ai-gateway-api-key";
        }
        else if (params.opts.tokenProvider === "moonshot") {
            authChoice = "moonshot-api-key";
        }
        else if (params.opts.tokenProvider === "kimi-code" ||
            params.opts.tokenProvider === "kimi-coding") {
            authChoice = "kimi-code-api-key";
        }
        else if (params.opts.tokenProvider === "dashscope-coding-plan") {
            authChoice = "dashscope-coding-plan-api-key";
        }
        else if (params.opts.tokenProvider === "moonshot-coding-plan") {
            authChoice = "moonshot-coding-plan-api-key";
        }
        else if (params.opts.tokenProvider === "google") {
            authChoice = "gemini-api-key";
        }
        else if (params.opts.tokenProvider === "zai") {
            authChoice = "zai-api-key";
        }
        else if (params.opts.tokenProvider === "xiaomi") {
            authChoice = "xiaomi-api-key";
        }
        else if (params.opts.tokenProvider === "synthetic") {
            authChoice = "synthetic-api-key";
        }
        else if (params.opts.tokenProvider === "venice") {
            authChoice = "venice-api-key";
        }
        else if (params.opts.tokenProvider === "opencode") {
            authChoice = "opencode-zen";
        }
        else if (params.opts.tokenProvider === "ephone") {
            authChoice = "ephone-api-key";
        }
    }
    if (authChoice === "ephone-api-key") {
        let hasCredential = false;
        let resolvedApiKey = "";
        if (!hasCredential && params.opts?.token && params.opts?.tokenProvider === "ephone") {
            resolvedApiKey = normalizeApiKeyInput(params.opts.token);
            await setEphoneApiKey(resolvedApiKey, params.agentDir);
            hasCredential = true;
        }
        const envKey = resolveEnvApiKey("ephone");
        if (envKey && !hasCredential) {
            const useExisting = await params.prompter.confirm({
                message: `使用已有 EPHONE_API_KEY (${envKey.source}, ${formatApiKeyPreview(envKey.apiKey)})？`,
                initialValue: true,
            });
            if (useExisting) {
                resolvedApiKey = envKey.apiKey;
                await setEphoneApiKey(resolvedApiKey, params.agentDir);
                hasCredential = true;
            }
        }
        if (!hasCredential) {
            await params.prompter.note([
                "ePhone AI 是模型聚合平台，支持 Claude、GPT、MiniMax、Kimi 等主流模型。",
                "获取 API Key: https://platform.ephone.ai",
                "配置文档: https://clawd.org.cn/providers/ephone.html",
            ].join("\n"), "ePhone AI");
            const key = await params.prompter.text({
                message: "输入 ePhone AI API key",
                validate: validateApiKeyInput,
            });
            resolvedApiKey = normalizeApiKeyInput(String(key));
            await setEphoneApiKey(resolvedApiKey, params.agentDir);
        }
        nextConfig = applyAuthProfileConfig(nextConfig, {
            profileId: "ephone:default",
            provider: "ephone",
            mode: "api_key",
        });
        {
            // Try dynamic model discovery from ePhone API; fall back to static list
            const discovered = await discoverOpenAICompatibleModels({
                baseUrl: EPHONE_BASE_URL,
                apiKey: resolvedApiKey,
            });
            const staticOptions = EPHONE_MODELS.map((m) => ({ value: m.value, label: m.label }));
            const pinnedIds = EPHONE_MODELS.filter((m) => m.value !== "custom").map((m) => m.value);
            const modelOptions = discovered
                ? buildDiscoveredModelOptions({
                    discovered,
                    pinnedIds,
                    customLabel: "手动输入模型 ID       （查看完整列表: platform.ephone.ai/models）",
                })
                : staticOptions;
            const selection = await params.prompter.select({
                message: discovered
                    ? `选择 ePhone AI 模型（已从 API 获取 ${discovered.length} 个可用模型）`
                    : "选择 ePhone AI 模型",
                options: modelOptions,
                initialValue: EPHONE_DEFAULT_MODEL_ID,
            });
            let modelId;
            if (selection === "custom") {
                await params.prompter.note("查看完整模型列表: https://platform.ephone.ai/models", "ePhone AI 模型");
                const input = await params.prompter.text({
                    message: "输入模型 ID",
                    validate: (val) => (String(val).trim().length > 0 ? undefined : "模型 ID 不能为空"),
                });
                modelId = typeof input === "string" ? input.trim() : EPHONE_DEFAULT_MODEL_ID;
            }
            else {
                modelId = String(selection);
            }
            const modelRef = `ephone/${modelId}`;
            const applied = await applyDefaultModelChoice({
                config: nextConfig,
                setDefaultModel: params.setDefaultModel,
                defaultModel: modelRef,
                applyDefaultConfig: (cfg) => applyEphoneConfig(cfg, modelId),
                applyProviderConfig: (cfg) => applyEphoneProviderConfig(cfg, modelId),
                noteDefault: modelRef,
                noteAgentModel,
                prompter: params.prompter,
            });
            nextConfig = applied.config;
            agentModelOverride = applied.agentModelOverride ?? modelRef;
        }
        return { config: nextConfig, agentModelOverride };
    }
    if (authChoice === "openrouter-api-key") {
        const store = ensureAuthProfileStore(params.agentDir, {
            allowKeychainPrompt: false,
        });
        const profileOrder = resolveAuthProfileOrder({
            cfg: nextConfig,
            store,
            provider: "openrouter",
        });
        const existingProfileId = profileOrder.find((profileId) => Boolean(store.profiles[profileId]));
        const existingCred = existingProfileId ? store.profiles[existingProfileId] : undefined;
        let profileId = "openrouter:default";
        let mode = "api_key";
        let hasCredential = false;
        if (existingProfileId && existingCred?.type) {
            profileId = existingProfileId;
            mode =
                existingCred.type === "oauth"
                    ? "oauth"
                    : existingCred.type === "token"
                        ? "token"
                        : "api_key";
            hasCredential = true;
        }
        if (!hasCredential && params.opts?.token && params.opts?.tokenProvider === "openrouter") {
            await setOpenrouterApiKey(normalizeApiKeyInput(params.opts.token), params.agentDir);
            hasCredential = true;
        }
        if (!hasCredential) {
            const envKey = resolveEnvApiKey("openrouter");
            if (envKey) {
                const useExisting = await params.prompter.confirm({
                    message: `Use existing OPENROUTER_API_KEY (${envKey.source}, ${formatApiKeyPreview(envKey.apiKey)})?`,
                    initialValue: true,
                });
                if (useExisting) {
                    await setOpenrouterApiKey(envKey.apiKey, params.agentDir);
                    hasCredential = true;
                }
            }
        }
        if (!hasCredential) {
            const key = await params.prompter.text({
                message: "Enter OpenRouter API key",
                validate: validateApiKeyInput,
            });
            await setOpenrouterApiKey(normalizeApiKeyInput(String(key)), params.agentDir);
            hasCredential = true;
        }
        if (hasCredential) {
            nextConfig = applyAuthProfileConfig(nextConfig, {
                profileId,
                provider: "openrouter",
                mode,
            });
        }
        {
            const applied = await applyDefaultModelChoice({
                config: nextConfig,
                setDefaultModel: params.setDefaultModel,
                defaultModel: OPENROUTER_DEFAULT_MODEL_REF,
                applyDefaultConfig: applyOpenrouterConfig,
                applyProviderConfig: applyOpenrouterProviderConfig,
                noteDefault: OPENROUTER_DEFAULT_MODEL_REF,
                noteAgentModel,
                prompter: params.prompter,
            });
            nextConfig = applied.config;
            agentModelOverride = applied.agentModelOverride ?? agentModelOverride;
        }
        return { config: nextConfig, agentModelOverride };
    }
    // 新增：硅基流动 API Key 认证与默认模型配置
    if (authChoice === "siliconflow-api-key") {
        let hasCredential = false;
        let resolvedApiKey = "";
        if (!hasCredential && params.opts?.token && params.opts?.tokenProvider === "siliconflow") {
            resolvedApiKey = normalizeApiKeyInput(params.opts.token);
            await setSiliconflowApiKey(resolvedApiKey, params.agentDir);
            hasCredential = true;
        }
        const envKey = resolveEnvApiKey("siliconflow");
        if (envKey && !hasCredential) {
            const useExisting = await params.prompter.confirm({
                message: `使用已有 SILICONFLOW_API_KEY (${envKey.source}, ${formatApiKeyPreview(envKey.apiKey)})？`,
                initialValue: true,
            });
            if (useExisting) {
                resolvedApiKey = envKey.apiKey;
                await setSiliconflowApiKey(resolvedApiKey, params.agentDir);
                hasCredential = true;
            }
        }
        if (!hasCredential) {
            const key = await params.prompter.text({
                message: "输入硅基流动 (SiliconFlow) API key",
                validate: validateApiKeyInput,
            });
            resolvedApiKey = normalizeApiKeyInput(String(key));
            await setSiliconflowApiKey(resolvedApiKey, params.agentDir);
        }
        nextConfig = applyAuthProfileConfig(nextConfig, {
            profileId: "siliconflow:default",
            provider: "siliconflow",
            mode: "api_key",
        });
        {
            const discovered = await discoverOpenAICompatibleModels({
                baseUrl: SILICONFLOW_BASE_URL,
                apiKey: resolvedApiKey,
            });
            if (discovered) {
                const options = buildDiscoveredModelOptions({ discovered, customLabel: "手动输入模型 ID" });
                const selection = await params.prompter.select({
                    message: `选择硅基流动模型（已获取 ${discovered.length} 个可用模型）`,
                    options,
                });
                let modelId;
                if (selection === "custom") {
                    const input = await params.prompter.text({
                        message: "输入模型 ID",
                        validate: (val) => (String(val).trim().length > 0 ? undefined : "模型 ID 不能为空"),
                    });
                    modelId = typeof input === "string" ? input.trim() : "Qwen/Qwen2.5-32B-Instruct";
                }
                else {
                    modelId = String(selection);
                }
                const modelRef = `siliconflow/${modelId}`;
                const applied = await applyDefaultModelChoice({
                    config: nextConfig,
                    setDefaultModel: params.setDefaultModel,
                    defaultModel: modelRef,
                    applyDefaultConfig: (cfg) => applySiliconflowConfig(cfg),
                    applyProviderConfig: (cfg) => applySiliconflowProviderConfig(cfg),
                    noteDefault: modelRef,
                    noteAgentModel,
                    prompter: params.prompter,
                });
                nextConfig = applied.config;
                agentModelOverride = applied.agentModelOverride ?? modelRef;
            }
            else {
                const applied = await applyDefaultModelChoice({
                    config: nextConfig,
                    setDefaultModel: params.setDefaultModel,
                    defaultModel: SILICONFLOW_DEFAULT_MODEL_REF,
                    applyDefaultConfig: applySiliconflowConfig,
                    applyProviderConfig: applySiliconflowProviderConfig,
                    noteDefault: SILICONFLOW_DEFAULT_MODEL_REF,
                    noteAgentModel,
                    prompter: params.prompter,
                });
                nextConfig = applied.config;
                agentModelOverride = applied.agentModelOverride ?? agentModelOverride;
            }
        }
        return { config: nextConfig, agentModelOverride };
    }
    // 新增：阿里云百炼 (DashScope) API Key 认证与默认模型配置
    if (authChoice === "dashscope-api-key") {
        let hasCredential = false;
        let resolvedApiKey = "";
        if (!hasCredential && params.opts?.token && params.opts?.tokenProvider === "dashscope") {
            resolvedApiKey = normalizeApiKeyInput(params.opts.token);
            await setDashscopeApiKey(resolvedApiKey, params.agentDir);
            hasCredential = true;
        }
        const envKey = resolveEnvApiKey("dashscope");
        if (envKey && !hasCredential) {
            const useExisting = await params.prompter.confirm({
                message: `使用已有 DASHSCOPE_API_KEY (${envKey.source}, ${formatApiKeyPreview(envKey.apiKey)})？`,
                initialValue: true,
            });
            if (useExisting) {
                resolvedApiKey = envKey.apiKey;
                await setDashscopeApiKey(resolvedApiKey, params.agentDir);
                hasCredential = true;
            }
        }
        if (!hasCredential) {
            const key = await params.prompter.text({
                message: "输入阿里云百炼 (DashScope) API key",
                validate: validateApiKeyInput,
            });
            resolvedApiKey = normalizeApiKeyInput(String(key));
            await setDashscopeApiKey(resolvedApiKey, params.agentDir);
        }
        nextConfig = applyAuthProfileConfig(nextConfig, {
            profileId: "dashscope:default",
            provider: "dashscope",
            mode: "api_key",
        });
        {
            const discovered = await discoverOpenAICompatibleModels({
                baseUrl: DASHSCOPE_BASE_URL,
                apiKey: resolvedApiKey,
            });
            if (discovered) {
                const options = buildDiscoveredModelOptions({ discovered, customLabel: "手动输入模型 ID" });
                const selection = await params.prompter.select({
                    message: `选择阿里云百炼模型（已获取 ${discovered.length} 个可用模型）`,
                    options,
                });
                let modelId;
                if (selection === "custom") {
                    const input = await params.prompter.text({
                        message: "输入模型 ID",
                        validate: (val) => (String(val).trim().length > 0 ? undefined : "模型 ID 不能为空"),
                    });
                    modelId = typeof input === "string" ? input.trim() : "qwen-plus";
                }
                else {
                    modelId = String(selection);
                }
                const modelRef = `dashscope/${modelId}`;
                const applied = await applyDefaultModelChoice({
                    config: nextConfig,
                    setDefaultModel: params.setDefaultModel,
                    defaultModel: modelRef,
                    applyDefaultConfig: (cfg) => applyDashscopeConfig(cfg),
                    applyProviderConfig: (cfg) => applyDashscopeProviderConfig(cfg),
                    noteDefault: modelRef,
                    noteAgentModel,
                    prompter: params.prompter,
                });
                nextConfig = applied.config;
                agentModelOverride = applied.agentModelOverride ?? modelRef;
            }
            else {
                const applied = await applyDefaultModelChoice({
                    config: nextConfig,
                    setDefaultModel: params.setDefaultModel,
                    defaultModel: DASHSCOPE_DEFAULT_MODEL_REF,
                    applyDefaultConfig: applyDashscopeConfig,
                    applyProviderConfig: applyDashscopeProviderConfig,
                    noteDefault: DASHSCOPE_DEFAULT_MODEL_REF,
                    noteAgentModel,
                    prompter: params.prompter,
                });
                nextConfig = applied.config;
                agentModelOverride = applied.agentModelOverride ?? agentModelOverride;
            }
        }
        return { config: nextConfig, agentModelOverride };
    }
    // 新增：阿里云百炼 (Coding Plan) API Key 认证与默认模型配置
    if (authChoice === "dashscope-coding-plan-api-key") {
        let hasCredential = false;
        let resolvedApiKey = "";
        if (!hasCredential &&
            params.opts?.token &&
            params.opts?.tokenProvider === "dashscope-coding-plan") {
            resolvedApiKey = normalizeApiKeyInput(params.opts.token);
            await setDashscopeCodingPlanApiKey(resolvedApiKey, params.agentDir);
            hasCredential = true;
        }
        const envKey = resolveEnvApiKey("dashscope-coding-plan");
        if (envKey && !hasCredential) {
            const useExisting = await params.prompter.confirm({
                message: `使用已有 DASHSCOPE_CODING_PLAN_API_KEY (${envKey.source}, ${formatApiKeyPreview(envKey.apiKey)})？`,
                initialValue: true,
            });
            if (useExisting) {
                resolvedApiKey = envKey.apiKey;
                await setDashscopeCodingPlanApiKey(resolvedApiKey, params.agentDir);
                hasCredential = true;
            }
        }
        if (!hasCredential) {
            const key = await params.prompter.text({
                message: "输入阿里云百炼 (Coding Plan) API key",
                validate: validateApiKeyInput,
            });
            resolvedApiKey = normalizeApiKeyInput(String(key));
            await setDashscopeCodingPlanApiKey(resolvedApiKey, params.agentDir);
        }
        nextConfig = applyAuthProfileConfig(nextConfig, {
            profileId: "dashscope-coding-plan:default",
            provider: "dashscope-coding-plan",
            mode: "api_key",
        });
        {
            const discovered = await discoverOpenAICompatibleModels({
                baseUrl: DASHSCOPE_CODING_PLAN_BASE_URL,
                apiKey: resolvedApiKey,
            });
            const staticOptions = DASHSCOPE_CODING_PLAN_MODELS.map((m) => ({
                value: m.value,
                label: m.label,
            }));
            const pinnedIds = DASHSCOPE_CODING_PLAN_MODELS.filter((m) => m.value !== "custom").map((m) => m.value);
            const modelOptions = discovered
                ? buildDiscoveredModelOptions({ discovered, pinnedIds, customLabel: "手动输入模型 ID" })
                : staticOptions;
            const selection = await params.prompter.select({
                message: discovered
                    ? `选择阿里云百炼 (Coding Plan) 模型（已获取 ${discovered.length} 个可用模型）`
                    : "选择阿里云百炼 (Coding Plan) 模型",
                options: modelOptions,
                initialValue: DASHSCOPE_CODING_PLAN_DEFAULT_MODEL_ID,
            });
            let modelId;
            if (selection === "custom") {
                const input = await params.prompter.text({
                    message: "输入模型 ID",
                    validate: (val) => (String(val).trim().length > 0 ? undefined : "模型 ID 不能为空"),
                });
                modelId = typeof input === "string" ? input.trim() : DASHSCOPE_CODING_PLAN_DEFAULT_MODEL_ID;
            }
            else {
                modelId = String(selection);
            }
            const modelRef = `dashscope-coding-plan/${modelId}`;
            const applied = await applyDefaultModelChoice({
                config: nextConfig,
                setDefaultModel: params.setDefaultModel,
                defaultModel: modelRef,
                applyDefaultConfig: (cfg) => applyDashscopeCodingPlanConfig(cfg, modelId),
                applyProviderConfig: (cfg) => applyDashscopeCodingPlanProviderConfig(cfg, modelId),
                noteDefault: modelRef,
                noteAgentModel,
                prompter: params.prompter,
            });
            nextConfig = applied.config;
            agentModelOverride = applied.agentModelOverride ?? modelRef;
        }
        return { config: nextConfig, agentModelOverride };
    }
    // 新增：Kimi Coding Plan API Key 认证与默认模型配置
    if (authChoice === "moonshot-coding-plan-api-key") {
        let hasCredential = false;
        if (!hasCredential &&
            params.opts?.token &&
            params.opts?.tokenProvider === "moonshot-coding-plan") {
            await setMoonshotCodingPlanApiKey(normalizeApiKeyInput(params.opts.token), params.agentDir);
            hasCredential = true;
        }
        const envKey = resolveEnvApiKey("moonshot-coding-plan");
        if (envKey && !hasCredential) {
            const useExisting = await params.prompter.confirm({
                message: `使用已有 MOONSHOT_CODING_PLAN_API_KEY (${envKey.source}, ${formatApiKeyPreview(envKey.apiKey)})？`,
                initialValue: true,
            });
            if (useExisting) {
                await setMoonshotCodingPlanApiKey(envKey.apiKey, params.agentDir);
                hasCredential = true;
            }
        }
        if (!hasCredential) {
            await params.prompter.note([
                "Kimi Coding 使用 OpenAI 兼容协议的专用端点。",
                "在 Kimi 会员页面获取 API Key：https://kimi.moonshot.cn/",
            ].join("\n"), "Kimi Coding Plan");
            const key = await params.prompter.text({
                message: "输入 Kimi Coding API key",
                validate: validateApiKeyInput,
            });
            await setMoonshotCodingPlanApiKey(normalizeApiKeyInput(String(key)), params.agentDir);
        }
        nextConfig = applyAuthProfileConfig(nextConfig, {
            profileId: "moonshot-coding-plan:default",
            provider: "moonshot-coding-plan",
            mode: "api_key",
        });
        {
            const useDefaultModel = await params.prompter.confirm({
                message: `使用默认模型 (${MOONSHOT_CODING_PLAN_DEFAULT_MODEL_ID})？`,
                initialValue: true,
            });
            let modelId = MOONSHOT_CODING_PLAN_DEFAULT_MODEL_ID;
            if (!useDefaultModel) {
                const input = await params.prompter.text({
                    message: "输入模型 ID",
                    validate: (val) => (String(val).trim().length > 0 ? undefined : "模型 ID 不能为空"),
                });
                if (typeof input === "string") {
                    modelId = input.trim();
                }
            }
            const modelRef = `moonshot-coding-plan/${modelId}`;
            const applied = await applyDefaultModelChoice({
                config: nextConfig,
                setDefaultModel: params.setDefaultModel,
                defaultModel: modelRef,
                applyDefaultConfig: (cfg) => applyMoonshotCodingPlanConfig(cfg, modelId),
                applyProviderConfig: (cfg) => applyMoonshotCodingPlanProviderConfig(cfg, modelId),
                noteDefault: modelRef,
                noteAgentModel,
                prompter: params.prompter,
            });
            nextConfig = applied.config;
            agentModelOverride = applied.agentModelOverride ?? modelRef;
        }
        return { config: nextConfig, agentModelOverride };
    }
    // 新增：DeepSeek API Key 认证与默认模型配置
    if (authChoice === "deepseek-api-key") {
        let hasCredential = false;
        let resolvedApiKey = "";
        if (!hasCredential && params.opts?.token && params.opts?.tokenProvider === "deepseek") {
            resolvedApiKey = normalizeApiKeyInput(params.opts.token);
            await setDeepseekApiKey(resolvedApiKey, params.agentDir);
            hasCredential = true;
        }
        const envKey = resolveEnvApiKey("deepseek");
        if (envKey && !hasCredential) {
            const useExisting = await params.prompter.confirm({
                message: `使用已有 DEEPSEEK_API_KEY (${envKey.source}, ${formatApiKeyPreview(envKey.apiKey)})？`,
                initialValue: true,
            });
            if (useExisting) {
                resolvedApiKey = envKey.apiKey;
                await setDeepseekApiKey(resolvedApiKey, params.agentDir);
                hasCredential = true;
            }
        }
        if (!hasCredential) {
            const key = await params.prompter.text({
                message: "输入 DeepSeek API key",
                validate: validateApiKeyInput,
            });
            resolvedApiKey = normalizeApiKeyInput(String(key));
            await setDeepseekApiKey(resolvedApiKey, params.agentDir);
        }
        nextConfig = applyAuthProfileConfig(nextConfig, {
            profileId: "deepseek:default",
            provider: "deepseek",
            mode: "api_key",
        });
        {
            const discovered = await discoverOpenAICompatibleModels({
                baseUrl: DEEPSEEK_BASE_URL,
                apiKey: resolvedApiKey,
            });
            if (discovered) {
                const options = buildDiscoveredModelOptions({ discovered, customLabel: "手动输入模型 ID" });
                const selection = await params.prompter.select({
                    message: `选择 DeepSeek 模型（已获取 ${discovered.length} 个可用模型）`,
                    options,
                });
                let modelId;
                if (selection === "custom") {
                    const input = await params.prompter.text({
                        message: "输入模型 ID",
                        validate: (val) => (String(val).trim().length > 0 ? undefined : "模型 ID 不能为空"),
                    });
                    modelId = typeof input === "string" ? input.trim() : "deepseek-chat";
                }
                else {
                    modelId = String(selection);
                }
                const modelRef = `deepseek/${modelId}`;
                const applied = await applyDefaultModelChoice({
                    config: nextConfig,
                    setDefaultModel: params.setDefaultModel,
                    defaultModel: modelRef,
                    applyDefaultConfig: (cfg) => applyDeepseekConfig(cfg),
                    applyProviderConfig: (cfg) => applyDeepseekProviderConfig(cfg),
                    noteDefault: modelRef,
                    noteAgentModel,
                    prompter: params.prompter,
                });
                nextConfig = applied.config;
                agentModelOverride = applied.agentModelOverride ?? modelRef;
            }
            else {
                const applied = await applyDefaultModelChoice({
                    config: nextConfig,
                    setDefaultModel: params.setDefaultModel,
                    defaultModel: DEEPSEEK_DEFAULT_MODEL_REF,
                    applyDefaultConfig: applyDeepseekConfig,
                    applyProviderConfig: applyDeepseekProviderConfig,
                    noteDefault: DEEPSEEK_DEFAULT_MODEL_REF,
                    noteAgentModel,
                    prompter: params.prompter,
                });
                nextConfig = applied.config;
                agentModelOverride = applied.agentModelOverride ?? agentModelOverride;
            }
        }
        return { config: nextConfig, agentModelOverride };
    }
    if (authChoice === "ai-gateway-api-key") {
        let hasCredential = false;
        if (!hasCredential &&
            params.opts?.token &&
            params.opts?.tokenProvider === "vercel-ai-gateway") {
            await setVercelAiGatewayApiKey(normalizeApiKeyInput(params.opts.token), params.agentDir);
            hasCredential = true;
        }
        const envKey = resolveEnvApiKey("vercel-ai-gateway");
        if (envKey) {
            const useExisting = await params.prompter.confirm({
                message: `Use existing AI_GATEWAY_API_KEY (${envKey.source}, ${formatApiKeyPreview(envKey.apiKey)})?`,
                initialValue: true,
            });
            if (useExisting) {
                await setVercelAiGatewayApiKey(envKey.apiKey, params.agentDir);
                hasCredential = true;
            }
        }
        if (!hasCredential) {
            const key = await params.prompter.text({
                message: "Enter Vercel AI Gateway API key",
                validate: validateApiKeyInput,
            });
            await setVercelAiGatewayApiKey(normalizeApiKeyInput(String(key)), params.agentDir);
        }
        nextConfig = applyAuthProfileConfig(nextConfig, {
            profileId: "vercel-ai-gateway:default",
            provider: "vercel-ai-gateway",
            mode: "api_key",
        });
        {
            const applied = await applyDefaultModelChoice({
                config: nextConfig,
                setDefaultModel: params.setDefaultModel,
                defaultModel: VERCEL_AI_GATEWAY_DEFAULT_MODEL_REF,
                applyDefaultConfig: applyVercelAiGatewayConfig,
                applyProviderConfig: applyVercelAiGatewayProviderConfig,
                noteDefault: VERCEL_AI_GATEWAY_DEFAULT_MODEL_REF,
                noteAgentModel,
                prompter: params.prompter,
            });
            nextConfig = applied.config;
            agentModelOverride = applied.agentModelOverride ?? agentModelOverride;
        }
        return { config: nextConfig, agentModelOverride };
    }
    if (authChoice === "cloudflare-ai-gateway-api-key") {
        let hasCredential = false;
        let accountId = params.opts?.cloudflareAiGatewayAccountId?.trim() ?? "";
        let gatewayId = params.opts?.cloudflareAiGatewayGatewayId?.trim() ?? "";
        const ensureAccountGateway = async () => {
            if (!accountId) {
                const value = await params.prompter.text({
                    message: "Enter Cloudflare Account ID",
                    validate: (val) => (String(val).trim() ? undefined : "Account ID is required"),
                });
                accountId = String(value).trim();
            }
            if (!gatewayId) {
                const value = await params.prompter.text({
                    message: "Enter Cloudflare AI Gateway ID",
                    validate: (val) => (String(val).trim() ? undefined : "Gateway ID is required"),
                });
                gatewayId = String(value).trim();
            }
        };
        const optsApiKey = normalizeApiKeyInput(params.opts?.cloudflareAiGatewayApiKey ?? "");
        if (!hasCredential && accountId && gatewayId && optsApiKey) {
            await setCloudflareAiGatewayConfig(accountId, gatewayId, optsApiKey, params.agentDir);
            hasCredential = true;
        }
        const envKey = resolveEnvApiKey("cloudflare-ai-gateway");
        if (!hasCredential && envKey) {
            const useExisting = await params.prompter.confirm({
                message: `Use existing CLOUDFLARE_AI_GATEWAY_API_KEY (${envKey.source}, ${formatApiKeyPreview(envKey.apiKey)})?`,
                initialValue: true,
            });
            if (useExisting) {
                await ensureAccountGateway();
                await setCloudflareAiGatewayConfig(accountId, gatewayId, normalizeApiKeyInput(envKey.apiKey), params.agentDir);
                hasCredential = true;
            }
        }
        if (!hasCredential && optsApiKey) {
            await ensureAccountGateway();
            await setCloudflareAiGatewayConfig(accountId, gatewayId, optsApiKey, params.agentDir);
            hasCredential = true;
        }
        if (!hasCredential) {
            await ensureAccountGateway();
            const key = await params.prompter.text({
                message: "Enter Cloudflare AI Gateway API key",
                validate: validateApiKeyInput,
            });
            await setCloudflareAiGatewayConfig(accountId, gatewayId, normalizeApiKeyInput(String(key)), params.agentDir);
            hasCredential = true;
        }
        if (hasCredential) {
            nextConfig = applyAuthProfileConfig(nextConfig, {
                profileId: "cloudflare-ai-gateway:default",
                provider: "cloudflare-ai-gateway",
                mode: "api_key",
            });
        }
        {
            const applied = await applyDefaultModelChoice({
                config: nextConfig,
                setDefaultModel: params.setDefaultModel,
                defaultModel: CLOUDFLARE_AI_GATEWAY_DEFAULT_MODEL_REF,
                applyDefaultConfig: (cfg) => applyCloudflareAiGatewayConfig(cfg, {
                    accountId: accountId || params.opts?.cloudflareAiGatewayAccountId,
                    gatewayId: gatewayId || params.opts?.cloudflareAiGatewayGatewayId,
                }),
                applyProviderConfig: (cfg) => applyCloudflareAiGatewayProviderConfig(cfg, {
                    accountId: accountId || params.opts?.cloudflareAiGatewayAccountId,
                    gatewayId: gatewayId || params.opts?.cloudflareAiGatewayGatewayId,
                }),
                noteDefault: CLOUDFLARE_AI_GATEWAY_DEFAULT_MODEL_REF,
                noteAgentModel,
                prompter: params.prompter,
            });
            nextConfig = applied.config;
            agentModelOverride = applied.agentModelOverride ?? agentModelOverride;
        }
        return { config: nextConfig, agentModelOverride };
    }
    if (authChoice === "moonshot-api-key") {
        let hasCredential = false;
        let resolvedApiKey = "";
        if (!hasCredential && params.opts?.token && params.opts?.tokenProvider === "moonshot") {
            resolvedApiKey = normalizeApiKeyInput(params.opts.token);
            await setMoonshotApiKey(resolvedApiKey, params.agentDir);
            hasCredential = true;
        }
        const envKey = resolveEnvApiKey("moonshot");
        if (envKey && !hasCredential) {
            const useExisting = await params.prompter.confirm({
                message: `使用已有 MOONSHOT_API_KEY (${envKey.source}, ${formatApiKeyPreview(envKey.apiKey)})？`,
                initialValue: true,
            });
            if (useExisting) {
                resolvedApiKey = envKey.apiKey;
                await setMoonshotApiKey(resolvedApiKey, params.agentDir);
                hasCredential = true;
            }
        }
        if (!hasCredential) {
            const key = await params.prompter.text({
                message: "输入 Moonshot API key",
                validate: validateApiKeyInput,
            });
            resolvedApiKey = normalizeApiKeyInput(String(key));
            await setMoonshotApiKey(resolvedApiKey, params.agentDir);
        }
        nextConfig = applyAuthProfileConfig(nextConfig, {
            profileId: "moonshot:default",
            provider: "moonshot",
            mode: "api_key",
        });
        {
            const discovered = await discoverOpenAICompatibleModels({
                baseUrl: MOONSHOT_BASE_URL,
                apiKey: resolvedApiKey,
            });
            if (discovered) {
                const options = buildDiscoveredModelOptions({ discovered, customLabel: "手动输入模型 ID" });
                const selection = await params.prompter.select({
                    message: `选择 Moonshot 模型（已获取 ${discovered.length} 个可用模型）`,
                    options,
                    initialValue: "kimi-k2.5",
                });
                let modelId;
                if (selection === "custom") {
                    const input = await params.prompter.text({
                        message: "输入模型 ID",
                        validate: (val) => (String(val).trim().length > 0 ? undefined : "模型 ID 不能为空"),
                    });
                    modelId = typeof input === "string" ? input.trim() : "kimi-k2.5";
                }
                else {
                    modelId = String(selection);
                }
                const modelRef = `moonshot/${modelId}`;
                nextConfig = applyMoonshotProviderConfig(nextConfig);
                nextConfig = {
                    ...nextConfig,
                    agents: {
                        ...nextConfig.agents,
                        defaults: { ...nextConfig.agents?.defaults, model: { primary: modelRef } },
                    },
                };
                if (params.setDefaultModel) {
                    await params.prompter.note(`Default model set to ${modelRef}`, "Model configured");
                }
                else {
                    await noteAgentModel(modelRef);
                    agentModelOverride = modelRef;
                }
            }
            else {
                const applied = await applyDefaultModelChoice({
                    config: nextConfig,
                    setDefaultModel: params.setDefaultModel,
                    defaultModel: MOONSHOT_DEFAULT_MODEL_REF,
                    applyDefaultConfig: applyMoonshotConfig,
                    applyProviderConfig: applyMoonshotProviderConfig,
                    noteAgentModel,
                    prompter: params.prompter,
                });
                nextConfig = applied.config;
                agentModelOverride = applied.agentModelOverride ?? agentModelOverride;
            }
        }
        return { config: nextConfig, agentModelOverride };
    }
    if (authChoice === "moonshot-api-key-cn") {
        let hasCredential = false;
        let resolvedApiKey = "";
        if (!hasCredential && params.opts?.token && params.opts?.tokenProvider === "moonshot") {
            resolvedApiKey = normalizeApiKeyInput(params.opts.token);
            await setMoonshotApiKey(resolvedApiKey, params.agentDir);
            hasCredential = true;
        }
        const envKey = resolveEnvApiKey("moonshot");
        if (envKey && !hasCredential) {
            const useExisting = await params.prompter.confirm({
                message: `使用已有 MOONSHOT_API_KEY (${envKey.source}, ${formatApiKeyPreview(envKey.apiKey)})？`,
                initialValue: true,
            });
            if (useExisting) {
                resolvedApiKey = envKey.apiKey;
                await setMoonshotApiKey(resolvedApiKey, params.agentDir);
                hasCredential = true;
            }
        }
        if (!hasCredential) {
            const key = await params.prompter.text({
                message: "输入 Moonshot API key (.cn)",
                validate: validateApiKeyInput,
            });
            resolvedApiKey = normalizeApiKeyInput(String(key));
            await setMoonshotApiKey(resolvedApiKey, params.agentDir);
        }
        nextConfig = applyAuthProfileConfig(nextConfig, {
            profileId: "moonshot:default",
            provider: "moonshot",
            mode: "api_key",
        });
        {
            const discovered = await discoverOpenAICompatibleModels({
                baseUrl: MOONSHOT_CN_BASE_URL,
                apiKey: resolvedApiKey,
            });
            if (discovered) {
                const options = buildDiscoveredModelOptions({ discovered, customLabel: "手动输入模型 ID" });
                const selection = await params.prompter.select({
                    message: `选择 Moonshot 模型（已获取 ${discovered.length} 个可用模型）`,
                    options,
                    initialValue: "kimi-k2.5",
                });
                let modelId;
                if (selection === "custom") {
                    const input = await params.prompter.text({
                        message: "输入模型 ID",
                        validate: (val) => (String(val).trim().length > 0 ? undefined : "模型 ID 不能为空"),
                    });
                    modelId = typeof input === "string" ? input.trim() : "kimi-k2.5";
                }
                else {
                    modelId = String(selection);
                }
                const modelRef = `moonshot/${modelId}`;
                nextConfig = applyMoonshotCnProviderConfig(nextConfig);
                nextConfig = {
                    ...nextConfig,
                    agents: {
                        ...nextConfig.agents,
                        defaults: { ...nextConfig.agents?.defaults, model: { primary: modelRef } },
                    },
                };
                if (params.setDefaultModel) {
                    await params.prompter.note(`Default model set to ${modelRef}`, "Model configured");
                }
                else {
                    await noteAgentModel(modelRef);
                    agentModelOverride = modelRef;
                }
            }
            else {
                const applied = await applyDefaultModelChoice({
                    config: nextConfig,
                    setDefaultModel: params.setDefaultModel,
                    defaultModel: MOONSHOT_DEFAULT_MODEL_REF,
                    applyDefaultConfig: applyMoonshotCnConfig,
                    applyProviderConfig: applyMoonshotCnProviderConfig,
                    noteAgentModel,
                    prompter: params.prompter,
                });
                nextConfig = applied.config;
                agentModelOverride = applied.agentModelOverride ?? agentModelOverride;
            }
        }
        return { config: nextConfig, agentModelOverride };
    }
    if (authChoice === "kimi-code-api-key") {
        let hasCredential = false;
        const tokenProvider = params.opts?.tokenProvider?.trim().toLowerCase();
        if (!hasCredential &&
            params.opts?.token &&
            (tokenProvider === "kimi-code" || tokenProvider === "kimi-coding")) {
            await setKimiCodingApiKey(normalizeApiKeyInput(params.opts.token), params.agentDir);
            hasCredential = true;
        }
        if (!hasCredential) {
            await params.prompter.note([
                "Kimi Coding uses a dedicated endpoint and API key.",
                "Get your API key at: https://www.kimi.com/code/en",
            ].join("\n"), "Kimi Coding");
        }
        const envKey = resolveEnvApiKey("kimi-coding");
        if (envKey) {
            const useExisting = await params.prompter.confirm({
                message: `Use existing KIMI_API_KEY (${envKey.source}, ${formatApiKeyPreview(envKey.apiKey)})?`,
                initialValue: true,
            });
            if (useExisting) {
                await setKimiCodingApiKey(envKey.apiKey, params.agentDir);
                hasCredential = true;
            }
        }
        if (!hasCredential) {
            const key = await params.prompter.text({
                message: "Enter Kimi Coding API key",
                validate: validateApiKeyInput,
            });
            await setKimiCodingApiKey(normalizeApiKeyInput(String(key)), params.agentDir);
        }
        nextConfig = applyAuthProfileConfig(nextConfig, {
            profileId: "kimi-coding:default",
            provider: "kimi-coding",
            mode: "api_key",
        });
        {
            const applied = await applyDefaultModelChoice({
                config: nextConfig,
                setDefaultModel: params.setDefaultModel,
                defaultModel: KIMI_CODING_MODEL_REF,
                applyDefaultConfig: applyKimiCodingConfig,
                applyProviderConfig: applyKimiCodingProviderConfig,
                noteDefault: KIMI_CODING_MODEL_REF,
                noteAgentModel,
                prompter: params.prompter,
            });
            nextConfig = applied.config;
            agentModelOverride = applied.agentModelOverride ?? agentModelOverride;
        }
        return { config: nextConfig, agentModelOverride };
    }
    if (authChoice === "gemini-api-key") {
        let hasCredential = false;
        if (!hasCredential && params.opts?.token && params.opts?.tokenProvider === "google") {
            await setGeminiApiKey(normalizeApiKeyInput(params.opts.token), params.agentDir);
            hasCredential = true;
        }
        const envKey = resolveEnvApiKey("google");
        if (envKey) {
            const useExisting = await params.prompter.confirm({
                message: `Use existing GEMINI_API_KEY (${envKey.source}, ${formatApiKeyPreview(envKey.apiKey)})?`,
                initialValue: true,
            });
            if (useExisting) {
                await setGeminiApiKey(envKey.apiKey, params.agentDir);
                hasCredential = true;
            }
        }
        if (!hasCredential) {
            const key = await params.prompter.text({
                message: "Enter Gemini API key",
                validate: validateApiKeyInput,
            });
            await setGeminiApiKey(normalizeApiKeyInput(String(key)), params.agentDir);
        }
        nextConfig = applyAuthProfileConfig(nextConfig, {
            profileId: "google:default",
            provider: "google",
            mode: "api_key",
        });
        if (params.setDefaultModel) {
            const applied = applyGoogleGeminiModelDefault(nextConfig);
            nextConfig = applied.next;
            if (applied.changed) {
                await params.prompter.note(`Default model set to ${GOOGLE_GEMINI_DEFAULT_MODEL}`, "Model configured");
            }
        }
        else {
            agentModelOverride = GOOGLE_GEMINI_DEFAULT_MODEL;
            await noteAgentModel(GOOGLE_GEMINI_DEFAULT_MODEL);
        }
        return { config: nextConfig, agentModelOverride };
    }
    if (authChoice === "zai-api-key") {
        let hasCredential = false;
        let resolvedApiKey = "";
        if (!hasCredential && params.opts?.token && params.opts?.tokenProvider === "zai") {
            resolvedApiKey = normalizeApiKeyInput(params.opts.token);
            await setZaiApiKey(resolvedApiKey, params.agentDir);
            hasCredential = true;
        }
        const envKey = resolveEnvApiKey("zai");
        if (envKey && !hasCredential) {
            const useExisting = await params.prompter.confirm({
                message: `使用已有 ZAI_API_KEY (${envKey.source}, ${formatApiKeyPreview(envKey.apiKey)})？`,
                initialValue: true,
            });
            if (useExisting) {
                resolvedApiKey = envKey.apiKey;
                await setZaiApiKey(resolvedApiKey, params.agentDir);
                hasCredential = true;
            }
        }
        if (!hasCredential) {
            const key = await params.prompter.text({
                message: "输入 Z.AI API key",
                validate: validateApiKeyInput,
            });
            resolvedApiKey = normalizeApiKeyInput(String(key));
            await setZaiApiKey(resolvedApiKey, params.agentDir);
        }
        nextConfig = applyAuthProfileConfig(nextConfig, {
            profileId: "zai:default",
            provider: "zai",
            mode: "api_key",
        });
        {
            const discovered = await discoverOpenAICompatibleModels({
                baseUrl: ZAI_BASE_URL,
                apiKey: resolvedApiKey,
            });
            if (discovered) {
                const options = buildDiscoveredModelOptions({ discovered, customLabel: "手动输入模型 ID" });
                const selection = await params.prompter.select({
                    message: `选择 Z.AI 模型（已获取 ${discovered.length} 个可用模型）`,
                    options,
                    initialValue: "glm-5",
                });
                let modelId;
                if (selection === "custom") {
                    const input = await params.prompter.text({
                        message: "输入模型 ID",
                        validate: (val) => (String(val).trim().length > 0 ? undefined : "模型 ID 不能为空"),
                    });
                    modelId = typeof input === "string" ? input.trim() : "glm-5";
                }
                else {
                    modelId = String(selection);
                }
                const modelRef = `zai/${modelId}`;
                nextConfig = applyZaiConfig(nextConfig);
                nextConfig = {
                    ...nextConfig,
                    agents: {
                        ...nextConfig.agents,
                        defaults: { ...nextConfig.agents?.defaults, model: { primary: modelRef } },
                    },
                };
                if (params.setDefaultModel) {
                    await params.prompter.note(`Default model set to ${modelRef}`, "Model configured");
                }
                else {
                    await noteAgentModel(modelRef);
                    agentModelOverride = modelRef;
                }
            }
            else {
                const applied = await applyDefaultModelChoice({
                    config: nextConfig,
                    setDefaultModel: params.setDefaultModel,
                    defaultModel: ZAI_DEFAULT_MODEL_REF,
                    applyDefaultConfig: applyZaiConfig,
                    applyProviderConfig: (config) => ({
                        ...config,
                        agents: {
                            ...config.agents,
                            defaults: {
                                ...config.agents?.defaults,
                                models: {
                                    ...config.agents?.defaults?.models,
                                    [ZAI_DEFAULT_MODEL_REF]: {
                                        ...config.agents?.defaults?.models?.[ZAI_DEFAULT_MODEL_REF],
                                        alias: config.agents?.defaults?.models?.[ZAI_DEFAULT_MODEL_REF]?.alias ?? "GLM",
                                    },
                                },
                            },
                        },
                    }),
                    noteDefault: ZAI_DEFAULT_MODEL_REF,
                    noteAgentModel,
                    prompter: params.prompter,
                });
                nextConfig = applied.config;
                agentModelOverride = applied.agentModelOverride ?? agentModelOverride;
            }
        }
        return { config: nextConfig, agentModelOverride };
    }
    if (authChoice === "xiaomi-api-key") {
        let hasCredential = false;
        let resolvedApiKey = "";
        if (!hasCredential && params.opts?.token && params.opts?.tokenProvider === "xiaomi") {
            resolvedApiKey = normalizeApiKeyInput(params.opts.token);
            await setXiaomiApiKey(resolvedApiKey, params.agentDir);
            hasCredential = true;
        }
        const envKey = resolveEnvApiKey("xiaomi");
        if (envKey && !hasCredential) {
            const useExisting = await params.prompter.confirm({
                message: `使用已有 XIAOMI_API_KEY (${envKey.source}, ${formatApiKeyPreview(envKey.apiKey)})？`,
                initialValue: true,
            });
            if (useExisting) {
                resolvedApiKey = envKey.apiKey;
                await setXiaomiApiKey(resolvedApiKey, params.agentDir);
                hasCredential = true;
            }
        }
        if (!hasCredential) {
            const key = await params.prompter.text({
                message: "输入小米 MiMo API key",
                validate: validateApiKeyInput,
            });
            resolvedApiKey = normalizeApiKeyInput(String(key));
            await setXiaomiApiKey(resolvedApiKey, params.agentDir);
        }
        nextConfig = applyAuthProfileConfig(nextConfig, {
            profileId: "xiaomi:default",
            provider: "xiaomi",
            mode: "api_key",
        });
        {
            const discovered = await discoverOpenAICompatibleModels({
                baseUrl: XIAOMI_API_BASE_URL,
                apiKey: resolvedApiKey,
            });
            if (discovered) {
                const options = buildDiscoveredModelOptions({ discovered, customLabel: "手动输入模型 ID" });
                const selection = await params.prompter.select({
                    message: `选择小米模型（已获取 ${discovered.length} 个可用模型）`,
                    options,
                    initialValue: "mimo-v2-flash",
                });
                let modelId;
                if (selection === "custom") {
                    const input = await params.prompter.text({
                        message: "输入模型 ID",
                        validate: (val) => (String(val).trim().length > 0 ? undefined : "模型 ID 不能为空"),
                    });
                    modelId = typeof input === "string" ? input.trim() : "mimo-v2-flash";
                }
                else {
                    modelId = String(selection);
                }
                const modelRef = `xiaomi/${modelId}`;
                nextConfig = applyXiaomiProviderConfig(nextConfig);
                nextConfig = {
                    ...nextConfig,
                    agents: {
                        ...nextConfig.agents,
                        defaults: { ...nextConfig.agents?.defaults, model: { primary: modelRef } },
                    },
                };
                if (params.setDefaultModel) {
                    await params.prompter.note(`Default model set to ${modelRef}`, "Model configured");
                }
                else {
                    await noteAgentModel(modelRef);
                    agentModelOverride = modelRef;
                }
            }
            else {
                const applied = await applyDefaultModelChoice({
                    config: nextConfig,
                    setDefaultModel: params.setDefaultModel,
                    defaultModel: XIAOMI_DEFAULT_MODEL_REF,
                    applyDefaultConfig: applyXiaomiConfig,
                    applyProviderConfig: applyXiaomiProviderConfig,
                    noteDefault: XIAOMI_DEFAULT_MODEL_REF,
                    noteAgentModel,
                    prompter: params.prompter,
                });
                nextConfig = applied.config;
                agentModelOverride = applied.agentModelOverride ?? agentModelOverride;
            }
        }
        return { config: nextConfig, agentModelOverride };
    }
    if (authChoice === "synthetic-api-key") {
        if (params.opts?.token && params.opts?.tokenProvider === "synthetic") {
            await setSyntheticApiKey(String(params.opts.token).trim(), params.agentDir);
        }
        else {
            const key = await params.prompter.text({
                message: "Enter Synthetic API key",
                validate: (value) => (value?.trim() ? undefined : "Required"),
            });
            await setSyntheticApiKey(String(key).trim(), params.agentDir);
        }
        nextConfig = applyAuthProfileConfig(nextConfig, {
            profileId: "synthetic:default",
            provider: "synthetic",
            mode: "api_key",
        });
        {
            const applied = await applyDefaultModelChoice({
                config: nextConfig,
                setDefaultModel: params.setDefaultModel,
                defaultModel: SYNTHETIC_DEFAULT_MODEL_REF,
                applyDefaultConfig: applySyntheticConfig,
                applyProviderConfig: applySyntheticProviderConfig,
                noteDefault: SYNTHETIC_DEFAULT_MODEL_REF,
                noteAgentModel,
                prompter: params.prompter,
            });
            nextConfig = applied.config;
            agentModelOverride = applied.agentModelOverride ?? agentModelOverride;
        }
        return { config: nextConfig, agentModelOverride };
    }
    if (authChoice === "venice-api-key") {
        let hasCredential = false;
        if (!hasCredential && params.opts?.token && params.opts?.tokenProvider === "venice") {
            await setVeniceApiKey(normalizeApiKeyInput(params.opts.token), params.agentDir);
            hasCredential = true;
        }
        if (!hasCredential) {
            await params.prompter.note([
                "Venice AI provides privacy-focused inference with uncensored models.",
                "Get your API key at: https://venice.ai/settings/api",
                "Supports 'private' (fully private) and 'anonymized' (proxy) modes.",
            ].join("\n"), "Venice AI");
        }
        const envKey = resolveEnvApiKey("venice");
        if (envKey) {
            const useExisting = await params.prompter.confirm({
                message: `Use existing VENICE_API_KEY (${envKey.source}, ${formatApiKeyPreview(envKey.apiKey)})?`,
                initialValue: true,
            });
            if (useExisting) {
                await setVeniceApiKey(envKey.apiKey, params.agentDir);
                hasCredential = true;
            }
        }
        if (!hasCredential) {
            const key = await params.prompter.text({
                message: "Enter Venice AI API key",
                validate: validateApiKeyInput,
            });
            await setVeniceApiKey(normalizeApiKeyInput(String(key)), params.agentDir);
        }
        nextConfig = applyAuthProfileConfig(nextConfig, {
            profileId: "venice:default",
            provider: "venice",
            mode: "api_key",
        });
        {
            const applied = await applyDefaultModelChoice({
                config: nextConfig,
                setDefaultModel: params.setDefaultModel,
                defaultModel: VENICE_DEFAULT_MODEL_REF,
                applyDefaultConfig: applyVeniceConfig,
                applyProviderConfig: applyVeniceProviderConfig,
                noteDefault: VENICE_DEFAULT_MODEL_REF,
                noteAgentModel,
                prompter: params.prompter,
            });
            nextConfig = applied.config;
            agentModelOverride = applied.agentModelOverride ?? agentModelOverride;
        }
        return { config: nextConfig, agentModelOverride };
    }
    if (authChoice === "opencode-zen") {
        let hasCredential = false;
        if (!hasCredential && params.opts?.token && params.opts?.tokenProvider === "opencode") {
            await setOpencodeZenApiKey(normalizeApiKeyInput(params.opts.token), params.agentDir);
            hasCredential = true;
        }
        if (!hasCredential) {
            await params.prompter.note([
                "OpenCode Zen provides access to Claude, GPT, Gemini, and more models.",
                "Get your API key at: https://opencode.ai/auth",
                "Requires an active OpenCode Zen subscription.",
            ].join("\n"), "OpenCode Zen");
        }
        const envKey = resolveEnvApiKey("opencode");
        if (envKey) {
            const useExisting = await params.prompter.confirm({
                message: `Use existing OPENCODE_API_KEY (${envKey.source}, ${formatApiKeyPreview(envKey.apiKey)})?`,
                initialValue: true,
            });
            if (useExisting) {
                await setOpencodeZenApiKey(envKey.apiKey, params.agentDir);
                hasCredential = true;
            }
        }
        if (!hasCredential) {
            const key = await params.prompter.text({
                message: "Enter OpenCode Zen API key",
                validate: validateApiKeyInput,
            });
            await setOpencodeZenApiKey(normalizeApiKeyInput(String(key)), params.agentDir);
        }
        nextConfig = applyAuthProfileConfig(nextConfig, {
            profileId: "opencode:default",
            provider: "opencode",
            mode: "api_key",
        });
        {
            const applied = await applyDefaultModelChoice({
                config: nextConfig,
                setDefaultModel: params.setDefaultModel,
                defaultModel: OPENCODE_ZEN_DEFAULT_MODEL,
                applyDefaultConfig: applyOpencodeZenConfig,
                applyProviderConfig: applyOpencodeZenProviderConfig,
                noteDefault: OPENCODE_ZEN_DEFAULT_MODEL,
                noteAgentModel,
                prompter: params.prompter,
            });
            nextConfig = applied.config;
            agentModelOverride = applied.agentModelOverride ?? agentModelOverride;
        }
        return { config: nextConfig, agentModelOverride };
    }
    if (authChoice === "ollama-local") {
        let nextConfig = params.config;
        const baseUrlRaw = await params.prompter.text({
            message: "Ollama 服务地址",
            initialValue: "http://127.0.0.1:11434",
            placeholder: "http://127.0.0.1:11434",
        });
        const baseUrl = String(baseUrlRaw).trim();
        const modelIdRaw = await params.prompter.text({
            message: "模型 ID (例如 'deepseek-r1:8b')",
            validate: (value) => value && String(value).trim().length > 0 ? undefined : "模型 ID 不能为空",
        });
        const modelId = String(modelIdRaw);
        nextConfig = applyCustomProviderConfig(nextConfig, {
            providerId: "ollama",
            protocol: "openai-completions",
            baseUrl,
            modelId,
        });
        // Override api to "ollama" (native protocol)
        const providers = { ...nextConfig.models?.providers };
        if (providers.ollama) {
            providers.ollama = { ...providers.ollama, api: "ollama" };
            nextConfig = {
                ...nextConfig,
                models: { ...nextConfig.models, providers },
            };
        }
        const modelRef = `ollama/${modelId}`;
        if (params.setDefaultModel) {
            const currentModel = nextConfig.agents?.defaults?.model;
            const newModelConfig = currentModel && typeof currentModel === "object"
                ? { ...currentModel, primary: modelRef }
                : { primary: modelRef };
            nextConfig = {
                ...nextConfig,
                agents: {
                    ...nextConfig.agents,
                    defaults: {
                        ...nextConfig.agents?.defaults,
                        model: newModelConfig,
                    },
                },
            };
            await params.prompter.note(`默认模型已设置为 ${modelRef}`, "Ollama 配置完成");
        }
        else {
            agentModelOverride = modelRef;
            await noteAgentModel(modelRef);
        }
        return { config: nextConfig, agentModelOverride };
    }
    if (authChoice === "custom-provider-api-key") {
        let nextConfig = params.config;
        const protocol = (await params.prompter.select({
            message: "Select protocol",
            options: [
                { value: "openai-completions", label: "OpenAI Compatible (Default)" },
                { value: "anthropic-messages", label: "Anthropic Compatible" },
            ],
        }));
        const providerIdRaw = await params.prompter.text({
            message: "Provider ID (e.g. 'my-local-llm')",
            initialValue: "custom",
            validate: (value) => {
                if (!value || String(value).trim().length === 0)
                    return "Provider ID is required";
                if (!/^[a-z0-9-_]+$/.test(String(value)))
                    return "Provider ID must be lowercase alphanumeric with hyphens/underscores";
            },
        });
        const providerId = String(providerIdRaw);
        const defaultBaseUrl = protocol === "anthropic-messages"
            ? "https://api.anthropic.com/v1"
            : "https://api.openai.com/v1";
        const baseUrlRaw = await params.prompter.text({
            message: "Base URL",
            initialValue: defaultBaseUrl,
            placeholder: "https://api.example.com/v1",
        });
        const baseUrl = String(baseUrlRaw).trim();
        const modelIdRaw = await params.prompter.text({
            message: "Model ID (e.g. 'llama-3-8b')",
            validate: (value) => value && String(value).trim().length > 0 ? undefined : "Model ID is required",
        });
        const modelId = String(modelIdRaw);
        const apiKeyRaw = await params.prompter.text({
            message: "API Key",
        });
        const apiKey = normalizeApiKeyInput(String(apiKeyRaw));
        nextConfig = applyCustomProviderConfig(nextConfig, {
            providerId,
            protocol,
            baseUrl,
            modelId,
            apiKey,
        });
        const modelRef = `${providerId}/${modelId}`;
        if (params.setDefaultModel) {
            const currentModel = nextConfig.agents?.defaults?.model;
            const newModelConfig = currentModel && typeof currentModel === "object"
                ? { ...currentModel, primary: modelRef }
                : { primary: modelRef };
            nextConfig = {
                ...nextConfig,
                agents: {
                    ...nextConfig.agents,
                    defaults: {
                        ...nextConfig.agents?.defaults,
                        model: newModelConfig,
                    },
                },
            };
            await params.prompter.note(`Default model set to ${modelRef}`, "Model configured");
        }
        else {
            agentModelOverride = modelRef;
            await noteAgentModel(modelRef);
        }
        return { config: nextConfig, agentModelOverride };
    }
    return null;
}
