Graph serialisation update - Links & Reroutes (#279)

* nit

* Add LGraph state POCO

Moves last_link_id, last_node_id and creates same for group and reroute

fix import

* Add new serialisation to LGraph

Add LGraph schema 1
Remove reroute from old serialised graph
Remove brittle inherited types
Ensure stale links are not kept when clearing graph

* Add serialisable exports

* Ensure valid LGraph.state during deserialise
This commit is contained in:
filtered
2024-11-08 10:33:54 +11:00
committed by GitHub
parent 1c40aad87c
commit fc0b8087db
5 changed files with 141 additions and 58 deletions

View File

@@ -1,5 +1,5 @@
import type { ISlotType, Dictionary, INodeFlags, INodeInputSlot, INodeOutputSlot, Point, Rect, Size } from "../interfaces"
import type { LGraph } from "../LGraph"
import type { LGraph, LGraphState } from "../LGraph"
import type { IGraphGroupFlags, LGraphGroup } from "../LGraphGroup"
import type { LGraphNode, NodeId } from "../LGraphNode"
import type { LiteGraph } from "../litegraph"
@@ -19,6 +19,17 @@ export interface Serialisable<SerialisableObject> {
asSerialisable(): SerialisableObject
}
export interface SerialisableGraph {
/** Schema version. @remarks Version bump should add to const union, which is used to narrow type during deserialise. */
version: 0 | 1
config: LGraph["config"]
state: LGraphState
groups?: ISerialisedGroup[]
nodes?: ISerialisedNode[]
links?: SerialisableLLink[]
extra?: Record<any, any>
}
/** Serialised LGraphNode */
export interface ISerialisedNode {
title?: string
@@ -40,15 +51,17 @@ export interface ISerialisedNode {
widgets_values?: TWidgetValue[]
}
/** Contains serialised graph elements */
/**
* Original implementation from static litegraph.d.ts
* Maintained for backwards compat
*/
export type ISerialisedGraph<
TNode = ReturnType<LGraphNode["serialize"]>,
TLink = ReturnType<LLink["serialize"]>,
TGroup = ReturnType<LGraphGroup["serialize"]>
> = {
last_node_id: LGraph["last_node_id"]
last_link_id: LGraph["last_link_id"]
last_reroute_id?: LGraph["last_reroute_id"]
last_node_id: NodeId
last_link_id: number
nodes: TNode[]
links: TLink[]
groups: TGroup[]