diff --git a/src/core/graph/widgets/dynamicWidgets.test.ts b/src/core/graph/widgets/dynamicWidgets.test.ts index 2454553508b..a2cdcbb77b6 100644 --- a/src/core/graph/widgets/dynamicWidgets.test.ts +++ b/src/core/graph/widgets/dynamicWidgets.test.ts @@ -7,6 +7,7 @@ import { test, vi } from 'vitest' +import type { z } from 'zod' import { useChainCallback } from '@/composables/functional/useChainCallback' import { addAutogrow, @@ -16,7 +17,12 @@ import { liveAutogrowGroupOf } from '@/core/graph/widgets/dynamicWidgets' 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 type { + ComfyInputsSpec, + ComfyNodeDef as ComfyNodeDefV1, + zAutogrowOptions, + zDynamicComboInputSpec +} from '@/schemas/nodeDefSchema' import { useLitegraphService } from '@/services/litegraphService' import { useLinkStore } from '@/stores/linkStore' import { toLinkId } from '@/types/linkId' @@ -668,41 +674,134 @@ class SourceNode extends LGraphNode { } } +type ChildInputs = NonNullable + +type DynamicComboInputSpec = z.infer + +type AutogrowInputSpec = [ + type: 'COMFY_AUTOGROW_V3', + options: z.infer +] + +type ComboOption = [key: string, childInputs: ChildInputs] + +function dynamicCombo( + defaultOption: ComboOption, + ...remainingOptions: ComboOption[] +): DynamicComboInputSpec { + const options = [defaultOption, ...remainingOptions] + const declaredKeys = options.map(([key]) => key) + const realizedKeys = Object.keys( + Object.fromEntries(declaredKeys.map((key) => [key, null])) + ) + if (realizedKeys.length !== declaredKeys.length) + throw new Error( + `Duplicate option keys: ${declaredKeys.filter((key, index) => declaredKeys.indexOf(key) !== index).join(', ')}` + ) + if (realizedKeys.some((key, index) => key !== declaredKeys[index])) + throw new Error( + `Applying the combo would order options as [${realizedKeys.join(', ')}] instead of [${declaredKeys.join(', ')}].` + ) + + return [ + 'COMFY_DYNAMICCOMBO_V3', + { + options: options.map(([key, inputs]) => ({ + key, + inputs: { required: inputs } + })) + } + ] +} + +describe('dynamicCombo fixture builder', () => { + function option(key: string): ComboOption { + return [key, {}] + } + + const rejectedCases: { + name: string + options: [ComboOption, ...ComboOption[]] + message: RegExp + }[] = [ + { + name: 'a numeric key after a named one', + options: [option('Seedance'), option('0')], + message: /\[0, Seedance\] instead of \[Seedance, 0\]/ + }, + { + name: 'numeric keys out of ascending order', + options: [option('0'), option('2'), option('1')], + message: /\[0, 1, 2\] instead of \[0, 2, 1\]/ + }, + { + name: 'a key declared twice', + options: [option('a'), option('b'), option('a')], + message: /Duplicate option keys: a/ + } + ] + + test.for(rejectedCases)('rejects $name', ({ options, message }) => { + expect(() => dynamicCombo(...options)).toThrow(message) + }) + + test('accepts a numeric key too large to be an array index', () => { + expect(() => + dynamicCombo(option('0'), option('Seedance'), option('4294967295')) + ).not.toThrow() + }) +}) + +function autogrow(template: { + names: string[] + min: number + input: ChildInputs +}): AutogrowInputSpec { + const { names, min, input } = template + return [ + 'COMFY_AUTOGROW_V3', + { template: { input: { required: input }, names, min } } + ] +} + +function testNodeDef( + name: string, + displayName: string, + required: ChildInputs, + output: string[], + outputName: string[] = [...output] +): ComfyNodeDefV1 { + return { + name, + display_name: displayName, + category: 'testing', + python_module: 'nodes', + description: '', + input: { required }, + output, + output_name: outputName, + output_node: false + } +} + /** * 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', {}] } } - } - ] - } - ] - } +const resizeNodeDef = testNodeDef( + RESIZE_NODE_TYPE, + 'Resize Image Mask', + { + image: ['IMAGE', {}], + resize_type: dynamicCombo( + ['scale dimensions', { width: ['INT', {}] }], + ['scale by multiplier', { multiplier: ['FLOAT', {}] }] + ) }, - output: ['IMAGE'], - output_name: ['resized'], - output_node: false -} + ['IMAGE'], + ['resized'] +) /** * The node saved on `scale by multiplier`, so its serialized inputs carry @@ -764,6 +863,18 @@ describe('Dynamic combo child links on workflow load (FE-258)', () => { await useLitegraphService().registerNodeDef(RESIZE_NODE_TYPE, resizeNodeDef) }) + test('lays out the default option, which the saved workflow does not select', () => { + const graph = new LGraph() + const node = LiteGraph.createNode(RESIZE_NODE_TYPE) + assert.ok(node, 'resize node') + graph.add(node) + + const children = node.inputs + .map((input) => input.name) + .filter((name) => name.startsWith('resize_type.')) + expect(children).toEqual(['resize_type.width']) + }) + test('keeps the link on the child the selected option lays out', () => { const graph = new LGraph() graph.configure(savedDynamicComboChildWorkflow()) @@ -791,47 +902,24 @@ const REFERENCE_NODE_TYPE = 'test/AutogrowInsideCombo' * 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 - } - } - ] - } - } - } - ] - } - ] - } +const referenceNodeDef = testNodeDef( + REFERENCE_NODE_TYPE, + 'Autogrow Inside Combo', + { + model: dynamicCombo([ + 'Seedance', + { + generate_audio: ['BOOLEAN', { default: true }], + reference_images: autogrow({ + names: ['image_1', 'image_2', 'image_3'], + min: 2, + input: { reference_image: ['IMAGE', {}] } + }) + } + ]) }, - output: ['VIDEO'], - output_name: ['VIDEO'], - output_node: false -} + ['VIDEO'] +) describe('Autogrow nested inside a group widget (FE-258)', () => { beforeEach(async () => { @@ -871,46 +959,25 @@ const GROWN_NODE_TYPE = 'test/AutogrowBeforeOrdinaryChild' * 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 }] - } - } - } - ] - } - ] - } +const grownNodeDef = testNodeDef( + GROWN_NODE_TYPE, + 'Autogrow Before Ordinary Child', + { + model: dynamicCombo([ + 'gpt-image-1', + { + seed: ['INT', { default: 0 }], + images: autogrow({ + names: ['image_1', 'image_2', 'image_3', 'image_4'], + min: 0, + input: { image: ['IMAGE', {}] } + }), + mask: ['MASK', { forceInput: true }] + } + ]) }, - output: ['IMAGE'], - output_name: ['IMAGE'], - output_node: false -} + ['IMAGE'] +) class ImageMaskSourceNode extends LGraphNode { constructor(title?: string) { diff --git a/src/core/graph/widgets/dynamicWidgets.ts b/src/core/graph/widgets/dynamicWidgets.ts index 0e6b23d50e9..b99f9892ac7 100644 --- a/src/core/graph/widgets/dynamicWidgets.ts +++ b/src/core/graph/widgets/dynamicWidgets.ts @@ -243,8 +243,8 @@ function dynamicComboWidget( } const result = commitMutatedInputs(node, previous, inputLinks) if (!result.ok) return - //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. + //A callback can grow the group it lands on, so slots captured before the + //batch are stale for every entry after it. for (const { input, link } of result.replacements) { const slot = node.inputs.indexOf(input) if (slot === -1) continue diff --git a/src/lib/litegraph/src/linkDeduplication.ts b/src/lib/litegraph/src/linkDeduplication.ts index be0359220f7..53ac5d98b9d 100644 --- a/src/lib/litegraph/src/linkDeduplication.ts +++ b/src/lib/litegraph/src/linkDeduplication.ts @@ -182,11 +182,6 @@ function groupNameOf(inputName: string): string | undefined { 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 @@ -195,25 +190,17 @@ function isGroupWidgetChildInput(node: LGraphNode, inputName: string): boolean { } /** - * 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"). + * Autogrow groups renumber their own slots as the graph configures, so their + * links belong to {@link LGraph.configure}'s final pass rather than to an + * early realignment, which destroys them. A group is not a widget, and the + * dotted name alone will not serve — `INodeInputSlot.name` is arbitrary, so an + * ordinary input may be dotted without belonging to any group — so ownership + * comes from the registry `applyAutogrow` populates. * - * 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. + * That registry only covers groups the selected option laid out. Children of + * an unselected option still reach this filter, because `ComfyNode.configure` + * appends every serialized input the definition lacks. Realigning those is + * safe: their group's handler bails on the same missing key. */ function isAutogrowGroupInput(node: LGraphNode, inputName: string): boolean { const groupName = groupNameOf(inputName) @@ -226,21 +213,13 @@ function isAutogrowGroupInput(node: LGraphNode, inputName: string): boolean { } /** - * 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. + * Re-points a node's group widget child links at the slot their name will map + * to, before the node's group widget values are applied. A saved link arrives + * on the wrong slot because the definition lays out the default option's + * children while `target_slot` counts the serialized layout, so applying the + * widget's value would rebuild those inputs and drop it. Ordinary inputs join + * the batch so an occupied destination slot is vacated in the same atomic + * update instead of blocking the move. */ export function realignGroupWidgetChildLinks( node: LGraphNode,