
    g                        U d dl mZ d dlmZmZmZmZ d dlmZ d dl	m
Z
 d dlmZmZmZmZmZ erd dlmZ d dlmZ d dlmZ  ee      Zd	ed
<    G d de      Zy)    )annotations)TYPE_CHECKINGCallableFinalcast)
get_logger)
AppSession)ActiveSessionInfoSessionClientSessionInfoSessionManagerSessionStorage)
ScriptData)ScriptCache)UploadedFileManagerr   _LOGGERc                      e Zd ZdZ	 	 	 	 	 	 	 	 	 	 ddZ	 	 d	 	 	 	 	 	 	 	 	 	 	 ddZddZddZddZddZ	dd	Z
dd
ZddZy)WebsocketSessionManagera  A SessionManager used to manage sessions with lifecycles tied to those of a
    browser tab's websocket connection.

    WebsocketSessionManagers differentiate between "active" and "inactive" sessions.
    Active sessions are those with a currently active websocket connection. Inactive
    sessions are sessions without. Eventual cleanup of inactive sessions is a detail left
    to the specific SessionStorage that a WebsocketSessionManager is instantiated with.
    c                J    || _         || _        || _        || _        i | _        y N)_session_storage_uploaded_file_mgr_script_cache_message_enqueued_callback_active_session_info_by_id)selfsession_storageuploaded_file_managerscript_cachemessage_enqueued_callbacks        `/var/www/openai/venv/lib/python3.12/site-packages/streamlit/runtime/websocket_session_manager.py__init__z WebsocketSessionManager.__init__/   s0     !0"7)*C' IK'    Nc                "   |r	|rJ d       || j                   v rt        j                  d|       |xr+ || j                   vxr | j                  j	                  |      }|r{|j
                  }|j                          t        |||j                        | j                   |j                  <   | j                  j                  |j                         |j                  S t        || j                  | j                  | j                  ||      }t        j                  dt        |      |j                         |j                  | j                   vsJ d|j                   d       t        ||      | j                   |j                  <   |j                  S )NzHOnly one of existing_session_id and session_id_override should be truthyzESession with id %s is already connected! Connecting to a new session.)script_datar   r   r    	user_infosession_id_overridez1Created new session for client %s. Session ID: %szsession.id 'z' registered multiple times!)r   r   warningr   getsessionregister_file_watchersr
   script_run_countiddeleter	   r   r   r   debug)	r   clientr%   r&   existing_session_idr'   session_infoexisting_sessionr*   s	            r!   connect_sessionz'WebsocketSessionManager.connect_session>   s    (,? 	
V	
@ $"A"AAOOW#   ?#4+J+JJ?%%))*=> 	 +33335CT --DD++,<,?,?@
 !!(()9)<)<=#&&&#"&"9"9++&*&E&E 3
 	?FWZZ	
 zz!@!@@ 	
7::,&BC	
@ 7HPW6X''

3zzr#   c                   || j                   v rz| j                   |   }|j                  }|j                          |j                          | j                  j                  t        d ||j                               | j                   |= y y )N)r0   r*   r,   )r   r*   request_script_stopdisconnect_file_watchersr   saver   r,   )r   
session_idactive_session_infor*   s       r!   disconnect_sessionz*WebsocketSessionManager.disconnect_sessionw   s    888"&"A"A*"M)11G''),,.!!&&#%8%I%I //
; 9r#   c                8    | j                   j                  |      S r   )r   r)   r   r9   s     r!   get_active_session_infoz/WebsocketSessionManager.get_active_session_info   s    ..22:>>r#   c                    || j                   v S r   )r   r=   s     r!   is_active_sessionz)WebsocketSessionManager.is_active_session   s    T<<<<r#   c                H    t        | j                  j                               S r   )listr   valuesr   s    r!   list_active_sessionsz,WebsocketSessionManager.list_active_sessions   s    D33::<==r#   c                4   || j                   v r7| j                   |   }| j                   |= |j                  j                          y | j                  j	                  |      }|r6| j                  j                  |       |j                  j                          y y r   )r   r*   shutdownr   r)   r.   )r   r9   r:   r2   s       r!   close_sessionz%WebsocketSessionManager.close_session   s    888"&"A"A*"M//
;''002,,00<!!((4  ))+ r#   c                ~    | j                  |      }|rt        t        |      S | j                  j	                  |      S r   )r>   r   r   r   r)   )r   r9   r2   s      r!   get_session_infoz(WebsocketSessionManager.get_session_info   s9    33J?\22$$((44r#   c                    t        t        t           | j                               | j                  j                         z   S r   )r   rB   r   rE   r   rD   s    r!   list_sessionsz%WebsocketSessionManager.list_sessions   s6    k"D$=$=$?@##((*+	
r#   )
r   r   r   r   r   r   r    zCallable[[], None] | NonereturnNone)NN)r0   r   r%   r   r&   zdict[str, str | bool | None]r1   
str | Noner'   rO   rM   str)r9   rP   rM   rN   )r9   rP   rM   zActiveSessionInfo | None)r9   rP   rM   bool)rM   zlist[ActiveSessionInfo])r9   rP   rM   zSessionInfo | None)rM   zlist[SessionInfo])__name__
__module____qualname____doc__r"   r4   r;   r>   r@   rE   rH   rJ   rL    r#   r!   r   r   %   s    K'K  3K "	K
 $=K 
K( +/*.77  7 0	7
 (7 (7 
7r<"?=>
,5
r#   r   N)
__future__r   typingr   r   r   r   streamlit.loggerr   streamlit.runtime.app_sessionr	   !streamlit.runtime.session_managerr
   r   r   r   r   streamlit.runtime.script_datar   +streamlit.runtime.scriptrunner.script_cacher   'streamlit.runtime.uploaded_file_managerr   rR   r   __annotations__r   rV   r#   r!   <module>r`      sK    # 7 7 ' 4  8GKH% %B
n B
r#   