Skip to content

Commit 7cee4e4

Browse files
feat(ui): add addEdgeToMetadata graph helper
1 parent 071c7c7 commit 7cee4e4

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

invokeai/frontend/web/src/features/nodes/util/graph/generation/Graph.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,21 @@ describe('Graph', () => {
522522
});
523523
});
524524

525+
describe('addEdgeToMetadata', () => {
526+
it('should add an edge to the metadata node', () => {
527+
const g = new Graph();
528+
const n1 = g.addNode({
529+
id: 'n1',
530+
type: 'img_resize',
531+
});
532+
g.upsertMetadata({ test: 'test' });
533+
g.addEdgeToMetadata(n1, 'width', 'width');
534+
const metadata = g._getMetadataNode();
535+
expect(g.getEdgesFrom(n1).length).toBe(1);
536+
expect(g.getEdgesTo(metadata as unknown as AnyInvocation).length).toBe(1);
537+
});
538+
});
539+
525540
describe('setMetadataReceivingNode', () => {
526541
it('should set the metadata receiving node', () => {
527542
const g = new Graph();

invokeai/frontend/web/src/features/nodes/util/graph/generation/Graph.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,21 @@ export class Graph {
372372
return metadataNode;
373373
}
374374

375+
/**
376+
* Adds an edge from a node to a metadata field. Use this when the metadata value is dynamic depending on a node.
377+
* @param fromNode The node to add an edge from
378+
* @param fromField The field of the node to add an edge from
379+
* @param metadataField The metadata field to add an edge to (will overwrite hard-coded metadata)
380+
* @returns
381+
*/
382+
addEdgeToMetadata<TFrom extends AnyInvocation>(
383+
fromNode: TFrom,
384+
fromField: OutputFields<TFrom>,
385+
metadataField: string
386+
): Edge {
387+
// @ts-expect-error `Graph` excludes `core_metadata` nodes due to its excessively wide typing
388+
return this.addEdge(fromNode, fromField, this._getMetadataNode(), metadataField);
389+
}
375390
/**
376391
* Set the node that should receive metadata. All other edges from the metadata node are deleted.
377392
* @param node The node to set as the receiving node

0 commit comments

Comments
 (0)