Skip to content

feat: support Anthropic web search (server_tool_use) translation#241

Open
jackylam0812 wants to merge 1 commit intoericc-ch:masterfrom
jackylam0812:feat/anthropic-web-search-support
Open

feat: support Anthropic web search (server_tool_use) translation#241
jackylam0812 wants to merge 1 commit intoericc-ch:masterfrom
jackylam0812:feat/anthropic-web-search-support

Conversation

@jackylam0812
Copy link
Copy Markdown

Problem

When using copilot-api with Claude Code or other Anthropic API clients that enable web search, the server-side tool types (server_tool_use, server_tool_result, web_search_tool_result) are not recognized. This causes:

  1. translateAnthropicToolsToOpenAI to crash when encountering server tools (no input_schema)
  2. handleUserMessage to silently drop server_tool_result / web_search_tool_result blocks
  3. handleAssistantMessage to silently drop server_tool_use blocks
  4. No web search capability at all through the proxy

Solution

Type definitions (anthropic-types.ts)

  • Added AnthropicServerTool, AnthropicServerToolUseBlock, AnthropicServerToolResultBlock, AnthropicWebSearchToolResultBlock types
  • Extended AnthropicUserContentBlock and AnthropicAssistantContentBlock unions

Message translation (non-stream-translation.ts)

  • translateAnthropicToolsToOpenAI: Filter out server tools (they have no input_schema) instead of crashing
  • handleUserMessage: Convert server_tool_result / web_search_tool_result blocks into user text messages with search context
  • handleAssistantMessage: Convert server_tool_use blocks into text annotations
  • Export hasWebSearchTool helper for handler use

Web search proxy (handler.ts)

  • When web search server tools are present and mode is non-streaming:
    • Detect search intent from the model's response
    • Perform proxy search via a separate gpt-4o Copilot request
    • Return synthetic server_tool_use + web_search_tool_result blocks

Limitations

  • Web search proxy currently works in non-streaming mode only
  • Search is performed via gpt-4o as a proxy (not direct Bing API)
  • Streaming support can be added in a follow-up PR

Testing

Tested with Claude Code + copilot-api v0.7.0:

  • ✅ Web search requests properly translated and proxied
  • ✅ Existing tool_use / tool_result flow unaffected
  • ✅ Models without web search work as before

Add support for Anthropic's server-side tool types (web_search) that are
used by Claude Code and other Anthropic API clients.

Changes:
- anthropic-types.ts: Add AnthropicServerTool, AnthropicServerToolUseBlock,
  AnthropicServerToolResultBlock, AnthropicWebSearchToolResultBlock types
- non-stream-translation.ts: Filter server tools from OpenAI tool translation,
  handle server_tool_use/result blocks in message conversion, export
  hasWebSearchTool helper
- handler.ts: Add web search proxy logic — when Anthropic client requests
  web_search server tool, detect search intent and perform proxy search
  via gpt-4o, returning synthetic server_tool_use/web_search_tool_result
  blocks

This fixes compatibility with Claude Code's WebSearch feature when using
copilot-api as the API backend. The proxy approach works in non-streaming
mode by delegating actual web search to a web-capable Copilot model.

Closes ericc-ch#241
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