Ë
    tPêjÍ  ã                   ó’  — U d Z ddlmZ ddlmZmZ ddlmZ ddlm	Z	 ddl
mZmZ ddlmZ ddlmZ  G d	„ d
ee«      ZdZeeedf      ed<   dZeed<    G d„ de«      Zdedefd„Zdeeef   dz  deedf   fd„Zdedeeef   dz  deedf   fd„Zej:                  deddœdededeeef   dz  dedeeef   dz  deeef   fd„Zy)a¹  
Fetch an A2A agent's well-known card from the upstream agent.

Different agent runtimes publish the card at different URL shapes, so the
fetcher dispatches by ``discovery_mode``:

- ``well_known_fallback`` (pure A2A): the card lives at one of the standard
  well-known paths on the agent's own base URL. We try the canonical path,
  then the previous-spec path, then a non-standard root fallback.

- ``langgraph_platform``: LangGraph Platform mounts a single card endpoint at
  ``{base}/.well-known/agent-card.json`` and disambiguates assistants via the
  ``assistant_id`` query parameter. There is no per-assistant subpath, so the
  pure-A2A fallback strategy returns 404 for these deployments.
é    )ÚEnum)ÚAnyÚFinal)Ú	urlencode)Úverbose_proxy_logger)Ú	SSRFErrorÚasync_safe_get)Úget_async_httpx_client)ÚhttpxSpecialProviderc                   ó   — e Zd ZdZdZdZy)ÚDiscoveryModezmHow to locate the upstream agent card.

    String-valued so it serializes cleanly over JSON / Pydantic.
    Úwell_known_fallbackÚlanggraph_platformN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚWELL_KNOWN_FALLBACKÚLANGGRAPH_PLATFORM© ó    ú`/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/litellm/proxy/a2a/discovery.pyr   r      s   „ ñð
 0ÐØ-Ñr   r   )z/.well-known/agent-card.jsonz/.well-known/agent.jsonz/agent.json.ÚAGENT_CARD_WELL_KNOWN_PATHSg      $@Ú!DEFAULT_DISCOVERY_TIMEOUT_SECONDSc                   ó   — e Zd ZdZy)ÚAgentCardDiscoveryErrorzFRaised when none of the well-known paths returned a usable agent card.N)r   r   r   r   r   r   r   r   r   1   s   „ ÚPr   r   Úbase_urlÚreturnc                 ó$   — | j                  d«      S )Nú/)Úrstrip)r   s    r   Ú_normalize_base_urlr"   5   s   € Ø�?‰?˜3ÓÐr   ÚparamsNc                 ó¤   ‡— | xs i j                  d«      }|st        d«      ‚t        dt        |«      i«      Št	        ˆfd„t
        D «       «      S )aU  Build the paths to try for LangGraph Platform discovery.

    LangGraph serves the card at ``/.well-known/agent-card.json`` with the
    ``assistant_id`` carried as a query parameter. We still try the other
    A2A path variants (with the same query string appended) so we degrade
    gracefully if a deployment uses an older spec name.
    Úassistant_idz9langgraph_platform discovery requires params.assistant_idc              3   ó,   •K  — | ]  }|› d ‰› �–— Œ y­w)ú?Nr   )Ú.0ÚpathÚquerys     €r   ú	<genexpr>z2_build_langgraph_platform_paths.<locals>.<genexpr>G   s   øè ø€ ÒK t�D�6˜˜5˜'Ô"ÑKùs   ƒ)Úgetr   r   ÚstrÚtupler   )r#   r%   r*   s     @r   Ú_build_langgraph_platform_pathsr/   9   sN   ø€ ð "š< R×,Ñ,¨^Ó<€LÙÜ%Ð&aÓbÐbÜ˜n¬c°,Ó.?Ð@ÓA€EÜÓKÔ/JÔKÓKÐKr   Úmodec                 óŒ   — | t         j                  k(  rt        S | t         j                  k(  rt	        |«      S t        d| › �«      ‚)Nzunsupported discovery_mode: )r   r   r   r   r/   r   )r0   r#   s     r   Ú_paths_for_moder2   J   sB   € ØŒ}×0Ñ0Ò0Ü*Ð*ØŒ}×/Ñ/Ò/Ü.¨vÓ6Ð6Ü
!Ð$@ÀÀÐ"GÓ
HÐHr   )Údiscovery_moder#   ÚtimeoutÚheadersr3   r4   r5   c             ƒ   óV  K  — | st        d«      ‚t        | «      }t        ||«      }t        t        j
                  d|i¬«      }d}|D ]º  }	|› |	› �}
	 t        ||
|xs i ¬«      ƒ d{  –—† }|j                  d	k\  r3|
› d
|j                  › �}t        j                  d|j                  |
«       Œf	 |j                  «       }t        |t        «      s|
› dt        |«      j                   › �}Œ¢t        j                  d|
«       |c S  t        d| › d|j"                  › d|› �«      ‚7 Œ»# t        $ r)}|
› d|› �}t        j                  d|
|«       Y d}~�Œ
d}~wt        $ r)}|
› d|› �}t        j                  d|
|«       Y d}~�Œ:d}~ww xY w# t        $ r}|
› d|› d�}Y d}~�ŒYd}~ww xY w­w)a`  
    Fetch an agent card from ``base_url`` using the strategy chosen by
    ``discovery_mode``. Returns the parsed JSON from the first path that
    responds with a JSON body.

    Raises:
        AgentCardDiscoveryError: if every path fails (network error, non-2xx,
            or non-JSON body), or if the chosen mode is missing required params.
    zbase_url is requiredr4   )Úllm_providerr#   N)r5   z: z.A2A discovery blocked by SSRF guard for %s: %szA2A discovery failed for %s: %si�  z: HTTP zA2A discovery HTTP %s for %sz: invalid JSON (ú)z: expected JSON object, got zA2A discovery succeeded at %sz Could not fetch agent card from z (mode=z). Last error: )r   r"   r2   r
   r   ÚA2Ar	   r   r   ÚdebugÚ	ExceptionÚstatus_codeÚjsonÚ
isinstanceÚdictÚtyper   Úvalue)r   r3   r#   r4   r5   Ú
normalizedÚpathsÚclientÚ
last_errorr)   ÚurlÚresponseÚexcÚcards                 r   Úfetch_well_known_cardrJ   R   sß  è ø€ ñ" Ü%Ð&<Ó=Ð=ä+¨HÓ5€JÜ" >°6Ó:€EÜ*Ü)×-Ñ-Ø˜7Ð#ô€Fð
 "€JØò 'ˆØ�˜T˜FÐ#ˆð	ô ,¨F°CÀÂÈBÔO×OˆHð ×Ñ 3Ò&Ø˜5 ¨×(<Ñ(<Ð'=Ð>ˆJÜ ×&Ñ&Ð'EÀx×G[ÑG[Ð]`ÔaØð	Ø—=‘=“?ˆDô
 ˜$¤Ô%Ø˜5Ð <¼TÀ$»Z×=PÑ=PÐ<QÐRˆJØä×"Ñ"Ð#BÀCÔHØŠðO'ôR "Ø
*¨8¨*°G¸N×<PÑ<PÐ;QÐQ`ÐakÐ`lÐmóð ð9 PùÜò 	Ø˜5  3 %˜ˆJÜ ×&Ñ&Ð'WÐY\Ð^aÔbÝûÜò 	Ø˜5  3 %˜ˆJÜ ×&Ñ&Ð'HÈ#ÈsÔSÝûð	ûô ò 	Ø˜5Ð 0°°°QÐ7ˆJÝûð	üs€   ‚AF)ÁD%Á'D#Á(D%Á,AF)Â/F
Â?A$F)Ä#D%Ä%	FÄ.EÅF)ÅFÅFÅ<F)ÆFÆF)Æ
	F&ÆF!ÆF)Æ!F&Æ&F)) r   Úenumr   Útypingr   r   Úurllib.parser   Úlitellm._loggingr   Ú$litellm.litellm_core_utils.url_utilsr   r	   Ú&litellm.llms.custom_httpx.http_handlerr
   Úlitellm.types.llms.custom_httpr   r-   r   r   r.   Ú__annotations__r   r;   r   r"   r?   r/   r2   r   ÚfloatrJ   r   r   r   ú<module>rT      sX  ðòõ  ß Ý "å 1ß JÝ IÝ ?ô.�C˜ô .ð7Ð ˜U 5¨¨c¨¡?Ñ3ó ð ,0Ð ! 5Ó /ôQ˜iô Qð  #ð  ¨#ó  ðLØ��c�‰N˜TÑ!ðLà
ˆ3�ˆ8�_óLð"I˜-ð I°°c¸3°h±À$Ñ1Fð IÈ5ÐQTÐVYÐQYÉ?ó Ið %2×$EÑ$EØ$(Ø6Ø%)òGØðGð "ðGð ��c�‰N˜TÑ!ð	Gð
 ðGð �#�s�(‰^˜dÑ"ðGð 
ˆ#ˆsˆ(�^ôGr   