feat(gateway): harden provider request execution

Preserve exact request payloads and model client surface and API operation explicitly.

Add Anthropic compatibility profiles, bounded stream commitment, and scoped OAuth retry behavior across provider transports.
This commit is contained in:
elky
2026-07-27 09:36:31 +08:00
parent 79b70f7b5c
commit 531cf11025
152 changed files with 13984 additions and 2075 deletions
@@ -259,6 +259,10 @@ impl<'a> AdminAppState<'a> {
admin_endpoint_signature_parts(&payload.api_format)
.ok_or_else(|| format!("无效的 api_format: {}", payload.api_format))?;
validate_admin_endpoint_stream_policy(normalized_api_format, payload.config.as_ref())?;
crate::provider_transport::validate_anthropic_compatibility_profile_config(
payload.config.as_ref(),
)
.map_err(|_| "无效的 Anthropic compatibility profile".to_string())?;
let base_url = normalize_admin_base_url(&payload.base_url)?;
let existing_endpoints = self
@@ -369,6 +373,10 @@ impl<'a> AdminAppState<'a> {
existing_endpoint.api_format.as_str(),
updated.config.as_ref(),
)?;
crate::provider_transport::validate_anthropic_compatibility_profile_config(
updated.config.as_ref(),
)
.map_err(|_| "无效的 Anthropic compatibility profile".to_string())?;
}
if provider_type == "codex"
@@ -192,6 +192,15 @@ fn normalize_import_endpoint_format(value: &str) -> Result<String, String> {
.ok_or_else(|| format!("无效的 api_format: {value}"))
}
fn fixed_provider_import_endpoint_supported(provider_type: &str, api_format: &str) -> bool {
crate::provider_transport::provider_types::fixed_provider_template(provider_type).is_none()
|| crate::provider_transport::provider_types::fixed_provider_endpoint_template_by_api_format(
provider_type,
api_format,
)
.is_some()
}
fn normalize_import_key_formats(
item: &ImportedProviderKey,
provider_endpoint_formats: &BTreeSet<String>,
@@ -1419,6 +1428,12 @@ impl<'a> AdminAppState<'a> {
for imported_provider_item in imported_providers {
let (raw_provider, imported_provider) = imported_provider_item.into_parts();
let provider_name = invalid!(trim_required(&imported_provider.name, "name"));
invalid!(
crate::provider_transport::validate_anthropic_compatibility_profile_config(
imported_provider.config.as_ref(),
)
.map_err(|_| "无效的 Anthropic compatibility profile".to_string())
);
let existing_provider = providers_by_name.get(&provider_name).cloned();
let provider = if let Some(existing) = existing_provider {
@@ -1513,6 +1528,42 @@ impl<'a> AdminAppState<'a> {
let normalized_api_format = invalid!(normalize_import_endpoint_format(
&imported_endpoint.api_format
));
invalid!(
crate::provider_transport::validate_anthropic_compatibility_profile_config(
imported_endpoint.config.as_ref(),
)
.map_err(|_| "无效的 Anthropic compatibility profile".to_string())
);
if !fixed_provider_import_endpoint_supported(
&provider.provider_type,
&normalized_api_format,
) {
let retired = existing_endpoints_by_format.remove(&normalized_api_format);
if let Some(mut retired) = retired {
if retired.is_active {
retired.is_active = false;
retired.updated_at_unix_secs = SystemTime::now()
.duration_since(UNIX_EPOCH)
.ok()
.map(|duration| duration.as_secs());
let Some(_) = self.update_provider_catalog_endpoint(&retired).await?
else {
return Ok(Err(invalid_request(format!(
"停用 Provider '{provider_name}' 的已移除 Endpoint '{normalized_api_format}' 失败"
))));
};
stats.endpoints.updated += 1;
} else {
stats.endpoints.skipped += 1;
}
} else {
stats.endpoints.skipped += 1;
}
stats.errors.push(format!(
"固定 Provider '{provider_name}' 不再支持 Endpoint '{normalized_api_format}',已跳过或停用"
));
continue;
}
let existing_endpoint = existing_endpoints_by_format
.get(&normalized_api_format)
.cloned();