Ë
    uPêjù-  ã                   óT  — U d Z ddlZddlZddlmZmZmZ dZeed<   dede	dz  fd„Z
	 dd	e	d
e	de	ed   z  fd„Zde	dee	   fd„Zde	de	dz  fd„Zde	de	dz  fd„Zde	de	dz  fd„Zde	de	dz  fd„Z	 d de	de	dee	   de	de	dedz  de	fd„Zde	de	fd„Zde	de	dz  fd„Zde	dedz  fd„Zy)!z°
Utility functions for managed resources.

This module provides common utility functions that can be used across
different managed resource types (files, vector stores, etc.).
é    N)ÚAnyÚFinalÚLiteral)ÚazureÚazure_aiÚ&PASSTHROUGH_MANAGED_ID_AZURE_PROVIDERSÚcustom_llm_providerÚreturnc                 ó¼   — t        t        | d| «      xs d«      j                  «       }|sy|t        v s|j	                  d«      ry|dk(  s|j	                  d«      ryy)a-  Map a pass-through ``custom_llm_provider`` to the provider scope that
    namespaces passthrough managed object IDs, or ``None`` when the route is not
    an OpenAI/Azure pass-through and managed IDs must not apply.

    Scoping is keyed on the explicit provider that the pass-through route
    forwards (``openai``, ``azure``, ``azure_ai``), not on the upstream URL, so
    a third-party OpenAI-compatible endpoint never triggers managed-ID minting.

    ``azure`` and ``azure_ai`` deliberately collapse to one ``"azure"`` scope:
    they expose the same Azure OpenAI files/batches surface, so an ID minted
    while routing as one must still resolve while routing as the other.
    Splitting them would make a managed ID minted on ``azure`` fail to resolve
    when replayed on ``azure_ai`` and vice versa.
    ÚvalueÚ N)z.azurez	.azure_air   Úopenaiz.openai)ÚstrÚgetattrÚlowerr   Úendswith)r	   Úproviders     úr/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/litellm/llms/base_llm/managed_resources/utils.pyÚ'resolve_passthrough_managed_id_providerr      sc   € ô" œ'Ð"5°wÐ@SÓTÒZÐXZÓ[×aÑaÓc€HÙØØÔ9Ñ9¸X×=NÑ=NÐOfÔ=gØØ�8Ò˜x×0Ñ0°Ô;ØØó    Úresource_idÚprefixFc                 óÜ   — t        | t        «      sy| dt        | «       dz  z  z   }	 t        j                  |«      j                  «       }|j                  |«      r|S y# t        $ r Y yw xY w)z÷
    Check if a resource ID is a base64 encoded unified ID.

    Args:
        resource_id: The resource ID to check
        prefix: The expected prefix for unified IDs

    Returns:
        Decoded string if valid unified ID, False otherwise
    Fú=é   )Ú
isinstancer   ÚlenÚbase64Úurlsafe_b64decodeÚdecodeÚ
startswithÚ	Exception)r   r   ÚpaddedÚdecodeds       r   Úis_base64_encoded_unified_idr%   *   su   € ô �k¤3Ô'Øð   #¬#¨kÓ*:Ð):¸QÑ)>Ñ"?Ñ?€FðÜ×1Ñ1°&Ó9×@Ñ@ÓBˆØ×Ñ˜fÔ%ØˆNàøÜò Ùðús   ¨5A Á	A+Á*A+Ú
unified_idc                 ó  — 	 t        | t        «      sg S t        | «      }|r|} t        j                  d| «      }|r<|j                  d«      j                  d«      D �cg c]  }|j                  «       ‘Œ c}S g S c c}w # t        $ r g cY S w xY w)aZ  
    Extract target model names from a unified resource ID.

    Args:
        unified_id: The unified resource ID (decoded or encoded)

    Returns:
        List of target model names

    Example:
        unified_id = "litellm_proxy:vector_store;unified_id,uuid;target_model_names,gpt-4,gemini-2.0"
        returns: ["gpt-4", "gemini-2.0"]
    ztarget_model_names,([^;]+)é   ú,)	r   r   r%   ÚreÚsearchÚgroupÚsplitÚstripr"   )r&   Ú
decoded_idÚmatchÚmodels       r   Ú*extract_target_model_names_from_unified_idr2   J   s‹   € ð ä˜*¤cÔ*ØˆIô 9¸ÓDˆ
ÙØ#ˆJô —y‘yÐ!>À
ÓKˆÙà/4¯{©{¸1«~×/CÑ/CÀCÓ/HÖI e�E—K‘K•MÒIÐIàˆ	ùò Jøô ò ØŠ	ðús/   ‚A> ”A	A> ÁA9Á4A> Á7A> Á9A> Á>BÂBc                 óÒ   — 	 t        | t        «      syt        | «      }|r|} t        j                  d| «      }|r|j                  d«      j                  «       S y# t        $ r Y yw xY w)a.  
    Extract resource type from a unified resource ID.

    Args:
        unified_id: The unified resource ID (decoded or encoded)

    Returns:
        Resource type string or None

    Example:
        unified_id = "litellm_proxy:vector_store;unified_id,uuid;..."
        returns: "vector_store"
    Nzlitellm_proxy:([^;]+)r(   ©r   r   r%   r*   r+   r,   r.   r"   ©r&   r/   r0   s      r   Ú%extract_resource_type_from_unified_idr6   o   sj   € ð ä˜*¤cÔ*Øô 9¸ÓDˆ
ÙØ#ˆJô —y‘yÐ!9¸:ÓFˆÙØ—;‘;˜q“>×'Ñ'Ó)Ð)àøÜò Ùðúó   ‚A “AA Á	A&Á%A&c                 óÒ   — 	 t        | t        «      syt        | «      }|r|} t        j                  d| «      }|r|j                  d«      j                  «       S y# t        $ r Y yw xY w)a  
    Extract the UUID from a unified resource ID.

    Args:
        unified_id: The unified resource ID (decoded or encoded)

    Returns:
        UUID string or None

    Example:
        unified_id = "litellm_proxy:vector_store;unified_id,abc-123;..."
        returns: "abc-123"
    Nzunified_id,([^;]+)r(   r4   r5   s      r   Ú$extract_unified_uuid_from_unified_idr9   “   sj   € ð ä˜*¤cÔ*Øô 9¸ÓDˆ
ÙØ#ˆJô —y‘yÐ!6¸
ÓCˆÙØ—;‘;˜q“>×'Ñ'Ó)Ð)àøÜò Ùðúr7   c                 óÒ   — 	 t        | t        «      syt        | «      }|r|} t        j                  d| «      }|r|j                  d«      j                  «       S y# t        $ r Y yw xY w)a2  
    Extract model ID from a unified resource ID.

    Args:
        unified_id: The unified resource ID (decoded or encoded)

    Returns:
        Model ID string or None

    Example:
        unified_id = "litellm_proxy:vector_store;...;model_id,gpt-4-model-id;..."
        returns: "gpt-4-model-id"
    Nz(?:^|;)model_id,([^;]+)r(   r4   r5   s      r   Ú extract_model_id_from_unified_idr;   ·   sj   € ð ä˜*¤cÔ*Øô 9¸ÓDˆ
ÙØ#ˆJô —y‘yÐ!;¸ZÓHˆÙØ—;‘;˜q“>×'Ñ'Ó)Ð)àøÜò Ùðúr7   c                 óì   — 	 t        | t        «      syt        | «      }|r|} g d¢}|D ];  }t        j                  || «      }|sŒ|j                  d«      j                  «       c S  y# t        $ r Y yw xY w)aC  
    Extract provider resource ID from a unified resource ID.

    Args:
        unified_id: The unified resource ID (decoded or encoded)

    Returns:
        Provider resource ID string or None

    Example:
        unified_id = "litellm_proxy:vector_store;...;resource_id,vs_abc123;..."
        returns: "vs_abc123"
    N)zresource_id,([^;]+)zvector_store_id,([^;]+)zfile_id,([^;]+)r(   r4   )r&   r/   ÚpatternsÚpatternr0   s        r   Ú,extract_provider_resource_id_from_unified_idr?   á   s�   € ð ä˜*¤cÔ*Øô 9¸ÓDˆ
ÙØ#ˆJò
ˆð  ò 	.ˆGÜ—I‘I˜g zÓ2ˆEÚØ—{‘{ 1“~×+Ñ+Ó-Ò-ð	.ð
 øÜò Ùðús"   ‚A' “0A' Á A' Á%A' Á'	A3Á2A3Úresource_typeÚunified_uuidÚtarget_model_namesÚprovider_resource_idÚmodel_idÚadditional_fieldsc                 óÎ   — d| › �d|› �ddj                  |«      › �d|› �d|› �g}|r.|j                  «       D ]  \  }}|j                  |› d|› �«       Œ dj                  |«      S )a€  
    Generate a unified ID string (before base64 encoding).

    Args:
        resource_type: Type of resource (e.g., "vector_store", "file")
        unified_uuid: UUID for this unified resource
        target_model_names: List of target model names
        provider_resource_id: Resource ID from the provider
        model_id: Model ID from the router
        additional_fields: Additional fields to include in the ID

    Returns:
        Unified ID string (not yet base64 encoded)

    Example:
        generate_unified_id_string(
            resource_type="vector_store",
            unified_uuid="abc-123",
            target_model_names=["gpt-4", "gemini"],
            provider_resource_id="vs_xyz",
            model_id="model-id-123",
        )
        returns: "litellm_proxy:vector_store;unified_id,abc-123;target_model_names,gpt-4,gemini;resource_id,vs_xyz;model_id,model-id-123"
    úlitellm_proxy:zunified_id,ztarget_model_names,r)   zresource_id,z	model_id,ú;)ÚjoinÚitemsÚappend)	r@   rA   rB   rC   rD   rE   ÚpartsÚkeyr   s	            r   Úgenerate_unified_id_stringrN     s“   € ðD ˜˜Ð(Ø
�l�^Ð$Ø
˜cŸh™hÐ'9Ó:Ð;Ð<Ø
Ð+Ð,Ð-Ø
�H�:Ðð€Eñ Ø+×1Ñ1Ó3ò 	+‰JˆC�Ø�L‰L˜C˜5  % Ð)Õ*ð	+ð �8‰8�E‹?Ðr   Úunified_id_stringc                 ó‚   — t        j                  | j                  «       «      j                  «       j	                  d«      S )zÂ
    Encode a unified ID string to base64.

    Args:
        unified_id_string: The unified ID string to encode

    Returns:
        Base64 encoded unified ID (URL-safe, padding stripped)
    r   )r   Úurlsafe_b64encodeÚencoder    Úrstrip)rO   s    r   Úencode_unified_idrT   =  s4   € ô ×#Ñ#Ð$5×$<Ñ$<Ó$>Ó?×FÑFÓH×OÑOÐPSÓTÐTr   Úencoded_unified_idc                 óº   — 	 | dt        | «       dz  z  z   }t        j                  |«      j                  «       }|j	                  d«      r|S y# t
        $ r Y yw xY w)zµ
    Decode a base64 encoded unified ID.

    Args:
        encoded_unified_id: The base64 encoded unified ID

    Returns:
        Decoded unified ID string or None if invalid
    r   r   rG   N)r   r   r   r    r!   r"   )rU   r#   r$   s      r   Údecode_unified_idrW   J  sj   € ðà*¨S´SÐ9KÓ5LÐ4LÈqÑ4PÑ-QÑQˆô  ×1Ñ1°&Ó9×@Ñ@ÓBˆð ×ÑÐ.Ô/ØˆNàøÜò Ùðús   ‚A
A Á	AÁAc                 óÐ   — 	 t        | «      }|s| j                  d«      r| }nyt        |«      t        |«      t	        |«      t        |«      t        |«      dœS # t        $ r Y yw xY w)aÁ  
    Parse a unified ID into its components.

    Args:
        unified_id: The unified ID (encoded or decoded)

    Returns:
        Dictionary with parsed components or None if invalid

    Example:
        {
            "resource_type": "vector_store",
            "unified_uuid": "abc-123",
            "target_model_names": ["gpt-4", "gemini"],
            "provider_resource_id": "vs_xyz",
            "model_id": "model-id-123"
        }
    rG   N)r@   rA   rB   rC   rD   )rW   r!   r6   r9   r2   r?   r;   r"   )r&   r/   s     r   Úparse_unified_idrY   d  su   € ð*ä& zÓ2ˆ
Ùà×$Ñ$Ð%5Ô6Ø'‘
àô CÀ:ÓNÜ@ÀÓLÜ"LÈZÓ"XÜ$PÐQ[Ó$\Ü8¸ÓDñ
ð 	
øô ò Ùðús   ‚!A ¤4A Á	A%Á$A%)rG   )N)Ú__doc__r   r*   Útypingr   r   r   r   Ú__annotations__r   r   r%   Úlistr2   r6   r9   r;   r?   ÚdictrN   rT   rW   rY   © r   r   ú<module>r`      s‡  ðòó Û 	ß &Ñ &à0EÐ &¨Ó EðØðàˆ4�Zóð: #ñØðàðð 	ˆ7�5‰>Ñóð@"Øð"à	ˆ#�Yó"ðJ!Øð!àˆ4�Zó!ðH!Øð!àˆ4�Zó!ðH'Øð'àˆ4�Zó'ðT(Øð(àˆ4�Zó(ðb &*ñ.Øð.àð.ð ˜S™	ð.ð ð	.ð
 ð.ð ˜d‘{ð.ð 	ó.ðb
U¨ð 
U°ó 
Uð¨#ð °#¸±*ó ð4'Øð'à	ˆD�[ô'r   