Ë
    uPê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	m
Z
 erddlmZ dZeed<   d	e
d
eeef   dz  ddfd„Zded	e
deeef   fd„Zdddefd„Zd	ddddefd„Zy)zß
Helper util for handling XAI-specific cost calculation
- Uses the generic cost calculator which already handles tiered pricing correctly
- Handles XAI-specific reasoning token billing (billed as part of completion tokens)
é    )ÚMapping)ÚTYPE_CHECKINGÚFinal)Úgeneric_cost_per_token)ÚPromptTokensDetailsWrapperÚUsage)Ú	ModelInfog{®Gázt?Ú!_DEFAULT_WEB_SEARCH_COST_PER_CALLÚusageÚdetailsNÚreturnc                 óØ   — |€y|| _         	 t        |j                  d«      xs d«      }|dk  ry| j
                  xs
 t        «       }||_        || _        y# t        t        f$ r Y yw xY w)z�
    Attach server_side_tool_usage_details and mirror web_search_calls onto
    prompt_tokens_details.web_search_requests for built-in tool cost gating.
    NÚweb_search_callsr   )Úserver_side_tool_usage_detailsÚintÚgetÚ	TypeErrorÚ
ValueErrorÚprompt_tokens_detailsr   Úweb_search_requests)r   r   r   r   s       úe/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/litellm/llms/xai/cost_calculator.pyÚ-apply_server_side_tool_usage_details_to_usager      s   € ð
 €ØØ+2€EÔ(ðÜ"% g§k¡kÐ2DÓ&EÒ&JÈÓ"KÐð ˜1ÒØØ#(×#>Ñ#>Ò#^ÔB\ÓB^ÐØ0@ÐÔ-Ø"7€EÕøô ”zÐ"ò Ùðús   ŒA ÁA)Á(A)Úmodelc                 óÂ  — t        t        |dd«      xs d«      }t        t        |dd«      xs d«      }t        t        |dd«      xs d«      }t        |d«      r0|j                  r$t        t        |j                  dd«      xs d«      nd}|||z   k(  }|r|n||z   }t	        |j
                  ||j                  |j                  d¬«      }t        | |d	¬
«      \  }	}
|	|
fS )a¼  
    Calculates the cost per token for a given XAI model, prompt tokens, and completion tokens.
    Uses the generic cost calculator for all pricing logic, with XAI-specific reasoning token handling.

    Input:
        - model: str, the model name without provider prefix
        - usage: LiteLLM Usage block, containing XAI-specific usage information

    Returns:
        Tuple[float, float] - prompt_cost_in_usd, completion_cost_in_usd
    Úprompt_tokensr   Úcompletion_tokensÚtotal_tokensÚcompletion_tokens_detailsÚreasoning_tokensN)r   r   r   r   r   Úxai)r   r   Úcustom_llm_provider)	r   ÚgetattrÚhasattrr   r   r   r   r   r   )r   r   r   r   r   r   Úalready_normalisedÚtotal_completion_tokensÚmodified_usageÚprompt_costÚcompletion_costs              r   Úcost_per_tokenr)   '   sû   € ô  œw u¨o¸qÓAÒFÀQÓG€MÜ"¤7¨5Ð2EÀqÓ#IÒ#NÈQÓOÐÜœg e¨^¸QÓ?ÒDÀ1ÓE€Lô �5Ð5Ô6¸5×;ZÒ;Zô 	ŒG�E×3Ñ3Ð5GÈÓKÒPÈqÔQàð ð !-°Ð@QÑ0QÑ QÐÙ:LÑ%6ÐRcÐfvÑRvÐä!Ø×)Ñ)Ø1Ø×'Ñ'Ø#×9Ñ9Ø"&ô€Nô $:ÀÈ^ÐqvÔ#wÑ €K�à˜Ð'Ð'ó    Ú
model_infor	   c                 óæ   — | j                  d«      }t        |t        «      st        S dD ]+  }|j                  |«      }|€Œ	 t	        |«      }|dkD  sŒ)|c S  t        S # t
        t        f$ r Y ŒEw xY w)zî
    Per-invocation web_search price from model_info when configured.

    Prefer ``search_context_cost_per_query`` (same shape as Gemini/Anthropic web
    search pricing in the model cost map). Fall back to current xAI list pricing.
    Úsearch_context_cost_per_query)Úsearch_context_size_mediumÚsearch_context_size_lowÚsearch_context_size_highr   )r   Ú
isinstancer   r
   Úfloatr   r   )r+   Úsearch_costsÚkeyÚvalueÚcosts        r   Ú)_web_search_cost_per_call_from_model_infor7   P   s‹   € ð %Ÿ.™.Ð)HÓI€LÜ�l¤GÔ,Ü0Ð0ðò ˆð
 × Ñ  Ó%ˆØˆ=Øð	Ü˜“<ˆDð �!‹8ØŠKðô -Ð,øô	 œ:Ð&ò 	Ùð	ús   ÁAÁA0Á/A0r   c                 óÎ   — t        | dd«      }t        |t        «      sy	 t        |j	                  d«      xs d«      }|dk  ryt        |«      |z  S # t
        t        f$ r Y yw xY w)a"  
    Calculate the cost of web search requests for X.AI models.

    Counts invocations from usage.server_side_tool_usage_details.web_search_calls.
    Per-call rate comes from model_info.search_context_cost_per_query when set,
    otherwise the default xAI tools rate ($5 / 1k calls).
    r   Ng        r   r   )r"   r1   r   r   r   r   r   r7   )r   r+   r   r   s       r   Úcost_per_web_search_requestr9   k   sv   € ô ˜UÐ$DÀdÓK€GÜ�gœwÔ'ØðÜ"% g§k¡kÐ2DÓ&EÒ&JÈÓ"KÐð ˜1ÒØÜ4°ZÓ@ÐCSÑSÐSøô	 ”zÐ"ò Ùðús    A ÁA$Á#A$)Ú__doc__Úcollections.abcr   Útypingr   r   Ú.litellm.litellm_core_utils.llm_cost_calc.utilsr   Úlitellm.types.utilsr   r   r	   r
   Ú__annotations__ÚstrÚobjectr   Útupler2   r)   r7   r9   © r*   r   ú<module>rD      s­   ðòõ $ß 'å Qß AáÝ-ð ,8Ð ! 5Ó 7ð8¸ð 8ÈÐQTÐV\ÐQ\ÑI]Ð`dÑIdð 8Ðimó 8ð&&(˜#ð &( eð &(°°e¸U°lÑ0Có &(ðR-¸+ð -È%ó -ð6T wð T¸Kð TÈEô Tr*   