
    wi$              	       4   d dl Z d dlZd dlZd dlZd dlmZ d dlmZmZm	Z	m
Z
mZmZ d dlmc mc mc 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mZ d dlmZmZ d dl m!Z! d d	l"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.  ej^                  e0      Z1e G d d             Z2e G d d             Z3e G d d             Z4e2e3z  e4z  Z5de6e   de7fdZ8de6e   de6e jr                     de:e7ef   fdZ;dede&fdZ<	 d"de$de5de=de%fdZ>d e
e.   de6e$   fd!Z?y)#    N)	dataclass)AnyCallable
ForwardRefSequencecastget_type_hints)	DocstringDocstringParameterDocstringSectionDocstringSectionKindDocstringSectionText)trace)Status
StatusCode)Fieldcreate_model)	FieldInfo)FunctionFunctionCallEntryFunctionResultEntryFunctionTool)RunException)MCPClientProtocol)set_available_attributes)RunOutputEntriesc                   ,    e Zd ZU eed<   eed<   eed<   y)RunFunctionnamecallabletoolN__name__
__module____qualname__str__annotations__r   r        [/home/ubuntu/bibliothek-app/.venv/lib/python3.12/site-packages/mistralai/extra/run/tools.pyr   r   %   s    
I
r)   r   c                   ,    e Zd ZU eed<   eed<   eed<   y)RunCoroutiner   	awaitabler!   Nr"   r(   r)   r*   r,   r,   ,   s    
I
r)   r,   c                   ,    e Zd ZU eed<   eed<   eed<   y)
RunMCPToolr   r!   
mcp_clientN)r#   r$   r%   r&   r'   r   r   r(   r)   r*   r/   r/   3   s    
I
!!r)   r/   docstring_sectionsreturnc                     g }| D ]?  }|j                   t        j                  k(  s!|j                  t	        t
        |             A dj                  d |D              S )zIGiven a list of docstring sections create a description for the function.
c              3   4   K   | ]  }|j                     y wN)value).0texts     r*   	<genexpr>z,_get_function_description.<locals>.<genexpr>C   s     :DTZZ:s   )kindr   r9   appendr   r   join)r1   text_sectionssections      r*   _get_function_descriptionr@   =   sX    02M% F<</444  &:G!DEF 99:M:::r)   params_from_sig
type_hintsc                    t        t        j                  j                  d | D                    }i }i }|D ]  }|j                  ||j
                  <    |D ]G  }|j
                  |vrd||j
                  <   |j                  |j
                        ||j
                  <   I i }|D ]^  }	|	j                  t        j                  j                  ur|	j                  nd}
|	j                  t        j                  j                  ur|	j                  nt        }t        |t              r||	j
                     }t        |
t              r|
}nmd}t!        |d      r_t!        |d      rS|j"                  D ]  }t        |t              s|} n |j$                  d   }t        |t&              r||	j
                     }||
du rt)               }nt)        |
      }||	j
                     |_        t+        t,        |      |f||	j
                  <   a t/        di |j1                         }|j3                  d	d       |j                  d
i       j5                         D ]  }|j3                  d	d        |S )z`Given a list of docstring sections and type annotations create the most accurate tool parametersc              3      K   | ]<  }|j                   t        j                  t        j                  fv r|j                   > y wr6   )r;   r   
parametersother_parametersr7   )r8   r?   s     r*   r:   z+_get_function_parameters.<locals>.<genexpr>M   s>      &
||$//1E1V1VWX MM&
s   AA .N__metadata____args__r   )defaulttitle
properties)_)list	itertoolschainfrom_iterabledescriptionr   getrJ   inspect	Parameterempty
annotationr   
isinstancer&   r   hasattrrH   rI   r   r   r   typer   model_json_schemapopvalues)r1   rA   rB   params_from_docstringsparam_descriptionsparam_annotations	param_docparamfieldsprJ   rW   
field_infometaschemaprops                   r*   _get_function_parametersri   F   sJ    8<%% &
-&
 	
8 + C	-6-B-B9>>*C ! C:://-/uzz*(2uzz(B%**%C 13F '> yy0A0A0G0GG!))SLL0A0A0G0GGALLS 	 j#&#AFF+Jgy) J Jz>2wz:7V '33 D!$	2%)

 (003
 j*5!2166!:J #~"W
"73
!3AFF!;
tZ0*=qvvO'>R ((::<F
JJw

<,335  $ Mr)   funcc                    | j                   }t        j                  |       }|st        j	                  d| d       g }nFt        |d      }|j                  d      }t        |      dk(  rt        j	                  d| d       t        j                  |       }t        |j                  j                               }t        | d	d
d
      }t        t        |t        |      t!        |||      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)r1   rA   rB   )r   rR   rE   strict)function)r#   rT   getdocloggerwarningr
   parselen	signaturerN   rE   r]   r	   r   r   r@   ri   )rj   r   docr1   	docstringsigrA   rB   s           r*   create_tool_callr}      s    ==D ..
Clm	
  c(3	&__e_<!"a'NNTF"op
 

D
!C3>>0023OT4RVWJ12DE/#5 /%
 	
 r)   function_callrun_toolcontinue_on_fn_errorc                   K   t        | j                  t              rt        j                  | j                        n| j                  }t        j                  t              }|j                  t        j                  j                  j                   d| j                         5 }t        j                  t        j                  j                  j                  t        j                  t        j                   j"                  j                  t        j$                  | j&                  t        j(                  t        | j                  t              r| j                  nt        j*                  | j                        t        j,                  | j                  t        j.                  di}t1        ||       	 t        |t2              r |j4                  di |}nit        |t6              r |j8                  di | d{   }n>t        |t:              r.|j<                  j?                  | j                  |       d{   }t        t              r|nt        j*                  |      }|jA                  t        jB                  |       	 ddd       tQ        | jR                        S 7 7 m# tD        $ r}	|jG                  |	       |jI                  tK        tL        jN                  t        |	                   |du r>tQ        | jR                  d| j                   dt        |	             cY d}	~	cddd       S tU        d| j                   d	| j                   d
      |	d}	~	ww xY w# 1 sw Y   xY ww)z7Run the function with arguments of a FunctionCallEntry. rs   NTzError while executing z: )tool_call_idresultzFailed to execute tool z with arguments ''r(   )+rX   	argumentsr&   jsonloadsr   
get_tracerr#   start_as_current_spangen_ai_attributesGenAiOperationNameValuesEXECUTE_TOOLr7   r   GEN_AI_OPERATION_NAMEGEN_AI_PROVIDER_NAMEGenAiProviderNameValues
MISTRAL_AIGEN_AI_TOOL_CALL_IDidGEN_AI_TOOL_CALL_ARGUMENTSdumpsGEN_AI_TOOL_NAMEGEN_AI_TOOL_TYPEr   r   r    r,   r-   r/   r0   execute_toolset_attributeGEN_AI_TOOL_CALL_RESULT	Exceptionrecord_exception
set_statusr   r   ERRORr   r   r   )
r~   r   r   r   tracerspanfunction_call_attributesres
result_stres
             r*   create_function_resultr      s     m--s3 	

=**+$$ 
 h'F		%	%55BBHH
I=K]K]J^_
 $	 335F5_5_5l5l5r5r224E4]4]4h4h4n4n11=3C3C88-1137 ;H:Q:QM334..0B0B..
	$
  	!'?@	(K0'h''4)4Hl3.H..;;;Hj1$//<<!&&	  !+3 4$**S/J0HH*U3$L "//
 ' <
  
	!!!$OOF:#3#3SV<=#t+*!.!;!;3M4F4F3Gr#a&R =$ $D )-*<*<)==N}OfOfNgghi
	5$ $s|   B!M.#D
M".AJ%6J!7>J%5J#6AJ%M.!J%#J%%	M.A6M$M%M")
M.3'MMM""M+'M.output_entriesc                 \    g }| D ]$  }t        |t              s|j                  |       & |S )z:Extract all FunctionCallEntry from a conversation response)rX   r   r<   )r   function_callsentrys      r*   get_function_callsr      s9     N )e./!!%() r)   )F)@rT   rO   r   loggingdataclassesr   typingr   r   r   r   r   r	   >opentelemetry.semconv._incubating.attributes.gen_ai_attributessemconv_incubating
attributesr   griffer
   r   r   r   r   opentelemetryr   opentelemetry.tracer   r   pydanticr   r   pydantic.fieldsr   mistralai.client.modelsr   r   r   r   mistralai.extra.exceptionsr   mistralai.extra.mcp.baser   "mistralai.extra.observability.otelr   mistralai.extra.run.resultr   	getLoggerr#   ru   r   r,   r/   RunToolrN   r&   r@   rU   dictri   r}   boolr   r   r(   r)   r*   <module>r      s|       ! L L Z Z    2 ( %  4 6 8			8	$       " " " 
$z
1;$7G2H ;S ;J-.J'++,J S#XJZ$8 $ $T "'4$44 4 	4n-.	
r)   