
    xj                        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dfd}	                                                     |          }n# t          $ r}| d }~ww xY w|	d<   t          |          	d<   	d         S )	N
final_argsr   r   r   c                t    t          j                    5   |           cd d d            S # 1 swxY w Y   d S )N)r   managed_callback_guard)r   r   s    5/home/agent/.hermes/hermes-agent/agent/relay_tools.pyguardedz(execute.<locals>.invoke.<locals>.guarded(   s    
 577 , ,x
++, , , , , , , , , , , , , , , , , ,s   -11valuejson)r   r   r   r   )
isinstancedictcopyrunBaseException	_jsonable)
r   r   resultexcr
   r   callback_contextcallback_errorobserved_args
raw_results
       r   invokezexecute.<locals>.invoke$   s    %/	4%@%@J		d	, 	, 	, 	, 	, 	,	%**,,00-HHFF 	 	 	 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   r4   r4      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                   | (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          |          rS	 	 t           |dd                    S # t          $ r t           |                      cY S w xY w# 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>c   s*    IIIic4C)D//III    c                ,    g | ]}t          |          S rA   )r!   )rB   rD   s     r   
<listcomp>z_jsonable.<locals>.<listcomp>e   s    222D	$222rF   
model_dumpr   F)modewarnings)r   r	   intfloatboolr   itemslisttuplesetgetattrcallabler!   	TypeErrorr6   varsAttributeError)r   rI   s     r   r!   r!   _   sg   }
53UD*ABB}% JII5;;==IIII%$s+,, 322E2222d33J
 		/ %!H!H!HIII / / / ...../ 	 	 	D	e%%%~&   5zzs<   0C
 
!C.+C1 -C..C1 1
C>=C>D  E EleftrightrN   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   r:   r!   rU   
ValueError)rX   rY   s     r   r9   r9   w   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   r0   r0      sw    u%% " """" " " "{5!!!!!"
[  s   , AA)r   r	   r
   r   r   r   r   r	   r   r   r   r   )r   r   r   r   )rX   r   rY   r   r   rN   )__doc__
__future__r   rc   r.   ra   r   loggingcollections.abcr   typingr   agentr   	getLogger__name__r7   r4   r!   r9   r0   rA   rF   r   <module>rn      s   8 8 " " " " " "         $ $ $ $ $ $            		8	$	$ '+JM JM JM JM JM JMZ   0   	 	 	 	 	 	rF   