Skip to content

A cross-platform desktop All-in-One assistant for Claude Code, Codex, OpenCode,DeepSeek Harness and more. Only official website: ccswitch.io

fix(copilot): keep tool-free Claude requests on Auto (#9998)

Allen Xu5eeac32cf130
diff --git a/src-tauri/src/proxy/copilot_optimizer.rs b/src-tauri/src/proxy/copilot_optimizer.rs
index 40f4d2246..f2c3c758b 100644
--- a/src-tauri/src/proxy/copilot_optimizer.rs
+++ b/src-tauri/src/proxy/copilot_optimizer.rs
@@ -72,8 +72,7 @@ pub fn classify_responses_request(body: &Value) -> CopilotClassification {
/// - 匹配 compact 模式 → "agent"
/// 3. 最后消息 role 非 user → "user"(安全默认)
///
-/// Warmup 检测(与参考实现对齐):
-/// - 请求头中有 `anthropic-beta` + 无 tools + 非 compact → warmup
+/// Warmup 检测要求匹配 Claude Code 的单 token 探针。
///
/// `compact_detection`:是否启用 compact 检测。为 false 时跳过,
/// 确保 `CopilotOptimizerConfig.compact_detection` 开关真正生效。
@@ -94,7 +93,7 @@ pub fn classify_request(
_ => {
return CopilotClassification {
initiator: "user",
- is_warmup: is_warmup_request(body, has_anthropic_beta, false),
+ is_warmup: false,
is_compact: false,
is_subagent,
}
@@ -148,18 +147,28 @@ pub fn classify_request(
/// 检测是否为 warmup/探针请求(适合降级到小模型)。
///
-/// 与参考实现对齐,三个条件同时满足:
-/// 1. 请求头有 `anthropic-beta`(Claude Code warmup 探针的标志)
-/// 2. 无 tools 定义
-/// 3. 非 compact 请求
fn is_warmup_request(body: &Value, has_anthropic_beta: bool, is_compact: bool) -> bool {
- if !has_anthropic_beta || is_compact {
+ if !has_anthropic_beta || is_compact || body["max_tokens"].as_u64() != Some(1) {
return false;
}
- // 无工具定义
- body.get("tools")
+ if body
+ .get("tools")
.and_then(|tools| tools.as_array())
- .is_none_or(|tools| tools.is_empty())
+ .is_some_and(|tools| !tools.is_empty())
+ {
+ return false;
+ }
+ let Some([message]) = body["messages"].as_array().map(Vec::as_slice) else {
+ return false;
+ };
+ // Claude Code 启动检查发送 ".",模型校验发送带临时缓存标记的 "Hi"。
+ match &message["content"] {
+ Value::String(text) => text == ".",
+ Value::Array(blocks) => matches!(blocks.as_slice(), [block]
+ if block["type"] == "text" && block["text"] == "Hi"
+ && block["cache_control"]["type"] == "ephemeral"),
+ _ => false,
+ }
}
/// 检测是否为 Claude Code 上下文压缩/compact 请求。
@@ -789,17 +798,71 @@ mod tests {
"user",
false,
),
- // Warmup: has_anthropic_beta && no tools && initiator == user
+ // Warmup: Claude Code 单 token 探针(has_anthropic_beta && max_tokens == 1 && 无 tools)
(
- "warmup_beta_no_tools",
- json!({"messages": [{"role": "user", "content": "Hello"}]}),
+ "warmup_dot_probe",
+ json!({
+ "model": "claude-sonnet-4-20250514",
+ "max_tokens": 1,
+ "messages": [{"role": "user", "content": "."}]
+ }),
true,
"user",
true,
),
+ (
+ "not_warmup_dot_probe_without_beta",
+ json!({
+ "model": "claude-sonnet-4-20250514",
+ "max_tokens": 1,
+ "messages": [{"role": "user", "content": "."}]
+ }),
+ false,
+ "user",
+ false,
+ ),
+ (
+ "warmup_model_validation_probe",
+ json!({
+ "max_tokens": 1,
+ "messages": [{"role": "user", "content": [
+ {"type": "text", "text": "Hi", "cache_control": {"type": "ephemeral"}}
+ ]}]
+ }),
+ true,
+ "user",
+ true,
+ ),
+ (
+ "not_warmup_normal_max_tokens",
+ json!({
+ "max_tokens": 32_000,
+ "tools": [],
+ "messages": [{"role": "user", "content": "."}]
+ }),
+ true,
+ "user",
+ false,
+ ),
+ (
+ "not_warmup_normal_text_single_token",
+ json!({
+ "max_tokens": 1,
+ "tools": [],
+ "messages": [{"role": "user", "content": "Reply with YES."}]
+ }),
+ true,
+ "user",
+ false,
+ ),
(
"not_warmup_with_tools",
- json!({"tools": tools, "messages": [{"role": "user", "content": "Hello"}]}),
+ json!({
+ "model": "claude-sonnet-4-20250514",
+ "max_tokens": 1,
+ "tools": tools,
+ "messages": [{"role": "user", "content": "."}]
+ }),
true,
"user",
false,
diff --git a/src-tauri/src/proxy/forwarder/tests.rs b/src-tauri/src/proxy/forwarder/tests.rs
index d25afbd53..242590e1f 100644
--- a/src-tauri/src/proxy/forwarder/tests.rs
+++ b/src-tauri/src/proxy/forwarder/tests.rs
@@ -322,7 +322,7 @@ async fn copilot_auto_forwarding_pairs_model_and_session_and_recovers_from_401s(
.clone_from(&provider.id);
for (text, warmup, stream, expected_model) in [
- ("warmup", true, false, "claude-haiku-4.5"),
+ (".", true, false, "claude-haiku-4.5"),
("unsupported", false, false, "embed"),
("ordinary", false, false, "claude-haiku-4.6"),
("retry", false, false, "claude-haiku-4.6"),
@@ -336,11 +336,17 @@ async fn copilot_auto_forwarding_pairs_model_and_session_and_recovers_from_401s(
"copilot-session-token",
HeaderValue::from_static("client-canonical"),
);
- if warmup {
- headers.insert("anthropic-beta", HeaderValue::from_static("warmup"));
+ if warmup || text == "ordinary" {
+ headers.insert(
+ "anthropic-beta",
+ HeaderValue::from_static("claude-code-20250219"),
+ );
}
- let mut body = json!({"model": "claude-haiku-4-5", "max_tokens": 128, "stream": stream,
+ let mut body = json!({"model": "claude-haiku-4-5", "max_tokens": if warmup { 1 } else { 128 }, "stream": stream,
"messages": [{"role": "user", "content": text}]});
+ if text == "ordinary" {
+ body["tools"] = json!([]);
+ }
if stream {
body["system"] =
json!("You are a helpful AI assistant tasked with summarizing conversations");