o
    i                  	   @   s  d dl Z d dlZd dlmZ d dlZd dlmZmZ d dlm	Z	 d dl
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 d d	lmZ d d
lm Z m!Z!m"Z"m#Z# e$e%Z&eG dd dZ'eG dd dZ(eG dd dZ)ee'e(e)f Z*de+e de,fddZ-de+e de+ej. de/e,ef fddZ0dede!fddZ1	d%de#de*de2de fd d!Z3d"ee de+e# fd#d$Z4dS )&    N)	dataclass)Fieldcreate_model)	FieldInfo)castCallableSequenceAny
ForwardRefget_type_hintsUnion)	DocstringDocstringSectionKindDocstringSectionTextDocstringParameterDocstringSection)RunException)MCPClientProtocol)RunOutputEntries)FunctionResultEntryFunctionToolFunctionFunctionCallEntryc                   @   &   e Zd ZU eed< eed< eed< dS )RunFunctionnamecallabletoolN__name__
__module____qualname__str__annotations__r   r    r$   r$   _/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/mistralai/extra/run/tools.pyr   !      
 r   c                   @   r   )RunCoroutiner   	awaitabler   Nr   r$   r$   r$   r%   r'   (   r&   r'   c                   @   r   )
RunMCPToolr   r   
mcp_clientN)r   r    r!   r"   r#   r   r   r$   r$   r$   r%   r)   /   r&   r)   docstring_sectionsreturnc                 C   s>   g }| D ]}|j tjkr|tt| qddd |D S )zIGiven a list of docstring sections create a description for the function.
c                 s   s    | ]}|j V  qd S N)value).0textr$   r$   r%   	<genexpr>?   s    z,_get_function_description.<locals>.<genexpr>)kindr   r1   appendr   r   join)r+   text_sectionssectionr$   r$   r%   _get_function_description9   s   r8   params_from_sig
type_hintsc                 C   s  t tjdd | D }i }i }|D ]}|j||j< q|D ]}|j|vr*d||j< ||j||j< qi }|D ]w}	|	jtj	j
urD|	jnd}
|	jtj	j
urP|	jnt}t|tr\||	j }t|
trd|
}n*d}t|drt|dr|jD ]}t|tr~|} nqs|jd }t|tr||	j }|du r|
du rt }nt|
d	}||	j |_tt||f||	j< q8tdi | }|dd |di  D ]}|dd q|S )z`Given a list of docstring sections and type annotations create the most accurate tool parametersc                 s   s(    | ]}|j tjtjfv r|jV  qd S r.   )r3   r   
parametersother_parametersr/   )r0   r7   r$   r$   r%   r2   I   s    
z+_get_function_parameters.<locals>.<genexpr> .N__metadata____args__r   )default_title
properties)rA   )list	itertoolschainfrom_iterabledescriptionr   getr@   inspect	Parameterempty
annotationr	   
isinstancer"   r   hasattrr>   r?   r
   r   r   typer   model_json_schemapopvalues)r+   r9   r:   params_from_docstringsparam_descriptionsparam_annotations	param_docparamfieldspr@   rM   
field_infometaschemapropr$   r$   r%   _get_function_parametersB   sV   











r_   funcc              
   C   s   | j }t| }|std| d g }nt|dd}|jdd}t|dkr1td| d t| }t	|j
 }t| d	d
d
d}tdt|t|t|||dd	ddS )zGParse a function docstring / type annotations to create a FunctionTool.z
Function 'zN' without a docstring is being parsed, add docstring for more accurate result.google)parserF)warningsr   zM' has no relevant docstring sections, add docstring for more accurate result.TN)include_extraslocalnsglobalnsfunction)r+   r9   r:   )r   rH   r;   strict)rP   rg   )r   rJ   getdocloggerwarningr   parselen	signaturerD   r;   rS   r   r   r   r8   r_   )r`   r   docr+   	docstringsigr9   r:   r$   r$   r%   create_tool_call   s:   



rr   Ffunction_callrun_toolcontinue_on_fn_errorc              
      s  t | jtrt| jn| j}z1t |tr|jd	i |}n!t |tr0|jd	i |I dH }nt |t	r@|j
| j|I dH }W n4 tyu } z(|du rct| jd| j dt| dW  Y d}~S td| j d| j d|d}~ww t| jt |tr|dS t|dS )
z7Run the function with arguments of a FunctionCallEntry.NTzError while executing z: )tool_call_idresultzFailed to execute tool z with arguments ''r$   )rN   	argumentsr"   jsonloadsr   r   r'   r(   r)   r*   execute_toolr   	Exceptionr   rv   r   dumps)rs   rt   ru   ry   reser$   r$   r%   create_function_result   s@   




r   output_entriesc                 C   s&   g }| D ]}t |tr|| q|S )z:Extract all FunctionCallEntry from a conversation response)rN   r   r4   )r   function_callsentryr$   r$   r%   get_function_calls   s   

r   )F)5rE   loggingdataclassesr   rJ   pydanticr   r   pydantic.fieldsr   rz   typingr   r   r   r	   r
   r   r   griffer   r   r   r   r   mistralai.extra.exceptionsr   mistralai.extra.mcp.baser   mistralai.extra.run.resultr   mistralai.modelsr   r   r   r   	getLoggerr   rj   r   r'   r)   RunToolrD   r"   r8   rK   dictr_   rr   boolr   r   r$   r$   r$   r%   <module>   sZ    $
	

M+
"