feat(workflow): simplify node converter registry

This commit is contained in:
Eternity
2026-03-09 03:08:44 +08:00
parent 8f789d47a2
commit 966bd8528d
2 changed files with 30 additions and 32 deletions

View File

@@ -45,7 +45,8 @@ from app.core.workflow.nodes.enums import (
AssignmentOperator, AssignmentOperator,
HttpAuthType, HttpAuthType,
HttpContentType, HttpContentType,
HttpErrorHandle HttpErrorHandle,
NodeType
) )
from app.core.workflow.nodes.http_request.config import ( from app.core.workflow.nodes.http_request.config import (
HttpAuthConfig, HttpAuthConfig,
@@ -73,25 +74,24 @@ class DifyConverter(BaseConverter):
def __init__(self): def __init__(self):
self.CONFIG_CONVERT_MAP = { self.CONFIG_CONVERT_MAP = {
"start": self.convert_start_node_config, NodeType.START: self.convert_start_node_config,
"llm": self.convert_llm_node_config, NodeType.LLM: self.convert_llm_node_config,
"answer": self.convert_end_node_config, NodeType.END: self.convert_end_node_config,
"if-else": self.convert_if_else_node_config, NodeType.IF_ELSE: self.convert_if_else_node_config,
"loop": self.convert_loop_node_config, NodeType.LOOP: self.convert_loop_node_config,
"iteration": self.convert_iteration_node_config, NodeType.ITERATION: self.convert_iteration_node_config,
"assigner": self.convert_assigner_node_config, NodeType.ASSIGNER: self.convert_assigner_node_config,
"code": self.convert_code_node_config, NodeType.CODE: self.convert_code_node_config,
"http-request": self.convert_http_node_config, NodeType.HTTP_REQUEST: self.convert_http_node_config,
"template-transform": self.convert_jinja_render_node_config, NodeType.JINJARENDER: self.convert_jinja_render_node_config,
"knowledge-retrieval": self.convert_knowledge_node_config, NodeType.KNOWLEDGE_RETRIEVAL: self.convert_knowledge_node_config,
"parameter-extractor": self.convert_parameter_extractor_node_config, NodeType.PARAMETER_EXTRACTOR: self.convert_parameter_extractor_node_config,
"question-classifier": self.convert_question_classifier_node_config, NodeType.QUESTION_CLASSIFIER: self.convert_question_classifier_node_config,
"variable-aggregator": self.convert_variable_aggregator_node_config, NodeType.VAR_AGGREGATOR: self.convert_variable_aggregator_node_config,
"tool": self.convert_tool_node_config, NodeType.TOOL: self.convert_tool_node_config,
"": self.convert_notes_config, NodeType.NOTES: self.convert_notes_config,
"loop-start": lambda x: {}, NodeType.CYCLE_START: lambda x: {},
"iteration-start": lambda x: {}, NodeType.BREAK: lambda x: {},
"loop-end": lambda x: {},
} }
def get_node_convert(self, node_type): def get_node_convert(self, node_type):

View File

@@ -50,7 +50,7 @@ class DifyAdapter(BasePlatformAdapter, DifyConverter):
def __init__(self, config: dict[str, Any]): def __init__(self, config: dict[str, Any]):
DifyConverter.__init__(self) DifyConverter.__init__(self)
BasePlatformAdapter.__init__(self, config) BasePlatformAdapter.__init__(self, config)
def get_metadata(self) -> PlatformMetadata: def get_metadata(self) -> PlatformMetadata:
return PlatformMetadata( return PlatformMetadata(
@@ -84,7 +84,7 @@ class DifyAdapter(BasePlatformAdapter, DifyConverter):
require_fields = frozenset({'app', 'kind', 'version', 'workflow'}) require_fields = frozenset({'app', 'kind', 'version', 'workflow'})
if not all(field in self.config for field in require_fields): if not all(field in self.config for field in require_fields):
return False return False
if self.config.get("app",{}).get("mode") == "workflow": if self.config.get("app", {}).get("mode") == "workflow":
self.errors.append(ExceptionDefineition( self.errors.append(ExceptionDefineition(
type=ExceptionType.PLATFORM, type=ExceptionType.PLATFORM,
detail="workflow mode is not supported" detail="workflow mode is not supported"
@@ -163,13 +163,14 @@ class DifyAdapter(BasePlatformAdapter, DifyConverter):
def _convert_node(self, node: dict[str, Any]) -> NodeDefinition | None: def _convert_node(self, node: dict[str, Any]) -> NodeDefinition | None:
node_data = node["data"] node_data = node["data"]
try: try:
node_type = self.map_node_type(node_data["type"])
return NodeDefinition( return NodeDefinition(
id=node["id"], id=node["id"],
type=self.map_node_type(node_data["type"]), type=node_type,
name=node_data.get("title") or "notes", name=node_data.get("title") or "notes",
cycle=node.get("parentId"), cycle=node.get("parentId"),
description=None, description=None,
config=self._convert_node_config(node), config=self._convert_node_config(node_type, node),
position={ position={
"x": node["position"]["x"], "x": node["position"]["x"],
"y": node["position"]["y"] "y": node["position"]["y"]
@@ -183,17 +184,16 @@ class DifyAdapter(BasePlatformAdapter, DifyConverter):
except Exception as e: except Exception as e:
logger.debug(f"convert node error - {e}", exc_info=True) logger.debug(f"convert node error - {e}", exc_info=True)
def _convert_node_config(self, node: dict): def _convert_node_config(self, node_type: str, node: dict):
node_data = node["data"]
node_type = node_data["type"]
try: try:
node_data = node["data"]
converter = self.get_node_convert(node_type) converter = self.get_node_convert(node_type)
if node_type not in self.CONFIG_CONVERT_MAP: if node_type == NodeType.UNKNOWN:
self.errors.append(ExceptionDefineition( self.errors.append(ExceptionDefineition(
type=ExceptionType.NODE, type=ExceptionType.NODE,
node_id=node["id"], node_id=node["id"],
node_name=node["data"]["title"], node_name=node["data"]["title"],
detail=f"node type {node_type} is unsupported", detail=f"node type {node_data.get('type')} is unsupported",
)) ))
return converter(node) return converter(node)
except Exception as e: except Exception as e:
@@ -214,7 +214,7 @@ class DifyAdapter(BasePlatformAdapter, DifyConverter):
if source in self.branch_node_cache: if source in self.branch_node_cache:
case_id = edge["sourceHandle"] case_id = edge["sourceHandle"]
if case_id == "false": if case_id == "false":
label = f'CASE{len(self.branch_node_cache[source])+1}' label = f'CASE{len(self.branch_node_cache[source]) + 1}'
else: else:
label = f'CASE{self.branch_node_cache[source].index(case_id) + 1}' label = f'CASE{self.branch_node_cache[source].index(case_id) + 1}'
if source in self.error_branch_node_cache: if source in self.error_branch_node_cache:
@@ -257,5 +257,3 @@ class DifyAdapter(BasePlatformAdapter, DifyConverter):
def _convert_execution(self, execution: dict[str, Any]) -> ExecutionConfig: def _convert_execution(self, execution: dict[str, Any]) -> ExecutionConfig:
return ExecutionConfig() return ExecutionConfig()