diff --git a/astrbot/core/conversation_mgr.py b/astrbot/core/conversation_mgr.py index b0f5c136d..b665488e4 100644 --- a/astrbot/core/conversation_mgr.py +++ b/astrbot/core/conversation_mgr.py @@ -88,7 +88,10 @@ class ConversationManager: return self.session_conversations.get(unified_msg_origin, None) async def get_conversation( - self, unified_msg_origin: str, conversation_id: str + self, + unified_msg_origin: str, + conversation_id: str, + create_if_not_exists: bool = False, ) -> Conversation: """获取会话的对话 @@ -98,6 +101,13 @@ class ConversationManager: Returns: conversation (Conversation): 对话对象 """ + conv = self.db.get_conversation_by_user_id(unified_msg_origin, conversation_id) + if not conv and create_if_not_exists: + # 如果对话不存在且需要创建,则新建一个对话 + conversation_id = await self.new_conversation(unified_msg_origin) + return self.db.get_conversation_by_user_id( + unified_msg_origin, conversation_id + ) return self.db.get_conversation_by_user_id(unified_msg_origin, conversation_id) async def get_conversations(self, unified_msg_origin: str) -> List[Conversation]: diff --git a/packages/astrbot/main.py b/packages/astrbot/main.py index 0790326c1..0a3f8ba6c 100644 --- a/packages/astrbot/main.py +++ b/packages/astrbot/main.py @@ -655,25 +655,16 @@ UID: {user_id} 此 ID 可用于设置管理员。 return size_per_page = 6 - session_curr_cid = ( - await self.context.conversation_manager.get_curr_conversation_id( - message.unified_msg_origin - ) - ) + + conv_mgr = self.context.conversation_manager + umo = message.unified_msg_origin + session_curr_cid = await conv_mgr.get_curr_conversation_id(umo) if not session_curr_cid: - message.set_result( - MessageEventResult().message( - "当前未处于对话状态,请 /switch 序号 切换或者 /new 创建。" - ) - ) - return + session_curr_cid = await conv_mgr.new_conversation(umo) - ( - contexts, - total_pages, - ) = await self.context.conversation_manager.get_human_readable_context( - message.unified_msg_origin, session_curr_cid, page, size_per_page + contexts, total_pages = await conv_mgr.get_human_readable_context( + umo, session_curr_cid, page, size_per_page ) history = "" @@ -682,12 +673,12 @@ UID: {user_id} 此 ID 可用于设置管理员。 context = context[:150] + "..." history += f"{context}\n" - ret = f"""当前对话历史记录: -{history} -第 {page} 页 | 共 {total_pages} 页 - -*输入 /history 2 跳转到第 2 页 -""" + ret = ( + f"当前对话历史记录:" + f"{history if history else '无历史记录'}\n\n" + f"第 {page} 页 | 共 {total_pages} 页\n" + f"*输入 /history 2 跳转到第 2 页" + ) message.set_result(MessageEventResult().message(ret).use_t2i(False)) @@ -1022,14 +1013,10 @@ UID: {user_id} 此 ID 可用于设置管理员。 curr_cid_title = "无" if cid: conversation = await self.context.conversation_manager.get_conversation( - message.unified_msg_origin, cid + unified_msg_origin=message.unified_msg_origin, + conversation_id=cid, + create_if_not_exists=True, ) - if not conversation: - message.set_result( - MessageEventResult().message( - "请先进入一个对话。可以使用 /new 创建。" - ) - ) if not conversation.persona_id and not conversation.persona_id == "[%None]": curr_persona_name = ( self.context.provider_manager.selected_default_persona["name"]