Ë
    tPêjÝ)  ã                   óÎ   — d Z ddlmZmZ ddlmZ deeef   fd„Zdeeef   fd„Z	deeef   fd„Z
deeef   defd	„Zdeeef   defd
„Zdeeef   defd„Zdeeef   defd„Zy)zð
LiteLLM Web Search Tool Definition

This module defines the standard web search tool used across LiteLLM.
Native provider tools (like Anthropic's web_search_20250305) are converted
to this format for consistent interception and execution.
é    )ÚAnyÚFinal©ÚLITELLM_WEB_SEARCH_TOOL_NAMEÚreturnc                  ó*   — t         ddddddœidgdœdœS )	a=  
    Get the standard LiteLLM web search tool definition.

    This is the canonical tool definition that all native web search tools
    (like Anthropic's web_search_20250305, Claude Code's web_search, etc.)
    are converted to for interception.

    Returns:
        Dict containing the Anthropic-style tool definition with:
        - name: Tool name
        - description: What the tool does
        - input_schema: JSON schema for tool parameters

    Example:
        >>> tool = get_litellm_web_search_tool()
        >>> tool['name']
        'litellm_web_search'
    ú€Search the web for information. Use this when you need current information or answers to questions that require up-to-date data.ÚobjectÚqueryÚstringúThe search query to execute©ÚtypeÚdescription©r   Ú
propertiesÚrequired)Únamer   Úinput_schemar   © ó    úv/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/litellm/integrations/websearch_interception/tools.pyÚget_litellm_web_search_toolr      s;   € ô( -ðPð àØ$Ø#@ñðð !˜	ñ	
ñð r   c                  ó0   — dt         ddddddœidgdœd	œd
œS )aE  
    Get the standard LiteLLM web search tool definition in OpenAI format.

    Used by async_pre_call_deployment_hook which runs in the chat completions
    path where tools must be in OpenAI format (type: "function" with
    function.parameters).

    Returns:
        Dict containing the OpenAI-style tool definition.
    Úfunctionr	   r
   r   r   r   r   r   )r   r   Ú
parameters)r   r   r   r   r   r   Ú"get_litellm_web_search_tool_openair   4   sC   € ð ä0ðTð !àØ (Ø'Dñðð %˜Iñ	ñ
ñð r   c                  ó,   — dt         ddddddœidgdœd	œS )
a®  
    Get the standard LiteLLM web search tool definition in Responses API format.

    Used by async_pre_call_deployment_hook on the Responses API path, where a
    function tool is a flat object (``type: "function"`` with a top-level
    ``name`` and ``parameters``) rather than the nested ``function`` wrapper
    used by Chat Completions.

    Returns:
        Dict containing the Responses-style function tool definition.
    r   r	   r
   r   r   r   r   r   )r   r   r   r   r   r   r   r   Ú%get_litellm_web_search_tool_responsesr   U   s>   € ð Ü,ðPð àØ$Ø#@ñðð !˜	ñ	
ñð r   Útoolc                 ó²   — | j                  dd«      }t        |t        «      sy|dk(  r| j                  d«      t        k(  S |dk(  xs |j	                  d«      S )a¦  
    Check if a tool is a web search tool for the Responses API.

    Detects:
    - OpenAI native Responses web search tools, whose ``type`` is one of
      ``web_search``, ``web_search_2025_08_26``, ``web_search_preview``,
      ``web_search_preview_2025_03_11`` (matched by the ``web_search`` prefix)
    - The LiteLLM standard function tool in Responses shape:
      ``{"type": "function", "name": "litellm_web_search"}``

    Args:
        tool: Tool dictionary to check

    Returns:
        True if tool is a Responses-API web search tool

    Example:
        >>> is_web_search_tool_responses({"type": "web_search"})
        True
        >>> is_web_search_tool_responses({"type": "web_search_preview"})
        True
        >>> is_web_search_tool_responses({"type": "function", "name": "litellm_web_search"})
        True
        >>> is_web_search_tool_responses({"type": "function", "name": "get_weather"})
        False
    r   Ú Fr   r   Ú
web_searchÚweb_search_)ÚgetÚ
isinstanceÚstrr   Ú
startswith©r    Ú	tool_types     r   Úis_web_search_tool_responsesr+   u   sY   € ð6 —x‘x ¨Ó+€IÜ�i¤Ô%Øà�JÒØ�x‰x˜ÓÔ#?Ñ?Ð?à˜Ñ$ÒK¨	×(<Ñ(<¸]Ó(KÐKr   c                 óÎ   — | j                  dd«      }| j                  dd«      }|dk(  r2d| v r.| j                  di «      }|j                  dd«      }|t        k(  ry|t        k(  ryy)aÇ  
    Check if a tool is a web search tool for Chat Completions API (strict check).

    This is a stricter version that ONLY checks for the exact LiteLLM web search tool name.
    Use this for Chat Completions API to avoid false positives with user-defined tools.

    Detects ONLY:
    - LiteLLM standard: name == "litellm_web_search" (Anthropic format)
    - OpenAI format: type == "function" with function.name == "litellm_web_search"

    Args:
        tool: Tool dictionary to check

    Returns:
        True if tool is exactly the LiteLLM web search tool

    Example:
        >>> is_web_search_tool_chat_completion({"name": "litellm_web_search"})
        True
        >>> is_web_search_tool_chat_completion({"type": "function", "function": {"name": "litellm_web_search"}})
        True
        >>> is_web_search_tool_chat_completion({"name": "web_search"})
        False
        >>> is_web_search_tool_chat_completion({"name": "WebSearch"})
        False
    r   r"   r   r   TF)r%   r   ©r    Ú	tool_namer*   Úfunction_defÚfunction_names        r   Ú"is_web_search_tool_chat_completionr1   š   st   € ð6 —x‘x ¨Ó+€IØ—x‘x ¨Ó+€Ið �JÒ :°Ñ#5Ø"Ÿh™h z°2Ó6ˆØ+×/Ñ/°¸Ó;ˆØÔ8Ò8Øð Ô0Ò0Øàr   c                 óx   — | j                  dd«      }t        |t        «      sy|j                  d«      xr |dk7  S )aÌ  
    Check if a tool is an Anthropic-native ``web_search_*`` tool.

    Native clients (Anthropic SDK, Claude Desktop, Anthropic Console) send
    tools like ``{"type": "web_search_20250305", "name": "web_search"}`` and
    expect the response to contain ``web_search_tool_result`` content blocks
    so that citations can be rendered. This helper identifies that contract
    so the agentic loop can emit native-format blocks for those clients
    without affecting clients that send the LiteLLM standard tool.

    Returns False for the LiteLLM standard tool (``litellm_web_search``),
    the OpenAI-shaped variant, the bare ``WebSearch`` legacy name, and the
    bare ``web_search`` name (Claude Code style).
    r   r"   Fr$   r   )r%   r&   r'   r(   r)   s     r   Ú#is_anthropic_native_web_search_toolr3   Æ   s<   € ð —x‘x ¨Ó+€IÜ�i¤Ô%ØØ×Ñ Ó.ÒJ°9À
Ñ3JÐJr   c                 ó  — | j                  dd«      }| j                  dd«      }|dk(  r2d| v r.| j                  di «      }|j                  dd«      }|t        k(  ry|t        k(  ry|j                  d«      ry|dk(  r|ry|dk(  rd	| vryy
)uä  
    Check if a tool is a web search tool (native or LiteLLM standard).

    Detects:
    - LiteLLM standard: name == "litellm_web_search"
    - OpenAI format: type == "function" with function.name == "litellm_web_search"
    - Anthropic native: type starts with "web_search_" (e.g., "web_search_20250305")
    - Claude Code: name == "web_search" with a type field
    - Custom: name == "WebSearch" (legacy interception marker â€” only matched
      when input_schema is absent; see note below)

    Note on the legacy ``WebSearch`` name:
        Clients like Claude Desktop / Cowork ship a *client-side* tool called
        ``WebSearch`` (a fully-formed Anthropic client tool with its own
        ``input_schema``) that they handle themselves. Treating that as our
        interception marker hijacks it server-side and the client's own tool
        handler never fires â€” which means Cowork's separate native
        ``web_search_20250305`` sub-request (where citation data actually
        flows) never gets made.

        Real Anthropic client tools always carry an ``input_schema`` (the API
        rejects them otherwise), so a bare ``{name: "WebSearch"}`` with no
        schema is the only thing that could be a legacy interception marker.
        Gate the match on schema absence to keep both groups working.

    Args:
        tool: Tool dictionary to check

    Returns:
        True if tool is a web search tool

    Example:
        >>> is_web_search_tool({"name": "litellm_web_search"})
        True
        >>> is_web_search_tool({"type": "function", "function": {"name": "litellm_web_search"}})
        True
        >>> is_web_search_tool({"type": "web_search_20250305", "name": "web_search"})
        True
        >>> is_web_search_tool({"name": "calculator"})
        False
        >>> is_web_search_tool({"name": "WebSearch"})  # legacy interception marker
        True
        >>> is_web_search_tool({"name": "WebSearch", "input_schema": {"type": "object"}})  # Cowork client tool
        False
    r   r"   r   r   Tr$   r#   Ú	WebSearchr   F)r%   r   r(   r-   s        r   Úis_web_search_toolr6   Û   s«   € ð\ —x‘x ¨Ó+€IØ—x‘x ¨Ó+€Ið �JÒ :°Ñ#5Ø"Ÿh™h z°2Ó6ˆØ+×/Ñ/°¸Ó;ˆØÔ8Ò8Øð Ô0Ò0Øð ×Ñ˜MÔ*Øð �LÒ ¡YØð �KÒ N¸$Ñ$>Øàr   N)Ú__doc__Útypingr   r   Úlitellm.constantsr   Údictr'   r   r   r   Úboolr+   r1   r3   r6   r   r   r   ú<module>r<      sÈ   ðñ÷ å :ð# T¨#¨s¨(¡^ó #ðL¨D°°c°©Nó ðB¨t°C¸°H©~ó ð@"L t¨C°¨H¡~ð "L¸$ó "LðJ)¨T°#°s°(©^ð )Àó )ðXK¨d°3¸°8©nð KÀó Kð*I˜T # s (™^ð I°ô Ir   