diff --git a/browser_tests/assets/subgraphs/subgraph-dynamic-combo-promoted-child.json b/browser_tests/assets/subgraphs/subgraph-dynamic-combo-promoted-child.json new file mode 100644 index 00000000000..ba7c8384343 --- /dev/null +++ b/browser_tests/assets/subgraphs/subgraph-dynamic-combo-promoted-child.json @@ -0,0 +1,229 @@ +{ + "id": "f1e2d3c4-0000-4000-8000-fe25800258ff", + "revision": 0, + "last_node_id": 67, + "last_link_id": 2, + "nodes": [ + { + "id": 1, + "type": "LoadImage", + "pos": [80, 120], + "size": [274, 314], + "flags": {}, + "order": 0, + "mode": 0, + "inputs": [], + "outputs": [ + { + "localized_name": "IMAGE", + "name": "IMAGE", + "type": "IMAGE", + "links": [1] + }, + { + "localized_name": "MASK", + "name": "MASK", + "type": "MASK", + "links": [] + } + ], + "properties": { + "cnr_id": "comfy-core", + "ver": "0.27.0", + "Node name for S&R": "LoadImage" + }, + "widgets_values": ["example.png", "image"] + }, + { + "id": 66, + "type": "7c0d9e16-1d55-4f2e-9a61-fe2580258001", + "pos": [460, 120], + "size": [280, 120], + "flags": {}, + "order": 1, + "mode": 0, + "inputs": [ + { + "localized_name": "image", + "name": "image", + "type": "IMAGE", + "link": 1 + }, + { + "localized_name": "resize_type.multiplier", + "name": "resize_type.multiplier", + "type": "FLOAT", + "label": "scale_multiplier", + "widget": { + "name": "resize_type.multiplier" + }, + "link": null + } + ], + "outputs": [ + { + "localized_name": "resized", + "name": "resized", + "type": "IMAGE", + "links": [2] + } + ], + "properties": { + "proxyWidgets": [["57", "resize_type.multiplier"]] + }, + "widgets_values": [4] + }, + { + "id": 67, + "type": "PreviewImage", + "pos": [820, 120], + "size": [210, 246], + "flags": {}, + "order": 2, + "mode": 0, + "inputs": [ + { + "localized_name": "images", + "name": "images", + "type": "IMAGE", + "link": 2 + } + ], + "outputs": [], + "properties": {}, + "widgets_values": [] + } + ], + "links": [ + [1, 1, 0, 66, 0, "IMAGE"], + [2, 66, 0, 67, 0, "IMAGE"] + ], + "groups": [], + "definitions": { + "subgraphs": [ + { + "id": "7c0d9e16-1d55-4f2e-9a61-fe2580258001", + "version": 1, + "state": { + "lastGroupId": 0, + "lastNodeId": 57, + "lastLinkId": 50, + "lastRerouteId": 0 + }, + "revision": 0, + "config": {}, + "name": "Resize Image Subgraph", + "inputNode": { + "id": -10, + "bounding": [420, 60, 190, 80] + }, + "outputNode": { + "id": -20, + "bounding": [980, 100, 128, 68] + }, + "inputs": [ + { + "id": "e981cdcd-e740-4932-91e0-06392d5ac526", + "name": "image", + "type": "IMAGE", + "linkIds": [45], + "localized_name": "image", + "pos": [587, 85] + }, + { + "id": "df3b8dbd-5649-4dbd-8551-c5cb254eafc5", + "name": "resize_type.multiplier", + "type": "FLOAT", + "linkIds": [50], + "label": "scale_multiplier", + "pos": [587, 105] + } + ], + "outputs": [ + { + "id": "517ebe35-9129-4f66-a88e-2fdca4c417cf", + "name": "resized", + "type": "IMAGE", + "linkIds": [24], + "localized_name": "resized", + "pos": [1000, 130] + } + ], + "widgets": [], + "nodes": [ + { + "id": 57, + "type": "ResizeImageMaskNode", + "pos": [700, 120], + "size": [270, 106], + "flags": {}, + "order": 0, + "mode": 0, + "inputs": [ + { + "localized_name": "input", + "name": "input", + "type": "IMAGE,MASK", + "link": 45 + }, + { + "localized_name": "resize_type.multiplier", + "name": "resize_type.multiplier", + "type": "FLOAT", + "widget": { + "name": "resize_type.multiplier" + }, + "link": 50 + } + ], + "outputs": [ + { + "localized_name": "resized", + "name": "resized", + "type": "IMAGE", + "links": [24] + } + ], + "properties": { + "cnr_id": "comfy-core", + "ver": "0.27.0", + "Node name for S&R": "ResizeImageMaskNode" + }, + "widgets_values": ["scale by multiplier", 4, "lanczos"] + } + ], + "groups": [], + "links": [ + { + "id": 45, + "origin_id": -10, + "origin_slot": 0, + "target_id": 57, + "target_slot": 0, + "type": "IMAGE" + }, + { + "id": 50, + "origin_id": -10, + "origin_slot": 1, + "target_id": 57, + "target_slot": 1, + "type": "FLOAT" + }, + { + "id": 24, + "origin_id": 57, + "origin_slot": 0, + "target_id": -20, + "target_slot": 0, + "type": "IMAGE" + } + ], + "extra": {} + } + ] + }, + "config": {}, + "extra": {}, + "version": 0.4 +} diff --git a/browser_tests/tests/subgraph/subgraphPromotedDynamicComboChild.spec.ts b/browser_tests/tests/subgraph/subgraphPromotedDynamicComboChild.spec.ts new file mode 100644 index 00000000000..a02072ecd8c --- /dev/null +++ b/browser_tests/tests/subgraph/subgraphPromotedDynamicComboChild.spec.ts @@ -0,0 +1,55 @@ +import { expect } from '@playwright/test' + +import { comfyPageFixture as test } from '@e2e/fixtures/ComfyPage' +import { getInputNames } from '@e2e/fixtures/utils/nodeInputLinks' +import { getPromotedWidgetNames } from '@e2e/fixtures/utils/promotedWidgets' + +// Reduced from the `utility_seedvr2_3b_int8_upscale_image` template (FE-258): +// a `ResizeImageMaskNode` whose `resize_type` dynamic combo is set to a +// non-default option, with that option's `multiplier` child widget promoted +// through the subgraph boundary. +test.describe( + 'Promoted dynamic combo child widget', + { tag: ['@subgraph', '@widget', '@vue-nodes'] }, + () => { + test.beforeEach(async ({ comfyPage }) => { + await comfyPage.workflow.loadWorkflow( + 'subgraphs/subgraph-dynamic-combo-promoted-child' + ) + }) + + test('survives a workflow load', async ({ comfyPage }) => { + const multiplier = comfyPage.vueNodes.getWidgetRowByLabel( + 'Resize Image Subgraph', + 'scale_multiplier' + ) + await expect(multiplier).toBeVisible() + await expect( + comfyPage.vueNodes.getInputNumberControls(multiplier).input + ).toHaveValue('4.00') + + await expect + .poll(() => getPromotedWidgetNames(comfyPage, '66')) + .toEqual(['resize_type.multiplier']) + + await expect + .poll(() => getInputNames(comfyPage, '66', '')) + .toEqual(['image', 'resize_type.multiplier']) + }) + + test('keeps the links of the nodes around it', async ({ comfyPage }) => { + await expect + .poll(() => + comfyPage.page.evaluate(() => + [...window.app!.graph.links.values()] + .map( + (link) => + `${link.origin_id}:${link.origin_slot}->${link.target_id}:${link.target_slot}` + ) + .sort() + ) + ) + .toEqual(['1:0->66:0', '66:0->67:0']) + }) + } +) diff --git a/src/core/graph/widgets/dynamicWidgets.test.ts b/src/core/graph/widgets/dynamicWidgets.test.ts index c56f722d55e..a2294b77611 100644 --- a/src/core/graph/widgets/dynamicWidgets.test.ts +++ b/src/core/graph/widgets/dynamicWidgets.test.ts @@ -6,8 +6,13 @@ import { addDynamicCombo } from '@/core/graph/widgets/__fixtures__/dynamicInputHelpers' import { LGraph, LGraphNode, LiteGraph } from '@/lib/litegraph/src/litegraph' +import { realignGroupWidgetChildLinks } from '@/lib/litegraph/src/linkDeduplication' +import type { SerialisableGraph } from '@/lib/litegraph/src/types/serialisation' +import type { ComfyNodeDef as ComfyNodeDefV1 } from '@/schemas/nodeDefSchema' import { useLitegraphService } from '@/services/litegraphService' import { useLinkStore } from '@/stores/linkStore' +import { toLinkId } from '@/types/linkId' +import { toNodeId } from '@/types/nodeId' setActivePinia(createTestingPinia({ stubActions: false })) beforeEach(() => setActivePinia(createTestingPinia({ stubActions: false }))) @@ -457,3 +462,304 @@ describe('Autogrow', () => { } }) }) + +const RESIZE_NODE_TYPE = 'test/ResizeImageMask' +const SOURCE_NODE_TYPE = 'test/MultiplierSource' + +class SourceNode extends LGraphNode { + constructor(title?: string) { + super(title ?? 'Source') + this.addOutput('out', 'FLOAT') + } +} + +/** + * A reduced `ResizeImageMaskNode` (FE-258): a dynamic combo whose default + * option lays out a `width` child, and whose other option lays out a + * `multiplier` child instead. + */ +const resizeNodeDef: ComfyNodeDefV1 = { + name: RESIZE_NODE_TYPE, + display_name: 'Resize Image Mask', + category: 'testing', + python_module: 'nodes', + description: '', + input: { + required: { + image: ['IMAGE', {}], + resize_type: [ + 'COMFY_DYNAMICCOMBO_V3', + { + options: [ + { + key: 'scale dimensions', + inputs: { required: { width: ['INT', {}] } } + }, + { + key: 'scale by multiplier', + inputs: { required: { multiplier: ['FLOAT', {}] } } + } + ] + } + ] + } + }, + output: ['IMAGE'], + output_name: ['resized'], + output_node: false +} + +/** + * The node saved on `scale by multiplier`, so its serialized inputs carry + * `resize_type.multiplier` — a child the node definition does not lay out — + * on the slot the definition gives to `resize_type.width`. + */ +function savedDynamicComboChildWorkflow(): SerialisableGraph { + return { + id: 'ab000000-0000-4000-8000-00000000f258', + version: 1, + revision: 0, + state: { lastNodeId: 2, lastLinkId: 1, lastGroupId: 0, lastRerouteId: 0 }, + nodes: [ + { + id: 1, + type: SOURCE_NODE_TYPE, + pos: [0, 0], + size: [140, 60], + flags: {}, + order: 0, + mode: 0, + inputs: [], + outputs: [{ name: 'out', type: 'FLOAT', links: [1] }], + properties: {} + }, + { + id: 2, + type: RESIZE_NODE_TYPE, + pos: [300, 0], + size: [200, 120], + flags: {}, + order: 1, + mode: 0, + inputs: [ + { name: 'image', type: 'IMAGE', link: null }, + { name: 'resize_type.multiplier', type: 'FLOAT', link: 1 } + ], + outputs: [{ name: 'resized', type: 'IMAGE', links: [] }], + properties: {}, + widgets_values: ['scale by multiplier', 4] + } + ], + links: [ + { + id: toLinkId(1), + origin_id: 1, + origin_slot: 0, + target_id: 2, + target_slot: 1, + type: 'FLOAT' + } + ] + } +} + +describe('Dynamic combo child links on workflow load (FE-258)', () => { + beforeEach(async () => { + LiteGraph.registerNodeType(SOURCE_NODE_TYPE, SourceNode) + await useLitegraphService().registerNodeDef(RESIZE_NODE_TYPE, resizeNodeDef) + }) + + test('keeps the link on the child the selected option lays out', () => { + const graph = new LGraph() + graph.configure(savedDynamicComboChildWorkflow()) + + const target = graph.getNodeById(toNodeId(2)) + assert.ok(target, 'configured target node') + const multiplierSlot = target.inputs.findIndex( + (input) => input.name === 'resize_type.multiplier' + ) + expect({ + inputNames: target.inputs.map((input) => input.name), + multiplierLinkId: target.getInputLink(multiplierSlot)?.id + }).toEqual({ + inputNames: ['image', 'resize_type', 'resize_type.multiplier'], + multiplierLinkId: toLinkId(1) + }) + }) +}) + +const REFERENCE_NODE_TYPE = 'test/AutogrowInsideCombo' + +/** + * Shaped after `ByteDance2ReferenceNode`: a dynamic combo whose option holds + * both an ordinary child widget and an autogrow group, so the group's children + * are named `model.reference_images.` and its registry key is + * `model.reference_images`. + */ +const referenceNodeDef: ComfyNodeDefV1 = { + name: REFERENCE_NODE_TYPE, + display_name: 'Autogrow Inside Combo', + category: 'testing', + python_module: 'nodes', + description: '', + input: { + required: { + model: [ + 'COMFY_DYNAMICCOMBO_V3', + { + options: [ + { + key: 'Seedance', + inputs: { + required: { + generate_audio: ['BOOLEAN', { default: true }], + reference_images: [ + 'COMFY_AUTOGROW_V3', + { + template: { + input: { + required: { reference_image: ['IMAGE', {}] } + }, + names: ['image_1', 'image_2', 'image_3'], + min: 2 + } + } + ] + } + } + } + ] + } + ] + } + }, + output: ['VIDEO'], + output_name: ['VIDEO'], + output_node: false +} + +describe('Autogrow nested inside a group widget (FE-258)', () => { + beforeEach(async () => { + await useLitegraphService().registerNodeDef( + REFERENCE_NODE_TYPE, + referenceNodeDef + ) + }) + + test('leaves an autogrow child link where the group put it', () => { + const graph = new LGraph() + const node = LiteGraph.createNode(REFERENCE_NODE_TYPE) + assert.ok(node, 'reference node') + graph.add(node) + const slotOf = (name: string) => + node.inputs.findIndex((input) => input.name === name) + const connectedSlot = slotOf('model.reference_images.image_1') + const nested = connectInput(node, connectedSlot, graph) + + realignGroupWidgetChildLinks(node, { + id: node.id, + inputs: node.inputs.map((input) => ({ + name: input.name, + type: String(input.type), + link: input.name === 'model.reference_images.image_3' ? nested.id : null + })) + }) + + expect(nested.target_slot).toBe(connectedSlot) + }) +}) + +const GROWN_NODE_TYPE = 'test/AutogrowBeforeOrdinaryChild' + +/** + * Shaped after `OpenAIGPTImageNodeV2`: a dynamic combo option whose autogrow + * group is followed by an ordinary child input. Reloading such a node replays + * both links, and the group grows a slot while doing so. + */ +const grownNodeDef: ComfyNodeDefV1 = { + name: GROWN_NODE_TYPE, + display_name: 'Autogrow Before Ordinary Child', + category: 'testing', + python_module: 'nodes', + description: '', + input: { + required: { + model: [ + 'COMFY_DYNAMICCOMBO_V3', + { + options: [ + { + key: 'gpt-image-1', + inputs: { + required: { + seed: ['INT', { default: 0 }], + images: [ + 'COMFY_AUTOGROW_V3', + { + template: { + input: { required: { image: ['IMAGE', {}] } }, + names: ['image_1', 'image_2', 'image_3', 'image_4'], + min: 0 + } + } + ], + mask: ['MASK', { forceInput: true }] + } + } + } + ] + } + ] + } + }, + output: ['IMAGE'], + output_name: ['IMAGE'], + output_node: false +} + +class ImageMaskSourceNode extends LGraphNode { + constructor(title?: string) { + super(title ?? 'ImageMaskSource') + this.addOutput('image', 'IMAGE') + this.addOutput('mask', 'MASK') + } +} + +describe('Autogrow followed by an ordinary combo child (FE-258)', () => { + beforeEach(async () => { + LiteGraph.registerNodeType('test/ImageMaskSource', ImageMaskSourceNode) + await useLitegraphService().registerNodeDef(GROWN_NODE_TYPE, grownNodeDef) + }) + + test('keeps the autogrow slot count across a load', () => { + const graph = new LGraph() + const source = new ImageMaskSourceNode() + graph.add(source) + const node = LiteGraph.createNode(GROWN_NODE_TYPE) + assert.ok(node, 'gpt image like node') + graph.add(node) + const slotOf = (name: string) => + node.inputs.findIndex((input) => input.name === name) + source.connect(0, node, slotOf('model.images.image_1')) + source.connect(1, node, slotOf('model.mask')) + const inputNames = (target: LGraphNode) => + target.inputs.map( + (input, slot) => `${input.name}${target.getInputLink(slot) ? '*' : ''}` + ) + const before = inputNames(node).filter((name) => + name.startsWith('model.images.') + ) + + const reloaded = new LGraph() + reloaded.configure(structuredClone(graph.serialize())) + + const reloadedNode = reloaded.getNodeById(node.id) + assert.ok(reloadedNode, 'reloaded node') + expect({ + images: inputNames(reloadedNode).filter((name) => + name.startsWith('model.images.') + ), + maskConnected: inputNames(reloadedNode).includes('model.mask*') + }).toEqual({ images: before, maskConnected: true }) + }) +}) diff --git a/src/core/graph/widgets/dynamicWidgets.ts b/src/core/graph/widgets/dynamicWidgets.ts index 7e595248de2..5bfb0c02841 100644 --- a/src/core/graph/widgets/dynamicWidgets.ts +++ b/src/core/graph/widgets/dynamicWidgets.ts @@ -247,7 +247,11 @@ function dynamicComboWidget( } const result = commitMutatedInputs(node, previous, inputLinks) if (!result.ok) return - for (const { input, link, slot } of result.replacements) { + //A callback can grow the group it lands on, shifting every input after + //it, so the slot captured before the batch is stale for later entries. + for (const { input, link } of result.replacements) { + const slot = node.inputs.indexOf(input) + if (slot === -1) continue node.onConnectionsChange?.(LiteGraph.INPUT, slot, true, link, input) } restoreRemovedValues(value, addedWidgetNames) diff --git a/src/lib/litegraph/src/LGraph.inputSlotRealign.test.ts b/src/lib/litegraph/src/LGraph.inputSlotRealign.test.ts index 79b547b917c..e3751518470 100644 --- a/src/lib/litegraph/src/LGraph.inputSlotRealign.test.ts +++ b/src/lib/litegraph/src/LGraph.inputSlotRealign.test.ts @@ -10,6 +10,7 @@ import { import { LGraph, LGraphNode, LiteGraph } from '@/lib/litegraph/src/litegraph' import { normalizeConfiguredTopology, + realignGroupWidgetChildLinks, realignInputLinkSlots } from '@/lib/litegraph/src/linkDeduplication' import type { @@ -796,3 +797,97 @@ describe('realignInputLinkSlots', () => { expect(movable.target_slot).toBe(4) }) }) + +describe('realignGroupWidgetChildLinks (FE-258)', () => { + function groupWidgetSetup(inputNames: string[], groupWidgetName: string) { + const graph = new LGraph() + const source = new LGraphNode('Source') + source.addOutput('out', 'number') + const target = new LGraphNode('Target') + for (const name of inputNames) target.addInput(name, 'number') + target.addWidget('combo', groupWidgetName, 'scale dimensions', () => {}, { + values: ['scale dimensions', 'scale by multiplier'] + }) + graph.add(source) + graph.add(target) + return { graph, source, target } + } + + function serializedInputs( + target: LGraphNode, + links: Record + ) { + return target.inputs.map((input) => ({ + name: input.name, + type: 'number', + link: links[input.name] ?? null + })) + } + + it('moves a child link off the slot the default option laid out', () => { + const { graph, source, target } = groupWidgetSetup( + ['input', 'resize_type.width', 'resize_type', 'resize_type.multiplier'], + 'resize_type' + ) + const link = source.connect(0, target, 1)! + + realignGroupWidgetChildLinks(target, { + id: target.id, + inputs: serializedInputs(target, { 'resize_type.multiplier': link.id }) + }) + + expect(link.target_slot).toBe(3) + expect( + useLinkStore().getInputSlotLink(graphScopeOf(graph), target.id, 3)?.id + ).toBe(link.id) + }) + + it.for([ + { label: 'a plain ordinary', ordinaryName: 'roll' }, + { label: 'a dotted ordinary', ordinaryName: 'metadata.scale' } + ])( + 'moves $label input link that holds a child destination slot', + ({ ordinaryName }) => { + const { source, target } = groupWidgetSetup( + [ + 'image', + 'resize_type.width', + 'resize_type', + ordinaryName, + 'resize_type.multiplier' + ], + 'resize_type' + ) + const child = source.connect(0, target, 1)! + const ordinary = source.connect(0, target, 4)! + + realignGroupWidgetChildLinks(target, { + id: target.id, + inputs: serializedInputs(target, { + 'resize_type.multiplier': child.id, + [ordinaryName]: ordinary.id + }) + }) + + expect({ + child: child.target_slot, + ordinary: ordinary.target_slot + }).toEqual({ child: 4, ordinary: 3 }) + } + ) + + it('leaves a node that has no group widget child input', () => { + const { source, target } = groupWidgetSetup( + ['first', 'second', 'resize_type'], + 'resize_type' + ) + const link = source.connect(0, target, 0)! + + realignGroupWidgetChildLinks(target, { + id: target.id, + inputs: serializedInputs(target, { second: link.id }) + }) + + expect(link.target_slot).toBe(0) + }) +}) diff --git a/src/lib/litegraph/src/LGraphNode.ts b/src/lib/litegraph/src/LGraphNode.ts index a69b254599c..99c25617bf6 100644 --- a/src/lib/litegraph/src/LGraphNode.ts +++ b/src/lib/litegraph/src/LGraphNode.ts @@ -50,6 +50,7 @@ import { badgeDrawObjects, badgeRows } from './nodeBadgeDraw' import { LGraphButton } from './LGraphButton' import type { LGraphButtonOptions } from './LGraphButton' import { LGraphCanvas } from './LGraphCanvas' +import { realignGroupWidgetChildLinks } from './linkDeduplication' import { LLink, replaceLinkTopology, slotFloatingLinks } from './LLink' import { inputHasLink, @@ -1160,6 +1161,8 @@ export class LGraphNode // SubgraphNode callback. this._internalConfigureAfterSlots?.() + realignGroupWidgetChildLinks(this, info) + const restoration = createWidgetRestorationState( info, this.constructor.nodeData?.fallbackWidgetsValuesNames diff --git a/src/lib/litegraph/src/linkDeduplication.ts b/src/lib/litegraph/src/linkDeduplication.ts index e89dd40da9c..f594cae4c93 100644 --- a/src/lib/litegraph/src/linkDeduplication.ts +++ b/src/lib/litegraph/src/linkDeduplication.ts @@ -7,6 +7,7 @@ import { toNodeId } from '@/types/nodeId' import type { NodeId } from '@/types/nodeId' import cloneDeep from 'es-toolkit/compat/cloneDeep' import type { LGraph } from './LGraph' +import type { LGraphNode } from './LGraphNode' import type { LinkId, LLink, SerialisedLLinkArray } from './LLink' import type { ExportedSubgraph, @@ -143,6 +144,87 @@ export function detachSerialisedLinks( return linkByInputName } +function groupNameOf(inputName: string): string | undefined { + const separator = inputName ? inputName.lastIndexOf('.') : -1 + return separator < 1 ? undefined : inputName.slice(0, separator) +} + +/** + * Whether a group widget on `node` owns `inputName`'s slot. Group widgets + * (dynamic combos) name their child inputs `.` and + * replace the whole set whenever their own value changes. + */ +function isGroupWidgetChildInput(node: LGraphNode, inputName: string): boolean { + const groupName = groupNameOf(inputName) + if (groupName === undefined) return false + + return node.widgets?.some((widget) => widget.name === groupName) ?? false +} + +/** + * Whether a registered autogrow group owns `inputName`'s slot. + * + * Autogrow groups grow and renumber their own slots while the graph + * configures, and realigning their links by name that early destroys them + * (see `browser_tests/tests/subgraph/subgraphConvertAutogrowInputs.spec.ts`, + * "loads with both reference images connected"). + * + * Ownership comes from the registry `applyAutogrow` populates, under the same + * key autogrow's own connection handler resolves a slot's group by. A group + * is not a widget, so there is no widget name to match a prefix against as + * {@link isGroupWidgetChildInput} does, and the dotted name alone will not + * serve: `INodeInputSlot.name` is an arbitrary string, so an ordinary input + * may be dotted without belonging to any group. + * + * The registry only covers groups the selected option laid out. Children of + * an option that is not selected still reach this filter, because + * `ComfyNode.configure` appends every serialized input the definition lacks. + * Realigning those is harmless: their group's handler bails on the same + * missing key, so nothing renumbers behind the move. + */ +function isAutogrowGroupInput(node: LGraphNode, inputName: string): boolean { + const groupName = groupNameOf(inputName) + if (groupName === undefined) return false + + const autogrowGroups = node.comfyDynamic?.autogrow + return ( + autogrowGroups !== undefined && Object.hasOwn(autogrowGroups, groupName) + ) +} + +/** + * Realigns a node's input links by name before its group widget values are + * applied, for nodes that have a group widget child input. + * + * Applying a group widget's value rebuilds every child input of the group and + * hands each surviving link to the new input of the same name. A link sitting + * on the wrong slot — the node definition lays out the default option's + * children, while `target_slot` counts the serialized layout — is handed to an + * input that the selected option does not define, and is dropped. Through a + * subgraph boundary that demotes the promoted widget to a disconnected input + * slot. + * + * The node's ordinary inputs join the batch so that a link still occupying a + * child's destination slot is moved in the same atomic update instead of + * blocking it. Links owned by an autogrow group are left to + * {@link LGraph.configure}'s final pass. + */ +export function realignGroupWidgetChildLinks( + node: LGraphNode, + nodeData: Pick +): void { + const { graph } = node + if (!graph) return + + const inputs = nodeData.inputs?.filter( + (input) => !isAutogrowGroupInput(node, input.name) + ) + if (!inputs?.some((input) => isGroupWidgetChildInput(node, input.name))) + return + + realignInputLinkSlots(graph, [[node.id, { id: nodeData.id, inputs }]]) +} + /** * Re-points each link's `target_slot` at the configured input with the same * name as the serialized input that references it. Replays moved connections