mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-07-28 00:35:32 +08:00
e7badb1a24
* refactor(ui): simplify UpdateBadge to minimal dot indicator * feat(provider): add individual test and proxy config for providers Add support for provider-specific model test and proxy configurations: - Add ProviderTestConfig and ProviderProxyConfig types in Rust and TypeScript - Create ProviderAdvancedConfig component with collapsible panels - Update stream_check service to merge provider config with global config - Proxy config UI follows global proxy style (single URL input) Provider-level configs stored in meta field, no database schema changes needed. * feat(ui): add failover toggle and improve proxy controls - Add FailoverToggle component with slide animation - Simplify ProxyToggle style to match FailoverToggle - Add usage statistics button when proxy is active - Fix i18n parameter passing for failover messages - Add missing failover translation keys (inQueue, addQueue, priority) - Replace AboutSection icon with app logo * fix(proxy): support system proxy fallback and provider-level proxy config - Remove no_proxy() calls in http_client.rs to allow system proxy fallback - Add get_for_provider() to build HTTP client with provider-specific proxy - Update forwarder.rs and stream_check.rs to use provider proxy config - Fix EditProviderDialog.tsx to include provider.meta in useMemo deps - Add useEffect in ProviderAdvancedConfig.tsx to sync expand state Fixes #636 Fixes #583 * fix(ui): sync toast theme with app setting * feat(settings): add log config management Fixes #612 Fixes #514 * fix(proxy): increase request body size limit to 200MB Fixes #666 * docs(proxy): update timeout config descriptions and defaults Fixes #612 * fix(proxy): filter x-goog-api-key header to prevent duplication * fix(proxy): prevent proxy recursion when system proxy points to localhost Detect if HTTP_PROXY, HTTPS_PROXY, or ALL_PROXY environment variables point to loopback addresses (localhost, 127.0.0.1), and bypass system proxy in such cases to avoid infinite request loops. * fix(i18n): add providerAdvanced i18n keys and fix failover toast parameter - Add providerAdvanced.* i18n keys to en.json, zh.json, and ja.json - Fix failover toggleFailed toast to pass detail parameter - Remove Chinese fallback text from UI for English/Japanese users * fix(tray): restore tray-provider events and enable Auto failover properly - Emit provider-switched event on tray provider click (backward compatibility) - Auto button now: starts proxy, takes over live config, enables failover * fix(log): enable dynamic log level and single file mode - Initialize log at Trace level for dynamic adjustment - Change rotation strategy to KeepSome(1) for single file - Set max file size to 1GB - Delete old log file on startup for clean start * fix(tray): fix clippy uninlined format args warning Use inline format arguments: {app_type_str} instead of {} * fix(provider): allow typing :// in endpoint URL inputs Change input type from "url" to "text" to prevent browser URL validation from blocking :// input. Closes #681 * fix(stream-check): use Gemini native streaming API format - Change endpoint from OpenAI-compatible to native streamGenerateContent - Add alt=sse parameter for SSE format response - Use x-goog-api-key header instead of Bearer token - Convert request body to Gemini contents/parts format * feat(proxy): add request logging for debugging Add debug logs for outgoing requests including URL and body content with byte size, matching the existing response logging format. * fix(log): prevent usize underflow in KeepSome rotation strategy KeepSome(n) internally computes n-2, so n=1 causes underflow. Use KeepSome(2) as the minimum safe value.
199 lines
6.3 KiB
TypeScript
199 lines
6.3 KiB
TypeScript
import { useCallback, useEffect, useMemo, useState } from "react";
|
|
import { useTranslation } from "react-i18next";
|
|
import { Save } from "lucide-react";
|
|
import { Button } from "@/components/ui/button";
|
|
import { FullScreenPanel } from "@/components/common/FullScreenPanel";
|
|
import type { Provider } from "@/types";
|
|
import {
|
|
ProviderForm,
|
|
type ProviderFormValues,
|
|
} from "@/components/providers/forms/ProviderForm";
|
|
import { providersApi, vscodeApi, type AppId } from "@/lib/api";
|
|
|
|
interface EditProviderDialogProps {
|
|
open: boolean;
|
|
provider: Provider | null;
|
|
onOpenChange: (open: boolean) => void;
|
|
onSubmit: (provider: Provider) => Promise<void> | void;
|
|
appId: AppId;
|
|
isProxyTakeover?: boolean; // 代理接管模式下不读取 live(避免显示被接管后的代理配置)
|
|
}
|
|
|
|
export function EditProviderDialog({
|
|
open,
|
|
provider,
|
|
onOpenChange,
|
|
onSubmit,
|
|
appId,
|
|
isProxyTakeover = false,
|
|
}: EditProviderDialogProps) {
|
|
const { t } = useTranslation();
|
|
|
|
// 默认使用传入的 provider.settingsConfig,若当前编辑对象是"当前生效供应商",则尝试读取实时配置替换初始值
|
|
const [liveSettings, setLiveSettings] = useState<Record<
|
|
string,
|
|
unknown
|
|
> | null>(null);
|
|
|
|
// 使用 ref 标记是否已经加载过,防止重复读取覆盖用户编辑
|
|
const [hasLoadedLive, setHasLoadedLive] = useState(false);
|
|
|
|
useEffect(() => {
|
|
let cancelled = false;
|
|
const load = async () => {
|
|
if (!open || !provider) {
|
|
setLiveSettings(null);
|
|
setHasLoadedLive(false);
|
|
return;
|
|
}
|
|
|
|
// 关键修复:只在首次打开时加载一次
|
|
if (hasLoadedLive) {
|
|
return;
|
|
}
|
|
|
|
// 代理接管模式:Live 配置已被代理改写,读取 live 会导致编辑界面展示代理地址/占位符等内容
|
|
// 因此直接回退到 SSOT(数据库)配置,避免用户困惑与误保存
|
|
if (isProxyTakeover) {
|
|
if (!cancelled) {
|
|
setLiveSettings(null);
|
|
setHasLoadedLive(true);
|
|
}
|
|
return;
|
|
}
|
|
|
|
// OpenCode uses additive mode - each provider's config is stored independently in DB
|
|
// Reading live config would return the full opencode.json (with $schema, provider, mcp etc.)
|
|
// instead of just the provider fragment, causing incorrect nested structure on save
|
|
if (appId === "opencode") {
|
|
if (!cancelled) {
|
|
setLiveSettings(null);
|
|
setHasLoadedLive(true);
|
|
}
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const currentId = await providersApi.getCurrent(appId);
|
|
if (currentId && provider.id === currentId) {
|
|
try {
|
|
const live = (await vscodeApi.getLiveProviderSettings(
|
|
appId,
|
|
)) as Record<string, unknown>;
|
|
if (!cancelled && live && typeof live === "object") {
|
|
setLiveSettings(live);
|
|
setHasLoadedLive(true);
|
|
}
|
|
} catch {
|
|
// 读取实时配置失败则回退到 SSOT(不打断编辑流程)
|
|
if (!cancelled) {
|
|
setLiveSettings(null);
|
|
setHasLoadedLive(true);
|
|
}
|
|
}
|
|
} else {
|
|
if (!cancelled) {
|
|
setLiveSettings(null);
|
|
setHasLoadedLive(true);
|
|
}
|
|
}
|
|
} finally {
|
|
// no-op
|
|
}
|
|
};
|
|
void load();
|
|
return () => {
|
|
cancelled = true;
|
|
};
|
|
}, [open, provider?.id, appId, hasLoadedLive, isProxyTakeover]); // 只依赖 provider.id,不依赖整个 provider 对象
|
|
|
|
const initialSettingsConfig = useMemo(() => {
|
|
return (liveSettings ?? provider?.settingsConfig ?? {}) as Record<
|
|
string,
|
|
unknown
|
|
>;
|
|
}, [liveSettings, provider?.settingsConfig]); // 只依赖 settingsConfig,不依赖整个 provider
|
|
|
|
// 固定 initialData,防止 provider 对象更新时重置表单
|
|
const initialData = useMemo(() => {
|
|
if (!provider) return null;
|
|
return {
|
|
name: provider.name,
|
|
notes: provider.notes,
|
|
websiteUrl: provider.websiteUrl,
|
|
settingsConfig: initialSettingsConfig,
|
|
category: provider.category,
|
|
meta: provider.meta,
|
|
icon: provider.icon,
|
|
iconColor: provider.iconColor,
|
|
};
|
|
}, [
|
|
open, // 修复:编辑保存后再次打开显示旧数据,依赖 open 确保每次打开时重新读取最新 provider 数据
|
|
provider?.id, // 只依赖 ID,provider 对象更新不会触发重新计算
|
|
provider?.meta, // 需要依赖 meta 以便正确初始化 testConfig 和 proxyConfig
|
|
initialSettingsConfig,
|
|
]);
|
|
|
|
const handleSubmit = useCallback(
|
|
async (values: ProviderFormValues) => {
|
|
if (!provider) return;
|
|
|
|
// 注意:values.settingsConfig 已经是最终的配置字符串
|
|
// ProviderForm 已经为不同的 app 类型(Claude/Codex/Gemini)正确组装了配置
|
|
const parsedConfig = JSON.parse(values.settingsConfig) as Record<
|
|
string,
|
|
unknown
|
|
>;
|
|
|
|
const updatedProvider: Provider = {
|
|
...provider,
|
|
name: values.name.trim(),
|
|
notes: values.notes?.trim() || undefined,
|
|
websiteUrl: values.websiteUrl?.trim() || undefined,
|
|
settingsConfig: parsedConfig,
|
|
icon: values.icon?.trim() || undefined,
|
|
iconColor: values.iconColor?.trim() || undefined,
|
|
...(values.presetCategory ? { category: values.presetCategory } : {}),
|
|
// 保留或更新 meta 字段
|
|
...(values.meta ? { meta: values.meta } : {}),
|
|
};
|
|
|
|
await onSubmit(updatedProvider);
|
|
onOpenChange(false);
|
|
},
|
|
[onSubmit, onOpenChange, provider],
|
|
);
|
|
|
|
if (!provider || !initialData) {
|
|
return null;
|
|
}
|
|
|
|
return (
|
|
<FullScreenPanel
|
|
isOpen={open}
|
|
title={t("provider.editProvider")}
|
|
onClose={() => onOpenChange(false)}
|
|
footer={
|
|
<Button
|
|
type="submit"
|
|
form="provider-form"
|
|
className="bg-primary text-primary-foreground hover:bg-primary/90"
|
|
>
|
|
<Save className="h-4 w-4 mr-2" />
|
|
{t("common.save")}
|
|
</Button>
|
|
}
|
|
>
|
|
<ProviderForm
|
|
appId={appId}
|
|
providerId={provider.id}
|
|
submitLabel={t("common.save")}
|
|
onSubmit={handleSubmit}
|
|
onCancel={() => onOpenChange(false)}
|
|
initialData={initialData}
|
|
showButtons={false}
|
|
/>
|
|
</FullScreenPanel>
|
|
);
|
|
}
|