
    PmjJ                        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m	Z	 ddl
mZ ddlmZ  ej        e          ZddddZddZddZddZdS )z2Core NeMo Relay adapter for Hermes tool execution.    )annotationsN)Callable)Any)relay_runtime)metadata	tool_namestrargsdict[str, Any]callbackCallable[[dict[str, Any]], Any]
session_idr   dict[str, Any] | Nonereturntuple[Any, dict[str, Any]]c               $   t          j        |          \  }}}|||                                s           fS i dt          j                    dfd}	 t          |                    ||j        j        j	        | t                    ||t          |pi                               }	nr# t          $ re}
t          j        |
          rt          |
t                    r1/dv r+t                              dd	
           d         fcY d}
~
S  d}
~
ww xY wdv r t#          |	d                   r
d         fS t          |	t$                    r|	fS t'          j        t          |	          d          fS )z?Run one tool call through Relay and return its final arguments.N	next_argsr   r   c                    t          | t                    r| n	                                                               }n# t          $ r}| d }~ww xY w|d<   t          |          d<   d         S )Nvaluejson)
isinstancedictcopyrunBaseException	_jsonable)	r   resultexcr
   r   callback_contextcallback_errorobserved_args
raw_results	      7/home/thesage/.hermes/hermes-agent/agent/relay_tools.pyinvokezexecute.<locals>.invoke$   s    %/	4%@%@J		d	%**,,00=IIFF 	 	 	 N	 %
7&v..
6&!!s   (A 
AAA)handler   r   z_NeMo Relay tool post-processing failed after dispatch success; returning the Hermes tool resultT)exc_infor   F)ensure_ascii)r   r   r   r   )r   resolve_execution_contextmanaged_execution_enabledcontextvarscopy_context_run_awaitablerun_in_session_asyncrelaytoolsexecuter   r    _is_relay_wrapped_callback_errorr   	Exceptionloggerwarning_json_equalr	   r   dumps)r   r
   r   r   r   runtimesessionparentr$   managedr   r   r    r!   r"   s    ``        @@@@r#   r0   r0      s     -FzRRGWf'/1R1R1T1T/x~~t##M!#J+/N"/11
" 
" 
" 
" 
" 
" 
" 
" 
" 
" 
" ((#+$"8>r22 )  

 

    &>sNSS ' ! sI&&
	6&:%%NN3    
 g&5555555#& *Wj6H!I!I'"M11'3 &%%:i((u===}LLs&   )AB> >
D-AD(!D-'D((D-r   r   c                6   | (t          | t          t          t          t          f          r| S t          | t
                    rd |                                 D             S t          | t          t          t          f          rd | D             S t          | dd           }t          |          r*	 t           |d                    S # t          $ r Y nw xY w	 t          t          |                     S # t          t           f$ r t          |           cY S w xY w)Nc                N    i | ]"\  }}t          |          t          |          #S  )r	   r   ).0keyitems      r#   
<dictcomp>z_jsonable.<locals>.<dictcomp>Z   s*    IIIic4C)D//III    c                ,    g | ]}t          |          S r=   )r   )r>   r@   s     r#   
<listcomp>z_jsonable.<locals>.<listcomp>\   s    222D	$222rB   
model_dumpr   )mode)r   r	   intfloatboolr   itemslisttuplesetgetattrcallabler   r2   vars	TypeErrorAttributeError)r   rE   s     r#   r   r   V   s+   }
53UD*ABB}% JII5;;==IIII%$s+,, 322E2222d33J
 	ZZV444555 	 	 	D	e%%%~&   5zzs$   /C 
CCC5 5 DDleftrightrI   c                    	 t          j        t          |           dd          t          j        t          |          dd          k    S # t          t          f$ r	 | |k    cY S w xY w)NT),:)	sort_keys
separators)r   r6   r   rQ   
ValueError)rS   rT   s     r#   r5   r5   i   s    zdOOt

 
 
Z	%((DZPPPQ 	Q z"   u}s   A	A A&%A&c                    t          j        |           s| S 	 t          j                     n$# t          $ r t          j        |           cY S w xY wt	          d          )NzQSynchronous Hermes Relay tool execution cannot run on an active event-loop thread)inspectisawaitableasyncioget_running_loopRuntimeErrorr   )r   s    r#   r,   r,   r   sw    u%% " """" " " "{5!!!!!"
[  s   , AA)r   r	   r
   r   r   r   r   r	   r   r   r   r   )r   r   r   r   )rS   r   rT   r   r   rI   )__doc__
__future__r   r^   r*   r\   r   loggingcollections.abcr   typingr   agentr   	getLogger__name__r3   r0   r   r5   r,   r=   rB   r#   <module>ri      s   8 8 " " " " " "         $ $ $ $ $ $            		8	$	$ '+AM AM AM AM AM AMH   &   	 	 	 	 	 	rB   