From 54b272206ec3c54fb680cb22ec2607b2fcd47765 Mon Sep 17 00:00:00 2001 From: Stream Date: Tue, 16 Sep 2025 18:32:58 +0800 Subject: [PATCH] refactor: add version param to get_session_memories and get_persistent_memories --- api/services/chatflow_memory_service.py | 58 +++++++++++++++++++------ 1 file changed, 44 insertions(+), 14 deletions(-) diff --git a/api/services/chatflow_memory_service.py b/api/services/chatflow_memory_service.py index 3bb600ccc9..7b12c2266d 100644 --- a/api/services/chatflow_memory_service.py +++ b/api/services/chatflow_memory_service.py @@ -34,27 +34,56 @@ logger = logging.getLogger(__name__) class ChatflowMemoryService: @staticmethod - def get_persistent_memories(app: App) -> Sequence[MemoryBlockWithVisibility]: - stmt = select(ChatflowMemoryVariable).where( - and_( - ChatflowMemoryVariable.tenant_id == app.tenant_id, - ChatflowMemoryVariable.app_id == app.id, - ChatflowMemoryVariable.conversation_id == None + def get_persistent_memories( + app: App, + version: int | None = None + ) -> Sequence[MemoryBlockWithVisibility]: + if version is None: + # If version not specified, get the latest version + stmt = select(ChatflowMemoryVariable).distinct(ChatflowMemoryVariable.memory_id).where( + and_( + ChatflowMemoryVariable.tenant_id == app.tenant_id, + ChatflowMemoryVariable.app_id == app.id, + ChatflowMemoryVariable.conversation_id == None + ) + ).order_by(ChatflowMemoryVariable.version.desc()) + else: + stmt = select(ChatflowMemoryVariable).where( + and_( + ChatflowMemoryVariable.tenant_id == app.tenant_id, + ChatflowMemoryVariable.app_id == app.id, + ChatflowMemoryVariable.conversation_id == None, + ChatflowMemoryVariable.version == version + ) ) - ) with Session(db.engine) as session: db_results = session.execute(stmt).all() return ChatflowMemoryService._with_visibility(app, [result[0] for result in db_results]) @staticmethod - def get_session_memories(app: App, conversation_id: str) -> Sequence[MemoryBlockWithVisibility]: - stmt = select(ChatflowMemoryVariable).where( - and_( - ChatflowMemoryVariable.tenant_id == app.tenant_id, - ChatflowMemoryVariable.app_id == app.id, - ChatflowMemoryVariable.conversation_id == conversation_id + def get_session_memories( + app: App, + conversation_id: str, + version: int | None = None + ) -> Sequence[MemoryBlockWithVisibility]: + if version is None: + # If version not specified, get the latest version + stmt = select(ChatflowMemoryVariable).distinct(ChatflowMemoryVariable.memory_id).where( + and_( + ChatflowMemoryVariable.tenant_id == app.tenant_id, + ChatflowMemoryVariable.app_id == app.id, + ChatflowMemoryVariable.conversation_id == conversation_id + ) + ).order_by(ChatflowMemoryVariable.version.desc()) + else: + stmt = select(ChatflowMemoryVariable).where( + and_( + ChatflowMemoryVariable.tenant_id == app.tenant_id, + ChatflowMemoryVariable.app_id == app.id, + ChatflowMemoryVariable.conversation_id == conversation_id, + ChatflowMemoryVariable.version == version + ) ) - ) with Session(db.engine) as session: db_results = session.execute(stmt).all() return ChatflowMemoryService._with_visibility(app, [result[0] for result in db_results]) @@ -340,6 +369,7 @@ class ChatflowMemoryService: value=chatflow_memory_variable.value, end_user_editable=spec.end_user_editable, end_user_visible=spec.end_user_visible, + version=chatflow_memory_variable.version ) ) return results