Fix broken connections when copying and pasting an agent (#9138)

### Summary
This pull request fixes the issue of broken connections when copying and
pasting an agent. The connections are now preserved during the
copy-paste operation.

### Changes
- Modified the `useCopyPaste.ts` file to ensure that connections are
preserved during the copy-paste operation.

### Steps to Reproduce
1. Copy a small section from an Agent that features links to outside the
area you're selecting (use shift + click & drag).
2. Go to a fresh builder canvas in a new tab.
3. Paste the copied section.
4. Try to save the Agent; previously, it would fail due to the outgoing
link being broken.

### Testing
- Verified that connections remain intact when copying and pasting
agents.
- Tested by saving the agent to ensure no broken links.

### Related Issue
Fixes
[#9137](https://github.com/Significant-Gravitas/AutoGPT/issues/9137)

### Notes
Please review the changes and let me know if any further adjustments are
needed.

---------

Co-authored-by: Swifty <craigswift13@gmail.com>
Co-authored-by: Nicholas Tindle <nicholas.tindle@agpt.co>
Co-authored-by: Bently <tomnoon9@gmail.com>
This commit is contained in:
Ritik Dutta
2025-01-28 21:19:06 +05:30
committed by GitHub
parent c5747c59d7
commit 33c718a9d7

View File

@@ -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,
},