Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

feat(webv2): reconnect workflow edges from either end

Josh Corbett7f9451ba58bb
diff --git a/invokeai/frontend/webv2/src/features/workflow/core/document.test.ts b/invokeai/frontend/webv2/src/features/workflow/core/document.test.ts
index 7b317382ce..687349fa6b 100644
--- a/invokeai/frontend/webv2/src/features/workflow/core/document.test.ts
+++ b/invokeai/frontend/webv2/src/features/workflow/core/document.test.ts
@@ -169,6 +169,18 @@ describe('projectGraphReducer', () => {
expect(next.edges[0]?.id).toBe('edge-replacement');
});
+ it('reconnecting an edge replaces it in one step and ignores unknown edges', () => {
+ const { doc, nodeAId, nodeBId } = createDocWithNodes();
+ const original = createEdge(nodeAId, nodeBId);
+ const withEdge = projectGraphReducer(doc, { edge: original, type: 'addEdge' });
+ const moved = { ...createEdge(nodeAId, nodeBId), id: 'edge-moved', targetHandle: 'b' };
+
+ const next = projectGraphReducer(withEdge, { edge: moved, edgeId: original.id, type: 'reconnectEdge' });
+
+ expect(next.edges).toEqual([moved]);
+ expect(projectGraphReducer(withEdge, { edge: moved, edgeId: 'missing', type: 'reconnectEdge' })).toBe(withEdge);
+ });
+
it('sets field values without disturbing other inputs', () => {
const { doc, nodeAId } = createDocWithNodes();
const next = projectGraphReducer(doc, { fieldName: 'a', nodeId: nodeAId, type: 'setFieldValue', value: 42 });
diff --git a/invokeai/frontend/webv2/src/features/workflow/core/document.ts b/invokeai/frontend/webv2/src/features/workflow/core/document.ts
index fc34887250..d073810656 100644
--- a/invokeai/frontend/webv2/src/features/workflow/core/document.ts
+++ b/invokeai/frontend/webv2/src/features/workflow/core/document.ts
@@ -305,6 +305,8 @@ export type ProjectGraphAction =
| { type: 'advanceSeedFields'; advances: readonly WorkflowSeedFieldAdvance[] }
| { type: 'addEdge'; edge: WorkflowEdge }
| { type: 'removeEdges'; edgeIds: string[] }
+ /** Replaces `edgeId` with `edge` as one undoable step (dragging an edge end to a new handle). */
+ | { type: 'reconnectEdge'; edgeId: string; edge: WorkflowEdge }
| { type: 'exposeField'; fieldIdentifier: FieldIdentifier }
| { type: 'unexposeField'; fieldIdentifier: FieldIdentifier }
| { type: 'removeFormElement'; elementId: string }
@@ -325,6 +327,7 @@ const undoLabels: Partial<Record<ProjectGraphAction['type'], string>> = {
addEdge: 'Connect workflow fields',
addFormElement: 'Edit workflow form',
addGraphElements: 'Paste workflow nodes',
+ reconnectEdge: 'Reconnect workflow fields',
addNode: 'Add workflow node',
addNodeAndEdge: 'Add workflow node',
exposeField: 'Expose workflow field',
@@ -580,6 +583,16 @@ const applyProjectGraphAction = (document: ProjectGraphState, action: ProjectGra
}),
};
}
+ case 'reconnectEdge': {
+ if (!document.edges.some((edge) => edge.id === action.edgeId)) {
+ return document;
+ }
+
+ return addEdgeToDocument(
+ { ...document, edges: document.edges.filter((edge) => edge.id !== action.edgeId) },
+ action.edge
+ );
+ }
case 'unexposeField': {
const element = findNodeFieldElement(document.form, action.fieldIdentifier);
diff --git a/invokeai/frontend/webv2/src/features/workflow/ui/editor/WorkflowEditorView.tsx b/invokeai/frontend/webv2/src/features/workflow/ui/editor/WorkflowEditorView.tsx
index 2d8ab007f1..7fe3b8d728 100644
--- a/invokeai/frontend/webv2/src/features/workflow/ui/editor/WorkflowEditorView.tsx
+++ b/invokeai/frontend/webv2/src/features/workflow/ui/editor/WorkflowEditorView.tsx
@@ -1,4 +1,4 @@
-import type { ProjectGraphState, XYPosition } from '@features/workflow/contracts';
+import type { ProjectGraphState, WorkflowEdge as WorkflowDocumentEdge, XYPosition } from '@features/workflow/contracts';
import type { WorkflowPerfSource, WorkflowRuntimeApi } from '@features/workflow/ui/contracts';
import { Box, Flex, HStack, Spinner, Stack, Text } from '@chakra-ui/react';
@@ -37,6 +37,8 @@ import {
type Connection,
type EdgeChange,
type EdgeTypes,
+ type FinalConnectionState,
+ type HandleType,
type IsValidConnection,
type NodeChange,
type NodeTypes,
@@ -112,6 +114,22 @@ const GRID_SIZE = 25;
const SNAP_GRID: [number, number] = [GRID_SIZE, GRID_SIZE];
const DELETE_KEY_CODES = ['Backspace', 'Delete'];
+const RECONNECT_DISCONNECT_DISTANCE_PX = 5;
+
+const toDocumentEdge = (connection: Connection): WorkflowDocumentEdge | null =>
+ connection.sourceHandle && connection.targetHandle
+ ? {
+ id: createWorkflowId('edge'),
+ source: connection.source,
+ sourceHandle: connection.sourceHandle,
+ target: connection.target,
+ targetHandle: connection.targetHandle,
+ type:
+ connection.sourceHandle === LOOP_LINKAGE_FIELD && connection.targetHandle === LOOP_LINKAGE_FIELD
+ ? 'loop_linkage'
+ : 'default',
+ }
+ : null;
const DEFAULT_EDGE_OPTIONS = { style: { strokeWidth: 2 } };
// A fresh graph starts clear of the floating toolbar in the left gutter, so
@@ -703,6 +721,9 @@ const WorkflowFlow = ({ runtime }: { runtime: WorkflowRuntimeApi }) => {
[editGraph]
);
+ // The edge whose end is being dragged; it must not count against its own reconnection.
+ const reconnectingEdgeRef = useRef<{ edgeId: string; didReconnect: boolean; startPosition: XYPosition } | null>(null);
+
const isValidConnection: IsValidConnection<WorkflowFlowEdge> = useCallback(
(connection) => {
if (!connection.sourceHandle || !connection.targetHandle) {
@@ -714,6 +735,11 @@ const WorkflowFlow = ({ runtime }: { runtime: WorkflowRuntimeApi }) => {
return true;
}
+ const reconnectingEdgeId = reconnectingEdgeRef.current?.edgeId;
+ const document = reconnectingEdgeId
+ ? { edges: projectGraph.edges.filter((edge) => edge.id !== reconnectingEdgeId), nodes: projectGraph.nodes }
+ : projectGraph;
+
return (
validateConnection(
{
@@ -722,7 +748,7 @@ const WorkflowFlow = ({ runtime }: { runtime: WorkflowRuntimeApi }) => {
targetHandle: connection.targetHandle,
targetNodeId: connection.target,
},
- projectGraph,
+ document,
templates
) === null
);
@@ -732,30 +758,80 @@ const WorkflowFlow = ({ runtime }: { runtime: WorkflowRuntimeApi }) => {
const onConnect = useCallback(
(connection: Connection) => {
- if (!connection.sourceHandle || !connection.targetHandle) {
+ const edge = toDocumentEdge(connection);
+
+ if (edge) {
+ editGraph({ edge, type: 'addEdge' });
+ }
+ },
+ [editGraph]
+ );
+
+ const onReconnectStart = useCallback((event: ReactMouseEvent, edge: WorkflowFlowEdge) => {
+ reconnectingEdgeRef.current = {
+ didReconnect: false,
+ edgeId: edge.id,
+ startPosition: { x: event.clientX, y: event.clientY },
+ };
+ }, []);
+
+ const onReconnect = useCallback(
+ (oldEdge: WorkflowFlowEdge, connection: Connection) => {
+ const edge = toDocumentEdge(connection);
+
+ if (!edge) {
return;
}
- editGraph({
- edge: {
- id: createWorkflowId('edge'),
- source: connection.source,
- sourceHandle: connection.sourceHandle,
- target: connection.target,
- targetHandle: connection.targetHandle,
- type:
- connection.sourceHandle === LOOP_LINKAGE_FIELD && connection.targetHandle === LOOP_LINKAGE_FIELD
- ? 'loop_linkage'
- : 'default',
- },
- type: 'addEdge',
- });
+ if (reconnectingEdgeRef.current?.edgeId === oldEdge.id) {
+ reconnectingEdgeRef.current.didReconnect = true;
+ }
+
+ const isSameConnection =
+ oldEdge.source === edge.source &&
+ oldEdge.sourceHandle === edge.sourceHandle &&
+ oldEdge.target === edge.target &&
+ oldEdge.targetHandle === edge.targetHandle;
+
+ if (!isSameConnection) {
+ editGraph({ edge, edgeId: oldEdge.id, type: 'reconnectEdge' });
+ }
+ },
+ [editGraph]
+ );
+
+ const onReconnectEnd = useCallback(
+ (event: MouseEvent | TouchEvent, edge: WorkflowFlowEdge, _handleType: HandleType, state: FinalConnectionState) => {
+ const reconnecting = reconnectingEdgeRef.current;
+
+ reconnectingEdgeRef.current = null;
+
+ if (!reconnecting || reconnecting.edgeId !== edge.id || reconnecting.didReconnect || state.toHandle) {
+ return;
+ }
+
+ // Dragging an end away and dropping it on empty canvas disconnects; a plain click, or a
+ // drop on a handle that rejected the connection, leaves the edge alone.
+ const position = getEventClientPosition(event);
+ const didMove =
+ position !== null &&
+ Math.hypot(position.x - reconnecting.startPosition.x, position.y - reconnecting.startPosition.y) >
+ RECONNECT_DISCONNECT_DISTANCE_PX;
+
+ if (didMove) {
+ editGraph({ edgeIds: [edge.id], type: 'removeEdges' });
+ }
},
[editGraph]
);
const onConnectEnd = useCallback<OnConnectEnd>(
(event, connectionState) => {
+ // xyflow also raises connect-end for a reconnect drag; that gesture is settled by onReconnectEnd.
+ if (reconnectingEdgeRef.current) {
+ return;
+ }
+
if (!flowInstance || !(event.target instanceof Element) || !event.target.closest('.react-flow__pane')) {
return;
}
@@ -1067,6 +1143,9 @@ const WorkflowFlow = ({ runtime }: { runtime: WorkflowRuntimeApi }) => {
style={flowStyle}
onConnect={onConnect}
onConnectEnd={onConnectEnd}
+ onReconnect={onReconnect}
+ onReconnectEnd={onReconnectEnd}
+ onReconnectStart={onReconnectStart}
onEdgeClick={onEdgeClick}
onEdgesChange={onEdgesChange}
onInit={onFlowInit}
diff --git a/invokeai/frontend/webv2/src/workbench/autoRoutePolicy.test.ts b/invokeai/frontend/webv2/src/workbench/autoRoutePolicy.test.ts
index 61716db19e..c51f2568ea 100644
--- a/invokeai/frontend/webv2/src/workbench/autoRoutePolicy.test.ts
+++ b/invokeai/frontend/webv2/src/workbench/autoRoutePolicy.test.ts
@@ -161,6 +161,7 @@ describe('Workflow edit classification', () => {
'addGraphElements',
'addNode',
'addNodeAndEdge',
+ 'reconnectEdge',
'removeEdges',
'removeNodes',
'setFieldValue',
diff --git a/invokeai/frontend/webv2/src/workbench/autoRoutePolicy.ts b/invokeai/frontend/webv2/src/workbench/autoRoutePolicy.ts
index 3123d696ef..cc5af4777b 100644
--- a/invokeai/frontend/webv2/src/workbench/autoRoutePolicy.ts
+++ b/invokeai/frontend/webv2/src/workbench/autoRoutePolicy.ts
@@ -78,6 +78,7 @@ export const isHighConfidenceVideoEdit = (changedKeys: readonly string[]): boole
const GRAPH_EDIT_CONFIDENCE = {
addEdge: true,
+ reconnectEdge: true,
addFormElement: false,
addGraphElements: true,
addNode: true,