Skip to content

Add Subgraphs #1000

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 112 commits into from
Jun 28, 2025
Merged

Add Subgraphs #1000

merged 112 commits into from
Jun 28, 2025

Conversation

webfiltered
Copy link
Contributor

@webfiltered webfiltered commented May 2, 2025

[API] Subgraph System

This PR introduces a comprehensive subgraph system to litegraph.js, enabling modular graph composition through encapsulated subgraphs.

Core Features

Subgraph Implementation

The new subgraph system allows nodes to be converted into reusable subgraph components:

// Convert selected nodes to subgraph
const subgraphNode = graph.convertNodesToSubgraph(selectedNodes)

// Access the subgraph
const subgraph = subgraphNode.subgraph
subgraph.addInput("data", "number")
subgraph.addOutput("result", "string")

Subgraph I/O Nodes

Special nodes handle subgraph inputs and outputs with hoverable UI:

  • SubgraphInputNode: Receives data from parent graph
  • SubgraphOutputNode: Sends data to parent graph
  • Support for linking from nodes directly to I/O nodes
  • Empty slot interactions for dynamic slot management

Execution System

Rewritten & upstreamed execution logic to handle subgraphs:

  • ExecutableNodeDTO for cleaner execution flow
  • Bypass functionality
  • Recursion prevention for nested subgraphs

UI additions

  • Subgraph I/O nodes
  • Context menu integration for subgraph operations
  • Cursor changes when hovering over interactive elements
  • Variadic slot support (basic implementation)

Usage

Creating Subgraphs

// Method 1: Convert existing nodes
const nodes = [node1, node2, node3]
const subgraphNode = graph.convertNodesToSubgraph(nodes)

// Method 2: Create empty subgraph
const subgraphNode = LiteGraph.createNode("graph/subgraph")
const subgraph = subgraphNode.subgraph

// Add I/O
subgraph.addInput("value", "number")
subgraph.addOutput("result", "boolean")

Nesting Subgraphs

// Subgraphs can contain other subgraphs
const outerSubgraph = outerNode.subgraph
const innerNode = LiteGraph.createNode("graph/subgraph")
outerSubgraph.add(innerNode)

// Recursion prevention built-in
// Attempting circular references throws RecursionError

Notes

  • Subgraph nodes preserve their internal state across graph serialization
  • I/O nodes support dynamic slot management with empty virtual slots
  • New event: dropped-on-io-node via LinkConnector
  • Compatible with existing serialization format (with version checks)

@webfiltered webfiltered marked this pull request as ready for review June 28, 2025 22:20
@webfiltered webfiltered merged commit bcaaa00 into master Jun 28, 2025
2 of 4 checks passed
@webfiltered webfiltered deleted the subgraph branch June 28, 2025 22:21
@webfiltered webfiltered mentioned this pull request Jun 28, 2025
@Tekaiguy
Copy link

I don't see how to convert subgraphs back to regular nodes. Unlike the old group nodes feature, subgraphs seem to be created destructively. Is that a planned feature still to come?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants