diff --git a/agent/application/states/__init__.py b/agent/application/states/__init__.py index 24e4ca3..cb0d944 100644 --- a/agent/application/states/__init__.py +++ b/agent/application/states/__init__.py @@ -1,17 +1,11 @@ # -*- coding: utf-8 -*- from application.states.auth import AuthState from application.states.conversation import ConversationState -from application.states.conversation_historycollapse import ConversationHistoryCollapseState -from application.states.create_conversation import CreateConversationState from application.states.database import DatabaseState -from application.states.sidebar import SidebarState __all__ = [ "AuthState", - "ConversationHistoryCollapseState", "ConversationState", - "CreateConversationState", "DatabaseState", - "SidebarState", ] diff --git a/agent/application/states/auth.py b/agent/application/states/auth.py index 7e32691..d7c4162 100644 --- a/agent/application/states/auth.py +++ b/agent/application/states/auth.py @@ -127,3 +127,24 @@ class AuthState(rx.State): self.code = "" self.agree_policy = False return rx.toast("登录成功", color="green") + + @rx.var + async def storage_key(self) -> str: + """ + LocalStorage 键 + """ + # 获取认证状态 + auth = await self.get_state(AuthState) + # 获取用户唯一标识 + user_id = auth.user_id or "default" + return f"sidebar_{user_id}" + + # 激活的导航按钮 + activated_nav_button: str = rx.LocalStorage(storage_key, sync=True) + + @rx.event + def switch_nav_button(self, button: str): + """ + 将指定导航按钮设置为激活的导航按钮 + """ + self.activated_nav_button = button diff --git a/agent/application/states/conversation.py b/agent/application/states/conversation.py index 62913c9..3263fae 100644 --- a/agent/application/states/conversation.py +++ b/agent/application/states/conversation.py @@ -26,7 +26,6 @@ import reflex as rx from application.models import Conversation, Dialog, ThoughtNode from application.states.auth import AuthState -from application.states.create_conversation import CreateConversationState from application.states.database import DatabaseState @@ -68,15 +67,22 @@ class ConversationState(rx.State): # 用户唯一标识 user_id: str = "" + # 会话字典 conversations: Dict[str, Conversation] = {} - # 当前会话唯一标识 + # 会话唯一标识 conversation_id: str = "" + # 会话历史展示状态,True表示展示,False表示隐藏 + is_conversation_history_shown: bool = False + + # 正在新建会话状态,True表示正在新建,False表示未正在新建 + is_creating_conversation: bool = False + @rx.event async def on_mount(self): """ - 页面挂载时初始化会话列表和当前会话唯一标识 + 页面挂载时初始化会话列表和会话唯一标识 """ # 获取登录用户唯一标识 auth_state = await self.get_state(AuthState) @@ -120,7 +126,7 @@ class ConversationState(rx.State): # 获取会话描述 description = form_data["description"].strip() or "新会话" - # 先在数据库新建会话,再在会话字典新建会话 + # 先在数据库中创建会话记录,再在会话字典中创建会话记录 database_state = await self.get_state(DatabaseState) conversation_id, created_at = await database_state.create_conversation( user_id=self.user_id, description=description @@ -128,11 +134,10 @@ class ConversationState(rx.State): self.conversations[conversation_id] = Conversation( description=description, created_at=created_at ) + # 将新建会话的唯一标识设置为当前会话唯一标识 self.conversation_id = conversation_id - - # 关闭新建会话模态窗 - create_conversation_state = await self.get_state(CreateConversationState) - create_conversation_state.is_modal_open = False + # 将正在新建会话状态设置为已完成 + self.is_creating_conversation = False @rx.event async def delete_conversation(self, conversation_id: str) -> None: @@ -402,3 +407,18 @@ class ConversationState(rx.State): # 指定运行 dialog = self.conversations[self.conversation_id].dialogs[dialog_id] dialog.is_collapse_expanded = not dialog.is_collapse_expanded + + @rx.event + def toggle_conversation_history_shown(self) -> None: + """ + 切换会话历史展示状态 + :return: None + """ + self.is_conversation_history_shown = not self.is_conversation_history_shown + + @rx.event + def toggle_creating_conversation(self) -> None: + """ + 切换正在新建会话状态 + """ + self.is_creating_conversation = not self.is_creating_conversation diff --git a/agent/application/states/conversation_historycollapse.py b/agent/application/states/conversation_historycollapse.py deleted file mode 100644 index dbd889c..0000000 --- a/agent/application/states/conversation_historycollapse.py +++ /dev/null @@ -1,22 +0,0 @@ -# -*- coding: utf-8 -*- -""" -会话历史折叠面板状态 -""" -import reflex as rx - - -class ConversationHistoryCollapseState(rx.State): - """ - 会话历史折叠面板状态 - """ - - # 展开状态,True表示展开,False表示折叠 - is_expanded: bool = False - - @rx.event - def toggle_expanded(self) -> None: - """ - 切换展开状态 - :return: None - """ - self.is_expanded = not self.is_expanded diff --git a/agent/application/states/create_conversation.py b/agent/application/states/create_conversation.py deleted file mode 100644 index 26351dd..0000000 --- a/agent/application/states/create_conversation.py +++ /dev/null @@ -1,21 +0,0 @@ -# -*- coding: utf-8 -*- -""" -新建会话状态 -""" -import reflex as rx - - -class CreateConversationState(rx.State): - """ - 新建会话状态 - """ - - # 新建会话模态窗打开状态,True表示打开,False表示关闭 - is_modal_open: bool = False - - @rx.event - def toggle_modal(self) -> None: - """ - 打开/关闭新建会话模态窗 - """ - self.is_modal_open = not self.is_modal_open diff --git a/agent/application/states/sidebar.py b/agent/application/states/sidebar.py deleted file mode 100644 index b6052e8..0000000 --- a/agent/application/states/sidebar.py +++ /dev/null @@ -1,34 +0,0 @@ -# -*- coding: utf-8 -*- -""" -侧边栏状态 -""" -import reflex as rx - -from application.states.auth import AuthState - - -class SidebarState(rx.State): - """ - 侧边栏状态 - """ - - @rx.var - async def storage_key(self) -> str: - """ - LocalStorage 键 - """ - # 获取认证状态 - auth = await self.get_state(AuthState) - # 获取用户唯一标识 - user_id = auth.user_id or "default" - return f"sidebar_{user_id}" - - # 激活的导航按钮 - activated_nav_button: str = rx.LocalStorage(storage_key, sync=True) - - @rx.event - def switch_nav_button(self, button: str): - """ - 将指定导航按钮设置为激活的导航按钮 - """ - self.activated_nav_button = button