Ë
    tPêjá<  ã                   óD   — d dl mZmZ d dlmZmZ d dlmZ  G d„ d«      Zy)é    )ÚMappingÚSequence)ÚAnyÚFinal)Úverbose_proxy_loggerc                   ód  — e Zd ZdZg d¢Zg d¢ZddgZedee	e
f   dz  fd„«       Zed	ee	ef   d
e	dee	e
f   ddfd„«       Zed	ee	ef   dee	   de	ddfd„«       Zed	ee	ef   dee	ee	ef   f   ddfd„«       Zededefd„«       Zedee	ef   dee	ef   fd„«       Zedee	e
f   dee	e
f   fd„«       Zed	ee	e
f   ded
e	dee	   de	dee	e
f   fd„«       Zed	ee	e
f   dee	e
f   fd„«       Zed	ee	e
f   dee	e
f   fd„«       Zed	ee	e
f   dee	e
f   fd„«       Zed	ee	e
f   dee	e
f   fd„«       Zy)ÚCustomOpenAPISpeczˆ
    Handler for customizing OpenAPI specifications with Pydantic models
    for documentation purposes without runtime validation.
    )z/v1/chat/completionsz/chat/completionsz!/engines/{model}/chat/completionsz,/openai/deployments/{model}/chat/completions)z/v1/embeddingsz/embeddingsz/engines/{model}/embeddingsz&/openai/deployments/{model}/embeddingsz/v1/responsesz
/responsesÚreturnNc                 óÔ   — 	 | j                  «       S # t        $ r$ 	 | j                  «       cY S # t        $ r Y Y yw xY wt        $ r!}t	        j
                  d| |«       Y d}~yd}~ww xY w)zÔ
        Get JSON schema from a Pydantic model, handling both v1 and v2 APIs.

        Args:
            model_class: Pydantic model class

        Returns:
            JSON schema dict or None if failed
        Nz$Failed to generate schema for %s: %s)Úmodel_json_schemaÚAttributeErrorÚschemaÚ	Exceptionr   Údebug)Úmodel_classÚes     ús/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/litellm/proxy/common_utils/custom_openapi_spec.pyÚget_pydantic_schemaz%CustomOpenAPISpec.get_pydantic_schema   so   € ð	à×0Ñ0Ó2Ð2øÜò 	ðà"×)Ñ)Ó+Ò+øÜ!ò âðúô ò 	ô !×&Ñ&Ð'MÈ{Ð\]Ô^Üûð		ús0   ‚ ’	A'œ.«A'®	;·A'º;»A'ÁA"Á"A'Úopenapi_schemaÚschema_nameÚ
schema_defc                 ód   — d| vri | d<   d| d   vri | d   d<   t         j                  | ||i«       y)a  
        Add a schema definition to the OpenAPI components/schemas section.

        Args:
            openapi_schema: The OpenAPI schema dict to modify
            schema_name: Name for the schema component
            schema_def: The schema definition
        Ú
componentsÚschemasN)r	   Ú_move_defs_to_components)r   r   r   s      r   Úadd_schema_to_componentsz*CustomOpenAPISpec.add_schema_to_components8   sJ   € ð ˜~Ñ-Ø+-ˆN˜<Ñ(Ø˜N¨<Ñ8Ñ8Ø68ˆN˜<Ñ(¨Ñ3ô 	×2Ñ2°>ÀKÐQ[ÐC\Õ]ó    ÚpathsÚ
schema_refc                 óò  — |D �]l  }|| j                  di «      v sŒd| d   |   v sŒ$|j                  d«      d   }| j                  di «      j                  di «      j                  |i «      }|j                  di «      }|j                  dg «      }d	|v rt        j                  | |d	   «       d
|i dœ}|j	                  «       D ]E  \  }	}
t        j                  |
«      }t        j                  |«      }|	dk(  r	dddœg|d<   ||d   |	<   ŒG ddd|iidœ| d   |   d   d<   d| d   |   d   v s�Œ-| d   |   d   d   }|D �cg c]  }|j                  d«      dk(  sŒ|‘Œ }}|| d   |   d   d<   �Œo yc c}w )aŽ  
        Add request body with expanded form fields for better Swagger UI display.
        This keeps the request body but expands it to show individual fields in the UI.

        Args:
            openapi_schema: The OpenAPI schema dict to modify
            paths: List of paths to update
            schema_ref: Reference to the schema component (e.g., "#/components/schemas/ModelName")
        r   Úpostú/éÿÿÿÿr   r   Ú
propertiesÚrequiredú$defsÚobject)Útyper%   r$   ÚmessagesÚuserzHello, how are you?)ÚroleÚcontentÚexampleTzapplication/jsonr   )r%   r,   ÚrequestBodyÚ
parametersÚinÚpathN)ÚgetÚsplitr	   r   ÚitemsÚ_expand_field_definitionÚ_rewrite_defs_refs)r   r   r   r1   r   Úactual_schemaÚschema_propertiesÚrequired_fieldsÚexpanded_schemaÚ
field_nameÚ	field_defÚexpanded_fieldÚexisting_paramsÚparamÚfiltered_paramss                  r   Úadd_request_body_to_pathsz+CustomOpenAPISpec.add_request_body_to_pathsM   sí  € ð ó -	ZˆDØ�~×)Ñ)¨'°2Ó6Ò6¸6À^ÐT[ÑE\Ð]aÑEbÒ;bà(×.Ñ.¨sÓ3°BÑ7�Ø .× 2Ñ 2°<ÀÓ D× HÑ HÈÐTVÓ W× [Ñ [Ð\gÐikÓ l�Ø$1×$5Ñ$5°lÀBÓ$GÐ!Ø"/×"3Ñ"3°JÀÓ"C�ð ˜mÑ+Ü%×>Ñ>¸~È}Ð]dÑOeÔfð
 %Ø /Ø"$ñ#�ð .?×-DÑ-DÓ-Fò 
OÑ)�J 	Ü%6×%OÑ%OÐPYÓ%Z�Nô &7×%IÑ%IÈ.Ó%Y�Nð " ZÒ/Ø>DÐQfÑ5gÐ4h˜ yÑ1à@N�O LÑ1°*Ò=ð
Oð !%Ø 2°X¸Ð4OÐPñH�˜wÑ'¨Ñ-¨fÑ5°mÑDð   >°'Ñ#:¸4Ñ#@ÀÑ#HÓHØ&4°WÑ&=¸dÑ&CÀFÑ&KÈLÑ&Y�Oà:IÖ&g°ÈUÏYÉYÐW[Ë_Ð`fÓMf¢uÐ&g�OÐ&gØJY�N 7Ñ+¨DÑ1°&Ñ9¸,ÓGñ[-	ZùòX 'hs   ÅE4ÅE4Údefsc                 óæ   — |syd| vri | d<   d| d   vri | d   d<   |j                  «       D ]C  \  }}t        j                  |«      }|| d   d   |<   d|v sŒ+t        j                  | |d   «       ŒE y)a&  
        Move $defs from Pydantic v2 schema to OpenAPI components/schemas.
        This makes the definitions resolvable in Swagger/OpenAPI viewers.

        Args:
            openapi_schema: The OpenAPI schema dict to modify
            defs: The $defs dictionary from Pydantic schema
        Nr   r   r&   )r4   r	   r6   r   )r   rB   Údef_nameÚ
def_schemaÚrewritten_defs        r   r   z*CustomOpenAPISpec._move_defs_to_components‡   sœ   € ñ Øð ˜~Ñ-Ø+-ˆN˜<Ñ(Ø˜N¨<Ñ8Ñ8Ø68ˆN˜<Ñ(¨Ñ3ð %)§J¡J£Lò 	`Ñ ˆH�jä-×@Ñ@ÀÓLˆMØ@MˆN˜<Ñ(¨Ñ3°HÑ=ð ˜*Ò$Ü!×:Ñ:¸>È:ÐV]ÑK^Õ_ñ	`r   r   c                 óŒ  — t        | t        «      r{i }| j                  «       D ]d  \  }}|dk(  r<t        |t        «      r,|j	                  d«      r|j                  dd«      }d|› �||<   ŒG|dk(  rŒMt        j                  |«      ||<   Œf |S t        | t        «      r#| D �cg c]  }t        j                  |«      ‘Œ c}S | S c c}w )aJ  
        Recursively rewrite $ref values from #/$defs/... to #/components/schemas/...
        This converts Pydantic v2 references to OpenAPI-compatible references.

        Args:
            schema: Schema object to process (can be dict, list, or primitive)

        Returns:
            Schema with rewritten references
        z$refz#/$defs/Ú ú#/components/schemas/r&   )	Ú
isinstanceÚdictr4   ÚstrÚ
startswithÚreplacer	   r6   Úlist)r   ÚresultÚkeyÚvaluerD   Úitems         r   r6   z$CustomOpenAPISpec._rewrite_defs_refs¤   sÅ   € ô �fœdÔ#ØˆFØ$Ÿl™l›nò 
N‘
��UØ˜&’=¤Z°´sÔ%;À×@PÑ@PÐQ[Ô@\à$Ÿ}™}¨Z¸Ó<�HØ$9¸(¸Ð"D�F˜3’KØ˜G’^àô #4×"FÑ"FÀuÓ"M�F˜3’Kð
Nð ˆMÜ˜¤Ô%ØKQÖRÀ4Ô%×8Ñ8¸Õ>ÒRÐRàˆMùò Ss   Â Cr<   c                 óx   — d| v rd| d   iS d| v r(| d   }|D ]  }|j                  d«      dk7  sŒ|c S  ddiS ddiS )zç
        Extract a simple schema from a Pydantic field definition for parameter display.

        Args:
            field_def: Pydantic field definition

        Returns:
            Simplified schema for OpenAPI parameter
        r(   ÚanyOfÚnullÚstring)r2   )r<   Úany_ofÚoptions      r   Ú_extract_field_schemaz'CustomOpenAPISpec._extract_field_schemaÃ   so   € ð �YÑØ˜I fÑ-Ð.Ð.ð �iÑØ% gÑ.ˆFà ò "�Ø—:‘:˜fÓ%¨Ó/Ø!’Mð"ð ˜HÐ%Ð%ð ˜Ð!Ð!r   c                 ó"   — | j                  «       S )aD  
        Expand a Pydantic field definition for inline use in OpenAPI schema.
        This creates a full field definition that Swagger UI can render as individual form fields.

        Args:
            field_def: Pydantic field definition

        Returns:
            Expanded field definition for OpenAPI schema
        )Úcopy)r<   s    r   r5   z*CustomOpenAPISpec._expand_field_definitionß   s   € ð �~‰~ÓÐr   r   Úoperation_namec                 óR  — 	 t         j                  |«      }|�It         j                  | ||«       t         j                  | |d|› �«       t	        j
                  d|«       | S t	        j
                  d|«       	 | S # t        $ r"}t	        j
                  d||«       Y d}~| S d}~ww xY w)aò  
        Generic method to add a request schema to OpenAPI specification.

        Args:
            openapi_schema: The OpenAPI schema dict to modify
            model_class: The Pydantic model class to get schema from
            schema_name: Name for the schema component
            paths: List of paths to add the request body to
            operation_name: Name of the operation for logging (e.g., "chat completion", "embedding")

        Returns:
            Modified OpenAPI schema
        NrI   z,Successfully added %s schema to OpenAPI speczCould not get schema for %sz#Failed to add %s request schema: %s)r	   r   r   rA   r   r   r   )r   r   r   r   r]   Úrequest_schemar   s          r   Úadd_request_schemaz$CustomOpenAPISpec.add_request_schemaî   sº   € ð*	aä$5×$IÑ$IÈ+Ó$VˆNð Ð)ä!×:Ñ:¸>È;ÐXfÔgô "×;Ñ;Ø" EÐ-BÀ;À-Ð+Pôô %×*Ñ*Ð+YÐ[fÔgð Ðô %×*Ñ*Ð+HÈ+ÕVð Ðøô	 ò 	aä ×&Ñ&Ð'LÈnÐ^_×`Ð`àÐûð		aús   ‚AA; Á"A; Á;	B&ÂB!Â!B&c                 ó¼   — 	 ddl m} t        j                  | |dt        j                  d¬«      S # t
        $ r"}t        j                  d|«       | cY d}~S d}~ww xY w)a5  
        Add ProxyChatCompletionRequest schema to chat completion endpoints for documentation.
        This shows the request body in Swagger without runtime validation.

        Args:
            openapi_schema: The OpenAPI schema dict to modify

        Returns:
            Modified OpenAPI schema
        r   )ÚProxyChatCompletionRequestrb   zchat completion©r   r   r   r   r]   z/Failed to import ProxyChatCompletionRequest: %sN)Úlitellm.proxy._typesrb   r	   r`   ÚCHAT_COMPLETION_PATHSÚImportErrorr   r   )r   rb   r   s      r   Ú"add_chat_completion_request_schemaz4CustomOpenAPISpec.add_chat_completion_request_schema  sa   € ð	"ÝGä$×7Ñ7Ø-Ø6Ø8Ü'×=Ñ=Ø0ð 8ó ð øô ò 	"Ü ×&Ñ&Ð'XÐZ[Ô\Ø!Õ!ûð	"úó   ‚-0 °	A¹AÁAÁAc                 ó¼   — 	 ddl m} t        j                  | |dt        j                  d¬«      S # t
        $ r"}t        j                  d|«       | cY d}~S d}~ww xY w)a%  
        Add EmbeddingRequest schema to embedding endpoints for documentation.
        This shows the request body in Swagger without runtime validation.

        Args:
            openapi_schema: The OpenAPI schema dict to modify

        Returns:
            Modified OpenAPI schema
        r   )ÚEmbeddingRequestrj   Ú	embeddingrc   z%Failed to import EmbeddingRequest: %sN)Úlitellm.types.embeddingrj   r	   r`   ÚEMBEDDING_PATHSrf   r   r   )r   rj   r   s      r   Úadd_embedding_request_schemaz.CustomOpenAPISpec.add_embedding_request_schema7  sa   € ð	"Ý@ä$×7Ñ7Ø-Ø,Ø.Ü'×7Ñ7Ø*ð 8ó ð øô ò 	"Ü ×&Ñ&Ð'NÐPQÔRØ!Õ!ûð	"úrh   c                 ó¼   — 	 ddl m} t        j                  | |dt        j                  d¬«      S # t
        $ r"}t        j                  d|«       | cY d}~S d}~ww xY w)a2  
        Add ResponsesAPIRequestParams schema to responses API endpoints for documentation.
        This shows the request body in Swagger without runtime validation.

        Args:
            openapi_schema: The OpenAPI schema dict to modify

        Returns:
            Modified OpenAPI schema
        r   )ÚResponsesAPIRequestParamsrp   zresponses APIrc   z.Failed to import ResponsesAPIRequestParams: %sN)Úlitellm.types.llms.openairp   r	   r`   ÚRESPONSES_API_PATHSrf   r   r   )r   rp   r   s      r   Ú add_responses_api_request_schemaz2CustomOpenAPISpec.add_responses_api_request_schemaQ  sa   € ð	"ÝKä$×7Ñ7Ø-Ø5Ø7Ü'×;Ñ;Ø.ð 8ó ð øô ò 	"Ü ×&Ñ&Ð'WÐYZÔ[Ø!Õ!ûð	"úrh   c                 ó„   — t         j                  | «      } t         j                  | «      } t         j                  | «      } | S )zï
        Add LLM API request schema bodies to OpenAPI specification for documentation.

        Args:
            openapi_schema: The base OpenAPI schema

        Returns:
            OpenAPI schema with added request body schemas
        )r	   rg   rn   rs   )r   s    r   Úadd_llm_api_request_schema_bodyz1CustomOpenAPISpec.add_llm_api_request_schema_bodym  s>   € ô +×MÑMÈnÓ]ˆô +×GÑGÈÓWˆô +×KÑKÈNÓ[ˆàÐr   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__re   rm   rr   Ústaticmethodr   rL   r'   r   rK   r   r   r   rA   r   r6   rZ   r5   r(   r`   rg   rn   rs   ru   © r   r   r	   r	      sî  „ ñò
Ðò€Oð +¨LÐ9Ðàð¨G°C¸°KÑ,@À4Ñ,Gò ó ðð4 ð^Ø˜S #˜X™ð^Ø58ð^ØFMÈcÐSYÈkÑFZð^à	ò^ó ð^ð( ð7Z°$°s¸C°x±.ð 7ZÈÐRUÉð 7ZÐdgð 7ZÐlpò 7Zó ð7Zðr ð`°°c¸3°h±ð `ÀwÈsÐT[Ð\_ÐadÐ\dÑTeÐOeÑGfð `Ðkoò `ó ð`ð8 ð 3ð ¨3ò ó ðð< ð"¨¨c°3¨h©ð "¸DÀÀcÀ¹Nò "ó ð"ð6 ð ¨D°°f°Ñ,=ð  À$ÀsÈFÀ{ÑBSò  ó ð ð ð*Ø˜S &˜[Ñ)ð*àð*ð ð*ð ˜‰}ð	*ð
 ð*ð 
ˆc�6ˆkÑ	ò*ó ð*ðX ð"Ø˜S &˜[Ñ)ð"à	ˆc�6ˆkÑ	ò"ó ð"ð6 ð"°T¸#¸v¸+Ñ5Fð "È4ÐPSÐU[ÐP[ÑK\ò "ó ð"ð2 ð"Ø˜S &˜[Ñ)ð"à	ˆc�6ˆkÑ	ò"ó ð"ð6 ðØ˜S &˜[Ñ)ðà	ˆc�6ˆkÑ	òó ñr   r	   N)	Úcollections.abcr   r   Útypingr   r   Úlitellm._loggingr   r	   r{   r   r   ú<module>r      s   ðß -ß å 1÷|ò |r   