diff --git a/eslint-suppressions.json b/eslint-suppressions.json index d4e305a77ff..519df1b3f6c 100644 --- a/eslint-suppressions.json +++ b/eslint-suppressions.json @@ -184,11 +184,6 @@ "count": 1 } }, - "src/lib/litegraph/src/subgraph/subgraphDeduplication.ts": { - "comfy/no-new-error-throw": { - "count": 1 - } - }, "src/platform/assets/services/assetService.ts": { "comfy/no-new-error-throw": { "count": 21 diff --git a/src/composables/useCopy.test.ts b/src/composables/useCopy.test.ts index 5c468bd0cfc..a2da17daf9e 100644 --- a/src/composables/useCopy.test.ts +++ b/src/composables/useCopy.test.ts @@ -64,7 +64,7 @@ function selectDocumentText(selectedCharacters: number): void { function readSerializedClipboardMetadata(dataTransfer: DataTransfer): string { const match = dataTransfer .getData('text/html') - .match(/data-metadata="([A-Za-z0-9+/=]+)"/)?.[1] + .match(/data-comfy-metadata="([A-Za-z0-9+/=]+)"/)?.[1] expect(match).toBeDefined() if (!match) throw new Error('Expected clipboard metadata to be written') diff --git a/src/composables/useCopy.ts b/src/composables/useCopy.ts index 9825791f784..91470065a2b 100644 --- a/src/composables/useCopy.ts +++ b/src/composables/useCopy.ts @@ -10,7 +10,7 @@ const CANVAS_CLIPBOARD_KEY = 'litegrapheditor_clipboard' const CANVAS_CLIPBOARD_ID_KEY = 'litegrapheditor_clipboard_id' const clipboardHTMLWrapper = [ - '
Text' ] const clipboardByteChunkSize = 0x8000 diff --git a/src/composables/usePaste.test.ts b/src/composables/usePaste.test.ts index 8027178c0c9..ac4dad62601 100644 --- a/src/composables/usePaste.test.ts +++ b/src/composables/usePaste.test.ts @@ -18,6 +18,7 @@ import type { LGraphNode } from '@/lib/litegraph/src/litegraph' import { app } from '@/scripts/app' +import { useToastStore } from '@/platform/updates/common/toastStore' import { createMockLGraphNode } from '@/utils/__tests__/litegraphTestUtils' import { createNode } from '@/utils/litegraphUtil' import { @@ -615,7 +616,34 @@ describe('usePaste', () => { }) it('should handle clipboard items with metadata', async () => { - const data = { test: 'data' } + const data = { + nodes: [], + groups: [], + reroutes: [], + links: [], + subgraphs: [] + } + const encoded = btoa(JSON.stringify(data)) + const html = `
` + + usePaste() + + const dataTransfer = new DataTransfer() + dataTransfer.setData('text/html', html) + + const event = new ClipboardEvent('paste', { clipboardData: dataTransfer }) + document.dispatchEvent(event) + + await vi.waitFor(() => { + expect(mockCanvas._deserializeItems).toHaveBeenCalledWith( + data, + expect.any(Object) + ) + }) + }) + + it('accepts validated legacy data-metadata clipboard items', async () => { + const data = { nodes: [] } const encoded = btoa(JSON.stringify(data)) const html = `
` @@ -623,6 +651,7 @@ describe('usePaste', () => { const dataTransfer = new DataTransfer() dataTransfer.setData('text/html', html) + dataTransfer.setData('text/plain', 'some text') const event = new ClipboardEvent('paste', { clipboardData: dataTransfer }) document.dispatchEvent(event) @@ -635,6 +664,88 @@ describe('usePaste', () => { }) }) + it.for([ + { name: 'null payload', data: null }, + { name: 'empty object', data: {} }, + { name: 'malformed node', data: { nodes: [{ type: 'KSampler' }] } }, + { name: 'malformed group', data: { groups: [{ id: 1 }] } }, + { name: 'malformed reroute', data: { reroutes: [{ id: 1 }] } }, + { name: 'malformed link', data: { links: [{ id: 1 }] } }, + { name: 'malformed subgraph', data: { subgraphs: [{ id: 'invalid' }] } } + ])('falls back for malformed Comfy metadata: $name', async ({ data }) => { + const encoded = btoa(JSON.stringify(data)) + const html = `
` + + usePaste() + + const dataTransfer = new DataTransfer() + dataTransfer.setData('text/html', html) + dataTransfer.setData('text/plain', 'some text') + + document.dispatchEvent( + new ClipboardEvent('paste', { clipboardData: dataTransfer }) + ) + + await vi.waitFor(() => { + expect(mockCanvas._deserializeItems).not.toHaveBeenCalled() + expect(mockCanvas.pasteFromClipboard).toHaveBeenCalled() + }) + }) + + it('falls back when Comfy metadata is not valid JSON', async () => { + const parseError = vi.spyOn(console, 'error').mockImplementation(() => {}) + onTestFinished(() => parseError.mockRestore()) + const html = `
` + + usePaste() + + const dataTransfer = new DataTransfer() + dataTransfer.setData('text/html', html) + dataTransfer.setData('text/plain', 'some text') + + document.dispatchEvent( + new ClipboardEvent('paste', { clipboardData: dataTransfer }) + ) + + await vi.waitFor(() => { + expect(parseError).toHaveBeenCalledOnce() + expect(mockCanvas._deserializeItems).not.toHaveBeenCalled() + expect(mockCanvas.pasteFromClipboard).toHaveBeenCalled() + }) + }) + + it('should toast a deserialization error without falling back', async () => { + const deserializeError = new Error('Paste failed') + vi.mocked(mockCanvas._deserializeItems).mockImplementation(() => { + throw deserializeError + }) + const data = { + nodes: [], + groups: [], + reroutes: [], + links: [], + subgraphs: [] + } + const encoded = btoa(JSON.stringify(data)) + const html = `
` + + usePaste() + + const dataTransfer = new DataTransfer() + dataTransfer.setData('text/html', html) + dataTransfer.setData('text/plain', 'some text') + + const event = new ClipboardEvent('paste', { clipboardData: dataTransfer }) + document.dispatchEvent(event) + + await vi.waitFor(() => { + expect(useToastStore().add).toHaveBeenCalledWith( + expect.objectContaining({ severity: 'error' }) + ) + expect(mockCanvas.pasteFromClipboard).not.toHaveBeenCalled() + }) + }) + it('should skip node metadata paste when a media node is selected', async () => { const mockNode = createMockLGraphNode({ is_selected: true, @@ -648,7 +759,7 @@ describe('usePaste', () => { const nodeData = { nodes: [{ type: 'KSampler' }] } const encoded = btoa(JSON.stringify(nodeData)) - const html = `
` + const html = `
` const dataTransfer = new DataTransfer() dataTransfer.setData('text/html', html) diff --git a/src/composables/usePaste.ts b/src/composables/usePaste.ts index f88cf3a35b3..1f1adbe70f9 100644 --- a/src/composables/usePaste.ts +++ b/src/composables/usePaste.ts @@ -1,6 +1,8 @@ import { useEventListener } from '@vueuse/core' +import { useErrorHandling } from '@/composables/useErrorHandling' import type { LGraphCanvas, LGraphNode } from '@/lib/litegraph/src/litegraph' +import { zClipboardItems } from '@/platform/workflow/validation/schemas/workflowSchema' import { useCanvasStore } from '@/renderer/core/canvas/canvasStore' import { app } from '@/scripts/app' import { useWorkspaceStore } from '@/stores/workspaceStore' @@ -42,19 +44,31 @@ export function cloneDataTransfer(original: DataTransfer): DataTransfer { function pasteClipboardItems(data: DataTransfer): boolean { const rawData = data.getData('text/html') - const match = rawData.match(/data-metadata="([A-Za-z0-9+/=]+)"/)?.[1] + const match = rawData.match( + /data-(?:comfy-)?metadata="([A-Za-z0-9+/=]+)"/ + )?.[1] if (!match) return false + + let parsed: unknown try { - // Decode UTF-8 safe base64 const binaryString = atob(match) const bytes = Uint8Array.from(binaryString, (c) => c.charCodeAt(0)) const decodedData = new TextDecoder().decode(bytes) - useCanvasStore().getCanvas()._deserializeItems(JSON.parse(decodedData), {}) - return true + parsed = JSON.parse(decodedData) } catch (err) { console.error(err) + return false + } + + const clipboardItems = zClipboardItems.safeParse(parsed) + if (!clipboardItems.success) return false + + try { + useCanvasStore().getCanvas()._deserializeItems(clipboardItems.data, {}) + } catch (err) { + useErrorHandling().toastErrorHandler(err) } - return false + return true } function isWorkflow( diff --git a/src/extensions/core/groupNode.ts b/src/extensions/core/groupNode.ts index d0247e942d2..f01dcb8fd54 100644 --- a/src/extensions/core/groupNode.ts +++ b/src/extensions/core/groupNode.ts @@ -8,6 +8,7 @@ import type { } from '@/lib/litegraph/src/litegraph' import { LiteGraph } from '@/lib/litegraph/src/litegraph' import { outputLinks } from '@/lib/litegraph/src/node/slotLinks' +import { slotTypeKey } from '@/lib/litegraph/src/utils/type' import type { SerializedNodeId } from '@/types/nodeId' import { parseNodeId } from '@/types/nodeId' import type { @@ -229,7 +230,9 @@ export class GroupNodeConfig { if (!linksFrom) return const firstLink = linksFrom[0]?.at(0) - let type: string | number | null = firstLink?.[5] ?? null + let type: string | number | null = firstLink + ? slotTypeKey(firstLink[5]) + : null if (type === 'COMBO') { // Use the array items const output = node.outputs?.[0] @@ -326,13 +329,14 @@ export class GroupNodeConfig { } config.forceInput = true + const rerouteTypeKey = slotTypeKey(rerouteType) return { input: { required: { - [rerouteType]: [rerouteType, config] + [rerouteTypeKey]: [rerouteTypeKey, config] } }, - output: [rerouteType], + output: [rerouteTypeKey], output_name: [], output_is_list: [] } diff --git a/src/extensions/core/rerouteNode.ts b/src/extensions/core/rerouteNode.ts index 93b24540ef4..fef9732f12f 100644 --- a/src/extensions/core/rerouteNode.ts +++ b/src/extensions/core/rerouteNode.ts @@ -6,6 +6,7 @@ import { } from '@/lib/litegraph/src/litegraph' import type { ISlotType } from '@/lib/litegraph/src/interfaces' import { outputLinks } from '@/lib/litegraph/src/node/slotLinks' +import { slotTypeKey } from '@/lib/litegraph/src/utils/type' import { app } from '../../scripts/app' import { getWidgetConfig, mergeIfValid, setWidgetConfig } from './widgetInputs' @@ -115,7 +116,7 @@ app.registerExtension({ } } - const displayType = inputType || outputType || '*' + const displayType = slotTypeKey(inputType || outputType || '*') const color = LGraphCanvas.link_type_colors[displayType] let widgetConfig diff --git a/src/extensions/core/widgetInputs.ts b/src/extensions/core/widgetInputs.ts index c8251728cff..1b071d9f270 100644 --- a/src/extensions/core/widgetInputs.ts +++ b/src/extensions/core/widgetInputs.ts @@ -13,6 +13,7 @@ import type { import type { IWidgetLocator } from '@/lib/litegraph/src/interfaces' import { NodeSlot } from '@/lib/litegraph/src/node/NodeSlot' import { outputHasLinks, outputLinks } from '@/lib/litegraph/src/node/slotLinks' +import { slotTypeKey } from '@/lib/litegraph/src/utils/type' import type { IBaseWidget } from '@/lib/litegraph/src/types/widgets' import { assetService } from '@/platform/assets/services/assetService' import { createAssetWidget } from '@/platform/assets/utils/createAssetWidget' @@ -196,7 +197,7 @@ export class PrimitiveNode extends LGraphNode { ) { // Fires before the link is made allowing us to reject it if it isn't valid // No widget, we can't connect - if (!input.widget && !(input.type in ComfyWidgets)) { + if (!input.widget && !(slotTypeKey(input.type) in ComfyWidgets)) { return false } diff --git a/src/lib/litegraph/src/LGraph.test.ts b/src/lib/litegraph/src/LGraph.test.ts index d5a29e8c355..3620379fb32 100644 --- a/src/lib/litegraph/src/LGraph.test.ts +++ b/src/lib/litegraph/src/LGraph.test.ts @@ -57,7 +57,7 @@ import { } from './__fixtures__/duplicateLinks' import { duplicateSubgraphNodeIds } from './__fixtures__/duplicateSubgraphNodeIds' import { nestedSubgraphProxyWidgets } from './__fixtures__/nestedSubgraphProxyWidgets' -import { nodeIdSpaceExhausted } from './__fixtures__/nodeIdSpaceExhausted' +import { nodeIdsAtFormerLimit } from './__fixtures__/nodeIdsAtFormerLimit' import { uniqueSubgraphNodeIds } from './__fixtures__/uniqueSubgraphNodeIds' import { test } from './__fixtures__/testExtensions' @@ -2347,11 +2347,19 @@ describe('deduplicateSubgraphNodeIds (via configure)', () => { }) }) - it('throws when node ID space is exhausted', () => { - expect(() => { - const graph = new LGraph() - graph.configure(structuredClone(nodeIdSpaceExhausted)) - }).toThrow('Node ID space exhausted') + it('remaps duplicate node IDs above the former fixed limit', () => { + const graph = new LGraph() + + graph.configure(structuredClone(nodeIdsAtFormerLimit)) + + expect(nodeIdSet(graph, SUBGRAPH_B)).toEqual( + new Set([ + toNodeId(100_000_001), + toNodeId(100_000_002), + toNodeId(100_000_003) + ]) + ) + expect(graph.state.lastNodeId).toBe(100_000_003) }) it('is a no-op when subgraph node IDs are already unique', () => { diff --git a/src/lib/litegraph/src/LGraph.ts b/src/lib/litegraph/src/LGraph.ts index 0263ab1dd5f..fae0e01fea7 100644 --- a/src/lib/litegraph/src/LGraph.ts +++ b/src/lib/litegraph/src/LGraph.ts @@ -65,6 +65,7 @@ import { useEntityIdStore } from '@/stores/entityIdStore' import { useExecutionOrderStore } from '@/stores/executionOrderStore' import { useGraphMetadataStore } from '@/stores/graphMetadataStore' import { rekeyGraphId } from '@/stores/rekeyGraphId' +import { toGroupId } from '@/types/groupId' import { UNASSIGNED_NODE_ID, compareNodeIds, @@ -186,6 +187,7 @@ import { import { collectReservedGroupIds, collectReservedLinkIds, + collectReservedNodeIds, collectReservedRerouteIds, normalizeSubgraphDefinitions, topologicalSortSubgraphs @@ -1373,7 +1375,9 @@ export class LGraph groupId === -1 || layoutStore.getGroupLayout(this.rootGraph.id, groupId) ) { - node.id = mintGroupId(state) + node.id = mintGroupId(state, () => + collectReservedGroupIds(this.rootGraph) + ) } observeGroupId(state, node.id) @@ -1405,7 +1409,16 @@ export class LGraph // give him an id if (node.id === UNASSIGNED_NODE_ID) { const mintMode = nodeIdMintModeFor(this) - node.id = mintNodeId(state, mintMode) + node.id = mintNodeId( + state, + mintMode, + () => + new Set( + [...collectReservedNodeIds(this.rootGraph)] + .map(Number) + .filter(Number.isSafeInteger) + ) + ) } else { observeNodeId(state, node.id) } @@ -1420,7 +1433,16 @@ export class LGraph node.graph = this attachNodeToStores(this, node, () => - mintNodeId(state, nodeIdMintModeFor(this)) + mintNodeId( + state, + nodeIdMintModeFor(this), + () => + new Set( + [...collectReservedNodeIds(this.rootGraph)] + .map(Number) + .filter(Number.isSafeInteger) + ) + ) ) this._nodes.push(node) @@ -1877,7 +1899,9 @@ export class LGraph addFloatingLink(link: LLink): LLink | undefined { if (link.id === -1) { - link.id = mintLinkId(this.state) + link.id = mintLinkId(this.state, () => + collectReservedLinkIds(this.rootGraph) + ) } if (!registerLinkTopology(this, link)) return @@ -1984,7 +2008,11 @@ export class LGraph floating }: OptionalProps): Reroute | undefined { const rerouteId = - id === undefined ? mintRerouteId(this.state) : toRerouteId(id) + id === undefined + ? mintRerouteId(this.state, () => + collectReservedRerouteIds(this.rootGraph) + ) + : toRerouteId(id) observeRerouteId(this.state, rerouteId) const existingReroute = this.reroutes.get(rerouteId) @@ -2118,34 +2146,27 @@ export class LGraph return this.createSubgraphs([data])[0] } - createSubgraphs(data: ExportedSubgraph[]): Subgraph[] { + createSubgraphs( + data: ExportedSubgraph[], + rootNodes?: ISerialisedNode[] + ): Subgraph[] { if (!data.length) return [] const normalized = normalizeSubgraphDefinitions( data, { - nodeIds: this.collectReservedNodeIds(), + nodeIds: collectReservedNodeIds(this.rootGraph), groupIds: collectReservedGroupIds(this.rootGraph), linkIds: collectReservedLinkIds(this.rootGraph), rerouteIds: collectReservedRerouteIds(this.rootGraph) }, - this.state - ).subgraphs - return this.createNormalizedSubgraphs(normalized) - } - - private collectReservedNodeIds( - rootNodes: ISerialisedNode[] = [] - ): Set { - const reserved = new Set() - for (const owner of [ - this.rootGraph, - ...this.rootGraph.subgraphs.values() - ]) { - for (const node of owner.nodes) reserved.add(node.id) + this.state, + rootNodes + ) + if (rootNodes && normalized.rootNodes) { + rootNodes.splice(0, rootNodes.length, ...normalized.rootNodes) } - for (const node of rootNodes) reserved.add(toNodeId(node.id)) - return reserved + return this.createNormalizedSubgraphs(normalized.subgraphs) } private createNormalizedSubgraphs(data: ExportedSubgraph[]): Subgraph[] { @@ -2703,7 +2724,9 @@ export class LGraph // Shared definitions may survive, so unpacked groups need fresh layout // ids, like the reroutes below. for (const groupInfo of groups) { - const groupId = mintGroupId(this.rootGraph.state) + const groupId = mintGroupId(this.rootGraph.state, () => + collectReservedGroupIds(this.rootGraph) + ) groupInfo.id = groupId const group = new LGraphGroup(groupInfo.title, groupId) this.add(group, true) @@ -2793,7 +2816,9 @@ export class LGraph const rerouteIdMap = new Map() const oldReroutes = subgraphNode.subgraph.reroutes for (const reroute of oldReroutes.values()) { - const migratedId = mintRerouteId(this.state) + const migratedId = mintRerouteId(this.state, () => + collectReservedRerouteIds(this.rootGraph) + ) const migratedReroute = this.setReroute({ id: migratedId, pos: [reroute.pos[0] + offsetX, reroute.pos[1] + offsetY], @@ -3134,17 +3159,13 @@ export class LGraph const runtimeLastNodeId = runtimeOptional(lastNodeId) const runtimeLastRerouteId = runtimeOptional(lastRerouteId) if (runtimeLastGroupId != null) - state.lastGroupId = Math.max(state.lastGroupId, runtimeLastGroupId) + observeGroupId(state, toGroupId(runtimeLastGroupId)) if (runtimeLastLinkId != null) - state.lastLinkId = toLinkId( - Math.max(state.lastLinkId, runtimeLastLinkId) - ) + observeLinkId(state, toLinkId(runtimeLastLinkId)) if (runtimeLastNodeId != null) - state.lastNodeId = Math.max(state.lastNodeId, runtimeLastNodeId) + observeNodeId(state, toNodeId(runtimeLastNodeId)) if (runtimeLastRerouteId != null) - state.lastRerouteId = toRerouteId( - Math.max(state.lastRerouteId, runtimeLastRerouteId) - ) + observeRerouteId(state, toRerouteId(runtimeLastRerouteId)) } // Links @@ -3190,7 +3211,7 @@ export class LGraph ? normalizeSubgraphDefinitions( subgraphs, { - nodeIds: this.collectReservedNodeIds(nodesData), + nodeIds: collectReservedNodeIds(this.rootGraph, nodesData), groupIds: collectReservedGroupIds(this, data.groups), linkIds: collectReservedLinkIds(this, data.floatingLinks), rerouteIds: collectReservedRerouteIds(this) diff --git a/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts b/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts index be08aafe128..a141af972bb 100644 --- a/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts +++ b/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts @@ -27,7 +27,7 @@ import { SubgraphNode, createUuidv4 } from '@/lib/litegraph/src/litegraph' -import { remapClipboardSubgraphNodeIds } from '@/lib/litegraph/src/LGraphCanvas' +import { toLinkId } from '@/types/linkId' import { toNodeId } from '@/types/nodeId' import type { ClipboardItems, @@ -63,138 +63,67 @@ function createSerialisedNode( } } -describe('remapClipboardSubgraphNodeIds', () => { - it('remaps pasted subgraph interior IDs and proxyWidgets references', () => { +describe('clipboard ID allocation', () => { + it('wraps at the safe-integer boundary without mutating clipboard data', () => { + const nodeType = 'test/clipboard-id-wrap' + registerClipboardNodeType(nodeType) const rootGraph = new LGraph() - const existingNode = new LGraphNode('existing') - existingNode.id = toNodeId(1) - rootGraph.add(existingNode) - - const subgraphId = createUuidv4() - const pastedSubgraph: ExportedSubgraph = { - id: subgraphId, - version: 1, - revision: 0, - state: { - lastNodeId: 0, - lastLinkId: 0, - lastGroupId: 0, - lastRerouteId: 0 - }, - config: {}, - name: 'Pasted Subgraph', - inputNode: { - id: SUBGRAPH_INPUT_ID, - bounding: [0, 0, 10, 10] - }, - outputNode: { - id: SUBGRAPH_OUTPUT_ID, - bounding: [0, 0, 10, 10] - }, - inputs: [], - outputs: [], - widgets: [], - nodes: [createSerialisedNode(1, 'test/node')], + rootGraph.state.lastNodeId = Number.MAX_SAFE_INTEGER + rootGraph.state.lastGroupId = Number.MAX_SAFE_INTEGER + rootGraph.state.lastLinkId = toLinkId(Number.MAX_SAFE_INTEGER) + rootGraph.state.lastRerouteId = toRerouteId(Number.MAX_SAFE_INTEGER) + const canvas = createCanvas(rootGraph) + const parsed: ClipboardItems = { + nodes: [ + createSerialisedNode(7, nodeType), + createSerialisedNode(8, nodeType) + ], + groups: [{ id: 7, title: 'Group', bounding: [0, 0, 100, 100] }], links: [ { - id: 1, - type: '*', - origin_id: 1, + id: 7, + origin_id: 7, origin_slot: 0, - target_id: 1, - target_slot: 0 + target_id: 8, + target_slot: 0, + type: '*', + parentId: 7 } ], - groups: [] + reroutes: [{ id: 7, pos: [0, 0], linkIds: [7] }] } + const original = structuredClone(parsed) - const parsed: ClipboardItems = { - nodes: [createSerialisedNode(99, subgraphId, [['1', 'seed']])], - groups: [], - reroutes: [], - links: [], - subgraphs: [pastedSubgraph] - } - - remapClipboardSubgraphNodeIds(parsed, rootGraph) + const result = canvas._deserializeItems(parsed, {}) - const remappedSubgraph = parsed.subgraphs?.[0] - expect(remappedSubgraph).toBeDefined() - - const remappedLink = remappedSubgraph?.links?.[0] - expect(remappedLink).toBeDefined() - - const remappedInteriorId = remappedSubgraph?.nodes?.[0]?.id - expect(remappedInteriorId).not.toBe(1) - expect(remappedLink?.origin_id).toBe(remappedInteriorId) - expect(remappedLink?.target_id).toBe(remappedInteriorId) - - const remappedNode = parsed.nodes?.[0] - expect(remappedNode).toBeDefined() - expect(remappedNode?.properties?.proxyWidgets).toStrictEqual([ - [String(remappedInteriorId), 'seed'] + expect(parsed).toEqual(original) + expect(result?.created).toHaveLength(4) + expect(rootGraph.nodes.map((node) => node.id)).toEqual([ + toNodeId(1), + toNodeId(2) ]) + expect(rootGraph.groups[0].id).toBe(1) + expect([...rootGraph.links.keys()]).toEqual([toLinkId(1)]) + expect([...rootGraph.reroutes.keys()]).toEqual([toRerouteId(1)]) }) - it('remaps pasted SubgraphNode previewExposures sourceNodeId references', () => { + it('emits one complete canvas change pair for public paste', () => { + const nodeType = 'test/clipboard-change-hooks' + registerClipboardNodeType(nodeType) const rootGraph = new LGraph() - const existingNode = new LGraphNode('existing') - existingNode.id = toNodeId(1) - rootGraph.add(existingNode) - - const subgraphId = createUuidv4() - const pastedSubgraph: ExportedSubgraph = { - id: subgraphId, - version: 1, - revision: 0, - state: { - lastNodeId: 0, - lastLinkId: 0, - lastGroupId: 0, - lastRerouteId: 0 - }, - config: {}, - name: 'Pasted Subgraph', - inputNode: { id: SUBGRAPH_INPUT_ID, bounding: [0, 0, 10, 10] }, - outputNode: { id: SUBGRAPH_OUTPUT_ID, bounding: [0, 0, 10, 10] }, - inputs: [], - outputs: [], - widgets: [], - nodes: [createSerialisedNode(1, 'test/node')], - links: [], - groups: [] - } - - const hostInfo = createSerialisedNode(99, subgraphId) - hostInfo.properties = { - previewExposures: [ - { - name: '$$canvas-image-preview', - sourceNodeId: '1', - sourcePreviewName: '$$canvas-image-preview' - } - ] - } - - const parsed: ClipboardItems = { - nodes: [hostInfo], - groups: [], - reroutes: [], - links: [], - subgraphs: [pastedSubgraph] - } + const canvas = createCanvas(rootGraph) + const before = vi.spyOn(canvas, 'emitBeforeChange') + const after = vi.spyOn(canvas, 'emitAfterChange') + localStorage.setItem( + 'litegrapheditor_clipboard', + JSON.stringify({ nodes: [createSerialisedNode(1, nodeType)] }) + ) + onTestFinished(() => localStorage.removeItem('litegrapheditor_clipboard')) - remapClipboardSubgraphNodeIds(parsed, rootGraph) + canvas.pasteFromClipboard() - const remappedInteriorId = parsed.subgraphs?.[0]?.nodes?.[0]?.id - expect(remappedInteriorId).not.toBe(1) - expect(parsed.nodes?.[0]?.properties?.previewExposures).toStrictEqual([ - { - name: '$$canvas-image-preview', - sourceNodeId: String(remappedInteriorId), - sourcePreviewName: '$$canvas-image-preview' - } - ]) + expect(before).toHaveBeenCalledOnce() + expect(after).toHaveBeenCalledOnce() }) }) diff --git a/src/lib/litegraph/src/LGraphCanvas.ts b/src/lib/litegraph/src/LGraphCanvas.ts index 38aa610f504..3691db9e0fb 100644 --- a/src/lib/litegraph/src/LGraphCanvas.ts +++ b/src/lib/litegraph/src/LGraphCanvas.ts @@ -48,14 +48,13 @@ import type { ContextMenu } from './ContextMenu' import { createCursorCache } from './cursorCache' import { DragAndScale } from './DragAndScale' import type { AnimationOptions } from './DragAndScale' -import { mintNodeId, observeNodeId } from './idAllocation' import type { LGraph, SubgraphId } from './LGraph' import { LGraphGroup } from './LGraphGroup' import type { SlotTypeDefaultNodeOpts } from './LiteGraphGlobal' import { LGraphNode } from './LGraphNode' import type { NodeProperty } from './LGraphNode' import { detachSerialisedLinks } from './linkDeduplication' -import { parseNodeId, serializeNodeId, toNodeId } from '@/types/nodeId' +import { parseNodeId, serializeNodeId } from '@/types/nodeId' import type { SerializedNodeId } from '@/types/nodeId' import { LLink, slotFloatingLinks } from './LLink' import { inputHasLink, nodeLinkIds, outputLinks } from './node/slotLinks' @@ -169,6 +168,7 @@ import type { IBaseWidget, TWidgetValue } from './types/widgets' import { alignNodes, distributeNodes, getBoundaryNodes } from './utils/arrange' import { findFirstNode, getDraggedItems } from './utils/collections' import { resolveConnectingLinkColor } from './utils/linkColors' +import { slotTypeKey } from './utils/type' import { createUuidv4 } from '@/utils/uuid' import { BaseWidget } from './widgets/BaseWidget' import { toConcreteWidget } from './widgets/widgetMap' @@ -287,6 +287,20 @@ interface ClipboardPasteResult { subgraphs: Map } +type InitializedClipboardItems = { + [Key in keyof ClipboardItems]-?: NonNullable +} + +interface ClipboardPasteContext { + connectInputs: boolean + dx: number + dy: number + graph: LGraph + items: InitializedClipboardItems + result: ClipboardPasteResult + targetSlotByLink: Map +} + /** Legacy selection views derived from the selection store. */ interface SelectionView { items: SelectedItemsView @@ -4257,7 +4271,7 @@ export class LGraphCanvas implements CustomEventDispatcher } _deserializeItems( - parsed: ClipboardItems, + clipboardItems: ClipboardItems, options: IPasteFromClipboardOptions ): ClipboardPasteResult | undefined { const { connectInputs = false, position = this.graph_mouse } = options @@ -4274,231 +4288,45 @@ export class LGraphCanvas implements CustomEventDispatcher graph.beforeChange() this.emitBeforeChange() - // Parse & initialise - parsed.nodes ??= [] - parsed.groups ??= [] - parsed.reroutes ??= [] - parsed.links ??= [] - parsed.subgraphs ??= [] - - // Find top-left-most boundary - let offsetX = Infinity - let offsetY = Infinity - for (const item of [...parsed.nodes, ...parsed.reroutes]) { - if (item.pos[0] < offsetX) offsetX = item.pos[0] - if (item.pos[1] < offsetY) offsetY = item.pos[1] - } - - // TODO: Remove when implementing `asSerialisable` - for (const group of parsed.groups) { - if (group.bounding[0] < offsetX) offsetX = group.bounding[0] - if (group.bounding[1] < offsetY) offsetY = group.bounding[1] - } - - const results: ClipboardPasteResult = { - created: [], - nodes: new Map(), - links: new Map(), - reroutes: new Map(), - subgraphs: new Map() - } - const { created, nodes, links, reroutes } = results - - // const failedNodes: ISerialisedNode[] = [] - const subgraphIdMap: Record = {} - // SubgraphV2: Remove always-clone behaviour - //Update subgraph ids - for (const subgraphInfo of parsed.subgraphs) - subgraphInfo.id = subgraphIdMap[subgraphInfo.id] = createUuidv4() - const allNodeInfo: ISerialisedNode[] = [ - [parsed.nodes], - parsed.subgraphs.map((s) => s.nodes ?? []) - ].flat(2) - for (const nodeInfo of allNodeInfo) - if (nodeInfo.type in subgraphIdMap) - nodeInfo.type = subgraphIdMap[nodeInfo.type] - remapClipboardSubgraphNodeIds(parsed, graph.rootGraph) - // Subgraphs - const subgraphs = graph.createSubgraphs(parsed.subgraphs) - for (const subgraph of subgraphs) - results.subgraphs.set(subgraph.id, subgraph) - - // Groups - for (const info of parsed.groups) { - info.id = -1 - - const group = new LGraphGroup() - group.configure(info) - graph.add(group) - created.push(group) - } - - // Nodes - const dx = position[0] - offsetX - const dy = position[1] - offsetY - const targetSlotByLink = new Map() - for (const info of parsed.nodes) { - const node = LiteGraph.createNode(info.type) - if (!node) { - // failedNodes.push(info) - continue - } - - nodes.set(serializeNodeId(info.id), node) - info.id = -1 - - const linkByInputName = detachSerialisedLinks(info) - // `add` snapshots the position into the layout store; configure runs after. - node.pos = [info.pos[0] + dx, info.pos[1] + dy] - graph.add(node) - node.configure(info) - - // `configure` overrides may reorder inputs to match the node definition, - // moving the slot each serialized link targets. - for (const [slot, input] of (info.inputs ?? []).entries()) { - const linkId = linkByInputName.get(input.name) - if (linkId != null && !targetSlotByLink.has(linkId)) - targetSlotByLink.set(linkId, slot) - } - - if (node instanceof SubgraphNode) { - if ( - node.properties.proxyWidgets !== undefined && - LiteGraph.LGraph.proxyWidgetMigrationFlush - ) { - LiteGraph.LGraph.proxyWidgetMigrationFlush(node, info) - } - LiteGraph.LGraph.autoExposePreviewNodes?.(node) - } - - created.push(node) - } - - // Reroutes - for (const info of parsed.reroutes) { - const { id, ...rerouteInfo } = info - - const reroute = graph.setReroute(rerouteInfo) - if (!reroute) continue - created.push(reroute) - reroutes.set(toRerouteId(id), reroute) - } - - // Remap reroute parentIds for pasted reroutes - for (const reroute of reroutes.values()) { - if (reroute.parentId == null) continue - - const mapped = reroutes.get(reroute.parentId) - if (mapped) reroute.parentId = mapped.id - } - - // Links - for (const info of parsed.links) { - // Find the copied node / reroute ID - let outNode: LGraphNode | null | undefined = nodes.get( - serializeNodeId(info.origin_id) - ) - let afterRerouteId: RerouteId | undefined - if (info.parentId != null) - afterRerouteId = reroutes.get(toRerouteId(info.parentId))?.id - - // If it wasn't copied, use the original graph value - if ( - connectInputs && - LiteGraph.ctrl_shift_v_paste_connect_unselected_outputs - ) { - const originNodeId = parseNodeId(info.origin_id) - outNode ??= originNodeId ? graph.getNodeById(originNodeId) : null - if (info.parentId !== undefined) { - afterRerouteId ??= toRerouteId(info.parentId) - } - } - - const inNode = nodes.get(serializeNodeId(info.target_id)) - if (inNode) { - const link = outNode?.connect( - info.origin_slot, - inNode, - targetSlotByLink.get(toLinkId(info.id)) ?? info.target_slot, - afterRerouteId - ) - if (link) { - transferLinkPresentation( - graphScopeOf(graph), - { - hidden: info.hidden === true, - label: typeof info.label === 'string' ? info.label : undefined - }, - link.id - ) - links.set(toLinkId(info.id), link) - } - } - } - - // Remove reroutes that no pasted link passes through - for (const [sourceId, reroute] of reroutes) { - if (reroute.totalLinks === 0) { - graph.removeReroute(reroute.id) - reroutes.delete(sourceId) - - const index = created.indexOf(reroute) - if (index !== -1) created.splice(index, 1) - } - } - - // Children of pasted groups are in `created` already, so skip them here. - for (const item of created) { - // Repositioning a paste is not a user drag, so it ignores the pin. - if (item instanceof LGraphNode) - item.setPos(item.pos[0] + dx, item.pos[1] + dy) - else item.move(dx, dy, true) - } - - // TODO: Report failures, i.e. `failedNodes` - - const newPositions = created - .filter((item): item is LGraphNode => item instanceof LGraphNode) - .map((node) => ({ - nodeId: node.id, - bounds: { - x: node.pos[0], - y: node.pos[1], - width: node.size[0], - height: node.size[1] - } - })) - - const rootGraphId = graph.rootGraph.id - layoutStore.batchUpdateNodeBounds(rootGraphId, newPositions, { - source: LayoutSource.Canvas - }) - - // Bring cloned/pasted nodes to front so they render above the originals - const { setNodeZIndex } = useLayoutMutations(LayoutSource.Canvas) - for (const { nodeId } of newPositions) { - setNodeZIndex(rootGraphId, nodeId, layoutStore.allocateZIndex()) - } - - this.selectItems(created) - forEachNode(graph, (n) => n.onGraphConfigured?.()) - forEachNode(graph, (n) => n.onAfterGraphConfigured?.()) - - graph.afterChange() - this.emitAfterChange() - - return results - } - - pasteFromClipboard(options: IPasteFromClipboardOptions = {}): void { - this.emitBeforeChange() try { - this._pasteFromClipboard(options) + const items = initializeClipboardItems(clipboardItems) + const [offsetX, offsetY] = clipboardOffset(items) + const result = createClipboardPasteResult() + const context: ClipboardPasteContext = { + connectInputs, + dx: position[0] - offsetX, + dy: position[1] - offsetY, + graph, + items, + result, + targetSlotByLink: new Map() + } + + remapClipboardSubgraphIds(items) + createClipboardSubgraphs(context) + createClipboardGroups(context) + createClipboardNodes(context) + createClipboardReroutes(context) + createClipboardLinks(context) + removeUnusedClipboardReroutes(context) + positionClipboardItems(context) + updateClipboardNodeLayout(context) + + this.selectItems(result.created) + forEachNode(graph, (n) => n.onGraphConfigured?.()) + forEachNode(graph, (n) => n.onAfterGraphConfigured?.()) + + return result } finally { + graph.afterChange() this.emitAfterChange() } } + pasteFromClipboard(options: IPasteFromClipboardOptions = {}): void { + this._pasteFromClipboard(options) + } + processNodeDblClicked(n: LGraphNode): void { this.onShowNodePanel?.(n) this.onNodeDblClicked?.(n) @@ -6388,7 +6216,7 @@ export class LGraphCanvas implements CustomEventDispatcher visibleReroutes.push(reroute) reroute._colour = link.color || - LGraphCanvas.link_type_colors[link.type] || + LGraphCanvas.link_type_colors[slotTypeKey(link.type)] || this.default_link_color } @@ -6901,7 +6729,8 @@ export class LGraphCanvas implements CustomEventDispatcher } // check for defaults nodes for this slottype - const fromSlotType = slotX.type == LiteGraph.EVENT ? '_event_' : slotX.type + const fromSlotType = + slotX.type == LiteGraph.EVENT ? '_event_' : slotTypeKey(slotX.type) const slotTypesDefault = isFrom ? LiteGraph.slot_types_default_out : LiteGraph.slot_types_default_in @@ -7094,7 +6923,8 @@ export class LGraphCanvas implements CustomEventDispatcher } // get defaults nodes for this slottype - const fromSlotType = slotX.type == LiteGraph.EVENT ? '_event_' : slotX.type + const fromSlotType = + slotX.type == LiteGraph.EVENT ? '_event_' : slotTypeKey(slotX.type) const slotTypesDefault = isFrom ? LiteGraph.slot_types_default_out : LiteGraph.slot_types_default_in @@ -8964,157 +8794,255 @@ export class LGraphCanvas implements CustomEventDispatcher } } -export interface LGraphCanvas { - /** @deprecated Use {@link LGraphCanvas.applyNodePositions} instead. */ - repositionNodesVueMode(positions: NewNodePosition[]): void +function initializeClipboardItems( + clipboardItems: ClipboardItems +): InitializedClipboardItems { + const items = structuredClone(clipboardItems) + return { + groups: items.groups ?? [], + links: items.links ?? [], + nodes: items.nodes ?? [], + reroutes: items.reroutes ?? [], + subgraphs: items.subgraphs ?? [] + } } -defineDeprecatedProperty( - LGraphCanvas.prototype, - 'repositionNodesVueMode', - 'applyNodePositions', - 'LGraphCanvas.repositionNodesVueMode is deprecated. Use applyNodePositions instead.' -) - -function patchLinkNodeIds( - links: - | { origin_id: SerializedNodeId; target_id: SerializedNodeId }[] - | undefined, - remappedIds: Map -) { - if (!links?.length) return - - for (const link of links) { - const newOriginId = remappedIds.get(link.origin_id) - if (newOriginId !== undefined) link.origin_id = newOriginId - - const newTargetId = remappedIds.get(link.target_id) - if (newTargetId !== undefined) link.target_id = newTargetId +function clipboardOffset(items: InitializedClipboardItems): Point { + let x = Infinity + let y = Infinity + for (const item of [...items.nodes, ...items.reroutes]) { + x = Math.min(x, item.pos[0]) + y = Math.min(y, item.pos[1]) + } + for (const group of items.groups) { + x = Math.min(x, group.bounding[0]) + y = Math.min(y, group.bounding[1]) } + return [x, y] } -function remapNodeId( - nodeId: string, - remappedIds: Map -): SerializedNodeId | undefined { - const directMatch = remappedIds.get(nodeId) - if (directMatch !== undefined) return directMatch - if (!/^-?\d+$/.test(nodeId)) return undefined +function createClipboardPasteResult(): ClipboardPasteResult { + return { + created: [], + links: new Map(), + nodes: new Map(), + reroutes: new Map(), + subgraphs: new Map() + } +} - const numericId = Number(nodeId) - if (!Number.isSafeInteger(numericId)) return undefined +function remapClipboardSubgraphIds(items: InitializedClipboardItems): void { + const remappedIds = new Map() + for (const subgraph of items.subgraphs) { + const nextId = createUuidv4() + remappedIds.set(subgraph.id, nextId) + subgraph.id = nextId + } - return remappedIds.get(numericId) + const allNodes = [ + ...items.nodes, + ...items.subgraphs.flatMap((subgraph) => subgraph.nodes ?? []) + ] + for (const node of allNodes) { + const nextType = remappedIds.get(node.type) + if (nextType) node.type = nextType + } } -function remapProxyWidgets( - info: ISerialisedNode, - remappedIds: Map | undefined -) { - if (!remappedIds || remappedIds.size === 0) return +function createClipboardSubgraphs(context: ClipboardPasteContext): void { + const { graph, items, result } = context + for (const subgraph of graph.createSubgraphs(items.subgraphs, items.nodes)) { + result.subgraphs.set(subgraph.id, subgraph) + } +} - const proxyWidgets = info.properties?.proxyWidgets - if (!Array.isArray(proxyWidgets)) return +function createClipboardGroups(context: ClipboardPasteContext): void { + const { graph, items, result } = context + for (const info of items.groups) { + info.id = -1 + const group = new LGraphGroup() + group.configure(info) + graph.add(group) + result.created.push(group) + } +} - for (const entry of proxyWidgets) { - if (!Array.isArray(entry)) continue +function createClipboardNodes(context: ClipboardPasteContext): void { + const { dx, dy, graph, items, result, targetSlotByLink } = context + for (const info of items.nodes) { + const node = LiteGraph.createNode(info.type) + if (!node) continue - const [nodeId] = entry - if (typeof nodeId !== 'string' || nodeId === '-1') continue + result.nodes.set(serializeNodeId(info.id), node) + info.id = -1 + const linkByInputName = detachSerialisedLinks(info) + node.pos = [info.pos[0] + dx, info.pos[1] + dy] + graph.add(node) + node.configure(info) - const remappedNodeId = remapNodeId(nodeId, remappedIds) - if (remappedNodeId !== undefined) entry[0] = String(remappedNodeId) + recordClipboardTargetSlots(info, linkByInputName, targetSlotByLink) + configurePastedSubgraphNode(node, info) + result.created.push(node) } } -function hasStringSourceNodeId( - value: unknown -): value is { sourceNodeId: string } { - return ( - typeof value === 'object' && - value !== null && - 'sourceNodeId' in value && - typeof value.sourceNodeId === 'string' - ) +function recordClipboardTargetSlots( + info: ISerialisedNode, + linkByInputName: Map, + targetSlotByLink: Map +): void { + for (const [slot, input] of (info.inputs ?? []).entries()) { + const linkId = linkByInputName.get(input.name) + if (linkId != null && !targetSlotByLink.has(linkId)) { + targetSlotByLink.set(linkId, slot) + } + } } -function remapPreviewExposures( - info: ISerialisedNode, - remappedIds: Map | undefined -) { - if (!remappedIds || remappedIds.size === 0) return +function configurePastedSubgraphNode( + node: LGraphNode, + info: ISerialisedNode +): void { + if (!(node instanceof SubgraphNode)) return - const previewExposures = info.properties?.previewExposures - if (!Array.isArray(previewExposures)) return + const flushProxyWidgets = LiteGraph.LGraph.proxyWidgetMigrationFlush + if (node.properties.proxyWidgets !== undefined && flushProxyWidgets) { + flushProxyWidgets(node, info) + } + LiteGraph.LGraph.autoExposePreviewNodes?.(node) +} - for (const entry of previewExposures) { - if (!hasStringSourceNodeId(entry) || entry.sourceNodeId === '-1') continue +function createClipboardReroutes(context: ClipboardPasteContext): void { + const { graph, items, result } = context + for (const info of items.reroutes) { + const { id, ...rerouteInfo } = info + const reroute = graph.setReroute(rerouteInfo) + if (!reroute) continue + result.created.push(reroute) + result.reroutes.set(toRerouteId(id), reroute) + } - const remappedNodeId = remapNodeId(entry.sourceNodeId, remappedIds) - if (remappedNodeId !== undefined) - entry.sourceNodeId = String(remappedNodeId) + for (const reroute of result.reroutes.values()) { + if (reroute.parentId == null) continue + const parent = result.reroutes.get(reroute.parentId) + if (parent) reroute.parentId = parent.id } } -export function remapClipboardSubgraphNodeIds( - parsed: ClipboardItems, - rootGraph: LGraph +function createClipboardLinks(context: ClipboardPasteContext): void { + for (const info of context.items.links) createClipboardLink(context, info) +} + +function createClipboardLink( + context: ClipboardPasteContext, + info: InitializedClipboardItems['links'][number] ): void { - const usedNodeIds = new Set() - forEachNode(rootGraph, (node) => { - const numericId = Number(node.id) - if (!Number.isInteger(numericId)) return - usedNodeIds.add(numericId) - observeNodeId(rootGraph.state, toNodeId(numericId)) - }) + const { graph, result, targetSlotByLink } = context + const targetNode = result.nodes.get(serializeNodeId(info.target_id)) + if (!targetNode) return + + const [originNode, afterRerouteId] = clipboardLinkOrigin(context, info) + const link = originNode?.connect( + info.origin_slot, + targetNode, + targetSlotByLink.get(toLinkId(info.id)) ?? info.target_slot, + afterRerouteId + ) + if (!link) return + + transferLinkPresentation( + graphScopeOf(graph), + { + hidden: info.hidden === true, + label: typeof info.label === 'string' ? info.label : undefined + }, + link.id + ) + result.links.set(toLinkId(info.id), link) +} - function nextUniqueNodeId() { - let nextId = Number(mintNodeId(rootGraph.state)) - while (usedNodeIds.has(nextId)) { - nextId = Number(mintNodeId(rootGraph.state)) +function clipboardLinkOrigin( + context: ClipboardPasteContext, + info: InitializedClipboardItems['links'][number] +): [LGraphNode | null | undefined, RerouteId | undefined] { + const { connectInputs, graph, result } = context + let originNode: LGraphNode | null | undefined = result.nodes.get( + serializeNodeId(info.origin_id) + ) + let afterRerouteId = + info.parentId == null + ? undefined + : result.reroutes.get(toRerouteId(info.parentId))?.id + + if ( + connectInputs && + LiteGraph.ctrl_shift_v_paste_connect_unselected_outputs + ) { + const originNodeId = parseNodeId(info.origin_id) + originNode ??= originNodeId ? graph.getNodeById(originNodeId) : null + if (info.parentId !== undefined) { + afterRerouteId ??= toRerouteId(info.parentId) } - usedNodeIds.add(nextId) - return nextId } - const subgraphNodeIdMap = new Map< - SubgraphId, - Map - >() - for (const subgraphInfo of parsed.subgraphs ?? []) { - const remappedIds = new Map() - const interiorNodes = subgraphInfo.nodes ?? [] - - for (const nodeInfo of interiorNodes) { - if (typeof nodeInfo.id !== 'number') continue + return [originNode, afterRerouteId] +} - if (usedNodeIds.has(nodeInfo.id)) { - const oldId = nodeInfo.id - const newId = nextUniqueNodeId() - remappedIds.set(oldId, newId) - nodeInfo.id = newId - continue - } +function removeUnusedClipboardReroutes(context: ClipboardPasteContext): void { + const { graph, result } = context + for (const [sourceId, reroute] of result.reroutes) { + if (reroute.totalLinks !== 0) continue - usedNodeIds.add(nodeInfo.id) - observeNodeId(rootGraph.state, toNodeId(nodeInfo.id)) - } + graph.removeReroute(reroute.id) + result.reroutes.delete(sourceId) + const index = result.created.indexOf(reroute) + if (index !== -1) result.created.splice(index, 1) + } +} - if (remappedIds.size > 0) { - patchLinkNodeIds(subgraphInfo.links, remappedIds) - subgraphNodeIdMap.set(subgraphInfo.id, remappedIds) +function positionClipboardItems(context: ClipboardPasteContext): void { + const { dx, dy, result } = context + for (const item of result.created) { + if (item instanceof LGraphNode) { + item.setPos(item.pos[0] + dx, item.pos[1] + dy) + } else { + item.move(dx, dy, true) } } +} - const allNodeInfo: ISerialisedNode[] = [ - parsed.nodes ? [parsed.nodes] : [], - parsed.subgraphs ? parsed.subgraphs.map((s) => s.nodes ?? []) : [] - ].flat(2) - - for (const nodeInfo of allNodeInfo) { - if (typeof nodeInfo.type !== 'string') continue - const remappedIds = subgraphNodeIdMap.get(nodeInfo.type) - remapProxyWidgets(nodeInfo, remappedIds) - remapPreviewExposures(nodeInfo, remappedIds) +function updateClipboardNodeLayout(context: ClipboardPasteContext): void { + const { graph, result } = context + const positions = result.created + .filter((item): item is LGraphNode => item instanceof LGraphNode) + .map((node) => ({ + nodeId: node.id, + bounds: { + x: node.pos[0], + y: node.pos[1], + width: node.size[0], + height: node.size[1] + } + })) + + const rootGraphId = graph.rootGraph.id + layoutStore.batchUpdateNodeBounds(rootGraphId, positions, { + source: LayoutSource.Canvas + }) + const { setNodeZIndex } = useLayoutMutations(LayoutSource.Canvas) + for (const { nodeId } of positions) { + setNodeZIndex(rootGraphId, nodeId, layoutStore.allocateZIndex()) } } + +export interface LGraphCanvas { + /** @deprecated Use {@link LGraphCanvas.applyNodePositions} instead. */ + repositionNodesVueMode(positions: NewNodePosition[]): void +} + +defineDeprecatedProperty( + LGraphCanvas.prototype, + 'repositionNodesVueMode', + 'applyNodePositions', + 'LGraphCanvas.repositionNodesVueMode is deprecated. Use applyNodePositions instead.' +) diff --git a/src/lib/litegraph/src/LGraphNode.ts b/src/lib/litegraph/src/LGraphNode.ts index 3d8b78f87aa..98f24c63527 100644 --- a/src/lib/litegraph/src/LGraphNode.ts +++ b/src/lib/litegraph/src/LGraphNode.ts @@ -75,6 +75,7 @@ import { resolveInputSlotView } from './node/slotDescriptorView' import { initializeWidgetsView } from './node/widgetsView' +import { collectReservedLinkIds } from './subgraph/subgraphDeduplication' import { extensionConfigureView, hydrateExtensionPayload, @@ -3063,7 +3064,7 @@ export class LGraphNode connect( slot: number | string, target_node: LGraphNode | number | null, - target_slot: ISlotType, + target_slot: number | string, afterRerouteId?: RerouteId ): LLink | null { // Allow legacy API support for searching target_slot by string, without mutating the input variables @@ -3225,7 +3226,9 @@ export class LGraphNode const maybeCommonType = input.type && output.type && commonType(input.type, output.type) - const linkId = mintLinkId(graph.state) + const linkId = mintLinkId(graph.state, () => + collectReservedLinkIds(graph.rootGraph) + ) const link = new LLink( linkId, diff --git a/src/lib/litegraph/src/__fixtures__/nodeIdSpaceExhausted.ts b/src/lib/litegraph/src/__fixtures__/nodeIdsAtFormerLimit.ts similarity index 90% rename from src/lib/litegraph/src/__fixtures__/nodeIdSpaceExhausted.ts rename to src/lib/litegraph/src/__fixtures__/nodeIdsAtFormerLimit.ts index b099bbf4abe..b95b110d5a8 100644 --- a/src/lib/litegraph/src/__fixtures__/nodeIdSpaceExhausted.ts +++ b/src/lib/litegraph/src/__fixtures__/nodeIdsAtFormerLimit.ts @@ -4,15 +4,7 @@ import { } from '@/lib/litegraph/src/constants' import type { SerialisableGraph } from '@/lib/litegraph/src/types/serialisation' -/** - * Workflow where lastNodeId is near the MAX_NODE_ID ceiling (100_000_000) - * and root node 100_000_000 reserves the only remaining candidate ID. - * - * Both subgraph definitions share node IDs [3, 8, 37]. When SubgraphB's - * duplicates need remapping, candidate 100_000_000 is already reserved, - * so the next candidate (100_000_001) exceeds MAX_NODE_ID and must throw. - */ -export const nodeIdSpaceExhausted = { +export const nodeIdsAtFormerLimit = { id: 'cccccccc-cccc-4ccc-8ccc-cccccccccccc', version: 1, revision: 0, diff --git a/src/lib/litegraph/src/canvas/linkBadgeRenderer.ts b/src/lib/litegraph/src/canvas/linkBadgeRenderer.ts index 40fc7720555..1ebe6d6ae0b 100644 --- a/src/lib/litegraph/src/canvas/linkBadgeRenderer.ts +++ b/src/lib/litegraph/src/canvas/linkBadgeRenderer.ts @@ -7,6 +7,7 @@ import { compareNodeIds } from '@/types/nodeId' import { useLinkPresentationStore } from '@/stores/linkPresentationStore' import { layoutHiddenLinkBadges, queryLinkBadgeAtPoint } from './linkBadges' import { getLinkEndpointPositions } from './linkGeometry' +import { slotTypeKey } from '../utils/type' export function queryHiddenLinkBadgeAtPoint( host: object, @@ -63,7 +64,7 @@ export function layoutGraphLinkBadges( startPos, endPos, (typeof link.color === 'string' && link.color) || - linkTypeColors[link.type] || + linkTypeColors[slotTypeKey(link.type)] || defaultLinkColor ) ) diff --git a/src/lib/litegraph/src/canvas/linkBadges.test.ts b/src/lib/litegraph/src/canvas/linkBadges.test.ts index 99eaaa87213..127b515ed6e 100644 --- a/src/lib/litegraph/src/canvas/linkBadges.test.ts +++ b/src/lib/litegraph/src/canvas/linkBadges.test.ts @@ -57,6 +57,7 @@ describe('linkBadgeText', () => { expected: 'Checkpoint' }, { type: 'MODEL', presentation: {}, expected: 'MODEL' }, + { type: ['IMAGE', 'MASK'], presentation: {}, expected: 'IMAGE,MASK' }, { type: '', presentation: {}, expected: '*' }, { type: -1, presentation: {}, expected: '*' } ] satisfies { diff --git a/src/lib/litegraph/src/canvas/linkBadges.ts b/src/lib/litegraph/src/canvas/linkBadges.ts index 3e72d8c5eff..ec1141cae68 100644 --- a/src/lib/litegraph/src/canvas/linkBadges.ts +++ b/src/lib/litegraph/src/canvas/linkBadges.ts @@ -6,6 +6,7 @@ import type { LLink } from '../LLink' import type { LinkId } from '@/types/linkId' import type { LinkPresentation } from '@/types/linkPresentation' import { overlapBounding } from '../measure' +import { slotTypeKey } from '../utils/type' export const BADGE_GAP = 14 const BADGE_HEIGHT = 18 @@ -57,7 +58,7 @@ export function linkBadgeText( const label = presentation.label?.trim() if (label) return label if (typeof type === 'number') return '*' - return type || '*' + return String(slotTypeKey(type) || '*') } function makeBadge(text: string, color: string): LGraphBadge { diff --git a/src/lib/litegraph/src/idAllocation.property.test.ts b/src/lib/litegraph/src/idAllocation.property.test.ts index 96a115509fa..8492e43bac6 100644 --- a/src/lib/litegraph/src/idAllocation.property.test.ts +++ b/src/lib/litegraph/src/idAllocation.property.test.ts @@ -53,10 +53,10 @@ describe('idAllocation has no collision avoidance against a concurrent external agent.lastNodeId = lastObservedId const frontendIds = Array.from({ length: mintCount }, () => - Number(mintNodeId(frontend)) + Number(mintNodeId(frontend, 'sequential', new Set())) ) const agentIds = Array.from({ length: mintCount }, () => - Number(mintNodeId(agent)) + Number(mintNodeId(agent, 'sequential', new Set())) ) return frontendIds.every((id, index) => id === agentIds[index]) @@ -97,7 +97,9 @@ describe("mintNodeId's 'crdt-disjoint' mode never collides with a simulated agen const frontend = createLGraphState() frontend.lastNodeId = lastObservedId - const frontendMintedId = BigInt(mintNodeId(frontend, 'crdt-disjoint')) + const frontendMintedId = BigInt( + mintNodeId(frontend, 'crdt-disjoint', new Set()) + ) return frontendMintedId !== agentId } @@ -113,7 +115,7 @@ describe("mintNodeId's 'crdt-disjoint' mode never collides with a simulated agen (mintCount, agentIds) => { const frontend = createLGraphState() const frontendIds = Array.from({ length: mintCount }, () => - BigInt(mintNodeId(frontend, 'crdt-disjoint')) + BigInt(mintNodeId(frontend, 'crdt-disjoint', new Set())) ) const agentIdSet = new Set(agentIds) return frontendIds.every((id) => !agentIdSet.has(id)) @@ -140,7 +142,7 @@ describe("mintNodeId's 'crdt-disjoint' mode never collides with a simulated agen fc.property(fc.integer({ min: 1, max: 50 }), (mintCount) => { const frontend = createLGraphState() const ids = Array.from({ length: mintCount }, () => - BigInt(mintNodeId(frontend, 'crdt-disjoint')) + BigInt(mintNodeId(frontend, 'crdt-disjoint', new Set())) ) return ids.every( @@ -158,7 +160,7 @@ describe("mintNodeId's 'crdt-disjoint' mode never collides with a simulated agen fc.property(fc.integer({ min: 2, max: 50 }), (mintCount) => { const frontend = createLGraphState() const ids = Array.from({ length: mintCount }, () => - BigInt(mintNodeId(frontend, 'crdt-disjoint')) + BigInt(mintNodeId(frontend, 'crdt-disjoint', new Set())) ) return new Set(ids).size === ids.length diff --git a/src/lib/litegraph/src/idAllocation.test.ts b/src/lib/litegraph/src/idAllocation.test.ts index 90418f976c3..21e1a1416f5 100644 --- a/src/lib/litegraph/src/idAllocation.test.ts +++ b/src/lib/litegraph/src/idAllocation.test.ts @@ -3,6 +3,7 @@ import { describe, expect, it } from 'vitest' import { AGENT_RESERVED_BIT, createLGraphState, + findNextAvailableId, isReservedBitRangeNodeId, matchesReservedBitConvention, mintGroupId, @@ -22,18 +23,31 @@ import { toRerouteId } from '@/types/rerouteId' describe('idAllocation', () => { it('mints increasing ids for each entity kind', () => { const state = createLGraphState() + const reservedIds = new Set() - expect([mintNodeId(state), mintNodeId(state)]).toEqual(['1', '2']) - expect([mintGroupId(state), mintGroupId(state)]).toEqual([1, 2]) - expect([mintLinkId(state), mintLinkId(state)]).toEqual([1, 2]) - expect([mintRerouteId(state), mintRerouteId(state)]).toEqual([1, 2]) + expect([ + mintNodeId(state, 'sequential', reservedIds), + mintNodeId(state, 'sequential', reservedIds) + ]).toEqual(['1', '2']) + expect([ + mintGroupId(state, reservedIds), + mintGroupId(state, reservedIds) + ]).toEqual([1, 2]) + expect([ + mintLinkId(state, reservedIds), + mintLinkId(state, reservedIds) + ]).toEqual([1, 2]) + expect([ + mintRerouteId(state, reservedIds), + mintRerouteId(state, reservedIds) + ]).toEqual([1, 2]) }) it("mints from a disjoint range in 'crdt-disjoint' mode, ignoring lastNodeId", () => { const state = createLGraphState() state.lastNodeId = 5 - const id = BigInt(mintNodeId(state, 'crdt-disjoint')) + const id = BigInt(mintNodeId(state, 'crdt-disjoint', new Set())) expect(state.lastNodeId).toBe(5) // Bit 40 clear (never the agent's `2**40 | random52` range), bit 41 set. @@ -70,6 +84,123 @@ describe('idAllocation', () => { expect(state.lastNodeId).toBe(12) }) + it.for([ + { + name: 'node', + observe: (state: ReturnType, value: number) => + observeNodeId(state, toNodeId(value)), + mint: (state: ReturnType) => + mintNodeId(state, 'sequential', new Set()) + }, + { + name: 'group', + observe: (state: ReturnType, value: number) => + observeGroupId(state, toGroupId(value)), + mint: (state: ReturnType) => + mintGroupId(state, new Set()) + }, + { + name: 'link', + observe: (state: ReturnType, value: number) => + observeLinkId(state, toLinkId(value)), + mint: (state: ReturnType) => + mintLinkId(state, new Set()) + }, + { + name: 'reroute', + observe: (state: ReturnType, value: number) => + observeRerouteId(state, toRerouteId(value)), + mint: (state: ReturnType) => + mintRerouteId(state, new Set()) + } + ])( + 'continues $name allocation above the former limit', + ({ observe, mint }) => { + const state = createLGraphState() + observe(state, 100_000_001) + + expect(Number(mint(state))).toBe(100_000_002) + } + ) + + it.for([ + { + name: 'node', + setCounter: (state: ReturnType) => { + state.lastNodeId = Number.MAX_SAFE_INTEGER + }, + mint: ( + state: ReturnType, + reservedIds: ReadonlySet + ) => mintNodeId(state, 'sequential', reservedIds) + }, + { + name: 'group', + setCounter: (state: ReturnType) => { + state.lastGroupId = Number.MAX_SAFE_INTEGER + }, + mint: mintGroupId + }, + { + name: 'link', + setCounter: (state: ReturnType) => { + state.lastLinkId = toLinkId(Number.MAX_SAFE_INTEGER) + }, + mint: mintLinkId + }, + { + name: 'reroute', + setCounter: (state: ReturnType) => { + state.lastRerouteId = toRerouteId(Number.MAX_SAFE_INTEGER) + }, + mint: mintRerouteId + } + ])( + 'wraps $name allocation to an available safe ID', + ({ setCounter, mint }) => { + const state = createLGraphState() + setCounter(state) + + expect(Number(mint(state, new Set([1])))).toBe(2) + } + ) + + it('checks the candidate after the last reserved ID', () => { + expect(findNextAvailableId(new Set([1, 2, 3]), 1)).toBe(4) + }) + + it('skips a reserved ID above a stale counter', () => { + const state = createLGraphState() + state.lastGroupId = 1 + + expect(mintGroupId(state, new Set([2]))).toBe(3) + }) + + it('collects reservations only when the counter reaches its boundary', () => { + const state = createLGraphState() + let collections = 0 + const collectReservedIds = () => { + collections++ + return new Set([1]) + } + + expect(mintGroupId(state, collectReservedIds)).toBe(1) + expect(collections).toBe(0) + + state.lastGroupId = Number.MAX_SAFE_INTEGER + expect(mintGroupId(state, collectReservedIds)).toBe(2) + expect(collections).toBe(1) + }) + + it('wraps a reserved maximum candidate to the first available ID', () => { + expect( + findNextAvailableId( + new Set([Number.MAX_SAFE_INTEGER, 1]), + Number.MAX_SAFE_INTEGER + ) + ).toBe(2) + }) + describe('isReservedBitRangeNodeId', () => { it.for([ { id: toNodeId(AGENT_RESERVED_BIT.toString()), name: 'the agent floor' }, diff --git a/src/lib/litegraph/src/idAllocation.ts b/src/lib/litegraph/src/idAllocation.ts index 87241ad10a2..f23761b8f7f 100644 --- a/src/lib/litegraph/src/idAllocation.ts +++ b/src/lib/litegraph/src/idAllocation.ts @@ -15,6 +15,8 @@ export interface LGraphState { lastRerouteId: RerouteId } +type ReservedIds = ReadonlySet | (() => ReadonlySet) + export function createLGraphState(): LGraphState { return { lastGroupId: 0, @@ -24,8 +26,36 @@ export function createLGraphState(): LGraphState { } } +export function findNextAvailableId( + usedIds: ReadonlySet, + startAt = 1 +): number { + let candidate = Number.isSafeInteger(startAt) && startAt > 0 ? startAt : 1 + let attempts = 0 + while (usedIds.has(candidate) && attempts < usedIds.size) { + candidate = candidate === Number.MAX_SAFE_INTEGER ? 1 : candidate + 1 + attempts++ + } + return candidate +} + +function mintSequentialId(lastId: number, reservedIds: ReservedIds): number { + if (Number.isSafeInteger(lastId) && lastId >= 0) { + const nextId = lastId + 1 + if ( + Number.isSafeInteger(nextId) && + (typeof reservedIds === 'function' || !reservedIds.has(nextId)) + ) + return nextId + } + const usedIds = + typeof reservedIds === 'function' ? reservedIds() : reservedIds + return findNextAvailableId(usedIds, lastId + 1) +} + /** - * `'sequential'` (default) is the plain-local `++lastNodeId` counter. + * `'sequential'` advances the local counter, wrapping to an unused safe integer + * at the boundary. * `'crdt-disjoint'` is for a graph bound to the in-app agent's collaborative * doc, where a local mint can otherwise land on an id the agent independently * mints for the same doc — see {@link mintCrdtDisjointNodeId}. @@ -132,42 +162,69 @@ export function matchesReservedBitConvention(id: NodeId): boolean { export function mintNodeId( state: LGraphState, - mode: NodeIdMintMode = 'sequential' + mode: NodeIdMintMode, + reservedIds: ReservedIds ): NodeId { - return mode === 'crdt-disjoint' - ? mintCrdtDisjointNodeId() - : toNodeId(++state.lastNodeId) + if (mode === 'crdt-disjoint') return mintCrdtDisjointNodeId() + const id = mintSequentialId(state.lastNodeId, reservedIds) + if (id > state.lastNodeId) state.lastNodeId = id + return toNodeId(id) } -export function mintGroupId(state: LGraphState): GroupId { - return toGroupId(++state.lastGroupId) +export function mintGroupId( + state: LGraphState, + reservedIds: ReservedIds +): GroupId { + const id = mintSequentialId(state.lastGroupId, reservedIds) + if (id > state.lastGroupId) state.lastGroupId = id + return toGroupId(id) } -export function mintLinkId(state: LGraphState): LinkId { - state.lastLinkId = toLinkId(Number(state.lastLinkId) + 1) - return state.lastLinkId +export function mintLinkId( + state: LGraphState, + reservedIds: ReservedIds +): LinkId { + const lastLinkId = Number(state.lastLinkId) + const id = mintSequentialId(lastLinkId, reservedIds) + if (id > lastLinkId) state.lastLinkId = toLinkId(id) + return toLinkId(id) } -export function mintRerouteId(state: LGraphState): RerouteId { - state.lastRerouteId = toRerouteId(Number(state.lastRerouteId) + 1) - return state.lastRerouteId +export function mintRerouteId( + state: LGraphState, + reservedIds: ReservedIds +): RerouteId { + const lastRerouteId = Number(state.lastRerouteId) + const id = mintSequentialId(lastRerouteId, reservedIds) + if (id > lastRerouteId) state.lastRerouteId = toRerouteId(id) + return toRerouteId(id) } export function observeNodeId(state: LGraphState, id: NodeId): void { const numericId = Number(id) - if (Number.isInteger(numericId) && numericId > state.lastNodeId) { + if ( + Number.isSafeInteger(numericId) && + numericId >= 0 && + numericId > state.lastNodeId + ) { state.lastNodeId = numericId } } export function observeGroupId(state: LGraphState, id: GroupId): void { - if (id > state.lastGroupId) state.lastGroupId = id + if (Number.isSafeInteger(id) && id >= 0 && id > state.lastGroupId) { + state.lastGroupId = id + } } export function observeLinkId(state: LGraphState, id: LinkId): void { - if (id > state.lastLinkId) state.lastLinkId = id + if (Number.isSafeInteger(id) && id >= 0 && id > state.lastLinkId) { + state.lastLinkId = id + } } export function observeRerouteId(state: LGraphState, id: RerouteId): void { - if (id > state.lastRerouteId) state.lastRerouteId = id + if (Number.isSafeInteger(id) && id >= 0 && id > state.lastRerouteId) { + state.lastRerouteId = id + } } diff --git a/src/lib/litegraph/src/interfaces.ts b/src/lib/litegraph/src/interfaces.ts index 8edc8a9ae8f..38a88ead163 100644 --- a/src/lib/litegraph/src/interfaces.ts +++ b/src/lib/litegraph/src/interfaces.ts @@ -299,11 +299,11 @@ export type Direction = 'top' | 'bottom' | 'left' | 'right' export type CompassCorners = 'NE' | 'SE' | 'SW' | 'NW' /** - * A string that represents a specific data / slot type, e.g. `STRING`. + * A value that represents a specific data / slot type, e.g. `STRING`. * - * Can be comma-delimited to specify multiple allowed types, e.g. `STRING,INT`. + * Multiple allowed types may be comma-delimited or stored as an array. */ -export type ISlotType = number | string +export type ISlotType = number | string | string[] export interface INodeSlot extends HasBoundingRect { /** diff --git a/src/lib/litegraph/src/subgraph/SubgraphInput.ts b/src/lib/litegraph/src/subgraph/SubgraphInput.ts index 5a167c51bee..860ab5bf677 100644 --- a/src/lib/litegraph/src/subgraph/SubgraphInput.ts +++ b/src/lib/litegraph/src/subgraph/SubgraphInput.ts @@ -19,6 +19,7 @@ import type { IBaseWidget } from '@/lib/litegraph/src/types/widgets' import type { SubgraphInputNode } from './SubgraphInputNode' import type { SubgraphOutput } from './SubgraphOutput' import { SubgraphSlot } from './SubgraphSlotBase' +import { collectReservedLinkIds } from './subgraphDeduplication' import { isNodeSlot, isSubgraphOutput } from './subgraphUtils' /** @@ -87,7 +88,9 @@ export class SubgraphInput extends SubgraphSlot { subgraph.beforeChange() } - const linkId = mintLinkId(subgraph.state) + const linkId = mintLinkId(subgraph.state, () => + collectReservedLinkIds(subgraph.rootGraph) + ) const link = new LLink( linkId, @@ -105,44 +108,60 @@ export class SubgraphInput extends SubgraphSlot { } subgraph._addLink(link) - if (existingLink) { - this.parent._disconnectNodeInput(node, slot, existingLink) - if (subgraph.getLink(link.id) !== link) { - subgraph.afterChange() - return - } - } + return this.finishConnection( + link, + existingLink, + slot, + node, + inputIndex, + inputWidget + ) + } - this.linkIds.push(link.id) - anchorRerouteChain(subgraph, link) - subgraph.incrementVersion() - - if (inputWidget) { - // Keep the widget reference in sync with the active upstream widget. - // Stale references can appear across nested promotion rebinds. - this._widget = inputWidget - this.events.dispatch('input-connected', { - input: slot, - widget: inputWidget, - node - }) - } else { - this.events.dispatch('input-connected', { input: slot }) - } - if (subgraph.getLink(link.id) !== link) { - subgraph.afterChange() - return - } + private finishConnection( + link: LLink, + existingLink: LLink | undefined, + slot: INodeInputSlot, + node: LGraphNode, + inputIndex: number, + inputWidget: IBaseWidget | undefined + ): LLink | undefined { + const { subgraph } = this.parent + try { + if (existingLink) { + this.parent._disconnectNodeInput(node, slot, existingLink) + if (subgraph.getLink(link.id) !== link) return + } - node.onConnectionsChange?.(NodeSlotType.INPUT, inputIndex, true, link, slot) - if (subgraph.getLink(link.id) !== link) { + this.linkIds.push(link.id) + anchorRerouteChain(subgraph, link) + subgraph.incrementVersion() + + if (inputWidget) { + this._widget = inputWidget + this.events.dispatch('input-connected', { + input: slot, + widget: inputWidget, + node + }) + } else { + this.events.dispatch('input-connected', { input: slot }) + } + if (subgraph.getLink(link.id) !== link) return + + node.onConnectionsChange?.( + NodeSlotType.INPUT, + inputIndex, + true, + link, + slot + ) + if (subgraph.getLink(link.id) !== link) return + + return link + } finally { subgraph.afterChange() - return } - - subgraph.afterChange() - - return link } get labelPos(): Point { diff --git a/src/lib/litegraph/src/subgraph/SubgraphInputNode.ts b/src/lib/litegraph/src/subgraph/SubgraphInputNode.ts index 8dff9fca585..b5a2072c18b 100644 --- a/src/lib/litegraph/src/subgraph/SubgraphInputNode.ts +++ b/src/lib/litegraph/src/subgraph/SubgraphInputNode.ts @@ -23,6 +23,7 @@ import { EmptySubgraphInput } from './EmptySubgraphInput' import { SubgraphIONodeBase } from './SubgraphIONodeBase' import type { SubgraphInput } from './SubgraphInput' import type { SubgraphOutput } from './SubgraphOutput' +import { collectReservedLinkIds } from './subgraphDeduplication' export class SubgraphInputNode extends SubgraphIONodeBase @@ -108,7 +109,9 @@ export class SubgraphInputNode if (outputIndex === -1 || inputIndex === -1) throw new Error('Invalid slot indices.') - const linkId = mintLinkId(subgraph.state) + const linkId = mintLinkId(subgraph.state, () => + collectReservedLinkIds(subgraph.rootGraph) + ) return new LLink( linkId, diff --git a/src/lib/litegraph/src/subgraph/SubgraphOutput.ts b/src/lib/litegraph/src/subgraph/SubgraphOutput.ts index 6d7c548cf8a..c627757509f 100644 --- a/src/lib/litegraph/src/subgraph/SubgraphOutput.ts +++ b/src/lib/litegraph/src/subgraph/SubgraphOutput.ts @@ -15,6 +15,7 @@ import { NodeSlotType } from '@/lib/litegraph/src/types/globalEnums' import type { SubgraphInput } from './SubgraphInput' import type { SubgraphOutputNode } from './SubgraphOutputNode' import { SubgraphSlot } from './SubgraphSlotBase' +import { collectReservedLinkIds } from './subgraphDeduplication' import { isNodeSlot, isSubgraphInput } from './subgraphUtils' /** @@ -58,7 +59,9 @@ export class SubgraphOutput extends SubgraphSlot { subgraph.beforeChange() } - const linkId = mintLinkId(subgraph.state) + const linkId = mintLinkId(subgraph.state, () => + collectReservedLinkIds(subgraph.rootGraph) + ) const link = new LLink( linkId, diff --git a/src/lib/litegraph/src/subgraph/subgraphDeduplication.test.ts b/src/lib/litegraph/src/subgraph/subgraphDeduplication.test.ts index 77c09f7027d..5f24905342d 100644 --- a/src/lib/litegraph/src/subgraph/subgraphDeduplication.test.ts +++ b/src/lib/litegraph/src/subgraph/subgraphDeduplication.test.ts @@ -5,6 +5,7 @@ import { import { describe, expect, it } from 'vitest' import { toLinkId } from '@/types/linkId' +import { toNodeId } from '@/types/nodeId' import { toRerouteId } from '@/types/rerouteId' import { isUuidShapedSubgraphId } from '@/schemas/subgraphIdSchema' @@ -367,6 +368,74 @@ describe('normalizeSubgraphDefinitions', () => { expect(result.inputs![0].linkIds).toEqual([toLinkId(1)]) expect(subgraph.floatingLinks).toHaveLength(1) }) + + it('allocates beyond the former fixed limit', () => { + const subgraph = makeSubgraph('sg', ['dummy']) + const state = freshState() + state.lastNodeId = 100_000_000 + + const result = normalizeSubgraphDefinitions( + [subgraph], + { + nodeIds: new Set([toNodeId(1)]), + groupIds: new Set(), + linkIds: new Set(), + rerouteIds: new Set() + }, + state + ) + + expect(result.subgraphs[0].nodes![0].id).toBe(100_000_001) + expect(state.lastNodeId).toBe(100_000_001) + }) + + it('wraps colliding IDs at the safe-integer boundary', () => { + const subgraph = makeSubgraph('sg', ['dummy']) + subgraph.links = [chainedLink(1)] + const state = freshState() + state.lastLinkId = toLinkId(Number.MAX_SAFE_INTEGER) + + const result = normalizeSubgraphDefinitions( + [subgraph], + { + nodeIds: new Set(), + groupIds: new Set(), + linkIds: new Set([1]), + rerouteIds: new Set() + }, + state + ) + + expect(result.subgraphs[0].links![0].id).toBe(toLinkId(2)) + expect(state.lastLinkId).toBe(toLinkId(Number.MAX_SAFE_INTEGER)) + }) + + it('updates every root-level reference to a remapped interior node', () => { + const subgraph = makeSubgraph('sg', ['dummy']) + const rootNode = makeSubgraph('root', ['sg']).nodes![0] + rootNode.properties = { + proxyWidgets: [['1', 'seed']], + previewExposures: [{ sourceNodeId: '1' }] + } + + const result = normalizeSubgraphDefinitions( + [subgraph], + { + nodeIds: new Set([toNodeId(1)]), + groupIds: new Set(), + linkIds: new Set(), + rerouteIds: new Set() + }, + freshState(), + [rootNode] + ) + const remappedId = result.subgraphs[0].nodes![0].id + + expect(result.rootNodes![0].properties).toEqual({ + proxyWidgets: [[String(remappedId), 'seed']], + previewExposures: [{ sourceNodeId: String(remappedId) }] + }) + }) }) describe('deduplicateSubgraphGroupIds', () => { diff --git a/src/lib/litegraph/src/subgraph/subgraphDeduplication.ts b/src/lib/litegraph/src/subgraph/subgraphDeduplication.ts index 4b52722d378..325f29042d3 100644 --- a/src/lib/litegraph/src/subgraph/subgraphDeduplication.ts +++ b/src/lib/litegraph/src/subgraph/subgraphDeduplication.ts @@ -20,6 +20,7 @@ import { toNodeId } from '@/types/nodeId' import type { NodeId, SerializedNodeId } from '@/types/nodeId' import { toLinkId } from '@/types/linkId' import { toRerouteId } from '@/types/rerouteId' +import type { NodeProperty } from '@/types/nodeState' import { createUuidv4 } from '@/utils/uuid' import type { ExportedSubgraph, @@ -29,8 +30,6 @@ import type { SerialisableLLink } from '../types/serialisation' -const MAX_ID = 100_000_000 - interface DeduplicationResult< Subgraph extends { id: string; nodes?: { type: string }[] } = ExportedSubgraph, @@ -254,11 +253,15 @@ function deduplicateClonedSubgraphNodeIds( } for (const subgraph of clonedSubgraphs) { - patchProxyWidgets(subgraph.nodes ?? [], subgraphIdSet, remapBySubgraph) + patchSubgraphNodeReferences( + subgraph.nodes ?? [], + subgraphIdSet, + remapBySubgraph + ) } if (clonedRootNodes) { - patchProxyWidgets(clonedRootNodes, subgraphIdSet, remapBySubgraph) + patchSubgraphNodeReferences(clonedRootNodes, subgraphIdSet, remapBySubgraph) } } @@ -282,9 +285,7 @@ function remapNodeIds( const numericId = numericSerializedNodeId(id) if (usedNodeIdKeys.has(key)) { - const newId = findNextAvailableId(usedNodeIds, () => - Number(mintNodeId(state)) - ) + const newId = Number(mintNodeId(state, 'sequential', usedNodeIds)) remappedIds.set(key, newId) node.id = newId usedNodeIds.add(newId) @@ -304,6 +305,18 @@ function remapNodeIds( return remappedIds } +export function collectReservedNodeIds( + rootGraph: Pick, + rootNodes: ISerialisedNode[] = [] +): Set { + return new Set([ + ...rootNodes.map((node) => toNodeId(node.id)), + ...[rootGraph, ...rootGraph.subgraphs.values()].flatMap((owner) => + owner.nodes.map((node) => node.id) + ) + ]) +} + /** Parses a serialized node ID as an integer, or `null` when non-numeric. */ function numericSerializedNodeId(id: SerializedNodeId): number | null { const key = toNodeId(id) @@ -313,23 +326,6 @@ function numericSerializedNodeId(id: SerializedNodeId): number | null { : null } -/** - * Finds the next unused ID by repeatedly calling `advance`. - * Throws if the ID space is exhausted. - */ -function findNextAvailableId( - usedIds: Set, - advance: () => number -): number { - for (;;) { - const nextId = advance() - if (nextId > MAX_ID) { - throw new Error('Node ID space exhausted') - } - if (!usedIds.has(nextId)) return nextId - } -} - /** Patches origin_id / target_id in serialized links. */ function patchSerialisedLinks( links: SerialisableLLink[], @@ -392,7 +388,7 @@ export function deduplicateSubgraphLinkIds( const remapped = remapNumericIds( [...(subgraph.links ?? []), ...(subgraph.floatingLinks ?? [])], usedLinkIds, - () => mintLinkId(state), + () => mintLinkId(state, usedLinkIds), (id) => observeLinkId(state, toLinkId(id)), 'link' ) @@ -417,7 +413,7 @@ export function deduplicateSubgraphGroupIds( remapNumericIds( subgraph.groups ?? [], usedGroupIds, - () => mintGroupId(state), + () => mintGroupId(state, usedGroupIds), (id) => observeGroupId(state, toGroupId(id)), 'group' ) @@ -470,7 +466,7 @@ function remapRerouteIds( return remapNumericIds( subgraph.reroutes ?? [], usedRerouteIds, - () => mintRerouteId(state), + () => mintRerouteId(state, usedRerouteIds), (id) => observeRerouteId(state, toRerouteId(id)), 'reroute' ) @@ -488,7 +484,7 @@ function remapNumericIds( for (const item of items) { const oldId = item.id if (usedIds.has(oldId)) { - const newId = findNextAvailableId(usedIds, nextId) + const newId = nextId() remapped.set(oldId, newId) item.id = newId usedIds.add(newId) @@ -581,8 +577,7 @@ export function topologicalSortSubgraphs( return sorted } -/** Patches legacy proxyWidgets in root-level SubgraphNode instances. */ -function patchProxyWidgets( +function patchSubgraphNodeReferences( rootNodes: ISerialisedNode[], subgraphIdSet: Set, remapBySubgraph: Map> @@ -592,14 +587,40 @@ function patchProxyWidgets( const remappedIds = remapBySubgraph.get(node.type) if (!remappedIds) continue - const proxyWidgets = node.properties?.proxyWidgets - if (!Array.isArray(proxyWidgets)) continue + patchProxyWidgetReferences(node.properties?.proxyWidgets, remappedIds) + patchPreviewExposureReferences( + node.properties?.previewExposures, + remappedIds + ) + } +} - for (const entry of proxyWidgets) { - if (!Array.isArray(entry)) continue - const oldId = toNodeId(entry[0]) - const newId = remappedIds.get(oldId) - if (newId !== undefined) entry[0] = String(newId) - } +function patchProxyWidgetReferences( + property: NodeProperty | undefined, + remappedIds: Map +): void { + if (!Array.isArray(property)) return + for (const entry of property) { + if (!Array.isArray(entry)) continue + const newId = remappedIds.get(toNodeId(entry[0])) + if (newId !== undefined) entry[0] = String(newId) + } +} + +function patchPreviewExposureReferences( + property: NodeProperty | undefined, + remappedIds: Map +): void { + if (!Array.isArray(property)) return + for (const entry of property) { + if ( + typeof entry !== 'object' || + entry === null || + !('sourceNodeId' in entry) || + typeof entry.sourceNodeId !== 'string' + ) + continue + const newId = remappedIds.get(toNodeId(entry.sourceNodeId)) + if (newId !== undefined) entry.sourceNodeId = String(newId) } } diff --git a/src/lib/litegraph/src/utils/type.ts b/src/lib/litegraph/src/utils/type.ts index cc7b99f5932..ccf9801bb63 100644 --- a/src/lib/litegraph/src/utils/type.ts +++ b/src/lib/litegraph/src/utils/type.ts @@ -39,10 +39,15 @@ export function isNodeBindable(widget: unknown): widget is NodeBindable { ) } +export function slotTypeKey(type: ISlotType): string | number { + return Array.isArray(type) ? type.join(',') : type +} + export function commonType(...types: ISlotType[]): ISlotType | undefined { - if (!isStrings(types)) return undefined + const keys = types.map(slotTypeKey) + if (!isStrings(keys)) return undefined - const withoutWildcards = without(types, '*') + const withoutWildcards = without(keys, '*') if (withoutWildcards.length === 0) return '*' const typeLists: string[][] = withoutWildcards.map((type) => type.split(',')) diff --git a/src/platform/workflow/validation/schemas/workflowSchema.test.ts b/src/platform/workflow/validation/schemas/workflowSchema.test.ts index 8297c4d2427..8f9fa0e50c3 100644 --- a/src/platform/workflow/validation/schemas/workflowSchema.test.ts +++ b/src/platform/workflow/validation/schemas/workflowSchema.test.ts @@ -1,7 +1,10 @@ import fs from 'fs' import { describe, expect, it } from 'vitest' -import { validateComfyWorkflow } from '@/platform/workflow/validation/schemas/workflowSchema' +import { + validateComfyWorkflow, + zClipboardItems +} from '@/platform/workflow/validation/schemas/workflowSchema' import { defaultGraph } from '@/scripts/defaultGraph' const WORKFLOW_DIR = 'src/platform/workflow/validation/schemas/__fixtures__' @@ -391,3 +394,29 @@ describe('parseComfyWorkflow', () => { }) }) }) + +describe('zClipboardItems', () => { + it('normalizes array-like widget values and preserves array slot types', () => { + const node = JSON.parse(JSON.stringify(defaultGraph.nodes[0])) + node.inputs = [{ name: 'input', type: ['IMAGE', 'MASK'] }] + node.widgets_values = { 0: 'first', 1: 2, length: 2 } + + const result = zClipboardItems.safeParse({ nodes: [node] }) + + expect(result.success).toBe(true) + if (!result.success) throw result.error + expect(result.data.nodes?.[0].inputs?.[0].type).toEqual(['IMAGE', 'MASK']) + expect(result.data.nodes?.[0].widgets_values).toEqual(['first', 2]) + }) + + it('normalizes sparse widget values without allocating their declared length', () => { + const node = JSON.parse(JSON.stringify(defaultGraph.nodes[0])) + node.widgets_values = { 0: 'first', length: Number.MAX_SAFE_INTEGER } + + const result = zClipboardItems.safeParse({ nodes: [node] }) + + expect(result.success).toBe(true) + if (!result.success) throw result.error + expect(result.data.nodes?.[0].widgets_values).toEqual(['first']) + }) +}) diff --git a/src/platform/workflow/validation/schemas/workflowSchema.ts b/src/platform/workflow/validation/schemas/workflowSchema.ts index bb3e20187a0..ce80c658d22 100644 --- a/src/platform/workflow/validation/schemas/workflowSchema.ts +++ b/src/platform/workflow/validation/schemas/workflowSchema.ts @@ -2,8 +2,15 @@ import { z } from 'zod' import type { SafeParseReturnType } from 'zod' import { fromZodError } from 'zod-validation-error' import type { RendererType } from '@/lib/litegraph/src/LGraph' -import { parseLinkId } from '@/types/linkId' +import type { + ClipboardItems, + ExportedSubgraph +} from '@/lib/litegraph/src/types/serialisation' +import type { TWidgetValue } from '@/lib/litegraph/src/types/widgets' +import { parseLinkId, toLinkId } from '@/types/linkId' import type { LinkPresentation } from '@/types/linkPresentation' +import type { NodeProperty } from '@/types/nodeState' +import { toRerouteId } from '@/types/rerouteId' const zRendererType = z.enum([ 'LG', @@ -210,6 +217,15 @@ const zVersion = z.union([ z.literal('unknown') ]) +const zNodeProperty = z.union([ + z.string(), + z.number(), + z.boolean(), + z.null(), + z.record(z.unknown()), + z.array(z.unknown()) +]) satisfies z.ZodType + const zProperties = z .object({ ['Node name for S&R']: z.string().optional(), @@ -218,10 +234,25 @@ const zProperties = z ver: zVersion.optional(), models: z.array(zModelFile).optional() }) - .passthrough() + .catchall(zNodeProperty.optional()) const zWidgetValues = z.union([z.array(z.any()), z.record(z.any())]) +function normalizeClipboardWidgetValues( + values: z.output | undefined +): TWidgetValue[] | undefined { + if (!values || Array.isArray(values)) return values + const length = values.length + if (!Number.isSafeInteger(length) || length < 0) return [] + return Object.entries(values) + .filter(([key]) => { + const index = Number(key) + return Number.isInteger(index) && index >= 0 && index < length + }) + .sort(([left], [right]) => Number(left) - Number(right)) + .map(([, value]) => value) +} + const zComfyNode = z .object({ id: zNodeId, @@ -511,6 +542,95 @@ const zSubgraphDefinition = zComfyWorkflow1 }) .passthrough() +const zClipboardNode = zComfyNode.transform((node) => { + return { + ...node, + widgets_values: normalizeClipboardWidgetValues(node.widgets_values) + } +}) + +const zClipboardSubgraphInstance = zSubgraphInstance.transform((node) => { + return { + ...node, + widgets_values: normalizeClipboardWidgetValues(node.widgets_values) + } +}) + +const zClipboardGroup = zGroup.extend({ id: z.number() }) +const zClipboardReroute = zReroute.extend({ linkIds: z.array(z.number()) }) + +interface ClipboardSubgraphInput extends Omit< + z.input, + 'groups' | 'nodes' | 'reroutes' | 'subgraphs' | 'definitions' +> { + groups?: z.input[] + nodes: z.input[] + reroutes?: z.input[] + subgraphs?: z.input[] + definitions?: { subgraphs: ClipboardSubgraphInput[] } +} + +const zClipboardSubgraphDefinition: z.ZodType< + ExportedSubgraph, + z.ZodTypeDef, + ClipboardSubgraphInput +> = zSubgraphDefinition + .extend({ + groups: z.array(zClipboardGroup).optional(), + nodes: z.array(zClipboardNode), + reroutes: z.array(zClipboardReroute).optional(), + subgraphs: z.array(zClipboardSubgraphInstance).optional(), + definitions: z + .object({ + subgraphs: z.lazy(() => z.array(zClipboardSubgraphDefinition)) + }) + .optional() + }) + .transform(({ config, extra, ...subgraph }) => { + const normalizedExtra = extra + ? { + ...extra, + reroutes: extra.reroutes?.map((reroute) => ({ + ...reroute, + linkIds: reroute.linkIds ?? [] + })), + linkExtensions: extra.linkExtensions?.map((link) => ({ + ...link, + id: toLinkId(link.id), + parentId: toRerouteId(link.parentId) + })) + } + : undefined + return { + ...subgraph, + config: config ?? undefined, + extra: normalizedExtra + } + }) + +const zClipboardItemsSchema = z + .object({ + nodes: z.array(zClipboardNode).optional(), + groups: z.array(zClipboardGroup).optional(), + reroutes: z.array(zClipboardReroute).optional(), + links: z.array(zComfyLinkObject).optional(), + subgraphs: z.array(zClipboardSubgraphDefinition).optional() + }) + .refine( + (items) => + items.nodes !== undefined || + items.groups !== undefined || + items.reroutes !== undefined || + items.links !== undefined || + items.subgraphs !== undefined + ) + +export const zClipboardItems: z.ZodType< + ClipboardItems, + z.ZodTypeDef, + z.input +> = zClipboardItemsSchema + export type ModelFile = z.infer export type ComfyLinkObject = z.infer export type ComfyNode = z.infer diff --git a/src/stores/entityIdStore.test.ts b/src/stores/entityIdStore.test.ts index 843cbd35649..33019a564c1 100644 --- a/src/stores/entityIdStore.test.ts +++ b/src/stores/entityIdStore.test.ts @@ -13,12 +13,14 @@ describe(useEntityIdStore, () => { it('keeps allocation state when a root graph is rekeyed', () => { const store = useEntityIdStore() const state = store.get(first) - mintNodeId(state) + mintNodeId(state, 'sequential', new Set()) store.rekey(first, second) expect(store.get(second)).toBe(state) - expect(Number(mintNodeId(store.get(second)))).toBe(2) + expect(Number(mintNodeId(store.get(second), 'sequential', new Set()))).toBe( + 2 + ) }) it('replaces compatibility state without sharing the caller object', () => { diff --git a/src/workbench/extensions/agent/crdt/linkMintPort.ts b/src/workbench/extensions/agent/crdt/linkMintPort.ts index 91d1fa2032b..c3fe65f50f7 100644 --- a/src/workbench/extensions/agent/crdt/linkMintPort.ts +++ b/src/workbench/extensions/agent/crdt/linkMintPort.ts @@ -8,6 +8,7 @@ */ import type { NodeId as WireNodeId } from '@comfyorg/comfy-multi-player' +import type { ISlotType } from '@/lib/litegraph/src/interfaces' import { reportError } from '@/platform/telemetry/reportError' import type { GraphOperation } from './graphOperations' @@ -31,7 +32,7 @@ export interface LinkTopologyView { originSlot: number targetNodeId: string | number targetSlot: number - type: string | number + type: ISlotType } interface LinkEventFeed {