
    Oji                        % S r SSKrSSKrSSKrSSKrSSKrSSKrSSKJrJrJ	r	J
r
JrJrJr  SSKrSSKrSSKrSSKJr  SSKJr  SSKJr  SSKJr  SS	KJr  SS
KJr  \R4                  S:  a  SSKJr  O\R8                  r\R:                  (       a  SSKJr   SSK!J"r#  O/\r \RH                  \%S'   \r#\RH                  \%S'    SSKJr   SSK!J"r#  Sr'\RP                  " S5      r)S\RT                  S\RV                  4S jr,S\\-\R\                  4   S\
\-   4S jr/S\\-\R\                  4   S\
\-   4S jr0 SBS\\-\Rb                  4   S\
\Rd                     S\Rd                  4S jjr3  SCS\
\RT                     S\4S\5\6   4S jjr7   SDS\
\RT                     S\
\8\-\4      S\4S\8\-\\S\4   \4   4   4S jjr9S \Rt                  S\Rt                  4S! jr;S \\8\-\<4   \5\<   \<4   S\\8\-\<4   \5\<   \<4   4S" jr=S#\S\44S$ jr>S%\S#\S&\-S'\-S\4
S( jr?S \Rt                  S)\S\4   S\Rt                  4S* jr@S \Rt                  S+\S\4   S\4S, jrAS \Rt                  S+\S\4   S\4S- jrBS.\R                  S/\8\-\\S\4   \4   4   S\5\R                     4S0 jrES.\R                  S/\8\-\\S\4   \4   4   S\5\R                     4S1 jrF SBS\
\RT                     S\44S2 jjrG SBS\
\RT                     S\64S3 jjrHS\
\RV                     SS4S4 jrI SBS\
\RT                     S\44S5 jjrJ SBS\
\RT                     S\
\RV                     4S6 jjrK  SCS\
\RT                     S\4S\L\
\RV                     \8\-\4   4   4S7 jjrMS8\\R                  \R                  4   S9\R                  S\\R                  \R                  4   4S: jrP  SES;\\-\R                  \-   \R                  4   S<\
\R                     S=\
\-   S\L\R                  \6\-4   4S> jjrU SBS\
\RT                     S\44S? jjrV  SFS\
\RT                     S@\-S\RV                  4SA jjrWg! \& a    Sr Sr# GNf = f)GzNExtra utils depending on types that are shared between sync and async modules.    N)AnyCallableDictOptionalUnionget_args
get_origin   )_common)
_mcp_utils)_transformers)errors)types)McpToGenAiToolAdapter)   
   )	UnionType)ClientSession)ToolMcpClientSessionMcpToolr   zgoogle_genai.modelsconfigreturnc                 \    [        U [        5      (       a  [        R                  " S0 U D6$ U $ )N )
isinstancedictr   GenerateContentConfig)r   s    [/var/www/html/leaf-doctor-v1/venv/lib/python3.13/site-packages/google/genai/_extra_utils.py%_create_generate_content_config_modelr    <   s*     &&000M    srcc                    [        U [        5      (       a  U R                  S5      (       a  U $ [        U [        5      (       a*  U R	                  S5      (       a  U S   (       a  U S   S   $ S$ [        U [
        R                  5      (       a3  U R                  (       a"  U R                  (       a  U R                  S   $ S$ g)z9Extracts the first GCS URI from the source, if available.zgs://gcs_urir   N)r   str
startswithr   getr   BatchJobSourcer$   r"   s    r   _get_gcs_urir*   E   s     ScnnW55J#t!3!3 #I3y>!8D8#u++,, [[3;;q>2d2	r!   c                 6   [        U [        5      (       a  U R                  S5      (       a  U $ [        U [        5      (       a  U R	                  S5      (       a  U S   $ [        U [
        R                  5      (       a  U R                  (       a  U R                  $ g)z8Extracts the BigQuery URI from the source, if available.zbq://bigquery_uriN)r   r%   r&   r   r'   r   r(   r,   r)   s    r   _get_bigquery_urir-   R   st     ScnnW55J#t!8!8~#u++,,1A1A	r!   c                    Uc  [         R                  " 5       nSnUR                  (       d  [        R                  " 5       nSU 3Ul        UR
                  (       ds  [        U 5      n[        U 5      nU(       a%  UR                  S5      (       a  USS  S3Ul        U$ U(       a*  U=(       d    [        R                  " 5       nU SU 3Ul        U$ )zBFormats the destination uri based on the source uri for Vertex AI.Ngenai_batch_job_z.jsonliz/dest_dest_)	r   CreateBatchJobConfigdisplay_namer   timestamped_unique_namedestr*   r-   endswith)r"   r   unique_namegcs_source_uribigquery_source_uris        r   format_destinationr9   _   s    
 ^'')F+			113K,[M:F	!#&N+C0.11(;;%cr*+51fk
 
-	 
D7#B#B#Dk*+6+?fk	-r!   is_agent_platformc                    U (       d  / $ [        U 5      n/ nU(       a  UR                  (       d  U$ [        UR                  5       Hw  u  pE[        U[        R
                  5      (       d  M&  [        USS5      (       a  UR                  U5        [        USS5      (       d  M]  U(       a  Mf  UR                  U5        My     U$ )z9Checks if the config contains any AFC incompatible tools.function_declarationsNmcp_servers)r    tools	enumerater   r   r   getattrappend)r   r:   config_modelincompatible_tools_indexesindextools         r   "find_afc_incompatible_tool_indexesrF   y   s    
 
I6v>,*,	\//%%|112kedEJJ''t,d33 ''. t]D))2C2C ''. 3 
$#r!   mcp_to_genai_tool_adaptersis_caller_method_async.c                 F   0 nU (       d  U$ [        U 5      nUR                  (       a  UR                   Hv  n[        U5      (       d  M  [        R                  " U5      (       a8  U(       d1  [
        R                  " SUR                   SUR                   S35      eXSUR                  '   Mx     U(       al  U(       d  [
        R                  " S5      eUR                  5        H*  u  pgUR                  U5      (       d  M  [        SU S35      e   UR                  U5        U$ )z'Returns a function map from the config.z	Function zh is a coroutine function, which is not supported for automatic function calling. Please manually invoke z to get the function response.z3MCP tools are not supported in synchronous methods.Tool $ is already defined for the request.)r    r>   callableinspectiscoroutinefunctionr   UnsupportedFunctionError__name__itemsr'   
ValueErrorupdate)r   rG   rH   function_maprB   rE   	tool_name_s           r   get_function_maprW      s    OQ,	6v>,""	$&&t,,5K//$-- )'EG 
 '+T]]# #  !++
?  388:				)	$	$I;BC
 	
 ;
 23	r!   argsc                 l    U R                  5        VVs0 s H  u  pU[        U5      _M     snn$ s  snnf )z:Converts float values in dict with no decimal to integers.)rQ   ,convert_number_values_for_function_call_argsrX   keyvalues      r   1convert_number_values_for_dict_function_call_argsr^      s<     


$*# 
7>>$
  
s   0c                 p   [        U [        5      (       a   U R                  5       (       a  [        U 5      $ [        U [        5      (       a/  U R                  5        VVs0 s H  u  pU[        U5      _M     snn$ [        U [        5      (       a  U  Vs/ s H  n[        U5      PM     sn$ U $ s  snnf s  snf )z2Converts float values with no decimal to integers.)r   float
is_integerintr   rQ   rZ   listr[   s      r   rZ   rZ      s     e!2!2t9d **,&JC 	9%@@&  dIMIM4U;  
+
s   B-B3
annotationc                      [         R                  " U 5      =(       a    [        U [        R                  5      $ ! [
         a     gf = fNF)rM   isclass
issubclasspydantic	BaseModel	TypeError)rd   s    r   is_annotation_pydantic_modelrl      sC    ??:& :H&&,  
 s   69 
AAr]   
param_name	func_namec                    [        U [        5      (       a  [        U5      (       a	   U" S	0 U D6$ [        U [        5      (       a>  [        U5      [        :X  a+  [        U5      S   nU  Vs/ s H  n[        XeX#5      PM     sn$ [        U [        5      (       aP  [        U5      [        :X  a=  [        U5      u  pxU R                  5        V	V
s0 s H  u  pU	[        XX#5      _M     sn
n	$ [        U5      [        [        4;   a  [        U5       Hk  n[        U5      (       a  [        U5      [        L d9  [        X5      (       d)  [        U [        5      (       d  ML  [        U5      (       d  M^   [        XX#5      s  $    [
        R                  " SU SU SU  SU S3	5      e[        U [        5      (       a  U[        L a  U $ [        X5      (       d#  [
        R                  " SU SU SU  SU S3	5      eU $ ! [        R                   a*  n[
        R                  " SU SU SU  SU SU 3
5      eS nAff = fs  snf s  sn
n	f ! [        R                   a     GMK  f = f)
NzFailed to parse parameter z for function z> from function call part because function call argument value z- is not compatible with parameter annotation z, due to error r   z- cannot be converted to parameter annotation .r   )r   r   rl   ri   ValidationErrorr    UnknownFunctionCallArgumentErrorrc   r	   r   convert_if_exist_pydantic_modelrQ   r   r   rb   r`   )r]   rd   rm   rn   e	item_typeitemrV   
value_typekvargs               r   rs   rs      s}    t!=j!I!I %   tJ!74!?$Q'I D 	(O  tJ!74!?Z(MA KKM!DA 	
*1*PP!  
y11
#C==Z_4##%%*Fs*K*K	0*  $ 
1
1
$ZL 1;  <q	  s
e 3L	E	&	&

1
1
$ZLyk JDDI7 K55?L	C 
 
,k ## 33&zl 3[ 7 \- & '' 	
	s5   G2 )H3
H8*H>2H0%H++H0>IIfunctionc                     [         R                  " U5      nUR                  n0 nUR                  R	                  5        H(  u  pVXP;   d  M  [        X   UR                  UU5      XE'   M*     U$ N)rM   	signaturerP   
parametersrQ   rs   rd   )rX   r{   r~   rn   converted_argsrm   params          r   convert_argument_from_functionr     sp     ))).$//557j#B






	$n  8 
r!   function_to_invokec                     [        X5      n U" S0 UD6$ ! [         a1  n[        R                  " SUR                   SU SU  SU 35      eS nAff = fNzFailed to invoke function z with converted arguments z, from model returned function call argument z because of error r   r   	Exceptionr   FunctionInvocationErrorrP   rX   r   r   rt   s       r   invoke_function_from_dict_argsr   -  su     2$K.///	 

(
(
$%7%@%@$A B  ./ 01!	6 s    
A,AAc                    #    [        X5      n U" S0 UD6I S h  vN $  N! [         a1  n[        R                  " SUR                   SU SU  SU 35      eS nAff = f7fr   r   r   s       r   $invoke_function_from_dict_args_asyncr   ;  s}      2$K.#5n5555	 

(
(
$%7%@%@$A B  ./ 01!	6 s0   A! ! A! 
A,AAAresponserT   c                    / nU R                   GbA  [        U R                   S   R                  [        R                  5      (       Ga
  U R                   S   R                  R
                  b  U R                   S   R                  R
                   H  nUR                  (       d  M  UR                  R                  nUc  M1  UR                  R                  c  MJ  X   n[        UR                  R                  5      n [        U[        5      (       d  S[        Xe5      0n[        R                  R                  UWS9n	UR!                  U	5        M     U$ ! [         a  nS[        U5      0n SnANQSnAff = f)6Returns the function response parts from the response.Nr   resulterrornamer   )
candidatesr   contentr   Contentpartsfunction_callr   rX   r^   r   r   r   r%   Partfrom_function_responserA   )
r   rT   func_response_partspartrn   funcrX   func_responsert   func_response_parts
             r   get_function_response_partsr   I  sJ   
 %
X((+33U]]
C
C


a
 
(
(
.
.
:##A&..44$$))i		4#5#5#:#:#F&@##
	,D"7888DM
 #ZZ>>] ? 
 	""#56) 5* 
  	,"CF+-	,s   ="E
E5E00E5c                 ,  #    / nU R                   Gb  [        U R                   S   R                  [        R                  5      (       Ga  U R                   S   R                  R
                  Gby  U R                   S   R                  R
                   GHQ  nUR                  (       d  M  UR                  R                  nUc  M2  X   nUR                  R                  b  UR                  R                  O0 n[        U5      n [        U[        5      (       aF  UR                  [        R                  " XGS95      I Sh  vN nUR                  (       a  SU0n	O\SU0n	OW[        R                  " U5      (       a  S[!        Xu5      I Sh  vN 0n	O&S["        R$                  " [&        Xu5      I Sh  vN 0n	 [        R,                  R/                  XIS9nUR1                  U5        GMT     U$  N Nc N?! [(         a  n
S[+        U
5      0n	 Sn
A
NWSn
A
ff = f7f)r   Nr   )r   rX   r   r   r   )r   r   r   r   r   r   r   r   rX   r^   r   	call_toolFunctionCallisErrorrM   rN   r   asyncio	to_threadr   r   r%   r   r   rA   )r   rT   r   r   rn   r   raw_argsrX   mcp_tool_responser   rt   r   s               r   !get_function_response_parts_asyncr   l  s    
 %
X((+33U]]
C
C


a
 
(
(
.
.
:##A&..44$$))i		& !!&&2 ## 	
 AJ	,344&*nn""	=' ! !((&(9:m'):;m**400 DT PPM
  1 12D! M #ZZ>> ? 
 	""#56K 5L 
/! Q  	,"CF+-	,s   B>H>H;G0>G*?G0HG0H*G0G,	G0HG0.G./G046H*G0,G0.G00
H:HHHHc                    U (       d  g[        U 5      nU(       az  UR                  (       ai  UR                  R                   bQ  [        UR                  R                  5      S::  a.  [        R                  SUR                  R                   S35        gUR                  (       a  UR                  R                  c  gUR                  R                  (       a  UR                  R                   bj  SUR                  R                  ;   aP  [        UR                  R                  5      S:  a-  [        R                  SUR                  R                   S35        UR                  R                  $ )	z6Returns whether automatic function calling is enabled.Fr   z6max_remote_calls in automatic_function_calling_config zu is less than or equal to 0. Disabling automatic function calling. Please set max_remote_calls to a positive integer.Tmaximum_remote_callsz`automatic_function_calling.disable` is set to `True`. And `automatic_function_calling.maximum_remote_calls` is a positive number a9  . Disabling automatic function calling. If you want to enable automatic function calling, please set `automatic_function_calling.disable` to `False` or leave it unset, and set `automatic_function_calling.maximum_remote_calls` to a positive integer or leave `automatic_function_calling.maximum_remote_calls` unset.)r    automatic_function_callingr   rb   loggerwarningdisablemodel_fields_setr   rB   s     r   should_disable_afcr     sF    
6v>, 

1
1

1
1
F
F
l55JJ
Kq
P
NN	33HHI J>	>  
1
1		0	0	8	8	@ --55

1
1
F
F !		0	0	A	AB
l55JJ
Ka
O
NN	 33HHI JD		D 
	0	0	8	88r!   c                    U (       d  [         $  [        U 5      (       a  [        S5      e[        U 5      nUR                  (       a  UR                  R
                  c  [         $ [        UR                  R
                  5      $ )NzUautomatic function calling is not enabled, but SDK is trying to get max remote calls.)_DEFAULT_MAX_REMOTE_CALLS_AFCr   rR   r    r   r   rb   r   s     r   get_max_remote_calls_afcr     sq     
((J
	  7v>,

1
1		0	0	E	E	M((	\44II	JJr!   c                 "   U (       a,  U R                   (       a  U R                   R                  (       d  gU R                  nU(       a  UR                  OSnU R                   R                  R                  nU(       a  U(       d  [        S5      egg)z9Raises an error if the config is not compatible with AFC.NFav  Running in streaming mode with stream_function_call_arguments enabled, this feature is not compatible with automatic function calling (AFC). Please set config.automatic_function_calling.disable to True to disable AFC or leave config.tool_config. function_calling_config.stream_function_call_arguments to be empty or set to False to disable streaming function call arguments.)tool_configfunction_calling_configr   r   stream_function_call_argumentsrR   )r   
afc_configdisable_afc_configstream_function_calls       r   'raise_error_for_afc_incompatible_configr     sz     77
00*-7z))U00OO  "4
	I  #5r!   c                     U (       d  g[        U 5      nUR                  (       d  gUR                  R                  (       + $ )NT)r    r   ignore_call_historyr   s     r   should_append_afc_historyr     s5     
6v>,		0	044HH	HHr!   c                    U (       d  g[        U 5      nUR                  SS0S9nUR                  Ul        UR                  (       a  [        R                  " UR                  5      (       ax  UR
                  c  [        R                  " 0 S9Ul        UR
                  R                  c  0 UR
                  l        [        R                  " UR
                  R                  5        U$ )zVReturns a parsed config with an appended MCP header if MCP tools or sessions are used.Nr>   rS   )headers)
r    
model_copyr>   r   has_mcp_tool_usagehttp_optionsr   HttpOptionsr   set_mcp_usage_header)r   rB   config_model_copys      r   parse_config_for_mcp_usager     s     
6v>, #--gt_-E(..J99,:L:LMM%%-','8'8'D$%%--5/1$$,##$5$B$B$J$JK	r!   c                   #    0 n[        U 5      nU(       d  SU4$ UR                  SS0S9nUR                  (       Ga-  / Ul        UR                   GH  n[        b  [	        U[        5      (       a  [        XUR                  5       I Sh  vN US9nUR                  R                  UR                  5        UR                   H  nUR                  (       d  M  UR                   H]  nUR                  (       d  M  UR                  UR                  5      (       a  [        SUR                   S35      eUX(R                  '   M_     M     M  UR                  R                  U5        GM     XB4$  N7f)zReturns a parsed config with MCP sessions converted to GenAI tools.

Also returns a map of MCP tools to GenAI tool adapters to be used for AFC.
Nr>   r   )r:   rJ   rK   )r   r   r>   r   r   r   
list_toolsextendr<   r   r'   rR   rA   )	r   r:   rG   parsed_configparsed_config_copyrE   mcp_to_genai_tool_adapter
genai_toolfunction_declarations	            r   parse_config_for_mcp_sessionsr   $  sc     BD,V4-	+++ %///G!##		%*T;K*L*L$9))=N%
! 	  ''(A(G(GH399J---(2(H(H$%***-112F2K2KLL"2778 9& & 
 . ++D+DE )I : 	  ''-) $, 
	77' *s    BE0	E.
A
E0E0:A5E0contentschunkc                     Ube  UR                   bX  UR                   S   R                  n[        R                  " U 5      n [	        U [
        5      (       a  Ub  U R                  U5        U $ )zFAppends the contents of the chunk to the contents list and returns it.r   )r   r   t
t_contentsr   rc   rA   )r   r   chunk_contents      r   append_chunk_contentsr   Q  s\    
 5++7$$Q'//M||H%H(D!!m&?oom$	/r!   fileuser_http_optionsuser_mime_typec           	      T   SnUn[        U [        R                  5      (       a  Uc  [        S5      e[	        U S5      (       a  SU R
                  ;  a  [        S5      eU R                  5       nU R                  S[        R                  5        U R                  5       U-
  nU R                  U[        R                  5        O[        R                  " U 5      nU(       a$  [        R                  R                  U5      (       d  [        U  S35      e[        R                  R                  U5      nUc  [         R"                  " U5      u  pGUc  [        S5      eU(       aD  [        U[$        5      (       a  [&        R(                  " S0 UD6nUnS	Ul        S
SSU U S.Ul        O[&        R(                  " S	S
SSU U S.S9n[        U [.        [        R0                  45      (       a@  UR,                  c  0 Ul        [        R                  R3                  U 5      UR,                  S'   XU4$ )a%  Prepares the HTTP options, file bytes size and mime type for a resumable upload.

This function inspects a file (from a path or an in-memory object) to
determine its size and MIME type. It then constructs the necessary HTTP
headers and options required to initiate a resumable upload session.
NzfUnknown mime type: Could not determine the mimetype for your file
 please set the `mime_type` argumentmodebz'The file must be opened in binary mode.r   z is not a valid file path.ziUnknown mime type: Could not determine the mimetype for your file
    please set the `mime_type` argument zapplication/json	resumablestart)zContent-TypezX-Goog-Upload-ProtocolzX-Goog-Upload-Commandz#X-Goog-Upload-Header-Content-Lengthz!X-Goog-Upload-Header-Content-Type)api_versionr   zX-Goog-Upload-File-Namer   )r   ioIOBaserR   hasattrr   tellseekosSEEK_ENDSEEK_SETfspathpathisfileFileNotFoundErrorgetsize	mimetypes
guess_typer   r   r   r   r   r%   PathLikebasename)	r   r   r   
size_bytes	mime_typeoffsetfs_pathrV   r   s	            r   prepare_resumable_uploadr   ^  s    *)bii  8  tV	DII	BCCYY[FIIav%JIIfbkk"iioG"''..11&@ABB)J))'2li; 
 #T**++@.?@$L!L*"-!(2<09{L $$.&1%,6@\4=;
	L sBKK())#!68gg6F6Ft6LL23	9	,,r!   c                     U (       d  g[        U 5      nUR                  (       d  gUR                   H  n[        USS5      (       d  M    g   g)zBChecks whether the configuration contains any MCP server requests.Fr=   NT)r    r>   r@   )r   rB   rE   s      r   has_agent_platform_mcp_serversr     sG     8@L""	}d	+	+ # r!   usagec                    SU 3nU (       d  [         R                  " 5       nO.[        U [        5      (       a  [         R                  " S0 U D6nOU nUR                  (       d  [         R
                  " 5       Ul        UR                  R                  =(       d    0 nSU;   a  X$S   ;  a  US==   SU 3-  ss'   OX$S'   SU;   a  X$S   ;  a  US==   SU 3-  ss'   OX$S'   XCR                  l        U$ )zSets the afc version label.zgoogle-genai-sdk/z
user-agent zx-goog-api-clientr   )r   r   r   r   r   r   r   )r   r   usage_headerrB   existing_headerss        r   get_usage_headerr    s    
 %UG,,	..0L&$..88LL		"	" % 1 1 3L!..66<"%%L99|$!L>(::$%1\",,,?@@*+<./AA+,8()&6#	r!   r}   rf   )NNF)NN)Nafc)X__doc__r   rM   r   loggingsystypingr   r   r   r   r   r   r	   r   r   ri   r   r   r   r   r   r   r   	_adaptersr   version_infor   _UnionGenericAliasTYPE_CHECKINGmcpr   r   	mcp.typesr   r   Type__annotations__ImportErrorr   	getLoggerr   GenerateContentConfigOrDictr   r    r%   BatchJobSourceOrDictr*   r-   r(   r1   r9   boolrc   rb   rF   r   rW   
StringDictr^   objectrZ   rl   rs   r   r   r   GenerateContentResponser   r   r   r   r   r   r   r   tupler   ContentListUnionContentListUnionDictr   r   r   HttpOptionsOrDictr   r   r   r  r   r!   r   <module>r     s?    U   	  
  M M M  	        , w'')	3'"%FKK%'6;;5)
 !# 			0	1--
  
	sE...	/
c]

	sE...	/
c]
 48	sE(((	)U//0 6 ;?#$U667$$ 
#Y$4 ;? 	#(!U667! (S''(!!
 !! 
#uXc3h')>>?
?@!H



S&[!4<7
8
4Vd6lF23$	S 	T 	;;;-0;=@;;|


(0c(:"


2:382D


2:382D ++ sE(38"46K"KLLM  
%** F1++1sE(38"46K"KLLM1 
%**1j ;?49U66749	49p ;?KU667KK(HU=X=X4Y 	4 ;?IU667I	I ;?U667e))*, ;?#*8U667*8*8 U(()#	#$&*8Z
E**E,F,FFG
((
 5!!5#=#==>
 <@$(F-
R[[%ryy0
1F- 7 78F- SMF- 			F-T ;?U667	" ;?U667   I 
 Gs   Q! !
Q/.Q/