a
    ”˜ëj>  ã                   @   s   d Z G dd„ dƒZdS )z:Middleware for making it easier to do distributed tracing.c                   @   s&   e Zd ZdZdd„ Zedœdd„ZdS )ÚTracingMiddlewareaZ  Middleware for propagating distributed tracing context using LangSmith.

    This middleware checks for the 'langsmith-trace' header and propagates the
    tracing context if present. It does not start new traces by default.
    It is designed to work with ASGI applications.

    Attributes:
        app: The ASGI application being wrapped.
    c                 C   s   ddl m} || _|| _dS )zInitialize the middleware.é    )Útracing_contextN)Zlangsmith.run_helpersr   Ú_with_headersÚapp)Úselfr   r   © r   úS/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/langsmith/middleware.pyÚ__init__   s    zTracingMiddleware.__init__)Úscopec                 Ã   s„   |d dkrld|v rlt |d ƒ}d|v rl| j|d��$ |  |||¡I dH  W d  ƒ n1 s^0    Y  dS |  |||¡I dH  dS )a  Handle incoming requests and propagate tracing context if applicable.

        Args:
            scope: A dict containing ASGI connection scope.
            receive: An awaitable callable for receiving ASGI events.
            send: An awaitable callable for sending ASGI events.

        If the request is HTTP and contains the 'langsmith-trace' header,
        it propagates the tracing context before calling the wrapped application.
        Otherwise, it calls the application directly without modifying the context.
        ÚtypeÚhttpÚheaderss   langsmith-trace)ÚparentN)Údictr   r   )r   r
   ZreceiveÚsendr   r   r   r   Ú__call__   s    2zTracingMiddleware.__call__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r	   r   r   r   r   r   r   r      s   
r   N)r   r   r   r   r   r   Ú<module>   s   