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.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
Improve image collection chaining (#9168)
Jonathan69c588288800
diff --git a/invokeai/app/invocations/primitives.py b/invokeai/app/invocations/primitives.pyindex 7ec6c3dc14..6249de0cd8 100644--- a/invokeai/app/invocations/primitives.py+++ b/invokeai/app/invocations/primitives.py@@ -279,15 +279,28 @@ class ImageInvocation(BaseInvocation):title="Image Collection Primitive",tags=["primitives", "image", "collection"],category="primitives",- version="1.0.1",+ version="1.0.2",)class ImageCollectionInvocation(BaseInvocation):"""A collection of image primitive values"""- collection: list[ImageField] = InputField(description="The collection of image values")+ collection: Optional[list[ImageField]] = InputField(+ default=None,+ description="An optional image collection to append to",+ input=Input.Connection,+ title="Collection",+ ui_order=0,+ )+ images: Optional[list[ImageField]] = InputField(+ default=None,+ description="The images to append to the collection",+ input=Input.Direct,+ title="Images",+ ui_order=1,+ )def invoke(self, context: InvocationContext) -> ImageCollectionOutput:- return ImageCollectionOutput(collection=self.collection)+ return ImageCollectionOutput(collection=[*(self.collection or []), *(self.images or [])])# endregiondiff --git a/invokeai/frontend/web/openapi.json b/invokeai/frontend/web/openapi.jsonindex bd39ee6ffa..0c8d8dc51c 100644--- a/invokeai/frontend/web/openapi.json+++ b/invokeai/frontend/web/openapi.json@@ -32518,11 +32518,34 @@}],"default": null,- "description": "The collection of image values",+ "description": "An optional image collection to append to","field_kind": "input",- "input": "any",- "orig_required": true,- "title": "Collection"+ "input": "connection",+ "orig_default": null,+ "orig_required": false,+ "title": "Collection",+ "ui_order": 0+ },+ "images": {+ "anyOf": [+ {+ "items": {+ "$ref": "#/components/schemas/ImageField"+ },+ "type": "array"+ },+ {+ "type": "null"+ }+ ],+ "default": null,+ "description": "The images to append to the collection",+ "field_kind": "input",+ "input": "direct",+ "orig_default": null,+ "orig_required": false,+ "title": "Images",+ "ui_order": 1},"type": {"const": "image_collection",@@ -32536,7 +32559,7 @@"tags": ["primitives", "image", "collection"],"title": "Image Collection Primitive","type": "object",- "version": "1.0.1",+ "version": "1.0.2","output": {"$ref": "#/components/schemas/ImageCollectionOutput"}diff --git a/invokeai/frontend/web/src/features/nodes/components/flow/panels/TopPanel/UpdateNodesButton.tsx b/invokeai/frontend/web/src/features/nodes/components/flow/panels/TopPanel/UpdateNodesButton.tsxindex 5072d3ae7f..23fa81a7fd 100644--- a/invokeai/frontend/web/src/features/nodes/components/flow/panels/TopPanel/UpdateNodesButton.tsx+++ b/invokeai/frontend/web/src/features/nodes/components/flow/panels/TopPanel/UpdateNodesButton.tsx@@ -3,10 +3,9 @@ import { logger } from 'app/logging/logger';import { useAppStore } from 'app/store/storeHooks';import { useGetNodesNeedUpdate } from 'features/nodes/hooks/useGetNodesNeedUpdate';import { $templates, nodesChanged } from 'features/nodes/store/nodesSlice';-import { selectNodes } from 'features/nodes/store/selectors';-import { NodeUpdateError } from 'features/nodes/types/error';+import { selectEdges, selectNodes } from 'features/nodes/store/selectors';import { isInvocationNode } from 'features/nodes/types/invocation';-import { getNeedsUpdate, updateNode } from 'features/nodes/util/node/nodeUpdate';+import { getConnectedInputNames, getNeedsUpdate, updateNode } from 'features/nodes/util/node/nodeUpdate';import { toast } from 'features/toast/toast';import { memo, useCallback } from 'react';import { useTranslation } from 'react-i18next';@@ -20,6 +19,7 @@ const useUpdateNodes = () => {const updateNodes = useCallback(() => {const nodes = selectNodes(store.getState());+ const edges = selectEdges(store.getState());const templates = $templates.get();let unableToUpdateCount = 0;@@ -35,17 +35,16 @@ const useUpdateNodes = () => {return;}try {- const updatedNode = updateNode(node, template);+ const connectedInputNames = getConnectedInputNames(node.id, edges);+ const updatedNode = updateNode(node, template, { connectedInputNames });store.dispatch(nodesChanged([{ type: 'remove', id: updatedNode.id },{ type: 'add', item: updatedNode },]));- } catch (e) {- if (e instanceof NodeUpdateError) {- unableToUpdateCount++;- }+ } catch {+ unableToUpdateCount++;}});diff --git a/invokeai/frontend/web/src/features/nodes/util/node/nodeUpdate.test.ts b/invokeai/frontend/web/src/features/nodes/util/node/nodeUpdate.test.tsnew file mode 100644index 0000000000..07d826c078--- /dev/null+++ b/invokeai/frontend/web/src/features/nodes/util/node/nodeUpdate.test.ts@@ -0,0 +1,143 @@+import type { InvocationTemplate } from 'features/nodes/types/invocation';+import { buildInvocationNode } from 'features/nodes/util/node/buildInvocationNode';+import { updateNode } from 'features/nodes/util/node/nodeUpdate';+import { describe, expect, it } from 'vitest';++const imageCollectionOutput = {+ collection: {+ fieldKind: 'output',+ name: 'collection',+ title: 'Collection',+ description: 'The output images',+ type: {+ name: 'ImageField',+ cardinality: 'COLLECTION',+ batch: false,+ },+ ui_hidden: false,+ },+} satisfies InvocationTemplate['outputs'];++const oldImageCollectionTemplate = {+ title: 'Image Collection Primitive',+ type: 'image_collection',+ version: '1.0.1',+ tags: ['primitives', 'image', 'collection'],+ description: 'A collection of image primitive values',+ outputType: 'image_collection_output',+ inputs: {+ collection: {+ name: 'collection',+ title: 'Collection',+ required: false,+ description: 'The collection of image values',+ fieldKind: 'input',+ input: 'any',+ ui_hidden: false,+ type: {+ name: 'ImageField',+ cardinality: 'COLLECTION',+ batch: false,+ },+ default: undefined,+ },+ },+ outputs: imageCollectionOutput,+ useCache: true,+ nodePack: 'invokeai',+ classification: 'stable',+ category: 'primitives',+} satisfies InvocationTemplate;++const oldestImageCollectionTemplate = {+ ...oldImageCollectionTemplate,+ version: '1.0.0',+} satisfies InvocationTemplate;++const currentImageCollectionTemplate = {+ ...oldImageCollectionTemplate,+ version: '1.0.2',+ inputs: {+ collection: {+ name: 'collection',+ title: 'Collection',+ required: false,+ description: 'An optional image collection to append to',+ fieldKind: 'input',+ input: 'connection',+ ui_hidden: false,+ type: {+ name: 'ImageField',+ cardinality: 'COLLECTION',+ batch: false,+ },+ default: undefined,+ },+ images: {+ name: 'images',+ title: 'Images',+ required: false,+ description: 'The images to append to the collection',+ fieldKind: 'input',+ input: 'direct',+ ui_hidden: false,+ type: {+ name: 'ImageField',+ cardinality: 'COLLECTION',+ batch: false,+ },+ default: undefined,+ },+ },+} satisfies InvocationTemplate;++describe('updateNode', () => {+ it('moves old image_collection direct collection values to the new images field', () => {+ const node = buildInvocationNode({ x: 0, y: 0 }, oldImageCollectionTemplate);+ const images = [{ image_name: 'first' }, { image_name: 'second' }];+ const collectionInput = node.data.inputs.collection;+ if (!collectionInput) {+ throw new Error('Expected collection input');+ }+ collectionInput.value = images;++ const updated = updateNode(node, currentImageCollectionTemplate, { connectedInputNames: new Set() });++ expect(updated.data.version).toBe('1.0.2');+ expect(updated.data.inputs.images?.value).toEqual(images);+ expect(updated.data.inputs.collection?.value).toEqual([]);+ });++ it('moves 1.0.0 image_collection direct collection values to the new images field', () => {+ const node = buildInvocationNode({ x: 0, y: 0 }, oldestImageCollectionTemplate);+ const images = [{ image_name: 'first' }];+ const collectionInput = node.data.inputs.collection;+ if (!collectionInput) {+ throw new Error('Expected collection input');+ }+ collectionInput.value = images;++ const updated = updateNode(node, currentImageCollectionTemplate, { connectedInputNames: new Set() });++ expect(updated.data.version).toBe('1.0.2');+ expect(updated.data.inputs.images?.value).toEqual(images);+ expect(updated.data.inputs.collection?.value).toEqual([]);+ });++ it('preserves old image_collection direct collection values when collection is connected', () => {+ const node = buildInvocationNode({ x: 0, y: 0 }, oldImageCollectionTemplate);+ const images = [{ image_name: 'stale' }];+ const collectionInput = node.data.inputs.collection;+ if (!collectionInput) {+ throw new Error('Expected collection input');+ }+ collectionInput.value = images;++ const updated = updateNode(node, currentImageCollectionTemplate, {+ connectedInputNames: new Set(['collection']),+ });++ expect(updated.data.inputs.images?.value).toBeUndefined();+ expect(updated.data.inputs.collection?.value).toEqual(images);+ });+});diff --git a/invokeai/frontend/web/src/features/nodes/util/node/nodeUpdate.ts b/invokeai/frontend/web/src/features/nodes/util/node/nodeUpdate.tsindex 59eac81b33..00c19a0cf3 100644--- a/invokeai/frontend/web/src/features/nodes/util/node/nodeUpdate.ts+++ b/invokeai/frontend/web/src/features/nodes/util/node/nodeUpdate.ts@@ -1,5 +1,5 @@import { deepClone } from 'common/util/deepClone';-import { satisfies } from 'compare-versions';+import { compare, satisfies } from 'compare-versions';import { defaultsDeep, keys, pick } from 'es-toolkit/compat';import { NodeUpdateError } from 'features/nodes/types/error';import type { InvocationNode, InvocationNodeData, InvocationTemplate } from 'features/nodes/types/invocation';@@ -7,6 +7,26 @@ import { zParsedSemver } from 'features/nodes/types/semver';import { buildInvocationNode } from './buildInvocationNode';+type ConnectedInputEdge = { type?: string; target: string; targetHandle?: string | null };++type UpdateNodeOptions = {+ connectedInputNames: Set<string>;+};++export const getConnectedInputNames = (nodeId: string, edges: ConnectedInputEdge[]): Set<string> =>+ new Set(+ edges.flatMap((edge) =>+ edge.type === 'default' && edge.target === nodeId && edge.targetHandle ? [edge.targetHandle] : []+ )+ );++export const getUpdatedFieldName = (node: InvocationNode, fieldName: string): string => {+ if (node.data.type === 'image_collection' && fieldName === 'collection' && node.data.inputs.images) {+ return 'images';+ }+ return fieldName;+};+export const getNeedsUpdate = (data: InvocationNodeData, template: InvocationTemplate): boolean => {if (data.type !== template.type) {return true;@@ -29,6 +49,34 @@ const getMayUpdateNode = (node: InvocationNode, template: InvocationTemplate): breturn satisfies(node.data.version, `^${templateMajor}`);};+export const migrateImageCollectionInputValues = (+ node: InvocationNode,+ options: UpdateNodeOptions & { sourceVersion?: string }+) => {+ if (node.data.type !== 'image_collection') {+ return;+ }+ if (options.sourceVersion && compare(options.sourceVersion, '1.0.2', '>=')) {+ return;+ }++ const collection = node.data.inputs.collection;+ const images = node.data.inputs.images;+ if (!collection || !images || !Array.isArray(collection.value)) {+ return;+ }+ if (Array.isArray(images.value) && images.value.length > 0) {+ return;+ }++ if (options.connectedInputNames.has('collection')) {+ return;+ }++ images.value = collection.value;+ collection.value = [];+};+/*** Updates a node to the latest version of its template:* - Create a new node data object with the latest version of the template.@@ -40,7 +88,11 @@ const getMayUpdateNode = (node: InvocationNode, template: InvocationTemplate): b* @param template The invocation template to update to.* @throws {NodeUpdateError} If the node is not an invocation node.*/-export const updateNode = (node: InvocationNode, template: InvocationTemplate): InvocationNode => {+export const updateNode = (+ node: InvocationNode,+ template: InvocationTemplate,+ options: UpdateNodeOptions+): InvocationNode => {const mayUpdate = getMayUpdateNode(node, template);if (!mayUpdate || node.data.type !== template.type) {@@ -54,8 +106,10 @@ export const updateNode = (node: InvocationNode, template: InvocationTemplate):// being valid. We rely on the template's major version to be majorly incremented if this kind of// merge would result in an invalid node.const clone = deepClone(node);+ const sourceVersion = clone.data.version;clone.data.version = template.version;defaultsDeep(clone, defaults); // mutates!+ migrateImageCollectionInputValues(clone, { ...options, sourceVersion });// Remove any fields that are not in the templateclone.data.inputs = pick(clone.data.inputs, keys(defaults.data.inputs));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.tsnew file mode 100644index 0000000000..d4f0f75a57--- /dev/null+++ b/invokeai/frontend/web/src/features/nodes/util/workflow/graphToWorkflow.test.ts@@ -0,0 +1,122 @@+import { $templates } from 'features/nodes/store/nodesSlice';+import type { InvocationTemplate } from 'features/nodes/types/invocation';+import { isWorkflowInvocationNode } from 'features/nodes/types/workflow';+import { graphToWorkflow } from 'features/nodes/util/workflow/graphToWorkflow';+import type { NonNullableGraph } from 'services/api/types';+import { afterEach, beforeEach, describe, expect, it } from 'vitest';++const imageCollectionTemplate = {+ title: 'Image Collection Primitive',+ type: 'image_collection',+ version: '1.0.2',+ tags: ['primitives', 'image', 'collection'],+ description: 'A collection of image primitive values',+ outputType: 'image_collection_output',+ inputs: {+ collection: {+ name: 'collection',+ title: 'Collection',+ required: false,+ description: 'An optional image collection to append to',+ fieldKind: 'input',+ input: 'connection',+ ui_hidden: false,+ type: { name: 'ImageField', cardinality: 'COLLECTION', batch: false },+ default: undefined,+ },+ images: {+ name: 'images',+ title: 'Images',+ required: false,+ description: 'The images to append to the collection',+ fieldKind: 'input',+ input: 'direct',+ ui_hidden: false,+ type: { name: 'ImageField', cardinality: 'COLLECTION', batch: false },+ default: undefined,+ },+ },+ outputs: {+ collection: {+ fieldKind: 'output',+ name: 'collection',+ title: 'Collection',+ description: 'The output images',+ type: { name: 'ImageField', cardinality: 'COLLECTION', batch: false },+ ui_hidden: false,+ },+ },+ useCache: true,+ nodePack: 'invokeai',+ classification: 'stable',+ category: 'primitives',+} satisfies InvocationTemplate;++describe('graphToWorkflow', () => {+ const originalTemplates = $templates.get();++ beforeEach(() => {+ $templates.set({ image_collection: imageCollectionTemplate });+ });++ afterEach(() => {+ $templates.set(originalTemplates);+ });++ it('moves legacy image_collection graph collection values to the visible images field', () => {+ const images = [{ image_name: 'legacy.png' }];+ const graph: NonNullableGraph = {+ id: 'graph',+ nodes: {+ image_collection: {+ id: 'image_collection',+ type: 'image_collection',+ collection: images,+ },+ },+ edges: [],+ };++ const workflow = graphToWorkflow(graph, false);+ const node = workflow.nodes[0];++ if (!node || !isWorkflowInvocationNode(node)) {+ throw new Error('Expected an image_collection workflow node');+ }+ expect(node.data.inputs.images?.value).toEqual(images);+ expect(node.data.inputs.collection?.value).toEqual([]);+ });++ it('preserves legacy image_collection graph collection values when collection is connected', () => {+ const images = [{ image_name: 'shadowed.png' }];+ const graph: NonNullableGraph = {+ id: 'graph',+ nodes: {+ source: {+ id: 'source',+ type: 'image_collection',+ },+ target: {+ id: 'target',+ type: 'image_collection',+ collection: images,+ },+ },+ edges: [+ {+ source: { node_id: 'source', field: 'collection' },+ destination: { node_id: 'target', field: 'collection' },+ },+ ],+ };++ const workflow = graphToWorkflow(graph, false);+ const node = workflow.nodes[1];++ if (!node || !isWorkflowInvocationNode(node)) {+ throw new Error('Expected an image_collection workflow node');+ }+ expect(node.data.inputs.images?.value).toBeUndefined();+ expect(node.data.inputs.collection?.value).toEqual(images);+ });+});diff --git a/invokeai/frontend/web/src/features/nodes/util/workflow/graphToWorkflow.ts b/invokeai/frontend/web/src/features/nodes/util/workflow/graphToWorkflow.tsindex c09f4e1729..0dc24db323 100644--- a/invokeai/frontend/web/src/features/nodes/util/workflow/graphToWorkflow.ts+++ b/invokeai/frontend/web/src/features/nodes/util/workflow/graphToWorkflow.ts@@ -5,7 +5,8 @@ import { $templates } from 'features/nodes/store/nodesSlice';import { NODE_WIDTH } from 'features/nodes/types/constants';import type { FieldInputInstance } from 'features/nodes/types/field';import type { WorkflowV3 } from 'features/nodes/types/workflow';-import { getDefaultForm } from 'features/nodes/types/workflow';+import { getDefaultForm, isWorkflowInvocationNode } from 'features/nodes/types/workflow';+import { getConnectedInputNames, migrateImageCollectionInputValues } from 'features/nodes/util/node/nodeUpdate';import { buildFieldInputInstance } from 'features/nodes/util/schema/buildFieldInputInstance';import type { NonNullableGraph } from 'services/api/types';import { v4 as uuidv4 } from 'uuid';@@ -104,6 +105,21 @@ export const graphToWorkflow = (graph: NonNullableGraph, autoLayout = true): Wor});});+ workflow.nodes.filter(isWorkflowInvocationNode).forEach((node) => {+ if (+ node.data.type === 'image_collection' &&+ node.data.inputs.collection &&+ !node.data.inputs.images &&+ templates.image_collection?.inputs.images+ ) {+ node.data.inputs.images = buildFieldInputInstance(node.id, templates.image_collection.inputs.images);+ }++ migrateImageCollectionInputValues(node, {+ connectedInputNames: getConnectedInputNames(node.id, workflow.edges),+ });+ });+if (autoLayout) {// Best-effort auto layout via dagre - not perfect but better than nothingconst dagreGraph = new dagre.graphlib.Graph();diff --git a/invokeai/frontend/web/src/features/nodes/util/workflow/validateWorkflow.test.ts b/invokeai/frontend/web/src/features/nodes/util/workflow/validateWorkflow.test.tsindex c1d0858831..b7ead12fa2 100644--- a/invokeai/frontend/web/src/features/nodes/util/workflow/validateWorkflow.test.ts+++ b/invokeai/frontend/web/src/features/nodes/util/workflow/validateWorkflow.test.ts@@ -1,11 +1,60 @@import { get } from 'es-toolkit/compat';+import { addElement } from 'features/nodes/components/sidePanel/builder/form-manipulation';import { CONNECTOR_INPUT_HANDLE, CONNECTOR_OUTPUT_HANDLE } from 'features/nodes/store/util/connectorTopology';import { img_resize, main_model_loader } from 'features/nodes/store/util/testUtils';+import type { InvocationTemplate } from 'features/nodes/types/invocation';import type { WorkflowV3 } from 'features/nodes/types/workflow';-import { getDefaultForm } from 'features/nodes/types/workflow';+import { buildNodeFieldElement, getDefaultForm, isNodeFieldElement } from 'features/nodes/types/workflow';import { validateWorkflow } from 'features/nodes/util/workflow/validateWorkflow';import { describe, expect, it } from 'vitest';+const imageCollectionTemplate = {+ title: 'Image Collection Primitive',+ type: 'image_collection',+ version: '1.0.2',+ tags: ['primitives', 'image', 'collection'],+ description: 'A collection of image primitive values',+ outputType: 'image_collection_output',+ inputs: {+ collection: {+ name: 'collection',+ title: 'Collection',+ required: false,+ description: 'An optional image collection to append to',+ fieldKind: 'input',+ input: 'connection',+ ui_hidden: false,+ type: { name: 'ImageField', cardinality: 'COLLECTION', batch: false },+ default: undefined,+ },+ images: {+ name: 'images',+ title: 'Images',+ required: false,+ description: 'The images to append to the collection',+ fieldKind: 'input',+ input: 'direct',+ ui_hidden: false,+ type: { name: 'ImageField', cardinality: 'COLLECTION', batch: false },+ default: undefined,+ },+ },+ outputs: {+ collection: {+ fieldKind: 'output',+ name: 'collection',+ title: 'Collection',+ description: 'The output images',+ type: { name: 'ImageField', cardinality: 'COLLECTION', batch: false },+ ui_hidden: false,+ },+ },+ useCache: true,+ nodePack: 'invokeai',+ classification: 'stable',+ category: 'primitives',+} satisfies InvocationTemplate;+//TODO(psyche): Test workflow validation for form builder fieldsdescribe('validateWorkflow', () => {const buildConnectorNode = (id: string) => ({@@ -106,6 +155,32 @@ describe('validateWorkflow', () => {new Promise((resolve) => {resolve(false);});+ const addLegacyImageCollectionNode = (workflow: WorkflowV3, id: string, images = [{ image_name: `${id}.png` }]) => {+ workflow.nodes.push({+ id,+ type: 'invocation',+ data: {+ id,+ type: 'image_collection',+ version: '1.0.1',+ label: '',+ notes: '',+ isOpen: true,+ isIntermediate: true,+ useCache: true,+ nodePack: 'invokeai',+ inputs: {+ collection: {+ name: 'collection',+ label: '',+ description: '',+ value: images,+ },+ },+ },+ position: { x: 0, y: 0 },+ });+ };it('should reset images that are inaccessible', async () => {const validationResult = await validateWorkflow({workflow: getWorkflow(),@@ -274,4 +349,85 @@ describe('validateWorkflow', () => {expect(validationResult.workflow.edges).toEqual([unresolvedEdge]);expect(validationResult.warnings).toEqual([]);});++ it('should migrate image_collection direct values when its old collection edge is invalid', async () => {+ const workflow = getWorkflow();+ const images = [{ image_name: 'legacy.png' }];+ addLegacyImageCollectionNode(workflow, 'image-collection', images);+ workflow.edges.push({+ id: 'missing-source-edge',+ type: 'default',+ source: 'missing-node',+ sourceHandle: 'collection',+ target: 'image-collection',+ targetHandle: 'collection',+ });++ const validationResult = await validateWorkflow({+ workflow,+ templates: { img_resize, main_model_loader, image_collection: imageCollectionTemplate },+ checkImageAccess: resolveTrue,+ checkBoardAccess: resolveTrue,+ checkModelAccess: resolveTrue,+ });++ expect(validationResult.workflow.edges).toEqual([]);+ expect(get(validationResult.workflow, 'nodes[2].data.inputs.images.value')).toEqual(images);+ expect(get(validationResult.workflow, 'nodes[2].data.inputs.collection.value')).toEqual([]);+ });++ it('should preserve image_collection collection values when its old collection edge is valid', async () => {+ const workflow = getWorkflow();+ const images = [{ image_name: 'shadowed.png' }];+ addLegacyImageCollectionNode(workflow, 'source-collection', []);+ addLegacyImageCollectionNode(workflow, 'target-collection', images);+ workflow.edges.push({+ id: 'valid-edge',+ type: 'default',+ source: 'source-collection',+ sourceHandle: 'collection',+ target: 'target-collection',+ targetHandle: 'collection',+ });++ const validationResult = await validateWorkflow({+ workflow,+ templates: { img_resize, main_model_loader, image_collection: imageCollectionTemplate },+ checkImageAccess: resolveTrue,+ checkBoardAccess: resolveTrue,+ checkModelAccess: resolveTrue,+ });++ expect(validationResult.workflow.edges).toHaveLength(1);+ expect(get(validationResult.workflow, 'nodes[3].data.inputs.images.value')).toBeUndefined();+ expect(get(validationResult.workflow, 'nodes[3].data.inputs.collection.value')).toEqual(images);+ });++ it('should remap image_collection form and exposed collection fields to images', async () => {+ const workflow = getWorkflow();+ const images = [{ image_name: 'legacy.png' }];+ addLegacyImageCollectionNode(workflow, 'image-collection', images);+ workflow.exposedFields = [{ nodeId: 'image-collection', fieldName: 'collection' }];+ const element = buildNodeFieldElement('image-collection', 'collection', {+ name: 'ImageField',+ cardinality: 'COLLECTION',+ batch: false,+ });+ addElement({ form: workflow.form, element, parentId: workflow.form.rootElementId });++ const validationResult = await validateWorkflow({+ workflow,+ templates: { img_resize, main_model_loader, image_collection: imageCollectionTemplate },+ checkImageAccess: resolveTrue,+ checkBoardAccess: resolveTrue,+ checkModelAccess: resolveTrue,+ });++ expect(validationResult.workflow.exposedFields).toEqual([{ nodeId: 'image-collection', fieldName: 'images' }]);+ const updatedElement = validationResult.workflow.form.elements[element.id];+ if (!updatedElement || !isNodeFieldElement(updatedElement)) {+ throw new Error('Expected a node field form element');+ }+ expect(updatedElement.data.fieldIdentifier.fieldName).toBe('images');+ });});diff --git a/invokeai/frontend/web/src/features/nodes/util/workflow/validateWorkflow.ts b/invokeai/frontend/web/src/features/nodes/util/workflow/validateWorkflow.tsindex 448214defe..741591dcdf 100644--- a/invokeai/frontend/web/src/features/nodes/util/workflow/validateWorkflow.ts+++ b/invokeai/frontend/web/src/features/nodes/util/workflow/validateWorkflow.ts@@ -16,7 +16,12 @@ import {isNodeFieldElement,isWorkflowInvocationNode,} from 'features/nodes/types/workflow';-import { getNeedsUpdate, updateNode } from 'features/nodes/util/node/nodeUpdate';+import {+ getConnectedInputNames,+ getNeedsUpdate,+ getUpdatedFieldName,+ updateNode,+} from 'features/nodes/util/node/nodeUpdate';import { t } from 'i18next';import type { JsonObject } from 'type-fest';@@ -58,6 +63,96 @@ export const validateWorkflow = async (args: ValidateWorkflowArgs): Promise<Vali// Now we can validate the graphconst { nodes, edges } = _workflow;const warnings: WorkflowWarning[] = [];+ const validEdges: WorkflowV3['edges'] = [];++ for (const edge of edges) {+ // Validate each edge. If the edge is invalid, we must remove it to prevent runtime errors with reactflow.+ const sourceNode = nodes.find(({ id }) => id === edge.source);+ const targetNode = nodes.find(({ id }) => id === edge.target);+ const sourceTemplate = sourceNode ? templates[sourceNode.data.type] : undefined;+ const targetTemplate = targetNode ? templates[targetNode.data.type] : undefined;+ const issues: string[] = [];++ if (!sourceNode) {+ // The edge's source/output node does not exist+ issues.push(+ t('nodes.sourceNodeDoesNotExist', {+ node: edge.source,+ })+ );+ }++ if (sourceNode?.type === 'invocation' && !sourceTemplate) {+ // The edge's source/output node template does not exist+ issues.push(+ t('nodes.missingTemplate', {+ node: edge.source,+ type: sourceNode?.data.type,+ })+ );+ }++ if (sourceNode && sourceTemplate && edge.type === 'default' && !(edge.sourceHandle in sourceTemplate.outputs)) {+ // The edge's source/output node field does not exist+ issues.push(+ t('nodes.sourceNodeFieldDoesNotExist', {+ node: edge.source,+ field: edge.sourceHandle,+ })+ );+ }++ if (!targetNode) {+ // The edge's target/input node does not exist+ issues.push(+ t('nodes.targetNodeDoesNotExist', {+ node: edge.target,+ })+ );+ }++ if (targetNode?.type === 'invocation' && !targetTemplate) {+ // The edge's target/input node template does not exist+ issues.push(+ t('nodes.missingTemplate', {+ node: edge.target,+ type: targetNode?.data.type,+ })+ );+ }++ if (targetNode && targetTemplate && edge.type === 'default' && !(edge.targetHandle in targetTemplate.inputs)) {+ // The edge's target/input node field does not exist+ issues.push(+ t('nodes.targetNodeFieldDoesNotExist', {+ node: edge.target,+ field: edge.targetHandle,+ })+ );+ }++ if (!issues.length && edge.type === 'default') {+ const connectionError = validateConnection(edge, nodes, validEdges, templates, null, true);+ if (connectionError) {+ issues.push(connectionError);+ }+ }++ if (issues.length) {+ const source = edge.type === 'default' ? `${edge.source}.${edge.sourceHandle}` : edge.source;+ const target = edge.type === 'default' ? `${edge.target}.${edge.targetHandle}` : edge.target;+ warnings.push({+ message: t('nodes.deletedInvalidEdge', { source, target }),+ issues,+ data: edge,+ });+ } else {+ validEdges.push(edge);+ }+ }++ // Remove invalid edges before node updates so migrations only trust surviving connections.+ _workflow.edges = validEdges;for (const node of nodes) {if (!isWorkflowInvocationNode(node)) {@@ -81,7 +176,8 @@ export const validateWorkflow = async (args: ValidateWorkflowArgs): Promise<Vali// This node needs to be updated, based on comparison of its version to the template versionif (getNeedsUpdate(node.data, template)) {try {- const updatedNode = updateNode(node, template);+ const connectedInputNames = getConnectedInputNames(node.id, validEdges);+ const updatedNode = updateNode(node, template, { connectedInputNames });node.data = updatedNode.data;} catch {const message = t('nodes.unableToUpdateNode', {@@ -150,97 +246,25 @@ export const validateWorkflow = async (args: ValidateWorkflowArgs): Promise<Vali}}- const validEdges = [];-- for (const edge of edges) {- // Validate each edge. If the edge is invalid, we must remove it to prevent runtime errors with reactflow.- const sourceNode = nodes.find(({ id }) => id === edge.source);- const targetNode = nodes.find(({ id }) => id === edge.target);- const sourceTemplate = sourceNode ? templates[sourceNode.data.type] : undefined;- const targetTemplate = targetNode ? templates[targetNode.data.type] : undefined;- const issues: string[] = [];-- if (!sourceNode) {- // The edge's source/output node does not exist- issues.push(- t('nodes.sourceNodeDoesNotExist', {- node: edge.source,- })- );- }-- if (sourceNode?.type === 'invocation' && !sourceTemplate) {- // The edge's source/output node template does not exist- issues.push(- t('nodes.missingTemplate', {- node: edge.source,- type: sourceNode?.data.type,- })- );- }-- if (sourceNode && sourceTemplate && edge.type === 'default' && !(edge.sourceHandle in sourceTemplate.outputs)) {- // The edge's source/output node field does not exist- issues.push(- t('nodes.sourceNodeFieldDoesNotExist', {- node: edge.source,- field: edge.sourceHandle,- })- );+ _workflow.exposedFields = _workflow.exposedFields.map((fieldIdentifier) => {+ const node = nodes.filter(isWorkflowInvocationNode).find(({ id }) => id === fieldIdentifier.nodeId);+ if (!node) {+ return fieldIdentifier;}+ return { ...fieldIdentifier, fieldName: getUpdatedFieldName(node, fieldIdentifier.fieldName) };+ });- if (!targetNode) {- // The edge's target/input node does not exist- issues.push(- t('nodes.targetNodeDoesNotExist', {- node: edge.target,- })- );- }-- if (targetNode?.type === 'invocation' && !targetTemplate) {- // The edge's target/input node template does not exist- issues.push(- t('nodes.missingTemplate', {- node: edge.target,- type: targetNode?.data.type,- })- );- }-- if (targetNode && targetTemplate && edge.type === 'default' && !(edge.targetHandle in targetTemplate.inputs)) {- // The edge's target/input node field does not exist- issues.push(- t('nodes.targetNodeFieldDoesNotExist', {- node: edge.target,- field: edge.targetHandle,- })- );- }-- if (!issues.length && edge.type === 'default') {- const connectionError = validateConnection(edge, nodes, validEdges, templates, null, true);- if (connectionError) {- issues.push(connectionError);- }+ for (const element of Object.values(_workflow.form.elements)) {+ if (!isNodeFieldElement(element)) {+ continue;}-- if (issues.length) {- const source = edge.type === 'default' ? `${edge.source}.${edge.sourceHandle}` : edge.source;- const target = edge.type === 'default' ? `${edge.source}.${edge.targetHandle}` : edge.target;- warnings.push({- message: t('nodes.deletedInvalidEdge', { source, target }),- issues,- data: edge,- });- } else {- validEdges.push(edge);+ const node = nodes.filter(isWorkflowInvocationNode).find(({ id }) => id === element.data.fieldIdentifier.nodeId);+ if (!node) {+ continue;}+ element.data.fieldIdentifier.fieldName = getUpdatedFieldName(node, element.data.fieldIdentifier.fieldName);}- // Remove invalid edges- _workflow.edges = validEdges;-// Migrated exposed fields to form elements if they exist and the form does not// Note: If the form is invalid per its zod schema, it will be reset to a default, empty form!if (_workflow.exposedFields.length > 0 && getIsFormEmpty(_workflow.form)) {diff --git a/invokeai/frontend/web/src/services/api/schema.ts b/invokeai/frontend/web/src/services/api/schema.tsindex 79462d702c..375b4957e6 100644--- a/invokeai/frontend/web/src/services/api/schema.ts+++ b/invokeai/frontend/web/src/services/api/schema.ts@@ -13818,10 +13818,16 @@ export type components = {use_cache?: boolean;/*** Collection- * @description The collection of image values+ * @description An optional image collection to append to* @default null*/collection?: components["schemas"]["ImageField"][] | null;+ /**+ * Images+ * @description The images to append to the collection+ * @default null+ */+ images?: components["schemas"]["ImageField"][] | null;/*** type* @default image_collectiondiff --git a/tests/app/invocations/test_image.py b/tests/app/invocations/test_image.pyindex 7c0036d3db..05d2cd32b7 100644--- a/tests/app/invocations/test_image.py+++ b/tests/app/invocations/test_image.py@@ -8,6 +8,7 @@ import torchfrom PIL import Image, ImageFilterfrom invokeai.app.invocations.image import ImageField, OklabUnsharpMaskInvocation, OklchImageHueAdjustmentInvocation+from invokeai.app.invocations.primitives import ImageCollectionInvocationfrom invokeai.backend.image_util.color_conversion import (linear_srgb_from_oklab,linear_srgb_from_oklch,@@ -47,6 +48,37 @@ def _max_abs_diff_uint8(left: Image.Image, right: Image.Image) -> int:return int(numpy.abs(left_arr - right_arr).max())+def test_image_collection_invocation_preserves_existing_collection_values() -> None:+ images = [ImageField(image_name="first"), ImageField(image_name="second")]++ output = ImageCollectionInvocation(collection=images).invoke(MagicMock())++ assert output.collection == images+++def test_image_collection_invocation_appends_direct_images_after_chained_collection() -> None:+ chained_images = [ImageField(image_name="chained")]+ direct_images = [ImageField(image_name="direct_1"), ImageField(image_name="direct_2")]++ output = ImageCollectionInvocation(collection=chained_images, images=direct_images).invoke(MagicMock())++ assert output.collection == [*chained_images, *direct_images]+++def test_image_collection_invocation_supports_empty_direct_images() -> None:+ chained_images = [ImageField(image_name="chained")]++ output = ImageCollectionInvocation(collection=chained_images, images=None).invoke(MagicMock())++ assert output.collection == chained_images+++def test_image_collection_invocation_outputs_empty_collection_when_inputs_are_empty() -> None:+ output = ImageCollectionInvocation(collection=None, images=None).invoke(MagicMock())++ assert output.collection == []++def test_oklab_unsharp_mask_invocation_preserves_alpha_and_sharpens_lightness_only() -> None:input_image = Image.new("RGBA", (3, 1))input_image.putdata(