o
    i                     @  sv   d dl mZ d dlZd dlmZ d dlZddlmZ eg ejf Z	ej
e	dB  dZdd	d
ZdddZdddZdS )    )annotationsN)Callable   )loggeragent_http_sessionreturn_ClientFactoryc                    s    d  d fdd} t |  | S )Nr   aiohttp.ClientSessionc                    sj    d u s j r3td ddlm}  z|  jj}W n ty$   d }Y nw tj	ddd}tj
||d  S )Nz-http_session(): creating a new httpclient ctxr   )get_job_context2   x   )limit_per_hostkeepalive_timeout)proxy	connector)closedr   debugjobr
   proc
http_proxyRuntimeErroraiohttpTCPConnectorClientSession)r
   r   r   	g_session c/var/www/html/livekit_bhavya/venv/lib/python3.10/site-packages/livekit/agents/utils/http_context.py_new_session   s   
z&_new_session_ctx.<locals>._new_sessionr   r	   )_ContextVarset)r   r   r   r   _new_session_ctx   s   
r"   r	   c                  C  s    t d} | du rtd|  S )zOptional utility function to avoid having to manually manage an aiohttp.ClientSession lifetime.
    On job processes, this http session will be bound to the main event loop.
    Na  Attempted to use an http session outside of a job context. This is probably because you are trying to use a plugin without using the agent worker api. You may need to create your own aiohttp.ClientSession, pass it into the plugin constructor as a kwarg, and manage its lifecycle.)r    getr   valr   r   r   http_session(   s   
r&   Nonec                    s@   t d } | d urtd |   I d H  t d  d S d S )Nz*http_session(): closing the httpclient ctx)r    r#   r   r   closer!   r$   r   r   r   _close_http_ctx6   s   

r)   )r   r   r   )r   r'   )
__future__r   contextvarscollections.abcr   r   logr   r   r   
ContextVarr    r"   r&   r)   r   r   r   r   <module>   s    

