Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 62 additions & 8 deletions src/crates/adapters/ai-adapters/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1513,7 +1513,7 @@ mod tests {
}

#[test]
fn openai_chat_rejects_unmapped_model_config_toggle_for_an_unverified_endpoint() {
fn openai_chat_uses_format_fallback_for_an_unverified_endpoint() {
let client = AIClient::new(AIConfig {
name: "openai-compatible".to_string(),
base_url: "https://example.com/v1".to_string(),
Expand All @@ -1538,16 +1538,16 @@ mod tests {
vec![ReasoningPresetAction::Toggle { enabled: true }],
));

let error = openai::chat::try_build_request_body(
let body = openai::chat::try_build_request_body(
&client,
&client.config.request_url,
vec![json!({ "role": "user", "content": "hello" })],
None,
None,
)
.expect_err("an unverified compatible endpoint must reject generic toggle");
.expect("an unverified compatible endpoint should use the format fallback");

assert!(error.to_string().contains("unsupported"));
assert_eq!(body["reasoning_effort"], "medium");
}

#[test]
Expand Down Expand Up @@ -1587,7 +1587,7 @@ mod tests {
);

assert_eq!(request_body["thinking"]["type"], "enabled");
assert_eq!(request_body["reasoning_effort"], "max");
assert_eq!(request_body["reasoning_effort"], "high");
}

#[test]
Expand Down Expand Up @@ -1795,7 +1795,7 @@ mod tests {
}

#[test]
fn build_openbitfun_openai_request_body_maps_deepseek_pro_xhigh_to_max() {
fn build_openbitfun_openai_request_body_maps_deepseek_pro_xhigh_to_high() {
let mut client = make_test_client("openai", None);
client.config.name = "openbitfun".to_string();
client.config.base_url = "https://api.openbitfun.com/v1".to_string();
Expand All @@ -1819,7 +1819,7 @@ mod tests {
);

assert_eq!(request_body["thinking"]["type"], "enabled");
assert_eq!(request_body["reasoning_effort"], "max");
assert_eq!(request_body["reasoning_effort"], "high");
}

#[test]
Expand Down Expand Up @@ -1926,6 +1926,60 @@ mod tests {
assert!(request_body.get("thinking").is_none());
}

#[test]
fn siliconflow_effort_and_budget_use_documented_fields() {
let base_config = || AIConfig {
name: "siliconflow".to_string(),
base_url: "https://api.siliconflow.cn/v1".to_string(),
request_url: "https://api.siliconflow.cn/v1/chat/completions".to_string(),
api_key: "test-key".to_string(),
model: "Pro/zai-org/GLM-5.2".to_string(),
format: "openai".to_string(),
context_window: 128000,
max_tokens: Some(4096),
max_images_per_request: None,
temperature: None,
top_p: None,
inline_think_in_text: false,
custom_headers: None,
custom_headers_mode: None,
skip_ssl_verify: false,
custom_request_body: None,
custom_request_body_mode: None,
};

let effort_client = AIClient::new(base_config()).with_reasoning_preset(&reasoning_preset(
"high",
vec![ReasoningPresetAction::Effort {
value: "xhigh".to_string(),
}],
));
let effort_body = openai::chat::build_request_body(
&effort_client,
&effort_client.config.request_url,
vec![json!({ "role": "user", "content": "hello" })],
None,
None,
);
assert_eq!(effort_body["enable_thinking"], true);
assert_eq!(effort_body["reasoning_effort"], "max");
assert!(effort_body.get("thinking_budget").is_none());

let budget_client = AIClient::new(base_config()).with_reasoning_preset(&reasoning_preset(
"budget",
vec![ReasoningPresetAction::BudgetTokens { value: 4096 }],
));
let budget_body = openai::chat::build_request_body(
&budget_client,
&budget_client.config.request_url,
vec![json!({ "role": "user", "content": "hello" })],
None,
None,
);
assert_eq!(budget_body["enable_thinking"], true);
assert_eq!(budget_body["thinking_budget"], 4096);
}

#[test]
fn build_responses_request_body_applies_explicit_none_effort() {
let client = AIClient::new(AIConfig {
Expand Down Expand Up @@ -2205,7 +2259,7 @@ mod tests {

assert_eq!(request_body["thinking"]["type"], "enabled");
assert!(request_body["thinking"].get("budget_tokens").is_none());
assert_eq!(request_body["output_config"]["effort"], "max");
assert_eq!(request_body["output_config"]["effort"], "high");
}

#[test]
Expand Down
5 changes: 5 additions & 0 deletions src/crates/adapters/ai-adapters/src/client/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,9 @@ mod tests {
);
}
}

#[test]
fn unknown_api_formats_remain_rejected() {
assert!(ApiFormat::parse("unknown-gateway").is_err());
}
}
96 changes: 1 addition & 95 deletions src/crates/adapters/ai-adapters/src/client/quirks.rs
Original file line number Diff line number Diff line change
@@ -1,80 +1,4 @@
pub(crate) fn is_dashscope_url(url: &str) -> bool {
url.contains("dashscope.aliyuncs.com") || url.contains("dashscope-intl.aliyuncs.com")
}

pub(crate) fn is_siliconflow_url(url: &str) -> bool {
url.contains("api.siliconflow.cn") || url.contains("api.siliconflow.com")
}

pub(crate) fn is_deepseek_url(url: &str) -> bool {
url.contains("api.deepseek.com")
}

pub(crate) fn is_zhipuai_url(url: &str) -> bool {
reqwest::Url::parse(url.trim()).ok().is_some_and(|url| {
url.scheme() == "https"
&& url.port_or_known_default() == Some(443)
&& url.host_str().is_some_and(|host| {
matches!(host.trim_end_matches('.'), "open.bigmodel.cn" | "api.z.ai")
})
})
}

pub(crate) fn is_deepseek_reasoning_effort_model(model_name: &str) -> bool {
matches!(
model_name.trim().to_ascii_lowercase().as_str(),
"deepseek-v4-flash" | "deepseek-v4-pro"
)
}

pub(crate) fn is_glm_52_reasoning_effort_model(model_name: &str) -> bool {
matches!(
model_name.trim().to_ascii_lowercase().as_str(),
"glm-5.2" | "glm-5-2" | "glm-5p2"
)
}

pub(crate) fn normalize_deepseek_reasoning_effort(
model_name: &str,
effort: &str,
) -> Option<&'static str> {
let is_flash = model_name.trim().eq_ignore_ascii_case("deepseek-v4-flash");
match effort.trim().to_ascii_lowercase().as_str() {
"" => None,
"low" if is_flash => Some("low"),
"high" => Some("high"),
"max" => Some("max"),
"low" | "medium" => Some("high"),
"xhigh" => Some("max"),
"none" | "minimal" => None,
_ => None,
}
}

pub(crate) fn normalize_glm_52_reasoning_effort(effort: &str) -> Option<&'static str> {
match effort.trim().to_ascii_lowercase().as_str() {
"high" | "low" | "medium" => Some("high"),
"max" | "xhigh" => Some("max"),
"" | "none" | "minimal" => None,
_ => None,
}
}

pub(crate) fn parse_glm_major_minor(model_name: &str) -> Option<(u32, u32)> {
let lower = model_name.to_ascii_lowercase();
let tail = lower.strip_prefix("glm-")?;
let mut parts = tail.split('-');
let version = parts.next()?;

let mut version_parts = version.split('.');
let major = version_parts.next()?.parse().ok()?;
let minor = version_parts
.next()
.and_then(|value| value.parse().ok())
.unwrap_or(0);

Some((major, minor))
}
use crate::providers::reasoning::parse_glm_major_minor;

pub(crate) fn should_append_tool_stream(url: &str, model_name: &str) -> bool {
if url.contains("bigmodel.cn") || url.contains("api.z.ai") {
Expand All @@ -88,21 +12,3 @@ pub(crate) fn should_append_tool_stream(url: &str, model_name: &str) -> bool {
parse_glm_major_minor(model_name)
.is_some_and(|(major, minor)| major > 4 || (major == 4 && minor >= 5))
}

pub(crate) fn apply_openai_compatible_toggle(
request_body: &mut serde_json::Value,
enabled: bool,
url: &str,
) -> bool {
if is_dashscope_url(url) || is_siliconflow_url(url) {
request_body["enable_thinking"] = serde_json::json!(enabled);
return true;
}
if is_deepseek_url(url) {
request_body["thinking"] = serde_json::json!({
"type": if enabled { "enabled" } else { "disabled" }
});
return true;
}
false
}
Loading
Loading