
    BvhG#                     v   d dl Z d dlZd dlZd dlZd dlm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  ej                   dd	
      Z ej                   ddd      Zej'                  ed       dee   ddfdZej-                          ej.                  ddd      fdee   fd       Zedk(  r e        yy)    N)Optional)print   )_async_prompt_patch_anyio_open_process)Agent)_load_agent_configrichziA squad of lightweight composable AI applications built on Hugging Face's Inference Client and MCP stack.)rich_markup_modehelprunzRun the Agent in the CLIT)namer   invoke_without_command)r   
agent_pathreturnc                   K   t                t        |       \  }}|j                  dg       }|j                  dg       }t        j                         t        j                         dt        j
                         }t        j                  t        j                        }d8fd	 d}	 |j                  t        j                         d}t        |      dkD  rt        d	       |D ]  }|d
   }	|d   }
d|	z   dz   }t               }|D ]\  }|d   dk(  r|j                  di       n|j                  di       }|j                         D ]  \  }}||v s|j!                  |        ^ |st        d|	 d       |	j#                  dd      j%                         }t        d|	 d|
 d| dd       t'               d{   j)                         }j+                         rI |r!	 |j-                  t        j                         yt        j                  t        j                  |       y|D ]  }|d   dk(  r|j                  di       n|j                  di       }|j                         D ]t  \  }}||v s|r||   j#                  ||      ||<   &t/        j0                  |d      }||   j#                  ||      ||<   |rt        d| d       ft        d | d!       v   t                t3        |j                  d"      |j                  d#      |j                  d$      ||%      4 d{   }|j5                          d{    t        d&t        |j6                         d'       |j6                  D ]%  }t        d|j8                  j:                   d(       ' 	 j=                          j+                         rY	 ddd      d{    |r!	 |j-                  t        j                         yt        j                  t        j                  |       y	 t'               d{   }d	 |jC                  |,      2 3 d{   }j+                         rs nj+                         rY ddd      d{    |r!	 |j-                  t        j                         yt        j                  t        j                  |       ytE        |d-      r|jF                  d   jH                  }|jJ                  rt        |jJ                  dd.       |jL                  s|jL                  D ]  }|jN                  rt        d/|jN                   d0d       |j8                  j:                  r"t        |j8                  j:                   d       |j8                  jP                  syt        |j8                  jP                   d        t        d1|j:                   d2|jR                   d3|jJ                   d4d*       t                o# t        t        f$ r+ t        j                  t        j                  fd       Y w xY w7 # t        t        f$ r Y yw xY w7 87 #7 # t        t        f$ r Y yw xY w7 Y# t>        $ r t        d)d*       Y nt@        $ r% sj+                         rY t        d+d*       Y nbw xY w7 7 R# t        t        f$ r Y yw xY w6 # tT        $ r5}tW        jX                         }t        d5| d3| d6d*       dY d}~d}~ww xY wddd      d{  7   n# 1 d{  7  sw Y   nxY wn;# tT        $ r/}tW        jX                         }t        d7| d3| d6d*       |d}~ww xY w|r6	 |j-                  t        j                         y# t        t        f$ r Y yw xY wt        j                  t        j                  |       y# r6	 |j-                  t        j                         w # t        t        f$ r Y w w xY wt        j                  t        j                  |       w xY ww)9z
    Tiny Agent loop.

    Args:
        agent_path (`str`, *optional*):
            Path to a local folder containing an `agent.json` and optionally a custom `PROMPT.md` file or a built-in agent stored in a Hugging Face dataset.

    inputsserversTNc                      r d j                          t        dd       y t        dd       j                          y )NFz4
[red]Interrupted. Press Ctrl+C again to quit.[/red]Tflushz
[red]Exiting...[/red])setr   )abort_event
exit_eventfirst_sigints   L/RAG/venv/lib/python3.12/site-packages/huggingface_hub/inference/_mcp/cli.py_sigint_handlerz"run_agent.<locals>._sigint_handler5   s9     LOOIQUV't4    Fc                              S )N )_r   s    r   <lambda>zrun_agent.<locals>.<lambda>G   s    O4Er   r   z}[bold blue]Some initial inputs are required by the agent. Please provide a value or leave empty to load from env.[/bold blue]iddescriptionz${input:}typestdioenvheadersz[yellow]Input z7 defined in config but not used by any server.[/yellow]-r!   u   [blue] • z	[/blue]: z. (default: load from z). )end)r    z'[green]Value successfully loaded from 'z	'[/green]z[yellow]No value found for 'z0' in environment variables. Continuing.[/yellow]providermodelendpointUrl)r.   r/   base_urlr   promptz[bold blue]Agent loaded with z tools:[/bold blue]z[/blue]z"
[red]EOF received, exiting.[/red]r   z7
[red]Keyboard interrupt during input processing.[/red])r   choices)r,   r   z<Tool >z

[green]Tool[z] 
z	[/green]
z#
[bold red]Error during agent run: [/bold red])
[bold red]An unexpected error occurred: )r   N)-r   r	   getasyncioEventget_running_loopsignal	getsignalSIGINTadd_signal_handlerAttributeErrorNotImplementedErrorlenr   r   itemsaddreplaceupperr   stripis_setremove_signal_handlerosgetenvr   
load_toolsavailable_toolsfunctionr   clearEOFErrorKeyboardInterruptr   hasattrr3   deltacontent
tool_callsr#   	argumentstool_call_id	Exception	traceback
format_exc)r   configr2   r   r   looporiginal_sigint_handlersigint_registered_in_loop
input_iteminput_idr$   env_special_value
input_varsserverenv_or_headerskeyvalueenv_variable_key
user_inputvalue_from_envagenttchunkrS   calletb_strr   r   r   r   s                              @@@@r   	run_agentrp      s     '
3NFFZZ"%FjjB'G--/KJL##%D$..v}}=	KB$)!	G##FMM?C(,% v;?V %
%d+(7$.$9C$?! !U
%F>DVnPW>WVZZr%:]c]g]ghqsu]vN&4&:&:&<
U,5&NN3/ '= & "N8*4klm $,#3#3C#=#C#C#E !(9[MAWXhWiikl %2Z$HHOOQ
$$&x %**6==9 MM&--)@A &F>DVnPW>WVZZr%:]c]g]ghqsu]vN&4&:&:&<
U,5)6DS6I6Q6QRceo6ps 313;KR1P6DS6I6Q6QRces6ts 3#1$),STdSeen*o$p$)*FGWFX  YI  )J%& '= &; %Z G ZZ
+**W%ZZ.
 
 ""$$$1#e6K6K2L1MM`ab**AJJOO#4G<= + !!# $$&#
 
L %**6==9 MM&--)@As'4
'K!KJ#'L(',yyy'U e&--/!%,,."K
 
L %**6==9 MM&--)@AK #5)4$)MM!$4$:$:E$}} %emm4 H$//,1,<,<D'+ww(-twwiq.Ar(J'+}}'9'9(-1C1C0D3(O'+}}'>'>(-1H1H0IPR(S -= ""25::,bASAS@TTVW\WdWdVeeo p&*
 G[ M  34 	GMM&--)EF	GD IB #$78 S
 %
R #$78 k "L ?tL( 'K,>,>,@ X`deC
R #$78 Q (V4 ! (&113F@2fX[Yaef#'L(w
 
 
 
 
@  %%':1#Rx{S[_` %**6==9"$78  MM&--)@A %**6==9"$78  MM&--)@Asx  B&a-]" 0"X B]" A-]" Y#]" +a.Y &a3A]" ?B<]" ;Y3<]" ?]Y6A<]]" Y9]"  a#Y< &a)Z8Z9Z [9[7[[7'[9]" []" a[" 4&aA[90B[95A'[9]6Y]" Y]" Y0-a/Y00a3]" 6]9]" <ZaZaZ[*],[]	[][][7]" "[41a3[44a7[99	\7*\2,]2\77]:]" ]]" ]]]]" !_: "	^+*^^_: a _  ?a _a_(a:a>`a`0-a/`00(aazPath to a local folder containing an agent.json file or a built-in agent stored in the 'tiny-agents/tiny-agents' Hugging Face dataset (https://huggingface.co/datasets/tiny-agents/tiny-agents)F)r   show_defaultpathc                     	 t        j                  t        |              y # t        $ r$ t	        dd       t        j                  d      t        $ r}t	        d| dd       |d }~ww xY w)Nz8
[red]Application terminated by KeyboardInterrupt.[/red]Tr      )coder7   r6   )r9   r   rp   rQ   r   typerExitrX   )rr   rn   s     r   r   r      sd    IdO$ #IQUVjjc"" :1#[IQUVs   ! 4A-A((A-__main__)r9   rJ   r<   rY   typingr   rv   r
   r   
_cli_hacksr   r   rj   r   utilsr	   Typerapprun_cli	add_typerstrrp   callbackArgumentr   __name__r    r   r   <module>r      s     	      @  % ekk	t
 %++		#
 gE "oBoB	oBd 	(%..H 
3- * zE r   