
    'h                     D    d dl mZmZ d dlmZmZ d dlmZ  G d d      Zy)    )DictList)datetime	timedelta)uuid4c                   N    e Zd Zd
defdZdefdZdedefdZdeddfdZ	dd	Z
y)SessionManagerNsession_expiry_minutesc                 .    i | _         || _        || _        y)a"  
        Initialize the session manager with a specified expiry time.
        
        Args:
            session_expiry_minutes (int): Number of minutes after which an inactive session expires
            
            llm_client: The language model client (hotelgraph in this case)
        N)	_sessions_session_expiry_minutes_llm_client)selfr
   
llm_clients      4/home/kushmeetdev/Regenta/Chatbot/session_manager.py__init__zSessionManager.__init__   s     +-'=$%    returnc                     t        t                     }g t        j                         dt        j                         j	                         idddd| j
                  |<   |S )zo
        Create a new session.
        
        Returns:
            str: The newly created session ID
        session_startFN)historylast_accessmetadatahas_guest_details
guest_name
guest_room)strr   r   now	isoformatr   r   
session_ids     r   create_sessionzSessionManager.create_session   sW     \
#<<>X\\^557 "'	&
z" r   r!   c                     || j                   v S )z
        Check if a session exists.
        
        Args:
            session_id (str): The session ID to check
            
        Returns:
            bool: True if session exists, False otherwise
        )r   r    s     r   session_existszSessionManager.session_exists)   s     T^^++r   c           	          || j                   vrt        d| d       t        d| d      	 t        d|z          | j                   |= y# t        $ r$}t        d| dt	        |              Y d}~yd}~ww xY w)z
        Explicitly close a session and cleanup its thread.
        
        Args:
            session_id (str): The session ID to close
        zSession z already closed or not foundz
 not foundzSession closed: zError closing session z: N)r   printKeyError	Exceptionr   )r   r!   es      r   close_sessionzSessionManager.close_session6   s     T^^+HZL(DEFXj\<==	C$z12z* 	C*:,bQABB	Cs   A
 
	A7A22A7c                    t        j                         }| j                  j                         D cg c]&  \  }}||d   z
  t	        | j
                        kD  r|( }}}|D ]  }t        d|z          | j                  |=  yc c}}w )zHRemove sessions that haven't been accessed for the specified expiry timer   )minuteszSession expired and closed: N)r   r   r   itemsr   r   r&   )r   current_timer!   dataexpired_sessionss        r   cleanup_expired_sessionsz'SessionManager.cleanup_expired_sessionsJ   s    ||~/3~~/C/C/E
+:td=11IdFbFb4cc 
 
 + 	+J0:=>z*	+
s   +B)   N)r   N)__name__
__module____qualname__intr   r   r"   boolr$   r*   r1    r   r   r	   r	      sH    &s & ,
, 
, 
,C C C(+r   r	   N)typingr   r   r   r   uuidr   r	   r8   r   r   <module>r;      s     ( P+ P+r   