o
    : hN                     @  s  U d dl mZ d dlmZmZmZ d dlZd dlZd dlmZm	Z	m
Z
 d dlmZ d dlZd dlmZ d dlmZ ejZe
eeef Zi ejejd ejdejd	ejejejd
ejdejdejejejdejdejdejejejdejdejdejejejdejdejdejejejdejdejdejejejdejdejdejiZded< dwd"d#Zddd$dxd,d-Z d.d/ Z!d0d1 Z"d2d3 Z#d4d5 Z$d6d7 Z%d8d9 Z&dyd;d<Z'G d=d> d>Z(e)ed?f Z*e
e+ee,e*e-d? df Z.G d@dA dAe(Z/e
e(ej(e)eef e	dBef f Z0dzdFdGZ1d{dJdKZ2G dLdM dMZ3G dNdO dOeZ4e
e3ej3e4ee0 e0f Z5d|dRdSZ6G dTdU dUZ7e
ee5 e5f Z8d}dYdZZ9e
e7e8f Z:d~d^d_Z;ej<j=Z>d
e>j?e>j?e>j?d`e>j?dae>j?de>j@e>j@e>j@dbe>j@dce>j@de>jAe>jAe>jAdde>jAdee>jAiZBe
e>eef ZCddhdiZDG djdk dkeZEe
eCeEej<f ZFddodpZGG dqdr drZHe
eHejIf ZJddudvZKdS )    )annotations)IterableMappingSequenceN)AnyCallableUnion)	TypedDict)protos)content_typesZtype_unspecifiedunspecified   Ztype_stringstring   Ztype_numbernumber   Ztype_integerinteger   Ztype_booleanboolean   Z
type_arrayarray   Ztype_objectobjectzdict[TypeOptions, Type]
_TYPE_TYPExTypeOptionsreturnTypec                 C     t | tr	|  } t|  S N)
isinstancestrlowerr   r    r$   e/var/www/html/figdemos/bartoux_crm/venv/lib/python3.10/site-packages/google/generativeai/responder.pyto_type@      
r&   )descriptionsrequiredfCallable[..., Any]r(   Mapping[str, str] | Noner)   Sequence[str] | Nonedict[str, Any]c          	        s   |du ri }t t| j i }  D ]1\}}|jtjjtjjtjj	fv rEt
j||dd}|jtjjkr?|j|f||< qt|f||< qt| j|}|durU||d< n fdd D |d< t | j| jd}|d rp||d< |S )	a  Generates the OpenAPI Schema for a python function.

    Args:
        f: The function to generate an OpenAPI Schema for.
        descriptions: Optional. A `{name: description}` mapping for annotating input
            arguments of the function with user-provided descriptions. It
            defaults to an empty dictionary (i.e. there will not be any
            description for any of the inputs).
        required: Optional. For the user to specify the set of required arguments in
            function calls to `f`. If unspecified, it will be automatically
            inferred from `f`.

    Returns:
        dict[str, Any]: The OpenAPI Schema for the function `f` in JSON format.
    N)descriptionr)   c                   sB   g | ]} | j tjjkr | jtjjtjjtjjfv r|qS r$   )defaultinspect	ParameteremptykindPOSITIONAL_OR_KEYWORDKEYWORD_ONLYPOSITIONAL_ONLY).0kdefaultsr$   r%   
<listcomp>   s    z$_generate_schema.<locals>.<listcomp>)namer/   
properties
parameters)dictr1   	signaturer?   itemsr4   r2   r5   r6   r7   pydanticFieldget
annotationr3   r   _build_schema__name____doc__)	r*   r(   r)   fields_dictr=   paramfieldr?   schemar$   r:   r%   _generate_schemaF   s6   




rN   c                 C  sl   t j| fi | }|di }| D ]	\}}t|| qt|| t| t| t| t	| |S )Nz$defs)
rC   create_modelmodel_json_schemapoprB   unpack_defsconvert_to_nullableadd_object_typestrip_titlesstrip_additional_properties)fnamerJ   r?   defsr=   valuer$   r$   r%   rG      s   
rG   c                 C  s  |  dd }|d u rd S | D ]x\}}| dd }|d ur1||dd  }t|| |||< q| dd }|d urat|D ] \}}	|	 dd }|d ur_||dd  }t|| |||< q?q| dd }
|
d ur|
 dd }|d ur||dd  }t|| ||d< qqd S )Nr>   z$refzdefs/anyOfrB   )rE   rB   splitrR   	enumerate)rM   rX   r>   r=   rY   ref_keyrefanyofiatyperB   r$   r$   r%   rR      s<   


rR   c                 C  s^   |  dd }| dd }|d ur| D ]\}}t| q| dd }|d ur-t| d S d S )Ntitler>   rB   )rQ   rE   rB   rU   )rM   rc   r>   r=   rY   rB   r$   r$   r%   rU         
rU   c                 C  s^   |  dd  | dd }|d ur| D ]\}}t| q| dd }|d ur-t| d S d S )NadditionalPropertiesr>   rB   )rQ   rE   rB   rV   rM   r>   r=   rY   rB   r$   r$   r%   rV      rd   rV   c                 C  sf   |  dd }|d ur!| dd  d| d< | D ]\}}t| q|  dd }|d ur1t| d S d S )Nr>   r)   r   typerB   )rE   rQ   rB   rT   rf   r$   r$   r%   rT      s   
rT   c                 C  s   |  dd }|d ur8t|dkrtd|\}}|ddikr$| | n|ddikr0| | ntdd| d< | dd }|d urO| D ]\}}t| qF| d	d }|d ur_t| d S d S )
Nr[   r   zInvalid input: Type Unions are not supported, except for `Optional` types. Please provide an `Optional` type or a non-Union type.rg   nullTnullabler>   rB   )rQ   len
ValueErrorupdaterE   rB   rS   )rM   r`   abr>   r=   rY   rB   r$   r$   r%   rS      s.   
rS   rM   c                 C  s   | d u r| S |   } | dd }|d ur|| d< | dd }|d ur(t|| d< | dd }|d ur6|| d< | dd }|d urFt|| d< | dd }|d ur[dd | D | d< | S )	Nrg   type_formatformat_rB   r>   c                 S  s   i | ]	\}}|t |qS r$   )_rename_schema_fields)r8   r9   vr$   r$   r%   
<dictcomp>'  s    z)_rename_schema_fields.<locals>.<dictcomp>)copyrQ   rE   r&   rr   rB   )rM   ro   rq   rB   r>   r$   r$   r%   rr     s&   rr   c                   @  sn   e Zd Zddddd	ZedddZedddZedddZed ddZ	d!ddZ
ed"d#ddZdS )$FunctionDeclarationNr?   r=   r!   r/   r?   dict[str, Any] | Nonec                C  s   t j||t|d| _dS )zmA  class wrapping a `protos.FunctionDeclaration`, describes a function for `genai.GenerativeModel`'s `tools`.r=   r/   r?   N)r
   rv   rr   _proto)selfr=   r/   r?   r$   r$   r%   __init__-  s   
zFunctionDeclaration.__init__r   c                 C     | j jS r   )rz   r=   r{   r$   r$   r%   r=   3     zFunctionDeclaration.namec                 C  r}   r   )rz   r/   r~   r$   r$   r%   r/   7  r   zFunctionDeclaration.descriptionprotos.Schemac                 C  r}   r   )rz   r?   r~   r$   r$   r%   r?   ;  r   zFunctionDeclaration.parametersc                 C  s   | ddi d}||_ |S )N ry   rz   )clsprotor{   r$   r$   r%   
from_proto?  s   zFunctionDeclaration.from_protoprotos.FunctionDeclarationc                 C     | j S r   r   r~   r$   r$   r%   to_protoE     zFunctionDeclaration.to_protofunctionr+   r(   dict[str, str] | Nonec                 C  s.   |du ri }t | |d}tdi |d| iS )a  Builds a `CallableFunctionDeclaration` from a python function.

        The function should have type annotations.

        This method is able to generate the schema for arguments annotated with types:

        `AllowedTypes = float | int | str | list[AllowedTypes] | dict`

        This method does not yet build a schema for `TypedDict`, that would allow you to specify the dictionary
        contents. But you can build these manually.
        N)r(   r   r$   )rN   CallableFunctionDeclaration)r   r(   rM   r$   r$   r%   from_functionH  s   z!FunctionDeclaration.from_function)r=   r!   r/   r!   r?   rx   )r   r!   )r   r   )r   rv   )r   r   r   )r   r+   r(   r   )rH   
__module____qualname__r|   propertyr=   r/   r?   classmethodr   r   staticmethodr   r$   r$   r$   r%   rv   ,  s    
rv   	ValueTypec                      s2   e Zd ZdZddd fddZdddZ  ZS )r   zAn extension of `FunctionDeclaration` that can be built from a Python function, and is callable.

    Note: The Python function must have type annotations.
    Nrw   r=   r!   r/   r?   rx   r   r+   c                  s   t  j|||d || _d S )Nry   )superr|   r   )r{   r=   r/   r?   r   	__class__r$   r%   r|   h  s   
z$CallableFunctionDeclaration.__init__fcprotos.FunctionCallr   protos.FunctionResponsec                 C  s4   | j di |j}t|tsd|i}tj|j|dS )Nresult)r=   responser$   )r   argsr    r@   r
   FunctionResponser=   )r{   r   r   r$   r$   r%   __call__s  s   
z$CallableFunctionDeclaration.__call__)r=   r!   r/   r!   r?   rx   r   r+   )r   r   r   r   )rH   r   r   rI   r|   r   __classcell__r$   r$   r   r%   r   b  s
    
r   .funFunctionDeclarationType0FunctionDeclaration | protos.FunctionDeclarationc                 C  sl   t | ttjfr
| S t | tr!d| v rtdi | S tdi | S t| r*t| S tdt| j	 d| )Nr   z_Invalid argument type: Expected an instance of `genai.FunctionDeclarationType`. Received type: .r$   )
r    rv   r
   r@   r   callabler   	TypeErrorrg   rH   )r   r$   r$   r%   _make_function_declaration  s   

r   fdr   c                 C  s   t | tjr| S |  S r   )r    r
   rv   r   )r   r$   r$   r%   
_encode_fd  s   r   c                   @  sD   e Zd ZdZdddZeddd	ZdddZdddZdd Z	dS )Toolz\A wrapper for `protos.Tool`, Contains a collection of related `FunctionDeclaration` objects.function_declarations!Iterable[FunctionDeclarationType]c                 C  sd   dd |D | _ i | _| j D ]}|j}|| jv rtd|| j|j< qtjdd | j D d| _d S )Nc                 S     g | ]}t |qS r$   )r   )r8   r*   r$   r$   r%   r<         z!Tool.__init__.<locals>.<listcomp>r   c                 S  r   r$   )r   )r8   r   r$   r$   r%   r<     r   r   )_function_declarations_indexr=   rk   r
   r   rz   )r{   r   r   r=   r$   r$   r%   r|     s   

zTool.__init__r   6list[FunctionDeclaration | protos.FunctionDeclaration]c                 C  r   r   )r   r~   r$   r$   r%   r     s   zTool.function_declarationsr=   str | protos.FunctionCallr   c                 C     t |ts|j}| j| S r   r    r!   r=   r   r{   r=   r$   r$   r%   __getitem__     

zTool.__getitem__r   r   protos.FunctionResponse | Nonec                 C  s   | | }t |s
d S ||S r   )r   )r{   r   declarationr$   r$   r%   r     s   zTool.__call__c                 C  r   r   r   r~   r$   r$   r%   r     r   zTool.to_protoN)r   r   )r   r   r=   r   r   r   )r   r   r   r   )
rH   r   r   rI   r|   r   r   r   r   r   r$   r$   r$   r%   r     s    


r   c                   @     e Zd ZU ded< dS )ToolDictzlist[FunctionDeclarationType]r   NrH   r   r   __annotations__r$   r$   r$   r%   r        
 r   toolToolTypec              
   C  s   t | tr| S t | tjrt| jdS t | tr1d| v r#tdi | S | }ttjdi |gdS t | tr;t| dS zt| gdW S  ty[ } ztdt	| j
 d| |d }~ww )Nr   r   zPInvalid argument type: Expected an instance of `genai.ToolType`. Received type: r   r$   )r    r   r
   r   r@   rv   r   	Exceptionr   rg   rH   )r   r   er$   r$   r%   
_make_tool  s,   



r   c                   @  s6   e Zd ZdZdddZdd
dZdddZdd ZdS )FunctionLibraryzYA container for a set of `Tool` objects, manages lookup and execution of their functions.toolsIterable[ToolType]c                 C  s`   t |}t|| _i | _| jD ]}|jD ]}|j}|| jv r&td| d|| j|j< qqd S )Nz2Invalid operation: A `FunctionDeclaration` named 'zI' is already defined. Each `FunctionDeclaration` must have a unique name.)_make_toolslist_toolsr   r   r=   rk   )r{   r   r   r   r=   r$   r$   r%   r|     s   




zFunctionLibrary.__init__r=   r   r   r   c                 C  r   r   r   r   r$   r$   r%   r     r   zFunctionLibrary.__getitem__r   r   protos.Part | Nonec                 C  s(   | | }t |s
d S ||}tj|dS )N)function_response)r   r
   Part)r{   r   r   r   r$   r$   r%   r     s
   zFunctionLibrary.__call__c                 C  s   dd | j D S )Nc                 S  s   g | ]}|  qS r$   )r   )r8   r   r$   r$   r%   r<     r   z,FunctionLibrary.to_proto.<locals>.<listcomp>)r   r~   r$   r$   r%   r     s   zFunctionLibrary.to_protoN)r   r   r   )r   r   r   r   )rH   r   r   rI   r|   r   r   r   r$   r$   r$   r%   r     s    


r   r   	ToolsType
list[Tool]c                 C  sf   t | tr,t | ts,dd | D } t| dkr*tdd | D r*tdd | D g} | S | }t|gS )Nc                 S  r   r$   )r   r8   tr$   r$   r%   r<     r   z_make_tools.<locals>.<listcomp>r   c                 s  s    | ]
}t |jd kV  qdS )r   N)rj   r   r   r$   r$   r%   	<genexpr>  s    z_make_tools.<locals>.<genexpr>c                 S  s   g | ]}|j d  qS )r   r   r   r$   r$   r%   r<     s    )r    r   r   rj   allr   )r   r   r$   r$   r%   r     s   
r   libFunctionLibraryType | NoneFunctionLibrary | Nonec                 C  s$   | d u r| S t | tr| S t| dS )N)r   )r    r   )r   r$   r$   r%   to_function_library  s
   

r   	mode_autoautomode_anyany	mode_nonenoneFunctionCallingModeTypeFunctionCallingModec                 C  r   r   )r    r!   r"   _FUNCTION_CALLING_MODEr#   r$   r$   r%   to_function_calling_mode;  r'   r   c                   @  s   e Zd ZU ded< ded< dS )FunctionCallingConfigDictr   modez	list[str]allowed_function_namesNr   r$   r$   r$   r%   r   A  s   
 r   objFunctionCallingConfigTypeprotos.FunctionCallingConfigc                 C  sz   t | tjr| S t | tttfrdt| i} n!t | tr,|  } | 	d}t|| d< nt
dt| j d| t| S )Nr   zaInvalid argument type: Could not convert input to `protos.FunctionCallingConfig`. Received type: r   )r    r
   FunctionCallingConfigr   r!   intr   r@   ru   rQ   r   rg   rH   )r   r   r$   r$   r%   to_function_calling_configK  s   


r   c                   @  r   )ToolConfigDictr   function_calling_configNr   r$   r$   r$   r%   r   ^  r   r   ToolConfigTypeprotos.ToolConfigc                 C  sZ   t | tjr| S t | tr"| d}t|}|| d< tjdi | S tdt| j d)Nr   zVInvalid argument type: Could not convert input to `protos.ToolConfig`. Received type: r   r$   )	r    r
   
ToolConfigr@   rQ   r   r   rg   rH   )r   fccr$   r$   r%   to_tool_confige  s   

r   )r   r   r   r   )r*   r+   r(   r,   r)   r-   r   r.   )rM   r.   )r   r   r   r   )r   r   r   r   )r   r   r   r   )r   r   r   r   )r   r   r   r   )r   r   r   r   )r   r   r   r   )r   r   r   r   )L
__future__r   collections.abcr   r   r   r1   typingr   r   r   typing_extensionsr	   rC   google.generativeair
   google.generativeai.typesr   r   r   r!   r   TYPE_UNSPECIFIEDSTRINGNUMBERINTEGERBOOLEANARRAYOBJECTr   r   r&   rN   rG   rR   rU   rV   rT   rS   rr   rv   r@   
StructTypefloatboolr   r   r   r   r   r   r   r   r   r   r   r   r   FunctionLibraryTyper   r   Moder   AUTOANYNONEr   r   r   r   r   r   r   r   r   r   r$   r$   r$   r%   <module>   s   	

 	M!
2



(
$

	


