
    +jMe                        U d Z ddlZddlZddlZddlZddl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ZddlZddlZddlmZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ej        dk    rddlmZ nej        Zej        rddlmZ  ddl!m"Z# n5eZ ej$        e%d<   eZ#ej$        e%d<   	 ddlmZ  ddl!m"Z# n# e&$ r dZ dZ#Y nw xY wdZ' ej(        d          Z)dej*        dej+        fdZ,dee-ej.        f         de
e-         fdZ/dee-ej.        f         de
e-         fdZ0	 dAdee-ej1        f         de
ej2                 dej2        fdZ3	 	 dBde
ej*                 de4de5e6         fdZ7	 	 	 dCde
ej*                 de
e8e-ef                  de4de8e-eedef         ef         f         fd Z9d!ej:        dej:        fd"Z;d!ee8e-e<f         e5e<         e<f         dee8e-e<f         e5e<         e<f         fd#Z=d$ede4fd%Z>d&ed$ed'e-d(e-def
d)Z?d!ej:        d*edef         dej:        fd+Z@d!ej:        d,edef         defd-ZAd!ej:        d,edef         defd.ZBd/ejC        d0e8e-eedef         ef         f         de5ejD                 fd1ZEd/ejC        d0e8e-eedef         ef         f         de5ejD                 fd2ZF	 dAde
ej*                 de4fd3ZG	 dAde
ej*                 de6fd4ZHde
ej+                 ddfd5ZI	 dAde
ej*                 de4fd6ZJ	 dAde
ej*                 de
ej+                 fd7ZK	 	 dBde
ej*                 de4deLe
ej+                 e8e-ef         f         fd8ZMd9eejN        ejO        f         d:ejC        deejN        ejO        f         fd;ZP	 	 dDd<ee-ejQ        e-         ejR        f         d=e
ejS                 d>e
e-         deLejT        e6e-f         fd?ZU	 dAde
ej*                 de4fd@ZVdS )EzNExtra 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                 R    t          | t                    rt          j        di | S | S )N )
isinstancedictr   GenerateContentConfig)r   s    ]/home/wildlama/visual-decline/.venv/lib/python3.11/site-packages/google/genai/_extra_utils.py%_create_generate_content_config_modelr    <   s2      &00000M    srcc                 P   t          | t                    r|                     d          r| S t          | t                    r-|                     d          r| d         r| d         d         ndS t          | t
          j                  r| j        r| j        r| j        d         ndS dS )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 3cnnW55 3J#t 3!3!3 3 #I83y>!D8#u+,, 3 3 [23;q>>d2	r!   c                    t          | t                    r|                     d          r| S t          | t                    r|                     d          r| d         S t          | t
          j                  r| j        r| j        S dS )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   s     S cnnW55 J#t !8!8 ~#u+,, 1A 	r!   c                 `   |t          j                    }d}|j        st          j                    }d| |_        |j        skt          |           }t          |           }|r(|                    d          r|dd          d|_        n#|r!|pt          j                    }| d| |_        |S )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33K:[::F	 @!#&&N+C00 @.11(;; @%crc*111fkk	 @D7#B#D#Dk*??+??fk	-r!   Fis_agent_platformc                 L   | sg S t          |           }g }|r|j        s|S t          |j                  D ]n\  }}t          |t          j                  s t          |dd          r|                    |           t          |dd          r|s|                    |           o|S )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22 / /keTdEJ'' t,d33 / ''... t]D)) /2C / ''...	##r!   mcp_to_genai_tool_adaptersis_caller_method_async.c                    i }| s|S t          |           }|j        r^|j        D ]V}t          |          rEt          j        |          r'|s%t          j        d|j         d|j         d          |||j        <   W|rm|st          j        d          |                                D ]-\  }}|	                    |          rt          d| d          .|                    |           |S )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 	/G$- G GG G G  
 '+T]# 
4! +
?   388:: 
 
	1			)	$	$ 
CICCC
 
 	

 2333	r!   argsc                 >    d |                                  D             S )z:Converts float values in dict with no decimal to integers.c                 4    i | ]\  }}|t          |          S r   ,convert_number_values_for_function_call_args.0keyvalues      r   
<dictcomp>zEconvert_number_values_for_dict_function_call_args.<locals>.<dictcomp>   s7     
 
 

#u 
7>>
 
 
r!   )rQ   rX   s    r   1convert_number_values_for_dict_function_call_argsrc      s+    
 



 
 
 r!   c                    t          | t                    r#|                                 rt          |           S t          | t                    rd |                                 D             S t          | t                    rd | D             S | S )z2Converts float values with no decimal to integers.c                 4    i | ]\  }}|t          |          S r   r[   r]   s      r   ra   z@convert_number_values_for_function_call_args.<locals>.<dictcomp>   s7       C 	9%@@  r!   c                 ,    g | ]}t          |          S r   r[   )r^   r`   s     r   
<listcomp>z@convert_number_values_for_function_call_args.<locals>.<listcomp>   s.       @E4U;;  r!   )r   float
is_integerintr   rQ   listrb   s    r   r\   r\      s     e !2!2 t99d  **,,    d  IM    
+r!   
annotationc                     	 t          j        |           ot          | t          j                  S # t
          $ r Y dS w xY wNF)rM   isclass
issubclasspydantic	BaseModel	TypeError)rl   s    r   is_annotation_pydantic_modelrt      sV    ?:&& :H&, ,  
   55s   -0 
>>r`   
param_name	func_namec                    t          | t                    rRt          |          rC	  |di | S # t          j        $ r(}t          j        d d d|  d| d| 
          d }~ww xY wt          | t                    r=t          |          t          k    r%t          |          d         fd| D             S t          | t                    rLt          |          t          k    r4t          |          \  }fd| 
                                D             S t          |          t          t          fv rt          |          D ]}t          |          rt          |          t          u s4t          | |          s$t          | t                    r9t          |          r*	 t          | |          c S # t          j        $ r Y w xY wt          j        d d d|  d	| d
	          t          | t                    r|t          u r| S t          | |          s!t          j        d d d|  d| d
	          | S )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   c                 4    g | ]}t          |          S r   convert_if_exist_pydantic_model)r^   itemrv   	item_typeru   s     r   rg   z3convert_if_exist_pydantic_model.<locals>.<listcomp>   s7        	(iYOO  r!   c           	      <    i | ]\  }}|t          |          S r   ry   )r^   kvrv   ru   
value_types      r   ra   z3convert_if_exist_pydantic_model.<locals>.<dictcomp>   s>       Aq 	
*1j*iPP  r!   z- cannot be converted to parameter annotation .r   )r   r   rt   rq   ValidationErrorr    UnknownFunctionCallArgumentErrorrk   r	   r   rQ   r   r   rz   rj   rh   )	r`   rl   ru   rv   erV   argr|   r   s	     ``   @@r   rz   rz      sg    t 	!=j!I!I 	Z  %   #   3-z - -- -- - - - *+- -   t J!7!74!?!?$$Q'I         t J!7!74!?!?Z((MAz     KKMM    
y111
##  C=='__44s## 5%% 5*Fs*K*K 5	0S*i    
 ' 	 	 	
(	 
1	Z 	 		 		 	 	 	 	   s 
e 3 3L	E:	&	& 

1	CZ 	C 	Cy 	C 	CDI	C 	C5?	C 	C 	C  
 
,s'   2 A)#A$$A)'F;;GGfunctionc                     t          j        |          }|j        }i }|j                                        D ])\  }}|| v r t          | |         |j        ||          ||<   *|S N)rM   	signaturerP   
parametersrQ   rz   rl   )rX   r   r   rv   converted_argsru   params          r   convert_argument_from_functionr     s     )))).$/5577  j%T#B
z




	$ $nZ  
r!   function_to_invokec                     t          | |          }	  |di |S # t          $ r*}t          j        d|j         d| d|  d|           d }~ww xY w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   r   s       r   invoke_function_from_dict_argsr   -  s     2$8JKK./////	   

(	6%7%@ 	6 	6 .	6 	6	6 	623	6 	6  s    
A%A		Ac                    K   t          | |          }	  |di | d {V S # t          $ r*}t          j        d|j         d| d|  d|           d }~ww xY wr   r   r   s       r   $invoke_function_from_dict_args_asyncr   ;  s       2$8JKK.##55n555555555	   

(	6%7%@ 	6 	6 .	6 	6	6 	623	6 	6  s   " 
A%AAresponserT   c                 d   g }| j         %t          | j         d         j        t          j                  r| j         d         j        j        | j         d         j        j        D ]}|j        s
|j        j        }||j        j        ||         }t          |j        j                  }	 t          |t                    sdt          ||          i}n(# t          $ r}dt          |          i}Y d}~nd}~ww xY wt          j                            ||          }	|                    |	           |S )6Returns the function response parts from the response.Nr   resulterrornamer   )
candidatesr   contentr   Contentpartsfunction_callr   rX   rc   r   r   r   r%   Partfrom_function_responserA   )
r   rT   func_response_partspartrv   funcrX   func_responser   func_response_parts
             r   get_function_response_partsr   I  sZ   
 %
X(+3U]
C
C &

a
 
(
.
:#A&.4 7 7 $)i		4#5#:#FI&@#
 
	,D"788 8tDDM  	, 	, 	,"CFF+------	,"Z>>] ? 
 
 	""#5666	s   )'C
C6C11C6c                 v  K   g }| j         t          | j         d         j        t          j                  r| j         d         j        j        i| j         d         j        j        D ]P}|j        s|j        j        }|6||         }|j        j        |j        j        ni }t          |          }	 t          |t                    r@|                    t          j        ||                     d{V }|j        rd|i}	nUd|i}	nPt          j        |          rdt!          ||           d{V i}	n#dt#          j        t&          ||           d{V i}	n(# t(          $ r}
dt+          |
          i}	Y d}
~
nd}
~
ww xY wt          j                            ||	          }|                    |           R|S )r   Nr   )r   rX   r   r   r   )r   r   r   r   r   r   r   r   rX   rc   r   	call_toolFunctionCallisErrorrM   rN   r   asyncio	to_threadr   r   r%   r   r   rA   )r   rT   r   r   rv   r   raw_argsrX   mcp_tool_responser   r   r   s               r   !get_function_response_parts_asyncr   l  sB     
 %
X(+3U]
C
C &

a
 
(
.
:#A&.4 %7 %7 $)i		I& !&2 ## 	
 AJJ	,344 &*nn"	===' ' ! ! ! ! ! ! !( <&(9:mm'):;mm*400 	 DT4 P PPPPPPPMM
  12D$! !      M
  	, 	, 	,"CFF+------	,"Z>>] ? 
 
 	""#5666	s   3B%E
E>#E99E>c                    | sdS t          |           }|r[|j        rT|j        j        	 Gt          |j        j                  dk    r*t                              d|j        j         d           dS |j        r|j        j        dS |j        j        r`|j        j        	 Sd|j        j        v rEt          |j        j                  dk    r(t                              d|j        j         d	           |j        j        S )
z6Returns whether automatic function calling is enabled.FNr   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   rj   loggerwarningdisablemodel_fields_setr   rB   s     r   should_disable_afcr     sR    
 56v>>, 

1 
1
F
l5J
K
Kq
P
P
NN	>3H	> 	> 	>   4 
1		0	8	@5 -5

1
F !		0	AB B
l5J
K
Ka
O
O
NN		D 3H		D 		D 		D   
	0	88r!   c                     | st           S 	 t          |           rt          d          t          |           }|j        r|j        j        t           S t          |j        j                  S )NzUautomatic function calling is not enabled, but SDK is trying to get max remote calls.)_DEFAULT_MAX_REMOTE_CALLS_AFCr   rR   r    r   r   rj   r   s     r   get_max_remote_calls_afcr     s|     
 )((J 
	   7v>>,

1)		0	E	M((	\4I	J	JJr!   c                     | r| j         r| j         j        sdS | j        }|r|j        nd}| j         j        j        }|r|st          d          dS dS )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     s      7
 F0*-7Bz))U0O   "4 
	I     r!   c                 T    | sdS t          |           }|j        sdS |j        j         S )NT)r    r   ignore_call_historyr   s     r   should_append_afc_historyr     s=     
 46v>>,		0 44H	HHr!   c                 b   | sdS t          |           }|                    ddi          }|j        |_        |j        rpt          j        |j                  rW|j        t          j        i           |_        |j        j        i |j        _        t          j	        |j        j                   |S )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     
 46v>>, #--gt_-EE(. LJ9,:LMM L%-','8'D'D'D$%-5/1$,#$5$B$JKKK	r!   c                 X  K   i }t          |           }|sd|fS |                    ddi          }|j        rg |_        |j        D ]}t          t	          |t                    rt          ||                                 d{V |          }|j                            |j                   |j        D ]V}|j        rM|j        D ]E}|j	        r<|
                    |j	                  rt          d|j	         d          |||j	        <   FW|j                            |           ||fS )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   $  s      BD,V44-	 ,+++ %///GG .!# . .		%*T;K*L*L	%$9))))))))=N%
 %
 %
! 	 ''(A(GHHH39 	 	J- 
(2(H 	 	$%* -112F2KLL "&27 & & &  
 . ++?+DE	 	 ''----	7	77r!   contentschunkc                     |Y|j         R|j         d         j        }t          j        |           } t	          | t
                    r||                     |           | S )zFAppends the contents of the chunk to the contents list and returns it.Nr   )r   r   t
t_contentsr   rk   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           	         d}|}t          | t          j                  r|t          d          t	          | d          rd| j        vrt          d          |                                 }|                     dt          j	                   |                                 |z
  }|                     |t          j
                   nt          j        |           }|rt          j                            |          st          |  d          t          j                            |          }|t!          j        |          \  }}|t          d          |r?t          |t$                    rt'          j        di |}|}d	|_        d
dd| | d|_        nt'          j        d	d
dd| | d          }t          | t.          t          j        f          r5|j        i |_        t          j                            |           |j        d<   |||fS )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  ^  sD    *)bi   8   tV D	DI		BCCCYY[[FIIav%JIIfbk""""iooG C"'..11 CAAABBB))J)'22li;  
  #T** A+@@.?@@$L!L*"-!(2<09^ L $.&1%,6@?4=
 
	 	 	L sBK()) M#!68g6F6Ft6L6LL23	z9	,,r!   c                 z    | sdS t          |           }|j        sdS |j        D ]}t          |dd          r dS dS )zBChecks whether the configuration contains any MCP server requests.Fr=   NT)r    r>   r@   )r   rB   rE   s      r   has_agent_platform_mcp_serversr    se      U8@@L U"  	}d	+	+ tt5r!   r   rn   )NNF)NN)W__doc__r   rM   r   loggingsystypingr   r   r   r   r   r   r	   r   r   rq   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   boolrk   rj   rF   r   rW   
StringDictrc   objectr\   rt   rz   r   r   r   GenerateContentResponser   r   r   r   r   r   r   r   tupler   ContentListUnionContentListUnionDictr   r   r   HttpOptionsOrDictr   r  r  r   r!   r   <module>r      s|    U T T   				  



  M M M M M M M M M M M M M M M M M M     				                                      , , , , , , w')	 333333'''''''"%FK%%%'6;555555)))))))	   GGG !# 		0	1	1-
    
	sE..	/
c]
 
 
 

	sE..	/
c]
 
 
 
 48 	sE((	)U/0    6 ;?#$ $U67$$ 
#Y$ $ $ $4 ;? 	#(! !U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 1 1 1j ;?49 49U6749	49 49 49 49p ;?K KU67KK K K K(HU=X4Y 	   4 ;?I IU67I	I I I I ;? U67e)*   , ;?#*8 *8U67*8*8 U()#	#$&*8 *8 *8 *8Z
E*E,FFG
(
 5!5#==>
 
 
 
 <@$(F- F-
R[%ry0
1F- 78F- SMF- 			F- F- F- F-T ;? U67	     s   $B1 1	B=<B=