Ë
    tPêj	  ã                   óŠ   — U d Z ddlmZ ddlmZ dZeed<   dedefd„Z	d	edefd
„Z
dedefd„Zdedefd„Zdeeef   defd„Zy)z»
Single chokepoint for reading prompt-compression token savings out of a parsed
SpendLog ``metadata`` JSON dict. Imported by the daily-spend DB writer and by
cost-savings read endpoints.
é    )ÚMapping)ÚFinalÚheadroomÚHEADROOM_GUARDRAIL_PROVIDERÚvalueÚreturnc                 ór   — t        | t        «      st        | t        t        f«      sy| dk  ryt        | «      S )Nr   )Ú
isinstanceÚboolÚintÚfloat)r   s    úu/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/litellm/proxy/spend_tracking/compression_savings.pyÚ_saved_tokens_or_zeror      s0   € Ü�%œÔ¤j°¼¼e¸Ô&EØØˆq‚yØÜˆu‹:Ðó    Ústatsc                 óX   — t        | t        «      syt        | j                  d«      «      S )Nr   Útokens_saved)r
   r   r   Úget)r   s    r   Ú_tokens_saved_from_statsr      s#   € Ü�eœWÔ%ØÜ  §¡¨>Ó!:Ó;Ð;r   Úentryc                 óŠ   — t        | t        «      sy| j                  d«      t        k7  ryt	        | j                  d«      «      S )Nr   Úguardrail_providerÚguardrail_response)r
   r   r   r   r   )r   s    r   Ú_headroom_entry_saved_tokensr      s:   € Ü�eœWÔ%ØØ‡y�yÐ%Ó&Ô*EÒEØÜ# E§I¡IÐ.BÓ$CÓDÐDr   Úguardrail_informationc                 ór   — t        | t        «      r| gn| }t        |t        «      syt        d„ |D «       «      S )Nr   c              3   ó2   K  — | ]  }t        |«      –— Œ y ­w)N)r   )Ú.0r   s     r   ú	<genexpr>z)_headroom_saved_tokens.<locals>.<genexpr>'   s   è ø€ ÒH°uÔ+¨E×2ÑHùs   ‚)r
   r   ÚlistÚsum)r   Úentriess     r   Ú_headroom_saved_tokensr#   #   s7   € Ü0:Ð;PÔRYÔ0ZÐ+Ñ,Ð`u€GÜ�gœtÔ$ØÜÑHÀÔHÓHÐHr   Úmetadatac                 ól   — t        | j                  d«      «      t        | j                  d«      «      z   S )a;  
    Return the total prompt tokens saved by compression for one request.

    Sums two disjoint sources:

    - the native ``compression_savings`` key, written only by
      ``CompressionInterceptionLogger`` in its pre-call deployment hook
    - ``guardrail_information`` entries with ``guardrail_provider ==
      "headroom"``, written only by the Headroom guardrail

    Each writer records only its own transform pass and the two run at
    different stages (guardrail pre-call vs deployment pre-call), so when both
    fire on one request their measured savings are independent and additive;
    summing them never double-counts. Malformed or missing values contribute 0.
    A bare dict ``guardrail_information`` is treated as a single entry, matching
    the spend-log redactor's normalization of that legacy shape.
    Úcompression_savingsr   )r   r   r#   )r$   s    r   Ú extract_compression_saved_tokensr'   *   s6   € ô$ $ H§L¡LÐ1FÓ$GÓHÔKaØ�‰Ð,Ó-óLñ ð r   N)Ú__doc__Úcollections.abcr   Útypingr   r   Ú__annotations__Úobjectr   r   r   r   r#   Ústrr'   © r   r   ú<module>r/      s‹   ðòõ $Ý à%/Ð ˜UÓ /ð ð ¨Có ð< Fð <¨só <ðE¨ð E°3ó EðI°&ð I¸Só Ið¨w°s¸F°{Ñ/Cð Èô r   