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.

fix(web): preserve loop linkage workflow edges

JPPhotof1174b0cebdd
diff --git a/invokeai/frontend/web/src/features/nodes/types/invocation.ts b/invokeai/frontend/web/src/features/nodes/types/invocation.ts
index 0019c13bb9..c14f5f9b54 100644
--- a/invokeai/frontend/web/src/features/nodes/types/invocation.ts
+++ b/invokeai/frontend/web/src/features/nodes/types/invocation.ts
@@ -240,6 +240,14 @@ const zDefaultInvocationNodeEdge = z.custom<Edge<Record<string, never>, 'default
);
export type DefaultInvocationNodeEdge = z.infer<typeof zDefaultInvocationNodeEdge>;
+const zLoopLinkageInvocationNodeEdgeValidationSchema = z.looseObject({
+ type: z.literal('loop_linkage'),
+});
+const zLoopLinkageInvocationNodeEdge = z.custom<Edge<Record<string, never>, 'loop_linkage'>>(
+ (val) => zLoopLinkageInvocationNodeEdgeValidationSchema.safeParse(val).success
+);
+export type LoopLinkageInvocationNodeEdge = z.infer<typeof zLoopLinkageInvocationNodeEdge>;
+
const zInvocationNodeEdgeCollapsedData = z.object({
count: z.number().int().min(1),
});
@@ -253,7 +261,11 @@ const zCollapsedInvocationNodeEdge = z.custom<Edge<InvocationNodeEdgeCollapsedDa
(val) => zInvocationNodeEdgeCollapsedValidationSchema.safeParse(val).success
);
export type CollapsedInvocationNodeEdge = z.infer<typeof zCollapsedInvocationNodeEdge>;
-export const zAnyEdge = z.union([zDefaultInvocationNodeEdge, zCollapsedInvocationNodeEdge]);
+export const zAnyEdge = z.union([
+ zDefaultInvocationNodeEdge,
+ zLoopLinkageInvocationNodeEdge,
+ zCollapsedInvocationNodeEdge,
+]);
export type AnyEdge = z.infer<typeof zAnyEdge>;
// #endregion
diff --git a/invokeai/frontend/web/src/features/nodes/types/workflow.ts b/invokeai/frontend/web/src/features/nodes/types/workflow.ts
index bfb7b92b18..0dac404410 100644
--- a/invokeai/frontend/web/src/features/nodes/types/workflow.ts
+++ b/invokeai/frontend/web/src/features/nodes/types/workflow.ts
@@ -57,10 +57,13 @@ const zWorkflowEdgeDefault = zWorkflowEdgeBase.extend({
targetHandle: z.string().trim().min(1),
hidden: z.boolean().optional(),
});
+const zWorkflowEdgeLoopLinkage = zWorkflowEdgeDefault.extend({
+ type: z.literal('loop_linkage'),
+});
const zWorkflowEdgeCollapsed = zWorkflowEdgeBase.extend({
type: z.literal('collapsed'),
});
-const zWorkflowEdge = z.union([zWorkflowEdgeDefault, zWorkflowEdgeCollapsed]);
+const zWorkflowEdge = z.union([zWorkflowEdgeDefault, zWorkflowEdgeLoopLinkage, zWorkflowEdgeCollapsed]);
// #endregion
// #region Workflow Builder
diff --git a/invokeai/frontend/web/src/features/nodes/util/workflow/buildWorkflow.test.ts b/invokeai/frontend/web/src/features/nodes/util/workflow/buildWorkflow.test.ts
index c7bdd80ace..5300db8657 100644
--- a/invokeai/frontend/web/src/features/nodes/util/workflow/buildWorkflow.test.ts
+++ b/invokeai/frontend/web/src/features/nodes/util/workflow/buildWorkflow.test.ts
@@ -40,4 +40,35 @@ describe('buildWorkflowFast', () => {
}
expect(serializedWorkflowIdInput.value).toBe('workflow-123');
});
+
+ it('preserves loop_linkage edges when saving a loaded workflow', async () => {
+ Object.assign(globalThis, {
+ window: {
+ location: {
+ origin: 'http://localhost',
+ },
+ },
+ });
+
+ const { buildWorkflowFast } = await import('features/nodes/util/workflow/buildWorkflow');
+ const node = buildNode(call_saved_workflow);
+ const loopEdge = {
+ id: 'loop-edge',
+ source: node.id,
+ sourceHandle: 'loop_linkage',
+ target: node.id,
+ targetHandle: 'loop_linkage',
+ type: 'loop_linkage',
+ } as unknown as ReturnType<typeof getInitialWorkflow>['edges'][number];
+
+ const workflow = buildWorkflowFast({
+ _version: 1,
+ formFieldInitialValues: {},
+ ...getInitialWorkflow(),
+ nodes: [node],
+ edges: [loopEdge],
+ });
+
+ expect(workflow.edges).toContainEqual(loopEdge);
+ });
});
diff --git a/invokeai/frontend/web/src/features/nodes/util/workflow/buildWorkflow.ts b/invokeai/frontend/web/src/features/nodes/util/workflow/buildWorkflow.ts
index 6d35fdf3f4..c1cdb1179f 100644
--- a/invokeai/frontend/web/src/features/nodes/util/workflow/buildWorkflow.ts
+++ b/invokeai/frontend/web/src/features/nodes/util/workflow/buildWorkflow.ts
@@ -52,7 +52,7 @@ export const buildWorkflowFast = (nodesState: NodesState): WorkflowV3 => {
}
for (const edge of edges) {
- if (edge.type === 'default' && edge.sourceHandle && edge.targetHandle) {
+ if ((edge.type === 'default' || edge.type === 'loop_linkage') && edge.sourceHandle && edge.targetHandle) {
const { id, type, source, target, sourceHandle, targetHandle, hidden } = edge;
newWorkflow.edges.push({ id, type, source, target, sourceHandle, targetHandle, hidden });
} else if (edge.type === 'collapsed') {
diff --git a/invokeai/frontend/web/src/features/nodes/util/workflow/graphToWorkflow.test.ts b/invokeai/frontend/web/src/features/nodes/util/workflow/graphToWorkflow.test.ts
index 18c30d486b..e692f7d071 100644
--- a/invokeai/frontend/web/src/features/nodes/util/workflow/graphToWorkflow.test.ts
+++ b/invokeai/frontend/web/src/features/nodes/util/workflow/graphToWorkflow.test.ts
@@ -182,6 +182,31 @@ describe('issue #9151: graphToWorkflow + zod validation roundtrip', () => {
expect(findInput(migrated, 'core_metadata:sbmUlPbCpY', 'z_image_seed_variance_strength')?.value).toBe(0.1);
expect(findInput(migrated, 'core_metadata:sbmUlPbCpY', 'z_image_seed_variance_randomize_percent')?.value).toBe(50);
});
+
+ it('loads webv2 workflows containing loop_linkage edges', () => {
+ const workflow = graphToWorkflow(
+ {
+ ...userFirstGraph,
+ edges: [
+ {
+ destination: { field: 'loop_linkage', node_id: 'lora_selector:lXZkTpWiQQ' },
+ source: { field: 'loop_linkage', node_id: 'core_metadata:sbmUlPbCpY' },
+ },
+ ],
+ } as NonNullableGraph,
+ false
+ );
+ const legacyWorkflow = JSON.parse(JSON.stringify(workflow)) as Record<string, unknown>;
+ legacyWorkflow.meta = { ...(legacyWorkflow.meta as Record<string, unknown>), version: '3.0.0' };
+ legacyWorkflow.edges = (legacyWorkflow.edges as Array<Record<string, unknown>>).map((edge) => ({
+ ...edge,
+ type: 'loop_linkage',
+ }));
+
+ const migrated = parseAndMigrateWorkflow(legacyWorkflow);
+
+ expect(migrated.edges[0]?.type).toBe('loop_linkage');
+ });
});
const imageCollectionTemplate = {