feat(workflow): simplify node converter registry
This commit is contained in:
@@ -45,7 +45,8 @@ from app.core.workflow.nodes.enums import (
|
||||
AssignmentOperator,
|
||||
HttpAuthType,
|
||||
HttpContentType,
|
||||
HttpErrorHandle
|
||||
HttpErrorHandle,
|
||||
NodeType
|
||||
)
|
||||
from app.core.workflow.nodes.http_request.config import (
|
||||
HttpAuthConfig,
|
||||
@@ -73,25 +74,24 @@ class DifyConverter(BaseConverter):
|
||||
|
||||
def __init__(self):
|
||||
self.CONFIG_CONVERT_MAP = {
|
||||
"start": self.convert_start_node_config,
|
||||
"llm": self.convert_llm_node_config,
|
||||
"answer": self.convert_end_node_config,
|
||||
"if-else": self.convert_if_else_node_config,
|
||||
"loop": self.convert_loop_node_config,
|
||||
"iteration": self.convert_iteration_node_config,
|
||||
"assigner": self.convert_assigner_node_config,
|
||||
"code": self.convert_code_node_config,
|
||||
"http-request": self.convert_http_node_config,
|
||||
"template-transform": self.convert_jinja_render_node_config,
|
||||
"knowledge-retrieval": self.convert_knowledge_node_config,
|
||||
"parameter-extractor": self.convert_parameter_extractor_node_config,
|
||||
"question-classifier": self.convert_question_classifier_node_config,
|
||||
"variable-aggregator": self.convert_variable_aggregator_node_config,
|
||||
"tool": self.convert_tool_node_config,
|
||||
"": self.convert_notes_config,
|
||||
"loop-start": lambda x: {},
|
||||
"iteration-start": lambda x: {},
|
||||
"loop-end": lambda x: {},
|
||||
NodeType.START: self.convert_start_node_config,
|
||||
NodeType.LLM: self.convert_llm_node_config,
|
||||
NodeType.END: self.convert_end_node_config,
|
||||
NodeType.IF_ELSE: self.convert_if_else_node_config,
|
||||
NodeType.LOOP: self.convert_loop_node_config,
|
||||
NodeType.ITERATION: self.convert_iteration_node_config,
|
||||
NodeType.ASSIGNER: self.convert_assigner_node_config,
|
||||
NodeType.CODE: self.convert_code_node_config,
|
||||
NodeType.HTTP_REQUEST: self.convert_http_node_config,
|
||||
NodeType.JINJARENDER: self.convert_jinja_render_node_config,
|
||||
NodeType.KNOWLEDGE_RETRIEVAL: self.convert_knowledge_node_config,
|
||||
NodeType.PARAMETER_EXTRACTOR: self.convert_parameter_extractor_node_config,
|
||||
NodeType.QUESTION_CLASSIFIER: self.convert_question_classifier_node_config,
|
||||
NodeType.VAR_AGGREGATOR: self.convert_variable_aggregator_node_config,
|
||||
NodeType.TOOL: self.convert_tool_node_config,
|
||||
NodeType.NOTES: self.convert_notes_config,
|
||||
NodeType.CYCLE_START: lambda x: {},
|
||||
NodeType.BREAK: lambda x: {},
|
||||
}
|
||||
|
||||
def get_node_convert(self, node_type):
|
||||
|
||||
@@ -50,7 +50,7 @@ class DifyAdapter(BasePlatformAdapter, DifyConverter):
|
||||
|
||||
def __init__(self, config: dict[str, Any]):
|
||||
DifyConverter.__init__(self)
|
||||
BasePlatformAdapter.__init__(self, config)
|
||||
BasePlatformAdapter.__init__(self, config)
|
||||
|
||||
def get_metadata(self) -> PlatformMetadata:
|
||||
return PlatformMetadata(
|
||||
@@ -84,7 +84,7 @@ class DifyAdapter(BasePlatformAdapter, DifyConverter):
|
||||
require_fields = frozenset({'app', 'kind', 'version', 'workflow'})
|
||||
if not all(field in self.config for field in require_fields):
|
||||
return False
|
||||
if self.config.get("app",{}).get("mode") == "workflow":
|
||||
if self.config.get("app", {}).get("mode") == "workflow":
|
||||
self.errors.append(ExceptionDefineition(
|
||||
type=ExceptionType.PLATFORM,
|
||||
detail="workflow mode is not supported"
|
||||
@@ -163,13 +163,14 @@ class DifyAdapter(BasePlatformAdapter, DifyConverter):
|
||||
def _convert_node(self, node: dict[str, Any]) -> NodeDefinition | None:
|
||||
node_data = node["data"]
|
||||
try:
|
||||
node_type = self.map_node_type(node_data["type"])
|
||||
return NodeDefinition(
|
||||
id=node["id"],
|
||||
type=self.map_node_type(node_data["type"]),
|
||||
type=node_type,
|
||||
name=node_data.get("title") or "notes",
|
||||
cycle=node.get("parentId"),
|
||||
description=None,
|
||||
config=self._convert_node_config(node),
|
||||
config=self._convert_node_config(node_type, node),
|
||||
position={
|
||||
"x": node["position"]["x"],
|
||||
"y": node["position"]["y"]
|
||||
@@ -183,17 +184,16 @@ class DifyAdapter(BasePlatformAdapter, DifyConverter):
|
||||
except Exception as e:
|
||||
logger.debug(f"convert node error - {e}", exc_info=True)
|
||||
|
||||
def _convert_node_config(self, node: dict):
|
||||
node_data = node["data"]
|
||||
node_type = node_data["type"]
|
||||
def _convert_node_config(self, node_type: str, node: dict):
|
||||
try:
|
||||
node_data = node["data"]
|
||||
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(
|
||||
type=ExceptionType.NODE,
|
||||
node_id=node["id"],
|
||||
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)
|
||||
except Exception as e:
|
||||
@@ -214,7 +214,7 @@ class DifyAdapter(BasePlatformAdapter, DifyConverter):
|
||||
if source in self.branch_node_cache:
|
||||
case_id = edge["sourceHandle"]
|
||||
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:
|
||||
label = f'CASE{self.branch_node_cache[source].index(case_id) + 1}'
|
||||
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:
|
||||
return ExecutionConfig()
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user