Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/lib/litegraph/src/LGraph.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 3 additions & 1 deletion src/lib/litegraph/src/LGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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'
Expand Down Expand Up @@ -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(
Expand All @@ -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,
Expand All @@ -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

Expand Down Expand Up @@ -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<typeof useExecutionErrorStore>
Expand All @@ -239,13 +238,15 @@ function createWidgetUpdateHandler({
options
)
}
executionErrorStore.clearWidgetRelatedErrors(
nodeExecId,
widgetName,
widgetName,
newValue,
options
)
if (nodeExecId) {
executionErrorStore.clearWidgetRelatedErrors(
nodeExecId,
widgetName,
widgetName,
newValue,
options
)
}
}
}

Expand Down Expand Up @@ -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
)
}

Expand All @@ -341,7 +340,7 @@ interface WidgetProcessingContext {
hostNode: LGraphNode | null
liveWidgets: Map<WidgetId, IBaseWidget>
slotMetadata: Map<string, WidgetSlotMetadata>
nodeExecId: NodeExecutionId
nodeExecId: NodeExecutionId | null
nodeErrors: Parameters<typeof hasWidgetError>[2]
widgetValueStore: ReturnType<typeof useWidgetValueStore>
executionErrorStore: ReturnType<typeof useExecutionErrorStore>
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -506,7 +504,9 @@ export function computeProcessedWidgets({
liveWidgets,
slotMetadata,
nodeExecId,
nodeErrors: executionErrorStore.lastNodeErrors?.[nodeExecId],
nodeErrors: nodeExecId
? executionErrorStore.lastNodeErrors?.[nodeExecId]
: undefined,
widgetValueStore,
executionErrorStore,
missingModelStore,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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 } })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
22 changes: 22 additions & 0 deletions src/utils/graphTraversalUtil.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
getLocalNodeIdFromExecutionId,
getNodeByExecutionId,
getNodeByLocatorId,
getNodeByState,
getRootGraph,
getSubgraphPathFromExecutionId,
executionIdFromState,
Expand Down Expand Up @@ -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')]
Expand Down
11 changes: 11 additions & 0 deletions src/utils/graphTraversalUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,17 @@ export function executionIdFromState(
return getExecutionIdByNode(rootGraph, node) ?? fallback
}

export function getNodeByState(
rootGraph: LGraph,
state: Pick<NodeState, 'id' | 'graphId'>
): 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 `<subgraph-definition-uuid>:<node-id>` where
Expand Down
Loading