Ë
    uPêjù  ã                   óv   — d dl 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
 d dlmZ d dlmZ  G d„ d	e«      Zy)
é    )ÚFinalÚcastN)ÚBaseAzureLLM)ÚOpenAIImageEditConfig)Úget_secret_str)ÚGenericLiteLLMParams)Ú_add_path_to_api_basec                   ó–   — e Zd Zedededefd„«       Z	 	 	 ddedededz  d	edz  d
edz  defd„Zded
edz  d	edefd„Zdededefd„Z	y)ÚAzureImageEditConfigÚdataÚrequest_urlÚreturnc                 óv   — d|v r.d|v r*| j                  «       D ��ci c]  \  }}|dk7  sŒ||“Œ c}}S | S c c}}w )a)  
        Azure OpenAI ``.../openai/deployments/{deployment}/images/edits`` routes by
        deployment in the URL; including ``model`` in multipart fields can break
        the same way as image generations (LiteLLM #26316).

        Non-deployment edit URLs keep ``model`` when present.
        zimages/editsú/openai/deployments/Úmodel)Úitems)r   r   ÚkÚvs       úq/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/litellm/llms/azure/image_edit/transformation.pyÚ%azure_deployment_image_edit_form_dataz:AzureImageEditConfig.azure_deployment_image_edit_form_data   sD   € ð ˜[Ñ(Ð-CÀ{Ñ-RØ%)§Z¡Z£\×B™T˜Q °Q¸'³\�A�q‘DÓBÐBØˆùó Cs   œ5ª5NÚheadersr   Úapi_keyÚlitellm_paramsÚapi_basec                 óx   — t        di |xs i ¤Ž}|�|j                  €||_        t        j                  ||¬«      S )a  
        Validate Azure environment and set up authentication headers.

        Delegates to ``BaseAzureLLM._base_validate_azure_environment`` so the
        Azure image-edit route uses the same auth resolution as every other
        Azure provider (videos, vector_stores, responses, containers, ...):

        - prefers the Azure-style ``api-key`` header when an API key is available
        - falls back to ``Authorization: Bearer <azure_ad_token>`` only when AAD
          auth is configured

        The previous implementation unconditionally set
        ``Authorization: Bearer <api_key>``, which is correct for OpenAI direct
        but not for Azure OpenAI / API Management gateways that expect the
        ``api-key`` header. Subscription-key-based deployments (e.g., behind
        Azure APIM) responded with ``401 "Access denied due to missing
        subscription key"``.

        API-key precedence (matches ``AzureVideosConfig``):

        - ``litellm_params["api_key"]`` is the source of truth.
        - The positional ``api_key`` kwarg only fills in when
          ``litellm_params["api_key"]`` is empty.
        - This is a deliberate change from the old ``or`` chain (where the
          positional ``api_key`` argument won) so behavior matches every other
          Azure ``validate_environment`` implementation. In production the only
          caller (``llm_http_handler.image_edit``) sources both values from
          the same ``litellm_params.api_key``, so the precedence only matters
          for direct callers of this method.
        )r   r   © )r   r   r   Ú _base_validate_azure_environment)Úselfr   r   r   r   r   Úparamss          r   Úvalidate_environmentz)AzureImageEditConfig.validate_environment   sA   € ôL -ÑF°Ò0DÀ"ÑFˆØÐ 6§>¡>Ð#9Ø$ˆFŒNÜ×<Ñ<ÀWÐ]cÔdÐdó    c                 ó>  — |xs t         j                  xs t        d«      }|€t        d|› d�«      ‚t	        t
        dz  |j                  d«      «      xs/ t         j                  xs t        d«      xs t         j                  }t        j                  ||d¬«      }|�|S t        j                  |«      }t        |j                  «      }d	|vr|r||d	<   d
|vrt        |d
|› d�¬«      }n|}t        j                  |«      j!                  |¬«      }	t        |	«      S )aƒ  
        Constructs a complete URL for the API request.

        Args:
        - api_base: Base URL, e.g.,
            "https://litellm8397336933.openai.azure.com"
            OR
            "https://litellm8397336933.openai.azure.com/openai/deployments/<deployment_name>/images/edits?api-version=2024-05-01-preview"
        - model: Model name (deployment name).
        - litellm_params: Additional query parameters, including "api_version".

        Returns:
        - A complete URL string, e.g.,
        "https://litellm8397336933.openai.azure.com/openai/deployments/<deployment_name>/images/edits?api-version=2024-05-01-preview"
        ÚAZURE_API_BASENz^api_base is required for Azure AI Studio. Please set the api_base parameter. Passed `api_base=ú`Úapi_versionÚAZURE_API_VERSIONz/openai/images/edits)r   r%   Úroutezapi-versionr   z/images/edits)r   Úending_path)r   )Úlitellmr   r   Ú
ValueErrorr   ÚstrÚgetr%   ÚAZURE_DEFAULT_API_VERSIONr   Úget_azure_v1_image_urlÚhttpxÚURLÚdictr   r	   Ú	copy_with)
r   r   r   r   r%   Úv1_urlÚoriginal_urlÚquery_paramsÚnew_urlÚ	final_urls
             r   Úget_complete_urlz%AzureImageEditConfig.get_complete_urlF   s=  € ð* ÒSœw×/Ñ/ÒS´>ÐBRÓ3SˆØÐÜØpÐqyÐpzÐz{Ð|óð ô ”�t‘˜^×/Ñ/°Ó>Ó?ò 1Ü×"Ñ"ò1äÐ1Ó2ò1ô ×0Ñ0ð	 	ô %×;Ñ;ØØ#Ø(ô
ˆð
 ÐØˆMä#Ÿi™i¨Ó1ˆô # <×#6Ñ#6Ó7ˆð  Ñ,±Ø*5ˆL˜Ñ'ð "¨Ñ1Ü+Ø!Ø2°5°'¸ÐGô‰Gð
 ˆGô !Ÿ9™9 WÓ-×7Ñ7¸|Ð7ÓLˆ	ä�9‹~Ðr!   Úresolved_request_urlc                 ó&   — | j                  ||«      S )N)r   )r   r   r9   s      r   Ú finalize_image_edit_request_dataz5AzureImageEditConfig.finalize_image_edit_request_data‰   s   € Ø×9Ñ9¸$Ð@TÓUÐUr!   )NNN)
Ú__name__Ú
__module__Ú__qualname__Ústaticmethodr1   r+   r   r    r8   r;   r   r!   r   r   r      sÜ   „ Øð
°Dð 
Àsð 
Ètò 
ó ð
ð  #Ø&*Ø#ñ)eàð)eð ð)eð �t‘ð	)eð
 ˜t™ð)eð ˜‘*ð)eð 
ó)eðVAàðAð ˜‘*ðAð ð	Að
 
óAðFV°Tð VÐQTð VÐY]ô Vr!   r   )Útypingr   r   r/   r)   Úlitellm.llms.azure.common_utilsr   Ú-litellm.llms.openai.image_edit.transformationr   Úlitellm.secret_managers.mainr   Úlitellm.types.routerr   Úlitellm.utilsr	   r   r   r!   r   ú<module>rF      s.   ðß ã ã Ý 8Ý OÝ 7Ý 5Ý /ô}VÐ0õ }Vr!   