
    qiS&                     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   )ANSI   )_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                        }d5 !"fd	 d}	 |j                  t        j                         d}i }t        |      dkD  rt        t        j                  t        j                   d	                   |D ]o  }	|	d
   }
|	d   }d|
 d}t#               }|D ]\  }|d   dk(  r|j                  di       n|j                  di       }|j%                         D ]  \  }}||v s|j'                  |        ^ |j                  d      }t)        |t*              r||v r|j'                  d       |s#t        t        j,                  d|
 d             |
j/                  dd      j1                         }t        t        j                   d|
       d| d| dz   d       t3        !       d{   j5                         }!j7                         rI |r!	 |j9                  t        j                         yt        j                  t        j                  |       y|}|s]t;        j<                  |d      }|r#t        t        j>                  d| d              n"t        t        j,                  d!| d"             |||
<   |D ]c  }|d   dk(  r|j                  di       n|j                  di       }|j%                         D ]"  \  }}||v s||   j/                  ||      ||<   $ e r t                |j                  d      }t)        |t*              r5|}|j%                         D ]  \  }
}|j/                  d|
 d|      } ||d<   tA        |j                  d#      |j                  d$      |j                  d%      |j                  d      ||&      4 d{   }|jC                          d{    t        t        j                  t        j                   d'jE                  t        |jF                                                 |jF                  D ]7  }t        t        j                   d|jH                  jJ                                9 	  jM                          !j7                         rY	 ddd      d{    |r!	 |j9                  t        j                         yt        j                  t        j                  |       y	 t3        !       d{   }d"	 |jU                  | +      2 3 d{   } j7                         r"s n!j7                         rY ddd      d{    |r!	 |j9                  t        j                         yt        j                  t        j                  |       ytW        |d,      r|jX                  d   jZ                  }|j\                  rt        |j\                  dd-       |j^                  s|j^                  D ]  }|j`                  rt        d.|j`                   d/d       |jH                  jJ                  r"t        |jH                  jJ                   d       |jH                  jb                  syt        |jH                  jb                   d        t        t        j>                  d0|jJ                   d1|jd                   d2|j\                   d2      d)       t                # t        t        f$ r+ t        j                  t        j                  fd       Y w xY w7 d# t        t        f$ r Y yw xY w7 7 y7 # t        t        f$ r Y yw xY w7 l# tN        $ r# t        t        jP                  d(      d)       Y ntR        $ r8 "s j7                         rY At        t        jP                  d*      d)       Y nuw xY w7 7 # t        t        f$ r Y yw xY w6 # tf        $ rG}ti        jj                         }t        t        jP                  d3| d2|       d)       d"Y d}~Od}~ww xY wddd      d{  7   n# 1 d{  7  sw Y   nxY wnM# tf        $ rA}ti        jj                         }t        t        jP                  d4| d2|       d)       |d}~ww xY w|r6	 |j9                  t        j                         y# t        t        f$ r Y yw xY wt        j                  t        j                  |       y# r6	 |j9                  t        j                         w # t        t        f$ r Y w w xY wt        j                  t        j                  |       w xY ww)6z
    Tiny Agent loop.

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

    inputsserversTNc                      r3d j                          t        t        j                  d      d       y t        t        j                  d      d       j                          y )NFz)
Interrupted. Press Ctrl+C again to quit.Tflushz
Exiting...)setprintr   red)abort_event
exit_eventfirst_sigints   T/opt/pipecat/venv/lib/python3.12/site-packages/huggingface_hub/inference/_mcp/cli.py_sigint_handlerz"run_agent.<locals>._sigint_handler5   sJ     LOO$((GHPTUdhh~&d3    Fc                              S )N )_r    s    r   <lambda>zrun_agent.<locals>.<lambda>G   s
    O4E r!   r   zfSome initial inputs are required by the agent. Please provide a value or leave empty to load from env.iddescriptionz${input:}typestdioenvheadersapiKeyzInput 'zJ' defined in config but not used by any server or as an API key. Skipping.-r$   u    • z: z. (default: load from z). )end)r    z Value successfully loaded from ''zNo value found for 'z'' in environment variables. Continuing.providermodelendpointUrl)r3   r4   base_urlapi_keyr   promptzAgent loaded with {} tools:z
EOF received, exiting.r   z,
Keyboard interrupt during input processing.)r   choices)r0   r   z<Tool >z

Tool[z] 
z
Error during agent run: 
An unexpected error occurred: )r   N)6r   r
   getasyncioEventget_running_loopsignal	getsignalSIGINTadd_signal_handlerAttributeErrorNotImplementedErrorlenr   r   boldbluer   itemsadd
isinstancestryellowreplaceupperr   stripis_setremove_signal_handlerosgetenvgreenr	   
load_toolsformatavailable_toolsfunctionr   clearEOFErrorr   KeyboardInterruptr   hasattrr9   deltacontent
tool_callsr&   	argumentstool_call_id	Exception	traceback
format_exc)#r   configr8   r   r   looporiginal_sigint_handlersigint_registered_in_loopresolved_inputs
input_iteminput_idr'   env_special_valueinput_usagesserverenv_or_headerskeyvalueraw_api_keyenv_variable_key
user_inputfinal_valuesubstituted_api_keyvalagenttchunkr_   calletb_strr    r   r   r   s#                                  @@@@r   	run_agentr      s	     '
3NFFZZ"%FjjB'G--/KJL##%D$..v}}=	eB$)!	G##FMM?C(,% +-v;?		IIR % 8n
%d+(7&/z$<!  #u% 2F>DVnPW>WVZZr%:]c]g]ghqsu]vN&4&:&:&< 2
U,5(,,S122 %jj2k3/4E4T $$X.#%hZ 0) )  $,#3#3C#=#C#C#E IIhZ01bE[\l[mmo4pp %2Z$HHOOQ
$$&N %**6==9 MM&--)@AU )""$)),<b"AK"djj+KL\K]]^)_`a KK"67G6HHo p
 -8) & nF>DVnPW>WVZZr%:]c]g]ghqsu]vN&4&:&:&< n
U,52@2E2M2MN_al2mN3/nni8nt Gjj*k3'"-!0!6!6!8 a#&9&A&AIhZWYBZ\_&`#a2F8ZZ
+**W%ZZ.JJx(
 ?	( ?	( ""$$$$))DII&C&J&J3uOdOdKe&fghi** <dii%

'8 9:;< !!# $$&%?	( ?	(N %**6==9 MM&--)@As'4
'K!KJ#'L(',yyy'U  e&--/!%,,."M?	( ?	(N %**6==9 MM&--)@AK #5)4$)MM!$4$:$:E$}} %emm4 H$//,1,<,< !TD'+ww(-twwiq.Ar(J'+}}'9'9(-1C1C0D3(O'+}}'>'>(-1H1H0IPR(S!T " $

Yuzzl"UEWEWDXXZ[`[h[hZiik+l m&*
 G[ A  34 	GMM&--)EF	Gb IX #$78 U?	( %?	(T #$78 k "L $((#=>dK( 'K,>,>,@ dhh'VW_cdE?	(T #$78 Q (V4 ! (&113F$((%?s"VH#MNVZ[#'L(y?	( ?	( ?	( ?	( ?	(B  %%'dhh9!BvhGHPTU %**6==9"$78  MM&--)@A %**6==9"$78  MM&--)@Asx  B&g;-c0 0"]3 B-c0  Cc0 ^0#c0 0g;3^3 &g;8B1c0 *Cc0 :_;c0 >c_B?cc0 _c0 "g;%_ &g;+_):_&;_)a5a2aa2'a5c0 ac0 g;a 6&g;Aa52Ba57A:a51c36^-)c0 ,^--c0 3_g;_g;c0 cc0 _# g;"_##g;&_)))aca.c1 acaca2c0 a/,g;.a//g;2a55	c><c :c ccc0 cc0 c, c#!c,(c0 /f 0	d:9<d55d::f =g; e  g; e2/g;1e22(g;g8f>=g8>gg8g(g88g;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        $ r7 t	        t        j                  d      d       t        j                  d      t        $ r*}t	        t        j                  d|       d       |d }~ww xY w)Nz-
Application terminated by KeyboardInterrupt.Tr      )coder<   )
r>   r   r   r]   r   r   r   typerExitrd   )r   r~   s     r   r   r      ss    IdO$ #dhhGHPTUjjc"" dhh9!=>dKs   ! AB(%BB__main__)r>   rT   rA   re   typingr   r   utilsr   
_cli_hacksr   r   rz   r	   r
   Typerapprun_cli	add_typerrM   r   callbackArgumentr   __name__r#   r!   r   <module>r      s     	      @  % ekk	t
 %++		#
 gE "IBIB	IBX 	(%..H 
3- * zE r!   