This commit is contained in:
parent
b0323d256a
commit
e80c19a1c5
|
|
@ -42,9 +42,6 @@ class Conversation(BaseModel):
|
|||
|
||||
id: str = Field(default_factory=lambda: str(uuid7()), description="会话唯一标识")
|
||||
description: str = Field(default="新会话", description="会话描述")
|
||||
is_actived: bool = Field(
|
||||
default=False, description="已激活,True 表示激活,False 表示不激活"
|
||||
)
|
||||
is_running: bool = Field(
|
||||
default=False, description="正在运行,True 表示正在运行,False 表示未正在运行"
|
||||
)
|
||||
|
|
|
|||
|
|
@ -65,7 +65,7 @@ class ConversationState(rx.State):
|
|||
|
||||
# 当前用户唯一标识
|
||||
user_id: str = ""
|
||||
# 会话列表
|
||||
# 键为会话唯一标识,值为会话实例的会话字典
|
||||
conversations: Dict[str, Conversation] = {} # 按照会话唯一标识顺序排序
|
||||
# 当前会话唯一标识
|
||||
conversation_id: str = ""
|
||||
|
|
@ -87,24 +87,17 @@ class ConversationState(rx.State):
|
|||
return
|
||||
|
||||
database_state = await self.get_state(DatabaseState)
|
||||
# 获取会话列表
|
||||
# 获取会话字典
|
||||
self.conversations = await database_state.get_conversations(
|
||||
user_id=self.user_id
|
||||
)
|
||||
# 若会话列表为空则先创建会话记录再添加会话实例
|
||||
# 若会话字典为空则先创建会话记录再在会话字典中添加会话实例
|
||||
if not self.conversations:
|
||||
self.conversations.append(
|
||||
self.conversations.update(
|
||||
await database_state.create_conversations_record(user_id=self.user_id)
|
||||
)
|
||||
# 将最后一个会话作为当前会话
|
||||
self.idx = -1
|
||||
self.conversation = self.conversations[self.idx]
|
||||
# 将当前会话设置为已激活
|
||||
self.conversation.is_actived = True
|
||||
# 获取对话列表
|
||||
self.conversation.dialogs = await database_state.get_dialogs(
|
||||
conversation_id=self.conversation.id
|
||||
)
|
||||
# 将最后一个会话作为当前会话并更新会话唯一标识
|
||||
self.conversation_id = next(reversed(self.conversations))
|
||||
|
||||
@rx.event
|
||||
def toggle_conversation_history_shown(self) -> None:
|
||||
|
|
@ -136,8 +129,10 @@ class ConversationState(rx.State):
|
|||
获取会话历史项列表,用于前端渲染会话历史
|
||||
:return: 会话历史项列表
|
||||
"""
|
||||
items = []
|
||||
for conversation in reversed(self.conversations): # 按照创建顺序倒序排序
|
||||
items: List[ConversationHistoryItem] = []
|
||||
for conversation in reversed(
|
||||
self.conversations.values()
|
||||
): # 按照会话唯一标识倒序排序
|
||||
items.append(
|
||||
ConversationHistoryItem(
|
||||
id=conversation.id,
|
||||
|
|
@ -161,31 +156,28 @@ class ConversationState(rx.State):
|
|||
|
||||
# 创建会话记录再添加会话实例
|
||||
database_state = await self.get_state(DatabaseState)
|
||||
self.conversations.append(
|
||||
self.conversations.update(
|
||||
await database_state.create_conversations_record(
|
||||
user_id=self.user_id, description=description
|
||||
)
|
||||
)
|
||||
# 将当前会话设置为未激活
|
||||
self.conversation.is_actived = False
|
||||
# 将最后一个会话作为当前会话并设置为已激活
|
||||
self.conversation = self.conversations[-1]
|
||||
self.conversation.is_actived = True
|
||||
# 将最后一个会话作为当前会话并更新会话唯一标识
|
||||
self.conversation_id = next(reversed(self.conversations))
|
||||
|
||||
# 会话创建状态设置为未正在创建
|
||||
self.is_conversation_creating = False
|
||||
|
||||
@rx.event
|
||||
async def delete_conversation(self, idx: int) -> None:
|
||||
async def delete_conversation(self, conversation_id: str) -> None:
|
||||
"""
|
||||
删除指定索引的会话
|
||||
:param idx: 指定索引
|
||||
删除指定会话
|
||||
:param conversation_id: 指定会话唯一标识
|
||||
:return: None
|
||||
"""
|
||||
# 先逻辑删除会话记录再在会话列表删除会话实例
|
||||
database_state = await self.get_state(DatabaseState)
|
||||
await database_state.delete_conversations_record(
|
||||
conversation_id=self.conversations[idx].id
|
||||
conversation_id=conversation_id
|
||||
)
|
||||
del self.conversations[idx]
|
||||
|
||||
|
|
|
|||
|
|
@ -202,9 +202,9 @@ class DatabaseState(rx.State):
|
|||
|
||||
async def get_conversations(self, user_id: str) -> Dict[str, Conversation]:
|
||||
"""
|
||||
获取会话列表
|
||||
获取会话字典
|
||||
:param user_id: 用户唯一标识
|
||||
:return: 会话列表
|
||||
:return: 会话字典
|
||||
"""
|
||||
records: Dict[str, Conversation] = {}
|
||||
async with rx.asession() as session:
|
||||
|
|
@ -242,27 +242,29 @@ class DatabaseState(rx.State):
|
|||
|
||||
async def create_conversations_record(
|
||||
self, user_id: str, description: str = "新会话"
|
||||
) -> Conversation:
|
||||
) -> Dict[str, Conversation]:
|
||||
"""
|
||||
创建会话记录
|
||||
:param user_id: 用户唯一标识
|
||||
:param description: 会话描述,默认为"新会话"
|
||||
:return: 会话实例
|
||||
:return: 键为会话唯一标识,值为会话实例的会话字典
|
||||
"""
|
||||
async with rx.asession() as session:
|
||||
record = ConversationRecord(user_id=user_id, description=description)
|
||||
session.add(record)
|
||||
await session.commit()
|
||||
await session.refresh(record)
|
||||
return Conversation(
|
||||
id=record.id,
|
||||
description=record.description,
|
||||
created_at=record.created_at,
|
||||
)
|
||||
return {
|
||||
record.id: Conversation(
|
||||
id=record.id,
|
||||
description=record.description,
|
||||
created_at=record.created_at,
|
||||
)
|
||||
}
|
||||
|
||||
async def delete_conversations_record(self, conversation_id: str) -> None:
|
||||
"""
|
||||
逻辑删除会话记录
|
||||
逻辑删除会话记录(将会话已删除设置为 True)
|
||||
:param conversation_id: 指定会话唯一标识
|
||||
:return: None
|
||||
"""
|
||||
|
|
|
|||
Loading…
Reference in New Issue