o
    ưi                  	   @   s   d Z ddlmZmZmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZ G dd de
Zd	ee d
efddZ	ddeeef deded
efddZ	ddeeef ded
efddZdS )z4
Common utilities for A2A (Agent-to-Agent) Protocol
    )AnyDictList)	BaseModel)convert_content_list_to_str)BaseLLMException)AllMessageValuesc                	       s:   e Zd ZdZi fdededeeef f fddZ  Z	S )A2AErrorz&Base exception for A2A protocol errorsstatus_codemessageheadersc                    s   t  j|||d d S )N)r
   r   r   )super__init__)selfr
   r   r   	__class__ T/home/app/Keep/.python/lib/python3.10/site-packages/litellm/llms/a2a/common_utils.pyr      s
   
zA2AError.__init__)
__name__
__module____qualname____doc__intstrr   r   r   __classcell__r   r   r   r   r	      s    
r	   messagesreturnc                 C   s~   g }| D ]5}t |d}t|tr| dd}nt|tr%|dd}nt|dd}|r9|| d|  qd|S )at  
    Convert OpenAI messages to a single prompt string for A2A agent.
    
    Formats each message as "{role}: {content}" and joins with newlines
    to preserve conversation history. Handles both string and list content.
    
    Args:
        messages: List of OpenAI-format messages
    
    Returns:
        Formatted prompt string with full conversation context
    )r   roleuserz: 
)r   
isinstancer   Z
model_dumpgetdictappendjoin)r   Zconversation_partsmsgZcontent_textr   r   r   r   convert_messages_to_prompt   s   



r&   
   r   depth	max_depthc                 C   s   | du s||kr
dS |  dg }g }|D ]&}| ddkr'|| dd qd|v r:t||d |}|r:|| qd|S )aE  
    Extract text content from A2A message parts.
    
    Args:
        message: A2A message dict with 'parts' containing text parts
        depth: Current recursion depth (internal use)
        max_depth: Maximum recursion depth to prevent infinite loops
    
    Returns:
        Concatenated text from all text parts
    N partskindtext    )r!   r#   extract_text_from_a2a_messager$   )r   r(   r)   r+   Z
text_partspartZnested_textr   r   r   r0   ?   s   

r0   response_dictc           	      C   s   |  di }t|tsdS d|v rt|d|dS | d}|r&t|d|dS | d}|r9t|tr9t|d|dS | di }t|trR| d}|rRt|d|dS | d	g }|rkt|dkrk|d }t|d|dS dS )
a  
    Extract text content from A2A response result.
    
    Args:
        response_dict: A2A response dict with 'result' containing message
        max_depth: Maximum recursion depth to prevent infinite loops
    
    Returns:
        Text from response message parts
    resultr*   r+   r   )r(   r)   r   artifactstatus	artifacts)r!   r    r"   r0   len)	r2   r)   r3   r   r4   r5   Zstatus_messager6   Zfirst_artifactr   r   r   extract_text_from_a2a_response_   s6   





r8   N)r   r'   )r'   )r   typingr   r   r   Zpydanticr   Z8litellm.litellm_core_utils.prompt_templates.common_utilsr   Z)litellm.llms.base_llm.chat.transformationr   Zlitellm.types.llms.openair   r	   r   r&   r   r0   r8   r   r   r   r   <module>   s4    !

!
