perf: history 和 persona 指令当对话不存在的时候自动创建

fixes: #1997
This commit is contained in:
Soulter
2025-07-03 15:39:42 +08:00
parent f1954f9a43
commit 14e0aa3ec5
2 changed files with 27 additions and 30 deletions
+11 -1
View File
@@ -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]:
+16 -29
View File
@@ -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"]