Skip to content

feat: 支持Claude Code调用WebSearch工具#372

Draft
foresee-io wants to merge 1 commit into
su-kaka:masterfrom
foresee-io:feat/websearch
Draft

feat: 支持Claude Code调用WebSearch工具#372
foresee-io wants to merge 1 commit into
su-kaka:masterfrom
foresee-io:feat/websearch

Conversation

@foresee-io
Copy link
Copy Markdown

Warning

  • 该功能分支正投入日常使用测试,由于贡献者 @foresee-io 手头仅使用着Claude Code,还不能完全确保在其他桥接模式下能正确使用。
  • 使用了AI辅助编程和人工校核,具体技术路径见此处

当前使用 GCLI接入 + Anthropic输出,在Claude Code 2.1.118 版本使用时,WebSearch工具将返回空值,是不可用的状态。因此进行适配,适配效果如下图。

Claude Code 前端 gcli2api 后端
independent-search independent-search_api

/v1/messages/antigravity/v1/messages 端点中新增对 Anthropic 原生 web_search_20250305 / web_search_20260209 工具类型的支持。

此前,客户端发送此类工具类型时将遭遇错误,因 convert_tools() 将其视为常规函数工具。现已实现对该类工具的识别,将其与函数工具分离,并转换为 Gemini 的 {"googleSearch": {}} 格式。

主要变更:

  • src/models.py:扩展 ClaudeToolClaudeContentBlockClaudeUsage 类,增加与 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 原生路由均不受影响。

…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant