Line data Source code
1 : /**
2 : * This code is adapted from rgthree-comfy's link_fixer.ts
3 : * @see https://github.com/rgthree/rgthree-comfy/blob/b84f39c7c224de765de0b54c55b967329011819d/src_web/common/link_fixer.ts
4 : *
5 : * MIT License
6 : *
7 : * Copyright (c) 2023 Regis Gaughan, III (rgthree)
8 : *
9 : * Permission is hereby granted, free of charge, to any person obtaining a copy
10 : * of this software and associated documentation files (the "Software"), to deal
11 : * in the Software without restriction, including without limitation the rights
12 : * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 : * copies of the Software, and to permit persons to whom the Software is
14 : * furnished to do so, subject to the following conditions:
15 : *
16 : * The above copyright notice and this permission notice shall be included in all
17 : * copies or substantial portions of the Software.
18 : *
19 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 : * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 : * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 : * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 : * SOFTWARE.
26 : */
27 : import type { INodeOutputSlot } from '@comfyorg/graph-model/slots'
28 : import { NodeInputSlot } from '@/lib/litegraph/src/node/NodeInputSlot'
29 : import { NodeOutputSlot } from '@/lib/litegraph/src/node/NodeOutputSlot'
30 : import { outputLinkIds } from '@/lib/litegraph/src/node/slotLinks'
31 : import { toLinkId } from '@comfyorg/graph-model/linkId'
32 : import { parseNodeId } from '@comfyorg/graph-model/nodeId'
33 : import type { SerializedNodeId } from '@comfyorg/graph-model/nodeId'
34 : import type { SerialisedLLinkArray } from '@/lib/litegraph/src/LLink'
35 : import type { LGraph, LGraphNode, LLink } from '@/lib/litegraph/src/litegraph'
36 : import type {
37 : ISerialisedGraph,
38 : ISerialisedNode
39 : } from '@comfyorg/graph-model/serialisation'
40 :
41 : type FixableSerialisedGraph = Omit<ISerialisedGraph, 'links'> & {
42 : links: (SerialisedLLinkArray | null)[]
43 : }
44 :
45 : interface BadLinksData<T = FixableSerialisedGraph | LGraph> {
46 : hasBadLinks: boolean
47 : fixed: boolean
48 : graph: T
49 : patched: number
50 : deleted: number
51 : }
52 :
53 : enum IoDirection {
54 : INPUT,
55 : OUTPUT
56 : }
57 :
58 8 : function isLiveGraph(graph: FixableSerialisedGraph | LGraph): graph is LGraph {
59 8 : return 'getNodeById' in graph
60 : }
61 :
62 4 : function getNodeById(
63 : graph: FixableSerialisedGraph | LGraph,
64 : id: SerializedNodeId
65 : ) {
66 4 : if (isLiveGraph(graph)) {
67 0 : const parsedNodeId = parseNodeId(id)
68 0 : return parsedNodeId ? graph.getNodeById(parsedNodeId) : null
69 : }
70 4 : return graph.nodes.find((node: ISerialisedNode) => node.id == id)!
71 : }
72 :
73 4 : function outputLinkIdsOf(
74 : node: ISerialisedNode | LGraphNode,
75 : slot: number
76 : ): readonly number[] | undefined {
77 4 : const slotData = node.outputs?.[slot]
78 4 : if (!slotData) return undefined
79 4 : if (!(slotData instanceof NodeOutputSlot)) return slotData.links ?? undefined
80 :
81 0 : const { graph, id } = slotData.node
82 0 : if (!graph) return undefined
83 0 : const ids = outputLinkIds(graph, id, slot)
84 4 : return ids.length ? ids : undefined
85 : }
86 :
87 2 : function extendLink(link: SerialisedLLinkArray) {
88 2 : return {
89 : link: link,
90 : id: link[0],
91 : origin_id: link[1],
92 : origin_slot: link[2],
93 : target_id: link[3],
94 : target_slot: link[4],
95 : type: link[5]
96 : }
97 : }
98 :
99 : /**
100 : * Takes a ISerialisedGraph or live LGraph and inspects the links and nodes to ensure the linking
101 : * makes logical sense. Can apply fixes when passed the `fix` argument as true.
102 : *
103 : * Note that fixes are a best-effort attempt. Seems to get it correct in most cases, but there is a
104 : * chance it correct an anomaly that results in placing an incorrect link (say, if there were two
105 : * links in the data). Users should take care to not overwrite work until manually checking the
106 : * result.
107 : */
108 2 : export function fixBadLinks(
109 : graph: FixableSerialisedGraph | LGraph,
110 : options: {
111 : fix?: boolean
112 : silent?: boolean
113 : logger?: { log: (...args: unknown[]) => void }
114 : } = {}
115 : ): BadLinksData {
116 2 : const { fix = false, silent = false, logger: _logger = console } = options
117 2 : const logger = {
118 : log: (...args: unknown[]) => {
119 8 : if (!silent) {
120 8 : _logger.log(...args)
121 : }
122 : }
123 : }
124 :
125 2 : const patchedNodeSlots: {
126 : [nodeId: string]:
127 : | {
128 : inputs?: { [slot: number]: number | null | undefined }
129 : outputs?: {
130 : [slots: number]:
131 : | {
132 : links: number[]
133 : changes: {
134 : [linkId: number]: 'ADD' | 'REMOVE' | undefined
135 : }
136 : }
137 : | undefined
138 : }
139 : }
140 : | undefined
141 : } = {}
142 :
143 2 : const data: {
144 : patchedNodes: Array<ISerialisedNode | LGraphNode>
145 : deletedLinks: number[]
146 : } = {
147 : patchedNodes: [],
148 : deletedLinks: []
149 : }
150 :
151 : /**
152 : * Internal patch node. We keep track of changes in patchedNodeSlots in case we're in a dry run.
153 : */
154 2 : function patchNodeSlot(
155 : node: ISerialisedNode | LGraphNode,
156 : ioDir: IoDirection,
157 : slot: number,
158 : linkId: number,
159 : op: 'ADD' | 'REMOVE'
160 : ) {
161 2 : const patchedNode = patchedNodeSlots[node.id] ?? {}
162 2 : patchedNodeSlots[node.id] = patchedNode
163 2 : if (ioDir == IoDirection.INPUT) {
164 1 : patchedNode['inputs'] = patchedNode['inputs'] || {}
165 : // We can set to null (delete), so undefined means we haven't set it at all.
166 1 : if (patchedNode['inputs'][slot] !== undefined) {
167 0 : logger.log(
168 : ` > Already set ${node.id}.inputs[${slot}] to ${
169 : patchedNode['inputs'][slot]!
170 : } Skipping.`
171 : )
172 0 : return false
173 : }
174 1 : const linkIdToSet = op === 'REMOVE' ? null : linkId
175 1 : const inputSlot = node.inputs?.[slot]
176 1 : if (fix && !inputSlot) {
177 1 : logger.log(
178 : ` > Cannot patch ${node.id}.inputs[${slot}] because the input slot is missing.`
179 : )
180 1 : return false
181 : }
182 0 : patchedNode['inputs'][slot] = linkIdToSet
183 : // Live NodeInputSlot mirrors are store-derived and need no repair;
184 : // only serialized plain slots carry a writable link field.
185 1 : if (fix && !(inputSlot instanceof NodeInputSlot)) {
186 0 : const writable = inputSlot as { link?: number | null }
187 0 : writable.link = linkIdToSet
188 : }
189 : } else {
190 1 : patchedNode['outputs'] = patchedNode['outputs'] || {}
191 1 : patchedNode['outputs'][slot] = patchedNode['outputs'][slot] || {
192 : links: [...(outputLinkIdsOf(node, slot) ?? [])],
193 : changes: {}
194 : }
195 1 : if (patchedNode['outputs'][slot]['changes'][linkId] !== undefined) {
196 0 : logger.log(
197 : ` > Already set ${node.id}.outputs[${slot}] to ${
198 : patchedNode['inputs']![slot]
199 : }! Skipping.`
200 : )
201 0 : return false
202 : }
203 1 : patchedNode['outputs'][slot]['changes'][linkId] = op
204 1 : if (op === 'ADD') {
205 : const linkIdIndex =
206 : patchedNode['outputs'][slot]['links'].indexOf(linkId)
207 0 : if (linkIdIndex !== -1) {
208 0 : logger.log(
209 : ` > Hmmm.. asked to add ${linkId} but it is already in list...`
210 : )
211 0 : return false
212 : }
213 0 : patchedNode['outputs'][slot]['links'].push(linkId)
214 : // Live NodeOutputSlot mirrors are store-derived and need no repair;
215 : // only serialized plain slots carry a writable links array.
216 0 : if (fix && !(node.outputs?.[slot] instanceof NodeOutputSlot)) {
217 0 : node.outputs = node.outputs || []
218 : const slotData = (node.outputs[slot] ??=
219 : {} satisfies Partial<INodeOutputSlot> as INodeOutputSlot)
220 : const writable = slotData as { links?: number[] | null }
221 0 : writable.links = writable.links || []
222 0 : writable.links.push(linkId)
223 : }
224 : } else {
225 1 : const linkIdIndex =
226 : patchedNode['outputs'][slot]['links'].indexOf(linkId)
227 1 : if (linkIdIndex === -1) {
228 0 : logger.log(
229 : ` > Hmmm.. asked to remove ${linkId} but it doesn't exist...`
230 : )
231 0 : return false
232 : }
233 1 : patchedNode['outputs'][slot]['links'].splice(linkIdIndex, 1)
234 1 : if (fix && !(node.outputs?.[slot] instanceof NodeOutputSlot)) {
235 : const writable = node.outputs?.[slot] as { links?: number[] | null }
236 1 : writable.links?.splice(linkIdIndex, 1)
237 : }
238 : }
239 : }
240 1 : data.patchedNodes.push(node)
241 1 : return true
242 : }
243 :
244 : /**
245 : * Internal to check if a node (or patched data) has a linkId.
246 : */
247 6 : function nodeHasLinkId(
248 : node: ISerialisedNode | LGraphNode,
249 : ioDir: IoDirection,
250 : slot: number,
251 : linkId: number
252 : ) {
253 : // Patched data should be canonical. We can double check if fixing too.
254 6 : let has: boolean
255 6 : const patchedNode = patchedNodeSlots[node.id]
256 6 : if (ioDir === IoDirection.INPUT) {
257 3 : const nodeHasIt = node.inputs?.[slot]?.link === linkId
258 3 : if (patchedNode?.inputs) {
259 1 : const patchedHasIt = patchedNode.inputs[slot] === linkId
260 : // If we're fixing, double check that node matches.
261 1 : if (fix && nodeHasIt !== patchedHasIt) {
262 0 : throw Error('Error. Expected node to match patched data.')
263 : }
264 1 : has = patchedHasIt
265 : } else {
266 2 : has = nodeHasIt
267 : }
268 : } else {
269 3 : const nodeHasIt = outputLinkIdsOf(node, slot)?.includes(toLinkId(linkId))
270 3 : const patchedOutput = patchedNode?.outputs?.[slot]
271 3 : if (patchedOutput?.changes[linkId]) {
272 1 : const patchedHasIt = patchedOutput.links.includes(linkId)
273 : // If we're fixing, double check that node matches.
274 1 : if (fix && nodeHasIt !== patchedHasIt) {
275 0 : throw Error('Error. Expected node to match patched data.')
276 : }
277 1 : has = patchedHasIt
278 : } else {
279 2 : has = !!nodeHasIt
280 : }
281 : }
282 6 : return has
283 : }
284 :
285 : /**
286 : * Internal to check if a node (or patched data) has a linkId.
287 : */
288 1 : function nodeHasAnyLink(
289 : node: ISerialisedNode | LGraphNode,
290 : ioDir: IoDirection,
291 : slot: number
292 : ) {
293 : // Patched data should be canonical. We can double check if fixing too.
294 1 : let hasAny: boolean
295 1 : const patchedNode = patchedNodeSlots[node.id]
296 1 : if (ioDir === IoDirection.INPUT) {
297 1 : const nodeHasAny = node.inputs?.[slot]?.link != null
298 1 : if (patchedNode?.inputs) {
299 0 : const patchedHasAny = patchedNode.inputs[slot] != null
300 : // If we're fixing, double check that node matches.
301 0 : if (fix && nodeHasAny !== patchedHasAny) {
302 0 : throw Error('Error. Expected node to match patched data.')
303 : }
304 0 : hasAny = patchedHasAny
305 : } else {
306 1 : hasAny = nodeHasAny
307 : }
308 : } else {
309 0 : const nodeHasAny = outputLinkIdsOf(node, slot)?.length
310 0 : const patchedOutput = patchedNode?.outputs?.[slot]
311 0 : if (patchedOutput?.changes) {
312 0 : const patchedHasAny = patchedOutput.links.length
313 : // If we're fixing, double check that node matches.
314 0 : if (fix && nodeHasAny !== patchedHasAny) {
315 0 : throw Error('Error. Expected node to match patched data.')
316 : }
317 0 : hasAny = !!patchedHasAny
318 : } else {
319 0 : hasAny = !!nodeHasAny
320 : }
321 : }
322 1 : return hasAny
323 : }
324 :
325 : const links: Array<SerialisedLLinkArray | LLink | null> = isLiveGraph(graph)
326 : ? Array.from(graph.links.values())
327 : : graph.links
328 :
329 2 : const linksReverse = [...links]
330 2 : linksReverse.reverse()
331 2 : for (const l of linksReverse) {
332 1 : if (!l) continue
333 1 : const link = !Array.isArray(l) ? l : extendLink(l)
334 :
335 1 : const originNode = getNodeById(graph, link.origin_id)
336 1 : const originHasLink = () =>
337 : nodeHasLinkId(originNode!, IoDirection.OUTPUT, link.origin_slot, link.id)
338 1 : const patchOrigin = (op: 'ADD' | 'REMOVE', id = link.id) =>
339 : patchNodeSlot(originNode!, IoDirection.OUTPUT, link.origin_slot, id, op)
340 :
341 1 : const targetNode = getNodeById(graph, link.target_id)
342 1 : const targetHasLink = () =>
343 : nodeHasLinkId(targetNode!, IoDirection.INPUT, link.target_slot, link.id)
344 1 : const targetHasAnyLink = () =>
345 : nodeHasAnyLink(targetNode!, IoDirection.INPUT, link.target_slot)
346 1 : const patchTarget = (op: 'ADD' | 'REMOVE', id = link.id) =>
347 : patchNodeSlot(targetNode!, IoDirection.INPUT, link.target_slot, id, op)
348 :
349 1 : const originLog = `origin(${link.origin_id}).outputs[${link.origin_slot}].links`
350 1 : const targetLog = `target(${link.target_id}).inputs[${link.target_slot}].link`
351 :
352 1 : if (!originNode || !targetNode) {
353 0 : if (!originNode && !targetNode) {
354 0 : logger.log(
355 : `Link ${link.id} is invalid, ` +
356 : `both origin ${link.origin_id} and target ${link.target_id} do not exist`
357 : )
358 0 : } else if (!originNode) {
359 0 : logger.log(
360 : `Link ${link.id} is funky... ` +
361 : `origin ${link.origin_id} does not exist, but target ${link.target_id} does.`
362 : )
363 0 : if (targetHasLink()) {
364 0 : logger.log(
365 : ` > [PATCH] ${targetLog} does have link, will remove the inputs' link first.`
366 : )
367 0 : patchTarget('REMOVE', -1)
368 : }
369 0 : } else if (!targetNode) {
370 0 : logger.log(
371 : `Link ${link.id} is funky... ` +
372 : `target ${link.target_id} does not exist, but origin ${link.origin_id} does.`
373 : )
374 0 : if (originHasLink()) {
375 0 : logger.log(
376 : ` > [PATCH] Origin's links' has ${link.id}; will remove the link first.`
377 : )
378 0 : patchOrigin('REMOVE')
379 : }
380 : }
381 0 : continue
382 : }
383 :
384 1 : if (targetHasLink() || originHasLink()) {
385 1 : if (!originHasLink()) {
386 0 : logger.log(
387 : `${link.id} is funky... ${originLog} does NOT contain it, but ${targetLog} does.`
388 : )
389 :
390 0 : logger.log(
391 : ` > [PATCH] Attempt a fix by adding this ${link.id} to ${originLog}.`
392 : )
393 0 : patchOrigin('ADD')
394 1 : } else if (!targetHasLink()) {
395 1 : logger.log(
396 : `${link.id} is funky... ${targetLog} is NOT correct (is ${
397 : targetNode.inputs?.[link.target_slot]?.link
398 : }), but ${originLog} contains it`
399 : )
400 1 : if (!targetHasAnyLink()) {
401 1 : logger.log(
402 : ` > [PATCH] ${targetLog} is not defined, will set to ${link.id}.`
403 : )
404 : const patched = patchTarget('ADD')
405 1 : if (!patched) {
406 1 : logger.log(
407 : ` > [PATCH] Nvm, ${targetLog} already patched. Removing ${link.id} from ${originLog}.`
408 : )
409 1 : patchOrigin('REMOVE')
410 : }
411 : } else {
412 0 : logger.log(
413 : ` > [PATCH] ${targetLog} is defined, removing ${link.id} from ${originLog}.`
414 : )
415 0 : patchOrigin('REMOVE')
416 : }
417 : }
418 : }
419 : }
420 :
421 : // Now that we've cleaned up the inputs, outputs, run through it looking for dangling links.,
422 2 : for (const l of linksReverse) {
423 1 : if (!l) continue
424 1 : const link = !Array.isArray(l) ? l : extendLink(l)
425 1 : const originNode = getNodeById(graph, link.origin_id)
426 1 : const targetNode = getNodeById(graph, link.target_id)
427 : // Now that we've manipulated the linking, check again if they both exist.
428 1 : if (
429 : (!originNode ||
430 : !nodeHasLinkId(
431 : originNode,
432 : IoDirection.OUTPUT,
433 : link.origin_slot,
434 : link.id
435 : )) &&
436 : (!targetNode ||
437 : !nodeHasLinkId(
438 : targetNode,
439 : IoDirection.INPUT,
440 : link.target_slot,
441 : link.id
442 : ))
443 : ) {
444 1 : logger.log(
445 : `${link.id} is def invalid; BOTH origin node ${link.origin_id} ${
446 : !originNode ? 'is removed' : `doesn't have ${link.id}`
447 : } and ${link.origin_id} target node ${
448 : !targetNode ? 'is removed' : `doesn't have ${link.id}`
449 : }.`
450 : )
451 1 : data.deletedLinks.push(link.id)
452 1 : continue
453 : }
454 : }
455 :
456 : // If we're fixing, then we've been patching along the way. Now go through and actually delete
457 : // the zombie links from `app.graph.links`
458 2 : if (fix) {
459 1 : for (let i = data.deletedLinks.length - 1; i >= 0; i--) {
460 1 : logger.log(`Deleting link #${data.deletedLinks[i]}.`)
461 1 : if (isLiveGraph(graph)) {
462 0 : graph._removeLink(toLinkId(data.deletedLinks[i]))
463 : } else {
464 1 : const links = graph.links
465 : // Sometimes we got objects for links if passed after ComfyUI's loadGraphData modifies the
466 : // data. We make a copy now, but can handle the bastardized objects just in case.
467 1 : const idx = links.findIndex(
468 : (l) =>
469 : l != null &&
470 : (l[0] === data.deletedLinks[i] ||
471 : ('id' in l && l.id === data.deletedLinks[i]))
472 : )
473 1 : if (idx === -1) {
474 0 : logger.log(`INDEX NOT FOUND for #${data.deletedLinks[i]}`)
475 : }
476 1 : logger.log(`splicing ${idx} from links`)
477 1 : links.splice(idx, 1)
478 : }
479 : }
480 : // If we're a serialized graph, we can filter out the links because it's just an array.
481 1 : if (!isLiveGraph(graph)) {
482 1 : graph.links = graph.links.filter((link) => link !== null)
483 : }
484 : }
485 2 : if (!data.patchedNodes.length && !data.deletedLinks.length) {
486 1 : return {
487 : hasBadLinks: false,
488 : fixed: false,
489 : graph,
490 : patched: data.patchedNodes.length,
491 : deleted: data.deletedLinks.length
492 : }
493 : }
494 :
495 2 : logger.log(
496 : `${fix ? 'Made' : 'Would make'} ${data.patchedNodes.length || 'no'} node link patches, and ${
497 : data.deletedLinks.length || 'no'
498 : } stale link removals.`
499 : )
500 :
501 2 : const hasChanges = !!(data.patchedNodes.length || data.deletedLinks.length)
502 2 : let hasBadLinks: boolean = hasChanges
503 : // If we're fixing, then let's run it again to see if there are no more bad links.
504 2 : if (fix) {
505 : const rerun = fixBadLinks(graph, { fix: false, silent: true })
506 1 : hasBadLinks = rerun.hasBadLinks
507 : }
508 :
509 2 : return {
510 : hasBadLinks,
511 : fixed: fix && hasChanges && !hasBadLinks,
512 : graph,
513 : patched: data.patchedNodes.length,
514 : deleted: data.deletedLinks.length
515 : }
516 : }
|