o
    i=                     @  s  d dl mZ d dlZd dlZd dlZd dlmZmZ d dlm	Z	m
Z
mZ d dlmZ d dlmZmZ d dlmZmZmZmZmZmZmZ d dlmZmZmZmZmZ d	d
lm Z  ddl!m"Z" erfd	dl#m$Z$ G dd deZ%G dd de%Z&G dd deZ'G dd deddZ(G dd deddZ)e)ed B Z*G dd de+Z,G dd de+Z-G dd  d eZ.eG d!d" d"Z/G d#d$ d$eZ0eG d%d& d&Z1ed'e/e1Z2ed(Z3ed)e	e d*Z4G d+d, d,e%ee2e3e4f Z5G d-d. d.e5e/e3e4f Z6G d/d0 d0e5e1e3e4f Z7ee.j8d1ded9d:Z9e	dfe.j8d1dgd=d:Z9edde.j8d>dhdCd:Z9e	dfdde.j8d>didEd:Z9	dfddde.j8dFdjdJd:Z9dkdMdNZ:dldOdPZ;dmdRdSZ<dndTdUZ=dodXdYZ>dpd\d]Z?dqdadbZ@G dcdd ddZAdS )r    )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                   @  s   e Zd ZeedddZdS )Toolreturnstrc                 C     d S N selfr!   r!   a/var/www/html/livekit_bhavya/venv/lib/python3.10/site-packages/livekit/agents/llm/tool_context.pyid$      zTool.idNr   r   )__name__
__module____qualname__propertyr   r%   r!   r!   r!   r$   r   #   s    r   c                   @  s$   e Zd Zd
ddZedddZd	S )ProviderToolr%   r   r   Nonec                C  
   || _ d S r    _idr#   r%   r!   r!   r$   __init__*      
zProviderTool.__init__c                 C     | j S r    r/   r"   r!   r!   r$   r%   -      zProviderTool.idNr%   r   r   r-   r'   )r(   r)   r*   r2   r+   r%   r!   r!   r!   r$   r,   )   s    
r,   c                   @  sZ   e Zd ZeG dd dZeG dd dZdd	d
ZedddZee	dddZ
dS )Toolsetc                   @     e Zd ZU ded< ded< dS )zToolset.ToolCalledEventr   ctxdict[str, Any]	argumentsNr(   r)   r*   __annotations__r!   r!   r!   r$   ToolCalledEvent3      
 r>   c                   @  r8   )zToolset.ToolCompletedEventr   r9   zAny | Exception | NoneoutputNr<   r!   r!   r!   r$   ToolCompletedEvent8   r?   rA   r%   r   r   r-   c                C  r.   r    r/   r1   r!   r!   r$   r2   =   r3   zToolset.__init__c                 C  r4   r    r/   r"   r!   r!   r$   r%   @   r5   z
Toolset.id
list[Tool]c                 C  r   r    r!   r"   r!   r!   r$   toolsD   r&   zToolset.toolsNr6   r'   r   rB   )r(   r)   r*   r   r>   rA   r2   r+   r%   r   rC   r!   r!   r!   r$   r7   2   s    
r7   c                   @  s   e Zd ZU ded< dS )FunctionzRequired[str]nameNr<   r!   r!   r!   r$   rE   J   s   
 rE   F)totalc                   @  r8   )NamedToolChoicezRequired[Literal['function']]typezRequired[Function]functionNr<   r!   r!   r!   r$   rH   N   s   
 rH   )r
   requirednonec                      s,   e Zd Zd	 fddZed
ddZ  ZS )	ToolErrormessager   r   r-   c                   s   t  | || _dS )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)superr2   _message)r#   rN   	__class__r!   r$   r2   W   s   	
zToolError.__init__c                 C  r4   r    )rP   r"   r!   r!   r$   rN   c   r5   zToolError.message)rN   r   r   r-   r'   )r(   r)   r*   r2   r+   rN   __classcell__r!   r!   rQ   r$   rM   V   s    rM   c                      s   e Zd Zd fddZ  ZS )StopResponser   r-   c                   s   t    dS )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)rO   r2   r"   rQ   r!   r$   r2   i   s   zStopResponse.__init__)r   r-   )r(   r)   r*   r2   rS   r!   r!   rQ   r$   rT   h   s    rT   c                   @  s   e Zd ZdZe ZdS )ToolFlagr   N)r(   r)   r*   NONEr
   IGNORE_ON_ENTERr!   r!   r!   r$   rU   t   s    
rU   c                   @  &   e Zd ZU ded< ded< ded< dS )FunctionToolInfor   rF   
str | NonedescriptionrU   flagsNr<   r!   r!   r!   r$   rY   y      
 rY   c                   @  s*   e Zd ZU dZded< ded< ded< dS )	RawFunctionDescriptionar  
    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   rF   zNotRequired[str | None]r[   zdict[str, object]
parametersN)r(   r)   r*   __doc__r=   r!   r!   r!   r$   r^      s
   
 
r^   c                   @  rX   )RawFunctionToolInfor   rF   r:   
raw_schemarU   r\   Nr<   r!   r!   r!   r$   ra      r]   ra   _InfoT_P_R)boundc                   @  sN   e Zd ZdZdd ddZed!ddZed"ddZdd#ddZd$ddZ	dS )%_BaseFunctionToolz>Base class for function tool wrappers with descriptor support.NfuncCallable[_P, _R]inforc   instancer   r   r-   c                 C  s"   t | | || _|| _|| _d S r    )	functoolsupdate_wrapper_func_info	_instancer#   rh   rj   rk   r!   r!   r$   r2      s   
z_BaseFunctionTool.__init__r   c                 C  s   | j jS r    )ro   rF   r"   r!   r!   r$   r%         z_BaseFunctionTool.idc                 C  r4   r    )ro   r"   r!   r!   r$   rj      r5   z_BaseFunctionTool.infoobjobjtypetype | Noner   c                 C  sT   |d u r| S | j | j| j|d}t| j}t|j dd  }|j|d|_	|S )N)rk   r   )r_   )
rR   rn   ro   inspect	signaturelistr_   valuesreplace__signature__)r#   rs   rt   
bound_toolsigparamsr!   r!   r$   __get__   s   z_BaseFunctionTool.__get__args_P.argskwargs	_P.kwargsre   c                 O  s4   | j d ur| j| j g|R i |S | j|i |S r    )rp   rn   )r#   r   r   r!   r!   r$   __call__   s   
z_BaseFunctionTool.__call__r    )rh   ri   rj   rc   rk   r   r   r-   r'   )r   rc   )rs   r   rt   ru   r   r   )r   r   r   r   r   re   )
r(   r)   r*   r`   r2   r+   r%   rj   r   r   r!   r!   r!   r$   rg      s    rg   c                      &   e Zd ZdZ	dd fddZ  ZS )FunctionToolz4Wrapper for a function decorated with @function_toolNrh   ri   rj   rY   rk   r   r   r-   c                   "   t  ||| t| d| j d S )N__livekit_tool_inforO   r2   setattrro   rq   rQ   r!   r$   r2         zFunctionTool.__init__r    )rh   ri   rj   rY   rk   r   r   r-   r(   r)   r*   r`   r2   rS   r!   r!   rQ   r$   r          r   c                      r   )RawFunctionToolzDWrapper for a function decorated with @function_tool(raw_schema=...)Nrh   ri   rj   ra   rk   r   r   r-   c                   r   )N__livekit_raw_tool_infor   rq   rQ   r!   r$   r2      r   zRawFunctionTool.__init__r    )rh   ri   rj   ra   rk   r   r   r-   r   r!   r!   rQ   r$   r      r   r   )r\   fri   rb   'RawFunctionDescription | dict[str, Any]r\   r   RawFunctionTool[_P, _R]c                C  r   r    r!   r   rb   r\   r!   r!   r$   function_tool      r   r-   5Callable[[Callable[_P, _R]], RawFunctionTool[_P, _R]]c                C  r   r    r!   r   r!   r!   r$   r      r   rF   r[   r\   rF   rZ   r[   FunctionTool[_P, _R]c                C  r   r    r!   r   rF   r[   r\   r!   r!   r$   r         2Callable[[Callable[_P, _R]], FunctionTool[_P, _R]]c                C  r   r    r!   r   r!   r!   r$   r      r   )rF   r[   rb   r\   Callable[_P, _R] | None.RawFunctionDescription | dict[str, Any] | NoneFunctionTool[_P, _R] | RawFunctionTool[_P, _R] | Callable[[Callable[_P, _R]], FunctionTool[_P, _R]] | Callable[[Callable[_P, _R]], RawFunctionTool[_P, _R]]c                  sR   d
fdd}d fdd	}| d ur!d ur|| S || S d ur'|S |S )Nrh   ri   r   r   c                   sN   d usJ  dstddvrtdtd i  d}t| |S )NrF   z!raw function name cannot be emptyr_   z6raw function description must contain a parameters key)rF   rb   r\   )get
ValueErrorra   r   )rh   rj   )r\   rb   r!   r$   deco_raw  s   

zfunction_tool.<locals>.deco_rawr   c                   s8   ddl m} || }tp| j p|jd}t| |S )Nr   )parse_from_objectr   )docstring_parserr   rY   r(   r[   r   )rh   r   	docstringrj   )r[   r\   rF   r!   r$   	deco_func  s   
z function_tool.<locals>.deco_func)rh   ri   r   r   )rh   ri   r   r   r!   )r   rF   r[   rb   r\   r   r   r!   )r[   r\   rF   rb   r$   r      s
   r   TypeGuard[FunctionTool]c                 C  
   t | tS r    )
isinstancer   r   r!   r!   r$   is_function_tool*  s   
r   c                 C  r4   r    rj   r   r!   r!   r$   get_function_info/     r   TypeGuard[RawFunctionTool]c                 C  r   r    )r   r   r   r!   r!   r$   is_raw_function_tool3  r3   r   c                 C  r4   r    r   r   r!   r!   r$   get_raw_function_info7  r   r   tool%FunctionTool | RawFunctionTool | Nonec                 C  s   t | sdS t| ttfr| S d}t| dr)t| jttfr)| j}|| |j}n%t| dd }r<t|t	r<t| |}nt| dd }rNt|t
rNt| |}|ra|jj}tjd| dd|id |S )	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)callabler   r   r   hasattrr   rR   rj   getattrrY   ra   rF   r   warning)r   resolved_toolwrappedrj   	tool_namer!   r!   r$   _resolve_wrapped_tool;  s6   

r   
cls_or_obj$list[FunctionTool | RawFunctionTool]c                 C  sL   g }t | D ]\}}t|ttfr|| qt| }r#|| q|S r    )rv   
getmembersr   r   r   appendr   )r   methods_membernormalized_toolr!   r!   r$   find_function_toolsb  s   
r   rC   Sequence[Tool | Toolset]	list[str]c                 C  sJ   g }| D ]}t |ttfr||jj qt |tr"|t|j	 q|S )zSGet names of all function and raw function tools in the list, unwrapping tool sets.)
r   r   r   r   rj   rF   r7   extendget_fnc_tool_namesrC   )rC   namesr   r!   r!   r$   r   m  s   
r   c                   @  s   e Zd ZdZd5ddZed6dd	Zed7ddZed8ddZ	ed9ddZ
d:ddZd;ddZd5ddZd6ddZedd d<d%d&Zed'd(d=d,d&Zed>d.d&Zed?d0d&Zd@d4d&Zd'S )AToolContextz-Stateless container for a set of AI functionsrC   r   r   r-   c                 C  s   |  | d S r    )update_toolsr#   rC   r!   r!   r$   r2   |     zToolContext.__init__c                 C  s   | g S r    r!   )clsr!   r!   r$   empty  rr   zToolContext.empty)dict[str, FunctionTool | RawFunctionTool]c                 C  s
   | j  S )zOA copy of all function tools in the tool context, including those in tool sets.)_fnc_tools_mapcopyr"   r!   r!   r$   function_tools  s   
zToolContext.function_toolslist[ProviderTool]c                 C  r4   )zOA copy of all provider tools in the tool context, including those in tool sets.)_provider_toolsr"   r!   r!   r$   provider_tools     zToolContext.provider_toolslist[Toolset]c                 C  r4   )z,A copy of all tool sets in the tool context.)
_tool_setsr"   r!   r!   r$   toolsets  r   zToolContext.toolsetsrB   c                 C  s(   g }| t| j  | | j |S )z,Flatten the tool context to a list of tools.)r   rx   r   ry   r   r   r!   r!   r$   flatten  s   zToolContext.flattenotherobjectboolc                 C  s   t |tsdS | j |j krdS | jD ]}| j| |j| ur% dS qt| jt|jkr2dS dd | jD }dd |jD }||krHdS dd | jD }dd |jD }||kr^dS dS )NFc                 S     h | ]}t |qS r!   r%   .0r   r!   r!   r$   	<setcomp>      z%ToolContext.__eq__.<locals>.<setcomp>c                 S  r   r!   r   r   r!   r!   r$   r     r   c                 S  r   r!   r   r   tool_setr!   r!   r$   r     r   c                 S  r   r!   r   r   r!   r!   r$   r     r   T)r   r   r   keyslenr   r   )r#   r   rF   self_provider_idsother_provider_idsself_tool_set_idsother_tool_set_idsr!   r!   r$   __eq__  s&   

zToolContext.__eq__c                   sN   t |_i _g _g _d fdd t|tD ]} | qd S )Nr   Tool | Toolsetr   r-   c                   s   t | trj|  d S t | ttfr-| jjjv r$t	d| jj | j| jj< d S t | t
rD| jD ]} | q5j|  d S t|  }rP | d S t| rXt	dt	dt|  )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: )r   r,   r   r   r   r   rj   rF   r   r   r7   rC   r   r   r   rI   )r   tr   add_toolr#   r!   r$   r     s"   



z*ToolContext.update_tools.<locals>.add_tool)r   r   r   r-   )rx   _toolsr   r   r   	itertoolschainr   )r#   rC   r   r!   r   r$   r     s   

zToolContext.update_toolsc                 C  s   t | j S r    )r   r   r   r"   r!   r!   r$   r     r   zToolContext.copyT)strictformat%Literal['openai', 'openai.responses']r   list[dict[str, Any]]c                C  r   r    r!   )r#   r   r   r!   r!   r$   parse_function_tools  s   z ToolContext.parse_function_toolsN)tool_behaviorLiteral['google']r   ,_provider_format.google.TOOL_BEHAVIOR | Nonec                C  r   r    r!   )r#   r   r   r!   r!   r$   r     r   Literal['aws']c                 C  r   r    r!   r#   r   r!   r!   r$   r        Literal['anthropic']c                 C  r   r    r!   r   r!   r!   r$   r     r   5Literal['openai', 'google', 'aws', 'anthropic'] | strr   r   c                 K  s   |dkrt jj| fi |S |dkrt jj| fi |S |dkr*t jj| fi |S |dkr8t jj| fi |S |dkrFt jj| fi |S td| )z7Parse the function tools to a provider-specific schema.openaizopenai.responsesgoogle	anthropicawszUnsupported provider format: )r   r  
to_fnc_ctxto_responses_fnc_ctxr  r  r  r   )r#   r   r   r!   r!   r$   r     s   )rC   r   r   r-   )r   r   )r   r   )r   r   )r   r   rD   )r   r   r   r   )r   r   r   r   r   r   )r   r   r   r   r   r   )r   r   r   r   )r   r  r   r   )r   r  r   r   r   r   )r(   r)   r*   r`   r2   classmethodr   r+   r   r   r   r   r   r   r   r   r   r!   r!   r!   r$   r   y  s4    




#r   )r   ri   rb   r   r\   rU   r   r   r    )r   r-   rb   r   r\   rU   r   r   )
r   ri   rF   rZ   r[   rZ   r\   rU   r   r   )
r   r-   rF   rZ   r[   rZ   r\   rU   r   r   )r   r   rF   rZ   r[   rZ   rb   r   r\   rU   r   r   )r   r   r   r   )r   r   r   rY   )r   r   r   r   )r   r   r   ra   )r   r   r   r   )r   r   r   r   )rC   r   r   r   )B
__future__r   rl   rv   r   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   r,   r7   rE   rH   
ToolChoice	ExceptionrM   rT   rU   rY   r^   ra   rc   rd   re   rg   r   r   rV   r   r   r   r   r   r   r   r   r   r!   r!   r!   r$   <module>   s   $	#

	

1




'
