@@ -49,6 +49,7 @@ class BaseGraph:
4949 def __init__ (self , nodes : list , edges : list , entry_point : str , use_burr : bool = False , burr_config : dict = None ):
5050
5151 self .nodes = nodes
52+ self .raw_edges = edges
5253 self .edges = self ._create_edges ({e for e in edges })
5354 self .entry_point = entry_point .node_name
5455 self .initial_state = {}
@@ -168,4 +169,25 @@ def execute(self, initial_state: dict) -> Tuple[dict, list]:
168169 result = bridge .execute (initial_state )
169170 return (result ["_state" ], [])
170171 else :
171- return self ._execute_standard (initial_state )
172+ return self ._execute_standard (initial_state )
173+
174+ def append_node (self , node ):
175+ """
176+ Adds a node to the graph.
177+
178+ Args:
179+ node (BaseNode): The node instance to add to the graph.
180+ """
181+
182+ # if node name already exists in the graph, raise an exception
183+ if node .node_name in {n .node_name for n in self .nodes }:
184+ raise ValueError (f"Node with name '{ node .node_name } ' already exists in the graph. You can change it by setting the 'node_name' attribute." )
185+
186+ # get the last node in the list
187+ last_node = self .nodes [- 1 ]
188+ # add the edge connecting the last node to the new node
189+ self .raw_edges .append ((last_node , node ))
190+ # add the node to the list of nodes
191+ self .nodes .append (node )
192+ # update the edges connecting the last node to the new node
193+ self .edges = self ._create_edges ({e for e in self .raw_edges })
0 commit comments