AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(frontend/builder): Sync frontend node IDs with backend after save (#11075)
Reinier van der Leerb67555391d12
diff --git a/autogpt_platform/frontend/src/hooks/useAgentGraph.tsx b/autogpt_platform/frontend/src/hooks/useAgentGraph.tsxindex 44bf913baa..2c9870d347 100644--- a/autogpt_platform/frontend/src/hooks/useAgentGraph.tsx+++ b/autogpt_platform/frontend/src/hooks/useAgentGraph.tsx@@ -629,7 +629,7 @@ export default function useAgentGraph(recommended_schedule_cron: agentRecommendedScheduleCron || null,nodes: xyNodes.map((node): NodeCreatable => ({- id: node.id,+ id: node.data.backend_id ?? node.id,block_id: node.data.block_id,input_default: prepareNodeInputData(node),metadata: {@@ -695,38 +695,56 @@ export default function useAgentGraph(// Update the node IDs on the frontendsetSavedAgent(newSavedAgent);- setXYNodes((prev) => {- return newSavedAgent.nodes++ // Create a mapping from old frontend IDs to new backend IDs+ const oldToNewNodeIDMap = new Map<string, string>();++ setXYNodes((prev) =>+ newSavedAgent.nodes.map((backendNode) => {const key = `${backendNode.block_id}_${backendNode.metadata.position.x}_${backendNode.metadata.position.y}`;- const frontendNodeID = blockIDToNodeIDMap[key];- const frontendNode = prev.find((node) => node.id === frontendNodeID);+ const oldFrontendNodeID = blockIDToNodeIDMap[key];+ const frontendNode = prev.find(+ (node) => node.id === oldFrontendNodeID,+ );++ if (frontendNode) {+ // Store the ID mapping for edge updates+ oldToNewNodeIDMap.set(oldFrontendNodeID, backendNode.id);+ }const { position, ...metadata } = backendNode.metadata;return frontendNode? ({...frontendNode,+ id: backendNode.id, // Update the actual node ID to match backendposition,data: {...frontendNode.data,hardcodedValues: removeEmptyStringsAndNulls(frontendNode.data.hardcodedValues,),- status: undefined,backend_id: backendNode.id,- executionResults: [],metadata,+ status: undefined,+ executionResults: undefined,},} satisfies CustomNode): _backendNodeToXYNode(backendNode, newSavedAgent); // fallback})- .filter((node) => node !== null);- });- // Reset bead count- setXYEdges((edges) => {- return edges.map(- (edge): CustomEdge => ({+ .filter((node) => node !== null),+ );++ // Reset bead count & update edge references to use the new node IDs+ setXYEdges((edges) =>+ edges.map((edge): CustomEdge => {+ const newSourceId = oldToNewNodeIDMap.get(edge.source) || edge.source;+ const newTargetId = oldToNewNodeIDMap.get(edge.target) || edge.target;++ return {...edge,+ source: newSourceId,+ target: newTargetId,data: {...edge.data,edgeColor: edge.data?.edgeColor ?? "grey",@@ -734,9 +752,9 @@ export default function useAgentGraph(beadDown: 0,beadData: new Map(),},- }),- );- });+ };+ }),+ );return newSavedAgent;}, [api,