From 3af6314fd9a72c9ce928066b0702031f67d84569 Mon Sep 17 00:00:00 2001 From: jaeone94 <89377375+jaeone94@users.noreply.github.com> Date: Wed, 5 Aug 2026 11:02:16 +0900 Subject: [PATCH] fix(subgraph): mirror promoted image widget previews --- ...aph-with-nested-promoted-image-widget.json | 252 ++++++++++++++++++ .../subgraph-with-promoted-image-widget.json | 183 +++++++++++++ .../subgraphPromotedImageWidget.spec.ts | 203 ++++++++++++++ .../subgraph/liftNodeErrorsToBoundary.ts | 62 +---- .../graph/subgraph/promotedInputBoundary.ts | 52 ++++ .../subgraph/subgraphHostExecution.test.ts | 128 +++++++++ .../graph/subgraph/subgraphHostExecution.ts | 87 ++++++ .../infrastructure/LGraphCanvasEventMap.ts | 5 + src/locales/en/main.json | 2 + .../vueNodes/components/ImagePreview.test.ts | 13 + .../vueNodes/components/ImagePreview.vue | 7 +- .../components/LGraphNode.subgraph.test.ts | 6 + .../vueNodes/components/LGraphNode.test.ts | 6 + .../vueNodes/components/LGraphNode.vue | 71 ++++- .../vueNodes/components/NodeContent.vue | 2 + .../vueNodes/components/NodeWidgets.test.ts | 6 + .../resolveLinkedImageWidgetValue.test.ts | 153 +++++++++++ .../resolveLinkedImageWidgetValue.ts | 82 ++++++ .../composables/useProcessedWidgets.test.ts | 100 +++++++ .../composables/useProcessedWidgets.ts | 50 +++- .../form/dropdown/FormDropdownInput.test.ts | 6 + .../form/dropdown/FormDropdownInput.vue | 1 + src/stores/executionErrorStore.ts | 9 +- src/stores/subgraphNavigationStore.ts | 73 +++++ 24 files changed, 1484 insertions(+), 75 deletions(-) create mode 100644 browser_tests/assets/subgraphs/subgraph-with-nested-promoted-image-widget.json create mode 100644 browser_tests/assets/subgraphs/subgraph-with-promoted-image-widget.json create mode 100644 browser_tests/tests/subgraph/subgraphPromotedImageWidget.spec.ts create mode 100644 src/core/graph/subgraph/promotedInputBoundary.ts create mode 100644 src/core/graph/subgraph/subgraphHostExecution.test.ts create mode 100644 src/core/graph/subgraph/subgraphHostExecution.ts create mode 100644 src/renderer/extensions/vueNodes/composables/resolveLinkedImageWidgetValue.test.ts create mode 100644 src/renderer/extensions/vueNodes/composables/resolveLinkedImageWidgetValue.ts diff --git a/browser_tests/assets/subgraphs/subgraph-with-nested-promoted-image-widget.json b/browser_tests/assets/subgraphs/subgraph-with-nested-promoted-image-widget.json new file mode 100644 index 00000000000..74a37ee3731 --- /dev/null +++ b/browser_tests/assets/subgraphs/subgraph-with-nested-promoted-image-widget.json @@ -0,0 +1,252 @@ +{ + "id": "aaaaaaaa-bbbb-4ccc-8ddd-ffffffffffff", + "revision": 0, + "last_node_id": 12, + "last_link_id": 0, + "nodes": [ + { + "id": 11, + "type": "44444444-5555-4666-8777-888888888888", + "pos": [200, 300], + "size": [400, 300], + "flags": {}, + "order": 0, + "mode": 0, + "inputs": [], + "outputs": [ + { "name": "IMAGE", "type": "IMAGE", "links": null }, + { "name": "MASK", "type": "MASK", "links": null } + ], + "properties": {}, + "widgets_values": [""] + }, + { + "id": 12, + "type": "44444444-5555-4666-8777-888888888888", + "pos": [700, 300], + "size": [400, 300], + "flags": {}, + "order": 1, + "mode": 0, + "inputs": [], + "outputs": [ + { "name": "IMAGE", "type": "IMAGE", "links": null }, + { "name": "MASK", "type": "MASK", "links": null } + ], + "properties": {}, + "widgets_values": [""] + } + ], + "links": [], + "groups": [], + "definitions": { + "subgraphs": [ + { + "id": "44444444-5555-4666-8777-888888888888", + "version": 1, + "state": { + "lastGroupId": 0, + "lastNodeId": 77, + "lastLinkId": 3, + "lastRerouteId": 0 + }, + "revision": 0, + "config": {}, + "name": "Outer Promoted Image Widget", + "inputNode": { + "id": -10, + "bounding": [100, 300, 120, 60] + }, + "outputNode": { + "id": -20, + "bounding": [700, 300, 120, 60] + }, + "inputs": [ + { + "id": "55555555-6666-4777-8888-999999999999", + "name": "image", + "type": "COMBO", + "linkIds": [1], + "pos": [200, 320] + } + ], + "outputs": [ + { + "id": "66666666-7777-4888-8999-aaaaaaaaaaaa", + "name": "IMAGE", + "type": "IMAGE", + "linkIds": [2], + "pos": [720, 320] + }, + { + "id": "77777777-8888-4999-8aaa-bbbbbbbbbbbb", + "name": "MASK", + "type": "MASK", + "linkIds": [3], + "pos": [720, 340] + } + ], + "widgets": [], + "nodes": [ + { + "id": 77, + "type": "11111111-2222-4333-8444-555555555555", + "pos": [300, 200], + "size": [315, 360], + "flags": {}, + "order": 0, + "mode": 0, + "inputs": [ + { + "name": "image", + "type": "COMBO", + "widget": { "name": "image" }, + "link": 1 + } + ], + "outputs": [ + { "name": "IMAGE", "type": "IMAGE", "links": [2] }, + { "name": "MASK", "type": "MASK", "links": [3] } + ], + "properties": {}, + "widgets_values": [] + } + ], + "groups": [], + "links": [ + { + "id": 1, + "origin_id": -10, + "origin_slot": 0, + "target_id": 77, + "target_slot": 0, + "type": "COMBO" + }, + { + "id": 2, + "origin_id": 77, + "origin_slot": 0, + "target_id": -20, + "target_slot": 0, + "type": "IMAGE" + }, + { + "id": 3, + "origin_id": 77, + "origin_slot": 1, + "target_id": -20, + "target_slot": 1, + "type": "MASK" + } + ], + "extra": {} + }, + { + "id": "11111111-2222-4333-8444-555555555555", + "version": 1, + "state": { + "lastGroupId": 0, + "lastNodeId": 1, + "lastLinkId": 3, + "lastRerouteId": 0 + }, + "revision": 0, + "config": {}, + "name": "Inner Promoted Image Widget", + "inputNode": { + "id": -10, + "bounding": [100, 300, 120, 60] + }, + "outputNode": { + "id": -20, + "bounding": [700, 300, 120, 60] + }, + "inputs": [ + { + "id": "99999999-8888-4777-8666-555555555555", + "name": "image", + "type": "COMBO", + "linkIds": [1], + "pos": [200, 320] + } + ], + "outputs": [ + { + "id": "22222222-3333-4444-8555-666666666666", + "name": "IMAGE", + "type": "IMAGE", + "linkIds": [2], + "pos": [720, 320] + }, + { + "id": "33333333-4444-4555-8666-777777777777", + "name": "MASK", + "type": "MASK", + "linkIds": [3], + "pos": [720, 340] + } + ], + "widgets": [], + "nodes": [ + { + "id": 1, + "type": "LoadImage", + "pos": [300, 200], + "size": [315, 360], + "flags": {}, + "order": 0, + "mode": 0, + "inputs": [ + { + "localized_name": "image", + "name": "image", + "type": "COMBO", + "widget": { "name": "image" }, + "link": 1 + } + ], + "outputs": [ + { "name": "IMAGE", "type": "IMAGE", "links": [2] }, + { "name": "MASK", "type": "MASK", "links": [3] } + ], + "properties": { "Node name for S&R": "LoadImage" }, + "widgets_values": ["interior.png", "image"] + } + ], + "groups": [], + "links": [ + { + "id": 1, + "origin_id": -10, + "origin_slot": 0, + "target_id": 1, + "target_slot": 0, + "type": "COMBO" + }, + { + "id": 2, + "origin_id": 1, + "origin_slot": 0, + "target_id": -20, + "target_slot": 0, + "type": "IMAGE" + }, + { + "id": 3, + "origin_id": 1, + "origin_slot": 1, + "target_id": -20, + "target_slot": 1, + "type": "MASK" + } + ], + "extra": {} + } + ] + }, + "config": {}, + "extra": { + "ds": { "offset": [0, 0], "scale": 1 } + }, + "version": 0.4 +} diff --git a/browser_tests/assets/subgraphs/subgraph-with-promoted-image-widget.json b/browser_tests/assets/subgraphs/subgraph-with-promoted-image-widget.json new file mode 100644 index 00000000000..3f6d5f8d71f --- /dev/null +++ b/browser_tests/assets/subgraphs/subgraph-with-promoted-image-widget.json @@ -0,0 +1,183 @@ +{ + "id": "aaaaaaaa-bbbb-4ccc-8ddd-eeeeeeeeeeee", + "revision": 0, + "last_node_id": 12, + "last_link_id": 0, + "nodes": [ + { + "id": 11, + "type": "11111111-2222-4333-8444-555555555555", + "pos": [200, 300], + "size": [400, 300], + "flags": {}, + "order": 0, + "mode": 0, + "inputs": [], + "outputs": [ + { + "name": "IMAGE", + "type": "IMAGE", + "links": null + }, + { + "name": "MASK", + "type": "MASK", + "links": null + } + ], + "properties": {}, + "widgets_values": [""] + }, + { + "id": 12, + "type": "11111111-2222-4333-8444-555555555555", + "pos": [700, 300], + "size": [400, 300], + "flags": {}, + "order": 1, + "mode": 0, + "inputs": [], + "outputs": [ + { + "name": "IMAGE", + "type": "IMAGE", + "links": null + }, + { + "name": "MASK", + "type": "MASK", + "links": null + } + ], + "properties": {}, + "widgets_values": [""] + } + ], + "links": [], + "groups": [], + "definitions": { + "subgraphs": [ + { + "id": "11111111-2222-4333-8444-555555555555", + "version": 1, + "state": { + "lastGroupId": 0, + "lastNodeId": 1, + "lastLinkId": 1, + "lastRerouteId": 0 + }, + "revision": 0, + "config": {}, + "name": "Promoted Image Widget", + "inputNode": { + "id": -10, + "bounding": [100, 300, 120, 60] + }, + "outputNode": { + "id": -20, + "bounding": [700, 300, 120, 60] + }, + "inputs": [ + { + "id": "99999999-8888-4777-8666-555555555555", + "name": "image", + "type": "COMBO", + "linkIds": [1], + "pos": [200, 320] + } + ], + "outputs": [ + { + "id": "22222222-3333-4444-8555-666666666666", + "name": "IMAGE", + "type": "IMAGE", + "linkIds": [2], + "pos": [720, 320] + }, + { + "id": "33333333-4444-4555-8666-777777777777", + "name": "MASK", + "type": "MASK", + "linkIds": [3], + "pos": [720, 340] + } + ], + "widgets": [], + "nodes": [ + { + "id": 1, + "type": "LoadImage", + "pos": [300, 200], + "size": [315, 360], + "flags": {}, + "order": 0, + "mode": 0, + "inputs": [ + { + "localized_name": "image", + "name": "image", + "type": "COMBO", + "widget": { + "name": "image" + }, + "link": 1 + } + ], + "outputs": [ + { + "name": "IMAGE", + "type": "IMAGE", + "links": [2] + }, + { + "name": "MASK", + "type": "MASK", + "links": [3] + } + ], + "properties": { + "Node name for S&R": "LoadImage" + }, + "widgets_values": ["interior.png", "image"] + } + ], + "groups": [], + "links": [ + { + "id": 1, + "origin_id": -10, + "origin_slot": 0, + "target_id": 1, + "target_slot": 0, + "type": "COMBO" + }, + { + "id": 2, + "origin_id": 1, + "origin_slot": 0, + "target_id": -20, + "target_slot": 0, + "type": "IMAGE" + }, + { + "id": 3, + "origin_id": 1, + "origin_slot": 1, + "target_id": -20, + "target_slot": 1, + "type": "MASK" + } + ], + "extra": {} + } + ] + }, + "config": {}, + "extra": { + "ds": { + "offset": [0, 0], + "scale": 1 + } + }, + "version": 0.4 +} diff --git a/browser_tests/tests/subgraph/subgraphPromotedImageWidget.spec.ts b/browser_tests/tests/subgraph/subgraphPromotedImageWidget.spec.ts new file mode 100644 index 00000000000..f8202b34a45 --- /dev/null +++ b/browser_tests/tests/subgraph/subgraphPromotedImageWidget.spec.ts @@ -0,0 +1,203 @@ +import { expect } from '@playwright/test' + +import { comfyPageFixture as test } from '@e2e/fixtures/ComfyPage' +import type { ComfyPage } from '@e2e/fixtures/ComfyPage' +import { assetPath } from '@e2e/fixtures/utils/paths' + +async function uploadImageToHost( + comfyPage: ComfyPage, + nodeId: string, + filename: string +) { + const node = comfyPage.vueNodes.getNodeLocator(nodeId) + const uploadResponse = comfyPage.page.waitForResponse( + (response) => + response.url().includes('/upload/') && response.status() === 200 + ) + + await node + .locator('input[type="file"][aria-label="Upload"]') + .setInputFiles(assetPath(filename)) + await uploadResponse +} + +test.describe( + 'Promoted image widget projection', + { tag: ['@subgraph', '@widget', '@vue-nodes'] }, + () => { + test.beforeEach(async ({ comfyPage }) => { + await comfyPage.page.route('**/api/view?*', (route) => + route.fulfill({ + contentType: 'image/webp', + path: assetPath('image64x64.webp') + }) + ) + await comfyPage.workflow.loadWorkflow( + 'subgraphs/subgraph-with-promoted-image-widget' + ) + await uploadImageToHost(comfyPage, '11', 'image64x64.webp') + await uploadImageToHost(comfyPage, '12', 'image32x32.webp') + }) + + test('shows the entered host value and preview without changing the definition value', async ({ + comfyPage + }) => { + const firstHost = comfyPage.vueNodes.getNodeLocator('11') + const secondHost = comfyPage.vueNodes.getNodeLocator('12') + await expect( + firstHost.getByRole('button', { + name: 'image64x64.webp', + exact: true + }) + ).toBeVisible() + await expect( + secondHost.getByRole('button', { + name: 'image32x32.webp', + exact: true + }) + ).toBeVisible() + + await comfyPage.vueNodes.enterSubgraph('12') + await expect.poll(() => comfyPage.subgraph.isInSubgraph()).toBe(true) + + const interiorNode = comfyPage.vueNodes.getNodeLocator('1') + const projectedValue = interiorNode.getByRole('button', { + name: 'image32x32.webp', + exact: true + }) + const previewImage = interiorNode.locator('.image-preview img') + + await expect(projectedValue).toBeVisible() + await expect(projectedValue).toBeDisabled() + await expect(previewImage).toBeVisible() + await expect( + interiorNode.getByRole('button', { name: 'Edit or mask image' }) + ).toHaveCount(0) + await expect + .poll(async () => { + const src = await previewImage.getAttribute('src') + if (!src) return null + const params = new URL(src, 'http://localhost').searchParams + return { + filename: params.get('filename'), + subfolder: params.get('subfolder'), + type: params.get('type') + } + }) + .toEqual({ + filename: 'image32x32.webp', + subfolder: '', + type: 'input' + }) + + await expect + .poll(() => + comfyPage.page.evaluate(() => { + const node = window.app!.canvas.graph?.nodes.find( + (candidate) => String(candidate.id) === '1' + ) + return node?.widgets?.find((widget) => widget.name === 'image') + ?.value + }) + ) + .toBe('interior.png') + + const goToParent = interiorNode.getByRole('button', { + name: 'Go to parent node' + }) + await expect(goToParent).toBeVisible() + await goToParent.click() + await expect.poll(() => comfyPage.subgraph.isInSubgraph()).toBe(false) + await expect(secondHost).toBeVisible() + }) + + test('keeps the interior fallback read-only when the shared host is ambiguous', async ({ + comfyPage + }) => { + await comfyPage.page.evaluate(() => { + window.location.hash = '#11111111-2222-4333-8444-555555555555' + }) + await expect.poll(() => comfyPage.subgraph.isInSubgraph()).toBe(true) + + const interiorNode = comfyPage.vueNodes.getNodeLocator('1') + const fallbackValue = interiorNode.getByRole('button', { + name: 'interior.png', + exact: true + }) + + await expect(fallbackValue).toBeVisible() + await expect(fallbackValue).toBeDisabled() + await expect( + interiorNode.getByRole('button', { name: 'Go to parent node' }) + ).toHaveCount(0) + }) + } +) + +test.describe( + 'Nested promoted image widget projection', + { tag: ['@subgraph', '@widget', '@vue-nodes'] }, + () => { + test.beforeEach(async ({ comfyPage }) => { + await comfyPage.page.route('**/api/view?*', (route) => + route.fulfill({ + contentType: 'image/webp', + path: assetPath('image32x32.webp') + }) + ) + await comfyPage.workflow.loadWorkflow( + 'subgraphs/subgraph-with-nested-promoted-image-widget' + ) + await uploadImageToHost(comfyPage, '12', 'image32x32.webp') + }) + + test('preserves the selected root host through nested entry and return', async ({ + comfyPage + }) => { + await comfyPage.vueNodes.enterSubgraph('12') + + const intermediateHost = comfyPage.vueNodes.getNodeLocator('77') + await expect( + intermediateHost.getByRole('button', { + name: 'image32x32.webp', + exact: true + }) + ).toBeVisible() + await expect(intermediateHost.locator('.image-preview img')).toBeVisible() + + await comfyPage.vueNodes.enterSubgraph('77') + + const interiorNode = comfyPage.vueNodes.getNodeLocator('1') + await expect( + interiorNode.getByRole('button', { + name: 'image32x32.webp', + exact: true + }) + ).toBeVisible() + await expect(interiorNode.locator('.image-preview img')).toBeVisible() + + await comfyPage.page.getByTestId('subgraph-breadcrumb-back').click() + + await expect( + intermediateHost.getByRole('button', { + name: 'image32x32.webp', + exact: true + }) + ).toBeVisible() + await expect(intermediateHost.locator('.image-preview img')).toBeVisible() + + await comfyPage.vueNodes.enterSubgraph('77') + await interiorNode + .getByRole('button', { name: 'Go to parent node' }) + .click() + + await expect.poll(() => comfyPage.subgraph.isInSubgraph()).toBe(false) + await expect( + comfyPage.vueNodes.getNodeLocator('12').getByRole('button', { + name: 'image32x32.webp', + exact: true + }) + ).toBeVisible() + }) + } +) diff --git a/src/core/graph/subgraph/liftNodeErrorsToBoundary.ts b/src/core/graph/subgraph/liftNodeErrorsToBoundary.ts index 52d41acf517..2cbaf0d4714 100644 --- a/src/core/graph/subgraph/liftNodeErrorsToBoundary.ts +++ b/src/core/graph/subgraph/liftNodeErrorsToBoundary.ts @@ -2,12 +2,11 @@ import { groupBy, partition } from 'es-toolkit' import type { LGraph } from '@/lib/litegraph/src/litegraph' import type { NodeError } from '@/schemas/apiSchema' -import { tryNormalizeNodeExecutionId } from '@/types/nodeIdentification' -import type { NodeExecutionId } from '@/types/nodeIdentification' import { isNodeLevelValidationError } from '@/utils/executionErrorUtil' import type { NodeValidationError } from '@/utils/executionErrorUtil' import { getNodeByExecutionId } from '@/utils/graphTraversalUtil' -import { isSubgraph } from '@/utils/typeGuardUtil' + +import { resolvePromotedInputBoundaryChain } from './promotedInputBoundary' export interface LiftedErrorExtraInfo { input_name: string @@ -15,11 +14,6 @@ export interface LiftedErrorExtraInfo { source_input_name: string } -export interface LiftedSurface { - hostExecId: NodeExecutionId - hostInputName: string -} - interface ErrorPlacement { kind: 'own' | 'lifted' targetExecId: string @@ -44,50 +38,6 @@ export function getLiftedErrorSource( return { input_name, source_execution_id, source_input_name } } -function getHostExecutionId(executionId: string): NodeExecutionId | null { - const separatorIndex = executionId.lastIndexOf(':') - if (separatorIndex <= 0) return null - return tryNormalizeNodeExecutionId(executionId.slice(0, separatorIndex)) -} - -/** - * Boundary surfaces that expose `(executionId, inputName)`, innermost first. - * Walks one host per level and stops at the last resolvable surface, so an - * unresolvable deeper host falls back to the shallower one (fail-open). - */ -export function resolveLiftChain( - rootGraph: LGraph, - executionId: string, - inputName: string -): LiftedSurface[] { - const chain: LiftedSurface[] = [] - let currentExecId = executionId - let currentInputName = inputName - - for (;;) { - const node = getNodeByExecutionId(rootGraph, currentExecId) - const graph = node?.graph - if (!node || !graph || !isSubgraph(graph)) break - - const slot = node.inputs?.find((input) => input.name === currentInputName) - if (slot?.link == null) break - - const subgraphInput = graph - .getLink(slot.link) - ?.resolve(graph)?.subgraphInput - if (!subgraphInput) break - - const hostExecId = getHostExecutionId(currentExecId) - if (!hostExecId || !getNodeByExecutionId(rootGraph, hostExecId)) break - - chain.push({ hostExecId, hostInputName: subgraphInput.name }) - currentExecId = hostExecId - currentInputName = subgraphInput.name - } - - return chain -} - function createEmptyNodeError(nodeError: NodeError): NodeError { return { ...nodeError, @@ -116,7 +66,9 @@ function toErrorPlacement( const inputName = error.extra_info?.input_name const surface = inputName && !isNodeLevelValidationError(error) - ? resolveLiftChain(rootGraph, executionId, inputName).at(-1) + ? resolvePromotedInputBoundaryChain(rootGraph, executionId, inputName).at( + -1 + ) : undefined if (!inputName || !surface) { @@ -128,14 +80,14 @@ function toErrorPlacement( } const liftedExtraInfo: LiftedErrorExtraInfo = { - input_name: surface.hostInputName, + input_name: surface.inputName, source_execution_id: executionId, source_input_name: inputName } return { kind: 'lifted', - targetExecId: surface.hostExecId, + targetExecId: surface.hostExecutionId, error: { ...error, extra_info: { diff --git a/src/core/graph/subgraph/promotedInputBoundary.ts b/src/core/graph/subgraph/promotedInputBoundary.ts new file mode 100644 index 00000000000..8358c7f1d6d --- /dev/null +++ b/src/core/graph/subgraph/promotedInputBoundary.ts @@ -0,0 +1,52 @@ +import type { LGraph } from '@/lib/litegraph/src/litegraph' +import { tryNormalizeNodeExecutionId } from '@/types/nodeIdentification' +import type { NodeExecutionId } from '@/types/nodeIdentification' +import { getNodeByExecutionId } from '@/utils/graphTraversalUtil' +import { isSubgraph } from '@/utils/typeGuardUtil' + +interface PromotedInputBoundary { + hostExecutionId: NodeExecutionId + inputName: string +} + +function getHostExecutionId(executionId: string): NodeExecutionId | null { + const separatorIndex = executionId.lastIndexOf(':') + if (separatorIndex <= 0) return null + return tryNormalizeNodeExecutionId(executionId.slice(0, separatorIndex)) +} + +/** Resolves promoted input boundaries from the innermost host outward. */ +export function resolvePromotedInputBoundaryChain( + rootGraph: LGraph, + executionId: string, + inputName: string +): PromotedInputBoundary[] { + const chain: PromotedInputBoundary[] = [] + let currentExecutionId = executionId + let currentInputName = inputName + + for (;;) { + const node = getNodeByExecutionId(rootGraph, currentExecutionId) + const graph = node?.graph + if (!node || !graph || !isSubgraph(graph)) break + + const slot = node.inputs?.find((input) => input.name === currentInputName) + if (slot?.link == null) break + + const subgraphInput = graph + .getLink(slot.link) + ?.resolve(graph)?.subgraphInput + if (!subgraphInput) break + + const hostExecutionId = getHostExecutionId(currentExecutionId) + if (!hostExecutionId || !getNodeByExecutionId(rootGraph, hostExecutionId)) { + break + } + + chain.push({ hostExecutionId, inputName: subgraphInput.name }) + currentExecutionId = hostExecutionId + currentInputName = subgraphInput.name + } + + return chain +} diff --git a/src/core/graph/subgraph/subgraphHostExecution.test.ts b/src/core/graph/subgraph/subgraphHostExecution.test.ts new file mode 100644 index 00000000000..c3ec7524859 --- /dev/null +++ b/src/core/graph/subgraph/subgraphHostExecution.test.ts @@ -0,0 +1,128 @@ +import { createTestingPinia } from '@pinia/testing' +import { setActivePinia } from 'pinia' +import { beforeEach, describe, expect, it } from 'vitest' + +import { + createTestRootGraph, + createTestSubgraph, + createTestSubgraphNode +} from '@/lib/litegraph/src/subgraph/__fixtures__/subgraphHelpers' +import { createNodeExecutionId } from '@/types/nodeIdentification' + +import { + findSubgraphHostAncestorExecutionId, + findUniqueSubgraphHostExecutionId, + resolveEnteredSubgraphHostExecutionId +} from './subgraphHostExecution' + +describe('subgraph host execution context', () => { + beforeEach(() => { + setActivePinia(createTestingPinia({ stubActions: false })) + }) + + it('resolves a definition with one host instance', () => { + const rootGraph = createTestRootGraph() + const subgraph = createTestSubgraph({ rootGraph }) + const host = createTestSubgraphNode(subgraph, { id: 11 }) + rootGraph.add(host) + + expect(findUniqueSubgraphHostExecutionId(rootGraph, subgraph)).toBe('11') + }) + + it('does not choose between two host instances of one definition', () => { + const rootGraph = createTestRootGraph() + const subgraph = createTestSubgraph({ rootGraph }) + rootGraph.add(createTestSubgraphNode(subgraph, { id: 11 })) + rootGraph.add(createTestSubgraphNode(subgraph, { id: 12 })) + + expect( + findUniqueSubgraphHostExecutionId(rootGraph, subgraph) + ).toBeUndefined() + }) + + it('treats a nested host under two shared outer instances as ambiguous', () => { + const rootGraph = createTestRootGraph() + const outerSubgraph = createTestSubgraph({ rootGraph }) + const innerSubgraph = createTestSubgraph({ rootGraph }) + outerSubgraph.add( + createTestSubgraphNode(innerSubgraph, { + parentGraph: outerSubgraph, + id: 22 + }) + ) + rootGraph.add(createTestSubgraphNode(outerSubgraph, { id: 11 })) + rootGraph.add(createTestSubgraphNode(outerSubgraph, { id: 12 })) + + expect( + findUniqueSubgraphHostExecutionId(rootGraph, innerSubgraph) + ).toBeUndefined() + }) + + it('preserves an exact ancestor when navigating back outward', () => { + const rootGraph = createTestRootGraph() + const outerSubgraph = createTestSubgraph({ rootGraph }) + const innerSubgraph = createTestSubgraph({ rootGraph }) + outerSubgraph.add( + createTestSubgraphNode(innerSubgraph, { + parentGraph: outerSubgraph, + id: 22 + }) + ) + rootGraph.add(createTestSubgraphNode(outerSubgraph, { id: 11 })) + rootGraph.add(createTestSubgraphNode(outerSubgraph, { id: 12 })) + + expect( + findSubgraphHostAncestorExecutionId( + rootGraph, + createNodeExecutionId([12, 22]), + outerSubgraph + ) + ).toBe('12') + }) + + it('uses the selected host when entering from the root graph', () => { + const rootGraph = createTestRootGraph() + const subgraph = createTestSubgraph({ rootGraph }) + rootGraph.add(createTestSubgraphNode(subgraph, { id: 11 })) + const selectedHost = createTestSubgraphNode(subgraph, { id: 12 }) + rootGraph.add(selectedHost) + + expect( + resolveEnteredSubgraphHostExecutionId( + rootGraph, + undefined, + rootGraph, + selectedHost + ) + ).toBe('12') + }) + + it('extends the exact path when entering a nested subgraph', () => { + const rootGraph = createTestRootGraph() + const outerSubgraph = createTestSubgraph({ rootGraph }) + const innerSubgraph = createTestSubgraph({ rootGraph }) + const innerHost = createTestSubgraphNode(innerSubgraph, { + parentGraph: outerSubgraph, + id: 22 + }) + outerSubgraph.add(innerHost) + rootGraph.add(createTestSubgraphNode(outerSubgraph, { id: 12 })) + + expect( + resolveEnteredSubgraphHostExecutionId( + rootGraph, + createNodeExecutionId([12]), + outerSubgraph, + innerHost + ) + ).toBe('12:22') + expect( + resolveEnteredSubgraphHostExecutionId( + rootGraph, + undefined, + outerSubgraph, + innerHost + ) + ).toBeUndefined() + }) +}) diff --git a/src/core/graph/subgraph/subgraphHostExecution.ts b/src/core/graph/subgraph/subgraphHostExecution.ts new file mode 100644 index 00000000000..e30948a3faa --- /dev/null +++ b/src/core/graph/subgraph/subgraphHostExecution.ts @@ -0,0 +1,87 @@ +import type { LGraph, Subgraph } from '@/lib/litegraph/src/litegraph' +import type { SubgraphNode } from '@/lib/litegraph/src/subgraph/SubgraphNode' +import { + appendNodeExecutionId, + createNodeExecutionId, + getAncestorExecutionIds +} from '@/types/nodeIdentification' +import type { NodeExecutionId } from '@/types/nodeIdentification' +import { getNodeByExecutionId } from '@/utils/graphTraversalUtil' + +export function findUniqueSubgraphHostExecutionId( + rootGraph: LGraph, + targetSubgraph: Subgraph +): NodeExecutionId | undefined { + let match: NodeExecutionId | undefined + let isAmbiguous = false + + function visit( + graph: LGraph | Subgraph, + parentExecutionId: NodeExecutionId | undefined, + ancestorSubgraphIds: ReadonlySet + ): void { + for (const node of graph.nodes) { + if (!node.isSubgraphNode()) continue + if (ancestorSubgraphIds.has(node.subgraph.id)) continue + + const executionId = parentExecutionId + ? appendNodeExecutionId(parentExecutionId, node.id) + : createNodeExecutionId([node.id]) + + if (node.subgraph === targetSubgraph) { + if (match) { + isAmbiguous = true + return + } + match = executionId + } + + const nextAncestorIds = new Set(ancestorSubgraphIds) + nextAncestorIds.add(node.subgraph.id) + visit(node.subgraph, executionId, nextAncestorIds) + if (isAmbiguous) return + } + } + + visit(rootGraph, undefined, new Set()) + return isAmbiguous ? undefined : match +} + +export function findSubgraphHostAncestorExecutionId( + rootGraph: LGraph, + currentHostExecutionId: NodeExecutionId | undefined, + targetSubgraph: Subgraph +): NodeExecutionId | undefined { + if (!currentHostExecutionId) return undefined + + for (const executionId of getAncestorExecutionIds( + currentHostExecutionId + ).reverse()) { + const node = getNodeByExecutionId(rootGraph, executionId) + if (node?.isSubgraphNode() && node.subgraph === targetSubgraph) { + return executionId + } + } + + return undefined +} + +export function resolveEnteredSubgraphHostExecutionId( + rootGraph: LGraph, + currentHostExecutionId: NodeExecutionId | undefined, + closingGraph: LGraph | Subgraph, + fromNode: SubgraphNode +): NodeExecutionId | undefined { + if (fromNode.graph !== closingGraph) return undefined + if (closingGraph === rootGraph) { + return createNodeExecutionId([fromNode.id]) + } + if (!currentHostExecutionId) return undefined + + const currentHost = getNodeByExecutionId(rootGraph, currentHostExecutionId) + if (!currentHost?.isSubgraphNode() || currentHost.subgraph !== closingGraph) { + return undefined + } + + return appendNodeExecutionId(currentHostExecutionId, fromNode.id) +} diff --git a/src/lib/litegraph/src/infrastructure/LGraphCanvasEventMap.ts b/src/lib/litegraph/src/infrastructure/LGraphCanvasEventMap.ts index 48338c19963..a3c43df0c6b 100644 --- a/src/lib/litegraph/src/infrastructure/LGraphCanvasEventMap.ts +++ b/src/lib/litegraph/src/infrastructure/LGraphCanvasEventMap.ts @@ -16,6 +16,11 @@ export interface LGraphCanvasEventMap { /** The old active graph, or `null` if there was no active graph. */ oldGraph: LGraph | Subgraph | null | undefined } + 'subgraph-opening': { + subgraph: Subgraph + closingGraph: LGraph + fromNode: SubgraphNode + } 'subgraph-opened': { subgraph: Subgraph closingGraph: LGraph diff --git a/src/locales/en/main.json b/src/locales/en/main.json index a6c4b35e05a..0063358dfd9 100644 --- a/src/locales/en/main.json +++ b/src/locales/en/main.json @@ -176,6 +176,8 @@ "devOnly": "DEV", "loadWorkflow": "Load Workflow", "goToNode": "Go to Node", + "goToParentNode": "Go to parent node", + "parentNode": "Parent node", "setAsBackground": "Set as Background", "customBackground": "Custom Background", "settings": "Settings", diff --git a/src/renderer/extensions/vueNodes/components/ImagePreview.test.ts b/src/renderer/extensions/vueNodes/components/ImagePreview.test.ts index 3f71da658ae..d925dba069a 100644 --- a/src/renderer/extensions/vueNodes/components/ImagePreview.test.ts +++ b/src/renderer/extensions/vueNodes/components/ImagePreview.test.ts @@ -151,6 +151,19 @@ describe('ImagePreview', () => { screen.getByRole('button', { name: 'Edit or mask image' }) }) + it('hides mask/edit while preserving view and download actions when read-only', () => { + renderImagePreview({ + imageUrls: [defaultProps.imageUrls[0]], + readOnly: true + }) + + expect(screen.getByRole('img')).toBeVisible() + expect( + screen.queryByRole('button', { name: 'Edit or mask image' }) + ).not.toBeInTheDocument() + expect(screen.getByRole('button', { name: 'Download image' })).toBeVisible() + }) + it('hides mask and download buttons when image fails to load', async () => { renderImagePreview({ imageUrls: [defaultProps.imageUrls[0]] diff --git a/src/renderer/extensions/vueNodes/components/ImagePreview.vue b/src/renderer/extensions/vueNodes/components/ImagePreview.vue index c12e386faab..67750517abb 100644 --- a/src/renderer/extensions/vueNodes/components/ImagePreview.vue +++ b/src/renderer/extensions/vueNodes/components/ImagePreview.vue @@ -108,7 +108,9 @@ >