base_node.py 3.9 KB

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