o
    i/                     @   s   d Z ddlZddlZddlmZmZmZmZ ddlm	Z	 ddl
mZ ddl
mZ ddl
mZ dd	l
mZ ed
ZdejdefddZ	ddejdeej dejfddZG dd dejZG dd dejZdS )z&[Experimental] Auth Tokens API client.    N)AnyDictListOptional)	urlencode   )_api_module)_common)_tokens_converters)typeszgoogle_genai.tokenssetupreturnc                    sV   g }|   D ]\ }t|tr|r fdd| D }n g}|| qd|S )zReturn field_masksc                    s   g | ]	}  d | qS ). ).0kkkr   Y/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/google/genai/tokens.py
<listcomp>$   s    z$_get_field_masks.<locals>.<listcomp>,)items
isinstancedictkeysextendjoin)r   fieldsvfieldr   r   r   _get_field_masks   s   
r    request_dictconfigc           	      C   s  |  d}|r| dr| d| d< t| d }t|tr*| ddur*| dr8t|tjr=|jdur=|js=|| d< nxt|trI| ddu sTt|tjr[|jdu r[| dd nZ| d r|  d}t 	 
 }|rg }|D ]}||v r}d| }|| qrng }|r|d d| n|| d< n| dd n|  dg }d|}|r|| d< n| dd |  ds| dd | S )z"Converts bidiGenerateContentSetup.bidiGenerateContentSetupr   lock_additional_fieldsN	fieldMaskzgenerationConfig.r   )getr    r   r   r   CreateAuthTokenConfigr$   popGenerationConfig
model_dumpr   appendr   )	r!   r"   
bidi_setup
field_maskadditional_fields_listgeneration_config_listfield_mask_listr   field_mask_strr   r   r   "_convert_bidi_setup_to_token_setup,   s`   









r2   c                   @   :   e Zd ZdZeddddeej dej	fddZ
dS )	Tokenszb[Experimental] Auth Tokens API client.

  This class provides methods for creating auth tokens.
  [The SDK's token creation implementation is experimental, and may change in future versions.Nr"   r"   r   c                C   s  t j|d}| jjrtdt| j|}|d}|r"d|}nd}|d}|r4| dt	| }|
dd |rAt||}d}|durV|jdurV|jjdurV|jj}t|}t|}| jd	|||}|jsni nt|j}	t jj|	| d
}
| j|
 |
S )a  [Experimental] Creates an auth token.

    Args:
      config (CreateAuthTokenConfig): Optional configuration for the request.

    The CreateAuthTokenConfig's `live_constrained_parameters` attrubite
    Can be used to lock the parameters of the live session so they
    can't be changed client side. This behavior has two basic modes depending on
    whether `lock_additional_fields` is set:

    If you do not pass `lock_additional_fields` the entire
    `live_constrained_parameters` is locked and can't be changed
    by the token's user.

    If you set `lock_additional_fields`, then the non-null fields of
    `live_constrained_parameters` are locked, and any additional fields
    specified in `lock_additional_fields`.

    Usage:

    .. code-block:: python

      # Case 1: If LiveEphemeralParameters is unset, unlock LiveConnectConfig
      # when using the token in Live API sessions. Each session connection can
      # use a different configuration.

      config = types.CreateAuthTokenConfig(
          uses=10,
          expire_time='2025-05-01T00:00:00Z',
      )
      auth_token = client.tokens.create(config=config)

    .. code-block:: python

      # Case 2: If LiveEphemeralParameters is set, lock all fields in
      # LiveConnectConfig when using the token in Live API sessions. For
      # example, changing `output_audio_transcription` in the Live API
      # connection will be ignored by the API.

      auth_token = client.tokens.create(
          config=types.CreateAuthTokenConfig(
              uses=10,
              live_constrained_parameters=types.LiveEphemeralParameters(
                  model='gemini-live-2.5-flash-preview',
                  config=types.LiveConnectConfig(
                      system_instruction='You are an LLM called Gemini.'
                  ),
              ),
          )
      )

    .. code-block:: python

      # Case 3: If LiveEphemeralParameters is set and lockAdditionalFields is
      # empty, lock LiveConnectConfig with set fields (e.g.
      # system_instruction in this example) when using the token in Live API
      # sessions.
      auth_token = client.tokens.create(
          config=types.CreateAuthTokenConfig(
              uses=10,
              live_constrained_parameters=types.LiveEphemeralParameters(
                  config=types.LiveConnectConfig(
                      system_instruction='You are an LLM called Gemini.'
                  ),
              ),
              lock_additional_fields=[],
          )
      )

    .. code-block:: python

      # Case 4: If LiveEphemeralParameters is set and lockAdditionalFields is
      # set, lock LiveConnectConfig with set and additional fields (e.g.
      # system_instruction, temperature in this example) when using the token
      # in Live API sessions.
      auth_token = client.tokens.create(
          config=types.CreateAuthTokenConfig(
              uses=10,
              live_constrained_parameters=types.LiveEphemeralParameters(
                  model='gemini-live-2.5-flash-preview',
                  config=types.LiveConnectConfig(
                      system_instruction='You are an LLM called Gemini.'
                  ),
              ),
              lock_additional_fields=['temperature'],
          )
      )
    r6   =This method is only supported in the Gemini Developer client._urlauth_tokens_query?r"   Npostresponsekwargs)r   CreateAuthTokenParameters_api_clientvertexai
ValueErrortokens_converters#_CreateAuthTokenParameters_to_mldevr&   
format_mapr   r(   r2   r"   http_optionsr	   convert_to_dictencode_unserializable_typesrequestbodyjsonloads	AuthToken_from_responser*   _verify_responseselfr"   parameter_modelr!   request_url_dictpathquery_paramsrG   r>   response_dictreturn_valuer   r   r   createw   sJ   `





zTokens.create__name__
__module____qualname____doc__r	   experimental_warningr   r   CreateAuthTokenConfigOrDictrN   rY   r   r   r   r   r4   q       r4   c                   @   r3   )	AsyncTokenszu[Experimental] Async Auth Tokens API client.

  This class provides asynchronous methods for creating auth tokens.
  r5   Nr6   r"   r   c                   s  t j|d}| jjrtdt| j|}|d}|r#d|}nd}|d}|r5| dt	| }|
dd t||}d}|durU|jdurU|jjdurU|jj}t|}t|}| jjd	|||d
I dH }|jsqi nt|j}	t jj|	| d}
| j|
 |
S )a  Creates an auth token asynchronously. Support in v1alpha only.

    Args:
      config (CreateAuthTokenConfig): Optional configuration for the request.

    Usage:

    .. code-block:: python

      client = genai.Client(
          api_key=API_KEY,
          http_options=types.HttpOptions(api_version='v1alpha'),
      )

      auth_token = await client.aio.tokens.create(
          config=types.CreateAuthTokenConfig(
              uses=10,
              live_constrained_parameters=types.LiveEphemeralParameters(
                  model='gemini-live-2.5-flash-preview',
                  config=types.LiveConnectConfig(
                      system_instruction='You are an LLM called Gemini.'
                  ),
              ),
          )
      )
    r6   r7   r8   r9   r:   r;   r"   Nr<   )rG   r=   )r   r@   rA   rB   rC   rD   rE   r&   rF   r   r(   r2   r"   rG   r	   rH   rI   async_requestrK   rL   rM   rN   rO   r*   rP   rQ   r   r   r   rY     sP   "





zAsyncTokens.createrZ   r   r   r   r   rb     ra   rb   )N)r^   rL   loggingtypingr   r   r   r   urllib.parser    r   r	   r
   rD   r   	getLoggerlogger
StringDictstrr    r`   r2   
BaseModuler4   rb   r   r   r   r   <module>   s,   

E 