
    `gj6                       U d Z ddlmZ ddlZddlZddlZddlZddlZddlZddl	Z	ddl
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j*                  e      Z e       Z e	j4                         Zdaded	<   d
dddZe G d d             Ze G d d             Z e G d d             Z! G d d      Z"dFdZ#dGdZ$dGdZ%dGdZ&dGdZ'dGdZ(dGdZ)dGdZ*dGdZ+dGdZ,dGd Z-dGd!Z.dGd"Z/dGd#Z0dGd$Z1dGd%Z2dHd&Z3dHd'Z4dId(Z5dJd)Z6dKd*Z7	 dL	 	 	 	 	 dMd+Z8dNd,Z9dOd-Z:dPd.Z;	 	 	 	 	 	 dQd/Z<dRd0Z=	 	 	 	 	 	 dSd1Z>dTd2Z?dUd3Z@dVd4ZAdWd5ZBdWd6ZCdXd7ZDdWd8ZEdWd9ZFdWd:ZGdYd;ZHdZd<ZId[d=ZJd\d]d>ZKd^d?ZLd_d@ZMd`dAZNdadBZOdFdCZPdZdDZQdFdEZRy)buC   nemo_relay — optional Hermes plugin for NeMo Relay observability.    )annotationsN)Callable)	dataclassfield)Path)AnyOptionalz'_Runtime | object | None'_RUNTIMEzanthropic.messageszopenai.chat_completionszopenai.responses)anthropic_messageschat_completionscodex_responsesc                      e Zd ZU ded<   dZded<   dZded<   dZded<   d	Zd
ed<   dZded<    e	e
      Zded<    e	e
      Zded<   y)_SessionStatestr
session_idNr   handleatif_exporter atif_subscriber_nameFboolis_embedded_subagentparent_session_iddefault_factorydict[str, Any]	llm_spans
tool_spans)__name__
__module____qualname____annotations__r   r   r   r   r   r   dictr   r        \/root/.hermes/venv/lib/python3.12/site-packages/plugins/observability/nemo_relay/__init__.pyr   r      sZ    OFCM3 "#"!&$&s %d ;I~;!&t!<J<r$   r   c                  ,    e Zd ZU ded<   ded<   ded<   y)_SubagentParentr   r   r   parent_handler   metadataN)r   r   r    r!   r#   r$   r%   r'   r'   *   s    r$   r'   c                      e Zd ZU dZded<   dZded<    ee      Zded	<   d
Z	ded<   dZ
ded<   d
Zded<   dZded<   dZded<   dZded<   d
Zded<   dZded<   dZded<   dZded<   dZded<   dZded<   dZded<   y) 	_Settingsr   r   plugins_toml_pathNdict[str, Any] | Noneplugins_configr   list[dict[str, Any]]dynamic_pluginsFr   adaptive_enabledobserve_onlyadaptive_modeatof_enabledatof_output_directoryhermes-atof.jsonlatof_filenameappend	atof_modeatif_enabledatif_output_directoryhermes-atif-{session_id}.jsonatif_filename_templateembeddedatif_subagent_export_modeHermes Agentatif_agent_nameunknownatif_agent_versionatif_model_name)r   r   r    r,   r!   r.   r   listr0   r1   r3   r4   r5   r7   r9   r:   r;   r=   r?   rA   rC   rD   r#   r$   r%   r+   r+   1   s    s,0N)0,1$,GO)G"d"'M3'L$!#3#,M3,IsL$!#3#"ACA%/s/)OS)''$OS$r$   r+   c                      e Zd ZddZddZddZddZddZddZddZ	ddZ
dd	Zdd
ZddZddZddZddZd dZddZddZddZddZ	 	 	 	 	 	 	 	 	 	 	 	 d!dZd"dZd"dZy)#_Runtimec                    || _         || _        i | _        i | _        d | _        d| _        d | _        d| _        | j                         | _	        d| _
        | j                  s| j                          y y )Nzhermes.nemo_relay.atofF)
nemo_relaysettingssessionssubagent_parentsatof_exporter_atof_subscriber_name_plugin_activation_shutdown_registered_configure_plugins_toml_plugin_config_initialized_plugin_config_needs_reinit_activate_direct_fallbacks)selfrI   rJ   s      r%   __init__z_Runtime.__init__F   sr    $ 24<>"&%="'+$)!*.*F*F*H'+0(..++- /r$   c                   | j                   j                  syt        | j                  dd       }|yt	        | j                   j                        }| j                   j
                  r{t        |dd       }t        |      rN	 | j                  |       t         ||| j                   j
                              | _	        | j                          yt        j                  d       t        |dd       }t        |      sy	 | j                  |       t         ||             y# t        $ r }t        j                  d|       Y d }~ad }~ww xY w# t        $ r"}t        j                  d|d	       Y d }~yd }~ww xY w)
NFpluginactivate_dynamic_pluginsTzZNeMo Relay dynamic plugin activation failed; continuing with static observability only: %szNeMo Relay dynamic plugins require a binding that exposes plugin.activate_dynamic_plugins (available in NeMo Relay 0.6+). Continuing with static observability only.
initializez'NeMo Relay plugins.toml init failed: %sexc_info)rJ   r.   getattrrI   _static_plugin_configr0   callable!_ensure_plugin_config_output_dirs_resolve_awaitablerO   _ensure_shutdown_registered	Exceptionloggerwarningdebug)rU   
plugin_modplugin_configactivate_dynamicexcrZ   s         r%   rQ   z _Runtime._configure_plugins_tomlT   sQ   }}++T__h=
-dmm.J.JK==((&z3MtT()::=I.@(8U8UV/D+ 446 A
 Zt<

#	22=Az-89% ! NN1 &  	LLBCRVLW	s1    AD <"E 	E(EE	E6E11E6c                j    | j                   ry t        j                  | j                         d| _         y )NT)rP   atexitregistershutdownrU   s    r%   rb   z$_Runtime._ensure_shutdown_registered|   s&    $$&$(!r$   c                6   | j                   sy g }| j                  | j                  }	 t        | j                         t        |dd       }t        |      rD	 t         |              d | _        d| _         t        | j                  j                        | _        n}|j                  d       nk	 t        | j                  dd       }t        |dd       }t        |      rt         |              d| _         t        | j                  j                        | _        |rt        d	j                  |            y # t        $ r}|j                  d|        Y d }~d }~ww xY w# t        $ r}|j                  d|        Y d }~d }~ww xY w# d | _        d| _         t        | j                  j                        | _        w xY w# t        $ r}|j                  d|        Y d }~d }~ww xY w# d| _         t        | j                  j                        | _        w xY w)
Nsubscriber flush failed: closez(dynamic plugin activation close failed: Fz-dynamic plugin activation has no close methodrX   clearz*static plugin configuration clear failed: ; )rR   rO   _flush_relay_subscribersrI   rc   r8   r]   r_   ra   r   rJ   r.   rS   RuntimeErrorjoin)rU   failures
activationrj   rr   rg   rs   s          r%   _clear_plugins_tomlz_Runtime._clear_plugins_toml   s   .. "".00JC(9 J6E
Z&uw/ /3D+6;D37;DMM<X<X7YD4 OP	V$T__hE

GT:E?&uw/ 38/378T8T3U0tyy233 =  C";C5 ABBC ! VOO&Nse$TUUV /3D+6;D37;DMM<X<X7YD4  T"LSE RSST 38/378T8T3U0sk   D4 E -?G 4	E=EE	F(F<F
 FF
 
4F>	G(
G#G+ #G((G+ +-Hc                2    d| _         | j                          y NF)rS   _configure_atofro   s    r%   rT   z#_Runtime._activate_direct_fallbacks   s    +0(r$   c                    | j                   r| j                  ry | j                         | _        | j                  s| j                          y | j	                          d| _         y r|   )rS   rR   rQ   rT   _clear_atofro   s    r%    _maybe_reinitialize_plugins_tomlz)_Runtime._maybe_reinitialize_plugins_toml   sS    //43R3R*.*F*F*H'..++-+0(r$   c                ^    | j                   xr  t        | j                  j                  |      S N)rR   _observability_exporter_enabledrJ   r.   )rU   exporter_names     r%   _plugins_toml_owns_exporterz$_Runtime._plugins_toml_owns_exporter   s-    .. 
3RMM((4
 	
r$   c                   |j                  dg       D ]  }t        |t              s|j                  d      dk7  r)|j                  d      du r=|j                  d      }t        |t              s_dD ]s  }|j                  |      }t        |t              s%|j                  d      }t        |t              sG|j	                         sXt        |      j                  d	d	
       u  y )N
componentskindobservabilityenabledFconfig)atofatifoutput_directoryTparentsexist_ok)get
isinstancer"   r   stripr   mkdir)rU   r   	componentcomponent_configr   exporter_configr   s          r%   r`   z*_Runtime._ensure_plugin_config_output_dirs   s    L"5 	NIi.}}V$7}}Y'50(}}X6.5!1 N"2"6"6}"E!/48#2#6#67I#J .49I9O9O9Q)*000MN	Nr$   c                   | j                   j                  r| j                  y | j                  j	                         }| j                   j
                  rKt        | j                   j
                        j                  dd       | j                   j
                  |_        | j                   j                  |_
        | j                   j                  j                         dk(  r&| j                  j                  j                  |_        n%| j                  j                  j                   |_        | j                  j#                  |      | _        | j                  j%                  | j&                         y )NTr   	overwrite)rJ   r4   rM   rI   AtofExporterConfigr5   r   r   r   r7   filenamer9   lowerAtofExporterMode	OverwritemodeAppendAtofExporterrm   rN   )rU   r   s     r%   r}   z_Runtime._configure_atof   s    }}))T-?-?-K335==..445;;DSW;X&*mm&I&IF#--55==""((*k9//::DDFK//::AAFK!__99&A##D$>$>?r$   c                    | j                   y t        | j                   dd       }t        |      r	  || j                         d | _         y d | _         y # t        $ r! t
        j                  dd       Y d | _         y w xY w)N
deregisterz!NeMo Relay ATOF deregister failedTr[   )rM   r]   r_   rN   rc   rd   rf   )rU   r   s     r%   r   z_Runtime._clear_atof   s~    %T//tD
JQ4556 "T  Q@4P!Qs   A  A=<A=c           
        | j                          t        |      }| j                  j                  |      }||S t	        |      }| j
                  j                  r| j                  d      s| j                  j                  || j
                  j                  | j
                  j                  t        |j                  d      xs | j
                  j                        ddd      |_        d| |_        |j                  j!                  |j                         | j"                  j                  |      }t%        |      }d }|4|j&                  }i ||j(                  }d	|_        |j,                  |_        | j                  j.                  j1                  d
| | j                  j2                  j4                  |d|i|      |_        || j                  |<   |S )Nr   r   modelzhermes-agentzobservability/nemo_relay)sourcerX   )
model_nameextrazhermes.nemo_relay.atif.Tzhermes-session-r   r   datar)   )r   _session_idrK   r   r   rJ   r:   r   rI   AtifExporterrA   rC   r   rD   r   r   rm   rL   	_metadatar(   r)   r   r   scopepush	ScopeTypeAgentr   )rU   kwargsr   statesubagent_parentr)   r(   s          r%   ensure_sessionz_Runtime.ensure_session   s   --/ (
!!*-L4==%%d.N.Nv.V"&//">">--00vzz'2Sdmm6S6ST!/;UV #? #E ,C:,)OE&(()C)CD//33J?V$&+99M?(?o&>&>?H)-E&&5&G&GE#,,11j\*OO%%++ 
+ 2 
 %*j!r$   c                   | j                   j                  r|j                  y |j                  r| j                   j                  dk7  ry | j                   j
                  }|sy t        |      j                  dd       | j                   j                  j                  |j                        }t        ||      j                  |j                  j                         d       y )NallTr   r   utf-8encoding)rJ   r:   r   r   r?   r;   r   r   r=   formatr   
write_textexport_json)rU   r   
output_dirr   s       r%   export_atifz_Runtime.export_atif  s    }}))U-@-@-H%%$--*Q*QUZ*Z]]88
Ztd;==77>>%JZJZ>[Z"--e.A.A.M.M.OZa-br$   c                   t        |      }| j                  j                  |d        | j                  j                  |d       }|y g }|j                  ;	 | j
                  j                  j                  |j                  t        |             	 t        | j
                         	 | j                  |       |j                  2|j                  r&	 |j                  j                  |j                         | j                  r*| j                   | j                  s	 | j#                          n5| j$                  j&                  r| j                   | j                  sd| _        |r't*        j-                  d|d	j/                  |             y y # t        $ r}|j                  d|        Y d }~d }~ww xY w# t        $ r}|j                  d|        Y d }~3d }~ww xY w# t        $ r}|j                  d|        Y d }~Ld }~ww xY w# t        $ r}|j                  d|        Y d }~9d }~ww xY w# t        $ r}|j                  d|        Y d }~d }~ww xY w)
N)outputzsession scope pop failed: rq   zATIF export failed: zATIF deregister failed: z#plugin configuration clear failed: Tz8NeMo Relay session %s teardown completed with errors: %srt   )r   rL   poprK   r   rI   r   	_jsonablerc   r8   ru   r   r   r   r   rR   rO   rz   rJ   r.   rS   rd   re   rw   )rU   r   r   r   rx   rj   s         r%   close_sessionz_Runtime.close_session  s$    (
!!*d3!!*d3= <<#D%%))%,,y?P)Q	?$T__5	:U# *u/I/IB##..u/I/IJ ++''/MMM((* MM((''/MM/3D,NNJ		(# =  D"<SE BCCD  	?OO7u=>>	?  	:OO23%899	:
  B":3% @AAB  M"EcU KLLMsx   :F F6 'G! %H H7 	F3F..F36	G?GG!	H	*HH		H4H//H47	I IIc                :   g }t        | j                        D ]  }	 | j                  |dd        | j
                  r	 | j                          | j                          | j                  r2| j                  &t        j                  | j                         d| _        |r&t        j                  dd	j                  |             yy# t        $ r!}|j	                  d| d|        Y d}~d}~ww xY w# t        $ r}|j	                  d|        Y d}~d}~ww xY w)
zAClose active sessions and the process-lifetime plugin activation.runtime_shutdown)r   reasonzsession z close failed: Nzplugin runtime close failed: Fz5NeMo Relay runtime shutdown completed with errors: %srt   )rE   rK   r   rc   r8   rR   rz   r   rP   rO   rl   
unregisterrn   rd   re   rw   )rU   rx   r   rj   s       r%   rn   z_Runtime.shutdownM  s    t}}- 	MJM""*HZ#[\	M
 **G((* 	$$)@)@)Hdmm,(-D%NNG		(#   M(:,ocU KLLM
  G"?u EFFGs.   CC3 	C0C++C03	D<DDc                    | j                  |      }| j                  j                  j                  ||j                  t        |      t        |             y )Nr   )r   rI   r   eventr   r   r   )rU   namer   r   s       r%   markz_Runtime.markd  sI    ##F+##<<6"v&	 	$ 	
r$   c                @   | j                  |      }t        |      }t        |      }|r9t        |j                  |j
                  t        ||            | j                  |<   | j                  j                  j                  d|j
                  t        |      |       y )N)r   r(   r)   zhermes.subagent.startr   )r   r   _child_session_idr'   r   r   _subagent_child_metadatarL   rI   r   r   r   )rU   r   parent_stater)   child_session_ids        r%   mark_subagent_startz_Runtime.mark_subagent_startm  s    **62V$,V46E"."9"9*111&(C7D!!"23
 	###&&6"	 	$ 	
r$   c                z    t        |      }|r| j                  j                  |d        | j                  d|       y )Nzhermes.subagent.stop)r   rL   r   r   )rU   r   r   s      r%   mark_subagent_stopz_Runtime.mark_subagent_stop~  s6    ,V4!!%%&6=		(&1r$   c           	         | j                   j                  xs | j                  d uxrM t        t	        t	        | j
                  dd       dd             xr  t        t	        | j
                  dd             S )Nllmexecute
LLMRequestrJ   r1   rO   r_   r]   rI   ro   s    r%   managed_llm_enabledz_Runtime.managed_llm_enabled  sd    ]]++Rt/F/Fd/R G%!F	SWXYG,EF	
r$   c           	         | j                   j                  xs | j                  d uxr+ t        t	        t	        | j
                  dd       dd             S )Ntoolsr   r   ro   s    r%   managed_tool_enabledz_Runtime.managed_tool_enabled  sI    ]]++Rt/F/Fd/R \'4!H)UYZ[	
r$   c                  	
 dd d dd 	d 
d	
fd}	 t         ||            }|rd   rt        |d         rd   S |S # t        $ r}
t        |	      r
 d }~ww xY w)NF)setvalue
normalizedc                    	   |             }dd<   |d<    |      d<   d   S # t         $ r}|t        |       d }~ww xY w)NTr   r   r   )rc   _original_downstream_error)	next_payloadrawrj   callback_errordownstream_error	next_callnormalize_payloadraw_responseshape_responses	      r%   _implzA_Runtime._run_managed_with_downstream_preservation.<locals>._impl  sm     1, ?@
 #'L$'L!)7)<L&--  !$#=c#B s   + 	AAAr   r   r   )r   r   returnr   )ra   rc    _is_relay_wrapped_callback_error_json_semantically_equal)rU   r   r   r   make_managed_executepreserve_raw_responser   managed_resultrj   r   r   r   s    ```     @@@r%   )_run_managed_with_downstream_preservationz2_Runtime._run_managed_with_downstream_preservation  s     05tSW'X+/15	. 	.	/0DU0KLN "U#(l9ST((  	+0PQTVd0e&&	s   A 	A*A%%A*c                0     j                        t        j                  d      xs i        j                  j	                  i       j                  d      }t        |      sS dfd}d fd} j                  ||t        |d      S )	Nrequestr   c                F    t        | d|       }t        |t              r|S S )Ncontent)r]   r   r"   )next_request	next_bodyrequest_bodys     r%   
_normalizez(_Runtime.execute_llm.<locals>._normalize  s%    iFI *9d ;9MMr$   c                (     d fd} |       S )Nc                   K   j                   j                  j                  t              j                  t        j                  d      j                  d      j                  d      j                  j                  d      t              t        j                  d      xs d            } t        j                  |       r
|  d {   S | S 7 w)Nturn_idapi_request_idapi_call_count)r   r  r  r   r   r   r   r   r)   r   )rI   r   r   _relay_llm_surfacer   r   r   rJ   r3   r   r   inspectisawaitable)resultimplr   r   rU   r   s    r%   _managed_executezE_Runtime.execute_llm.<locals>._make_managed.<locals>._managed_execute  s     ,,44&v. <<"'-zz)'<.4jj9I.J.4jj9I.J$(MM$?$?	 'v."6::g#6#<"= 5   &&v.!'<' (s   CCCCr   r   r#   )r  r	  r   r   rU   r   s   ` r%   _make_managedz+_Runtime.execute_llm.<locals>._make_managed  s     * $%%r$   Tr   )r   r   r   r   r  Callable[[Any], Any]r   r   )r   r   r   rI   r   r_   r   _llm_response_payload)rU   r   r   r   r  r   r   r   s   ``   @@@r%   execute_llmz_Runtime.execute_llm  s    ##F+ I!6!<"=//,,R>JJ{+		"	N	& 	&0 ==z#8-_c > 
 	
r$   c                6     j                        t        j                  d      xs d      t        j                  d      xs i       j                  d      }t	        |      sS d	fd}d
 fd} j                  ||t        |d      S )N	tool_nametoolargsr   c                ,    t        | t              r| S S r   )r   r"   )	next_argsr  s    r%   r   z)_Runtime.execute_tool.<locals>._normalize  s     *9d ;9EEr$   c                *     d fd} |       S )Nc                 l  K   j                   j                  j                  j                  t	        j                  d      j                  d      j                  d      j                  j                  d      t                    } t        j                  |       r
|  d {   S | S 7 w)Nr   r  tool_call_id)r   r  r  r   r   )rI   r   r   r   r   r   rJ   r3   r   r  r  )r  r  r  r   rU   r   r  s    r%   r	  zF_Runtime.execute_tool.<locals>._make_managed.<locals>._managed_execute  s     ..66 <<"'-zz)'<.4jj9I.J,2JJ~,F$(MM$?$?	 'v. 7  &&v.!'<' (s   B(B4+B2,B4r
  r#   )r  r	  r  r   rU   r   r  s   ` r%   r  z,_Runtime.execute_tool.<locals>._make_managed  s     ( $%%r$   Fr  )r  r   r   r   r  )r   r   r   r   r_   r   )rU   r   r   r   r  r  r   r  s   ``   @@@r%   execute_toolz_Runtime.execute_tool  s    ##F+

;/96:	F+1r2JJ{+		"K	F	& 	&. ==z9mSX > 
 	
r$   N)rI   r   rJ   r+   r   None)r   r   r   r  )r   r   r   r   )r   r   r   r  )r   r   r   r   )r   r   r   r  )r   r   r   r  )r   r   r   r   r   r  )r   r  r   r  r   r  r   z%Callable[[Callable[[Any], Any]], Any]r   r   r   r   )r   r   r   r   )r   r   r    rV   rQ   rb   rz   rT   r   r   r`   r}   r   r   r   r   rn   r   r   r   r   r   r   r  r  r#   r$   r%   rG   rG   E   s    .&P)'4R1
N&@	"$L
c-^.

"2

,', 0, -	,
 D,  $, 
,\&
P$
r$   rG   c                ,   t               j                  r
t                | j                  dt               | j                  dt
               | j                  dt               | j                  dt               | j                  dt               | j                  dt               | j                  dt               | j                  dt               | j                  d	t               | j                  d
t               | j                  dt               | j                  dt               | j                  dt                | j                  dt"               | j                  dt$               | j'                  dt(               | j'                  dt*               y )Non_session_starton_session_endon_session_finalizeon_session_resetpre_llm_callpost_llm_callpre_api_requestpost_api_requestapi_request_errorpre_tool_callpost_tool_callpre_approval_requestpost_approval_responsesubagent_startsubagent_stopllm_executiontool_execution)_load_settingsr0   _get_runtimeregister_hookr  r  r   r!  on_pre_llm_callon_post_llm_callon_pre_api_requeston_post_api_requeston_api_request_erroron_pre_tool_callon_post_tool_callon_pre_approval_requeston_post_approval_responseon_subagent_starton_subagent_stopregister_middlewareon_llm_execution_middlewareon_tool_execution_middleware)ctxs    r%   rm   rm     s@    ''(*:;&7+-@A(*:;no6o'78');<(*=>)+?@o'78&(9:,.EF.0IJ&(9:o'78O-HI,.JKr$   c                 @     t               t         fd       y y )Nc                 &    j                         S r   )r   r   runtimes   r%   <lambda>z"on_session_start.<locals>.<lambda>)  s    g,,V4 r$   r0  _saferC  s   `@r%   r  r  &  s    nG45 r$   c                 @     t               t         fd       y y )Nc                 h    j                  d       j                  j                               fS )Nzhermes.session.end)r   r   r   rC  s   r%   rE  z on_session_end.<locals>.<lambda>/  s0    w||$8&A7CVCVW^WmWmntWuCvw r$   rF  rC  s   `@r%   r  r  ,  s    nGwx r$   c                 @     t               t         fd       y y )Nc                 &    j                         S r   r   rC  s   r%   rE  z%on_session_finalize.<locals>.<lambda>5      g++F3 r$   rF  rC  s   `@r%   r   r   2      nG34 r$   c                 @     t               t         fd       y y )Nc                 &    j                         S r   rL  rC  s   r%   rE  z"on_session_reset.<locals>.<lambda>;  rM  r$   rF  rC  s   `@r%   r!  r!  8  rN  r$   c                 @     t               t         fd       y y )Nc                 (    j                  d       S )Nzhermes.turn.startr   rC  s   r%   rE  z!on_pre_llm_call.<locals>.<lambda>A  s    gll#6? r$   rF  rC  s   `@r%   r2  r2  >  s    nG?@ r$   c                 @     t               t         fd       y y )Nc                 (    j                  d       S )Nzhermes.turn.endrS  rC  s   r%   rE  z"on_post_llm_call.<locals>.<lambda>G  s    gll#4f= r$   rF  rC  s   `@r%   r3  r3  D  s    nG=> r$   c                 h     t               y j                         ry d fd}t        |       y )Nc                 J   j                        } j                  d      }t        |t              r|j                  d      ni }j                  j                  i t        |            }j                  j                  j                  t        j                  d      xs d      || j                  t        j                  d      j                  d      d      t              t        j                  d      xs d	      
      }|| j                  t              <   y )Nr   bodyproviderr   r   r  r   r  r   r   r  )r   r   r   r"   rI   r   r   r   callr   r   r   r   _api_key)r   request_payloadr   r   spanr   rD  s        r%   _recordz#on_pre_api_request.<locals>._recordQ  s    &&v. **Y/6@RV6W**62]_$$//Il4KL!!%%**

:&/%0<<vzz)'<PVPZPZ[kPlmnv&6::g.4"5 + 
 -1()r$   r  r0  r   rG  r   r_  rD  s   ` @r%   r4  r4  J  s/    nG""$1 
'Nr$   c                 h     t               y j                         ry d fd}t        |       y )Nc            
        j                        } | j                  j                  t              d       }|j	                  d       y j
                  j                  j                  |t        j                  d      xs i       t        j                  d      j                  d      d      t                     y )Nzhermes.api.response.unmatchedresponseusagefinish_reason)re  rf  r   r)   r   r   r   r\  r   rI   r   call_endr   r   r   r   r^  r   rD  s     r%   r_  z$on_post_api_request.<locals>._recordj  s    &&v.""8F#3T:<LL8&A''fjj,23VZZ%86::VeKfghv&	 	( 	
r$   r  r`  ra  s   ` @r%   r5  r5  c  /    nG""$
 
'Nr$   c                 h     t               y j                         ry d fd}t        |       y )Nc            	     N   j                        } | j                  j                  t              d       }|j	                  d       y j
                  j                  j                  |dt        j                  d      xs i       it              t                     y )Nzhermes.api.errorerrorrg  rh  rj  s     r%   r_  z%on_api_request_error.<locals>._record  s    &&v.""8F#3T:<LL+V4''i

7 3 9r:;6"v&	 	( 	
r$   r  r`  ra  s   ` @r%   r6  r6  z  rk  r$   c                 h     t               y j                         ry d fd}t        |       y )Nc                    j                        } j                  j                  j                  t	        j                  d      xs d      t        j                  d      xs i       | j                  t        j                  d      j                  d      d      t              t	        j                  d      xs d      	      }|| j                  t              <   y )
Nr  r  r  r   r  rZ  r  r   )r   r   r)   r  )r   rI   r   r[  r   r   r   r   r   r   	_tool_keyrj  s     r%   r_  z!on_pre_tool_call.<locals>._record  s    &&v.!!'',,

;'162fjj(.B/<<vzz)'<PVPZPZ[kPlmnv&VZZ7=2> - 
 /36*+r$   r  r0  r   rG  ra  s   ` @r%   r7  r7    s/    nG##%
3 
'Nr$   c                 h     t               y j                         ry d fd}t        |       y )Nc            
        j                        } | j                  j                  t              d       }|j	                  d       y j
                  j                  j                  |t        j                  d            t        j                  d      j                  d      d      t                     y )Nzhermes.tool.response.unmatchedr  statusduration_ms)ru  rv  rg  )r   r   r   rq  r   rI   r   ri  r   r   r   rj  s     r%   r_  z"on_post_tool_call.<locals>._record  s    &&v.##If$5t<<LL96B  ))fjj*+fjj&:6::VcKdefv&	 	* 	
r$   r  rr  ra  s   ` @r%   r8  r8    s/    nG##%
 
'Nr$   c                 @     t               t         fd       y y )Nc                 (    j                  d       S )Nzhermes.approval.requestrS  rC  s   r%   rE  z)on_pre_approval_request.<locals>.<lambda>  s    gll#<fE r$   rF  rC  s   `@r%   r9  r9    s    nGEF r$   c                 @     t               t         fd       y y )Nc                 (    j                  d       S )Nzhermes.approval.responserS  rC  s   r%   rE  z+on_post_approval_response.<locals>.<lambda>  s    gll#=vF r$   rF  rC  s   `@r%   r:  r:    s    nGFG r$   c                 @     t               t         fd       y y )Nc                 &    j                         S r   )r   rC  s   r%   rE  z#on_subagent_start.<locals>.<lambda>  s    g11&9 r$   rF  rC  s   `@r%   r;  r;    s    nG9: r$   c                 @     t               t         fd       y y )Nc                 &    j                         S r   )r   rC  s   r%   rE  z"on_subagent_stop.<locals>.<lambda>  s    g008 r$   rF  rC  s   `@r%   r<  r<    s    nG89 r$   c                     t               }| j                  d      }| j                  d      xs i }|!|j                         r|j                  |       S t	        |      r ||      S |S )Nr   r   )r0  r   r   r  r_   )r   rD  r   r   s       r%   r>  r>    sd    nG

;'Ijj#)rGw::<""6**	!!Nr$   c                     t               }| j                  d      }| j                  d      xs i }|!|j                         r|j                  |       S t	        |      r ||      S |S )Nr   r  )r0  r   r   r  r_   )r   rD  r   r  s       r%   r?  r?    sc    nG

;'I::f#Dw;;=##F++	Kr$   c                    t         5  t        t        u r
	 d d d        y t        t        t              rt        cd d d        S 	 dd l} 	 t	        | t                     at        cd d d        S # t        $ r.}t        j                  d|       t        aY d }~d d d        y d }~ww xY w# t        $ r0}t        j                  d|d       t        aY d }~d d d        y d }~ww xY w# 1 sw Y   y xY w)Nr   z-NeMo Relay plugin disabled: import failed: %s)rI   rJ   z+NeMo Relay plugin disabled: init failed: %sTr[   )
_LOCKr
   _INIT_FAILEDr   rG   rI   rc   rd   rf   r/  )nemo_runtimerj   s     r%   r0  r0    s    	 |#  h)	 
	-
	<.BRSH
 #   	LLH#N#H 	  	LLFVZL[#H! 	 s^   C!C!A+B%C!+	B"4BC!B""C!%	C.CC!CC!!C*c            	        t        d      } t        |       }t        |d      }t        d"i d| d|dt	        ||       d|d udt        |      dt        d	      d
t        d      dt        d      xs ddt        d      xs ddt        d      dt        d      dt        d      xs ddt               dt        d      xs ddt        d      xs dd t        d!      xs dS )#NHERMES_NEMO_RELAY_PLUGINS_TOMLadaptiver,   r.   r0   r1   r3   r4   HERMES_NEMO_RELAY_ATOF_ENABLEDr5   'HERMES_NEMO_RELAY_ATOF_OUTPUT_DIRECTORYr7   HERMES_NEMO_RELAY_ATOF_FILENAMEr6   r9   HERMES_NEMO_RELAY_ATOF_MODEr8   r:   HERMES_NEMO_RELAY_ATIF_ENABLEDr;   'HERMES_NEMO_RELAY_ATIF_OUTPUT_DIRECTORYr=   (HERMES_NEMO_RELAY_ATIF_FILENAME_TEMPLATEr<   r?   rA   !HERMES_NEMO_RELAY_ATIF_AGENT_NAMEr@   rC   $HERMES_NEMO_RELAY_ATIF_AGENT_VERSIONrB   rD   !HERMES_NEMO_RELAY_ATIF_MODEL_NAMEr#   )_env_load_plugins_config_enabled_component_configr+   _dynamic_plugin_specs_adaptive_mode	_env_bool_atif_subagent_export_mode)r,   r.   adaptive_configs      r%   r/  r/    sE   =>)*;<N/
KO +% .n>OP )4	
 %_5 ?@ ##LM <=TAT 45A ?@ ##LM  $$NOrSr #="> @AS^   FGT9  @ANY! r$   c                ^    | j                         D ci c]  \  }}|dvr|| c}}S c c}}w )zEReturn Relay's base config without embedding- or gateway-host fields.>   pluginsr0   )items)r.   keyr   s      r%   r^   r^     s>     )..0C44 	U
  s   )c                P   t        | t              sg S | j                  d      }| j                  d      }|@t        |t              st        j	                  d       g S |rt        j	                  d       g S |g S t        |t
              st        j                  d       g S g }d}t        |      D ]  \  }}t        |t              st        j                  d|       d}0|j                  d	      }|j                  d
      }	|j                  d      }
|j                  di       }|j                  d      }t        |t              r|j                         st        j                  d|       d}|	dvrt        j                  d|       d}t        |
t              r|
j                         st        j                  d|       d}t        |t              st        j                  d|       d}@|:t        |t              r|j                         st        j                  d|       d}||j                         |	t        |
j                         |      |d}|t        |j                         |      |d<   |j                  |        |rt        j	                  d       g S |S )Nr0   r  zInvalid NeMo Relay plugins config: expected [plugins] to be an object; no dynamic plugins will be activated. Continuing with static observability only.aM  Hermes cannot activate Relay gateway [[plugins.dynamic]] records because the Python binding does not expose the CLI lifecycle resolver for enablement, trust policy, and worker environments. Use Hermes-owned [[dynamic_plugins]] activation specs instead; no dynamic plugins will be activated. Continuing with static observability only.zUIgnoring invalid NeMo Relay dynamic_plugins config: expected an array of plugin specsFz:Invalid NeMo Relay dynamic_plugins[%d]: expected an objectT	plugin_idr   manifest_refr   environment_refz=Invalid NeMo Relay dynamic_plugins[%d]: plugin_id is required>   workerrust_dynamiczKInvalid NeMo Relay dynamic_plugins[%d]: kind must be rust_dynamic or workerz@Invalid NeMo Relay dynamic_plugins[%d]: manifest_ref is requiredz@Invalid NeMo Relay dynamic_plugins[%d]: config must be an objectzRInvalid NeMo Relay dynamic_plugins[%d]: environment_ref must be a non-empty string)r  r   r  r   zNeMo Relay dynamic plugin configuration is invalid; no dynamic plugins will be activated. Continuing with static observability only.)r   r"   r   rd   rn  rE   re   	enumerater   r   _config_relative_pathr8   )r.   r,   	raw_specsplugins_sectionspecsinvalidindexraw_specr  r   r  r   r  specs                 r%   r  r  #  s    nd+	""#45I$((3O"/40LL&
 ILLH I	i&c	
 	"$EG$Y/ 9x(D)NNLe GLL-	||F#||N3h+",,'89)S)1BNNOQV G11NN] G,,L4F4F4HNNRTY G&$'NNRTY G&?C08M8M8ONN#
 G"*1,2D2D2FHYZ	 
 &&;%%'):'D"# 	Ts9t L	
 	Lr$   c                <   t        |       }|j                         rt        |      S |rt        |      nt        j                         dz  }|j                         st        j                         |z  }t        j
                  j                  |j                  |z        S )zEResolve a plugin path relative to its physical ``plugins.toml`` file.zplugins.toml)r   is_absoluter   cwdospathabspathparent)r   r,   r  config_paths       r%   r  r    sv    ;D4y->$()DHHJQ_D_K""$hhj;.77??;--455r$   c                ^    t        | dd       }t        |dd       }t        |      r |        y y )Nsubscribersflush)r]   r_   )rI   r  r  s      r%   ru   ru     s/    *mT:KK$/E r$   c                    | sy 	 t        j                  t        |       j                  d            S # t        $ r"}t
        j                  d|d       Y d }~y d }~ww xY w)Nr   r   z'NeMo Relay plugins.toml load failed: %sTr[   )tomllibloadsr   	read_textrc   rd   rf   )r  rj   s     r%   r  r    sT    }}T$Z1171CDD >dSs   -3 	AAAc                8   t        | t              sy | j                  d      }t        |t              sy |D ]b  }t        |t              s|j                  d      |k7  s|j                  dd      s;|j                  d      }t        |t              r|c S i c S  y )Nr   r   r   Tr   )r   r"   r   rE   )r.   r   r   r   r   s        r%   r  r    s     nd+##L1Jj$' :	)T*== D(	i0Nx(#FD1v9r9: r$   c                l   t        | t              sy| j                  d      }t        |t              rA|j                  d      }t        |t              r |j	                         r|j	                         S | j                  d      }t        |t              r |j	                         r|j	                         S y)Nr2   tool_parallelismr   )r   r"   r   r   r   )r   r  r   s      r%   r  r    s    fd#zz"45"D)##F+dC TZZ\::<::fD$zz|r$   c                    t        | d      }t        |t              sy|j                  |      }t        |t              sy|j                  dd      duS )Nr   Fr   T)r  r   r"   r   )r.   r   observability_configr   s       r%   r   r     sT     5^_U*D1*..}=Oot,y$/u<<r$   c                ^    t         j                  j                  | d      j                         S )Nr   )r  environr   r   r   s    r%   r  r    s     ::>>$#))++r$   c                 F    t        d      j                         } | dk(  rdS dS )N+HERMES_NEMO_RELAY_ATIF_SUBAGENT_EXPORT_MODEr   r>   r  r   r   s    r%   r  r    s'    =>DDFDEM51z1r$   c                8    t        |       j                         dv S )N>   1onyestruer  r  s    r%   r  r    s    :!;;;r$   c                d    t        | j                  d      xs | j                  d      xs d      S )Nr   r   defaultr   r   r   s    r%   r   r     s*    vzz,'W6::6I+JWiXXr$   c                >    t        | j                  d      xs d      S )Nr   r   r  r  s    r%   r   r     s    vzz,-344r$   c                n    t        |       }||dd}dD ]  \  }}|j                  |      }||||<     |S )Nsubagent)r   trajectory_idnemo_relay_scope_role)	)subagent_idchild_subagent_id)r   r   )r  r  )
child_roler  )r   r   )parent_turn_idr  )parent_subagent_idr  )parent_trajectory_idr  )telemetry_schema_versionr  )r   r   )r   parent_metadatar   r)   targetr   r   s          r%   r   r     s[    (0&)!+H

 %  ##F+$HV% Or$   c                    t        | j                  d      xs# t        |        d| j                  d      xs d       S )Nr  :r  apir   r   r   r  s    r%   r\  r\    s>    vzz*+o+f2E1Fa

ScHdHmhmGn/oppr$   c           	         t        | j                  d      xs9 t        |        d| j                  d      xs d d| j                  d      xs d       S )Nr  r  r   r   r  r  r  r  s    r%   rq  rq    sZ    

>" 	f&!"!FJJy$9$?R#@&**[BYBc]cAde r$   c                    t        | j                  d      xs d      j                         j                         }t        j                  |t        | j                  d      xs d            S )Napi_moder   rY  r   )r   r   r   r   _RELAY_LLM_SURFACE_BY_API_MODE)r   r  s     r%   r  r    sX    6::j)/R0668>>@H)--FJJz"+e, r$   c                    d}|D ci c]  }|| v r| |   |t        | |          }}d|v r|j                  d|d          d|v r|j                  d|d          d|v r|j                  d|d          |S c c}w )N)r  r   platformtask_idr   r  r  r   r  r  r   r  r  child_statusrY  r   r  ru  r   r   r  r   r  r   child_trajectory_id)r   
setdefault)r   keysr  r)   s       r%   r   r     s    D. &=VC[4 	Yvc{##H 
 xOXl-CDh&2H=P4QRX%18<N3OPOs   A5c                   |  t        | t        t        t        t        f      r| S t        | t
              r6| j                         D ci c]  \  }}t        |      t        |       c}}S t        | t        t        t        f      r| D cg c]  }t        |       c}S 	 t        | d      rt        | j                  d            S 	 	 t        | d      rt        t        |             S 	 	 t        j                   t        j"                  | t                    S c c}}w c c}w # t        $ r Y gw xY w# t        $ r Y Tw xY w# t        $ r t        |       cY S w xY w)N
model_dumpjsonr  __dict__)r  )r   r   intfloatr   r"   r  r   rE   tupler   hasattrr  rc   varsr  r  dumps)r   kvs      r%   r   r   8  s/   }
53UD*AB%16?AA	!$??%$s+,&+,	!,,5,'U--6-:;; (5*%T%[)) &zz$**UC899 @,  
    5zsB   D
D &D$ 	D3 +-E $	D0/D03	D?>D?EEc                    	 dddd}t        j                  t        |       fi |t        j                  t        |      fi |k(  S # t        t        f$ r Y yw xY w)zGCompare JSON-compatible values without conflating booleans and numbers.FT),r  )ensure_ascii	sort_keys
separators)r  r  r   	TypeError
ValueError)leftrightoptionss      r%   r   r   O  s[    #(t:Vzz)D/5W5IeDT9`X_9```z" s   AA AAc                `    t        | t              r| j                  ||      S t        | ||      S r   )r   r"   r   r]   )objr  r  s      r%   _valuer  X  s,    #twwsG$$3W%%r$   c                v    t        | dd       }| j                  j                  dk(  rt        |t              r|S | S )Noriginal_DownstreamExecutionError)r]   	__class__r   r   BaseException)rj   r  s     r%   r   r   ^  s9     sJ-H
}}!<<HVcAdJr$   c                    |t        | t              syd|j                  j                   d| }t	        |       j                  |      S )NFzinternal error: z: )r   rv   r
  r   r   
startswith)rj   r   expecteds      r%   r   r   g  sK     Z\%B!.":":"C"C!DB~FVWHs8x((r$   c                *   t        |       }t        |t              rd|v r|S t        | d      }|!t        |t              r|j	                  d      }t        |t
              r|r|d   nd}t        |d      }t        |d      }ddd	}|it        |d
d      xs d|d
<   t        |d      }|t        |      |d<   t        t        |d            }|r||d<   t        |d      }	|	Jt        |	      |d<   n;t        |t              r+|j	                  d      xs |j	                  d      xs d|d<   t        | dt        |t              r|j	                  d      nd      ||t        t        | dt        |t              r|j	                  d      nd            dS )zCReturn the LLM response shape NeMo Relay's ATIF conversion expects.assistant_messagechoicesNr   messagerf  	assistantr   )roler   r  r   
tool_callsreasoning_contentoutput_textr   re  )r   r  rf  re  )r   r   r"   r  r   rE   _tool_calls_payload)
rd  payloadr  first_choicer  rf  r  r   r  	reasonings
             r%   r  r  v  s   !G'4 %8G%CXy)G:gt4++i(!+GT!:w71:DL\9-G<9M1<(L$*7FK$H$WK&!),+4W+=i(()FG
.8l+7$78	 5>y5I12	GT	"'.{{9'='a]A['a_a)$ ':gW[C\7;;w+?bfg.&6(GZX_aeMfW[[5Ilpqr	 r$   c           
         t        | t              sg S g }| D ]S  }t        |d      }|j                  t        |d      t        |dd      xs dt        |d      t        |d      dd       U |S )Nfunctionidtyper   	arguments)r   r   )r  r  r  )r   rE   r  r8   )r  r   r[  r  s       r%   r  r    s    j$'	')J 
$
+T4(tVZ8FJ"8V4!'+!>		

 r$   c                p    	  |         y # t         $ r"}t        j                  d|d       Y d }~y d }~ww xY w)Nz#NeMo Relay hook handling failed: %sTr[   )rc   rd   rf   )fnrj   s     r%   rG  rG    s5    P
 P:C$OOPs   
 	505c                d    t        j                         s S 	 t        j                          i i d fd}t        j                  |dd      }|j                          |j                          dv rd   j                  d      S # t        $ r t        j
                         cY S w xY w)Nc                 n    	 t        j                        d<   y # t        $ r} | d<   Y d } ~ y d } ~ ww xY w)Nr   rj   )asynciorunr  )rj   rn  r  r   s    r%   _runnerz#_resolve_awaitable.<locals>._runner  s3    	%kk%0F7O 	E%L	s    	4/4zhermes-nemo-relay-awaitableT)r  r   daemonrj   r   r  )r  r  r%  get_running_looprv   r&  	threadingThreadstartrw   r   )r   r'  threadrn  r  s   `  @@r%   ra   ra     s    u%"  "  F&(E *F
 LLN
KKM~El::g+  "{{5!!"s   B B/.B/c                     t         5  t        t        t              rt        j	                          d ad d d        y # 1 sw Y   y xY wr   )r  r   r
   rG   rn   r#   r$   r%   reset_for_testsr/    s5    	 h)  s	   +;Ar  )r   r   r   r  )r   r   r   r   )r   zOptional[_Runtime])r   r+   )r.   r   r   r   )r   )r.   r-   r,   r   r   r/   )r   r   r,   r   r   r   )rI   r   r   r  )r  r   r   r-   )r.   r-   r   r   r   r-   )r   r-   r   r   )r.   r-   r   r   r   r   )r   r   r   r   )r   r   )r   r   r   r   )r   r   r   r   )r   r   r  r   r   r   )r   r   r   r   )r   r   r   r   )r  r   r  r   r   r   r   )r  r   r  r   r  r   r   r   )rj   rc   r   r  )rj   rc   r   zException | Noner   r   )rd  r   r   r   )r  r   r   r/   )S__doc__
__future__r   rl   r%  r  r  loggingr  r*  r  collections.abcr   dataclassesr   r   pathlibr   typingr   r	   	getLoggerr   rd   objectr  RLockr  r
   r!   r  r   r'   r+   rG   rm   r  r  r   r!  r2  r3  r4  r5  r6  r7  r8  r9  r:  r;  r<  r>  r?  r0  r/  r^   r  r  ru   r  r  r  r   r  r  r  r   r   r   r\  rq  r  r   r   r   r  r   r   r  r  rG  ra   r/  r#   r$   r%   <module>r:     s   I "      	   $ (   			8	$x	'+
$ +.1)"  = = =    % % %&F
 F
RL06y55A?2..,.GH;:,0  d)dd dN6)
 &
=)
=
= 

=,2
<Y50q!H.&)!H&P:r$   