diff --git a/src/core/graph/widgets/dynamicGroupWidget.test.ts b/src/core/graph/widgets/dynamicGroupWidget.test.ts index 3ebc3b112ed..3d26a436204 100644 --- a/src/core/graph/widgets/dynamicGroupWidget.test.ts +++ b/src/core/graph/widgets/dynamicGroupWidget.test.ts @@ -31,6 +31,7 @@ import { useNodeDefStore } from '@/stores/nodeDefStore' import type { ComfyNodeDef } from '@/schemas/nodeDefSchema' import { graphToPrompt } from '@/utils/executionUtil' import { isWidgetVisibleOnSurface } from '@/types/widgetVisibility' +import { renameWidget } from '@/utils/widgetUtil' function setup(min = 0, max = 3) { const graph = new LGraph() @@ -354,6 +355,123 @@ describe('DynamicGroup widgets', () => { } ) + function setupTwiceDeepPromotion() { + const { node, graph, widget } = setup() + graph.remove(node) + const outerSubgraph = createTestSubgraph({ rootGraph: graph }) + const outerHost = createTestSubgraphNode(outerSubgraph) + graph.add(outerHost) + const innerSubgraph = createTestSubgraph({ rootGraph: graph }) + const innerHost = createTestSubgraphNode(innerSubgraph, { + parentGraph: outerSubgraph + }) + outerSubgraph.add(innerHost) + innerSubgraph.add(node) + widget('loras').value = 2 + const strength = widget('loras.1.strength') + expect( + promoteValueWidgetViaSubgraphInput(innerHost, node, strength).ok + ).toBe(true) + const innerPromoted = promotedInputWidget(innerHost.inputs[0]) + if (!innerPromoted) throw new Error('Inner promotion failed') + expect( + promoteValueWidgetViaSubgraphInput(outerHost, innerHost, innerPromoted).ok + ).toBe(true) + const outerInput = outerHost.inputs[0] + expect(promotedInputWidget(outerInput)?.label).toBe('LoRA #2 strength') + return { widget, innerHost, outerHost, outerSubgraph, outerInput } + } + + it.for([ + { label: 'LoRA #2 strength', expected: 'LoRA #1 strength' }, + { label: 'My strength', expected: 'My strength' } + ])( + 'shows $expected through two boundaries after removing a row before the promoted field $label', + ({ label, expected }) => { + const { widget, outerSubgraph, outerInput } = setupTwiceDeepPromotion() + outerSubgraph.renameInput(outerSubgraph.inputs[0], label) + + widget('loras.0').callback?.(undefined) + + expect(promotedInputWidget(outerInput)?.label).toBe(expected) + } + ) + + it.for([ + { + name: 'generated labels', + innerLabel: 'LoRA #2 strength', + outerLabel: 'LoRA #2 strength', + expectedInner: 'LoRA #1 strength', + expectedOuter: 'LoRA #1 strength' + }, + { + name: 'per-instance custom labels', + innerLabel: 'Inner strength', + outerLabel: 'Outer strength', + expectedInner: 'Inner strength', + expectedOuter: 'Outer strength' + } + ])( + 'preserves $name across multiple instances when rows are renumbered', + ({ innerLabel, outerLabel, expectedInner, expectedOuter }) => { + const { widget, innerHost, outerHost, outerInput } = + setupTwiceDeepPromotion() + const rootGraph = innerHost.subgraph.rootGraph + const otherOuterSubgraph = createTestSubgraph({ rootGraph }) + const otherOuterHost = createTestSubgraphNode(otherOuterSubgraph) + rootGraph.add(otherOuterHost) + const otherInnerHost = createTestSubgraphNode(innerHost.subgraph, { + parentGraph: otherOuterSubgraph + }) + otherOuterSubgraph.add(otherInnerHost) + const otherInnerPromoted = promotedInputWidget(otherInnerHost.inputs[0]) + assert.exists(otherInnerPromoted) + expect( + promoteValueWidgetViaSubgraphInput( + otherOuterHost, + otherInnerHost, + otherInnerPromoted + ).ok + ).toBe(true) + const otherOuterInput = otherOuterHost.inputs[0] + expect(promotedInputWidget(otherOuterInput)?.label).toBe( + 'LoRA #2 strength' + ) + const innerPromoted = promotedInputWidget(innerHost.inputs[0]) + const outerPromoted = promotedInputWidget(outerInput) + assert.exists(innerPromoted) + assert.exists(outerPromoted) + renameWidget(innerPromoted, innerHost, innerLabel) + renameWidget(outerPromoted, outerHost, outerLabel) + + widget('loras.0').callback?.(undefined) + + expect( + [ + innerHost.inputs[0], + otherInnerHost.inputs[0], + outerInput, + otherOuterInput + ].map((input) => promotedInputWidget(input)?.label) + ).toEqual([ + expectedInner, + 'LoRA #1 strength', + expectedOuter, + 'LoRA #1 strength' + ]) + } + ) + + it('removes a field promoted through two boundaries when its row is removed', async () => { + const { widget, innerHost, outerInput } = setupTwiceDeepPromotion() + + widget('loras.1').callback?.(undefined) + + expect(innerHost.inputs[0].link).toBeNull() + await vi.waitFor(() => expect(promotedInputWidget(outerInput)).toBeNull()) + }) + it.for([ { label: 'LoRA #2 strength', expected: 'LoRA 1행 strength' }, { label: 'My strength', expected: 'My strength' } diff --git a/src/core/graph/widgets/dynamicGroupWidget.ts b/src/core/graph/widgets/dynamicGroupWidget.ts index 3545f68b68e..afe7ab3a221 100644 --- a/src/core/graph/widgets/dynamicGroupWidget.ts +++ b/src/core/graph/widgets/dynamicGroupWidget.ts @@ -1,10 +1,13 @@ import { t } from '@/i18n' import { SUBGRAPH_INPUT_ID } from '@/lib/litegraph/src/constants' +import type { LGraph, Subgraph } from '@/lib/litegraph/src/LGraph' import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' +import type { SubgraphNode } from '@/lib/litegraph/src/subgraph/SubgraphNode' import type { IBaseWidget } from '@/lib/litegraph/src/types/widgets' import { VueOnlyWidget } from '@/lib/litegraph/src/widgets/VueOnlyWidget' import { captureInputLayout, + inputLink, replaceNodeInputs } from '@/lib/litegraph/src/node/slotLinks' import { @@ -18,6 +21,7 @@ import { useLitegraphService } from '@/services/litegraphService' import { useNodeDefStore } from '@/stores/nodeDefStore' import { useWidgetValueStore } from '@/stores/widgetValueStore' import { deriveWidgetSurfaces } from '@/types/widgetVisibility' +import { mapUniqueNodes } from '@/utils/graphTraversalUtil' import { isSubgraph } from '@/utils/typeGuardUtil' export function dynamicGroupWidget( @@ -120,14 +124,14 @@ export function dynamicGroupWidget( } } - function removeRow(index: number) { + function removeRow(index: number, hosts = createPromotionHostLookup()) { if (!node.widgets) return false const prefix = `${inputName}.${index}` const removed = node.widgets.filter( (widget) => widget.name === prefix || widget.name.startsWith(`${prefix}.`) ) if (!removed.length) return false - if (!removeRowInputs(prefix)) return false + if (!removeRowInputs(prefix, hosts)) return false for (const widget of removed.reverse()) { if (node.widgets.includes(widget)) node.removeWidget(widget) } @@ -136,16 +140,22 @@ export function dynamicGroupWidget( const row = Number(oldPrefix.slice(inputName.length + 1)) if (row <= index) continue const newPrefix = `${inputName}.${row - 1}` - renameRow(oldPrefix, newPrefix) + renameRow(oldPrefix, newPrefix, hosts) } return true } - function removeRowInputs(prefix: string) { + function removeRowInputs(prefix: string, hosts: PromotionHostLookup) { const previous = captureInputLayout(node) 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 result = replaceNodeInputs( node, previous, @@ -163,6 +173,8 @@ 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) return true } @@ -174,22 +186,10 @@ export function dynamicGroupWidget( return `${row} ${fields[field][1]?.display_name ?? field}` } - function renamePromotedInputLabel( - slot: number, - previous: string, - next: string - ) { - const graph = node.graph - if (!graph || !isSubgraph(graph)) return - const link = node.getInputLink(slot) - if (link?.origin_id !== SUBGRAPH_INPUT_ID) return - const promoted = graph.inputs[link.origin_slot] - if (promoted.label === previous) graph.renameInput(promoted, next) - } - function renameRowLabels( oldIndex: number, newIndex: number, + hosts: PromotionHostLookup, rowLabel?: string ) { for (const field of Object.keys(fields)) { @@ -204,11 +204,15 @@ export function dynamicGroupWidget( if (!input) continue if (input.label === previous) input.label = next input.localized_name = next - renamePromotedInputLabel(slot, previous, next) + renamePromotedInputLabel(node, slot, previous, next, hosts) } } - function renameRow(oldPrefix: string, newPrefix: string) { + function renameRow( + oldPrefix: string, + newPrefix: string, + hosts: PromotionHostLookup + ) { const rowLabel = rows().find((row) => row.name === oldPrefix)?.label for (const widget of node.widgets ?? []) { if (widget.name === oldPrefix || widget.name.startsWith(`${oldPrefix}.`)) @@ -222,11 +226,12 @@ export function dynamicGroupWidget( renameRowLabels( Number(oldPrefix.slice(inputName.length + 1)), Number(newPrefix.slice(inputName.length + 1)), + hosts, rowLabel ) } - function addRow(index: number) { + function addRow(index: number, hosts: PromotionHostLookup) { const previousSize: [number, number] = [...node.size] const start = node.widgets?.length ?? 0 const previous = captureInputLayout(node) @@ -249,7 +254,7 @@ export function dynamicGroupWidget( }) } }) - addRowFields(index) + addRowFields(index, hosts) const addedInputs = node.inputs.splice(previous.inputs.length) const inputs = [ ...previous.inputs, @@ -277,18 +282,22 @@ export function dynamicGroupWidget( return true } - function updateInputLabel(name: string, label: string) { + function updateInputLabel( + name: string, + label: string, + hosts: PromotionHostLookup + ) { const slot = node.findInputSlot(name) if (slot === -1) return const input = node.inputs[slot] const previous = input.localized_name if (!input.label || input.label === previous) input.label = label input.localized_name = label - if (previous) renamePromotedInputLabel(slot, previous, label) + if (previous) renamePromotedInputLabel(node, slot, previous, label, hosts) return input } - function addRowFields(index: number) { + function addRowFields(index: number, hosts: PromotionHostLookup) { for (const [fields, isOptional] of [ [template.required, false], [template.optional, true] @@ -302,7 +311,7 @@ export function dynamicGroupWidget( transformInputSpecV1ToV2(spec, { name, isOptional })), display_name: fieldLabel(index, field) }) - const input = updateInputLabel(name, fieldLabel(index, field)) + const input = updateInputLabel(name, fieldLabel(index, field), hosts) let auxiliaryIndex = 0 node.widgets?.slice(fieldStart).forEach((widget) => { const options = widget.options @@ -350,11 +359,12 @@ export function dynamicGroupWidget( const requested = Math.max(0, Math.trunc(value)) validateSavedRowCount(value, requested) const count = Math.max(min, requested) + const hosts = createPromotionHostLookup() for (let index = rows().length - 1; index >= count; index--) { - if (!removeRow(index)) break + if (!removeRow(index, hosts)) break } while (rows().length < count) { - if (!addRow(rows().length)) break + if (!addRow(rows().length, hosts)) break } publish() } @@ -363,6 +373,57 @@ export function dynamicGroupWidget( return { widget: controller } } +type PromotionHostLookup = (subgraph: Subgraph) => SubgraphNode[] + +function createPromotionHostLookup(): PromotionHostLookup { + let hosts: Map | undefined + return (subgraph) => { + if (!hosts) { + hosts = new Map() + for (const host of mapUniqueNodes(subgraph.rootGraph, (node) => + node.isSubgraphNode() ? node : undefined + )) { + const instances = hosts.get(host.subgraph) ?? [] + instances.push(host) + hosts.set(host.subgraph, instances) + } + } + return hosts.get(subgraph) ?? [] + } +} + +function renamePromotedInputLabel( + node: LGraphNode, + slot: number, + previous: string, + next: string, + hosts: PromotionHostLookup +) { + const graph = node.graph + if (!isSubgraph(graph)) return + const link = inputLink(graph, node.id, slot) + if (link?.origin_id !== SUBGRAPH_INPUT_ID) return + const promoted = graph.inputs[link.origin_slot] + if (promoted.label !== previous) return + graph.renameInput(promoted, next) + for (const host of hosts(graph)) + renamePromotedInputLabel(host, link.origin_slot, previous, next, hosts) +} + +function disconnectOrphanedPromotion( + graph: LGraph | Subgraph | null, + slot: number, + hosts: PromotionHostLookup +) { + if (!isSubgraph(graph) || graph.inputs[slot]?.linkIds.length) return + for (const host of hosts(graph)) { + const link = host.graph && inputLink(host.graph, host.id, slot) + if (link?.origin_id !== SUBGRAPH_INPUT_ID) continue + host.disconnectInput(slot) + disconnectOrphanedPromotion(host.graph, link.origin_slot, hosts) + } +} + class DynamicGroupNoticeWidget extends VueOnlyWidget { protected get vueOnlyLabel(): string { return this.label ?? this.name diff --git a/src/lib/litegraph/src/subgraph/SubgraphNode.ts b/src/lib/litegraph/src/subgraph/SubgraphNode.ts index f05672b379e..c0999e1ea8f 100644 --- a/src/lib/litegraph/src/subgraph/SubgraphNode.ts +++ b/src/lib/litegraph/src/subgraph/SubgraphNode.ts @@ -214,20 +214,24 @@ export class SubgraphNode extends LGraphNode implements BaseLGraph { subgraphEvents.addEventListener( 'renaming-input', (e) => { - const { index, newName } = e.detail + const { index, oldName, newName } = e.detail const input = this.inputs.at(index) if (!input) { console.error('Subgraph input not found') return } - input.label = newName + const previousLabel = input.label ?? input.name + if (previousLabel === oldName) input.label = newName // Do NOT change input.widget.name — it is the stable internal // identifier used by onGraphConfigured (widgetInputs.ts) to match // inputs to widgets. Changing it to the display label would cause // collisions when two promoted inputs share the same label. if (input.widgetId) { - useWidgetValueStore().setLabel(input.widgetId, newName) + const store = useWidgetValueStore() + const widgetLabel = + store.getWidget(input.widgetId)?.label ?? previousLabel + if (widgetLabel === oldName) store.setLabel(input.widgetId, newName) } this.invalidatePromotedViews() this.graph?.trigger('node:slot-label:changed', { diff --git a/src/workbench/extensions/agent/crdt/liveGraphApplier.changeTracker.test.ts b/src/workbench/extensions/agent/crdt/liveGraphApplier.changeTracker.test.ts index 329a9e564fd..509fc126e53 100644 --- a/src/workbench/extensions/agent/crdt/liveGraphApplier.changeTracker.test.ts +++ b/src/workbench/extensions/agent/crdt/liveGraphApplier.changeTracker.test.ts @@ -1,7 +1,15 @@ import { applyOps } from '@comfyorg/comfy-multi-player' import type { Op, WidgetCatalog } from '@comfyorg/comfy-multi-player' import { fromPartial } from '@total-typescript/shoehorn' -import { beforeEach, describe, expect, it, onTestFinished, vi } from 'vitest' +import { + assert, + beforeEach, + describe, + expect, + it, + onTestFinished, + vi +} from 'vitest' import { markRaw, ref } from 'vue' import { LGraph, LGraphNode, LiteGraph } from '@/lib/litegraph/src/litegraph' @@ -13,9 +21,12 @@ import type { ComfyApi } from '@/scripts/api' import type { ComfyApp } from '@/scripts/app' import { ChangeTracker } from '@/scripts/changeTracker' import { toNodeId } from '@/types/nodeId' +import { toRootGraphId } from '@/types/graphScopeId' +import { useLitegraphService } from '@/services/litegraphService' import { followedDoc } from './__fixtures__/followedDoc' import { LiveGraphApplier } from './liveGraphApplier' +import { attachDocOpMinter } from './docOpMinter' const appState = vi.hoisted(() => ({ rootGraph: undefined as LGraph | undefined @@ -59,10 +70,37 @@ class TestSink extends LGraphNode { } } +class DynamicGroupTest extends LGraphNode { + constructor() { + super('DynamicGroupTest') + this.comfyClass = 'DynamicGroupTest' + this.serialize_widgets = true + this.addWidget('text', 'before', 'head', () => {}) + useLitegraphService().addNodeInput(this, { + name: 'rows', + type: 'COMFY_DYNAMICGROUP_V3', + isOptional: false, + min: 0, + max: 3, + template: { required: { strength: ['FLOAT', { default: 1 }] } } + }) + this.addWidget('text', 'after', 'tail', () => {}) + } +} + const CATALOG: WidgetCatalog = { types: { TestSource: { widget_order: ['steps'] }, - TestSink: { widget_order: [] } + TestSink: { widget_order: [] }, + DynamicGroupTest: { + widget_order: [ + 'before', + 'rows', + 'rows.0.strength', + 'rows.1.strength', + 'after' + ] + } } } const CONTEXT = { actor: 'agent:test', opIds: ['op-1'] } @@ -94,6 +132,7 @@ async function workflowJson(graph: LGraph): Promise { beforeEach(() => { LiteGraph.registerNodeType('TestSource', TestSource) LiteGraph.registerNodeType('TestSink', TestSink) + LiteGraph.registerNodeType('DynamicGroupTest', DynamicGroupTest) ChangeTracker.isLoadingGraph = false }) @@ -174,4 +213,106 @@ describe('LiveGraphApplier with the real change tracker', () => { expect(tracker.activeState).toEqual(await workflowJson(graph)) expect(tracker.activeState).not.toEqual(beforeFrame) }) + + it('resizes an existing DynamicGroup remotely as one undo unit without outbound echo', async () => { + const graph = new LGraph() + appState.rootGraph = graph + onTestFinished(() => { + appState.rootGraph = undefined + }) + const node = LiteGraph.createNode('DynamicGroupTest') + assert.exists(node) + graph.add(node) + const count = node.widgets?.find((widget) => widget.name === 'rows') + assert.exists(count) + count.value = 2 + const first = node.widgets?.find( + (widget) => widget.name === 'rows.0.strength' + ) + const second = node.widgets?.find( + (widget) => widget.name === 'rows.1.strength' + ) + assert.exists(first) + assert.exists(second) + first.value = 0.5 + second.value = 0.7 + expect(node.serialize().widgets_values).toEqual([ + 'head', + 2, + 0.5, + 0.7, + 'tail' + ]) + const beforeFrame = await workflowJson(graph) + const { flags, ...serialized } = node.serialize() + const { doc, collector } = followedDoc( + { nodes: [{ ...serialized, flags: { ...flags } }], links: [] }, + CATALOG + ) + collector.take() + const applier = new LiveGraphApplier({ getGraph: () => graph }) + const tracker = markRaw( + new ChangeTracker(fromPartial({ path: '/dynamic.json' }), beforeFrame) + ) + useWorkflowStore().activeWorkflow = fromPartial({ changeTracker: tracker }) + graph.list_of_graphcanvas = [ + fromPartial({ + emitBeforeChange: () => tracker.beforeChange(), + emitAfterChange: () => tracker.afterChange(), + setDirty: () => {}, + deselect: () => {}, + checkPanels: () => {} + }) + ] + const enqueue = vi.fn() + const minter = attachDocOpMinter({ + isEnabled: () => true, + isDocBound: () => true, + getGraph: () => graph, + boundRootGraphId: () => toRootGraphId(graph.id), + docInputNames: () => [], + enqueue + }) + onTestFinished(() => minter.detach()) + const resize = op({ + op: 'set_widget', + node_id: 1, + widget: 'rows', + value: 0 + }) + const after = { + ...op({ + op: 'set_widget', + node_id: 1, + widget: 'after', + value: 'remote tail' + }), + op_id: 'op-after'.padEnd(32, '0') + } + + expect( + applyOps(doc, [resize, after], CATALOG).outcomes.map( + ({ outcome }) => outcome + ) + ).toEqual(['applied', 'applied']) + applier.applyChanges(doc, collector.take(), CONTEXT) + + expect(node.serialize().widgets_values).toEqual(['head', 0, 'remote tail']) + expect(tracker.undoQueue).toEqual([beforeFrame]) + expect(tracker.activeState).toEqual(await workflowJson(graph)) + const afterWidget = node.widgets?.find((widget) => widget.name === 'after') + assert.exists(afterWidget) + afterWidget.value = 'human tail' + await vi.waitFor(() => + expect(enqueue).toHaveBeenCalledExactlyOnceWith([ + { + op: 'set_widget', + node_id: '1', + widget: 'after', + value: 'human tail', + old: 'remote tail' + } + ]) + ) + }) })