
    [ThO                        U d 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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j0                  Zej2                  rddlmZ ddlmZ n/eZej@                  e!d<   eZej@                  e!d<   	 ddlmZ ddlmZ dZ# ejH                  d      Z%dejL                  dejN                  fdZ(de	e)ejT                  f   dee)   fdZ+de	e)ejT                  f   dee)   fdZ,	 d8de	e)ejZ                  f   deej\                     dej\                  fdZ/	 	 	 d9deejL                     dee0e)ef      de1de0e)e	edef   ef   f   fdZ2dejf                  dejf                  fdZ4de	e0e)e5f   e6e5   e5f   de	e0e)e5f   e6e5   e5f   fd Z7d!ede1fd"Z8d#ed!ed$e)d%e)def
d&Z9dejf                  d'edef   dejf                  fd(Z:dejf                  d)edef   defd*Z;dejf                  d)edef   defd+Z<d,ejz                  d-e0e)e	edef   ef   f   de6ej|                     fd.Z?d,ejz                  d-e0e)e	edef   ef   f   de6ej|                     fd/Z@	 d8deejL                     de1fd0ZA	 d8deejL                     deBfd1ZC	 d8deejL                     de1fd2ZD	 d8deejL                     deejN                     fd3ZE	 d8deejL                     deFeejN                     e0e)ef   f   fd4ZGd5e	ej                  ej                  f   d6ejz                  ddfd7ZJy# e"$ r dZdZY w xY w):zNExtra 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                 P    t        | t              rt        j                  di | S | S )N )
isinstancedictr   GenerateContentConfig)r   s    ^/home/www/backend.miabetepe.com/venv/lib/python3.12/site-packages/google/genai/_extra_utils.py%_create_generate_content_config_modelr    9   s'     &&000M    srcc                 0   t        | t              r| j                  d      r| S t        | t              r | j	                  d      r| d   r| d   d   S dS t        | t
        j                        r)| j                  r| j                  r| j                  d   S dS y)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*   B   s     ScnnW5J#t!3 #I3y>!8D8#u++, [[3;;q>2d2	r!   c                     t        | t              r| j                  d      r| S t        | t              r| j	                  d      r| d   S t        | t
        j                        r| j                  r| j                  S y)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-   O   se     ScnnW5J#t!8~#u++,1A1A	r!   c                 ^   |t        j                         }d}|j                  st        j                         }d| |_        |j
                  s^t        |       }t        |       }|r |j                  d      r|dd  d|_        |S |r$|xs 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13K,[M:F	!#&N+C0.11(;%cr*+51fk
 
-	 
D7#B#B#Dk*+6+?fk	-r!   mcp_to_genai_tool_adaptersis_caller_method_async.c                    i }| s|S t        |       }|j                  rs|j                  D ]d  }t        |      st        j                  |      r2|s0t        j                  d|j                   d|j                   d      |||j                  <   f |r`|st        j                  d      |j                         D ]%  \  }}|j                  |      st        d| d       |j                  |       |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    toolscallableinspectiscoroutinefunctionr   UnsupportedFunctionError__name__itemsr'   
ValueErrorupdate)r   r:   r;   function_mapconfig_modeltool	tool_name_s           r   get_function_maprM   v   s    OQ,	6v>,"" +	$&&t,5K//$-- )'EG 
 '+T]]#+  !++
?  388: 
	1			)	$I;BC
 	


 23	r!   argsc                 h    | j                         D ci c]  \  }}|t        |       c}}S c c}}w )z:Converts float values in dict with no decimal to integers.)rE   ,convert_number_values_for_function_call_argsrN   keyvalues      r   1convert_number_values_for_dict_function_call_argsrT      s9     



#u 
7>>
  
s   .c                 >   t        | t              r| j                         rt        |       S t        | t              r-| j                         D ci c]  \  }}|t        |       c}}S t        | t              r| D cg c]  }t        |       c}S | S c c}}w c c}w )z2Converts float values with no decimal to integers.)r   float
is_integerintr   rE   rP   listrQ   s      r   rP   rP      s     e!2t9d **,C 	9%@@  dIM@E4U;  
+
s   B=B
annotationc                     	 t        j                  |       xr t        | t        j                        S # t
        $ r Y yw xY w)NF)rA   isclass
issubclasspydantic	BaseModel	TypeError)rZ   s    r   is_annotation_pydantic_modelra      sC    ??:& :H&&,  
 s   03 	??rS   
param_name	func_namec                 :   t        | t              rt        |      r		  |d	i | S t        | t              r<t        |      t        k(  r*t        |      d   }| D cg c]  }t        ||||       c}S t        | t              rPt        |      t        k(  r>t        |      \  }}| j                         D 	
ci c]  \  }	}
|	t        |
|||       c}
}	S t        |      t        t        fv rt        |      D ]W  }t        |      rt        |      t        u s)t        | |      st        | t              s<t        |      sH	 t        | |||      c S  t        j                  d| d| d|  d| d	      t        | t              r
|t        u r| S t        | |      s"t        j                  d| d| d|  d| d	      | S # t        j                  $ r)}t        j                  d| d| d|  d| d| 
      d }~ww xY wc c}w c c}
}	w # t        j                  $ r Y &w xY w)
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   ra   r^   ValidationErrorr    UnknownFunctionCallArgumentErrorrY   r	   r   convert_if_exist_pydantic_modelrE   r   r   rX   rV   )rS   rZ   rb   rc   e	item_typeitemrL   
value_typekvargs               r   rh   rh      so    t!=j!I %   tJ!74!?$Q'I  	(iYO  tJ!74!?Z(MAz KKMAq 	
*1j*iPP  
y11
# C=Z_4s#%*Fs*K	0S*i  
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   F9 G84G=<H9G5$G00G5HHfunctionc                     t        j                  |      }|j                  }i }|j                  j	                         D ](  \  }}|| v st        | |   |j                  ||      ||<   * |S N)rA   	signaturerD   
parametersrE   rh   rZ   )rN   rp   rs   rc   converted_argsrb   params          r   convert_argument_from_functionrw      s}     ))).$//557 j%T#B
z





	$nZ  
r!   function_to_invokec                     t        | |      }	  |di |S # t        $ r0}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   rw   	Exceptionr   FunctionInvocationErrorrD   rN   rx   ru   ri   s       r   invoke_function_from_dict_argsr     sx     2$8JK.///	 

(
(
$%7%@%@$A B  ./ 01!	6 s    	A+A

Ac                    K   t        | |      }	  |di | d {   S 7 # t        $ r0}t        j                  d|j                   d| d|  d|       d }~ww xY wwrz   r{   r~   s       r   $invoke_function_from_dict_args_asyncr     s      2$8JK.#5n5555	 

(
(
$%7%@%@$A B  ./ 01!	6 s0   A"  " A" 	A+AAAresponserH   c                    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}t        j                  j                  |      }	|j!                  |	        |S # t        $ r}dt        |      i}Y d}~Qd}~ww xY w)6Returns the function response parts from the response.Nr   resulterrornamer   )
candidatesr   contentr   Contentpartsfunction_callr   rN   rT   r   r   r|   r%   Partfrom_function_responseappend)
r   rH   func_response_partspartrc   funcrN   func_responseri   func_response_parts
             r   get_function_response_partsr   -  sM   
 %
X((+33U]]
C


a
 
(
(
.
.
:##A&..44 7$$))i		4#5#5#:#:#FI&@##
	,D"788tDM
 #ZZ>>] ? 
 	""#56)7* 
  	,"CF+-	,s   ,E  	E 	EE c                   K   g }| j                   t        | j                   d   j                  t        j                        rp| j                   d   j                  j
                  L| j                   d   j                  j
                  D ]%  }|j                  s|j                  j                  }|*|j                  j                  A||   }t        |j                  j                        }	 t        |t              rD|j                  t        j                  ||             d{   }|j                  rd|i}n?d|i}n:t        j                  |      rdt!        ||       d{   i}ndt#        ||      i}t        j(                  j+                  ||      }
|j-                  |
       ( |S 7 7 M# t$        $ r}	dt'        |	      i}Y d}	~	Vd}	~	ww xY ww)r   Nr   )r   rN   r   r   r   )r   r   r   r   r   r   r   r   rN   rT   r   	call_toolFunctionCallisErrorrA   rB   r   r   r|   r%   r   r   r   )r   rH   r   r   rc   r   rN   mcp_tool_responser   ri   r   s              r   !get_function_response_parts_asyncr   P  s    
 %
X((+33U]]
C


a
 
(
(
.
.
:##A&..44  7$$))i		4#5#5#:#:#FI&@##
	,34&*nn""	=' ! !((&(9:m'):;m**40 DT4 PPM
 8tDM
 #ZZ>>] ? 
 	""#56A 7B 
+! Q  	,"CF+-	,s`   B2G5G$G19F:*F6+?F:*F8+F:?7G6F:8F::	GGGGGc                    | syt        |       }|rs|j                  rg|j                  j                  	 Pt        |j                  j                        dk  r.t        j                  d|j                  j                   d       y|j                  r|j                  j                  y|j                  j                  r~|j                  j                  	 gd|j                  j                  v rOt        |j                  j                        dkD  r-t        j                  d|j                  j                   d       |j                  j                  S )	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   rX   loggerwarningdisablemodel_fields_setr   rI   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                     | 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   rF   r    r   r   rX   r   s     r   get_max_remote_calls_afcr     sn     
((J
	  7v>,

1
1		0	0	E	E	M((	\44II	JJr!   c                 f    | syt        |       }|j                  sy|j                  j                   S )NT)r    r   ignore_call_historyr   s     r   should_append_afc_historyr     s5     
6v>,		0	044HH	HHr!   c                    | syt        |       }|j                  ddi      }|j                  |_        |j                  rt        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?   rG   )headers)
r    
model_copyr?   r   has_mcp_tool_usagehttp_optionsr   HttpOptionsr   set_mcp_usage_header)r   rI   config_model_copys      r   parse_config_for_mcp_usager     s     
6v>, #--gt_-E(..J99,:L:LM%%-','8'8'D$%%--5/1$$,##$5$B$B$J$JK	r!   c                   K   i }t        |       }|sd|fS |j                  ddi      }|j                  rg |_        |j                  D ]  }t        t	        |t              rt        ||j                          d{         }|j                  j                  |j                         |j                  D ]p  }|j                  s|j                  D ]R  }|j                  s|j                  |j                        rt        d|j                   d      |||j                  <   T r |j                  j                  |        ||fS 7 έw)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=   r>   )r   r   r?   r   r   r   
list_toolsextendfunction_declarationsr   r'   rF   r   )r   r:   parsed_configparsed_config_copyrJ   mcp_to_genai_tool_adapter
genai_toolfunction_declarations           r   parse_config_for_mcp_sessionsr     sh     BD,V4-	+++ %///G!## .		%*T;K*L$9))%
! 	  ''(A(G(GH399 	J--(2(H(H 	$%**-112F2K2KL"2778 9& & 
 . ++?+D+DE		 	  ''-)., 
7	77' *s    A:E<E=AEE"A*Econtentschunkc                     |a|j                   T|j                   d   j                  }t        j                  |       } t	        | t
              r|| j                  |       yyyyy)z7Appends the contents of the chunk to the contents list.Nr   )r   r   t
t_contentsr   rY   r   )r   r   chunk_contents      r   append_chunk_contentsr     se    
 5++7$$Q'//M||H%H(D!m&?oom$ '@! 8r!   rr   )NNF)K__doc__rA   loggingsystypingr   r   r   r   r   r   r	   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   r   boolrM   
StringDictrT   objectrY   rP   ra   rh   rw   r   r   GenerateContentResponser   r   r   r   rX   r   r   r   tupler   ContentListUnionContentListUnionDictr   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! (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,++,sE(38"46K"KLLM, 
%**,` ;?49U66749	49p ;?KU667KK* ;?IU667I	I ;?U667e))*, ;?)8U667)8
U(()#	#$&)8X	%E**E,F,FFG	%((	% 
	%M 
 Gs   ,M 	MM