This commit fixes a critical issue where saving a workflow with complex subgraphs could lead to a corrupted file, causing errors upon loading.
The root cause was twofold:
1. **Unsafe Object Cloning:** The `LiteGraph.cloneObject` function used `JSON.stringify`, which cannot handle circular references that can exist in a nodes properties, especially in complex graphs with promoted widgets. This has been replaced with `_.cloneDeep` from lodash for robust and safe deep cloning.
2. **Unsafe Node Configuration:** The `LGraphNode.configure` method used a generic loop that would overwrite the `inputs` and `outputs` arrays with plain objects from the serialized data. This created a temporary but dangerous state where class instances were replaced by plain objects, leading to `TypeError` exceptions when methods or private fields were accessed on them. The `configure` method has been refactored to handle `inputs`, `outputs`, and `properties` explicitly and safely, ensuring they are always proper class instances.
This fix makes the node loading process more robust and predictable, preventing data corruption and ensuring the stability of workflows with subgraphs.
Prior to this commit, subgraphNode inconsistently refers to either the
parent graph, or to indicate the current node is a subgraph.
This corrects the usage of subgraphNode to consistently refer to the
subgraph instance as defined in the constructor.
This solves a bug where graph serialization fails due to an incorrectly
reported infinite loop.
Port of https://github.com/Comfy-Org/litegraph.js/pull/1193
Subgraphs are loaded in order of creation. Under most circumstances,
this means newer subgraphs are loaded first. With nested subgraphs, this
means a subgraph node has it's inputs connected before it's inside is
loaded. When the inner subgraph is loaded, input-added events are
triggered even though inputs already exist on the subgraph node.
This is resolved by adding a check for if an input of the corresponding
name already exists when adding an input.
Port of https://github.com/Comfy-Org/litegraph.js/pull/1192