
    [ThjN                        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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  ej,                  d      Zeee	f   Zeed<    G d d	e      Zd
eee	e	f      dee   de	ddfdZd
e	dee   de	fdZ de!de	fdZ"de#de$fdZ%de	deee!f   ddfdZ& ejN                  dd      Z(dddddddde	de)d e)d!e)d"e)d#e)d$eee)      defd%Z*de	de)d e)d!e)d"e)d#e)d$ee)   defd&Z+ G d' dejX                        Z, G d( d)eejZ                        Z.defd*Z/d
eee!f   deee!f   fd+Z0d,ede
e
d-e	f   ge
d-e	f   f   fd.Z1d/edefd0Z2d1ed2edefd3Z3d1ed2eddfd4Z4y)5zCommon utilities for the SDK.    N)AnyCallable	FrozenSetOptionalUnionget_args
get_origin)alias_generators)	TypeAliaszgoogle_genai._common
StringDictc                       e Zd ZdZy)ExperimentalWarningz"Warning for experimental features.N)__name__
__module____qualname____doc__     Y/home/www/backend.miabetepe.com/venv/lib/python3.12/site-packages/google/genai/_common.pyr   r   %   s    *r   r   datakeysvaluereturnc           	         |yt        |dd       D ]  \  }}|j                  d      r|dd }| K|| vrGt        |t              r&t	        t        |            D cg c]  }i  c}| |<   nt        d| d|       t        |t              r0| .t        | |         D ]  \  }}t        |||dz   d ||            y| | |   D ]  }t        |||dz   d |         y|j                  d      r.|dd	 }| 
|| vri g| |<   | t        | |   d
   ||dz   d |        y| | j                  |i       } " | | j                  |d         }	|	S|sy||	k(  ryt        |	t              r"t        |t              r|	j                  |       yt        d|d    d|	 d| d      |d   dk(  r2t        | t              r"t        |t              r| j                  |       y|| |d   <   yyc c}w )a9  Examples:

  set_value_by_path({}, ['a', 'b'], v)
    -> {'a': {'b': v}}
  set_value_by_path({}, ['a', 'b[]', c], [v1, v2])
    -> {'a': {'b': [{'c': v1}, {'c': v2}]}}
  set_value_by_path({'a': {'b': [{'c': v1}, {'c': v2}]}}, ['a', 'b[]', 'd'], v3)
    -> {'a': {'b': [{'c': v1, 'd': v3}, {'c': v2, 'd': v3}]}}
  N[]zvalue z$ must be a list given an array path    [0]r   z+Cannot set value for an existing key. Key: z; Existing value: z; New value: ._self)	enumerateendswith
isinstancelistrangelen
ValueErrorset_value_by_path
setdefaultgetdictupdate)
r   r   r   ikeykey_name_jdexisting_datas
             r   r*   r*   )   sK    ]
$s)$ &fa
||DSbh		hd2eT"(-c%j(9:1B:$x.ugA#G  
E4	 T%5d8n- 	8DAq
AtAEG}eAh
7	8  > 7aaa!eg67	e	Sbh		hd2X		$x.+T!a%']EB__S"%d5&8 
HHT"X&M  M! mT*z%/FU#9$r( D  -mE7!E
 	

 r(g
*T4"8%ET"X5 / ;s   	G$c           
         |dgk(  r| S t        |      D ]  \  }}| s y|j                  d      r0|dd }|| v r%| |   D cg c]  }t        |||dz   d        c}c S  y|j                  d      r*|dd }|| v r| |   rt        | |   d   ||dz   d       c S  y|| v r| |   } t        | t              rt        | |      rt        | |      }  y | S c c}w )	zExamples:

  get_value_by_path({'a': {'b': v}}, ['a', 'b'])
    -> v
  get_value_by_path({'a': {'b': [{'c': v1}, {'c': v2}]}}, ['a', 'b[]', 'c'])
    -> [v1, v2]
  r"   Nr   r   r   r   r    r   )r#   r$   get_value_by_pathr%   	BaseModelhasattrgetattr)r   r   r/   r0   r1   r4   s         r   r7   r7   n   s     
gYK$ fa
||DSbh	T	=A(^L!!T!a%']3LL	e	Sbh	T	d8n h!2DQMBB	CydI&74+=tS!+, 
+! Ms   Cobjc                 @   t        | t        j                        r| j                  d      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 )ad  Recursively converts a given object to a dictionary.

  If the object is a Pydantic model, it uses the model's `model_dump()` method.

  Args:
    obj: The object to convert.

  Returns:
    A dictionary representation of the object, a list of objects if a list is
    passed, or the object itself if it is not a dictionary, list, or Pydantic
    model.
  T)exclude_none)r%   pydanticr8   
model_dumpr-   itemsconvert_to_dictr&   )r;   r0   r   items       r   rA   rA      s     X''(>>t>,,#t:=))+FJCC''FF#t.12dOD!22J	 G2s   B>B
annotationc                    t        |       }t        |       }|t        ury|rt        |      dk7  ry|d   }t        |      }t        |      }|t        ury|rt        |      dk7  ry|\  }}|t
        u xr |t        j                  u S )zChecks if the given annotation is list[dict[str, typing.Any]]
  or typing.List[typing.Dict[str, typing.Any]].

  This maps to Struct type in the API.
  Fr   r      )r	   r   r&   r(   r-   strtypingr   )rC   outer_origin
outer_argsinner_annotationinner_origin
inner_argskey_type
value_types           r   _is_struct_typerO      s     J',
#*	s:!+],-,()*	s:!+ $(J	S	5Z6::55r   modelresponsec                    t        |j                               }|D ]P  \  }}| j                  j                         D ci c]  \  }}|j                  | }}}| j                  vr||vr|j	                  |       b|j                  ||      }| j                  |   j                  }t        j                  |      t        u rt        j                  |      d   }t        |t              r(t        j                  |      t        urt        ||       t        |t               s
t        |      r|D ]5  }t        |t              st        t        j                  |      d   |       7 S yc c}}w )zfRemoves extra fields from the response that are not in the model.

  Mutates the response in place.
  r   N)r&   r@   model_fieldsaliaspopr,   rC   rG   r	   r   r   r%   r-   _remove_extra_fieldsrO   )	rP   rQ   
key_valuesr0   r   
field_info	alias_maprC   rB   s	            r   rV   rV      sM    HNN$%* Ejc5 6;5G5G5M5M5O"1#z
#I  %$$$I)=ll3
--S
!C##C(33J $-??:.q1j %6#4#4Z#@#L:u-	E4	 		$ E$dD!
vz:1=t
DE7Es    E1Tr8   )boundrE   d         indent_levelindent_deltamax_len	max_itemsdepthvisitedr`   ra   rb   rc   rd   re   c          
         |
t               }t        |       }||v ry|dk  ryt        t        |      |gz         }d|z  }d||z   z  }	t        | t        j
                        r| j                  j                  }
g }t        t        |       j                        }|D ]g  }t        |       j                  |   }|j                  s(	 t        | |      }|8t        |||z   ||||dz
  |      }|j                  |	 | d|        i |s|
 d	S |
 d
dj!                  |      z   d| dz   S t        | t"              r'd| v r| j%                  dd      }d| dS t        |       S t        | t&              r0t)        |       |kD  rt        | d|dz
         dd  dS t        |       S t        | t*        j,                  j.                        r| syt)        |       |kD  rdt)        |        dS g }	 t        | j1                         t"              }|D ]O  }| |   }t        |||z   ||||dz
  |      }t        |||z   ||||dz
  |      }|j                  |	 | d|        Q ddj!                  |      z   d| dz   S t        | t        t4        t6        f      rt9        | ||||||      S t        |       }|j%                  dd|	       S # t        $ r Y 4w xY w# t2        $ r t        | j1                               }Y w xY w)z-Returns a representation of the given object.Nz<... Circular reference ...>r   z<... Max depth ...> r   r_   =z()z(
,

)z"""z\"\"\"   r   z...'z{}z
<dict len=>)r0   z: z{
})	frozensetidr&   r%   r>   r8   	__class__r   sortedtyperS   reprr:   AttributeError_pretty_reprappendjoinrF   replacebytesr(   collectionsabcMappingr   	TypeErrortupleset_format_collection)r;   r`   ra   rb   rc   rd   re   obj_idindentnext_indent_strcls_namer@   fields
field_namerX   r   
value_represcapedsorted_keyskvk_reprv_reprraw_reprs                           r   rv   rv      sl    _kGc7&w)
QY d7mvh./'&<,67/X''(}}%%HEDI**+F B
9))*5j__Z( 

#l2#	j llo&zl!J<@A-B0 z_Zsejj//Bvha.@@#ss{E;/g7)39#u
3x'S'!)_%cr*+4009#{../
3x)#c(1%%E%388:3/k  ;
a&a
#l2#	f 
#l2#	f llo&vhb9:);* UZZ&&2fXR88#eS)*!!  CyHDB&7"899[  L  %$k%s$   K'K 	KK"K76K7c                   t        | t              rd}nMt        | t              rd}n:t        | t              rt        |       } | rd}nd}nt	        dt        |              | s|d   |d   z   S d|z  }d||z   z  }	g }
t        |       D ]Y  \  }}||k\  r%|
j                  |	 d	t        |       |z
   d
        n,|
j                  |	t        |||z   ||||dz
  |      z          [ |d    ddj                  |
      z   dz   d| |d    z   S )z(Formats a collection (list, tuple, set).)[])(rk   ){rn   )zset(rk   zUnsupported collection type: r   r   rg   z<... z more items ...>r_   rj   ri   ,)r%   r&   r   r   r)   rs   r#   rw   r(   rv   rx   )r;   r`   ra   rb   rc   rd   re   bracketsr   r   elementsr/   elems                r   r   r   l  sM    #t	C		C	3i("(8cDEE{Xa[((<F\L89OHS> 
4	>OO"#5SI)=(>>NO )L8)#ai		

( qk]"

8 44s:r&(ST+=WWWr   c                        e Zd Z ej                  ej                  dddddddej                  f	      Z	de
f fdZedej                  e   d	ee
ef   d
ee
ef   defd       Zdee
ef   fdZ xZS )r8   Tr   forbidbase64)	alias_generatorpopulate_by_namefrom_attributesprotected_namespacesextraarbitrary_types_allowedser_json_bytesval_json_bytesignored_typesr   c                 V    	 t        |       S # t        $ r t        |          cY S w xY wN)rv   	Exceptionsuper__repr__)selfrq   s    r   r   zBaseModel.__repr__  s0     $  W s   
 ((clsrQ   kwargsc                    |d uxr5 d|v xr/ |d   d uxr& t        |d   t              xr d|d   v xr |d   d   }|st        | |       | j                  |      }|S )Nconfiginclude_all_fields)r%   r-   rV   model_validate)r   rQ   r   should_skip_removing_fieldsvalidated_responses        r   _from_responsezBaseModel._from_response  s     	d 	3F	3x$	3 	6(#T*	3 	x 00		3
 812   '3)++H5r   c                 (    | j                  dd      S )NTjson)r=   mode)r?   )r   s    r   to_json_dictzBaseModel.to_json_dict  s    ??6?::r   )r   r   r   r>   
ConfigDictr
   to_camelrG   TypeVarmodel_configrF   r   classmethodTyperZ   r-   objectr   r   __classcell__rq   s   @r   r8   r8     s    $$$&//"^^%,    ;;q>(,S&[(9CGVCT 0;Df- ;r   c                   6     e Zd ZdZededef fd       Z xZS )CaseInSensitiveEnumzCase insensitive enum.r   r   c                 B   	 | |j                            S # t        $ r 	 | |j                            cY S # t        $ r\ t        j                  | d| j
                          	 t        |   | |      }t        |      |_	        ||_
        |cY cY S #  Y Y Y y xY ww xY ww xY w)Nz is not a valid )upperKeyErrorlowerwarningswarnr   r   __new__rF   _name__value_)r   r   unknown_enum_valrq   s      r   	_missing_zCaseInSensitiveEnum._missing_  s     5;;=!! 
/~>?	 #W_S%8
$'J

!%*

"!
!	
sG    	B5B-B#(BBBBBBBB)r   r   r   r   r   r   r   r   r   s   @r   r   r     s&    C C  r   r   c                      t         j                   j                         j                  d      } t        j                         j
                  dd }|  d| S )z]Composes a timestamped unique name.

  Returns:
      A string representing a unique name.
  z%Y%m%d%H%M%Sr   r]   r2   )datetimenowstrftimeuuiduuid4hex)	timestamp	unique_ids     r   timestamped_unique_namer     sL     ##%..~>)jjlq#);a	{	##r   c                    i }t        | t              s| S | j                         D ]:  \  }}t        |t              r(t	        j
                  |      j                  d      ||<   ?t        |t        j                        r|j                         ||<   mt        |t              rt        |      ||<   t        |t              rt        d |D              r5|D cg c]&  }t	        j
                  |      j                  d      ( c}||<   t        d |D              r#|D cg c]  }|j                          c}||<   |D cg c]  }t        |       c}||<   6|||<   = |S c c}w c c}w c c}w )a  Converts unserializable types in dict to json.dumps() compatible types.

  This function is called in models.py after calling convert_to_dict(). The
  convert_to_dict() can convert pydantic object to dict. However, the input to
  convert_to_dict() is dict mixed of pydantic object and nested dict(the output
  of converters). So they may be bytes in the dict and they are out of
  `ser_json_bytes` control in model_dump(mode='json') called in
  `convert_to_dict`, as well as datetime deserialization in Pydantic json mode.

  Returns:
    A dictionary with json.dumps() incompatible type (e.g. bytes datetime)
    to compatible type (e.g. base64 encoded string, isoformat date string).
  asciic              3   <   K   | ]  }t        |t                y wr   )r%   rz   .0r   s     r   	<genexpr>z.encode_unserializable_types.<locals>.<genexpr>  s     1aZ5!1s   c              3   P   K   | ]  }t        |t        j                           y wr   )r%   r   r   s     r   r   z.encode_unserializable_types.<locals>.<genexpr>  s     =!Z8,,-=s   $&)r%   r-   r@   rz   r   urlsafe_b64encodedecoder   	isoformatencode_unserializable_typesr&   all)r   processed_datar0   r   r   s        r   r   r     sB    ').	D$	KJJL "jc5%"44U;BB7KnS	E8,,	-!OO-nS	E4	 7>nS	E4	 	151	1AF
<=F$$Q'..w7
s 
=u=	=6;<q{{}<sGLM!:1=Ms!nS#"$ 

 =Ms   +E%E*E/message.c                 P     dt         dt        f   dt         dt        f   f fd}|S )z&Experimental warning, only warns once.func.r   c                 t     dt        j                         dt        dt        dt        f fd       }|S )NFargsr   r   c                  T    sdt        j                  t        d        | i |S )NTrE   )r   category
stacklevel)r   r   r   )r   r   r   r   warning_dones     r   wrapperz8experimental_warning.<locals>.decorator.<locals>.wrapper  s4     (	

 4"6""r   )	functoolswrapsr   )r   r   r   r   s   ` @r   	decoratorz'experimental_warning.<locals>.decorator  s?    L__T	#s 	#c 	#c 	# 	# Nr   )r   r   )r   r   s   ` r   experimental_warningr     s0    hsCx( Xc3h-?  
r   key_strc                 B    | j                  dd      j                         S )z?Normalizes a key for case-insensitive and snake/camel matching.r2    )ry   r   )r   s    r   _normalize_key_for_matchingr   .  s    	b	!	'	'	))r   target_dictupdate_dictc                    i }| j                         D ci c]  }t        |      | }}|j                         D ]  \  }}t        |      }||v r||   }n|}t        |t              r2t        | j                  |      t              rt        | |   |      ||<   _t        |t              r%t        | j                  |      t              r|||<   |||<    |S c c}w )a  Aligns the keys of update_dict to the case of target_dict keys.

  Args:
      target_dict: The dictionary with the target key casing.
      update_dict: The dictionary whose keys need to be aligned.

  Returns:
      A new dictionary with keys aligned to target_dict's key casing.
  )r   r   r@   r%   r-   r,   align_key_caser&   )r   r   aligned_update_dictr0   target_keys_mapr   normalized_update_keyaligned_keys           r   r   r   3  s     %'FQFVFVFXYs05s:Y/Y%%' /jc57</#$9:kk%:kook.JD#Q)7K8PRW)X+&	E4	 Z0Ld%S).+&).+&/ 
# Zs   Cc                 p   t        | |      }|j                         D ]  \  }}|| v r3t        | |   t              r t        |t              rt	        | |   |       =|| v rRt        | |   t        |            s:t        j                  d| dt        | |          dt        |       d       || |<   || |<    y)a  Recursively updates a target dictionary with values from an update dictionary.

  We don't enforce the updated dict values to have the same type with the
  target_dict values except log warnings.
  Users providing the update_dict should be responsible for constructing correct
  data.

  Args:
      target_dict (dict): The dictionary to be updated.
      update_dict (dict): The dictionary containing updates.
  zType mismatch for key 'z'. Existing type: z, new type: z. Overwriting.N)r   r@   r%   r-   recursive_dict_updaters   loggerwarning)r   r   r   r0   r   s        r   r   r   T  s    $ '{K@'--/ jc5{{3'.ud#K,e4		J{3/?e$Mnn#C5 );s#$%\$u+nN k#k#r   )5r   r   collections.abcr{   r   enumr   loggingrG   r   r   r   r   r   r   r	   r   r   r>   r
   typing_extensionsr   	getLoggerr  r-   rF   r   __annotations__Warningr   r&   r*   r7   r   rA   rs   boolrO   rV   r   rZ   intrv   r   r8   Enumr   r   r   r   r   r   r   r   r   r   <module>r     s    $        R R R    % '			1	2S#X
I &+' +BHT#s(^4 BDI Bc BVZ BJ C  tCy  S  F C .6 6 6@(E(EsF{+(E	(ET FNN3k* (,t:	t: t: 	t:
 t: t: t: in%t: 	t:n2X	2X 2X 	2X
 2X 2X 2X s^2X 	2Xj/;"" /;d#tyy .$ $#d3;&7 #Df<M #L# (HS#X4F3GRUWZRZI[3[*\ &* * *
*4B!!*4!	!r   