o
    0Xxi,                  
   @   s  d 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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	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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 )+zNExtra 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   srcconfigreturnc                 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   Ngenai_batch_job_zgs://z.jsonliz/destzbq://_dest_zUnsupported source: )r   _CreateBatchJobParametersr   CreateBatchJobConfigdisplay_namer
   timestamped_unique_namedest
startswithendswith
ValueError)r   r   unique_name r   X/home/air/biblejyuku/back/venv/lib/python3.10/site-packages/google/genai/_extra_utils.pyformat_destination&   s"   
r    c                 C   s|   | rt | trtjdi | n| }i }|s|S |jr<|jD ]}t|r;t|r6t	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.Nr   )
isinstancedictr   GenerateContentConfigtoolscallableinspectiscoroutinefunctionr   UnsupportedFunctionError__name__)r   config_modelfunction_maptoolr   r   r   get_function_mapD   s*   



r-   argsc                 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   s   i | ]	\}}|t |qS r   ,convert_number_values_for_function_call_args).0keyvaluer   r   r   
<dictcomp>d   s    z@convert_number_values_for_function_call_args.<locals>.<dictcomp>c                 S   s   g | ]}t |qS r   r/   )r1   r3   r   r   r   
<listcomp>i   s    z@convert_number_values_for_function_call_args.<locals>.<listcomp>)r!   float
is_integerintr"   itemslist)r.   r   r   r   r0   ]   s   

r0   
annotationc                 C   s.   zt | ot| tjW S  ty   Y dS w )NF)r&   isclass
issubclasspydantic	BaseModel	TypeError)r;   r   r   r   is_annotation_pydantic_modelo   s   rA   r3   
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)r1   item)rC   	item_typerB   r   r   r5      s    z3convert_if_exist_pydantic_model.<locals>.<listcomp>c                    s    i | ]\}}|t | qS r   rD   )r1   kv)rC   rB   
value_typer   r   r4      s    z3convert_if_exist_pydantic_model.<locals>.<dictcomp>z- cannot be converted to parameter annotation .r   )r!   r"   rA   r>   ValidationErrorr    UnknownFunctionCallArgumentErrorr:   r   r   r9   r   r   rE   r8   r6   )r3   r;   rB   rC   e_argr   )rC   rG   rB   rJ   r   rE   {   s   

rE   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)   
parametersr9   rE   r;   	Exceptionr   FunctionInvocationError)r.   rQ   rR   rC   converted_argsrB   paramrN   r   r   r   invoke_function_from_dict_args   s4   
rX   responser+   c           	      C   s   g }| j d jjD ]C}|jsq	|jj}|| }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.r   resulterrorN)namerY   )
candidatescontentpartsfunction_callr\   r0   r.   rX   rT   strr   Partfrom_function_responseappend)	rY   r+   func_response_partspartrC   funcr.   rN   func_responser   r   r   get_function_response_parts   s$   ri   c                 C   s   | rt | trtjd	i | n| }|r1|jr1|jjdur1t|jjdkr1td|jj d dS |r<|jr<|jj	du r>dS |jj	r[|jjdur[t|jjdkr[td|jj d |jj	S )
z6Returns whether automatic function calling is enabled.Nr   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.TFz`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   )
r!   r"   r   r#   automatic_function_callingmaximum_remote_callsr8   loggingwarningdisabler   r*   r   r   r   should_disable_afc   sL   rp   c                 C   sX   t | rtd| rt| trtjdi | n| }|r$|jr$|jjdu r&tS t	|jjS )zBReturns the remaining remote calls for automatic function calling.zUautomatic function calling is not enabled, but SDK is trying to get max remote calls.Nr   )
rp   r   r!   r"   r   r#   rj   rk   _DEFAULT_MAX_REMOTE_CALLS_AFCr8   ro   r   r   r   get_max_remote_calls_afc!  s"   rr   c                 C   s:   | rt | trtjdi | n| }|r|jsdS |jj S )NTr   )r!   r"   r   r#   rj   ignore_call_historyro   r   r   r   should_append_afc_history8  s   

rt   )N)*__doc__r&   rl   typingr   r   r   r   r   r   r   sysr>    r
   r   r   version_infor   _UnionGenericAliasrq   ra   CreateBatchJobConfigOrDictr   r    GenerateContentConfigOrDictr"   objectr-   r:   r0   boolrA   rE   rX   GenerateContentResponserb   ri   rp   r8   rr   rt   r   r   r   r   <module>   s   $





>




9
