
    g3                    *   d dl mZ d dlmZ d dlmZ d dl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 d dlmZ d d	lmZ  G d
 de      Z G d de	      Ze G d d             Ze G d d             Z G d de      Z G d de	      Z G d de	      Zy)    )annotations)abstractmethod)	dataclass)TYPE_CHECKINGCallableProtocolcast)
ForwardMsg)
AppSession)
ScriptData)ScriptCache)UploadedFileManagerc                      e Zd ZdZy)SessionClientDisconnectedErrorz5Raised by operations on a disconnected SessionClient.N__name__
__module____qualname____doc__     V/var/www/openai/venv/lib/python3.12/site-packages/streamlit/runtime/session_manager.pyr   r      s    ?r   r   c                  "    e Zd ZdZedd       Zy)SessionClientz1Interface for sending data to a session's client.c                    t         )zDeliver a ForwardMsg to the client.

        If the SessionClient has been disconnected, it should raise a
        SessionClientDisconnectedError.
        NotImplementedError)selfmsgs     r   write_forward_msgzSessionClient.write_forward_msg$   s
     "!r   N)r   r
   returnNone)r   r   r   r   r   r    r   r   r   r   r   !   s    ;" "r   r   c                  4    e Zd ZU dZded<   ded<   dZded<   y	)
ActiveSessionInfozType containing data related to an active session.

    This type is nearly identical to SessionInfo. The difference is that when using it,
    we are guaranteed that SessionClient is not None.
    r   clientr   sessionr   intscript_run_countN)r   r   r   r   __annotations__r(   r   r   r   r$   r$   .   s      cr   r$   c                  D    e Zd ZU dZded<   ded<   dZded<   dd	Zdd
Zy)SessionInfozType containing data related to an AppSession.

    For each AppSession, the Runtime tracks that session's
    script_run_count. This is used to track the age of messages in
    the ForwardMsgCache.
    zSessionClient | Noner%   r   r&   r   r'   r(   c                    | j                   d uS )N)r%   r   s    r   	is_activezSessionInfo.is_activeH   s    {{$&&r   c                P    | j                         sJ d       t        t        |       S )Nz.A SessionInfo with no client cannot be active!)r.   r	   r$   r-   s    r   	to_activezSessionInfo.to_activeK   s(    ~~Q!QQ
 %t,,r   N)r!   bool)r!   r$   )r   r   r   r   r)   r(   r.   r0   r   r   r   r+   r+   ;   s*     ! c'-r   r+   c                      e Zd ZdZy)SessionStorageErrorzException class for errors raised by SessionStorage.

    The original error that causes a SessionStorageError to be (re)raised will generally
    be an I/O error specific to the concrete SessionStorage implementation.
    Nr   r   r   r   r3   r3   T   s    r   r3   c                  T    e Zd Zedd       Zedd       Zedd       Zed	d       Zy)
SessionStoragec                    t         )a.  Return the SessionInfo corresponding to session_id, or None if one does not
        exist.

        Parameters
        ----------
        session_id
            The unique ID of the session being fetched.

        Returns
        -------
        SessionInfo or None

        Raises
        ------
        SessionStorageError
            Raised if an error occurs while attempting to fetch the session. This will
            generally happen if there is an error with the underlying storage backend
            (e.g. if we lose our connection to it).
        r   r   
session_ids     r   getzSessionStorage.get]   
    * "!r   c                    t         )a  Save the given session.

        Parameters
        ----------
        session_info
            The SessionInfo being saved.

        Raises
        ------
        SessionStorageError
            Raised if an error occurs while saving the given session.
        r   )r   session_infos     r   savezSessionStorage.savet   
     "!r   c                    t         )a  Mark the session corresponding to session_id for deletion and stop tracking
        it.

        Note that:
          - Calling delete on an ID corresponding to a nonexistent session is a no-op.
          - Calling delete on an ID should cause the given session to no longer be
            tracked by this SessionStorage, but exactly when and how the session's data
            is eventually cleaned up is a detail left up to the implementation.

        Parameters
        ----------
        session_id
            The unique ID of the session to delete.

        Raises
        ------
        SessionStorageError
            Raised if an error occurs while attempting to delete the session.
        r   r7   s     r   deletezSessionStorage.delete   r:   r   c                    t         )zList all sessions tracked by this SessionStorage.

        Returns
        -------
        List[SessionInfo]

        Raises
        ------
        SessionStorageError
            Raised if an error occurs while attempting to list sessions.
        r   r-   s    r   listzSessionStorage.list   s
     "!r   Nr8   strr!   zSessionInfo | None)r<   r+   r!   r"   r8   rD   r!   r"   r!   zlist[SessionInfo])r   r   r   r   r9   r=   r@   rB   r   r   r   r5   r5   \   sP    " ", " " " ", " "r   r5   c                      e Zd ZdZe	 	 	 	 	 	 	 	 	 	 dd       Ze	 	 d	 	 	 	 	 	 	 	 	 	 	 dd       Zedd       Zedd       Zedd       Z	ddZ
dd	Zdd
ZddZddZddZy)SessionManagera7  SessionManagers are responsible for encapsulating all session lifecycle behavior
    that the Streamlit Runtime may care about.

    A SessionManager must define the following required methods:
      - __init__
      - connect_session
      - close_session
      - get_session_info
      - list_sessions

    SessionManager implementations may also choose to define the notions of active and
    inactive sessions. The precise definitions of active/inactive are left to the
    concrete implementation. SessionManagers that wish to differentiate between active
    and inactive sessions should have the required methods listed above operate on *all*
    sessions. Additionally, they should define the following methods for working with
    active sessions:
      - disconnect_session
      - get_active_session_info
      - is_active_session
      - list_active_sessions

    When active session-related methods are left undefined, their default
    implementations are the naturally corresponding required methods.

    The Runtime, unless there's a good reason to do otherwise, should generally work
    with the active-session versions of a SessionManager's methods. There isn't currently
    a need for us to be able to operate on inactive sessions stored in SessionStorage
    outside of the SessionManager itself. However, it's highly likely that we'll
    eventually have to do so, which is why the abstractions allow for this now.

    Notes
    -----
    Threading: All SessionManager methods are *not* threadsafe -- they must be called
    from the runtime's eventloop thread.
    c                    t         )a  Initialize a SessionManager with the given SessionStorage.

        Parameters
        ----------
        session_storage
            The SessionStorage instance backing this SessionManager.

        uploaded_file_manager
            Used to manage files uploaded by users via the Streamlit web client.

        script_cache
            ScriptCache instance. Caches user script bytecode.

        message_enqueued_callback
            A callback invoked after a message is enqueued to be sent to a web client.
        r   )r   session_storageuploaded_file_managerscript_cachemessage_enqueued_callbacks        r   __init__zSessionManager.__init__   s
    0 "!r   Nc                    t         )a  Create a new session or connect to an existing one.

        Parameters
        ----------
        client
            A concrete SessionClient implementation for communicating with
            the session's client.
        script_data
            Contains parameters related to running a script.
        user_info
            A dict that contains information about the session's user. For now,
            it only (optionally) contains the user's email address.

            {
                "email": "example@example.com"
            }
        existing_session_id
            The ID of an existing session to reconnect to. If one is not provided, a new
            session is created. Note that whether a SessionManager supports reconnecting
            to an existing session is left up to the concrete SessionManager
            implementation. Those that do not support reconnection should simply ignore
            this argument.
        session_id_override
            The ID to assign to a new session being created with this method. Setting
            this can be useful when the service that a Streamlit Runtime is running in
            wants to tie the lifecycle of a Streamlit session to some other session-like
            object that it manages. Only one of existing_session_id and
            session_id_override should be set.

        Returns
        -------
        str
            The session's unique string ID.
        r   )r   r%   script_data	user_infoexisting_session_idsession_id_overrides         r   connect_sessionzSessionManager.connect_session   s    V "!r   c                    t         )a*  Close and completely delete the session with the given id.

        This function may be called multiple times for the same session,
        which is not an error. (Subsequent calls just no-op.)

        Parameters
        ----------
        session_id
            The session's unique ID.
        r   r7   s     r   close_sessionzSessionManager.close_session  s
     "!r   c                    t         )zReturn the SessionInfo for the given id, or None if no such session
        exists.

        Parameters
        ----------
        session_id
            The session's unique ID.

        Returns
        -------
        SessionInfo or None
        r   r7   s     r   get_session_infozSessionManager.get_session_info%  r>   r   c                    t         )zReturn the SessionInfo for all sessions managed by this SessionManager.

        Returns
        -------
        List[SessionInfo]
        r   r-   s    r   list_sessionszSessionManager.list_sessions5  s
     "!r   c                4    t        | j                               S )zReturn the number of sessions tracked by this SessionManager.

        Subclasses of SessionManager shouldn't provide their own implementation of this
        method without a *very* good reason.

        Returns
        -------
        int
        )lenrZ   r-   s    r   num_sessionszSessionManager.num_sessions?  s     4%%'((r   c                &    | j                  |       y)zDisconnect the given session.

        This method should be idempotent.

        Parameters
        ----------
        session_id
            The session's unique ID.
        N)rV   r7   s     r   disconnect_sessionz!SessionManager.disconnect_sessionP  s     	:&r   c                j    | j                  |      }||j                         sy|j                         S )a/  Return the ActiveSessionInfo for the given id, or None if either no such
        session exists or the session is not active.

        Parameters
        ----------
        session_id
            The active session's unique ID.

        Returns
        -------
        ActiveSessionInfo or None
        N)rX   r.   r0   )r   r8   r&   s      r   get_active_session_infoz&SessionManager.get_active_session_info\  s6     ''
3?'"3"3"5  ""r   c                (    | j                  |      duS )z~Return True if the given session exists and is active, False otherwise.

        Returns
        -------
        bool
        N)ra   r7   s     r   is_active_sessionz SessionManager.is_active_sessionn  s     ++J7tCCr   c                d    | j                         D cg c]  }|j                          c}S c c}w )zReturn the session info for all active sessions tracked by this SessionManager.

        Returns
        -------
        List[ActiveSessionInfo]
        )rZ   r0   )r   ss     r   list_active_sessionsz#SessionManager.list_active_sessionsw  s-     (,'9'9';<';!';<<<s   -c                4    t        | j                               S )a   Return the number of active sessions tracked by this SessionManager.

        Subclasses of SessionManager shouldn't provide their own implementation of this
        method without a *very* good reason.

        Returns
        -------
        int
        )r\   rf   r-   s    r   num_active_sessionsz"SessionManager.num_active_sessions  s     4,,.//r   )
rJ   r5   rK   r   rL   r   rM   zCallable[[], None] | Noner!   r"   )NN)r%   r   rP   r   rQ   zdict[str, str | bool | None]rR   
str | NonerS   ri   r!   rD   rE   rC   rF   )r!   r'   )r8   rD   r!   zActiveSessionInfo | None)r8   rD   r!   r1   )r!   zlist[ActiveSessionInfo])r   r   r   r   r   rN   rT   rV   rX   rZ   r]   r_   ra   rc   rf   rh   r   r   r   rH   rH      s    "H "'"  3" "	"
 $=" 
" "2  +/*.*"*"  *" 0	*"
 (*" (*" 
*" *"X " " " " " "
)"
'#$D=
0r   rH   N)
__future__r   abcr   dataclassesr   typingr   r   r   r	   streamlit.proto.ForwardMsg_pb2r
   streamlit.runtime.app_sessionr   streamlit.runtime.script_datar   +streamlit.runtime.scriptrunner.script_cacher   'streamlit.runtime.uploaded_file_managerr   	Exceptionr   r   r$   r+   r3   r5   rH   r   r   r   <module>rt      s    #  ! : :988GK@Y @
"H 
" 	 	 	 - - -0) L"X L"^_0X _0r   