From fe7ec4dc976e44745191908e7f815578f8157cb4 Mon Sep 17 00:00:00 2001 From: Alexander Brown Date: Thu, 12 Nov 2026 19:17:38 +0000 Subject: [PATCH 01/11] feat: support dynamic combos and autogrow groups on subgraphs Squashes the dynamic combo subgraph stack (#22170, #22419, #22870, #22872) onto the in-subgraph edits branch. Conflict resolution: - Autogrow keeps the reconcile engine from main. The stack's group socket sync, promotion binding, and dynamic child registries sit on top of it; its disconnect handler and pending-connection tracking are dropped as superseded. - WidgetGrid keeps the theme-variable row gap and row offset tracking from main and takes the stack's per-row grid template. - Pass-through subgraph outputs still read the promoted widget value from the widget value store. Co-authored-by: Austin Mroz --- .fallow-baselines/health.json | 3 + .../agentAutogrowSpareInputFixture.ts | 4 +- .../agentAutogrowSpareInputReconcile.spec.ts | 2 +- .../agentAutogrowTabSwitchReconcile.spec.ts | 4 +- .../tests/graph/autogrowRowAlignment.spec.ts | 18 +- .../subgraphConvertAutogrowSlotOrder.spec.ts | 1 + .../vueNodes/widgets/advancedWidgets.spec.ts | 20 + .../syncConcatOrderWithLinks.test.ts | 12 +- src/core/graph/subgraph/promotionUtils.ts | 23 +- .../widgets/autogrowConvertToSubgraph.test.ts | 1 + .../widgets/dynamicComboPromotion.test.ts | 235 ++++++++ src/core/graph/widgets/dynamicWidgets.test.ts | 187 ++++--- src/core/graph/widgets/dynamicWidgets.ts | 529 +++++++++++++----- src/lib/litegraph/src/LGraphCanvas.ts | 44 +- src/lib/litegraph/src/canvas/LinkConnector.ts | 4 +- .../src/subgraph/ExecutableNodeDTO.ts | 165 +++--- .../litegraph/src/subgraph/SubgraphInput.ts | 35 +- .../litegraph/src/subgraph/SubgraphNode.ts | 120 +++- src/lib/litegraph/src/subgraph/hostInput.ts | 15 + .../litegraph/src/subgraph/subgraphUtils.ts | 26 +- .../canvas/links/autogrowGroupDropState.ts | 60 ++ .../vueNodes/components/LGraphNode.vue | 29 +- .../vueNodes/components/NodeSlots.vue | 33 +- .../vueNodes/components/NodeWidgets.vue | 6 +- .../vueNodes/components/WidgetGrid.vue | 107 +++- .../composables/processedWidgetRenderModel.ts | 45 ++ .../composables/useProcessedWidgets.ts | 15 +- .../extensions/vueNodes/types/widgetGrid.ts | 5 +- .../vueNodes/utils/nodeDataUtils.ts | 9 + src/services/litegraphService.ts | 45 +- ...dtProjection.reconnectAdoptionRace.test.ts | 2 + .../extensions/agent/crdt/docOpMinter.test.ts | 19 +- tools/devtools/nodes/inputs.py | 21 + 33 files changed, 1385 insertions(+), 459 deletions(-) create mode 100644 src/core/graph/widgets/dynamicComboPromotion.test.ts create mode 100644 src/lib/litegraph/src/subgraph/hostInput.ts create mode 100644 src/renderer/core/canvas/links/autogrowGroupDropState.ts diff --git a/.fallow-baselines/health.json b/.fallow-baselines/health.json index 25d8a297f65..92fcfae3613 100644 --- a/.fallow-baselines/health.json +++ b/.fallow-baselines/health.json @@ -1433,6 +1433,9 @@ } }, "src/lib/litegraph/src/subgraph/SubgraphInput.ts": { + "crap_high": { + "count": 1 + }, "crap_moderate": { "count": 1 } diff --git a/browser_tests/fixtures/agentAutogrowSpareInputFixture.ts b/browser_tests/fixtures/agentAutogrowSpareInputFixture.ts index ddcdaa0bbb8..e4a6f06127a 100644 --- a/browser_tests/fixtures/agentAutogrowSpareInputFixture.ts +++ b/browser_tests/fixtures/agentAutogrowSpareInputFixture.ts @@ -209,11 +209,11 @@ async function wireAutogrowNodeAndSwitchTabs(page: Page) { page.evaluate((id) => { const node = window.app!.graph.getNodeById(id) return (node?.inputs ?? []) - .filter((input) => input.name.startsWith('model.images.')) .map((input, index) => ({ name: input.name, connected: node!.getInputLink(index) !== null })) + .filter(({ name }) => name.startsWith('model.images.')) }, toNodeId(GPT_NODE_ID)) const readNodeColor = () => page.evaluate((id) => { @@ -256,7 +256,7 @@ async function wireAutogrowNodeAndSwitchTabs(page: Page) { { name: IMAGE_1_NAME, connected: true }, { name: IMAGE_2_NAME, connected: false } ]) - await expect(vueNodes.getInputSlotRow(gptNodeId, 1)).toContainText( + await expect(vueNodes.getInputSlotRow(gptNodeId, 2)).toContainText( IMAGE_2_FRIENDLY_LABEL ) }) diff --git a/browser_tests/tests/agent/agentAutogrowSpareInputReconcile.spec.ts b/browser_tests/tests/agent/agentAutogrowSpareInputReconcile.spec.ts index 164aa52f239..07cf99b12cf 100644 --- a/browser_tests/tests/agent/agentAutogrowSpareInputReconcile.spec.ts +++ b/browser_tests/tests/agent/agentAutogrowSpareInputReconcile.spec.ts @@ -20,7 +20,7 @@ test.describe( test('still draws the free autogrow slot on the node after the switch', async ({ autogrow: { vueNodes, gptNodeId } }) => { - await expect(vueNodes.getInputSlotRow(gptNodeId, 1)).toContainText( + await expect(vueNodes.getInputSlotRow(gptNodeId, 2)).toContainText( 'image_2' ) }) diff --git a/browser_tests/tests/agent/agentAutogrowTabSwitchReconcile.spec.ts b/browser_tests/tests/agent/agentAutogrowTabSwitchReconcile.spec.ts index ef63a21d574..351efc5cb3c 100644 --- a/browser_tests/tests/agent/agentAutogrowTabSwitchReconcile.spec.ts +++ b/browser_tests/tests/agent/agentAutogrowTabSwitchReconcile.spec.ts @@ -26,10 +26,10 @@ test.describe( }) => { await test.step('the node still shows its API-node color and friendly label', async () => { await expect.poll(readNodeColor).toEqual(API_NODE_COLOR) - await expect(vueNodes.getInputSlotRow(gptNodeId, 0)).not.toContainText( + await expect(vueNodes.getInputSlotRow(gptNodeId, 1)).not.toContainText( IMAGE_1_NAME ) - await expect(vueNodes.getInputSlotRow(gptNodeId, 0)).toContainText( + await expect(vueNodes.getInputSlotRow(gptNodeId, 1)).toContainText( IMAGE_1_FRIENDLY_LABEL ) await page diff --git a/browser_tests/tests/graph/autogrowRowAlignment.spec.ts b/browser_tests/tests/graph/autogrowRowAlignment.spec.ts index aafb968f0cf..684cc86c928 100644 --- a/browser_tests/tests/graph/autogrowRowAlignment.spec.ts +++ b/browser_tests/tests/graph/autogrowRowAlignment.spec.ts @@ -86,7 +86,14 @@ test.describe( `${REFERENCE_IMAGES_PREFIX}image_4` ) expect(image4Index).toBeGreaterThanOrEqual(0) - await image4Source.connectOutput(0, referenceNode, image4Index) + await comfyPage.vueNodes + .getOutputSlotConnectionDot(String(image4Source.id), 0) + .dragTo( + comfyPage.vueNodes.getInputSlotConnectionDot( + AUTOGROW_REFERENCE_NODE_ID, + image4Index + ) + ) const inputsAfterFourth = await getInputNames( comfyPage, AUTOGROW_REFERENCE_NODE_ID, @@ -96,7 +103,14 @@ test.describe( `${REFERENCE_IMAGES_PREFIX}image_5` ) expect(image5Index).toBeGreaterThanOrEqual(0) - await image5Source.connectOutput(0, referenceNode, image5Index) + await comfyPage.vueNodes + .getOutputSlotConnectionDot(String(image5Source.id), 0) + .dragTo( + comfyPage.vueNodes.getInputSlotConnectionDot( + AUTOGROW_REFERENCE_NODE_ID, + image5Index + ) + ) await comfyPage.nextFrame() await expect diff --git a/browser_tests/tests/subgraph/subgraphConvertAutogrowSlotOrder.spec.ts b/browser_tests/tests/subgraph/subgraphConvertAutogrowSlotOrder.spec.ts index ae048bdb2fd..68322074e53 100644 --- a/browser_tests/tests/subgraph/subgraphConvertAutogrowSlotOrder.spec.ts +++ b/browser_tests/tests/subgraph/subgraphConvertAutogrowSlotOrder.spec.ts @@ -50,6 +50,7 @@ test.describe( await expect .poll(() => getInputNames(comfyPage, AUTOGROW_NODE_ID, '')) .toEqual([ + 'images', 'images.image0', 'images.image1', 'images.image2', diff --git a/browser_tests/tests/vueNodes/widgets/advancedWidgets.spec.ts b/browser_tests/tests/vueNodes/widgets/advancedWidgets.spec.ts index e0314cdcadd..9c40c73d652 100644 --- a/browser_tests/tests/vueNodes/widgets/advancedWidgets.spec.ts +++ b/browser_tests/tests/vueNodes/widgets/advancedWidgets.spec.ts @@ -173,3 +173,23 @@ test.describe('Advanced Widget Visibility', { tag: '@vue-nodes' }, () => { await expect(node.getByText(SHOW_ADVANCED_INPUTS)).toBeHidden() }) }) + +test.describe('Advanced socket-only input', { tag: '@vue-nodes' }, () => { + test('is hidden until advanced inputs are shown', async ({ comfyPage }) => { + const nodeRef = await comfyPage.nodeOps.addNode( + 'DevToolsNodeWithAdvancedSocketInput', + {}, + { x: 500, y: 200 } + ) + const node = comfyPage.vueNodes.getNodeByTitle( + 'Node With Advanced Socket Input' + ) + const maskRow = comfyPage.vueNodes.getInputSlotRow(String(nodeRef.id), 1) + + await expect(maskRow).toHaveCount(0) + await node.getByText(SHOW_ADVANCED_INPUTS).click() + await expect(maskRow).toHaveCount(1) + await expect(maskRow.getByTestId('slot-dot')).toBeVisible() + await expect(maskRow).toHaveText('mask') + }) +}) diff --git a/src/composables/videoConcat/syncConcatOrderWithLinks.test.ts b/src/composables/videoConcat/syncConcatOrderWithLinks.test.ts index 0ab1cecd438..82feff61606 100644 --- a/src/composables/videoConcat/syncConcatOrderWithLinks.test.ts +++ b/src/composables/videoConcat/syncConcatOrderWithLinks.test.ts @@ -38,11 +38,15 @@ function buildConcat({ widgetBeforeAdding = false } = {}) { const source = new LGraphNode(title) source.addOutput('VIDEO', 'VIDEO') graph.add(source) - source.connect(0, concat, slot) + source.connect(0, concat, slotOf(concat, `0.video${slot}`)) } return { concat, widget } } +function slotOf(node: LGraphNode, name: string) { + return node.inputs.findIndex((input) => input.name === name) +} + function clipTitles(concat: LGraphNode, order: string[]) { return order.flatMap((name) => { const slot = concat.inputs.findIndex((input) => input.name === `0.${name}`) @@ -64,11 +68,11 @@ describe('syncConcatOrderWithLinks', () => { 'green' ]) - concat.disconnectInput(0) + concat.disconnectInput(slotOf(concat, '0.video0')) await animationFrame() await nextTick() - expect(concat.inputs[0].name).toBe('0.video0') + expect(concat.inputs[1].name).toBe('0.video0') expect(clipTitles(concat, widget.value.order ?? [])).toEqual([ 'blue', 'green' @@ -83,7 +87,7 @@ describe('syncConcatOrderWithLinks', () => { await nextTick() concat.onRemoved?.() - concat.disconnectInput(0) + concat.disconnectInput(slotOf(concat, '0.video0')) await animationFrame() await nextTick() diff --git a/src/core/graph/subgraph/promotionUtils.ts b/src/core/graph/subgraph/promotionUtils.ts index f3d9908b931..129ab1e328d 100644 --- a/src/core/graph/subgraph/promotionUtils.ts +++ b/src/core/graph/subgraph/promotionUtils.ts @@ -6,6 +6,7 @@ import type { IContextMenuValue } from '@/lib/litegraph/src/litegraph' import { LGraphNode } from '@/lib/litegraph/src/litegraph' import type { SubgraphNode } from '@/lib/litegraph/src/subgraph/SubgraphNode' import type { LinkId } from '@comfyorg/graph-model/linkId' +import { hostInputOf } from '@/lib/litegraph/src/subgraph/hostInput' import { reorderSubgraphInputs } from '@/lib/litegraph/src/subgraph/subgraphUtils' import type { IBaseWidget } from '@comfyorg/graph-model/widgets' import { nextUniqueName } from '@/lib/litegraph/src/strings' @@ -155,7 +156,7 @@ export function reorderSubgraphInputsByWidgetOrder( subgraphNode: SubgraphNode, orderedWidgets: readonly Pick[] ): void { - const remainingIndices = new Set(subgraphNode.inputs.keys()) + const remainingIndices = new Set(subgraphNode.subgraph.inputs.keys()) const orderedIndices = orderedWidgets.flatMap((orderedWidget) => { for (const index of remainingIndices) { if (isSamePromotedInput(subgraphNode, index, orderedWidget)) { @@ -175,8 +176,10 @@ function applySubgraphInputOrder( subgraphNode: SubgraphNode, orderedIndices: readonly number[] ): void { - const widgetValues = subgraphNode.inputs.map((input) => { - const id = input.widgetId + const hostInputAt = (index: number) => + hostInputOf(subgraphNode, subgraphNode.subgraph.inputs[index]) + const widgetValues = subgraphNode.subgraph.inputs.map((_, index) => { + const id = hostInputAt(index)?.widgetId if (!id) return undefined return useWidgetValueStore().getWidget(id)?.value }) @@ -185,14 +188,16 @@ function applySubgraphInputOrder( useWidgetValueStore().setNodeWidgetOrder( subgraphNode.rootGraph.id, subgraphNode.id, - subgraphNode.inputs.flatMap((input) => - input.widgetId ? [input.widgetId] : [] - ) + subgraphNode.inputs.flatMap((input) => { + const id = + input.widgetId ?? subgraphNode.getWidgetFromSlot(input)?.widgetId + return id ? [id] : [] + }) ) for (const [newIndex, oldIndex] of orderedIndices.entries()) { const value = widgetValues[oldIndex] - const id = subgraphNode.inputs[newIndex]?.widgetId + const id = hostInputAt(newIndex)?.widgetId if (value === undefined || !id) continue useWidgetValueStore().setValue(id, value) } @@ -203,8 +208,8 @@ function isSamePromotedInput( inputIndex: number, orderedWidget: Pick ): boolean { - const input = subgraphNode.inputs.at(inputIndex) - const linkedInput = input?._subgraphSlot + const linkedInput = subgraphNode.subgraph.inputs.at(inputIndex) + const input = hostInputOf(subgraphNode, linkedInput) if (!input || !linkedInput) return false for (const linkId of linkedInput.linkIds) { diff --git a/src/core/graph/widgets/autogrowConvertToSubgraph.test.ts b/src/core/graph/widgets/autogrowConvertToSubgraph.test.ts index c157c7c791a..28b9edcff49 100644 --- a/src/core/graph/widgets/autogrowConvertToSubgraph.test.ts +++ b/src/core/graph/widgets/autogrowConvertToSubgraph.test.ts @@ -146,6 +146,7 @@ describe('Convert to Subgraph with a grown autogrow group (FE-3325)', () => { test('wires the grown group ahead of mask and strength', () => { const { node } = buildGraph() expect(node.inputs.map((input) => input.name)).toEqual([ + 'images', 'images.image0', 'images.image1', 'images.image2', diff --git a/src/core/graph/widgets/dynamicComboPromotion.test.ts b/src/core/graph/widgets/dynamicComboPromotion.test.ts new file mode 100644 index 00000000000..c358db5c365 --- /dev/null +++ b/src/core/graph/widgets/dynamicComboPromotion.test.ts @@ -0,0 +1,235 @@ +import { + assert, + beforeEach, + describe, + expect, + onTestFinished, + test +} from 'vitest' + +import { promoteWidget } from '@/core/graph/subgraph/promotionUtils' +import { + LGraphNode, + LiteGraph, + SubgraphNode +} from '@/lib/litegraph/src/litegraph' +import type { LGraph, Positionable } from '@/lib/litegraph/src/litegraph' +import { createTestRootGraph } from '@/lib/litegraph/src/subgraph/__fixtures__/subgraphHelpers' +import type { INodeInputSlot } from '@comfyorg/graph-model/slots' +import { + bindPromotedDynamicInput, + unbindPromotedDynamicInput +} from '@/core/graph/widgets/dynamicWidgets' +import type { ComfyNodeDef as ComfyNodeDefV1 } from '@comfyorg/object-info-parser' +import { useLitegraphService } from '@/services/litegraphService' +import { graphToPrompt } from '@/utils/executionUtil' + +const COMBO_NODE_TYPE = 'test/PromotedDynamicCombo' +const SOURCE_NODE_TYPE = 'test/PromotedDynamicComboSource' + +const comboNodeDef: ComfyNodeDefV1 = { + name: COMBO_NODE_TYPE, + display_name: 'Promoted Dynamic Combo', + category: 'testing', + python_module: 'nodes', + description: '', + input: { + required: { + mode: [ + 'COMFY_DYNAMICCOMBO_V3', + { + options: [ + { + key: 'scale', + inputs: { + required: { + factor: ['INT', { default: 2 }], + image: ['IMAGE', {}] + } + } + }, + { + key: 'caption', + inputs: { required: { text: ['STRING', { default: 'hi' }] } } + } + ] + } + ] + } + }, + output: ['IMAGE'], + output_name: ['IMAGE'], + output_node: false +} + +class SourceNode extends LGraphNode { + constructor() { + super('Source') + this.addOutput('IMAGE', 'IMAGE') + } +} + +function names(node: LGraphNode) { + return { + inputs: node.inputs.map((input) => input.name), + widgets: node.widgets?.map((widget) => widget.name) ?? [] + } +} + +function widget(node: LGraphNode, name: string) { + const found = node.widgets?.find((w) => w.name === name) + assert(found, `widget ${name} on ${node.type}`) + return found +} + +/** Registers subgraph nodes with the dynamic input hooks the app installs. */ +function newGraph(): LGraph { + const graph = createTestRootGraph() + const listener = (e: CustomEvent<{ subgraph: SubgraphNode['subgraph'] }>) => { + const { subgraph } = e.detail + class DynamicSubgraphNode extends SubgraphNode { + constructor() { + super(graph, subgraph, { + id: -1, + type: subgraph.id, + pos: [0, 0], + size: [100, 100], + inputs: [], + outputs: [], + flags: {}, + mode: 0, + order: 0 + }) + } + protected override bindDynamicInput( + input: INodeInputSlot, + interiorNode: LGraphNode, + interiorInput: INodeInputSlot + ) { + return bindPromotedDynamicInput( + this, + input, + interiorNode, + interiorInput + ) + } + protected override unbindDynamicInput(input: INodeInputSlot) { + return unbindPromotedDynamicInput(this, input) + } + } + LiteGraph.registered_node_types[subgraph.id] = DynamicSubgraphNode + onTestFinished(() => { + delete LiteGraph.registered_node_types[subgraph.id] + }) + } + graph.events.addEventListener('subgraph-created', listener) + onTestFinished(() => + graph.events.removeEventListener('subgraph-created', listener) + ) + return graph +} + +function buildPromoted() { + const graph = newGraph() + const node = LiteGraph.createNode(COMBO_NODE_TYPE) + assert(node) + graph.add(node) + widget(node, 'mode.factor').value = 7 + const { subgraph, node: host } = graph.convertToSubgraph( + new Set([node]) + ) + const inner = subgraph.nodes.find((n) => n.type === COMBO_NODE_TYPE) + assert(inner, 'combo node inside the subgraph') + assert(host.isSubgraphNode()) + promoteWidget(inner, widget(inner, 'mode'), [host]) + return { graph, inner, host } +} + +describe('Promoting a dynamic combo onto a subgraph node', () => { + beforeEach(async () => { + LiteGraph.registerNodeType(SOURCE_NODE_TYPE, SourceNode) + await useLitegraphService().registerNodeDef(COMBO_NODE_TYPE, comboNodeDef) + }) + + test('moves the option inputs from the interior node to the subgraph node', () => { + const { inner, host } = buildPromoted() + expect(names(inner)).toEqual({ inputs: ['mode'], widgets: ['mode'] }) + expect(names(host)).toEqual({ + inputs: ['mode', 'mode.factor', 'mode.image'], + widgets: ['mode', 'mode.factor'] + }) + expect(widget(host, 'mode.factor').value).toBe(7) + }) + + test('rejects a link to the promoted combo input from a node output', () => { + const { graph, host } = buildPromoted() + const source = LiteGraph.createNode(SOURCE_NODE_TYPE) + assert(source) + graph.add(source) + + expect(source.connect(0, host, 0)).toBeNull() + expect(host.isInputConnected(0)).toBe(false) + }) + + test('switching the subgraph node option swaps its child inputs', () => { + const { inner, host } = buildPromoted() + widget(host, 'mode').value = 'caption' + expect(names(host).inputs).toEqual(['mode', 'mode.text']) + expect(names(inner).inputs).toEqual(['mode']) + }) + + test('demoting restores the option inputs on the interior node', async () => { + const { inner, host } = buildPromoted() + host.subgraph.inputs[0].disconnect() + await Promise.resolve() + expect(names(inner).inputs).toEqual(['mode', 'mode.factor', 'mode.image']) + expect(widget(inner, 'mode.factor').value).toBe(7) + expect(names(host)).toEqual({ inputs: ['mode'], widgets: [] }) + }) + + test('executes with the subgraph node option and child values', async () => { + const { graph, inner, host } = buildPromoted() + widget(host, 'mode').value = 'caption' + widget(host, 'mode.text').value = 'promoted' + const source = LiteGraph.createNode(SOURCE_NODE_TYPE) + assert(source) + graph.add(source) + + const { output } = await graphToPrompt(graph) + expect(output[`${host.id}:${inner.id}`].inputs).toMatchObject({ + mode: 'caption', + 'mode.text': 'promoted' + }) + + widget(host, 'mode').value = 'scale' + source.connect( + 0, + host, + host.inputs.findIndex((i) => i.name === 'mode.image') + ) + const { output: scaled } = await graphToPrompt(graph) + expect(scaled[`${host.id}:${inner.id}`].inputs).toMatchObject({ + mode: 'scale', + 'mode.factor': 7, + 'mode.image': [String(source.id), 0] + }) + }) + + test('keeps the subgraph node option and child values across save and load', () => { + const { graph, host } = buildPromoted() + widget(host, 'mode').value = 'caption' + widget(host, 'mode.text').value = 'saved' + + const reloaded = newGraph() + const data = structuredClone(graph.serialize()) + reloaded.configure(data) + const reloadedHost = reloaded.getNodeById(host.id) + assert(reloadedHost?.isSubgraphNode()) + reloadedHost.subgraph.nodes.forEach((n) => n.onAfterGraphConfigured?.()) + + expect(widget(reloadedHost, 'mode').value).toBe('caption') + expect(widget(reloadedHost, 'mode.text').value).toBe('saved') + const reloadedInner = reloadedHost.subgraph.nodes[0] + expect(names(reloadedInner).inputs).toEqual(['mode']) + }) +}) diff --git a/src/core/graph/widgets/dynamicWidgets.test.ts b/src/core/graph/widgets/dynamicWidgets.test.ts index 8ef87cbbc10..92856f6afc0 100644 --- a/src/core/graph/widgets/dynamicWidgets.test.ts +++ b/src/core/graph/widgets/dynamicWidgets.test.ts @@ -77,6 +77,26 @@ describe('Dynamic Combos', () => { node.widgets[1].value = '1' expect(node.widgets.length).toBe(3) }) + test('a nested combo removed by switching options is not rebuilt after configure', () => { + const node = testNode() + addDynamicCombo(node, [[[[], ['STRING']]], ['INT']]) + node.widgets[0].value = '1' + const widgetNames = node.widgets.map((w) => w.name) + + expect(() => node.onAfterGraphConfigured?.()).not.toThrow() + expect(node.widgets.map((w) => w.name)).toEqual(widgetNames) + }) + test('rejects a link to the combo input from a node output', () => { + const graph = new LGraph() + const node = testNode() + graph.add(node) + addDynamicCombo(node, [['INT'], ['STRING']]) + + const comboSlot = node.inputs.findIndex((i) => i.name === '0') + + expect(() => connectInput(node, comboSlot, graph)).toThrow() + expect(node.isInputConnected(comboSlot)).toBe(false) + }) test('Can add input', () => { const node = testNode() addDynamicCombo(node, [['INT'], ['IMAGE']]) @@ -269,24 +289,24 @@ describe('Autogrow', () => { const node = testNode() graph.add(node) addAutogrow(node, { input: inputsSpec, prefix: 'test' }) - connectInput(node, 0, graph) connectInput(node, 1, graph) connectInput(node, 2, graph) - expect(node.inputs.length).toBe(4) - expect(node.inputs[0].name).toBe('0.test0') - expect(node.inputs[2].name).toBe('0.test2') + connectInput(node, 3, graph) + expect(node.inputs.length).toBe(5) + expect(node.inputs[1].name).toBe('0.test0') + expect(node.inputs[3].name).toBe('0.test2') }) test('Can name by list of names', () => { const graph = new LGraph() const node = testNode() graph.add(node) addAutogrow(node, { input: inputsSpec, names: ['a', 'b', 'c'] }) - connectInput(node, 0, graph) connectInput(node, 1, graph) connectInput(node, 2, graph) - expect(node.inputs.length).toBe(3) - expect(node.inputs[0].name).toBe('0.a') - expect(node.inputs[2].name).toBe('0.c') + connectInput(node, 3, graph) + expect(node.inputs.length).toBe(4) + expect(node.inputs[1].name).toBe('0.a') + expect(node.inputs[3].name).toBe('0.c') }) test('liveAutogrowGroupOf recognizes an explicit-names member even though it does not end in a digit', () => { // `0.b` does not end in an ordinal digit, so @@ -297,53 +317,53 @@ describe('Autogrow', () => { const node = testNode() graph.add(node) addAutogrow(node, { input: inputsSpec, names: ['a', 'b', 'c'] }) - connectInput(node, 0, graph) connectInput(node, 1, graph) - expect(node.inputs[1].name).toBe('0.b') + connectInput(node, 2, graph) + expect(node.inputs[2].name).toBe('0.b') expect(liveAutogrowGroupOf(node, '0.b')).toBe('0') expect(liveAutogrowGroupOf(node, 'unrelated.b')).toBeUndefined() }) test('Can add autogrow with min input count', () => { const node = testNode() addAutogrow(node, { min: 4, input: inputsSpec }) - expect(node.inputs.length).toBe(5) + expect(node.inputs.length).toBe(6) }) test('Adding connections will cause growth up to max', () => { const graph = new LGraph() const node = testNode() graph.add(node) addAutogrow(node, { min: 1, input: inputsSpec, prefix: 'test', max: 3 }) - expect(node.inputs.length).toBe(2) + expect(node.inputs.length).toBe(3) - connectInput(node, 0, graph) - expect(node.inputs.length).toBe(2) connectInput(node, 1, graph) expect(node.inputs.length).toBe(3) connectInput(node, 2, graph) - expect(node.inputs.length).toBe(3) + expect(node.inputs.length).toBe(4) + connectInput(node, 3, graph) + expect(node.inputs.length).toBe(4) }) test('Removing connections decreases to min + 1', async () => { const graph = new LGraph() const node = testNode() graph.add(node) addAutogrow(node, { min: 4, input: inputsSpec, prefix: 'test' }) - connectInput(node, 3, graph) connectInput(node, 4, graph) connectInput(node, 5, graph) - expect(node.inputs.length).toBe(7) + connectInput(node, 6, graph) + expect(node.inputs.length).toBe(8) + node.disconnectInput(5) + await nextTick() + expect(node.inputs.length).toBe(7) node.disconnectInput(4) await nextTick() expect(node.inputs.length).toBe(6) - node.disconnectInput(3) - await nextTick() - expect(node.inputs.length).toBe(5) - connectInput(node, 0, graph) - expect(node.inputs.length).toBe(5) - node.disconnectInput(0) + connectInput(node, 1, graph) + expect(node.inputs.length).toBe(6) + node.disconnectInput(1) await nextTick() - expect(node.inputs.length).toBe(5) + expect(node.inputs.length).toBe(6) }) test( 'Disconnecting a just-connected slot still compacts to one spare slot ' + @@ -358,18 +378,22 @@ describe('Autogrow', () => { names: ['image_1', 'image_2', 'image_3', 'image_4'] }) - connectInput(node, 0, graph) - await nextTick() connectInput(node, 1, graph) + await nextTick() + connectInput(node, 2, graph) - node.disconnectInput(1) + node.disconnectInput(2) await nextTick() await nextTick() - expect(node.inputs.map((i) => i.name)).toEqual(['0.image_1', '0.image_2']) - expect(node.isInputConnected(0)).toBe(true) - expect(node.isInputConnected(1)).toBe(false) - expect(node.getInputLink(0)?.target_slot).toBe(0) + expect(node.inputs.map((i) => i.name)).toEqual([ + '0', + '0.image_1', + '0.image_2' + ]) + expect(node.isInputConnected(1)).toBe(true) + expect(node.isInputConnected(2)).toBe(false) + expect(node.getInputLink(1)?.target_slot).toBe(1) } ) test( @@ -385,15 +409,15 @@ describe('Autogrow', () => { names: ['image_1', 'image_2', 'image_3', 'image_4'] }) - connectInput(node, 0, graph) + connectInput(node, 1, graph) await nextTick() - const oldLink = node.getInputLink(0) + const oldLink = node.getInputLink(1) const swapSource = testNode() swapSource.addOutput('out', '*') graph.add(swapSource) node.onConnectInput?.( - 0, + 1, swapSource.outputs[0].type, swapSource.outputs[0], swapSource, @@ -401,16 +425,17 @@ describe('Autogrow', () => { ) node.onConnectionsChange?.( LiteGraph.INPUT, - 0, + 1, false, oldLink, - node.inputs[0] + node.inputs[1] ) - connectInput(node, 1, graph) + connectInput(node, 2, graph) await nextTick() expect(node.inputs.map((i) => i.name)).toEqual([ + '0', '0.image_1', '0.image_2', '0.image_3' @@ -430,44 +455,46 @@ describe('Autogrow', () => { names: ['image_1', 'image_2', 'image_3', 'image_4'] }) - connectInput(node, 0, graph) + connectInput(node, 1, graph) await nextTick() - expect(node.inputs.map((i) => i.name)).toEqual(['0.image_1', '0.image_2']) + expect(node.inputs.map((i) => i.name)).toEqual([ + '0', + '0.image_1', + '0.image_2' + ]) - const oldLink = node.getInputLink(0) + const oldLink = node.getInputLink(1) assert.exists(oldLink) const oldSource = graph.getNodeById(oldLink.origin_id) - //connectSlots (LGraphNode.ts) fires slot 0's disconnect (the swap's - //tail) and its matching connect synchronously, back to back, while - //replacing slot 0's link below. Chaining onto onConnectionsChange - - //the same public extension point real custom nodes use - lets a - //second, genuine connect land on a different slot in that exact - //window, without faking either connection. let sawSwapTail = false node.onConnectionsChange = useChainCallback( node.onConnectionsChange, (contype, slot, iscon) => { - if (contype !== LiteGraph.INPUT || slot !== 0 || iscon) return + if (contype !== LiteGraph.INPUT || slot !== 1 || iscon) return sawSwapTail = true - connectInput(node, 1, graph) + connectInput(node, 2, graph) } ) const replacement = testNode() replacement.addOutput('out', '*') graph.add(replacement) - const newLink = replacement.connect(0, node, 0) + const newLink = replacement.connect(0, node, 1) assert.exists(newLink) - expect(sawSwapTail).toBe(true) - expect(node.getInputLink(0)).toBe(newLink) + expect( + sawSwapTail, + 'a connect on slot 2 lands between the disconnect and connect that connectSlots fires while replacing slot 1' + ).toBe(true) + expect(node.getInputLink(1)).toBe(newLink) expect(oldSource?.isOutputConnected(0)).toBe(false) await nextTick() await nextTick() expect(node.inputs.map((i) => i.name)).toEqual([ + '0', '0.image_1', '0.image_2', '0.image_3' @@ -487,18 +514,18 @@ describe('Autogrow', () => { names: ['image_1', 'image_2', 'image_3', 'image_4'] }) - connectInput(node, 0, graph) + connectInput(node, 1, graph) await nextTick() - connectInput(node, 1, graph) - node.disconnectInput(1) - const reconnectLink = connectInput(node, 1, graph) + connectInput(node, 2, graph) + node.disconnectInput(2) + const reconnectLink = connectInput(node, 2, graph) await nextTick() await nextTick() - expect(node.getInputLink(1)).toBe(reconnectLink) - expect(node.isInputConnected(1)).toBe(true) + expect(node.getInputLink(2)).toBe(reconnectLink) + expect(node.isInputConnected(2)).toBe(true) expect(graph.getLink(reconnectLink.id)).toBe(reconnectLink) const sourceNode = graph.getNodeById(reconnectLink.origin_id) expect(sourceNode?.isOutputConnected(0)).toBe(true) @@ -532,9 +559,9 @@ describe('Autogrow', () => { node.onConnectionsChange = onConnectionsChange graph.add(node) addAutogrow(node, { min: 1, input: inputsSpec, prefix: 'test' }) - connectInput(node, 0, graph) connectInput(node, 1, graph) connectInput(node, 2, graph) + connectInput(node, 3, graph) const updateEndpoints = vi .spyOn(useLinkStore(), 'updateEndpoints') .mockReturnValue({ @@ -542,7 +569,7 @@ describe('Autogrow', () => { error: { code: 'occupied-target', message: 'Target is occupied' } }) - node.disconnectInput(1) + node.disconnectInput(2) const inputNames = node.inputs.map(({ name }) => name) const widgetNames = node.widgets.map(({ name }) => name) onConnectionsChange.mockClear() @@ -570,12 +597,12 @@ describe('Autogrow', () => { }) try { - connectInput(node, 0, graph) - expect(node.inputs.length).toBe(2) + connectInput(node, 1, graph) + expect(node.inputs.length).toBe(3) rafCallbacks.shift()?.(0) - node.disconnectInput(0) + node.disconnectInput(1) const staleDisconnectCallback = rafCallbacks.shift() expect(staleDisconnectCallback).toBeDefined() @@ -603,23 +630,27 @@ describe('Autogrow', () => { addAutogrow(node, { min: 1, input: videoSpec, prefix: 'vid' }) expect(node.inputs.map((i) => i.name)).toStrictEqual([ + '0', '0.img0', '0.img1', - '2.vid0', - '2.vid1' + '3', + '3.vid0', + '3.vid1' ]) - connectInput(node, 1, graph) + connectInput(node, 2, graph) expect(node.inputs.map((i) => i.name)).toStrictEqual([ + '0', '0.img0', '0.img1', '0.img2', - '2.vid0', - '2.vid1' + '3', + '3.vid0', + '3.vid1' ]) - const vid0Index = node.inputs.findIndex((i) => i.name === '2.vid0') - expect(vid0Index).toBe(3) + const vid0Index = node.inputs.findIndex((i) => i.name === '3.vid0') + expect(vid0Index).toBe(5) connectInput(node, vid0Index, graph) const vid0Link = node.inputs[vid0Index].link @@ -634,10 +665,10 @@ describe('Autogrow', () => { addAutogrow(node, { min: 1, input: inputsSpec, prefix: 'b' }) addNodeInput(node, { name: 'aa', isOptional: false, type: 'IMAGE' }) - connectInput(node, 0, graph) connectInput(node, 1, graph) - connectInput(node, 3, graph) - connectInput(node, 4, graph) + connectInput(node, 2, graph) + connectInput(node, 5, graph) + connectInput(node, 6, graph) const serialized = graph.serialize() graph.clear() @@ -645,19 +676,21 @@ describe('Autogrow', () => { const newNode = graph.nodes[0] expect(newNode.inputs.map((i) => i.name)).toStrictEqual([ + '0', '0.a0', '0.a1', '0.a2', - '2.b0', - '2.b1', - '2.b2', + '3', + '3.b0', + '3.b1', + '3.b2', 'aa' ]) - for (const slot of [0, 1, 3, 4]) { + for (const slot of [1, 2, 5, 6]) { expect.soft(newNode.isInputConnected(slot)).toBe(true) expect.soft(newNode.getInputLink(slot)?.target_slot).toBe(slot) } - for (const slot of [2, 5, 6]) { + for (const slot of [0, 3, 4, 7, 8]) { expect.soft(newNode.isInputConnected(slot)).toBe(false) } }) diff --git a/src/core/graph/widgets/dynamicWidgets.ts b/src/core/graph/widgets/dynamicWidgets.ts index 15b894cbea5..c5a44a0ab67 100644 --- a/src/core/graph/widgets/dynamicWidgets.ts +++ b/src/core/graph/widgets/dynamicWidgets.ts @@ -8,6 +8,7 @@ import type { INodeInputSlot } from '@comfyorg/graph-model/slots' import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' import { LiteGraph } from '@/lib/litegraph/src/litegraph' import type { LLink } from '@/lib/litegraph/src/LLink' +import { isSubgraphInput } from '@/lib/litegraph/src/subgraph/subgraphTypeGuards' import { commonType } from '@/lib/litegraph/src/utils/type' import { getWidgetIds, @@ -26,6 +27,7 @@ import { useApp } from '@/scripts/appInstance' import type { ComfyApp } from '@/types/comfy' import { captureInputLayout, + inputLink, replaceNodeInputs } from '@/lib/litegraph/src/node/slotLinks' import type { InputLayoutSnapshot } from '@/lib/litegraph/src/node/slotLinks' @@ -53,11 +55,18 @@ type AutogrowNode = LGraphNode & inputSpecs: InputSpecV2[] prefix?: string names?: string[] + inputSpec: InputSpecV2 } > } } +type DynamicComboNode = LGraphNode & { + comfyDynamic: { + dynamicCombo: Record + } +} + function commitMutatedInputs( node: LGraphNode, previous: InputLayoutSnapshot, @@ -68,6 +77,31 @@ function commitMutatedInputs( return replaceNodeInputs(node, previous, finalInputs, assignments) } +/** `onConnectionsChange` at each input's live slot: a callback can grow a group and shift later inputs. */ +function notifyRelinked( + node: LGraphNode, + relinked: readonly { input: INodeInputSlot; link: LLink }[] +) { + for (const { input, link } of relinked) { + const slot = node.inputs.indexOf(input) + if (slot !== -1) + node.onConnectionsChange?.(LiteGraph.INPUT, slot, true, link, input) + } +} + +/** Moves each removed input's link onto the input that replaced it by name. */ +function carryLinks( + node: LGraphNode, + removedInputs: INodeInputSlot[], + inputLinks: Map +) { + for (const input of removedInputs) { + const replacement = node.inputs.find((item) => item.name === input.name) + const link = inputLinks.get(input) + if (replacement && link) inputLinks.set(replacement, link) + } +} + function syncNodeWidgetOrder(node: LGraphNode) { const graphId = resolveNodeRootGraphId(node) if (!graphId || !node.widgets) return @@ -78,27 +112,6 @@ function syncNodeWidgetOrder(node: LGraphNode) { ) } -function ensureWidgetForInput(node: LGraphNode, input: INodeInputSlot) { - node.widgets ??= [] - const { widget } = input - if (widget && node.widgets.some((w) => w.name === widget.name)) return - node.widgets.push({ - draw(ctx, _n, _w, y) { - ctx.save() - ctx.fillStyle = LiteGraph.NODE_TEXT_COLOR - ctx.fillText(input.label ?? input.name, 20, y + 15) - ctx.restore() - }, - name: input.name, - options: {}, - serialize: false, - type: 'shim', - y: 0 - }) - input.alwaysVisible = true - input.widget = { name: input.name } -} - function dynamicComboWidget( node: LGraphNode, inputName: string, @@ -107,7 +120,6 @@ function dynamicComboWidget( widgetName?: string ) { const { addNodeInput } = useLitegraphServiceAccess() - const { deleteWidget } = useWidgetValueStore() const parseResult = zDynamicComboInputSpec.safeParse(untypedInputData) if (!parseResult.success) throw new Error('invalid DynamicCombo spec') const inputData = parseResult.data @@ -161,121 +173,81 @@ function dynamicComboWidget( positionalIndex ) : undefined - if (!restored && removed?.type === addedWidget.type) { + if (restored) addedWidget.value = restored.value + else if (removed?.type === addedWidget.type) addedWidget.value = removed.value - } } } + function addOptionInputs(spec: (typeof options)[string] | undefined) { + const inputTypes: [Record | undefined, boolean][] = [ + [spec?.required, false], + [spec?.optional, true] + ] + for (const [inputs, isOptional] of inputTypes) + for (const [key, inputSpec] of Object.entries(inputs ?? {})) { + const name = `${widget.name}.${key}` + const specToAdd = transformInputSpecV1ToV2(inputSpec, { + name, + isOptional + }) + specToAdd.display_name = key + addNodeInput(node, specToAdd) + } + } + /** Moves inputs added past `start` to after the combo's socket, reusing same-named existing inputs. */ + function placeAddedInputs(start: number, hasAddedWidgets: boolean) { + const socket = node.inputs.findIndex((i) => i.name === widget.name) + if (socket === -1 && node.inputs.length > start && !hasAddedWidgets) + throw new Error('Failed to find input socket for ' + widget.name) + const added = node.inputs.splice(start).map((input) => { + const existing = node.inputs.findIndex((i) => i.name === input.name) + return existing === -1 ? input : node.inputs.splice(existing, 1)[0] + }) + node.inputs.splice(socket + 1 || node.inputs.length, 0, ...added) + } const updateWidgets = (value?: string) => { if (!node.widgets) throw new Error('Not Reachable') - const newSpec = value ? options[value] : undefined - const removedOption = activeOption + const newSpec = + value && !widget.connectionSuppressed ? options[value] : undefined + const optionValues = removedWidgetValues.get(activeOption) ?? new Map() + removedWidgetValues.set(activeOption, optionValues) activeOption = value const previous = captureInputLayout(node) const inputLinks = new Map(previous.links) const removedInputs = remove(node.inputs, isInGroup) - for (const widget of remove(node.widgets, isInGroup)) { - const optionValues = removedWidgetValues.get(removedOption) ?? new Map() - optionValues.set(widget.name, { - type: widget.type, - value: widget.value - }) - removedWidgetValues.set(removedOption, optionValues) - widget.onRemove?.() - if (widget.widgetId) deleteWidget(widget.widgetId) + for (const child of node.widgets.filter(isInGroup)) { + optionValues.set(child.name, { type: child.type, value: child.value }) + node.removeWidget(child) } + forgetDynamicChildren(node, inputName) - if (!newSpec) { - const result = commitMutatedInputs(node, previous, inputLinks) - if (!result.ok) return - syncNodeWidgetOrder(node) - return - } - - const insertionPoint = node.widgets.findIndex((w) => w === widget) + 1 - const startingLength = node.widgets.length - const startingInputLength = node.inputs.length - + const insertionPoint = node.widgets.indexOf(widget) + 1 if (insertionPoint === 0) throw new Error("Dynamic widget doesn't exist on node") - const inputTypes: (Record | undefined)[] = [ - newSpec.required, - newSpec.optional - ] - inputTypes.forEach((inputType, idx) => { - for (const key in inputType ?? {}) { - const name = `${widget.name}.${key}` - const specToAdd = transformInputSpecV1ToV2(inputType![key], { - name, - isOptional: idx !== 0 - }) - specToAdd.display_name = key - addNodeInput(node, specToAdd) - } - }) + const startingWidgets = new Set(node.widgets) + const startingInputLength = node.inputs.length + addOptionInputs(newSpec) - const inputInsertionPoint = - node.inputs.findIndex((i) => i.name === widget.name) + 1 - const addedWidgets = node.widgets.splice(startingLength) - const addedWidgetNames = addedWidgets.map(({ name }) => name) + const addedWidgets = remove(node.widgets, (w) => !startingWidgets.has(w)) node.widgets.splice(insertionPoint, 0, ...addedWidgets) syncNodeWidgetOrder(node) - if (inputInsertionPoint === 0) { - if ( - addedWidgets.length === 0 && - node.inputs.length !== startingInputLength - ) - //input is inputOnly, but lacks an insertion point - throw new Error('Failed to find input socket for ' + widget.name) - const result = commitMutatedInputs(node, previous, inputLinks) - if (!result.ok) return - restoreRemovedValues(value, addedWidgetNames) - return - } - const addedInputs = node.inputs - .splice(startingInputLength) - .map((addedInput) => { - const existingInput = node.inputs.findIndex( - (existingInput) => addedInput.name === existingInput.name - ) - return existingInput === -1 - ? addedInput - : node.inputs.splice(existingInput, 1)[0] - }) - //assume existing inputs are in correct order - node.inputs.splice(inputInsertionPoint, 0, ...addedInputs) + placeAddedInputs(startingInputLength, addedWidgets.length > 0) + carryLinks(node, removedInputs, inputLinks) - for (const input of removedInputs) { - const replacement = node.inputs.find((item) => item.name === input.name) - const link = inputLinks.get(input) - if (replacement && link) inputLinks.set(replacement, link) - } 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. - 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) - + notifyRelinked(node, result.replacements) + restoreRemovedValues( + value, + addedWidgets.map(({ name }) => name) + ) if (!node.graph) return node._setConcreteSlots() node.arrange() node.graph.setDirtyCanvas(true, true) } - //Refit height on the callback channel: interaction fires it after the value - //setter, while configure (load, clone, paste) only fires the setter and must - //keep the serialised height. - widget.callback = useChainCallback(widget.callback, () => { - node.size = [node.size[0], node.computeSize([...node.size])[1]] - node.graph?.setDirtyCanvas(true, true) - }) - //A little hacky, but onConfigure won't work. - //It fires too late and is overly disruptive + widget.callback = useChainCallback(widget.callback, () => refitHeight(node)) let widgetValue = widget.value const getState = () => { const graphId = resolveNodeRootGraphId(node) @@ -296,9 +268,62 @@ function dynamicComboWidget( } }) widget.value = widgetValue + withComfyDynamicCombo(node) + node.comfyDynamic.dynamicCombo[widget.name] = { + inputSpec: untypedInputData, + sync() { + const hasChildren = + node.inputs.some(isInGroup) || !!node.widgets?.some(isInGroup) + if (!!widget.connectionSuppressed !== hasChildren) return + updateWidgets(activeOption) + refitHeight(node) + } + } return { widget, minWidth, minHeight } } +function refitHeight(node: LGraphNode) { + node.size = [node.size[0], node.computeSize([...node.size])[1]] + node.graph?.setDirtyCanvas(true, true) +} + +function hasDynamicCombos(node: LGraphNode): node is DynamicComboNode { + return !!node.comfyDynamic?.dynamicCombo +} + +function withComfyDynamicCombo( + node: LGraphNode +): asserts node is DynamicComboNode { + if (hasDynamicCombos(node)) return + node.comfyDynamic ??= {} + const combos: DynamicComboNode['comfyDynamic']['dynamicCombo'] = {} + node.comfyDynamic.dynamicCombo = combos + + const originalOnConnectInput = node.onConnectInput + node.onConnectInput = function (slot: number, ...args) { + const name = this.inputs.at(slot)?.widget?.name + if (name && Object.hasOwn(combos, name) && !isSubgraphInput(args[1])) + return false + return originalOnConnectInput?.apply(this, [slot, ...args]) ?? true + } + + node.onAfterGraphConfigured = useChainCallback( + node.onAfterGraphConfigured, + () => { + for (const combo of Object.values(combos)) combo.sync() + } + ) + node.onConnectionsChange = useChainCallback( + node.onConnectionsChange, + function (this: LGraphNode, contype: ISlotType, slot: number) { + const name = this.inputs.at(slot)?.widget?.name + if (contype !== LiteGraph.INPUT || !name || useApp().configuringGraph) + return + if (Object.hasOwn(combos, name)) combos[name].sync() + } + ) +} + export const dynamicWidgets = { COMFY_DYNAMICCOMBO_V3: dynamicComboWidget, COMFY_DYNAMICGROUP_V3: dynamicGroupWidget @@ -486,7 +511,7 @@ function addAutogrowGroup( ) { const { addNodeInput } = useLitegraphServiceAccess() const { max, min, inputSpecs } = node.comfyDynamic.autogrow[groupName] - if (ordinal >= max) return + if (ordinal >= max || isAutogrowGroupConnected(node, groupName)) return const previous = captureInputLayout(node) const inputLinks = new Map(previous.links) @@ -501,11 +526,8 @@ function addAutogrowGroup( ) const newInputs = namedSpecs.map((namedSpec) => { addNodeInput(node, namedSpec) - const input = node.inputs.splice(node.inputs.length - 1, 1)[0] - if (inputSpecs.length !== 1) ensureWidgetForInput(node, input) - return input + return node.inputs.splice(node.inputs.length - 1, 1)[0] }) - for (const newInput of newInputs) { for (const existingInput of remove( node.inputs, @@ -523,8 +545,8 @@ function addAutogrowGroup( groupName, node ).name - const lastIndex = node.inputs.findLastIndex((inp) => - inp.name.startsWith(targetName) + const lastIndex = node.inputs.findLastIndex( + (inp) => inp.name.startsWith(targetName) || inp.name === groupName ) const insertionIndex = lastIndex !== -1 @@ -582,6 +604,15 @@ export function liveAutogrowGroupOf( return undefined } +export function autogrowInputRole( + node: LGraphNode, + input: INodeInputSlot +): 'group' | 'child' | undefined { + if (!hasAutogrowGroups(node) || input.widget) return undefined + if (Object.hasOwn(node.comfyDynamic.autogrow, input.name)) return 'group' + return liveAutogrowGroupOf(node, input.name) ? 'child' : undefined +} + /** * Grows the autogrow group `name` belongs to until the node holds an input by * that name, and returns its live index. Undefined when `name` belongs to no @@ -600,6 +631,36 @@ export function growAutogrowInput( return index === -1 ? undefined : index } +function assignPackedLinks( + packed: readonly LLink[][], + layout: InputLayoutSnapshot, + { + start, + last, + inputAt + }: { + start: number + last: number + inputAt: (ordinal: number, column: number) => INodeInputSlot | undefined + } +) { + const assignments = new Map(layout.links) + const moved: { input: INodeInputSlot; link: LLink }[] = [] + for (const [column, links] of packed.entries()) { + for (const ordinal of range(start, last + 1)) { + const input = inputAt(ordinal, column) + if (input) assignments.delete(input) + } + for (const [offset, link] of links.entries()) { + const input = inputAt(start + offset, column) + if (!input) continue + assignments.set(input, link) + if (layout.links.get(input) !== link) moved.push({ input, link }) + } + } + return { assignments, moved } +} + function reconcileAutogrowGroup( groupName: string, node: AutogrowNode, @@ -637,20 +698,11 @@ function reconcileAutogrowGroup( if (!inputAt(ordinal, 0)) addAutogrowGroup(ordinal, groupName, node) const layout = captureInputLayout(node) - const assignments = new Map(layout.links) - const moved: { input: INodeInputSlot; link: LLink }[] = [] - for (const [column, links] of packed.entries()) { - for (const ordinal of range(start, last + 1)) { - const input = inputAt(ordinal, column) - if (input) assignments.delete(input) - } - for (const [offset, link] of links.entries()) { - const input = inputAt(start + offset, column) - if (!input) continue - assignments.set(input, link) - if (layout.links.get(input) !== link) moved.push({ input, link }) - } - } + const { assignments, moved } = assignPackedLinks(packed, layout, { + start, + last, + inputAt + }) const removed = layout.inputs.filter((input) => { if (!input.name.startsWith(`${groupName}.`)) return false const ordinal = resolveAutogrowOrdinal(input.name, groupName, node) @@ -665,23 +717,69 @@ function reconcileAutogrowGroup( assignments ) if (!result.ok) return - node.graph?.setDirtyCanvas(true, true) - for (const { input, link } of moved) { - const slot = node.inputs.indexOf(input) - if (slot !== -1) - node.onConnectionsChange(LiteGraph.INPUT, slot, true, link, input) - } - for (const input of removed) { - const widgetName = input.widget?.name - if (!widgetName) continue - for (const widget of remove(node.widgets, (w) => w.name === widgetName)) { - widget.onRemove?.() - if (widget.widgetId) useWidgetValueStore().deleteWidget(widget.widgetId) - } - syncNodeWidgetOrder(node) + notifyRelinked(node, moved) + const removedNames = new Set(removed.map((input) => input.widget?.name)) + for (const widget of node.widgets.filter((w) => removedNames.has(w.name))) + node.removeWidget(widget) + syncNodeWidgetOrder(node) + refitHeight(node) +} + +function isAutogrowGroupConnected(node: LGraphNode, groupName: string) { + const index = node.inputs.findIndex((input) => input.name === groupName) + if (index === -1 || !node.graph) return false + return inputLink(node.graph, node.id, index)?.type === node.inputs[index].type +} + +function syncAutogrowGroup(node: AutogrowNode, groupName: string) { + if (isAutogrowGroupConnected(node, groupName)) + removeGroupChildren(node, groupName) + else if (!node.inputs.some((input) => input.name.startsWith(`${groupName}.`))) + growAutogrowToMin(node, groupName) +} + +function growAutogrowToMin(node: AutogrowNode, groupName: string) { + const { min } = node.comfyDynamic.autogrow[groupName] + for (let i = 0; i === 0 || i < min + 1; i++) + addAutogrowGroup(i, groupName, node) +} + +function removeGroupChildren(node: LGraphNode, groupName: string) { + const prefix = `${groupName}.` + for (let slot = node.inputs.length - 1; slot >= 0; slot--) { + if ( + node.inputs[slot].name.startsWith(prefix) && + node.isInputConnected(slot) + ) + node.disconnectInput(slot) } - node.size = [node.size[0], node.computeSize([...node.size])[1]] + const previous = captureInputLayout(node) + const finalInputs = previous.inputs.filter( + (input) => !input.name.startsWith(prefix) + ) + if (finalInputs.length === previous.inputs.length) return + const result = replaceNodeInputs(node, previous, finalInputs) + if (!result.ok) return + for (const widget of node.widgets?.filter((w) => w.name.startsWith(prefix)) ?? + []) + node.removeWidget(widget) + forgetDynamicChildren(node, groupName) + syncNodeWidgetOrder(node) + refitHeight(node) +} + +function dynamicRegistries(node: LGraphNode): Record[] { + return [ + hasAutogrowGroups(node) && node.comfyDynamic.autogrow, + hasDynamicCombos(node) && node.comfyDynamic.dynamicCombo + ].filter((registry) => registry !== false) +} + +function forgetDynamicChildren(node: LGraphNode, groupName: string) { + for (const registry of dynamicRegistries(node)) + for (const nested of Object.keys(registry)) + if (nested.startsWith(`${groupName}.`)) delete registry[nested] } function restoreAutogrowGroup( @@ -709,7 +807,8 @@ function trimAutogrowGroups(node: AutogrowNode) { function withComfyAutogrow(node: LGraphNode): asserts node is AutogrowNode { if (node.comfyDynamic?.autogrow) return node.comfyDynamic ??= {} - node.comfyDynamic.autogrow = {} + const groups: AutogrowNode['comfyDynamic']['autogrow'] = {} + node.comfyDynamic.autogrow = groups node.onConfigure = useChainCallback( node.onConfigure, @@ -728,12 +827,34 @@ function withComfyAutogrow(node: LGraphNode): asserts node is AutogrowNode { } ) + const originalOnConnectInput = node.onConnectInput + node.onConnectInput = function (slot: number, ...args) { + const groupInput = this.inputs.at(slot) + if ( + groupInput && + Object.hasOwn(groups, groupInput.name) && + !isSubgraphInput(args[1]) + ) + return false + return originalOnConnectInput?.apply(this, [slot, ...args]) ?? true + } + + node.onAfterGraphConfigured = useChainCallback( + node.onAfterGraphConfigured, + function (this: AutogrowNode) { + for (const groupName of Object.keys(groups)) + syncAutogrowGroup(this, groupName) + } + ) + node.onConnectionsChange = useChainCallback( node.onConnectionsChange, function (this: AutogrowNode, contype: ISlotType, slot: number, iscon) { const input = this.inputs.at(slot) if (contype !== LiteGraph.INPUT || !input || useApp().configuringGraph) return + if (Object.hasOwn(this.comfyDynamic.autogrow, input.name)) + return syncAutogrowGroup(this, input.name) const groupName = liveAutogrowGroupOf(this, input.name) if (groupName === undefined) return if (iscon) return reconcileAutogrowGroup(groupName, this) @@ -747,6 +868,11 @@ function withComfyAutogrow(node: LGraphNode): asserts node is AutogrowNode { ) } function applyAutogrow(node: LGraphNode, inputSpecV2: InputSpecV2) { + const groupName = inputSpecV2.name + if (!node.inputs.some((input) => input.name === groupName)) + node.addInput(groupName, inputSpecV2.type, { + localized_name: inputSpecV2.display_name + }) withComfyAutogrow(node) const parseResult = zAutogrowOptions.safeParse(inputSpecV2) @@ -763,13 +889,110 @@ function applyAutogrow(node: LGraphNode, inputSpecV2: InputSpecV2) { transformInputSpecV1ToV2(v, { name, isOptional: index === 1 }) ) ) - node.comfyDynamic.autogrow[inputSpecV2.name] = { + node.comfyDynamic.autogrow[groupName] = { names, min, max: names?.length ?? max, prefix, - inputSpecs: inputsV2 + inputSpecs: inputsV2, + inputSpec: inputSpecV2 } - for (let i = 0; i === 0 || i < min + 1; i++) - addAutogrowGroup(i, inputSpecV2.name, node) + growAutogrowToMin(node, groupName) +} + +/** + * Makes `node` own the dynamic children (autogrow group or dynamic combo + * options) `interiorInput` defines, through `input`. False if it defines none. + */ +export function bindPromotedDynamicInput( + node: LGraphNode, + input: INodeInputSlot, + interiorNode: LGraphNode, + interiorInput: INodeInputSlot +): boolean { + return ( + bindPromotedAutogrowGroup(node, input, interiorNode, interiorInput) || + bindPromotedDynamicCombo(node, input, interiorNode, interiorInput) + ) +} + +/** Removes the dynamic children `node` owns through `input`; false if none. */ +export function unbindPromotedDynamicInput( + node: LGraphNode, + input: INodeInputSlot +): boolean { + const registry = dynamicRegistries(node).find((r) => + Object.hasOwn(r, input.name) + ) + if (!registry) return false + removeGroupChildren(node, input.name) + delete registry[input.name] + const groupWidget = node.widgets?.find((w) => w.name === input.name) + if (groupWidget) node.removeWidget(groupWidget) + input.widget = undefined + syncNodeWidgetOrder(node) + return true +} + +function bindPromotedDynamicCombo( + node: LGraphNode, + input: INodeInputSlot, + interiorNode: LGraphNode, + interiorInput: INodeInputSlot +): boolean { + const interiorName = interiorInput.widget?.name + if ( + !interiorName || + !hasDynamicCombos(interiorNode) || + !Object.hasOwn(interiorNode.comfyDynamic.dynamicCombo, interiorName) + ) + return false + if ( + hasDynamicCombos(node) && + Object.hasOwn(node.comfyDynamic.dynamicCombo, input.name) + ) + return true + input.widget = { name: input.name } + const { inputSpec } = interiorNode.comfyDynamic.dynamicCombo[interiorName] + const { widget } = dynamicComboWidget(node, input.name, inputSpec, useApp()) + if (input.label) widget.label = input.label + + const interiorWidgets = new Map( + interiorNode.widgets?.map((w) => [w.name, w.value]) + ) + if (interiorWidgets.has(interiorName)) + widget.value = interiorWidgets.get(interiorName) + for (const child of node.widgets?.filter((w) => + w.name.startsWith(`${input.name}.`) + ) ?? []) { + const interiorChildName = interiorName + child.name.slice(input.name.length) + if (interiorWidgets.has(interiorChildName)) + child.value = interiorWidgets.get(interiorChildName) + } + return true +} + +function bindPromotedAutogrowGroup( + node: LGraphNode, + input: INodeInputSlot, + interiorNode: LGraphNode, + interiorInput: INodeInputSlot +): boolean { + if ( + !hasAutogrowGroups(interiorNode) || + !Object.hasOwn(interiorNode.comfyDynamic.autogrow, interiorInput.name) + ) + return false + if ( + hasAutogrowGroups(node) && + Object.hasOwn(node.comfyDynamic.autogrow, input.name) + ) + return true + const { inputSpec } = interiorNode.comfyDynamic.autogrow[interiorInput.name] + applyAutogrow(node, { + ...inputSpec, + name: input.name, + display_name: input.label ?? inputSpec.display_name + }) + return true } diff --git a/src/lib/litegraph/src/LGraphCanvas.ts b/src/lib/litegraph/src/LGraphCanvas.ts index fd106fcd808..f7061e3ffef 100644 --- a/src/lib/litegraph/src/LGraphCanvas.ts +++ b/src/lib/litegraph/src/LGraphCanvas.ts @@ -16,6 +16,11 @@ import { MovingInputLink } from '@/lib/litegraph/src/canvas/MovingInputLink' import type { RenderLink } from '@/lib/litegraph/src/canvas/RenderLink' import { runCanvasFramePhase } from '@/lib/litegraph/src/canvas/canvasFrame' import { AutoPanController } from '@/renderer/core/canvas/useAutoPan' +import { + findAutogrowGroupDropRow, + setHoveredAutogrowGroup, + setIoInputDragType +} from '@/renderer/core/canvas/links/autogrowGroupDropState' import { LitegraphLinkAdapter } from '@/renderer/core/canvas/litegraph/litegraphLinkAdapter' import type { LinkRenderContext } from '@/renderer/core/canvas/litegraph/litegraphLinkAdapter' import { nodesInRenderOrder } from '@/renderer/core/canvas/litegraph/arrangeForLegacyRender' @@ -1263,6 +1268,7 @@ export class LGraphCanvas implements CustomEventDispatcher // @deprecated Workaround: Keep until connecting_links is removed. this.linkConnector.events.addEventListener('reset', () => { + setIoInputDragType(undefined) // Only stop link-drag autoPan; ghost placement manages its own. if (this.state.ghostNodeId == null) { this._autoPan?.stop() @@ -2654,6 +2660,7 @@ export class LGraphCanvas implements CustomEventDispatcher this._highlight_input = undefined this._highlight_pos = undefined this.linkConnector.overWidget = undefined + this.linkConnector.overInput = undefined // Hover transitions // TODO: Implement single lerp ease factor for current progress on hover in/out. @@ -3688,6 +3695,11 @@ export class LGraphCanvas implements CustomEventDispatcher const firstLink: RenderLink | undefined = linkConnector.renderLinks.at(0) const isSubgraphIOLink = linkConnector.isConnecting && firstLink?.isIoNodeLink + const ioInputDrag = + LiteGraph.vueNodesMode && + !!isSubgraphIOLink && + linkConnector.state.connectingTo === 'input' + setIoInputDragType(ioInputDrag ? firstLink.fromSlot.type : undefined) // get node over const node = @@ -3722,6 +3734,20 @@ export class LGraphCanvas implements CustomEventDispatcher // remove mouseover flag this.updateMouseOverNodes(node, e) + const autogrowDropRow = ioInputDrag + ? findAutogrowGroupDropRow(e.clientX, e.clientY) + : null + const autogrowGroupName = + node && autogrowDropRow?.nodeId === node.id + ? autogrowDropRow.groupName + : undefined + const autogrowGroupInput = node?.inputs.find( + (input) => + input.name === autogrowGroupName && + linkConnector.isInputValidDrop(node, input) + ) + setHoveredAutogrowGroup(autogrowGroupInput ? autogrowDropRow : null) + // mouse over a node if (node) { underPointer |= CanvasItem.Node @@ -3752,6 +3778,8 @@ export class LGraphCanvas implements CustomEventDispatcher inputId = isOverNodeInput(node, x, y, pos) outputId = isOverNodeOutput(node, x, y, pos) } + if (autogrowGroupInput) + inputId = node.inputs.indexOf(autogrowGroupInput) const overWidget = node.getWidgetOnPos(x, y, true) ?? undefined if (!node.mouseOver) { @@ -3783,6 +3811,7 @@ export class LGraphCanvas implements CustomEventDispatcher // State reset linkConnector.overWidget = undefined + linkConnector.overInput = undefined // Check if link is over anything it could connect to - record position of valid target for snap / highlight if (linkConnector.isConnecting) { @@ -3793,7 +3822,15 @@ export class LGraphCanvas implements CustomEventDispatcher if (!firstLink || !linkConnector.isNodeValidDrop(node)) { // No link, or none of the dragged links may be dropped here } else if (linkConnector.state.connectingTo === 'input') { - if (overWidget) { + if (autogrowGroupInput && autogrowDropRow) { + const rect = this.canvas.getBoundingClientRect() + const [clientX, clientY] = autogrowDropRow.dotClientPos + highlightPos = this.convertCanvasToOffset([ + clientX - rect.left, + clientY - rect.top + ]) + linkConnector.overInput = autogrowGroupInput + } else if (overWidget) { // Check widgets first - inputId is only valid if over the input socket const slot = node.getSlotFromWidget(overWidget) @@ -3813,7 +3850,7 @@ export class LGraphCanvas implements CustomEventDispatcher } // Not over a valid widget - treat drop on invalid widget same as node background - if (!linkConnector.overWidget) { + if (!linkConnector.overWidget && !linkConnector.overInput) { if (inputId === -1 && outputId === -1) { // Node background / title under the pointer const result = node.findInputByType(firstLink.fromSlot.type) @@ -5512,9 +5549,8 @@ export class LGraphCanvas implements CustomEventDispatcher // Reroute highlight overReroute?.drawHighlight(ctx, '#ffcc00aa') - // Ensure we're mousing over a node and connecting a link const node = this.node_over - if (!node) return + if (!node || LiteGraph.vueNodesMode) return const { strokeStyle, lineWidth } = ctx diff --git a/src/lib/litegraph/src/canvas/LinkConnector.ts b/src/lib/litegraph/src/canvas/LinkConnector.ts index b9ceb0a94b9..d5de101057a 100644 --- a/src/lib/litegraph/src/canvas/LinkConnector.ts +++ b/src/lib/litegraph/src/canvas/LinkConnector.ts @@ -129,6 +129,7 @@ export class LinkConnector { overWidget?: IBaseWidget /** The type (returned by downstream callback) for {@link overWidget} */ overWidgetType?: string + overInput?: INodeInputSlot /** The reroute beneath the pointer, if it is a valid connection target. */ overReroute?: Reroute @@ -856,7 +857,7 @@ export class LinkConnector { } // To input } else if (connectingTo === 'input') { - const input = node.getInputOnPos([canvasX, canvasY]) + const input = this.overInput ?? node.getInputOnPos([canvasX, canvasY]) const inputOrSocket = input ?? node.getSlotFromWidget(this.overWidget) // Input slot; fall back to a free compatible input if the slot rejects it @@ -1214,6 +1215,7 @@ export class LinkConnector { outputLinks.length = 0 floatingLinks.length = 0 hiddenReroutes.clear() + this.overInput = undefined state.multi = false state.draggingExistingLinks = false state.snapLinksPos = undefined diff --git a/src/lib/litegraph/src/subgraph/ExecutableNodeDTO.ts b/src/lib/litegraph/src/subgraph/ExecutableNodeDTO.ts index 154ccc9ae35..6d6ef2d3ee8 100644 --- a/src/lib/litegraph/src/subgraph/ExecutableNodeDTO.ts +++ b/src/lib/litegraph/src/subgraph/ExecutableNodeDTO.ts @@ -5,6 +5,7 @@ import type { SubgraphNode } from '@comfyorg/graph-model/entityRegistry' import { inputLinkId } from '@/lib/litegraph/src/node/slotLinks' +import { hostInputOf } from '@/lib/litegraph/src/subgraph/hostInput' import type { SerializedNodeId } from '@comfyorg/graph-model/nodeId' import type { LinkId } from '@comfyorg/graph-model/linkId' @@ -14,6 +15,7 @@ import { NullGraphError } from '@/lib/litegraph/src/infrastructure/NullGraphErro import { RecursionError } from '@/lib/litegraph/src/infrastructure/RecursionError' import { SlotIndexError } from '@/lib/litegraph/src/infrastructure/SlotIndexError' import type { ISlotType } from '@comfyorg/graph-model/interfaces' +import type { INodeInputSlot } from '@comfyorg/graph-model/slots' import { LGraphEventMode } from '@comfyorg/graph-model/globalEnums' import { litegraph } from '@/lib/litegraph/src/litegraphInstance' import { reportError } from '@comfyorg/telemetry/reportError' @@ -55,7 +57,13 @@ export class ExecutableNodeDTO implements ExecutableLGraphNode { /** The graph that this node is a part of. */ readonly graph: LGraph | Subgraph - inputs: { linkId: LinkId | null; name: string; type: ISlotType }[] + /** Own inputs, then ones owned via the subgraph node (`ownerSlot`). */ + inputs: { + linkId: LinkId | null + name: string + type: ISlotType + ownerSlot?: number + }[] /** Backing field for {@link id}. */ private _id: ExecutionId @@ -125,6 +133,7 @@ export class ExecutableNodeDTO implements ExecutableLGraphNode { name: x.name, type: x.type })) + this.inputs.push(...this._ownedInputs()) // Only create a wrapper if the node has an applyToGraph method if (this.node.applyToGraph) { @@ -132,6 +141,61 @@ export class ExecutableNodeDTO implements ExecutableLGraphNode { } } + private _subgraphNodeDto(): ExecutableLGraphNode | undefined { + return this.nodesByExecutionId.get(this.subgraphNodePath.join(':')) + } + + private _ownedInputs(): ExecutableNodeDTO['inputs'] { + const { subgraphNode } = this + const subgraphNodeDto = this._subgraphNodeDto() + if (!subgraphNode || !subgraphNodeDto) return [] + + return this.inputs.flatMap((input) => { + const link = + input.linkId == null ? null : this.graph.getLink(input.linkId) + if (!link?.originIsIoNode) return [] + const owner = hostInputOf( + subgraphNode, + subgraphNode.subgraph.inputs.at(link.origin_slot) + ) + if (!owner) return [] + const prefix = `${owner.name}.` + return subgraphNodeDto.inputs.flatMap((ownerInput, ownerSlot) => + ownerInput.name.startsWith(prefix) && + !subgraphNode.inputs[ownerSlot]?._subgraphSlot + ? [ + { + linkId: null, + name: input.name + ownerInput.name.slice(owner.name.length), + type: ownerInput.type, + ownerSlot + } + ] + : [] + ) + }) + } + + /** The value of the subgraph node's widget for an unlinked `input`. */ + private _hostWidgetInput( + subgraphNode: SubgraphNode, + input: INodeInputSlot + ): ResolvedInput | undefined { + const widget = subgraphNode.getWidgetFromSlot(input) + if ( + !widget || + widget.serialize === false || + widget.options.serialize === false + ) + return + return { + node: this, + origin_id: this.id, + origin_slot: -1, + widgetInfo: { value: widget.value } + } + } + /** Returns either the DTO itself, or the DTOs of the inner nodes of the subgraph. */ getInnerNodes(): ExecutableLGraphNode[] { return this.node.isSubgraphNode() @@ -153,17 +217,11 @@ export class ExecutableNodeDTO implements ExecutableLGraphNode { type?: ISlotType ): ResolvedInput | undefined { const uniqueId = `${this.subgraphNode?.subgraph.id}:${this.node.id}[I]${slot}` - if (visited.has(uniqueId)) { - const nodeInfo = `${this.node.id}${this.node.title ? ` (${this.node.title})` : ''}` - const pathInfo = - this.subgraphNodePath.length > 0 - ? ` at path ${this.subgraphNodePath.join(':')}` - : '' + if (visited.has(uniqueId)) throw new RecursionError( - `Circular reference detected while resolving input ${slot} of node ${nodeInfo}${pathInfo}. ` + + `Circular reference detected while resolving input ${slot} of node [${this.id}]. ` + `This creates an infinite loop in link resolution. UniqueID: [${uniqueId}]` ) - } visited.add(uniqueId) const input = this.inputs.at(slot) @@ -171,8 +229,13 @@ export class ExecutableNodeDTO implements ExecutableLGraphNode { throw new SlotIndexError( `No input found for flattened id [${this.id}] slot [${slot}]` ) - - // Nothing connected + const { subgraphNode } = this + if (input.ownerSlot !== undefined) + return this._resolveHostInput( + subgraphNode?.inputs[input.ownerSlot], + visited, + type + ) if (input.linkId == null) return const link = this.graph.getLink(input.linkId) @@ -180,73 +243,24 @@ export class ExecutableNodeDTO implements ExecutableLGraphNode { throw new InvalidLinkError( `No link found in parent graph for id [${this.id}] slot [${slot}] ${input.name}` ) - - const { subgraphNode } = this - - // Link goes up and out of this subgraph if (subgraphNode && link.originIsIoNode) { - const subgraphNodeInput = subgraphNode.inputs.at(link.origin_slot) - if (!subgraphNodeInput) - throw new SlotIndexError( - `No input found for slot [${link.origin_slot}] ${input.name}` - ) - - if (!subgraphNode.graph) - throw new NullGraphError( - `SubgraphNode ${subgraphNode.id} has no graph during input resolution` - ) - // Nothing connected - const linkId = - inputLinkId(subgraphNode.graph, subgraphNode.id, link.origin_slot) ?? - null - if (linkId === null) { - const id = subgraphNodeInput.widgetId - if (!id) return - - return { - node: this, - origin_id: this.id, - origin_slot: -1, - widgetInfo: { value: useWidgetValueStore().getWidget(id)?.value } - } - } - - const outerLink = subgraphNode.graph.getLink(linkId) - if (!outerLink) - throw new InvalidLinkError( - `No outer link found for slot [${link.origin_slot}] ${input.name}` - ) - - const subgraphNodeExecutionId: ExecutionId = - this.subgraphNodePath.join(':') - const subgraphNodeDto = this.nodesByExecutionId.get( - subgraphNodeExecutionId + const subgraphInput = subgraphNode.subgraph.inputs.at(link.origin_slot) + return this._resolveHostInput( + hostInputOf(subgraphNode, subgraphInput), + visited, + type ) - if (!subgraphNodeDto) - throw new Error( - `No subgraph node DTO found for id [${subgraphNodeExecutionId}]` - ) - - return subgraphNodeDto.resolveInput(outerLink.target_slot, visited) } - // Not part of a subgraph; use the original link - const outputNode = this.graph.getNodeById(link.origin_id) - if (!outputNode) - throw new InvalidLinkError( - `No input node found for id [${this.id}] slot [${slot}] ${input.name}` - ) - const outputNodeExecutionId: ExecutionId = [ ...this.subgraphNodePath, - outputNode.id + link.origin_id ].join(':') const outputNodeDto = this.nodesByExecutionId.get(outputNodeExecutionId) if (!outputNodeDto) throw new Error( `No output node DTO found for id [${outputNodeExecutionId}]` ) - return outputNodeDto.resolveOutput( link.origin_slot, type ?? input.type, @@ -254,6 +268,25 @@ export class ExecutableNodeDTO implements ExecutableLGraphNode { ) } + /** Resolves the subgraph node's `hostInput`: its link, else its widget value. */ + private _resolveHostInput( + hostInput: INodeInputSlot | undefined, + visited: Set, + type?: ISlotType + ): ResolvedInput | undefined { + const { subgraphNode } = this + if (!subgraphNode || !hostInput) return + const slot = subgraphNode.inputs.indexOf(hostInput) + if (!subgraphNode.isInputConnected(slot)) + return this._hostWidgetInput(subgraphNode, hostInput) + const subgraphNodeDto = this._subgraphNodeDto() + if (!subgraphNodeDto) + throw new Error( + `No subgraph node DTO found for id [${this.subgraphNodePath.join(':')}]` + ) + return subgraphNodeDto.resolveInput(slot, visited, type) + } + /** * Determines whether this output is a valid endpoint for a link (non-virtual, non-bypass). * @param slot The slot index of the output. diff --git a/src/lib/litegraph/src/subgraph/SubgraphInput.ts b/src/lib/litegraph/src/subgraph/SubgraphInput.ts index 78242119e03..5a96d3a16ee 100644 --- a/src/lib/litegraph/src/subgraph/SubgraphInput.ts +++ b/src/lib/litegraph/src/subgraph/SubgraphInput.ts @@ -71,29 +71,14 @@ export class SubgraphInput extends SubgraphSlot { ) return - // if (slot instanceof SubgraphOutput) { - // // Subgraph IO nodes have no special handling at present. - // return new LLink( - // ++subgraph.state.lastLinkId, - // this.type, - // this.parent.id, - // this.parent.slots.indexOf(this), - // node.id, - // inputIndex, - // afterRerouteId, - // ) - // } - const inputWidget = node.getWidgetFromSlot(slot) if (inputWidget && !this.matchesWidget(inputWidget)) { console.warn('Target input has invalid widget.', slot, node) return } - const existingLink = inputLink(subgraph, node.id, node.inputs.indexOf(slot)) - if (existingLink) { - subgraph.beforeChange() - } + const existingLink = inputLink(subgraph, node.id, inputIndex) + if (existingLink) subgraph.beforeChange() const linkId = mintLinkId( subgraph.state, @@ -126,16 +111,12 @@ export class SubgraphInput extends SubgraphSlot { anchorRerouteChain(subgraph, link) subgraph.incrementVersion() - if (inputWidget) { - this._widget = inputWidget - this.events.dispatch('input-connected', { - input: slot, - widget: inputWidget, - node - }) - } else { - this.events.dispatch('input-connected', { input: slot }) - } + if (inputWidget) this._widget = inputWidget + this.events.dispatch('input-connected', { + input: slot, + widget: inputWidget, + node + }) if (subgraph.getLink(link.id) !== link) return node.onConnectionsChange?.( diff --git a/src/lib/litegraph/src/subgraph/SubgraphNode.ts b/src/lib/litegraph/src/subgraph/SubgraphNode.ts index 5922c4a501d..6d213c391f2 100644 --- a/src/lib/litegraph/src/subgraph/SubgraphNode.ts +++ b/src/lib/litegraph/src/subgraph/SubgraphNode.ts @@ -62,6 +62,7 @@ import { widgetId } from '@comfyorg/graph-model/widgetId' import { deriveWidgetVisibility } from '@/types/widgetVisibility' import { ExecutableNodeDTO } from './ExecutableNodeDTO' +import { hostInputOf } from './hostInput' import type { ExecutableLGraphNode, ExecutionId } from './ExecutableNodeDTO' import { createPromotedWidgetStoreProjection } from './promotedWidgetStoreProjection' @@ -136,13 +137,18 @@ export class SubgraphNode extends LGraphNode implements BaseLGraph { this.graph = graph Object.defineProperty(this, 'widgets', { - get: () => [ - ...this.inputs.flatMap((input) => { - const widget = this._projectPromotedWidget(input) + get: () => { + const widgets = this.inputs.flatMap((input) => { + const widget = + this._projectPromotedWidget(input) ?? + this._extraWidgets.find((w) => w.name === input.widget?.name) return widget ? [widget] : [] - }), - ...this._extraWidgets - ], + }) + return [ + ...widgets, + ...this._extraWidgets.filter((widget) => !widgets.includes(widget)) + ] + }, set: () => { if (import.meta.env.DEV) console.warn( @@ -175,6 +181,7 @@ export class SubgraphNode extends LGraphNode implements BaseLGraph { const { inputNode, input } = link.resolve(subgraph) if (!inputNode || !input) return + if (this.bindDynamicInput(existingInput, inputNode, input)) return const widget = inputNode.getWidgetFromSlot(input) if (widget) @@ -197,7 +204,9 @@ export class SubgraphNode extends LGraphNode implements BaseLGraph { const widget = e.detail.input._widget if (widget) this.ensureWidgetRemoved(widget) - this.removeInput(e.detail.index) + const input = hostInputOf(this, e.detail.input) + if (input) this.unbindDynamicInput(input) + this.removeInput(input ? this.inputs.indexOf(input) : e.detail.index) this.invalidatePromotedViews() this.setDirtyCanvas(true, true) }, @@ -225,8 +234,8 @@ export class SubgraphNode extends LGraphNode implements BaseLGraph { subgraphEvents.addEventListener( 'renaming-input', (e) => { - const { index, oldName, newName } = e.detail - const input = this.inputs.at(index) + const { oldName, newName } = e.detail + const input = hostInputOf(this, e.detail.input) if (!input) { console.error('Subgraph input not found') return @@ -329,7 +338,9 @@ export class SubgraphNode extends LGraphNode implements BaseLGraph { // rewire, not a real disconnect: cancel the pending demotion so the // widget is never torn down. input._pendingDemotionToken = undefined - if (!e.detail.widget || !e.detail.node) return + if (!e.detail.node) return + if (this.bindDynamicInput(input, e.detail.node, e.detail.input)) return + if (!e.detail.widget) return this._setWidget( subgraphInput, @@ -353,6 +364,7 @@ export class SubgraphNode extends LGraphNode implements BaseLGraph { this.invalidatePromotedViews() return } + if (this._bindsRemainingLink(subgraphInput, input)) return // Defer the actual demotion by a microtask instead of clearing // input.widgetId/.widget/._widget here: a rewire (this disconnect @@ -369,6 +381,7 @@ export class SubgraphNode extends LGraphNode implements BaseLGraph { queueMicrotask(() => { if (input._pendingDemotionToken !== token) return input._pendingDemotionToken = undefined + this.unbindDynamicInput(input) if (widget) this.ensureWidgetRemoved(widget) if (id && !this.inputs.some((i) => i.widgetId === id)) { @@ -503,8 +516,10 @@ export class SubgraphNode extends LGraphNode implements BaseLGraph { ): void { const quarantineValuesByInputName = this._readQuarantineHostValuesByName() - let valueIndex = 0 - for (const input of this.inputs) { + for (const [ + valueIndex, + { input } + ] of this._serializedInputWidgets().entries()) { if (!input.widgetId) continue const value = quarantineValuesByInputName.has(input.name) ? quarantineValuesByInputName.get(input.name) @@ -512,10 +527,34 @@ export class SubgraphNode extends LGraphNode implements BaseLGraph { if (value !== undefined) { useWidgetValueStore().restoreValue(input.widgetId, value) } - valueIndex += 1 } } + /** + * Inputs whose widget value the host serializes, in input order: promoted + * widgets, and widgets the host owns for a promoted dynamic input. + */ + private _serializedInputWidgets(): { + input: INodeInputSlot + value: unknown + }[] { + return this.inputs.flatMap((input) => { + if (input.widgetId) + return [ + { + input, + value: useWidgetValueStore().getWidget(input.widgetId)?.value + } + ] + const owned = this._extraWidgets.find( + (widget) => widget.name === input.widget?.name + ) + return owned && owned.serialize !== false + ? [{ input, value: owned.value }] + : [] + }) + } + private _readQuarantineHostValuesByName(): Map { return new Map( [ @@ -535,19 +574,21 @@ export class SubgraphNode extends LGraphNode implements BaseLGraph { override _internalConfigureAfterSlots() { this._rebindInputSubgraphSlots() - this.inputs = this.inputs.filter((input) => input._subgraphSlot) + const groupNames = this.inputs.flatMap((input) => + input._subgraphSlot ? [`${input.name}.`] : [] + ) + this.inputs = this.inputs.filter( + (input) => + input._subgraphSlot || + groupNames.some((groupName) => input.name.startsWith(groupName)) + ) this.invalidatePromotedViews() this._hydratePreviewExposures() for (const input of this.inputs) { const subgraphInput = input._subgraphSlot - if (!subgraphInput) { - console.warn( - `[SubgraphNode.configure] No subgraph input found for input ${input.name}, skipping` - ) - continue - } + if (!subgraphInput) continue this._addSubgraphInputListeners(subgraphInput, input) this._resolveInputWidget(subgraphInput, input) @@ -609,6 +650,21 @@ export class SubgraphNode extends LGraphNode implements BaseLGraph { this.invalidatePromotedViews() } + private _bindsRemainingLink( + subgraphInput: SubgraphInput, + input: INodeInputSlot + ): boolean { + return subgraphInput.linkIds.some((linkId) => { + const { inputNode, input: target } = + this.subgraph.getLink(linkId)?.resolve(this.subgraph) ?? {} + return ( + !!inputNode && + !!target && + this.bindDynamicInput(input, inputNode, target) + ) + }) + } + private _resolveInputWidget( subgraphInput: SubgraphInput, input: INodeInputSlot @@ -633,6 +689,7 @@ export class SubgraphNode extends LGraphNode implements BaseLGraph { console.warn('Failed to find corresponding input', link, inputNode) continue } + if (this.bindDynamicInput(input, inputNode, targetInput)) break const widget = inputNode.getWidgetFromSlot(targetInput) if (widget) { @@ -743,6 +800,20 @@ export class SubgraphNode extends LGraphNode implements BaseLGraph { return undefined } + /** Binds an input linked to an interior dynamic input group, if it is one. */ + protected bindDynamicInput( + _input: INodeInputSlot, + _interiorNode: LGraphNode, + _interiorInput: INodeInputSlot + ): boolean { + return false + } + + /** Releases an input bound by {@link bindDynamicInput}, if it was. */ + protected unbindDynamicInput(_input: INodeInputSlot): boolean { + return false + } + /** * Runs the host widget's `onRemove` (unregistering DOM widgets) and clears it. * Unlike {@link ensureWidgetRemoved}, dispatches no demotion event, so it is @@ -956,6 +1027,7 @@ export class SubgraphNode extends LGraphNode implements BaseLGraph { if (index !== -1) { this._extraWidgets.splice(index, 1) this._widgetSlotsDirty = true + if (widget.widgetId) useWidgetValueStore().deleteWidget(widget.widgetId) return } @@ -1041,11 +1113,9 @@ export class SubgraphNode extends LGraphNode implements BaseLGraph { serialized.properties = serializedProperties - const widgetValues = this.inputs.flatMap((input) => { - if (!input.widgetId) return [] - const value = useWidgetValueStore().getWidget(input.widgetId)?.value - return [isWidgetValue(value) ? value : undefined] - }) + const widgetValues = this._serializedInputWidgets().map(({ value }) => + isWidgetValue(value) ? value : undefined + ) if (widgetValues.some((value) => value !== undefined)) { serialized.widgets_values = widgetValues diff --git a/src/lib/litegraph/src/subgraph/hostInput.ts b/src/lib/litegraph/src/subgraph/hostInput.ts new file mode 100644 index 00000000000..72a3b07c383 --- /dev/null +++ b/src/lib/litegraph/src/subgraph/hostInput.ts @@ -0,0 +1,15 @@ +import type { + SubgraphInput, + SubgraphNode +} from '@comfyorg/graph-model/entityRegistry' + +/** The subgraph node's input bound to `subgraphInput`. */ +export function hostInputOf( + subgraphNode: SubgraphNode, + subgraphInput: SubgraphInput | undefined +) { + return ( + subgraphInput && + subgraphNode.inputs.find((input) => input._subgraphSlot === subgraphInput) + ) +} diff --git a/src/lib/litegraph/src/subgraph/subgraphUtils.ts b/src/lib/litegraph/src/subgraph/subgraphUtils.ts index 65ce8a21d4a..da2f449914d 100644 --- a/src/lib/litegraph/src/subgraph/subgraphUtils.ts +++ b/src/lib/litegraph/src/subgraph/subgraphUtils.ts @@ -37,6 +37,7 @@ import type { SubgraphIO } from '@comfyorg/graph-model/serialisation' +import { hostInputOf } from './hostInput' import { SubgraphInputNode } from './SubgraphInputNode' import { SubgraphOutputNode } from './SubgraphOutputNode' @@ -593,7 +594,10 @@ export function reorderSubgraphInputs( const subgraph = subgraphNode.subgraph const n = subgraph.inputs.length - if (subgraphNode.inputs.length !== n) { + const hostInputs = subgraph.inputs.map((subgraphInput) => + hostInputOf(subgraphNode, subgraphInput) + ) + if (hostInputs.some((input) => !input)) { console.error('reorderSubgraphInputs: host and subgraph inputs differ', { hostInputs: subgraphNode.inputs.length, subgraphInputs: n @@ -616,9 +620,23 @@ export function reorderSubgraphInputs( const oldOrder = subgraph.inputs.map((i) => i.id) const previousInputs = captureInputLayout(subgraphNode) - const orderedHostInputs = orderedIndices.map( - (index) => previousInputs.inputs[index] - ) + const orderedHostInputs = orderedIndices.flatMap((index) => { + const hostInput = hostInputs[index]! + const prefix = `${hostInput.name}.` + return [ + hostInput, + ...previousInputs.inputs.filter( + (input) => !hostInputs.includes(input) && input.name.startsWith(prefix) + ) + ] + }) + if (orderedHostInputs.length !== previousInputs.inputs.length) { + console.error('reorderSubgraphInputs: host has unowned inputs', { + hostInputs: previousInputs.inputs.length, + ownedInputs: orderedHostInputs.length + }) + return + } const result = replaceNodeInputs( subgraphNode, diff --git a/src/renderer/core/canvas/links/autogrowGroupDropState.ts b/src/renderer/core/canvas/links/autogrowGroupDropState.ts new file mode 100644 index 00000000000..dc31b921b2b --- /dev/null +++ b/src/renderer/core/canvas/links/autogrowGroupDropState.ts @@ -0,0 +1,60 @@ +import { readonly, ref, shallowReadonly, shallowRef } from 'vue' + +import type { ISlotType } from '@comfyorg/graph-model/interfaces' +import { toNodeId } from '@comfyorg/graph-model/nodeId' +import type { NodeId } from '@comfyorg/graph-model/nodeId' + +interface AutogrowGroupDropTarget { + nodeId: NodeId + groupName: string +} + +interface AutogrowGroupDropRow extends AutogrowGroupDropTarget { + dotClientPos: [number, number] +} + +const ioInputDragType = ref() +const hoveredGroup = shallowRef(null) + +export function setHoveredAutogrowGroup( + target: AutogrowGroupDropTarget | null +) { + const current = hoveredGroup.value + if ( + current?.nodeId === target?.nodeId && + current?.groupName === target?.groupName + ) + return + hoveredGroup.value = target +} + +export function setIoInputDragType(type: ISlotType | undefined) { + ioInputDragType.value = type + if (type === undefined) setHoveredAutogrowGroup(null) +} + +/** The revealed autogrow drop row under a client point, if any. */ +export function findAutogrowGroupDropRow( + clientX: number, + clientY: number +): AutogrowGroupDropRow | null { + if (ioInputDragType.value === undefined) return null + const row = document + .elementFromPoint(clientX, clientY) + ?.closest('[data-autogrow-group]') + const groupName = row?.dataset['autogrowGroup'] + const nodeIdAttr = + row?.closest('[data-node-id]')?.dataset['nodeId'] + if (!row || !groupName || !nodeIdAttr) return null + + const dot = row.querySelector('.slot-dot') ?? row + const rect = dot.getBoundingClientRect() + return { + nodeId: toNodeId(nodeIdAttr), + groupName, + dotClientPos: [rect.left + rect.width / 2, rect.top + rect.height / 2] + } +} + +export const autogrowIoInputDragType = shallowReadonly(ioInputDragType) +export const hoveredAutogrowGroup = readonly(hoveredGroup) diff --git a/src/renderer/extensions/vueNodes/components/LGraphNode.vue b/src/renderer/extensions/vueNodes/components/LGraphNode.vue index 9e0bc0ed08d..909476d4e7d 100644 --- a/src/renderer/extensions/vueNodes/components/LGraphNode.vue +++ b/src/renderer/extensions/vueNodes/components/LGraphNode.vue @@ -251,7 +251,10 @@ import { shouldHideLinkedCoreLoadAudioPlayer, shouldHideLinkedCoreMediaInputPreview } from '@/renderer/extensions/vueNodes/utils/linkedCoreMediaUtils' -import { nonWidgetedInputs } from '@/renderer/extensions/vueNodes/utils/nodeDataUtils' +import { + isAdvancedSocketInput, + nonWidgetedInputs +} from '@/renderer/extensions/vueNodes/utils/nodeDataUtils' import { getNodeErrorSeverity } from '@/renderer/extensions/vueNodes/utils/nodeErrorState' import { shouldExpand } from '@/renderer/extensions/vueNodes/widgets/registry/widgetRegistry' import { @@ -601,13 +604,14 @@ const renderedWidgetIds = computed(() => { }) }) -const hasRenderableWidgets = computed(() => renderedWidgetIds.value.length > 0) -const { canSelectInputs, nodeType, processedWidgets } = useProcessedWidgets( - () => nodeData, - () => renderedWidgetIds.value -) +const { canSelectInputs, gridRows, nodeType, processedWidgets } = + useProcessedWidgets( + () => nodeData, + () => renderedWidgetIds.value + ) +const hasRenderableWidgets = computed(() => gridRows.value.length > 0) const processedWidgetModel = computed(() => ({ - processedWidgets: processedWidgets.value, + processedWidgets: gridRows.value, nodeType: nodeType.value, canSelectInputs: canSelectInputs.value })) @@ -625,11 +629,12 @@ const showAdvancedInputsButton = computed(() => { return hasUnpromotedWidgets(node) } - const hasAdvancedWidgets = widgetIds.value.some((id) => { - const visibility = widgetValueStore.getWidgetVisibility(id) - if (visibility) return visibility.surfaces.vueNode === 'advanced' - return widgetValueStore.getWidget(id)?.options?.advanced - }) + const hasAdvancedWidgets = + widgetIds.value.some((id) => { + const visibility = widgetValueStore.getWidgetVisibility(id) + if (visibility) return visibility.surfaces.vueNode === 'advanced' + return widgetValueStore.getWidget(id)?.options?.advanced + }) || nodeData.inputs.some((input) => isAdvancedSocketInput(node, input)) const alwaysShowAdvanced = settingStore.get( 'Comfy.Node.AlwaysShowAdvancedWidgets' ) diff --git a/src/renderer/extensions/vueNodes/components/NodeSlots.vue b/src/renderer/extensions/vueNodes/components/NodeSlots.vue index c7b5973e334..7dabf558958 100644 --- a/src/renderer/extensions/vueNodes/components/NodeSlots.vue +++ b/src/renderer/extensions/vueNodes/components/NodeSlots.vue @@ -44,15 +44,20 @@ import { computed, onErrorCaptured, ref, useTemplateRef, watch } from 'vue' import { useErrorHandling } from '@/composables/useErrorHandling' +import { autogrowInputRole } from '@/core/graph/widgets/dynamicWidgets' import { st } from '@/i18n' -import type { INodeSlot } from '@/lib/litegraph/src/litegraph' +import type { INodeSlot, LGraphNode } from '@/lib/litegraph/src/litegraph' +import type { INodeInputSlot } from '@comfyorg/graph-model/slots' +import { useSettingStore } from '@/platform/settings/settingStore' import { useCanvasStore } from '@/renderer/core/canvas/canvasStore' import { syncSlotOffsets } from '@/renderer/core/layout/slots/syncSlotOffsets' import { layoutStore } from '@/renderer/core/layout/store/layoutStore' import { + isAdvancedSocketInput, linkedWidgetedInputs, nonWidgetedInputs } from '@/renderer/extensions/vueNodes/utils/nodeDataUtils' +import { app } from '@/scripts/app' import { useExecutionErrorStore } from '@/stores/executionErrorStore' import { useLinkStore } from '@/stores/linkStore' import type { GraphScope } from '@comfyorg/graph-model/graphScopeId' @@ -61,7 +66,7 @@ import { toRootGraphId } from '@comfyorg/graph-model/graphScopeId' import type { NodeState } from '@comfyorg/graph-model/nodeState' -import { locatorIdFromState } from '@/utils/graphTraversalUtil' +import { getNodeByState, locatorIdFromState } from '@/utils/graphTraversalUtil' import { cn } from '@comfyorg/tailwind-utils' import InputSlot from './InputSlot.vue' @@ -111,10 +116,26 @@ function isOutputConnected(index: number): boolean { return linkStore.isOutputSlotConnected(scope, nodeData.id, index) } -const filteredInputs = computed(() => [ - ...nonWidgetedInputs(nodeData.inputs), - ...linkedWidgetInputs.value -]) +const settingStore = useSettingStore() + +function isListedInput(node: LGraphNode, input: INodeInputSlot, index: number) { + if (autogrowInputRole(node, input)) return false + return ( + !!nodeData.showAdvanced || + settingStore.get('Comfy.Node.AlwaysShowAdvancedWidgets') || + isInputConnected(index) || + !isAdvancedSocketInput(node, input) + ) +} + +const filteredInputs = computed(() => { + const node = app.isGraphReady ? getNodeByState(app.rootGraph, nodeData) : null + const inputs = + unified || !node + ? nodeData.inputs + : nodeData.inputs.filter((input, i) => isListedInput(node, input, i)) + return [...nonWidgetedInputs(inputs), ...linkedWidgetInputs.value] +}) const layoutKey = computed(() => [ diff --git a/src/renderer/extensions/vueNodes/components/NodeWidgets.vue b/src/renderer/extensions/vueNodes/components/NodeWidgets.vue index ea853ca3f58..36e29ab4fdd 100644 --- a/src/renderer/extensions/vueNodes/components/NodeWidgets.vue +++ b/src/renderer/extensions/vueNodes/components/NodeWidgets.vue @@ -22,7 +22,7 @@