
    /jC              	      D   S SK Jr  S SKrS SKrS SKrS SKrS SKJrJr  S SK	J
r
JrJr  S SKJr  S SKJrJr  S SKJrJrJrJrJrJrJr  S SKJrJrJrJrJr  S	S
K J!r!  SSK"J#r#  \(       a  S	SK$J%r%   " S S\5      r& " S S\&5      r' " S S5      r( " S S\SS9r) " S S\SS9r*\*\S   -  r+ " S S\,5      r- " S S\,5      r. " S S \5      r/\ " S! S"5      5       r0 " S# S$\5      r1\ " S% S&5      5       r2\" S'\0\25      r3\" S(5      r4\" S)\
\   S*9r5 " S+ S,\&\\3\4\54   5      r6 " S- S.\6\0\4\54   5      r7 " S/ S0\6\2\4\54   5      r8\\/Rr                  S1.       SBS2 jj5       r:\ SC\/Rr                  S1.       SDS3 jjj5       r:\SS\/Rr                  S4.         SES5 jj5       r:\ SCSS\/Rr                  S4.         SFS6 jjj5       r: SCSSS\/Rr                  S7.           SGS8 jjjr:SHS9 jr;SIS: jr<SJS; jr=SKS< jr>SLS= jr?SMS> jr@SNS? jrA " S@ SA5      rBg)O    )annotationsN)ABCabstractmethod)	AwaitableCallableSequence)	dataclass)Flagauto)TYPE_CHECKINGAnyGenericLiteral	TypeGuardTypeVaroverload)NotRequired	ParamSpecRequiredSelf	TypedDict   )logger   )_provider_format)
RunContextc                  2    \ rS rSr\\SS j5       5       rSrg)Tool$   c                    g N selfs    P/app/agent/.venv/lib/python3.13/site-packages/livekit/agents/llm/tool_context.pyidTool.id%   s        r"   Nreturnstr)__name__
__module____qualname____firstlineno__propertyr   r&   __static_attributes__r"   r(   r%   r   r   $   s      r(   r   c                  2    \ rS rSrSS jr\SS j5       rSrg)ProviderTool*   c                   Xl         g r!   _id)r$   r&   s     r%   __init__ProviderTool.__init__+   s    r(   c                    U R                   $ r!   r6   r#   s    r%   r&   ProviderTool.id.       xxr(   r6   N)r&   r+   r*   Noner)   )r,   r-   r.   r/   r8   r0   r&   r1   r"   r(   r%   r3   r3   *   s      r(   r3   c                      \ rS rSr\ " S S5      5       r\ " S S5      5       rSS.SS jjr\SS	 j5       r	\SS
 j5       r
SS jrSS jrSrg)Toolset3   c                  *    \ rS rSr% S\S'   S\S'   Srg)Toolset.ToolCalledEvent4   r   ctxdict[str, Any]	argumentsr"   Nr,   r-   r.   r/   __annotations__r1   r"   r(   r%   ToolCalledEventrB   4   s    !!r(   rI   c                  *    \ rS rSr% S\S'   S\S'   Srg)Toolset.ToolCompletedEvent9   r   rD   zAny | Exception | Noneoutputr"   NrG   r"   r(   r%   ToolCompletedEventrK   9   s    &&r(   rN   N)toolsc                   Xl         Ub  [        U5      O/ U l        U R                  R                  [	        U 5      5        g r!   )r7   list_toolsextendfind_function_tools)r$   r&   rO   s      r%   r8   Toolset.__init__>   s2    ?D?PUVX.t45r(   c                    U R                   $ r!   r6   r#   s    r%   r&   
Toolset.idC   r<   r(   c                    U R                   $ r!   )rR   r#   s    r%   rO   Toolset.toolsG   s    {{r(   c                   #    U R                    Vs/ s H  n[        U[        5      (       d  M  UPM     nnU(       a"  [        R                  " S U 5       6 I Sh  vN   U $ s  snf  N7f)zqInitialize the toolset and any nested toolsets.

Called automatically by ``AgentActivity`` when an agent starts.
c              3  @   #    U  H  oR                  5       v   M     g 7fr!   )setup.0toolsets     r%   	<genexpr> Toolset.setup.<locals>.<genexpr>R   s     "K(w==??(   N)rO   
isinstancer?   asynciogatherr$   tooltoolsetss      r%   r\   Toolset.setupK   sQ     
 &*ZZMZT:dG3LDZM.."K("KLLL NL    A(A!A!&A(A&A(c                   #    U R                    Vs/ s H  n[        U[        5      (       d  M  UPM     nnU(       a#  [        R                  " S U 5       6 I Sh  vN   ggs  snf  N7f)a3  Close the toolset and release any held resources.

Agent-scoped toolsets (passed to ``Agent(tools=...)``) are closed when the
``AgentActivity`` ends (on agent transition or session close). Session-scoped
toolsets (passed to ``AgentSession(tools=...)``) are closed only when the
``AgentSession`` shuts down.
c              3  @   #    U  H  oR                  5       v   M     g 7fr!   )acloser]   s     r%   r`   !Toolset.aclose.<locals>.<genexpr>_   s     "L8>>#3#38rb   N)rR   rc   r?   rd   re   rf   s      r%   rm   Toolset.acloseU   sQ      &*[[N[TJtW4MD[N.."L8"LMMM  OMrj   )r7   rR   )r&   r+   rO   zSequence[Tool | Toolset] | Noner*   r=   r)   )r*   Sequence[Tool | Toolset])r*   r   r*   r=   )r,   r-   r.   r/   r	   rI   rN   r8   r0   r&   rO   r\   rm   r1   r"   r(   r%   r?   r?   3   sq    " " " ' ' ' MQ 6
    
Nr(   r?   c                       \ rS rSr% S\S'   Srg)Functionc   zRequired[str]namer"   NrG   r"   r(   r%   rs   rs   c   s    
r(   rs   F)totalc                  *    \ rS rSr% S\S'   S\S'   Srg)NamedToolChoiceg   zRequired[Literal['function']]typezRequired[Function]functionr"   NrG   r"   r(   r%   rx   rx   g   s    
''  r(   rx   )r   requirednonec                  @   ^  \ rS rSrSU 4S jjr\SS j5       rSrU =r$ )	ToolErroro   c                0   > [         TU ]  U5        Xl        g)a  
Exception raised within AI functions.

This exception should be raised by users when an error occurs
in the context of AI operations. The provided message will be
visible to the LLM, allowing it to understand the context of
the error during FunctionOutput generation.
N)superr8   _message)r$   message	__class__s     r%   r8   ToolError.__init__p   s     	!r(   c                    U R                   $ r!   r   r#   s    r%   r   ToolError.message|   s    }}r(   r   )r   r+   r*   r=   r)   )	r,   r-   r.   r/   r8   r0   r   r1   __classcell__r   s   @r%   r   r   o   s    
   r(   r   c                  ,   ^  \ rS rSrSU 4S jjrSrU =r$ )StopResponse   c                "   > [         TU ]  5         g)z
Exception raised within AI functions.

This exception can be raised by the user to indicate that
the agent should not generate a response for the current
function call.
N)r   r8   )r$   r   s    r%   r8   StopResponse.__init__   s     	r(   r"   rq   )r,   r-   r.   r/   r8   r1   r   r   s   @r%   r   r      s     r(   r   c                  &    \ rS rSrSr\" 5       rSrg)ToolFlag   r   r"   N)r,   r-   r.   r/   NONEr   IGNORE_ON_ENTERr1   r"   r(   r%   r   r      s    DfOr(   r   c                  4    \ rS rSr% S\S'   S\S'   S\S'   Srg	)
FunctionToolInfo   r+   ru   
str | Nonedescriptionr   flagsr"   NrG   r"   r(   r%   r   r      s    
IOr(   r   c                  8    \ rS rSr% SrS\S'   S\S'   S\S'   S	rg
)RawFunctionDescription   aZ  
Represents the raw function schema format used in LLM function calling APIs.

This structure directly maps to OpenAI's function definition format as documented at:
https://platform.openai.com/docs/guides/function-calling?api-mode=responses

It is also compatible with other LLM providers that support raw JSON Schema-based
function definitions.
r+   ru   zNotRequired[str | None]r   zdict[str, object]
parametersr"   N)r,   r-   r.   r/   __doc__rH   r1   r"   r(   r%   r   r      s     I((!!r(   r   c                  4    \ rS rSr% S\S'   S\S'   S\S'   Srg	)
RawFunctionToolInfo   r+   ru   rE   
raw_schemar   r   r"   NrG   r"   r(   r%   r   r      s    
IOr(   r   _InfoT_P_R)boundc                  f    \ rS rSrSrS
SS jjr\SS j5       r\SS j5       rS
SS jjr	SS jr
S	rg)_BaseFunctionTool   z>Base class for function tool wrappers with descriptor support.Nc                T    [         R                  " X5        Xl        X l        X0l        g r!   )	functoolsupdate_wrapper_func_info	_instance)r$   funcinfoinstances       r%   r8   _BaseFunctionTool.__init__   s      ,
!
!r(   c                .    U R                   R                  $ r!   )r   ru   r#   s    r%   r&   _BaseFunctionTool.id   s    zzr(   c                    U R                   $ r!   )r   r#   s    r%   r   _BaseFunctionTool.info   s    zzr(   c                   Uc  U $ U R                  U R                  U R                  US9n[        R                  " U R                  5      n[        UR                  R                  5       5      SS  nUR                  US9Ul	        U$ )N)r   r   )r   )
r   r   r   inspect	signaturerQ   r   valuesreplace__signature__)r$   objobjtype
bound_toolsigparamss         r%   __get___BaseFunctionTool.__get__   su    ;K ^^DJJ

S^I


+cnn++-.qr2#&;;&;#A
 r(   c                    U R                   b   U R                  " U R                   /UQ70 UD6$ U R                  " U0 UD6$ r!   )r   r   )r$   argskwargss      r%   __call___BaseFunctionTool.__call__   s=    >>%::dnn>t>v>>zz4*6**r(   )r   r   r   r!   )r   Callable[_P, _R]r   r   r   r   r*   r=   r)   )r*   r   )r   r   r   ztype | Noner*   r   )r   z_P.argsr   z	_P.kwargsr*   r   )r,   r-   r.   r/   r   r8   r0   r&   r   r   r   r1   r"   r(   r%   r   r      s<    H"    
+r(   r   c                  D   ^  \ rS rSrSr S       SU 4S jjjrSrU =r$ )FunctionTool   z4Wrapper for a function decorated with @function_toolc                T   > [         TU ]  XU5        [        U SU R                  5        g )N__livekit_tool_infor   r8   setattrr   r$   r   r   r   r   s       r%   r8   FunctionTool.__init__   s%     	X.+TZZ8r(   r"   r!   )r   r   r   r   r   r   r*   r=   r,   r-   r.   r/   r   r8   r1   r   r   s   @r%   r   r      s7    > OS9$9,<9HK9	9 9r(   r   c                  D   ^  \ rS rSrSr S       SU 4S jjjrSrU =r$ )RawFunctionTool   zDWrapper for a function decorated with @function_tool(raw_schema=...)c                T   > [         TU ]  XU5        [        U SU R                  5        g )N__livekit_raw_tool_infor   r   s       r%   r8   RawFunctionTool.__init__   s%     	X./<r(   r"   r!   )r   r   r   r   r   r   r*   r=   r   r   s   @r%   r   r      s7    N RV=$=,?=KN=	= =r(   r   )r   c                   g r!   r"   fr   r   s      r%   function_toolr      s     "r(   c                   g r!   r"   r   s      r%   r   r      s     =@r(   ru   r   r   c                   g r!   r"   r   ru   r   r   s       r%   r   r      s     r(   c                   g r!   r"   r   s       r%   r   r     s     :=r(   )ru   r   r   r   c               r   ^^^^     SUU4S jjnSUUU4S jjnU b  Tb  U" U 5      $ U" U 5      $ Tb  U$ U$ )Nc                   > Tc   eTR                  S5      (       d  [        S5      eST;  a  [        S5      e[        TS   0 TETS9n[        X5      $ )Nru   z!raw function name cannot be emptyr   z6raw function description must contain a parameters key)ru   r   r   )get
ValueErrorr   r   )r   r   r   r   s     r%   deco_rawfunction_tool.<locals>.deco_raw  si     %%%~~f%%@AAz)UVV"F#%*~

 t**r(   c                   > SSK Jn  U" U 5      n[        T=(       d    U R                  T=(       d    UR                  TS9n[        X5      $ )Nr   )parse_from_objectr   )docstring_parserr   r   r,   r   r   )r   r   	docstringr   r   r   ru   s       r%   	deco_func function_tool.<locals>.deco_func2  sD    6%d+	&#<y'<'<

 D''r(   )r   r   r*   RawFunctionTool[_P, _R])r   r   r*   FunctionTool[_P, _R]r"   )r   ru   r   r   r   r   r   s    ````  r%   r   r     sV    ++	 + +&	( 	( 	}(4x{F)A,F!-8<9<r(   c                "    [        U [        5      $ r!   )rc   r   r   s    r%   is_function_toolr   C  s    a&&r(   c                    U R                   $ r!   r   r   s    r%   get_function_infor   H      66Mr(   c                "    [        U [        5      $ r!   )rc   r   r   s    r%   is_raw_function_toolr   L  s    a))r(   c                    U R                   $ r!   r   r   s    r%   get_raw_function_infor   P  r   r(   c                d   [        U 5      (       d  g[        U [        [        45      (       a  U $ Sn[	        U S5      (       aM  [        U R
                  [        [        45      (       a(  U R
                  nUR                  XR                  5      nOi[        U SS5      =n(       a!  [        U[        5      (       a  [        X5      nO4[        U SS5      =n(       a   [        U[        5      (       a  [        X5      nU(       a1  UR                  R                  n[        R                  " SU S3SU0S9  U$ )	zKConvert a wrapped tool to a FunctionTool or RawFunctionTool with a warning.N__wrapped__r   r   zfunction tool z is wrapped, this may cause unexpected behavior and not be supported in future versions, please wrap the original function before converting to a function tool.r   )extra)callablerc   r   r   hasattrr   r   r   getattrr   r   ru   r   warning)rg   resolved_toolwrappedr   	tool_names        r%   _resolve_wrapped_toolr	  T  s   D>>$788;?Mm$$t'',)HII""))$=$ 5t<
<$
<*C C %T0$ 94@
@$
@j!G G (3!&&++	YK (V V  	
 r(   c                    / n[         R                  " U 5       HX  u  p#[        U[        [        45      (       a  UR                  U5        M3  [        U5      =n(       d  MG  UR                  U5        MZ     U$ r!   )r   
getmembersrc   r   r   appendr	  )
cls_or_objmethods_membernormalized_tools        r%   rT   rT   {  s_    46G''
3	f|_=>>NN6" 5f ==_=NN?+	 4 Nr(   c                   / nU  H  n[        U[        [        45      (       a'  UR                  UR                  R
                  5        ME  [        U[        5      (       d  M\  UR                  [        UR                  5      5        M     U$ )zSGet names of all function and raw function tools in the list, unwrapping tool sets.)
rc   r   r   r  r   ru   r?   rS   get_fnc_tool_namesrO   )rO   namesrg   s      r%   r  r    sa    Ed\?;<<LL(g&&LL+DJJ78	  Lr(   c                  @   \ rS rSrSrSS jr\SS j5       r\SS j5       r	\SS j5       r
\SS j5       rSS jrSS	 jrSS
 jrSS jrSS jr\SS.     SS jj5       r\SS.     S S jj5       r\S!S j5       r\SS.     S"S jj5       r      S#S jrSrg)$ToolContexti  z-Stateless container for a set of AI functionsc                &    U R                  U5        g r!   )update_toolsr$   rO   s     r%   r8   ToolContext.__init__  s    % r(   c                    U " / 5      $ r!   r"   )clss    r%   emptyToolContext.empty  s    2wr(   c                6    U R                   R                  5       $ )zOA copy of all function tools in the tool context, including those in tool sets.)_fnc_tools_mapcopyr#   s    r%   function_toolsToolContext.function_tools  s     ""''))r(   c                    U R                   $ )zOA copy of all provider tools in the tool context, including those in tool sets.)_provider_toolsr#   s    r%   provider_toolsToolContext.provider_tools  s     ###r(   c                    U R                   $ )z,A copy of all tool sets in the tool context.)
_tool_setsr#   s    r%   rh   ToolContext.toolsets  s     r(   c                    / nUR                  [        U R                  R                  5       5      5        UR                  U R                  5        U$ )z,Flatten the tool context to a list of tools.)rS   rQ   r   r   r%  r  s     r%   flattenToolContext.flatten  s>    T$--44678T))*r(   c                8    U R                   R                  U5      $ r!   )r   r   )r$   ru   s     r%   get_function_toolToolContext.get_function_tool  s    ""&&t,,r(   c                   [        U[        5      (       d  gU R                  R                  5       UR                  R                  5       :w  a  gU R                   H$  nU R                  U   UR                  U   Ld  M$    g   [	        U R
                  5      [	        UR
                  5      :w  a  gU R
                   Vs1 s H  n[        U5      iM     nnUR
                   Vs1 s H  n[        U5      iM     nnXE:w  a  gU R                   Vs1 s H  n[        U5      iM     nnUR                   Vs1 s H  n[        U5      iM     nnXx:w  a  ggs  snf s  snf s  snf s  snf )NFT)rc   r  r   keyslenr%  r&   r)  )	r$   otherru   rg   self_provider_idsother_provider_idstool_setself_tool_set_idsother_tool_set_idss	            r%   __eq__ToolContext.__eq__  s6   %--##%)=)=)B)B)DD''D""4(0D0DT0JJ ( t##$E,A,A(BB262F2FG2F$RX2FG383H3HI3H4bh3HI2:>//J/hR\/J;@;K;KL;Kxbl;KL2 HI KLs   =E"EE2Ec                   ^ ^ [        U5      T l        0 T l        / T l        / T l        SUU 4S jjm[
        R                  " U[        T 5      5       H  nT" U5        M     g )Nc                  > [        U [        5      (       a  TR                  R                  U 5        g [        U [        [
        45      (       a}  TR                  R                  U R                  R                  5      nUb'  XLa"  [        SU R                  R                   35      eg U TR                  U R                  R                  '   g [        U [        5      (       a7  U R                   H  nT" U5        M     TR                  R                  U 5        g [        U 5      =n(       a	  T" U5        g [        U 5      (       a  [        S5      e[        S[!        U 5       35      e)Nzduplicate function name: zExpected an instance of FunctionTool or RawFunctionTool, got a callable object. If it's a wrapped tool, please consider wrapping the original function before converting to a function tool.zunknown tool type: )rc   r3   r%  r  r   r   r   r   r   ru   r   r?   rO   r)  r	  r  rz   )rg   existingtr  add_toolr$   s       r%   r@  *ToolContext.update_tools.<locals>.add_tool  s   $--$$++D1D<"ABB..22499>>B'+(+DTYY^^DT)UVV6:##DIINN3D'**AQK $&&t,$9$$???)$ C  !#6tDzl!CDDr(   )rg   zTool | Toolsetr*   r=   )rQ   rR   r   r%  r)  	itertoolschainrT   )r$   rO   rg   r@  s   `  @r%   r  ToolContext.update_tools  sV    5kIK35)+	E 	E: OOE+>t+DEDTN Fr(   c                H    [        U R                  R                  5       5      $ r!   )r  rR   r!  r#   s    r%   r!  ToolContext.copy  s    4;;++-..r(   T)strictc                   g r!   r"   r$   formatrG  s      r%   parse_function_tools ToolContext.parse_function_tools        #r(   N)tool_behaviorc                   g r!   r"   )r$   rJ  rN  s      r%   rK  rL    s      #r(   c                    g r!   r"   )r$   rJ  s     r%   rK  rL    s    TWr(   c                   g r!   r"   rI  s      r%   rK  rL    rM  r(   c                   US:X  a!  [         R                  R                  " U 40 UD6$ US:X  a!  [         R                  R                  " U 40 UD6$ US:X  a!  [         R                  R                  " U 40 UD6$ US:X  a!  [         R
                  R                  " U 40 UD6$ US:X  a!  [         R                  R                  " U 40 UD6$ [        SU 35      e)z7Parse the function tools to a provider-specific schema.openaizopenai.responsesgoogle	anthropicawszUnsupported provider format: )r   rS  
to_fnc_ctxto_responses_fnc_ctxrT  rU  rV  r   )r$   rJ  r   s      r%   rK  rL    s     X#**55dEfEE))#**??OOOx#**55dEfEE{"#--88HHHu_#''224B6BB8ABBr(   )r   r%  r)  rR   )rO   rp   r*   r=   )r*   r  )r*   z)dict[str, FunctionTool | RawFunctionTool])r*   zlist[ProviderTool])r*   zlist[Toolset])r*   z
list[Tool])ru   r+   r*   %FunctionTool | RawFunctionTool | None)r4  objectr*   bool)rJ  z%Literal['openai', 'openai.responses']rG  r[  r*   list[dict[str, Any]])rJ  zLiteral['google']rN  z,_provider_format.google.TOOL_BEHAVIOR | Noner*   r\  )rJ  zLiteral['aws']r*   r\  )rJ  zLiteral['anthropic']rG  r[  r*   r\  )rJ  z5Literal['openai', 'google', 'aws', 'anthropic'] | strr   r   r*   r\  )r,   r-   r.   r/   r   r8   classmethodr  r0   r"  r&  rh   r,  r/  r:  r  r!  r   rK  r1   r"   r(   r%   r  r    s=   7!   * * $ $  -4$L/ OS#;#HL#	# # 
 GK	#!# D	#
 
# # W W>B#*#7;#	# #CEC C 
	Cr(   r  )r   r   r   'RawFunctionDescription | dict[str, Any]r   r   r*   r   r!   )r   r=   r   r^  r   r   r*   z5Callable[[Callable[_P, _R]], RawFunctionTool[_P, _R]])
r   r   ru   r   r   r   r   r   r*   r   )
r   r=   ru   r   r   r   r   r   r*   z2Callable[[Callable[_P, _R]], FunctionTool[_P, _R]])r   zCallable[_P, _R] | Noneru   r   r   r   r   z.RawFunctionDescription | dict[str, Any] | Noner   r   r*   zFunctionTool[_P, _R] | RawFunctionTool[_P, _R] | Callable[[Callable[_P, _R]], FunctionTool[_P, _R]] | Callable[[Callable[_P, _R]], RawFunctionTool[_P, _R]])r   r   r*   zTypeGuard[FunctionTool])r   r   r*   r   )r   r   r*   zTypeGuard[RawFunctionTool])r   r   r*   r   )rg   r   r*   rY  )r  r   r*   z$list[FunctionTool | RawFunctionTool])rO   rp   r*   z	list[str])C
__future__r   rd   r   r   rB  abcr   r   collections.abcr   r   r   dataclassesr	   enumr
   r   typingr   r   r   r   r   r   r   typing_extensionsr   r   r   r   r   logr    r   voice.eventsr   r   r3   r?   rs   rx   
ToolChoice	Exceptionr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r	  rT   r  r  r"   r(   r%   <module>rk     s?   #     # 9 9 !  U U U O O  )3 4 ,N ,N`y !iu !
 w'ABB
	 $	9 	t 
   "Y "     
+-@	At_T3( +gfb"n5  +F9$%5r2%=> 9='(;R(CD = 

 mm	"" 8" 	"
 " 
" 
@ mm	@@ 8@ 	@
 ;@ 
@ 
 "mm  	
   
 
= "mm== = 	=
 = 8= 
= "&.= "AEmm.=.= .= 	.=
 ?.= .=<.=b'
*$N	LC LCr(   