
    )`i                         d dl m Z mZ d dlmZ d dlmZmZ 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 dd
lmZ defdZdefdZdefdZdS )    )datetimetimezone)
HTTPStatus)RequestResponse)HTTPException   )logger   )get_session_manager)SESSION_DISABLED_ERROR_DETAILSESSION_DISABLED_LOG_MESSAGESageMakerSessionHeaderSessionRequestType)get_session_id_from_requestrequest_typec                 b    | t           j        k    rt          S | t           j        k    rt          S dS )zMap session request type to the appropriate handler function.

    Args:
        request_type: The type of session request

    Returns:
        Handler function for the request type, or None if no handler
    N)r   NEW_SESSIONcreate_sessionCLOSEclose_session)r   s    /home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/model_hosting_container_standards/sagemaker/sessions/handlers.pyget_handler_for_request_typer      s3     )555	+1	1	1t    raw_requestc                 L  K   t          |           }t                      }|>t          j        t                     t          t          j        j        t                    	 |
                    |           t          j        d| d           t          t          j        j        d| dt          j        | i          S # t           $ rY}t          j        dt%          |                      t          t          j        j        dt%          |                     d}~ww xY w)aA  Close an existing session and clean up its resources.

    Args:
        raw_request: FastAPI Request object containing session ID in headers

    Returns:
        Response with 200 status and closed session ID in headers

    Raises:
        HTTPException: If session closure fails with 424 FAILED_DEPENDENCY status
    Nstatus_codedetailSession z closedr   contentheaderszFailed to close session: )r   r   r
   errorr   r   r   BAD_REQUESTvaluer   r   infor   OKr   CLOSED_SESSION_ID	Exception	exceptionstrFAILED_DEPENDENCY)r   
session_idsession_manageres       r   r   r   #   s;      -[99J)++O1222".40
 
 
 	

%%j1112z222333"+2z222+=*O
 
 
 	

  
 
 
=SVV==>>>"4:7s1vv77
 
 
 	

s   !AC   
D#
ADD#c                   K   t                      }|>t          j        t                     t	          t
          j        j        t                    	 |	                                }|j
        J t          j        |j
        t          j                                      d          }t          j        d|j         d           t%          t
          j        j        d|j         dt(          j        |j         d| i          S # t,          $ rY}t          j        d	t1          |                      t	          t
          j        j        d	t1          |                     d}~ww xY w)
aO  Create a new stateful session with expiration tracking.

    Args:
        raw_request: FastAPI Request object (unused but part of handler signature)

    Returns:
        Response with 200 status, session ID and expiration in headers

    Raises:
        HTTPException: If session creation fails with 424 FAILED_DEPENDENCY status
    Nr   )tzz%Y-%m-%dT%H:%M:%SZr    z createdz
; Expires=r!   zFailed to create session: )r   r
   r$   r   r   r   r%   r&   r   r   expiration_tsr   fromtimestampr   utcstrftimer'   r.   r   r(   r   NEW_SESSION_IDr*   r+   r,   r-   )r   r/   sessionr3   r0   s        r   r   r   G   s      *++O1222".40
 
 
 	

!0022$000 .!hl
 
 

('
(
( 	 	;w1;;;<<<"+;w1;;;&5':L7g7gXe7g7g
 
 
 	
  
 
 
>c!ff>>???"4:8A88
 
 
 	

s   B1D 
E'AE""E'N)r   r   httpr   fastapir   r   fastapi.exceptionsr   logging_configr
   managerr   modelsr   r   r   r   utilsr   r   r   r    r   r   <module>rA      s*   ' ' ' ' ' ' ' '       % % % % % % % % , , , , , , $ $ $ $ $ $ ( ( ( ( ( (            / . . . . ./A    "!
W !
 !
 !
 !
H'
g '
 '
 '
 '
 '
 '
r   