Ë
    uPêj  ã                   óR  — U d Z ddlmZ ddl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<   	 ddlmZ ddlmZmZ dedz  defd„Z eddddœ«      Zeed<   dZeed<   d"d„Zdddedz  fd„Zdddeddfd„Zdddeddfd„Z G d „ d!e«      Zy# e$ r Y ŒYw xY w)#zr
Custom A2A Card Resolver for LiteLLM.

Extends the A2A SDK's card resolver to support multiple well-known paths.
é    )ÚMappingProxyType)ÚTYPE_CHECKINGÚAnyÚFinal)Ú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                 óZ   ‡— | sy| j                  «       Št        ˆf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   ó&   •K  — | ]  }|‰v –— Œ
 y ­w)N© )Ú.0ÚpatternÚ	url_lowers     €úg/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/litellm/a2a_protocol/card_resolver.pyú	<genexpr>z/is_localhost_or_internal_url.<locals>.<genexpr>1   s   øè ø€ ÒJ¨ˆw˜)Ô#ÑJùs   ƒ)ÚlowerÚanyr   )r   r   s    @r   Úis_localhost_or_internal_urlr      s(   ø€ ñ Øà—y‘y“{€IäÓJÔ3IÔJÓJÐJó    ÚJSONRPCz	HTTP+JSONÚGRPC)Újsonrpcz	http+jsonÚgrpcÚ_CANONICAL_PROTOCOL_BINDINGSz0.3Ú_LEGACY_PROTOCOL_VERSIONÚ
agent_cardr	   c                 ó  —  t        | «      «       }|j                  | «       |j                  D ]S  }t        j	                  |j
                  j                  «       «      }|�||j
                  k(  rŒB||_        t        |_        ŒU |S )aú  
    Canonicalize the supported interfaces of spec-adjacent agent cards.

    Some A2A servers (e.g. LangGraph Platform) serve agent cards with lowercase
    bindings like "jsonrpc", but a2a-sdk's ClientFactory matches bindings
    case-sensitively against its uppercase TransportProtocol constants and fails
    with "no compatible transports found." for spec-adjacent casings.

    The same servers also speak the A2A 0.3 JSON dialect ("kind"-discriminated
    payloads) while declaring protocolVersion "1.0", which a2a-sdk's strict v1
    proto parsing rejects. A mis-cased binding fingerprints such a server, so its
    declared version is downgraded to 0.3 to route the SDK's ClientFactory onto
    its v0.3 compat transport, which speaks that dialect.
    )	ÚtypeÚCopyFromÚsupported_interfacesr    ÚgetÚprotocol_bindingr   r!   Úprotocol_version)r"   Ú
normalizedÚ	interfaceÚ	canonicals       r   Únormalize_agent_card_interfacesr-   ?   sƒ   € ð )œ˜ZÓ(Ó*€JØ×Ñ˜
Ô#Ø×4Ñ4ò >ˆ	Ü <× @Ñ @À×A[ÑA[×AaÑAaÓAcÓ dˆ	ØÐ 	¨Y×-GÑ-GÒ GØØ%.ˆ	Ô"Ü%=ˆ	Õ"ð>ð Ðr   c                 ód   — t        | dd«      }|r|S t        | dd«      }|rt        |d   dd«      S y)z9Return the agent endpoint URL from the resolved SDK card.r   Nr&   r   )Úgetattr)r"   r   Ú
interfacess      r   Úget_agent_card_urlr1   Y   s@   € ä˜ U¨DÓ1€CÙ
Øˆ
ä 
Ð,BÀDÓI€JÙÜ�z !‘} e¨TÓ2Ð2Ør   c                 ó†   — |j                  d«      dz   }t        | d«      r|| _        t        | dd«      }|r||d   _        yy)z4Set the agent endpoint URL on the resolved SDK card.ú/r   r&   Nr   )ÚrstripÚhasattrr   r/   )r"   r   r*   r0   s       r   Úset_agent_card_urlr6   e   sH   € àŸ
™
 3›¨#Ñ-€JÜˆz˜5Ô!Ø#ˆ
Œä 
Ð,BÀDÓI€JÙØ&ˆ
�1‰Õð r   Úbase_urlc                 ó   — t        | dd«      }|r&t        |«      r|j                  d«      dz   }|| _        t        | dd«      }|r9t        |d   dd«      }|r't        |«      r|j                  d«      dz   |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   Nr3   r&   r   )r/   r   r4   r   )r"   r7   Úcard_urlÚ	fixed_urlr0   Úinterface_urls         r   Úfix_agent_card_urlr<   p   s†   € ô ˜j¨%°Ó6€HáÔ0°Ô:à#Ÿ?™?¨3Ó/°#Ñ5ˆ	Ø"ˆ
Œä 
Ð,BÀDÓI€JÙÜ& z°!¡}°e¸TÓBˆÙÔ9¸-ÔHØ (§¡°Ó 4°sÑ :ˆJ�q‰MÔàÐr   c                   óL   ‡ — e Zd ZdZ	 	 ddedz  deeef   dz  ddfˆ fd„Zˆ x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              ƒ   ó¼  •K  — |�t         ‰| �  ||¬«      ƒ d{  –—† S t        t        g}d}|D ]>  }	 t	        j
                  d| j                  |«       t         ‰| �  ||¬«      ƒ d{  –—† c S  |�|‚t        d| j                  › ddj                  |«      › �«      ‚7 Œƒ7 Œ7# t        $ r.}t	        j
                  d| j                  ||«       |}Y d}~Œ¥d}~ww xY w­w)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 %s%sz(Failed to fetch agent card from %s%s: %sz Failed to fetch agent card from z. Tried paths: z, )	ÚsuperÚget_agent_cardr   r   r   Údebugr7   Ú	ExceptionÚjoin)Úselfr?   r@   ÚpathsÚ
last_errorÚpathÚeÚ	__class__s          €r   rC   z%LiteLLMA2ACardResolver.get_agent_card˜   s  øè ø€ ð, Ð)Ü™Ñ/Ø#5Ø'ð 0ó ÷ ð ô 'Ü+ð
ˆð
 ˆ
Øò 
	ˆDð	Ü×$Ñ$Ð%OÐQU×Q^ÑQ^Ð`dÔeÜ"™WÑ3Ø'+Ø +ð 4ó ÷ ò ð
	ð Ð!ØÐô Ð:¸4¿=¹=¸/ÈÐY]×YbÑYbÐchÓYiÐXjÐkÓlÐlð;øðùô ò Ü×$Ñ$Ð%OÐQU×Q^ÑQ^Ð`dÐfgÔhØ�
ÜûðüsJ   ƒCšB›C³5B"Á(B Á)B"Á,3CÂ B"Â"	CÂ+$CÃCÃCÃC)NN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚstrÚdictr   rC   Ú__classcell__)rL   s   @r   r>   r>   �   sL   ø„ ñð *.Ø-1ñ4mà $™Jð4mð ˜#˜s˜(‘^ dÑ*ð4mð 
÷	4mñ 4mr   r>   )r"   r	   r   r	   )rP   Útypesr   Útypingr   r   r   Úlitellm._loggingr   Úlitellm.constantsr   Ú	a2a.typesr	   r
   Ú__annotations__r   rQ   r   Ú
a2a.clientr   Úa2a.utils.constantsÚImportErrorÚboolr   r    r!   r-   r1   r6   r<   r>   r   r   r   ú<module>r^      s  ðòõ #ß ,Ñ ,å +Ý 4áÝ#ð Ð �#Ó Ø"@Ð ˜CÓ @Ø'@Ð  Ó @ð	Ý>÷ðK c¨D¡jð K°Tó Kñ* '7àØ Øñó'Ð ˜eó ð #(Ð ˜%Ó 'óð4	 ;ð 	°3¸±:ó 	ð' ;ð '°Sð '¸Tó 'ð ;ð ¸#ð À+ó ô>=mÐ-õ =møði ò 	Ùð	ús   ¾B ÂB&Â%B&