diff --git a/src/core/graph/widgets/dynamicGroupWidget.test.ts b/src/core/graph/widgets/dynamicGroupWidget.test.ts index fec0992e797..22eb83707f5 100644 --- a/src/core/graph/widgets/dynamicGroupWidget.test.ts +++ b/src/core/graph/widgets/dynamicGroupWidget.test.ts @@ -464,14 +464,134 @@ describe('DynamicGroup widgets', () => { ) it('removes a field promoted through two boundaries when its row is removed', async () => { - const { widget, innerHost, outerInput } = setupTwiceDeepPromotion() + const { widget, innerHost, outerHost, outerInput } = + setupTwiceDeepPromotion() widget('loras.1').callback?.(undefined) - expect(innerHost.inputs[0].link).toBeNull() + expect(innerHost.inputs).toEqual([]) + expect(outerHost.inputs).toEqual([]) await vi.waitFor(() => expect(promotedInputWidget(outerInput)).toBeNull()) }) + it('removes the deleted row inputs from both hosts while keeping the next row', () => { + const { node, widget, innerHost, outerHost } = setupTwiceDeepPromotion() + widget('loras').value = 3 + expect( + promoteValueWidgetViaSubgraphInput( + innerHost, + node, + widget('loras.2.strength') + ).ok + ).toBe(true) + const innerPromoted = promotedInputWidget(innerHost.inputs[1]) + assert.exists(innerPromoted) + expect( + promoteValueWidgetViaSubgraphInput(outerHost, innerHost, innerPromoted).ok + ).toBe(true) + + widget('loras.1').callback?.(undefined) + + expect(innerHost.subgraph.inputs).toHaveLength(1) + expect(outerHost.subgraph.inputs).toHaveLength(1) + expect( + [innerHost.inputs, outerHost.inputs].map((inputs) => + inputs.map((input) => promotedInputWidget(input)?.label) + ) + ).toEqual([['LoRA #2 strength'], ['LoRA #2 strength']]) + }) + + it('drops the rerouted promotion link of a removed subgraph input', () => { + const { node, widget, innerHost } = setupTwiceDeepPromotion() + const subgraph = innerHost.subgraph + const linkId = node.inputs.find( + (input) => input.name === 'loras.1.strength' + )?.link + const link = linkId == null ? undefined : subgraph.links.get(linkId) + assert.exists(link) + assert.exists(subgraph.createReroute([0, 0], link)) + + widget('loras.1').callback?.(undefined) + + expect(subgraph.inputs).toEqual([]) + expect(subgraph.floatingLinks.size).toBe(0) + }) + + it('drops the rerouted link of a later promoted field in the same row', () => { + const { node, widget, innerHost } = setupTwiceDeepPromotion() + const subgraph = innerHost.subgraph + expect( + promoteValueWidgetViaSubgraphInput( + innerHost, + node, + widget('loras.1.enabled') + ).ok + ).toBe(true) + const linkId = node.inputs.find( + (input) => input.name === 'loras.1.enabled' + )?.link + const link = linkId == null ? undefined : subgraph.links.get(linkId) + assert.exists(link) + assert.exists(subgraph.createReroute([0, 0], link)) + + widget('loras.1').callback?.(undefined) + + expect(subgraph.inputs).toEqual([]) + expect(subgraph.floatingLinks.size).toBe(0) + }) + + it('drops a floating host link left on a removed promoted input', () => { + const { widget, innerHost, outerSubgraph } = setupTwiceDeepPromotion() + const linkId = innerHost.inputs[0].link + const link = linkId == null ? undefined : outerSubgraph.links.get(linkId) + assert.exists(link) + assert.exists(outerSubgraph.createReroute([0, 0], link)) + link.disconnect(outerSubgraph, 'input') + expect(outerSubgraph.floatingLinks.size).toBe(1) + + widget('loras.1').callback?.(undefined) + + expect(innerHost.inputs).toEqual([]) + expect(outerSubgraph.floatingLinks.size).toBe(0) + }) + + it('removes every promoted field of the deleted row through two boundaries', () => { + const { node, widget, innerHost, outerHost } = setupTwiceDeepPromotion() + widget('loras').value = 3 + expect( + promoteValueWidgetViaSubgraphInput( + innerHost, + node, + widget('loras.1.enabled') + ).ok + ).toBe(true) + const enabled = promotedInputWidget(innerHost.inputs[1]) + assert.exists(enabled) + expect( + promoteValueWidgetViaSubgraphInput(outerHost, innerHost, enabled).ok + ).toBe(true) + expect( + promoteValueWidgetViaSubgraphInput( + innerHost, + node, + widget('loras.2.strength') + ).ok + ).toBe(true) + const strength = promotedInputWidget(innerHost.inputs[2]) + assert.exists(strength) + expect( + promoteValueWidgetViaSubgraphInput(outerHost, innerHost, strength).ok + ).toBe(true) + + widget('loras.1').callback?.(undefined) + + expect( + [innerHost.inputs, outerHost.inputs].map((inputs) => + inputs.map((input) => promotedInputWidget(input)?.label) + ) + ).toEqual([['LoRA #2 strength'], ['LoRA #2 strength']]) + }) + it('disconnects the deleted row at the outer host while keeping the next row connected', async () => { const { node, widget, innerHost, outerHost, outerInput } = setupTwiceDeepPromotion() @@ -509,8 +629,8 @@ describe('DynamicGroup widgets', () => { await vi.waitFor(() => expect(promotedInputWidget(outerInput)).toBeNull()) expect(graph.getLink(removed.id)).toBeUndefined() expect(graph.getLink(otherRemoved.id)).toBeUndefined() - expect(outerInput.link).toBeNull() - expect(otherHost.inputs[0].link).toBeNull() + expect(outerHost.inputs).toEqual([survivingInput]) + expect(otherHost.inputs).toHaveLength(1) assert.exists(survivingInput.link) expect(graph.getLink(survivingInput.link)).toBe(retained) expect(promotedInputWidget(survivingInput)?.value).toBe(0.37) diff --git a/src/core/graph/widgets/dynamicGroupWidget.ts b/src/core/graph/widgets/dynamicGroupWidget.ts index 46a34295646..23d6abe21bc 100644 --- a/src/core/graph/widgets/dynamicGroupWidget.ts +++ b/src/core/graph/widgets/dynamicGroupWidget.ts @@ -150,12 +150,17 @@ export function dynamicGroupWidget( const inputs = previous.inputs.filter( (input) => !input.name.startsWith(`${prefix}.`) ) - const promotedSlots = previous.inputs.flatMap((input) => { - const link = previous.links.get(input) - return !inputs.includes(input) && link?.origin_id === SUBGRAPH_INPUT_ID - ? [link.origin_slot] - : [] - }) + const graph = node.graph + const promotedInputs = new Set( + previous.inputs.flatMap((input) => { + const link = previous.links.get(input) + return isSubgraph(graph) && + !inputs.includes(input) && + link?.origin_id === SUBGRAPH_INPUT_ID + ? [graph.inputs[link.origin_slot]] + : [] + }) + ) const result = replaceNodeInputs( node, previous, @@ -173,8 +178,9 @@ export function dynamicGroupWidget( previous.inputs.forEach((input, slot) => { if (!inputs.includes(input)) node.onInputRemoved?.(slot, input) }) - for (const slot of promotedSlots) - disconnectOrphanedPromotion(node.graph, slot, hosts) + if (isSubgraph(graph)) + for (const input of promotedInputs) + disconnectOrphanedPromotion(graph, graph.inputs.indexOf(input), hosts) return true } @@ -415,14 +421,32 @@ function disconnectOrphanedPromotion( slot: number, hosts: PromotionHostLookup ) { - if (!isSubgraph(graph) || graph.inputs[slot]?.linkIds.length) return + if (!isSubgraph(graph) || graph.inputs[slot]?.linkIds.length !== 0) return + const input = graph.inputs[slot] for (const host of hosts(graph)) { + removeFloatingHostLinks(host, slot) const link = host.graph && inputLink(host.graph, host.id, slot) if (!link) continue host.disconnectInput(slot) if (link.origin_id === SUBGRAPH_INPUT_ID) disconnectOrphanedPromotion(host.graph, link.origin_slot, hosts) } + graph.removeInput(input) + if (!graph.inputs.includes(input)) removeFloatingLinksFrom(graph, slot) +} + +function removeFloatingLinksFrom(graph: Subgraph, slot: number) { + for (const link of graph.floatingLinks.values()) { + if (link.origin_id !== SUBGRAPH_INPUT_ID) continue + if (link.origin_slot === slot) graph.removeFloatingLink(link) + else if (link.origin_slot > slot) link.origin_slot-- + } +} + +function removeFloatingHostLinks(host: SubgraphNode, slot: number) { + for (const link of host.graph?.floatingLinks.values() ?? []) + if (link.target_id === host.id && link.target_slot === slot) + host.graph?.removeFloatingLink(link) } class DynamicGroupNoticeWidget extends VueOnlyWidget {