+
    iHàj?  ã                  óÈ   a € 0 t $ R t^ RIHt ^ RIt^ RIHt ^ RIHtH	t	 ]P                  ! ]4      t]! RRR7      tR]R&   ]! R	R
04      tR R ltR R ltRRRR/R R lltR# )a8  Ambient session-accounting context for auxiliary LLM calls.

Aux calls (vision, compression, title generation, web_extract, session_search, ...) go
through ``agent.auxiliary_client`` which has no session handle, so their usage was
historically discarded. The agent loop publishes ``(session_db, session_id)`` here
(mirroring ``agent.portal_tags``) and the aux client records usage at its single
response-validation chokepoint. ContextVar semantics isolate concurrent agents, propagate
to worker threads via ``tools.thread_context`` and to asyncio tasks automatically.
)ÚannotationsN)Ú
ContextVar)ÚAnyÚOptionalÚaux_accounting_context)ÚdefaultzContextVar[Optional[tuple]]Ú_accountingÚmoa_referenceÚmoa_aggregatorc               ó    € V ^8„  d   QhRRRR/# )é   Ú
session_dbr   Ú
session_idúOptional[str]© )Úformats   "Ú9/home/ubuntu/.hermes/hermes-agent/agent/aux_accounting.pyÚ__annotate__r      s   € ÷ 5ñ 5 sð 5¸ñ 5ó    c                óp   € V e	   V'       g   \         P                  R4      # \         P                  W34      # )z¢Publish the active session's accounting handles; returns the token for ``reset_accounting_context``.

``None`` handles (no DB / no session id) clear the context.
N)r   Úset)r   r   s   &&r   Úset_accounting_contextr      s-   € ð
 Ò§Ü�‰˜tÓ$Ð$Ü�?‰?˜JÐ3Ó4Ð4r   c               ó   € V ^8„  d   QhRR/# )r   ÚreturnÚNoner   )r   s   "r   r   r   %   s   € ÷ ñ  tñ r   c                ó€   €  \         P                  V 4       R#   \         d    \         P                  R4        R# i ; i)z@Restore the previous accounting context (pair with ``set_...``).N)r   ÚresetÚ	Exceptionr   )Útokens   &r   Úreset_accounting_contextr   %   s0   € ðÜ×Ñ˜%Ö øÜô Ü�‰˜×ðús   ‚ ™ =¼=ÚproviderÚbase_urlc          
     ó,   € V ^8„  d   QhRRRRRRRRRR/# )	r   Úresponser   Útaskr   r    r!   r   r   r   )r   s   "r   r   r   -   s=   € ÷ 2Nñ 2NØð2NØ&ð2NØ5Bð2Nàð2Nð 
ñ2Nr   c               ó~  €  V'       d   V\         9   d   R# \        P                  4       pVf   R# Vw  rV\        V RR4      pVf   R# ^ RIHpHp	Hp
 V
! V	! WrR7      V 4      pVP                  '       gK   VP                  '       g9   VP                  '       g'   VP                  '       g   VP                  '       g   R# \        \        V RR4      ;'       g    R4      ;'       g    RpRp V! WËW#R7      pVP                  e   \        VP                  4      p^ RIHp T! TP*                  YaYÂTTP                  TP                  TP                  TP                  TP                  TR7       R#   \          d    \"        P%                  R	R
R7        Lzi ; i  \          d    \"        P%                  RR
R7        R# i ; i)a}  Record an auxiliary response's token usage against the ambient session.

Strictly best-effort (accounting must never break an aux call). No-ops outside an
agent turn, for main-loop-accounted tasks (``_EXCLUDED_TASKS``), or without usage.
The model is read from ``response.model`` (accurate after aux provider fallback);
*provider*/*base_url* reflect the originally-resolved route.
NÚusage)Úestimate_usage_costÚnormalize_usageÚwith_served_service_tier)r    ÚmodelÚ Úunknown)r    r!   z Aux usage cost estimation failedT)Úexc_info)Úcall_off_event_loop)	r*   Úbilling_providerÚbilling_base_urlÚinput_tokensÚoutput_tokensÚcache_read_tokensÚcache_write_tokensÚreasoning_tokensÚestimated_cost_usdz&Aux usage recording failed (non-fatal))Ú_EXCLUDED_TASKSr   ÚgetÚgetattrÚagent.usage_pricingr'   r(   r)   r1   r2   r3   r4   r5   ÚstrÚ
amount_usdÚfloatr   ÚloggerÚdebugÚagent.runtime_session_storer.   Úrecord_auxiliary_usage)r#   r$   r    r!   Úctxr   r   Ú	raw_usager'   r(   r)   r&   r*   Úestimated_costÚcostr.   s   &&$$            r   Úrecord_aux_usagerF   -   s‚  € ð'Nß�tœÔ.ÙÜ�o‰oÓˆØŠ;ÙØ!$Ñˆ
Ü˜H g¨tÓ4ˆ	ØÒÙçfÑfá(©¸Ô)VÐX`Óaˆà××Ð %×"5×"5Ð"5Ø×&×&Ð&¨%×*B×*BÐ*BØ×%×%Ð%áÜ”G˜H g¨rÓ2×8Ð8°bÓ9×FÐF¸YˆØˆð	LÙ& u¸hÔZˆDØ�‰Ò*Ü!& t§¡Ó!7�õ
 	DÙØ×-Ñ-Ø EÐW_Ø×+Ñ+¸5×;NÑ;NØ#×5Ñ5È%×JbÑJbØ"×3Ñ3È÷	
øô ô 	LÜ�L‰LÐ;ÀdˆLÖKð	Lûô ô NÜ�‰Ð=Èˆ×MðNúso   ‚F ŠF —F ±F Á=F ÂF ÂF Â*F Â>F ÃF Ã%F Ã+-E. ÄAF Å."FÆF ÆFÆF Æ"F<Æ;F<)Ú__conditional_annotations__Ú__doc__Ú
__future__r   ÚloggingÚcontextvarsr   Útypingr   r   Ú	getLoggerÚ__name__r>   r   Ú__annotations__Ú	frozensetr7   r   r   rF   )rG   s   @r   Ú<module>rQ      sy   øðôõ #ã Ý "ß  à	×	Ò	˜8Ó	$€ñ ,6Ð6NÐX\Ô+]€Ð(Ó ]ñ ˜_Ð.>Ð?Ó@€õ5õð2NØEIð2Nà"÷2Nñ 2Nr   