diff --git a/invokeai/frontend/web/src/features/nodes/util/graph/generation/Graph.test.ts b/invokeai/frontend/web/src/features/nodes/util/graph/generation/Graph.test.ts index a8be96e484..e5a97bb50b 100644 --- a/invokeai/frontend/web/src/features/nodes/util/graph/generation/Graph.test.ts +++ b/invokeai/frontend/web/src/features/nodes/util/graph/generation/Graph.test.ts @@ -522,6 +522,21 @@ describe('Graph', () => { }); }); + describe('addEdgeToMetadata', () => { + it('should add an edge to the metadata node', () => { + const g = new Graph(); + const n1 = g.addNode({ + id: 'n1', + type: 'img_resize', + }); + g.upsertMetadata({ test: 'test' }); + g.addEdgeToMetadata(n1, 'width', 'width'); + const metadata = g._getMetadataNode(); + expect(g.getEdgesFrom(n1).length).toBe(1); + expect(g.getEdgesTo(metadata as unknown as AnyInvocation).length).toBe(1); + }); + }); + describe('setMetadataReceivingNode', () => { it('should set the metadata receiving node', () => { const g = new Graph(); diff --git a/invokeai/frontend/web/src/features/nodes/util/graph/generation/Graph.ts b/invokeai/frontend/web/src/features/nodes/util/graph/generation/Graph.ts index 008f86918a..41142e5628 100644 --- a/invokeai/frontend/web/src/features/nodes/util/graph/generation/Graph.ts +++ b/invokeai/frontend/web/src/features/nodes/util/graph/generation/Graph.ts @@ -372,6 +372,21 @@ export class Graph { return metadataNode; } + /** + * Adds an edge from a node to a metadata field. Use this when the metadata value is dynamic depending on a node. + * @param fromNode The node to add an edge from + * @param fromField The field of the node to add an edge from + * @param metadataField The metadata field to add an edge to (will overwrite hard-coded metadata) + * @returns + */ + addEdgeToMetadata( + fromNode: TFrom, + fromField: OutputFields, + metadataField: string + ): Edge { + // @ts-expect-error `Graph` excludes `core_metadata` nodes due to its excessively wide typing + return this.addEdge(fromNode, fromField, this._getMetadataNode(), metadataField); + } /** * Set the node that should receive metadata. All other edges from the metadata node are deleted. * @param node The node to set as the receiving node