o
    ưi                     @   s   U d Z ddlmZmZmZmZ ddlmZ ddlm	Z	 er#ddl
mZ dZeed< dZeed	< d
Zeed< zddlmZ ddlmZmZ W n	 eyN   Y nw dee defddZdddeddfddZG dd deZdS )zr
Custom A2A Card Resolver for LiteLLM.

Extends the A2A SDK's card resolver to support multiple well-known paths.
    )TYPE_CHECKINGAnyDictOptional)verbose_logger)LOCALHOST_URL_PATTERNS)	AgentCardN_A2ACardResolverz/.well-known/agent-card.jsonAGENT_CARD_WELL_KNOWN_PATHz/.well-known/agent.jsonPREV_AGENT_CARD_WELL_KNOWN_PATH)A2ACardResolver)r
   r   urlreturnc                    s&   | sdS |    t fddtD S )a  
    Check if a URL is a localhost or internal URL.

    This detects common development URLs that are accidentally left in
    agent cards when deploying to production.

    Args:
        url: The URL to check

    Returns:
        True if the URL is localhost/internal
    Fc                 3   s    | ]}| v V  qd S )N ).0patternZ	url_lowerr   Y/home/app/Keep/.python/lib/python3.10/site-packages/litellm/a2a_protocol/card_resolver.py	<genexpr>0   s    z/is_localhost_or_internal_url.<locals>.<genexpr>)loweranyr   )r   r   r   r   is_localhost_or_internal_url   s   r   
agent_cardr   base_urlc                 C   s0   t | dd}|rt|r|dd }|| _| S )a  
    Fix the agent card URL if it contains a localhost/internal address.

    Many A2A agents are deployed with agent cards that contain internal URLs
    like "http://0.0.0.0:8001/" or "http://localhost:8000/". This function
    replaces such URLs with the provided base_url.

    Args:
        agent_card: The agent card to fix
        base_url: The base URL to use as replacement

    Returns:
        The agent card with the URL fixed if necessary
    r   N/)getattrr   rstripr   )r   r   Zcard_urlZ	fixed_urlr   r   r   fix_agent_card_url3   s
   r   c                       sD   e Zd ZdZ		d	dee deeeef  ddf fddZ  Z	S )
LiteLLMA2ACardResolverz
    Custom A2A card resolver that supports multiple well-known paths.

    Extends the base A2ACardResolver to try both:
    - /.well-known/agent-card.json (standard)
    - /.well-known/agent.json (previous/alternative)
    Nrelative_card_pathhttp_kwargsr   r   c                    s   |durt  j||dI dH S ttg}d}|D ]=}ztd| j |  t  j||dI dH W   S  tyU } ztd| j | d|  |}W Y d}~qd}~ww |dur\|td| j dd| )a  
        Fetch the agent card, trying multiple well-known paths.

        First tries the standard path, then falls back to the previous path.

        Args:
            relative_card_path: Optional path to the agent card endpoint.
                If None, tries both well-known paths.
            http_kwargs: Optional dictionary of keyword arguments to pass to httpx.get

        Returns:
            AgentCard from the A2A agent

        Raises:
            A2AClientHTTPError or A2AClientJSONError if both paths fail
        N)r   r    z$Attempting to fetch agent card from z Failed to fetch agent card from z: z. Tried paths: z, )	superget_agent_cardr
   r   r   debugr   	Exceptionjoin)selfr   r    pathsZ
last_errorpathe	__class__r   r   r"   U   sD   
z%LiteLLMA2ACardResolver.get_agent_card)NN)
__name__
__module____qualname____doc__r   strr   r   r"   __classcell__r   r   r*   r   r   L   s    
r   )r/   typingr   r   r   r   Zlitellm._loggingr   Zlitellm.constantsr   Z	a2a.typesr   r	   __annotations__r
   r0   r   Z
a2a.clientr   Za2a.utils.constantsImportErrorboolr   r   r   r   r   r   r   <module>   s$    