From f871e00330904fc053e14edf0ba1025f4de7a7c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E4=B8=AB=E8=AE=B2=E6=A2=B5?= Date: Thu, 2 Jul 2026 01:15:49 +0800 Subject: [PATCH] feat(ai): support OpenAI max completion tokens --- crates/dbx-core/src/ai.rs | 114 ++++++++++++++++++++++++++++++++++---- 1 file changed, 102 insertions(+), 12 deletions(-) diff --git a/crates/dbx-core/src/ai.rs b/crates/dbx-core/src/ai.rs index 7387782f85..4bc57ba3c4 100644 --- a/crates/dbx-core/src/ai.rs +++ b/crates/dbx-core/src/ai.rs @@ -466,8 +466,16 @@ fn responses_token_usage(event: &serde_json::Value) -> Option { Some(TokenUsage { input_tokens: input as u32, output_tokens: output as u32 }) } +fn is_openai_api_endpoint(endpoint: &str) -> bool { + reqwest::Url::parse(endpoint) + .ok() + .and_then(|url| url.host_str().map(|host| host.eq_ignore_ascii_case("api.openai.com"))) + .unwrap_or(false) +} + fn is_openai_api_config(config: &AiConfig) -> bool { - matches!(config.provider, AiProvider::Openai) || config.endpoint.to_ascii_lowercase().contains("api.openai.com") + // OpenAI provider can be routed through a custom proxy while still requiring OpenAI request semantics. + matches!(config.provider, AiProvider::Openai) || is_openai_api_endpoint(&config.endpoint) } fn is_openai_reasoning_model(model: &str) -> bool { @@ -475,6 +483,18 @@ fn is_openai_reasoning_model(model: &str) -> bool { model.starts_with("gpt-5") || model.starts_with("o1") || model.starts_with("o3") || model.starts_with("o4") } +fn uses_openai_max_completion_tokens(config: &AiConfig) -> bool { + is_openai_api_config(config) && is_openai_reasoning_model(&config.model) +} + +fn set_chat_completion_token_limit(body: &mut serde_json::Value, config: &AiConfig, max_tokens: u32) { + if uses_openai_max_completion_tokens(config) { + body["max_completion_tokens"] = json!(max_tokens); + } else { + body["max_tokens"] = json!(max_tokens); + } +} + /// Kimi K2.5+ models (including K2.7-Code) require fixed sampling parameters: /// temperature=1.0, top_p=0.95, n=1. Any other value returns an error. /// @@ -497,7 +517,7 @@ fn is_kimi_model(model: &str) -> bool { } pub fn supports_temperature(config: &AiConfig) -> bool { - !(is_kimi_model(&config.model) || is_openai_api_config(config) && is_openai_reasoning_model(&config.model)) + !(is_kimi_model(&config.model) || uses_openai_max_completion_tokens(config)) } fn add_temperature_if_supported_for_config(body: &mut serde_json::Value, config: &AiConfig, temperature: Option) { @@ -893,8 +913,8 @@ pub async fn call_openai_compatible(client: &reqwest::Client, request: AiComplet let mut body_obj = json!({ "model": request.config.model, "messages": messages, - "max_tokens": request.max_tokens.unwrap_or(2048), }); + set_chat_completion_token_limit(&mut body_obj, &request.config, request.max_tokens.unwrap_or(2048)); add_temperature_if_supported(&mut body_obj, &request); if !request.config.enable_thinking && !is_kimi_model(&request.config.model) { body_obj["extra_body"] = json!({ @@ -1150,12 +1170,13 @@ pub async fn test_connection_core(config: &AiConfig) -> Result