mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-07-30 10:25:05 +08:00
fix(proxy): unify preview/runtime URL rules and proxy checks
- align Codex preview URL building with runtime /v1 normalization - align Claude full-url detection with runtime adapter patterns - reflect Claude ?beta=true in proxy preview and avoid query-only mismatch false positives - enforce openai_chat proxy requirement even when baseUrl is missing - wire Codex api format selector through ProviderForm and persist meta/config - fix EndpointField stale async preview race on clear - add pending feedback for ProxyToggle pre-disable checks - deduplicate provider baseUrl extraction into a shared util
This commit is contained in:
@@ -35,7 +35,11 @@ import {
|
||||
} from "@/config/opencodeProviderPresets";
|
||||
import { OpenCodeFormFields } from "./OpenCodeFormFields";
|
||||
import type { UniversalProviderPreset } from "@/config/universalProviderPresets";
|
||||
import { applyTemplateValues } from "@/utils/providerConfigUtils";
|
||||
import {
|
||||
applyTemplateValues,
|
||||
extractCodexWireApi,
|
||||
setCodexWireApi,
|
||||
} from "@/utils/providerConfigUtils";
|
||||
import { mergeProviderMeta } from "@/utils/providerMetaUtils";
|
||||
import { getCodexCustomTemplate } from "@/config/codexTemplates";
|
||||
import CodexConfigEditor from "./CodexConfigEditor";
|
||||
@@ -46,7 +50,7 @@ import { Label } from "@/components/ui/label";
|
||||
import { ProviderPresetSelector } from "./ProviderPresetSelector";
|
||||
import { BasicFormFields } from "./BasicFormFields";
|
||||
import { ClaudeFormFields } from "./ClaudeFormFields";
|
||||
import { CodexFormFields } from "./CodexFormFields";
|
||||
import { CodexFormFields, type CodexApiFormat } from "./CodexFormFields";
|
||||
import { GeminiFormFields } from "./GeminiFormFields";
|
||||
import { OmoFormFields } from "./OmoFormFields";
|
||||
import { type OmoGlobalConfigFieldsRef } from "./OmoGlobalConfigFields";
|
||||
@@ -357,14 +361,26 @@ export function ProviderForm({
|
||||
onConfigChange: (config) => form.setValue("settingsConfig", config),
|
||||
});
|
||||
|
||||
const [localApiFormat, setLocalApiFormat] = useState<ClaudeApiFormat>(() => {
|
||||
if (appId !== "claude") return "anthropic";
|
||||
return initialData?.meta?.apiFormat ?? "anthropic";
|
||||
});
|
||||
const [localClaudeApiFormat, setLocalClaudeApiFormat] =
|
||||
useState<ClaudeApiFormat>(() => {
|
||||
if (appId !== "claude") return "anthropic";
|
||||
return initialData?.meta?.apiFormat === "openai_chat"
|
||||
? "openai_chat"
|
||||
: "anthropic";
|
||||
});
|
||||
|
||||
const handleApiFormatChange = useCallback((format: ClaudeApiFormat) => {
|
||||
setLocalApiFormat(format);
|
||||
}, []);
|
||||
const [localCodexApiFormat, setLocalCodexApiFormat] =
|
||||
useState<CodexApiFormat>(() => {
|
||||
if (appId !== "codex") return "responses";
|
||||
if (initialData?.meta?.apiFormat === "chat") return "chat";
|
||||
if (initialData?.meta?.apiFormat === "responses") return "responses";
|
||||
|
||||
const initialConfig = initialData?.settingsConfig?.config;
|
||||
if (typeof initialConfig === "string") {
|
||||
return extractCodexWireApi(initialConfig) ?? "responses";
|
||||
}
|
||||
return "responses";
|
||||
});
|
||||
|
||||
const {
|
||||
codexAuth,
|
||||
@@ -374,6 +390,7 @@ export function ProviderForm({
|
||||
codexModelName,
|
||||
codexAuthError,
|
||||
setCodexAuth,
|
||||
setCodexConfig,
|
||||
handleCodexApiKeyChange,
|
||||
handleCodexBaseUrlChange,
|
||||
handleCodexModelNameChange,
|
||||
@@ -392,6 +409,18 @@ export function ProviderForm({
|
||||
[originalHandleCodexConfigChange, debouncedValidate],
|
||||
);
|
||||
|
||||
const handleClaudeApiFormatChange = useCallback((format: ClaudeApiFormat) => {
|
||||
setLocalClaudeApiFormat(format);
|
||||
}, []);
|
||||
|
||||
const handleCodexApiFormatChange = useCallback(
|
||||
(format: CodexApiFormat) => {
|
||||
setLocalCodexApiFormat(format);
|
||||
setCodexConfig((prev) => setCodexWireApi(prev, format));
|
||||
},
|
||||
[setCodexConfig],
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
if (appId === "codex" && !initialData && selectedPresetId === "custom") {
|
||||
const template = getCodexCustomTemplate();
|
||||
@@ -1166,6 +1195,13 @@ export function ProviderForm({
|
||||
}
|
||||
}
|
||||
|
||||
const providerApiFormat =
|
||||
appId === "claude" && category !== "official"
|
||||
? localClaudeApiFormat
|
||||
: appId === "codex" && category !== "official"
|
||||
? localCodexApiFormat
|
||||
: undefined;
|
||||
|
||||
const baseMeta: ProviderMeta | undefined =
|
||||
payload.meta ?? (initialData?.meta ? { ...initialData.meta } : undefined);
|
||||
payload.meta = {
|
||||
@@ -1180,10 +1216,7 @@ export function ProviderForm({
|
||||
pricingConfig.enabled && pricingConfig.pricingModelSource !== "inherit"
|
||||
? pricingConfig.pricingModelSource
|
||||
: undefined,
|
||||
apiFormat:
|
||||
appId === "claude" && category !== "official"
|
||||
? localApiFormat
|
||||
: undefined,
|
||||
apiFormat: providerApiFormat,
|
||||
};
|
||||
|
||||
onSubmit(payload);
|
||||
@@ -1277,6 +1310,9 @@ export function ProviderForm({
|
||||
|
||||
if (appId === "codex") {
|
||||
const template = getCodexCustomTemplate();
|
||||
setLocalCodexApiFormat(
|
||||
extractCodexWireApi(template.config) ?? "responses",
|
||||
);
|
||||
resetCodexConfig(template.auth, template.config);
|
||||
}
|
||||
if (appId === "gemini") {
|
||||
@@ -1311,6 +1347,7 @@ export function ProviderForm({
|
||||
const auth = preset.auth ?? {};
|
||||
const config = preset.config ?? "";
|
||||
|
||||
setLocalCodexApiFormat(extractCodexWireApi(config) ?? "responses");
|
||||
resetCodexConfig(auth, config);
|
||||
|
||||
form.reset({
|
||||
@@ -1381,9 +1418,9 @@ export function ProviderForm({
|
||||
);
|
||||
|
||||
if (preset.apiFormat) {
|
||||
setLocalApiFormat(preset.apiFormat);
|
||||
setLocalClaudeApiFormat(preset.apiFormat);
|
||||
} else {
|
||||
setLocalApiFormat("anthropic");
|
||||
setLocalClaudeApiFormat("anthropic");
|
||||
}
|
||||
|
||||
form.reset({
|
||||
@@ -1518,8 +1555,8 @@ export function ProviderForm({
|
||||
defaultOpusModel={defaultOpusModel}
|
||||
onModelChange={handleModelChange}
|
||||
speedTestEndpoints={speedTestEndpoints}
|
||||
apiFormat={localApiFormat}
|
||||
onApiFormatChange={handleApiFormatChange}
|
||||
apiFormat={localClaudeApiFormat}
|
||||
onApiFormatChange={handleClaudeApiFormatChange}
|
||||
/>
|
||||
)}
|
||||
|
||||
@@ -1547,6 +1584,9 @@ export function ProviderForm({
|
||||
modelName={codexModelName}
|
||||
onModelNameChange={handleCodexModelNameChange}
|
||||
speedTestEndpoints={speedTestEndpoints}
|
||||
apiFormat={localCodexApiFormat}
|
||||
onApiFormatChange={handleCodexApiFormatChange}
|
||||
shouldShowApiFormatSelector={category !== "official"}
|
||||
/>
|
||||
)}
|
||||
|
||||
|
||||
@@ -49,6 +49,8 @@ export function EndpointField({
|
||||
// 调用后端 API 获取 URL 预览
|
||||
useEffect(() => {
|
||||
if (!value || !appType || !showUrlPreview) {
|
||||
// 标记当前所有已发出的请求为过期,避免旧请求回写
|
||||
lastRequestIdRef.current += 1;
|
||||
setUrlPreview(null);
|
||||
return;
|
||||
}
|
||||
@@ -114,7 +116,7 @@ export function EndpointField({
|
||||
</p>
|
||||
<p className="text-xs text-muted-foreground/70 mt-0.5 pl-4">
|
||||
{t("providerForm.directRequestUrlDesc", {
|
||||
defaultValue: "CLI 硬拼接默认后缀后的实际请求地址",
|
||||
defaultValue: "CLI 直连模式下的实际请求地址",
|
||||
})}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
*/
|
||||
|
||||
import { Radio, Loader2 } from "lucide-react";
|
||||
import { useState } from "react";
|
||||
import { Switch } from "@/components/ui/switch";
|
||||
import { useProxyStatus } from "@/hooks/useProxyStatus";
|
||||
import { cn } from "@/lib/utils";
|
||||
@@ -14,6 +15,7 @@ import { toast } from "sonner";
|
||||
import type { AppId } from "@/lib/api";
|
||||
import { proxyApi } from "@/lib/api/proxy";
|
||||
import type { Provider } from "@/types";
|
||||
import { extractProviderBaseUrl } from "@/utils/providerBaseUrl";
|
||||
|
||||
interface ProxyToggleProps {
|
||||
className?: string;
|
||||
@@ -21,48 +23,13 @@ interface ProxyToggleProps {
|
||||
currentProvider?: Provider | null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 provider 配置中提取 base URL
|
||||
*/
|
||||
function extractBaseUrl(provider: Provider, appId: AppId): string | null {
|
||||
try {
|
||||
const config = provider.settingsConfig;
|
||||
if (!config) return null;
|
||||
|
||||
if (appId === "claude") {
|
||||
const envUrl = config?.env?.ANTHROPIC_BASE_URL;
|
||||
return typeof envUrl === "string" ? envUrl.trim() : null;
|
||||
}
|
||||
|
||||
if (appId === "codex" || appId === "opencode") {
|
||||
const tomlConfig = config?.config;
|
||||
if (typeof tomlConfig === "string") {
|
||||
const match = tomlConfig.match(/base_url\s*=\s*(['"])([^'"]+)\1/);
|
||||
return match?.[2]?.trim() || null;
|
||||
}
|
||||
const baseUrl = config?.base_url;
|
||||
return typeof baseUrl === "string" ? baseUrl.trim() : null;
|
||||
}
|
||||
|
||||
if (appId === "gemini") {
|
||||
const envUrl = config?.env?.GOOGLE_GEMINI_BASE_URL;
|
||||
if (typeof envUrl === "string") return envUrl.trim();
|
||||
const baseUrl = config?.GEMINI_API_BASE || config?.base_url;
|
||||
return typeof baseUrl === "string" ? baseUrl.trim() : null;
|
||||
}
|
||||
|
||||
return null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export function ProxyToggle({
|
||||
className,
|
||||
activeApp,
|
||||
currentProvider,
|
||||
}: ProxyToggleProps) {
|
||||
const { t } = useTranslation();
|
||||
const [isCheckingRequirement, setIsCheckingRequirement] = useState(false);
|
||||
const { isRunning, takeoverStatus, setTakeoverForApp, isPending, status } =
|
||||
useProxyStatus();
|
||||
|
||||
@@ -73,36 +40,45 @@ export function ProxyToggle({
|
||||
currentProvider &&
|
||||
currentProvider.category !== "official"
|
||||
) {
|
||||
const baseUrl = extractBaseUrl(currentProvider, activeApp);
|
||||
const baseUrl = extractProviderBaseUrl(currentProvider, activeApp);
|
||||
const apiFormat = currentProvider.meta?.apiFormat;
|
||||
setIsCheckingRequirement(true);
|
||||
try {
|
||||
let proxyRequirement: string | null = null;
|
||||
|
||||
if (baseUrl) {
|
||||
try {
|
||||
const proxyRequirement = await proxyApi.checkProxyRequirement(
|
||||
// 先按 API 格式做硬性判断(baseUrl 缺失时仍需提示)
|
||||
if (activeApp === "claude" && apiFormat === "openai_chat") {
|
||||
proxyRequirement = "openai_chat_format";
|
||||
}
|
||||
|
||||
if (!proxyRequirement && baseUrl) {
|
||||
proxyRequirement = await proxyApi.checkProxyRequirement(
|
||||
activeApp,
|
||||
baseUrl,
|
||||
apiFormat,
|
||||
);
|
||||
|
||||
if (proxyRequirement) {
|
||||
const warningKey =
|
||||
proxyRequirement === "openai_chat_format"
|
||||
? "notifications.openAIChatFormatWarningOnDisable"
|
||||
: proxyRequirement === "url_mismatch"
|
||||
? "notifications.urlMismatchWarningOnDisable"
|
||||
: "notifications.fullUrlWarningOnDisable";
|
||||
|
||||
toast.warning(
|
||||
t(warningKey, {
|
||||
defaultValue:
|
||||
"当前供应商配置可能依赖代理模式,关闭代理后可能无法正常工作。建议更换为基础地址配置或保持代理开启。",
|
||||
}),
|
||||
{ duration: 6000, closeButton: true },
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Failed to check proxy requirement:", error);
|
||||
}
|
||||
|
||||
if (proxyRequirement) {
|
||||
const warningKey =
|
||||
proxyRequirement === "openai_chat_format"
|
||||
? "notifications.openAIChatFormatWarningOnDisable"
|
||||
: proxyRequirement === "url_mismatch"
|
||||
? "notifications.urlMismatchWarningOnDisable"
|
||||
: "notifications.fullUrlWarningOnDisable";
|
||||
|
||||
toast.warning(
|
||||
t(warningKey, {
|
||||
defaultValue:
|
||||
"当前供应商配置可能依赖代理模式,关闭代理后可能无法正常工作。建议更换为基础地址配置或保持代理开启。",
|
||||
}),
|
||||
{ duration: 6000, closeButton: true },
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Failed to check proxy requirement:", error);
|
||||
} finally {
|
||||
setIsCheckingRequirement(false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -155,7 +131,7 @@ export function ProxyToggle({
|
||||
)}
|
||||
title={tooltipText}
|
||||
>
|
||||
{isPending ? (
|
||||
{isPending || isCheckingRequirement ? (
|
||||
<Loader2 className="h-4 w-4 animate-spin text-muted-foreground" />
|
||||
) : (
|
||||
<Radio
|
||||
@@ -170,7 +146,7 @@ export function ProxyToggle({
|
||||
<Switch
|
||||
checked={takeoverEnabled}
|
||||
onCheckedChange={handleToggle}
|
||||
disabled={isPending}
|
||||
disabled={isPending || isCheckingRequirement}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user