o
    +iT                     @   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defddZdS )TracingMiddlewareaW  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.

    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)langsmith.run_helpersr   _with_headersapp)selfr   r    r   Q/var/www/html/psymed-ai/venv/lib/python3.10/site-packages/langsmith/middleware.py__init__   s   
zTracingMiddleware.__init__scopec                    s   |d dkr8d|v r8t |d }d|v r8| j|d | |||I dH  W d   dS 1 s1w   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 (callable): An awaitable callable for receiving ASGI events.
            send (callable): 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   receivesendr   r   r   r	   __call__   s   
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    