fix(provider): support unversioned API roots in model fetch

This commit is contained in:
zhefox
2026-05-26 15:39:56 +08:00
parent c4927162b7
commit d28a389a93
11 changed files with 525 additions and 41 deletions
@@ -13,8 +13,8 @@ use crate::claude_code::build_claude_code_messages_url;
use crate::snapshot::GatewayProviderTransportSnapshot;
use crate::url::{
build_claude_messages_url, build_gemini_content_url, build_openai_chat_url,
build_openai_responses_url, build_passthrough_path_url,
google_openai_compat_base_includes_api_root, normalize_gemini_content_action_path,
build_openai_responses_url, build_passthrough_path_url, normalize_gemini_content_action_path,
openai_compatible_base_includes_api_root,
};
use crate::vertex::{
build_vertex_api_key_gemini_content_url, build_vertex_api_key_gemini_embedding_url,
@@ -403,7 +403,7 @@ fn build_provider_v1_url(
.unwrap_or_else(|| upstream_base_url.trim())
.trim_end_matches('/');
let path = if base_without_query.ends_with("/v1")
|| google_openai_compat_base_includes_api_root(base_without_query)
|| openai_compatible_base_includes_api_root(base_without_query)
{
v1_path
} else {
+162 -10
View File
@@ -7,12 +7,11 @@ use url::Url;
pub fn build_openai_chat_url(upstream_base_url: &str, query: Option<&str>) -> String {
let (trimmed, base_query) = split_base_url_query(upstream_base_url);
let trimmed = trimmed.trim_end_matches('/');
let mut url =
if trimmed.ends_with("/v1") || google_openai_compat_base_includes_api_root(trimmed) {
format!("{trimmed}/chat/completions")
} else {
format!("{trimmed}/v1/chat/completions")
};
let mut url = if openai_compatible_base_includes_api_root(trimmed) {
format!("{trimmed}/chat/completions")
} else {
format!("{trimmed}/v1/chat/completions")
};
append_merged_query(&mut url, base_query, None, query, &[]);
url
}
@@ -31,7 +30,7 @@ pub fn build_openai_responses_url(
};
let mut url = if is_codex_cli_backend_url(trimmed)
|| trimmed.ends_with("/codex")
|| trimmed.ends_with("/v1")
|| openai_compatible_base_includes_api_root(trimmed)
{
format!("{trimmed}{suffix}")
} else {
@@ -51,7 +50,7 @@ pub fn build_openai_image_url(
let suffix = openai_image_path_suffix(request_path);
let mut url = if openai_image_base_includes_operation_path(trimmed) {
trimmed.to_string()
} else if trimmed.ends_with("/v1") || google_openai_compat_base_includes_api_root(trimmed) {
} else if openai_compatible_base_includes_api_root(trimmed) {
format!("{trimmed}{suffix}")
} else {
format!("{trimmed}/v1{suffix}")
@@ -200,6 +199,48 @@ pub fn build_passthrough_path_url(
Some(url)
}
pub fn build_bigmodel_coding_models_url(upstream_base_url: &str) -> Option<String> {
let (trimmed_base_url, base_query) = split_base_url_query(upstream_base_url);
let trimmed_base_url = trimmed_base_url.trim_end_matches('/');
if trimmed_base_url.is_empty() || !bigmodel_coding_models_base_is_supported(trimmed_base_url) {
return None;
}
let path = Url::parse(trimmed_base_url)
.ok()
.map(|url| url.path().trim_end_matches('/').to_string())
.unwrap_or_else(|| trimmed_base_url.trim_end_matches('/').to_string());
let mut url = if path.ends_with("/models/models") {
trimmed_base_url.to_string()
} else if path.ends_with("/models") {
format!("{trimmed_base_url}/models")
} else {
format!("{trimmed_base_url}/models/models")
};
append_merged_query(&mut url, base_query, None, None, &[]);
Some(url)
}
pub fn build_openai_compatible_models_url(upstream_base_url: &str) -> Option<String> {
if let Some(url) = build_bigmodel_coding_models_url(upstream_base_url) {
return Some(url);
}
let (trimmed_base_url, base_query) = split_base_url_query(upstream_base_url);
let trimmed_base_url = trimmed_base_url.trim_end_matches('/');
if trimmed_base_url.is_empty() {
return None;
}
let mut url = if openai_compatible_base_includes_api_root(trimmed_base_url) {
format!("{trimmed_base_url}/models")
} else {
format!("{trimmed_base_url}/v1/models")
};
append_merged_query(&mut url, base_query, None, None, &[]);
Some(url)
}
pub fn build_gemini_files_passthrough_url(
upstream_base_url: &str,
path: &str,
@@ -254,6 +295,49 @@ pub(crate) fn google_openai_compat_base_includes_api_root(base_url: &str) -> boo
false
}
pub fn openai_compatible_base_includes_api_root(base_url: &str) -> bool {
let trimmed = base_url.trim().trim_end_matches('/');
trimmed.ends_with("/v1")
|| google_openai_compat_base_includes_api_root(trimmed)
|| bigmodel_coding_base_includes_api_root(trimmed)
|| openai_compatible_base_includes_unversioned_api_root(trimmed)
}
pub fn openai_compatible_base_includes_unversioned_api_root(base_url: &str) -> bool {
let trimmed = base_url.trim().trim_end_matches('/');
let path = Url::parse(trimmed)
.ok()
.map(|url| url.path().trim_end_matches('/').to_ascii_lowercase())
.unwrap_or_else(|| trimmed.to_ascii_lowercase());
path.ends_with("/api")
}
fn bigmodel_coding_base_includes_api_root(base_url: &str) -> bool {
let Ok(parsed) = Url::parse(base_url.trim()) else {
return false;
};
let Some(host) = parsed.host_str().map(|value| value.to_ascii_lowercase()) else {
return false;
};
host == "open.bigmodel.cn" && parsed.path().trim_end_matches('/') == "/api/coding/paas/v4"
}
fn bigmodel_coding_models_base_is_supported(base_url: &str) -> bool {
let Ok(parsed) = Url::parse(base_url.trim()) else {
return false;
};
let Some(host) = parsed.host_str().map(|value| value.to_ascii_lowercase()) else {
return false;
};
if host != "open.bigmodel.cn" {
return false;
}
matches!(
parsed.path().trim_end_matches('/'),
"/api/coding/paas/v4" | "/api/coding/paas/v4/models" | "/api/coding/paas/v4/models/models"
)
}
fn looks_like_vertex_ai_host(host: &str) -> bool {
const VERTEX_AI_HOST: &str = "aiplatform.googleapis.com";
host == VERTEX_AI_HOST
@@ -343,8 +427,9 @@ fn merge_query_string(
#[cfg(test)]
mod tests {
use super::{
build_gemini_content_url, build_gemini_files_passthrough_url,
build_gemini_video_predict_long_running_url, build_openai_chat_url, build_openai_image_url,
build_bigmodel_coding_models_url, build_gemini_content_url,
build_gemini_files_passthrough_url, build_gemini_video_predict_long_running_url,
build_openai_chat_url, build_openai_compatible_models_url, build_openai_image_url,
build_openai_responses_url, build_passthrough_path_url,
normalize_gemini_content_action_path,
};
@@ -378,6 +463,73 @@ mod tests {
);
}
#[test]
fn openai_urls_preserve_bigmodel_coding_api_root() {
assert_eq!(
build_openai_chat_url(
"https://open.bigmodel.cn/api/coding/paas/v4",
Some("trace=1")
),
"https://open.bigmodel.cn/api/coding/paas/v4/chat/completions?trace=1"
);
assert_eq!(
build_openai_responses_url("https://open.bigmodel.cn/api/coding/paas/v4", None, false),
"https://open.bigmodel.cn/api/coding/paas/v4/responses"
);
}
#[test]
fn openai_urls_preserve_unversioned_api_root() {
assert_eq!(
build_openai_chat_url("https://proxy.example.com/api", Some("trace=1")),
"https://proxy.example.com/api/chat/completions?trace=1"
);
assert_eq!(
build_openai_responses_url("https://proxy.example.com/api", None, false),
"https://proxy.example.com/api/responses"
);
assert_eq!(
build_openai_image_url(
"https://proxy.example.com/api",
Some("/v1/images/generations"),
None
),
"https://proxy.example.com/api/images/generations"
);
}
#[test]
fn bigmodel_coding_models_url_uses_double_models_resource() {
assert_eq!(
build_bigmodel_coding_models_url(
"https://open.bigmodel.cn/api/coding/paas/v4?tenant=demo"
)
.as_deref(),
Some("https://open.bigmodel.cn/api/coding/paas/v4/models/models?tenant=demo")
);
assert_eq!(
build_bigmodel_coding_models_url("https://open.bigmodel.cn/api/coding/paas/v4/models")
.as_deref(),
Some("https://open.bigmodel.cn/api/coding/paas/v4/models/models")
);
assert_eq!(
build_bigmodel_coding_models_url(
"https://open.bigmodel.cn/api/coding/paas/v4/models/models"
)
.as_deref(),
Some("https://open.bigmodel.cn/api/coding/paas/v4/models/models")
);
}
#[test]
fn openai_compatible_models_url_preserves_unversioned_api_root() {
assert_eq!(
build_openai_compatible_models_url("https://proxy.example.com/api?tenant=demo")
.as_deref(),
Some("https://proxy.example.com/api/models?tenant=demo")
);
}
#[test]
fn openai_responses_url_preserves_codex_path_prefix() {
assert_eq!(