fix: chatflow message visibility from index

This commit is contained in:
Stream 2025-09-28 21:20:37 +08:00
parent 8833fee232
commit 15be85514d
No known key found for this signature in database
GPG Key ID: 033728094B100D70
3 changed files with 21 additions and 9 deletions

View File

@ -67,7 +67,10 @@ class ChatflowMessage(Base):
id: Mapped[str] = mapped_column(StringUUID, primary_key=True, server_default=sa.text("uuid_generate_v4()"))
conversation_id: Mapped[str] = mapped_column(StringUUID, nullable=False)
index: Mapped[int] = mapped_column(sa.Integer, nullable=False)
index: Mapped[int] = mapped_column(sa.Integer, nullable=False) # This index starts from 0
version: Mapped[int] = mapped_column(sa.Integer, nullable=False)
data: Mapped[str] = mapped_column(sa.Text, nullable=False) # Serialized PromptMessage JSON
created_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, server_default=func.current_timestamp())
updated_at: Mapped[datetime] = mapped_column(
DateTime, nullable=False, server_default=func.current_timestamp(), onupdate=func.current_timestamp()
)

View File

@ -68,21 +68,21 @@ class ChatflowHistoryService:
next_index = max_index + 1
# Save new message to append-only table
message_data = {
'role': prompt_message.role.value,
'content': prompt_message.get_text_content(),
'timestamp': time.time()
}
new_message = ChatflowMessage(
conversation_id=chatflow_conv.id,
index=next_index,
version=1,
data=json.dumps(message_data)
data=json.dumps(prompt_message)
)
session.add(new_message)
session.commit()
# 添加每次保存消息后简单增长visible_count
current_metadata = ChatflowConversationMetadata.model_validate_json(chatflow_conv.conversation_metadata)
new_visible_count = current_metadata.visible_count + 1
new_metadata = ChatflowConversationMetadata(visible_count=new_visible_count)
chatflow_conv.conversation_metadata = new_metadata.model_dump_json()
@staticmethod
def save_app_message(
prompt_message: PromptMessage,
@ -209,7 +209,7 @@ class ChatflowHistoryService:
else:
if create_if_missing:
# Create a new chatflow conversation
default_metadata = ChatflowConversationMetadata(visible_count=20)
default_metadata = ChatflowConversationMetadata(visible_count=0)
new_chatflow_conv = ChatflowConversation(
tenant_id=tenant_id,
app_id=app_id,

View File

@ -549,6 +549,15 @@ class ChatflowMemoryService:
)
ChatflowMemoryService.save_memory(updated_memory, variable_pool, is_draft)
# 添加以下代码:重置 visible_count 为 preserved_turns
ChatflowHistoryService.update_visible_count(
conversation_id=memory_block.conversation_id,
node_id=memory_block.node_id,
new_visible_count=memory_block.spec.preserved_turns,
app_id=memory_block.app_id,
tenant_id=memory_block.tenant_id
)
@staticmethod
def delete_memory(app: App, memory_id: str, created_by: MemoryCreatedBy):
workflow = WorkflowService().get_published_workflow(app)