o
    Yik-                     @   s  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mZ d dlZddlmZ ddlmZ ejdkr=ejZnejZg dZeejjeejjeejjeejjeejje ejj!dejj"iZ#de j$d	ed
ee%e&e% f ddfddZ'de j$de eef dede j$fddZ(de eef de eef fddZ)de j$j*defddZ+dede j$j*defddZ,de
d de j$d	edej-fddZ.dej-deee  fd d!Z/dS )"    N)_GenericAliasAnyCallableget_args
get_originLiteralOptionalUnion   )_extra_utils)types)   
   )_py_builtin_type_to_schema_type_raise_for_unsupported_param&_handle_params_as_deferred_annotations0_add_unevaluated_items_to_fixed_len_tuple_schema!_is_builtin_primitive_or_compound_is_default_value_compatible_parse_schema_from_parameter_get_required_fieldsparam	func_name	exceptionreturnc                 C   s   t d|  d| d| d|)NzFailed to parse the parameter  of function z for automatic function calling.Automatic function calling works best with simpler function signature schema, consider manually parsing your function declaration for function .)
ValueError)r   r   r    r   g/home/kim/smarthome/.venv/lib/python3.10/site-packages/google/genai/_automatic_function_calling_util.pyr   8   s   r   annotation_under_futurenamec                 C   s    t | jtr| j|| d} | S )z7Catches the case when type hints are stored as strings.
annotation)
isinstancer#   strreplace)r   r    r!   r   r   r   r   C   s   r   json_schemac                 C   sB   |  dr|  drt| d | d kr|  ddkrd| d< | S )NZmaxItemsZprefixItemstypearrayFZunevaluatedItems)getlen)r'   r   r   r   r   J   s   r   r#   c                 C   s   | t  v S N)r   keysr"   r   r   r   r   Y   s   r   default_valuec                    s   t  r	t S t tst tjst trYt }|ttfv r0tfddt	 D S |t
u r9tt
S |tu rOttsDdS t fddD S |tu rYt	 v S dS )Nc                 3       | ]}t  |V  qd S r,   r   .0arg)r.   r   r   	<genexpr>m   
    
z/_is_default_value_compatible.<locals>.<genexpr>Fc                 3   s*    | ] t  fd dtD V  qdS )c                 3   r/   r,   r0   r1   itemr   r   r4   ~   r5   z9_is_default_value_compatible.<locals>.<genexpr>.<genexpr>N)anyr   )r2   r"   r6   r   r4   }   s    
)r   r$   r   builtin_typesGenericAliasVersionedUnionTyper   r	   r8   r   dictlistallr   )r.   r#   originr   )r#   r.   r   r   _   s0   



r   
api_option)Z	VERTEX_AIZ
GEMINI_APIc              	   C   s  t  }d|j d| d| d|j d	}t|jr8|jtjjur0t|j|js,t	||j|_t
|j |_|S t|jtrtdd t|jD rt
t |_g |_t }t|jD ]0}|jdkred	|_qZt| tjd
tjj|d|}|jd	d|vr|j| ||jd	d qZt|jdkr|jd j|_d|_|jtjjur|jdurt|j|jst	||j|_|S t|jtst|jtjrt|j}t|j}	|tu rt
t |_|jtjjurt|j|jst	||j|_|S |tu r.tdd |	D s
t	d|j dt
t  |_t!|	|_"|jtjjur,t|j|js(t	||j|_|S |t!u rct
t! |_t| tjd
tjj|	d d||_#|jtjjurat|j|js]t	||j|_|S |t$u rg |_t
t |_t }|	D ]m}t|tdu st%|dr|jdkrd	|_qut| tjd
tjj|d|}t|jj&dkrtd|jj&v r|jj&D ]}
t%|
dr|
j't!u r|j#|_#q|jd	d|vr|j| ||jd	d qut|jdkr|jd j|_d|_|jdur|jtjjurt|j|jst	||j|_|S t()|jr[|jtjjur.|jdur.|j|_t
t |_i |_*|jj+# D ]\}}t| tj|tjj|jd||j*|< q<t,||_-|S t.||t	 dS )zSparse schema from parameter.

  from the simplest case to the most complex case.
  zDefault value z of parameter r   z1 is not compatible with the parameter annotation r   c                 s   s$    | ]}t |p|td u V  qd S r,   )r   r(   r1   r   r   r   r4      s
    
z/_parse_schema_from_parameter.<locals>.<genexpr>NoneTypeTr7   r"   )Zexclude_noner
   r   Nc                 s   s    | ]}t |tV  qd S r,   )r$   r%   r1   r   r   r   r4      s    zLiteral type z must be a list of strings.__name__   
__origin__)/r   Schemadefaultr#   r   inspect	Parameteremptyr   r   r   r(   r$   r;   r>   r   r<   Zany_ofsetrB   nullabler   POSITIONAL_OR_KEYWORDZmodel_dump_jsonappendaddr+   r   r9   r:   r   r   r%   r=   enumitemsr	   hasattr__args__rD   r   Zis_annotation_pydantic_model
propertiesZmodel_fieldsr   requiredr   )r@   r   r   schemaZdefault_value_error_msgZunique_typesr3   Zschema_in_any_ofr?   argsZoptional_arg
field_nameZ
field_infor   r   r   r      s0  	


	











	






	r   rU   c                 C   s   | j sd S dd | j  D S )Nc                 S   s$   g | ]\}}|j s|jd u r|qS r,   )rK   rF   )r2   rW   Zfield_schemar   r   r   
<listcomp>A  s    
z(_get_required_fields.<locals>.<listcomp>)rS   rP   )rU   r   r   r   r   >  s
   r   )0rG   sysr   r9   typingr   r   r   r   r   r   r   r	   Zpydantic r   version_info	UnionTyper;   _UnionGenericAlias__all__r%   TypeSTRINGintZINTEGERfloatNUMBERboolZBOOLEANr=   ZARRAYr<   ZOBJECTZNULLr   rH   	Exceptionr(   r   r   r   r#   r   r   rE   r   r   r   r   r   r   <module>   st   (

&




-
  3