
    wis                         d Z ddlmZmZ e G d d                      Ze G d d                      Z G d d          Zd	S )
uB   会话上下文管理：LLM 对话历史 + Shell Session 绑定。    )	dataclassfieldc                       e Zd ZU dZeed<   eed<   dZeed<   dZeed<    e	e
          Ze
e         ed	<   ed
efd            ZdS )SessionContextu6   会话上下文：LLM 历史 + Shell Session 绑定。
session_iduser_id   seq namedefault_factoryllm_messagesreturnc                     | j         p| j        S )u+   显示名称：自定义名 > session_id。)r   r   selfs    5/root/projects/qq-shell/src/butler/session/context.pydisplay_namezSessionContext.display_name   s     y+DO+    N)__name__
__module____qualname____doc__str__annotations__r
   intr   r   listr   dictpropertyr    r   r   r   r      s         @@OOOLLLCLLLD#NNN$uT:::L$t*:::,c , , , X, , ,r   r   c                       e Zd ZU dZeed<    ee          Zeee	f         ed<   dZ
eed<   dZeed<   ed	e	d
z  fd            Zd
S )UserSessionsu   用户的所有会话。r   r   sessionsr   active_session_idr	   	_next_seqr   Nc                 @    | j                             | j                  S )u   当前活跃会话。)r$   getr%   r   s    r   activezUserSessions.active   s     }  !7888r   )r   r   r   r   r   r   r   r   r$   r   r%   r&   r   r    r)   r!   r   r   r#   r#      s         ""LLL*/%*E*E*EHd3&'EEEsIs9- 9 9 9 X9 9 9r   r#   c            	          e Zd ZdZddZdedefdZdededefdZ	deded	e
defd
Zdededz  fdZdedededdfdZdededz  fdZdedee         fdZdedefdZdedededz  fdZdededededz  fdZdededefdZdS )ContextManageru'   管理所有用户的会话上下文。r   Nc                     i | _         d S )N)_usersr   s    r   __init__zContextManager.__init__(   s    /1r   r   c                 ^    || j         vrt          |          | j         |<   | j         |         S )u$   获取或创建用户会话集合。)r   )r-   r#   )r   r   s     r   _get_or_create_userz"ContextManager._get_or_create_user+   s3    $+%%#/#@#@#@DK {7##r   r   c                     |                      |          }|j        r|j        S t          |||j                  }||j        |<   ||_        |xj        dz  c_        |S )u-   获取活跃会话，或创建默认会话。r   r   r
   r	   )r0   r)   r   r&   r$   r%   )r   r   r   userctxs        r   get_or_createzContextManager.get_or_create1   sq    ''00; 	;!
 
 

 %(j!!+!
r   r
   c                     |                      |          }t          |||          }||j        |<   ||_        t	          |j        |dz             |_        |S )u   新建会话上下文。r2   r	   )r0   r   r$   r%   maxr&   )r   r   r   r
   r3   r4   s         r   new_sessionzContextManager.new_sessionA   sd    ''00!
 
 

 %(j!!+T^S1W55
r   c                 r    | j                             |          }||j        dS g |j        _        |j        S )u)   清理活跃会话的 LLM 对话历史。N)r-   r(   r)   r   r   r   r3   s      r   clear_contextzContextManager.clear_contextN   s8    {w''<4;.4#% {r   rolecontentc                     | j                             |          }||j        dS |j        j                            ||d           dS )u/   追加一条 LLM 对话记录到活跃会话。N)r<   r=   )r-   r(   r)   r   append)r   r   r<   r=   r3   s        r   add_messagezContextManager.add_messageV   sM    {w''<4;.F '''(J(JKKKKKr   c                 L    | j                             |          }|dS |j        S )u   获取活跃会话上下文。N)r-   r(   r)   r:   s      r   r(   zContextManager.get]   s'    {w''<4{r   c                     | j                             |          }|g S t          |j                                                  S )u   列出用户的所有会话。)r-   r(   r   r$   valuesr:   s      r   list_sessionszContextManager.list_sessionsd   s;    {w''<IDM((**+++r   c                 L    | j                             |          }|dS |j        S )u   获取活跃会话 ID。Nr   )r-   r(   r%   r:   s      r   get_active_session_idz$ContextManager.get_active_session_idk   s(    {w''<2%%r   c                 l    | j                             |          }|	||j        vrdS ||_        |j        S )u   切换到指定会话。N)r-   r(   r$   r%   r)   r   r   r   r3   s       r   switch_sessionzContextManager.switch_sessionr   s;    {w''<:T]::4!+{r   new_namec                     | j                             |          }|	||j        vrdS ||j        |         _        |j        |         S )u   重命名会话。N)r-   r(   r$   r   )r   r   r   rJ   r3   s        r   rename_sessionzContextManager.rename_sessionz   sG    {w''<:T]::4)1j!&}Z((r   c                     | j                             |          }|	||j        vrdS t          |j                  dk    rdS |j        |= |j        |k    r&t          t          |j                            |_        dS )u6   删除会话。不能删除最后一个活跃会话。NFr	   T)r-   r(   r$   lenr%   nextiterrH   s       r   delete_sessionzContextManager.delete_session   s|    {w''<:T]::5t}""5M*%!Z//%)$t}*=*=%>%>D"tr   )r   N)r   r   r   r   r.   r   r#   r0   r   r5   r   r8   r;   r@   r(   r   rD   rF   rI   rL   boolrQ   r!   r   r   r+   r+   %   s       112 2 2 2$3 $< $ $ $ $S c n     3 C c n    S ^d-B    L3 Lc LC LD L L L L3 >D#8    ,S ,T.-A , , , ,&S &S & & & &c s ~PT?T    )c )s )c )n_cNc ) ) ) )
c 
s 
t 
 
 
 
 
 
r   r+   N)r   dataclassesr   r   r   r#   r+   r!   r   r   <module>rT      s    H H ( ( ( ( ( ( ( ( , , , , , , , , 9 9 9 9 9 9 9 9g g g g g g g g g gr   