ó
    °"³j�  ã                  ód  • S r SSKJr  SSKrSSKJr  SSKJr  SSKJr  SSK	J
r
  SSKJr  S	S
KJr  S	SKJr  \(       a  SSKJr  S	SKJr  S	SKJr  S	SKJrJr  SS jrSSSS.       SS jjrSSS.       SS jjrSSSS.           SS jjrSSSS.           SS jjrSS jrg)zºShared [genai-prices](https://github.com/pydantic/genai-prices) helpers: best-effort cost calculation, the provider lookup order used for usage extraction, and the context window lookup.é    )ÚannotationsN)ÚIterator)Údatetime)ÚTYPE_CHECKING)Ú
calc_price©Úget_snapshoté   )ÚCostCalculationFailedWarning)Ú	UserError)ÚPriceCalculation)ÚModelResponse)ÚAbstractModel)ÚRequestUsageÚRunUsagec                 ó   • [        5         g)z«Load genai-prices' deferred data snapshot at `Model` construction, keeping the one-time cost off the event loop.

See https://github.com/pydantic/pydantic-ai/issues/7405.
Nr   © ó    ÚV/home/mande/repo/quber/.venv/lib/python3.13/site-packages/pydantic_ai/_genai_prices.pyÚpreload_pricing_datar      s	   € ô
 …Nr   )Úprovider_api_urlÚprovider_idÚprovider_fallbackc              #  ó^   #   • SU 4US4US44 H  u  p4U(       d	  U(       d  M  X44v •  M     g7f)a£  Yield `(provider_id, provider_api_url)` genai-prices lookup references, most specific first.

The API URL identifies a provider more precisely than a name (e.g. several providers reselling the
same models), so it's tried first, then the provider ID, then the fallback ID; references with
nothing to match on are skipped. Shared by `RequestUsage.extract` and `lookup_context_window` so
the lookup order is defined once.
Nr   )r   r   r   Úcandidate_idÚcandidate_urls        r   Úiter_provider_referencesr       s?   é € ð *.Ð/?Ð(@À;ÐPTÐBUÐXiÐkoÐWpÓ'qÑ#ˆÞŸ=˜=ØÐ-Ô-ò (rùs   ‚-£
-)r   Úprovider_namec               óB  • [        U [        5      (       a  U nO$U R                  U R                  p# U R                  n[        XS9 H-  u  pE [        5       R                  USXES9u  pgUR                  s  $    g! [
        [        4 a    Sn NOf = f! [         a     MW  f = f)a¢  Look up a model's context window in [genai-prices](https://github.com/pydantic/genai-prices) data.

Takes a model instance, whose `model_name`, `system`, and `base_url` are matched on, or a model name
together with the provider name and/or API URL to match on. Returns the context window recorded for
the model under the first provider reference that knows it, or `None` if none does or no context
window is recorded.
N)r   r   )Úproviderr   r   )Ú
isinstanceÚstrÚ
model_nameÚsystemÚbase_urlÚAttributeErrorr   r   r	   Úfind_provider_modelÚLookupErrorÚcontext_window)Úmodelr   r   r#   r   r   Ú_Ú
model_infos           r   Úlookup_context_windowr-   2   s·   € ô �%œ×ÑØ‰
à$)×$4Ñ$4°e·l±l�Mð	$Ø$Ÿ~™~Ðô
 (@Ø)ô(Ñ#ˆð	Ü(›N×>Ñ>Ø T°|ð ?ð ‰MˆAð
 ×(Ñ(Ò(ñ(ð øô ¤	Ð*ó 	$ð  $Òð	$ûô ó 	Úð	ús#   ±A8 ÁBÁ8BÂBÂ
BÂB)r   r   Úgenai_request_timestampc               ób   • U(       a   [        U UUUS9$ [        U UUUS9$ ! [         a     Nf = f)a;  Price a usage object with [genai-prices](https://github.com/pydantic/genai-prices), propagating its errors.

Tries matching on `provider_api_url` first as it's more specific, then falls back to `provider_name`.
Only `ModelResponse.cost()` wants this behaviour; everything internal goes through `best_effort_price`.
)r   r.   )r   r.   )r   r(   )Úusager#   r   r   r.   s        r   Úcalculate_price_for_usager1   V   sT   € ö ð		ÜØØØ!1Ø(?ñ	ð ô ØØØ!Ø 7ñ	ð øô	 ó 	áð	ús   ‰! ¡
.­.c               óä   • U(       d  g [        U UUUUS9$ ! [        [        4 a     g[         a=  n[        R
                  " S[        U5      R                   SU 3[        SS9   SnAgSnAff = f)a  Price a usage object, degrading any failure to `None`; pricing must never fail a run.

A missing model name (e.g. a synthetic response from a capability) leaves nothing to look up.
`genai-prices` raises `LookupError` for providers/models it doesn't know about (including `test` and
`function` models) and `ValueError` for usage it can't price (e.g. cache token counts that imply a
negative uncached remainder); both are expected. Anything else is unexpected and surfaces as a
`CostCalculationFailedWarning` rather than being raised.
N©r#   r   r   r.   zFailed to get cost: z: é   )Ú
stacklevel)	r1   r(   Ú
ValueErrorÚ	ExceptionÚwarningsÚwarnÚtypeÚ__name__r   )r0   r#   r   r   r.   Úes         r   Úbest_effort_pricer=   w   s}   € ö  ØðÜ(ØØ!Ø-Ø'Ø$;ñ
ð 	
øô œÐ$ó ñ Üó Ü�ŠÐ,¬T°!«W×-=Ñ-=Ð,>¸bÀÀÐDÔFbÐopÒqÜûðús   Š —A/©	A/²3A*Á*A/c                óò   • U R                   R                  c`  [        U R                   U R                  U R                  U R
                  U R                  S9=n b  UR                  U R                   l        ggg)a3  Fill `response.usage.cost` with a best-effort price if it's still unset.

An already-set cost is never overwritten, so a provider-reported cost could take precedence in future; no model
sets one today. If pricing data is unavailable the cost stays `None`, distinguishing "unknown" from a genuine
zero cost.
Nr3   )r0   Úcostr=   r#   Úprovider_urlr   Ú	timestampÚtotal_price)ÚresponseÚprices     r   Úfill_response_costrE   ›   sv   € ð 	�‰×ÑÑ#ä&Ø—‘Ø#×.Ñ.Ø!)×!6Ñ!6Ø&×4Ñ4Ø(0×(:Ñ(:ñð ˆEð ñ	ð $×/Ñ/ˆ�‰Õð	ð 	$r   )ÚreturnÚNone)r   ú
str | Noner   rH   r   rH   rF   z'Iterator[tuple[str | None, str | None]])r*   zAbstractModel | strr   rH   r   rH   rF   z
int | None)r0   úRequestUsage | RunUsager#   r"   r   rH   r   rH   r.   údatetime | NonerF   r   )r0   rI   r#   rH   r   rH   r   rH   r.   rJ   rF   zPriceCalculation | None)rC   r   rF   rG   ) Ú__doc__Ú
__future__r   r8   Úcollections.abcr   r   Útypingr   Úgenai_pricesr   Úgenai_prices.data_snapshotr	   Ú	_warningsr   Ú
exceptionsr   Úgenai_prices.typesr   Úmessagesr   Úmodels._abstractr   r0   r   r   r   r   r-   r1   r=   rE   r   r   r   Ú<module>rV      s?  ðñ Aå "ã Ý $Ý Ý  å #Ý 3å 3Ý !æÝ3å'Ý/ß-ôð $(Ø"Ø$(ñ	.à ð.ð ð.ð "ð	.ð
 -õ.ð* $(Ø $ñ	!Øð!ð !ð!ð ð	!ð
 õ!ðP $(Ø $Ø/3ñØ"ðð ðð !ð	ð
 ðð -ðð õðJ $(Ø $Ø/3ñ!Ø"ð!ð ð!ð !ð	!ð
 ð!ð -ð!ð õ!õH0r   