Skip to content

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 broken connections when copying and pasting an agent (#9138)

Ritik Dutta33c718a9d7b7
diff --git a/autogpt_platform/frontend/src/hooks/useCopyPaste.ts b/autogpt_platform/frontend/src/hooks/useCopyPaste.ts
index c5c6400fa8..56339e1b5f 100644
--- a/autogpt_platform/frontend/src/hooks/useCopyPaste.ts
+++ b/autogpt_platform/frontend/src/hooks/useCopyPaste.ts
@@ -10,14 +10,22 @@ export function useCopyPaste(getNextNodeId: () => string) {
if (event.ctrlKey || event.metaKey) {
if (event.key === "c" || event.key === "C") {
const selectedNodes = getNodes().filter((node) => node.selected);
- const selectedEdges = getEdges().filter((edge) => edge.selected);
+ const selectedNodeIds = new Set(selectedNodes.map((node) => node.id));
+
+ // Only copy edges where both source and target nodes are selected
+ const selectedEdges = getEdges().filter(
+ (edge) =>
+ edge.selected &&
+ selectedNodeIds.has(edge.source) &&
+ selectedNodeIds.has(edge.target),
+ );
const copiedData = {
nodes: selectedNodes.map((node) => ({
...node,
data: {
...node.data,
- connections: [],
+ connections: node.data.connections || [], // Preserve connections
},
})),
edges: selectedEdges,
@@ -62,6 +70,7 @@ export function useCopyPaste(getNextNodeId: () => string) {
},
data: {
...node.data,
+ connections: node.data.connections || [], // Preserve connections
status: undefined,
executionResults: undefined,
},