Ë
    tPêj“’  ã                   óš  — U d dl Z d dlZd dlZd dlmZmZmZ d dlmZmZ d dl	m
Z
mZ d dlmZmZmZ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 d d	lmZ d d
lmZ d dlmZ d dl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+ d dl,m-Z- d dl.m/Z/ d dl0m1Z1 d dl2m3Z3 d dl4m5Z5 d dl6m7Z7 d dl8m9Z9 d dl:m;Z;m<Z<m=Z=m>Z>m?Z? ddl@m&ZA ddlBmCZCmDZDmEZEmFZFmGZGmHZH erd dlImJZK  G d„ de«      ZLde3d d!fd"„ZMe?jœ                  jž                  eCe?j                   jž                  eEe?j¢                  jž                  eFe?j¤                  jž                  eGe?j¦                  jž                  eDe?j¨                  jž                  eHe?jª                  jž                  e'e?j¬                  jž                  eAiZWeeXd#<    ej²                  d$«      ZZeeXd%<   e?jœ                  jž                  e#e?jª                  jž                  e%e?j                   jž                  e)e?j¢                  jž                  e+e?j¤                  jž                  e-e?j¨                  jž                  e/iZ[ee\e]e^e   f      eXd&<   d'„ Z_d(„ Z`e[jÃ                   e`«       «        e_«       ZbeeXd)<   eWjÃ                  eb«        G d*„ d+«      Zcd,ed-e>d dfd.„Zd G d/„ d0«      Ze ee«       ZfeeXd1<    e jÎ                  «       ZheeXd2<   y)3é    N)ÚCallableÚIteratorÚMapping)ÚdatetimeÚtimezone)ÚchainÚcount)ÚTYPE_CHECKINGÚFinalÚLiteralÚOptionalÚProtocolÚcast)ÚValidationError)ÚRouter)Úverbose_proxy_logger)Úuuid)ÚCustomGuardrail)Ú
safe_dumps)Ú.effective_scan_only_tool_results_for_guardrailÚ)effective_skip_tool_message_for_guardrail)ÚBedrockGuardrail)ÚGraySwanGuardrail)Úinitialize_guardrail)ÚlakeraAI_Moderation)ÚLakeraAIGuardrail)Ú_OPTIONAL_PresidioPIIMasking)ÚToolPermissionGuardrail)Úget_instance_fn)ÚPrismaClient)ÚTableActions)ÚGuardrailsRepository)Ú
get_secret)Ú	GuardrailÚGuardrailEventHooksÚLakeraCategoryThresholdsÚLitellmParamsÚSupportedGuardrailIntegrationsé   )Úinitialize_bedrockÚinitialize_hide_secretsÚinitialize_lakeraÚinitialize_lakera_v2Úinitialize_presidioÚinitialize_tool_permission)Úmodelsc                   ó>   — e Zd Zedefd„«       Zdeeeef      fd„Z	y)Ú_GuardrailRowLikeÚreturnc                  ó   — y ©N© ©Úselfs    úp/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/litellm/proxy/guardrails/guardrail_registry.pyÚguardrail_idz_GuardrailRowLike.guardrail_idF   s   € Ø#&ó    c                  ó   — y r5   r6   r7   s    r9   Ú__iter__z_GuardrailRowLike.__iter__H   s   � r;   N)
Ú__name__Ú
__module__Ú__qualname__ÚpropertyÚstrr:   r   ÚtupleÚobjectr=   r6   r;   r9   r2   r2   E   s+   „ ØØ&˜cÒ&ó Ø&Ø;˜( 5¨¨f¨Ñ#5Ñ6Ô;r;   r2   Úprisma_clientr3   z3TableActions[prisma_models.LiteLLM_GuardrailsTable]c                 ó,   — t        | «      j                  S )zLTyped view of the guardrails table actions exposed by the Prisma repository.)r"   Útable)rE   s    r9   Ú_guardrail_tablerH   K   s   € ä Ó.×4Ñ4Ð4r;   Úguardrail_initializer_registryz$625f63f4-935a-50e5-98b5-fbe77babc74aÚCONFIG_GUARDRAIL_ID_NAMESPACEÚguardrail_class_registryc            	      ój  — i } 	 t         j                  j                  t        «      }t         j                  j	                  |d«      }t         j                  j                  |«      st        j                  d«       | S t        j                  |«      D �]b  }t         j                  j	                  ||«      }t         j                  j                  |«      r|j                  d«      rŒUt         j                  j	                  |d«      }t         j                  j                  |«      sŒ•d|› �}	 t        j                  d|«       t        j                  |«      }t        |d«      r]t        |dd«      }t        |t         «      rt| j#                  |«       t        j                  d	|t%        |j'                  «       «      «       n4t        |d
«      r(t        |d
d«      }	|	| |<   t        j                  d|«       �Œe t        j                  dt/        | «      t%        | j'                  «       «      «       | S # t(        $ r"}
t        j*                  d||
«       Y d}
~
�ŒÅd}
~
wt,        $ r"}
t        j*                  d||
«       Y d}
~
�Œîd}
~
ww xY w# t,        $ r!}
t        j*                  d|
«       Y d}
~
| S d}
~
ww xY w)a€  
    Get guardrail initializers by discovering them from the guardrail_hooks directory structure.

    Scans the guardrail_hooks directory for subdirectories containing __init__.py files
    with either guardrail_initializer_registry or initialize_guardrail functions.

    Returns:
        Dict[str, Callable]: A dictionary mapping guardrail types to their initializer functions
    Úguardrail_hooksú#guardrail_hooks directory not foundÚ__ú__init__.pyú)litellm.proxy.guardrails.guardrail_hooks.úDiscovering guardrails in: %srI   Nz.Found guardrail_initializer_registry in %s: %sr   z)Found initialize_guardrail function in %súCould not import %s: %súError processing %s: %sz(Discovered %s guardrail initializers: %sú,Error discovering guardrail initializers: %s)ÚosÚpathÚdirnameÚ__file__ÚjoinÚexistsr   ÚdebugÚlistdirÚisdirÚ
startswithÚ	importlibÚimport_moduleÚhasattrÚgetattrÚ
isinstanceÚdictÚupdateÚlistÚkeysÚImportErrorÚerrorÚ	ExceptionÚlen)Údiscovered_initializersÚcurrent_dirÚ	hooks_dirÚitemÚ	item_pathÚ	init_fileÚmodule_pathÚmoduleÚregistryÚinitialize_fnÚes              r9   Ú$get_guardrail_initializer_from_hooksrx   g   sO  € ð &(Ðð=VäŸW™WŸ_™_¬XÓ6ˆÜŸ7™7Ÿ<™<¨Ð5FÓGˆ	ä�w‰w�~‰~˜iÔ(Ü ×&Ñ&Ð'LÔMØ*Ð*ô —J‘J˜yÓ)ó *	ˆDÜŸ™Ÿ™ Y°Ó5ˆIô —7‘7—=‘= Ô+¨t¯©¸tÔ/DØô Ÿ™Ÿ™ Y°Ó>ˆIÜ—7‘7—>‘> )Ô,ØàEÀdÀVÐLˆKðä$×*Ñ*Ð+JÈKÔXä"×0Ñ0°Ó=�ô ˜6Ð#CÔDÜT[ØÐ @À$óU�Hô " (¬DÔ1Ø/×6Ñ6°xÔ@Ü,×2Ñ2ØLÈkÔ[_Ð`h×`mÑ`mÓ`oÓ[põô
 ˜VÐ%;Ô<äKRÐSYÐ[qÐswÓKx�MØ4AÐ+¨DÑ1Ü(×.Ñ.Ð/ZÐ\gÔhùðG*	ôX 	×"Ñ"Ø6ÜÐ'Ó(ÜÐ(×-Ñ-Ó/Ó0ô	
ð #Ð"øô! ò Ü$×*Ñ*Ð+DÀkÐSTÔUÝûÜò Ü$×*Ñ*Ð+DÀkÐSTÔUÝûðûô ò VÜ×"Ñ"Ð#QÐST×UÐUà"Ð"ûðVús\   „A8J Á=B/J Ä-CH1Ç5:J È1	JÈ:IÉJ ÉJÉ#J É:J Ê JÊJ Ê	J2ÊJ-Ê-J2c                  ó8  — 	 i } 	 t         j                  j                  t        «      }t         j                  j	                  |d«      }t         j                  j                  |«      st        j                  d«       | S t        j                  |«      D ]ÿ  }t         j                  j	                  ||«      }t         j                  j                  |«      r|j                  d«      rŒTt         j                  j	                  |d«      }t         j                  j                  |«      sŒ”d|› �}	 t        j                  d|«       t        j                  |«      }t        |d«      r.t        |dd«      }t        |t         «      r| j#                  |«       �Œ 	 | S # t$        $ r"}	t        j                  d	||	«       Y d}	~	�Œ+d}	~	wt&        $ r"}	t        j(                  d
||	«       Y d}	~	�ŒTd}	~	ww xY w# t&        $ r!}	t        j*                  d|	«       Y d}	~	| S d}	~	ww xY w)za
    Get guardrail classes by discovering them from the guardrail_hooks directory structure.
    rM   rN   rO   rP   rQ   rR   rK   NrS   rT   rU   )rV   rW   rX   rY   rZ   r[   r   r\   r]   r^   r_   r`   ra   rb   rc   rd   re   rf   ri   rk   Ú	exceptionrj   )
Údiscovered_classesrn   ro   rp   rq   rr   rs   rt   ru   rw   s
             r9   Úget_guardrail_class_from_hooksr|   µ   sÅ  € ðð !#Ðð/VäŸW™WŸ_™_¬XÓ6ˆÜŸ7™7Ÿ<™<¨Ð5FÓGˆ	ä�w‰w�~‰~˜iÔ(Ü ×&Ñ&Ð'LÔMØ%Ð%ô —J‘J˜yÓ)ò "	ˆDÜŸ™Ÿ™ Y°Ó5ˆIô —7‘7—=‘= Ô+¨t¯©¸tÔ/DØô Ÿ™Ÿ™ Y°Ó>ˆIä—7‘7—>‘> )Ô,ØàEÀdÀVÐLˆKðä$×*Ñ*Ð+JÈKÔXä"×0Ñ0°Ó=�ô ˜6Ð#=Ô>ÜKRØÐ :¸DóL�Hô " (¬DÔ1Ø*×1Ñ1°(Ô;ùñ7"	ðN Ðøô ò Ü$×*Ñ*Ð+DÀkÐSTÔUÝûÜò Ü$×.Ñ.Ð/HÈ+ÐWXÔYÝûðûô ò VÜ×"Ñ"Ð#QÐST×UÐUàÐûðVús\   …A8G/ Á>B.G/ Ä-A%FÆG/ Æ	G,Æ!F>Æ8G/ Æ>G,Ç
G'Ç!G/ Ç'G,Ç,G/ Ç/	HÇ8HÈHÚ_discovered_initializersc                   ó´   — e Zd ZdZd„ Zdededz  fd„Zdede	fd	„Z
d
ede	fd„Zd
edede	fd„Zede	dee   fd„«       Zd
ede	dedz  fd„Zdede	dedz  fd„Zy)ÚGuardrailRegistryzi
    Registry for guardrails

    Handles adding, removing, and getting guardrails in DB + in memory
    c                  ó   — y r5   r6   r7   s    r9   Ú__init__zGuardrailRegistry.__init__  s   € Ør;   Úguardrail_namer3   Nc                 ó¢   — t         j                  j                  t        ¬«      }|D ]&  }t	        |t        «      sŒ|j
                  |k(  sŒ$|c S  y)zW
        Returns the initialized guardrail callback for a given guardrail name
        )Úcallback_typeN)ÚlitellmÚlogging_callback_managerÚget_custom_loggers_for_typer   rd   r‚   )r8   r‚   Úactive_guardrailsÚactive_guardrails       r9   Ú"get_initialized_guardrail_callbackz4GuardrailRegistry.get_initialized_guardrail_callback  sS   € ô $×<Ñ<×XÑXÔgvÐXÓwÐØ 1ò 	,ÐÜÐ*¬OÕ<Ø#×2Ñ2°nÓDØ+Ò+ð	,ð r;   Ú	guardrailrE   c           	   ƒ   óF  K  — 	 |j                  d«      }|j                  di «      }t        |d«      r|j                  «       }n|rt        |«      ni }t	        |«      }t	        |j                  di «      «      }t        |«      j                  |||t        j                  t        j                  «      t        j                  t        j                  «      dœ¬«      ƒ d{  –—† }t        |«      }	|j                  |	d<   |	S 7 Œ # t        $ r}
t        d	|
› �«      ‚d}
~
ww xY w­w)
z1
        Add a guardrail to the database
        r‚   Úlitellm_paramsÚ
model_dumpÚguardrail_info)r‚   r�   r�   Ú
created_atÚ
updated_at)ÚdataNr:   zError adding guardrail to DB: )Úgetrb   rŽ   re   r   rH   Úcreater   Únowr   Úutcr:   rk   )r8   r‹   rE   r‚   Úlitellm_params_objÚlitellm_params_dictr�   r�   Úcreated_guardrailÚguardrail_dictrw   s              r9   Úadd_guardrail_to_dbz%GuardrailRegistry.add_guardrail_to_db  s  è ø€ ð	BØ$-§M¡MÐ2BÓ$CˆNà(1¯©Ð6FÈÓ(KÐÜÐ)¨<Ô8Ø&8×&CÑ&CÓ&EÑ#áBT¤dÐ+=Ô&>ÐZ\Ð#Ü)3Ð4GÓ)HˆNÜ)3°I·M±MÐBRÐTVÓ4WÓ)XˆNô AQÐQ^Ó@_×@fÑ@fà&4Ø&4Ø&4Ü"*§,¡,¬x¯|©|Ó"<Ü"*§,¡,¬x¯|©|Ó"<ñð Agó A÷ ;Ðô %)¨£OˆNØ->×-KÑ-KˆN˜>Ñ*à!Ð!ð;ùô ò 	BÜÐ<¸Q¸CÐ@ÓAÐAûð	Büs;   ‚D!„CD ÃD Ã D Ã?D!Ä D Ä	DÄDÄDÄD!r:   c              ƒ   ó¨   K  — 	 t        |«      j                  d|i¬«      ƒ d{  –—†  dd|› d�iS 7 Œ# t        $ r}t        d|› �«      ‚d}~ww xY w­w)z6
        Delete a guardrail from the database
        r:   ©ÚwhereNÚmessageú
Guardrail z deleted successfullyz"Error deleting guardrail from DB: )rH   Údeleterk   )r8   r:   rE   rw   s       r9   Údelete_guardrail_from_dbz*GuardrailRegistry.delete_guardrail_from_db>  so   è ø€ ð	Fä" =Ó1×8Ñ8ÀÐP\Ð?]Ð8Ó^×^Ð^à ¨L¨>Ð9NÐOÐPÐPð _ùô ò 	FÜÐ@ÀÀÐDÓEÐEûð	Füs0   ‚A„ 3 ¤1¥3 °A±3 ³	A¼A
Á
AÁAc           	   ƒ   ó  K  — 	 |j                  d«      }|j                  di «      }t        |d«      r|j                  «       }n|rt        |«      ni }t	        |«      }t	        |j                  di «      «      }t        |«      j                  d|i|||t        j                  t        j                  «      dœ¬«      ƒ d{  –—† }	|	€t        d	|› �«      ‚t        |	«      S 7 Œ# t        $ r}
t        d
|
› �«      ‚d}
~
ww xY w­w)z4
        Update a guardrail in the database
        r‚   r�   rŽ   r�   r:   )r‚   r�   r�   r‘   )rž   r’   Nz)Guardrail not found, passed guardrail_id=z Error updating guardrail in DB: )r“   rb   rŽ   re   r   rH   rf   r   r•   r   r–   Ú
ValueErrorrk   )r8   r:   r‹   rE   r‚   r—   r˜   r�   r�   Úupdated_guardrailrw   s              r9   Úupdate_guardrail_in_dbz(GuardrailRegistry.update_guardrail_in_dbJ  s  è ø€ ð	DØ$-§M¡MÐ2BÓ$CˆNà(1¯©Ð6FÈÓ(KÐÜÐ)¨<Ô8Ø&8×&CÑ&CÓ&EÑ#áBT¤dÐ+=Ô&>ÐZ\Ð#Ü)3Ð4GÓ)HˆNÜ)3°I·M±MÐBRÐTVÓ4WÓ)XˆNô HXÐXeÓGf×GmÑGmØ% |Ð4à&4Ø&4Ø&4Ü"*§,¡,¬x¯|©|Ó"<ñ	ð Hnó H÷ BÐð !Ð(Ü Ð#LÈ\ÈNÐ![Ó\Ð\ô Ð)Ó*Ð*ðBùô ò 	DÜÐ>¸q¸cÐBÓCÐCûð	Düs;   ‚D„B<C" Ã C ÃC" ÃDÃ C" Ã"	C>Ã+C9Ã9C>Ã>Dc           
   ƒ   óú   K  — 	 t        | «      j                  ddiddi¬«      ƒ d{  –—† }g }|D ]%  }|j                  t        di t	        |«      ¤Ž«       Œ' |S 7 Œ2# t
        $ r}t        d|› �«      ‚d}~ww xY w­w)	z¡
        Get all active guardrails from the database.
        Only rows with status == "active" are returned (pending_review and rejected are excluded).
        ÚstatusÚactiver�   Údesc)rž   ÚorderNz"Error getting guardrails from DB: r6   )rH   Ú	find_manyÚappendr$   re   rk   )rE   Úguardrails_from_dbÚ
guardrailsr‹   rw   s        r9   Úget_all_guardrails_from_dbz,GuardrailRegistry.get_all_guardrails_from_dbk  s¢   è ø€ ð	FÜ.>¸}Ó.M×.WÑ.WØ Ð*Ø# VÐ,ð /Xó /÷ )Ðð
 24ˆJØ/ò B�	Ø×!Ñ!¤)Ñ"@¬t°I«Ñ"@ÕAðBð Ðð)ùô ò 	FÜÐ@ÀÀÐDÓEÐEûð	Füs8   ‚A;„#A §A¨1A ÁA;ÁA Á	A8Á%A3Á3A8Á8A;c              ƒ   óÆ   K  — 	 t        |«      j                  d|i¬«      ƒ d{  –—† }|syt        di t        |«      ¤ŽS 7 Œ# t        $ r}t	        d|› �«      ‚d}~ww xY w­w)z=
        Get a guardrail by its ID from the database
        r:   r�   Nú!Error getting guardrail from DB: r6   ©rH   Úfind_uniquer$   re   rk   )r8   r:   rE   r‹   rw   s        r9   Úget_guardrail_by_id_from_dbz-GuardrailRegistry.get_guardrail_by_id_from_db�  sr   è ø€ ð	EÜ%5°mÓ%D×%PÑ%PÐXfÐhtÐWuÐ%PÓ%v×vˆIáØäÑ1¤ Y£Ñ1Ð1ð  wùô ò 	EÜÐ?À¸sÐCÓDÐDûð	EüóA   ‚A!„ A ¤A ¥A «A!¬A ¿A!Á A Á	AÁAÁAÁA!c              ƒ   óÆ   K  — 	 t        |«      j                  d|i¬«      ƒ d{  –—† }|syt        di t        |«      ¤ŽS 7 Œ# t        $ r}t	        d|› �«      ‚d}~ww xY w­w)z?
        Get a guardrail by its name from the database
        r‚   r�   Nr²   r6   r³   )r8   r‚   rE   r‹   rw   s        r9   Úget_guardrail_by_name_from_dbz/GuardrailRegistry.get_guardrail_by_name_from_db�  sy   è ø€ ð
	EÜ%5°mÓ%D×%PÑ%PØ'¨Ð8ð &Qó &÷  ˆIñ ØäÑ1¤ Y£Ñ1Ð1ð ùô ò 	EÜÐ?À¸sÐCÓDÐDûð	Eür¶   )r>   r?   r@   Ú__doc__r�   rB   r   rŠ   r$   r    r›   r¢   r¦   Ústaticmethodrg   r°   rµ   r¸   r6   r;   r9   r   r     sê   „ ñòð	Àð 	ÈÐ[_ÑI_ó 	ð B°9ð  BÈ\ó  BðD
F¸3ð 
FÈ|ó 
FðD¸ð DÈð DÐcoó DðB ðFØ#ðFà	ˆi‰òFó ðFð*E¸cð EÐR^ð EÐclÐosÑcsó EðEÀ#ð EÐVbð EÐgpÐswÑgwô Er;   r   Úinstancer�   c                 óž   — |j                   �t        |j                   «      | _         |j                  �t        |j                  «      | _        yy)a.  Override the parallel/raw-scan flags only when ``litellm_params`` explicitly
    sets them, preserving whatever default the guardrail's own constructor chose
    otherwise (its constructor default may be True, so blindly copying an
    absent/None config value would silently clobber it back to False).N)Úrun_in_parallelÚboolÚscan_raw_request)r»   r�   s     r9   Ú _apply_configured_bool_overridesrÀ      sG   € ð
 ×%Ñ%Ð1Ü#'¨×(FÑ(FÓ#GˆÔ Ø×&Ñ&Ð2Ü$(¨×)HÑ)HÓ$IˆÕ!ð 3r;   c                   óî  — e Zd ZdZd„ Zdedefd„Z	 	 	 d!dededz  d	ed
   de	d   dedz  f
d„Z
	 d"dededededz  dedz  f
d„Z	 d#dedede	d   ddfd„Zdeddfd„Zdee   fd„Zdededz  fd„Zdede	d   dz  fd„Zdee   fd„Zdededz  fd„Zdee   dee   fd„Zedeeeef   z  dz  deeef   dz  fd„«       Zdededefd„Z	 	 d$dededz  de	d   dedz  fd„Zd"dededz  dedz  fd „Zy)%ÚInMemoryGuardrailHandlerz[
    Class that handles initializing guardrails and adding them to the CallbackManager
    c                 ó2   — i | _         	 i | _        	 i | _        y r5   )ÚIN_MEMORY_GUARDRAILSÚ guardrail_id_to_custom_guardrailÚ_sourcesr7   s    r9   r�   z!InMemoryGuardrailHandler.__init__°  s.   € Ø:<ˆÔ!ð	ð TVˆÔ-ð	ð =?ˆŒð	r;   r‚   r3   c                 ó~   ‡ ‡— t        ‰fˆfd„t        d«      D «       «      }d„ |D «       }t        ˆ fd„|D «       «      S )Nc              3   ó,   •K  — | ]  }‰› d |› �–— Œ y­w)ú:Nr6   )Ú.0Ú
occurrencer‚   s     €r9   ú	<genexpr>z@InMemoryGuardrailHandler._stable_guardrail_id.<locals>.<genexpr>Ã  s   øè ø€ Ò0mÐV`°NÐ3CÀ1ÀZÀLÔ1QÑ0mùs   ƒr)   c           	   3   ó€   K  — | ]6  }t        t        j                  t        |j	                  d «      «      «      –— Œ8 y­w)zutf-8N)rB   r   Úuuid5rJ   Úencode)rÊ   Úseeds     r9   rÌ   z@InMemoryGuardrailHandler._stable_guardrail_id.<locals>.<genexpr>Ä  s,   è ø€ ÒwÐim¤¤D§J¡JÔ/LÈdÏkÉkÐZaÓNbÓ$c× dÑwùs   ‚<>c              3   ó@   •K  — | ]  }|‰j                   vsŒ|–— Œ y ­wr5   )rÄ   )rÊ   Úcandidate_idr8   s     €r9   rÌ   z@InMemoryGuardrailHandler._stable_guardrail_id.<locals>.<genexpr>Å  s   øè ø€ Òt \ÀlÐZ^×ZsÑZsÒFs”LÑtùs   ƒ—)r   r	   Únext)r8   r‚   ÚseedsÚcandidate_idss   ``  r9   Ú_stable_guardrail_idz-InMemoryGuardrailHandler._stable_guardrail_idÂ  s;   ù€ Ü˜nÐ.Ó0mÔdiÐjkÓdlÔ0mÓnˆÙwÐqvÔwˆÜÓt°]ÔtÓtÐtr;   Nr‹   Úconfig_file_pathÚ
llm_routerr   Úsource)ÚdbÚconfigc           
      óª  — |j                  d«      xs | j                  |d   «      }||d<   || j                  v r3t        j                  d«       || j
                  |<   | j                  |   S d}|d   }t        j                  d|«       t        |t        «      rt        di |¤Ž}n|}d|v r|d   rt        di |d   ¤Ž}	|	|_
        |j                  r>|j                  j                  d«      r#t        t        |j                  «      «      |_        |j                  r>|j                  j                  d«      r#t        t        |j                  «      «      |_        |j                   }
|
€t#        d	«      ‚t$        j                  |
«      }|r8d
dl}|j)                  |«      }d|j*                  v r ||||«      }nB |||«      }n8t        |
t        «      rd|
v r| j-                  ||
||¬«      }nt#        d|
› �«      ‚|�ydD ]  }t/        ||t1        ||d«      «       Œ t3        |«      }|r"|j5                  «       st#        d|d   › d�«      ‚|rt7        |«      rt#        d|d   › d�«      ‚t9        ||«       t;        |j                  d«      |d   ||j                  d«      ¬«      }|| j                  |<   || j<                  |<   || j
                  |<   |S )z¶
        Initialize a guardrail from a dictionary and add it to the litellm callback manager

        Returns a Guardrail object if the guardrail is initialized successfully
        r:   r‚   z3guardrail_id already exists in IN_MEMORY_GUARDRAILSNr�   zlitellm_params= %sÚcategory_thresholdszos.environ/zguardrail_type is requiredr   rØ   ú.)r‹   Úguardrail_typer�   r×   zUnsupported guardrail: )Ú skip_system_message_in_guardrailÚskip_tool_message_in_guardrailÚscan_only_tool_resultsr    z×: scan_only_tool_results is enabled, but this guardrail's role filtering never scans tool results, so no request content would ever be scanned. Remove scan_only_tool_results or the guardrail's role-filtering option.zÁ: scan_only_tool_results and skip_tool_message_in_guardrail are enabled together, which excludes every message from scanning, so no request content would ever be scanned. Remove one of the two.r�   )r:   r‚   r�   r�   r6   )r“   rÖ   rÄ   r   r\   rÆ   rd   re   r'   r&   rÝ   Úapi_keyr_   rB   r#   Úapi_baser‹   r¤   rI   ÚinspectÚ	signatureÚ
parametersÚinitialize_custom_guardrailÚsetattrrc   r   Úsupports_scan_only_tool_resultsr   rÀ   r$   rÅ   )r8   r‹   r×   rØ   rÙ   r:   Úcustom_guardrail_callbackÚlitellm_params_datar�   Úlakera_category_thresholdsrß   Úinitializerrå   ÚsigÚscoping_paramÚscan_only_tool_results_enabledÚparsed_guardrails                    r9   r   z-InMemoryGuardrailHandler.initialize_guardrailÇ  s+  € ð (Ÿm™m¨NÓ;Òu¸t×?XÑ?XÐYbÐcsÑYtÓ?uˆØ$0ˆ	�.Ñ!Ø˜4×4Ñ4Ñ4Ü ×&Ñ&Ð'\Ô]ð +1ˆD�M‰M˜,Ñ'Ø×,Ñ,¨\Ñ:Ð:à<@Ð!Ø%.Ð/?Ñ%@ÐÜ×"Ñ"Ð#7Ð9LÔMäÐ)¬4Ô0Ü*ÑAÐ-@ÑA‰Nà0ˆNà Ð$7Ñ7Ð<OÐPeÒ<fÜ0HÑ0vÐK^Ð_tÑKuÑ0vÐ&Ø1KˆNÔ.à×!Ò! n×&<Ñ&<×&GÑ&GÈÔ&VÜ%(¬°N×4JÑ4JÓ)KÓ%LˆNÔ"à×"Ò" ~×'>Ñ'>×'IÑ'IÈ-Ô'XÜ&)¬*°^×5LÑ5LÓ*MÓ&NˆNÔ#à .× 8Ñ 8ˆàÐ!ÜÐ9Ó:Ð:ä;×?Ñ?ÀÓOˆáãà ×*Ñ*¨;Ó7ˆCØ˜sŸ~™~Ñ-Ù,7Ø"ØØó-Ñ)ñ -8¸È	Ó,RÑ)Ü˜¬Ô,°¸Ñ1FØ(,×(HÑ(HØ#Ø-Ø-Ø!1ð	 )Ió )Ñ%ô Ð6°~Ð6FÐGÓHÐHà$Ð0ð"ò p�ô
 Ð1°=Ä'È.ÐZgÐimÓBnÕoðpô 5cØ)ó5Ð*ñ .Ð6O×6oÑ6oÔ6qÜ Ø  Ð+;Ñ!<Ð =ð >jð jóð ñ
 .Ô2[Ð\uÔ2vÜ Ø  Ð+;Ñ!<Ð =ð >dð dóð ô
 -Ð-FÈÔWä"+Ø"Ÿ™ ~Ó6Ø$Ð%5Ñ6Ø)Ø$Ÿ=™=Ð)9Ó:ô	#
Ðð 3Cˆ×!Ñ! ,Ñ/Ø>Wˆ×-Ñ-¨lÑ;Ø&,ˆ�‰�lÑ#àÐr;   rß   r�   c                 óÌ  — |st        d«      ‚t        j                  d|«       t        ||¬«      }|j                  }|€$t        d|› ddj                  t        «      › �«      ‚|j                  }t        |d«      r|j                  d	¬
«      }n|rt        |«      ni }dD ]  }	|j                  |	d«       Œ  |d|d   ||dœ|¤Ž}
t        j                  j                  |
«       |
S )z›
        Initialize a Custom Guardrail from a python file or module path

        This initializes it by adding it to the litellm callback manager
        zTGuardrailsAIException - Please pass the config_file_path to initialize_guardrails_v2z!Initializing custom guardrail: %s)r×   Nzmode is required for guardrail z* please set mode to one of the following: z, rŽ   T)Úexclude_none)r‹   ÚmodeÚ
default_onr‚   )r‚   Ú
event_hookrö   r6   )rk   r   r\   r   rõ   r¤   rZ   r%   rö   rb   rŽ   re   Úpopr…   r†   Úadd_litellm_callback)r8   r‹   rß   r�   r×   Ú_guardrail_classrõ   rö   Úextra_paramsÚkeyÚ_guardrail_callbacks              r9   rè   z4InMemoryGuardrailHandler.initialize_custom_guardrail4  s.  € ñ  ÜÐrÓsÐsä×"Ñ"Ø/Øô	
ô
 CRØÐ-=ôC
Ðð %×)Ñ)ˆØˆ<ÜØ1°.Ð1AÐAkÐlp×luÑluô  wJó  mKð  lLð  Móð ð +×5Ñ5ˆ
ô
 �> <Ô0Ø)×4Ñ4À$Ð4ÓG‰Lá3Aœ4 Ô/ÀrˆLð 7ò 	(ˆCØ×Ñ˜S $Õ'ð	(ñ &6ð &
Ø$Ð%5Ñ6ØØ!ñ&
ð ñ	&
Ðô 	×(Ñ(×=Ñ=Ð>QÔRà"Ð"r;   r:   c                 óà   — || j                   |<   || j                  |<   | j                  j                  |«      }|r3t	        t
        |j                  di «      «      }|j                  |¬«       yy)zœ
        Update a guardrail in memory

        - updates the guardrail in memory
        - updates the guardrail params in litellm.callback_manager
        r�   )r�   N)rÄ   rÆ   rÅ   r“   r   r'   Úupdate_in_memory_litellm_params)r8   r:   r‹   rÙ   rë   Úupdated_litellm_paramss         r9   Úupdate_in_memory_guardrailz3InMemoryGuardrailHandler.update_in_memory_guardrailj  sn   € ð 3<ˆ×!Ñ! ,Ñ/Ø&,ˆ�‰�lÑ#à+/×+PÑ+P×+TÑ+TÐUaÓ+bÐ!Ù$Ü,0´À	ÇÁÐN^Ð`bÓ@cÓ,dÐ"Ø%×EÑEÐUkÐEÕlð %r;   c                 óð   — | j                   j                  |d«       | j                  j                  |d«       | j                  j                  |d«      }|€yt        j
                  j                  |«       y)aŠ  
        Delete a guardrail in memory and remove from litellm callbacks.

        The callback is purged from every callback list, not just
        litellm.callbacks: request handling promotes guardrail callbacks into the
        success/failure/async lists, so removing it from only litellm.callbacks
        leaves the old instance stranded in those lists on every re-initialization.
        N)rÄ   rø   rÆ   rÅ   r…   r†   Úremove_callback_from_all_lists)r8   r:   rë   s      r9   Údelete_in_memory_guardrailz3InMemoryGuardrailHandler.delete_in_memory_guardrail~  sg   € ð 	×!Ñ!×%Ñ% l°DÔ9Ø�‰×Ñ˜,¨Ô-à+/×+PÑ+P×+TÑ+TÐUaÐcgÓ+hÐ!Ø$Ð,Øä×(Ñ(×GÑGÐHaÕbr;   c                 óH   — t        | j                  j                  «       «      S )z/
        List all guardrails in memory
        )rg   rÄ   Úvaluesr7   s    r9   Úlist_in_memory_guardrailsz2InMemoryGuardrailHandler.list_in_memory_guardrails‘  s   € ô �D×-Ñ-×4Ñ4Ó6Ó7Ð7r;   c                 ó8   — | j                   j                  |«      S )z7
        Get a guardrail by its ID from memory
        )rÄ   r“   ©r8   r:   s     r9   Úget_guardrail_by_idz,InMemoryGuardrailHandler.get_guardrail_by_id—  s   € ð ×(Ñ(×,Ñ,¨\Ó:Ð:r;   c                 ó8   — | j                   j                  |«      S )zB
        Return the provenance of an in-memory guardrail.
        )rÆ   r“   r	  s     r9   Ú
get_sourcez#InMemoryGuardrailHandler.get_source�  s   € ð �}‰}× Ñ  Ó.Ð.r;   c                 ó¦   — | j                   j                  «       D ��cg c]&  \  }}| j                  j                  |«      dk(  sŒ%|‘Œ( c}}S c c}}w )a4  
        List in-memory guardrails owned by config.yaml.

        DB-sourced entries are excluded: a read surface that also queries the DB
        would double-count live ones, and a DB-sourced entry that's missing from
        the DB is stale (deleted on another pod, awaiting reconciliation here).
        rÛ   )rÄ   ÚitemsrÆ   r“   )r8   ÚgidÚgs      r9   Úlist_config_guardrailsz/InMemoryGuardrailHandler.list_config_guardrails£  sC   € ð !%× 9Ñ 9× ?Ñ ?Ó A×h‘f�c˜1ÀTÇ]Á]×EVÑEVÐWZÓE[Ð_gÓEg’ÓhÐhùÓhs   ž&AÁAc                 óv   — | j                   j                  |«      dk7  ry| j                  j                  |«      S )zã
        Get a config-owned in-memory guardrail by its ID, or None.

        Mirrors the fallback in get_guardrail_info: a DB-sourced in-memory entry
        that missed the DB lookup is stale and must not be surfaced.
        rÛ   N)rÆ   r“   rÄ   r	  s     r9   Úget_config_guardrail_by_idz3InMemoryGuardrailHandler.get_config_guardrail_by_id­  s5   € ð �=‰=×Ñ˜\Ó*¨hÒ6ØØ×(Ñ(×,Ñ,¨\Ó:Ð:r;   Údb_guardrail_idsc                 óØ   — | j                   j                  «       D ��cg c]  \  }}|dk(  r||vr|‘Œ }}}|D ])  }t        j                  d|«       | j	                  |«       Œ+ |S c c}}w )aC  
        Drop in-memory entries that originated from the DB but are no longer
        present in db_guardrail_ids. Config-loaded guardrails are never touched.

        Called by the periodic DB polling tick so that a guardrail deleted
        on another pod is eventually purged from this pod's memory + callbacks.
        rÚ   z]Reconcile: removing stale DB-backed guardrail '%s' from memory (deleted in DB by another pod))rÆ   r  r   Úinfor  )r8   r  r:   rÙ   Ú	stale_idss        r9   Úreconcile_db_guardrailsz0InMemoryGuardrailHandler.reconcile_db_guardrails¸  sƒ   € ð )-¯©×(;Ñ(;Ó(=÷
á$�˜fØ˜Š~ ,Ð6FÑ"Fò ð
ˆ	ñ 
ð
 &ò 	:ˆLÜ ×%Ñ%ØoØôð ×+Ñ+¨LÕ9ð	:ð Ðùó
s   žA&Úparamsc                 óü   — | €yt        | t        «      r| j                  «       S t        | t        «      r	 t        di | ¤Žj                  «       S | S # t        $ r"}t        j                  d|«       | cY d}~S d}~ww xY w)aK  
        Render litellm_params to a canonical dict so an in-memory LitellmParams and
        the raw dict loaded from the DB compare equal when they describe the same
        config. The in-memory side is a LitellmParams whose model_dump() carries
        every field default and coerces enums, while the DB side is the raw stored
        dict holding only the keys originally provided. Comparing those two shapes
        directly never matches, so each DB poll would re-initialize the guardrail
        forever; normalizing both through LitellmParams keeps the diff meaningful.
        NziCould not normalize guardrail litellm_params for comparison; treating the guardrail as changed. Error: %sr6   )rd   r'   rŽ   re   r   r   Úwarning)r  rw   s     r9   Ú(_normalize_litellm_params_for_comparisonzAInMemoryGuardrailHandler._normalize_litellm_params_for_comparisonÍ  s   € ð ˆ>ØÜ�fœmÔ,Ø×$Ñ$Ó&Ð&Ü�fœdÔ#ðÜ$Ñ. vÑ.×9Ñ9Ó;Ð;ð ˆøô #ò Ü$×,Ñ,ØØôð •ûðús   µA Á	A;ÁA6Á0A;Á6A;Únew_guardrailc                 óL  — | j                   j                  |«      }|€y|j                  d«      |j                  d«      k7  ry| j                  |j                  d«      «      }| j                  |j                  d«      «      }i }|�m|�kt        |j	                  «       «      t        |j	                  «       «      z  }|D ]2  }|j                  |«      }	|j                  |«      }
|	|
k7  sŒ+|	|
dœ||<   Œ4 n||k7  rd||dœi}|rt        j                  d|«       t        |«      dkD  S )z±
        Check if guardrail params or name have changed compared to in-memory version.
        Returns True if params/name changed or guardrail doesn't exist in memory.
        Tr‚   r�   )ÚoldÚnewz)Guardrail params changed. Differences: %sr   )rÄ   r“   r  Úsetrh   r   r\   rl   )r8   r:   r  ÚexistingÚexisting_dictÚnew_dictÚchanged_fieldsÚall_keysrü   Úold_valÚnew_vals              r9   Ú_has_guardrail_params_changedz6InMemoryGuardrailHandler._has_guardrail_params_changedé  s;  € ð
 ×3Ñ3×7Ñ7¸ÓEˆØÐØð �<‰<Ð(Ó)¨]×->Ñ->Ð?OÓ-PÒPØð  $×LÑLÈXÏ\É\ÐZjÓMkÓlˆØ×GÑGÈ×HYÑHYÐZjÓHkÓlˆð ˆØÐ$¨Ð)=Ü! -×"4Ñ"4Ó"6Ó7¼#¸h¿m¹m»oÓ:NÑNˆHØò K�Ø'×+Ñ+¨CÓ0�Ø"Ÿ,™, sÓ+�Ø˜gÓ%Ø29À'Ñ*J�N 3Ò'ñ	Kð
 ˜hÒ&Ø.¸ÈhÑ0WÐXˆNñ Ü ×&Ñ&Ð'RÐTbÔcô �>Ó" QÑ&Ð&r;   c                 óì  — |j                  d«      }|st        j                  d«       y| j                  j                  |«      }| j                  j                  ||«      }|| j                  v r| j                  |«       	 | j                  |||¬«      S # t        $ rU |�Qt        j                  d|«       	 | j                  |||¬«       ‚ # t        $ r t        j                  d|«       Y ‚ w xY w‚ w xY w)a_  
        Force re-initialization of a guardrail even if it exists in memory.
        Removes old callback from litellm.callbacks and creates fresh instance.

        If the new config fails to initialize (e.g. an invalid on_flagged
        combination), the previous instance is restored rather than left
        deleted: initialize_guardrail's own ValueError/TypeError propagate
        uncaught, so a caller reaching this point after already deleting the
        old instance would otherwise leave the guardrail providing no
        protection at all, not merely "still enforcing the old config."
        r:   z2Cannot reinitialize guardrail without guardrail_idN©r‹   r×   rÙ   z\Reinitializing guardrail %s with updated params failed; restoring the previous configurationz+Restoring previous guardrail %s also failed)	r“   r   rj   rÄ   rÆ   r  r   rk   rz   )r8   r‹   r×   rÙ   r:   Úprevious_guardrailÚprevious_sources          r9   Úreinitialize_guardrailz/InMemoryGuardrailHandler.reinitialize_guardrail  s
  € ð" (Ÿm™m¨NÓ;ˆÙÜ ×&Ñ&Ð'[Ô\Øà$(×$=Ñ$=×$AÑ$AÀ,Ó$OÐØ!%§¡×!2Ñ!2°<ÀÓ!Hˆà˜4×4Ñ4Ñ4Ø×+Ñ+¨LÔ9ð	Ø×,Ñ,°yÐScÐlrÐ,ÓsÐsøÜò 	Ø!Ð-Ü$×.Ñ.ØrØ ôðpØ×-Ñ-Ø"4ÐGWÐ`oð .ô ð
 øô !ò pÜ(×2Ñ2Ð3`ÐbnÕoØðpúàð	ús0   ÂB Â!C3Â7CÃC3ÃC.Ã+C3Ã-C.Ã.C3c                 óF  — |j                  d«      }|st        j                  d«       y| j                  ||«      r=|j                  dd«      }t        j                  d||«       | j                  ||d¬«      S d| j                  |<   | j                  j                  |«      S )	z‘
        Sync a guardrail from DB - initializes if new, re-initializes if changed.
        This is the method to call during DB polling.
        r:   z*Cannot sync guardrail without guardrail_idNr‚   ÚUnknownz:Guardrail '%s' (ID: %s) params changed, re-initializing...rÚ   r+  )r“   r   rj   r)  r  r.  rÆ   rÄ   )r8   r‹   r×   r:   r‚   s        r9   Úsync_guardrail_from_dbz/InMemoryGuardrailHandler.sync_guardrail_from_db=  s¨   € ð
 (Ÿm™m¨NÓ;ˆÙÜ ×&Ñ&Ð'SÔTØà×-Ñ-¨l¸IÔFØ$-§M¡MÐ2BÀIÓ$NˆNÜ ×%Ñ%ØLÈnÐ^jôð ×.Ñ.Ø#Ø!1Øð /ó ð ð '+ˆ�‰�lÑ#Ø×(Ñ(×,Ñ,¨\Ó:Ð:r;   )NNrÛ   r5   )rÚ   )NrÛ   ) r>   r?   r@   r¹   r�   rB   rÖ   r$   r   r   r   r'   r   rè   r  r  rg   r  r
  r  r  r  r!  r  rº   r   rD   r  r¾   r)  r.  r1  r6   r;   r9   rÂ   rÂ   «  sc  „ ñòð$u°3ð u¸3ó uð (,Ø)-Ø*2ñk àðk ð  ™*ðk ð ˜XÑ&ð	k ð
 ˜Ñ'ðk ð 
�TÑ	ók ðd (,ñ4#àð4#ð ð4#ð &ð	4#ð
  ™*ð4#ð 
˜4Ñ	ó4#ðt +/ñ	màðmð ðmð ˜Ñ'ð	mð
 
ómð(c°sð c¸tó cð&8¨4°	©?ó 8ð;°ð ;¸	ÀDÑ8Hó ;ð/ sð /¨w°~Ñ/FÈÑ/Mó /ði¨¨Y©ó ið	;°sð 	;¸yÈ4Ñ?Oó 	;ð¸¸C¹ð ÀTÈ#ÁYó ð* ðØ ¨¨V¨Ñ 4Ñ4°tÑ;ðà	��f�Ñ	 Ñ	$òó ðð6"'¸#ð "'Èið "'Ð\`ó "'ðN (,Ø*2ñ	.àð.ð  ™*ð.ð ˜Ñ'ð	.ð
 
�TÑ	ó.ñ`;°	ð ;ÈSÐSWÉZð ;ÐclÐosÑcsô ;r;   rÂ   ÚIN_MEMORY_GUARDRAIL_HANDLERÚGUARDRAIL_RECONCILE_LOCK)iÚasyncior`   rV   Úcollections.abcr   r   r   r   r   Ú	itertoolsr   r	   Útypingr
   r   r   r   r   r   Úpydanticr   r…   r   Úlitellm._loggingr   Úlitellm._uuidr   Ú%litellm.integrations.custom_guardrailr   Ú*litellm.litellm_core_utils.safe_json_dumpsr   Ú1litellm.llms.base_llm.guardrail_translation.utilsr   r   Ú;litellm.proxy.guardrails.guardrail_hooks.bedrock_guardrailsr   Ú1litellm.proxy.guardrails.guardrail_hooks.grayswanr   r   Úinitialize_grayswanÚ2litellm.proxy.guardrails.guardrail_hooks.lakera_air   Ú5litellm.proxy.guardrails.guardrail_hooks.lakera_ai_v2r   Ú1litellm.proxy.guardrails.guardrail_hooks.presidior   Ú8litellm.proxy.guardrails.guardrail_hooks.tool_permissionr   Úlitellm.proxy.types_utils.utilsr   Úlitellm.proxy.utilsr    Ú%litellm.repositories.prisma_protocolsr!   Ú'litellm.repositories.table_repositoriesr"   Úlitellm.secret_managers.mainr#   Úlitellm.types.guardrailsr$   r%   r&   r'   r(   Úguardrail_hooks.llm_as_a_judgeÚinitialize_llm_as_a_judgeÚguardrail_initializersr*   r+   r,   r-   r.   r/   Úprismar0   Úprisma_modelsr2   rH   ÚBEDROCKÚvalueÚLAKERAÚ	LAKERA_V2ÚPRESIDIOÚHIDE_SECRETSÚTOOL_PERMISSIONÚGRAYSWANÚLLM_AS_A_JUDGErI   Ú__annotations__ÚUUIDrJ   rK   re   rB   Útyperx   r|   rf   r}   r   rÀ   rÂ   r2  ÚLockr3  r6   r;   r9   ú<module>r]     s�  ðô Û Û 	ß 7Ñ 7ß 'ß "ß J× Jå $ã Ý Ý 1Ý Ý AÝ A÷õõõõ SÝ Sõõõ <Ý ,Ý >Ý HÝ 3÷õ õ÷÷ ñ Ý.ô<˜ô <ð5 Lð 5Ð5jó 5ð #×*Ñ*×0Ñ0Ð2DØ"×)Ñ)×/Ñ/Ð1BØ"×,Ñ,×2Ñ2Ð4HØ"×+Ñ+×1Ñ1Ð3FØ"×/Ñ/×5Ñ5Ð7NØ"×2Ñ2×8Ñ8Ð:TØ"×+Ñ+×1Ñ1Ð3FØ"×1Ñ1×7Ñ7Ð9Rð	)Ð  ó 	ð (1 t§y¡yÐ1WÓ'XÐ ˜uÓ Xð #×*Ñ*×0Ñ0Ð2BØ"×+Ñ+×1Ñ1Ð3DØ"×)Ñ)×/Ñ/Ð1DØ"×,Ñ,×2Ñ2Ð4EØ"×+Ñ+×1Ñ1Ð3OØ"×2Ñ2×8Ñ8Ð:QðEÐ ˜%  S¨$¨Ñ*?Ð%?Ñ @ÑAó òK#ò\@ðF × Ñ Ñ >Ó @Ô Añ #GÓ"HÐ ˜%Ó Hà × %Ñ %Ð&>Ô ?÷\Eñ \Eð~J¨ð JÐP]ð JÐbfó J÷k;ñ k;ñb &>Ó%?Ð ˜UÓ ?à". '§,¡,£.Ð ˜%Ô 0r;   