o
    Zh0                  
   @   s(  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 ejdkrFdd	lmZ nejZd
ZedZdejdejfddZ	d/dedeej dejfddZ	d/deej deeef fddZdeeef deeef fddZ de	eee!f e"e! e!f de	eee!f e"e! e!f fddZ#dede$fddZ%dededededef
d d!Z&deeef d"edefd#d$Z'd%ej(d&eeef de"ej) fd'd(Z*	d/deej de$fd)d*Z+	d/deej de,fd+d,Z-	d/deej de$fd-d.Z.dS )0zNExtra utils depending on types that are shared between sync and async modules.    N)AnyCallableDictOptionalUnionget_args
get_origin   )_common)errors)types)   
   )	UnionTyper   zgoogle_genai.modelsconfigreturnc                 C   s   t | trtjdi | S | S )N )
isinstancedictr   GenerateContentConfigr   r   r   P/var/www/html/lang_env/lib/python3.10/site-packages/google/genai/_extra_utils.py%_create_generate_content_config_model(   s   
r   srcc                 C   s   t j|djp
t  }d}|jst }d| |_|jsO| dr3| 	dr3| dd  d|_|S | drH|p=t }|  d	| |_|S t
d
|  |S )z4Formats the destination uri based on the source uri.r   NZgenai_batch_job_zgs://z.jsonliz/destzbq://Z_dest_zUnsupported source: )r   Z_CreateBatchJobParametersr   CreateBatchJobConfigZdisplay_namer
   Ztimestamped_unique_namedest
startswithendswith
ValueError)r   r   Zunique_namer   r   r   format_destination1   s"   
r   c                 C   sb   i }| s|S t | }|jr/|jD ]}t|r.t|r)td|j d|j d|||j< q|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.)r   Ztoolscallableinspectiscoroutinefunctionr   ZUnsupportedFunctionError__name__)r   function_mapconfig_modelZtoolr   r   r   get_function_mapO   s    



r&   argsc                 C   s   dd |   D S )z:Converts float values in dict with no decimal to integers.c                 S      i | ]	\}}|t |qS r   ,convert_number_values_for_function_call_args.0keyvaluer   r   r   
<dictcomp>h       zEconvert_number_values_for_dict_function_call_args.<locals>.<dictcomp>)itemsr'   r   r   r   1convert_number_values_for_dict_function_call_argsd   s   r3   c                 C   sR   t | tr|  rt| S t | trdd |  D S t | tr'dd | D S | S )z2Converts float values with no decimal to integers.c                 S   r(   r   r)   r+   r   r   r   r/   u   r0   z@convert_number_values_for_function_call_args.<locals>.<dictcomp>c                 S   s   g | ]}t |qS r   r)   )r,   r.   r   r   r   
<listcomp>z   s    z@convert_number_values_for_function_call_args.<locals>.<listcomp>)r   float
is_integerintr   r1   listr2   r   r   r   r*   n   s   

r*   
annotationc                 C   s.   zt | ot| tjW S  ty   Y dS w )NF)r!   isclass
issubclasspydanticZ	BaseModel	TypeError)r9   r   r   r   is_annotation_pydantic_model   s   r>   r.   
param_name	func_namec                    s  t | tr3t|r3z|di | W S  tjy2 } ztd d  d|  d| d| 
d }~ww t | trOt|tkrOt	|d  fdd| D S t | trmt|tkrmt	|\} fd	d
| 
 D S t|ttfv rt	|D ]/}t	|rt|tu st | |st | trt|rz
t| | W   S  tjy   Y qyw qytd d  d|  d| d	t | tr|tu r| S t | |st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                    s   g | ]	}t | qS r   convert_if_exist_pydantic_model)r,   item)r@   	item_typer?   r   r   r4      s    z3convert_if_exist_pydantic_model.<locals>.<listcomp>c                    s    i | ]\}}|t | qS r   rA   )r,   kv)r@   r?   
value_typer   r   r/      s    z3convert_if_exist_pydantic_model.<locals>.<dictcomp>z- cannot be converted to parameter annotation .r   )r   r   r>   r<   ZValidationErrorr   Z UnknownFunctionCallArgumentErrorr8   r   r   r1   r   r   rB   r7   r5   )r.   r9   r?   r@   e_argr   )r@   rD   r?   rG   r   rB      s   

rB   function_to_invokec                 C   s   t |}|j}i }|j D ]\}}|| v r#t| | |j||||< qz|di |W S  tyI } zt	d| d| d|  d| d }~ww )NzFailed to invoke function z with converted arguments z, from model returned function call argument z because of error r   )
r!   	signaturer#   
parametersr1   rB   r9   	Exceptionr   ZFunctionInvocationError)r'   rL   rM   r@   Zconverted_argsr?   paramrI   r   r   r   invoke_function_from_dict_args   s4   
rQ   responser$   c           
      C   s   g }| j durot| j d jtjro| j d jjduro| j d jjD ]M}|js'q!|jj}|durn|jjdurn|| }t	|jj}z	dt
||i}W n ty` } zdt|i}W Y d}~nd}~ww tjj||d}	||	 q!|S )z6Returns the function response parts from the response.Nr   resulterror)namerR   )
candidatesr   contentr   ZContentpartsZfunction_callrU   r'   r3   rQ   rO   strPartZfrom_function_responseappend)
rR   r$   Zfunc_response_partspartr@   funcr'   Zfunc_responserI   Zfunc_response_partr   r   r   get_function_response_parts   s6   


r^   c                 C   s   | sdS t | }|r(|jr(|jjdur(t|jjdkr(td|jj d dS |jr1|jjdu r3dS |jjrV|jjdurVd|jjv rVt|jjdkrVt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_   r7   loggerwarningdisableZmodel_fields_setr   r%   r   r   r   should_disable_afc  sH   re   c                 C   sD   | st S 	 t| rtdt| }|jr|jjd u r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_AFCre   r   r   r`   r_   r7   rd   r   r   r   get_max_remote_calls_afc;  s   rg   c                 C   s$   | sdS t | }|jsdS |jj S )NT)r   r`   Zignore_call_historyrd   r   r   r   should_append_afc_historyO  s   
rh   )N)/__doc__r!   loggingsystypingr   r   r   r   r   r   r   r<    r
   r   r   version_infor   _UnionGenericAliasrf   	getLoggerra   ZGenerateContentConfigOrDictr   r   rY   ZCreateBatchJobConfigOrDictr   r   r   r&   r3   objectr8   r*   boolr>   rB   rQ   ZGenerateContentResponserZ   r^   re   r7   rg   rh   r   r   r   r   <module>   s   $











>



#
8
