[Bug] 进站流水线无条件剥除 toolConfig 导致全协议强制工具调用 (tool_choice / functionCallingConfig) 彻底瘫痪失效 / Inbound pipeline unconditionally strips toolConfig, breaking forced tool_choice
Author: cubelikeplayDanielCreated Sep 29, 2026Updated Sep 29, 2026
问题描述 / Description
在与各类主流 AI Agent 框架(如 LangChain、LlamaIndex、Claude SDK、OpenAI SDK、AutoGen 等)配合使用 Antigravity Tools 时,Agent 经常需要指定强制工具调用(Forced Tool Choice),以确保工作流确定性地执行某一特定工具(例如数据查询、权限检查或表单提交)。
经过全链路源码审计与本地 8045 守护进程的连续多轮实机测试,发现网关在处理客户端发来的 tool_choice 参数时存在两个严重的深层缺陷,导致无论是 Claude 协议端点还是 OpenAI 协议端点,所有强制工具调用均 100% 失效,模型直接无视强制约束输出普通闲聊文本:
- Claude 协议请求体结构缺失
tool_choice字段: 在src-tauri/src/proxy/mappers/claude/models.rs中,ClaudeRequest结构体完全遗漏了tool_choice字段声明,反序列化时客户端传来的tool_choice被直接静默吞噬(Silently Dropped)。 - 进站拓扑对齐流水线一刀切强制删除
toolConfig: 在src-tauri/src/proxy/pipeline/inbound.rs(第 853-860 行) 的align_google_request_prefix_topology函数中,网关在处理所有协议请求的最后阶段,硬编码执行了无条件移除:这导致上游 Google Gemini API 根本接收不到任何// 3. [REMOVED v4.8.2] toolConfig / tool_config: 官方 Antigravity 报文不携带该字段... inner_request.as_object_mut().map(|m| { m.remove("toolConfig"); m.remove("tool_config"); });toolConfig.functionCallingConfig,模型完全不知道存在强制调用约束!
复现步骤与实测证据 (5 轮测试 100% 确定性复现) / Reproduction Steps & Evidence
我们在本地 8045 运行实例上分别针对 Claude 协议与 OpenAI 协议执行了连续 5 轮严格的实机复现测试:
场景 1:Claude 协议 /v1/messages 显式指定 tool_choice: {"type": "tool", "name": "lookup_user_profile"}
curl -X POST http://127.0.0.1:8045/v1/messages \
-H "Authorization: Bearer <API_KEY>" \
-H "Content-Type: application/json" \
-H "anthropic-version: 2023-06-01" \
-d '{
"model": "claude-3-5-sonnet-20241022",
"max_tokens": 100,
"messages": [{"role": "user", "content": "Hello, please do not do anything except calling the tool."}],
"tools": [{
"name": "lookup_user_profile",
"description": "Lookup the user profile",
"input_schema": {"type": "object", "properties": {"uid": {"type": "string"}}, "required": ["uid"]}
}],
"tool_choice": {"type": "tool", "name": "lookup_user_profile"}
}'实测响应结果 (连续 5 轮):
- 轮次 1-5:HTTP 状态码
200 OK,返回内容块类型:['thinking', 'text']。 - 缺陷表现:
tool_use生成数为 0!模型完全忽略强制参数,正常回答文本。
场景 2:OpenAI 协议 /v1/chat/completions 显式指定 tool_choice: {"type": "function", "function": {"name": "lookup_user_profile"}}
curl -X POST http://127.0.0.1:8045/v1/chat/completions \
-H "Authorization: Bearer <API_KEY>" \
-H "Content-Type: application/json" \
-d '{
"model": "gemini-2.5-flash",
"messages": [{"role": "user", "content": "Just say hi."}],
"tools": [{
"type": "function",
"function": {
"name": "lookup_user_profile",
"description": "Lookup user profile",
"parameters": {"type": "object", "properties": {"uid": {"type": "string"}}, "required": ["uid"]}
}
}],
"tool_choice": {"type": "function", "function": {"name": "lookup_user_profile"}}
}'实测响应结果 (连续 5 轮):
- 轮次 1-5:HTTP 状态码
200 OK,tool_calls为null或空数组。 - 缺陷表现:模型直接输出字符串
"Hi!",强制调用指令被流水线完全抹杀。
源码根因深度分析 / Root Cause Analysis
src-tauri/src/proxy/mappers/claude/models.rs(第 8-42 行):ClaudeRequest结构体缺少:#[serde(skip_serializing_if = "Option::is_none")] pub tool_choice: Option<serde_json::Value>,src-tauri/src/proxy/mappers/claude/request.rs: 在将 Claude 请求转换为 Gemini 请求时,未将 Anthropic 的tool_choice映射为 Gemini 官方的toolConfig:- Anthropic
{"type": "auto"}-> Gemini{"functionCallingConfig": {"mode": "AUTO"}} - Anthropic
{"type": "any"}-> Gemini{"functionCallingConfig": {"mode": "ANY"}} - Anthropic
{"type": "tool", "name": "foo"}-> Gemini{"functionCallingConfig": {"mode": "ANY", "allowedFunctionNames": ["foo"]}} - Anthropic
{"type": "none"}-> Gemini{"functionCallingConfig": {"mode": "NONE"}}
- Anthropic
src-tauri/src/proxy/pipeline/inbound.rs(第 853-860 行): 历史在修复 #4.8.2 时,为了防止默认生成的配置产生冲突,使用了激进的全部移除策略:导致即使客户端显式传了inner_request.as_object_mut().map(|m| { m.remove("toolConfig"); m.remove("tool_config"); });tool_choice并正确构筑了合法的toolConfig,也在此处被无差别当场斩杀。
建议修复方案 / Proposed Solution
- 在
ClaudeRequest中引入tool_choice字段,并在transform_claude_request_in中将其规范映射至inner_request["toolConfig"]; - 修正
align_google_request_prefix_topology的过滤边界: 仅在没有指定显式模式或存在非法冗余时进行清洗,若存在客户端合法显式指定的functionCallingConfig(如mode == "ANY"或包含allowedFunctionNames),应予以保留并按标准格式发往 Google 上游:// 保留客户端显式指定的 toolConfig if let Some(tc) = inner_request.get_mut("toolConfig") { // 仅规整清理非标 snake_case 字段,保留标准的 camelCase functionCallingConfig } inner_request.as_object_mut().map(|m| { m.remove("tool_config"); // 移除废弃的 snake_case,保留 toolConfig });
这样即可彻底恢复所有主流 Agent 框架在 Antigravity Tools 下的强制工具调用能力!非常感谢开发团队的辛勤工作!
Source: lbjlaq/Antigravity-Manager