node.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import logging
  2. from abc import abstractmethod
  3. from collections.abc import Generator, Mapping, Sequence
  4. from typing import TYPE_CHECKING, Any, Generic, Optional, TypeVar, Union, cast
  5. from core.workflow.entities.node_entities import NodeRunResult
  6. from core.workflow.nodes.enums import NodeType
  7. from core.workflow.nodes.event import NodeEvent, RunCompletedEvent
  8. from models.workflow import WorkflowNodeExecutionStatus
  9. from .entities import BaseNodeData
  10. if TYPE_CHECKING:
  11. from core.workflow.graph_engine.entities.event import InNodeEvent
  12. from core.workflow.graph_engine.entities.graph import Graph
  13. from core.workflow.graph_engine.entities.graph_init_params import GraphInitParams
  14. from core.workflow.graph_engine.entities.graph_runtime_state import GraphRuntimeState
  15. logger = logging.getLogger(__name__)
  16. GenericNodeData = TypeVar("GenericNodeData", bound=BaseNodeData)
  17. class BaseNode(Generic[GenericNodeData]):
  18. _node_data_cls: type[BaseNodeData]
  19. _node_type: NodeType
  20. def __init__(
  21. self,
  22. id: str,
  23. config: Mapping[str, Any],
  24. graph_init_params: "GraphInitParams",
  25. graph: "Graph",
  26. graph_runtime_state: "GraphRuntimeState",
  27. previous_node_id: Optional[str] = None,
  28. thread_pool_id: Optional[str] = None,
  29. ) -> None:
  30. self.id = id
  31. self.tenant_id = graph_init_params.tenant_id
  32. self.app_id = graph_init_params.app_id
  33. self.workflow_type = graph_init_params.workflow_type
  34. self.workflow_id = graph_init_params.workflow_id
  35. self.graph_config = graph_init_params.graph_config
  36. self.user_id = graph_init_params.user_id
  37. self.user_from = graph_init_params.user_from
  38. self.invoke_from = graph_init_params.invoke_from
  39. self.workflow_call_depth = graph_init_params.call_depth
  40. self.graph = graph
  41. self.graph_runtime_state = graph_runtime_state
  42. self.previous_node_id = previous_node_id
  43. self.thread_pool_id = thread_pool_id
  44. node_id = config.get("id")
  45. if not node_id:
  46. raise ValueError("Node ID is required.")
  47. self.node_id = node_id
  48. node_data = self._node_data_cls.model_validate(config.get("data", {}))
  49. self.node_data = cast(GenericNodeData, node_data)
  50. @abstractmethod
  51. def _run(self) -> NodeRunResult | Generator[Union[NodeEvent, "InNodeEvent"], None, None]:
  52. """
  53. Run node
  54. :return:
  55. """
  56. raise NotImplementedError
  57. def run(self) -> Generator[Union[NodeEvent, "InNodeEvent"], None, None]:
  58. try:
  59. result = self._run()
  60. except Exception as e:
  61. logger.exception(f"Node {self.node_id} failed to run")
  62. result = NodeRunResult(
  63. status=WorkflowNodeExecutionStatus.FAILED,
  64. error=str(e),
  65. )
  66. if isinstance(result, NodeRunResult):
  67. yield RunCompletedEvent(run_result=result)
  68. else:
  69. yield from result
  70. @classmethod
  71. def extract_variable_selector_to_variable_mapping(
  72. cls,
  73. *,
  74. graph_config: Mapping[str, Any],
  75. config: Mapping[str, Any],
  76. ) -> Mapping[str, Sequence[str]]:
  77. """
  78. Extract variable selector to variable mapping
  79. :param graph_config: graph config
  80. :param config: node config
  81. :return:
  82. """
  83. node_id = config.get("id")
  84. if not node_id:
  85. raise ValueError("Node ID is required when extracting variable selector to variable mapping.")
  86. node_data = cls._node_data_cls(**config.get("data", {}))
  87. return cls._extract_variable_selector_to_variable_mapping(
  88. graph_config=graph_config, node_id=node_id, node_data=cast(GenericNodeData, node_data)
  89. )
  90. @classmethod
  91. def _extract_variable_selector_to_variable_mapping(
  92. cls,
  93. *,
  94. graph_config: Mapping[str, Any],
  95. node_id: str,
  96. node_data: GenericNodeData,
  97. ) -> Mapping[str, Sequence[str]]:
  98. """
  99. Extract variable selector to variable mapping
  100. :param graph_config: graph config
  101. :param node_id: node id
  102. :param node_data: node data
  103. :return:
  104. """
  105. return {}
  106. @classmethod
  107. def get_default_config(cls, filters: Optional[dict] = None) -> dict:
  108. """
  109. Get default config of node.
  110. :param filters: filter by node config parameters.
  111. :return:
  112. """
  113. return {}
  114. @property
  115. def node_type(self) -> NodeType:
  116. """
  117. Get node type
  118. :return:
  119. """
  120. return self._node_type