§
    Ròmjˆ8  ã                   ór  — d Z ddlmZmZ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 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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e-de.deddfd„Z/de-de0fd„Z1de-de.deddfd„Z2de-de0fd„Z3de-de.deddfd„Z4de-de0fd„Z5de-de.deddfd „Z6de-de0fd!„Z7de-de.deddfd"„Z8de-de0fd#„Z9de-de.deddfd$„Z:de-de0fd%„Z;de-de.deddfd&„Z<de-de0fd'„Z=de-de.deddfd(„Z>de-de0fd)„Z?de-de.deddfd*„Z@de-de0fd+„ZAg d,¢ZBdS )-a4  Global guardrail registration for tools and LLMs.

Guardrails can either sanitize data recorded on lifecycle events or reject a
call before it runs.

In managed ``tools.execute()`` and ``llm.execute()`` flows, sanitize
guardrails are observability-only: they change the payload written to emitted
events, not the value passed to the user callback or returned to the caller.

Example::

    import nemo_relay

    def redact(tool_name, args):
        return {**args, "api_key": "***"}

    nemo_relay.guardrails.register_tool_sanitize_request("redact", 10, redact)
é    )ÚEventSanitizeGuardrailÚ LlmConditionalExecutionGuardrailÚLlmSanitizeRequestGuardrailÚLlmSanitizeResponseGuardrailÚ!ToolConditionalExecutionGuardrailÚToolSanitizeGuardrail)Ú.deregister_llm_conditional_execution_guardrail)Ú)deregister_llm_sanitize_request_guardrail)Ú*deregister_llm_sanitize_response_guardrail)Ú"deregister_mark_sanitize_guardrail)Ú'deregister_scope_sanitize_end_guardrail)Ú)deregister_scope_sanitize_start_guardrail)Ú/deregister_tool_conditional_execution_guardrail)Ú*deregister_tool_sanitize_request_guardrail)Ú+deregister_tool_sanitize_response_guardrail)Ú,register_llm_conditional_execution_guardrail)Ú'register_llm_sanitize_request_guardrail)Ú(register_llm_sanitize_response_guardrail)Ú register_mark_sanitize_guardrail)Ú%register_scope_sanitize_end_guardrail)Ú'register_scope_sanitize_start_guardrail)Ú-register_tool_conditional_execution_guardrail)Ú(register_tool_sanitize_request_guardrail)Ú)register_tool_sanitize_response_guardrailÚnameÚpriorityÚ	guardrailÚreturnNc                 ó$   — t          | ||¦  «        S )z9Register a sanitizer for mark event observability fields.)Ú_native_register_mark_sanitize©r   r   r   s      ú]/home/thesage/.hermes/hermes-agent/venv/lib/python3.11/site-packages/nemo_relay/guardrails.pyÚregister_mark_sanitizer#   W   s   € å)¨$°¸)ÑDÔDÐDó    c                 ó    — t          | ¦  «        S )z-Remove a global mark event sanitizer by name.)Ú _native_deregister_mark_sanitize©r   s    r"   Úderegister_mark_sanitizer(   \   s   € å+¨DÑ1Ô1Ð1r$   c                 ó$   — t          | ||¦  «        S )z:Register a sanitizer for every scope start event category.)Ú%_native_register_scope_sanitize_startr!   s      r"   Úregister_scope_sanitize_startr+   a   s   € å0°°xÀÑKÔKÐKr$   c                 ó    — t          | ¦  «        S )z4Remove a global scope-start event sanitizer by name.)Ú'_native_deregister_scope_sanitize_startr'   s    r"   Úderegister_scope_sanitize_startr.   f   s   € å2°4Ñ8Ô8Ð8r$   c                 ó$   — t          | ||¦  «        S )z8Register a sanitizer for every scope end event category.)Ú#_native_register_scope_sanitize_endr!   s      r"   Úregister_scope_sanitize_endr1   k   s   € å.¨t°X¸yÑIÔIÐIr$   c                 ó    — t          | ¦  «        S )z2Remove a global scope-end event sanitizer by name.)Ú%_native_deregister_scope_sanitize_endr'   s    r"   Úderegister_scope_sanitize_endr4   p   s   € å0°Ñ6Ô6Ð6r$   c                 ó$   — t          | ||¦  «        S )a’  Register a guardrail that sanitizes tool inputs for emitted start events.

    Args:
        name: Unique guardrail name used for later replacement or removal.
        priority: Execution order for the guardrail. Lower values run first.
        guardrail: Callable invoked as ``guardrail(tool_name, args)`` that must
            return the sanitized payload to record on the emitted start event.

    Returns:
        None: This function returns after the guardrail is registered.

    Notes:
        In managed ``nemo_relay.tools.execute()`` flows, sanitize guardrails are
        observability-only. They change the payload written to events, not the
        arguments passed to the tool callback.

    Example::

        import nemo_relay

        def redact(tool_name, args):
            return {**args, "api_key": "***"}

        nemo_relay.guardrails.register_tool_sanitize_request("redact", 10, redact)
    )Ú&_native_register_tool_sanitize_requestr!   s      r"   Úregister_tool_sanitize_requestr7   z   s   € õ4 2°$¸À)ÑLÔLÐLr$   c                 ó    — t          | ¦  «        S )a—  Remove a previously registered tool sanitize-request guardrail.

    Args:
        name: Guardrail name previously passed to
            ``register_tool_sanitize_request()``.

    Returns:
        bool: ``True`` if a guardrail was removed, otherwise ``False``.

    Notes:
        Removal affects only future executions. In-flight calls continue using
        the guardrail chain they already resolved.
    )Ú(_native_deregister_tool_sanitize_requestr'   s    r"   Ú deregister_tool_sanitize_requestr:   —   ó   € õ 4°DÑ9Ô9Ð9r$   c                 ó$   — t          | ||¦  «        S )a]  Register a guardrail that sanitizes tool outputs for emitted end events.

    Args:
        name: Unique guardrail name used for later replacement or removal.
        priority: Execution order for the guardrail. Lower values run first.
        guardrail: Callable invoked as ``guardrail(tool_name, result)`` that
            must return the sanitized payload to record on emitted end events.

    Returns:
        None: This function returns after the guardrail is registered.

    Notes:
        This guardrail affects event payloads only. The caller still receives
        the original tool result.
    )Ú'_native_register_tool_sanitize_responser!   s      r"   Úregister_tool_sanitize_responser>   ¨   s   € õ  3°4¸À9ÑMÔMÐMr$   c                 ó    — t          | ¦  «        S )a™  Remove a previously registered tool sanitize-response guardrail.

    Args:
        name: Guardrail name previously passed to
            ``register_tool_sanitize_response()``.

    Returns:
        bool: ``True`` if a guardrail was removed, otherwise ``False``.

    Notes:
        Removal affects only future executions. In-flight calls continue using
        the guardrail chain they already resolved.
    )Ú)_native_deregister_tool_sanitize_responser'   s    r"   Ú!deregister_tool_sanitize_responserA   »   s   € õ 5°TÑ:Ô:Ð:r$   c                 ó$   — t          | ||¦  «        S )a]  Register a guardrail that can reject a tool call before execution.

    Args:
        name: Unique guardrail name used for later replacement or removal.
        priority: Execution order for the guardrail. Lower values run first.
        guardrail: Callable invoked as ``guardrail(tool_name, args)``. Return
            ``None`` to allow execution or a rejection message to block it.

    Returns:
        None: This function returns after the guardrail is registered.

    Notes:
        Conditional-execution guardrails run before request intercepts and
        before the tool callback is invoked.
    )Ú+_native_register_tool_conditional_executionr!   s      r"   Ú#register_tool_conditional_executionrD   Ì   s   € õ  7°t¸XÀyÑQÔQÐQr$   c                 ó    — t          | ¦  «        S )a¡  Remove a previously registered tool conditional-execution guardrail.

    Args:
        name: Guardrail name previously passed to
            ``register_tool_conditional_execution()``.

    Returns:
        bool: ``True`` if a guardrail was removed, otherwise ``False``.

    Notes:
        Removal affects only future executions. In-flight calls continue using
        the guardrail chain they already resolved.
    )Ú-_native_deregister_tool_conditional_executionr'   s    r"   Ú%deregister_tool_conditional_executionrG   ß   s   € õ 9¸Ñ>Ô>Ð>r$   c                 ó$   — t          | ||¦  «        S )a  Register a guardrail that sanitizes LLM requests for emitted start events.

    Args:
        name: Unique guardrail name used for later replacement or removal.
        priority: Execution order for the guardrail. Lower values run first.
        guardrail: Callable invoked as ``guardrail(request)`` that must return
            the sanitized request recorded on the emitted start event.

    Returns:
        None: This function returns after the guardrail is registered.

    Notes:
        In managed ``nemo_relay.llm.execute()`` and
        ``nemo_relay.llm.stream_execute()`` flows, this is observability-only
        and does not mutate the request forwarded to the provider callback.

    Example::

        import nemo_relay

        def strip_auth(request):
            headers = {k: v for k, v in request.headers.items() if k.lower() != "authorization"}
            return nemo_relay.LLMRequest(headers, request.content)

        nemo_relay.guardrails.register_llm_sanitize_request("strip-auth", 10, strip_auth)
    )Ú%_native_register_llm_sanitize_requestr!   s      r"   Úregister_llm_sanitize_requestrJ   õ   s   € õ6 1°°xÀÑKÔKÐKr$   c                 ó    — t          | ¦  «        S )a•  Remove a previously registered LLM sanitize-request guardrail.

    Args:
        name: Guardrail name previously passed to
            ``register_llm_sanitize_request()``.

    Returns:
        bool: ``True`` if a guardrail was removed, otherwise ``False``.

    Notes:
        Removal affects only future executions. In-flight calls continue using
        the guardrail chain they already resolved.
    )Ú'_native_deregister_llm_sanitize_requestr'   s    r"   Úderegister_llm_sanitize_requestrM     s   € õ 3°4Ñ8Ô8Ð8r$   c                 ó$   — t          | ||¦  «        S )ae  Register a guardrail that sanitizes LLM outputs for emitted end events.

    Args:
        name: Unique guardrail name used for later replacement or removal.
        priority: Execution order for the guardrail. Lower values run first.
        guardrail: Callable invoked as ``guardrail(response)`` that must return
            the sanitized payload recorded on the emitted end event.

    Returns:
        None: This function returns after the guardrail is registered.

    Notes:
        This guardrail changes only event payloads. The raw provider response
        returned to the caller is left unchanged.
    )Ú&_native_register_llm_sanitize_responser!   s      r"   Úregister_llm_sanitize_responserP   $  s   € õ  2°$¸À)ÑLÔLÐLr$   c                 ó    — t          | ¦  «        S )a—  Remove a previously registered LLM sanitize-response guardrail.

    Args:
        name: Guardrail name previously passed to
            ``register_llm_sanitize_response()``.

    Returns:
        bool: ``True`` if a guardrail was removed, otherwise ``False``.

    Notes:
        Removal affects only future executions. In-flight calls continue using
        the guardrail chain they already resolved.
    )Ú(_native_deregister_llm_sanitize_responser'   s    r"   Ú deregister_llm_sanitize_responserS   7  r;   r$   c                 ó$   — t          | ||¦  «        S )aS  Register a guardrail that can reject an LLM call before execution.

    Args:
        name: Unique guardrail name used for later replacement or removal.
        priority: Execution order for the guardrail. Lower values run first.
        guardrail: Callable invoked as ``guardrail(request)``. Return ``None``
            to allow execution or a rejection message to block the call.

    Returns:
        None: This function returns after the guardrail is registered.

    Notes:
        Conditional-execution guardrails run before request intercepts, codecs,
        and provider execution.
    )Ú*_native_register_llm_conditional_executionr!   s      r"   Ú"register_llm_conditional_executionrV   H  s   € õ  6°d¸HÀiÑPÔPÐPr$   c                 ó    — t          | ¦  «        S )aŸ  Remove a previously registered LLM conditional-execution guardrail.

    Args:
        name: Guardrail name previously passed to
            ``register_llm_conditional_execution()``.

    Returns:
        bool: ``True`` if a guardrail was removed, otherwise ``False``.

    Notes:
        Removal affects only future executions. In-flight calls continue using
        the guardrail chain they already resolved.
    )Ú,_native_deregister_llm_conditional_executionr'   s    r"   Ú$deregister_llm_conditional_executionrY   [  s   € õ 8¸Ñ=Ô=Ð=r$   )r#   r(   r+   r.   r1   r4   r7   r:   r>   rA   rD   rG   rJ   rM   rP   rS   rV   rY   )CÚ__doc__Ú
nemo_relayr   r   r   r   r   r   Únemo_relay._nativer	   rX   r
   rL   r   rR   r   r&   r   r3   r   r-   r   rF   r   r9   r   r@   r   rU   r   rI   r   rO   r   r    r   r0   r   r*   r   rC   r   r6   r   r=   ÚstrÚintr#   Úboolr(   r+   r.   r1   r4   r7   r:   r>   rA   rD   rG   rJ   rM   rP   rS   rV   rY   Ú__all__© r$   r"   ú<module>rb      sº  ððð ð&ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ðð ð ð ð ð ðð ð ð ð ð ðð ð ð ð ð ð fÐ eÐ eÐ eÐ eÐ eðð ð ð ð ð ðð ð ð ð ð ðð ð ð ð ð ðð ð ð ð ð ðð ð ð ð ð ðð ð ð ð ð ðð ð ð ð ð ðð ð ð ð ð ð bÐ aÐ aÐ aÐ aÐ aðð ð ð ð ð ðð ð ð ð ð ðð ð ð ð ð ðð ð ð ð ð ðð ð ð ð ð ðE ð E°ð EÐ@Vð EÐ[_ð Eð Eð Eð Eð
2 3ð 2¨4ð 2ð 2ð 2ð 2ð
L¨ð L°sð LÐG]ð LÐbfð Lð Lð Lð Lð
9¨#ð 9°$ð 9ð 9ð 9ð 9ð
J cð J°Sð JÐE[ð JÐ`dð Jð Jð Jð Jð
7¨ð 7°ð 7ð 7ð 7ð 7ðM¨ð M¸ð MÐH]ð MÐbfð Mð Mð Mð Mð::¨3ð :°4ð :ð :ð :ð :ð"N¨#ð N¸ð NÐI^ð NÐcgð Nð Nð Nð Nð&;¨Cð ;°Dð ;ð ;ð ;ð ;ð"R¨cð R¸Sð RÐMnð RÐswð Rð Rð Rð Rð&?°ð ?¸ð ?ð ?ð ?ð ?ð,L¨ð L°sð LÐGbð LÐgkð Lð Lð Lð Lð<9¨#ð 9°$ð 9ð 9ð 9ð 9ð"M¨ð M¸ð MÐHdð MÐimð Mð Mð Mð Mð&:¨3ð :°4ð :ð :ð :ð :ð"Q¨Sð Q¸Cð QÐLlð QÐquð Qð Qð Qð Qð&>¨sð >°tð >ð >ð >ð >ð"ð ð €€€r$   