Ë
    uPêjÝ  ã                   ó  — d Z ddlmZmZmZ ddlmZ ddlmZm	Z	 ddl
mZmZmZmZ ddlmZ erddlmZ 	 ddlmZmZmZ d	Z G d„ d«      Z	 	 	 ddededz  dedz  dedz  def
d„Z	 ddededddeddf
d„Zy# e$ r d
ZdZdZdZY ŒEw xY w)z`
A2A Protocol Exception Mapping Utils.

Maps A2A SDK exceptions to LiteLLM A2A exception types.
é    )ÚTYPE_CHECKINGÚAnyÚFinal)Úverbose_logger)Úis_localhost_or_internal_urlÚset_agent_card_url)ÚA2AAgentCardErrorÚA2AConnectionErrorÚA2AErrorÚA2ALocalhostURLError)ÚCONNECTION_ERROR_PATTERNS)ÚClient)r   ÚClientConfigÚcreate_clientTFNc                   ód   — e Zd ZdZededefd„«       Zededz  defd„«       Zededefd„«       Z	y)	ÚA2AExceptionCheckerszA
    Helper class for checking various A2A error conditions.
    Ú	error_strÚreturnc                 óv   ‡— t        | t        «      sy| j                  «       Št        ˆfd„t        D «       «      S )zÕ
        Check if an error string indicates a connection error.

        Args:
            error_str: The error string to check

        Returns:
            True if the error indicates a connection issue
        Fc              3   ó&   •K  — | ]  }|‰v –— Œ
 y ­w©N© ©Ú.0ÚpatternÚerror_str_lowers     €úq/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/litellm/a2a_protocol/exception_mapping_utils.pyú	<genexpr>z;A2AExceptionCheckers.is_connection_error.<locals>.<genexpr>9   s   øè ø€ ÒW°'�7˜oÔ-ÑWùó   ƒ)Ú
isinstanceÚstrÚlowerÚanyr   )r   r   s    @r   Úis_connection_errorz(A2AExceptionCheckers.is_connection_error*   s0   ø€ ô ˜)¤SÔ)Øà!*§¡Ó!2ˆÜÓWÔ=VÔWÓWÐWó    ÚurlNc                 ó   — t        | «      S )z²
        Check if a URL is a localhost/internal URL.

        Args:
            url: The URL to check

        Returns:
            True if the URL is localhost/internal
        )r   )r&   s    r   Úis_localhost_urlz%A2AExceptionCheckers.is_localhost_url;   s   € ô ,¨CÓ0Ð0r%   c                 óv   ‡— t        | t        «      sy| j                  «       Šg d¢}t        ˆfd„|D «       «      S )zã
        Check if an error string indicates an agent card error.

        Args:
            error_str: The error string to check

        Returns:
            True if the error is related to agent card fetching/parsing
        F)z
agent cardz
agent-cardz.well-knownzcard not foundzinvalid agentc              3   ó&   •K  — | ]  }|‰v –— Œ
 y ­wr   r   r   s     €r   r   z;A2AExceptionCheckers.is_agent_card_error.<locals>.<genexpr>^   s   øè ø€ ÒQ°'�7˜oÔ-ÑQùr   )r    r!   r"   r#   )r   Úagent_card_patternsr   s     @r   Úis_agent_card_errorz(A2AExceptionCheckers.is_agent_card_errorH   s:   ø€ ô ˜)¤SÔ)Øà!*§¡Ó!2ˆò&
Ðô ÓQÐ=PÔQÓQÐQr%   )
Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ústaticmethodr!   Úboolr$   r(   r,   r   r%   r   r   r   %   sy   „ ñð ðX sð X¨tò Xó ðXð  ð
1˜c D™jð 
1¨Tò 
1ó ð
1ð ðR sð R¨tò Ró ñRr%   r   Úoriginal_exceptionÚcard_urlÚapi_baseÚmodelr   c                 ó@  — t        | «      }|r;|r9t        j                  |«      r$t        j                  |«      rt	        ||| |¬«      ‚t        j                  |«      rt        |||¬«      ‚t        j                  |«      rt        ||xs ||¬«      ‚t        ||¬«      ‚)as  
    Map an A2A SDK exception to a LiteLLM A2A exception type.

    Args:
        original_exception: The original exception from the A2A SDK
        card_url: The URL from the agent card (if available)
        api_base: The original API base URL
        model: The model/agent name

    Returns:
        A mapped LiteLLM A2A exception

    Raises:
        A2ALocalhostURLError: If the error is a connection error to a localhost URL
        A2AConnectionError: If the error is a general connection error
        A2AAgentCardError: If the error is related to agent card issues
        A2AError: For other A2A-related errors
    )Úlocalhost_urlÚbase_urlÚoriginal_errorr6   )Úmessager&   r6   )r;   r6   )	r!   r   r(   r$   r   r,   r	   r
   r   )r3   r4   r5   r6   r   s        r   Úmap_a2a_exceptionr<   a   s²   € ô0 Ð-Ó.€Iñ 	ÙÜ ×1Ñ1°(Ô;Ü ×4Ñ4°YÔ?ä"Ø"ØØ-Øô	
ð 	
ô ×/Ñ/°	Ô:ÜØØØô
ð 	
ô ×/Ñ/°	Ô:Ü ØØÒ$˜HØô
ð 	
ô ØØôð r%   ÚerrorÚ
agent_cardÚ
a2a_clientÚA2AClientTypeÚis_streamingc              ƒ   ó¨  K  — t         st        d«      ‚|€t        d«      ‚|rdnd}t        j                  d|| j
                  | j                  | j                  «       t        || j                  «       t        |dd«      }|€t        d«      ‚t        |t        ||¬	«      ¬
«      ƒ d{  –—† }||_        t        |dd«      |_        ||_        |S 7 Œ&­w)a  
    Handle A2ALocalhostURLError by fixing the URL and creating a new client.

    This is called when we catch an A2ALocalhostURLError and want to retry
    with the corrected URL.

    Args:
        error: The localhost URL error
        agent_card: The agent card object to fix
        a2a_client: The current A2A client
        is_streaming: Whether this is a streaming request (for logging)

    Returns:
        A new A2A client with the fixed URL

    Raises:
        ImportError: If the A2A SDK is not installed
    zVA2A SDK is required for localhost retry handling. Install it with: pip install a2a-sdkNzsCannot retry A2A localhost URL fix: no agent card is available to rewrite, so the upstream URL cannot be corrected.z
streaming Ú zjA2A %srequest to '%s' failed: %s. Agent card contains localhost/internal URL. Retrying with base_url '%s'.Ú_litellm_httpx_clientz|Cannot retry A2A localhost URL fix: the client was not created by create_a2a_client, so no LiteLLM httpx client is attached.)Úhttpx_clientÚ	streaming)Úclient_configÚ_litellm_call_context)ÚA2A_SDK_AVAILABLEÚImportErrorÚRuntimeErrorr   Úwarningr8   r:   r9   r   Úgetattrr   r   rD   rH   Ú_litellm_agent_card)r=   r>   r?   rA   Úrequest_typerE   Ú
new_clients          r   Úhandle_a2a_localhost_retryrQ       sø   è ø€ õ0 ÜÐrÓsÐsàÐÜð@ó
ð 	
ñ
 +7™,¸B€LÜ×ÑØtØØ×ÑØ×ÑØ�‰ôô �z 5§>¡>Ô2ô
 " *Ð.EÀtÓL€LØÐÜðIó
ð 	
ô
 ,ØÜ"Ø%Ø"ô
ô÷ €Jð (4€JÔ$Ü'.ØÐ+¨Tó(€JÔ$ð &0€JÔ"ØÐðús   ‚B'CÂ)CÂ*'C)NNN)F)r0   Útypingr   r   r   Úlitellm._loggingr   Ú"litellm.a2a_protocol.card_resolverr   r   Úlitellm.a2a_protocol.exceptionsr	   r
   r   r   Úlitellm.constantsr   Ú
a2a.clientr   r@   r   r   rI   rJ   r   Ú	Exceptionr!   r<   r2   rQ   r   r%   r   ú<module>rY      sý   ðñ÷ -Ñ ,å +÷÷ó õ 8áÝ2ðß>Ñ>àÐ÷9Rñ 9Rð|  ØØñ	<Ø!ð<à�D‰jð<ð �D‰jð<ð �‰:ð	<ð
 ó<ðF ñ	CØðCàðCð  ðCð ð	Cð
 ôCøðE ò ØÐØ€FØ€LØ‚Mð	ús   ¶A8 Á8BÂB