Spaces:
Runtime error
Runtime error
File size: 4,063 Bytes
cd8bd0a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | // URL/format normalizers for provider key validation. Pure string helpers extracted from
// validation.ts (god-file decomposition): no I/O, no side effects — they only shape base URLs
// and chat endpoints per provider format. Behavior is byte-identical to the original inline defs.
import {
stripAnthropicMessagesSuffix,
stripClaudeCodeCompatibleEndpointSuffix,
} from "@omniroute/open-sse/services/claudeCodeCompatible.ts";
import { isOpenAICompatibleProvider } from "@/shared/constants/providers";
export const OPENAI_LIKE_FORMATS = new Set(["openai", "openai-responses"]);
export const GEMINI_LIKE_FORMATS = new Set(["gemini", "gemini-cli"]);
export function normalizeBaseUrl(baseUrl: string) {
// Guard against a non-string baseUrl reaching .trim() / .replace() — see #2463
// where NVIDIA NIM validation surfaced as `e.startsWith is not a function`
// after the bundler renamed `baseUrl` to `e`. Any malformed providerSpecificData
// (e.g. saved as object from a UI bug) would otherwise crash mid-validation.
const value = typeof baseUrl === "string" ? baseUrl : "";
return value.trim().replace(/\/$/, "");
}
export function normalizeAzureOpenAIBaseUrl(baseUrl: string) {
return normalizeBaseUrl(baseUrl)
.replace(/\/openai$/i, "")
.replace(/\/openai\/deployments\/[^/]+\/chat\/completions.*$/i, "");
}
export function normalizeAnthropicBaseUrl(baseUrl: string) {
return stripAnthropicMessagesSuffix(baseUrl || "");
}
export function normalizeClaudeCodeCompatibleBaseUrl(baseUrl: string) {
return stripClaudeCodeCompatibleEndpointSuffix(baseUrl || "");
}
export function addModelsSuffix(baseUrl: string) {
const normalized = normalizeBaseUrl(baseUrl);
if (!normalized) return "";
const suffixes = ["/chat/completions", "/responses", "/chat", "/messages"];
if (normalized.endsWith("/models")) {
return normalized;
}
for (const suffix of suffixes) {
if (normalized.endsWith(suffix)) {
return `${normalized.slice(0, -suffix.length)}/models`;
}
}
return `${normalized}/models`;
}
export function resolveBaseUrl(entry: any, providerSpecificData: any = {}) {
if (providerSpecificData?.baseUrl) return normalizeBaseUrl(providerSpecificData.baseUrl);
if (entry?.baseUrl) return normalizeBaseUrl(entry.baseUrl);
return "";
}
export function resolveChatUrl(provider: string, baseUrl: string, providerSpecificData: any = {}) {
const normalized = normalizeBaseUrl(baseUrl);
if (!normalized) return "";
if (isOpenAICompatibleProvider(provider)) {
if (providerSpecificData?.chatPath) {
return `${normalized}${providerSpecificData.chatPath}`;
}
if (providerSpecificData?.apiType === "responses") {
return `${normalized}/responses`;
}
return `${normalized}/chat/completions`;
}
if (
normalized.endsWith("/chat/completions") ||
normalized.endsWith("/responses") ||
normalized.endsWith("/chat")
) {
return normalized;
}
if (normalized.endsWith("/v1")) {
return `${normalized}/chat/completions`;
}
return normalized;
}
export function normalizeHerokuChatUrl(baseUrl: string) {
const normalized = normalizeBaseUrl(baseUrl);
if (!normalized) return "";
return normalized.endsWith("/v1/chat/completions")
? normalized
: `${normalized}/v1/chat/completions`;
}
export function normalizeDatabricksChatUrl(baseUrl: string) {
const normalized = normalizeBaseUrl(baseUrl);
if (!normalized) return "";
return normalized.endsWith("/chat/completions") ? normalized : `${normalized}/chat/completions`;
}
export function normalizeSnowflakeChatUrl(baseUrl: string) {
const normalized = normalizeBaseUrl(baseUrl)
.replace(/\/cortex\/inference:complete$/, "")
.replace(/\/api\/v2$/, "");
if (!normalized) return "";
return `${normalized}/api/v2/cortex/inference:complete`;
}
export function normalizeGigachatChatUrl(baseUrl: string) {
const normalized = normalizeBaseUrl(baseUrl).replace(/\/chat\/completions$/, "");
if (!normalized) return "";
return `${normalized}/chat/completions`;
}
|