refactor: improve error handling and code formatting
- Enhanced error messages in Rust backend to include file paths - Improved provider switching error handling with detailed messages - Added MCP button placeholder in UI (functionality TODO) - Applied code formatting across frontend components - Extended error notification duration to 6s for better readability
This commit is contained in:
@@ -60,17 +60,20 @@ pub fn write_codex_live_atomic(auth: &Value, config_text_opt: Option<&str>) -> R
|
||||
let config_path = get_codex_config_path();
|
||||
|
||||
if let Some(parent) = auth_path.parent() {
|
||||
std::fs::create_dir_all(parent).map_err(|e| format!("创建 Codex 目录失败: {}", e))?;
|
||||
std::fs::create_dir_all(parent)
|
||||
.map_err(|e| format!("创建 Codex 目录失败: {}: {}", parent.display(), e))?;
|
||||
}
|
||||
|
||||
// 读取旧内容用于回滚
|
||||
let old_auth = if auth_path.exists() {
|
||||
Some(fs::read(&auth_path).map_err(|e| format!("读取旧 auth.json 失败: {}", e))?)
|
||||
Some(fs::read(&auth_path)
|
||||
.map_err(|e| format!("读取旧 auth.json 失败: {}: {}", auth_path.display(), e))?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let _old_config = if config_path.exists() {
|
||||
Some(fs::read(&config_path).map_err(|e| format!("读取旧 config.toml 失败: {}", e))?)
|
||||
Some(fs::read(&config_path)
|
||||
.map_err(|e| format!("读取旧 config.toml 失败: {}: {}", config_path.display(), e))?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
@@ -81,8 +84,13 @@ pub fn write_codex_live_atomic(auth: &Value, config_text_opt: Option<&str>) -> R
|
||||
None => String::new(),
|
||||
};
|
||||
if !cfg_text.trim().is_empty() {
|
||||
toml::from_str::<toml::Table>(&cfg_text)
|
||||
.map_err(|e| format!("config.toml 格式错误: {}", e))?;
|
||||
toml::from_str::<toml::Table>(&cfg_text).map_err(|e| {
|
||||
format!(
|
||||
"config.toml 语法错误: {} (路径: {})",
|
||||
e,
|
||||
config_path.display()
|
||||
)
|
||||
})?;
|
||||
}
|
||||
|
||||
// 第一步:写 auth.json
|
||||
|
||||
@@ -336,8 +336,13 @@ pub async fn switch_provider(
|
||||
if auth_path.exists() {
|
||||
let auth: Value = crate::config::read_json_file(&auth_path)?;
|
||||
let config_str = if config_path.exists() {
|
||||
std::fs::read_to_string(&config_path)
|
||||
.map_err(|e| format!("读取 config.toml 失败: {}", e))?
|
||||
std::fs::read_to_string(&config_path).map_err(|e| {
|
||||
format!(
|
||||
"读取 config.toml 失败: {}: {}",
|
||||
config_path.display(),
|
||||
e
|
||||
)
|
||||
})?
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
|
||||
@@ -118,16 +118,19 @@ pub fn read_json_file<T: for<'a> Deserialize<'a>>(path: &Path) -> Result<T, Stri
|
||||
return Err(format!("文件不存在: {}", path.display()));
|
||||
}
|
||||
|
||||
let content = fs::read_to_string(path).map_err(|e| format!("读取文件失败: {}", e))?;
|
||||
let content = fs::read_to_string(path)
|
||||
.map_err(|e| format!("读取文件失败: {}: {}", path.display(), e))?;
|
||||
|
||||
serde_json::from_str(&content).map_err(|e| format!("解析 JSON 失败: {}", e))
|
||||
serde_json::from_str(&content)
|
||||
.map_err(|e| format!("解析 JSON 失败: {}: {}", path.display(), e))
|
||||
}
|
||||
|
||||
/// 写入 JSON 配置文件
|
||||
pub fn write_json_file<T: Serialize>(path: &Path, data: &T) -> Result<(), String> {
|
||||
// 确保目录存在
|
||||
if let Some(parent) = path.parent() {
|
||||
fs::create_dir_all(parent).map_err(|e| format!("创建目录失败: {}", e))?;
|
||||
fs::create_dir_all(parent)
|
||||
.map_err(|e| format!("创建目录失败: {}: {}", parent.display(), e))?;
|
||||
}
|
||||
|
||||
let json =
|
||||
@@ -139,7 +142,8 @@ pub fn write_json_file<T: Serialize>(path: &Path, data: &T) -> Result<(), String
|
||||
/// 原子写入文本文件(用于 TOML/纯文本)
|
||||
pub fn write_text_file(path: &Path, data: &str) -> Result<(), String> {
|
||||
if let Some(parent) = path.parent() {
|
||||
fs::create_dir_all(parent).map_err(|e| format!("创建目录失败: {}", e))?;
|
||||
fs::create_dir_all(parent)
|
||||
.map_err(|e| format!("创建目录失败: {}: {}", parent.display(), e))?;
|
||||
}
|
||||
atomic_write(path, data.as_bytes())
|
||||
}
|
||||
@@ -147,7 +151,8 @@ pub fn write_text_file(path: &Path, data: &str) -> Result<(), String> {
|
||||
/// 原子写入:写入临时文件后 rename 替换,避免半写状态
|
||||
pub fn atomic_write(path: &Path, data: &[u8]) -> Result<(), String> {
|
||||
if let Some(parent) = path.parent() {
|
||||
fs::create_dir_all(parent).map_err(|e| format!("创建目录失败: {}", e))?;
|
||||
fs::create_dir_all(parent)
|
||||
.map_err(|e| format!("创建目录失败: {}: {}", parent.display(), e))?;
|
||||
}
|
||||
|
||||
let parent = path.parent().ok_or_else(|| "无效的路径".to_string())?;
|
||||
@@ -164,10 +169,12 @@ pub fn atomic_write(path: &Path, data: &[u8]) -> Result<(), String> {
|
||||
tmp.push(format!("{}.tmp.{}", file_name, ts));
|
||||
|
||||
{
|
||||
let mut f = fs::File::create(&tmp).map_err(|e| format!("创建临时文件失败: {}", e))?;
|
||||
let mut f = fs::File::create(&tmp)
|
||||
.map_err(|e| format!("创建临时文件失败: {}: {}", tmp.display(), e))?;
|
||||
f.write_all(data)
|
||||
.map_err(|e| format!("写入临时文件失败: {}", e))?;
|
||||
f.flush().map_err(|e| format!("刷新临时文件失败: {}", e))?;
|
||||
.map_err(|e| format!("写入临时文件失败: {}: {}", tmp.display(), e))?;
|
||||
f.flush()
|
||||
.map_err(|e| format!("刷新临时文件失败: {}: {}", tmp.display(), e))?;
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
@@ -185,12 +192,14 @@ pub fn atomic_write(path: &Path, data: &[u8]) -> Result<(), String> {
|
||||
if path.exists() {
|
||||
let _ = fs::remove_file(path);
|
||||
}
|
||||
fs::rename(&tmp, path).map_err(|e| format!("原子替换失败: {}", e))?;
|
||||
fs::rename(&tmp, path)
|
||||
.map_err(|e| format!("原子替换失败: {} -> {}: {}", tmp.display(), path.display(), e))?;
|
||||
}
|
||||
|
||||
#[cfg(not(windows))]
|
||||
{
|
||||
fs::rename(&tmp, path).map_err(|e| format!("原子替换失败: {}", e))?;
|
||||
fs::rename(&tmp, path)
|
||||
.map_err(|e| format!("原子替换失败: {} -> {}: {}", tmp.display(), path.display(), e))?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
24
src/App.tsx
24
src/App.tsx
@@ -22,7 +22,7 @@ function App() {
|
||||
const [currentProviderId, setCurrentProviderId] = useState<string>("");
|
||||
const [isAddModalOpen, setIsAddModalOpen] = useState(false);
|
||||
const [editingProviderId, setEditingProviderId] = useState<string | null>(
|
||||
null
|
||||
null,
|
||||
);
|
||||
const [notification, setNotification] = useState<{
|
||||
message: string;
|
||||
@@ -42,7 +42,7 @@ function App() {
|
||||
const showNotification = (
|
||||
message: string,
|
||||
type: "success" | "error",
|
||||
duration = 3000
|
||||
duration = 3000,
|
||||
) => {
|
||||
// 清除之前的定时器
|
||||
if (timeoutRef.current) {
|
||||
@@ -208,6 +208,7 @@ function App() {
|
||||
};
|
||||
|
||||
const handleSwitchProvider = async (id: string) => {
|
||||
try {
|
||||
const success = await window.api.switchProvider(id, activeApp);
|
||||
if (success) {
|
||||
setCurrentProviderId(id);
|
||||
@@ -216,7 +217,7 @@ function App() {
|
||||
showNotification(
|
||||
t("notifications.switchSuccess", { appName }),
|
||||
"success",
|
||||
2000
|
||||
2000,
|
||||
);
|
||||
// 更新托盘菜单
|
||||
await window.api.updateTrayMenu();
|
||||
@@ -227,6 +228,14 @@ function App() {
|
||||
} else {
|
||||
showNotification(t("notifications.switchFailed"), "error");
|
||||
}
|
||||
} catch (error) {
|
||||
const detail = extractErrorMessage(error);
|
||||
const msg = detail
|
||||
? `${t("notifications.switchFailed")}: ${detail}`
|
||||
: t("notifications.switchFailed");
|
||||
// 详细错误展示稍长时间,便于用户阅读
|
||||
showNotification(msg, "error", detail ? 6000 : 3000);
|
||||
}
|
||||
};
|
||||
|
||||
const handleImportSuccess = async () => {
|
||||
@@ -297,6 +306,15 @@ function App() {
|
||||
<div className="flex items-center gap-4">
|
||||
<AppSwitcher activeApp={activeApp} onSwitch={setActiveApp} />
|
||||
|
||||
<button
|
||||
onClick={() => {
|
||||
/* TODO: MCP 功能待实现 */
|
||||
}}
|
||||
className="inline-flex items-center gap-2 px-6 py-2 text-sm font-medium rounded-md transition-colors bg-emerald-500 text-white hover:bg-emerald-600 dark:bg-emerald-600 dark:hover:bg-emerald-700"
|
||||
>
|
||||
MCP
|
||||
</button>
|
||||
|
||||
<button
|
||||
onClick={() => setIsAddModalOpen(true)}
|
||||
className={`inline-flex items-center gap-2 ${buttonStyles.primary}`}
|
||||
|
||||
@@ -3,7 +3,7 @@ import { CheckCircle, Loader2, AlertCircle } from "lucide-react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
|
||||
interface ImportProgressModalProps {
|
||||
status: 'importing' | 'success' | 'error';
|
||||
status: "importing" | "success" | "error";
|
||||
message?: string;
|
||||
backupId?: string;
|
||||
onComplete?: () => void;
|
||||
@@ -15,16 +15,20 @@ export function ImportProgressModal({
|
||||
message,
|
||||
backupId,
|
||||
onComplete,
|
||||
onSuccess
|
||||
onSuccess,
|
||||
}: ImportProgressModalProps) {
|
||||
const { t } = useTranslation();
|
||||
|
||||
useEffect(() => {
|
||||
if (status === 'success') {
|
||||
console.log('[ImportProgressModal] Success detected, starting 2 second countdown');
|
||||
if (status === "success") {
|
||||
console.log(
|
||||
"[ImportProgressModal] Success detected, starting 2 second countdown",
|
||||
);
|
||||
// 成功后等待2秒自动关闭并刷新数据
|
||||
const timer = setTimeout(() => {
|
||||
console.log('[ImportProgressModal] 2 seconds elapsed, calling callbacks...');
|
||||
console.log(
|
||||
"[ImportProgressModal] 2 seconds elapsed, calling callbacks...",
|
||||
);
|
||||
if (onSuccess) {
|
||||
onSuccess();
|
||||
}
|
||||
@@ -34,7 +38,7 @@ export function ImportProgressModal({
|
||||
}, 2000);
|
||||
|
||||
return () => {
|
||||
console.log('[ImportProgressModal] Cleanup timer');
|
||||
console.log("[ImportProgressModal] Cleanup timer");
|
||||
clearTimeout(timer);
|
||||
};
|
||||
}
|
||||
@@ -46,7 +50,7 @@ export function ImportProgressModal({
|
||||
|
||||
<div className="relative bg-white dark:bg-gray-900 rounded-xl shadow-2xl p-8 max-w-md w-full mx-4">
|
||||
<div className="flex flex-col items-center text-center">
|
||||
{status === 'importing' && (
|
||||
{status === "importing" && (
|
||||
<>
|
||||
<Loader2 className="w-12 h-12 text-blue-500 animate-spin mb-4" />
|
||||
<h3 className="text-lg font-semibold text-gray-900 dark:text-gray-100 mb-2">
|
||||
@@ -58,7 +62,7 @@ export function ImportProgressModal({
|
||||
</>
|
||||
)}
|
||||
|
||||
{status === 'success' && (
|
||||
{status === "success" && (
|
||||
<>
|
||||
<CheckCircle className="w-12 h-12 text-green-500 mb-4" />
|
||||
<h3 className="text-lg font-semibold text-gray-900 dark:text-gray-100 mb-2">
|
||||
@@ -75,7 +79,7 @@ export function ImportProgressModal({
|
||||
</>
|
||||
)}
|
||||
|
||||
{status === 'error' && (
|
||||
{status === "error" && (
|
||||
<>
|
||||
<AlertCircle className="w-12 h-12 text-red-500 mb-4" />
|
||||
<h3 className="text-lg font-semibold text-gray-900 dark:text-gray-100 mb-2">
|
||||
|
||||
@@ -53,7 +53,8 @@ const JsonEditor: React.FC<JsonEditorProps> = ({
|
||||
}
|
||||
} catch (e) {
|
||||
// 简单处理JSON解析错误
|
||||
const message = e instanceof SyntaxError ? e.message : t("jsonEditor.invalidJson");
|
||||
const message =
|
||||
e instanceof SyntaxError ? e.message : t("jsonEditor.invalidJson");
|
||||
diagnostics.push({
|
||||
from: 0,
|
||||
to: doc.length,
|
||||
|
||||
@@ -42,11 +42,11 @@ const collectTemplatePaths = (
|
||||
source: unknown,
|
||||
templateKeys: string[],
|
||||
currentPath: TemplatePath = [],
|
||||
acc: TemplatePath[] = []
|
||||
acc: TemplatePath[] = [],
|
||||
): TemplatePath[] => {
|
||||
if (typeof source === "string") {
|
||||
const hasPlaceholder = templateKeys.some((key) =>
|
||||
source.includes(`\${${key}}`)
|
||||
source.includes(`\${${key}}`),
|
||||
);
|
||||
if (hasPlaceholder) {
|
||||
acc.push([...currentPath]);
|
||||
@@ -56,14 +56,14 @@ const collectTemplatePaths = (
|
||||
|
||||
if (Array.isArray(source)) {
|
||||
source.forEach((item, index) =>
|
||||
collectTemplatePaths(item, templateKeys, [...currentPath, index], acc)
|
||||
collectTemplatePaths(item, templateKeys, [...currentPath, index], acc),
|
||||
);
|
||||
return acc;
|
||||
}
|
||||
|
||||
if (source && typeof source === "object") {
|
||||
Object.entries(source).forEach(([key, value]) =>
|
||||
collectTemplatePaths(value, templateKeys, [...currentPath, key], acc)
|
||||
collectTemplatePaths(value, templateKeys, [...currentPath, key], acc),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -82,7 +82,7 @@ const getValueAtPath = (source: any, path: TemplatePath) => {
|
||||
const setValueAtPath = (
|
||||
target: any,
|
||||
path: TemplatePath,
|
||||
value: unknown
|
||||
value: unknown,
|
||||
): any => {
|
||||
if (path.length === 0) {
|
||||
return value;
|
||||
@@ -120,7 +120,7 @@ const setValueAtPath = (
|
||||
const applyTemplateValuesToConfigString = (
|
||||
presetConfig: any,
|
||||
currentConfigString: string,
|
||||
values: TemplateValueMap
|
||||
values: TemplateValueMap,
|
||||
) => {
|
||||
const replacedConfig = applyTemplateValues(presetConfig, values);
|
||||
const templateKeys = Object.keys(values);
|
||||
@@ -203,7 +203,7 @@ const ProviderForm: React.FC<ProviderFormProps> = ({
|
||||
: "",
|
||||
});
|
||||
const [category, setCategory] = useState<ProviderCategory | undefined>(
|
||||
initialData?.category
|
||||
initialData?.category,
|
||||
);
|
||||
|
||||
// Claude 模型配置状态
|
||||
@@ -224,7 +224,7 @@ const ProviderForm: React.FC<ProviderFormProps> = ({
|
||||
useState(false);
|
||||
// 新建供应商:收集端点测速弹窗中的“自定义端点”,提交时一次性落盘到 meta.custom_endpoints
|
||||
const [draftCustomEndpoints, setDraftCustomEndpoints] = useState<string[]>(
|
||||
[]
|
||||
[],
|
||||
);
|
||||
// 端点测速弹窗状态
|
||||
const [isEndpointModalOpen, setIsEndpointModalOpen] = useState(false);
|
||||
@@ -232,7 +232,7 @@ const ProviderForm: React.FC<ProviderFormProps> = ({
|
||||
useState(false);
|
||||
// -1 表示自定义,null 表示未选择,>= 0 表示预设索引
|
||||
const [selectedCodexPreset, setSelectedCodexPreset] = useState<number | null>(
|
||||
showPresets && isCodex ? -1 : null
|
||||
showPresets && isCodex ? -1 : null,
|
||||
);
|
||||
|
||||
const setCodexAuth = (value: string) => {
|
||||
@@ -244,7 +244,7 @@ const ProviderForm: React.FC<ProviderFormProps> = ({
|
||||
setCodexConfigState((prev) =>
|
||||
typeof value === "function"
|
||||
? (value as (input: string) => string)(prev)
|
||||
: value
|
||||
: value,
|
||||
);
|
||||
};
|
||||
|
||||
@@ -305,7 +305,7 @@ const ProviderForm: React.FC<ProviderFormProps> = ({
|
||||
}
|
||||
try {
|
||||
const stored = window.localStorage.getItem(
|
||||
CODEX_COMMON_CONFIG_STORAGE_KEY
|
||||
CODEX_COMMON_CONFIG_STORAGE_KEY,
|
||||
);
|
||||
if (stored && stored.trim()) {
|
||||
return stored.trim();
|
||||
@@ -322,7 +322,7 @@ const ProviderForm: React.FC<ProviderFormProps> = ({
|
||||
|
||||
// -1 表示自定义,null 表示未选择,>= 0 表示预设索引
|
||||
const [selectedPreset, setSelectedPreset] = useState<number | null>(
|
||||
showPresets ? -1 : null
|
||||
showPresets ? -1 : null,
|
||||
);
|
||||
const [apiKey, setApiKey] = useState("");
|
||||
const [codexAuthError, setCodexAuthError] = useState("");
|
||||
@@ -390,11 +390,11 @@ const ProviderForm: React.FC<ProviderFormProps> = ({
|
||||
const configString = JSON.stringify(
|
||||
initialData.settingsConfig,
|
||||
null,
|
||||
2
|
||||
2,
|
||||
);
|
||||
const hasCommon = hasCommonConfigSnippet(
|
||||
configString,
|
||||
commonConfigSnippet
|
||||
commonConfigSnippet,
|
||||
);
|
||||
setUseCommonConfig(hasCommon);
|
||||
setSettingsConfigError(validateSettingsConfig(configString));
|
||||
@@ -410,14 +410,14 @@ const ProviderForm: React.FC<ProviderFormProps> = ({
|
||||
if (config.env) {
|
||||
setClaudeModel(config.env.ANTHROPIC_MODEL || "");
|
||||
setClaudeSmallFastModel(
|
||||
config.env.ANTHROPIC_SMALL_FAST_MODEL || ""
|
||||
config.env.ANTHROPIC_SMALL_FAST_MODEL || "",
|
||||
);
|
||||
setBaseUrl(config.env.ANTHROPIC_BASE_URL || ""); // 初始化基础 URL
|
||||
|
||||
// 初始化 Kimi 模型选择
|
||||
setKimiAnthropicModel(config.env.ANTHROPIC_MODEL || "");
|
||||
setKimiAnthropicSmallFastModel(
|
||||
config.env.ANTHROPIC_SMALL_FAST_MODEL || ""
|
||||
config.env.ANTHROPIC_SMALL_FAST_MODEL || "",
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -425,7 +425,7 @@ const ProviderForm: React.FC<ProviderFormProps> = ({
|
||||
// Codex 初始化时检查 TOML 通用配置
|
||||
const hasCommon = hasTomlCommonConfigSnippet(
|
||||
codexConfig,
|
||||
codexCommonConfigSnippet
|
||||
codexCommonConfigSnippet,
|
||||
);
|
||||
setUseCodexCommonConfig(hasCommon);
|
||||
}
|
||||
@@ -445,7 +445,7 @@ const ProviderForm: React.FC<ProviderFormProps> = ({
|
||||
if (selectedPreset !== null && selectedPreset >= 0) {
|
||||
const preset = providerPresets[selectedPreset];
|
||||
setCategory(
|
||||
preset?.category || (preset?.isOfficial ? "official" : undefined)
|
||||
preset?.category || (preset?.isOfficial ? "official" : undefined),
|
||||
);
|
||||
} else if (selectedPreset === -1) {
|
||||
setCategory("custom");
|
||||
@@ -454,7 +454,7 @@ const ProviderForm: React.FC<ProviderFormProps> = ({
|
||||
if (selectedCodexPreset !== null && selectedCodexPreset >= 0) {
|
||||
const preset = codexProviderPresets[selectedCodexPreset];
|
||||
setCategory(
|
||||
preset?.category || (preset?.isOfficial ? "official" : undefined)
|
||||
preset?.category || (preset?.isOfficial ? "official" : undefined),
|
||||
);
|
||||
} else if (selectedCodexPreset === -1) {
|
||||
setCategory("custom");
|
||||
@@ -506,7 +506,7 @@ const ProviderForm: React.FC<ProviderFormProps> = ({
|
||||
if (commonConfigSnippet.trim()) {
|
||||
window.localStorage.setItem(
|
||||
COMMON_CONFIG_STORAGE_KEY,
|
||||
commonConfigSnippet
|
||||
commonConfigSnippet,
|
||||
);
|
||||
} else {
|
||||
window.localStorage.removeItem(COMMON_CONFIG_STORAGE_KEY);
|
||||
@@ -569,7 +569,7 @@ const ProviderForm: React.FC<ProviderFormProps> = ({
|
||||
}
|
||||
} else {
|
||||
const currentSettingsError = validateSettingsConfig(
|
||||
formData.settingsConfig
|
||||
formData.settingsConfig,
|
||||
);
|
||||
setSettingsConfigError(currentSettingsError);
|
||||
if (currentSettingsError) {
|
||||
@@ -634,7 +634,7 @@ const ProviderForm: React.FC<ProviderFormProps> = ({
|
||||
};
|
||||
|
||||
const handleChange = (
|
||||
e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>
|
||||
e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>,
|
||||
) => {
|
||||
const { name, value } = e.target;
|
||||
|
||||
@@ -664,7 +664,7 @@ const ProviderForm: React.FC<ProviderFormProps> = ({
|
||||
const { updatedConfig, error: snippetError } = updateCommonConfigSnippet(
|
||||
formData.settingsConfig,
|
||||
commonConfigSnippet,
|
||||
checked
|
||||
checked,
|
||||
);
|
||||
|
||||
if (snippetError) {
|
||||
@@ -697,7 +697,7 @@ const ProviderForm: React.FC<ProviderFormProps> = ({
|
||||
const { updatedConfig } = updateCommonConfigSnippet(
|
||||
formData.settingsConfig,
|
||||
previousSnippet,
|
||||
false
|
||||
false,
|
||||
);
|
||||
// 直接更新 formData,不通过 handleChange
|
||||
updateSettingsConfigValue(updatedConfig);
|
||||
@@ -719,7 +719,7 @@ const ProviderForm: React.FC<ProviderFormProps> = ({
|
||||
const removeResult = updateCommonConfigSnippet(
|
||||
formData.settingsConfig,
|
||||
previousSnippet,
|
||||
false
|
||||
false,
|
||||
);
|
||||
if (removeResult.error) {
|
||||
setCommonConfigError(removeResult.error);
|
||||
@@ -731,7 +731,7 @@ const ProviderForm: React.FC<ProviderFormProps> = ({
|
||||
const addResult = updateCommonConfigSnippet(
|
||||
removeResult.updatedConfig,
|
||||
value,
|
||||
true
|
||||
true,
|
||||
);
|
||||
|
||||
if (addResult.error) {
|
||||
@@ -775,11 +775,11 @@ const ProviderForm: React.FC<ProviderFormProps> = ({
|
||||
? config.editorValue
|
||||
: (config.defaultValue ?? ""),
|
||||
},
|
||||
])
|
||||
]),
|
||||
);
|
||||
appliedSettingsConfig = applyTemplateValues(
|
||||
preset.settingsConfig,
|
||||
initialTemplateValues
|
||||
initialTemplateValues,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -794,7 +794,7 @@ const ProviderForm: React.FC<ProviderFormProps> = ({
|
||||
});
|
||||
setSettingsConfigError(validateSettingsConfig(configString));
|
||||
setCategory(
|
||||
preset.category || (preset.isOfficial ? "official" : undefined)
|
||||
preset.category || (preset.isOfficial ? "official" : undefined),
|
||||
);
|
||||
|
||||
// 设置选中的预设
|
||||
@@ -824,7 +824,7 @@ const ProviderForm: React.FC<ProviderFormProps> = ({
|
||||
if (preset.name?.includes("Kimi")) {
|
||||
setKimiAnthropicModel(config.env.ANTHROPIC_MODEL || "");
|
||||
setKimiAnthropicSmallFastModel(
|
||||
config.env.ANTHROPIC_SMALL_FAST_MODEL || ""
|
||||
config.env.ANTHROPIC_SMALL_FAST_MODEL || "",
|
||||
);
|
||||
}
|
||||
} else {
|
||||
@@ -872,7 +872,7 @@ const ProviderForm: React.FC<ProviderFormProps> = ({
|
||||
// Codex: 应用预设
|
||||
const applyCodexPreset = (
|
||||
preset: (typeof codexProviderPresets)[0],
|
||||
index: number
|
||||
index: number,
|
||||
) => {
|
||||
const authString = JSON.stringify(preset.auth || {}, null, 2);
|
||||
setCodexAuth(authString);
|
||||
@@ -890,7 +890,7 @@ const ProviderForm: React.FC<ProviderFormProps> = ({
|
||||
|
||||
setSelectedCodexPreset(index);
|
||||
setCategory(
|
||||
preset.category || (preset.isOfficial ? "official" : undefined)
|
||||
preset.category || (preset.isOfficial ? "official" : undefined),
|
||||
);
|
||||
|
||||
// 清空 API Key,让用户重新输入
|
||||
@@ -906,7 +906,7 @@ const ProviderForm: React.FC<ProviderFormProps> = ({
|
||||
const customConfig = generateThirdPartyConfig(
|
||||
"custom",
|
||||
"https://your-api-endpoint.com/v1",
|
||||
"gpt-5-codex"
|
||||
"gpt-5-codex",
|
||||
);
|
||||
|
||||
setFormData({
|
||||
@@ -929,7 +929,7 @@ const ProviderForm: React.FC<ProviderFormProps> = ({
|
||||
const configString = setApiKeyInConfig(
|
||||
formData.settingsConfig,
|
||||
key.trim(),
|
||||
{ createIfMissing: selectedPreset !== null && selectedPreset !== -1 }
|
||||
{ createIfMissing: selectedPreset !== null && selectedPreset !== -1 },
|
||||
);
|
||||
|
||||
// 更新表单配置
|
||||
@@ -1025,7 +1025,7 @@ const ProviderForm: React.FC<ProviderFormProps> = ({
|
||||
const { updatedConfig } = updateTomlCommonConfigSnippet(
|
||||
codexConfig,
|
||||
previousSnippet,
|
||||
false
|
||||
false,
|
||||
);
|
||||
setCodexConfig(updatedConfig);
|
||||
setUseCodexCommonConfig(false);
|
||||
@@ -1038,12 +1038,12 @@ const ProviderForm: React.FC<ProviderFormProps> = ({
|
||||
const removeResult = updateTomlCommonConfigSnippet(
|
||||
codexConfig,
|
||||
previousSnippet,
|
||||
false
|
||||
false,
|
||||
);
|
||||
const addResult = updateTomlCommonConfigSnippet(
|
||||
removeResult.updatedConfig,
|
||||
sanitizedValue,
|
||||
true
|
||||
true,
|
||||
);
|
||||
|
||||
if (addResult.error) {
|
||||
@@ -1065,7 +1065,7 @@ const ProviderForm: React.FC<ProviderFormProps> = ({
|
||||
try {
|
||||
window.localStorage.setItem(
|
||||
CODEX_COMMON_CONFIG_STORAGE_KEY,
|
||||
sanitizedValue
|
||||
sanitizedValue,
|
||||
);
|
||||
} catch {
|
||||
// ignore localStorage 写入失败
|
||||
@@ -1078,7 +1078,7 @@ const ProviderForm: React.FC<ProviderFormProps> = ({
|
||||
if (!isUpdatingFromCodexCommonConfig.current) {
|
||||
const hasCommon = hasTomlCommonConfigSnippet(
|
||||
value,
|
||||
codexCommonConfigSnippet
|
||||
codexCommonConfigSnippet,
|
||||
);
|
||||
setUseCodexCommonConfig(hasCommon);
|
||||
}
|
||||
@@ -1306,7 +1306,7 @@ const ProviderForm: React.FC<ProviderFormProps> = ({
|
||||
// 处理模型输入变化,自动更新 JSON 配置
|
||||
const handleModelChange = (
|
||||
field: "ANTHROPIC_MODEL" | "ANTHROPIC_SMALL_FAST_MODEL",
|
||||
value: string
|
||||
value: string,
|
||||
) => {
|
||||
if (field === "ANTHROPIC_MODEL") {
|
||||
setClaudeModel(value);
|
||||
@@ -1336,7 +1336,7 @@ const ProviderForm: React.FC<ProviderFormProps> = ({
|
||||
// Kimi 模型选择处理函数
|
||||
const handleKimiModelChange = (
|
||||
field: "ANTHROPIC_MODEL" | "ANTHROPIC_SMALL_FAST_MODEL",
|
||||
value: string
|
||||
value: string,
|
||||
) => {
|
||||
if (field === "ANTHROPIC_MODEL") {
|
||||
setKimiAnthropicModel(value);
|
||||
@@ -1361,7 +1361,7 @@ const ProviderForm: React.FC<ProviderFormProps> = ({
|
||||
useEffect(() => {
|
||||
if (!initialData) return;
|
||||
const parsedKey = getApiKeyFromConfig(
|
||||
JSON.stringify(initialData.settingsConfig)
|
||||
JSON.stringify(initialData.settingsConfig),
|
||||
);
|
||||
if (parsedKey) setApiKey(parsedKey);
|
||||
}, [initialData]);
|
||||
@@ -1544,7 +1544,9 @@ const ProviderForm: React.FC<ProviderFormProps> = ({
|
||||
templateValueEntries.length > 0 && (
|
||||
<div className="space-y-3">
|
||||
<h3 className="text-sm font-semibold text-gray-900 dark:text-gray-100">
|
||||
{t("providerForm.parameterConfig", { name: selectedTemplatePreset.name.trim() })}
|
||||
{t("providerForm.parameterConfig", {
|
||||
name: selectedTemplatePreset.name.trim(),
|
||||
})}
|
||||
</h3>
|
||||
<div className="space-y-4">
|
||||
{templateValueEntries.map(([key, config]) => (
|
||||
@@ -1583,14 +1585,14 @@ const ProviderForm: React.FC<ProviderFormProps> = ({
|
||||
applyTemplateValuesToConfigString(
|
||||
selectedTemplatePreset.settingsConfig,
|
||||
formData.settingsConfig,
|
||||
nextValues
|
||||
nextValues,
|
||||
);
|
||||
setFormData((prevForm) => ({
|
||||
...prevForm,
|
||||
settingsConfig: configString,
|
||||
}));
|
||||
setSettingsConfigError(
|
||||
validateSettingsConfig(configString)
|
||||
validateSettingsConfig(configString),
|
||||
);
|
||||
} catch (err) {
|
||||
console.error("更新模板值失败:", err);
|
||||
@@ -1830,7 +1832,7 @@ const ProviderForm: React.FC<ProviderFormProps> = ({
|
||||
onChange={(e) =>
|
||||
handleModelChange(
|
||||
"ANTHROPIC_SMALL_FAST_MODEL",
|
||||
e.target.value
|
||||
e.target.value,
|
||||
)
|
||||
}
|
||||
placeholder={t("providerForm.fastModelPlaceholder")}
|
||||
|
||||
@@ -170,7 +170,7 @@ const CodexConfigEditor: React.FC<CodexConfigEditorProps> = ({
|
||||
|
||||
trimmedBaseUrl,
|
||||
|
||||
trimmedModel
|
||||
trimmedModel,
|
||||
);
|
||||
|
||||
onAuthChange(JSON.stringify(auth, null, 2));
|
||||
@@ -208,7 +208,7 @@ const CodexConfigEditor: React.FC<CodexConfigEditorProps> = ({
|
||||
};
|
||||
|
||||
const handleTemplateInputKeyDown = (
|
||||
e: React.KeyboardEvent<HTMLInputElement>
|
||||
e: React.KeyboardEvent<HTMLInputElement>,
|
||||
) => {
|
||||
if (e.key === "Enter") {
|
||||
e.preventDefault();
|
||||
@@ -509,7 +509,7 @@ const CodexConfigEditor: React.FC<CodexConfigEditorProps> = ({
|
||||
{JSON.stringify(
|
||||
generateThirdPartyAuth(templateApiKey),
|
||||
null,
|
||||
2
|
||||
2,
|
||||
)}
|
||||
</pre>
|
||||
</div>
|
||||
@@ -526,7 +526,7 @@ const CodexConfigEditor: React.FC<CodexConfigEditorProps> = ({
|
||||
|
||||
templateBaseUrl,
|
||||
|
||||
templateModelName
|
||||
templateModelName,
|
||||
)
|
||||
: ""}
|
||||
</pre>
|
||||
|
||||
@@ -210,8 +210,7 @@ const EndpointSpeedTest: React.FC<EndpointSpeedTestProps> = ({
|
||||
});
|
||||
}, [entries]);
|
||||
|
||||
const handleAddEndpoint = useCallback(
|
||||
async () => {
|
||||
const handleAddEndpoint = useCallback(async () => {
|
||||
const candidate = customUrl.trim();
|
||||
let errorMsg: string | null = null;
|
||||
|
||||
@@ -277,14 +276,19 @@ const EndpointSpeedTest: React.FC<EndpointSpeedTestProps> = ({
|
||||
|
||||
setCustomUrl("");
|
||||
} catch (error) {
|
||||
const message =
|
||||
error instanceof Error ? error.message : String(error);
|
||||
const message = error instanceof Error ? error.message : String(error);
|
||||
setAddError(message || t("endpointTest.saveFailed"));
|
||||
console.error(t("endpointTest.addEndpointFailed"), error);
|
||||
}
|
||||
},
|
||||
[customUrl, entries, normalizedSelected, onChange, appType, providerId, t],
|
||||
);
|
||||
}, [
|
||||
customUrl,
|
||||
entries,
|
||||
normalizedSelected,
|
||||
onChange,
|
||||
appType,
|
||||
providerId,
|
||||
t,
|
||||
]);
|
||||
|
||||
const handleRemoveEndpoint = useCallback(
|
||||
async (entry: EndpointEntry) => {
|
||||
@@ -358,7 +362,9 @@ const EndpointSpeedTest: React.FC<EndpointSpeedTestProps> = ({
|
||||
return {
|
||||
...entry,
|
||||
latency:
|
||||
typeof match.latency === "number" ? Math.round(match.latency) : null,
|
||||
typeof match.latency === "number"
|
||||
? Math.round(match.latency)
|
||||
: null,
|
||||
status: match.status,
|
||||
error: match.error ?? null,
|
||||
};
|
||||
@@ -367,7 +373,9 @@ const EndpointSpeedTest: React.FC<EndpointSpeedTestProps> = ({
|
||||
|
||||
if (autoSelect) {
|
||||
const successful = results
|
||||
.filter((item) => typeof item.latency === "number" && item.latency !== null)
|
||||
.filter(
|
||||
(item) => typeof item.latency === "number" && item.latency !== null,
|
||||
)
|
||||
.sort((a, b) => (a.latency! || 0) - (b.latency! || 0));
|
||||
const best = successful[0];
|
||||
if (best && best.url && best.url !== normalizedSelected) {
|
||||
@@ -376,7 +384,9 @@ const EndpointSpeedTest: React.FC<EndpointSpeedTestProps> = ({
|
||||
}
|
||||
} catch (error) {
|
||||
const message =
|
||||
error instanceof Error ? error.message : `${t("endpointTest.testFailed", { error: String(error) })}`;
|
||||
error instanceof Error
|
||||
? error.message
|
||||
: `${t("endpointTest.testFailed", { error: String(error) })}`;
|
||||
setLastError(message);
|
||||
} finally {
|
||||
setIsTesting(false);
|
||||
@@ -554,7 +564,8 @@ const EndpointSpeedTest: React.FC<EndpointSpeedTestProps> = ({
|
||||
<div className="flex items-center gap-2">
|
||||
{latency !== null ? (
|
||||
<div className="text-right">
|
||||
<div className={`font-mono text-sm font-medium ${
|
||||
<div
|
||||
className={`font-mono text-sm font-medium ${
|
||||
latency < 300
|
||||
? "text-green-600 dark:text-green-400"
|
||||
: latency < 500
|
||||
@@ -562,14 +573,17 @@ const EndpointSpeedTest: React.FC<EndpointSpeedTestProps> = ({
|
||||
: latency < 800
|
||||
? "text-orange-600 dark:text-orange-400"
|
||||
: "text-red-600 dark:text-red-400"
|
||||
}`}>
|
||||
}`}
|
||||
>
|
||||
{latency}ms
|
||||
</div>
|
||||
</div>
|
||||
) : isTesting ? (
|
||||
<Loader2 className="h-4 w-4 animate-spin text-gray-400" />
|
||||
) : entry.error ? (
|
||||
<div className="text-xs text-gray-400">{t("endpointTest.failed")}</div>
|
||||
<div className="text-xs text-gray-400">
|
||||
{t("endpointTest.failed")}
|
||||
</div>
|
||||
) : (
|
||||
<div className="text-xs text-gray-400">—</div>
|
||||
)}
|
||||
|
||||
@@ -52,7 +52,11 @@ const KimiModelSelector: React.FC<KimiModelSelectorProps> = ({
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(t("kimiSelector.requestFailed", { error: `${response.status} ${response.statusText}` }));
|
||||
throw new Error(
|
||||
t("kimiSelector.requestFailed", {
|
||||
error: `${response.status} ${response.statusText}`,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
@@ -64,7 +68,11 @@ const KimiModelSelector: React.FC<KimiModelSelectorProps> = ({
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(t("kimiSelector.fetchModelsFailed") + ":", err);
|
||||
setError(err instanceof Error ? err.message : t("kimiSelector.fetchModelsFailed"));
|
||||
setError(
|
||||
err instanceof Error
|
||||
? err.message
|
||||
: t("kimiSelector.fetchModelsFailed"),
|
||||
);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ interface ProviderListProps {
|
||||
onNotify?: (
|
||||
message: string,
|
||||
type: "success" | "error",
|
||||
duration?: number
|
||||
duration?: number,
|
||||
) => void;
|
||||
}
|
||||
|
||||
@@ -154,7 +154,7 @@ const ProviderList: React.FC<ProviderListProps> = ({
|
||||
<div
|
||||
key={provider.id}
|
||||
className={cn(
|
||||
isCurrent ? cardStyles.selected : cardStyles.interactive
|
||||
isCurrent ? cardStyles.selected : cardStyles.interactive,
|
||||
)}
|
||||
>
|
||||
<div className="flex items-start justify-between">
|
||||
@@ -167,7 +167,7 @@ const ProviderList: React.FC<ProviderListProps> = ({
|
||||
<div
|
||||
className={cn(
|
||||
badgeStyles.success,
|
||||
!isCurrent && "invisible"
|
||||
!isCurrent && "invisible",
|
||||
)}
|
||||
>
|
||||
<CheckCircle2 size={12} />
|
||||
@@ -183,7 +183,9 @@ const ProviderList: React.FC<ProviderListProps> = ({
|
||||
handleUrlClick(provider.websiteUrl!);
|
||||
}}
|
||||
className="inline-flex items-center gap-1 text-blue-500 dark:text-blue-400 hover:opacity-90 transition-colors"
|
||||
title={t("providerForm.visitWebsite", { url: provider.websiteUrl })}
|
||||
title={t("providerForm.visitWebsite", {
|
||||
url: provider.websiteUrl,
|
||||
})}
|
||||
>
|
||||
{provider.websiteUrl}
|
||||
</button>
|
||||
@@ -212,7 +214,7 @@ const ProviderList: React.FC<ProviderListProps> = ({
|
||||
"inline-flex items-center gap-1 px-3 py-1.5 text-sm font-medium rounded-md transition-colors w-full whitespace-nowrap justify-center",
|
||||
claudeApplied
|
||||
? "border border-gray-300 text-gray-600 hover:border-red-300 hover:text-red-600 hover:bg-red-50 dark:border-gray-600 dark:text-gray-400 dark:hover:border-red-800 dark:hover:text-red-400 dark:hover:bg-red-900/20"
|
||||
: "border border-gray-300 text-gray-700 hover:border-green-300 hover:text-green-600 hover:bg-green-50 dark:border-gray-600 dark:text-gray-300 dark:hover:border-green-700 dark:hover:text-green-400 dark:hover:bg-green-900/20"
|
||||
: "border border-gray-300 text-gray-700 hover:border-green-300 hover:text-green-600 hover:bg-green-50 dark:border-gray-600 dark:text-gray-300 dark:hover:border-green-700 dark:hover:text-green-400 dark:hover:bg-green-900/20",
|
||||
)}
|
||||
title={
|
||||
claudeApplied
|
||||
@@ -234,7 +236,7 @@ const ProviderList: React.FC<ProviderListProps> = ({
|
||||
"inline-flex items-center gap-1.5 px-3 py-1.5 text-sm font-medium rounded-md transition-colors w-[90px] justify-center whitespace-nowrap",
|
||||
isCurrent
|
||||
? "bg-gray-100 text-gray-400 dark:bg-gray-800 dark:text-gray-500 cursor-not-allowed"
|
||||
: "bg-blue-500 text-white hover:bg-blue-600 dark:bg-blue-600 dark:hover:bg-blue-700"
|
||||
: "bg-blue-500 text-white hover:bg-blue-600 dark:bg-blue-600 dark:hover:bg-blue-700",
|
||||
)}
|
||||
>
|
||||
{isCurrent ? <Check size={14} /> : <Play size={14} />}
|
||||
@@ -256,7 +258,7 @@ const ProviderList: React.FC<ProviderListProps> = ({
|
||||
buttonStyles.icon,
|
||||
isCurrent
|
||||
? "text-gray-400 cursor-not-allowed"
|
||||
: "text-gray-500 hover:text-red-500 hover:bg-red-100 dark:text-gray-400 dark:hover:text-red-400 dark:hover:bg-red-500/10"
|
||||
: "text-gray-500 hover:text-red-500 hover:bg-red-100 dark:text-gray-400 dark:hover:text-red-400 dark:hover:bg-red-500/10",
|
||||
)}
|
||||
title={t("provider.deleteProvider")}
|
||||
>
|
||||
|
||||
@@ -26,7 +26,10 @@ interface SettingsModalProps {
|
||||
onImportSuccess?: () => void | Promise<void>;
|
||||
}
|
||||
|
||||
export default function SettingsModal({ onClose, onImportSuccess }: SettingsModalProps) {
|
||||
export default function SettingsModal({
|
||||
onClose,
|
||||
onImportSuccess,
|
||||
}: SettingsModalProps) {
|
||||
const { t, i18n } = useTranslation();
|
||||
|
||||
const normalizeLanguage = (lang?: string | null): "zh" | "en" =>
|
||||
@@ -67,10 +70,12 @@ export default function SettingsModal({ onClose, onImportSuccess }: SettingsModa
|
||||
|
||||
// 导入/导出相关状态
|
||||
const [isImporting, setIsImporting] = useState(false);
|
||||
const [importStatus, setImportStatus] = useState<'idle' | 'importing' | 'success' | 'error'>('idle');
|
||||
const [importStatus, setImportStatus] = useState<
|
||||
"idle" | "importing" | "success" | "error"
|
||||
>("idle");
|
||||
const [importError, setImportError] = useState<string>("");
|
||||
const [importBackupId, setImportBackupId] = useState<string>("");
|
||||
const [selectedImportFile, setSelectedImportFile] = useState<string>('');
|
||||
const [selectedImportFile, setSelectedImportFile] = useState<string>("");
|
||||
|
||||
useEffect(() => {
|
||||
loadSettings();
|
||||
@@ -340,7 +345,7 @@ export default function SettingsModal({ onClose, onImportSuccess }: SettingsModa
|
||||
// 如果未知或为空,回退到 releases 首页
|
||||
if (!targetVersion || targetVersion === unknownLabel) {
|
||||
await window.api.openExternal(
|
||||
"https://github.com/farion1231/cc-switch/releases"
|
||||
"https://github.com/farion1231/cc-switch/releases",
|
||||
);
|
||||
return;
|
||||
}
|
||||
@@ -348,7 +353,7 @@ export default function SettingsModal({ onClose, onImportSuccess }: SettingsModa
|
||||
? targetVersion
|
||||
: `v${targetVersion}`;
|
||||
await window.api.openExternal(
|
||||
`https://github.com/farion1231/cc-switch/releases/tag/${tag}`
|
||||
`https://github.com/farion1231/cc-switch/releases/tag/${tag}`,
|
||||
);
|
||||
} catch (error) {
|
||||
console.error(t("console.openReleaseNotesFailed"), error);
|
||||
@@ -358,7 +363,7 @@ export default function SettingsModal({ onClose, onImportSuccess }: SettingsModa
|
||||
// 导出配置处理函数
|
||||
const handleExportConfig = async () => {
|
||||
try {
|
||||
const defaultName = `cc-switch-config-${new Date().toISOString().split('T')[0]}.json`;
|
||||
const defaultName = `cc-switch-config-${new Date().toISOString().split("T")[0]}.json`;
|
||||
const filePath = await window.api.saveFileDialog(defaultName);
|
||||
|
||||
if (!filePath) return; // 用户取消了
|
||||
@@ -380,8 +385,8 @@ export default function SettingsModal({ onClose, onImportSuccess }: SettingsModa
|
||||
const filePath = await window.api.openFileDialog();
|
||||
if (filePath) {
|
||||
setSelectedImportFile(filePath);
|
||||
setImportStatus('idle'); // 重置状态
|
||||
setImportError('');
|
||||
setImportStatus("idle"); // 重置状态
|
||||
setImportError("");
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(t("settings.selectFileFailed") + ":", error);
|
||||
@@ -394,22 +399,22 @@ export default function SettingsModal({ onClose, onImportSuccess }: SettingsModa
|
||||
if (!selectedImportFile || isImporting) return;
|
||||
|
||||
setIsImporting(true);
|
||||
setImportStatus('importing');
|
||||
setImportStatus("importing");
|
||||
|
||||
try {
|
||||
const result = await window.api.importConfigFromFile(selectedImportFile);
|
||||
|
||||
if (result.success) {
|
||||
setImportBackupId(result.backupId || '');
|
||||
setImportStatus('success');
|
||||
setImportBackupId(result.backupId || "");
|
||||
setImportStatus("success");
|
||||
// ImportProgressModal 会在2秒后触发数据刷新回调
|
||||
} else {
|
||||
setImportError(result.message || t("settings.configCorrupted"));
|
||||
setImportStatus('error');
|
||||
setImportStatus("error");
|
||||
}
|
||||
} catch (error) {
|
||||
setImportError(String(error));
|
||||
setImportStatus('error');
|
||||
setImportStatus("error");
|
||||
} finally {
|
||||
setIsImporting(false);
|
||||
}
|
||||
@@ -642,18 +647,22 @@ export default function SettingsModal({ onClose, onImportSuccess }: SettingsModa
|
||||
disabled={!selectedImportFile || isImporting}
|
||||
className={`px-3 py-2 text-xs font-medium rounded-lg transition-colors text-white ${
|
||||
!selectedImportFile || isImporting
|
||||
? 'bg-gray-400 cursor-not-allowed'
|
||||
: 'bg-blue-500 hover:bg-blue-600 dark:bg-blue-600 dark:hover:bg-blue-700'
|
||||
? "bg-gray-400 cursor-not-allowed"
|
||||
: "bg-blue-500 hover:bg-blue-600 dark:bg-blue-600 dark:hover:bg-blue-700"
|
||||
}`}
|
||||
>
|
||||
{isImporting ? t("settings.importing") : t("settings.import")}
|
||||
{isImporting
|
||||
? t("settings.importing")
|
||||
: t("settings.import")}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* 显示选择的文件 */}
|
||||
{selectedImportFile && (
|
||||
<div className="text-xs text-gray-600 dark:text-gray-400 px-2 py-1 bg-gray-50 dark:bg-gray-900 rounded break-all">
|
||||
{selectedImportFile.split('/').pop() || selectedImportFile.split('\\').pop() || selectedImportFile}
|
||||
{selectedImportFile.split("/").pop() ||
|
||||
selectedImportFile.split("\\").pop() ||
|
||||
selectedImportFile}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
@@ -757,15 +766,15 @@ export default function SettingsModal({ onClose, onImportSuccess }: SettingsModa
|
||||
</div>
|
||||
|
||||
{/* Import Progress Modal */}
|
||||
{importStatus !== 'idle' && (
|
||||
{importStatus !== "idle" && (
|
||||
<ImportProgressModal
|
||||
status={importStatus}
|
||||
message={importError}
|
||||
backupId={importBackupId}
|
||||
onComplete={() => {
|
||||
setImportStatus('idle');
|
||||
setImportError('');
|
||||
setSelectedImportFile('');
|
||||
setImportStatus("idle");
|
||||
setImportError("");
|
||||
setSelectedImportFile("");
|
||||
}}
|
||||
onSuccess={() => {
|
||||
if (onImportSuccess) {
|
||||
@@ -773,7 +782,12 @@ export default function SettingsModal({ onClose, onImportSuccess }: SettingsModa
|
||||
}
|
||||
void window.api
|
||||
.updateTrayMenu()
|
||||
.catch((error) => console.error("[SettingsModal] Failed to refresh tray menu", error));
|
||||
.catch((error) =>
|
||||
console.error(
|
||||
"[SettingsModal] Failed to refresh tray menu",
|
||||
error,
|
||||
),
|
||||
);
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
|
||||
@@ -32,7 +32,7 @@ export function generateThirdPartyAuth(apiKey: string): Record<string, any> {
|
||||
export function generateThirdPartyConfig(
|
||||
providerName: string,
|
||||
baseUrl: string,
|
||||
modelName = "gpt-5-codex"
|
||||
modelName = "gpt-5-codex",
|
||||
): string {
|
||||
// 清理供应商名称,确保符合TOML键名规范
|
||||
const cleanProviderName =
|
||||
@@ -71,7 +71,7 @@ export const codexProviderPresets: CodexProviderPreset[] = [
|
||||
config: generateThirdPartyConfig(
|
||||
"packycode",
|
||||
"https://codex-api.packycode.com/v1",
|
||||
"gpt-5-codex"
|
||||
"gpt-5-codex",
|
||||
),
|
||||
// Codex 请求地址候选(用于地址管理/测速)
|
||||
endpointCandidates: [
|
||||
|
||||
@@ -110,7 +110,8 @@ export const providerPresets: ProviderPreset[] = [
|
||||
apiKeyUrl: "https://console.streamlake.ai/console/wanqing/api-key",
|
||||
settingsConfig: {
|
||||
env: {
|
||||
ANTHROPIC_BASE_URL: "https://vanchin.streamlake.ai/api/gateway/v1/endpoints/${ENDPOINT_ID}/claude-code-proxy",
|
||||
ANTHROPIC_BASE_URL:
|
||||
"https://vanchin.streamlake.ai/api/gateway/v1/endpoints/${ENDPOINT_ID}/claude-code-proxy",
|
||||
ANTHROPIC_AUTH_TOKEN: "",
|
||||
ANTHROPIC_MODEL: "KAT-Coder",
|
||||
ANTHROPIC_SMALL_FAST_MODEL: "KAT-Coder",
|
||||
@@ -146,5 +147,4 @@ export const providerPresets: ProviderPreset[] = [
|
||||
],
|
||||
category: "third_party",
|
||||
},
|
||||
|
||||
];
|
||||
|
||||
@@ -20,7 +20,8 @@ const getInitialLanguage = (): "zh" | "en" => {
|
||||
|
||||
const navigatorLang =
|
||||
typeof navigator !== "undefined"
|
||||
? navigator.language?.toLowerCase() ?? navigator.languages?.[0]?.toLowerCase()
|
||||
? (navigator.language?.toLowerCase() ??
|
||||
navigator.languages?.[0]?.toLowerCase())
|
||||
: undefined;
|
||||
|
||||
if (navigatorLang?.startsWith("zh")) {
|
||||
|
||||
@@ -22,9 +22,10 @@ export const isLinux = (): boolean => {
|
||||
try {
|
||||
const ua = navigator.userAgent || "";
|
||||
// WebKitGTK/Chromium 在 Linux/Wayland/X11 下 UA 通常包含 Linux 或 X11
|
||||
return /linux|x11/i.test(ua) && !/android/i.test(ua) && !isMac() && !isWindows();
|
||||
return (
|
||||
/linux|x11/i.test(ua) && !/android/i.test(ua) && !isMac() && !isWindows()
|
||||
);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -150,7 +150,9 @@ export const tauriAPI = {
|
||||
},
|
||||
|
||||
// 选择配置目录(可选默认路径)
|
||||
selectConfigDirectory: async (defaultPath?: string): Promise<string | null> => {
|
||||
selectConfigDirectory: async (
|
||||
defaultPath?: string,
|
||||
): Promise<string | null> => {
|
||||
try {
|
||||
// 后端参数为 snake_case:default_path
|
||||
return await invoke("pick_directory", { default_path: defaultPath });
|
||||
@@ -384,7 +386,9 @@ export const tauriAPI = {
|
||||
|
||||
// theirs: 导入导出与文件对话框
|
||||
// 导出配置到文件
|
||||
exportConfigToFile: async (filePath: string): Promise<{
|
||||
exportConfigToFile: async (
|
||||
filePath: string,
|
||||
): Promise<{
|
||||
success: boolean;
|
||||
message: string;
|
||||
filePath: string;
|
||||
@@ -398,7 +402,9 @@ export const tauriAPI = {
|
||||
},
|
||||
|
||||
// 从文件导入配置
|
||||
importConfigFromFile: async (filePath: string): Promise<{
|
||||
importConfigFromFile: async (
|
||||
filePath: string,
|
||||
): Promise<{
|
||||
success: boolean;
|
||||
message: string;
|
||||
backupId?: string;
|
||||
@@ -415,7 +421,9 @@ export const tauriAPI = {
|
||||
saveFileDialog: async (defaultName: string): Promise<string | null> => {
|
||||
try {
|
||||
// 后端参数为 snake_case:default_name
|
||||
const result = await invoke<string | null>("save_file_dialog", { default_name: defaultName });
|
||||
const result = await invoke<string | null>("save_file_dialog", {
|
||||
default_name: defaultName,
|
||||
});
|
||||
return result;
|
||||
} catch (error) {
|
||||
console.error("打开保存对话框失败:", error);
|
||||
|
||||
@@ -25,5 +25,5 @@ ReactDOM.createRoot(document.getElementById("root")!).render(
|
||||
<UpdateProvider>
|
||||
<App />
|
||||
</UpdateProvider>
|
||||
</React.StrictMode>
|
||||
</React.StrictMode>,
|
||||
);
|
||||
|
||||
@@ -178,16 +178,16 @@ export const getApiKeyFromConfig = (jsonString: string): string => {
|
||||
// 模板变量替换
|
||||
export const applyTemplateValues = (
|
||||
config: any,
|
||||
templateValues: Record<string, TemplateValueConfig> | undefined
|
||||
templateValues: Record<string, TemplateValueConfig> | undefined,
|
||||
): any => {
|
||||
const resolvedValues = Object.fromEntries(
|
||||
Object.entries(templateValues ?? {}).map(([key, value]) => {
|
||||
const resolvedValue =
|
||||
value.editorValue !== undefined
|
||||
? value.editorValue
|
||||
: value.defaultValue ?? "";
|
||||
: (value.defaultValue ?? "");
|
||||
return [key, resolvedValue];
|
||||
})
|
||||
}),
|
||||
);
|
||||
|
||||
const replaceInString = (str: string): string => {
|
||||
@@ -384,6 +384,7 @@ export const setCodexBaseUrl = (
|
||||
return configText.replace(pattern, replacementLine);
|
||||
}
|
||||
|
||||
const prefix = configText && !configText.endsWith("\n") ? `${configText}\n` : configText;
|
||||
const prefix =
|
||||
configText && !configText.endsWith("\n") ? `${configText}\n` : configText;
|
||||
return `${prefix}${replacementLine}\n`;
|
||||
};
|
||||
|
||||
14
src/vite-env.d.ts
vendored
14
src/vite-env.d.ts
vendored
@@ -64,31 +64,33 @@ declare global {
|
||||
testApiEndpoints: (
|
||||
urls: string[],
|
||||
options?: { timeoutSecs?: number },
|
||||
) => Promise<Array<{
|
||||
) => Promise<
|
||||
Array<{
|
||||
url: string;
|
||||
latency: number | null;
|
||||
status?: number;
|
||||
error?: string;
|
||||
}>>;
|
||||
}>
|
||||
>;
|
||||
// 自定义端点管理
|
||||
getCustomEndpoints: (
|
||||
appType: AppType,
|
||||
providerId: string
|
||||
providerId: string,
|
||||
) => Promise<CustomEndpoint[]>;
|
||||
addCustomEndpoint: (
|
||||
appType: AppType,
|
||||
providerId: string,
|
||||
url: string
|
||||
url: string,
|
||||
) => Promise<void>;
|
||||
removeCustomEndpoint: (
|
||||
appType: AppType,
|
||||
providerId: string,
|
||||
url: string
|
||||
url: string,
|
||||
) => Promise<void>;
|
||||
updateEndpointLastUsed: (
|
||||
appType: AppType,
|
||||
providerId: string,
|
||||
url: string
|
||||
url: string,
|
||||
) => Promise<void>;
|
||||
};
|
||||
platform: {
|
||||
|
||||
Reference in New Issue
Block a user