Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.
Python agentagentic-aiagentic-frameworkagentic-workflowaiautomationclaudedeepseekgenaigptllmlow-codemcpnextjsno-codeopenaipythonskillsworkflow
refactor(api): inject code executor from node factory (#32618)
-LAN-700a4029c6af
diff --git a/api/.importlinter b/api/.importlinterindex ee9b8464a4b..cd674dbf955 100644--- a/api/.importlinter+++ b/api/.importlinter@@ -110,7 +110,6 @@ ignore_imports =core.workflow.nodes.agent.agent_node -> core.model_managercore.workflow.nodes.agent.agent_node -> core.provider_managercore.workflow.nodes.agent.agent_node -> core.tools.tool_manager- core.workflow.nodes.code.code_node -> core.helper.code_executor.code_executorcore.workflow.nodes.datasource.datasource_node -> models.modelcore.workflow.nodes.datasource.datasource_node -> models.toolscore.workflow.nodes.datasource.datasource_node -> services.datasource_provider_servicediff --git a/api/core/app/workflow/node_factory.py b/api/core/app/workflow/node_factory.pyindex bc4470cd504..965f3ddb1d5 100644--- a/api/core/app/workflow/node_factory.py+++ b/api/core/app/workflow/node_factory.py@@ -1,9 +1,10 @@-from typing import TYPE_CHECKING, final+from collections.abc import Mapping+from typing import TYPE_CHECKING, Any, finalfrom typing_extensions import overridefrom configs import dify_config-from core.helper.code_executor.code_executor import CodeExecutor+from core.helper.code_executor.code_executor import CodeExecutionError, CodeExecutorfrom core.helper.code_executor.code_node_provider import CodeNodeProviderfrom core.helper.ssrf_proxy import ssrf_proxyfrom core.rag.retrieval.dataset_retrieval import DatasetRetrieval@@ -13,7 +14,8 @@ from core.workflow.enums import NodeTypefrom core.workflow.file.file_manager import file_managerfrom core.workflow.graph.graph import NodeFactoryfrom core.workflow.nodes.base.node import Node-from core.workflow.nodes.code.code_node import CodeNode+from core.workflow.nodes.code.code_node import CodeNode, WorkflowCodeExecutor+from core.workflow.nodes.code.entities import CodeLanguagefrom core.workflow.nodes.code.limits import CodeNodeLimitsfrom core.workflow.nodes.document_extractor import DocumentExtractorNode, UnstructuredApiConfigfrom core.workflow.nodes.http_request import HttpRequestNode, build_http_request_config@@ -27,6 +29,24 @@ if TYPE_CHECKING:from core.workflow.runtime import GraphRuntimeState+class DefaultWorkflowCodeExecutor:+ def execute(+ self,+ *,+ language: CodeLanguage,+ code: str,+ inputs: Mapping[str, Any],+ ) -> Mapping[str, Any]:+ return CodeExecutor.execute_workflow_code_template(+ language=language,+ code=code,+ inputs=inputs,+ )++ def is_execution_error(self, error: Exception) -> bool:+ return isinstance(error, CodeExecutionError)++@finalclass DifyNodeFactory(NodeFactory):"""@@ -43,7 +63,7 @@ class DifyNodeFactory(NodeFactory):) -> None:self.graph_init_params = graph_init_paramsself.graph_runtime_state = graph_runtime_state- self._code_executor: type[CodeExecutor] = CodeExecutor+ self._code_executor: WorkflowCodeExecutor = DefaultWorkflowCodeExecutor()self._code_providers: tuple[type[CodeNodeProvider], ...] = CodeNode.default_code_providers()self._code_limits = CodeNodeLimits(max_string_length=dify_config.CODE_MAX_STRING_LENGTH,diff --git a/api/core/workflow/nodes/code/code_node.py b/api/core/workflow/nodes/code/code_node.pyindex e3035d3bf07..f7a6c41f0af 100644--- a/api/core/workflow/nodes/code/code_node.py+++ b/api/core/workflow/nodes/code/code_node.py@@ -1,8 +1,7 @@from collections.abc import Mapping, Sequencefrom decimal import Decimal-from typing import TYPE_CHECKING, Any, ClassVar, cast+from typing import TYPE_CHECKING, Any, ClassVar, Protocol, cast-from core.helper.code_executor.code_executor import CodeExecutionError, CodeExecutor, CodeLanguagefrom core.helper.code_executor.code_node_provider import CodeNodeProviderfrom core.helper.code_executor.javascript.javascript_code_provider import JavascriptCodeProviderfrom core.helper.code_executor.python3.python3_code_provider import Python3CodeProvider@@ -11,7 +10,7 @@ from core.variables.types import SegmentTypefrom core.workflow.enums import NodeType, WorkflowNodeExecutionStatusfrom core.workflow.node_events import NodeRunResultfrom core.workflow.nodes.base.node import Node-from core.workflow.nodes.code.entities import CodeNodeData+from core.workflow.nodes.code.entities import CodeLanguage, CodeNodeDatafrom core.workflow.nodes.code.limits import CodeNodeLimitsfrom .exc import (@@ -25,6 +24,18 @@ if TYPE_CHECKING:from core.workflow.runtime import GraphRuntimeState+class WorkflowCodeExecutor(Protocol):+ def execute(+ self,+ *,+ language: CodeLanguage,+ code: str,+ inputs: Mapping[str, Any],+ ) -> Mapping[str, Any]: ...++ def is_execution_error(self, error: Exception) -> bool: ...++class CodeNode(Node[CodeNodeData]):node_type = NodeType.CODE_DEFAULT_CODE_PROVIDERS: ClassVar[tuple[type[CodeNodeProvider], ...]] = (@@ -40,7 +51,7 @@ class CodeNode(Node[CodeNodeData]):graph_init_params: "GraphInitParams",graph_runtime_state: "GraphRuntimeState",*,- code_executor: type[CodeExecutor] | None = None,+ code_executor: WorkflowCodeExecutor,code_providers: Sequence[type[CodeNodeProvider]] | None = None,code_limits: CodeNodeLimits,) -> None:@@ -50,7 +61,7 @@ class CodeNode(Node[CodeNodeData]):graph_init_params=graph_init_params,graph_runtime_state=graph_runtime_state,)- self._code_executor: type[CodeExecutor] = code_executor or CodeExecutor+ self._code_executor: WorkflowCodeExecutor = code_executorself._code_providers: tuple[type[CodeNodeProvider], ...] = (tuple(code_providers) if code_providers else self._DEFAULT_CODE_PROVIDERS)@@ -98,7 +109,7 @@ class CodeNode(Node[CodeNodeData]):# Run codetry:_ = self._select_code_provider(code_language)- result = self._code_executor.execute_workflow_code_template(+ result = self._code_executor.execute(language=code_language,code=code,inputs=variables,@@ -106,7 +117,13 @@ class CodeNode(Node[CodeNodeData]):# Transform resultresult = self._transform_result(result=result, output_schema=self.node_data.outputs)- except (CodeExecutionError, CodeNodeError) as e:+ except CodeNodeError as e:+ return NodeRunResult(+ status=WorkflowNodeExecutionStatus.FAILED, inputs=variables, error=str(e), error_type=type(e).__name__+ )+ except Exception as e:+ if not self._code_executor.is_execution_error(e):+ raisereturn NodeRunResult(status=WorkflowNodeExecutionStatus.FAILED, inputs=variables, error=str(e), error_type=type(e).__name__)diff --git a/api/tests/integration_tests/workflow/nodes/test_code.py b/api/tests/integration_tests/workflow/nodes/test_code.pyindex 1a9d69b2d22..e0ea14b7899 100644--- a/api/tests/integration_tests/workflow/nodes/test_code.py+++ b/api/tests/integration_tests/workflow/nodes/test_code.py@@ -68,6 +68,7 @@ def init_code_node(code_config: dict):config=code_config,graph_init_params=init_params,graph_runtime_state=graph_runtime_state,+ code_executor=node_factory._code_executor,code_limits=CodeNodeLimits(max_string_length=dify_config.CODE_MAX_STRING_LENGTH,max_number=dify_config.CODE_MAX_NUMBER,diff --git a/api/tests/unit_tests/core/workflow/graph_engine/test_mock_nodes_template_code.py b/api/tests/unit_tests/core/workflow/graph_engine/test_mock_nodes_template_code.pyindex de08cc34974..e760d7b3d30 100644--- a/api/tests/unit_tests/core/workflow/graph_engine/test_mock_nodes_template_code.py+++ b/api/tests/unit_tests/core/workflow/graph_engine/test_mock_nodes_template_code.py@@ -24,6 +24,16 @@ DEFAULT_CODE_LIMITS = CodeNodeLimits()+class _NoopCodeExecutor:+ def execute(self, *, language: object, code: str, inputs: dict[str, object]) -> dict[str, object]:+ _ = (language, code, inputs)+ return {}++ def is_execution_error(self, error: Exception) -> bool:+ _ = error+ return False++class TestMockTemplateTransformNode:"""Test cases for MockTemplateTransformNode."""@@ -319,6 +329,7 @@ class TestMockCodeNode:graph_init_params=graph_init_params,graph_runtime_state=graph_runtime_state,mock_config=mock_config,+ code_executor=_NoopCodeExecutor(),code_limits=DEFAULT_CODE_LIMITS,)@@ -384,6 +395,7 @@ class TestMockCodeNode:graph_init_params=graph_init_params,graph_runtime_state=graph_runtime_state,mock_config=mock_config,+ code_executor=_NoopCodeExecutor(),code_limits=DEFAULT_CODE_LIMITS,)@@ -453,6 +465,7 @@ class TestMockCodeNode:graph_init_params=graph_init_params,graph_runtime_state=graph_runtime_state,mock_config=mock_config,+ code_executor=_NoopCodeExecutor(),code_limits=DEFAULT_CODE_LIMITS,)