From 44f451bd7d72ded499b661d373de7e05becdbcd5 Mon Sep 17 00:00:00 2001 From: Harry Date: Tue, 11 Nov 2025 16:06:16 +0800 Subject: [PATCH] refactor(api): improve graph validation logic in WorkflowService - Updated the validate_graph_structure method to handle empty graph cases gracefully. - Introduced a variable for workflow_id to ensure consistent handling of unknown workflow IDs. - Enhanced code readability and maintainability by refining the method's structure. --- api/services/workflow_service.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/api/services/workflow_service.py b/api/services/workflow_service.py index 8743945409..b6d64d95da 100644 --- a/api/services/workflow_service.py +++ b/api/services/workflow_service.py @@ -905,14 +905,21 @@ class WorkflowService: return new_app - def validate_graph_structure(self, user_id: str, app_model: App, graph: Mapping[str, Any]) -> None: + def validate_graph_structure(self, user_id: str, app_model: App, graph: Mapping[str, Any]): """ Validate workflow graph structure by instantiating the Graph object. This leverages the built-in graph validators (including trigger/UserInput exclusivity) and raises any structural errors before persisting the workflow. """ + node_configs = graph.get("nodes", []) + node_configs = cast(list[dict[str, object]], node_configs) + # is empty graph + if not node_configs: + return + + workflow_id = app_model.workflow_id or "UNKNOWN" Graph.init( graph_config=graph, # TODO(Mairuis): Add root node id @@ -921,7 +928,7 @@ class WorkflowService: graph_init_params=GraphInitParams( tenant_id=app_model.tenant_id, app_id=app_model.id, - workflow_id=app_model.workflow_id, + workflow_id=workflow_id, graph_config=graph, user_id=user_id, user_from=UserFrom.ACCOUNT,