From 06bbfa2e04a9073c10da3eb1a7b7618640b33487 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Thu, 5 Oct 2023 13:08:09 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(utils.py):=20fix=20find=5Fla?= =?UTF-8?q?st=5Fnode=20function=20to=20receive=20nodes=20and=20edges=20as?= =?UTF-8?q?=20arguments=20instead=20of=20data=20dictionary=20=F0=9F=90=9B?= =?UTF-8?q?=20fix(utils.py):=20fix=20update=5Fsource=5Fhandle=20function?= =?UTF-8?q?=20to=20receive=20g=5Fnodes=20and=20g=5Fedges=20as=20arguments?= =?UTF-8?q?=20instead=20of=20flow=5Fdata=20=F0=9F=90=9B=20fix(utils.py):?= =?UTF-8?q?=20fix=20get=5Fupdated=5Fedges=20function=20to=20receive=20g=5F?= =?UTF-8?q?nodes=20and=20g=5Fedges=20as=20arguments=20instead=20of=20base?= =?UTF-8?q?=5Fflow=20and=20group=5Fnode=5Fid=20=E2=9C=A8=20feat(utils.py):?= =?UTF-8?q?=20add=20support=20for=20passing=20nodes=20and=20edges=20as=20a?= =?UTF-8?q?rguments=20to=20find=5Flast=5Fnode,=20update=5Fsource=5Fhandle,?= =?UTF-8?q?=20and=20get=5Fupdated=5Fedges=20functions=20to=20improve=20reu?= =?UTF-8?q?sability=20and=20modularity?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/langflow/graph/graph/utils.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/backend/langflow/graph/graph/utils.py b/src/backend/langflow/graph/graph/utils.py index 2288e4130..71b81fea1 100644 --- a/src/backend/langflow/graph/graph/utils.py +++ b/src/backend/langflow/graph/graph/utils.py @@ -2,11 +2,10 @@ from collections import deque import copy -def find_last_node(data): +def find_last_node(nodes, edges): """ This function receives a flow and returns the last node. """ - nodes, edges = data["nodes"], data["edges"] return next((n for n in nodes if all(e["source"] != n["id"] for e in edges)), None) @@ -29,7 +28,9 @@ def ungroup_node(group_node_data, base_flow): g_edges = flow["data"]["edges"] # Redirect edges to the correct proxy node - updated_edges = get_updated_edges(base_flow, g_nodes, group_node_data["id"]) + updated_edges = get_updated_edges( + base_flow, g_nodes, g_edges, group_node_data["id"] + ) # Update template values update_template(template, g_nodes) @@ -183,7 +184,7 @@ def set_new_target_handle(proxy_id, new_edge, target_handle, node): new_edge["data"]["targetHandle"] = new_target_handle -def update_source_handle(new_edge, flow_data): +def update_source_handle(new_edge, g_nodes, g_edges): """ Updates the source handle of a given edge to the last node in the flow data. @@ -194,7 +195,7 @@ def update_source_handle(new_edge, flow_data): Returns: dict: The updated edge with the new source handle. """ - last_node = copy.deepcopy(find_last_node(flow_data)) + last_node = copy.deepcopy(find_last_node(g_nodes, g_edges)) new_edge["source"] = last_node["id"] new_source_handle = new_edge["data"]["sourceHandle"] new_source_handle["id"] = last_node["id"] @@ -202,7 +203,7 @@ def update_source_handle(new_edge, flow_data): return new_edge -def get_updated_edges(base_flow, g_nodes, group_node_id): +def get_updated_edges(base_flow, g_nodes, g_edges, group_node_id): """ Given a base flow, a list of graph nodes and a group node id, returns a list of updated edges. An updated edge is an edge that has its target or source handle updated based on the group node id. @@ -222,7 +223,7 @@ def get_updated_edges(base_flow, g_nodes, group_node_id): new_edge = update_target_handle(new_edge, g_nodes, group_node_id) if new_edge["source"] == group_node_id: - new_edge = update_source_handle(new_edge, base_flow) + new_edge = update_source_handle(new_edge, g_nodes, g_edges) if edge["target"] == group_node_id or edge["source"] == group_node_id: updated_edges.append(new_edge)