refactor: make save_memory and get_memory_by_spec work on latest version

This commit is contained in:
Stream 2025-09-15 19:28:22 +08:00
parent e3903f34e4
commit 3d761a3189
No known key found for this signature in database
GPG Key ID: 033728094B100D70

View File

@ -71,23 +71,22 @@ class ChatflowMemoryService:
app_id=memory.app_id,
node_id=memory.node_id,
conversation_id=memory.conversation_id
).first()
if existing:
existing.value = memory.value
else:
session.add(
ChatflowMemoryVariable(
memory_id=memory.spec.id,
tenant_id=memory.tenant_id,
app_id=memory.app_id,
node_id=memory.node_id,
conversation_id=memory.conversation_id,
name=memory.spec.name,
value=memory.value,
term=memory.spec.term,
scope=memory.spec.scope,
)
).order_by(ChatflowMemoryVariable.version.desc()).first()
new_version = 1 if not existing else existing.version + 1
session.add(
ChatflowMemoryVariable(
memory_id=memory.spec.id,
tenant_id=memory.tenant_id,
app_id=memory.app_id,
node_id=memory.node_id,
conversation_id=memory.conversation_id,
name=memory.spec.name,
value=memory.value,
term=memory.spec.term,
scope=memory.spec.scope,
version=new_version,
)
)
session.commit()
if is_draft:
@ -163,7 +162,7 @@ class ChatflowMemoryService:
ChatflowMemoryVariable.conversation_id == \
(conversation_id if spec.term == MemoryTerm.SESSION else None),
)
)
).order_by(ChatflowMemoryVariable.version.desc()).limit(1)
result = session.execute(stmt).scalar()
if result:
return MemoryBlock(