Ë
    uPêj§  ã                   ó²   — U d Z ddlmZmZ erddlmZmZ ddedddedz  d	ee	e	f   fd
„Z
ddddd	e	fd„ZdZeed<   dZeed<   ddd	edz  fd„Zddddd	e	fd„Zy)zi
This file is used to calculate the cost of the Gemini API.

Handles the context caching for Gemini API.
é    )ÚTYPE_CHECKINGÚFinal)Ú	ModelInfoÚUsageNÚmodelÚusager   Úservice_tierÚreturnc                 ó&   — ddl m}  || |d|¬«      S )z§
    Calculates the cost per token for a given model, prompt tokens, and completion tokens.

    Follows the same logic as Anthropic's cost per token calculation.
    r   )Úgeneric_cost_per_tokenÚgemini)r   r   Úcustom_llm_providerr	   )Ú.litellm.litellm_core_utils.llm_cost_calc.utilsr   )r   r   r	   r   s       úh/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/litellm/llms/gemini/cost_calculator.pyÚcost_per_tokenr      s!   € õ Vá!ØØØ$Ø!ô	ð ó    Ú
model_infor   c                 ó°  — ddl m} ddlm} d}|j	                  d«      xs i }|j	                  d|«      }| �d| j
                  �Xt        | j
                  |«      rBt        | j
                  d«      r,| j
                  j                  �| j
                  j                  nd} || «      }|xs |xs d}	|j	                  d	«      xs d
}
|	dkD  r|
d
k(  rdn|	}||z  S )a%  
    Calculates the cost of web search (grounding with Google Search).

    Billing mode is determined by ``web_search_billing_unit`` in model_info:
    - ``"per_query"``: charged per individual search query (Gemini 3.x).
    - ``"per_prompt"`` (default): charged per grounded prompt (Gemini 2.x),
      regardless of how many queries were executed internally.

    Reads the per-request cost from ``search_context_cost_per_query`` in
    ``model_info`` when available, falling back to $0.035 for models not
    yet updated in the pricing JSON.
    r   )Ú"get_web_search_requests_from_usage©ÚPromptTokensDetailsWrappergìQ¸…ë¡?Úsearch_context_cost_per_queryÚsearch_context_size_mediumNÚweb_search_requestsÚweb_search_billing_unitÚ
per_prompté   )	r   r   Úlitellm.types.utilsr   ÚgetÚprompt_tokens_detailsÚ
isinstanceÚhasattrr   )r   r   r   r   Ú_DEFAULT_COSTÚsearch_costsÚ_costÚrequests_from_prompt_detailsÚrequests_from_server_tool_useÚnumber_of_web_search_requestsÚbilling_modeÚbillable_requestss               r   Úcost_per_web_search_requestr+      sö   € õõ ?à €MØ$Ÿ.™.Ð)HÓIÒOÈR€LØ×#Ñ#Ð$@À-ÓP€Eð
 ÐØ×+Ñ+Ð7Ü˜5×6Ñ6Ð8RÔSÜ˜×3Ñ3Ð5JÔKØ×+Ñ+×?Ñ?ÐKð 	×#Ñ#×7Ò7ð ð !ñ ,NÈeÓ+TÐ!Ø+GÒ+mÐKhÒ+mÐlmÐ!à$Ÿ.™.Ð)BÓCÒSÀ|€Là+¨aÒ/°LÀLÒ4P‰ÐWtð ð Ð$Ñ$Ð$r   gyé&1¬Œ?Ú,GOOGLE_MAPS_GROUNDING_DEFAULT_COST_PER_QUERYgš™™™™™™?Ú-GOOGLE_MAPS_GROUNDING_DEFAULT_COST_PER_PROMPTzUsage | Nonec                 óx   — ddl m} | �| j                  nd }t        ||«      rt	        |d«      sy |j
                  S )Nr   r   Úgoogle_maps_grounding_requests)r   r   r    r!   r"   r/   )r   r   Údetailss      r   r/   r/   M   s;   € Ý>à49Ð4E�U×0Ò0È4€GÜ�gÐ9Ô:Ä'È'ÐSsÔBtØØ×1Ñ1Ð1r   c                 ó¾   — t        | «      }|r|dk  ry|j                  d«      xs d}|dk(  rt        nt        }|j                  d«      }|€|n|}|dk(  r|nd}||z  S )aý  
    Calculates the cost of Grounding with Google Maps.

    Billing follows ``web_search_billing_unit`` in model_info the same way Google Search grounding
    does: ``"per_query"`` (Gemini 3.x) multiplies the executed Maps queries, ``"per_prompt"``
    (default, Gemini 2.x) charges one flat fee per grounded prompt.

    The rate comes from ``google_maps_grounding_cost_per_query`` in ``model_info``, falling back
    to Google's list price for that billing unit when the pricing JSON has no entry yet.
    r   g        r   r   Ú	per_queryÚ$google_maps_grounding_cost_per_queryr   )r/   r   r,   r-   )r   r   Úrequestsr)   Údefault_costÚconfigured_costÚcostÚbilled_requestss           r   Ú&cost_per_google_maps_grounding_requestr9   V   s{   € ô 5°UÓ;€HÙ�x 1’}ØØ$Ÿ.™.Ð)BÓCÒSÀ|€Lð ˜;Ò&õ 	5ä:ð ð
 (Ÿ^™^Ð,RÓS€OØ"1Ð"9‘,¸€DØ)5¸Ò)D™XÈ!€OØ�/Ñ!Ð!r   )N)Ú__doc__Útypingr   r   r   r   r   ÚstrÚtupleÚfloatr   r+   r,   Ú__annotations__r-   Úintr/   r9   © r   r   ú<module>rB      s¬   ðò÷ (áß4ñ˜#ð  gð ¸SÀ4¹Zð ÐSXÐY^Ð`eÐYeÑSfó ð )% wð )%¸Kð )%ÈEó )%ðX 7<Ð ,¨eÓ ;Ø7<Ð -¨uÓ <ð2¨.ð 2¸SÀ4¹Zó 2ð"°'ð "À{ð "ÐW\ô "r   