diff --git a/src/lib/litegraph/src/LGraph.test.ts b/src/lib/litegraph/src/LGraph.test.ts index eea4ddc8179..56ad6996577 100644 --- a/src/lib/litegraph/src/LGraph.test.ts +++ b/src/lib/litegraph/src/LGraph.test.ts @@ -334,6 +334,13 @@ describe('LGraph', () => { expect(graph.last_node_id).toBe(7) }) + it.for(['constructor', 'toString', '__proto__'])( + 'does not return inherited property %s as a node', + (id) => { + expect(new LGraph().getNodeById(toNodeId(id))).toBeNull() + } + ) + describe('duplicate node-instance invariants', () => { function createGraphsSharingANodeId() { const ownerGraph = new LGraph() diff --git a/src/lib/litegraph/src/LGraph.ts b/src/lib/litegraph/src/LGraph.ts index 0c2eecd38b3..59b59e43c7a 100644 --- a/src/lib/litegraph/src/LGraph.ts +++ b/src/lib/litegraph/src/LGraph.ts @@ -1609,7 +1609,9 @@ export class LGraph * Returns a node by its id. */ getNodeById(id: NodeId | null | undefined): LGraphNode | null { - return id != null && id !== UNASSIGNED_NODE_ID + return id != null && + id !== UNASSIGNED_NODE_ID && + Object.hasOwn(this._nodes_by_id, id) ? (this._nodes_by_id[id] ?? null) : null } diff --git a/src/renderer/extensions/vueNodes/composables/processedWidgetRenderModel.ts b/src/renderer/extensions/vueNodes/composables/processedWidgetRenderModel.ts index 5798a02c550..7daaf8049d1 100644 --- a/src/renderer/extensions/vueNodes/composables/processedWidgetRenderModel.ts +++ b/src/renderer/extensions/vueNodes/composables/processedWidgetRenderModel.ts @@ -30,8 +30,8 @@ import { useWidgetValueStore } from '@/stores/widgetValueStore' import { - createLeafNodeExecutionId, - createLeafNodeLocatorId + createNodeExecutionId, + createNodeLocatorId } from '@/types/nodeIdentification' import type { NodeExecutionId, NodeLocatorId } from '@/types/nodeIdentification' import type { NodeId } from '@/types/nodeId' @@ -47,8 +47,7 @@ import type { WidgetId } from '@/types/widgetId' import { executionIdFromState, executionIdToNodeLocatorId, - getNodeByLocatorId, - locatorIdFromState, + getNodeByState, subgraphIdFromState } from '@/utils/graphTraversalUtil' import { mapLiveWidgetsById } from '@/utils/litegraphUtil' @@ -155,8 +154,7 @@ function getHostNode( nodeData: NodeState ): LGraphNode | null { if (!rootGraph) return null - const locatorId = locatorIdFromState(nodeData, rootGraph.id) - return locatorId ? getNodeByLocatorId(rootGraph, locatorId) : null + return getNodeByState(rootGraph, nodeData) } function isWidgetVisible( @@ -171,7 +169,7 @@ function isWidgetVisible( function hasWidgetError( widget: { name: string; errorTarget?: WidgetErrorTarget }, - nodeExecId: NodeExecutionId, + nodeExecId: NodeExecutionId | null, nodeErrors: | { errors: { extra_info?: { input_name?: string } }[] } | undefined, @@ -183,8 +181,9 @@ function hasWidgetError( !!nodeErrors?.errors.some( (e) => e.extra_info?.input_name === widget.name ) || - missingModelStore.isWidgetMissingModel(nodeExecId, widget.name) || - missingMediaStore.isWidgetMissingMedia(nodeExecId, widget.name) + (nodeExecId !== null && + (missingModelStore.isWidgetMissingModel(nodeExecId, widget.name) || + missingMediaStore.isWidgetMissingMedia(nodeExecId, widget.name))) const target = widget.errorTarget if (!target) return hasHostError @@ -214,7 +213,7 @@ function createWidgetUpdateHandler({ id: WidgetId live?: { node: LGraphNode; widget: IBaseWidget } errorTarget?: WidgetErrorTarget - nodeExecId: NodeExecutionId + nodeExecId: NodeExecutionId | null widgetName: string widgetOptions: IWidgetOptions executionErrorStore: ReturnType @@ -239,13 +238,15 @@ function createWidgetUpdateHandler({ options ) } - executionErrorStore.clearWidgetRelatedErrors( - nodeExecId, - widgetName, - widgetName, - newValue, - options - ) + if (nodeExecId) { + executionErrorStore.clearWidgetRelatedErrors( + nodeExecId, + widgetName, + widgetName, + newValue, + options + ) + } } } @@ -323,12 +324,10 @@ function widgetNodeLocatorId( ) if (sourceLocator) return sourceLocator } - if (!bareWidgetId) return undefined - return ( - createLeafNodeLocatorId( - subgraphIdFromState(ctx.nodeData, ctx.rootGraphId), - bareWidgetId - ) ?? undefined + if (!bareWidgetId || bareWidgetId.includes(':')) return undefined + return createNodeLocatorId( + subgraphIdFromState(ctx.nodeData, ctx.rootGraphId), + bareWidgetId ) } @@ -341,7 +340,7 @@ interface WidgetProcessingContext { hostNode: LGraphNode | null liveWidgets: Map slotMetadata: Map - nodeExecId: NodeExecutionId + nodeExecId: NodeExecutionId | null nodeErrors: Parameters[2] widgetValueStore: ReturnType executionErrorStore: ReturnType @@ -473,8 +472,7 @@ export function computeProcessedWidgets({ const nodeExecId = isGraphReady && rootGraph ? executionIdFromState(rootGraph, nodeData) - : createLeafNodeExecutionId(nodeData.id) - if (!nodeExecId) return [] + : createNodeExecutionId([nodeData.id]) const hostNode = getHostNode(rootGraph, nodeData) const liveWidgets = hostNode @@ -506,7 +504,9 @@ export function computeProcessedWidgets({ liveWidgets, slotMetadata, nodeExecId, - nodeErrors: executionErrorStore.lastNodeErrors?.[nodeExecId], + nodeErrors: nodeExecId + ? executionErrorStore.lastNodeErrors?.[nodeExecId] + : undefined, widgetValueStore, executionErrorStore, missingModelStore, diff --git a/src/renderer/extensions/vueNodes/composables/useProcessedWidgets.test.ts b/src/renderer/extensions/vueNodes/composables/useProcessedWidgets.test.ts index c0bdc39c7ba..f5ea8f0275e 100644 --- a/src/renderer/extensions/vueNodes/composables/useProcessedWidgets.test.ts +++ b/src/renderer/extensions/vueNodes/composables/useProcessedWidgets.test.ts @@ -11,6 +11,7 @@ import { } from '@/lib/litegraph/src/subgraph/__fixtures__/subgraphHelpers' import type { SubgraphNode } from '@/lib/litegraph/src/subgraph/SubgraphNode' import type { IBaseWidget } from '@/lib/litegraph/src/types/widgets' +import { useMissingMediaStore } from '@/platform/missingMedia/missingMediaStore' import { useMissingModelStore } from '@/platform/missingModel/missingModelStore' import { computeProcessedWidgets } from '@/renderer/extensions/vueNodes/composables/useProcessedWidgets' import WidgetDOM from '@/renderer/extensions/vueNodes/widgets/components/WidgetDOM.vue' @@ -401,6 +402,36 @@ describe('promoted subgraph widgets', () => { }) describe('computeProcessedWidgets', () => { + it('renders an opaque-ID widget before its graph is ready', () => { + const nodeId = toNodeId('insert:abc123:root:node:5') + const id = widgetId(GRAPH_ID, nodeId, 'text') + registerWidgetState(id, { type: 'text', value: 'before' }) + const missingModelSpy = vi.spyOn( + useMissingModelStore(), + 'isWidgetMissingModel' + ) + const missingMediaSpy = vi.spyOn( + useMissingMediaStore(), + 'isWidgetMissingMedia' + ) + const clearErrorSpy = vi.spyOn( + useExecutionErrorStore(), + 'clearWidgetRelatedErrors' + ) + + const [processed] = processWidgets({ widgetIds: [id], nodeId }) + + expect(processed.simplified.value).toBe('before') + expect(processed.hasError).toBe(false) + expect(missingModelSpy).not.toHaveBeenCalled() + expect(missingMediaSpy).not.toHaveBeenCalled() + + processed.updateHandler('after') + + expect(useWidgetValueStore().getWidget(id)?.value).toBe('after') + expect(clearErrorSpy).not.toHaveBeenCalled() + }) + it('applies advanced border styling to advanced widgets', () => { const id = widgetId(GRAPH_ID, toNodeId(1), 'text') registerWidgetState(id, { type: 'text', options: { advanced: true } }) diff --git a/src/renderer/extensions/vueNodes/composables/useProcessedWidgets.ts b/src/renderer/extensions/vueNodes/composables/useProcessedWidgets.ts index 8a50c9c8a4a..b519bad54a8 100644 --- a/src/renderer/extensions/vueNodes/composables/useProcessedWidgets.ts +++ b/src/renderer/extensions/vueNodes/composables/useProcessedWidgets.ts @@ -17,18 +17,14 @@ import { app } from '@/scripts/app' import { nodeTypeValidForApp } from '@/stores/appModeStore' import type { NodeState } from '@/types/nodeState' import type { WidgetId } from '@/types/widgetId' -import { - getNodeByLocatorId, - locatorIdFromState -} from '@/utils/graphTraversalUtil' +import { getNodeByState } from '@/utils/graphTraversalUtil' export { computeProcessedWidgets } export type { ProcessedWidget } function getHostNode(nodeData: NodeState): LGraphNode | null { if (!app.isGraphReady) return null - const locatorId = locatorIdFromState(nodeData, app.rootGraph.id) - return locatorId ? getNodeByLocatorId(app.rootGraph, locatorId) : null + return getNodeByState(app.rootGraph, nodeData) } export function useProcessedWidgets( diff --git a/src/utils/graphTraversalUtil.test.ts b/src/utils/graphTraversalUtil.test.ts index d9111785110..dbe5cf2f7e4 100644 --- a/src/utils/graphTraversalUtil.test.ts +++ b/src/utils/graphTraversalUtil.test.ts @@ -18,6 +18,7 @@ import { getLocalNodeIdFromExecutionId, getNodeByExecutionId, getNodeByLocatorId, + getNodeByState, getRootGraph, getSubgraphPathFromExecutionId, executionIdFromState, @@ -1100,6 +1101,27 @@ describe('graphTraversalUtil', () => { }) }) + describe('getNodeByState', () => { + it('uses graph identity to disambiguate colon-bearing node IDs', () => { + const subgraphUuid = 'a1b2c3d4-e5f6-7890-abcd-ef1234567890' + const nodeId = toNodeId(`${subgraphUuid}:node:5`) + const rootNode = createMockNode(nodeId) + const interiorNode = createMockNode(nodeId) + const subgraph = createMockSubgraph(subgraphUuid, [interiorNode]) + const graph = createMockGraph([ + rootNode, + createMockNode('456', { isSubgraph: true, subgraph }) + ]) + + expect( + getNodeByState(graph, { id: nodeId, graphId: ROOT_GRAPH_ID }) + ).toBe(rootNode) + expect( + getNodeByState(graph, { id: nodeId, graphId: subgraphUuid }) + ).toBe(interiorNode) + }) + }) + describe('getNodeByLocatorId', () => { it('should find node in root graph', () => { const nodes = [createMockNode('123'), createMockNode('456')] diff --git a/src/utils/graphTraversalUtil.ts b/src/utils/graphTraversalUtil.ts index ca84ff75002..474f044c802 100644 --- a/src/utils/graphTraversalUtil.ts +++ b/src/utils/graphTraversalUtil.ts @@ -633,6 +633,17 @@ export function executionIdFromState( return getExecutionIdByNode(rootGraph, node) ?? fallback } +export function getNodeByState( + rootGraph: LGraph, + state: Pick +): LGraphNode | null { + const graph = + state.graphId === rootGraph.id + ? rootGraph + : findSubgraphByUuid(rootGraph, state.graphId) + return graph?.getNodeById(state.id) ?? null +} + /** * Get a node by its locator ID from anywhere in the graph hierarchy. * For subgraph nodes, the format is `:` where