o
    ưi8                     @   s@   d Z ddlmZmZmZ erddlmZ neZG dd dZdS )z
Cost calculator for A2A (Agent-to-Agent) calls.

Supports dynamic cost parameters that allow platform owners
to define custom costs per agent query or per token.
    )TYPE_CHECKINGAnyOptional)Loggingc                	   @   sL   e Zd Zedee defddZededee dee defdd	Z	d
S )A2ACostCalculatorlitellm_logging_objreturnc                 C   s   | du rdS | j }|dd}|durt|S |di pi }|ddur,t|d S |d}|d}|dus>|durFtj|||dS dS )	a  
        Calculate the cost of an A2A send_message call.

        Supports multiple cost parameters for platform owners:
        - cost_per_query: Fixed cost per query
        - input_cost_per_token + output_cost_per_token: Token-based pricing

        Priority order:
        1. response_cost - if set directly (backward compatibility)
        2. cost_per_query - fixed cost per query
        3. input_cost_per_token + output_cost_per_token - token-based cost
        4. Default to 0.0

        Args:
            litellm_logging_obj: The LiteLLM logging object containing call details

        Returns:
            float: The cost of the A2A call
        N        response_costlitellm_paramsZcost_per_queryinput_cost_per_tokenoutput_cost_per_token)model_call_detailsr   r   )r   getfloatr   _calculate_token_based_cost)r   r   r
   r   r   r    r   [/home/app/Keep/.python/lib/python3.10/site-packages/litellm/a2a_protocol/cost_calculator.pycalculate_a2a_cost   s$   

z$A2ACostCalculator.calculate_a2a_costr   r   r   c                 C   sf   |  d}|du rdS t|ddpd}t|ddpd}||r"t|nd }||r,t|nd }|| S )a  
        Calculate cost based on token usage and per-token pricing.

        Args:
            model_call_details: The model call details containing usage
            input_cost_per_token: Cost per input token (can be None, defaults to 0)
            output_cost_per_token: Cost per output token (can be None, defaults to 0)

        Returns:
            float: The calculated cost
        usageNr	   prompt_tokensr   completion_tokens)r   getattrr   )r   r   r   r   r   r   Z
input_costZoutput_costr   r   r   r   I   s   
z-A2ACostCalculator._calculate_token_based_costN)
__name__
__module____qualname__staticmethodr   LitellmLoggingObjectr   r   dictr   r   r   r   r   r      s"    5r   N)	__doc__typingr   r   r   Z*litellm.litellm_core_utils.litellm_loggingr   r   r   r   r   r   r   <module>   s    