
    ki                        d Z ddlmZ ddlZddlmZ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  ee          Z G d
 de          Z G d d          Z G d d          ZddZdS )z>Persistent session-store adapters for research and QA history.    )annotationsN)datetime	timedelta)AnyProtocol)settings)MySQLClient)RedisClient)
get_loggerc                      e Zd ZddZddZdS )ResearchSessionStore
session_idstruser_idreturnlist[dict[str, Any]]c                
   K   d S N )selfr   r   s      :D:\work\zm-rag\backend\app\infrastructure\session_store.pyload_sessionz!ResearchSessionStore.load_session   s          questionanswerrefsNonec                
   K   d S r   r   )r   r   r   r   r   r   s         r   save_sessionz!ResearchSessionStore.save_session   s       	r   Nr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )__name__
__module____qualname__r   r   r   r   r   r   r      s<                r   r   c                  >    e Zd ZddZddZddZedd            ZdS )RedisResearchSessionStoreredis_clientr
   ttl_secondsintr   r   c               "    || _         || _        d S r   )_redis_ttl_seconds)r   r'   r(   s      r   __init__z"RedisResearchSessionStore.__init__!       "'r   r   r   r   r   c                   K   |                      ||          }| j        j                            |           d {V }|sg S t	          j        |          S r   )_keyr+   rawgetjsonloads)r   r   r   keyr1   s        r   r   z&RedisResearchSessionStore.load_session%   s]      ii
G,,KO'',,,,,,,, 	Iz#r   r   r   r   c                b  K   |                      ||          }| j        j                            |           d {V }|rt	          j        |          ng }|                    |||d           | j        j                            || j        t	          j	        |d                     d {V  d S )Nr   r   
referencesFensure_ascii)
r0   r+   r1   r2   r3   r4   appendsetexr,   dumps)	r   r   r   r   r   r   r5   r1   historys	            r   r   z&RedisResearchSessionStore.save_session,   s       ii
G,,KO'',,,,,,,,;>(F
3B!V4HH	
 	
 	
 ko##JwU333
 
 	
 	
 	
 	
 	
 	
 	
 	
 	
r   c                    d| d|  S )Nzresearch:session::r   )r   r   s     r   r0   zRedisResearchSessionStore._key@   s    9799Z999r   N)r'   r
   r(   r)   r   r   r    r!   )r   r   r   r   r   r   )r"   r#   r$   r-   r   r   staticmethodr0   r   r   r   r&   r&       sp        ( ( ( (   
 
 
 
( : : : \: : :r   r&   c                  &    e Zd ZddZddZddZdS )MySQLResearchSessionStoremysql_clientr	   r(   r)   r   r   c               "    || _         || _        d S r   )_mysqlr,   )r   rD   r(   s      r   r-   z"MySQLResearchSessionStore.__init__F   r.   r   r   r   r   r   c                x  K   | j                             d||f           d {V }|sg S |                    d          }t          |t                    rt          j        |          S t          |t          t          f          r't          j        |	                    d                    S t          |pg           S )Nz
            SELECT history
            FROM research_sessions
            WHERE session_id = %s
              AND user_id = %s
              AND (expires_at IS NULL OR expires_at > UTC_TIMESTAMP())
            LIMIT 1
            r>   zutf-8)rF   	fetch_oner2   
isinstancer   r3   r4   bytes	bytearraydecodelist)r   r   r   rowr>   s        r   r   z&MySQLResearchSessionStore.load_sessionJ   s      K)) !

 

 

 

 

 

 

 

  	I'')$$gs## 	':g&&&gy122 	7:gnnW55666GMr"""r   r   r   r   c                <  K   |                      ||           d {V }|                    |||d           t          j        |d          }t	          j                    t          | j                  z   }| j        	                    d||||f           d {V  d S )Nr7   Fr9   )secondsaI  
            INSERT INTO research_sessions (session_id, user_id, history, expires_at)
            VALUES (%s, %s, %s, %s) AS new_values
            ON DUPLICATE KEY UPDATE
                history = new_values.history,
                expires_at = new_values.expires_at,
                updated_at = CURRENT_TIMESTAMP
            )
r   r;   r3   r=   r   utcnowr   r,   rF   execute)	r   r   r   r   r   r   r>   payload
expires_ats	            r   r   z&MySQLResearchSessionStore.save_session`   s       ))*g>>>>>>>>!V4HH	
 	
 	
 *W5999_&&4;L)M)M)MM
k!! ':6

 

 
	
 
	
 
	
 
	
 
	
 
	
 
	
 
	
 
	
r   N)rD   r	   r(   r)   r   r   r    r!   )r"   r#   r$   r-   r   r   r   r   r   rC   rC   E   sP        ( ( ( (# # # #,
 
 
 
 
 
r   rC   r'   r
   rD   MySQLClient | Noner   c                    t           j                                        dk    r7|t          |t           j                  S t
                              d           t          | t           j                  S )zBBuild the configured session store, with Redis fallback if needed.mysqlN)r(   1mysql_session_store_unavailable_fallback_to_redis)r   research_session_backendlowerrC   research_session_ttl_secondsloggerwarningr&   )r'   rD   s     r   build_research_session_storer^   {   sy     (..00G;;#,$A    	JKKK$9   r   )r'   r
   rD   rU   r   r   )__doc__
__future__r   r3   r   r   typingr   r   
app.configr   app.infrastructure.mysql_clientr	   app.infrastructure.redis_clientr
   app.utils.loggerr   r"   r\   r   r&   rC   r^   r   r   r   <module>rf      sG   D D " " " " " "  ( ( ( ( ( ( ( (                       7 7 7 7 7 7 7 7 7 7 7 7 ' ' ' ' ' '	H		    8   ": ": ": ": ": ": ": ":J3
 3
 3
 3
 3
 3
 3
 3
l     r   