2025-10-16 13:02:38 +08:00
|
|
|
|
import { useEffect, useMemo, useState } from "react";
|
2025-10-16 10:49:56 +08:00
|
|
|
|
import { useForm } from "react-hook-form";
|
|
|
|
|
|
import { zodResolver } from "@hookform/resolvers/zod";
|
|
|
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
|
|
import { Button } from "@/components/ui/button";
|
2025-10-16 21:40:42 +08:00
|
|
|
|
import { Form } from "@/components/ui/form";
|
2025-10-16 12:13:51 +08:00
|
|
|
|
import { providerSchema, type ProviderFormData } from "@/lib/schemas/provider";
|
2025-10-16 13:02:38 +08:00
|
|
|
|
import type { AppType } from "@/lib/api";
|
2025-10-16 19:40:22 +08:00
|
|
|
|
import type { ProviderCategory, CustomEndpoint } from "@/types";
|
2025-10-16 15:32:26 +08:00
|
|
|
|
import { providerPresets, type ProviderPreset } from "@/config/providerPresets";
|
2025-10-16 13:02:38 +08:00
|
|
|
|
import {
|
|
|
|
|
|
codexProviderPresets,
|
|
|
|
|
|
type CodexProviderPreset,
|
|
|
|
|
|
} from "@/config/codexProviderPresets";
|
|
|
|
|
|
import { applyTemplateValues } from "@/utils/providerConfigUtils";
|
2025-10-16 18:50:44 +08:00
|
|
|
|
import CodexConfigEditor from "@/components/ProviderForm/CodexConfigEditor";
|
2025-10-16 20:32:11 +08:00
|
|
|
|
import { CommonConfigEditor } from "./CommonConfigEditor";
|
2025-10-16 21:40:42 +08:00
|
|
|
|
import { ProviderPresetSelector } from "./ProviderPresetSelector";
|
|
|
|
|
|
import { BasicFormFields } from "./BasicFormFields";
|
|
|
|
|
|
import { ClaudeFormFields } from "./ClaudeFormFields";
|
|
|
|
|
|
import { CodexFormFields } from "./CodexFormFields";
|
2025-10-16 18:50:44 +08:00
|
|
|
|
import {
|
|
|
|
|
|
useProviderCategory,
|
|
|
|
|
|
useApiKeyState,
|
|
|
|
|
|
useBaseUrlState,
|
|
|
|
|
|
useModelState,
|
|
|
|
|
|
useCodexConfigState,
|
2025-10-16 19:56:00 +08:00
|
|
|
|
useApiKeyLink,
|
|
|
|
|
|
useCustomEndpoints,
|
2025-10-16 20:21:42 +08:00
|
|
|
|
useKimiModelSelector,
|
2025-10-16 20:25:39 +08:00
|
|
|
|
useTemplateValues,
|
2025-10-16 20:32:11 +08:00
|
|
|
|
useCommonConfigSnippet,
|
2025-10-16 21:04:32 +08:00
|
|
|
|
useCodexCommonConfig,
|
2025-10-16 22:41:36 +08:00
|
|
|
|
useSpeedTestEndpoints,
|
2025-10-16 18:50:44 +08:00
|
|
|
|
} from "./hooks";
|
2025-10-16 13:02:38 +08:00
|
|
|
|
|
|
|
|
|
|
const CLAUDE_DEFAULT_CONFIG = JSON.stringify({ env: {}, config: {} }, null, 2);
|
2025-10-16 15:32:26 +08:00
|
|
|
|
const CODEX_DEFAULT_CONFIG = JSON.stringify({ auth: {}, config: "" }, null, 2);
|
2025-10-16 13:02:38 +08:00
|
|
|
|
|
|
|
|
|
|
type PresetEntry = {
|
|
|
|
|
|
id: string;
|
|
|
|
|
|
preset: ProviderPreset | CodexProviderPreset;
|
|
|
|
|
|
};
|
2025-10-16 10:49:56 +08:00
|
|
|
|
|
|
|
|
|
|
interface ProviderFormProps {
|
2025-10-16 13:02:38 +08:00
|
|
|
|
appType: AppType;
|
2025-10-16 10:49:56 +08:00
|
|
|
|
submitLabel: string;
|
2025-10-16 13:02:38 +08:00
|
|
|
|
onSubmit: (values: ProviderFormValues) => void;
|
2025-10-16 10:49:56 +08:00
|
|
|
|
onCancel: () => void;
|
|
|
|
|
|
initialData?: {
|
|
|
|
|
|
name?: string;
|
|
|
|
|
|
websiteUrl?: string;
|
|
|
|
|
|
settingsConfig?: Record<string, unknown>;
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function ProviderForm({
|
2025-10-16 13:02:38 +08:00
|
|
|
|
appType,
|
2025-10-16 10:49:56 +08:00
|
|
|
|
submitLabel,
|
|
|
|
|
|
onSubmit,
|
|
|
|
|
|
onCancel,
|
|
|
|
|
|
initialData,
|
|
|
|
|
|
}: ProviderFormProps) {
|
|
|
|
|
|
const { t } = useTranslation();
|
2025-10-16 17:40:25 +08:00
|
|
|
|
const isEditMode = Boolean(initialData);
|
|
|
|
|
|
|
2025-10-16 16:51:47 +08:00
|
|
|
|
const [selectedPresetId, setSelectedPresetId] = useState<string | null>(
|
2025-10-16 20:21:42 +08:00
|
|
|
|
initialData ? null : "custom"
|
2025-10-16 16:51:47 +08:00
|
|
|
|
);
|
2025-10-16 13:02:38 +08:00
|
|
|
|
const [activePreset, setActivePreset] = useState<{
|
|
|
|
|
|
id: string;
|
|
|
|
|
|
category?: ProviderCategory;
|
|
|
|
|
|
} | null>(null);
|
2025-10-16 17:44:23 +08:00
|
|
|
|
const [isEndpointModalOpen, setIsEndpointModalOpen] = useState(false);
|
2025-10-16 13:02:38 +08:00
|
|
|
|
|
2025-10-16 19:40:22 +08:00
|
|
|
|
// 新建供应商:收集端点测速弹窗中的"自定义端点",提交时一次性落盘到 meta.custom_endpoints
|
2025-10-16 20:21:42 +08:00
|
|
|
|
const [draftCustomEndpoints, setDraftCustomEndpoints] = useState<string[]>(
|
|
|
|
|
|
[]
|
|
|
|
|
|
);
|
2025-10-16 19:40:22 +08:00
|
|
|
|
|
2025-10-16 17:40:25 +08:00
|
|
|
|
// 使用 category hook
|
|
|
|
|
|
const { category } = useProviderCategory({
|
|
|
|
|
|
appType,
|
|
|
|
|
|
selectedPresetId,
|
|
|
|
|
|
isEditMode,
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-10-16 13:02:38 +08:00
|
|
|
|
useEffect(() => {
|
2025-10-16 16:51:47 +08:00
|
|
|
|
setSelectedPresetId(initialData ? null : "custom");
|
2025-10-16 13:02:38 +08:00
|
|
|
|
setActivePreset(null);
|
|
|
|
|
|
}, [appType, initialData]);
|
2025-10-16 10:49:56 +08:00
|
|
|
|
|
|
|
|
|
|
const defaultValues: ProviderFormData = useMemo(
|
|
|
|
|
|
() => ({
|
|
|
|
|
|
name: initialData?.name ?? "",
|
|
|
|
|
|
websiteUrl: initialData?.websiteUrl ?? "",
|
|
|
|
|
|
settingsConfig: initialData?.settingsConfig
|
|
|
|
|
|
? JSON.stringify(initialData.settingsConfig, null, 2)
|
2025-10-16 13:02:38 +08:00
|
|
|
|
: appType === "codex"
|
|
|
|
|
|
? CODEX_DEFAULT_CONFIG
|
|
|
|
|
|
: CLAUDE_DEFAULT_CONFIG,
|
2025-10-16 10:49:56 +08:00
|
|
|
|
}),
|
2025-10-16 20:21:42 +08:00
|
|
|
|
[initialData, appType]
|
2025-10-16 10:49:56 +08:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
const form = useForm<ProviderFormData>({
|
|
|
|
|
|
resolver: zodResolver(providerSchema),
|
|
|
|
|
|
defaultValues,
|
|
|
|
|
|
mode: "onSubmit",
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-10-16 17:40:25 +08:00
|
|
|
|
// 使用 API Key hook
|
2025-10-16 20:21:42 +08:00
|
|
|
|
const {
|
|
|
|
|
|
apiKey,
|
|
|
|
|
|
handleApiKeyChange,
|
|
|
|
|
|
showApiKey: shouldShowApiKey,
|
|
|
|
|
|
} = useApiKeyState({
|
2025-10-16 17:40:25 +08:00
|
|
|
|
initialConfig: form.watch("settingsConfig"),
|
|
|
|
|
|
onConfigChange: (config) => form.setValue("settingsConfig", config),
|
|
|
|
|
|
selectedPresetId,
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-10-16 22:54:51 +08:00
|
|
|
|
// 使用 Base URL hook (仅 Claude 模式)
|
|
|
|
|
|
const { baseUrl, handleClaudeBaseUrlChange } = useBaseUrlState({
|
2025-10-16 17:44:23 +08:00
|
|
|
|
appType,
|
|
|
|
|
|
category,
|
|
|
|
|
|
settingsConfig: form.watch("settingsConfig"),
|
2025-10-16 22:54:51 +08:00
|
|
|
|
codexConfig: "",
|
2025-10-16 17:44:23 +08:00
|
|
|
|
onSettingsConfigChange: (config) => form.setValue("settingsConfig", config),
|
|
|
|
|
|
onCodexConfigChange: () => {
|
2025-10-16 22:54:51 +08:00
|
|
|
|
// Codex 使用 useCodexConfigState 管理 Base URL
|
2025-10-16 17:44:23 +08:00
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-10-16 17:58:49 +08:00
|
|
|
|
// 使用 Model hook
|
2025-10-16 20:21:42 +08:00
|
|
|
|
const { claudeModel, claudeSmallFastModel, handleModelChange } =
|
|
|
|
|
|
useModelState({
|
|
|
|
|
|
settingsConfig: form.watch("settingsConfig"),
|
|
|
|
|
|
onConfigChange: (config) => form.setValue("settingsConfig", config),
|
|
|
|
|
|
});
|
2025-10-16 17:58:49 +08:00
|
|
|
|
|
2025-10-16 18:50:44 +08:00
|
|
|
|
// 使用 Codex 配置 hook (仅 Codex 模式)
|
|
|
|
|
|
const {
|
|
|
|
|
|
codexAuth,
|
|
|
|
|
|
codexConfig,
|
|
|
|
|
|
codexApiKey,
|
|
|
|
|
|
codexBaseUrl,
|
|
|
|
|
|
codexAuthError,
|
|
|
|
|
|
setCodexAuth,
|
|
|
|
|
|
handleCodexApiKeyChange,
|
|
|
|
|
|
handleCodexBaseUrlChange,
|
|
|
|
|
|
handleCodexConfigChange,
|
|
|
|
|
|
resetCodexConfig,
|
|
|
|
|
|
} = useCodexConfigState({ initialData });
|
|
|
|
|
|
|
2025-10-16 20:21:42 +08:00
|
|
|
|
const [isCodexEndpointModalOpen, setIsCodexEndpointModalOpen] =
|
|
|
|
|
|
useState(false);
|
2025-10-16 21:04:32 +08:00
|
|
|
|
const [isCodexTemplateModalOpen, setIsCodexTemplateModalOpen] =
|
|
|
|
|
|
useState(false);
|
2025-10-16 18:50:44 +08:00
|
|
|
|
|
2025-10-16 10:49:56 +08:00
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
form.reset(defaultValues);
|
|
|
|
|
|
}, [defaultValues, form]);
|
|
|
|
|
|
|
2025-10-16 20:21:42 +08:00
|
|
|
|
const presetCategoryLabels: Record<string, string> = useMemo(
|
|
|
|
|
|
() => ({
|
|
|
|
|
|
official: t("providerPreset.categoryOfficial", {
|
|
|
|
|
|
defaultValue: "官方",
|
|
|
|
|
|
}),
|
|
|
|
|
|
cn_official: t("providerPreset.categoryCnOfficial", {
|
|
|
|
|
|
defaultValue: "国内官方",
|
|
|
|
|
|
}),
|
|
|
|
|
|
aggregator: t("providerPreset.categoryAggregator", {
|
|
|
|
|
|
defaultValue: "聚合服务",
|
|
|
|
|
|
}),
|
|
|
|
|
|
third_party: t("providerPreset.categoryThirdParty", {
|
|
|
|
|
|
defaultValue: "第三方",
|
|
|
|
|
|
}),
|
|
|
|
|
|
}),
|
|
|
|
|
|
[t]
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
const presetEntries = useMemo(() => {
|
|
|
|
|
|
if (appType === "codex") {
|
|
|
|
|
|
return codexProviderPresets.map<PresetEntry>((preset, index) => ({
|
|
|
|
|
|
id: `codex-${index}`,
|
|
|
|
|
|
preset,
|
|
|
|
|
|
}));
|
|
|
|
|
|
}
|
|
|
|
|
|
return providerPresets.map<PresetEntry>((preset, index) => ({
|
|
|
|
|
|
id: `claude-${index}`,
|
|
|
|
|
|
preset,
|
|
|
|
|
|
}));
|
|
|
|
|
|
}, [appType]);
|
|
|
|
|
|
|
|
|
|
|
|
// 使用 Kimi 模型选择器 hook
|
|
|
|
|
|
const {
|
|
|
|
|
|
shouldShow: shouldShowKimiSelector,
|
|
|
|
|
|
kimiAnthropicModel,
|
|
|
|
|
|
kimiAnthropicSmallFastModel,
|
|
|
|
|
|
handleKimiModelChange,
|
|
|
|
|
|
} = useKimiModelSelector({
|
|
|
|
|
|
initialData,
|
|
|
|
|
|
settingsConfig: form.watch("settingsConfig"),
|
|
|
|
|
|
onConfigChange: (config) => form.setValue("settingsConfig", config),
|
|
|
|
|
|
selectedPresetId,
|
|
|
|
|
|
presetName:
|
|
|
|
|
|
selectedPresetId && selectedPresetId !== "custom"
|
|
|
|
|
|
? presetEntries.find((item) => item.id === selectedPresetId)?.preset
|
|
|
|
|
|
.name || ""
|
|
|
|
|
|
: "",
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-10-16 20:25:39 +08:00
|
|
|
|
// 使用模板变量 hook (仅 Claude 模式)
|
|
|
|
|
|
const {
|
|
|
|
|
|
templateValues,
|
|
|
|
|
|
templateValueEntries,
|
|
|
|
|
|
selectedPreset: templatePreset,
|
|
|
|
|
|
handleTemplateValueChange,
|
|
|
|
|
|
validateTemplateValues,
|
|
|
|
|
|
} = useTemplateValues({
|
|
|
|
|
|
selectedPresetId: appType === "claude" ? selectedPresetId : null,
|
|
|
|
|
|
presetEntries: appType === "claude" ? presetEntries : [],
|
|
|
|
|
|
settingsConfig: form.watch("settingsConfig"),
|
|
|
|
|
|
onConfigChange: (config) => form.setValue("settingsConfig", config),
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-10-16 20:32:11 +08:00
|
|
|
|
// 使用通用配置片段 hook (仅 Claude 模式)
|
|
|
|
|
|
const {
|
|
|
|
|
|
useCommonConfig,
|
|
|
|
|
|
commonConfigSnippet,
|
|
|
|
|
|
commonConfigError,
|
|
|
|
|
|
handleCommonConfigToggle,
|
|
|
|
|
|
handleCommonConfigSnippetChange,
|
|
|
|
|
|
} = useCommonConfigSnippet({
|
|
|
|
|
|
settingsConfig: form.watch("settingsConfig"),
|
|
|
|
|
|
onConfigChange: (config) => form.setValue("settingsConfig", config),
|
|
|
|
|
|
initialData: appType === "claude" ? initialData : undefined,
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-10-16 21:04:32 +08:00
|
|
|
|
// 使用 Codex 通用配置片段 hook (仅 Codex 模式)
|
|
|
|
|
|
const {
|
|
|
|
|
|
useCommonConfig: useCodexCommonConfigFlag,
|
|
|
|
|
|
commonConfigSnippet: codexCommonConfigSnippet,
|
|
|
|
|
|
commonConfigError: codexCommonConfigError,
|
|
|
|
|
|
handleCommonConfigToggle: handleCodexCommonConfigToggle,
|
|
|
|
|
|
handleCommonConfigSnippetChange: handleCodexCommonConfigSnippetChange,
|
|
|
|
|
|
} = useCodexCommonConfig({
|
|
|
|
|
|
codexConfig,
|
|
|
|
|
|
onConfigChange: handleCodexConfigChange,
|
|
|
|
|
|
initialData: appType === "codex" ? initialData : undefined,
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-10-16 20:32:11 +08:00
|
|
|
|
const [isCommonConfigModalOpen, setIsCommonConfigModalOpen] = useState(false);
|
|
|
|
|
|
|
2025-10-16 10:49:56 +08:00
|
|
|
|
const handleSubmit = (values: ProviderFormData) => {
|
2025-10-16 20:25:39 +08:00
|
|
|
|
// 验证模板变量(仅 Claude 模式)
|
|
|
|
|
|
if (appType === "claude" && templateValueEntries.length > 0) {
|
|
|
|
|
|
const validation = validateTemplateValues();
|
|
|
|
|
|
if (!validation.isValid && validation.missingField) {
|
|
|
|
|
|
form.setError("settingsConfig", {
|
|
|
|
|
|
type: "manual",
|
|
|
|
|
|
message: t("providerForm.fillParameter", {
|
|
|
|
|
|
label: validation.missingField.label,
|
|
|
|
|
|
defaultValue: `请填写 ${validation.missingField.label}`,
|
|
|
|
|
|
}),
|
|
|
|
|
|
});
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-16 18:50:44 +08:00
|
|
|
|
let settingsConfig: string;
|
|
|
|
|
|
|
|
|
|
|
|
// Codex: 组合 auth 和 config
|
|
|
|
|
|
if (appType === "codex") {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const authJson = JSON.parse(codexAuth);
|
|
|
|
|
|
const configObj = {
|
|
|
|
|
|
auth: authJson,
|
|
|
|
|
|
config: codexConfig ?? "",
|
|
|
|
|
|
};
|
|
|
|
|
|
settingsConfig = JSON.stringify(configObj);
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
// 如果解析失败,使用表单中的配置
|
|
|
|
|
|
settingsConfig = values.settingsConfig.trim();
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// Claude: 使用表单配置
|
|
|
|
|
|
settingsConfig = values.settingsConfig.trim();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-16 13:02:38 +08:00
|
|
|
|
const payload: ProviderFormValues = {
|
2025-10-16 10:49:56 +08:00
|
|
|
|
...values,
|
2025-10-16 13:02:38 +08:00
|
|
|
|
name: values.name.trim(),
|
2025-10-16 10:49:56 +08:00
|
|
|
|
websiteUrl: values.websiteUrl?.trim() ?? "",
|
2025-10-16 18:50:44 +08:00
|
|
|
|
settingsConfig,
|
2025-10-16 13:02:38 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
if (activePreset) {
|
|
|
|
|
|
payload.presetId = activePreset.id;
|
|
|
|
|
|
if (activePreset.category) {
|
|
|
|
|
|
payload.presetCategory = activePreset.category;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-16 19:56:00 +08:00
|
|
|
|
// 新建供应商时:添加自定义端点
|
|
|
|
|
|
if (!initialData && customEndpointsMap) {
|
|
|
|
|
|
payload.meta = { custom_endpoints: customEndpointsMap };
|
2025-10-16 19:40:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-16 13:02:38 +08:00
|
|
|
|
onSubmit(payload);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const groupedPresets = useMemo(() => {
|
|
|
|
|
|
return presetEntries.reduce<Record<string, PresetEntry[]>>((acc, entry) => {
|
|
|
|
|
|
const category = entry.preset.category ?? "others";
|
|
|
|
|
|
if (!acc[category]) {
|
|
|
|
|
|
acc[category] = [];
|
|
|
|
|
|
}
|
|
|
|
|
|
acc[category].push(entry);
|
|
|
|
|
|
return acc;
|
|
|
|
|
|
}, {});
|
|
|
|
|
|
}, [presetEntries]);
|
|
|
|
|
|
|
|
|
|
|
|
const categoryKeys = useMemo(() => {
|
|
|
|
|
|
return Object.keys(groupedPresets).filter(
|
2025-10-16 20:21:42 +08:00
|
|
|
|
(key) => key !== "custom" && groupedPresets[key]?.length
|
2025-10-16 13:02:38 +08:00
|
|
|
|
);
|
|
|
|
|
|
}, [groupedPresets]);
|
|
|
|
|
|
|
2025-10-16 17:44:23 +08:00
|
|
|
|
// 判断是否显示端点测速(仅第三方和自定义类别)
|
|
|
|
|
|
const shouldShowSpeedTest =
|
|
|
|
|
|
category === "third_party" || category === "custom";
|
|
|
|
|
|
|
2025-10-16 19:56:00 +08:00
|
|
|
|
// 使用 API Key 链接 hook (Claude)
|
|
|
|
|
|
const {
|
|
|
|
|
|
shouldShowApiKeyLink: shouldShowClaudeApiKeyLink,
|
|
|
|
|
|
websiteUrl: claudeWebsiteUrl,
|
|
|
|
|
|
} = useApiKeyLink({
|
|
|
|
|
|
appType: "claude",
|
|
|
|
|
|
category,
|
|
|
|
|
|
selectedPresetId,
|
|
|
|
|
|
presetEntries,
|
|
|
|
|
|
formWebsiteUrl: form.watch("websiteUrl") || "",
|
|
|
|
|
|
});
|
2025-10-16 19:37:43 +08:00
|
|
|
|
|
2025-10-16 19:56:00 +08:00
|
|
|
|
// 使用 API Key 链接 hook (Codex)
|
|
|
|
|
|
const {
|
|
|
|
|
|
shouldShowApiKeyLink: shouldShowCodexApiKeyLink,
|
|
|
|
|
|
websiteUrl: codexWebsiteUrl,
|
|
|
|
|
|
} = useApiKeyLink({
|
|
|
|
|
|
appType: "codex",
|
|
|
|
|
|
category,
|
|
|
|
|
|
selectedPresetId,
|
|
|
|
|
|
presetEntries,
|
|
|
|
|
|
formWebsiteUrl: form.watch("websiteUrl") || "",
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 使用自定义端点 hook
|
|
|
|
|
|
const customEndpointsMap = useCustomEndpoints({
|
|
|
|
|
|
appType,
|
|
|
|
|
|
selectedPresetId,
|
|
|
|
|
|
presetEntries,
|
|
|
|
|
|
draftCustomEndpoints,
|
|
|
|
|
|
baseUrl,
|
|
|
|
|
|
codexBaseUrl,
|
|
|
|
|
|
});
|
2025-10-16 19:37:43 +08:00
|
|
|
|
|
2025-10-16 22:41:36 +08:00
|
|
|
|
// 使用端点测速候选 hook
|
|
|
|
|
|
const speedTestEndpoints = useSpeedTestEndpoints({
|
|
|
|
|
|
appType,
|
|
|
|
|
|
selectedPresetId,
|
|
|
|
|
|
presetEntries,
|
|
|
|
|
|
baseUrl,
|
|
|
|
|
|
codexBaseUrl,
|
|
|
|
|
|
initialData,
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-10-16 13:02:38 +08:00
|
|
|
|
const handlePresetChange = (value: string) => {
|
|
|
|
|
|
setSelectedPresetId(value);
|
|
|
|
|
|
if (value === "custom") {
|
|
|
|
|
|
setActivePreset(null);
|
2025-10-16 16:51:47 +08:00
|
|
|
|
form.reset(defaultValues);
|
2025-10-16 18:50:44 +08:00
|
|
|
|
|
|
|
|
|
|
// Codex 自定义模式:重置为空配置
|
|
|
|
|
|
if (appType === "codex") {
|
|
|
|
|
|
resetCodexConfig({}, "");
|
|
|
|
|
|
}
|
2025-10-16 13:02:38 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const entry = presetEntries.find((item) => item.id === value);
|
|
|
|
|
|
if (!entry) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
setActivePreset({
|
|
|
|
|
|
id: value,
|
|
|
|
|
|
category: entry.preset.category,
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
if (appType === "codex") {
|
|
|
|
|
|
const preset = entry.preset as CodexProviderPreset;
|
2025-10-16 18:50:44 +08:00
|
|
|
|
const auth = preset.auth ?? {};
|
|
|
|
|
|
const config = preset.config ?? "";
|
2025-10-16 13:02:38 +08:00
|
|
|
|
|
2025-10-16 18:50:44 +08:00
|
|
|
|
// 重置 Codex 配置
|
|
|
|
|
|
resetCodexConfig(auth, config);
|
|
|
|
|
|
|
|
|
|
|
|
// 更新表单其他字段
|
2025-10-16 13:02:38 +08:00
|
|
|
|
form.reset({
|
|
|
|
|
|
name: preset.name,
|
|
|
|
|
|
websiteUrl: preset.websiteUrl ?? "",
|
2025-10-16 18:50:44 +08:00
|
|
|
|
settingsConfig: JSON.stringify({ auth, config }, null, 2),
|
2025-10-16 13:02:38 +08:00
|
|
|
|
});
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const preset = entry.preset as ProviderPreset;
|
|
|
|
|
|
const config = applyTemplateValues(
|
|
|
|
|
|
preset.settingsConfig,
|
2025-10-16 20:21:42 +08:00
|
|
|
|
preset.templateValues
|
2025-10-16 13:02:38 +08:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
form.reset({
|
|
|
|
|
|
name: preset.name,
|
|
|
|
|
|
websiteUrl: preset.websiteUrl ?? "",
|
|
|
|
|
|
settingsConfig: JSON.stringify(config, null, 2),
|
2025-10-16 10:49:56 +08:00
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
<Form {...form}>
|
2025-10-16 12:13:51 +08:00
|
|
|
|
<form onSubmit={form.handleSubmit(handleSubmit)} className="space-y-6">
|
2025-10-16 16:51:47 +08:00
|
|
|
|
{/* 预设供应商选择(仅新增模式显示) */}
|
|
|
|
|
|
{!initialData && (
|
2025-10-16 21:40:42 +08:00
|
|
|
|
<ProviderPresetSelector
|
|
|
|
|
|
selectedPresetId={selectedPresetId}
|
|
|
|
|
|
groupedPresets={groupedPresets}
|
|
|
|
|
|
categoryKeys={categoryKeys}
|
|
|
|
|
|
presetCategoryLabels={presetCategoryLabels}
|
|
|
|
|
|
onPresetChange={handlePresetChange}
|
2025-10-16 17:44:23 +08:00
|
|
|
|
/>
|
|
|
|
|
|
)}
|
|
|
|
|
|
|
2025-10-16 21:40:42 +08:00
|
|
|
|
{/* 基础字段 */}
|
|
|
|
|
|
<BasicFormFields form={form} />
|
|
|
|
|
|
|
|
|
|
|
|
{/* Claude 专属字段 */}
|
|
|
|
|
|
{appType === "claude" && (
|
|
|
|
|
|
<ClaudeFormFields
|
|
|
|
|
|
shouldShowApiKey={shouldShowApiKey(
|
|
|
|
|
|
form.watch("settingsConfig"),
|
|
|
|
|
|
isEditMode
|
|
|
|
|
|
)}
|
2025-10-16 20:21:42 +08:00
|
|
|
|
apiKey={apiKey}
|
2025-10-16 21:40:42 +08:00
|
|
|
|
onApiKeyChange={handleApiKeyChange}
|
|
|
|
|
|
category={category}
|
|
|
|
|
|
shouldShowApiKeyLink={shouldShowClaudeApiKeyLink}
|
|
|
|
|
|
websiteUrl={claudeWebsiteUrl}
|
|
|
|
|
|
templateValueEntries={templateValueEntries}
|
|
|
|
|
|
templateValues={templateValues}
|
|
|
|
|
|
templatePresetName={templatePreset?.name || ""}
|
|
|
|
|
|
onTemplateValueChange={handleTemplateValueChange}
|
|
|
|
|
|
shouldShowSpeedTest={shouldShowSpeedTest}
|
|
|
|
|
|
baseUrl={baseUrl}
|
|
|
|
|
|
onBaseUrlChange={handleClaudeBaseUrlChange}
|
|
|
|
|
|
isEndpointModalOpen={isEndpointModalOpen}
|
|
|
|
|
|
onEndpointModalToggle={setIsEndpointModalOpen}
|
|
|
|
|
|
onCustomEndpointsChange={setDraftCustomEndpoints}
|
|
|
|
|
|
shouldShowKimiSelector={shouldShowKimiSelector}
|
|
|
|
|
|
shouldShowModelSelector={category !== "official" && !shouldShowKimiSelector}
|
|
|
|
|
|
claudeModel={claudeModel}
|
|
|
|
|
|
claudeSmallFastModel={claudeSmallFastModel}
|
|
|
|
|
|
onModelChange={handleModelChange}
|
|
|
|
|
|
kimiAnthropicModel={kimiAnthropicModel}
|
|
|
|
|
|
kimiAnthropicSmallFastModel={kimiAnthropicSmallFastModel}
|
|
|
|
|
|
onKimiModelChange={handleKimiModelChange}
|
2025-10-16 22:41:36 +08:00
|
|
|
|
speedTestEndpoints={speedTestEndpoints}
|
2025-10-16 20:21:42 +08:00
|
|
|
|
/>
|
2025-10-16 17:58:49 +08:00
|
|
|
|
)}
|
|
|
|
|
|
|
2025-10-16 21:40:42 +08:00
|
|
|
|
{/* Codex 专属字段 */}
|
2025-10-16 22:28:36 +08:00
|
|
|
|
{appType === "codex" && (
|
2025-10-16 21:40:42 +08:00
|
|
|
|
<CodexFormFields
|
|
|
|
|
|
codexApiKey={codexApiKey}
|
|
|
|
|
|
onApiKeyChange={handleCodexApiKeyChange}
|
|
|
|
|
|
category={category}
|
|
|
|
|
|
shouldShowApiKeyLink={shouldShowCodexApiKeyLink}
|
|
|
|
|
|
websiteUrl={codexWebsiteUrl}
|
|
|
|
|
|
shouldShowSpeedTest={shouldShowSpeedTest}
|
|
|
|
|
|
codexBaseUrl={codexBaseUrl}
|
|
|
|
|
|
onBaseUrlChange={handleCodexBaseUrlChange}
|
|
|
|
|
|
isEndpointModalOpen={isCodexEndpointModalOpen}
|
|
|
|
|
|
onEndpointModalToggle={setIsCodexEndpointModalOpen}
|
|
|
|
|
|
onCustomEndpointsChange={setDraftCustomEndpoints}
|
2025-10-16 22:41:36 +08:00
|
|
|
|
speedTestEndpoints={speedTestEndpoints}
|
2025-10-16 21:40:42 +08:00
|
|
|
|
/>
|
2025-10-16 18:50:44 +08:00
|
|
|
|
)}
|
|
|
|
|
|
|
2025-10-16 20:32:11 +08:00
|
|
|
|
{/* 配置编辑器:Claude 使用通用配置编辑器,Codex 使用专用编辑器 */}
|
2025-10-16 18:50:44 +08:00
|
|
|
|
{appType === "codex" ? (
|
|
|
|
|
|
<CodexConfigEditor
|
|
|
|
|
|
authValue={codexAuth}
|
|
|
|
|
|
configValue={codexConfig}
|
|
|
|
|
|
onAuthChange={setCodexAuth}
|
|
|
|
|
|
onConfigChange={handleCodexConfigChange}
|
2025-10-16 21:04:32 +08:00
|
|
|
|
useCommonConfig={useCodexCommonConfigFlag}
|
|
|
|
|
|
onCommonConfigToggle={handleCodexCommonConfigToggle}
|
|
|
|
|
|
commonConfigSnippet={codexCommonConfigSnippet}
|
|
|
|
|
|
onCommonConfigSnippetChange={handleCodexCommonConfigSnippetChange}
|
|
|
|
|
|
commonConfigError={codexCommonConfigError}
|
2025-10-16 18:50:44 +08:00
|
|
|
|
authError={codexAuthError}
|
2025-10-16 21:04:32 +08:00
|
|
|
|
isCustomMode={selectedPresetId === "custom"}
|
|
|
|
|
|
onWebsiteUrlChange={(url) => form.setValue("websiteUrl", url)}
|
|
|
|
|
|
onNameChange={(name) => form.setValue("name", name)}
|
|
|
|
|
|
isTemplateModalOpen={isCodexTemplateModalOpen}
|
|
|
|
|
|
setIsTemplateModalOpen={setIsCodexTemplateModalOpen}
|
2025-10-16 18:50:44 +08:00
|
|
|
|
/>
|
|
|
|
|
|
) : (
|
2025-10-16 20:32:11 +08:00
|
|
|
|
<CommonConfigEditor
|
|
|
|
|
|
value={form.watch("settingsConfig")}
|
|
|
|
|
|
onChange={(value) => form.setValue("settingsConfig", value)}
|
|
|
|
|
|
useCommonConfig={useCommonConfig}
|
|
|
|
|
|
onCommonConfigToggle={handleCommonConfigToggle}
|
|
|
|
|
|
commonConfigSnippet={commonConfigSnippet}
|
|
|
|
|
|
onCommonConfigSnippetChange={handleCommonConfigSnippetChange}
|
|
|
|
|
|
commonConfigError={commonConfigError}
|
|
|
|
|
|
onEditClick={() => setIsCommonConfigModalOpen(true)}
|
|
|
|
|
|
isModalOpen={isCommonConfigModalOpen}
|
|
|
|
|
|
onModalClose={() => setIsCommonConfigModalOpen(false)}
|
2025-10-16 18:50:44 +08:00
|
|
|
|
/>
|
|
|
|
|
|
)}
|
2025-10-16 10:49:56 +08:00
|
|
|
|
|
|
|
|
|
|
<div className="flex justify-end gap-2">
|
|
|
|
|
|
<Button variant="outline" type="button" onClick={onCancel}>
|
|
|
|
|
|
{t("common.cancel", { defaultValue: "取消" })}
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
<Button type="submit">{submitLabel}</Button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</form>
|
|
|
|
|
|
</Form>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-16 13:02:38 +08:00
|
|
|
|
export type ProviderFormValues = ProviderFormData & {
|
|
|
|
|
|
presetId?: string;
|
|
|
|
|
|
presetCategory?: ProviderCategory;
|
2025-10-16 19:40:22 +08:00
|
|
|
|
meta?: {
|
|
|
|
|
|
custom_endpoints?: Record<string, CustomEndpoint>;
|
|
|
|
|
|
};
|
2025-10-16 13:02:38 +08:00
|
|
|
|
};
|