Files
ComfyUI_frontend/src/lib/litegraph
AustinMroz 3c8b7b015c Fix subgraphNode widget cloning with compressed target_slot (#7388)
## Cause
When graphs are actually exported, several layers of cleanup are
applied. Among these is link compression. Any widgets with inputs that
aren't used do not have inputs stored in the workflow. This was
implemented for backwards compatibility with the old "convert to input"
system for widgets. As part of this process, the target_slots on links
are rewritten such that they point to the index of the widget as if
unconnected widgets did not exist.

This "incorrect" state for links is only corrected AFTER a workflow has
loaded because the 'fix' method needs nodes to be initialized in order
to calculate the correct target_slot

This becomes a problem when subgraphs are introduced. SubgraphInputs
need to resolve a link to its target slot in order to construct a clone
of the linked widget DURING the loading process. Since this target slot
is not accurate, this can result in the cloned widget having the wrong
type.

For a minimal reproduction:
- Create a subgraph with an Empty Latent Image with batch_size linked to
the Subgraph Input
- Export the workflow
- On load, the batch_size has step and min attributes which incorrectly
correspond to width

## Fix
There's multiple possible ways to address this and input on direction is
appreciated
- Fix links before loading graph
  - Likely to break with any dynamic state
- Fix links, then load graph again
- Ugly, bad performance, dynamic state may require multiple passes to
correctly ripple
- In the Subgraph code, ignore target_slot and instead `.find()` input
with matching linkId (proposed)
- Promising, but means accepting that state is just wrong sometimes.
Another forever footgun.
- Entirely remove the input compression
- Some people may complain, and old workflows still need to be supported
- Only remove target_slot redirection inside subgraphs
- Creates ugly logical difference between what happens inside and
outside subgraphs.
  - Still leaves old workflows broken

┆Issue is synchronized with this [Notion
page](https://www.notion.so/PR-7388-Remove-target_slot-compression-from-subgraph-exports-2c66d73d3650815d8c96c5047958ab67)
by [Unito](https://www.unito.io)
2025-12-11 12:32:35 -08:00
..

@ComfyOrg/litegraph

This is the litegraph version used in ComfyUI_frontend.

It is a fork of the original litegraph.js. Some APIs may by unchanged, however it is largely incompatible with the original.

Some early highlights:

Usage

This library is included as a git subtree in the ComfyUI frontend project at src/lib/litegraph.

litegraph.js

A TypeScript library to create graphs in the browser similar to Unreal Blueprints.

Description of the original litegraph.js

A library in Javascript to create graphs in the browser similar to Unreal Blueprints. Nodes can be programmed easily and it includes an editor to construct and tests the graphs.

It can be integrated easily in any existing web applications and graphs can be run without the need of the editor.

Node Graph

Features

  • Renders on Canvas2D (zoom in/out and panning, easy to render complex interfaces, can be used inside a WebGLTexture)
  • Easy to use editor (searchbox, keyboard shortcuts, multiple selection, context menu, ...)
  • Optimized to support hundreds of nodes per graph (on editor but also on execution)
  • Customizable theme (colors, shapes, background)
  • Callbacks to personalize every action/drawing/event of nodes
  • Graphs can be executed in NodeJS
  • Highly customizable nodes (color, shape, widgets, custom rendering)
  • Easy to integrate in any JS application (one single file, no dependencies)
  • Typescript support

Integration

This library is integrated as a git subtree in the ComfyUI frontend project. To use it in your code:

import { LGraph, LGraphNode, LiteGraph } from '@/lib/litegraph'

How to code a new Node type

Here is an example of how to build a node that sums two inputs:

import { LiteGraph, LGraphNode } from "./litegraph"

class MyAddNode extends LGraphNode {
  // Name to show
  title = "Sum"

  constructor() {
    this.addInput("A", "number")
    this.addInput("B", "number")
    this.addOutput("A+B", "number")
    this.properties.precision = 1
  }

  // Function to call when the node is executed
  onExecute() {
    var A = this.getInputData(0)
    if (A === undefined) A = 0
    var B = this.getInputData(1)
    if (B === undefined) B = 0
    this.setOutputData(0, A + B)
  }
}

// Register the node type
LiteGraph.registerNodeType("basic/sum", MyAddNode)

Server side

It also works server-side using NodeJS although some nodes do not work in server (audio, graphics, input, etc).

import { LiteGraph, LGraph } from "./litegraph.js"

const graph = new LGraph()

const firstNode = LiteGraph.createNode("basic/sum")
graph.add(firstNode)

const secondNode = LiteGraph.createNode("basic/sum")
graph.add(secondNode)

firstNode.connect(0, secondNode, 1)

graph.start()

Projects using it

ComfyUI

ComfyUI default workflow

Projects using the original litegraph.js

Click to expand

webglstudio.org

WebGLStudio

MOI Elephant

MOI Elephant

Mynodes

MyNodes

Feedback

Please open an issue on the GitHub repo.

Development

Litegraph has no runtime dependencies. The build tooling has been tested on Node.JS 20.18.x

Releasing

Use GitHub actions to release normal versions.

  1. Run the Release a New Version action, selecting the version increment type
  2. Merge the resolution PR
  3. A GitHub release is automatically published on merge

Pre-release

The action directly translates Version increment type to the pnpm version command. Pre-release ID (suffix) is the option for the --preid argument.

e.g. Use prerelease increment type to automatically bump the patch version and create a pre-release version. Subsequent runs of prerelease will update the prerelease version only. Use patch when ready to remove the pre-release suffix.

Contributors

You can find the current list of contributors on GitHub.

Contributors (pre-fork)

  • atlasan
  • kriffe
  • rappestad
  • InventivetalentDev
  • NateScarlet
  • coderofsalvation
  • ilyabesk
  • gausszhou