o
    ưi                     @   s   d Z ddlmZmZ ddlmZ deeef fddZdeeef fddZd	eeef de	fd
dZ
d	eeef de	fddZdS )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DictLITELLM_WEB_SEARCH_TOOL_NAMEreturnc                   C   s    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   Zinput_schemar    r   r   h/home/app/Keep/.python/lib/python3.10/site-packages/litellm/integrations/websearch_interception/tools.pyget_litellm_web_search_tool   s   r   c                   C   s&   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   r   "get_litellm_web_search_tool_openai4   s   r   toolc                 C   s\   |  dd}|  dd}|dkr&d| v r&|  di }| dd}|tkr&dS |tkr,dS dS )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   TF)getr   r   Z	tool_nameZ	tool_typeZfunction_defZfunction_namer   r   r   "is_web_search_tool_chat_completionU   s   r   c                 C   s   |  dd}|  dd}|dkr&d| v r&|  di }| dd}|tkr&dS |tkr,dS |dr3dS |dkr;|r;dS |dkrAdS d	S )
a}  
    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 format)

    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
    r   r   r   r   TZweb_search_Z
web_searchZ	WebSearchF)r   r   
startswithr   r   r   r   is_web_search_tool   s    
r   N)__doc__typingr   r   Zlitellm.constantsr   strr   r   boolr   r   r   r   r   r   <module>   s    &!,