
    Rmj_e                     .   d 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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m0Z1 ddlm2Z3 ddlm4Z5 ddlm6Z7 ddlm8Z9 ddlm:Z; ddlm<Z= d  Z>d! Z?d" Z@d# ZAd$ ZBd% ZCd& ZDd' ZEd( ZFd) ZGd* ZHd+ ZId, ZJd- ZKd. ZLd/ ZMd0 ZNd1 ZOd2 ZPd3 ZQd4 ZRd5 ZSd6 ZTd7 ZUd8 ZVd9 ZWd: ZXd; ZYd< ZZd= Z[g d>Z\d?S )@a:  Scope-local middleware and subscriber registration.

These helpers mirror the global ``guardrails``, ``intercepts``, and
``subscribers`` modules, but the registrations apply only while the owning
scope is active. When that scope is popped, the registrations are removed
automatically.

Example::

    import nemo_relay

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

    with nemo_relay.scope.scope("request", nemo_relay.ScopeType.Agent) as handle:
        nemo_relay.scope_local.register_tool_sanitize_request(handle, "redact", 10, redact)
    )4scope_deregister_llm_conditional_execution_guardrail)(scope_deregister_llm_execution_intercept)&scope_deregister_llm_request_intercept)/scope_deregister_llm_sanitize_request_guardrail)0scope_deregister_llm_sanitize_response_guardrail)/scope_deregister_llm_stream_execution_intercept)(scope_deregister_mark_sanitize_guardrail)-scope_deregister_scope_sanitize_end_guardrail)/scope_deregister_scope_sanitize_start_guardrail)scope_deregister_subscriber)5scope_deregister_tool_conditional_execution_guardrail))scope_deregister_tool_execution_intercept)'scope_deregister_tool_request_intercept)0scope_deregister_tool_sanitize_request_guardrail)1scope_deregister_tool_sanitize_response_guardrail)2scope_register_llm_conditional_execution_guardrail)&scope_register_llm_execution_intercept)$scope_register_llm_request_intercept)-scope_register_llm_sanitize_request_guardrail).scope_register_llm_sanitize_response_guardrail)-scope_register_llm_stream_execution_intercept)&scope_register_mark_sanitize_guardrail)+scope_register_scope_sanitize_end_guardrail)-scope_register_scope_sanitize_start_guardrail)scope_register_subscriber)3scope_register_tool_conditional_execution_guardrail)'scope_register_tool_execution_intercept)%scope_register_tool_request_intercept).scope_register_tool_sanitize_request_guardrail)/scope_register_tool_sanitize_response_guardrailc                 0    t          | j        |||          S )z,Register a scope-local mark event sanitizer.)_register_mark_sanitizeuuidscope_handlenamepriority	guardrails       ^/home/thesage/.hermes/hermes-agent/venv/lib/python3.11/site-packages/nemo_relay/scope_local.pyregister_mark_sanitizer*   v   s    "<#4dHiPPP    c                 ,    t          | j        |          S )z*Remove a scope-local mark event sanitizer.)_deregister_mark_sanitizer#   r%   r&   s     r)   deregister_mark_sanitizer/   {   s    $\%6===r+   c                 0    t          | j        |||          S )z8Register a scope-local sanitizer for scope start events.)_register_scope_sanitize_startr#   r$   s       r)   register_scope_sanitize_startr2      s    ),*;T8YWWWr+   c                 ,    t          | j        |          S )z1Remove a scope-local scope-start event sanitizer.) _deregister_scope_sanitize_startr#   r.   s     r)   deregister_scope_sanitize_startr5      s    +L,=tDDDr+   c                 0    t          | j        |||          S )z6Register a scope-local sanitizer for scope end events.)_register_scope_sanitize_endr#   r$   s       r)   register_scope_sanitize_endr8      s    '(949UUUr+   c                 ,    t          | j        |          S )z/Remove a scope-local scope-end event sanitizer.)_deregister_scope_sanitize_endr#   r.   s     r)   deregister_scope_sanitize_endr;      s    ),*;TBBBr+   c                 0    t          | j        |||          S )a  Register a scope-local tool sanitize-request guardrail.

    Args:
        scope_handle: Owning scope handle. The registration is removed when
            this scope is popped.
        name: Unique guardrail name within the owning scope.
        priority: Execution order for the guardrail. Lower values run first.
        guardrail: Callable invoked as ``guardrail(tool_name, args)`` that
            returns the sanitized payload recorded on emitted start events.

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

    Notes:
        As with the global variant, this sanitizes emitted event payloads only.
    )_register_tool_sanitize_requestr#   r$   s       r)   register_tool_sanitize_requestr>          $ +<+<dHiXXXr+   c                 ,    t          | j        |          S )a  Remove a scope-local tool sanitize-request guardrail.

    Args:
        scope_handle: Scope handle that owns the registration.
        name: Guardrail name previously passed to
            ``register_tool_sanitize_request()``.

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

    Notes:
        Removing the guardrail early affects only future work in the owning
        scope. Popping the scope would also remove it automatically.
    )!_deregister_tool_sanitize_requestr#   r.   s     r)    deregister_tool_sanitize_requestrB           -\->EEEr+   c                 0    t          | j        |||          S )a  Register a scope-local tool sanitize-response guardrail.

    Args:
        scope_handle: Owning scope handle. The registration is removed when
            this scope is popped.
        name: Unique guardrail name within the owning scope.
        priority: Execution order for the guardrail. Lower values run first.
        guardrail: Callable invoked as ``guardrail(tool_name, result)`` that
            returns the sanitized payload recorded on emitted end events.

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

    Notes:
        As with the global variant, this sanitizes emitted event payloads only.
    ) _register_tool_sanitize_responser#   r$   s       r)   register_tool_sanitize_responserF      s    $ ,L,=tXyYYYr+   c                 ,    t          | j        |          S )a  Remove a scope-local tool sanitize-response guardrail.

    Args:
        scope_handle: Scope handle that owns the registration.
        name: Guardrail name previously passed to
            ``register_tool_sanitize_response()``.

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

    Notes:
        Removing the guardrail early affects only future work in the owning
        scope. Popping the scope would also remove it automatically.
    )"_deregister_tool_sanitize_responser#   r.   s     r)   !deregister_tool_sanitize_responserI      s     .l.?FFFr+   c                 0    t          | j        |||          S )a  Register a scope-local tool conditional-execution guardrail.

    Args:
        scope_handle: Owning scope handle. The registration is removed when
            this scope is popped.
        name: Unique guardrail name within the owning scope.
        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 scope-local guardrail is
        registered.

    Notes:
        Scope-local conditional guardrails run in addition to global
        conditional guardrails for calls emitted under the owning scope.
    )$_register_tool_conditional_executionr#   r$   s       r)   #register_tool_conditional_executionrL      s    & 00A4S\]]]r+   c                 ,    t          | j        |          S )a  Remove a scope-local tool conditional-execution guardrail.

    Args:
        scope_handle: Scope handle that owns the registration.
        name: Guardrail name previously passed to
            ``register_tool_conditional_execution()``.

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

    Notes:
        Removing the guardrail early affects only future work in the owning
        scope. Popping the scope would also remove it automatically.
    )&_deregister_tool_conditional_executionr#   r.   s     r)   %deregister_tool_conditional_executionrO      s     2,2CTJJJr+   c                 2    t          | j        ||||          S )a
  Register a scope-local tool request intercept.

    Args:
        scope_handle: Owning scope handle. The registration is removed when
            this scope is popped.
        name: Unique intercept name within the owning scope.
        priority: Execution order for the intercept. Lower values run first.
        break_chain: Whether to stop applying lower-priority request intercepts
            after this intercept runs.
        fn: Callable invoked as ``fn(tool_name, args)`` that returns the
            rewritten tool arguments.

    Returns:
        None: This function returns after the scope-local intercept is
        registered.

    Notes:
        Scope-local request intercepts are merged with global intercepts using
        the same priority ordering rules.
    )_register_tool_requestr#   r%   r&   r'   break_chainfns        r)   register_tool_requestrU     s    * ","3T8[RTUUUr+   c                 ,    t          | j        |          S )a  Remove a scope-local tool request intercept.

    Args:
        scope_handle: Scope handle that owns the registration.
        name: Intercept name previously passed to ``register_tool_request()``.

    Returns:
        bool: ``True`` if an intercept was removed, otherwise ``False``.

    Notes:
        Removing the intercept early affects only future work in the owning
        scope. Popping the scope would also remove it automatically.
    )_deregister_tool_requestr#   r.   s     r)   deregister_tool_requestrX   ,  s     $L$5t<<<r+   c                 0    t          | j        |||          S )a$  Register scope-local middleware around tool execution.

    Args:
        scope_handle: Owning scope handle. The registration is removed when
            this scope is popped.
        name: Unique intercept name within the owning scope.
        priority: Execution order for the intercept. Lower values run first.
        fn: Callable invoked as ``fn(tool_name, args, next_call)``. It may call
            ``next_call(args)`` to continue execution, modify the result, or
            short-circuit the tool call entirely. It must return
            ``ToolExecutionInterceptOutcome``.

    Returns:
        None: This function returns after the scope-local intercept is
        registered.

    Notes:
        Execution intercepts wrap only calls emitted while the owning scope
        remains active.
    )_register_tool_executionr#   r%   r&   r'   rT   s       r)   register_tool_executionr\   =  s    * $L$5tXrJJJr+   c                 ,    t          | j        |          S )a  Remove a scope-local tool execution intercept.

    Args:
        scope_handle: Scope handle that owns the registration.
        name: Intercept name previously passed to
            ``register_tool_execution()``.

    Returns:
        bool: ``True`` if an intercept was removed, otherwise ``False``.

    Notes:
        Removing the intercept early affects only future work in the owning
        scope. Popping the scope would also remove it automatically.
    )_deregister_tool_executionr#   r.   s     r)   deregister_tool_executionr_   U  s     &l&7>>>r+   c                 0    t          | j        |||          S )a  Register a scope-local LLM sanitize-request guardrail.

    Args:
        scope_handle: Owning scope handle. The registration is removed when
            this scope is popped.
        name: Unique guardrail name within the owning scope.
        priority: Execution order for the guardrail. Lower values run first.
        guardrail: Callable invoked as ``guardrail(request)`` that returns the
            sanitized request recorded on emitted start events.

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

    Notes:
        As with the global variant, this sanitizes emitted event payloads only.
    )_register_llm_sanitize_requestr#   r$   s       r)   register_llm_sanitize_requestrb   l  s    $ *,*;T8YWWWr+   c                 ,    t          | j        |          S )a  Remove a scope-local LLM sanitize-request guardrail.

    Args:
        scope_handle: Scope handle that owns the registration.
        name: Guardrail name previously passed to
            ``register_llm_sanitize_request()``.

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

    Notes:
        Removing the guardrail early affects only future work in the owning
        scope. Popping the scope would also remove it automatically.
    ) _deregister_llm_sanitize_requestr#   r.   s     r)   deregister_llm_sanitize_requestre          ,L,=tDDDr+   c                 0    t          | j        |||          S )a  Register a scope-local LLM sanitize-response guardrail.

    Args:
        scope_handle: Owning scope handle. The registration is removed when
            this scope is popped.
        name: Unique guardrail name within the owning scope.
        priority: Execution order for the guardrail. Lower values run first.
        guardrail: Callable invoked as ``guardrail(response)`` that returns the
            sanitized payload recorded on emitted end events.

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

    Notes:
        As with the global variant, this sanitizes emitted event payloads only.
    )_register_llm_sanitize_responser#   r$   s       r)   register_llm_sanitize_responseri     r?   r+   c                 ,    t          | j        |          S )a  Remove a scope-local LLM sanitize-response guardrail.

    Args:
        scope_handle: Scope handle that owns the registration.
        name: Guardrail name previously passed to
            ``register_llm_sanitize_response()``.

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

    Notes:
        Removing the guardrail early affects only future work in the owning
        scope. Popping the scope would also remove it automatically.
    )!_deregister_llm_sanitize_responser#   r.   s     r)    deregister_llm_sanitize_responserl     rC   r+   c                 0    t          | j        |||          S )a  Register a scope-local LLM conditional-execution guardrail.

    Args:
        scope_handle: Owning scope handle. The registration is removed when
            this scope is popped.
        name: Unique guardrail name within the owning scope.
        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 it.

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

    Notes:
        Scope-local conditional guardrails run in addition to global
        conditional guardrails for calls emitted under the owning scope.
    )#_register_llm_conditional_executionr#   r$   s       r)   "register_llm_conditional_executionro     s    & /|/@$R[\\\r+   c                 ,    t          | j        |          S )a  Remove a scope-local LLM conditional-execution guardrail.

    Args:
        scope_handle: Scope handle that owns the registration.
        name: Guardrail name previously passed to
            ``register_llm_conditional_execution()``.

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

    Notes:
        Removing the guardrail early affects only future work in the owning
        scope. Popping the scope would also remove it automatically.
    )%_deregister_llm_conditional_executionr#   r.   s     r)   $deregister_llm_conditional_executionrr     s     11BDIIIr+   c                 2    t          | j        ||||          S )a,  Register a scope-local LLM request intercept.

    Args:
        scope_handle: Owning scope handle. The registration is removed when
            this scope is popped.
        name: Unique intercept name within the owning scope.
        priority: Execution order for the intercept. Lower values run first.
        break_chain: Whether to stop applying lower-priority request intercepts
            after this intercept runs.
        fn: Callable invoked as ``fn(name, request, annotated)`` that returns a
            tuple of ``(request, annotated)`` for the next stage.

    Returns:
        None: This function returns after the scope-local intercept is
        registered.

    Notes:
        Scope-local request intercepts are merged with global intercepts using
        the same priority ordering rules.
    )_register_llm_requestr#   rR   s        r)   register_llm_requestru     s    * !!2D(KQSTTTr+   c                 ,    t          | j        |          S )a  Remove a scope-local LLM request intercept.

    Args:
        scope_handle: Scope handle that owns the registration.
        name: Intercept name previously passed to ``register_llm_request()``.

    Returns:
        bool: ``True`` if an intercept was removed, otherwise ``False``.

    Notes:
        Removing the intercept early affects only future work in the owning
        scope. Popping the scope would also remove it automatically.
    )_deregister_llm_requestr#   r.   s     r)   deregister_llm_requestrx     s     #<#4d;;;r+   c                 0    t          | j        |||          S )a  Register scope-local middleware around non-streaming LLM execution.

    Args:
        scope_handle: Owning scope handle. The registration is removed when
            this scope is popped.
        name: Unique intercept name within the owning scope.
        priority: Execution order for the intercept. Lower values run first.
        fn: Callable invoked as ``fn(name, request, next_call)`` that may call
            ``next_call(request)`` to continue execution or short-circuit it.

    Returns:
        None: This function returns after the scope-local intercept is
        registered.

    Notes:
        Execution intercepts wrap only calls emitted while the owning scope
        remains active.
    )_register_llm_executionr#   r[   s       r)   register_llm_executionr{     s    & #<#4dHbIIIr+   c                 ,    t          | j        |          S )a  Remove a scope-local LLM execution intercept.

    Args:
        scope_handle: Scope handle that owns the registration.
        name: Intercept name previously passed to
            ``register_llm_execution()``.

    Returns:
        bool: ``True`` if an intercept was removed, otherwise ``False``.

    Notes:
        Removing the intercept early affects only future work in the owning
        scope. Popping the scope would also remove it automatically.
    )_deregister_llm_executionr#   r.   s     r)   deregister_llm_executionr~   &  s     %\%6===r+   c                 0    t          | j        |||          S )a	  Register scope-local middleware around streaming LLM execution.

    Args:
        scope_handle: Owning scope handle. The registration is removed when
            this scope is popped.
        name: Unique intercept name within the owning scope.
        priority: Execution order for the intercept. Lower values run first.
        fn: Callable invoked as ``fn(request, next_call)`` that returns an
            async iterator of chunks, either by delegating to ``next_call`` or
            by replacing the stream entirely.

    Returns:
        None: This function returns after the scope-local intercept is
        registered.

    Notes:
        Streaming execution intercepts wrap chunk production only. They do not
        replace the collector or finalizer callbacks.
    )_register_llm_stream_executionr#   r[   s       r)   register_llm_stream_executionr   8  s    ( *,*;T8RPPPr+   c                 ,    t          | j        |          S )a  Remove a scope-local streaming LLM execution intercept.

    Args:
        scope_handle: Scope handle that owns the registration.
        name: Intercept name previously passed to
            ``register_llm_stream_execution()``.

    Returns:
        bool: ``True`` if an intercept was removed, otherwise ``False``.

    Notes:
        Removing the intercept early affects only future work in the owning
        scope. Popping the scope would also remove it automatically.
    ) _deregister_llm_stream_executionr#   r.   s     r)   deregister_llm_stream_executionr   O  rf   r+   c                 .    t          | j        ||          S )a  Register an event subscriber that is active only for ``scope_handle``.

    Args:
        scope_handle: Owning scope handle. The registration is removed when
            this scope is popped.
        name: Unique subscriber name within the owning scope.
        callback: Callable invoked as ``callback(event)`` for each lifecycle
            event emitted while the scope remains active.

    Returns:
        None: This function returns after the scope-local subscriber is
        registered.

    Notes:
        The subscriber observes only events emitted while the owning scope
        remains active.

    Example::

        import nemo_relay

        def log_event(event):
            print(event.kind, event.name)

        with nemo_relay.scope.scope("request", nemo_relay.ScopeType.Agent) as handle:
            nemo_relay.scope_local.register_subscriber(handle, "logger", log_event)
    )_register_subscriberr#   )r%   r&   callbacks      r)   register_subscriberr   f  s    8   14BBBr+   c                 ,    t          | j        |          S )a  Remove a scope-local event subscriber.

    Args:
        scope_handle: Scope handle that owns the registration.
        name: Subscriber name previously passed to ``register_subscriber()``.

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

    Notes:
        Removing the subscriber early affects only future event delivery in the
        owning scope. Popping the scope would also remove it automatically.
    )_deregister_subscriberr#   r.   s     r)   deregister_subscriberr     s     ","3T:::r+   )r*   r/   r2   r5   r8   r;   r>   rB   rF   rI   rL   rO   rU   rX   r\   r_   rb   re   ri   rl   ro   rr   ru   rx   r{   r~   r   r   r   r   N)]__doc__nemo_relay._nativer   rq   r   r}   r   rw   r   rd   r   rk   r   r   r	   r-   r
   r:   r   r4   r   r   r   rN   r   r^   r   rW   r   rA   r   rH   r   rn   r   rz   r   rt   r   ra   r   rh   r   r   r   r"   r   r7   r   r1   r   r   r   rK   r   rZ   r   rQ   r   r=   r    rE   r*   r/   r2   r5   r8   r;   r>   rB   rF   rI   rL   rO   rU   rX   r\   r_   rb   re   ri   rl   ro   rr   ru   rx   r{   r~   r   r   r   r   __all__ r+   r)   <module>r      s   $                                                                                                                                                      Q Q Q
> > >
X X X
E E E
V V V
C C CY Y Y*F F F$Z Z Z*G G G$^ ^ ^,K K K.V V V0= = ="K K K0? ? ?.X X X*E E E$Y Y Y*F F F$] ] ],J J J.U U U0< < <"J J J,> > >$Q Q Q.E E E.C C C>; ; ;"% % %r+   