feat: 支持Claude Code调用WebSearch工具#372
Draft
foresee-io wants to merge 1 commit into
Draft
Conversation
…I and Antigravity
Add support for Anthropic-native web_search_20250305 / web_search_20260209 tool
types in /v1/messages and /antigravity/v1/messages endpoints. Previously, clients
sending these tool types would hit errors as convert_tools() treated them as
regular function tools. Now they are detected, separated from function tools,
and converted to Gemini's {"googleSearch": {}} format.
Key changes:
- src/models.py: extend ClaudeTool, ClaudeContentBlock, ClaudeUsage with
web_search-related fields (type, max_uses, allowed_domains, blocked_domains,
user_location, encrypted_content, page_age, citations, server_tool_use)
- src/converter/anthropic2gemini.py: add separate_web_search_tools() and
grounding-to-citations helpers; modify request, non-stream response, and
stream response converters to inject grounding metadata blocks
- src/converter/gemini_fix.py: extract inject_google_search_tool() as shared
helper; add -search suffix support for antigravity mode; fix falsy-bug
where tool.get("googleSearch") returned {} for existing googleSearch tools
All changes follow an "additive branch, default no-op" pattern: when no
web_search tool is present or no groundingMetadata is in the response, the
new code paths are entirely skipped. Router and API layers are untouched.
Existing -search model suffix, function calling, thinking, anti-truncation,
fake-streaming, and Gemini-native routes are all unaffected.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Warning
当前使用 GCLI接入 + Anthropic输出,在Claude Code 2.1.118 版本使用时,WebSearch工具将返回空值,是不可用的状态。因此进行适配,适配效果如下图。
于
/v1/messages及/antigravity/v1/messages端点中新增对 Anthropic 原生web_search_20250305/web_search_20260209工具类型的支持。此前,客户端发送此类工具类型时将遭遇错误,因
convert_tools()将其视为常规函数工具。现已实现对该类工具的识别,将其与函数工具分离,并转换为 Gemini 的{"googleSearch": {}}格式。主要变更:
src/models.py:扩展ClaudeTool、ClaudeContentBlock、ClaudeUsage类,增加与 web_search 相关的字段(type,max_uses,allowed_domains,blocked_domains,user_location,encrypted_content,page_age,citations,server_tool_use)。src/converter/anthropic2gemini.py:新增separate_web_search_tools()及grounding-to-citations辅助函数;修改请求、非流式响应及流式响应转换器,以注入 grounding 元数据块。src/converter/gemini_fix.py:提取inject_google_search_tool()作为共享辅助函数;增加对 antigravity 模式下-search后缀的支持;修复了当存在现有googleSearch工具时,tool.get("googleSearch")错误返回{}的假值缺陷。所有变更遵循“增量分支,默认无操作”(additive branch, default no-op)模式:若不存在
web_search工具或响应中不含groundingMetadata,则将完全跳过新代码路径。路由层与 API 层未作改动。现有的-search模型后缀、函数调用(function calling)、思维链(thinking)、抗截断(anti-truncation)、伪流式(fake-streaming)及 Gemini 原生路由均不受影响。