import { useCallback, useState } from "react"; import { useTranslation } from "react-i18next"; import { FormLabel } from "@/components/ui/form"; import { Download, Info, Loader2 } from "lucide-react"; import { Button } from "@/components/ui/button"; import { toast } from "sonner"; import EndpointSpeedTest from "./EndpointSpeedTest"; import { ApiKeySection, EndpointField, ModelInputWithFetch } from "./shared"; import { fetchModelsForConfig, showFetchModelsError, type FetchedModel, } from "@/lib/api/model-fetch"; import type { ProviderCategory } from "@/types"; interface EndpointCandidate { url: string; } interface GeminiFormFieldsProps { providerId?: string; // API Key shouldShowApiKey: boolean; apiKey: string; onApiKeyChange: (key: string) => void; category?: ProviderCategory; shouldShowApiKeyLink: boolean; websiteUrl: string; isPartner?: boolean; partnerPromotionKey?: string; // Base URL shouldShowSpeedTest: boolean; baseUrl: string; onBaseUrlChange: (url: string) => void; isEndpointModalOpen: boolean; onEndpointModalToggle: (open: boolean) => void; onCustomEndpointsChange: (endpoints: string[]) => void; autoSelect: boolean; onAutoSelectChange: (checked: boolean) => void; // Model shouldShowModelField: boolean; model: string; onModelChange: (value: string) => void; // Speed Test Endpoints speedTestEndpoints: EndpointCandidate[]; } export function GeminiFormFields({ providerId, shouldShowApiKey, apiKey, onApiKeyChange, category, shouldShowApiKeyLink, websiteUrl, isPartner, partnerPromotionKey, shouldShowSpeedTest, baseUrl, onBaseUrlChange, isEndpointModalOpen, onEndpointModalToggle, onCustomEndpointsChange, autoSelect, onAutoSelectChange, shouldShowModelField, model, onModelChange, speedTestEndpoints, }: GeminiFormFieldsProps) { const { t } = useTranslation(); const [fetchedModels, setFetchedModels] = useState([]); const [isFetchingModels, setIsFetchingModels] = useState(false); const handleFetchModels = useCallback(() => { if (!baseUrl || !apiKey) { showFetchModelsError(null, t, { hasApiKey: !!apiKey, hasBaseUrl: !!baseUrl, }); return; } setIsFetchingModels(true); fetchModelsForConfig(baseUrl, apiKey) .then((models) => { setFetchedModels(models); if (models.length === 0) { toast.info(t("providerForm.fetchModelsEmpty")); } else { toast.success( t("providerForm.fetchModelsSuccess", { count: models.length }), ); } }) .catch((err) => { console.warn("[ModelFetch] Failed:", err); showFetchModelsError(err, t); }) .finally(() => setIsFetchingModels(false)); }, [baseUrl, apiKey, t]); // 检测是否为 Google 官方(使用 OAuth) const isGoogleOfficial = partnerPromotionKey?.toLowerCase() === "google-official"; return ( <> {/* Google OAuth 提示 */} {isGoogleOfficial && (

{t("provider.form.gemini.oauthTitle", { defaultValue: "OAuth 认证模式", })}

{t("provider.form.gemini.oauthHint", { defaultValue: "Google 官方使用 OAuth 个人认证,无需填写 API Key。首次使用时会自动打开浏览器进行登录。", })}

)} {/* API Key 输入框 */} {shouldShowApiKey && !isGoogleOfficial && ( )} {/* Base URL 输入框(统一使用与 Codex 相同的样式与交互) */} {shouldShowSpeedTest && ( onEndpointModalToggle(true)} /> )} {/* Model 输入框 */} {shouldShowModelField && (
{t("provider.form.gemini.model", { defaultValue: "模型" })}
)} {/* 端点测速弹窗 */} {shouldShowSpeedTest && isEndpointModalOpen && ( onEndpointModalToggle(false)} autoSelect={autoSelect} onAutoSelectChange={onAutoSelectChange} onCustomEndpointsChange={onCustomEndpointsChange} /> )} ); }