
    Rmj"              	         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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Zdddddddd-d Zdddd!d.d$Zddddd%d/d&Zedddddddd'd0d*            Zg d+ZdS )1az  Scope stack operations.

Scopes define the hierarchy that tool calls, LLM calls, and mark events attach
to. They are the main way to model agents, tasks, and nested units of work.

Example::

    import nemo_relay

    with nemo_relay.scope.scope("demo-agent", nemo_relay.ScopeType.Agent) as handle:
        nemo_relay.scope.event("checkpoint", handle=handle, data={"step": 1})
    )annotations)contextmanager)datetime)Iterator)Json)ensure_scope_stack)ScopeAttributesScopeHandle	ScopeType)event)
get_handle)	pop_scope)
push_scopereturnr
   c                 :    t                       t                      S )a$  Return the current top-of-stack ``ScopeHandle``.

    Returns:
        ScopeHandle: The scope currently at the top of the active scope stack.

    Notes:
        If the current Python context does not yet have a scope stack, one is
        created automatically before the handle lookup.
    )_ensure_scope_stack_native_get_handle     X/home/thesage/.hermes/hermes-agent/venv/lib/python3.11/site-packages/nemo_relay/scope.pyr   r   ,   s     r   Nhandle
attributesdatametadatainput	timestampnamestr
scope_typer   r   ScopeHandle | Noner   ScopeAttributes | Noner   Json | Noner   r   r   datetime | Nonec          
     L    t                       t          | |||||||          S )aA  Push a new child scope and return its handle.

    Args:
        name: Human-readable name for the new scope.
        scope_type: Semantic scope type, such as ``ScopeType.Agent`` or
            ``ScopeType.Function``.
        handle: Optional parent scope handle. When omitted, the current
            top-of-stack scope becomes the parent.
        attributes: Optional native scope attributes attached to the emitted
            start event.
        data: Optional JSON application payload stored on the scope handle.
        metadata: Optional JSON metadata recorded on the scope start event.
        input: Optional JSON payload exported as the semantic scope input.
        timestamp: Optional timezone-aware ``datetime`` recorded as the handle
            start time and on the scope start event. When omitted, the current
            runtime time is used.

    Returns:
        ScopeHandle: Handle for the newly pushed scope.

    Notes:
        A scope stack is created automatically if the current context does not
        yet have one. ``timestamp`` must be a timezone-aware ``datetime``;
        strings and naive datetimes are rejected.

    Example::

        import nemo_relay

        with nemo_relay.scope.scope("parent", nemo_relay.ScopeType.Agent) as parent:
            handle = nemo_relay.scope.push(
                "worker",
                nemo_relay.ScopeType.Function,
                handle=parent,
                attributes=None,
                data={"step": 1},
                metadata={"source": "scope.push"},
            )
            nemo_relay.scope.pop(handle)
    r   )r   _native_push_scope)r   r    r   r   r   r   r   r   s           r   pushr'   :   s@    f 	 	 	 	r   outputr   r   r)   Nonec               H    t                       t          | |||           dS )aA  Pop a scope previously returned by ``push()`` or ``scope()``.

    Args:
        handle: Scope handle to close.
        output: Optional JSON payload exported as the semantic scope output.
        metadata: Optional JSON metadata to append to the metadata set when the scope was created.
        timestamp: Optional timezone-aware ``datetime`` recorded on the scope
            end event. When omitted, the runtime default end timestamp is used.

    Returns:
        None: This function returns after the scope is closed successfully.

    Notes:
        The handle must correspond to an active scope in the current scope
        stack. Popping a scope also removes any scope-local registrations owned
        by that scope. ``timestamp`` must be a timezone-aware ``datetime``;
        strings and naive datetimes are rejected.
    r(   N)r   _native_pop_scope)r   r)   r   r   s       r   popr-   z   s-    * fVh)TTTTTTr   r   r   r   r   c               J    t                       t          | ||||           dS )a<  Emit a ``Mark`` event under the current or provided scope.

    Args:
        name: Event name to emit.
        handle: Optional scope handle that should own the event. When omitted,
            the current top-of-stack scope is used.
        data: Optional JSON payload attached to the event.
        metadata: Optional JSON metadata attached to the event.
        timestamp: Optional timezone-aware ``datetime`` recorded on the mark
            event. When omitted, the current runtime time is used.

    Returns:
        None: This function returns after the event has been emitted.

    Notes:
        A scope stack is created automatically when needed before the event is
        emitted through the native runtime. ``timestamp`` must be a
        timezone-aware ``datetime``; strings and naive datetimes are rejected.
    r.   N)r   _native_event)r   r   r   r   r   s        r   r   r      s/    6 $vD8yYYYYYYr   )r   r   r   r   r   r   end_timestampr1   Iterator[ScopeHandle]c          
   #  F  K   t                       d}	d}
d}	 t          | |||||||          }	|	V  d}
n$# t          $ r}d}
t          |          } d}~ww xY w	 |	d|
i}|||d<   t	          |	||           dS dS # |	d|
i}|||d<   t	          |	||           w w xY w)	a  Create a scope for the duration of a ``with`` block.

    OTEL status codes will be automatically recorded in the scope's metadata.

    Args:
        name: Human-readable name for the new scope.
        scope_type: Semantic scope type, such as ``ScopeType.Agent`` or
            ``ScopeType.Function``.
        handle: Optional parent scope handle. When omitted, the current
            top-of-stack scope becomes the parent.
        attributes: Optional native scope attributes attached to the emitted
            start event.
        data: Optional JSON application payload stored on the scope handle.
        metadata: Optional JSON metadata recorded on the scope start event.
        input: Optional JSON payload exported as the semantic scope input.
        timestamp: Optional timezone-aware ``datetime`` recorded as the handle
            start time and on the scope start event.
        end_timestamp: Optional timezone-aware ``datetime`` recorded on the
            scope end event.

    Yields:
        ScopeHandle: Handle for the scope that remains active inside the
        ``with`` block.

    Notes:
        The scope is always popped when the ``with`` block exits, even if the
        body raises an exception. Timestamp arguments must be timezone-aware
        ``datetime`` objects; strings and naive datetimes are rejected.

    Example::

        import nemo_relay

        with nemo_relay.scope.scope(
            "demo",
            nemo_relay.ScopeType.Agent,
            handle=None,
            attributes=None,
            data={"stage": "start"},
            metadata={"owner": "docs"},
        ) as handle:
            nemo_relay.scope.event("inside", handle=handle, data={"ok": True}, metadata={"step": 1})
    NUNSETr   OKERRORzotel.status_codezotel.status_description)r   r   )r   r&   	Exceptionr   r-   )r   r    r   r   r   r   r   r   r1   pushed_handlestatus_codestatus_messagees                r   scoper<      s)     p MKNK*!	
 	
 	
    Q 	 $*K8H)6D23MJJJJJJ	 %$=$*K8H)6D23MJJJJJ	 %s&   6 A> 
A AAA> >"B )r   r   r-   r'   r<   )r   r
   )r   r   r    r   r   r!   r   r"   r   r#   r   r#   r   r#   r   r$   r   r
   )
r   r
   r)   r#   r   r#   r   r$   r   r*   )r   r   r   r!   r   r#   r   r#   r   r$   r   r*   )r   r   r    r   r   r!   r   r"   r   r#   r   r#   r   r#   r   r$   r1   r$   r   r2   )__doc__
__future__r   
contextlibr   r   typingr   
nemo_relayr   nemo_relay._contextr   r   nemo_relay._nativer	   r
   r   r   r0   r   r   r   r,   r   r&   r'   r-   r<   __all__r   r   r   <module>rE      s.    # " " " " " % % % % % %                   I I I I I I         
                    
       $ "&)- !%= = = = = =B 37PTswU U U U U U8 "& !%Z Z Z Z Z Z> 
 "&)- !%%)QK QK QK QK QK QKh :
9
9r   