refactor(providers): unify upstream base-url normalization

normalizeOpenAIBaseUrl, normalizeClaudeBaseUrl and normalizeGeminiBaseUrl were identical except for the empty-input fallback; collapse them into one helper with a fallback parameter.
This commit is contained in:
LTbinglingfeng
2026-06-13 02:12:17 +08:00
Unverified
parent cd44dca9c0
commit d268be1400
+5 -31
View File
@@ -29,35 +29,9 @@ export const withDisableAllModelsRule = (models?: string[]) => {
export const withoutDisableAllModelsRule = (models?: string[]) =>
stripDisableAllModelsRule(models);
const normalizeOpenAIBaseUrl = (baseUrl: string): string => {
const normalizeUpstreamBaseUrl = (baseUrl: string, fallback = ''): string => {
let trimmed = String(baseUrl || '').trim();
if (!trimmed) return '';
trimmed = trimmed.replace(/\/?v0\/management\/?$/i, '');
trimmed = trimmed.replace(/\/+$/g, '');
if (!/^https?:\/\//i.test(trimmed)) {
trimmed = `http://${trimmed}`;
}
return trimmed;
};
const normalizeClaudeBaseUrl = (baseUrl: string): string => {
let trimmed = String(baseUrl || '').trim();
if (!trimmed) {
return 'https://api.anthropic.com';
}
trimmed = trimmed.replace(/\/?v0\/management\/?$/i, '');
trimmed = trimmed.replace(/\/+$/g, '');
if (!/^https?:\/\//i.test(trimmed)) {
trimmed = `http://${trimmed}`;
}
return trimmed;
};
const normalizeGeminiBaseUrl = (baseUrl: string): string => {
let trimmed = String(baseUrl || '').trim();
if (!trimmed) {
return DEFAULT_GEMINI_BASE_URL;
}
if (!trimmed) return fallback;
trimmed = trimmed.replace(/\/?v0\/management\/?$/i, '');
trimmed = trimmed.replace(/\/+$/g, '');
if (!/^https?:\/\//i.test(trimmed)) {
@@ -81,7 +55,7 @@ const buildGeminiModelResource = (model: string): string => {
};
export const buildOpenAIChatCompletionsEndpoint = (baseUrl: string): string => {
const trimmed = normalizeOpenAIBaseUrl(baseUrl);
const trimmed = normalizeUpstreamBaseUrl(baseUrl);
if (!trimmed) return '';
if (trimmed.endsWith('/chat/completions')) {
return trimmed;
@@ -90,7 +64,7 @@ export const buildOpenAIChatCompletionsEndpoint = (baseUrl: string): string => {
};
export const buildClaudeMessagesEndpoint = (baseUrl: string): string => {
const trimmed = normalizeClaudeBaseUrl(baseUrl);
const trimmed = normalizeUpstreamBaseUrl(baseUrl, 'https://api.anthropic.com');
if (!trimmed) return '';
if (trimmed.endsWith('/v1/messages')) {
return trimmed;
@@ -108,7 +82,7 @@ export const buildGeminiGenerateContentEndpoint = (
const resource = buildGeminiModelResource(model);
if (!resource) return '';
const trimmed = normalizeGeminiBaseUrl(baseUrl);
const trimmed = normalizeUpstreamBaseUrl(baseUrl, DEFAULT_GEMINI_BASE_URL);
if (!trimmed) return '';
if (/:generateContent$/i.test(trimmed)) {
return trimmed;