2025-08-21 22:20:57 +08:00
|
|
|
|
import React, { useState, useEffect } from "react";
|
2025-08-23 23:11:39 +08:00
|
|
|
|
import { Provider } from "../types";
|
2025-08-30 21:54:11 +08:00
|
|
|
|
import { AppType } from "../lib/tauri-api";
|
2025-08-21 22:20:57 +08:00
|
|
|
|
import {
|
|
|
|
|
|
updateCoAuthoredSetting,
|
|
|
|
|
|
checkCoAuthoredSetting,
|
|
|
|
|
|
extractWebsiteUrl,
|
2025-08-26 10:37:44 +08:00
|
|
|
|
getApiKeyFromConfig,
|
|
|
|
|
|
hasApiKeyField,
|
|
|
|
|
|
setApiKeyInConfig,
|
2025-08-21 22:20:57 +08:00
|
|
|
|
} from "../utils/providerConfigUtils";
|
|
|
|
|
|
import { providerPresets } from "../config/providerPresets";
|
2025-08-30 22:08:41 +08:00
|
|
|
|
import { codexProviderPresets } from "../config/codexProviderPresets";
|
2025-08-21 22:20:57 +08:00
|
|
|
|
import "./AddProviderModal.css";
|
2025-08-08 15:03:38 +08:00
|
|
|
|
|
|
|
|
|
|
interface ProviderFormProps {
|
2025-08-30 21:54:11 +08:00
|
|
|
|
appType?: AppType;
|
2025-08-21 22:20:57 +08:00
|
|
|
|
title: string;
|
|
|
|
|
|
submitText: string;
|
|
|
|
|
|
initialData?: Provider;
|
|
|
|
|
|
showPresets?: boolean;
|
|
|
|
|
|
onSubmit: (data: Omit<Provider, "id">) => void;
|
|
|
|
|
|
onClose: () => void;
|
2025-08-08 15:03:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const ProviderForm: React.FC<ProviderFormProps> = ({
|
2025-08-30 21:54:11 +08:00
|
|
|
|
appType = "claude",
|
2025-08-08 15:03:38 +08:00
|
|
|
|
title,
|
|
|
|
|
|
submitText,
|
|
|
|
|
|
initialData,
|
|
|
|
|
|
showPresets = false,
|
|
|
|
|
|
onSubmit,
|
2025-08-21 22:20:57 +08:00
|
|
|
|
onClose,
|
2025-08-08 15:03:38 +08:00
|
|
|
|
}) => {
|
2025-08-30 21:54:11 +08:00
|
|
|
|
// 对于 Codex,需要分离 auth 和 config
|
|
|
|
|
|
const isCodex = appType === "codex";
|
|
|
|
|
|
|
2025-08-08 15:03:38 +08:00
|
|
|
|
const [formData, setFormData] = useState({
|
2025-08-21 22:20:57 +08:00
|
|
|
|
name: initialData?.name || "",
|
|
|
|
|
|
websiteUrl: initialData?.websiteUrl || "",
|
|
|
|
|
|
settingsConfig: initialData
|
|
|
|
|
|
? JSON.stringify(initialData.settingsConfig, null, 2)
|
|
|
|
|
|
: "",
|
|
|
|
|
|
});
|
2025-08-30 21:54:11 +08:00
|
|
|
|
|
|
|
|
|
|
// Codex 特有的状态
|
|
|
|
|
|
const [codexAuth, setCodexAuth] = useState("");
|
|
|
|
|
|
const [codexConfig, setCodexConfig] = useState("");
|
2025-08-30 22:08:41 +08:00
|
|
|
|
const [codexApiKey, setCodexApiKey] = useState("");
|
|
|
|
|
|
const [selectedCodexPreset, setSelectedCodexPreset] = useState<number | null>(
|
2025-08-30 23:02:49 +08:00
|
|
|
|
null
|
2025-08-30 22:08:41 +08:00
|
|
|
|
);
|
2025-08-30 21:54:11 +08:00
|
|
|
|
|
|
|
|
|
|
// 初始化 Codex 配置
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
if (isCodex && initialData) {
|
|
|
|
|
|
const config = initialData.settingsConfig;
|
|
|
|
|
|
if (typeof config === "object" && config !== null) {
|
|
|
|
|
|
setCodexAuth(JSON.stringify(config.auth || {}, null, 2));
|
|
|
|
|
|
setCodexConfig(config.config || "");
|
2025-08-30 22:08:41 +08:00
|
|
|
|
try {
|
|
|
|
|
|
const auth = config.auth || {};
|
2025-08-31 16:39:38 +08:00
|
|
|
|
if (auth && typeof auth.OPENAI_API_KEY === "string") {
|
|
|
|
|
|
setCodexApiKey(auth.OPENAI_API_KEY);
|
2025-08-30 22:08:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
} catch {
|
|
|
|
|
|
// ignore
|
|
|
|
|
|
}
|
2025-08-30 21:54:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}, [isCodex, initialData]);
|
2025-08-21 22:20:57 +08:00
|
|
|
|
const [error, setError] = useState("");
|
|
|
|
|
|
const [disableCoAuthored, setDisableCoAuthored] = useState(false);
|
|
|
|
|
|
const [selectedPreset, setSelectedPreset] = useState<number | null>(null);
|
|
|
|
|
|
const [apiKey, setApiKey] = useState("");
|
2025-08-08 15:03:38 +08:00
|
|
|
|
|
|
|
|
|
|
// 初始化时检查禁用签名状态
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
if (initialData) {
|
2025-08-21 22:20:57 +08:00
|
|
|
|
const configString = JSON.stringify(initialData.settingsConfig, null, 2);
|
|
|
|
|
|
const hasCoAuthoredDisabled = checkCoAuthoredSetting(configString);
|
|
|
|
|
|
setDisableCoAuthored(hasCoAuthoredDisabled);
|
2025-08-08 15:03:38 +08:00
|
|
|
|
}
|
2025-08-21 22:20:57 +08:00
|
|
|
|
}, [initialData]);
|
2025-08-08 15:03:38 +08:00
|
|
|
|
|
|
|
|
|
|
const handleSubmit = (e: React.FormEvent) => {
|
2025-08-21 22:20:57 +08:00
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
setError("");
|
2025-08-08 15:03:38 +08:00
|
|
|
|
|
|
|
|
|
|
if (!formData.name) {
|
2025-08-21 22:20:57 +08:00
|
|
|
|
setError("请填写供应商名称");
|
|
|
|
|
|
return;
|
2025-08-08 15:03:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-21 22:20:57 +08:00
|
|
|
|
let settingsConfig: Record<string, any>;
|
|
|
|
|
|
|
2025-08-30 21:54:11 +08:00
|
|
|
|
if (isCodex) {
|
2025-08-31 16:39:38 +08:00
|
|
|
|
// Codex: 仅要求 auth.json 必填;config.toml 可为空
|
|
|
|
|
|
if (!codexAuth.trim()) {
|
|
|
|
|
|
setError("请填写 auth.json 配置");
|
2025-08-30 21:54:11 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
const authJson = JSON.parse(codexAuth);
|
|
|
|
|
|
settingsConfig = {
|
|
|
|
|
|
auth: authJson,
|
2025-08-31 16:39:38 +08:00
|
|
|
|
config: codexConfig ?? "",
|
2025-08-30 21:54:11 +08:00
|
|
|
|
};
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
setError("auth.json 格式错误,请检查JSON语法");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// Claude: 原有逻辑
|
|
|
|
|
|
if (!formData.settingsConfig.trim()) {
|
|
|
|
|
|
setError("请填写配置内容");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
settingsConfig = JSON.parse(formData.settingsConfig);
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
setError("配置JSON格式错误,请检查语法");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2025-08-08 15:03:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
onSubmit({
|
|
|
|
|
|
name: formData.name,
|
|
|
|
|
|
websiteUrl: formData.websiteUrl,
|
2025-08-21 22:20:57 +08:00
|
|
|
|
settingsConfig,
|
|
|
|
|
|
});
|
|
|
|
|
|
};
|
2025-08-08 15:03:38 +08:00
|
|
|
|
|
|
|
|
|
|
const handleChange = (
|
2025-08-30 23:02:49 +08:00
|
|
|
|
e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>
|
2025-08-08 15:03:38 +08:00
|
|
|
|
) => {
|
2025-08-21 22:20:57 +08:00
|
|
|
|
const { name, value } = e.target;
|
|
|
|
|
|
|
|
|
|
|
|
if (name === "settingsConfig") {
|
2025-08-08 15:03:38 +08:00
|
|
|
|
// 当用户修改配置时,尝试自动提取官网地址
|
2025-08-21 22:20:57 +08:00
|
|
|
|
const extractedWebsiteUrl = extractWebsiteUrl(value);
|
|
|
|
|
|
|
2025-08-08 15:03:38 +08:00
|
|
|
|
// 同时检查并同步选择框状态
|
2025-08-21 22:20:57 +08:00
|
|
|
|
const hasCoAuthoredDisabled = checkCoAuthoredSetting(value);
|
|
|
|
|
|
setDisableCoAuthored(hasCoAuthoredDisabled);
|
|
|
|
|
|
|
2025-08-26 10:37:44 +08:00
|
|
|
|
// 同步 API Key 输入框显示与值
|
|
|
|
|
|
const parsedKey = getApiKeyFromConfig(value);
|
|
|
|
|
|
setApiKey(parsedKey);
|
|
|
|
|
|
|
2025-08-08 15:03:38 +08:00
|
|
|
|
setFormData({
|
|
|
|
|
|
...formData,
|
|
|
|
|
|
[name]: value,
|
|
|
|
|
|
// 只有在官网地址为空时才自动填入
|
|
|
|
|
|
websiteUrl: formData.websiteUrl || extractedWebsiteUrl,
|
2025-08-21 22:20:57 +08:00
|
|
|
|
});
|
2025-08-08 15:03:38 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
setFormData({
|
|
|
|
|
|
...formData,
|
|
|
|
|
|
[name]: value,
|
2025-08-21 22:20:57 +08:00
|
|
|
|
});
|
2025-08-08 15:03:38 +08:00
|
|
|
|
}
|
2025-08-21 22:20:57 +08:00
|
|
|
|
};
|
2025-08-08 15:03:38 +08:00
|
|
|
|
|
|
|
|
|
|
// 处理选择框变化
|
|
|
|
|
|
const handleCoAuthoredToggle = (checked: boolean) => {
|
2025-08-21 22:20:57 +08:00
|
|
|
|
setDisableCoAuthored(checked);
|
|
|
|
|
|
|
2025-08-08 15:03:38 +08:00
|
|
|
|
// 更新JSON配置
|
2025-08-21 22:20:57 +08:00
|
|
|
|
const updatedConfig = updateCoAuthoredSetting(
|
|
|
|
|
|
formData.settingsConfig,
|
2025-08-30 23:02:49 +08:00
|
|
|
|
checked
|
2025-08-21 22:20:57 +08:00
|
|
|
|
);
|
2025-08-08 15:03:38 +08:00
|
|
|
|
setFormData({
|
|
|
|
|
|
...formData,
|
|
|
|
|
|
settingsConfig: updatedConfig,
|
2025-08-21 22:20:57 +08:00
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const applyPreset = (preset: (typeof providerPresets)[0], index: number) => {
|
|
|
|
|
|
const configString = JSON.stringify(preset.settingsConfig, null, 2);
|
2025-08-08 15:03:38 +08:00
|
|
|
|
|
|
|
|
|
|
setFormData({
|
|
|
|
|
|
name: preset.name,
|
|
|
|
|
|
websiteUrl: preset.websiteUrl,
|
2025-08-21 22:20:57 +08:00
|
|
|
|
settingsConfig: configString,
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-08-21 21:46:48 +08:00
|
|
|
|
// 设置选中的预设
|
2025-08-21 22:20:57 +08:00
|
|
|
|
setSelectedPreset(index);
|
|
|
|
|
|
|
|
|
|
|
|
// 清空 API Key 输入框,让用户重新输入
|
|
|
|
|
|
setApiKey("");
|
|
|
|
|
|
|
2025-08-08 15:03:38 +08:00
|
|
|
|
// 同步选择框状态
|
2025-08-21 22:20:57 +08:00
|
|
|
|
const hasCoAuthoredDisabled = checkCoAuthoredSetting(configString);
|
|
|
|
|
|
setDisableCoAuthored(hasCoAuthoredDisabled);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-08-30 22:08:41 +08:00
|
|
|
|
// Codex: 应用预设
|
|
|
|
|
|
const applyCodexPreset = (
|
|
|
|
|
|
preset: (typeof codexProviderPresets)[0],
|
2025-08-30 23:02:49 +08:00
|
|
|
|
index: number
|
2025-08-30 22:08:41 +08:00
|
|
|
|
) => {
|
|
|
|
|
|
const authString = JSON.stringify(preset.auth || {}, null, 2);
|
|
|
|
|
|
setCodexAuth(authString);
|
|
|
|
|
|
setCodexConfig(preset.config || "");
|
|
|
|
|
|
|
|
|
|
|
|
setFormData({
|
|
|
|
|
|
name: preset.name,
|
|
|
|
|
|
websiteUrl: preset.websiteUrl,
|
|
|
|
|
|
settingsConfig: formData.settingsConfig,
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
setSelectedCodexPreset(index);
|
|
|
|
|
|
|
2025-08-30 22:09:19 +08:00
|
|
|
|
// 清空 API Key,让用户重新输入
|
|
|
|
|
|
setCodexApiKey("");
|
2025-08-30 22:08:41 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
2025-08-21 22:20:57 +08:00
|
|
|
|
// 处理 API Key 输入并自动更新配置
|
|
|
|
|
|
const handleApiKeyChange = (key: string) => {
|
|
|
|
|
|
setApiKey(key);
|
|
|
|
|
|
|
2025-08-26 10:37:44 +08:00
|
|
|
|
const configString = setApiKeyInConfig(
|
|
|
|
|
|
formData.settingsConfig,
|
|
|
|
|
|
key.trim(),
|
2025-08-30 23:02:49 +08:00
|
|
|
|
{ createIfMissing: selectedPreset !== null }
|
2025-08-26 10:37:44 +08:00
|
|
|
|
);
|
2025-08-21 22:20:57 +08:00
|
|
|
|
|
2025-08-26 10:37:44 +08:00
|
|
|
|
// 更新表单配置
|
|
|
|
|
|
setFormData((prev) => ({
|
|
|
|
|
|
...prev,
|
|
|
|
|
|
settingsConfig: configString,
|
|
|
|
|
|
}));
|
2025-08-21 22:20:57 +08:00
|
|
|
|
|
2025-08-26 10:37:44 +08:00
|
|
|
|
// 同步选择框状态
|
|
|
|
|
|
const hasCoAuthoredDisabled = checkCoAuthoredSetting(configString);
|
|
|
|
|
|
setDisableCoAuthored(hasCoAuthoredDisabled);
|
|
|
|
|
|
};
|
2025-08-21 22:20:57 +08:00
|
|
|
|
|
2025-08-30 22:08:41 +08:00
|
|
|
|
// Codex: 处理 API Key 输入并写回 auth.json
|
|
|
|
|
|
const handleCodexApiKeyChange = (key: string) => {
|
|
|
|
|
|
setCodexApiKey(key);
|
|
|
|
|
|
try {
|
|
|
|
|
|
const auth = JSON.parse(codexAuth || "{}");
|
2025-08-31 16:39:38 +08:00
|
|
|
|
auth.OPENAI_API_KEY = key.trim();
|
2025-08-30 22:08:41 +08:00
|
|
|
|
setCodexAuth(JSON.stringify(auth, null, 2));
|
|
|
|
|
|
} catch {
|
|
|
|
|
|
// ignore
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-08-26 10:37:44 +08:00
|
|
|
|
// 根据当前配置决定是否展示 API Key 输入框
|
|
|
|
|
|
const showApiKey =
|
|
|
|
|
|
selectedPreset !== null || hasApiKeyField(formData.settingsConfig);
|
2025-08-21 22:20:57 +08:00
|
|
|
|
|
2025-08-29 09:03:11 +08:00
|
|
|
|
// 判断当前选中的预设是否是官方
|
2025-08-29 11:35:17 +08:00
|
|
|
|
const isOfficialPreset =
|
|
|
|
|
|
selectedPreset !== null &&
|
2025-08-29 09:03:11 +08:00
|
|
|
|
providerPresets[selectedPreset]?.isOfficial === true;
|
|
|
|
|
|
|
2025-08-30 22:08:41 +08:00
|
|
|
|
// Codex: 控制显示 API Key 与官方标记
|
|
|
|
|
|
const getCodexAuthApiKey = (authString: string): string => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const auth = JSON.parse(authString || "{}");
|
2025-08-31 16:39:38 +08:00
|
|
|
|
return typeof auth.OPENAI_API_KEY === "string" ? auth.OPENAI_API_KEY : "";
|
2025-08-30 22:08:41 +08:00
|
|
|
|
} catch {
|
|
|
|
|
|
return "";
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
const showCodexApiKey =
|
|
|
|
|
|
selectedCodexPreset !== null || getCodexAuthApiKey(codexAuth) !== "";
|
|
|
|
|
|
const isCodexOfficialPreset =
|
|
|
|
|
|
selectedCodexPreset !== null &&
|
|
|
|
|
|
codexProviderPresets[selectedCodexPreset]?.isOfficial === true;
|
|
|
|
|
|
|
2025-08-26 10:37:44 +08:00
|
|
|
|
// 初始时从配置中同步 API Key(编辑模式)
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
if (initialData) {
|
|
|
|
|
|
const parsedKey = getApiKeyFromConfig(
|
2025-08-30 23:02:49 +08:00
|
|
|
|
JSON.stringify(initialData.settingsConfig)
|
2025-08-26 10:37:44 +08:00
|
|
|
|
);
|
|
|
|
|
|
if (parsedKey) setApiKey(parsedKey);
|
2025-08-21 22:20:57 +08:00
|
|
|
|
}
|
2025-08-26 10:37:44 +08:00
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
|
|
}, []);
|
2025-08-08 15:03:38 +08:00
|
|
|
|
|
2025-08-26 12:34:47 +08:00
|
|
|
|
// 支持按下 ESC 关闭弹窗
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
const onKeyDown = (e: KeyboardEvent) => {
|
2025-08-26 15:12:27 +08:00
|
|
|
|
if (e.key === "Escape") {
|
2025-08-26 12:34:47 +08:00
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
onClose();
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
2025-08-26 15:12:27 +08:00
|
|
|
|
window.addEventListener("keydown", onKeyDown);
|
|
|
|
|
|
return () => window.removeEventListener("keydown", onKeyDown);
|
2025-08-26 12:34:47 +08:00
|
|
|
|
}, [onClose]);
|
|
|
|
|
|
|
2025-08-08 15:03:38 +08:00
|
|
|
|
return (
|
2025-08-26 15:12:27 +08:00
|
|
|
|
<div
|
|
|
|
|
|
className="modal-overlay"
|
|
|
|
|
|
onMouseDown={(e) => {
|
|
|
|
|
|
if (e.target === e.currentTarget) onClose();
|
|
|
|
|
|
}}
|
|
|
|
|
|
>
|
2025-08-08 15:03:38 +08:00
|
|
|
|
<div className="modal-content">
|
2025-08-26 12:34:47 +08:00
|
|
|
|
<div className="modal-titlebar">
|
|
|
|
|
|
<div className="modal-spacer" />
|
2025-08-26 15:12:27 +08:00
|
|
|
|
<div className="modal-title" title={title}>
|
|
|
|
|
|
{title}
|
|
|
|
|
|
</div>
|
2025-08-26 12:34:47 +08:00
|
|
|
|
<button
|
|
|
|
|
|
type="button"
|
|
|
|
|
|
className="modal-close-btn"
|
|
|
|
|
|
aria-label="关闭"
|
|
|
|
|
|
onClick={onClose}
|
|
|
|
|
|
title="关闭"
|
|
|
|
|
|
>
|
|
|
|
|
|
×
|
|
|
|
|
|
</button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<form onSubmit={handleSubmit} className="modal-form">
|
|
|
|
|
|
<div className="modal-body">
|
2025-08-26 15:12:27 +08:00
|
|
|
|
{error && <div className="error-message">{error}</div>}
|
|
|
|
|
|
|
2025-08-30 21:54:11 +08:00
|
|
|
|
{showPresets && !isCodex && (
|
2025-08-26 15:12:27 +08:00
|
|
|
|
<div className="presets">
|
|
|
|
|
|
<label>一键导入(只需要填写 key)</label>
|
|
|
|
|
|
<div className="preset-buttons">
|
2025-08-29 09:03:11 +08:00
|
|
|
|
{providerPresets.map((preset, index) => {
|
|
|
|
|
|
return (
|
|
|
|
|
|
<button
|
|
|
|
|
|
key={index}
|
|
|
|
|
|
type="button"
|
|
|
|
|
|
className={`preset-btn ${
|
|
|
|
|
|
selectedPreset === index ? "selected" : ""
|
|
|
|
|
|
} ${preset.isOfficial ? "official" : ""}`}
|
|
|
|
|
|
onClick={() => applyPreset(preset, index)}
|
|
|
|
|
|
>
|
|
|
|
|
|
{preset.name}
|
|
|
|
|
|
</button>
|
|
|
|
|
|
);
|
|
|
|
|
|
})}
|
2025-08-26 15:12:27 +08:00
|
|
|
|
</div>
|
2025-08-26 12:34:47 +08:00
|
|
|
|
</div>
|
2025-08-26 15:12:27 +08:00
|
|
|
|
)}
|
2025-08-08 15:03:38 +08:00
|
|
|
|
|
2025-08-30 22:08:41 +08:00
|
|
|
|
{showPresets && isCodex && (
|
|
|
|
|
|
<div className="presets">
|
|
|
|
|
|
<label>一键导入(只需要填写 key)</label>
|
|
|
|
|
|
<div className="preset-buttons">
|
|
|
|
|
|
{codexProviderPresets.map((preset, index) => (
|
|
|
|
|
|
<button
|
|
|
|
|
|
key={index}
|
|
|
|
|
|
type="button"
|
|
|
|
|
|
className={`preset-btn ${
|
|
|
|
|
|
selectedCodexPreset === index ? "selected" : ""
|
|
|
|
|
|
} ${preset.isOfficial ? "official" : ""}`}
|
|
|
|
|
|
onClick={() => applyCodexPreset(preset, index)}
|
|
|
|
|
|
>
|
|
|
|
|
|
{preset.name}
|
|
|
|
|
|
</button>
|
|
|
|
|
|
))}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
|
|
|
|
|
|
2025-08-21 22:20:57 +08:00
|
|
|
|
<div className="form-group">
|
2025-08-26 15:12:27 +08:00
|
|
|
|
<label htmlFor="name">供应商名称 *</label>
|
2025-08-21 22:20:57 +08:00
|
|
|
|
<input
|
|
|
|
|
|
type="text"
|
2025-08-26 15:12:27 +08:00
|
|
|
|
id="name"
|
|
|
|
|
|
name="name"
|
|
|
|
|
|
value={formData.name}
|
|
|
|
|
|
onChange={handleChange}
|
|
|
|
|
|
placeholder="例如:Anthropic 官方"
|
|
|
|
|
|
required
|
2025-08-21 22:20:57 +08:00
|
|
|
|
autoComplete="off"
|
|
|
|
|
|
/>
|
|
|
|
|
|
</div>
|
2025-08-08 15:03:38 +08:00
|
|
|
|
|
2025-08-30 21:54:11 +08:00
|
|
|
|
{!isCodex && (
|
|
|
|
|
|
<div
|
|
|
|
|
|
className={`form-group api-key-group ${!showApiKey ? "hidden" : ""}`}
|
|
|
|
|
|
>
|
|
|
|
|
|
<label htmlFor="apiKey">API Key *</label>
|
|
|
|
|
|
<input
|
|
|
|
|
|
type="text"
|
|
|
|
|
|
id="apiKey"
|
|
|
|
|
|
value={apiKey}
|
|
|
|
|
|
onChange={(e) => handleApiKeyChange(e.target.value)}
|
|
|
|
|
|
placeholder={
|
|
|
|
|
|
isOfficialPreset
|
|
|
|
|
|
? "官方登录无需填写 API Key,直接保存即可"
|
|
|
|
|
|
: "只需要填这里,下方配置会自动填充"
|
|
|
|
|
|
}
|
|
|
|
|
|
disabled={isOfficialPreset}
|
|
|
|
|
|
autoComplete="off"
|
|
|
|
|
|
style={
|
|
|
|
|
|
isOfficialPreset
|
|
|
|
|
|
? {
|
|
|
|
|
|
backgroundColor: "#f5f5f5",
|
|
|
|
|
|
cursor: "not-allowed",
|
|
|
|
|
|
color: "#999",
|
|
|
|
|
|
}
|
|
|
|
|
|
: {}
|
|
|
|
|
|
}
|
|
|
|
|
|
/>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
2025-08-26 15:12:27 +08:00
|
|
|
|
|
2025-08-30 22:08:41 +08:00
|
|
|
|
{isCodex && (
|
|
|
|
|
|
<div
|
|
|
|
|
|
className={`form-group api-key-group ${!showCodexApiKey ? "hidden" : ""}`}
|
|
|
|
|
|
>
|
|
|
|
|
|
<label htmlFor="codexApiKey">API Key *</label>
|
|
|
|
|
|
<input
|
|
|
|
|
|
type="text"
|
|
|
|
|
|
id="codexApiKey"
|
|
|
|
|
|
value={codexApiKey}
|
|
|
|
|
|
onChange={(e) => handleCodexApiKeyChange(e.target.value)}
|
|
|
|
|
|
placeholder={
|
|
|
|
|
|
isCodexOfficialPreset
|
|
|
|
|
|
? "官方无需填写 API Key,直接保存即可"
|
2025-08-30 23:02:49 +08:00
|
|
|
|
: "只需要填这里,下方 auth.json 会自动填充"
|
2025-08-30 22:08:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
disabled={isCodexOfficialPreset}
|
|
|
|
|
|
autoComplete="off"
|
|
|
|
|
|
style={
|
|
|
|
|
|
isCodexOfficialPreset
|
|
|
|
|
|
? {
|
|
|
|
|
|
backgroundColor: "#f5f5f5",
|
|
|
|
|
|
cursor: "not-allowed",
|
|
|
|
|
|
color: "#999",
|
|
|
|
|
|
}
|
|
|
|
|
|
: {}
|
|
|
|
|
|
}
|
|
|
|
|
|
/>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
|
|
|
|
|
|
2025-08-26 15:12:27 +08:00
|
|
|
|
<div className="form-group">
|
|
|
|
|
|
<label htmlFor="websiteUrl">官网地址</label>
|
|
|
|
|
|
<input
|
|
|
|
|
|
type="url"
|
|
|
|
|
|
id="websiteUrl"
|
|
|
|
|
|
name="websiteUrl"
|
|
|
|
|
|
value={formData.websiteUrl}
|
|
|
|
|
|
onChange={handleChange}
|
|
|
|
|
|
placeholder="https://example.com(可选)"
|
|
|
|
|
|
autoComplete="off"
|
|
|
|
|
|
/>
|
2025-08-08 15:03:38 +08:00
|
|
|
|
</div>
|
2025-08-26 15:12:27 +08:00
|
|
|
|
|
2025-08-30 21:54:11 +08:00
|
|
|
|
{/* Claude 或 Codex 的配置部分 */}
|
|
|
|
|
|
{isCodex ? (
|
|
|
|
|
|
// Codex: 双编辑器
|
|
|
|
|
|
<>
|
|
|
|
|
|
<div className="form-group">
|
|
|
|
|
|
<label htmlFor="codexAuth">auth.json (JSON) *</label>
|
|
|
|
|
|
<textarea
|
|
|
|
|
|
id="codexAuth"
|
|
|
|
|
|
value={codexAuth}
|
2025-08-30 22:08:41 +08:00
|
|
|
|
onChange={(e) => {
|
|
|
|
|
|
const value = e.target.value;
|
|
|
|
|
|
setCodexAuth(value);
|
|
|
|
|
|
try {
|
|
|
|
|
|
const auth = JSON.parse(value || "{}");
|
2025-08-30 23:02:49 +08:00
|
|
|
|
const key =
|
2025-08-31 16:39:38 +08:00
|
|
|
|
typeof auth.OPENAI_API_KEY === "string"
|
|
|
|
|
|
? auth.OPENAI_API_KEY
|
|
|
|
|
|
: "";
|
2025-08-30 22:08:41 +08:00
|
|
|
|
setCodexApiKey(key);
|
|
|
|
|
|
} catch {
|
|
|
|
|
|
// ignore
|
|
|
|
|
|
}
|
|
|
|
|
|
}}
|
2025-08-30 21:54:11 +08:00
|
|
|
|
placeholder={`{
|
2025-08-30 23:02:49 +08:00
|
|
|
|
"OPENAI_API_KEY": "sk-your-api-key-here"
|
2025-08-30 21:54:11 +08:00
|
|
|
|
}`}
|
|
|
|
|
|
rows={6}
|
|
|
|
|
|
style={{ fontFamily: "monospace", fontSize: "14px" }}
|
|
|
|
|
|
required
|
2025-08-26 15:12:27 +08:00
|
|
|
|
/>
|
2025-08-30 21:54:11 +08:00
|
|
|
|
<small className="field-hint">Codex auth.json 配置内容</small>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<div className="form-group">
|
2025-08-31 16:39:38 +08:00
|
|
|
|
<label htmlFor="codexConfig">config.toml (TOML)</label>
|
2025-08-30 21:54:11 +08:00
|
|
|
|
<textarea
|
|
|
|
|
|
id="codexConfig"
|
|
|
|
|
|
value={codexConfig}
|
|
|
|
|
|
onChange={(e) => setCodexConfig(e.target.value)}
|
2025-08-30 23:02:49 +08:00
|
|
|
|
placeholder={``}
|
2025-08-30 21:54:11 +08:00
|
|
|
|
rows={8}
|
|
|
|
|
|
style={{ fontFamily: "monospace", fontSize: "14px" }}
|
|
|
|
|
|
/>
|
|
|
|
|
|
<small className="field-hint">
|
2025-08-31 16:39:38 +08:00
|
|
|
|
Codex config.toml 配置内容(可留空)
|
2025-08-30 21:54:11 +08:00
|
|
|
|
</small>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</>
|
|
|
|
|
|
) : (
|
|
|
|
|
|
// Claude: 原有的单编辑器
|
|
|
|
|
|
<div className="form-group">
|
|
|
|
|
|
<div className="label-with-checkbox">
|
|
|
|
|
|
<label htmlFor="settingsConfig">
|
|
|
|
|
|
Claude Code 配置 (JSON) *
|
|
|
|
|
|
</label>
|
|
|
|
|
|
<label className="checkbox-label">
|
|
|
|
|
|
<input
|
|
|
|
|
|
type="checkbox"
|
|
|
|
|
|
checked={disableCoAuthored}
|
|
|
|
|
|
onChange={(e) => handleCoAuthoredToggle(e.target.checked)}
|
|
|
|
|
|
/>
|
|
|
|
|
|
禁止 Claude Code 签名
|
|
|
|
|
|
</label>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<textarea
|
|
|
|
|
|
id="settingsConfig"
|
|
|
|
|
|
name="settingsConfig"
|
|
|
|
|
|
value={formData.settingsConfig}
|
|
|
|
|
|
onChange={handleChange}
|
|
|
|
|
|
placeholder={`{
|
2025-08-08 15:03:38 +08:00
|
|
|
|
"env": {
|
|
|
|
|
|
"ANTHROPIC_BASE_URL": "https://api.anthropic.com",
|
|
|
|
|
|
"ANTHROPIC_AUTH_TOKEN": "sk-your-api-key-here"
|
|
|
|
|
|
}
|
|
|
|
|
|
}`}
|
2025-08-30 21:54:11 +08:00
|
|
|
|
rows={12}
|
|
|
|
|
|
style={{ fontFamily: "monospace", fontSize: "14px" }}
|
|
|
|
|
|
required
|
|
|
|
|
|
/>
|
|
|
|
|
|
<small className="field-hint">
|
|
|
|
|
|
完整的 Claude Code settings.json 配置内容
|
|
|
|
|
|
</small>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
2025-08-08 15:03:38 +08:00
|
|
|
|
</div>
|
2025-08-26 12:34:47 +08:00
|
|
|
|
|
|
|
|
|
|
<div className="modal-footer">
|
2025-08-26 15:12:27 +08:00
|
|
|
|
<button type="button" className="cancel-btn" onClick={onClose}>
|
|
|
|
|
|
取消
|
|
|
|
|
|
</button>
|
|
|
|
|
|
<button type="submit" className="submit-btn">
|
|
|
|
|
|
{submitText}
|
|
|
|
|
|
</button>
|
2025-08-26 12:34:47 +08:00
|
|
|
|
</div>
|
2025-08-08 15:03:38 +08:00
|
|
|
|
</form>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
2025-08-21 22:20:57 +08:00
|
|
|
|
);
|
|
|
|
|
|
};
|
2025-08-08 15:03:38 +08:00
|
|
|
|
|
2025-08-21 22:20:57 +08:00
|
|
|
|
export default ProviderForm;
|