2026-05-18 17:35:36 +00:00
|
|
|
interface ApiFormatPathDefinition {
|
|
|
|
|
value: string
|
|
|
|
|
default_path: string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function normalizeEndpointApiFormat(apiFormat: string): string {
|
|
|
|
|
switch (apiFormat.trim().toLowerCase()) {
|
|
|
|
|
default:
|
|
|
|
|
return apiFormat.trim().toLowerCase()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isCodexUrl(baseUrl: string): boolean {
|
|
|
|
|
const url = baseUrl.replace(/\/+$/, '')
|
|
|
|
|
return url.includes('/backend-api/codex') || url.endsWith('/codex')
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-26 16:50:36 +08:00
|
|
|
function parseBaseUrlParts(baseUrl?: string | null): { host: string; path: string } | null {
|
2026-05-26 15:39:56 +08:00
|
|
|
const raw = (baseUrl || '').trim()
|
2026-05-26 16:50:36 +08:00
|
|
|
if (!raw) return null
|
2026-05-26 15:39:56 +08:00
|
|
|
try {
|
|
|
|
|
const parsed = new URL(raw)
|
2026-05-26 16:50:36 +08:00
|
|
|
return {
|
|
|
|
|
host: parsed.hostname.toLowerCase(),
|
|
|
|
|
path: parsed.pathname.replace(/\/+$/, '').toLowerCase(),
|
|
|
|
|
}
|
2026-05-26 15:39:56 +08:00
|
|
|
} catch {
|
2026-05-26 16:50:36 +08:00
|
|
|
const pathStart = raw.indexOf('/')
|
|
|
|
|
return {
|
|
|
|
|
host: '',
|
|
|
|
|
path: pathStart >= 0 ? raw.slice(pathStart).split('?')[0].replace(/\/+$/, '').toLowerCase() : '',
|
|
|
|
|
}
|
2026-05-26 15:39:56 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-26 16:50:36 +08:00
|
|
|
function baseUrlHasPathApiRoot(baseUrl?: string | null): boolean {
|
|
|
|
|
const path = parseBaseUrlParts(baseUrl)?.path
|
|
|
|
|
return !!path && path !== '/'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function baseUrlEndsWithV1Root(baseUrl?: string | null): boolean {
|
|
|
|
|
return parseBaseUrlParts(baseUrl)?.path.endsWith('/v1') ?? false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isBigModelCodingApiRoot(baseUrl?: string | null): boolean {
|
|
|
|
|
const parts = parseBaseUrlParts(baseUrl)
|
|
|
|
|
return parts?.host === 'open.bigmodel.cn' && parts.path === '/api/coding/paas/v4'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isGoogleOpenAiCompatApiRoot(baseUrl?: string | null): boolean {
|
|
|
|
|
const parts = parseBaseUrlParts(baseUrl)
|
|
|
|
|
return parts?.host === 'generativelanguage.googleapis.com'
|
|
|
|
|
&& (parts.path === '/v1beta/openai' || parts.path === '/v1/openai')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isVertexOpenAiCompatApiRoot(baseUrl?: string | null): boolean {
|
|
|
|
|
const parts = parseBaseUrlParts(baseUrl)
|
|
|
|
|
return !!parts
|
|
|
|
|
&& (parts.host === 'aiplatform.googleapis.com' || parts.host.endsWith('.aiplatform.googleapis.com') || parts.host.endsWith('-aiplatform.googleapis.com'))
|
|
|
|
|
&& parts.path.endsWith('/endpoints/openapi')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function openAiCompatibleBaseIncludesApiRoot(baseUrl?: string | null): boolean {
|
|
|
|
|
return baseUrlEndsWithV1Root(baseUrl)
|
|
|
|
|
|| baseUrlHasPathApiRoot(baseUrl)
|
|
|
|
|
|| isBigModelCodingApiRoot(baseUrl)
|
|
|
|
|
|| isGoogleOpenAiCompatApiRoot(baseUrl)
|
|
|
|
|
|| isVertexOpenAiCompatApiRoot(baseUrl)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function v1CompatibleBaseIncludesApiRoot(baseUrl?: string | null): boolean {
|
|
|
|
|
return baseUrlEndsWithV1Root(baseUrl) || baseUrlHasPathApiRoot(baseUrl)
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-26 15:39:56 +08:00
|
|
|
function stripV1PrefixForApiRoot(path: string): string {
|
|
|
|
|
return path.replace(/^\/v1(?=\/)/i, '')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isOpenAiCompatibleFormat(apiFormat: string): boolean {
|
|
|
|
|
return apiFormat.startsWith('openai:') || apiFormat.startsWith('jina:')
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-26 16:50:36 +08:00
|
|
|
function isClaudeCompatibleFormat(apiFormat: string): boolean {
|
|
|
|
|
return apiFormat === 'claude:messages'
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-18 17:35:36 +00:00
|
|
|
export function getDefaultEndpointPath(params: {
|
|
|
|
|
apiFormat: string
|
|
|
|
|
providerType?: string | null
|
|
|
|
|
baseUrl?: string
|
|
|
|
|
apiFormats: ApiFormatPathDefinition[]
|
|
|
|
|
}): string {
|
|
|
|
|
const providerType = (params.providerType || '').toLowerCase()
|
|
|
|
|
const normalizedApiFormat = normalizeEndpointApiFormat(params.apiFormat)
|
|
|
|
|
if (providerType === 'vertex_ai') {
|
|
|
|
|
if (normalizedApiFormat === 'gemini:generate_content') {
|
|
|
|
|
return '/v1/projects/{project_id}/locations/{region}/publishers/google/models/{model}:{action}'
|
|
|
|
|
}
|
|
|
|
|
if (normalizedApiFormat === 'gemini:embedding') {
|
|
|
|
|
return '/v1/projects/{project_id}/locations/{region}/publishers/google/models/{model}:predict'
|
|
|
|
|
}
|
|
|
|
|
if (normalizedApiFormat === 'claude:messages') {
|
|
|
|
|
return '/v1/projects/{project_id}/locations/{region}/publishers/anthropic/models/{model}:{action}'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const format = params.apiFormats.find(f => f.value === normalizedApiFormat)
|
|
|
|
|
const defaultPath = format?.default_path || ''
|
|
|
|
|
const isCodex = providerType
|
|
|
|
|
? providerType === 'codex'
|
|
|
|
|
: (!!params.baseUrl && isCodexUrl(params.baseUrl))
|
|
|
|
|
if (normalizedApiFormat === 'openai:responses' && isCodex) {
|
|
|
|
|
return '/responses'
|
|
|
|
|
}
|
2026-05-26 16:50:36 +08:00
|
|
|
if (openAiCompatibleBaseIncludesApiRoot(params.baseUrl) && isOpenAiCompatibleFormat(normalizedApiFormat)) {
|
|
|
|
|
return stripV1PrefixForApiRoot(defaultPath)
|
|
|
|
|
}
|
|
|
|
|
if (v1CompatibleBaseIncludesApiRoot(params.baseUrl) && isClaudeCompatibleFormat(normalizedApiFormat)) {
|
2026-05-26 15:39:56 +08:00
|
|
|
return stripV1PrefixForApiRoot(defaultPath)
|
|
|
|
|
}
|
2026-05-18 17:35:36 +00:00
|
|
|
return defaultPath
|
|
|
|
|
}
|