diff --git a/browser_tests/assets/inputs/boolean_input.json b/browser_tests/assets/inputs/boolean_input.json new file mode 100644 index 00000000000..2fe77485b71 --- /dev/null +++ b/browser_tests/assets/inputs/boolean_input.json @@ -0,0 +1,38 @@ +{ + "last_node_id": 1, + "last_link_id": 0, + "nodes": [ + { + "id": 1, + "type": "PrimitiveBoolean", + "pos": [300, 200], + "size": [350, 240], + "flags": {}, + "order": 0, + "mode": 0, + "inputs": [], + "outputs": [ + { + "name": "BOOLEAN", + "type": "BOOLEAN", + "links": [] + } + ], + "properties": { + "Node name for S&R": "PrimitiveBoolean" + }, + "widgets_values": [true], + "title": "Boolean" + } + ], + "links": [], + "groups": [], + "config": {}, + "extra": { + "ds": { + "scale": 0.8, + "offset": [0, 0] + } + }, + "version": 0.4 +} diff --git a/browser_tests/assets/inputs/checkpoint_loader.json b/browser_tests/assets/inputs/checkpoint_loader.json new file mode 100644 index 00000000000..d3e6c8ca13a --- /dev/null +++ b/browser_tests/assets/inputs/checkpoint_loader.json @@ -0,0 +1,49 @@ +{ + "last_node_id": 4, + "last_link_id": 0, + "nodes": [ + { + "id": 4, + "type": "CheckpointLoaderSimple", + "pos": [400, 300], + "size": [350, 240], + "flags": {}, + "order": 0, + "mode": 0, + "outputs": [ + { + "name": "MODEL", + "type": "MODEL", + "links": [], + "slot_index": 0 + }, + { + "name": "CLIP", + "type": "CLIP", + "links": [], + "slot_index": 1 + }, + { + "name": "VAE", + "type": "VAE", + "links": [], + "slot_index": 2 + } + ], + "properties": { + "Node name for S&R": "CheckpointLoaderSimple" + }, + "widgets_values": ["sd_xl_base_1.0.safetensors"] + } + ], + "links": [], + "groups": [], + "config": {}, + "extra": { + "ds": { + "scale": 0.8, + "offset": [0, 0] + } + }, + "version": 0.4 +} diff --git a/browser_tests/tests/sidebar/modelLibrary.spec.ts b/browser_tests/tests/sidebar/modelLibrary.spec.ts index de32de68b5f..8e5ff69f297 100644 --- a/browser_tests/tests/sidebar/modelLibrary.spec.ts +++ b/browser_tests/tests/sidebar/modelLibrary.spec.ts @@ -301,3 +301,67 @@ test.describe('Model library sidebar - empty state', () => { }) }) }) + +test.describe('Model library drops on Vue nodes', { tag: '@vue-nodes' }, () => { + test.use({ initialSettings: { 'Comfy.ModelLibrary.UseAssetBrowser': false } }) + + test.beforeEach(async ({ comfyPage }) => { + await comfyPage.modelLibrary.mockFoldersWithFiles(MOCK_FOLDERS) + await comfyPage.workflow.loadWorkflow('inputs/checkpoint_loader') + const tab = comfyPage.menu.modelLibraryTab + await tab.open() + await tab.refreshButton.click() + await tab.getFolderByLabel('checkpoints').click() + await expect(tab.getLeafByLabel('dreamshaper_8')).toBeVisible() + await comfyPage.command.executeCommand('Comfy.Canvas.FitView') + await comfyPage.canvasOps.waitForViewToSettle() + }) + + test.afterEach(async ({ comfyPage }) => { + await comfyPage.modelLibrary.clearMocks() + await comfyPage.canvasOps.resetView() + }) + + test('updates an existing loader without creating another node', async ({ + comfyPage + }) => { + const [loader] = await comfyPage.nodeOps.getNodeRefsByType( + 'CheckpointLoaderSimple' + ) + const filename = await loader.getWidgetByName('ckpt_name') + const node = comfyPage.vueNodes.getNodeByTitle('Load Checkpoint') + const nodeCount = await comfyPage.nodeOps.getGraphNodesCount() + await comfyPage.menu.modelLibraryTab + .getLeafByLabel('dreamshaper_8') + .dragTo(node) + await comfyPage.nextFrame() + await expect(node).toContainText('dreamshaper_8.safetensors') + await expect + .poll(() => filename.getValue()) + .toBe('dreamshaper_8.safetensors') + await expect + .poll(() => comfyPage.nodeOps.getGraphNodesCount()) + .toBe(nodeCount) + }) + + test('still creates a loader when dropped onto empty canvas', async ({ + comfyPage + }) => { + await comfyPage.menu.topbar.newWorkflowButton.click() + await expect.poll(() => comfyPage.nodeOps.getGraphNodesCount()).toBe(0) + const box = (await comfyPage.canvas.boundingBox())! + await comfyPage.menu.modelLibraryTab + .getLeafByLabel('dreamshaper_8') + .dragTo(comfyPage.canvas, { + targetPosition: { x: box.width / 2, y: box.height / 2 } + }) + await comfyPage.nextFrame() + await expect.poll(() => comfyPage.nodeOps.getGraphNodesCount()).toBe(1) + const [loader] = await comfyPage.nodeOps.getNodeRefsByType( + 'CheckpointLoaderSimple' + ) + await expect + .poll(async () => (await loader.getWidgetByName('ckpt_name')).getValue()) + .toBe('dreamshaper_8.safetensors') + }) +}) diff --git a/browser_tests/tests/vueNodes/widgets/widgetValuePersistence.spec.ts b/browser_tests/tests/vueNodes/widgets/widgetValuePersistence.spec.ts index 2dc041fd4b1..af2866e57f8 100644 --- a/browser_tests/tests/vueNodes/widgets/widgetValuePersistence.spec.ts +++ b/browser_tests/tests/vueNodes/widgets/widgetValuePersistence.spec.ts @@ -1,3 +1,5 @@ +import type { Locator } from '@playwright/test' + import { comfyExpect as expect, comfyPageFixture as test @@ -5,6 +7,8 @@ import { import { WidgetSelectDropdownFixture } from '@e2e/fixtures/components/WidgetSelectDropdown' import { createMockJob } from '@e2e/fixtures/helpers/AssetsHelper' import { TestIds } from '@e2e/fixtures/selectors' +import type { DraftPayloadV2 } from '@/platform/workflow/persistence/base/draftTypes' +import type { ComfyWorkflowJSON } from '@/platform/workflow/validation/schemas/workflowSchema' import { assetPath } from '@e2e/fixtures/utils/paths' import { mockViewFiles } from '@e2e/fixtures/utils/viewFileMocks' @@ -16,6 +20,54 @@ test.describe( await comfyPage.workflow.setupWorkflowsDirectory({}) }) + test.describe('Boolean drafts', () => { + test.use({ initialSettings: { 'Comfy.Workflow.Persist': true } }) + + for (const { action, toggleValue } of [ + { action: 'click', toggleValue: (toggle: Locator) => toggle.click() }, + { + action: 'Enter', + toggleValue: (toggle: Locator) => toggle.press('Enter') + } + ]) { + test(`keeps a toggle-only ${action} edit after reload`, async ({ + comfyPage + }) => { + await comfyPage.workflow.loadWorkflow('inputs/boolean_input') + await comfyPage.workflow.waitForDraftPersisted() + await comfyPage.workflow.reloadAndWaitForApp() + const toggle = comfyPage.vueNodes + .getNodeByTitle('Boolean') + .getByRole('switch') + await expect(toggle).toBeChecked() + + await toggleValue(toggle) + await expect(toggle).not.toBeChecked() + + await expect + .poll(() => + comfyPage.page.evaluate(() => { + return Object.keys(localStorage).some((key) => { + if (!key.startsWith('Comfy.Workflow.Draft.v2:')) return false + const payload: DraftPayloadV2 = JSON.parse( + localStorage.getItem(key)! + ) + const workflow: ComfyWorkflowJSON = JSON.parse(payload.data) + return workflow.nodes.some( + (node) => + node.type === 'PrimitiveBoolean' && + Object.values(node.widgets_values ?? {})[0] === false + ) + }) + }) + ) + .toBe(true) + await comfyPage.workflow.reloadAndWaitForApp() + await expect(toggle).not.toBeChecked() + }) + } + }) + test('an emptied text widget remains empty after save and reopen', async ({ comfyPage }) => { diff --git a/docs/adr/README.md b/docs/adr/README.md index 4b7e4db25d6..0ed3db33649 100644 --- a/docs/adr/README.md +++ b/docs/adr/README.md @@ -66,6 +66,7 @@ An Architecture Decision Record captures an important architectural decision mad | [TESTING-E2E-0027](TESTING-E2E-0027-coverage-measurement-and-test-selection.md) | E2E Coverage Measurement and Test Selection | Proposed | 2026-09-01 | | [WEBSITE-ACCEPTANCE-0036](WEBSITE-ACCEPTANCE-0036-separate-live-customer-tests-from-offline-ci.md) | Separate live customer acceptance from offline CI | Proposed | 2026-09-21 | | [WEBSITE-REQUESTS-0030](WEBSITE-REQUESTS-0030-creator-widgets-and-router-request-composition.md) | Creator widgets and Router request composition | Deprecated | 2026-09-09 | +| [WIDGET-GROUP-0031](WIDGET-GROUP-0031-dynamic-group-widget-lifecycle.md) | DynamicGroup Widget Lifecycle | Proposed | 2026-09-11 | | [WIDGET-SERIALIZATION-0006](WIDGET-SERIALIZATION-0006-preserve-primitive-widget-values-across-copy-and-paste.md) | PrimitiveNode Widget Restoration Lifecycle | Accepted | 2026-02-22 | | [WORKSHOP-TELEMETRY-0034](WORKSHOP-TELEMETRY-0034-browser-delivery-health.md) | Browser delivery health for Models | Proposed | 2026-09-18 | | [WORKSHOP-VALIDATION-0036](WORKSHOP-VALIDATION-0036-declarative-input-constraints.md) | Declarative input constraints | Proposed | 2026-09-22 | diff --git a/docs/adr/WIDGET-GROUP-0031-dynamic-group-widget-lifecycle.md b/docs/adr/WIDGET-GROUP-0031-dynamic-group-widget-lifecycle.md new file mode 100644 index 00000000000..a75dcc41ea5 --- /dev/null +++ b/docs/adr/WIDGET-GROUP-0031-dynamic-group-widget-lifecycle.md @@ -0,0 +1,62 @@ +# ADR-WIDGET-GROUP-0031: DynamicGroup Widget Lifecycle + +Date: 2026-09-11 + +## Status + +Proposed + +## Context + +DynamicGroup repeats a backend widget template. Its row count is workflow state; +execution consumes individually named fields. A group can sit between ordinary +widgets or inside a DynamicCombo option, so restoration must create its fields +before the following saved values are applied. + +## Decision + +DynamicGroup editing is Node 2.0-only. Group fields and presentation controls +are excluded from the legacy canvas through widget surface visibility. Shared +restoration and serialization remain active so switching renderers does not +remove stored rows or values. Each group has one non-interactive canvas notice +identifying it as Node 2.0-only, regardless of its row count. The notice is excluded +from workflow values and prompts and hidden in Node 2.0 and the properties panel. + +Use ordinary widgets and their existing store, renderer and prompt serializer +for each field. A hidden, workflow-serialized controller precedes the fields and +recreates rows when its saved count is assigned. It retains that count while a +containing DynamicCombo detaches the widgets to cache their values. + +Row headers and the add button are presentation widgets excluded from both +workflow values and execution. The count is also excluded from execution. +Deleting a row renumbers the remaining fields and preserves their values and +connections. API import restores submitted rows in encounter order with contiguous +indices, including rows exceeding `max`. Saving and reloading preserves those +rows. Adding a row remains disabled at or above `max`; execution validity belongs +to backend validation. Restoration uses the common positional or named binding: +missing values retain defaults, and values beyond the current widgets are unused. +Template changes do not trigger a group-specific value-count or type check; +positional saves can therefore shift values when a template changes. Allocation +rejects row counts exceeding both the configured maximum and the number of saved +widget values, without requiring values for every current field. Invalid group +definitions fail construction so the existing error-node restoration retains +the original workflow data. + +Reject a second group-owned value store or a composite widget that renders its +children itself: both would duplicate existing restoration, error, link and +visibility handling. Group fields resolve their original template definitions +so combo refresh and later row creation see the same options. + +## Consequences + +### Positive + +Field widgets retain the common editing, error and persistence behavior without +new node instance properties or a separate workflow format. + +### Negative + +The controller must precede its fields in serialized widget order. Presentation +widgets require unique names but must never become backend input keys. This +feature covers widget templates; socket-only and nested dynamic templates are +outside the backend contract. diff --git a/packages/object-info-parser/src/__tests__/nodeDefSchema.test.ts b/packages/object-info-parser/src/__tests__/nodeDefSchema.test.ts index 1139786c776..9b39e6ae930 100644 --- a/packages/object-info-parser/src/__tests__/nodeDefSchema.test.ts +++ b/packages/object-info-parser/src/__tests__/nodeDefSchema.test.ts @@ -23,6 +23,75 @@ const EXAMPLE_NODE_DEF: ComfyNodeDef = { } describe('validateNodeDef', () => { + it.for([ + { min: 2, max: 1 }, + { max: 21 }, + { max: -1 }, + { min: 0.5 }, + { template: 'invalid' }, + { template: {} }, + { + template: { + required: { + nested: [ + 'COMFY_DYNAMICGROUP_V3', + { min: 1, template: { required: { value: ['STRING', {}] } } } + ] + } + } + }, + { template: { required: { nested: ['COMFY_AUTOGROW_V3', {}] } } }, + { template: { required: { nested: ['COMFY_DYNAMICCOMBO_V3', {}] } } }, + { template: { required: { value: ['FLOAT', { forceInput: true }] } } }, + { template: { required: { 'bad.name': ['STRING', {}] } } }, + { + template: { + required: { value: ['STRING', {}] }, + optional: { value: ['STRING', {}] } + } + } + ])( + 'rejects malformed DynamicGroup definitions at the boundary: %j', + (options) => { + expect( + validateComfyNodeDef( + { + ...EXAMPLE_NODE_DEF, + input: { + required: { + rows: [ + 'COMFY_DYNAMICGROUP_V3', + { + template: { required: { value: ['STRING', {}] } }, + ...options + } + ] + } + } + }, + () => {} + ) + ).toBeNull() + } + ) + + it('retains valid DynamicGroup template options and supplies count defaults', () => { + const template = { + required: { + model: ['COMBO', { remote: { route: '/models', refresh_button: true } }] + }, + optional: { strength: ['FLOAT', { default: 1 }] } + } + const parsed = validateComfyNodeDef({ + ...EXAMPLE_NODE_DEF, + input: { required: { rows: ['COMFY_DYNAMICGROUP_V3', { template }] } } + }) + expect(parsed?.input?.required?.rows).toEqual([ + 'COMFY_DYNAMICGROUP_V3', + { template, min: 0, max: 20 } + ]) + }) + it('accepts a valid node definition', () => { expect(validateComfyNodeDef(EXAMPLE_NODE_DEF)).not.toBeNull() }) diff --git a/packages/object-info-parser/src/__tests__/sanitizeUserContent.test.ts b/packages/object-info-parser/src/__tests__/sanitizeUserContent.test.ts index 6d0b91b942a..d2dd4031f11 100644 --- a/packages/object-info-parser/src/__tests__/sanitizeUserContent.test.ts +++ b/packages/object-info-parser/src/__tests__/sanitizeUserContent.test.ts @@ -1,7 +1,7 @@ import { describe, expect, it } from 'vitest' import { sanitizeUserContent } from '../helpers/sanitizeUserContent' -import type { ComfyNodeDef } from '../schemas/nodeDefSchema' +import type { ComfyNodeDef, InputSpec } from '../schemas/nodeDefSchema' function makeNodeDef( name: string, @@ -36,6 +36,36 @@ describe('sanitizeUserContent', () => { expect(required?.choiceV2).toEqual(['COMBO', { options: ['keep-me', 'b'] }]) }) + it('sanitizes both DynamicGroup template sections without mutating the definition', () => { + const group = [ + 'COMFY_DYNAMICGROUP_V3', + { + min: 0, + max: 5, + template: { + required: { model: [['private.safetensors', 'safe', 42], {}] }, + optional: { image: ['COMBO', { options: ['private.png', 'safe'] }] } + } + } + ] satisfies InputSpec + const defs = { + Group: makeNodeDef('Group', 'custom_nodes', { required: { group } }) + } + const original = structuredClone(defs) + expect(sanitizeUserContent(defs).Group.input?.required?.group).toEqual([ + 'COMFY_DYNAMICGROUP_V3', + { + min: 0, + max: 5, + template: { + required: { model: [['safe', 42], {}] }, + optional: { image: ['COMBO', { options: ['safe'] }] } + } + } + ]) + expect(defs).toEqual(original) + }) + it('zeros combo lists for known upload nodes in required/optional/hidden sections', () => { const defs = { LoadImage: makeNodeDef('LoadImage', 'nodes', { diff --git a/packages/object-info-parser/src/helpers/sanitizeUserContent.ts b/packages/object-info-parser/src/helpers/sanitizeUserContent.ts index c7d18255fdf..33e12ffb100 100644 --- a/packages/object-info-parser/src/helpers/sanitizeUserContent.ts +++ b/packages/object-info-parser/src/helpers/sanitizeUserContent.ts @@ -7,7 +7,8 @@ import type { } from '../schemas/nodeDefSchema' import { isComboInputSpecV1, - isComboInputSpecV2 + isComboInputSpecV2, + zDynamicGroupInputSpec } from '../schemas/nodeDefSchema' const USER_CONTENT_REGEX = @@ -58,9 +59,9 @@ function sanitizeNode(def: ComfyNodeDef): ComfyNodeDef { } function sanitizeInputSpecSection( - section: ComfyInputsSpec['required'] | ComfyInputsSpec['optional'], + section: ComfyInputsSpec['required'], forceEmpty: boolean -): ComfyInputsSpec['required'] | ComfyInputsSpec['optional'] { +): ComfyInputsSpec['required'] { if (!section) return section const nextEntries = Object.entries(section).map(([key, value]) => { @@ -98,6 +99,24 @@ function sanitizeInputSpec(inputSpec: unknown, forceEmpty: boolean): unknown { return sanitizeComboInputSpecV2(inputSpec as ComboInputSpecV2, forceEmpty) } + if (inputSpec[0] === 'COMFY_DYNAMICGROUP_V3') { + const group = zDynamicGroupInputSpec.safeParse(inputSpec) + if (group.success) { + const { template } = group.data[1] + return [ + inputSpec[0], + { + ...inputSpec[1], + template: { + ...template, + required: sanitizeInputSpecSection(template.required, forceEmpty), + optional: sanitizeInputSpecSection(template.optional, forceEmpty) + } + } + ] + } + } + return inputSpec } diff --git a/packages/object-info-parser/src/schemas/nodeDefSchema.ts b/packages/object-info-parser/src/schemas/nodeDefSchema.ts index 376f46144e6..b6426c1cf04 100644 --- a/packages/object-info-parser/src/schemas/nodeDefSchema.ts +++ b/packages/object-info-parser/src/schemas/nodeDefSchema.ts @@ -202,19 +202,90 @@ export function getComboSpecComboOptions( ) } -const excludedLiterals = new Set(['INT', 'FLOAT', 'BOOLEAN', 'STRING', 'COMBO']) +const excludedLiterals = new Set([ + 'INT', + 'FLOAT', + 'BOOLEAN', + 'STRING', + 'COMBO', + 'COMFY_DYNAMICGROUP_V3' +]) const zCustomInputSpec = z.tuple([ z.string().refine((value) => !excludedLiterals.has(value)), zBaseInputOptions.optional() ]) -const zInputSpec = z.union([ +const zWidgetInputSpec = z.union([ zIntInputSpec, zFloatInputSpec, zBooleanInputSpec, zStringInputSpec, zComboInputSpec, - zComboInputSpecV2, + zComboInputSpecV2 +]) + +const zDynamicGroupFields = z.record( + z + .string() + .min(1) + .refine( + (name) => !name.includes('.'), + 'DynamicGroup field names must not contain dots' + ), + z + .union([zWidgetInputSpec, zCustomInputSpec]) + .refine( + (spec) => + typeof spec[0] !== 'string' || + ![ + 'COMFY_DYNAMICGROUP_V3', + 'COMFY_AUTOGROW_V3', + 'COMFY_DYNAMICCOMBO_V3', + 'COMFY_MATCHTYPE_V3' + ].includes(spec[0]), + 'DynamicGroup fields must not contain dynamic inputs' + ) + .refine( + (spec) => !spec[1]?.forceInput, + 'DynamicGroup fields must not force input sockets' + ) +) + +export const zDynamicGroupInputSpec = z.tuple([ + z.literal('COMFY_DYNAMICGROUP_V3'), + zBaseInputOptions + .extend({ + template: z + .object({ + required: zDynamicGroupFields.optional(), + optional: zDynamicGroupFields.optional() + }) + .refine( + (template) => + Object.keys({ ...template.required, ...template.optional }).length > + 0, + 'DynamicGroup template must contain a field' + ) + .refine( + (template) => + !Object.keys(template.required ?? {}).some((name) => + Object.hasOwn(template.optional ?? {}, name) + ), + 'DynamicGroup field names must be unique' + ), + min: z.number().int().nonnegative().default(0), + max: z.number().int().positive().max(20).default(20), + group_name: z.string().optional() + }) + .refine(({ min, max }) => min <= max, { + message: 'DynamicGroup min must not exceed max', + path: ['min'] + }) +]) + +const zInputSpec = z.union([ + zWidgetInputSpec, + zDynamicGroupInputSpec, zCustomInputSpec ]) diff --git a/src/components/graph/GraphCanvas.test.ts b/src/components/graph/GraphCanvas.test.ts index eda22523cf8..52dcb8017d8 100644 --- a/src/components/graph/GraphCanvas.test.ts +++ b/src/components/graph/GraphCanvas.test.ts @@ -1,7 +1,16 @@ +import { fromPartial } from '@total-typescript/shoehorn' import { getActivePinia } from 'pinia' -import { render, screen } from '@testing-library/vue' +import { fireEvent, render, screen } from '@testing-library/vue' import type { RenderOptions } from '@testing-library/vue' -import { beforeEach, describe, expect, it, vi } from 'vitest' +import { + assert, + beforeEach, + describe, + expect, + it, + onTestFinished, + vi +} from 'vitest' import { nextTick } from 'vue' import { createI18n } from 'vue-i18n' @@ -10,6 +19,7 @@ import { useSettingStore } from '@/platform/settings/settingStore' import { useReleaseStore } from '@/platform/updates/common/releaseStore' import { useWorkflowStore } from '@/platform/workflow/management/stores/workflowStore' import { useCanvasStore } from '@/renderer/core/canvas/canvasStore' +import { api } from '@/scripts/api' import { app } from '@/scripts/app' import { useBootstrapStore } from '@/stores/bootstrapStore' import { useExecutionStore } from '@/stores/executionStore' @@ -222,6 +232,34 @@ describe('GraphCanvas first-run tour wiring', () => { }) }) +describe('GraphCanvas canvas parent', () => { + it('keeps title-editor touches within the canvas parent used by extensions', async () => { + vi.spyOn(api, 'getSystemStats').mockResolvedValue( + fromPartial({ devices: [] }) + ) + await mountGraphCanvas({ + TitleEditor: { template: '' } + }) + // eslint-disable-next-line testing-library/no-node-access -- Extensions use this canvas ID. + const canvas = document.getElementById('graph-canvas') + assert.exists(canvas) + // eslint-disable-next-line testing-library/no-node-access -- Canvas-parent listeners are the compatibility contract under test. + const parent = canvas.parentElement + assert.exists(parent) + const receivedTouch = vi.fn() + parent.addEventListener('touchstart', receivedTouch, true) + onTestFinished(() => + parent.removeEventListener('touchstart', receivedTouch, true) + ) + + await fireEvent.touchStart( + screen.getByRole('textbox', { name: 'Node title' }) + ) + + expect(receivedTouch).toHaveBeenCalledOnce() + }) +}) + describe('GraphCanvas execution progress updates', () => { async function mountProgressHarness({ totalNodes = 1_000, diff --git a/src/components/graph/GraphCanvas.vue b/src/components/graph/GraphCanvas.vue index 4ac4856beaa..3f01583ae33 100644 --- a/src/components/graph/GraphCanvas.vue +++ b/src/components/graph/GraphCanvas.vue @@ -73,6 +73,7 @@ () const canvasRef = ref(null) +const transformPaneRef = + useTemplateRef>('transformPaneRef') +const vueNodeDropTarget = computed(() => { + const element: unknown = transformPaneRef.value?.$el + return element instanceof HTMLElement ? element : null +}) const canvasPanelBoundsRef = useTemplateRef('canvasPanelBoundsRef') const nodeSearchboxPopoverRef = shallowRef { vi.mocked(useModelToNodeStore().getNodeProvider) ).toHaveBeenCalledWith('checkpoints') expect(mockStartDrag).toHaveBeenCalledWith(mockNodeDef, { - widgetValues: { ckpt_name: 'model.safetensors' }, + widgetValues: [{ selector: 'ckpt_name', value: 'model.safetensors' }], source: 'sidebar_drag' }) }) diff --git a/src/composables/node/startModelNodeDragFromAsset.test.ts b/src/composables/node/startModelNodeDragFromAsset.test.ts index 31698664e2b..0605e5aca62 100644 --- a/src/composables/node/startModelNodeDragFromAsset.test.ts +++ b/src/composables/node/startModelNodeDragFromAsset.test.ts @@ -40,7 +40,9 @@ describe('startModelNodeDragFromAsset', () => { expect(error).toBeUndefined() expect(useNodeDragToCanvas().startDrag).toHaveBeenCalledWith(nodeDef, { - widgetValues: { ckpt_name: 'sd_xl_base_1.0.safetensors' }, + widgetValues: [ + { selector: 'ckpt_name', value: 'sd_xl_base_1.0.safetensors' } + ], source: 'sidebar_drag' }) }) @@ -57,7 +59,9 @@ describe('startModelNodeDragFromAsset', () => { startModelNodeDragFromAsset(createAsset(), 'asset_browser') expect(useNodeDragToCanvas().startDrag).toHaveBeenCalledWith(nodeDef, { - widgetValues: { ckpt_name: 'sd_xl_base_1.0.safetensors' }, + widgetValues: [ + { selector: 'ckpt_name', value: 'sd_xl_base_1.0.safetensors' } + ], source: 'asset_browser' }) }) diff --git a/src/composables/node/startModelNodeDragFromAsset.ts b/src/composables/node/startModelNodeDragFromAsset.ts index 7cebf0dab19..2c568f6e04e 100644 --- a/src/composables/node/startModelNodeDragFromAsset.ts +++ b/src/composables/node/startModelNodeDragFromAsset.ts @@ -14,7 +14,9 @@ export function startModelLoaderDrag( filename: string, source: NodeAddSource = 'sidebar_drag' ) { - const widgetValues = provider.key ? { [provider.key]: filename } : undefined + const widgetValues = provider.key + ? [{ selector: provider.key, value: filename }] + : undefined useNodeDragToCanvas().startDrag(provider.nodeDef, { widgetValues, source }) } diff --git a/src/composables/node/useNodeDragToCanvas.test.ts b/src/composables/node/useNodeDragToCanvas.test.ts index 00849fd3e5f..ba387a55e6b 100644 --- a/src/composables/node/useNodeDragToCanvas.test.ts +++ b/src/composables/node/useNodeDragToCanvas.test.ts @@ -260,7 +260,7 @@ describe('useNodeDragToCanvas', () => { const { startDrag } = useNodeDragToCanvas() startDrag(mockNodeDef, { - widgetValues: { ckpt_name: 'model.safetensors' } + widgetValues: [{ selector: 'ckpt_name', value: 'model.safetensors' }] }) document.dispatchEvent( @@ -274,6 +274,47 @@ describe('useNodeDragToCanvas', () => { expect(widget.value).toBe('model.safetensors') }) + it('should apply a pattern selection to the first matching widget', () => { + mockCanvas.canvas.getBoundingClientRect.mockReturnValue({ + left: 0, + right: 500, + top: 0, + bottom: 500 + }) + mockConvertEventToCanvasOffset.mockReturnValue([150, 150]) + const strength = { name: 'loras.0.strength', value: 0.5 } + const first = { name: 'loras.0.lora_name', value: 'A.safetensors' } + const second = { name: 'loras.1.lora_name', value: 'B.safetensors' } + mockAddNodeOnGraph.mockReturnValue({ + id: 1, + widgets: [strength, first, second] + }) + + const { startDrag } = useNodeDragToCanvas() + startDrag(mockNodeDef, { + widgetValues: [ + { + selector: /^loras\.(?:0|[1-9]\d*)\.lora_name$/, + value: 'C.safetensors' + } + ] + }) + + document.dispatchEvent( + new PointerEvent('pointerup', { + clientX: 250, + clientY: 250, + bubbles: true + }) + ) + + expect([strength.value, first.value, second.value]).toEqual([ + 0.5, + 'C.safetensors', + 'B.safetensors' + ]) + }) + it('should warn but still place the node when a requested widget is missing', () => { mockCanvas.canvas.getBoundingClientRect.mockReturnValue({ left: 0, @@ -290,7 +331,7 @@ describe('useNodeDragToCanvas', () => { const { startDrag } = useNodeDragToCanvas() startDrag(mockNodeDef, { - widgetValues: { ckpt_name: 'model.safetensors' } + widgetValues: [{ selector: 'ckpt_name', value: 'model.safetensors' }] }) document.dispatchEvent( diff --git a/src/composables/node/useNodeDragToCanvas.ts b/src/composables/node/useNodeDragToCanvas.ts index 65a67fe63c8..9b5e9fcf8de 100644 --- a/src/composables/node/useNodeDragToCanvas.ts +++ b/src/composables/node/useNodeDragToCanvas.ts @@ -8,14 +8,15 @@ import { useToastStore } from '@/platform/updates/common/toastStore' import { useCanvasStore } from '@/renderer/core/canvas/canvasStore' import { useLitegraphService } from '@/services/litegraphService' import type { ComfyNodeDefImpl } from '@/stores/nodeDefStore' +import { matchesWidgetName } from '@/utils/widgetBinding' +import type { WidgetValueBinding } from '@/utils/widgetBinding' type DragMode = 'click' | 'native' -type WidgetValues = Record type Position = { x: number; y: number } interface StartDragOptions { mode?: DragMode - widgetValues?: WidgetValues + widgetValues?: readonly WidgetValueBinding[] source?: NodeAddSource } @@ -23,7 +24,7 @@ const isDragging = ref(false) const draggedNode = shallowRef(null) const dragMode = ref('click') const lastNativeDragPosition = shallowRef() -const pendingWidgetValues = shallowRef() +const pendingWidgetValues = shallowRef() const pendingSource = ref('sidebar_drag') let listenersSetup = false @@ -36,11 +37,16 @@ function trackNativeDragPosition(e: DragEvent) { lastNativeDragPosition.value = { x: e.clientX, y: e.clientY } } -function applyWidgetValues(node: LGraphNode, values: WidgetValues) { - for (const [name, value] of Object.entries(values)) { - const widget = node.widgets?.find((w) => w.name === name) +function applyWidgetValues( + node: LGraphNode, + values: readonly WidgetValueBinding[] +) { + for (const { selector, value } of values) { + const widget = node.widgets?.find((w) => + matchesWidgetName(w.name, selector) + ) if (!widget) { - console.error(`Widget ${name} not found on node ${node.type}`) + console.error(`Widget ${selector} not found on node ${node.type}`) useToastStore().add({ severity: 'warn', summary: t('g.warning'), diff --git a/src/composables/useCanvasDrop.test.ts b/src/composables/useCanvasDrop.test.ts index abb59b5d15a..09f01b71b9c 100644 --- a/src/composables/useCanvasDrop.test.ts +++ b/src/composables/useCanvasDrop.test.ts @@ -1,13 +1,18 @@ import { fromPartial } from '@total-typescript/shoehorn' -import { describe, expect, it, vi } from 'vitest' +import { assert, describe, expect, it, onTestFinished, vi } from 'vitest' import { effectScope, ref } from 'vue' import { useCanvasDrop } from '@/composables/useCanvasDrop' import { usePragmaticDroppable } from '@/composables/usePragmaticDragAndDrop' import { ComfyWorkflow } from '@/platform/workflow/management/stores/workflowStore' import { app } from '@/scripts/app' - -const insertWorkflow = vi.hoisted(() => vi.fn()) +import { LGraphNode } from '@/lib/litegraph/src/litegraph' +import { useLitegraphService } from '@/services/litegraphService' +import { useWorkflowService } from '@/platform/workflow/core/services/workflowService' +import { ComfyModelDef } from '@/stores/modelStore' +import { useModelToNodeStore } from '@/stores/modelToNodeStore' +import { useNodeDefStore } from '@/stores/nodeDefStore' +import type { ComfyNodeDefImpl } from '@/stores/nodeDefStore' vi.mock(import('@/composables/usePragmaticDragAndDrop'), () => ({ usePragmaticDraggable: vi.fn(), @@ -23,21 +28,13 @@ vi.mock( }) ) -vi.mock( - import('@/platform/workflow/core/services/workflowService'), - () => ({ useWorkflowService: () => ({ insertWorkflow }) }) -) - -vi.mock(import('@/services/litegraphService'), () => ({ - useLitegraphService: () => ({ addNodeOnGraph: vi.fn() }) -})) - -vi.mock(import('@/scripts/app'), () => ({ - app: { canvas: { selectOnly: false } } -})) +vi.mock(import('@/platform/workflow/core/services/workflowService')) +vi.mock(import('@/services/litegraphService')) +vi.mock(import('@/scripts/app')) function registerDrop() { const scope = effectScope() + onTestFinished(() => scope.stop()) scope.run(() => useCanvasDrop(ref(document.createElement('canvas')))) const onDrop = vi.mocked(usePragmaticDroppable).mock.lastCall?.[1].onDrop if (!onDrop) throw new Error('canvas drop target was not registered') @@ -45,6 +42,53 @@ function registerDrop() { } describe('useCanvasDrop', () => { + it.for([ + { selector: 'model_a', description: 'exact widget name' }, + { selector: /^model_/, description: 'widget name pattern' } + ])( + 'drops a model into the first widget matching $description', + async ({ selector }) => { + useNodeDefStore().nodeDefsByName = { + TestLoader: fromPartial({ name: 'TestLoader' }) + } + useModelToNodeStore().quickRegister('test-models', 'TestLoader', selector) + const node = new LGraphNode('Test loader') + node.comfyClass = 'TestLoader' + const strength = node.addWidget('number', 'strength', 0.5, () => {}) + const first = node.addWidget('text', 'model_a', 'A.safetensors', () => {}) + const second = node.addWidget( + 'text', + 'model_b', + 'B.safetensors', + () => {} + ) + assert.exists(app.canvas.graph) + vi.spyOn(app.canvas.graph, 'getNodeOnPos').mockReturnValue(node) + const onDrop = registerDrop() + + await onDrop( + fromPartial({ + location: { current: { input: { clientX: 10, clientY: 20 } } }, + source: { + data: { + type: 'tree-explorer-node', + data: { + data: new ComfyModelDef('C.safetensors', 'test-models', 0) + } + } + } + }) + ) + + expect([strength.value, first.value, second.value]).toEqual([ + 0.5, + 'C.safetensors', + 'B.safetensors' + ]) + expect(useLitegraphService().addNodeOnGraph).not.toHaveBeenCalled() + } + ) + it.for([ { selectOnly: false, inserts: 1 }, { selectOnly: true, inserts: 0 } @@ -72,7 +116,7 @@ describe('useCanvasDrop', () => { }) ) - expect(insertWorkflow).toHaveBeenCalledTimes(inserts) + expect(useWorkflowService().insertWorkflow).toHaveBeenCalledTimes(inserts) } ) }) diff --git a/src/composables/useCanvasDrop.ts b/src/composables/useCanvasDrop.ts index 18a1bd2d169..d6b2416cba1 100644 --- a/src/composables/useCanvasDrop.ts +++ b/src/composables/useCanvasDrop.ts @@ -15,13 +15,14 @@ import { useModelToNodeStore } from '@/stores/modelToNodeStore' import { ComfyNodeDefImpl } from '@/stores/nodeDefStore' import type { RenderedTreeExplorerNode } from '@/types/treeExplorerTypes' import { isSelectOnly } from '@/utils/litegraphUtil' +import { matchesWidgetName } from '@/utils/widgetBinding' -export const useCanvasDrop = (canvasRef: Ref) => { +export const useCanvasDrop = (dropTargetRef: Ref) => { const modelToNodeStore = useModelToNodeStore() const litegraphService = useLitegraphService() const workflowService = useWorkflowService() - usePragmaticDroppable(() => canvasRef.value, { + usePragmaticDroppable(() => dropTargetRef.value, { getDropEffect: (args): Exclude => args.source.data.type === 'tree-explorer-node' ? 'copy' : 'move', onDrop: async (event) => { @@ -69,9 +70,10 @@ export const useCanvasDrop = (canvasRef: Ref) => { targetProvider = provider } } - if (targetGraphNode) { - const widget = targetGraphNode.widgets?.find( - (widget) => widget.name === targetProvider?.key + if (targetGraphNode && targetProvider) { + const { key } = targetProvider + const widget = targetGraphNode.widgets?.find((widget) => + matchesWidgetName(widget.name, key) ) if (widget) { widget.value = model.file_name diff --git a/src/composables/usePragmaticDragAndDrop.test.ts b/src/composables/usePragmaticDragAndDrop.test.ts new file mode 100644 index 00000000000..71ace2aaf09 --- /dev/null +++ b/src/composables/usePragmaticDragAndDrop.test.ts @@ -0,0 +1,98 @@ +import { dropTargetForElements } from '@atlaskit/pragmatic-drag-and-drop/element/adapter' +import { render, screen } from '@testing-library/vue' +import userEvent from '@testing-library/user-event' +import { describe, expect, it, vi } from 'vitest' +import { defineComponent, h, ref } from 'vue' + +import { usePragmaticDroppable } from '@/composables/usePragmaticDragAndDrop' + +vi.mock(import('@atlaskit/pragmatic-drag-and-drop/element/adapter')) + +const DropSurface = defineComponent({ + setup() { + const target = ref(null) + const visible = ref(false) + const version = ref(0) + usePragmaticDroppable(target, {}) + return () => + h('div', [ + h( + 'button', + { + onClick: () => { + visible.value = true + } + }, + 'Show target' + ), + h( + 'button', + { + onClick: () => { + visible.value = false + } + }, + 'Hide target' + ), + h( + 'button', + { + onClick: () => { + version.value++ + } + }, + 'Replace target' + ), + visible.value + ? h('div', { + ref: target, + key: version.value, + role: 'region', + 'aria-label': 'Drop surface' + }) + : null + ]) + } +}) + +describe('usePragmaticDroppable', () => { + it('registers a target that appears after the component mounts', async () => { + const user = userEvent.setup() + vi.mocked(dropTargetForElements).mockReturnValue(vi.fn()) + render(DropSurface) + expect(dropTargetForElements).not.toHaveBeenCalled() + + await user.click(screen.getByRole('button', { name: 'Show target' })) + + expect(dropTargetForElements).toHaveBeenCalledExactlyOnceWith({ + element: screen.getByRole('region', { name: 'Drop surface' }) + }) + }) + + it('releases replaced and removed targets and stops the last target on unmount', async () => { + const user = userEvent.setup() + const firstCleanup = vi.fn() + const secondCleanup = vi.fn() + const thirdCleanup = vi.fn() + vi.mocked(dropTargetForElements) + .mockReturnValueOnce(firstCleanup) + .mockReturnValueOnce(secondCleanup) + .mockReturnValueOnce(thirdCleanup) + const view = render(DropSurface) + await user.click(screen.getByRole('button', { name: 'Show target' })) + await user.click(screen.getByRole('button', { name: 'Replace target' })) + expect(firstCleanup).toHaveBeenCalledOnce() + expect(dropTargetForElements).toHaveBeenCalledTimes(2) + + await user.click(screen.getByRole('button', { name: 'Hide target' })) + expect(secondCleanup).toHaveBeenCalledOnce() + expect(dropTargetForElements).toHaveBeenCalledTimes(2) + await user.click(screen.getByRole('button', { name: 'Show target' })) + expect(dropTargetForElements).toHaveBeenCalledTimes(3) + expect(thirdCleanup).not.toHaveBeenCalled() + + view.unmount() + + expect(thirdCleanup).toHaveBeenCalledOnce() + }) +}) diff --git a/src/composables/usePragmaticDragAndDrop.ts b/src/composables/usePragmaticDragAndDrop.ts index 03788d171e0..bd0752fa9a4 100644 --- a/src/composables/usePragmaticDragAndDrop.ts +++ b/src/composables/usePragmaticDragAndDrop.ts @@ -2,7 +2,7 @@ import { draggable, dropTargetForElements } from '@atlaskit/pragmatic-drag-and-drop/element/adapter' -import { onBeforeUnmount, onMounted, toValue } from 'vue' +import { onBeforeUnmount, onMounted, toValue, watch } from 'vue' import type { MaybeRefOrGetter } from 'vue' export function usePragmaticDroppable( @@ -12,16 +12,14 @@ export function usePragmaticDroppable( let cleanup = () => {} onMounted(() => { - const element = toValue(dropTargetElement) - - if (!element) { - return - } - - cleanup = dropTargetForElements({ - element, - ...options - }) + cleanup = watch( + () => toValue(dropTargetElement), + (element, _previous, onCleanup) => { + if (!element) return + onCleanup(dropTargetForElements({ element, ...options })) + }, + { immediate: true, flush: 'post' } + ) }) onBeforeUnmount(() => { diff --git a/src/core/graph/widgets/dynamicGroupWidget.test.ts b/src/core/graph/widgets/dynamicGroupWidget.test.ts new file mode 100644 index 00000000000..d4acb8c8035 --- /dev/null +++ b/src/core/graph/widgets/dynamicGroupWidget.test.ts @@ -0,0 +1,1092 @@ +import axios, { AxiosHeaders } from 'axios' +import type { AxiosResponse } from 'axios' +import { cloneDeep } from 'es-toolkit' +import { useLinkStore } from '@/stores/linkStore' +import { api } from '@/scripts/api' + +import { + afterEach, + assert, + describe, + expect, + it, + onTestFinished, + vi +} from 'vitest' + +import { i18n } from '@/i18n' + +import { LGraph, LGraphNode, LiteGraph } from '@/lib/litegraph/src/litegraph' +import { useLitegraphService } from '@/services/litegraphService' +import { useWidgetValueStore } from '@/stores/widgetValueStore' +import { useWidgetStore } from '@/stores/widgetStore' +import { promotedInputWidget } from '@/core/graph/subgraph/promotedInputWidget' +import { multiClone } from '@/lib/litegraph/src/subgraph/subgraphUtils' +import { promoteValueWidgetViaSubgraphInput } from '@/core/graph/subgraph/promotionUtils' +import { + createTestSubgraph, + createTestSubgraphNode +} from '@/lib/litegraph/src/subgraph/__fixtures__/subgraphHelpers' +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() + const node = new LGraphNode('DynamicGroupTest') + node.comfyClass = 'DynamicGroupTest' + node.serialize_widgets = true + graph.add(node) + const { addNodeInput } = useLitegraphService() + addNodeInput(node, { + name: 'before', + type: 'STRING', + default: 'first', + isOptional: false + }) + addNodeInput(node, { + name: 'loras', + type: 'COMFY_DYNAMICGROUP_V3', + isOptional: false, + min, + max, + group_name: 'LoRA', + template: { + required: { + lora_name: ['COMBO', { options: ['A', 'B', 'C'] }], + strength: ['FLOAT', { default: 1 }] + }, + optional: { enabled: ['BOOLEAN', { default: true }] } + } + }) + addNodeInput(node, { + name: 'after', + type: 'STRING', + default: 'last', + isOptional: false + }) + function widget(name: string) { + const found = node.widgets?.find((widget) => widget.name === name) + if (!found) throw new Error(`Missing widget ${name}`) + return found + } + return { node, graph, widget } +} + +const namedValuesRestore = LiteGraph.namedValuesRestore +afterEach(() => { + LiteGraph.namedValuesRestore = namedValuesRestore +}) + +describe('DynamicGroup widgets', () => { + const forcedSibling: NonNullable['required'] = { + forced: ['INT', { forceInput: true }], + before: ['STRING', { default: 'before' }] + } + it.for([ + { name: 'group alone', siblings: {}, count: 1, expected: [1, 0.9, 1] }, + { + name: 'empty group with a forced sibling', + siblings: forcedSibling, + count: 0, + expected: ['before', 0] + }, + { + name: 'one row with a forced sibling', + siblings: forcedSibling, + count: 1, + expected: ['before', 1, 0.9, 1] + } + ])('round-trips $name', async ({ siblings, count, expected }) => { + LiteGraph.namedValuesRestore = false + const name = 'RegisteredDynamicGroupRestore' + await useLitegraphService().registerNodeDef(name, { + name, + display_name: name, + category: 'testing', + python_module: 'nodes', + description: '', + output_node: false, + output: [], + input: { + required: { + ...siblings, + rows: [ + 'COMFY_DYNAMICGROUP_V3', + { + template: { + required: { + weight: ['FLOAT', { default: 0.9 }], + strength: ['FLOAT', { default: 1 }] + } + } + } + ] + } + } + }) + try { + const graph = new LGraph() + const source = LiteGraph.createNode(name) + const restored = LiteGraph.createNode(name) + if (!source || !restored) throw new Error('Node construction failed') + graph.add(source) + graph.add(restored) + const controller = source.widgets?.find((w) => w.name === 'rows') + assert.exists(controller) + controller.value = count + const saved = source.serialize() + expect(saved.widgets_values).toEqual(expected) + + restored.configure(saved) + + expect(restored.serialize().widgets_values).toEqual(expected) + expect(saved.widgets_values).toEqual(expected) + } finally { + LiteGraph.unregisterNodeType(name) + } + }) + + it.for([ + { + name: 'clipboard cloning', + clone: (node: LGraphNode) => node.clone()?.serialize() + }, + { + name: 'subgraph conversion cloning', + clone: (node: LGraphNode) => multiClone([node])[0] + } + ])('clones populated rows through $name', async ({ clone }) => { + LiteGraph.namedValuesRestore = false + const name = 'RegisteredDynamicGroupClone' + await useLitegraphService().registerNodeDef(name, { + name, + display_name: name, + category: 'testing', + python_module: 'nodes', + description: '', + output_node: false, + output: [], + input: { + required: { + rows: [ + 'COMFY_DYNAMICGROUP_V3', + { + template: { + required: { + weight: ['FLOAT', { default: 1 }], + strength: ['FLOAT', { default: 1 }] + } + } + } + ] + } + } + }) + const graph = new LGraph() + const source = LiteGraph.createNode(name) + assert.exists(source) + graph.add(source) + const values = [2, 0.25, 0.8, 0.5, 0.6] + source.configure({ ...source.serialize(), widgets_values: values }) + + const copied = clone(source) + + expect(copied?.widgets_values).toEqual(values) + expect(source.serialize().widgets_values).toEqual(values) + }) + + it.for([ + { + name: 'a newly appended trailing widget', + values: ['first', 1, 'B', 0.4, false], + expected: ['first', 1, 'B', 0.4, false, 'last'] + }, + { + name: 'a removed template field across two rows', + values: ['first', 2, 'A', 0.8, true, 'removed', 'B', 0.5, false, 'tail'], + expected: ['first', 2, 'A', 0.8, true, 'removed', 'B', 0.5, false] + }, + { + name: 'an appended template field across two rows', + values: ['first', 2, 'A', 0.8, 'B', 0.5, 'tail'], + expected: ['first', 2, 'A', 0.8, 'B', 0.5, 'tail', true, 'last'] + }, + { + name: 'reordered template fields', + values: ['first', 1, 0.8, 'B', false, 'tail'], + expected: ['first', 1, 0.8, 'B', false, 'tail'] + }, + { + name: 'missing values in the remaining rows', + values: ['first', 3, 'B', 0.4, false], + expected: [ + 'first', + 3, + 'B', + 0.4, + false, + 'A', + 1, + true, + 'A', + 1, + true, + 'last' + ] + } + ])('restores positional values with $name', ({ values, expected }) => { + LiteGraph.namedValuesRestore = false + const { node } = setup() + const saved = node.serialize() + saved.widgets_values = values + + node.configure(saved) + + expect(node.serialize().widgets_values).toEqual(expected) + expect(saved.widgets_values).toEqual(values) + }) + + it('restores auxiliary controls when a trailing widget has no saved value', () => { + LiteGraph.namedValuesRestore = false + const { node, widget } = setup() + useLitegraphService().addNodeInput(node, { + name: 'seeds', + type: 'COMFY_DYNAMICGROUP_V3', + isOptional: false, + template: { + required: { seed: ['INT', { control_after_generate: true }] } + } + }) + useLitegraphService().addNodeInput(node, { + name: 'tail', + type: 'STRING', + isOptional: false, + default: 'untouched' + }) + const saved = node.serialize() + saved.widgets_values = ['first', 0, 'last', 1, 12, 'fixed'] + + node.configure(saved) + + expect(widget('seeds').value).toBe(1) + expect(widget('seeds.0.seed').value).toBe(12) + expect(widget('seeds.0.seed.0').value).toBe('fixed') + expect(widget('tail').value).toBe('untouched') + }) + + it('creates and serializes registered rich and custom template widgets', async () => { + const { node, graph, widget } = setup() + useWidgetStore().registerCustomWidgets({ + TEST_DYNAMIC_FIELD: (node, name) => ({ + widget: node.addWidget('text', name, 'custom', () => {}) + }) + }) + useLitegraphService().addNodeInput(node, { + name: 'rich', + type: 'COMFY_DYNAMICGROUP_V3', + isOptional: false, + template: { + required: { + color: ['COLOR', { default: '#ffffff' }], + custom: ['TEST_DYNAMIC_FIELD', {}], + override: [ + 'CUSTOM_DATA', + { widgetType: 'STRING', default: 'overridden' } + ] + } + } + }) + widget('rich').value = 1 + widget('rich.0.color').value = '#123456' + expect((await graphToPrompt(graph)).output[node.id].inputs).toMatchObject({ + 'rich.0.color': '#123456', + 'rich.0.custom': 'custom', + 'rich.0.override': 'overridden' + }) + const saved = node.serialize() + widget('rich').value = 0 + node.configure(saved) + expect(widget('rich.0.color').value).toBe('#123456') + }) + + it('rejects socket-only templates before creating group widgets', () => { + const { node } = setup() + const before = node.serialize() + expect(() => + useLitegraphService().addNodeInput(node, { + name: 'images', + type: 'COMFY_DYNAMICGROUP_V3', + isOptional: false, + template: { required: { image: ['IMAGE', {}] } } + }) + ).toThrow('requires a registered widget') + expect(node.serialize()).toEqual(before) + }) + + it.for([ + { label: 'LoRA #2 strength', expected: 'LoRA #1 strength' }, + { label: 'My strength', expected: 'My strength' } + ])( + 'shows $expected after removing a row before the promoted field $label', + ({ label, expected }) => { + const { node, graph, widget } = setup() + graph.remove(node) + const subgraph = createTestSubgraph({ rootGraph: graph }) + const host = createTestSubgraphNode(subgraph) + subgraph.add(node) + widget('loras').value = 2 + const strength = widget('loras.1.strength') + expect(strength.label).toBe('LoRA #2 strength') + expect(promoteValueWidgetViaSubgraphInput(host, node, strength).ok).toBe( + true + ) + const input = host.inputs[0] + expect(promotedInputWidget(input)?.label).toBe('LoRA #2 strength') + subgraph.renameInput(subgraph.inputs[0], label) + + widget('loras.0').callback?.(undefined) + + expect(strength.name).toBe('loras.0.strength') + expect(strength.label).toBe('LoRA #1 strength') + expect(promotedInputWidget(input)?.label).toBe(expected) + } + ) + + 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 { node, 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('disconnects the deleted row at the outer host while keeping the next row connected', async () => { + const { node, widget, innerHost, outerHost, outerInput } = + setupTwiceDeepPromotion() + const graph = innerHost.subgraph.rootGraph + widget('loras').value = 3 + widget('loras.2.lora_name').value = 'C' + widget('loras.2.strength').value = 0.37 + 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) + const survivingInput = outerHost.inputs[1] + const otherHost = createTestSubgraphNode(outerHost.subgraph) + graph.add(otherHost) + const source = new LGraphNode('Strength source') + source.addOutput('strength', 'FLOAT') + graph.add(source) + const removed = source.connect(0, outerHost, 0) + const otherRemoved = source.connect(0, otherHost, 0) + const retained = source.connect(0, outerHost, 1) + assert.exists(removed) + assert.exists(otherRemoved) + assert.exists(retained) + + widget('loras.1').callback?.(undefined) + + 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() + assert.exists(survivingInput.link) + expect(graph.getLink(survivingInput.link)).toBe(retained) + expect(promotedInputWidget(survivingInput)?.value).toBe(0.37) + expect(promotedInputWidget(survivingInput)?.label).toBe('LoRA #2 strength') + expect(widget('loras.1.lora_name').value).toBe('C') + expect(widget('loras.1.strength').value).toBe(0.37) + }) + + it('keeps the outer connection when another node still consumes the promoted input', () => { + const { widget, innerHost, outerHost, outerInput } = + setupTwiceDeepPromotion() + const graph = innerHost.subgraph.rootGraph + const other = new LGraphNode('Another strength consumer') + const input = other.addInput('strength', 'FLOAT') + innerHost.subgraph.add(other) + const internal = innerHost.subgraph.inputNode.slots[0].connect(input, other) + const source = new LGraphNode('Strength source') + source.addOutput('strength', 'FLOAT') + graph.add(source) + const external = source.connect(0, outerHost, 0) + assert.exists(internal) + assert.exists(external) + + widget('loras.1').callback?.(undefined) + + assert.exists(outerInput.link) + assert.exists(input.link) + expect(graph.getLink(outerInput.link)).toBe(external) + expect(innerHost.subgraph.getLink(input.link)).toBe(internal) + expect(innerHost.subgraph.inputs[0].linkIds).toEqual([internal.id]) + expect(widget('loras').value).toBe(1) + }) + + it.for([ + { label: 'LoRA #2 strength', expected: 'LoRA 1행 strength' }, + { label: 'My strength', expected: 'My strength' } + ])( + 'renumbers $label after restoring in another language', + ({ label, expected }) => { + const { node, widget } = setup() + widget('loras').value = 2 + widget('loras.1.strength').label = label + node.inputs[node.findInputSlot('loras.1.strength')].label = label + const saved = node.serialize() + const locale = i18n.global.locale.value + const messages = cloneDeep(i18n.global.getLocaleMessage('ko')) + onTestFinished(() => { + i18n.global.locale.value = locale + i18n.global.setLocaleMessage('ko', messages) + }) + i18n.global.mergeLocaleMessage('ko', { + dynamicGroup: { row: '{group} {index}행' } + }) + i18n.global.locale.value = 'ko' + const restored = setup() + restored.node.configure(saved) + + restored.widget('loras.0').callback?.(undefined) + + expect(restored.widget('loras.0.strength').label).toBe(expected) + expect(restored.widget('loras.0').label).toBe('LoRA 1행') + } + ) + + it('keeps group controls off the canvas while preserving Vue editing and saved values', () => { + const { node, widget } = setup(1) + widget('loras.$add').callback?.(undefined) + widget('loras.1.lora_name').value = 'C' + widget('loras.1.strength').label = 'My strength' + node.inputs[node.findInputSlot('loras.1.strength')].label = 'My strength' + const restored = setup(1) + restored.node.configure(node.serialize()) + + expect(restored.node.getLayoutWidgets().map((w) => w.name)).toEqual([ + 'before', + 'loras.$notice', + 'after' + ]) + for (const field of restored.node.widgets ?? []) { + if (!field.name.startsWith('loras.') || field.name === 'loras.$notice') + continue + expect(restored.node.isWidgetVisible(field)).toBe(false) + expect(restored.node.isWidgetRowVisible(field)).toBe(false) + if (!field.visibility) throw new Error('Missing widget visibility') + expect( + isWidgetVisibleOnSurface(field.visibility, 'vueNode', { + showAdvanced: false + }) + ).toBe(true) + } + expect(restored.widget('loras.1.lora_name').value).toBe('C') + expect(restored.widget('loras.1.strength').label).toBe('My strength') + restored.widget('loras.0').callback?.(undefined) + expect(restored.widget('loras.0.lora_name').value).toBe('C') + expect(restored.widget('loras.0.strength').label).toBe('My strength') + expect(restored.widget('loras').value).toBe(1) + }) + + it.for([0, 3])('shows one canvas notice per group with %s rows', (count) => { + const { node, widget } = setup() + widget('loras').value = count + const notice = widget('loras.$notice') + expect(node.getLayoutWidgets().map((w) => w.name)).toEqual([ + 'before', + 'loras.$notice', + 'after' + ]) + expect(node.isWidgetVisible(notice)).toBe(true) + if (!notice.visibility) throw new Error('Missing notice visibility') + for (const surface of ['vueNode', 'panel'] as const) { + expect( + isWidgetVisibleOnSurface(notice.visibility, surface, { + showAdvanced: true + }) + ).toBe(false) + } + expect(node.serialize().widgets_values).toHaveLength(3 + count * 3) + }) + + it('rolls back a rejected row addition without losing existing values or links', () => { + const { node, graph, widget } = setup() + widget('loras').value = 1 + widget('loras.0.strength').value = 0.5 + const source = new LGraphNode('Source') + source.addOutput('strength', 'FLOAT') + graph.add(source) + const link = source.connect(0, node, node.findInputSlot('loras.0.strength')) + const saved = node.serialize() + const previousWidgets = [...(node.widgets ?? [])] + vi.spyOn(console, 'error').mockImplementation(() => {}) + const update = vi.spyOn(useLinkStore(), 'updateEndpoints').mockReturnValue({ + ok: false, + error: { code: 'unowned-topology', message: 'Rejected' } + }) + update.mockClear() + + widget('loras').value = 3 + + expect(update).toHaveBeenCalledOnce() + expect(widget('loras').value).toBe(1) + expect(node.widgets).toEqual(previousWidgets) + expect(node.serialize()).toEqual(saved) + expect(node.getInputLink(node.findInputSlot('loras.0.strength'))).toBe(link) + }) + + it('leaves rows and links intact when row removal is rejected', () => { + const { node, graph, widget } = setup() + widget('loras').value = 2 + const source = new LGraphNode('Source') + source.addOutput('strength', 'FLOAT') + graph.add(source) + const link = source.connect(0, node, node.findInputSlot('loras.1.strength')) + const saved = node.serialize() + vi.spyOn(console, 'error').mockImplementation(() => {}) + vi.spyOn(useLinkStore(), 'updateEndpoints').mockReturnValue({ + ok: false, + error: { code: 'unowned-topology', message: 'Rejected' } + }) + + widget('loras.0').callback?.(undefined) + expect(node.serialize()).toEqual(saved) + widget('loras').value = 0 + expect(node.serialize()).toEqual(saved) + expect(node.getInputLink(node.findInputSlot('loras.1.strength'))).toBe(link) + }) + + it('preserves unrelated widgets when cleanup removes another row widget', () => { + const { node, widget } = setup() + widget('loras').value = 1 + const sibling = widget('loras.0.enabled') + widget('loras.0').onRemove = () => { + if (node.widgets?.includes(sibling)) node.removeWidget(sibling) + } + widget('loras.0').callback?.(undefined) + expect(widget('after').value).toBe('last') + expect(widget('loras').value).toBe(0) + }) + + it.for([false, true])( + 'rejects unsupported saved row counts before allocating with named restoration = %s', + (named) => { + LiteGraph.namedValuesRestore = named + const { node } = setup() + const saved = node.serialize() + saved.widgets_values = ['first', 1e9, 'last'] + saved.widgets_values_named = { + before: 'first', + loras: 1e9, + 'loras.999999999.lora_name': 'C', + after: 'last' + } + const add = vi.spyOn(node, 'addCustomWidget').mockImplementation(() => { + throw new Error('Unexpected row allocation') + }) + expect(() => node.configure(saved)).toThrow( + 'Invalid saved row count for DynamicGroup' + ) + expect(add).not.toHaveBeenCalled() + } + ) + + it('preserves saved values in an error node when the group definition is invalid', () => { + const { node, graph, widget } = setup() + const nodeType = 'test/InvalidDynamicGroup' + node.type = nodeType + widget('loras').value = 2 + widget('loras.1.lora_name').value = 'C' + const saved = graph.serialize() + class InvalidDynamicGroup extends LGraphNode { + constructor() { + super('Invalid group') + useLitegraphService().addNodeInput(this, { + name: 'loras', + type: 'COMFY_DYNAMICGROUP_V3', + isOptional: false, + min: 0, + max: 101, + template: { required: { name: ['STRING', {}] } } + }) + } + } + LiteGraph.registerNodeType(nodeType, InvalidDynamicGroup) + vi.spyOn(console, 'error').mockImplementation(() => {}) + try { + const restored = new LGraph() + restored.configure(saved) + const placeholder = restored.getNodeById(node.id) + expect(placeholder?.has_errors).toBe(true) + expect(placeholder?.serialize().widgets_values).toEqual( + saved.nodes[0].widgets_values + ) + } finally { + LiteGraph.unregisterNodeType(nodeType) + } + }) + + it('removes remote combo controls and subscriptions with their row', async () => { + const response: AxiosResponse = { + data: ['A', 'B'], + status: 200, + statusText: 'OK', + headers: new AxiosHeaders(), + config: { headers: new AxiosHeaders() } + } + vi.spyOn(axios, 'get').mockResolvedValue(response) + const { graph, node, widget } = setup() + useLitegraphService().addNodeInput(node, { + name: 'remote', + type: 'COMFY_DYNAMICGROUP_V3', + isOptional: false, + min: 0, + max: 3, + template: { + required: { + model: [ + 'COMBO', + { + remote: { + route: '/test/dynamic-group-remote', + refresh_button: true + } + } + ] + } + } + }) + widget('remote').value = 2 + expect(node.getLayoutWidgets().map((w) => w.name)).toEqual([ + 'before', + 'loras.$notice', + 'after', + 'remote.$notice' + ]) + const first = widget('remote.0.model') + const survivor = widget('remote.1.model') + await vi.waitFor(() => expect(first.options.values).toEqual(['A', 'B'])) + expect(first.value).toBe('A') + first.options.values = undefined + expect(first.options.values).toEqual(['A', 'B']) + vi.mocked(axios.get).mockResolvedValue({ ...response, data: ['C'] }) + first.refresh?.() + await vi.waitFor(() => expect(first.options.values).toEqual(['C'])) + const firstRefresh = vi.spyOn(first, 'refresh').mockImplementation(() => {}) + const survivorRefresh = vi + .spyOn(survivor, 'refresh') + .mockImplementation(() => {}) + widget('remote.0.model.0').callback?.(true) + widget('remote.1.model.0').callback?.(true) + api.dispatchCustomEvent('execution_success', { + prompt_id: 'test', + timestamp: 0 + }) + expect(firstRefresh).toHaveBeenCalledTimes(1) + expect(survivorRefresh).toHaveBeenCalledTimes(1) + + widget('remote.0').callback?.(undefined) + api.dispatchCustomEvent('execution_success', { + prompt_id: 'test', + timestamp: 0 + }) + expect(firstRefresh).toHaveBeenCalledTimes(1) + expect(survivorRefresh).toHaveBeenCalledTimes(2) + expect(widget('remote.0.model')).toBe(survivor) + expect( + node.widgets + ?.filter((w) => w.name.startsWith('remote.')) + .map((w) => w.name) + ).toEqual([ + 'remote.$notice', + 'remote.0', + 'remote.0.model', + 'remote.0.model.0', + 'remote.0.model.1', + 'remote.$add' + ]) + widget('remote.0').callback?.(undefined) + expect( + node.widgets + ?.filter((w) => w.name.startsWith('remote.')) + .map((w) => w.name) + ).toEqual(['remote.$notice', 'remote.$add']) + graph.remove(node) + api.dispatchCustomEvent('execution_success', { + prompt_id: 'test', + timestamp: 0 + }) + expect(survivorRefresh).toHaveBeenCalledTimes(2) + }) + + it('keeps control widgets with their row through removal and restoration', () => { + const { node, widget } = setup() + useLitegraphService().addNodeInput(node, { + name: 'seeds', + type: 'COMFY_DYNAMICGROUP_V3', + isOptional: false, + min: 0, + max: 3, + template: { + required: { + seed: ['INT', { default: 1, control_after_generate: true }] + } + } + }) + widget('seeds').value = 2 + widget('seeds.1.seed.0').value = 'fixed' + widget('seeds.0').callback?.(undefined) + expect(widget('seeds.0.seed.0').value).toBe('fixed') + const saved = node.serialize() + + widget('seeds').value = 0 + node.configure(saved) + + expect(widget('seeds.0.seed.0').value).toBe('fixed') + expect( + node.widgets?.filter((w) => w.name.startsWith('seeds.')) + ).toHaveLength(5) + }) + + it('restores rows and their edited values after switching the containing DynamicCombo', () => { + const { node, widget } = setup() + useLitegraphService().addNodeInput(node, { + name: 'mode', + type: 'COMFY_DYNAMICCOMBO_V3', + isOptional: false, + options: [ + { + key: 'on', + inputs: { + required: { + rows: [ + 'COMFY_DYNAMICGROUP_V3', + { + min: 0, + max: 3, + template: { required: { text: ['STRING', { default: '' }] } } + } + ] + } + } + }, + { key: 'off', inputs: {} } + ] + }) + widget('mode.rows').value = 2 + widget('mode.rows.1.text').value = 'saved' + + widget('mode').value = 'off' + widget('mode').value = 'on' + + expect(widget('mode.rows').value).toBe(2) + expect(widget('mode.rows.1.text').value).toBe('saved') + }) + + it('uses refreshed template options when adding another row', () => { + const { node, widget } = setup() + node.type = 'DynamicGroupTest' + widget('loras').value = 1 + const definition: ComfyNodeDef = { + name: 'DynamicGroupTest', + display_name: 'DynamicGroupTest', + category: 'test', + python_module: 'test', + description: '', + output: [], + output_node: false, + deprecated: false, + experimental: false, + input: { + required: { + loras: [ + 'COMFY_DYNAMICGROUP_V3', + { + min: 0, + max: 3, + template: { + required: { + lora_name: ['COMBO', { options: ['A', 'B', 'C', 'D'] }] + } + } + } + ] + } + } + } + useNodeDefStore().updateNodeDefs([definition]) + + widget('loras.$add').callback?.(undefined) + + expect(widget('loras.1.lora_name').options.values).toEqual([ + 'A', + 'B', + 'C', + 'D' + ]) + }) + + it('submits no group fields for zero rows, then creates complete rows up to max', async () => { + const { node, graph, widget } = setup() + expect((await graphToPrompt(graph)).output[node.id].inputs).toEqual({ + before: 'first', + after: 'last' + }) + for (let i = 0; i < 4; i++) widget('loras.$add').callback?.(undefined) + expect(widget('loras').value).toBe(3) + expect(widget('loras.$add').options.disabled).toBe(true) + expect((await graphToPrompt(graph)).output[node.id].inputs).toEqual({ + before: 'first', + after: 'last', + 'loras.0.lora_name': 'A', + 'loras.0.strength': 1, + 'loras.0.enabled': true, + 'loras.1.lora_name': 'A', + 'loras.1.strength': 1, + 'loras.1.enabled': true, + 'loras.2.lora_name': 'A', + 'loras.2.strength': 1, + 'loras.2.enabled': true + }) + }) + + it('preserves surviving values, links and widget identities when deleting the middle row', async () => { + const { node, graph, widget } = setup() + widget('loras').value = 3 + widget('loras.0.lora_name').value = 'A' + widget('loras.1.lora_name').value = 'B' + widget('loras.2.lora_name').value = 'C' + widget('loras.2.strength').value = 0.5 + const source = new LGraphNode('Source') + source.addOutput('value', 'FLOAT') + graph.add(source) + const retained = source.connect( + 0, + node, + node.findInputSlot('loras.2.strength') + ) + const removed = source.connect( + 0, + node, + node.findInputSlot('loras.1.strength') + ) + if (!retained || !removed) throw new Error('Failed to connect group fields') + + widget('loras.1').callback?.(undefined) + + expect(widget('loras').value).toBe(2) + expect(widget('loras.1.lora_name').value).toBe('C') + expect(widget('loras.1.strength').value).toBe(0.5) + expect(node.getInputLink(node.findInputSlot('loras.1.strength'))).toBe( + retained + ) + expect(graph.getLink(removed.id)).toBeUndefined() + const id = widget('loras.1.lora_name').widgetId + if (!id) throw new Error('Widget not registered') + expect(useWidgetValueStore().getWidget(id)?.value).toBe('C') + expect(node.widgets?.some((w) => w.name.startsWith('loras.2'))).toBe(false) + const inputs = (await graphToPrompt(graph)).output[node.id].inputs + expect(inputs['loras.1.strength']).toEqual([String(source.id), 0]) + expect(inputs['loras.1.lora_name']).toBe('C') + }) + + it('keeps min rows and ignores non-finite restored counts', () => { + const { widget } = setup(1, 2) + widget('loras.0').callback?.(undefined) + expect(widget('loras').value).toBe(1) + widget('loras').value = 2 + expect(widget('loras').value).toBe(2) + widget('loras').value = NaN + expect(widget('loras').value).toBe(2) + widget('loras').value = 1.9 + expect(widget('loras').value).toBe(1) + widget('loras').value = -1 + expect(widget('loras').value).toBe(1) + }) + + it.for([false, true])( + 'preserves restored overflow rows with named restoration = %s', + (named) => { + LiteGraph.namedValuesRestore = named + const { node, widget } = setup(0, 5) + widget('loras').value = 6 + widget('loras.5.lora_name').value = 'C' + const restored = setup(0, 5) + restored.node.configure(node.serialize()) + expect(restored.widget('loras').value).toBe(6) + expect(restored.widget('loras.5.lora_name').value).toBe('C') + restored.widget('loras.$add').callback?.(undefined) + expect(restored.widget('loras').value).toBe(6) + restored.widget('loras.0').callback?.(undefined) + expect(restored.widget('loras.$add').options.disabled).toBe(true) + restored.widget('loras.$add').callback?.(undefined) + expect(restored.widget('loras').value).toBe(5) + restored.widget('loras.0').callback?.(undefined) + expect(restored.widget('loras.$add').options.disabled).toBe(false) + restored.widget('loras.$add').callback?.(undefined) + expect(restored.widget('loras').value).toBe(5) + } + ) + + it.for([false, true])( + 'restores static widgets surrounding rows with named restoration = %s', + async (named) => { + LiteGraph.namedValuesRestore = named + const { node, graph, widget } = setup() + widget('before').value = 'head' + widget('loras').value = 2 + widget('loras.1.lora_name').value = 'B' + widget('loras.1.strength').value = 0.6 + widget('loras.1.enabled').value = false + widget('after').value = 'tail' + node.setSize([400, 600]) + const saved = node.serialize() + expect(saved.widgets_values).toEqual([ + 'head', + 2, + 'A', + 1, + true, + 'B', + 0.6, + false, + 'tail' + ]) + + const restored = setup() + restored.node.configure(saved) + + expect(restored.node.serialize().widgets_values).toEqual( + saved.widgets_values + ) + expect(restored.node.inputs.map((input) => input.name)).toEqual( + saved.inputs?.map((input) => input.name) + ) + expect(restored.node.size[1]).toBe(600) + expect( + (await graphToPrompt(restored.graph)).output[restored.node.id].inputs + ).toEqual((await graphToPrompt(graph)).output[node.id].inputs) + } + ) +}) diff --git a/src/core/graph/widgets/dynamicGroupWidget.ts b/src/core/graph/widgets/dynamicGroupWidget.ts new file mode 100644 index 00000000000..46a34295646 --- /dev/null +++ b/src/core/graph/widgets/dynamicGroupWidget.ts @@ -0,0 +1,432 @@ +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 { + getWidgetIds, + resolveNodeRootGraphId +} from '@/lib/litegraph/src/utils/widget' +import { transformInputSpecV1ToV2 } from '@/schemas/nodeDef/migration' +import type { InputSpec } from '@/schemas/nodeDefSchema' +import { zDynamicGroupInputSpec } from '@/schemas/nodeDefSchema' +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( + node: LGraphNode, + inputName: string, + inputData: InputSpec +) { + const { + min, + max, + template, + group_name = inputName + } = zDynamicGroupInputSpec.parse(inputData)[1] + const fields = { ...template.required, ...template.optional } + const store = useWidgetValueStore() + const { addNodeInput } = useLitegraphService() + const controller: IBaseWidget = node.addCustomWidget({ + name: inputName, + type: 'number', + value: min, + y: 0, + hidden: true, + options: { min, max, socketless: true, serialize: false } + }) + node.addCustomWidget( + new DynamicGroupNoticeWidget( + { + name: `${inputName}.$notice`, + label: group_name, + type: 'dynamic_group_notice', + value: undefined, + y: 0, + serialize: false, + options: { + socketless: true, + serialize: false, + surfaces: { canvas: 'shown', vueNode: 'never', panel: 'never' } + } + }, + node + ) + ) + const initialCount = controller.value + let rowCount = 0 + const add: IBaseWidget = node.addCustomWidget({ + name: `${inputName}.$add`, + label: t('dynamicGroup.add', { group: group_name }), + type: 'button', + value: undefined, + y: 0, + serialize: false, + options: { + socketless: true, + serialize: false, + iconClass: 'icon-[lucide--plus]', + surfaces: { canvas: 'never', vueNode: 'shown', panel: 'shown' } + }, + callback: () => + changeRows(() => { + if (rows().length < max) controller.value = rows().length + 1 + }) + }) + + function rows() { + return (node.widgets ?? []).filter( + (widget) => + widget.type === 'dynamic_group_row' && + widget.name.startsWith(`${inputName}.`) + ) + } + + function publish() { + const headers = rows() + rowCount = headers.length + headers.forEach((header, index) => { + header.label = t('dynamicGroup.row', { + group: group_name, + index: index + 1 + }) + header.options.disabled = headers.length <= min + }) + add.options.disabled = headers.length >= max + if (controller.widgetId) store.setValue(controller.widgetId, headers.length) + const graphId = resolveNodeRootGraphId(node) + if (graphId && node.widgets) + store.setNodeWidgetOrder(graphId, node.id, getWidgetIds(node.widgets)) + } + + function changeRows(change: () => void) { + const graph = node.graph + graph?.beforeChange() + graph?.canvasAction((canvas) => canvas.emitBeforeChange()) + try { + change() + node.setSize([node.size[0], node.computeSize([...node.size])[1]]) + } finally { + graph?.afterChange() + graph?.canvasAction((canvas) => canvas.emitAfterChange()) + node.setDirtyCanvas(true, true) + } + } + + 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, hosts)) return false + for (const widget of removed.reverse()) { + if (node.widgets.includes(widget)) node.removeWidget(widget) + } + for (const header of rows()) { + const oldPrefix = header.name + const row = Number(oldPrefix.slice(inputName.length + 1)) + if (row <= index) continue + const newPrefix = `${inputName}.${row - 1}` + renameRow(oldPrefix, newPrefix, hosts) + } + return true + } + + 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, + inputs, + previous.links, + true + ) + if (!result.ok) return false + for (const link of node.graph?.floatingLinks.values() ?? []) { + if (link.target_id !== node.id) continue + const slot = inputs.indexOf(previous.inputs[link.target_slot]) + if (slot === -1) node.graph?.removeFloatingLink(link) + else link.target_slot = slot + } + 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 + } + + function fieldLabel( + index: number, + field: string, + row = t('dynamicGroup.row', { group: group_name, index: index + 1 }) + ) { + return `${row} ${fields[field][1]?.display_name ?? field}` + } + + function renameRowLabels( + oldIndex: number, + newIndex: number, + hosts: PromotionHostLookup, + rowLabel?: string + ) { + for (const field of Object.keys(fields)) { + const name = `${inputName}.${newIndex}.${field}` + const slot = node.findInputSlot(name) + const input = slot === -1 ? undefined : node.inputs[slot] + const previous = + input?.localized_name ?? fieldLabel(oldIndex, field, rowLabel) + const next = fieldLabel(newIndex, field) + const widget = node.widgets?.find((widget) => widget.name === name) + if (widget?.label === previous) widget.label = next + if (!input) continue + if (input.label === previous) input.label = next + input.localized_name = next + renamePromotedInputLabel(node, slot, previous, next, hosts) + } + } + + 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}.`)) + widget.name = newPrefix + widget.name.slice(oldPrefix.length) + } + for (const input of node.inputs) { + if (!input.name.startsWith(`${oldPrefix}.`)) continue + input.name = newPrefix + input.name.slice(oldPrefix.length) + if (input.widget) input.widget.name = input.name + } + renameRowLabels( + Number(oldPrefix.slice(inputName.length + 1)), + Number(newPrefix.slice(inputName.length + 1)), + hosts, + rowLabel + ) + } + + function addRow(index: number, hosts: PromotionHostLookup) { + const previousSize: [number, number] = [...node.size] + const start = node.widgets?.length ?? 0 + const previous = captureInputLayout(node) + const header: IBaseWidget = node.addCustomWidget({ + name: `${inputName}.${index}`, + type: 'dynamic_group_row', + value: undefined, + y: 0, + serialize: false, + options: { + socketless: true, + serialize: false, + surfaces: { canvas: 'never', vueNode: 'shown', panel: 'shown' } + }, + callback: () => { + if (rows().length <= min) return + changeRows(() => { + removeRow(Number(header.name.slice(inputName.length + 1))) + publish() + }) + } + }) + addRowFields(index, hosts) + const addedInputs = node.inputs.splice(previous.inputs.length) + const inputs = [ + ...previous.inputs, + ...addedInputs.filter( + (added) => !previous.inputs.some((input) => input.name === added.name) + ) + ] + const result = replaceNodeInputs(node, previous, inputs, previous.links) + if (!result.ok) { + for (const widget of (node.widgets?.slice(start) ?? []).reverse()) + node.removeWidget(widget) + node.setSize(previousSize) + return false + } + for (const addedInput of addedInputs) { + const existing = previous.inputs.find( + (input) => input.name === addedInput.name + ) + if (existing) existing.widget = addedInput.widget + } + const widgets = node.widgets + if (!widgets) return false + const added = widgets.splice(start) + widgets.splice(widgets.indexOf(add), 0, ...added) + return true + } + + 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(node, slot, previous, label, hosts) + return input + } + + function addRowFields(index: number, hosts: PromotionHostLookup) { + for (const [fields, isOptional] of [ + [template.required, false], + [template.optional, true] + ] as const) { + for (const [field, spec] of Object.entries(fields ?? {})) { + const name = `${inputName}.${index}.${field}` + const refreshed = useNodeDefStore().getInputSpecForWidget(node, name) + const fieldStart = node.widgets?.length ?? 0 + addNodeInput(node, { + ...(refreshed ?? + transformInputSpecV1ToV2(spec, { name, isOptional })), + display_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 + options.surfaces = { + ...deriveWidgetSurfaces(widget), + canvas: 'never' + } + widget.options = options + if (widget.name === name) { + if (input?.label) widget.label = input.label + return + } + widget.label ??= widget.name + widget.name = `${name}.${auxiliaryIndex++}` + }) + } + } + } + + function validateSavedRowCount(value: number, requested: number) { + const graphId = resolveNodeRootGraphId(node) + if (!graphId) return + const widgets = node.widgets ?? [] + const position = widgets + .filter((widget) => widget.serialize !== false) + .indexOf(controller) + const restored = store.getRestoredWidgetValue( + graphId, + node.id, + inputName, + position + ) + if (restored?.value !== value) return + const savedValueCount = store.getRestoredWidgetValueCount(graphId, node.id) + if (requested > Math.max(max, savedValueCount ?? 0)) + throw new RangeError( + `Invalid saved row count for DynamicGroup '${inputName}'` + ) + } + + Object.defineProperty(controller, 'value', { + get: () => rowCount, + set(value: unknown) { + if (typeof value !== 'number' || !Number.isFinite(value)) return + 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, hosts)) break + } + while (rows().length < count) { + if (!addRow(rows().length, hosts)) break + } + publish() + } + }) + controller.value = initialCount + 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) continue + host.disconnectInput(slot) + if (link.origin_id === SUBGRAPH_INPUT_ID) + disconnectOrphanedPromotion(host.graph, link.origin_slot, hosts) + } +} + +class DynamicGroupNoticeWidget extends VueOnlyWidget { + protected get vueOnlyLabel(): string { + return this.label ?? this.name + } +} diff --git a/src/core/graph/widgets/dynamicInputSpec.test.ts b/src/core/graph/widgets/dynamicInputSpec.test.ts new file mode 100644 index 00000000000..3c187f21b4a --- /dev/null +++ b/src/core/graph/widgets/dynamicInputSpec.test.ts @@ -0,0 +1,127 @@ +import { describe, expect, it } from 'vitest' + +import type { ComfyInputsSpec, InputSpec } from '@/schemas/nodeDefSchema' +import { zDynamicGroupInputSpec } from '@/schemas/nodeDefSchema' + +import { resolveDynamicInputSpec } from './dynamicInputSpec' + +const inputs: ComfyInputsSpec = { + required: { + loras: [ + 'COMFY_DYNAMICGROUP_V3', + { + min: 0, + max: 3, + template: { + required: { name: ['COMBO', { options: ['A', 'B'] }] }, + optional: { strength: ['FLOAT', { default: 1 }] } + } + } + ] + } +} + +describe('DynamicGroup input specifications', () => { + it('resolves required fields independently of the minimum row count', () => { + expect( + resolveDynamicInputSpec(inputs, 'loras.5.name', () => undefined) + ).toEqual({ spec: ['COMBO', { options: ['A', 'B'] }], isOptional: false }) + expect( + resolveDynamicInputSpec(inputs, 'loras.2.strength', () => undefined) + ).toEqual({ spec: ['FLOAT', { default: 1 }], isOptional: true }) + }) + + it.for(['loras.01.name', 'loras.-1.name', 'loras.0.unknown', 'loras.0'])( + 'does not resolve invalid field name %s', + (name) => { + expect( + resolveDynamicInputSpec(inputs, name, () => undefined) + ).toBeUndefined() + } + ) + + it.for([ + 'constructor', + 'toString', + 'loras.0.constructor', + 'loras.0.toString' + ])('does not resolve inherited property %s', (name) => { + expect( + resolveDynamicInputSpec(inputs, name, () => undefined) + ).toBeUndefined() + }) + + it.for(['constructor', 'loras.0.constructor'])( + 'resolves an explicitly declared optional field: %s', + (name) => { + const spec: InputSpec = ['STRING', {}] + const optional = { constructor: spec } + expect( + resolveDynamicInputSpec( + { + required: { + loras: ['COMFY_DYNAMICGROUP_V3', { template: { optional } }] + }, + optional + }, + name, + () => undefined + ) + ).toEqual({ spec: ['STRING', {}], isOptional: true }) + } + ) + + it('requires a separator before a numeric field name', () => { + const numeric: ComfyInputsSpec = { + required: { + loras: [ + 'COMFY_DYNAMICGROUP_V3', + { template: { required: { '10': ['STRING', {}] } } } + ] + } + } + expect( + resolveDynamicInputSpec(numeric, 'loras.10', () => undefined) + ).toBeUndefined() + expect( + resolveDynamicInputSpec(numeric, 'loras.1.10', () => undefined)?.spec + ).toEqual(['STRING', {}]) + }) + + it('resolves groups inside the selected DynamicCombo option', () => { + const combo: ComfyInputsSpec = { + required: { + mode: [ + 'COMFY_DYNAMICCOMBO_V3', + { + options: [ + { key: 'on', inputs }, + { key: 'off', inputs: {} } + ] + } + ] + } + } + expect( + resolveDynamicInputSpec(combo, 'mode.loras.0.name', () => 'on')?.spec + ).toEqual(['COMBO', { options: ['A', 'B'] }]) + expect( + resolveDynamicInputSpec(combo, 'mode.loras.0.name', () => 'off') + ).toBeUndefined() + }) + + it.for([{ min: 2, max: 1 }, { max: 101 }, { min: 0.5 }, { max: false }])( + 'rejects invalid bounds %j', + (bounds) => { + expect( + zDynamicGroupInputSpec.safeParse([ + 'COMFY_DYNAMICGROUP_V3', + { + template: { required: { value: ['STRING', {}] } }, + ...bounds + } + ]).success + ).toBe(false) + } + ) +}) diff --git a/src/core/graph/widgets/dynamicInputSpec.ts b/src/core/graph/widgets/dynamicInputSpec.ts new file mode 100644 index 00000000000..82be4e998b5 --- /dev/null +++ b/src/core/graph/widgets/dynamicInputSpec.ts @@ -0,0 +1,60 @@ +import { + zDynamicComboInputSpec, + zDynamicGroupInputSpec +} from '@/schemas/nodeDefSchema' +import type { ComfyInputsSpec, InputSpec } from '@/schemas/nodeDefSchema' +import type { WidgetValue } from '@/types/simplifiedWidget' + +export function resolveDynamicInputSpec( + inputs: ComfyInputsSpec, + name: string, + getValue: (name: string) => WidgetValue, + prefix = '' +): { spec: InputSpec; isOptional: boolean } | undefined { + for (const [fields, isOptional] of [ + [inputs.required, false], + [inputs.optional, true] + ] as const) { + if (fields && Object.hasOwn(fields, name)) + return { spec: fields[name], isOptional } + for (const [key, spec] of Object.entries(fields ?? {})) { + if (!name.startsWith(`${key}.`)) continue + const resolved = resolveNestedInputSpec(spec, key, name, getValue, prefix) + if (resolved) return resolved + } + } +} + +function resolveNestedInputSpec( + spec: InputSpec, + key: string, + name: string, + getValue: (name: string) => WidgetValue, + prefix: string +): { spec: InputSpec; isOptional: boolean } | undefined { + const suffix = name.slice(key.length + 1) + const group = zDynamicGroupInputSpec.safeParse(spec).data + if (group) { + const separator = suffix.indexOf('.') + if (separator === -1) return + const index = suffix.slice(0, separator) + if (!/^(0|[1-9][0-9]*)$/.test(index)) return + const field = suffix.slice(separator + 1) + const template = group[1].template + if (template.required && Object.hasOwn(template.required, field)) + return { spec: template.required[field], isOptional: false } + if (template.optional && Object.hasOwn(template.optional, field)) + return { spec: template.optional[field], isOptional: true } + } + const combo = zDynamicComboInputSpec.safeParse(spec).data + const selected = combo?.[1].options.find( + ({ key: option }) => option === getValue(`${prefix}${key}`) + ) + if (selected) + return resolveDynamicInputSpec( + selected.inputs, + suffix, + getValue, + `${prefix}${key}.` + ) +} diff --git a/src/core/graph/widgets/dynamicTypes.ts b/src/core/graph/widgets/dynamicTypes.ts index cc5cdf2ea4e..ccc80231419 100644 --- a/src/core/graph/widgets/dynamicTypes.ts +++ b/src/core/graph/widgets/dynamicTypes.ts @@ -1,5 +1,9 @@ import { transformInputSpecV1ToV2 } from '@/schemas/nodeDef/migration' -import { zAutogrowOptions, zMatchTypeOptions } from '@/schemas/nodeDefSchema' +import { + zAutogrowOptions, + zDynamicGroupInputSpec, + zMatchTypeOptions +} from '@/schemas/nodeDefSchema' import type { InputSpec } from '@/schemas/nodeDefSchema' import type { InputSpec as InputSpecV2 } from '@/schemas/nodeDef/nodeDefSchemaV2' @@ -7,6 +11,16 @@ const dynamicTypeResolvers: Record< string, (inputSpec: InputSpecV2) => string[] > = { + COMFY_DYNAMICGROUP_V3: (input) => { + const template = zDynamicGroupInputSpec.safeParse([input.type, input]) + .data?.[1].template + return Object.entries({ + ...template?.required, + ...template?.optional + }).flatMap(([name, spec]) => + resolveInputType(transformInputSpecV1ToV2(spec, { name })) + ) + }, COMFY_AUTOGROW_V3: resolveAutogrowType, COMFY_MATCHTYPE_V3: (input) => zMatchTypeOptions diff --git a/src/core/graph/widgets/dynamicWidgets.ts b/src/core/graph/widgets/dynamicWidgets.ts index 9ab86d64e1d..e995e76f033 100644 --- a/src/core/graph/widgets/dynamicWidgets.ts +++ b/src/core/graph/widgets/dynamicWidgets.ts @@ -32,6 +32,7 @@ import { graphScopeOf } from '@/types/graphScopeId' import { useWidgetValueStore } from '@/stores/widgetValueStore' import type { WidgetValue } from '@/types/simplifiedWidget' import { widgetId } from '@/types/widgetId' +import { dynamicGroupWidget } from './dynamicGroupWidget' type MatchTypeNode = LGraphNode & Pick, 'onConnectionsChange'> & { @@ -289,7 +290,10 @@ function dynamicComboWidget( return { widget, minWidth, minHeight } } -export const dynamicWidgets = { COMFY_DYNAMICCOMBO_V3: dynamicComboWidget } +export const dynamicWidgets = { + COMFY_DYNAMICCOMBO_V3: dynamicComboWidget, + COMFY_DYNAMICGROUP_V3: dynamicGroupWidget +} const dynamicInputs: Record< string, (node: LGraphNode, inputSpec: InputSpecV2) => void diff --git a/src/lib/litegraph/src/subgraph/SubgraphNode.ts b/src/lib/litegraph/src/subgraph/SubgraphNode.ts index 9ccfbf7b334..d6ed6d8a7c7 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/locales/en/main.json b/src/locales/en/main.json index 4dff825ca63..d4f585c0353 100644 --- a/src/locales/en/main.json +++ b/src/locales/en/main.json @@ -1,4 +1,9 @@ { + "dynamicGroup": { + "add": "Add {group}", + "remove": "Remove {row}", + "row": "{group} #{index}" + }, "g": { "shortcutSuffix": " ({shortcut})", "megapixelsValue": "{count} MP", diff --git a/src/platform/assets/mappings/modelNodeMappings.ts b/src/platform/assets/mappings/modelNodeMappings.ts index 7510fdaf83d..ee720709f77 100644 --- a/src/platform/assets/mappings/modelNodeMappings.ts +++ b/src/platform/assets/mappings/modelNodeMappings.ts @@ -1,9 +1,11 @@ +const loraStackFilename = /^loras\.(?:0|[1-9]\d*)\.lora_name$/ + /** * Default mappings from model directories to loader nodes. * * Each entry maps a model folder (as it appears in the model browser) * to the node class that loads models from that folder and the - * input key where the model name is inserted. + * input name or pattern used to select the model widget. * * An empty key ('') means the node auto-loads models without a widget * selector, so no widget value is assigned when the node is added. @@ -12,10 +14,13 @@ * "a/b/c" → "a/b" → "a", so registering a parent directory covers * all its children unless a more specific entry exists. * - * Format: [modelDirectory, nodeClass, inputKey] + * Patterns enable the picker on every matching widget. Model drops populate + * the first matching widget. + * + * Format: [modelDirectory, nodeClass, inputNameOrPattern] */ export const MODEL_NODE_MAPPINGS: ReadonlyArray< - readonly [string, string, string] + readonly [string, string, string | RegExp] > = [ // Intentionally unmapped (widget values are model ids / HF repo ids, not // asset filenames): rmbg, florence2, LLM/Qwen-VL/*, qwen-tts, diffusers @@ -33,6 +38,8 @@ export const MODEL_NODE_MAPPINGS: ReadonlyArray< ['checkpoints', 'ImageOnlyCheckpointLoader', 'ckpt_name'], ['loras', 'LoraLoader', 'lora_name'], ['loras', 'LoraLoaderModelOnly', 'lora_name'], + ['loras', 'LoadLoraModel', loraStackFilename], + ['loras', 'LoadLoraTextEncoder', loraStackFilename], ['vae', 'VAELoader', 'vae_name'], ['controlnet', 'ControlNetLoader', 'control_net_name'], ['diffusion_models', 'UNETLoader', 'unet_name'], @@ -158,4 +165,4 @@ export const MODEL_NODE_MAPPINGS: ReadonlyArray< // ---- LTX-Video IC-LoRA (ComfyUI-LTXVideo) ---- ['loras', 'LTXICLoRALoaderModelOnly', 'lora_name'] -] as const satisfies ReadonlyArray +] as const satisfies ReadonlyArray diff --git a/src/platform/assets/services/assetService.test.ts b/src/platform/assets/services/assetService.test.ts index d75d96facab..71597811002 100644 --- a/src/platform/assets/services/assetService.test.ts +++ b/src/platform/assets/services/assetService.test.ts @@ -1,5 +1,7 @@ +import { fromPartial } from '@total-typescript/shoehorn' + import type { ComfyApp } from '@/scripts/app' -import { useModelToNodeStore } from '@/stores/modelToNodeStore' +import { useNodeDefStore } from '@/stores/nodeDefStore' import { useAssetsStore } from '@/stores/assetsStore' import { beforeEach, describe, expect, it, vi } from 'vitest' @@ -88,19 +90,13 @@ function validAsset(overrides: Partial = {}): AssetItem { } beforeEach(() => { - const registeredNodeTypes: Record = { - CheckpointLoaderSimple: 'ckpt_name', - LoraLoader: 'lora_name' - } - const nodeTypeCategories: Record = { - CheckpointLoaderSimple: 'checkpoints', - LoraLoader: 'loras' - } - vi.mocked(useModelToNodeStore().getRegisteredNodeTypes).mockImplementation( - () => registeredNodeTypes - ) - vi.mocked(useModelToNodeStore().getCategoryForNodeType).mockImplementation( - (nodeType: string) => nodeTypeCategories[nodeType] + useNodeDefStore().nodeDefsByName = Object.fromEntries( + [ + 'CheckpointLoaderSimple', + 'LoraLoader', + 'LoadLoraModel', + 'LoadLoraTextEncoder' + ].map((name) => [name, fromPartial({ name })]) ) vi.spyOn(useAssetsStore().inputAssets, 'invalidate').mockImplementation( mockInvalidateInputAssets @@ -142,6 +138,41 @@ describe(assetService.shouldUseWidgetAssetPicker, () => { ).toBe(true) }) + it.for([ + ['LoadLoraModel', 'loras.0.lora_name'], + ['LoadLoraModel', 'loras.1.lora_name'], + ['LoadLoraModel', 'loras.19.lora_name'], + ['LoadLoraTextEncoder', 'loras.0.lora_name'], + ['LoadLoraTextEncoder', 'loras.1.lora_name'], + ['LoadLoraTextEncoder', 'loras.19.lora_name'] + ])('uses the asset picker for %s / %s on cloud', ([nodeType, name]) => { + mockDistributionState.isCloud = true + expect(assetService.shouldUseWidgetAssetPicker(nodeType, name)).toBe(true) + }) + + it.for(['LoadLoraModel', 'LoadLoraTextEncoder'])( + 'keeps the combo widget for %s outside cloud', + (nodeType) => { + mockDistributionState.isCloud = false + expect( + assetService.shouldUseWidgetAssetPicker(nodeType, 'loras.1.lora_name') + ).toBe(false) + } + ) + + it.for([ + ['LoadLoraModel', 'loras.1.strength'], + ['LoadLoraModel', 'loras.-1.lora_name'], + ['LoadLoraModel', 'loras.01.lora_name'], + ['LoadLoraModel', 'prefix.loras.1.lora_name'], + ['LoadLoraModel', 'loras.1.lora_name.suffix'], + ['LoraLoader', 'loras.1.lora_name'], + ['UnknownNode', 'loras.1.lora_name'] + ])('does not treat %s / %s as an asset input', ([nodeType, name]) => { + mockDistributionState.isCloud = true + expect(assetService.shouldUseWidgetAssetPicker(nodeType, name)).toBe(false) + }) + it('returns false when nodeType is undefined', () => { mockDistributionState.isCloud = true diff --git a/src/platform/assets/services/assetService.ts b/src/platform/assets/services/assetService.ts index 881654e6d17..49ade3e2016 100644 --- a/src/platform/assets/services/assetService.ts +++ b/src/platform/assets/services/assetService.ts @@ -571,9 +571,7 @@ function createAssetService() { widgetName: string ): boolean { if (!nodeType || !widgetName) return false - return ( - useModelToNodeStore().getRegisteredNodeTypes()[nodeType] === widgetName - ) + return useModelToNodeStore().isModelWidget(nodeType, widgetName) } /** diff --git a/src/platform/workflow/core/services/workflowService.test.ts b/src/platform/workflow/core/services/workflowService.test.ts index 6b14c733143..e25cbc2f26d 100644 --- a/src/platform/workflow/core/services/workflowService.test.ts +++ b/src/platform/workflow/core/services/workflowService.test.ts @@ -388,6 +388,35 @@ describe('useWorkflowService', () => { ) }) + it('persists the displayed state when Undo fails before graph configuration', async () => { + vi.spyOn(useSettingStore(), 'get').mockImplementation( + (key: string) => key === 'Comfy.Workflow.Persist' + ) + const activeWorkflow = createModeTestWorkflow() + const saved = makeWorkflowData() + const current = makeWorkflowData({ note: 'latest edit' }) + const tracker = new ChangeTracker(activeWorkflow, saved) + activeWorkflow.changeTracker = tracker + tracker.activeState = current + tracker.undoQueue.push(saved) + workflowStore.activeWorkflow = activeWorkflow + vi.mocked(app.loadGraphData).mockImplementationOnce(async () => { + useWorkflowService().beforeLoadNewGraph(false) + return false + }) + + await tracker.undo() + + expect(useWorkflowDraftStoreV2().saveDraft).toHaveBeenCalledWith( + activeWorkflow.path, + JSON.stringify(current), + { name: activeWorkflow.key, isTemporary: activeWorkflow.isTemporary } + ) + expect(activeWorkflow.activeState).toEqual(current) + expect(tracker.undoQueue).toEqual([saved]) + expect(tracker.redoQueue).toEqual([]) + }) + it('should show an error toast when the V2 draft store cannot save', () => { vi.spyOn(useSettingStore(), 'get').mockImplementation((key: string) => { return key === 'Comfy.Workflow.Persist' diff --git a/src/platform/workflow/core/services/workflowService.ts b/src/platform/workflow/core/services/workflowService.ts index 0bf41073903..07e97b728c1 100644 --- a/src/platform/workflow/core/services/workflowService.ts +++ b/src/platform/workflow/core/services/workflowService.ts @@ -678,7 +678,9 @@ export const useWorkflowService = () => { workflowData: ComfyWorkflowJSON, shareId?: string ) => { + const graphState = app.rootGraphOrUndefined?.state await activateLoadedWorkflow(value, workflowData, shareId) + if (graphState !== app.rootGraphOrUndefined?.state) return useNodeOutputStore().restorePreviewsForWorkflow( useWorkspaceStore().workflow.activeWorkflow?.path ) @@ -689,6 +691,7 @@ export const useWorkflowService = () => { workflowData: ComfyWorkflowJSON, shareId?: string ) => { + const graphState = app.rootGraphOrUndefined?.state const workflowStore = useWorkspaceStore().workflow const { isAppMode } = useAppMode() const wasAppMode = isAppMode.value @@ -739,6 +742,7 @@ export const useWorkflowService = () => { ) { const loadedWorkflow = await workflowStore.openWorkflow(existingWorkflow) + if (graphState !== app.rootGraphOrUndefined?.state) return activateRunErrors(loadedWorkflow) if (loadedWorkflow.initialMode === undefined) { // Prefer the file's linearMode over the draft's since the file @@ -774,11 +778,13 @@ export const useWorkflowService = () => { } trackIfEnteringApp(tempWorkflow) const loadedWorkflow = await workflowStore.openWorkflow(tempWorkflow) + if (graphState !== app.rootGraphOrUndefined?.state) return activateRunErrors(loadedWorkflow) return } const loadedWorkflow = await workflowStore.openWorkflow(value) + if (graphState !== app.rootGraphOrUndefined?.state) return activateRunErrors(loadedWorkflow) if (shareId) { loadedWorkflow.shareId = shareId diff --git a/src/platform/workflow/core/utils/restoreDynamicGroupInputs.ts b/src/platform/workflow/core/utils/restoreDynamicGroupInputs.ts new file mode 100644 index 00000000000..7080e436cb9 --- /dev/null +++ b/src/platform/workflow/core/utils/restoreDynamicGroupInputs.ts @@ -0,0 +1,86 @@ +import type { IBaseWidget } from '@/lib/litegraph/src/types/widgets' +import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' +import type { ComfyApiWorkflow } from '@/platform/workflow/validation/schemas/workflowSchema' +import type { ComfyInputsSpec } from '@/schemas/nodeDefSchema' +import { + zDynamicComboInputSpec, + zDynamicGroupInputSpec +} from '@/schemas/nodeDefSchema' +import { useNodeDefStore } from '@/stores/nodeDefStore' + +export function restoreDynamicGroupInputs( + node: LGraphNode, + inputs: ComfyApiWorkflow[string]['inputs'] +) { + const definition = useNodeDefStore().fromLGraphNode(node) + const names = new Map() + const restoredCombos = new Set() + const groups: string[] = [] + + function restore(specs: ComfyInputsSpec, prefix = '') { + for (const [key, spec] of Object.entries({ + ...specs.required, + ...specs.optional + })) { + const name = `${prefix}${key}` + const controller = node.widgets?.find((widget) => widget.name === name) + if (!controller) continue + const combo = zDynamicComboInputSpec.safeParse(spec).data + if (combo) { + const selected = combo[1].options.find( + (option) => option.key === inputs[name] + ) + if (selected) { + controller.value = selected.key + controller.callback?.(selected.key) + restoredCombos.add(name) + restore(selected.inputs, `${name}.`) + } + continue + } + const group = zDynamicGroupInputSpec.safeParse(spec).data + if (!group) continue + groups.push(name) + const fields = { + ...group[1].template.required, + ...group[1].template.optional + } + restoreRows(name, fields, controller) + } + } + + function restoreRows( + name: string, + fields: Record, + controller: IBaseWidget + ) { + const rows = new Map() + for (const input of Object.keys(inputs)) { + if (!input.startsWith(`${name}.`)) continue + const suffix = input.slice(name.length + 1) + const separator = suffix.indexOf('.') + if (separator === -1) continue + const index = suffix.slice(0, separator) + const field = suffix.slice(separator + 1) + if (!/^(0|[1-9][0-9]*)$/.test(index) || !Object.hasOwn(fields, field)) + continue + if (!rows.has(index)) rows.set(index, rows.size) + names.set(input, `${name}.${rows.get(index)}.${field}`) + } + controller.value = rows.size + } + + if (definition) restore(definition.input) + return Object.fromEntries( + Object.entries(inputs) + .filter( + ([name]) => + !restoredCombos.has(name) && + (names.has(name) || + !groups.some( + (group) => name === group || name.startsWith(`${group}.`) + )) + ) + .map(([name, value]) => [names.get(name) ?? name, value]) + ) +} diff --git a/src/platform/workflow/management/stores/workflowStore.ts b/src/platform/workflow/management/stores/workflowStore.ts index 45524d7e0af..9a0ae552c70 100644 --- a/src/platform/workflow/management/stores/workflowStore.ts +++ b/src/platform/workflow/management/stores/workflowStore.ts @@ -212,7 +212,11 @@ export const useWorkflowStore = defineStore('workflow', () => { if (!openWorkflowPaths.value.includes(workflow.path)) { openWorkflowPaths.value.push(workflow.path) } + const graphState = comfyApp.rootGraphOrUndefined?.state const loadedWorkflow = await workflow.load() + if (graphState !== comfyApp.rootGraphOrUndefined?.state) { + throw new DOMException('Workflow load was replaced', 'AbortError') + } activeWorkflow.value = loadedWorkflow comfyApp.canvas.bg_tint = loadedWorkflow.tintCanvasBg diff --git a/src/renderer/extensions/vueNodes/components/LGraphNode.test.ts b/src/renderer/extensions/vueNodes/components/LGraphNode.test.ts index aeeb09ca0fd..32d9c27442e 100644 --- a/src/renderer/extensions/vueNodes/components/LGraphNode.test.ts +++ b/src/renderer/extensions/vueNodes/components/LGraphNode.test.ts @@ -1,4 +1,4 @@ -import { render, screen } from '@testing-library/vue' +import { fireEvent, render, screen } from '@testing-library/vue' import { getActivePinia } from 'pinia' import { beforeEach, describe, expect, it, vi } from 'vitest' @@ -218,6 +218,7 @@ describe('LGraphNode', () => { mockData.mockExecuting = false mockData.mockLgraphNode = null mockData.resizeCallback = null + app.dragOverNode = null const canvasStore = useCanvasStore() canvasStore.selectedNodeIds.clear() @@ -715,6 +716,24 @@ describe('LGraphNode', () => { }) describe('handleDrop', () => { + it('clears the native file target when another handler already consumed the drop', async () => { + mockData.mockLgraphNode = { + onDragOver: vi.fn(), + isSubgraphNode: () => false + } + app.dragOverNode = { + id: toNodeId('previous-node'), + onDragDrop: vi.fn(() => false) + } + const { container } = renderLGraphNode({ nodeData: mockNodeData }) + const event = new Event('drop', { bubbles: true, cancelable: true }) + event.preventDefault() + + await fireEvent(getNodeRoot(container), event) + + expect(app.dragOverNode).toBeNull() + }) + it('should set app.dragOverNode and let event bubble', async () => { mockData.mockLgraphNode = { onDragOver: vi.fn(), diff --git a/src/renderer/extensions/vueNodes/components/LGraphNode.vue b/src/renderer/extensions/vueNodes/components/LGraphNode.vue index 50b0e176bc5..0bf6c5ad2fd 100644 --- a/src/renderer/extensions/vueNodes/components/LGraphNode.vue +++ b/src/renderer/extensions/vueNodes/components/LGraphNode.vue @@ -766,8 +766,8 @@ function handleDragLeave() { isDraggingOver.value = false } -function handleDrop() { +function handleDrop(event: DragEvent) { isDraggingOver.value = false - app.dragOverNode = resolveLGraphNode() + app.dragOverNode = event.defaultPrevented ? null : resolveLGraphNode() } diff --git a/src/renderer/extensions/vueNodes/components/LGraphNodePreview.test.ts b/src/renderer/extensions/vueNodes/components/LGraphNodePreview.test.ts index e9889098619..c60c662658f 100644 --- a/src/renderer/extensions/vueNodes/components/LGraphNodePreview.test.ts +++ b/src/renderer/extensions/vueNodes/components/LGraphNodePreview.test.ts @@ -9,6 +9,7 @@ import type { WidgetGridItem } from '@/renderer/extensions/vueNodes/types/widget import type { ComfyNodeDef as ComfyNodeDefV2 } from '@/schemas/nodeDef/nodeDefSchemaV2' import LGraphNodePreview from '@/renderer/extensions/vueNodes/components/LGraphNodePreview.vue' import { fromPartial } from '@total-typescript/shoehorn' +import type { WidgetValueBinding } from '@/utils/widgetBinding' const WidgetGridProbe = { props: ['processedWidgets'], @@ -43,7 +44,7 @@ const nodeDef = fromPartial({ function renderedWidgets( def: ComfyNodeDefV2, - props: { widgetValues?: Record } = {} + props: { widgetValues?: readonly WidgetValueBinding[] } = {} ) { render(LGraphNodePreview, { props: { nodeDef: def, ...props }, @@ -63,7 +64,7 @@ function renderedWidgets( } function renderedComboWidget( - props: { widgetValues?: Record } = {} + props: { widgetValues?: readonly WidgetValueBinding[] } = {} ) { return renderedWidgets(nodeDef, props).find((w) => w.name === 'ckpt_name') } @@ -106,7 +107,9 @@ describe('LGraphNodePreview', () => { it('leads the combo options with the provided widget value', () => { const widget = renderedComboWidget({ - widgetValues: { ckpt_name: 'sd_xl_base_1.0.safetensors' } + widgetValues: [ + { selector: 'ckpt_name', value: 'sd_xl_base_1.0.safetensors' } + ] }) expect(widget?.options?.values).toEqual([ @@ -116,6 +119,28 @@ describe('LGraphNodePreview', () => { ]) }) + it('previews a pattern-bound value only on the first matching widget', () => { + const widgets = renderedWidgets( + fromPartial({ + name: 'MultiModel', + inputs: { + strength: { type: 'FLOAT', default: 0.5 }, + model_a: { type: 'COMBO', options: ['A.safetensors'] }, + model_b: { type: 'COMBO', options: ['B.safetensors'] } + } + }), + { + widgetValues: [{ selector: /^model_/, value: 'C.safetensors' }] + } + ) + + expect(widgets.map(({ name, value }) => ({ name, value }))).toEqual([ + { name: 'strength', value: 0.5 }, + { name: 'model_a', value: 'C.safetensors' }, + { name: 'model_b', value: 'B.safetensors' } + ]) + }) + it('keeps the combo options untouched when no value is provided', () => { const widget = renderedComboWidget() @@ -123,7 +148,9 @@ describe('LGraphNodePreview', () => { }) it('leads with an explicitly empty provided value', () => { - const widget = renderedComboWidget({ widgetValues: { ckpt_name: '' } }) + const widget = renderedComboWidget({ + widgetValues: [{ selector: 'ckpt_name', value: '' }] + }) expect(widget?.value).toBe('') expect(widget?.options?.values).toEqual([ diff --git a/src/renderer/extensions/vueNodes/components/LGraphNodePreview.vue b/src/renderer/extensions/vueNodes/components/LGraphNodePreview.vue index 89b74aee0f1..8e78ffc4fe4 100644 --- a/src/renderer/extensions/vueNodes/components/LGraphNodePreview.vue +++ b/src/renderer/extensions/vueNodes/components/LGraphNodePreview.vue @@ -51,6 +51,8 @@ import { toNodeId } from '@/types/nodeId' import type { NodeState } from '@/types/nodeState' import type { WidgetValue } from '@/types/simplifiedWidget' import { zeroUuid } from '@/utils/uuid' +import { matchesWidgetName } from '@/utils/widgetBinding' +import type { WidgetValueBinding } from '@/utils/widgetBinding' import { cn } from '@comfyorg/tailwind-utils' const { @@ -60,7 +62,7 @@ const { } = defineProps<{ nodeDef: ComfyNodeDefV2 position?: 'absolute' | 'relative' - widgetValues?: Record + widgetValues?: readonly WidgetValueBinding[] }>() const widgetStore = useWidgetStore() @@ -109,18 +111,32 @@ const nodeData = computed(() => ({ properties: {} })) +const widgetInputs = computed(() => + Object.entries(nodeDef.inputs || {}).filter(([, input]) => + widgetStore.inputIsWidget(input) + ) +) + +const resolvedWidgetValues = computed(() => + Object.fromEntries( + (widgetValues ?? []).flatMap(({ selector, value }) => { + const input = widgetInputs.value.find(([name]) => + matchesWidgetName(name, selector) + ) + return input ? [[input[0], value]] : [] + }) + ) +) + const previewWidgets = computed(() => - Object.entries(nodeDef.inputs || {}) - .filter( - ([, input]) => - widgetStore.inputIsWidget(input) && !input.hidden && !input.advanced - ) + widgetInputs.value + .filter(([, input]) => !input.hidden && !input.advanced) .map(([name, input]) => { const comboValues = input.type === 'COMBO' && Array.isArray(input.options) ? input.options : undefined - const leadValue = widgetValues?.[name] + const leadValue = resolvedWidgetValues.value[name] const value = (leadValue ?? input.default ?? comboValues?.[0] ?? diff --git a/src/renderer/extensions/vueNodes/components/WidgetGrid.test.ts b/src/renderer/extensions/vueNodes/components/WidgetGrid.test.ts index e73ee82735d..51087ed572e 100644 --- a/src/renderer/extensions/vueNodes/components/WidgetGrid.test.ts +++ b/src/renderer/extensions/vueNodes/components/WidgetGrid.test.ts @@ -1,9 +1,15 @@ -import { render, screen } from '@testing-library/vue' +import userEvent from '@testing-library/user-event' +import { render, screen, within } from '@testing-library/vue' import { fromPartial } from '@total-typescript/shoehorn' -import { defineComponent, markRaw } from 'vue' +import { computed, defineComponent, h, markRaw, ref } from 'vue' import { assert, beforeEach, describe, expect, it, vi } from 'vitest' import { createI18n } from 'vue-i18n' +import { testI18n } from '@/components/searchbox/v2/__test__/testUtils' +import WidgetDynamicGroupRow from '@/renderer/extensions/vueNodes/widgets/components/WidgetDynamicGroupRow.vue' +import WidgetButton from '@/renderer/extensions/vueNodes/widgets/components/WidgetButton.vue' +import WidgetSelectDefault from '@/renderer/extensions/vueNodes/widgets/components/WidgetSelectDefault.vue' + import { useCanvasStore } from '@/renderer/core/canvas/canvasStore' import { layoutStore } from '@/renderer/core/layout/store/layoutStore' import WidgetGrid from '@/renderer/extensions/vueNodes/components/WidgetGrid.vue' @@ -194,6 +200,136 @@ describe('WidgetGrid', () => { expect(screen.queryByText('prompt')).not.toBeInTheDocument() }) + it.for([ + { label: 'LoRA #1 model', displayed: 'model', first: 0 }, + { label: 'My model', displayed: 'My model', first: 0 }, + { label: 'LoRA #1 model', displayed: 'LoRA #1 model', first: 1 } + ])( + 'displays $displayed while keeping the accessible name $label', + ({ label, displayed, first }) => { + const items: WidgetGridItem[] = [ + { + renderKey: 'header', + visible: true, + simplified: { + name: 'loras.0', + type: 'dynamic_group_row', + value: undefined, + label: 'LoRA #1' + }, + vueComponent: markRaw(WidgetDynamicGroupRow) + }, + { + renderKey: 'model', + visible: true, + simplified: { + name: 'loras.0.model', + type: 'combo', + value: 'A', + label, + options: { values: ['A', 'B'] } + }, + vueComponent: markRaw(WidgetSelectDefault) + } + ] + renderGrid(items.slice(first), false) + + expect( + screen.getByTestId('widget-layout-field-label').textContent.trim() + ).toBe(displayed) + expect(screen.getByRole('combobox', { name: label })).toBeVisible() + } + ) + + it.for([0, 1])( + 'identifies repeated fields and restores keyboard focus after deletion with min = %s', + async (min) => { + const user = userEvent.setup() + const rows = ref(['A', 'B', 'C']) + const items = computed(() => [ + ...rows.value.flatMap((id, index) => [ + { + renderKey: `${id}-header`, + visible: true, + simplified: { + name: `loras.${index}`, + type: 'dynamic_group_row', + value: undefined, + label: `LoRA #${index + 1}`, + options: { disabled: rows.value.length <= min }, + callback: () => { + rows.value = rows.value.filter((row) => row !== id) + } + }, + vueComponent: markRaw(WidgetDynamicGroupRow) + }, + { + renderKey: `${id}-model`, + visible: true, + simplified: { + name: `loras.${index}.model`, + type: 'combo', + value: id, + label: 'Model', + options: { values: ['A', 'B', 'C'] } + }, + vueComponent: markRaw(WidgetSelectDefault) + } + ]), + { + renderKey: 'add', + visible: true, + simplified: { + name: 'loras.$add', + type: 'button', + value: undefined, + label: 'Add LoRA' + }, + vueComponent: markRaw(WidgetButton) + } + ]) + const Host = defineComponent({ + setup: () => () => + h(WidgetGrid, { + processedWidgets: items.value, + nodeType: 'TestNode', + syncLayout: false + }) + }) + render(Host, { + global: { + plugins: [testI18n], + directives: { tooltip: {} }, + stubs: { AppInput: AppInputStub, InputSlot: InputSlotStub } + } + }) + + for (const index of [1, 2, 3]) { + const group = screen.getByRole('group', { name: `LoRA #${index}` }) + expect( + within(group).getByRole('combobox', { name: 'Model' }) + ).toBeInTheDocument() + } + screen.getByRole('button', { name: 'Remove LoRA #2' }).focus() + await user.keyboard('{Enter}') + expect( + screen.getByRole('button', { name: 'Remove LoRA #2' }) + ).toHaveFocus() + expect( + screen.queryByRole('group', { name: 'LoRA #3' }) + ).not.toBeInTheDocument() + await user.keyboard('{Enter}') + if (min === 0) { + expect( + screen.getByRole('button', { name: 'Remove LoRA #1' }) + ).toHaveFocus() + await user.keyboard('{Enter}') + } + expect(screen.getByRole('button', { name: 'Add LoRA' })).toHaveFocus() + expect(screen.queryAllByRole('group')).toHaveLength(min) + } + ) + it('renders hidden converted widgets as input sockets without controls', () => { render(WidgetGrid, { props: { diff --git a/src/renderer/extensions/vueNodes/components/WidgetGrid.vue b/src/renderer/extensions/vueNodes/components/WidgetGrid.vue index ad593a7bfd8..d337704ac13 100644 --- a/src/renderer/extensions/vueNodes/components/WidgetGrid.vue +++ b/src/renderer/extensions/vueNodes/components/WidgetGrid.vue @@ -8,77 +8,80 @@ flex: gridTemplateRows.includes('auto') ? 1 : undefined }" > - + diff --git a/src/renderer/extensions/vueNodes/widgets/components/WidgetToggleSwitch.test.ts b/src/renderer/extensions/vueNodes/widgets/components/WidgetToggleSwitch.test.ts index e0517fcc4f0..199b4b37ebd 100644 --- a/src/renderer/extensions/vueNodes/widgets/components/WidgetToggleSwitch.test.ts +++ b/src/renderer/extensions/vueNodes/widgets/components/WidgetToggleSwitch.test.ts @@ -1,10 +1,12 @@ import { render, screen } from '@testing-library/vue' import userEvent from '@testing-library/user-event' +import { fromPartial } from '@total-typescript/shoehorn' import { defineComponent, ref } from 'vue' import { createI18n } from 'vue-i18n' import { describe, expect, it, vi } from 'vitest' import { ToggleGroup, ToggleGroupItem } from '@/components/ui/toggle-group' +import { useWorkflowStore } from '@/platform/workflow/management/stores/workflowStore' import type { IWidgetOptions } from '@/lib/litegraph/src/types/widgets' import type { SimplifiedWidget } from '@/types/simplifiedWidget' @@ -74,6 +76,51 @@ describe('WidgetToggleSwitch Value Binding', () => { return { ...result, user } } + it('uses the display label to identify and toggle a dynamically named field', async () => { + const widget = createToggleWidget(false) + widget.name = 'loras.0.enabled' + widget.label = 'LoRA #1 enabled' + const onModelUpdate = vi.fn() + const { user } = mountComponent(widget, false, onModelUpdate) + + await user.click(screen.getByRole('switch', { name: 'LoRA #1 enabled' })) + + expect(onModelUpdate).toHaveBeenCalledWith(true) + }) + + it.for([ + { name: 'switch', options: {}, role: 'switch', label: 'test_toggle' }, + { + name: 'labelled toggle', + options: { on: 'enabled', off: 'disabled' }, + role: 'button', + label: 'disabled' + } + ])( + 'records the updated value after clicking a $name', + async ({ options, role, label }) => { + let currentValue = true + const recordedValues: boolean[] = [] + useWorkflowStore().activeWorkflow = fromPartial({ + changeTracker: { + captureCanvasState: () => recordedValues.push(currentValue) + } + }) + const { user } = mountComponent( + createToggleWidget(true, options), + true, + (value) => { + currentValue = value ?? currentValue + } + ) + + await user.click(screen.getByRole(role, { name: label })) + + expect(currentValue).toBe(false) + expect(recordedValues).toEqual([false]) + } + ) + describe('Vue Event Emission', () => { it('emits Vue event when toggled from false to true', async () => { const widget = createToggleWidget(false) diff --git a/src/renderer/extensions/vueNodes/widgets/components/WidgetToggleSwitch.vue b/src/renderer/extensions/vueNodes/widgets/components/WidgetToggleSwitch.vue index 30a02c1e14d..f25c0197dc6 100644 --- a/src/renderer/extensions/vueNodes/widgets/components/WidgetToggleSwitch.vue +++ b/src/renderer/extensions/vueNodes/widgets/components/WidgetToggleSwitch.vue @@ -32,10 +32,11 @@ " > @@ -47,6 +48,7 @@ import { useI18n } from 'vue-i18n' import Switch from '@/components/ui/switch/Switch.vue' import { ToggleGroup, ToggleGroupItem } from '@/components/ui/toggle-group' +import { useWorkflowStore } from '@/platform/workflow/management/stores/workflowStore' import type { IWidgetOptions } from '@/lib/litegraph/src/types/widgets' import type { SimplifiedWidget } from '@/types/simplifiedWidget' import { useHideLayoutField } from '@/types/widgetTypes' @@ -70,7 +72,12 @@ const hasLabels = computed(() => { function handleOptionChange(value: string | undefined) { if (value) { - modelValue.value = value === 'on' + updateValue(value === 'on') } } + +function updateValue(value: boolean) { + modelValue.value = value + useWorkflowStore().activeWorkflow?.changeTracker.captureCanvasState() +} diff --git a/src/renderer/extensions/vueNodes/widgets/components/layout/WidgetLayoutField.vue b/src/renderer/extensions/vueNodes/widgets/components/layout/WidgetLayoutField.vue index d7824b8e482..b8e5762ce12 100644 --- a/src/renderer/extensions/vueNodes/widgets/components/layout/WidgetLayoutField.vue +++ b/src/renderer/extensions/vueNodes/widgets/components/layout/WidgetLayoutField.vue @@ -8,7 +8,7 @@ import { cn } from '@comfyorg/tailwind-utils' const { widget, rootClass } = defineProps<{ widget: Pick< SimplifiedWidget, - 'name' | 'label' | 'borderStyle' + 'name' | 'label' | 'displayLabel' | 'borderStyle' > rootClass?: string noBorder?: boolean @@ -38,7 +38,7 @@ const borderStyle = computed(() => class="content-center-safe truncate" > diff --git a/src/renderer/extensions/vueNodes/widgets/composables/useRemoteWidget.test.ts b/src/renderer/extensions/vueNodes/widgets/composables/useRemoteWidget.test.ts index 3ba04ed7e9f..b1a4b7dcb23 100644 --- a/src/renderer/extensions/vueNodes/widgets/composables/useRemoteWidget.test.ts +++ b/src/renderer/extensions/vueNodes/widgets/composables/useRemoteWidget.test.ts @@ -1,9 +1,14 @@ -import { useAuthStore } from '@/stores/authStore' -import axios from 'axios' - -import { beforeEach, describe, expect, it, vi } from 'vitest' +import axios, { AxiosHeaders } from 'axios' +import type { AxiosResponse } from 'axios' +import { assert, beforeEach, describe, expect, it, vi } from 'vitest' import type { IWidget } from '@/lib/litegraph/src/litegraph' +import { + LGraph, + LGraphNode, + isComboWidget +} from '@/lib/litegraph/src/litegraph' +import { useAuthStore } from '@/stores/authStore' import { api } from '@/scripts/api' import { useRemoteWidget } from '@/renderer/extensions/vueNodes/widgets/composables/useRemoteWidget' import type { RemoteWidgetConfig } from '@/schemas/nodeDefSchema' @@ -56,7 +61,14 @@ const createMockOptions = (inputOverrides = {}) => ({ }) function mockAxiosResponse(data: unknown, status = 200) { - vi.mocked(axios.get).mockResolvedValueOnce({ data, status }) + const response: AxiosResponse = { + data, + status, + statusText: '', + headers: new AxiosHeaders(), + config: { headers: new AxiosHeaders() } + } + vi.mocked(axios.get).mockResolvedValueOnce(response) } function mockAxiosError(error: Error | string) { @@ -707,6 +719,59 @@ describe('useRemoteWidget', () => { expect(refreshSpy).not.toHaveBeenCalled() }) + it('initializes a remote widget after the same node is removed and re-added', async () => { + const graph = new LGraph() + const node = new LGraphNode('remote') + graph.add(node) + const widget = node.addWidget('combo', 'model', DEFAULT_VALUE, () => {}, { + values: [] + }) + assert(isComboWidget(widget)) + const hook = useRemoteWidget({ + node, + widget, + remoteConfig: createMockConfig(), + defaultValue: DEFAULT_VALUE + }) + + try { + graph.remove(node) + graph.add(node) + mockAxiosResponse(['optionA', 'optionB']) + + await getResolvedValue(hook) + + expect(widget.value).toBe('optionA') + } finally { + graph.remove(node) + } + }) + + it('does not apply a pending response after the owning widget is removed', async () => { + let resolveResponse!: (value: AxiosResponse) => void + const response = new Promise>((resolve) => { + resolveResponse = resolve + }) + vi.mocked(axios.get).mockReturnValueOnce(response) + const options = createMockOptions() + options.widget.value = 'saved' + const cleanup = vi.fn() + options.widget.onRemove = cleanup + const hook = useRemoteWidget(options) + const loaded = getResolvedValue(hook) + options.widget.onRemove() + resolveResponse({ + data: ['replacement'], + status: 200, + statusText: 'OK', + headers: new AxiosHeaders(), + config: { headers: new AxiosHeaders() } + }) + await loaded + expect(options.widget.value).toBe('saved') + expect(cleanup).toHaveBeenCalledOnce() + }) + it('should cleanup event listener on node removal', async () => { let executionSuccessHandler: (() => void) | undefined diff --git a/src/renderer/extensions/vueNodes/widgets/composables/useRemoteWidget.ts b/src/renderer/extensions/vueNodes/widgets/composables/useRemoteWidget.ts index 6a062968fdd..036f6a6bf27 100644 --- a/src/renderer/extensions/vueNodes/widgets/composables/useRemoteWidget.ts +++ b/src/renderer/extensions/vueNodes/widgets/composables/useRemoteWidget.ts @@ -101,6 +101,7 @@ export function useRemoteWidget< const cacheKey = createCacheKey(remoteConfig) let isLoaded = false let refreshQueued = false + let removed = false const setSuccess = (entry: CacheEntry, data: T) => { entry.retryCount = 0 @@ -241,6 +242,7 @@ export function useRemoteWidget< function getValue(onFulfilled?: () => void) { void fetchValue() .then((data) => { + if (removed) return if (isFirstLoad()) onFirstLoad(data) if (refreshQueued && data !== defaultValue) { onRefresh() @@ -299,10 +301,14 @@ export function useRemoteWidget< // Register event listener api.addEventListener('execution_success', handleExecutionSuccess) - // Cleanup on node removal - node.onRemoved = useChainCallback(node.onRemoved, function () { + const cleanup = () => { api.removeEventListener('execution_success', handleExecutionSuccess) + } + widget.onRemove = useChainCallback(widget.onRemove, () => { + removed = true + cleanup() }) + node.onRemoved = useChainCallback(node.onRemoved, cleanup) return autoRefreshWidget } diff --git a/src/renderer/extensions/vueNodes/widgets/registry/widgetRegistry.ts b/src/renderer/extensions/vueNodes/widgets/registry/widgetRegistry.ts index 2fd1b7eea01..9301ee8d4d1 100644 --- a/src/renderer/extensions/vueNodes/widgets/registry/widgetRegistry.ts +++ b/src/renderer/extensions/vueNodes/widgets/registry/widgetRegistry.ts @@ -111,6 +111,16 @@ interface WidgetDefinition { } const coreWidgetDefinitions: Array<[string, WidgetDefinition]> = [ + [ + 'dynamic_group_row', + { + component: defineAsyncComponent( + () => import('../components/WidgetDynamicGroupRow.vue') + ), + aliases: [], + essential: false + } + ], [ 'button', { component: WidgetButton, aliases: ['BUTTON'], essential: false } diff --git a/src/scripts/app.test.ts b/src/scripts/app.test.ts index da1a8116885..c6aecb896ca 100644 --- a/src/scripts/app.test.ts +++ b/src/scripts/app.test.ts @@ -6,8 +6,19 @@ import { useToastStore } from '@/platform/updates/common/toastStore' import { useSettingStore } from '@/platform/settings/settingStore' import { useAuthStore } from '@/stores/authStore' import { useApiKeyAuthStore } from '@/stores/apiKeyAuthStore' -import { beforeEach, describe, expect, it, vi } from 'vitest' -import { ref } from 'vue' +import { + assert, + beforeEach, + describe, + expect, + it, + onTestFinished, + vi +} from 'vitest' +import { markRaw, ref } from 'vue' +import { useLitegraphService } from '@/services/litegraphService' +import { useNodeDefStore } from '@/stores/nodeDefStore' +import { transformInputSpecV1ToV2 } from '@/schemas/nodeDef/migration' vi.mock(import('@vueuse/router'), () => ({ useRouteHash: () => ref('') })) @@ -30,6 +41,7 @@ import type { LoadedComfyWorkflow } from '@/platform/workflow/management/stores/ import { useWorkflowService } from '@/platform/workflow/core/services/workflowService' import type { useWorkflowValidation } from '@/platform/workflow/validation/composables/useWorkflowValidation' import { createMockChangeTracker } from '@/utils/__tests__/litegraphTestUtils' +import { ChangeTracker } from '@/scripts/changeTracker' import { createTestCanvasElement, createTestDragAndScale, @@ -248,6 +260,19 @@ function createWorkflowGraphData(): ComfyWorkflowJSON { } } +function onceForExtensionEvent(event: string, result: () => unknown) { + const mock = mockExtensionService.invokeExtensionsAsync + const previous = mock.getMockImplementation() + let used = false + mock.mockImplementation((name: string, ...args: unknown[]) => { + if (!used && name === event) { + used = true + return result() + } + return previous?.(name, ...args) + }) +} + describe('ComfyApp', () => { let app: ComfyApp let mockCanvas: LGraphCanvas @@ -509,19 +534,33 @@ describe('ComfyApp', () => { app.canvasElRef.value = document.createElement('canvas') Reflect.set(app, 'rootGraphInternal', new LGraph()) const showDialog = vi.spyOn(useDialogStore(), 'showDialog') - mockExtensionService.invokeExtensionsAsync.mockImplementation( - async (hook: string) => { - if (hook === 'beforeConfigureGraph') { - throw new Error('bad extension') - } - } + const error = new Error('bad extension') + let releaseErrorHandling!: () => void + const errorHandling = new Promise((resolve) => { + releaseErrorHandling = resolve + }) + onceForExtensionEvent('beforeConfigureGraph', () => Promise.reject(error)) + onceForExtensionEvent('onGraphLoadError', () => errorHandling) + const load = app.loadGraphData( + createWorkflowGraphData(), + false, + true, + null, + { workflowNavigationId: 9 } ) - - await expect( - app.loadGraphData(createWorkflowGraphData(), false, true, null, { - workflowNavigationId: 9 - }) - ).resolves.toBe(false) + try { + await vi.waitFor(() => + expect( + mockExtensionService.invokeExtensionsAsync + ).toHaveBeenCalledWith('onGraphLoadError', error) + ) + expect(ChangeTracker.isLoadingGraph).toBe(true) + } finally { + releaseErrorHandling() + await load + } + await expect(load).resolves.toBe(false) + expect(ChangeTracker.isLoadingGraph).toBe(false) expect(showDialog).toHaveBeenCalledOnce() expect(useSubgraphNavigationStore().updateHash).toHaveBeenCalledWith( @@ -643,6 +682,337 @@ describe('ComfyApp', () => { expect(closed).toHaveLength(opened.length) }) + it('does not assign a newer workflow graph to an Undo load waiting after configuration', async () => { + app.canvasElRef.value = document.createElement('canvas') + Reflect.set(app, 'rootGraphInternal', new LGraph()) + const firstWorkflow = new ComfyWorkflow({ + path: 'workflows/undo.json', + modified: 0, + size: 0 + }) + const saved = createWorkflowGraphData() + saved.extra = { marker: 'saved' } + const changed = structuredClone(saved) + changed.extra = { marker: 'changed' } + const firstTracker = new ChangeTracker(firstWorkflow, saved) + firstTracker.activeState = changed + firstTracker._restoringState = true + firstWorkflow.changeTracker = firstTracker + const secondWorkflow = new ComfyWorkflow({ + path: 'workflows/other.json', + modified: 0, + size: 0 + }) + const other = createWorkflowGraphData() + other.extra = { marker: 'other' } + const secondTracker = new ChangeTracker(secondWorkflow, other) + secondWorkflow.changeTracker = secondTracker + mockWorkflowService.afterLoadNewGraph.mockImplementation( + async ( + workflow: string | ComfyWorkflow | null, + state: ComfyWorkflowJSON + ) => { + assert(workflow instanceof ComfyWorkflow) + workflow.changeTracker?.reset(state) + } + ) + let releasePending!: () => void + const pending = new Promise((resolve) => { + releasePending = resolve + }) + onceForExtensionEvent('afterConfigureGraph', () => pending) + + const undoLoad = app.loadGraphData(saved, false, false, firstWorkflow, { + skipAssetScans: true + }) + await vi.waitFor(() => + expect(mockExtensionService.invokeExtensionsAsync).toHaveBeenCalledWith( + 'afterConfigureGraph', + expect.anything() + ) + ) + await app.loadGraphData(other, false, false, secondWorkflow, { + skipAssetScans: true + }) + try { + expect(ChangeTracker.isLoadingGraph).toBe(false) + } finally { + releasePending() + await undoLoad + } + await expect(undoLoad).resolves.toBeUndefined() + + expect(firstTracker.activeState).toEqual(changed) + expect(firstTracker.initialState).toEqual(saved) + expect(secondTracker.activeState.extra).toMatchObject({ marker: 'other' }) + expect(app.rootGraph.extra).toMatchObject({ marker: 'other' }) + }) + + it.for(['activation', 'tracker restoration', 'afterLoadGraph'] as const)( + 'cancels an older restore overtaken during %s without replacing the newer workflow', + async (pauseAt) => { + app.canvasElRef.value = document.createElement('canvas') + const graph = new LGraph() + const previousGraph = singletonApp.rootGraphOrUndefined + const previousCanvas = singletonApp.canvas + onTestFinished(() => { + Reflect.set(singletonApp, 'rootGraphInternal', previousGraph) + singletonApp.canvas = previousCanvas + }) + Reflect.set(app, 'rootGraphInternal', graph) + Reflect.set(singletonApp, 'rootGraphInternal', graph) + singletonApp.canvas = mockCanvas + Reflect.set(mockCanvas, 'ds', { scale: 1, offset: [0, 0] }) + await useRealWorkflowService() + const workflowStore = useWorkflowStore() + vi.mocked(workflowStore.openWorkflow).mockRestore() + vi.mocked(workflowStore.isActive).mockRestore() + + const saved = createWorkflowGraphData() + saved.extra = { marker: 'saved' } + const changed = structuredClone(saved) + changed.extra = { marker: 'changed' } + const firstWorkflow = markLoaded( + new ComfyWorkflow({ + path: 'workflows/undo.json', + modified: 0, + size: 0 + }) + ) + const firstTracker = markRaw(new ChangeTracker(firstWorkflow, saved)) + firstTracker.activeState = changed + firstTracker._restoringState = true + firstWorkflow.changeTracker = firstTracker + const other = createWorkflowGraphData() + other.extra = { marker: 'other' } + const secondWorkflow = markLoaded( + new ComfyWorkflow({ + path: 'workflows/other.json', + modified: 0, + size: 0 + }) + ) + const secondTracker = markRaw(new ChangeTracker(secondWorkflow, other)) + secondWorkflow.changeTracker = secondTracker + let releasePause!: () => void + const pause = new Promise((resolve) => { + releasePause = resolve + }) + let reachedPause = false + if (pauseAt === 'activation') { + vi.spyOn(firstWorkflow, 'load').mockImplementationOnce(async () => { + reachedPause = true + await pause + return firstWorkflow + }) + } else if (pauseAt === 'tracker restoration') { + const openWorkflow = vi + .mocked(workflowStore.openWorkflow) + .getMockImplementation() + assert(openWorkflow) + vi.mocked(workflowStore.openWorkflow).mockImplementation( + async (workflow) => { + const loaded = await openWorkflow(workflow) + if (workflow.path === firstWorkflow.path) { + reachedPause = true + await pause + } + return loaded + } + ) + } else { + mockExtensionService.invokeExtensionsAsync.mockImplementation( + async (hook) => { + if (hook === 'afterLoadGraph' && !reachedPause) { + reachedPause = true + await pause + } + } + ) + } + + const olderLoad = app.loadGraphData( + saved, + false, + false, + firstWorkflow, + { + skipAssetScans: true + } + ) + try { + await vi.waitFor(() => expect(reachedPause).toBe(true)) + await app.loadGraphData(other, false, false, secondWorkflow, { + skipAssetScans: true + }) + } finally { + releasePause() + } + const result = await olderLoad + + expect(workflowStore.activeWorkflow?.path).toBe(secondWorkflow.path) + expect(workflowStore.activeWorkflow?.changeTracker).toBe(secondTracker) + expect(secondTracker.activeState.extra).toMatchObject({ + marker: 'other' + }) + expect(secondTracker.initialState.extra).toMatchObject({ + marker: 'other' + }) + expect(firstTracker.initialState).toEqual(saved) + expect(firstTracker.activeState.extra).toMatchObject({ + marker: pauseAt === 'afterLoadGraph' ? 'saved' : 'changed' + }) + expect(graph.extra).toMatchObject({ marker: 'other' }) + expect(ChangeTracker.isLoadingGraph).toBe(false) + expect(result).toBeUndefined() + } + ) + + it('does not assign a cleared canvas to Undo while the newer workflow is still preparing', async () => { + app.canvasElRef.value = document.createElement('canvas') + Reflect.set(app, 'rootGraphInternal', new LGraph()) + const firstWorkflow = new ComfyWorkflow({ + path: 'workflows/undo.json', + modified: 0, + size: 0 + }) + const saved = createWorkflowGraphData() + saved.extra = { marker: 'saved' } + const changed = structuredClone(saved) + changed.extra = { marker: 'changed' } + const tracker = new ChangeTracker(firstWorkflow, saved) + tracker.activeState = changed + tracker._restoringState = true + firstWorkflow.changeTracker = tracker + const secondWorkflow = new ComfyWorkflow({ + path: 'workflows/other.json', + modified: 0, + size: 0 + }) + const other = createWorkflowGraphData() + other.extra = { marker: 'other' } + mockWorkflowService.afterLoadNewGraph.mockImplementation( + async ( + workflow: string | ComfyWorkflow | null, + state: ComfyWorkflowJSON + ) => { + assert(workflow instanceof ComfyWorkflow) + workflow.changeTracker?.reset(state) + } + ) + let releaseAfterConfigure!: () => void + const afterConfigure = new Promise((resolve) => { + releaseAfterConfigure = resolve + }) + onceForExtensionEvent('afterConfigureGraph', () => afterConfigure) + + const undoLoad = app.loadGraphData(saved, false, false, firstWorkflow, { + skipAssetScans: true + }) + await vi.waitFor(() => + expect(mockExtensionService.invokeExtensionsAsync).toHaveBeenCalledWith( + 'afterConfigureGraph', + expect.anything() + ) + ) + let releaseBeforeConfigure!: () => void + const beforeConfigure = new Promise((resolve) => { + releaseBeforeConfigure = resolve + }) + onceForExtensionEvent('beforeConfigureGraph', () => beforeConfigure) + const newerLoad = app.loadGraphData(other, true, false, secondWorkflow, { + skipAssetScans: true + }) + try { + await vi.waitFor(() => expect(app.rootGraph.extra).toEqual({})) + releaseAfterConfigure() + await expect(undoLoad).resolves.toBeUndefined() + expect(ChangeTracker.isLoadingGraph).toBe(true) + expect(mockExtensionService.invokeExtensionsAsync).toHaveBeenCalledWith( + 'onGraphLoadError', + expect.objectContaining({ name: 'AbortError' }) + ) + + expect(tracker.activeState).toEqual(changed) + expect(tracker.initialState).toEqual(saved) + } finally { + releaseAfterConfigure() + releaseBeforeConfigure() + await Promise.all([undoLoad, newerLoad]) + } + expect(app.rootGraph.extra).toMatchObject({ marker: 'other' }) + expect(ChangeTracker.isLoadingGraph).toBe(false) + }) + + it('allows capture after a newer workflow finishes while old resource scans remain pending', async () => { + app.canvasElRef.value = document.createElement('canvas') + Reflect.set(app, 'rootGraphInternal', new LGraph()) + const oldGraph = createWorkflowGraphData() + oldGraph.extra = { marker: 'old' } + const currentGraph = createWorkflowGraphData() + currentGraph.extra = { marker: 'current' } + let releaseModels!: () => void + const modelsPending = new Promise((resolve) => { + releaseModels = resolve + }) + vi.mocked(runMissingModelPipeline).mockImplementationOnce(async () => { + await modelsPending + return { missingModels: [], confirmedCandidates: [] } + }) + const oldLoad = app.loadGraphData(oldGraph, false, false) + try { + await vi.waitFor(() => + expect(runMissingModelPipeline).toHaveBeenCalledOnce() + ) + await app.loadGraphData(currentGraph, false, false, null, { + skipAssetScans: true + }) + + expect(app.rootGraph.extra).toMatchObject({ marker: 'current' }) + expect(ChangeTracker.isLoadingGraph).toBe(false) + } finally { + releaseModels() + await oldLoad + } + }) + + it('keeps a configured load when a newer configure fails without clearing it', async () => { + app.canvasElRef.value = document.createElement('canvas') + Reflect.set(app, 'rootGraphInternal', new LGraph()) + const graph = createWorkflowGraphData() + graph.extra = { marker: 'configured' } + let releaseFirst!: () => void + const firstPending = new Promise((resolve) => { + releaseFirst = resolve + }) + onceForExtensionEvent('afterConfigureGraph', () => firstPending) + const firstLoad = app.loadGraphData(graph, false, false, null, { + skipAssetScans: true + }) + try { + await vi.waitFor(() => + expect( + mockExtensionService.invokeExtensionsAsync + ).toHaveBeenCalledWith('afterConfigureGraph', expect.anything()) + ) + vi.spyOn(app.rootGraph, 'configure').mockImplementationOnce(() => { + throw new Error('Configuration failed before clearing') + }) + await expect( + app.loadGraphData(createWorkflowGraphData(), false, false, null, { + skipAssetScans: true + }) + ).resolves.toBe(false) + releaseFirst() + + expect(await firstLoad).toBeTruthy() + expect(app.rootGraph.extra).toMatchObject({ marker: 'configured' }) + } finally { + releaseFirst() + await firstLoad + } + }) + it('lets an older valid load commit when its newer replacement fails', async () => { app.canvasElRef.value = document.createElement('canvas') Reflect.set(app, 'rootGraphInternal', new LGraph()) @@ -1742,6 +2112,151 @@ describe('ComfyApp', () => { }) describe('workflow lifecycle', () => { + it.for([ + { indices: ['1', '3'], nested: false }, + { indices: ['99', '1', '3', '5', '7', '9'], nested: false }, + { indices: ['1', '3'], nested: true } + ])( + 'restores submitted DynamicGroup rows in encounter order: %j', + async ({ indices, nested }) => { + const graph = new LGraph() + const previousSingletonGraph = singletonApp.rootGraphOrUndefined + onTestFinished(() => { + Reflect.set(singletonApp, 'rootGraphInternal', previousSingletonGraph) + }) + Reflect.set(app, 'rootGraphInternal', graph) + Reflect.set(singletonApp, 'rootGraphInternal', graph) + const nodeType = 'test/ApiDynamicGroup' + const group = { + min: 0, + max: 5, + template: { + required: { + strength: ['FLOAT', { default: 1 }], + '10': ['STRING', { default: '' }] + } + } + } as const + const input: NonNullable = { + required: { loras: ['COMFY_DYNAMICGROUP_V3', group] } + } + const definitionInput: NonNullable = nested + ? { + required: { + mode: [ + 'COMFY_DYNAMICCOMBO_V3', + { + options: [ + { key: 'off', inputs: {} }, + { key: 'on', inputs: input } + ] + } + ] + } + } + : input + const prefix = nested ? 'mode.loras' : 'loras' + class ApiDynamicGroup extends LGraphNode { + constructor() { + super('API DynamicGroup') + this.comfyClass = nodeType + this.serialize_widgets = true + for (const [name, spec] of Object.entries( + definitionInput.required ?? {} + )) { + useLitegraphService().addNodeInput( + this, + transformInputSpecV1ToV2(spec, { name, isOptional: false }) + ) + } + } + } + LiteGraph.registerNodeType(nodeType, ApiDynamicGroup) + const sourceType = 'test/ApiDynamicGroupSource' + class ApiDynamicGroupSource extends LGraphNode { + constructor() { + super('Source') + this.addOutput('strength', 'FLOAT') + } + } + LiteGraph.registerNodeType(sourceType, ApiDynamicGroupSource) + useNodeDefStore().updateNodeDefs([ + { + name: nodeType, + display_name: nodeType, + category: 'test', + python_module: 'test', + description: '', + output: [], + output_node: false, + deprecated: false, + experimental: false, + input: definitionInput + } + ]) + const inputs = Object.fromEntries( + indices.map((index, position) => [ + `${prefix}.${index}.strength`, + position + 0.5 + ]) + ) + try { + await app.loadApiJson( + { + '1': { + class_type: nodeType, + inputs: { + ...inputs, + [`${prefix}.${indices.at(-1)}.strength`]: ['2', 0], + [`${prefix}.10`]: 'malformed', + [prefix]: 0, + [`${prefix}.$add`]: 0, + [`${prefix}.$notice`]: 'not an input', + [`${prefix}.0`]: 0, + ...(nested ? { mode: 'on' } : {}) + }, + _meta: { title: nodeType } + }, + '2': { + class_type: sourceType, + inputs: {}, + _meta: { title: sourceType } + } + }, + '' + ) + const node = graph.getNodeById(toNodeId(1)) + expect( + node?.widgets?.filter((w) => w.type === 'dynamic_group_row') + ).toHaveLength(indices.length) + expect( + node?.widgets + ?.filter((w) => w.name.endsWith('.strength')) + .map((w) => [w.name, w.value]) + ).toEqual( + indices.map((_, position) => [ + `${prefix}.${position}.strength`, + position === indices.length - 1 ? 1 : position + 0.5 + ]) + ) + expect( + node?.widgets + ?.filter((w) => w.name.endsWith('.10')) + .map((w) => w.value) + ).toEqual(indices.map(() => '')) + const slot = node?.findInputSlot( + `${prefix}.${indices.length - 1}.strength` + ) + const link = slot === undefined ? undefined : node?.getInputLink(slot) + expect(link?.origin_id).toBe(toNodeId(2)) + expect(link?.origin_slot).toBe(0) + } finally { + LiteGraph.unregisterNodeType(nodeType) + LiteGraph.unregisterNodeType(sourceType) + } + } + ) + it('clears missing node packs before loading API JSON without missing nodes', async () => { const graph = new LGraph() const activeSubgraph = createTestSubgraph({ rootGraph: graph }) diff --git a/src/scripts/app.ts b/src/scripts/app.ts index f03dcf0a9aa..17d4ceea4c9 100644 --- a/src/scripts/app.ts +++ b/src/scripts/app.ts @@ -13,6 +13,7 @@ import { useCanvasScheduler } from '@/renderer/core/canvas/useCanvasScheduler' import { promotedInputSource } from '@/core/graph/subgraph/promotedInputWidget' import { resolveConcretePromotedWidget } from '@/core/graph/subgraph/resolveConcretePromotedWidget' +import { resolveDynamicInputSpec } from '@/core/graph/widgets/dynamicInputSpec' import { setBackendNodeText, st, t } from '@/i18n' import { normalizeI18nKey } from '@/utils/formatUtil' import { ChangeTracker } from '@/scripts/changeTracker' @@ -54,6 +55,7 @@ import type { } from '@/platform/telemetry/types' import { useToastStore } from '@/platform/updates/common/toastStore' import { MIME_ASSET_INFO } from '@/platform/assets/schemas/mediaAssetSchema' +import { restoreDynamicGroupInputs } from '@/platform/workflow/core/utils/restoreDynamicGroupInputs' import { updatePendingWarnings } from '@/platform/workflow/core/utils/pendingWarnings' import { useWorkflowService } from '@/platform/workflow/core/services/workflowService' import { @@ -399,6 +401,7 @@ export class ComfyApp { private configuringGraphLevel: number = 0 private graphLoadSequence = 0 private committedGraphLoadSequence = 0 + private readonly pendingGraphLoads = new Set() private pendingCamera: | { id: number; workflow: string | null | ComfyWorkflow } | undefined @@ -1342,6 +1345,13 @@ export class ComfyApp { useWorkflowService().beforeLoadNewGraph(clean) await useExtensionService().invokeExtensionsAsync('beforeLoadGraph') + this.pendingGraphLoads.add(loadId) + ChangeTracker.isLoadingGraph = true + const releaseGraphLoad = () => { + this.pendingGraphLoads.delete(loadId) + if (this.pendingGraphLoads.size === 0) + ChangeTracker.isLoadingGraph = false + } let reset_invalid_values = false const missingNodeTypes: MissingNodeType[] = [] try { @@ -1494,7 +1504,11 @@ export class ComfyApp { // below. Left unhandled, that would both reject silently and leak any // suppression/loading-state a `beforeLoadGraph` listener opened for // this load, since nothing ever notifies it the load ended. - await this.reportGraphLoadFailure(error) + try { + await this.reportGraphLoadFailure(error) + } finally { + releaseGraphLoad() + } void useSubgraphNavigationStore().updateHash( 'workflow-load', workflowNavigationId @@ -1536,7 +1550,6 @@ export class ComfyApp { } } - ChangeTracker.isLoadingGraph = true let activatedWorkflow: LoadedComfyWorkflow | undefined let reconcileResourceErrors: (() => void) | undefined let resourceScanLoadCompleted = false @@ -1611,6 +1624,16 @@ export class ComfyApp { // Resolves rather than throws: the close/replacement guards read this outcome. return false } + const configuredGraphState = this.rootGraph.state + const configuredLoadId = this.committedGraphLoadSequence + const isCurrentGraphLoad = () => + configuredGraphState === this.rootGraph.state && + configuredLoadId === this.committedGraphLoadSequence + const cancelReplacedLoad = () => + useExtensionService().invokeExtensionsAsync( + 'onGraphLoadError', + new DOMException('Workflow load was replaced', 'AbortError') + ) const snapTo = LiteGraph.alwaysSnapToGrid ? this.rootGraph.getSnapToGridSize() : 0 @@ -1675,6 +1698,10 @@ export class ComfyApp { 'afterConfigureGraph', missingNodeTypes ) + if (!isCurrentGraphLoad()) { + await cancelReplacedLoad() + return undefined + } const effectiveShareId = shareId ?? @@ -1690,12 +1717,29 @@ export class ComfyApp { } useTelemetry()?.trackWorkflowOpened(telemetryPayload) useTelemetry()?.trackWorkflowImported(telemetryPayload) - await useWorkflowService().afterLoadNewGraph( - workflow, - this.rootGraph.serialize() as unknown as ComfyWorkflowJSON, - effectiveShareId - ) + try { + await useWorkflowService().afterLoadNewGraph( + workflow, + this.rootGraph.serialize() as unknown as ComfyWorkflowJSON, + effectiveShareId + ) + } catch (error) { + if (isCurrentGraphLoad()) throw error + } + if (!isCurrentGraphLoad()) { + await cancelReplacedLoad() + return undefined + } await useExtensionService().invokeExtensionsAsync('afterLoadGraph') + if (!isCurrentGraphLoad()) { + await cancelReplacedLoad() + return undefined + } + for (const pendingLoadId of this.pendingGraphLoads) { + if (pendingLoadId <= loadId) + this.pendingGraphLoads.delete(pendingLoadId) + } + ChangeTracker.isLoadingGraph = this.pendingGraphLoads.size > 0 // Capture the workflow this load activated before the asset-scan awaits // below can hand control back and let the user switch to another one. activatedWorkflow = useWorkflowStore().activeWorkflow ?? undefined @@ -1763,7 +1807,7 @@ export class ComfyApp { 'workflow-load', workflowNavigationId ) - ChangeTracker.isLoadingGraph = false + releaseGraphLoad() // The retirement watcher skips transitions made during the load. useExecutionErrorStore().retireResolvedMissingNodePromptError() reconcileResourceErrors?.() @@ -2560,9 +2604,10 @@ export class ComfyApp { const node = app.rootGraph.getNodeById(currentNodeId) if (!node) return const targetNode = node + const inputs = restoreDynamicGroupInputs(node, data.inputs) - for (const input in data.inputs) { - const value = data.inputs[input] + for (const input in inputs) { + const value = inputs[input] if (value instanceof Array) { function connectInput() { const [fromId, fromSlot] = value @@ -2701,23 +2746,16 @@ export class ComfyApp { const nodeInputs = def.input for (const widget of node.widgets) { if (widget.type === 'combo') { - let inputType: 'required' | 'optional' | undefined - if (nodeInputs.required?.[widget.name] !== undefined) { - inputType = 'required' - } else if (nodeInputs.optional?.[widget.name] !== undefined) { - inputType = 'optional' - } - if (inputType !== undefined) { - // Get the input spec associated with the widget - const inputSpec = nodeInputs[inputType]?.[widget.name] - if (inputSpec) { - // Refresh the combo widget's options with the values from the input spec - if (isComboInputSpecV2(inputSpec)) { - widget.options.values = inputSpec[1]?.options - } else if (isComboInputSpecV1(inputSpec)) { - widget.options.values = inputSpec[0] - } - } + const resolved = resolveDynamicInputSpec( + nodeInputs, + widget.name, + (name) => node.widgets?.find((item) => item.name === name)?.value + ) + const inputSpec = resolved?.spec + if (inputSpec && isComboInputSpecV2(inputSpec)) { + widget.options.values = inputSpec[1]?.options + } else if (inputSpec && isComboInputSpecV1(inputSpec)) { + widget.options.values = inputSpec[0] } } } diff --git a/src/scripts/changeTracker.test.ts b/src/scripts/changeTracker.test.ts index 6c1bc211b83..5c48f010724 100644 --- a/src/scripts/changeTracker.test.ts +++ b/src/scripts/changeTracker.test.ts @@ -2,7 +2,15 @@ import { fromPartial } from '@total-typescript/shoehorn' import { useWorkflowStore } from '@/platform/workflow/management/stores/workflowStore' import { useSubgraphNavigationStore } from '@/stores/subgraphNavigationStore' import { useNodeOutputStore } from '@/stores/nodeOutputStore' -import { beforeEach, describe, expect, it, onTestFinished, vi } from 'vitest' +import { + assert, + beforeEach, + describe, + expect, + it, + onTestFinished, + vi +} from 'vitest' import { markRaw, ref } from 'vue' vi.mock(import('@vueuse/router'), () => ({ useRouteHash: () => ref('') })) @@ -219,6 +227,12 @@ describe('ChangeTracker', () => { () => {} ) app.rootGraph.subgraphs.clear() + vi.mocked(app.loadGraphData).mockImplementation(async (state) => { + const restored = await requireValidWorkflow(state) + useWorkflowStore().activeWorkflow?.changeTracker.reset(restored) + mockCanvasState(restored) + return true + }) }) describe('undoRedo', () => { @@ -1306,6 +1320,535 @@ describe('ChangeTracker', () => { }) describe('undo and redo', () => { + it('keeps Redo after Undo normalizes an interior node size', async () => { + const initial = await createSubgraphState() + const changed = structuredClone(initial) + getSubgraphDefinition(changed).nodes[0].widgets_values = [2] + const normalized = structuredClone(initial) + getSubgraphDefinition(normalized).nodes[0].size = [100, 178] + const tracker = createTracker(changed) + tracker.undoQueue.push(initial) + vi.mocked(app.loadGraphData).mockImplementationOnce(async () => { + tracker.reset(normalized) + mockCanvasState(normalized) + return true + }) + + await tracker.undo() + tracker.captureCanvasState() + + expect(tracker.undoQueue).toEqual([]) + expect(tracker.redoQueue).toEqual([changed]) + expect(tracker.activeState).toEqual(normalized) + await tracker.redo() + expect(tracker.activeState).toEqual(changed) + expect(tracker.undoQueue).toEqual([normalized]) + expect(tracker.redoQueue).toEqual([]) + }) + + it('clears Redo for an intentional resize after normalized Undo', async () => { + const initial = createState(1) + const changed = structuredClone(initial) + changed.nodes[0].widgets_values = [2] + const normalized = structuredClone(initial) + normalized.nodes[0].size = [100, 178] + const tracker = createTracker(changed) + tracker.undoQueue.push(initial) + vi.mocked(app.loadGraphData).mockImplementationOnce(async () => { + tracker.reset(normalized) + mockCanvasState(normalized) + return true + }) + await tracker.undo() + const resized = structuredClone(normalized) + resized.nodes[0].size = [350, 220] + mockCanvasState(resized) + + tracker.captureCanvasState() + + expect(tracker.redoQueue).toEqual([]) + expect(tracker.undoQueue).toEqual([normalized]) + expect(tracker.activeState).toEqual(resized) + await tracker.undo() + expect(tracker.activeState).toEqual(normalized) + await tracker.redo() + expect(tracker.activeState).toEqual(resized) + }) + + it.for(['undo', 'redo'] as const)( + 'keeps the restored workflow snapshot when another graph is configured during %s', + async (direction) => { + const restored = createState(1) + const changed = createState(2) + const normalized = structuredClone(restored) + normalized.nodes[0].size = [100, 178] + const tracker = createTracker(changed) + const history = { undo: tracker.undoQueue, redo: tracker.redoQueue } + history[direction].push(restored) + vi.mocked(app.loadGraphData).mockImplementationOnce(async () => { + tracker.reset(normalized) + mockCanvasState(createState(3)) + return true + }) + + await tracker[direction]() + + expect(tracker.activeState).toEqual(normalized) + expect(tracker.initialState).toEqual(changed) + expect(history[direction]).toEqual([]) + } + ) + + it('keeps the normalized snapshot if another workflow becomes active during Undo', async () => { + const initial = createState(1) + const tracker = createTracker(createState(2)) + tracker.undoQueue.push(initial) + const normalized = structuredClone(initial) + normalized.nodes[0].size = [100, 178] + const otherState = createState(3) + vi.mocked(app.loadGraphData).mockImplementationOnce(async () => { + tracker.reset(normalized) + createTracker(otherState) + mockCanvasState(otherState) + return true + }) + + await tracker.undo() + + expect(tracker.activeState).toEqual(normalized) + expect( + useWorkflowStore().activeWorkflow?.changeTracker.activeState + ).toEqual(otherState) + }) + + it('keeps Redo reachable when a load rejects after committing the normalized Undo state', async () => { + const saved = createState(1) + saved.nodes[0].widgets_values = [1] + const changed = structuredClone(saved) + changed.nodes[0].widgets_values = [2] + const normalized = structuredClone(saved) + normalized.nodes[0].size = [100, 178] + const tracker = createTracker(saved) + tracker.activeState = changed + tracker.undoQueue.push(saved) + const workflow = useWorkflowStore().activeWorkflow + assert.exists(workflow) + workflow.isModified = true + vi.mocked(useWorkflowStore().getWorkflowByPath).mockReturnValue(workflow) + const error = new Error('Post-load asset scan rejected') + vi.mocked(app.loadGraphData).mockImplementationOnce(async () => { + tracker.reset(normalized) + mockCanvasState(normalized) + throw error + }) + + await expect(tracker.undo()).rejects.toThrow(error) + + expect(tracker.activeState).toEqual(normalized) + expect(tracker.initialState).toEqual(normalized) + expect(tracker.undoQueue).toEqual([]) + expect(tracker.redoQueue).toEqual([changed]) + expect(workflow.isModified).toBe(false) + expect(tracker._restoringState).toBe(false) + await tracker.redo() + expect(tracker.activeState).toEqual(changed) + expect(workflow.isModified).toBe(true) + }) + + it('retains the Undo target when a same-workflow reload updates the tracker instead', async () => { + const saved = createState(1) + saved.nodes[0].widgets_values = [1] + const current = structuredClone(saved) + current.nodes[0].widgets_values = [2] + const reloaded = structuredClone(current) + reloaded.nodes[0].size = [100, 178] + const tracker = createTracker(saved) + tracker.activeState = current + tracker.undoQueue.push(saved) + const workflow = useWorkflowStore().activeWorkflow + assert.exists(workflow) + workflow.isModified = true + vi.mocked(useWorkflowStore().getWorkflowByPath).mockReturnValue(workflow) + vi.mocked(app.loadGraphData).mockImplementationOnce(async () => { + tracker.reset(reloaded) + mockCanvasState(reloaded) + return undefined + }) + + await tracker.undo() + + expect(tracker.activeState).toEqual(reloaded) + expect(tracker.initialState).toEqual(saved) + expect(tracker.undoQueue).toEqual([saved]) + expect(tracker.redoQueue).toEqual([]) + expect(workflow.isModified).toBe(true) + expect(tracker._restoringState).toBe(false) + }) + + it('keeps Redo when a late load error follows normalization of an interior node size', async () => { + const saved = await createSubgraphState() + const changed = structuredClone(saved) + getSubgraphDefinition(changed).nodes[0].widgets_values = [2] + const normalized = structuredClone(saved) + getSubgraphDefinition(normalized).nodes[0].size = [100, 178] + const tracker = createTracker(saved) + tracker.activeState = changed + tracker.undoQueue.push(saved) + const error = new Error('Post-load asset scan rejected') + vi.mocked(app.loadGraphData).mockImplementationOnce(async () => { + tracker.reset(normalized) + mockCanvasState(normalized) + throw error + }) + + await expect(tracker.undo()).rejects.toThrow(error) + + expect(tracker.activeState).toEqual(normalized) + expect(tracker.initialState).toEqual(normalized) + expect(tracker.undoQueue).toEqual([]) + expect(tracker.redoQueue).toEqual([changed]) + expect(tracker._restoringState).toBe(false) + }) + + it('retains the Undo target when an unrelated same-workflow reload precedes a late error', async () => { + const saved = createState(1) + saved.nodes[0].widgets_values = [1] + const current = structuredClone(saved) + current.nodes[0].widgets_values = [2] + const reloaded = structuredClone(current) + reloaded.nodes[0].size = [100, 178] + const tracker = createTracker(saved) + tracker.activeState = current + tracker.undoQueue.push(saved) + const workflow = useWorkflowStore().activeWorkflow + assert.exists(workflow) + workflow.isModified = true + vi.mocked(useWorkflowStore().getWorkflowByPath).mockReturnValue(workflow) + const error = new Error('Superseded Undo asset scan rejected') + vi.mocked(app.loadGraphData).mockImplementationOnce(async () => { + tracker.reset(reloaded) + mockCanvasState(reloaded) + throw error + }) + + await expect(tracker.undo()).rejects.toThrow(error) + + expect(tracker.activeState).toEqual(reloaded) + expect(tracker.initialState).toEqual(saved) + expect(tracker.undoQueue).toEqual([saved]) + expect(tracker.redoQueue).toEqual([]) + expect(workflow.isModified).toBe(true) + expect(tracker._restoringState).toBe(false) + }) + + it('retains a resize Undo target when an unrelated reload changes only the node size', async () => { + const saved = createState(1) + const current = structuredClone(saved) + current.nodes[0].size = [400, 220] + const reloaded = structuredClone(current) + reloaded.nodes[0].size = [400, 260] + const tracker = createTracker(saved) + tracker.activeState = current + tracker.undoQueue.push(saved) + vi.mocked(app.loadGraphData).mockImplementationOnce(async () => { + tracker.reset(reloaded) + mockCanvasState(reloaded) + return undefined + }) + + await tracker.undo() + + expect(tracker.activeState).toEqual(reloaded) + expect(tracker.initialState).toEqual(saved) + expect(tracker.undoQueue).toEqual([saved]) + expect(tracker.redoQueue).toEqual([]) + expect(tracker._restoringState).toBe(false) + }) + + it.for([ + { direction: 'undo', reverse: 'redo', start: 3, end: 1 }, + { direction: 'redo', reverse: 'undo', start: 1, end: 3 } + ] as const)( + 'applies both overlapping $direction requests', + async ({ direction, reverse, start, end }) => { + vi.useFakeTimers() + onTestFinished(() => { + vi.useRealTimers() + }) + const current = createState(start) + const intermediate = createState(2) + const final = createState(end) + const tracker = createTracker(current) + const history = { undo: tracker.undoQueue, redo: tracker.redoQueue } + history[direction].push(final, intermediate) + const loadGraphData = vi + .mocked(app.loadGraphData) + .getMockImplementation() + assert.exists(loadGraphData) + vi.mocked(app.loadGraphData).mockImplementationOnce(async (...args) => { + await new Promise((resolve) => setTimeout(resolve, 5)) + return loadGraphData(...args) + }) + + const requests = Promise.all([ + tracker[direction](), + tracker[direction]() + ]) + await vi.advanceTimersByTimeAsync(5) + await requests + + expect(tracker.activeState).toEqual(final) + expect(history[direction]).toEqual([]) + expect(history[reverse]).toEqual([current, intermediate]) + expect(tracker._restoringState).toBe(false) + } + ) + + it.for([ + { first: 'undo', second: 'redo' }, + { first: 'redo', second: 'undo' } + ] as const)( + 'applies overlapping $first then $second requests in order', + async ({ first, second }) => { + vi.useFakeTimers() + onTestFinished(() => { + vi.useRealTimers() + }) + const current = createState(3) + const previous = createState(2) + const tracker = createTracker(current) + const history = { undo: tracker.undoQueue, redo: tracker.redoQueue } + history[first].push(previous) + const loadGraphData = vi + .mocked(app.loadGraphData) + .getMockImplementation() + assert.exists(loadGraphData) + vi.mocked(app.loadGraphData).mockImplementationOnce(async (...args) => { + await new Promise((resolve) => setTimeout(resolve, 5)) + return loadGraphData(...args) + }) + + const requests = Promise.all([tracker[first](), tracker[second]()]) + await vi.advanceTimersByTimeAsync(5) + await requests + + expect(tracker.activeState).toEqual(current) + expect(history[first]).toEqual([previous]) + expect(history[second]).toEqual([]) + expect(tracker._restoringState).toBe(false) + } + ) + + it.for([false, undefined])( + 'processes a queued Undo after the pending restore returns %s', + async (result) => { + vi.useFakeTimers() + onTestFinished(() => { + vi.useRealTimers() + }) + const earlier = createState(1) + const previous = createState(2) + const current = createState(3) + const tracker = createTracker(current) + tracker.undoQueue.push(earlier, previous) + vi.mocked(app.loadGraphData).mockImplementationOnce( + () => new Promise((resolve) => setTimeout(() => resolve(result), 5)) + ) + + const requests = Promise.all([tracker.undo(), tracker.undo()]) + await vi.advanceTimersByTimeAsync(0) + expect(tracker.activeState).toEqual(current) + expect(tracker.undoQueue).toEqual([earlier]) + expect(tracker.redoQueue).toEqual([]) + await vi.advanceTimersByTimeAsync(5) + await requests + + expect(tracker.activeState).toEqual(previous) + expect(tracker.undoQueue).toEqual([earlier]) + expect(tracker.redoQueue).toEqual([current]) + expect(tracker._restoringState).toBe(false) + } + ) + + it.for([ + { direction: 'undo', reverse: 'redo' }, + { direction: 'redo', reverse: 'undo' } + ] as const)( + 'processes a queued $direction after the pending restore rejects', + async ({ direction, reverse }) => { + const current = createState(3) + const previous = createState(2) + const tracker = createTracker(current) + const history = { undo: tracker.undoQueue, redo: tracker.redoQueue } + history[direction].push(previous) + const error = new Error('Workflow load rejected') + vi.mocked(app.loadGraphData).mockRejectedValueOnce(error) + + const results = await Promise.allSettled([ + tracker[direction](), + tracker[direction]() + ]) + + expect(results).toEqual([ + { status: 'rejected', reason: error }, + { status: 'fulfilled', value: undefined } + ]) + expect(tracker.activeState).toEqual(previous) + expect(history[direction]).toEqual([]) + expect(history[reverse]).toEqual([current]) + expect(tracker._restoringState).toBe(false) + } + ) + + it('serializes distinct workflow requests after a pending Undo', async () => { + vi.useFakeTimers() + onTestFinished(() => { + vi.useRealTimers() + }) + const earlier = createState(1) + const previous = createState(2) + const current = createState(3) + const firstSelection = createState(4) + const secondSelection = createState(5) + const firstSource = [firstSelection] + const secondSource = [secondSelection] + const tracker = createTracker(current) + tracker.undoQueue.push(earlier, previous) + let releasePending!: () => void + const pending = new Promise((resolve) => { + releasePending = resolve + }) + vi.mocked(app.loadGraphData).mockImplementationOnce(async () => { + await pending + tracker.reset(previous) + mockCanvasState(previous) + return true + }) + + const undo = tracker.undo() + let selectionFinished = false + const firstRequest = tracker + .updateState(firstSource, tracker.undoQueue) + .then(() => { + selectionFinished = true + }) + const secondRequest = tracker.updateState(secondSource, tracker.undoQueue) + try { + await vi.advanceTimersByTimeAsync(0) + expect(selectionFinished).toBe(false) + } finally { + releasePending() + await Promise.all([undo, firstRequest, secondRequest]) + } + + expect(tracker.activeState).toEqual(secondSelection) + expect(tracker.undoQueue).toEqual([earlier, previous, firstSelection]) + expect(tracker.redoQueue).toEqual([current]) + expect(firstSource).toEqual([]) + expect(secondSource).toEqual([]) + }) + + it.for([false, undefined])( + 'preserves state and history when restoration returns %s', + async (result) => { + const initial = createState(1) + const current = createState(2) + const tracker = createTracker(current) + tracker.undoQueue.push(initial) + vi.mocked(app.loadGraphData).mockImplementationOnce(async () => { + expect(tracker.activeState).toEqual(current) + mockCanvasState(createState(3)) + return result + }) + + await tracker.undo() + + expect(tracker.activeState).toEqual(current) + expect(tracker.undoQueue).toEqual([initial]) + expect(tracker.redoQueue).toEqual([]) + expect(api.dispatchCustomEvent).not.toHaveBeenCalled() + expect(tracker._restoringState).toBe(false) + } + ) + + it('preserves state and history when restoration rejects', async () => { + const initial = createState(1) + const current = createState(2) + const tracker = createTracker(current) + tracker.undoQueue.push(initial) + const error = new Error('Workflow load rejected') + vi.mocked(app.loadGraphData).mockRejectedValueOnce(error) + + await expect(tracker.undo()).rejects.toThrow(error) + + expect(tracker.activeState).toEqual(current) + expect(tracker.undoQueue).toEqual([initial]) + expect(tracker.redoQueue).toEqual([]) + expect(api.dispatchCustomEvent).not.toHaveBeenCalled() + expect(tracker._restoringState).toBe(false) + }) + + it.for([ + { + direction: 'undo', + reverse: 'redo', + restoresSaved: true, + restoredValue: 1, + modified: false + }, + { + direction: 'redo', + reverse: 'undo', + restoresSaved: true, + restoredValue: 1, + modified: false + }, + { + direction: 'undo', + reverse: 'redo', + restoresSaved: false, + restoredValue: 3, + modified: true + } + ] as const)( + 'tracks unsaved changes after $direction normalizes a node (saved target: $restoresSaved)', + async ({ direction, reverse, restoredValue, modified }) => { + const saved = createState(1) + saved.nodes[0].widgets_values = [1] + const current = structuredClone(saved) + current.nodes[0].widgets_values = [2] + const restored = structuredClone(saved) + restored.nodes[0].widgets_values = [restoredValue] + const normalized = structuredClone(restored) + normalized.nodes[0].size = [100, 178] + const tracker = createTracker(saved) + tracker.activeState = current + const workflow = useWorkflowStore().activeWorkflow + assert.exists(workflow) + vi.mocked(useWorkflowStore().getWorkflowByPath).mockReturnValue( + workflow + ) + const history = { undo: tracker.undoQueue, redo: tracker.redoQueue } + history[direction].push(restored) + vi.mocked(app.loadGraphData).mockImplementationOnce(async () => { + tracker.reset(normalized) + mockCanvasState(normalized) + return true + }) + + await tracker[direction]() + tracker.captureCanvasState() + + expect(workflow.isModified).toBe(modified) + expect(tracker.activeState).toEqual(normalized) + expect(history[direction]).toEqual([]) + expect(history[reverse]).toEqual([current]) + await tracker[reverse]() + expect(tracker.activeState).toEqual(current) + expect(workflow.isModified).toBe(true) + } + ) + it('dispatches autoQueueGraphChanged for a data change in both directions', async () => { const initial = createState(1) const changed = structuredClone(initial) diff --git a/src/scripts/changeTracker.ts b/src/scripts/changeTracker.ts index b2814b7a42c..2b75624acc8 100644 --- a/src/scripts/changeTracker.ts +++ b/src/scripts/changeTracker.ts @@ -8,6 +8,7 @@ import { LGraphCanvas, LiteGraph } from '@/lib/litegraph/src/litegraph' import type { ComfyWorkflow } from '@/platform/workflow/management/stores/workflowStore' import { useWorkflowStore } from '@/platform/workflow/management/stores/workflowStore' import type { ComfyWorkflowJSON } from '@/platform/workflow/validation/schemas/workflowSchema' +import { collectSubgraphDefinitions } from '@/platform/workflow/core/utils/workflowFlattening' import type { ExecutedWsMessage } from '@/platform/remote/comfyui/execution/types' import { useDialogStore } from '@/stores/dialogStore' import { useExecutionStore } from '@/stores/executionStore' @@ -30,6 +31,40 @@ function withoutExecutionOrder(nodes: ComfyWorkflowJSON['nodes']) { return nodes.map((node) => _.omit(node, ['order'])) } +function withNormalizedNodeSizes(state: ComfyWorkflowJSON): ComfyWorkflowJSON { + const comparable = clone(state) + const subgraphs = collectSubgraphDefinitions( + comparable.definitions?.subgraphs ?? [] + ) + const nodes = [ + ...comparable.nodes, + ...subgraphs.flatMap((subgraph) => subgraph.nodes) + ] + for (const node of nodes) { + if ('size' in node) node.size = [0, 0] + } + return comparable +} + +function matchesRestoredTarget( + actual: ComfyWorkflowJSON, + target: ComfyWorkflowJSON, + previous: ComfyWorkflowJSON +): boolean { + if (ChangeTracker.graphEqual(actual, target)) return true + const comparableTarget = withNormalizedNodeSizes(target) + return ( + ChangeTracker.graphEqual( + withNormalizedNodeSizes(actual), + comparableTarget + ) && + !ChangeTracker.graphEqual( + comparableTarget, + withNormalizedNodeSizes(previous) + ) + ) +} + function isActiveTracker(tracker: ChangeTracker): boolean { return useWorkflowStore().activeWorkflow?.changeTracker === tracker } @@ -271,6 +306,7 @@ export class ChangeTracker { * Whether the redo/undo restoring is in progress. */ _restoringState: boolean = false + private restoreChain?: Promise ds?: { scale: number; offset: [number, number] } nodeOutputs?: Partial> @@ -296,10 +332,9 @@ export class ChangeTracker { * Save the current state as the initial state. */ reset(state?: ComfyWorkflowJSON) { - // Do not reset the state if we are restoring. + if (state) this.activeState = clone(state) if (this._restoringState) return - if (state) this.activeState = clone(state) this.initialState = clone(this.activeState) } @@ -469,22 +504,49 @@ export class ChangeTracker { } async updateState(source: ComfyWorkflowJSON[], target: ComfyWorkflowJSON[]) { - const prevState = source.pop() - if (prevState) { - const previousState = this.activeState - target.push(previousState) - this._restoringState = true - try { - await app.loadGraphData(prevState, false, false, this.workflow, { - checkForRerouteMigration: false, - silentAssetErrors: true - }) - this.activeState = prevState - this.updateModified(previousState) - } finally { - this._restoringState = false + const restore = async () => { + if (this._restoringState || source.length === 0) return + const prevState = source.pop() + if (prevState) { + const previousState = this.activeState + const restoresSavedState = ChangeTracker.graphEqual( + prevState, + this.initialState + ) + let restored = false + this._restoringState = true + try { + const result = await app.loadGraphData( + prevState, + false, + false, + this.workflow, + { + checkForRerouteMigration: false, + silentAssetErrors: true + } + ) + restored = result !== false && result !== undefined + } finally { + this._restoringState = false + restored ||= + !ChangeTracker.graphEqual(this.activeState, previousState) && + matchesRestoredTarget(this.activeState, prevState, previousState) + if (restored) { + target.push(previousState) + if (restoresSavedState) this.initialState = clone(this.activeState) + } else { + source.push(prevState) + } + if (restored) this.updateModified(previousState) + } } } + this.restoreChain = (this.restoreChain ?? Promise.resolve()).then( + restore, + restore + ) + return this.restoreChain } async undo() { diff --git a/src/services/litegraphService.ts b/src/services/litegraphService.ts index ba7491d2b11..2972a768564 100644 --- a/src/services/litegraphService.ts +++ b/src/services/litegraphService.ts @@ -53,7 +53,14 @@ import type { InputSpec, OutputSpec } from '@/schemas/nodeDef/nodeDefSchemaV2' -import type { ComfyNodeDef as ComfyNodeDefV1 } from '@/schemas/nodeDefSchema' +import type { + ComfyNodeDef as ComfyNodeDefV1, + InputSpec as InputSpecV1 +} from '@/schemas/nodeDefSchema' +import { + getInputSpecType, + zDynamicGroupInputSpec +} from '@/schemas/nodeDefSchema' import { ComfyApp, app } from '@/scripts/app' import { $el } from '@/scripts/ui' import { useExecutionStore } from '@/stores/executionStore' @@ -278,6 +285,19 @@ export const useLitegraphService = () => { addInputWidget(node, inputSpec, { dynamic: true }) } + function validateDynamicGroupWidgets(inputData: InputSpecV1, name: string) { + const { template } = zDynamicGroupInputSpec.parse(inputData)[1] + const fields = { ...template.required, ...template.optional } + for (const [field, spec] of Object.entries(fields)) { + if ( + !widgetStore.widgets.has(spec[1]?.widgetType ?? getInputSpecType(spec)) + ) + throw new TypeError( + `DynamicGroup field '${name}.${field}' requires a registered widget` + ) + } + } + /** * @internal Add a widget to the node. For both primitive types and custom widgets * (unless `socketless`), an input socket is also added. @@ -303,13 +323,11 @@ export const useLitegraphService = () => { const widgetConstructor = widgetStore.widgets.get(widgetInputSpec.type) if (!widgetConstructor || inputSpec.forceInput) return + const inputData = transformInputSpecV2ToV1(widgetInputSpec) + if (widgetInputSpec.type === 'COMFY_DYNAMICGROUP_V3') + validateDynamicGroupWidgets(inputData, inputName) const widgetsBefore = new Set(node.widgets ?? []) - const result = widgetConstructor( - node, - inputName, - transformInputSpecV2ToV1(widgetInputSpec), - app - ) + const result = widgetConstructor(node, inputName, inputData, app) const wrappedResult = result && !('type' in result) ? result : undefined const { minWidth = 1, minHeight = 1 } = wrappedResult ?? {} const returnedWidget = result && 'type' in result ? result : result?.widget diff --git a/src/stores/modelToNodeStore.test.ts b/src/stores/modelToNodeStore.test.ts index 2bf26241c59..5263e40555c 100644 --- a/src/stores/modelToNodeStore.test.ts +++ b/src/stores/modelToNodeStore.test.ts @@ -45,6 +45,8 @@ const MOCK_NODE_NAMES = [ 'ImageOnlyCheckpointLoader', 'LoraLoader', 'LoraLoaderModelOnly', + 'LoadLoraModel', + 'LoadLoraTextEncoder', 'VAELoader', 'ControlNetLoader', 'UNETLoader', @@ -250,6 +252,51 @@ describe('useModelToNodeStore', () => { ) }) + it('does not treat an auto-loading provider as an asset widget', () => { + expect(useModelToNodeStore().isModelWidget('FL_ChatterboxVC', '')).toBe( + false + ) + }) + + it('supports repeated model fields registered outside the default mappings', () => { + const store = useModelToNodeStore() + store.registerNodeProvider( + 'vae', + new ModelNodeProvider(createMockNodeDef('MultiVAE'), /^items\.\d+\.file$/) + ) + + expect(store.isModelWidget('MultiVAE', 'items.3.file')).toBe(true) + expect(store.isModelWidget('MultiVAE', 'items.3.strength')).toBe(false) + expect(store.getCategoryForNodeType('MultiVAE')).toBe('vae') + }) + + it('matches string selectors literally, including regex characters', () => { + const store = useModelToNodeStore() + store.quickRegister('vae', 'VAELoader', 'items.*.file') + + expect(store.isModelWidget('VAELoader', 'items.*.file')).toBe(true) + expect(store.isModelWidget('VAELoader', 'items.3.file')).toBe(false) + }) + + it.for(['g', 'y'])( + 'keeps repeated widget checks stable with the %s flag', + (flags) => { + const store = useModelToNodeStore() + store.registerNodeProvider( + 'vae', + new ModelNodeProvider( + createMockNodeDef('MultiVAE'), + new RegExp(/^items\.\d+\.file$/, flags) + ) + ) + + expect(store.isModelWidget('MultiVAE', 'items.1.file')).toBe(true) + expect(store.isModelWidget('MultiVAE', 'items.1.file')).toBe(true) + expect(store.isModelWidget('MultiVAE', 'items.2.file')).toBe(true) + expect(store.isModelWidget('MultiVAE', 'items.2.strength')).toBe(false) + } + ) + describe('getAllNodeProviders', () => { it('should return all providers for model type with multiple nodes', () => { const modelToNodeStore = useModelToNodeStore() @@ -272,7 +319,7 @@ describe('useModelToNodeStore', () => { ) const loraProviders = modelToNodeStore.getAllNodeProviders('loras') - expect(loraProviders).toHaveLength(3) + expect(loraProviders).toHaveLength(5) expect(loraProviders).toEqual( expect.arrayContaining([ expect.objectContaining({ diff --git a/src/stores/modelToNodeStore.ts b/src/stores/modelToNodeStore.ts index 77695de20aa..1c8650687f4 100644 --- a/src/stores/modelToNodeStore.ts +++ b/src/stores/modelToNodeStore.ts @@ -4,16 +4,17 @@ import { computed, ref } from 'vue' import { MODEL_NODE_MAPPINGS } from '@/platform/assets/mappings/modelNodeMappings' import type { ComfyNodeDefImpl } from '@/stores/nodeDefStore' import { useNodeDefStore } from '@/stores/nodeDefStore' +import { matchesWidgetName } from '@/utils/widgetBinding' /** Helper class that defines how to construct a node from a model. */ export class ModelNodeProvider { /** The node definition to use for this model. */ public nodeDef: ComfyNodeDefImpl - /** The node input key for where to insert the model name. */ - public key: string + /** The input name or pattern used to select the model widget. */ + public key: string | RegExp - constructor(nodeDef: ComfyNodeDefImpl, key: string) { + constructor(nodeDef: ComfyNodeDefImpl, key: string | RegExp) { this.nodeDef = nodeDef this.key = key } @@ -25,15 +26,16 @@ export const useModelToNodeStore = defineStore('modelToNode', () => { const nodeDefStore = useNodeDefStore() const haveDefaultsLoaded = ref(false) - /** Internal computed for reactive caching of registered node types */ - const registeredNodeTypes = computed>(() => { - return Object.fromEntries( + const registeredNodeTypes = computed< + Partial> + >(() => + Object.fromEntries( Object.values(modelToNodeMap.value) .filter((providers) => providers !== undefined) .flat() .map((provider) => [provider.nodeDef.name, provider.key]) ) - }) + ) /** Internal computed for efficient reverse lookup: nodeType -> category */ const nodeTypeToCategory = computed(() => { @@ -51,11 +53,16 @@ export const useModelToNodeStore = defineStore('modelToNode', () => { }) /** Get set of all registered node types for efficient lookup */ - function getRegisteredNodeTypes(): Record { + function getRegisteredNodeTypes(): Partial> { registerDefaults() return registeredNodeTypes.value } + function isModelWidget(nodeType: string, widgetName: string): boolean { + const key = getRegisteredNodeTypes()[nodeType] + return key !== undefined && matchesWidgetName(widgetName, key) + } + /** * Get the category for a given node type. * Performs efficient O(1) lookup using cached reverse map. @@ -125,7 +132,9 @@ export const useModelToNodeStore = defineStore('modelToNode', () => { */ function registerNodeProvider( modelType: string, - nodeProvider: ModelNodeProvider | { nodeDef: undefined; key: string } + nodeProvider: + | ModelNodeProvider + | { nodeDef: undefined; key: string | RegExp } ) { registerDefaults() if (!nodeProvider.nodeDef) return @@ -138,7 +147,11 @@ export const useModelToNodeStore = defineStore('modelToNode', () => { * @param nodeClass The node class name to register. * @param key The key to use for the node input. */ - function quickRegister(modelType: string, nodeClass: string, key: string) { + function quickRegister( + modelType: string, + nodeClass: string, + key: string | RegExp + ) { registerNodeProvider( modelType, new ModelNodeProvider(nodeDefStore.nodeDefsByName[nodeClass], key) @@ -162,6 +175,7 @@ export const useModelToNodeStore = defineStore('modelToNode', () => { return { modelToNodeMap, getRegisteredNodeTypes, + isModelWidget, getCategoryForNodeType, getNodeProvider, getAllNodeProviders, diff --git a/src/stores/nodeDefStore.test.ts b/src/stores/nodeDefStore.test.ts index 63d8ad93edf..cd9ba5c49b7 100644 --- a/src/stores/nodeDefStore.test.ts +++ b/src/stores/nodeDefStore.test.ts @@ -9,7 +9,11 @@ import { createTestSubgraphNode } from '@/lib/litegraph/src/subgraph/__fixtures__/subgraphHelpers' import type { ComfyNodeDef } from '@/schemas/nodeDefSchema' -import { useNodeDefStore, useNodeFrequencyStore } from '@/stores/nodeDefStore' +import { + ComfyNodeDefImpl, + useNodeDefStore, + useNodeFrequencyStore +} from '@/stores/nodeDefStore' import type { NodeDefFilter } from '@/stores/nodeDefStore' describe('useNodeDefStore', () => { @@ -37,6 +41,28 @@ describe('useNodeDefStore', () => { ...overrides }) + it('finds widget types inside a DynamicGroup template', () => { + const nodeDef = new ComfyNodeDefImpl( + createMockNodeDef({ + input: { + required: { + loras: [ + 'COMFY_DYNAMICGROUP_V3', + { + template: { + required: { name: ['COMBO', { options: ['A', 'B'] }] }, + optional: { strength: ['FLOAT', { default: 1 }] } + } + } + ] + } + } + }) + ) + + expect(nodeDef.inputTypes).toEqual(['COMBO', 'FLOAT']) + }) + describe('filter registry', () => { it('should register a new filter', () => { const filter: NodeDefFilter = { diff --git a/src/stores/nodeDefStore.ts b/src/stores/nodeDefStore.ts index 7a8f6040738..c1c40891eb5 100644 --- a/src/stores/nodeDefStore.ts +++ b/src/stores/nodeDefStore.ts @@ -6,10 +6,14 @@ import { computed, ref, watchEffect } from 'vue' import { resolveNodeDefText, t } from '@/i18n' import { promotedInputSource } from '@/core/graph/subgraph/promotedInputWidget' import { resolveConcretePromotedWidget } from '@/core/graph/subgraph/resolveConcretePromotedWidget' +import { resolveDynamicInputSpec } from '@/core/graph/widgets/dynamicInputSpec' import { resolveInputType } from '@/core/graph/widgets/dynamicTypes' import { LiteGraph } from '@/lib/litegraph/src/litegraph' import type { LGraphNode } from '@/lib/litegraph/src/litegraph' -import { transformNodeDefV1ToV2 } from '@/schemas/nodeDef/migration' +import { + transformNodeDefV1ToV2, + transformInputSpecV1ToV2 +} from '@/schemas/nodeDef/migration' import type { ComfyNodeDef as ComfyNodeDefV2, InputSpec as InputSpecV2, @@ -455,7 +459,20 @@ export const useNodeDefStore = defineStore('nodeDef', () => { const nodeDef = fromLGraphNode(node) if (!nodeDef) return undefined - return nodeDef.inputs[widgetName] + if (Object.hasOwn(nodeDef.inputs, widgetName)) + return nodeDef.inputs[widgetName] + const resolved = resolveDynamicInputSpec( + nodeDef.input, + widgetName, + (name) => node.widgets?.find((widget) => widget.name === name)?.value + ) + return ( + resolved && + transformInputSpecV1ToV2(resolved.spec, { + name: widgetName, + isOptional: resolved.isOptional + }) + ) } // A subgraph node's widget is a promoted input named after its slot; resolve // the interior source and read its real spec instead of fabricating one. diff --git a/src/stores/widgetValueStore.ts b/src/stores/widgetValueStore.ts index 133c0dc622f..5ab7be357fb 100644 --- a/src/stores/widgetValueStore.ts +++ b/src/stores/widgetValueStore.ts @@ -155,6 +155,17 @@ export const useWidgetValueStore = defineStore('widgetValue', () => { : undefined } + function getRestoredWidgetValueCount( + graphId: UUID, + nodeId: NodeId + ): number | undefined { + const restoration = graphWidgetRestorations.get(graphId)?.get(nodeId) + if (!restoration) return + return restoration.restoreNamed && restoration.named + ? Object.keys(restoration.named).length + : restoration.positional.length + } + function clearNodeWidgetRestoration(graphId: UUID, nodeId: NodeId): void { clearNodeScoped(graphWidgetRestorations, graphId, nodeId) } @@ -507,6 +518,7 @@ export const useWidgetValueStore = defineStore('widgetValue', () => { setNodeWidgetRestoration, clearNodeWidgetRestoration, getRestoredWidgetValue, + getRestoredWidgetValueCount, getWidget, getWidgetRenderState, getWidgetVisibility, diff --git a/src/types/simplifiedWidget.ts b/src/types/simplifiedWidget.ts index d5629a45e13..7c410c46f33 100644 --- a/src/types/simplifiedWidget.ts +++ b/src/types/simplifiedWidget.ts @@ -76,6 +76,8 @@ export interface SimplifiedWidget< /** Localized display label (falls back to name if not provided) */ label?: string + displayLabel?: string + /** Widget options including filtered PrimeVue props */ options?: O diff --git a/src/utils/litegraphUtil.test.ts b/src/utils/litegraphUtil.test.ts index 33cfe9a85ca..75dcb1bc1cc 100644 --- a/src/utils/litegraphUtil.test.ts +++ b/src/utils/litegraphUtil.test.ts @@ -6,6 +6,7 @@ import { LGraph, LGraphNode, LiteGraph } from '@/lib/litegraph/src/litegraph' import { createTestSubgraph } from '@/lib/litegraph/src/subgraph/__fixtures__/subgraphHelpers' import type { IBaseWidget } from '@/lib/litegraph/src/types/widgets' import type { InputSpec } from '@/schemas/nodeDef/nodeDefSchemaV2' +import { transformInputSpecV1ToV2 } from '@/schemas/nodeDef/migration' import { toNodeId } from '@/types/nodeId' import { widgetId } from '@/types/widgetId' import { createMockLGraphNode } from '@/utils/__tests__/litegraphTestUtils' @@ -385,4 +386,37 @@ describe('migrateWidgetsValues', () => { expect(migrateWidgetsValues(inputDefs, widgets, [1, 20])).toEqual([20]) }) + + it('preserves values when an unselected DynamicCombo option contains a group', () => { + const selector = transformInputSpecV1ToV2( + [ + 'COMFY_DYNAMICCOMBO_V3', + { + options: [ + { key: 'none', inputs: {} }, + { + key: 'group', + inputs: { + required: { + rows: [ + 'COMFY_DYNAMICGROUP_V3', + { template: { required: { strength: ['FLOAT', {}] } } } + ] + } + } + } + ] + } + ], + { name: 'selector' } + ) + + expect( + migrateWidgetsValues( + { ...inputDefs, selector }, + [makeWidget('steps')], + [1, 20] + ) + ).toEqual([1, 20]) + }) }) diff --git a/src/utils/litegraphUtil.ts b/src/utils/litegraphUtil.ts index 69d153bfa85..8efec36e136 100644 --- a/src/utils/litegraphUtil.ts +++ b/src/utils/litegraphUtil.ts @@ -20,6 +20,11 @@ import type { WidgetCallbackOptions } from '@/lib/litegraph/src/types/widgets' import type { InputSpec } from '@/schemas/nodeDef/nodeDefSchemaV2' +import { transformInputSpecV1ToV2 } from '@/schemas/nodeDef/migration' +import { + zAutogrowOptions, + zDynamicComboInputSpec +} from '@/schemas/nodeDefSchema' import { useToastStore } from '@/platform/updates/common/toastStore' import { useNodeZIndex } from '@/renderer/extensions/vueNodes/composables/useNodeZIndex' import { app } from '@/scripts/app' @@ -196,6 +201,8 @@ export function migrateWidgetsValues( widgets: IBaseWidget[], widgetsValues: TWidgetValue[] ): TWidgetValue[] { + if (Object.values(inputDefs).some(containsDynamicGroup)) return widgetsValues + const widgetNames = new Set(widgets.map((w) => w.name)) const originalWidgetsInputs = Object.values(inputDefs).filter( (input) => widgetNames.has(input.name) || input.forceInput @@ -244,6 +251,25 @@ export function migrateWidgetsValues( ) } +function containsDynamicGroup(input: InputSpec): boolean { + if (input.type === 'COMFY_DYNAMICGROUP_V3') return true + + const autogrow = + input.type === 'COMFY_AUTOGROW_V3' + ? zAutogrowOptions.safeParse(input).data + : undefined + const combo = zDynamicComboInputSpec.safeParse([input.type, input]).data + const nestedInputs = autogrow + ? [autogrow.template.input] + : (combo?.[1].options.map((option) => option.inputs) ?? []) + return nestedInputs.some((inputs) => + Object.entries({ ...inputs.required, ...inputs.optional }).some( + ([name, spec]) => + containsDynamicGroup(transformInputSpecV1ToV2(spec, { name })) + ) + ) +} + /** * Compress widget input slots by removing all unconnected widget input slots. * This should match the serialization format of legacy widget conversion. diff --git a/src/utils/widgetBinding.ts b/src/utils/widgetBinding.ts new file mode 100644 index 00000000000..13d96408cbd --- /dev/null +++ b/src/utils/widgetBinding.ts @@ -0,0 +1,16 @@ +export interface WidgetValueBinding { + selector: string | RegExp + value: string +} + +export function matchesWidgetName( + name: string, + selector: string | RegExp +): boolean { + return ( + name !== '' && + (typeof selector === 'string' + ? name === selector + : name.search(selector) !== -1) + ) +} diff --git a/src/workbench/extensions/agent/crdt/liveGraphApplier.changeTracker.test.ts b/src/workbench/extensions/agent/crdt/liveGraphApplier.changeTracker.test.ts index f7e7257b147..c7838b3cd05 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 @@ -61,10 +72,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'] } @@ -96,6 +134,7 @@ async function workflowJson(graph: LGraph): Promise { beforeEach(() => { LiteGraph.registerNodeType('TestSource', TestSource) LiteGraph.registerNodeType('TestSink', TestSink) + LiteGraph.registerNodeType('DynamicGroupTest', DynamicGroupTest) ChangeTracker.isLoadingGraph = false }) @@ -176,4 +215,107 @@ 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: () => [], + docPromotedWidgets: () => null, + 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' + } + ]) + ) + }) }) diff --git a/tools/devtools/README.md b/tools/devtools/README.md index 108114daeae..3c33c1d80fc 100644 --- a/tools/devtools/README.md +++ b/tools/devtools/README.md @@ -24,6 +24,19 @@ During CI/CD, these files are automatically copied to the ComfyUI `custom_nodes` cp -r tools/devtools/* /path/to/your/ComfyUI/custom_nodes/ComfyUI_devtools/ ``` +## DynamicGroup contract test + +`Node With Dynamic Group` repeats a LoRA-shaped widget template and returns its +received rows as JSON. It does not load models. Connect its output to Preview as +Text. Enable Modern Node Design (Node 2.0) to edit its dynamic rows; legacy +canvas shows one Node 2.0-only notice per group instead of editable controls. + +This node requires a backend providing `io.DynamicGroup` (currently +[feat: add DynamicGroup widget input](https://github.com/Comfy-Org/ComfyUI/pull/16260)). +Older backends do not register it. Browser tests using real `/object_info`, +workflow storage and execution live in the separate integration PR together +with selection of the unmerged backend. + ## Migration This directory was created as part of issue #4683 to merge the ComfyUI_devtools repository into the main frontend repository, eliminating the need for separate versioning and simplifying the development workflow. diff --git a/tools/devtools/nodes/inputs.py b/tools/devtools/nodes/inputs.py index e78428a7f62..24ff87845be 100644 --- a/tools/devtools/nodes/inputs.py +++ b/tools/devtools/nodes/inputs.py @@ -1,5 +1,6 @@ from __future__ import annotations +import json import time from comfy_api.v0_0_2 import IO @@ -495,6 +496,31 @@ async def execute(cls): return IO.NodeOutput() +class NodeWithDynamicGroup(IO.ComfyNode): + @classmethod + def define_schema(cls): + return IO.Schema( + node_id="DevToolsNodeWithDynamicGroup", + display_name="Node With Dynamic Group", + category="DevTools", + description="Echoes repeated widget rows without loading models.", + inputs=[ + IO.String.Input("before", default="first"), + IO.DynamicGroup.Input("loras", template=[ + IO.Combo.Input("lora_name", options=["A.safetensors", "B.safetensors", "C.safetensors"]), + IO.Float.Input("strength", default=1.0, min=-2.0, max=2.0, step=0.1), + IO.Boolean.Input("enabled", default=True, optional=True), + ], min=0, max=3, group_name="LoRA"), + IO.String.Input("after", default="last"), + ], + outputs=[IO.String.Output("rows")], + ) + + @classmethod + def execute(cls, before, loras, after): + return IO.NodeOutput(json.dumps({"before": before, "loras": loras, "after": after})) + + NODE_CLASS_MAPPINGS = { "DevToolsLongComboDropdown": LongComboDropdown, "DevToolsNodeWithOptionalInput": NodeWithOptionalInput, @@ -523,6 +549,9 @@ async def execute(cls): "DevToolsNodeWithDynamicCombo": NodeWithDynamicCombo, } +if hasattr(IO, "DynamicGroup"): + NODE_CLASS_MAPPINGS["DevToolsNodeWithDynamicGroup"] = NodeWithDynamicGroup + NODE_DISPLAY_NAME_MAPPINGS = { "DevToolsLongComboDropdown": "Long Combo Dropdown", "DevToolsNodeWithOptionalInput": "Node With Optional Input",