feat: expose version to MemoryBlock

This commit is contained in:
Stream 2025-09-23 23:09:45 +08:00
parent 3d7d4182a6
commit 5bf642c3f9
No known key found for this signature in database
GPG Key ID: 033728094B100D70
4 changed files with 59 additions and 26 deletions

View File

@ -61,20 +61,32 @@ class MemoryEditApi(Resource):
memory_spec = next((it for it in workflow.memory_blocks if it.id == args['id']), None)
if not memory_spec:
return {'error': 'Memory not found'}, 404
ChatflowMemoryService.save_memory(
MemoryBlock(
spec=memory_spec,
tenant_id=app_model.tenant_id,
value=update,
conversation_id=conversation_id,
node_id=node_id,
app_id=app_model.id,
edited_by_user=True,
created_by=MemoryCreatedBy(end_user_id=end_user.id),
),
variable_pool=VariablePool(),
# First get existing memory
existing_memory = ChatflowMemoryService.get_memory_by_spec(
spec=memory_spec,
tenant_id=app_model.tenant_id,
app_id=app_model.id,
created_by=MemoryCreatedBy(end_user_id=end_user.id),
conversation_id=conversation_id,
node_id=node_id,
is_draft=False
)
# Create updated memory instance
updated_memory = MemoryBlock(
spec=existing_memory.spec,
tenant_id=existing_memory.tenant_id,
app_id=existing_memory.app_id,
conversation_id=existing_memory.conversation_id,
node_id=existing_memory.node_id,
value=update, # New value
version=existing_memory.version, # Keep current version (save_memory will handle version increment)
edited_by_user=True,
created_by=existing_memory.created_by,
)
ChatflowMemoryService.save_memory(updated_memory, VariablePool(), False)
return '', 204

View File

@ -61,20 +61,32 @@ class MemoryEditApi(WebApiResource):
return {'error': 'Memory not found'}, 404
if not memory_spec.end_user_editable:
return {'error': 'Memory not editable'}, 403
ChatflowMemoryService.save_memory(
MemoryBlock(
spec=memory_spec,
tenant_id=app_model.tenant_id,
value=update,
conversation_id=conversation_id,
node_id=node_id,
app_id=app_model.id,
edited_by_user=True,
created_by=MemoryCreatedBy(end_user_id=end_user.id)
),
variable_pool=VariablePool(),
# First get existing memory
existing_memory = ChatflowMemoryService.get_memory_by_spec(
spec=memory_spec,
tenant_id=app_model.tenant_id,
app_id=app_model.id,
created_by=MemoryCreatedBy(end_user_id=end_user.id),
conversation_id=conversation_id,
node_id=node_id,
is_draft=False
)
# Create updated memory instance
updated_memory = MemoryBlock(
spec=existing_memory.spec,
tenant_id=existing_memory.tenant_id,
app_id=existing_memory.app_id,
conversation_id=existing_memory.conversation_id,
node_id=existing_memory.node_id,
value=update, # New value
version=existing_memory.version, # Keep current version (save_memory will handle version increment)
edited_by_user=True,
created_by=existing_memory.created_by,
)
ChatflowMemoryService.save_memory(updated_memory, VariablePool(), False)
return '', 204

View File

@ -1,10 +1,13 @@
from __future__ import annotations
from enum import StrEnum
from typing import Optional
from typing import TYPE_CHECKING, Optional
from uuid import uuid4
from pydantic import BaseModel, Field
from core.app.app_config.entities import ModelConfig
if TYPE_CHECKING:
from core.app.app_config.entities import ModelConfig
class MemoryScope(StrEnum):
@ -75,6 +78,7 @@ class MemoryBlock(BaseModel):
node_id: Optional[str] = None
edited_by_user: bool = False
created_by: MemoryCreatedBy
version: int = Field(description="Memory block version number")
class MemoryValueData(BaseModel):

View File

@ -210,6 +210,7 @@ class ChatflowMemoryService:
node_id=node_id,
spec=spec,
created_by=created_by,
version=1,
)
stmt = select(ChatflowMemoryVariable).where(
and_(
@ -234,6 +235,7 @@ class ChatflowMemoryService:
spec=spec,
edited_by_user=memory_value_data.edited_by_user,
created_by=created_by,
version=result.version,
)
return MemoryBlock(
tenant_id=tenant_id,
@ -243,6 +245,7 @@ class ChatflowMemoryService:
node_id=node_id,
spec=spec,
created_by=created_by,
version=1,
)
@staticmethod
@ -412,6 +415,7 @@ class ChatflowMemoryService:
node_id=chatflow_memory_variable.node_id,
edited_by_user=memory_value_data.edited_by_user,
created_by=created_by,
version=chatflow_memory_variable.version,
)
)
return results
@ -551,6 +555,7 @@ class ChatflowMemoryService:
node_id=memory_block.node_id,
edited_by_user=False,
created_by=memory_block.created_by,
version=memory_block.version,
)
ChatflowMemoryService.save_memory(updated_memory, variable_pool, is_draft)