
    Oj                        % S r SSKrSSKJrJr  SSKJrJ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rSSKrSSKJrJrJrJrJrJr  SSKJr  SSKJr  SS	KJr  \R:                  (       a  SSKrSSK r SS
K!J"r"  SSK!Jr  SSK!Jr  \
RF                  " S5      r$\RJ                  S:  a+  \RL                  r'\R,                  \RL                  4r(SSKJ)r)  O\RT                  r'\R,                  4r(SSK+J)r)  \R:                  (       a  SSK,J-r.  SSK/J0r1  O/\r.\Rd                  \3S'   \r1\Rd                  \3S'    SSK,J-r.  SSK/J0r1  SSSS.r5\5Rm                  5        V Vs0 s H  u  pX_M	     snn r7SS.S\"Rp                  S\9S\9S\:S\94
S jjr;S\"Rp                  S \9S\94S! jr<S"\"Rp                  S#\=S\94S$ jr>S%\R~                  S\@\R~                     4S& jrAS"\"Rp                  S \9S\\9   4S' jrBS(\S\R                  4S) jrDS*\R                  S\R                  4S+ jrGS,\\R                  \\R                     4   S\@\R                     4S- jrHS.\\R                  \@\R                     4   S\@\R                     4S/ jrJS0\R                  S\R                  4S1 jrKS0\R                  S\R                  4S2 jrLS0\R                  S\R                  4S3 jrNS4\\R                     S\R                  4S5 jrQS6\\\@\R                     \R                  \@\R                     4      S\@\R                     4S7 jrRS8\\\\9\4         S\\@\R                        4S9 jrT\\R                  \R                  \R                  4   rWS:\\W\R                  S4   S\R                  4S; jrXS\"Rp                  S<\\@\R                     \@\R                     \W4   S\\@\9   \@\R                     4   4S= jrYS<\\\R                  \R                  \R                  4      S\@\R                     4S> jr\S?\R~                  SS4S@ jr]SA\SS4SB jr^S?\S\\"Rp                     SS4SC jr_ SpSDSSE.S?\R~                  S\\"Rp                     SF\\R~                     SG\=SH\\`\:      SS4SI jjjraSJ\S\\"Rp                     S\R                  4SK jrcSA\\R                  \4   S\)\R~                     4SL jreS\\"Rp                     SA\\R                  \4   S\\R                     4SM jrfSA\\R                  \4   S\\R                     4SN jriSA\R                  S\\R                     4SO jrkS\"Rp                  SA\S\\\R`                  \4      4SP jrlS\"Rp                  SA\@\   S\@\R`                     4SQ jrmS\"Rp                  SR\9S\94SS jrnS\"Rp                  ST\R                  S\R                  4SU jrqS\"Rp                  ST\R                  S\R                  4SV jrtSW\\9\R                  4   S\R                  4SX jrwSW\x\9\4   S\x\9\4   4SY jryS\"Rp                  SR\9S\94SZ jrzS[\9S\94S\ jr{S]r|S^r}S_r~S`rS"\"Rp                  Sa\R~                  S\4Sb jrSR\\\9\GR                  \GR                  \GR                  4      S\94Sc jrSd\9S\\GR
                  \94   4Se jrS:\GR                  S\R                  4Sf jrS<\\\GR                     \GR                  4   S\@\R                     4Sg jr  SqSh\\\\GR                     \GR                  4      Si\=S\GR                  4Sj jjrSk\\R                  \\R                     4   S\GR                  4Sl jrSm\@\GR                     S\@\x\9\4      4Sn jrS \9S\=4So jrg! \4 a    Sr.Sr1 GNf = fs  snn f )rz"Transformers for Google GenAI SDK.    N)IterableMapping)EnumEnumMeta)AnyGenericAliasListOptionalSequenceUnion   )mcp_to_gemini_tool)get_value_by_path)is_duck_type_of)_api_client)_common)typeszgoogle_genai._transformers)   
   )	TypeGuard)ClientSession)ToolMcpClientSessionMcpToolexactMatchSpecbleuSpec	rougeSpec)exact_matchbleu
rouge_spec   )collection_hierarchy_depthclientresource_namecollection_identifierr"   returnc                   UR                  U S35      (       + =(       a    U SU 3R                  S5      S-   U:H  nU R                  (       a  UR                  S5      (       a  U$ UR                  S5      (       a  SU R                   SU 3$ UR                  U S35      (       a  SU R                   SU R                   SU 3$ U(       a"  SU R                   SU R                   SU SU 3$ U$ U(       a  U SU 3$ U$ )a  Prepends resource name with project, location, collection_identifier if needed.

The collection_identifier will only be prepended if it's not present
and the prepending won't violate the collection hierarchy depth.
When the prepending condition doesn't meet, returns the input
resource_name.

Args:
  client: The API client.
  resource_name: The user input resource name to be completed.
  collection_identifier: The collection identifier to be prepended. See
    collection identifiers in https://google.aip.dev/122.
  collection_hierarchy_depth: The collection hierarchy depth. Only set this
    field when the resource has nested collections. For example,
    `users/vhugo1802/events/birthday-dinner-226`, the collection_identifier is
    `users` and collection_hierarchy_depth is 4. See nested collections in
    https://google.aip.dev/122.

Example:

  resource_name = 'cachedContents/123'
  client.vertexai = True
  client.project = 'bar'
  client.location = 'us-west1'
  _resource_name(client, 'cachedContents/123',
    collection_identifier='cachedContents')
  returns: 'projects/bar/locations/us-west1/cachedContents/123'

Example:

  resource_name = 'projects/foo/locations/us-central1/cachedContents/123'
  # resource_name = 'locations/us-central1/cachedContents/123'
  client.vertexai = True
  client.project = 'bar'
  client.location = 'us-west1'
  _resource_name(client, resource_name,
    collection_identifier='cachedContents')
  returns: 'projects/foo/locations/us-central1/cachedContents/123'

Example:

  resource_name = '123'
  # resource_name = 'cachedContents/123'
  client.vertexai = False
  _resource_name(client, resource_name,
    collection_identifier='cachedContents')
  returns 'cachedContents/123'

Example:
  resource_name = 'some/wrong/cachedContents/resource/name/123'
  resource_prefix = 'cachedContents'
  client.vertexai = False
  # client.vertexai = True
  _resource_name(client, resource_name,
    collection_identifier='cachedContents')
  returns: 'some/wrong/cachedContents/resource/name/123'

Returns:
  The completed resource name.
/r   	projects/z
locations//locations/)
startswithcountvertexaiprojectlocation)r#   r$   r%   r"   $should_prepend_collection_identifiers        \/var/www/html/leaf-doctor-v1/venv/lib/python3.13/site-packages/google/genai/_transformers.py_resource_namer2   L   s*   J 
"
"&;%<A#>
?? $ ##1]O
4
:
:3
?!
C	#$	 ' __,,		!	!,	/	/(-99		!	!%:$;1"=	>	>(FOO3DAm_UU	-(FOO3DAF[E\\]^k]lmm+%&a77    modelc                    U(       d  [        S5      eSU;   d  SU;   d  SU;   a  [        S5      eU R                  (       ak  UR                  S5      (       d,  UR                  S5      (       d  UR                  S5      (       a  U$ S	U;   a  UR                  S	S
5      u  p#SU SU 3$ SU 3$ UR                  S5      (       a  U$ UR                  S5      (       a  U$ SU 3$ )Nzmodel is required.z..?&zinvalid model parameter.r)   models/publishers/r(   r   z/models/zpublishers/google/models/ztunedModels/)
ValueErrorr-   r+   split)r#   r4   	publishermodel_ids       r1   t_modelr>      s    	
)
**	U]cUlcUl
/
00__%%I&&M**l	!KKQ/i9+XhZ88(00	""l			.	)	)lugr3   
api_clientbase_modelsc                 H    U R                   (       a	  U(       a  ggU(       a  gg)Nzpublishers/google/modelsmodelstunedModels)r-   )r?   r@   s     r1   t_models_urlrD      s     'r3   responsec                 >   U (       d  / $ U R                  S5      nUb  U$ U R                  S5      nUb  U$ U R                  S5      nUb  U$ U R                  S5      b  U R                  S5      c  / $ [        R                  S5        [        R                  SU 5        / $ )NrB   rC   publisherModelshttpHeadersjsonPayloadz!Cannot determine the models type.z1Cannot determine the models type for response: %s)getloggerwarningdebug)rE   rB   tuned_modelspublisher_modelss       r1   t_extract_modelsrP      s     
I/7||H/E&M5=\\-5P,9A: ! ll=!-
,,}
%
-I
NN67
LLDhOIr3   c                 D   [        X5      nU(       d  g UR                  S5      (       a0  U R                  (       a  SU R                   SU R                   SU 3$ UR                  S5      (       a0  U R                  (       a  SU R                   SU R                   SU 3$ U$ )Nr9   r)   r*   r(   r8   z/publishers/google/)r>   r+   r-   r.   r/   )r?   r4   s     r1   t_caches_modelrR      s     *
$%	
m$$)<)< J&&'{:3F3F2GqP 	""z':':z))*+j6I6I5JJ]^c]deeLr3   imagec                 ^   Sn[        5       nU R                  S:X  a3  [        U SS5      (       a!  U R                  S;   a  SnUR	                  SS9  [
        R                  " 5       nU R                  " X140 UD6  UR                  5       nSUR                  5        3n[        R                  " XES	9$ )
NPNGJPEGfilename )1LRGBRGBXCMYKkeep)qualityimage/)data	mime_type)dictformatgetattrmodeupdateioBytesIOsavegetvaluelowerr   Blob)rS   image_formatsave_paramsimage_ioimage_bytesrb   s         r1   pil_to_blobrr     s    , $+llf
%R
(
(
**9
9Lv&ZZ\(**X3{3!!#+|))+,-)		::r3   function_responsec                     U (       d  [        S5      e[        U [        5      (       a  [        R                  R                  U 5      $ [        U [        R                  5      (       a  U $ [        S[        U 5       35      e)Nzfunction_response is required.zOCould not parse input as FunctionResponse. Unsupported function_response type: )	r:   
isinstancerc   r   FunctionResponsemodel_validater   	TypeErrortype)rs   s    r1   t_function_responserz     sv     

5
66!4((!!001BCC(%*@*@AA
	$$():$;#<	> r3   function_responsesc                     U (       d  [        S5      e[        U [        5      (       a  U  Vs/ s H  n[        U5      PM     sn$ [        U 5      /$ s  snf )Nz function_responses are required.)r:   ru   r   rz   )r{   rE   s     r1   t_function_responsesr}   %  sS     

7
88"H--:LM:Lh):LMM 2344 Ns   Ablobsc                     [        U [        5      (       a  U  Vs/ s H  n[        U5      PM     sn$ [        U 5      /$ s  snf N)ru   listt_blob)r~   blobs     r1   t_blobsr   3  s;     t%*+UTF4LU++5M? ,   >r   c                    U (       d  [        S5      e[        U [        R                  5      (       a  U $ [	        U [
        5      (       a  [        R                  R                  U 5      $ SU R                  R                  R                  5       ;   a9   SS K
nUR                  R                  nUb  [	        X5      (       a  [        U 5      $ [        S[        U 5       35      e! [         a    S n NCf = f)Nzblob is required.rS   r   z6Could not parse input as Blob. Unsupported blob type: )r:   r   r   rm   ru   rc   rw   	__class____name__rl   	PIL.ImageImageImportErrorrr   rx   ry   )r   PIL	PIL_Images      r1   r   r   <  s    	
(
))T5::&&Kd::$$T**''--//))//i D!<!<>tDzlK	   is   C   C/.C/c                     [        U 5      n U R                  (       a"  U R                  R                  S5      (       a  U $ [        SU R                  < 35      e)Nr`   Unsupported mime type: r   rb   r+   r:   r   s    r1   t_image_blobr   V  C    	$	^^11(;;K,T^^,>?@@r3   c                     [        U 5      n U R                  (       a"  U R                  R                  S5      (       a  U $ [        SU R                  < 35      e)Nzaudio/r   r   r   s    r1   t_audio_blobr   ]  r   r3   partc                    U c  [        S5      e[        U [        5      (       a  [        R                  " U S9$ [        U [        R                  5      (       a_  U R                  (       a  U R                  (       d  [        S5      e[        R                  R                  U R                  U R                  S9$ [        U [        5      (       a    [        R                  R                  U 5      $ [        U [        R                  5      (       a  U $ SU R                  R                   R#                  5       ;   aK   SS KnUR&                  R&                  nUb-  [        X5      (       a  [        R                  " [+        U 5      S9$ [        S	[-        U 5       35      e! [        R                   a4    [        R                  " [        R                  R                  U 5      S9s $ f = f! [(         a    S n Nf = f)
Nzcontent part is required.)textz$file uri and mime_type are required.)file_urirb   )	file_datarS   r   )inline_datazUnsupported content part type: )r:   ru   strr   Partr   Fileurirb   from_urirc   rw   pydanticValidationErrorFileDatar   r   rl   r   r   r   rr   ry   r   r   r   s      r1   t_partr   d  st   	\
0
11c::4  T5::&&884>>=>>::DNNKKdGZZ&&t,, T5::&&K''--//))//i D!<!<ZZK$5664T$ZLABB ## GZZ%.."?"?"EFFG  is%   F 5G! AGG!G0/G0partsc                     U b  [        U [        5      (       a  U (       d  [        S5      e[        U [        5      (       a  U  Vs/ s H  n[        U5      PM     sn$ [        U 5      /$ s  snf )Nzcontent parts are required.)ru   r   r:   r   )r   r   s     r1   t_partsr     sZ     ]z%..u
2
33t%*+UTF4LU++5M? ,s   A(predictionsc           
          U (       d  g / nU  H]  nUR                  S5      (       d  M  UR                  [        R                  " [        R                  " US   S   US   S   S9S95        M_     U$ )NrS   gcsUri
imageBytes)gcs_urirq   )rS   )rJ   appendr   GeneratedImager   )r   images
predictions      r1   t_image_predictionsr     so     
&j~~gmm


KK$W-h7(1,?   
-r3   contentc                    U c  [        S5      e[        U [        R                  5      (       a  U $ [	        U [
        5      (       a    [        R                  R                  U 5      $ [        U [        R                  5      (       a  [        R                  " [        U 5      /S9$ [        U [        R                  5      (       a;  U R                  (       a  [        R                  " U /S9$ [        R                  " U /S9$ [        R                  " U S9$ ! [        R                   aI    [        U 5      nUR                  (       a  [        R                  " U/S9O[        R                  " U/S9s $ f = f)Nzcontent is required.r   )r:   r   r   Contentru   rc   rw   r   r   r   function_callModelContentUserContentr   r   )r   possible_parts     r1   	t_contentr     s,    _
+
,,Wemm,,N]]))'22 Wejj))F7O#455Wejj))    	'+ gY/
 
			)) ## Wom (( 

M?
3  7s   D AE-,E-contentsc                    [        U[        5      (       a  U Vs/ s H  n[        U5      PM     nnO[        U5      /nU R                  (       a  / nU H  nUc  M  [        U[        5      (       a  [
        R                  R                  U5      nUR                  c  MK  UR                   HF  nUR                  (       a  UR                  UR                  5        M1  [        R                  S5        MH     M     U$ U$ s  snf )Nz/Non-text part found, only returning text parts.)ru   r   r   r-   rc   r   r   rw   r   r   r   rK   rL   )r#   r   r   transformed_contents
text_partsr   s         r1   t_contents_for_embedr     s     $>FGh7Ig.hG%h/0__J'		gt$$MM009'==$mmdyy		*nnNP	 $ ( % Hs   C<c                   ^^^ U b  [        U [        5      (       a  U (       d  [        S5      e[        U [        5      (       d  [        U 5      /$ / n/ nS[        [
        R                  [        4   S[        [
        R                     4S jnS[
        R                  S[        4S jmS[        [
        R                     S[        4U4S jjmS[        [
        R                     S	[        [
        R                     SS 4U4S
 jjmS[        [
        R                     S	[        [
        R                     S[
        R                  SS 4UUU4S jjnU  H  n[        U[
        R                  5      (       d  [        U[        5      (       aU  T" X5        [        U[        5      (       a%  UR                  [
        R                  " US95        My  UR                  U5        M  U" U5      (       a  U" XU5        M  [        U[        5      (       a0  UR                  [
        R                  R!                  U5      5        M  [        S[#        U5       35      e   T" X5        U$ )Nzcontents are required.r   r&   c                    [        U [        5      (       d>  [        U [        R                  5      (       d  [        U [        R
                  5      (       a  g[        U [        5      (       a)  U (       d  g [        R
                  R                  U 5        gSU R                  R                  R                  5       ;   a/   SS KnUR                  R                  nUb  [        X5      (       a  gg! [        R                   a>     [        R                  R                  U 5         g! [        R                   a      gf = ff = f! [          a    S n Nxf = f)NTFrS   r   )ru   r   r   r   r   r   rc   rw   r   r   r   r   r   rl   r   r   r   r   s      r1   _is_partt_contents.<locals>._is_part  s    	44,,4,,$

!!$' $..))//11IIOO	 
	:d#>#>% %% 	
..
'
'
-'' 			  	s<   3C+ <E  +D= D!!D95D=8D99D= EEc                 $    U R                   (       + $ r   )r   )r   s    r1   _is_user_part!t_contents.<locals>._is_user_part  s    !!!!r3   r   c                 .   > [        U4S jU  5       5      $ )Nc              3   4   >#    U  H  nT" U5      v   M     g 7fr    ).0r   r   s     r1   	<genexpr>6t_contents.<locals>._are_user_parts.<locals>.<genexpr>  s     5ut}T""us   )all)r   r   s    r1   _are_user_parts#t_contents.<locals>._are_user_parts  s    5u555r3   resultaccumulated_partsc                    > U(       d  g U R                  T" U5      (       a  [        R                  " US9O[        R                  " US95        / US S & g )Nr   )r   r   r   r   )r   r   r   s     r1   $_append_accumulated_parts_as_content8t_contents.<locals>._append_accumulated_parts_as_content  sP     
MM,-- 	 12&78
 ar3   current_partc                    > [        U5      nT" U5      T" U5      :X  a  UR                  U5        g T" X5        U/US S & g r   )r   r   )r   r   r   r   r   r   s      r1   _handle_current_part(t_contents.<locals>._handle_current_part!  sE    
 ,'L\"o6G&HH|,*6E*^r3   r   zUnsupported content type: )ru   r   r:   r   r   r   PartUnionDictr   r   r   boolr   r   r   r   rc   rw   ry   )	r   r   r   r   r   r   r   r   r   s	         @@@r1   
t_contentsr     s   
 *Xt44X
-
..	Hd	#	#h   "&(*#%%%s*+#$$%#J"%** " "6T%**- 6$ 65==!ejj) 
,5==!
,ejj)
, ''
, 	
, 
,& gw..*Wd2K2K*6E	GT	"	"e''g67g	'		6g>	GT	"	"mmEMM009:3DM?CDD  'vA	-r3   schemac                 8   U R                  SS5      S:X  a	  SU S'   U S	 gSU ;   au  U S    Hk  nSU;   d  M  US   S:X  d  M  SU S'   U S   R                  SS05        [        U S   5      S:X  d  ME  U S   S   R                  5        H	  u  p#X0U'   M     U S	 Mm     gg)	a  Process null fields in the schema so it is compatible with OpenAPI.

The OpenAPI spec does not support 'type: 'null' in the schema. This function
handles this case by adding 'nullable: True' to the null field and removing
the {'type': 'null'} entry.

https://swagger.io/docs/specification/v3_0/data-models/data-types/#null

Example of schema properties before and after handling null fields:
  Before:
    {
      "name": {
        "title": "Name",
        "type": "string"
      },
      "total_area_sq_mi": {
        "anyOf": [
          {
            "type": "integer"
          },
          {
            "type": "null"
          }
        ],
        "default": None,
        "title": "Total Area Sq Mi"
      }
    }

  After:
    {
      "name": {
        "title": "Name",
        "type": "string"
      },
      "total_area_sq_mi": {
        "type": "integer",
        "nullable": true,
        "default": None,
        "title": "Total Area Sq Mi"
      }
    }
ry   NnullTnullableanyOfr   r   )rJ   removelenitems)r   itemkeyvals       r1   handle_null_fieldsr   H  s    X ZZ'F:v&w	4DLF2!zw/0vg1$ /!,224hc3K 5Wo   r3   originc                     [        SU  35      e)z2Raises an error if the schema type is unsupported.Unsupported schema type: )r:   r   s    r1   "_raise_for_unsupported_schema_typer     s    .vh788r3   c                     U(       aJ  UR                   (       d8  U R                  S5      (       d  U R                  S5      (       a  [        S5      eg g g )NadditionalPropertiesadditional_propertiesztadditionalProperties is only supported in Gemini Enterprise Agent Platform mode, , not in Gemini Developer API mode.)r-   rJ   r:   )r   r#   s     r1   '_raise_for_unsupported_mldev_propertiesr     sT     oo
**+
,
,ZZ/00   L  M  M 1  r3   Torder_propertiesvisited_dicts_pathdefsr   r   c          	      ~  ^^^^ Tc
  [        5       m[        U 5      T;   a  gTR                  [        U 5      5        U R                  S5      S:X  a  U S	 [	        U T5        S H   u  pVU R                  US5      =nc  M  XpU'   M"     Tc8  U R                  S0 5      mTR                  5        H  u  p[        U	TTTTS9  M     [        U 5        U R                  SS5      =n
b&  U R                  TU
R                  S5      S	      5        S
[        R                  S[        R                  4UUUU4S jjnU R                  S5      =nb6  U V	s/ s H
  o" U	5      PM     sn	U S'   TR                  [        U 5      5        gU R                  S5      n[        U[        5      (       a  UR                   n[        U["        5      (       a  UR%                  5       nU R                  S5      nUb  US:X  a
  U/U S'   U S	 O['        S5      eUS:X  a  U R                  S5      =nbt  [)        UR                  5       5       H  u  nn	U" U	5      UU'   M     [+        UR                  5       5      S:  a)  T(       a"  SU ;  a  [)        UR-                  5       5      U S'   U R                  S5      =nb   [        U[.        5      (       a  U" U5      U S'   OUUS:X  aO  U R                  S5      =nb  U" U5      U S'   U R                  S5      =nb  U Vs/ s H  nU" U5      PM     snU S'   TR                  [        U 5      5        gs  sn	f s  snf )a  Updates the schema and each sub-schema inplace to be API-compatible.

- Inlines the $defs.

Example of a schema before and after (with mldev):
  Before:

  `schema`

  {
      'items': {
          '$ref': '#/$defs/CountryInfo'
      },
      'title': 'Placeholder',
      'type': 'array'
  }


  `defs`

  {
    'CountryInfo': {
      'properties': {
        'continent': {
            'title': 'Continent',
            'type': 'string'
        },
        'gdp': {
            'title': 'Gdp',
            'type': 'integer'}
        },
      }
      'required':['continent', 'gdp'],
      'title': 'CountryInfo',
      'type': 'object'
    }
  }

  After:

  `schema`
   {
      'items': {
        'properties': {
          'continent': {
            'title': 'Continent',
            'type': 'string'
          },
          'gdp': {
            'title': 'Gdp',
            'type': 'integer'
          },
        }
        'required':['continent', 'gdp'],
        'title': 'CountryInfo',
        'type': 'object'
      },
      'type': 'array'
  }
NtitlePlaceholderLiteralEnum))r   r   )any_ofr   )prefix_itemsprefixItems)property_orderingpropertyOrderingz$defsr   $refdefs/
sub_schemar&   c                    > U R                  SS5      =nb(  TUR                  S5      S      n [        U 5      T;   a  0 $ [        U TTTTS9  U $ )z@Returns the processed `sub_schema`, resolving its '$ref' if any.r   Nr   r   r   )popr;   idprocess_schema)r  refr#   r   r   r   s     r1   _recurse process_schema.<locals>._recurse  sb    ~~fd++8		'*2./j	J-	-	)- r3   r   ry   constSTRINGenumzLiteral values must be strings.OBJECT
propertiesr   r   r   r   ARRAYr   r   )setr  addrJ   r   r  r   r  r   rg   r;   r   
StringDictr   ru   r   valuer   upperr:   r   r   keysrc   )r   r#   r   r   r   	from_nameto_namer  _r  r  r  r   schema_typer	  r  name
additionalr   prefixesprefixs    ````                r1   r  r    s%   H Z%%
F$ZZ44w)&&9
i It,,9Wo 
\::gr"D 


+/	 & V
 ZZ%%c2
MM$syy)"-./7-- '2D2D    

7##f0>DEf
x
+fEF7Obj)


6"+T""##KS!!##%K **W
%
hwfVn
/899Hjj..
;":#3#3#56
$
#J/
4 7 j 
!A
% .&*:??+<&="#jj!788
E 
J	%	%)1*)=%&gG$$1 fWoJJ}--:>FGhFx/hGf]BvJ'S FN Hs   
L5L:r  c           	      R  ^ SnU  Hs  n[        UR                  [        5      (       a  SnM&  [        UR                  [        5      (       a  MG  [	        SUR
                   S[        UR                  5       35      e   U mU(       a@  U  Vs/ s H  n[        UR                  5      PM     nn[        U R                  U[        S9nUm " U4S jS[        R                  5      nUR                  5       n[        Xq5        US   S	   n[        R                  R                  U5      $ s  snf )
NFTzEnum member z( value must be a string or integer, got )ry   c                   $   > \ rS rSr%  Y \S'   Srg)"_process_enum.<locals>.PlaceholderiX  placeholderr   Nr   
__module____qualname____firstlineno____annotations____static_attributes__)enum_to_processs   r1   Placeholderr  X  s	      r3   r(  r  r   )ru   r  intr   rx   r  ry   r   r   r   	BaseModelmodel_json_schemar  r   Schemarw   )	r  r#   is_integer_enummemberstr_membersstr_enumr(  enum_schemar'  s	           @r1   _process_enumr2  D  s     /f&,,$$oc** &6<< !# 	  /37843v||$4K8DMM;S9HO!H&& ! --/+%L)-8+		$	$[	11 9s   
D$c                 T    [        U [        5      =(       a    [        S U  5       5      $ )zEVerifies the schema is of type dict[str, Any] for mypy type checking.c              3   B   #    U  H  n[        U[        5      v   M     g 7fr   )ru   r   )r   r   s     r1   r   (_is_type_dict_str_any.<locals>.<genexpr>e  s      *&,sjcfs   )ru   rc   r   r   s    r1   _is_type_dict_str_anyr6  a  s,     
FD	! 
c *&,* ' r3   c                   ^ T(       d  g [        T[        5      (       a;  [        T5      (       a+  [        TU 5        [        R
                  R                  T5      $ [        T[        5      (       a  [        TU 5      $ [        T[        R
                  5      (       ao  [        T5      [        [        R
                  " 5       5      :X  a  [        T5        TR                  SS9n[        X 5        [        R
                  R                  U5      $ [        T[        5      (       dn  [        T[        5      (       aY  [        T[        R                   5      (       a:  TR#                  5       n[        X 5        [        R
                  R                  U5      $ [        T[        5      (       dH  [        T[        5      (       d3  [        T[$        5      (       d  [&        R(                  " T5      [*        ;   a^   " U4S jS[        R                   5      nUR#                  5       n[        X 5        US   S   n[        R
                  R                  U5      $ [-        ST 35      e)NT)exclude_unsetc                   $   > \ rS rSr%  Y \S'   Srg)t_schema.<locals>.Placeholderi  r   r   Nr!  r   s   r1   r(  r:    s	    r3   r(  r  r   r   )ru   rc   r6  r  r   r,  rw   r   r2  r   r   
model_dumpr   ry   
issubclassr   r*  r+  VersionedUnionTypetyping
get_origin_UNION_TYPESr:   )r#   r   r   r(  s    `  r1   t_schemarA  j  s    
"7"?"?66"<<&&v..!!((VU\\**F|tELLN++ )0T2F6"<<&&v..
 V\
*
*
VT
"
"
VX//
0
0%%'F6"<<&&v..&&	FD	!	!	F.	/	/			6	"l	2h((  **,F6"L!-0F<<&&v...vh788r3   c                    U (       d  g [        U [        R                  5      (       a  U $ [        U [        5      (       a8  [        R                  " [        R
                  " [        R                  " U S9S9S9$ [        U [        5      (       a  [        R                  R                  U 5      $ [        S[        U 5       35      e)N)
voice_name)prebuilt_voice_config)voice_configzUnsupported speechConfig type: )r   r   SpeechConfigru   r   VoiceConfigPrebuiltVoiceConfigrc   rw   r:   ry   r   s    r1   t_speech_configrI    s     
VU//00M&&"'";";v"N
 
 ,,V444T&\NCDDr3   c                     [        U [        R                  5      (       a  U n[        U [        5      (       a  [        R                  R                  U 5      nWR                  b  [        S5      eU$ )Nz<multi_speaker_voice_config is not supported in the live API.)r   r   rF  ru   rc   rw   multi_speaker_voice_configr:   )r   speech_configs     r1   t_live_speech_configrM    sc     VU//00M&&55f=M--9
F  
r3   c                    U(       d  g [         R                  " U5      (       d  [         R                  " U5      (       a0  [        R                  " [        R
                  R                  XS9/S9$ [        b   [        U[        5      (       a  [        U5      $ [        U[        5      (       a  [        R                  R                  U5      $ U$ )N)r#   callablefunction_declarations)inspect
isfunctionismethodr   r   FunctionDeclarationfrom_callabler   r   r   ru   rc   rw   )r#   r   s     r1   t_toolrW    s     
7#3#3F#;#;::%%33 4 
  vw??f%%&$::$$V,,Mr3   c                    U(       d  / $ [         R                  " / S9n/ nU H  n[        X5      nUc  M  UR                  (       al  UR                  b_  U=R                  UR                  -  sl        UR	                  5       nS Ul        UR                  SS9(       a  UR                  U5        M  M  UR                  U5        M     UR                  (       a  UR                  U5        U$ )NrP  T)exclude_none)r   r   rW  rQ  
model_copyr;  r   )r#   r   function_tooltoolstooltransformed_tool	tool_copys          r1   t_toolsr`    s     
I**26-
%df+#

0
011=++22	
+ %//1	*.	'T2
,,y
! 3 	%&! " ((	LL	,r3   r  c                     [        XSS9$ )NcachedContents)r%   )r2   )r#   r  s     r1   t_cached_content_namerc    s    	<L	MMr3   srcc                    [        U[        5      (       a  [        R                  " S0 UD6n[	        U[        R                  5      (       a  [        UR                  S LUR                  S LUR                  S L/5      n[        UR                  S LUR                  S L/5      nU R                  (       a  U(       d  US:w  a  [        S5      e U$ U(       d  US:w  a  [        S5      eU$ [        U[        5      (       a  [        R                  " US9$ [        U[        5      (       a  UR                  S5      (       a  [        R                  " SU/S9$ UR                  S5      (       a  [        R                  " S	US
9$ [         R"                  " SU5      (       a  [        R                  " SUS9$ UR                  S5      (       a  [        R                  " US9$ [        SU 35      e)Nr   zExactly one of `gcs_uri` or `bigquery_uri`, or `vertex_dataset_name` must be set, other sources are not supported in Gemini Enterprise Agent Platform.Exactly one of `inlined_requests`, `file_name`, `inlined_embed_content_requests`, or `embed_content_file_name` must be set, other sources are not supported in Gemini API.)inlined_requestsgs://jsonlrd   r   bq://bigqueryrd   bigquery_uriz/^projects/[^/]+/locations/[^/]+/datasets/[^/]+$zvertex-dataset)rd   vertex_dataset_namefiles/)	file_namezUnsupported source: r   )ru   rc   r   BatchJobSourcer   sumr   rn  ro  rg  rq  r-   r:   r   r   r+   rematch)r#   rd  vertex_sourcesmldev_sourcess       r1   t_batch_job_sourcerx    s    T



%
%CS%..//KKt#D(##4/	
N D(T! M 	.A-M
 	
 . J 
=A-J
 	

 J#t55#s
~~g!!%  
	 	 !!  
Dc	J	J!!!!  
	!	!!!  	)#/00r3   c                 4   [        U[        5      (       a  [        R                  " S0 UD6n[	        U[        R                  5      (       a8  [        UR                  S LUR                  S L/5      nUS:w  a  [        S5      eU$ [        S[        U5       35      e)Nr   rf  zUnsupported source type: r   )
ru   rc   r   EmbeddingsBatchJobSourcer   rs  rg  rq  r:   ry   )r#   rd  rw  s      r1   t_embedding_batch_job_sourcer{  .  s     T

(
(
/3
/CS%8899D(T! M H 
 J
0c<
==r3   destc                    [        U [        5      (       a  [        R                  " S0 U D6n U $ [        U [        5      (       ad  U R                  S5      (       a  [        R                  " SU S9$ U R                  S5      (       a  [        R                  " SU S9$ [        SU  35      e[        U [        R                  5      (       a  U $ [        SU  35      e)	Nrh  ri  rj  rk  rl  rm  zUnsupported destination: r   )ru   rc   r   BatchJobDestinationr   r+   r:   r   )r|  s    r1   t_batch_job_destinationr  E  s     d$$,t,DK$w&&  
	!	!&& 
 24&9::tU6677K
07
88r3   c                     U R                  S0 5      R                  S/ 5      nU(       d  U $ U H;  nUR                  S0 5      nU(       d  M  SU;   d  M&  U R                  S5      U S'     U $    U $ )NinlinedResponsesrE   	embeddinginlinedEmbedContentResponses)rJ   r  )r|  inline_responsesrE   inner_responses       r1   t_recv_batch_job_destinationr  ^  sz    XX0"599" 
K"h\\*b1Nn$-1XX6H-Id)*	+ # 
+r3   c                 R   U R                   (       d@  Sn[        R                  " X!5      (       a  UR                  S5      S   $ [	        SU S35      eSn[        R                  " X15      (       a  UR                  S5      S   $ UR                  5       (       a  U$ [	        SU S35      e)Nzbatches/[^/]+$r(   r   zInvalid batch job name: .z:^projects/[^/]+/locations/[^/]+/batchPredictionJobs/[^/]+$)r-   rt  ru  r;   r:   isdigit)r#   r  mldev_patternvertex_patterns       r1   t_batch_job_namer  o  s    	%M	xx$$ZZ_R  1$q9::P.XXn##::c?2||~~K
/vQ7
88r3   statec                 h    U S:X  a  gU S:X  a  gU S:X  a  gU S:X  a  gU S	:X  a  g
U S:X  a  gU S:X  a  gU $ )NBATCH_STATE_UNSPECIFIEDJOB_STATE_UNSPECIFIEDBATCH_STATE_PENDINGJOB_STATE_PENDINGBATCH_STATE_RUNNINGJOB_STATE_RUNNINGBATCH_STATE_SUCCEEDEDJOB_STATE_SUCCEEDEDBATCH_STATE_FAILEDJOB_STATE_FAILEDBATCH_STATE_CANCELLEDJOB_STATE_CANCELLEDBATCH_STATE_EXPIREDJOB_STATE_EXPIREDr   )r  s    r1   t_job_stater    s[    
''"%%%%'' $$'' %%Lr3   g      ?g      4@g      @g      ?structc                    UR                  S5      =n(       a  SU;   a  UnSn[        nUR                  S5      S:w  at  U[        :  a  [        SU SU 35      eU R	                  SU0 S	9n[
        R                  " U5        XD-  n[        U[        -  [        5      nUR                  S5      S:w  a  Mt  UR                  S
5      =n(       a  [        SU SU SU 35      eUR                  S5      $ U$ )Nr  z/operations/g        doneTz
Operation z timed out.
GET)http_methodpathrequest_dicterrorz failed with error: z.
rE   )
rJ   !LRO_POLLING_INITIAL_DELAY_SECONDSLRO_POLLING_TIMEOUT_SECONDSRuntimeErrorrequesttimesleepminLRO_POLLING_MULTIPLIER!LRO_POLLING_MAXIMUM_DELAY_SECONDS)r?   r  r  	operationtotal_secondsdelay_secondsr  s          r1   t_resolve_operationr    s    jj  d n&<$*IM5M
--
4
'	4	4Zv]9+FGG$$$R % i jj$m
0
0
+m --
4
' g&&u&tf0s9+
F  ==$$Mr3   c                    [        U [        R                  5      (       a  U R                  n Oq[        U [        R                  5      (       a  U R
                  n OE[        U [        R                  5      (       a&  U R                  b  U R                  R
                  n OS n U c  [        S5      e[        U [        5      (       d  [        S[        U 5       S35      eU R                  S5      (       aO  U R                  S5      S   n[        R                  " SU5      nUc  [        SU  35      eUR!                  S	5      n U $ U R                  S5      (       a  U R                  S5      S   n U $ )
NzFile name is required.z"Could not convert object of type `z` to a file name.zhttps://rp  r   z	[a-z0-9]+z&Could not extract file name from URI: r   )r   r   r   r  Videor   GeneratedVideovideor:   ru   r   ry   r+   r;   rt  ru  group)r  suffixru  s      r1   t_file_namer    s,    T5::&&99DtU[[))88DtU1122zzZZ^^dd	\
-
..	D#		

,T$ZL8IJ  
__Z  ZZ!!$FHH[&)E}?vFGG;;q>D 
+ x  ::h"D	+r3   statusc                 n   U S:X  a  [         R                  R                  $ U S:X  a  [         R                  R                  $ U S:X  a  [         R                  R                  $ U S:X  a  [         R                  R
                  $ [         R                   H   n[        UR                  5      U :X  d  M  Us  $    U $ )NSTATE_UNSPECIFIEDCREATINGACTIVEFAILED)r   JobStater  r  r  r  r   r  )r  r  s     r1   t_tuning_job_statusr    s    "">>///>>+++>>--->>***	U[[	V	#   Mr3   c                     [        U [        5      (       a  [        R                  R	                  U 5      $ [        U [        R                  5      (       a  U $ [        S[        U 5       S35      e)NCould not convert input (type "z") to `types.Content`)ru   rc   r   r   rw   r   r:   ry   )r   s    r1   t_content_strictr    sX    ==''00w..N

)$w-8MN r3   c                     [        U [        5      (       a  U  Vs/ s H  n[        U5      PM     sn$ [        U 5      /$ s  snf r   )ru   r   r  )r   r   s     r1   t_contents_strictr    s?     (##5=>X'W%X>>X&'' ?r   turnsturn_completec                     U c  [         R                  " US9$  [         R                  " [        U S9US9$ ! [         a  n[	        S[        U 5       S35      UeS nAff = f)N)r  )r   )r  r  r  z") to `types.LiveClientContent`)r   LiveClientContentr  	Exceptionr:   ry   )r  r  es      r1   t_client_contentr    sv     ]""??	""/#  
 

)$u+ 7$ 	$ s   5 
AAAinputc                     U (       d  [        SU  35      e [        R                  " [        U S9S9$ ! [         a  n[        S[        U 5       S35      UeS nAff = f)Nz#A tool response is required, got: 
)r{   r  z$") to `types.LiveClientToolResponse`)r:   r   LiveClientToolResponser}   r  ry   )r  r  s     r1   t_tool_responser    ss     

;E7C
DD''/5I  
 

)$u+ 7) 	) s   2 
AAAmetricsc                 8   / nU  GHu  n[        U[        5      (       a    [        R                  R	                  U5      n[        U[        R                  5      (       a*  UR                  5       nSS/US'   UR                  U5        M  0 nSS/US'   [        US/5      R                  5       nUS:X  a  0 US'   OUS:X  a  0 US'   OUR                  S	5      (       a  UR                  S
S5      nSU0US'   Ov[        US5      (       aW  UR                  (       aF  SUR                  0n[        US/5      nU(       a  XS'   [        US/5      n	U	(       a  SU	0US'   XtS'   O[        SU 35      eUR                  U5        GMx     U$ ! [
        R                   a     GNZf = f)a  Prepares the metric payload for the evaluation request.

Args:
    request_dict: The dictionary containing the request details.
    resolved_metrics: A list of resolved metric objects.

Returns:
    The updated request dictionary with the prepared metric payload.
AVERAGESTANDARD_DEVIATIONaggregation_metricsr  r   exact_match_specr   	bleu_specrouger  rX   
rouge_typer    prompt_templatemetric_prompt_templatejudge_model_system_instructionsystem_instructionreturn_raw_outputcustom_output_format_configpointwise_metric_specz0Unsupported metric type or invalid metric name: )ru   rc   r   UnifiedMetricrw   r   r   r;  r   getvrl   r+   replacehasattrr  r:   )
r  metrics_payloadmetricunified_metric_payloadmetric_payload_itemmetric_namer  pointwise_specr  r  s
             r1   	t_metricsr  '  s    O	FD	!	!	&&55f=& 
FE//	0	0171B1B1D 9
45 	56,.

4/0
 &*002k		%24./& +-K(!!'** ((b1
-9:,FL)6,--&2H2H2F4J4JK!56
 1C-
. ()
 !#4;.6
7 8F34A+O
 	
 01i j a '' 	
	s   FFFc                 <    SU ;   =(       a    U S:g  =(       d    SU ;   $ )Ngeminizgemini-embedding-001maasr   )r4   s    r1   t_is_vertex_embed_content_modelr  m  s(     %;E%;; 	5	r3   r   )NT)__doc__base64collections.abcr   r   r  r   r   rR  rh   loggingrt  sysr  r   builtin_typesr>  r   r   r	   r
   r   r   
_mcp_utilsr   r   r   r  r   TYPE_CHECKINGr   r   r   rX   r   	getLoggerrK   version_info	UnionTyper=  r@  r   _UnionGenericAliastyping_extensionsmcpr   r   	mcp.typesr   r   Typer%  r   metric_name_sdk_api_mapr   metric_name_api_sdk_mapBaseApiClientr   r)  r2   r>   r   rD   r  r   rP   rR   rm   rr   FunctionResponseOrDictrv   rz   r}   BlobImageUnionDictr   r   r   
BlobOrDictr   r   r   r   r   r   r   r   ContentDictContentTyper   r   ContentListUnionContentListUnionDictr   r   r   r   r  r  r,  r2  SchemaUnionDictr6  rA  SpeechConfigUnionDictrF  rI  SpeechConfigOrDictrM  rW  r`  rc  BatchJobSourceUnionDictrr  rx  EmbeddingsBatchJobSourceOrDictrz  r{  BatchJobDestinationOrDictr~  r  rc   r  r  r  r  r  r  r  r  r   r  r  r  r  r  ContentOrDictr  r  r  r  r  r  MetricSubclassr  r  )kvs   00r1   <module>r     s	    )  -   	  	 
    E E * . $	    			7	8w$..,, 7 78,00,,,)	3'"%FKK%'6;;5) $ 
 -D,I,I,KL,KDA14,KL  '(Z%%ZZ 	Z
 !$Z 	ZzK-- c c 4))8<  	'

>))25c]";s ;uzz ;&33
 5$$--.	05
 
%
 
 !5))40H0H+IIJ	%**)) ejj 4Au// AEJJ AAu'' AEJJ AC%--. C5:: C<d5&&')<)<d5::>NNO 
%**	(738#456d5''()& EMM5#4#4e6I6IIJ*; 1 1478*
]]*8 %% D'e.?.?)@+MN  49d5==))* 4ie$$e&@&@%--OPi 
%--	iX8w11 8d 8v9s 9t 9
MM!+";";<M	M" *.l(
 "-1l(l([../l( 7%%
&l(
 l( !S*l( 
l(\2
2$[%>%>?2
\\2:%'',-w!!"-9[../-9%'',--9 ell-9`E%--s23Ee  !E&$$e  ! %%/2eEJJO$%*%%/3Cy	%**:N+";"; N3 N3 N81%%81		&	&81 81v>%%>		-	-> ##>.9
U444
59
92tCH~ $sCx. "9[66 9c 9c 9$s s & %( !$( !#  ))3:3E3E<
5ejj%++u7K7KKL
MD ennc.A(B  e11 emm (HU00153F3FFG(	%--( 		hu**+U-@-@@A 	
 *$$--.	0
 !!(C%&&'C	$sCx.CL3 4 ]) 
 G Ms   2_ _
__