feat: fetch memory block from WorkflowDraftVariable when debugging single node

This commit is contained in:
Stream 2025-08-21 15:17:25 +08:00
parent 4b085d46f6
commit 1fa8b26e55
No known key found for this signature in database
GPG Key ID: 9475891C9507B4F3

View File

@ -12,6 +12,7 @@ from core.app.app_config.entities import VariableEntityType
from core.app.apps.advanced_chat.app_config_manager import AdvancedChatAppConfigManager from core.app.apps.advanced_chat.app_config_manager import AdvancedChatAppConfigManager
from core.app.apps.workflow.app_config_manager import WorkflowAppConfigManager from core.app.apps.workflow.app_config_manager import WorkflowAppConfigManager
from core.file import File from core.file import File
from core.memory.entities import MemoryScope
from core.repositories import DifyCoreRepositoryFactory from core.repositories import DifyCoreRepositoryFactory
from core.variables import Variable from core.variables import Variable
from core.variables.variables import VariableUnion from core.variables.variables import VariableUnion
@ -45,6 +46,7 @@ from models.workflow import (
from repositories.factory import DifyAPIRepositoryFactory from repositories.factory import DifyAPIRepositoryFactory
from services.errors.app import IsDraftWorkflowError, WorkflowHashNotEqualError from services.errors.app import IsDraftWorkflowError, WorkflowHashNotEqualError
from services.workflow.workflow_converter import WorkflowConverter from services.workflow.workflow_converter import WorkflowConverter
from .chatflow_memory_service import ChatflowMemoryService
from .errors.workflow_service import DraftWorkflowDeletionError, WorkflowInUseError from .errors.workflow_service import DraftWorkflowDeletionError, WorkflowInUseError
from .workflow_draft_variable_service import ( from .workflow_draft_variable_service import (
@ -361,17 +363,10 @@ class WorkflowService:
tenant_id=draft_workflow.tenant_id, start_node_data=start_data, user_inputs=user_inputs tenant_id=draft_workflow.tenant_id, start_node_data=start_data, user_inputs=user_inputs
) )
# init variable pool # init variable pool
variable_pool = _setup_variable_pool( variable_pool = _setup_variable_pool(query=query, files=files or [], user_id=account.id,
query=query, user_inputs=user_inputs, workflow=draft_workflow,
files=files or [], node_type=node_type, conversation_id=conversation_id,
user_id=account.id, conversation_variables=[], is_draft=True)
user_inputs=user_inputs,
workflow=draft_workflow,
# NOTE(QuantumGhost): We rely on `DraftVarLoader` to load conversation variables.
conversation_variables=[],
node_type=node_type,
conversation_id=conversation_id,
)
else: else:
variable_pool = VariablePool( variable_pool = VariablePool(
@ -688,6 +683,7 @@ def _setup_variable_pool(
node_type: NodeType, node_type: NodeType,
conversation_id: str, conversation_id: str,
conversation_variables: list[Variable], conversation_variables: list[Variable],
is_draft: bool
): ):
# Only inject system variables for START node type. # Only inject system variables for START node type.
if node_type == NodeType.START: if node_type == NodeType.START:
@ -715,6 +711,7 @@ def _setup_variable_pool(
# Based on the definition of `VariableUnion`, # Based on the definition of `VariableUnion`,
# `list[Variable]` can be safely used as `list[VariableUnion]` since they are compatible. # `list[Variable]` can be safely used as `list[VariableUnion]` since they are compatible.
conversation_variables=cast(list[VariableUnion], conversation_variables), # conversation_variables=cast(list[VariableUnion], conversation_variables), #
memory_blocks=_fetch_memory_blocks(workflow, conversation_id, is_draft=is_draft),
) )
return variable_pool return variable_pool
@ -751,3 +748,21 @@ def _rebuild_single_file(tenant_id: str, value: Any, variable_entity_type: Varia
return build_from_mappings(mappings=value, tenant_id=tenant_id) return build_from_mappings(mappings=value, tenant_id=tenant_id)
else: else:
raise Exception("unreachable") raise Exception("unreachable")
def _fetch_memory_blocks(workflow: Workflow, conversation_id: str, is_draft: bool) -> Mapping[str, str]:
memory_blocks = {}
memory_block_specs = workflow.memory_blocks
memories = ChatflowMemoryService.get_memories_by_specs(
memory_block_specs=memory_block_specs,
tenant_id=workflow.tenant_id,
app_id=workflow.app_id,
conversation_id=conversation_id,
is_draft=is_draft,
)
for memory in memories:
if memory.scope == MemoryScope.APP:
memory_blocks[memory.memory_id] = memory.value
else: # NODE scope
memory_blocks[f"{memory.node_id}.{memory.memory_id}"] = memory.value
return memory_blocks