From e503f3ddcdd8ad0558790d52635ee50197cb2ac0 Mon Sep 17 00:00:00 2001 From: Amp Date: Mon, 24 Aug 2026 17:32:09 +0000 Subject: [PATCH 01/14] refactor: centralize remaining ECS entity data Amp-Thread-ID: https://ampcode.com/threads/T-01a034c8-4c01-75a8-a372-eede89c8fc37 --- .../ecs/ecs-component-entity-data-audit.md | 318 +++++++++++++++ docs/architecture/ecs/ecs-migration-plan.md | 126 ++---- .../breadcrumb/SubgraphBreadcrumb.vue | 20 +- .../rightSidePanel/RightSidePanel.test.ts | 6 +- .../subgraph/SubgraphEditor.vue | 9 +- src/components/topbar/WorkflowTabs.vue | 102 +++-- .../maskeditor/useMaskEditorSaver.ts | 2 - .../node/badgeRendererParity.test.ts | 1 + .../node/usePromotedPreviews.test.ts | 24 +- src/composables/node/usePromotedPreviews.ts | 28 +- src/core/graph/nodeShell/nodeShellState.ts | 3 + .../migration/proxyWidgetMigration.ts | 7 +- src/core/graph/subgraph/promotionUtils.ts | 11 +- src/core/graph/widgets/dynamicWidgets.ts | 1 - .../widgets/matchTypeConfiguring.test.ts | 7 + src/extensions/core/widgetInputs.ts | 26 +- .../litegraph/src/LGraph.serialise.test.ts | 138 +++++++ src/lib/litegraph/src/LGraph.test.ts | 355 +++++++++++++++++ src/lib/litegraph/src/LGraph.ts | 363 +++++++++++++++--- src/lib/litegraph/src/LGraphCanvas.ts | 34 +- src/lib/litegraph/src/LGraphGroup.test.ts | 60 +++ src/lib/litegraph/src/LGraphGroup.ts | 98 ++++- .../src/LGraphNode.nodeState.test.ts | 88 ++++- src/lib/litegraph/src/LGraphNode.test.ts | 28 ++ src/lib/litegraph/src/LGraphNode.ts | 335 ++++++++-------- .../src/LGraphNode.widgetOrder.test.ts | 81 +++- src/lib/litegraph/src/LLink.store.test.ts | 58 +++ src/lib/litegraph/src/LLink.ts | 96 ++++- src/lib/litegraph/src/canvas/LinkConnector.ts | 6 +- src/lib/litegraph/src/extensionPersistence.ts | 141 +++++++ .../infrastructure/createMutationView.test.ts | 13 + .../src/infrastructure/createMutationView.ts | 33 +- src/lib/litegraph/src/litegraph.ts | 1 + src/lib/litegraph/src/node/NodeOutputSlot.ts | 8 +- .../litegraph/src/node/slotDescriptorView.ts | 163 ++++++++ .../src/node/slotEcosystemPatterns.test.ts | 17 + .../litegraph/src/subgraph/SubgraphNode.ts | 19 +- .../subgraph/SubgraphSerialization.test.ts | 31 ++ .../subgraph/SubgraphWidgetPromotion.test.ts | 30 +- src/lib/litegraph/src/types/serialisation.ts | 6 +- .../useNodeReplacement.test.ts | 38 ++ src/renderer/core/canvas/canvasStore.ts | 6 +- .../core/canvas/litegraph/slotCalculations.ts | 20 +- .../operations/graphLayoutAttachment.ts | 179 ++++++++- .../layout/operations/layoutMutations.test.ts | 29 ++ .../core/layout/operations/layoutMutations.ts | 29 +- .../minimap/composables/useMinimap.test.ts | 3 - .../components/LGraphNode.subgraph.test.ts | 3 +- .../vueNodes/components/LGraphNode.test.ts | 3 +- .../vueNodes/components/LGraphNodePreview.vue | 3 +- .../vueNodes/components/NodeHeader.test.ts | 3 +- .../vueNodes/components/NodeSlots.test.ts | 3 +- .../vueNodes/components/NodeWidgets.test.ts | 3 +- .../composables/useNodeEventHandlers.test.ts | 41 +- .../composables/useNodeZIndex.test.ts | 14 +- .../vueNodes/composables/useNodeZIndex.ts | 6 +- .../composables/useProcessedWidgets.test.ts | 3 +- src/scripts/app.test.ts | 45 +++ src/scripts/app.ts | 69 +++- src/scripts/ui.ts | 6 +- src/services/litegraphService.ts | 7 +- src/stores/appModeStore.test.ts | 2 +- src/stores/clearNodeOwnedStoreState.ts | 22 ++ src/stores/entityIdStore.test.ts | 33 ++ src/stores/entityIdStore.ts | 37 ++ src/stores/executionOrderStore.ts | 50 +++ src/stores/graphDefinitionStore.ts | 171 +++++++++ src/stores/graphMetadataStore.test.ts | 39 ++ src/stores/graphMetadataStore.ts | 42 ++ src/stores/linkStateStore.ts | 88 +++++ src/stores/nodeOutputStore.test.ts | 39 +- src/stores/nodeOutputStore.ts | 109 +++--- src/stores/previewExposureStore.test.ts | 18 + src/stores/previewExposureStore.ts | 18 + src/stores/rerouteStore.ts | 11 + src/stores/widgetValueStore.test.ts | 14 + src/stores/widgetValueStore.ts | 77 ++++ src/types/nodeState.ts | 20 +- src/utils/__tests__/litegraphTestUtils.ts | 3 +- 79 files changed, 3561 insertions(+), 638 deletions(-) create mode 100644 docs/architecture/ecs/ecs-component-entity-data-audit.md create mode 100644 src/lib/litegraph/src/extensionPersistence.ts create mode 100644 src/lib/litegraph/src/node/slotDescriptorView.ts create mode 100644 src/stores/clearNodeOwnedStoreState.ts create mode 100644 src/stores/entityIdStore.test.ts create mode 100644 src/stores/entityIdStore.ts create mode 100644 src/stores/executionOrderStore.ts create mode 100644 src/stores/graphDefinitionStore.ts create mode 100644 src/stores/graphMetadataStore.test.ts create mode 100644 src/stores/graphMetadataStore.ts create mode 100644 src/stores/linkStateStore.ts diff --git a/docs/architecture/ecs/ecs-component-entity-data-audit.md b/docs/architecture/ecs/ecs-component-entity-data-audit.md new file mode 100644 index 00000000000..18a47ec96fc --- /dev/null +++ b/docs/architecture/ecs/ecs-component-entity-data-audit.md @@ -0,0 +1,318 @@ +# ECS Component and Entity data audit + +Status: Scoped migration implemented; structural follow-up remains +Verified: 2026-08-21 against `73c3c633f` +Scope: Implementation record for +[Centralize remaining Component and Entity data](ecs-migration-plan.md#2-centralize-remaining-component-and-entity-data) + +This audit records the baseline, implementation, and remaining structural work +for every concern in that section. `Implemented` means the scoped authority move +landed with its compatibility boundary and tests. It does not mean all legacy +facades or broader ECS architecture work have been removed. + +## Summary + +All 19 scoped concerns are implemented. Compatibility facades remain where +extensions or legacy rendering require their object identity and mutation +behavior. Five larger structural slices remain and are listed separately below. + +| Concern | Status | Current boundary | +| ----------------------------------- | ----------- | --------------------------------------------------------------------------------------------- | +| Graph and subgraph definitions | Implemented | Root-scoped records own ordered membership, registries, and subgraph definition metadata. | +| Remaining node visuals | Implemented | `NodeState` owns `boxcolor` behind an enumerable compatibility accessor. | +| Outputs and transient previews | Implemented | Store maps own reads; legacy output-map writes commit through stable nested mutation views. | +| Node ordering | Implemented | One action updates canonical layout z-index and legacy node-array order. | +| Store-driven serialization | Implemented | Graph, node, group, topology, reroute, and subgraph DTOs enumerate store authorities. | +| Legacy node geometry projection | Implemented | The layout adapter owns stable mutable geometry views and write-through. | +| Link non-topological state | Implemented | Separate persistent and runtime records follow topology lifecycle. | +| Plain slot descriptors | Implemented | Plain reactive descriptors back stable slot-class projections with native callback semantics. | +| Node properties | Implemented | `NodeState` owns the stable mutable property dictionary behind compatibility behavior. | +| Group presentation | Implemented | Graph-definition records own presentation while layout owns geometry. | +| Preview-exposure persistence | Implemented | Owner-scoped locators key runtime lookup and serialization; raw IDs are hydration-only input. | +| Extension persistence adapter | Implemented | Validated namespaced payloads and isolated hook views preserve canonical fields. | +| Graph metadata | Implemented | A graph-keyed store owns revision, config, and extra behind compatibility accessors. | +| Graph invalidation | Implemented | Graph-scoped batching coalesces composite mutations behind `_version`. | +| Unknown-node fallback | Implemented | `NodeState` owns opaque fallback DTOs behind `last_serialization`. | +| Execution order | Implemented | Graph-scoped derived records own execution order behind the node projection. | +| Entity ID allocation | Implemented | Root-keyed allocation state and shared helpers own minting and observation. | +| Delayed widget restoration | Implemented | Store-owned restoration state feeds registered and delayed widgets. | +| Widget and preview-exposure cleanup | Implemented | Removal, replacement, teardown, and failed configure clear node-owned records. | + +## Pre-migration baseline findings + +The following sections preserve the verified baseline used to derive the 19 +implementation commits. They describe the code before those commits, not the +current authority boundaries. + +### Graph definitions, metadata, execution order, and identity + +- `LGraph._nodes`, `_nodes_by_id`, `_groups`, `_subgraphs`, `config`, `extra`, + `revision`, and `state` remain the live authorities. `Subgraph.inputs`, + `outputs`, and `widgets` are ordered class-owned arrays. +- `LGraph.asSerialisable` enumerates those registries and calls class + serializers. Subgraph serialization does the same for definition metadata and + interfaces. +- `computeExecutionOrder` derives topology order and writes `node.order`; the + same mutable field remains in `ISerialisedNode` and normal node serialization. +- Allocation helpers centralize mint/observe behavior over root-shared + `LGraph.state`, but that state remains publicly replaceable and clipboard + import still advances counters through the live graph. +- Unknown node load stores the complete source DTO in `NodeState` behind the + `LGraphNode.last_serialization` compatibility accessor. Missing-node + serialization returns that DTO over current store-backed fields except for + selected compatibility overrides. + +Representative implementation: + +- `src/lib/litegraph/src/LGraph.ts`: graph fields, `add`, `removeNode`, + `computeExecutionOrder`, `configure`, `asSerialisable`, and `Subgraph`. +- `src/lib/litegraph/src/idAllocation.ts`: root-shared mint and observe helpers. +- `src/lib/litegraph/src/subgraph/subgraphDeduplication.ts`: recursive import + normalization. +- `src/lib/litegraph/src/LGraphNode.ts`: `order`, `last_serialization`, and + `serialize`. +- `src/platform/nodeReplacement/useNodeReplacement.ts`: unknown-node discovery + and replacement. + +Representative evidence: + +- `src/lib/litegraph/src/LGraph.test.ts`: shared counters, definition retention, + and cross-definition identity. +- `src/lib/litegraph/src/subgraph/SubgraphSerialization.test.ts`: interface, + membership, topology, and collision round trips. +- `src/lib/litegraph/src/idAllocation.test.ts`: mint and observe behavior. +- `src/platform/nodeReplacement/useNodeReplacement.test.ts`: unknown-node + replacement and compatibility values. + +### Node, group, slot, link, and layout projections + +- `NodeState` owns tracked shell fields including foreground/background color, + shape, flags, and mode. `boxcolor` remains a plain `LGraphNode` property. +- Persistent node/group/reroute geometry and node z-index live in `layoutStore`. + `LGraphNode` still owns `_posSize`, mutation views, geometry-version checks, + and layout write-through. These provide the stable indexed `pos` and `size` + references expected by extensions. +- `bringToFront` updates layout z-index and legacy `_nodes` order. + `sendToBack` changes only `_nodes`; renderer switching later re-sorts that + array from layout z-index. +- `NodeState.inputs` and `outputs` contain slot class instances. Their + connectivity facades read `linkStore`, while descriptor fields, node + back-references, drawing, geometry, and callbacks remain class behavior. +- `LLink` topology remains in `linkStore`. `linkStateStore` owns separately + categorized persistent color and runtime execution, interaction, render, and + hit-test state behind compatible `LLink` accessors and the same lifecycle. +- Group geometry is store-backed, but presentation fields remain on + `LGraphGroup`. + +Representative implementation: + +- `src/types/nodeState.ts`; `src/lib/litegraph/src/LGraphNode.ts`. +- `src/renderer/core/layout/operations/graphLayoutAttachment.ts` and + `layoutMutations.ts`. +- `src/lib/litegraph/src/LGraphCanvas.ts`: `bringToFront` and `sendToBack`. +- `src/renderer/core/canvas/litegraph/arrangeForLegacyRender.ts`. +- `src/lib/litegraph/src/node/SlotBase.ts`, `NodeSlot.ts`, `NodeInputSlot.ts`, and + `NodeOutputSlot.ts`. +- `src/lib/litegraph/src/LLink.ts` and + `src/renderer/core/canvas/litegraph/litegraphLinkAdapter.ts`. +- `src/lib/litegraph/src/LGraphGroup.ts`. + +Representative evidence: + +- `src/stores/nodeDataStore.test.ts`: shell transfer and slot-array identity. +- `src/lib/litegraph/src/LGraphNode.test.ts`: stable geometry views and + store-backed indexed writes. +- `src/renderer/core/layout/operations/layoutMutations.test.ts` and + `src/renderer/core/canvas/litegraph/arrangeForLegacyRender.test.ts`: z-order + mutation and legacy projection. +- `src/lib/litegraph/src/node/NodeInputSlot.test.ts`, + `NodeOutputSlot.test.ts`, and `slotEcosystemPatterns.test.ts`: derived + connectivity and extension compatibility. +- `src/lib/litegraph/src/canvas/LinkConnector.core.test.ts`: transient link + interaction state. + +### Outputs, properties, widgets, and preview exposures + +- `nodeOutputStore` maintains reactive output and preview maps, locator-aware + removal, object-URL cleanup, and workflow stash/restore. Its read/write paths + still synchronize in both directions with `app.nodeOutputs`, + `app.nodePreviewImages`, `node.images`, and `node.imgs`. +- `LGraphNode.properties` remains directly mutable. First-party code writes it + both through `setProperty` and directly. Serialization clones the whole map. +- Registered widget values and order are store-backed. General node configure + still restores from named or positional wire shadows, and delayed widget + creation plus some first-party consumers still read or write those shadows. +- `previewExposureStore` is further along than the prior plan wording implied: + subgraph configure hydrates it from the compatibility property, runtime reads + use it, and serialization projects from it. The remaining gap is scoped + identity and lifecycle cleanup, not initial authority. + +Representative implementation: + +- `src/stores/nodeOutputStore.ts`; `src/scripts/app.ts`. +- `src/lib/litegraph/src/LGraphNode.ts`: `properties`, `configure`, + `setProperty`, and `serialize`. +- `src/stores/widgetValueStore.ts`; `src/lib/litegraph/src/widgets/BaseWidget.ts`; + `src/extensions/core/widgetInputs.ts`. +- `src/stores/previewExposureStore.ts` and + `src/lib/litegraph/src/subgraph/SubgraphNode.ts`. + +Representative evidence: + +- `src/stores/nodeOutputStore.test.ts` and + `nodeOutputStore.workflowSwitch.test.ts`: mirrored maps, legacy image + projection, cleanup, and workflow switching. +- `src/lib/litegraph/src/LGraphNode.widgetOrder.test.ts`: named/positional + restoration and reorder behavior. +- `src/lib/litegraph/src/subgraph/SubgraphWidgetPromotion.test.ts`: store-backed + promoted values and preview-exposure round trips. +- `src/stores/previewExposureStore.test.ts`: graph isolation and root cleanup. + +### Serialization and extension boundary + +- `LGraph.asSerialisable`, `LGraphNode.serialize`, and the group/subgraph + serializers remain the wire-format oracle. No store-record serializer exists. +- Node and graph `onSerialize` hooks receive the complete mutable canonical DTO. + The repository has no adapter that extracts a validated namespaced extension + payload or rejects canonical-field writes. +- Prompt construction serializes the workflow and then invokes asynchronous + widget `serializeValue` hooks. At least one first-party hook mutates the + positional workflow widget projection, so prompt derivation is not isolated + from persistence DTOs. + +Representative implementation: + +- `src/lib/litegraph/src/LGraph.ts`: `asSerialisable` and graph `onSerialize`. +- `src/lib/litegraph/src/LGraphNode.ts`: node `serialize` and `onSerialize`. +- `src/utils/executionUtil.ts`: workflow and prompt construction. +- `src/extensions/core/dynamicPrompts.ts`: queue-time workflow projection write. + +## Corrections established by the baseline audit + +1. Preview-exposure persistence has already moved to a store for runtime reads + and serialization. Remaining work is scoped host/source cleanup and the + compatibility hydration boundary. +2. `_version` increments use one method, but mutation callers still own + invalidation policy. The missing boundary is centralized policy and batching, + not an increment helper. +3. `nodeOutputStore` already owns substantial locator, object-URL, and workflow + cleanup. It is not authoritative because it still reads legacy maps and + accepts live node image fields as reverse synchronization inputs. +4. Slot connectivity and persistent geometry already have store authorities. + The remaining work concerns plain descriptors and compatibility projection + ownership, not moving connectivity or duplicating geometry. +5. Widget and preview-exposure stores have root cleanup. They lack complete + node/owner cleanup across remove, replacement, failed configure, and released + subgraphs. + +## Implemented migration sequence rationale + +The implemented sequence followed the serialization dependencies: + +1. Finished narrow authorities and lifecycle first: `boxcolor`, ordering, + output/preview directionality, widget/preview owner cleanup, and graph + metadata/identity ownership. +2. Moved class projections behind compatibility adapters: node geometry, + slot descriptors, group presentation, link runtime categories, properties, + and unknown-node fallback records. +3. Parsed widget and extension compatibility payloads at workflow boundaries + and removed first-party writes to wire shadows. +4. Added a store-record serializer beside the mutable serializer and proved + normalized differential parity before changing production serialization. + +This ordering does not require a universal ECS world, command replay, or a new +undo model. + +## Progress record + +Each concern is completed in its own commit. The order follows the dependency +sequence above rather than the summary-table order. + +| Sequence | Concern | Status | Commit | Result | +| -------- | ----------------------------------- | -------- | ------------ | ------------------------------------------------------------------------------------------------ | +| 1 | Remaining node visuals | Complete | `fa5dcd8ca` | `boxcolor` moved into `NodeState` with an enumerable tracked compatibility accessor. | +| 2 | Node ordering | Complete | `06ae14e8b` | One action now updates layout z-index and the legacy node array for front/back order. | +| 3 | Outputs and transient previews | Complete | `570b38c2a` | Store maps own reads; the legacy output map commits mutations through a compatibility view. | +| 4 | Preview-exposure persistence | Complete | `2212ed302` | Host exposures use owner-scoped locators; raw node-ID entries are hydration-only input. | +| 5 | Widget and preview-exposure cleanup | Complete | `ecd148fa0` | Node removal, replacement, teardown, and failed configure clear node-owned store records. | +| 6 | Graph metadata | Complete | `298bf3da0` | Revision, config, and extra now use graph-keyed store records behind compatibility accessors. | +| 7 | Entity ID allocation | Complete | `7fbd10624` | Root-keyed allocation state now backs graph accessors; clipboard allocation uses shared helpers. | +| 8 | Graph and subgraph definitions | Complete | `8923225fb` | Root-scoped records now own ordered membership, the registry, and subgraph definition metadata. | +| 9 | Legacy node geometry projection | Complete | `854f68fb2` | The layout adapter now owns stable legacy views, synchronization, and geometry write-through. | +| 10 | Plain slot descriptors | Complete | `cc53dad291` | Plain store descriptors now back stable extension-visible slot class projections. | +| 11 | Group presentation | Complete | `3ac7b726a` | Graph-definition records now back mutable presentation fields and serialization. | +| 12 | Link non-topological state | Complete | `38b40988a` | Separate persistent and runtime records now back compatible link fields with topology lifecycle. | +| 13 | Node properties | Complete | `f75b30dbd` | Node state now owns stable mutable properties across lifecycle, callbacks, and serialization. | +| 14 | Unknown-node fallback | Complete | `ba4757f4c` | Node state now owns unknown-node fallback records behind the compatibility accessor. | +| 15 | Delayed widget restoration | Complete | `972947d939` | Store-owned restoration state now hydrates configured and delayed widgets from wire shadows. | +| 16 | Extension persistence adapter | Complete | `287bf33f38` | Validated payloads round trip through a namespace while legacy hooks mutate isolated DTO views. | +| 17 | Execution order | Complete | `7f6c2783dd` | Graph-scoped derived records own execution order behind the compatible node projection. | +| 18 | Graph invalidation | Complete | `335b69420c` | Graph-scoped batching coalesces composite invalidations behind the compatible version counter. | +| 19 | Store-driven serialization | Complete | `458d292f1` | Store authorities now build wire DTOs after normalized parity against mutable serialization. | + +Final full-suite verification also tightened slot-projection deletion and +updated test fixtures to preserve their store scope and projected-array +contract. + +Post-review regression verification restored legacy output-map write-through, +kept widget output changes observable by preview rendering, and removed a +redundant MatchType slot wrapper that hid descriptor updates from Vue. + +| Review follow-up | Commit | Result | +| ----------------------------------- | ----------- | --------------------------------------------------------------------------------------- | +| Graph extension isolation | `b8fdbf860` | Configure payloads remain in isolated persistence records and hook views. | +| Nested legacy output mutations | `cceb56330` | Output fields and their immediate arrays commit through stable mutation views. | +| Populated graph ID changes | `17546ff13` | Populated graphs reject ID reassignment instead of splitting scoped store state. | +| Native slot callback array behavior | `73c3c633f` | Callback-bearing array methods now receive and mutate the extension-visible slot array. | + +## Verified current limitations + +- Slot descriptors remain behind a virtual slot-class array. Native callback + behavior is preserved, but complete reflection compatibility still requires + restoring real extension-visible slot arrays. +- `graphDefinitionStore` still contains live node, group, subgraph, and + interface instances. Persisted UUIDs remain runtime store keys; populated + graph IDs are therefore immutable until instance-scoped identity replaces + that keying model. +- Store-driven persistence joins and compatibility adaptation still live in + and around `LGraph`. +- Replacement configure is destructive on failure, while additive configure + has only best-effort rollback. +- `SubgraphNode` still owns promoted-widget projection, preview hydration, and + host persistence responsibilities. + +## Structural follow-up plan + +These are future structural improvements, not incomplete rows in the scoped +19-concern migration. They should proceed as vertical slices rather than adding +more projections to `LGraph` and `LGraphNode`. + +1. **Replace the slot virtual array.** Keep extension-visible input and output + arrays as real slot-class arrays. Derive plain serializable descriptors at + the node-state boundary until a complete ID-based slot record can replace + both representations. Remove `slotDescriptorView` only after ecosystem + tests cover indexed access, mutation methods, reflection, and stable slot + identity. +2. **Introduce one plain graph record.** Replace live node, group, subgraph, and + interface instances in `graphDefinitionStore` with ordered IDs and plain + component records. Resolve runtime adapters through one instance-scoped + registry. The instance scope must allow two loaded graphs with the same + persisted UUID without rekeying one graph's state over the other. +3. **Extract persistence from `LGraph`.** Move store joins, invariant checks, + extension payload adaptation, and DTO construction into a focused graph + persistence adapter. Keep the mutable serializer as the compatibility + oracle until canonicalized DTOs compare with exact equality for root graphs, + nested subgraphs, groups, reroutes, widgets, unknown nodes, and extensions. +4. **Make configure transactional.** Stage graph records and topology before + publishing them, then swap atomically. This replaces the current best-effort + rollback for additive configuration and gives failed root replacement the + same no-partial-state guarantee. +5. **Decompose subgraph hosting.** Move promoted-widget projection, preview + exposure hydration, and host persistence out of `SubgraphNode` into focused + adapters, returning the class below the 1,000-line boundary without adding + another general-purpose manager. + +Each slice needs focused unit tests for its pure boundary plus the existing +Playwright bridge-history scenarios for delete/undo/redo, renderer switching, +navigation, and reload. Production authority should switch only after exact +serializer parity and the browser suite pass together. diff --git a/docs/architecture/ecs/ecs-migration-plan.md b/docs/architecture/ecs/ecs-migration-plan.md index dff2ddd8689..5e24ae8b2bd 100644 --- a/docs/architecture/ecs/ecs-migration-plan.md +++ b/docs/architecture/ecs/ecs-migration-plan.md @@ -1,7 +1,7 @@ # ECS migration plan -Status: Partial -Verified: 2026-08-20 against `13a302eadda871b939b148ecb87e3d845ceefff2` +Status: Scoped data centralization implemented; structural follow-up remains +Verified: 2026-08-21 against `73c3c633f` This plan records completed ECS migration work and the work that remains. Detailed evidence and unresolved risks are in the focused audits under @@ -86,58 +86,23 @@ not separate architecture changes. ## Current boundaries -The following authority boundaries remain in this phase: - -- `NodeState.inputs` and `outputs` contain slot class instances. Slot data is - not yet a plain component model. -- Widget order exists in both the store and `LGraphNode.widgets` during the - compatibility period. Vue filters store order through live widget objects and - still invokes their callbacks. -- Live graph object registries and store records coexist and must be attached - and detached together. -- Graph membership, graph metadata, groups, subgraph definitions and ordered - interfaces, node properties, group presentation, and extension-owned data - remain authoritative on live `LGraph`, `Subgraph`, node, and group objects. -- `LGraphNode.boxcolor` remains class-owned durable render state outside - `NodeState`, despite other shell visual fields being store-backed. -- Unknown nodes retain `last_serialization`, an opaque full-node record that - overrides normal serialization and can shadow migrated node, slot, widget, - and render state. -- Execution `order` is recomputed from topology but also restored and persisted - on `LGraphNode`; its derived-versus-wire-compatibility contract is not yet - enforced. -- Serialized `LGraph.state` allocation counters are directly mutable and - influence future durable IDs outside a command/store boundary. -- `node.widgets_values` and `widgets_values_named` form another widget-value - representation used for delayed dynamic-widget restoration and direct - production writes; property-bound widgets can also mirror values in - `node.properties`. -- `nodeOutputStore` mirrors `app.nodeOutputs` and `app.nodePreviewImages`, and - legacy render output also projects into live node image fields. It is not yet - the sole output authority. -- `LGraph._version` is manually incremented across graph, node, canvas, widget, - slot, and subgraph mutation callers rather than derived at one committed - transaction boundary. -- Durable graph `config`, `extra`, and `revision` metadata remains class-owned - and directly mutable; graph metadata is not yet included in the explicit - command schema. -- Node and graph `onSerialize` hooks can mutate complete persistence DTOs, while - generic configure assignment and `onConfigure` expose equivalent load-time - mutation channels. These extension hooks can override store-backed fields - without schema, ownership, replay, transaction, or undo boundaries. -- Prompt construction awaits arbitrary widget `serializeValue` hooks. Current - hooks can mutate widget/workflow shadows, choose random values, capture media, - upload files, and update UI state, so execution input assembly is neither a - pure store read nor isolated from unrelated effects. -- Node z-order exists in layout state and legacy `_nodes` ordering. - `sendToBack` currently updates only the legacy ordering path. -- Node and input-slot error flags are mutable legacy drawing projections of - error-store state and require synchronization by app hooks. -- Widget and preview stores lack the owner-scoped cleanup available in node, - link, and reroute stores. Subgraph release and failed operations can retain - records until root cleanup. -- Link paths, slot bounds, and hit-test geometry are transient renderer caches, - not persistent layout components. +The scoped authority migration is complete. These structural boundaries remain: + +- Slot descriptors sit behind a virtual extension-visible slot-class array. + Native callback semantics are preserved, but complete reflection compatibility + requires restoring real arrays at that boundary. +- `graphDefinitionStore` still contains live adapter instances, and persisted + graph UUIDs remain runtime store keys. Populated graph IDs are immutable until + an instance-scoped registry replaces that keying model. +- Store-driven persistence joins and extension adaptation still live in and + around `LGraph`. +- Replacement configure is destructive on failure; additive configure provides + only best-effort rollback rather than an atomic staged transaction. +- `SubgraphNode` still combines promoted-widget projection, preview hydration, + and host persistence responsibilities. +- Prompt construction still awaits effectful widget `serializeValue` hooks. +- Link paths, slot bounds, and hit-test geometry remain transient renderer + caches rather than persistent components. The current implementation also has no system-wide command protocol, workflow transaction, command replay, or command-based undo. These are factual limits, @@ -178,48 +143,16 @@ unless they cover a new invariant. ### 2. Centralize remaining Component and Entity data -- Add plain store-owned graph and subgraph definition records for membership, - metadata, ordered entity IDs, and subgraph interfaces. Keep live object - registries as runtime adapters rather than serialization authorities. -- Move remaining durable node visuals, including `boxcolor`, into the - store-owned shell or a focused visual component. -- Make `nodeOutputStore` authoritative for output and preview maps; expose - legacy `app` and node-image fields as compatibility projections. -- Make one layout ordering action update both Vue z-index and the legacy draw - projection; remove the asymmetric `sendToBack` mutation. -- Extract serialization only after stores contain sufficient authoritative - data and parity can be checked against the existing wire format. -- Move legacy geometry projection ownership out of `LGraphNode` without - changing `pos` and `size` extension behavior. -- Separate remaining link visual/runtime state when a renderer or interaction - system can consume plain records directly. -- Extract plain slot state only when it removes a real class dependency; retain - array order semantics and keep connectivity in `linkStore`. -- Move node properties, group presentation, preview-exposure persistence, and - extension-owned durable fields behind scoped store actions while retaining - compatibility property facades. -- Preserve serialization compatibility through a controlled adapter: isolate - namespaced extension payloads as validated plain data and prevent persistence - hooks from mutating canonical workflow and store-backed fields. -- Move graph `config`, `extra`, and `revision` into a focused store-owned plain - data record while preserving serialization compatibility. -- Derive graph invalidation from authoritative store revisions or one - centralized compatibility projection; remove scattered caller-owned - `_version` increments. -- Store opaque unknown-node fallback records by scoped node identity, define - their remapping and replacement lifecycle, and retain `last_serialization` - only as a compatibility facade. -- Treat execution order as a derived topology projection. Define whether - serialized `order` is ignored and recomputed or retained only as explicit - wire compatibility, not as an independent mutation channel. -- Move allocation counters behind the workflow identity owner. Creation/import - must no longer derive IDs from mutable state distributed across class and - import paths. -- Route delayed widget restoration through scoped store state and isolate - serialized widget arrays/maps as wire compatibility projections rather than - production mutation channels. -- Add owner/node cleanup for widget and preview records before treating failed - configure, replacement, and subgraph release as contained operations. +All 19 scoped concerns are implemented. The authoritative commit-by-commit +record and current compatibility boundaries are in the +[Component and Entity data audit](ecs-component-entity-data-audit.md#progress-record). + +Remaining work is the audit's five-part +[structural follow-up plan](ecs-component-entity-data-audit.md#structural-follow-up-plan): +restore real slot arrays, introduce an instance-scoped plain graph record, +extract persistence from `LGraph`, make configure transactional, and decompose +subgraph hosting. These improvements do not reopen the completed 19-concern +authority migration. ### 3. Retire duplicate state and synchronization bridges @@ -289,6 +222,7 @@ CRDT compatibility are explicitly not completion criteria for this phase. - [Executive summary](ecs-migration-summary.md) - [Decision traceability](ecs-decision-traceability.md) - [State authority audit](ecs-state-authority-audit.md) +- [Component and Entity data audit](ecs-component-entity-data-audit.md) - [Lifecycle audit](ecs-lifecycle-audit.md) - [Mutation audit](ecs-mutation-audit.md) - [Identity and scope audit](ecs-identity-scope-audit.md) diff --git a/src/components/breadcrumb/SubgraphBreadcrumb.vue b/src/components/breadcrumb/SubgraphBreadcrumb.vue index 25ed74b98c0..99f25767790 100644 --- a/src/components/breadcrumb/SubgraphBreadcrumb.vue +++ b/src/components/breadcrumb/SubgraphBreadcrumb.vue @@ -54,7 +54,7 @@ import Breadcrumb from 'primevue/breadcrumb' import Button from 'primevue/button' import type { MenuItem } from 'primevue/menuitem' -import { computed, onUpdated, ref, watch } from 'vue' +import { computed, onBeforeUnmount, onMounted, onUpdated, ref } from 'vue' import SubgraphBreadcrumbItem from '@/components/breadcrumb/SubgraphBreadcrumbItem.vue' import WorkflowActionsDropdown from '@/components/common/WorkflowActionsDropdown.vue' @@ -135,20 +135,14 @@ const handleBackClick = () => { void useCommandStore().execute('Comfy.Graph.ExitSubgraph') } -const breadcrumbElement = computed(() => { - if (!breadcrumbRef.value) return null - - const el = (breadcrumbRef.value as unknown as { $el: HTMLElement }).$el - const list = el?.querySelector('.p-breadcrumb-list') as HTMLElement - return list -}) - // Check for overflow on breadcrumb items and collapse/expand the breadcrumb to fit let overflowObserver: ReturnType | undefined -watch(breadcrumbElement, (el) => { - overflowObserver?.dispose() - overflowObserver = undefined +onMounted(() => { + const breadcrumb = breadcrumbRef.value + if (!breadcrumb) return + const root = (breadcrumb as unknown as { $el: HTMLElement }).$el + const el = root.querySelector('.p-breadcrumb-list') if (!el) return overflowObserver = useOverflowObserver(el, { @@ -193,6 +187,8 @@ watch(breadcrumbElement, (el) => { }) }) +onBeforeUnmount(() => overflowObserver?.dispose()) + // If e.g. the workflow name changes, we need to check the overflow again onUpdated(() => { if (!overflowObserver?.disposed.value) { diff --git a/src/components/rightSidePanel/RightSidePanel.test.ts b/src/components/rightSidePanel/RightSidePanel.test.ts index 9aaa4b1a149..d6234d8a034 100644 --- a/src/components/rightSidePanel/RightSidePanel.test.ts +++ b/src/components/rightSidePanel/RightSidePanel.test.ts @@ -1,6 +1,6 @@ import { createTestingPinia } from '@pinia/testing' import { render, screen } from '@testing-library/vue' -import { setActivePinia } from 'pinia' +import { getActivePinia, setActivePinia } from 'pinia' import { beforeEach, describe, expect, it, vi } from 'vitest' import { markRaw, nextTick } from 'vue' import { createI18n } from 'vue-i18n' @@ -53,7 +53,9 @@ function renderPanel( node: LGraphNode } ) { - const pinia = createTestingPinia({ createSpy: vi.fn, stubActions: false }) + const pinia = + (graphContext && getActivePinia()) || + createTestingPinia({ createSpy: vi.fn, stubActions: false }) setActivePinia(pinia) const rootGraph = graphContext?.rootGraph ?? new LGraph() diff --git a/src/components/rightSidePanel/subgraph/SubgraphEditor.vue b/src/components/rightSidePanel/subgraph/SubgraphEditor.vue index 9d6d0e3f975..8526ffc8098 100644 --- a/src/components/rightSidePanel/subgraph/SubgraphEditor.vue +++ b/src/components/rightSidePanel/subgraph/SubgraphEditor.vue @@ -32,7 +32,10 @@ import type { IBaseWidget } from '@/lib/litegraph/src/types/widgets' import { useCanvasStore } from '@/renderer/core/canvas/canvasStore' import AsyncSearchInput from '@/components/ui/search-input/AsyncSearchInput.vue' import { useLitegraphService } from '@/services/litegraphService' -import { usePreviewExposureStore } from '@/stores/previewExposureStore' +import { + getPreviewExposureHostLocator, + usePreviewExposureStore +} from '@/stores/previewExposureStore' import { useRightSidePanelStore } from '@/stores/workspace/rightSidePanelStore' import { cn } from '@comfyorg/tailwind-utils' @@ -114,7 +117,7 @@ const activePromotedRows = computed({ }) function getActivePreviewRows(node: SubgraphNode): PreviewRow[] { - const hostLocator = String(node.id) + const hostLocator = getPreviewExposureHostLocator(node) const rootGraphId = node.rootGraph.id const exposures = previewExposureStore.getExposures(rootGraphId, hostLocator) return exposures.flatMap((exposure): PreviewRow[] => { @@ -283,7 +286,7 @@ function demoteRow(row: ActiveRow) { } previewExposureStore.removeExposure( subgraphNode.rootGraph.id, - String(subgraphNode.id), + getPreviewExposureHostLocator(subgraphNode), row.exposure.name ) refreshActiveNodeRendering() diff --git a/src/components/topbar/WorkflowTabs.vue b/src/components/topbar/WorkflowTabs.vue index b6b9fa92965..2a19b61d246 100644 --- a/src/components/topbar/WorkflowTabs.vue +++ b/src/components/topbar/WorkflowTabs.vue @@ -106,7 +106,15 @@ import { useScroll } from '@vueuse/core' import ScrollPanel from 'primevue/scrollpanel' import SelectButton from 'primevue/selectbutton' -import { computed, nextTick, onUpdated, ref, watch } from 'vue' +import { + computed, + nextTick, + onBeforeUnmount, + onMounted, + onUpdated, + ref, + watch +} from 'vue' import type { WatchStopHandle } from 'vue' import CurrentUserButton from '@/components/topbar/CurrentUserButton.vue' import LoginButton from '@/components/topbar/LoginButton.vue' @@ -213,12 +221,7 @@ const handleWheel = (event: WheelEvent) => { }) } -const scrollContent = computed( - () => - (containerRef.value?.querySelector( - '.p-scrollpanel-content' - ) as HTMLElement | null) ?? null -) +const scrollContent = ref(null) const scroll = (direction: number) => { const el = scrollContent.value @@ -259,52 +262,45 @@ let overflowObserver: ReturnType | null = null let stopArrivedWatch: WatchStopHandle | null = null let stopOverflowWatch: WatchStopHandle | null = null -watch( - scrollContent, - (el, _prev, onCleanup) => { - stopArrivedWatch?.() - stopOverflowWatch?.() - overflowObserver?.dispose() - - if (!el) return - - const scrollState = useScroll(el) - - stopArrivedWatch = watch( - [ - () => scrollState.arrivedState.left, - () => scrollState.arrivedState.right - ], - ([atLeft, atRight]) => { - leftArrowEnabled.value = !atLeft - rightArrowEnabled.value = !atRight - }, - { immediate: true } - ) - - overflowObserver = useOverflowObserver(el) - stopOverflowWatch = watch( - overflowObserver.isOverflowing, - (isOverflow) => { - showOverflowArrows.value = isOverflow - if (!isOverflow) return - void nextTick(() => { - // Force a new check after arrows are updated - scrollState.measure() - void ensureActiveTabVisible({ waitForDom: false }) - }) - }, - { immediate: true } - ) - - onCleanup(() => { - stopArrivedWatch?.() - stopOverflowWatch?.() - overflowObserver?.dispose() - }) - }, - { immediate: true } -) +onMounted(() => { + const el = containerRef.value?.querySelector( + '.p-scrollpanel-content' + ) + if (!el) return + scrollContent.value = el + + const scrollState = useScroll(el) + + stopArrivedWatch = watch( + [() => scrollState.arrivedState.left, () => scrollState.arrivedState.right], + ([atLeft, atRight]) => { + leftArrowEnabled.value = !atLeft + rightArrowEnabled.value = !atRight + }, + { immediate: true } + ) + + overflowObserver = useOverflowObserver(el) + stopOverflowWatch = watch( + overflowObserver.isOverflowing, + (isOverflow) => { + showOverflowArrows.value = isOverflow + if (!isOverflow) return + void nextTick(() => { + // Force a new check after arrows are updated + scrollState.measure() + void ensureActiveTabVisible({ waitForDom: false }) + }) + }, + { immediate: true } + ) +}) + +onBeforeUnmount(() => { + stopArrivedWatch?.() + stopOverflowWatch?.() + overflowObserver?.dispose() +}) onUpdated(() => { if (!overflowObserver?.disposed.value) { diff --git a/src/composables/maskeditor/useMaskEditorSaver.ts b/src/composables/maskeditor/useMaskEditorSaver.ts index 2fccc402f86..2d7738b0de6 100644 --- a/src/composables/maskeditor/useMaskEditorSaver.ts +++ b/src/composables/maskeditor/useMaskEditorSaver.ts @@ -279,8 +279,6 @@ export function useMaskEditorSaver() { ): void { const mainRef = outputData.paintedMaskedImage.ref - node.images = [mainRef] - const imageWidget = node.widgets?.find((w) => w.name === 'image') if (imageWidget) { const widgetValue = diff --git a/src/composables/node/badgeRendererParity.test.ts b/src/composables/node/badgeRendererParity.test.ts index 1349dd2d2b0..1e2758ea90a 100644 --- a/src/composables/node/badgeRendererParity.test.ts +++ b/src/composables/node/badgeRendererParity.test.ts @@ -69,6 +69,7 @@ function vueBadgeText(node: LGraphNode): string { inputs: node.inputs, mode: node.mode, outputs: node.outputs, + properties: node.properties, title: node.title, type: node.type } diff --git a/src/composables/node/usePromotedPreviews.test.ts b/src/composables/node/usePromotedPreviews.test.ts index 0ebdc509321..31d61be58ec 100644 --- a/src/composables/node/usePromotedPreviews.test.ts +++ b/src/composables/node/usePromotedPreviews.test.ts @@ -7,7 +7,10 @@ import { createTestSubgraphNode } from '@/lib/litegraph/src/subgraph/__fixtures__/subgraphHelpers' import { useNodeOutputStore } from '@/stores/nodeOutputStore' -import { usePreviewExposureStore } from '@/stores/previewExposureStore' +import { + getPreviewExposureHostLocator, + usePreviewExposureStore +} from '@/stores/previewExposureStore' import { createNodeLocatorId } from '@/types/nodeIdentification' import { toNodeId } from '@/types/nodeId' @@ -93,7 +96,7 @@ function exposePreview( ) { usePreviewExposureStore().addExposure( setup.subgraphNode.rootGraph.id, - String(setup.subgraphNode.id), + getPreviewExposureHostLocator(setup.subgraphNode), { sourceNodeId, sourcePreviewName } ) } @@ -300,7 +303,7 @@ describe(usePromotedPreviews, () => { const store = usePreviewExposureStore() store.addExposure( outerSetup.subgraphNode.rootGraph.id, - String(innerHost.id), + getPreviewExposureHostLocator(innerHost), { sourceNodeId: String(leafNode.id), sourcePreviewName: CANVAS_IMAGE_PREVIEW_WIDGET @@ -308,7 +311,7 @@ describe(usePromotedPreviews, () => { ) store.addExposure( outerSetup.subgraphNode.rootGraph.id, - String(outerSetup.subgraphNode.id), + getPreviewExposureHostLocator(outerSetup.subgraphNode), { sourceNodeId: String(innerHost.id), sourcePreviewName: CANVAS_IMAGE_PREVIEW_WIDGET @@ -348,10 +351,9 @@ describe(usePromotedPreviews, () => { const secondHost = createTestSubgraphNode(outerSetup.subgraph, { id: 12 }) const firstHostLocator = String(firstHost.id) const secondHostLocator = String(secondHost.id) - const firstNestedLocator = `${firstHostLocator}:${innerHost.id}` - const secondNestedLocator = `${secondHostLocator}:${innerHost.id}` - const firstLeafExecutionId = `${firstNestedLocator}:${leafNode.id}` - const secondLeafExecutionId = `${secondNestedLocator}:${leafNode.id}` + const nestedLocator = getPreviewExposureHostLocator(innerHost) + const firstLeafExecutionId = `${firstHostLocator}:${innerHost.id}:${leafNode.id}` + const secondLeafExecutionId = `${secondHostLocator}:${innerHost.id}:${leafNode.id}` const store = usePreviewExposureStore() store.addExposure(firstHost.rootGraph.id, firstHostLocator, { @@ -362,11 +364,7 @@ describe(usePromotedPreviews, () => { sourceNodeId: String(innerHost.id), sourcePreviewName: CANVAS_IMAGE_PREVIEW_WIDGET }) - store.addExposure(firstHost.rootGraph.id, firstNestedLocator, { - sourceNodeId: String(leafNode.id), - sourcePreviewName: CANVAS_IMAGE_PREVIEW_WIDGET - }) - store.addExposure(firstHost.rootGraph.id, secondNestedLocator, { + store.addExposure(firstHost.rootGraph.id, nestedLocator, { sourceNodeId: String(leafNode.id), sourcePreviewName: CANVAS_IMAGE_PREVIEW_WIDGET }) diff --git a/src/composables/node/usePromotedPreviews.ts b/src/composables/node/usePromotedPreviews.ts index 03885471e78..c7689e5def8 100644 --- a/src/composables/node/usePromotedPreviews.ts +++ b/src/composables/node/usePromotedPreviews.ts @@ -5,7 +5,10 @@ import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' import { SubgraphNode } from '@/lib/litegraph/src/subgraph/SubgraphNode' import type { UUID } from '@/utils/uuid' import { useNodeOutputStore } from '@/stores/nodeOutputStore' -import { usePreviewExposureStore } from '@/stores/previewExposureStore' +import { + getPreviewExposureHostLocator, + usePreviewExposureStore +} from '@/stores/previewExposureStore' import type { NodeId } from '@/types/nodeId' import { appendNodeExecutionId, @@ -78,7 +81,7 @@ export function usePromotedPreviews( if (node.isDetached) return [] const rootGraphId = node.rootGraph.id - const hostLocator = String(node.id) + const hostLocator = getPreviewExposureHostLocator(node) const exposures = previewExposureStore.getExposures( rootGraphId, hostLocator @@ -88,6 +91,9 @@ export function usePromotedPreviews( const hostNodesByLocator = new Map([ [hostLocator, node] ]) + const hostExecutionsByLocator = new Map([ + [hostLocator, String(node.id)] + ]) function resolveNestedHost( rootGraphId: UUID, @@ -98,14 +104,14 @@ export function usePromotedPreviews( const sourceNode = currentHost?.subgraph.getNodeById(sourceNodeId) if (!(sourceNode instanceof SubgraphNode)) return undefined - const pathLocator = `${currentHostLocator}:${sourceNode.id}` - const definitionLocator = String(sourceNode.id) - const hasPathExposures = - previewExposureStore.getExposures(rootGraphId, pathLocator).length > 0 - const nestedHostLocator = hasPathExposures - ? pathLocator - : definitionLocator + const nestedHostLocator = getPreviewExposureHostLocator(sourceNode) + const currentExecutionId = hostExecutionsByLocator.get(currentHostLocator) + if (!currentExecutionId) return undefined hostNodesByLocator.set(nestedHostLocator, sourceNode) + hostExecutionsByLocator.set( + nestedHostLocator, + `${currentExecutionId}:${sourceNode.id}` + ) return { rootGraphId, hostNodeLocator: nestedHostLocator } } @@ -123,11 +129,13 @@ export function usePromotedPreviews( const leafHostLocator = resolved?.steps.at(-1)?.hostNodeLocator ?? hostLocator const leafHost = hostNodesByLocator.get(leafHostLocator) ?? node + const leafHostExecutionId = + hostExecutionsByLocator.get(leafHostLocator) ?? String(node.id) const interiorNode = leafHost.subgraph.getNodeById(leaf.sourceNodeId) if (!interiorNode) return [] const leafExecutionId = appendNodeExecutionId( - leafHostLocator, + leafHostExecutionId, leaf.sourceNodeId ) if (!leafExecutionId) return [] diff --git a/src/core/graph/nodeShell/nodeShellState.ts b/src/core/graph/nodeShell/nodeShellState.ts index cee9abdc383..cf64ced83a1 100644 --- a/src/core/graph/nodeShell/nodeShellState.ts +++ b/src/core/graph/nodeShell/nodeShellState.ts @@ -36,6 +36,7 @@ export function createNodeShellState( inputs: shallowReactive([]), mode: LGraphEventMode.ALWAYS, outputs: shallowReactive([]), + properties: {}, title: title || 'Unnamed', type: type ?? '', titleMode @@ -128,7 +129,9 @@ function transferNodeState(node: LGraphNode, replacement: LGraphNode): void { const { graphId: _graphId, id: _id, ...replacementState } = replacement._state Object.assign(registeredState, { bgcolor: undefined, + boxcolor: undefined, color: undefined, + lastSerialization: undefined, resizable: undefined, shape: undefined, showAdvanced: undefined, diff --git a/src/core/graph/subgraph/migration/proxyWidgetMigration.ts b/src/core/graph/subgraph/migration/proxyWidgetMigration.ts index bb53e7cbeea..424093ad07d 100644 --- a/src/core/graph/subgraph/migration/proxyWidgetMigration.ts +++ b/src/core/graph/subgraph/migration/proxyWidgetMigration.ts @@ -29,7 +29,10 @@ import type { import { isWidgetValue } from '@/lib/litegraph/src/types/widgets' import { useLinkStore } from '@/stores/linkStore' import { graphScopeOf } from '@/types/graphScopeId' -import { usePreviewExposureStore } from '@/stores/previewExposureStore' +import { + getPreviewExposureHostLocator, + usePreviewExposureStore +} from '@/stores/previewExposureStore' import { useWidgetValueStore } from '@/stores/widgetValueStore' import type { LinkTopology } from '@/types/linkTopology' import { toNodeId } from '@/types/nodeId' @@ -724,7 +727,7 @@ function migratePreview( } } - const hostNodeLocator = String(hostNode.id) + const hostNodeLocator = getPreviewExposureHostLocator(hostNode) const existing = store .getExposures(hostNode.rootGraph.id, hostNodeLocator) .find( diff --git a/src/core/graph/subgraph/promotionUtils.ts b/src/core/graph/subgraph/promotionUtils.ts index 5e5557a6380..0f819a20042 100644 --- a/src/core/graph/subgraph/promotionUtils.ts +++ b/src/core/graph/subgraph/promotionUtils.ts @@ -16,7 +16,10 @@ import { } from '@/composables/node/canvasImagePreviewTypes' import { useCanvasStore } from '@/renderer/core/canvas/canvasStore' import { useLitegraphService } from '@/services/litegraphService' -import { usePreviewExposureStore } from '@/stores/previewExposureStore' +import { + getPreviewExposureHostLocator, + usePreviewExposureStore +} from '@/stores/previewExposureStore' import { useSubgraphNavigationStore } from '@/stores/subgraphNavigationStore' import { toNodeId } from '@/types/nodeId' import type { SerializedNodeId } from '@/types/nodeId' @@ -221,7 +224,7 @@ function isPreviewExposed( subgraphNode: SubgraphNode, source: PromotedWidgetSource ): boolean { - const hostLocator = String(subgraphNode.id) + const hostLocator = getPreviewExposureHostLocator(subgraphNode) return usePreviewExposureStore() .getExposures(subgraphNode.rootGraph.id, hostLocator) .some( @@ -353,7 +356,7 @@ function promotePreviewViaExposure( ): void { const store = usePreviewExposureStore() const rootGraphId = subgraphNode.rootGraph.id - const hostLocator = String(subgraphNode.id) + const hostLocator = getPreviewExposureHostLocator(subgraphNode) const existing = store .getExposures(rootGraphId, hostLocator) .some( @@ -450,7 +453,7 @@ export function demoteWidget( if (isPreviewPseudoWidget(widget)) { const previewStore = usePreviewExposureStore() - const hostLocator = String(parent.id) + const hostLocator = getPreviewExposureHostLocator(parent) const exposure = previewStore .getExposures(parent.rootGraph.id, hostLocator) .find( diff --git a/src/core/graph/widgets/dynamicWidgets.ts b/src/core/graph/widgets/dynamicWidgets.ts index 75e946ffd54..1058921f641 100644 --- a/src/core/graph/widgets/dynamicWidgets.ts +++ b/src/core/graph/widgets/dynamicWidgets.ts @@ -350,7 +350,6 @@ function withComfyMatchType(node: LGraphNode): asserts node is MatchTypeNode { if (!outputType) throw new Error('invalid connection') this.outputs.forEach((_output, idx) => { if (!(outputGroups?.[idx] == matchKey)) return - this.outputs[idx] = shallowReactive(this.outputs[idx]) changeOutputType(this, idx, outputType) }) app.canvas?.setDirty(true, true) diff --git a/src/core/graph/widgets/matchTypeConfiguring.test.ts b/src/core/graph/widgets/matchTypeConfiguring.test.ts index 65ae689f110..69e9ee10e07 100644 --- a/src/core/graph/widgets/matchTypeConfiguring.test.ts +++ b/src/core/graph/widgets/matchTypeConfiguring.test.ts @@ -2,6 +2,7 @@ import { createTestingPinia } from '@pinia/testing' import { fromAny } from '@total-typescript/shoehorn' import { setActivePinia } from 'pinia' import { describe, expect, test } from 'vitest' +import { effect, stop } from 'vue' import { LGraph, LGraphNode, LiteGraph } from '@/lib/litegraph/src/litegraph' import { transformInputSpecV1ToV2 } from '@/schemas/nodeDef/migration' @@ -99,6 +100,10 @@ describe('MatchType during configure', () => { const graph = new LGraph() const switchNode = createMatchTypeNode(graph) const source1 = createSourceNode(graph, 'IMAGE') + const observedTypes: unknown[] = [] + const runner = effect(() => { + observedTypes.push(switchNode.outputs[0].type) + }) expect(app.configuringGraph).toBe(false) @@ -106,6 +111,8 @@ describe('MatchType during configure', () => { expect(switchNode.inputs[0].link).not.toBeNull() expect(switchNode.outputs[0].type).toBe('IMAGE') + expect(observedTypes).toEqual(['*', 'IMAGE']) + stop(runner) }) test('connects both inputs with same type', () => { diff --git a/src/extensions/core/widgetInputs.ts b/src/extensions/core/widgetInputs.ts index 1aab4ca1e76..41bae08495d 100644 --- a/src/extensions/core/widgetInputs.ts +++ b/src/extensions/core/widgetInputs.ts @@ -20,7 +20,9 @@ import { } from '@/schemas/nodeDefSchema' import type { ComfyNodeDef, InputSpec } from '@/schemas/nodeDefSchema' import { app } from '@/scripts/app' +import { useWidgetValueStore } from '@/stores/widgetValueStore' import type { WidgetValue } from '@/types/simplifiedWidget' +import { zeroUuid } from '@/utils/uuid' import { ComfyWidgets, addValueControlWidgets, @@ -130,16 +132,6 @@ export class PrimitiveNode extends LGraphNode { ) { this._onFirstConnection() - // Populate widget values from config data - if (this.widgets && this.widgets_values) { - for (let i = 0; i < this.widgets_values.length; i++) { - const w = this.widgets[i] - if (w) { - w.value = this.widgets_values[i] - } - } - } - // Merge values if required this._mergeWidgetConfig() } @@ -289,7 +281,13 @@ export class PrimitiveNode extends LGraphNode { !inputData?.[1]?.control_after_generate && (widget.type === 'number' || widget.type === 'combo') ) { - let control_value = this.widgets_values?.[1] + const graphId = this.graph?.rootGraph.id ?? zeroUuid + let control_value = + useWidgetValueStore().getPositionalRestoredWidgetValue( + graphId, + this.id, + 1 + ) if (!control_value) { control_value = 'fixed' } @@ -302,7 +300,11 @@ export class PrimitiveNode extends LGraphNode { ) if (this.widgets?.[1]) widget.linkedWidgets = [this.widgets[1]] - const filter = this.widgets_values?.[2] + const filter = useWidgetValueStore().getPositionalRestoredWidgetValue( + graphId, + this.id, + 2 + ) if (filter && this.widgets && this.widgets.length === 3) { this.widgets[2].value = filter } diff --git a/src/lib/litegraph/src/LGraph.serialise.test.ts b/src/lib/litegraph/src/LGraph.serialise.test.ts index dd4804af3e9..458ee43992e 100644 --- a/src/lib/litegraph/src/LGraph.serialise.test.ts +++ b/src/lib/litegraph/src/LGraph.serialise.test.ts @@ -74,4 +74,142 @@ describe('LGraph Serialisation', () => { expect(copiedLink).toMatchObject(expectedLink) }) + + test('round trips namespaced node and graph extension payloads', ({ + expect, + minimalGraph + }) => { + const node = new LGraphNode('Extended') + minimalGraph.add(node) + node.onSerialize = (data) => { + data.extensions = { 'example.node': { enabled: true } } + } + minimalGraph.onSerialize = (data) => { + data.revision = 999 + data.extensions = { 'example.graph': { version: 1 } } + } + + const saved = minimalGraph.asSerialisable() + expect(saved.revision).toBe(999) + const copied = new LGraph() + copied.configure(structuredClone(saved)) + + expect(copied.asSerialisable().extensions).toEqual({ + 'example.graph': { version: 1 } + }) + expect(copied.nodes[0].serialize().extensions).toEqual({ + 'example.node': { enabled: true } + }) + }) + + test('preserves canonical hook mutations while isolating legacy payloads', ({ + expect, + minimalGraph + }) => { + const node = new LGraphNode('Canonical') + minimalGraph.add(node) + node.onSerialize = (data) => { + data.pos = [999, 999] + Object.assign(data, { legacyData: { retained: true } }) + } + + const saved = node.serialize() + expect(saved.pos).toEqual([999, 999]) + expect(saved.extensions).toEqual({ legacyData: { retained: true } }) + + let configuredLegacyData: unknown + node.onConfigure = (data) => { + configuredLegacyData = Object.hasOwn(data, 'legacyData') + } + node.configure(Object.assign(saved, { legacyData: { retained: true } })) + + expect(configuredLegacyData).toBe(true) + expect(Object.hasOwn(node, 'legacyData')).toBe(false) + }) + + test('isolates graph extension payloads from graph properties', ({ + expect + }) => { + const graph = new LGraph() + let configuredData: object | undefined + graph.onConfigure = (data) => { + configuredData = data + } + + graph.configure( + Object.assign(graph.asSerialisable(), { + extensions: { namespaced: { enabled: true } }, + legacyData: { retained: true } + }) + ) + + expect(configuredData).toMatchObject({ + namespaced: { enabled: true }, + legacyData: { retained: true } + }) + expect(graph.asSerialisable().extensions).toEqual({ + namespaced: { enabled: true }, + legacyData: { retained: true } + }) + expect(Object.hasOwn(graph, 'extensions')).toBe(false) + expect(Object.hasOwn(graph, 'legacyData')).toBe(false) + + const clean = graph.asSerialisable() + delete clean.extensions + graph.configure(clean) + expect(configuredData).not.toHaveProperty('legacyData') + expect(graph.asSerialisable().extensions).toBeUndefined() + expect(Object.hasOwn(graph, 'extensions')).toBe(false) + expect(Object.hasOwn(graph, 'legacyData')).toBe(false) + }) + + test('removes namespaced extension payload keys across serializations', ({ + expect, + minimalGraph + }) => { + const node = new LGraphNode('Extended') + minimalGraph.add(node) + node.onSerialize = (data) => { + data.extensions = { 'example.node': { enabled: true } } + } + minimalGraph.onSerialize = (data) => { + data.extensions = { 'example.graph': { version: 1 } } + } + + expect(node.serialize().extensions).toHaveProperty('example.node') + expect(minimalGraph.asSerialisable().extensions).toHaveProperty( + 'example.graph' + ) + + node.onSerialize = (data) => { + delete data.extensions?.['example.node'] + } + minimalGraph.onSerialize = (data) => { + delete data.extensions?.['example.graph'] + } + + expect(node.serialize().extensions).toBeUndefined() + expect(minimalGraph.asSerialisable().extensions).toBeUndefined() + }) + + test('retains legacy flat payload updates across serializations', ({ + expect, + minimalGraph + }) => { + const node = new LGraphNode('Extended') + minimalGraph.add(node) + node.onSerialize = (data) => { + Object.assign(data, { legacyData: { version: 1 } }) + } + expect(node.serialize().extensions).toEqual({ + legacyData: { version: 1 } + }) + + node.onSerialize = (data) => { + Object.assign(data, { legacyData: { version: 2 } }) + } + expect(node.serialize().extensions).toEqual({ + legacyData: { version: 2 } + }) + }) }) diff --git a/src/lib/litegraph/src/LGraph.test.ts b/src/lib/litegraph/src/LGraph.test.ts index 9b340fbddbf..279ea3d094b 100644 --- a/src/lib/litegraph/src/LGraph.test.ts +++ b/src/lib/litegraph/src/LGraph.test.ts @@ -16,6 +16,7 @@ import { LiteGraph, LLink, Reroute, + serialiseMutableGraphParts, SubgraphNode } from '@/lib/litegraph/src/litegraph' import type { @@ -27,11 +28,15 @@ import type { import type { UUID } from '@/utils/uuid' import { createUuidv4, zeroUuid } from '@/utils/uuid' import { useLinkStore } from '@/stores/linkStore' +import { useExecutionOrderStore } from '@/stores/executionOrderStore' +import { useGraphDefinitionStore } from '@/stores/graphDefinitionStore' +import { useGraphMetadataStore } from '@/stores/graphMetadataStore' import { usePreviewExposureStore } from '@/stores/previewExposureStore' import { useRerouteStore } from '@/stores/rerouteStore' import { useWidgetValueStore } from '@/stores/widgetValueStore' import { slotFloatingLinks } from '@/lib/litegraph/src/LLink' import { toLinkId } from '@/types/linkId' +import { createNodeLocatorId } from '@/types/nodeIdentification' import { toRerouteId } from '@/types/rerouteId' import { UNASSIGNED_NODE_ID, toNodeId } from '@/types/nodeId' import { widgetId } from '@/types/widgetId' @@ -76,7 +81,105 @@ class DummyNode extends LGraphNode { } } +class ThrowingConfigureNode extends LGraphNode { + constructor() { + super('throwing configure') + } + + override configure(info: Parameters[0]): void { + super.configure(info) + const graphId = this.graph!.id + useWidgetValueStore().registerWidget(widgetId(graphId, this.id, 'value'), { + type: 'number', + value: 1, + options: {} + }) + usePreviewExposureStore().addExposure(graphId, String(this.id), { + sourceNodeId: this.id, + sourcePreviewName: 'preview' + }) + throw new Error('configure failed') + } +} + describe('LGraph', () => { + it('batches version updates while keeping distinct mutations distinct', () => { + const graph = new LGraph() + const version = graph._version + + graph.batchVersionUpdates(() => { + graph.add(new DummyNode()) + graph.add(new DummyNode()) + }) + expect(graph._version).toBe(version + 1) + + graph.add(new DummyNode()) + graph.add(new DummyNode()) + expect(graph._version).toBe(version + 3) + }) + + it('invalidates once when removing a node disconnects multiple links', () => { + const graph = new LGraph() + const source = new DummyNode() + const firstTarget = new DummyNode() + const secondTarget = new DummyNode() + source.addOutput('value', 'number') + firstTarget.addInput('value', 'number') + secondTarget.addInput('value', 'number') + graph.add(source) + graph.add(firstTarget) + graph.add(secondTarget) + source.connect(0, firstTarget, 0) + source.connect(0, secondTarget, 0) + const version = graph._version + + graph.remove(source) + + expect(graph._version).toBe(version + 1) + }) + + it('projects ordered membership from root-scoped definition records', () => { + const graph = new LGraph() + const first = new DummyNode() + const second = new DummyNode() + const group = new LGraphGroup() + graph.add(first) + graph.add(second) + graph.add(group) + + const store = useGraphDefinitionStore() + const membership = store.membership(graph.id, graph.id) + expect(graph.nodes).toBe(membership.nodes) + expect(graph.groups).toBe(membership.groups) + expect(membership.nodes).toEqual([first, second]) + expect(membership.groups).toEqual([group]) + + const previousId = graph.id + expect(() => { + graph.id = createUuidv4() + }).toThrow("Cannot change a populated graph's ID") + expect(graph.id).toBe(previousId) + expect(store.membership(graph.id, graph.id)).toBe(membership) + + graph.clear() + expect(store.membership(previousId, previousId).nodes).toEqual([]) + expect(store.membership(graph.id, graph.id).nodes).toEqual([]) + }) + + it('allows an empty graph to adopt a new ID', () => { + const graph = new LGraph() + const store = useGraphDefinitionStore() + const membership = store.membership(graph.id, graph.id) + const previousId = graph.id + const nextId = createUuidv4() + + graph.id = nextId + + expect(graph.id).toBe(nextId) + expect(store.membership(nextId, nextId)).toBe(membership) + expect(store.membership(previousId, previousId)).not.toBe(membership) + }) + it('should serialize deterministic node order', async () => { LiteGraph.registerNodeType('dummy', DummyNode) const node1 = new DummyNode() @@ -91,6 +194,108 @@ describe('LGraph', () => { expect(result1).toEqual(result2) }) + it('projects graph-scoped derived execution order to extensions and wire data', () => { + const root = new LGraph() + const rootNode = new DummyNode() + root.add(rootNode) + const subgraph = createTestSubgraph({ rootGraph: root }) + const subgraphNode = new DummyNode() + subgraph.add(subgraphNode) + const store = useExecutionOrderStore() + + store.set(graphScopeOf(root), rootNode.id, 7) + store.set(graphScopeOf(subgraph), subgraphNode.id, 9) + + expect(rootNode.order).toBe(7) + expect(subgraphNode.order).toBe(9) + expect(rootNode.serialize().order).toBe(7) + expect(subgraphNode.serialize().order).toBe(9) + + root.updateExecutionOrder() + expect(rootNode.order).toBe(0) + expect(subgraphNode.order).toBe(9) + }) + + it('cleans derived order on removal while preserving the detached projection', () => { + const graph = new LGraph() + const first = new DummyNode() + const removed = new DummyNode() + graph.add(first) + graph.add(removed) + const scope = graphScopeOf(graph) + const store = useExecutionOrderStore() + + expect(removed.order).toBe(1) + graph.remove(removed) + + expect(store.get(scope, removed.id)).toBeUndefined() + expect(removed.order).toBe(1) + expect(first.order).toBe(0) + }) + + it('hydrates wire order before topology recomputation replaces it', () => { + const node = new DummyNode() + const data = node.serialize() + data.order = 12 + + node.configure(data) + expect(node.order).toBe(12) + expect(node.serialize().order).toBe(12) + + const graph = new LGraph() + graph.add(node) + expect(node.order).toBe(0) + expect(node.serialize().order).toBe(0) + }) + + it('cleans partially configured node-owned records before rethrowing', () => { + LiteGraph.registerNodeType('throwing-configure', ThrowingConfigureNode) + const source = new LGraph() + const sourceNode = new ThrowingConfigureNode() + source.add(sourceNode) + const data = source.asSerialisable() + const graphId = data.id + const id = widgetId(graphId, sourceNode.id, 'value') + const target = new LGraph() + + expect(() => target.configure(data)).toThrow('configure failed') + + expect(target.nodes).toEqual([]) + expect(useWidgetValueStore().getWidget(id)).toBeUndefined() + expect( + usePreviewExposureStore().getExposures(graphId, String(sourceNode.id)) + ).toEqual([]) + }) + + it('preserves existing entities when keep-old configuration fails', () => { + LiteGraph.registerNodeType('throwing-configure', ThrowingConfigureNode) + const source = new LGraph() + const sourceNode = new ThrowingConfigureNode() + source.add(sourceNode) + const data = source.asSerialisable() + const graph = new LGraph() + data.id = graph.id + const existingNode = new DummyNode() + const existingGroup = new LGraphGroup('existing') + graph.add(existingNode) + graph.add(existingGroup) + + expect(() => graph.configure(data, true)).toThrow('configure failed') + + expect(graph.nodes).toEqual([existingNode]) + expect(graph.groups).toEqual([existingGroup]) + expect(existingNode.graph).toBe(graph) + expect(existingGroup.graph).toBe(graph) + expect( + useWidgetValueStore().getWidget( + widgetId(graph.id, sourceNode.id, 'value') + ) + ).toBeUndefined() + expect( + usePreviewExposureStore().getExposures(graph.id, String(sourceNode.id)) + ).toEqual([]) + }) + it('should handle adding null node gracefully', () => { const graph = new LGraph() const initialNodeCount = graph.nodes.length @@ -100,6 +305,38 @@ describe('LGraph', () => { expect(result).toBeUndefined() expect(graph.nodes.length).toBe(initialNodeCount) }) + + it('configures and serializes graph metadata through the store', () => { + const graph = new LGraph() + const id = createUuidv4() + graph.configure({ + id, + version: 1, + revision: 4, + state: { + lastGroupId: 0, + lastNodeId: 0, + lastLinkId: 0, + lastRerouteId: 0 + }, + config: { links_ontop: true }, + extra: { workflowRendererVersion: 'Vue' } + }) + + const metadata = useGraphMetadataStore().get(id) + expect(metadata).toMatchObject({ + revision: 4, + config: { links_ontop: true }, + extra: { workflowRendererVersion: 'Vue' } + }) + + graph.config.links_ontop = false + expect(graph.asSerialisable()).toMatchObject({ + revision: 4, + config: { links_ontop: false }, + extra: { workflowRendererVersion: 'Vue' } + }) + }) it('normalizes legacy numeric node ids when adding nodes', () => { const graph = new LGraph() const unassignedNode = new LGraphNode('legacy-unassigned') @@ -489,6 +726,75 @@ describe('Link serialization goldens (ADR-0008 topology-store migration)', () => }) }) +describe('Store-driven serialization parity', () => { + test('matches normalized mutable serialization across topology variants', ({ + expect, + linkedNodesGraph, + reroutesComplexGraph, + floatingLinkGraph + }) => { + for (const graph of [ + new LGraph(linkedNodesGraph), + reroutesComplexGraph, + new LGraph(floatingLinkGraph) + ]) { + const stored = graph.asSerialisable({ sortNodes: true }) + const mutable = serialiseMutableGraphParts(graph, true) + const normalizedStored = { + nodes: stored.nodes, + groups: stored.groups, + links: stored.links, + floatingLinks: stored.floatingLinks, + reroutes: stored.reroutes + } + expect(JSON.parse(JSON.stringify(normalizedStored))).toEqual( + JSON.parse(JSON.stringify(mutable)) + ) + } + }) + + test('rejects a stored node without a live adapter', ({ expect }) => { + const graph = createGraph(new DummyNode()) + graph._nodes = [] + + expect(() => graph.asSerialisable()).toThrow( + /Cannot serialize graph .*: node .* has no live adapter/ + ) + }) + + test('rejects a member group without presentation', ({ expect }) => { + const graph = new LGraph() + const group = new LGraphGroup() + graph.add(group) + useGraphDefinitionStore().deleteGroupPresentation( + graph.id, + graph.id, + group.id + ) + + expect(() => graph.asSerialisable()).toThrow( + `Cannot serialize graph ${graph.id}: group ${group.id} has no presentation` + ) + }) + + test('rejects a member group without layout', ({ expect }) => { + const graph = new LGraph() + const group = new LGraphGroup() + graph.add(group) + layoutStore.applyOperation({ + type: 'deleteGroup', + graphId: graph.id, + groupId: group.id, + source: LayoutSource.Canvas, + timestamp: Date.now() + }) + + expect(() => graph.asSerialisable()).toThrow( + `Cannot serialize graph ${graph.id}: group ${group.id} has no layout` + ) + }) +}) + describe('Graph Clearing and Callbacks', () => { test('clear() calls both node.onRemoved() and graph.onNodeRemoved()', ({ expect @@ -912,6 +1218,55 @@ describe('Subgraph Definition Garbage Collection', () => { expect(removedNodeIds.size).toBe(2) }) + it('removing a node clears its widget and preview exposure records', () => { + const rootGraph = new LGraph() + const node = new LGraphNode('owned state') + rootGraph.add(node) + const id = widgetId(rootGraph.id, node.id, 'value') + useWidgetValueStore().registerWidget(id, { + type: 'number', + value: 1, + options: {} + }) + usePreviewExposureStore().addExposure(rootGraph.id, String(node.id), { + sourceNodeId: node.id, + sourcePreviewName: 'preview' + }) + + rootGraph.remove(node) + + expect(useWidgetValueStore().getWidget(id)).toBeUndefined() + expect( + usePreviewExposureStore().getExposures(rootGraph.id, String(node.id)) + ).toEqual([]) + }) + + it('released subgraphs clear inner node-owned records', () => { + const rootGraph = new LGraph() + const { subgraph, innerNodes } = createSubgraphWithNodes(rootGraph, 1) + const innerNode = innerNodes[0] + const id = widgetId(rootGraph.id, innerNode.id, 'value') + const locator = createNodeLocatorId(subgraph.id, innerNode.id) + useWidgetValueStore().registerWidget(id, { + type: 'number', + value: 1, + options: {} + }) + usePreviewExposureStore().addExposure(rootGraph.id, locator, { + sourceNodeId: innerNode.id, + sourcePreviewName: 'preview' + }) + const host = createTestSubgraphNode(subgraph) + rootGraph.add(host) + + rootGraph.remove(host) + + expect(useWidgetValueStore().getWidget(id)).toBeUndefined() + expect( + usePreviewExposureStore().getExposures(rootGraph.id, locator) + ).toEqual([]) + }) + it('removing SubgraphNode fires onNodeRemoved callback', () => { const rootGraph = new LGraph() const { subgraph } = createSubgraphWithNodes(rootGraph, 2) diff --git a/src/lib/litegraph/src/LGraph.ts b/src/lib/litegraph/src/LGraph.ts index d11d2480352..d0237c4029c 100644 --- a/src/lib/litegraph/src/LGraph.ts +++ b/src/lib/litegraph/src/LGraph.ts @@ -50,7 +50,17 @@ import { outputLinks } from './node/slotLinks' import { normalizeWidgetsView } from './node/widgetsView' -import { UNASSIGNED_NODE_ID, parseNodeId, toNodeId } from '@/types/nodeId' +import { clearNodeOwnedStoreState } from '@/stores/clearNodeOwnedStoreState' +import { useEntityIdStore } from '@/stores/entityIdStore' +import { useExecutionOrderStore } from '@/stores/executionOrderStore' +import { useGraphDefinitionStore } from '@/stores/graphDefinitionStore' +import { useGraphMetadataStore } from '@/stores/graphMetadataStore' +import { + UNASSIGNED_NODE_ID, + parseNodeId, + serializeNodeId, + toNodeId +} from '@/types/nodeId' import type { NodeId, SerializedNodeId } from '@/types/nodeId' import { forEachNode, visitGraphNodes } from '@/utils/graphTraversalUtil' @@ -66,7 +76,11 @@ import { import type { DragAndScaleState } from './DragAndScale' import { LGraphCanvas } from './LGraphCanvas' import { Rectangle } from './infrastructure/Rectangle' -import { LGraphGroup } from './LGraphGroup' +import { + LGraphGroup, + registerGroupPresentation, + unregisterGroupPresentation +} from './LGraphGroup' import { LGraphNode } from './LGraphNode' import { LLink, @@ -142,6 +156,12 @@ import type { SerialisableReroute } from './types/serialisation' import { getAllNestedItems } from './utils/collections' +import { + extensionConfigureView, + GRAPH_CANONICAL_FIELDS, + hydrateExtensionPayload, + runExtensionSerializeHook +} from './extensionPersistence' import { collectReservedGroupIds, collectReservedLinkIds, @@ -214,6 +234,7 @@ function fireNodeRemovalLifecycle(node: LGraphNode): void { const graph: LGraph | null = node.graph graph?.events.dispatch('node:before-removed', { node }) node.onRemoved?.() + clearNodeOwnedStoreState(node) graph?.onNodeRemoved?.(node) } @@ -245,14 +266,17 @@ function teardownOwnedGraphs(owner: LGraph): void { for (const graph of ownedGraphs) { unregisterAllLinkTopologies(graph) unregisterAllRerouteChains(graph) + useGraphMetadataStore().clear(graph.id) } const nodes = new Set(lifecycleNodes) for (const graph of ownedGraphs) { for (const node of graph._nodes) nodes.add(node) } for (const node of nodes) { + const order = node.order detachNodeFromStores(owner, node, 'discard-values') node.graph = null + node.order = order } detachGraphLayouts([owner], { removeLayouts: !owner.isRootGraph }) } @@ -314,7 +338,103 @@ function walkSegment( * + onNodeRemoved: when a node inside this graph is removed */ function serialiseOwnedTopology(owner: LGraph) { + const scope = graphScopeOf(owner) + const topologies = [...useLinkStore().graphTopologies(scope)] + const serialiseLink = (link: (typeof topologies)[number]) => ({ + id: link.id, + origin_id: serializeNodeId(link.originNodeId), + origin_slot: link.originSlot, + target_id: serializeNodeId(link.targetNodeId), + target_slot: link.targetSlot, + type: link.type, + ...(link.parentId !== undefined && { parentId: link.parentId }) + }) + const links = topologies.filter((link) => !isFloatingTopology(link)) + const floatingLinks = topologies.filter(isFloatingTopology) + const reroutes = [...useRerouteStore().graphChains(scope)].map((reroute) => { + const position = layoutStore.getRerouteLayout( + owner.rootGraph.id, + reroute.id + )?.position ?? { x: 0, y: 0 } + const membership = useRerouteStore().getMembership(scope, reroute.id) + return { + id: reroute.id, + parentId: reroute.parentId, + pos: [position.x, position.y] as [number, number], + linkIds: [...membership.linkIds].sort((a, b) => a - b), + floating: reroute.floating + ? { slotType: reroute.floating.slotType } + : undefined + } + }) return { + links: links.length ? links.map(serialiseLink) : undefined, + floatingLinks: floatingLinks.length + ? floatingLinks.map(serialiseLink) + : undefined, + reroutes: reroutes.length ? reroutes : undefined + } +} + +function serialiseStoredNodes(owner: LGraph, sortNodes: boolean) { + const adapters = new Map(owner._nodes.map((node) => [node.id, node])) + const states = useNodeDataStore().getGraphNodesFor( + owner.rootGraph.id, + owner.id + ) + const ordered = sortNodes + ? [...states].sort((a, b) => Number(a.id) - Number(b.id)) + : states + return ordered.map((state) => { + const adapter = adapters.get(state.id) + if (!adapter) { + throw new Error( + `Cannot serialize graph ${owner.id}: node ${state.id} has no live adapter` + ) + } + return adapter.serializeFromStoreState(state) + }) +} + +function serialiseStoredGroups(owner: LGraph) { + const membership = useGraphDefinitionStore().membership( + owner.rootGraph.id, + owner.id + ) + return membership.groups.map((group) => { + const presentation = membership.groupPresentation.get(group.id) + const layout = layoutStore.getGroupLayout(owner.rootGraph.id, group.id) + if (!presentation || !layout) { + const missing = !presentation ? 'presentation' : 'layout' + throw new Error( + `Cannot serialize graph ${owner.id}: group ${group.id} has no ${missing}` + ) + } + return { + id: group.id, + title: presentation.title, + bounding: [ + layout.position.x, + layout.position.y, + layout.size.width, + layout.size.height + ], + color: presentation.color, + flags: presentation.flags + } + }) +} + +export function serialiseMutableGraphParts( + owner: LGraph, + sortNodes: boolean = false +) { + const nodes = sortNodes + ? [...owner._nodes].sort((a, b) => Number(a.id) - Number(b.id)) + : owner._nodes + return { + nodes: nodes.map((node) => node.serialize()), + groups: owner._groups.map((group) => group.serialize()), links: owner.links.size ? [...owner.links.values()].map((link) => link.asSerialisable()) : undefined, @@ -372,10 +492,42 @@ export class LGraph return toRaw(this)._id.value } set id(value: UUID) { - toRaw(this)._id.value = value + const raw = toRaw(this) + if (raw._id.value === value) return + const rootGraph = raw.rootGraph + const subgraphDefinition = + rootGraph && rootGraph !== raw + ? useGraphDefinitionStore().definition(rootGraph.id, raw._id.value) + : undefined + if ( + raw._id.value !== zeroUuid && + (raw._nodes.length > 0 || + raw._groups.length > 0 || + raw.links.size > 0 || + raw.floatingLinks.size > 0 || + raw.reroutes.size > 0 || + raw._subgraphs.size > 0 || + (subgraphDefinition && + (subgraphDefinition.inputs.length > 0 || + subgraphDefinition.outputs.length > 0 || + subgraphDefinition.widgets.length > 0))) + ) + throw new Error("Cannot change a populated graph's ID; clear it first") + if (!rootGraph || rootGraph === raw) + useGraphDefinitionStore().rekeyRoot(raw._id.value, value) + else + useGraphDefinitionStore().rekeyGraph(rootGraph.id, raw._id.value, value) + useGraphMetadataStore().rekey(raw._id.value, value) + useEntityIdStore().rekey(raw._id.value, value) + raw._id.value = value } - revision: number = 0 + get revision(): number { + return useGraphMetadataStore().get(this.id).revision + } + set revision(value: number) { + useGraphMetadataStore().get(this.id).revision = value + } private readonly _versionRef = shallowRef(-1) get _version(): number { @@ -400,24 +552,46 @@ export class LGraph list_of_graphcanvas: LGraphCanvas[] | null status: number = LGraph.STATUS_STOPPED - private _state: LGraphState = createLGraphState() - get state(): LGraphState { - return this._state + return useEntityIdStore().get(this.id) } set state(value: LGraphState) { - this._state = value + useEntityIdStore().set(this.id, value) } readonly events = new CustomEventTarget() - readonly _subgraphs: Map = new Map() + get _subgraphs(): Map { + return useGraphDefinitionStore().subgraphs(this.id) + } - _nodes: (LGraphNode | SubgraphNode)[] = [] + get _nodes(): (LGraphNode | SubgraphNode)[] { + return useGraphDefinitionStore().membership( + this.rootGraph?.id ?? this.id, + this.id + ).nodes + } + set _nodes(value: (LGraphNode | SubgraphNode)[]) { + useGraphDefinitionStore().membership( + this.rootGraph?.id ?? this.id, + this.id + ).nodes = value + } _nodes_by_id: Record = {} _nodes_in_order: LGraphNode[] = [] _nodes_executable: LGraphNode[] | null = null - _groups: LGraphGroup[] = [] + get _groups(): LGraphGroup[] { + return useGraphDefinitionStore().membership( + this.rootGraph?.id ?? this.id, + this.id + ).groups + } + set _groups(value: LGraphGroup[]) { + useGraphDefinitionStore().membership( + this.rootGraph?.id ?? this.id, + this.id + ).groups = value + } iteration: number = 0 globaltime: number = 0 /** @deprecated Unused */ @@ -435,12 +609,22 @@ export class LGraph _last_trigger_time?: number filter?: string /** Must contain serialisable values, e.g. primitive types */ - config: LGraphConfig = {} + get config(): LGraphConfig { + return useGraphMetadataStore().get(this.id).config + } + set config(value: LGraphConfig) { + useGraphMetadataStore().get(this.id).config = value + } vars: Dictionary = {} nodes_executing: boolean[] = [] nodes_actioning: (string | boolean)[] = [] nodes_executedAction: string[] = [] - extra: LGraphExtra = {} + get extra(): LGraphExtra { + return useGraphMetadataStore().get(this.id).extra + } + set extra(value: LGraphExtra) { + useGraphMetadataStore().get(this.id).extra = value + } /** @deprecated Deserialising a workflow sets this unused property. */ version?: number @@ -571,14 +755,23 @@ export class LGraph private resetAfterClear(): void { const graphId = this.id + const definitionStore = useGraphDefinitionStore() + useGraphMetadataStore().clear(graphId) + if (this.isRootGraph) useEntityIdStore().clear(graphId) if (this.isRootGraph && graphId !== zeroUuid) { + useExecutionOrderStore().clearRoot(graphId) usePreviewExposureStore().clearGraph(graphId) useWidgetValueStore().clearGraph(graphId) useLinkStore().clearGraph(toRootGraphId(graphId)) useRerouteStore().clearGraph(toRootGraphId(graphId)) useNodeDataStore().clearGraph(graphId) + definitionStore.clearRoot(graphId) layoutStore.clearGraph(graphId) + } else if (this.rootGraph) { + useExecutionOrderStore().clearGraph(graphScopeOf(this)) + definitionStore.clearGraph(this.rootGraph.id, graphId) } + this.reroutes.clear() this.id = this.isRootGraph ? createUuidv4() : zeroUuid this.revision = 0 @@ -596,8 +789,6 @@ export class LGraph // nodes that contain onExecute sorted in execution order this._nodes_executable = null - this.reroutes.clear() - // other scene stuff this._groups = [] @@ -915,21 +1106,7 @@ export class LGraph if (L.length != this._nodes.length && LiteGraph.debug) console.warn('something went wrong, nodes missing') - /** Ensure type is set */ - type OrderedLGraphNode = LGraphNode & { order: number } - - /** Sets the order property of each provided node to its index in {@link nodes}. */ - function setOrder( - nodes: LGraphNode[] - ): asserts nodes is OrderedLGraphNode[] { - const l = nodes.length - for (let i = 0; i < l; ++i) { - nodes[i].order = i - } - } - - // save order number in the node - setOrder(L) + const topologyOrder = new Map(L.map((node, order) => [node.id, order])) // sort now by priority L.sort(function (A, B) { @@ -939,11 +1116,15 @@ export class LGraph const Bp = B.constructor.priority || B.priority || 0 // if same priority, sort by order - return Ap == Bp ? A.order - B.order : Ap - Bp + return Ap == Bp + ? (topologyOrder.get(A.id) ?? 0) - (topologyOrder.get(B.id) ?? 0) + : Ap - Bp }) - // save order number in the node, again... - setOrder(L) + useExecutionOrderStore().replace( + graphScopeOf(this), + L.map((node) => node.id) + ) return L } @@ -1016,10 +1197,24 @@ export class LGraph /** * Increments the internal version counter. - * Centralized so a future VersionSystem can intercept, batch, or replace it. */ incrementVersion(): void { + if (this.versionBatchInvalidated) return this._version++ + if (this.versionBatchDepth > 0) this.versionBatchInvalidated = true + } + + private versionBatchDepth = 0 + private versionBatchInvalidated = false + + batchVersionUpdates(mutation: () => T): T { + this.versionBatchDepth++ + try { + return mutation() + } finally { + this.versionBatchDepth-- + if (this.versionBatchDepth === 0) this.versionBatchInvalidated = false + } } /** @@ -1140,6 +1335,7 @@ export class LGraph this.setDirtyCanvas(true) this.change() node.graph = this + registerGroupPresentation(this, node) attachGroupLayout(this, node) this.incrementVersion() return @@ -1217,6 +1413,7 @@ export class LGraph this._groups.splice(index, 1) } detachGroupLayout(node) + unregisterGroupPresentation(node) node.graph = undefined this.incrementVersion() this.setDirtyCanvas(true, true) @@ -1239,7 +1436,7 @@ export class LGraph nodesBeingRemoved.add(node) try { - this.removeNode(node) + this.batchVersionUpdates(() => this.removeNode(node)) } finally { nodesBeingRemoved.delete(node) } @@ -1285,6 +1482,8 @@ export class LGraph unregisterAllLinkTopologies(subgraph) unregisterAllRerouteChains(subgraph) detachAllNodesFromStores(subgraph) + useExecutionOrderStore().clearGraph(graphScopeOf(subgraph)) + useGraphMetadataStore().clear(subgraph.id) this.rootGraph.subgraphs.delete(subgraph.id) } detachGraphLayouts(releasedSubgraphs) @@ -1292,11 +1491,15 @@ export class LGraph // callback node.onRemoved?.() + clearNodeOwnedStoreState(node) + const order = node.order + useExecutionOrderStore().remove(graphScopeOf(this), node.id) detachNodeFromStores(this, node) detachNodeLayout(node) node.graph = null + node.order = order this.incrementVersion() // remove from canvas render @@ -2445,6 +2648,7 @@ export class LGraph nodes, reroutes, extra, + extensions, floatingLinks, definitions } = this.asSerialisable(option) @@ -2471,6 +2675,7 @@ export class LGraph definitions, config, extra, + ...(extensions && { extensions }), version: LiteGraph.VERSION } } @@ -2503,12 +2708,8 @@ export class LGraph Required> { const { id, revision, config, state } = this - const nodeList = options?.sortNodes - ? [...this._nodes].sort((a, b) => Number(a.id) - Number(b.id)) - : this._nodes - - const nodes = nodeList.map((node) => node.serialize()) - const groups = this._groups.map((x) => x.serialize()) + const nodes = serialiseStoredNodes(this, options?.sortNodes ?? false) + const groups = serialiseStoredGroups(this) const topology = serialiseOwnedTopology(this) // Save scale and offset @@ -2538,11 +2739,16 @@ export class LGraph } } - this.onSerialize?.(data) - return data + return runExtensionSerializeHook( + this, + data, + GRAPH_CANONICAL_FIELDS, + this.onSerialize?.bind(this) + ) } protected _configureBase(data: ISerialisedGraph | SerialisableGraph): void { + hydrateExtensionPayload(this, data, GRAPH_CANONICAL_FIELDS) const { id, extra } = data // Create a new graph ID if none is provided or the zero UUID is used on the root graph @@ -2575,6 +2781,11 @@ export class LGraph } const mayContinue = this.events.dispatch('configuring', options) if (!mayContinue) return + const existingNodes = !options.clearGraph ? [...this._nodes] : undefined + const existingGroups = !options.clearGraph ? [...this._groups] : undefined + const existingNodesById = !options.clearGraph + ? { ...this._nodes_by_id } + : undefined beginNamedValuesShadowDiffLoad() try { @@ -2668,7 +2879,8 @@ export class LGraph // copy all stored fields for (const i in data) { - if (LGraph.ConfigureProperties.has(i)) continue + if (LGraph.ConfigureProperties.has(i) || !GRAPH_CANONICAL_FIELDS.has(i)) + continue // @ts-expect-error #574 Legacy property assignment this[i] = data[i] @@ -2802,7 +3014,7 @@ export class LGraph LGraph.autoExposePreviewNodes?.(node) } - this.onConfigure?.(data) + this.onConfigure?.(extensionConfigureView(this, data)) this.incrementVersion() // Ensure the primary canvas is set to the correct graph @@ -2819,6 +3031,28 @@ export class LGraph this.setDirtyCanvas(true, true) return error + } catch (error) { + if (existingNodes && existingGroups && existingNodesById) { + const existingNodeSet = new Set(existingNodes) + const existingGroupSet = new Set(existingGroups) + try { + for (const node of [...this._nodes]) { + if (!existingNodeSet.has(node)) this.remove(node) + } + for (const group of [...this._groups]) { + if (!existingGroupSet.has(group)) this.remove(group) + } + } finally { + this._nodes = existingNodes + this._groups = existingGroups + this._nodes_by_id = existingNodesById + for (const node of existingNodes) attachNodeLayout(this, node) + for (const group of existingGroups) attachGroupLayout(this, group) + } + } else { + this.clear() + } + throw error } finally { endNamedValuesShadowDiffLoad() this.events.dispatch('configured') @@ -2888,19 +3122,43 @@ export class Subgraph static MAX_NESTED_SUBGRAPHS = 1000 /** The display name of the subgraph. */ - name: string = 'Unnamed Subgraph' + get name(): string { + return useGraphDefinitionStore().definition(this.rootGraph.id, this.id).name + } + set name(value: string) { + useGraphDefinitionStore().definition(this.rootGraph.id, this.id).name = + value + } /** Optional description shown as tooltip when hovering over the subgraph node. */ - description?: string + get description(): string | undefined { + return useGraphDefinitionStore().definition(this.rootGraph.id, this.id) + .description + } + set description(value: string | undefined) { + useGraphDefinitionStore().definition( + this.rootGraph.id, + this.id + ).description = value + } readonly inputNode = new SubgraphInputNode(this) readonly outputNode = new SubgraphOutputNode(this) /** Ordered list of inputs to the subgraph itself. Similar to a reroute, with the input side in the graph, and the output side in the subgraph. */ - readonly inputs: SubgraphInput[] = [] + get inputs(): SubgraphInput[] { + return useGraphDefinitionStore().definition(this.rootGraph.id, this.id) + .inputs + } /** Ordered list of outputs from the subgraph itself. Similar to a reroute, with the input side in the subgraph, and the output side in the graph. */ - readonly outputs: SubgraphOutput[] = [] + get outputs(): SubgraphOutput[] { + return useGraphDefinitionStore().definition(this.rootGraph.id, this.id) + .outputs + } /** A list of node widgets displayed in the parent graph, on the subgraph object. */ - readonly widgets: ExposedWidget[] = [] + get widgets(): ExposedWidget[] { + return useGraphDefinitionStore().definition(this.rootGraph.id, this.id) + .widgets + } private _rootGraph: LGraph override get rootGraph(): LGraph { @@ -2920,6 +3178,7 @@ export class Subgraph super() + useGraphDefinitionStore().clearRoot(this.id) this._rootGraph = rootGraph const cloned = structuredClone(data) @@ -3163,8 +3422,8 @@ export class Subgraph inputs: this.inputs.map((x) => x.asSerialisable()), outputs: this.outputs.map((x) => x.asSerialisable()), widgets: [...this.widgets], - nodes: this.nodes.map((node) => node.serialize()), - groups: this.groups.map((group) => group.serialize()), + nodes: serialiseStoredNodes(this, false), + groups: serialiseStoredGroups(this), ...topology, links: topology.links ?? [], extra: this.extra diff --git a/src/lib/litegraph/src/LGraphCanvas.ts b/src/lib/litegraph/src/LGraphCanvas.ts index d160196e4f2..09068cda673 100644 --- a/src/lib/litegraph/src/LGraphCanvas.ts +++ b/src/lib/litegraph/src/LGraphCanvas.ts @@ -21,12 +21,13 @@ import type { ContextMenu } from './ContextMenu' import { createCursorCache } from './cursorCache' import { DragAndScale } from './DragAndScale' import type { AnimationOptions } from './DragAndScale' +import { mintNodeId, observeNodeId } from './idAllocation' import type { LGraph, SubgraphId } from './LGraph' import { LGraphGroup } from './LGraphGroup' import { LGraphNode } from './LGraphNode' import type { NodeProperty } from './LGraphNode' import { detachSerialisedLinks } from './linkDeduplication' -import { parseNodeId, serializeNodeId } from '@/types/nodeId' +import { parseNodeId, serializeNodeId, toNodeId } from '@/types/nodeId' import type { SerializedNodeId } from '@/types/nodeId' import { LLink, slotFloatingLinks } from './LLink' import { @@ -4992,16 +4993,11 @@ export class LGraphCanvas implements CustomEventDispatcher const { graph } = this if (!graph) throw new NullGraphError() - const i = graph._nodes.indexOf(node) - if (i == -1) return - - useLayoutMutations(LayoutSource.Canvas).bringNodeToFront( - graph.rootGraph.id, - node.id + useLayoutMutations(LayoutSource.Canvas).setNodeOrder( + graph, + node.id, + 'front' ) - - graph._nodes.splice(i, 1) - graph._nodes.push(node) } /** @@ -5011,11 +5007,7 @@ export class LGraphCanvas implements CustomEventDispatcher const { graph } = this if (!graph) throw new NullGraphError() - const i = graph._nodes.indexOf(node) - if (i == -1) return - - graph._nodes.splice(i, 1) - graph._nodes.unshift(node) + useLayoutMutations(LayoutSource.Canvas).setNodeOrder(graph, node.id, 'back') } /** @@ -9054,13 +9046,14 @@ export function remapClipboardSubgraphNodeIds( const numericId = Number(node.id) if (!Number.isInteger(numericId)) return usedNodeIds.add(numericId) - if (rootGraph.state.lastNodeId < numericId) - rootGraph.state.lastNodeId = numericId + observeNodeId(rootGraph.state, toNodeId(numericId)) }) function nextUniqueNodeId() { - while (usedNodeIds.has(++rootGraph.state.lastNodeId)); - const nextId = rootGraph.state.lastNodeId + let nextId = Number(mintNodeId(rootGraph.state)) + while (usedNodeIds.has(nextId)) { + nextId = Number(mintNodeId(rootGraph.state)) + } usedNodeIds.add(nextId) return nextId } @@ -9085,8 +9078,7 @@ export function remapClipboardSubgraphNodeIds( } usedNodeIds.add(nodeInfo.id) - if (rootGraph.state.lastNodeId < nodeInfo.id) - rootGraph.state.lastNodeId = nodeInfo.id + observeNodeId(rootGraph.state, toNodeId(nodeInfo.id)) } if (remappedIds.size > 0) { diff --git a/src/lib/litegraph/src/LGraphGroup.test.ts b/src/lib/litegraph/src/LGraphGroup.test.ts index b105031cbc1..fc792262658 100644 --- a/src/lib/litegraph/src/LGraphGroup.test.ts +++ b/src/lib/litegraph/src/LGraphGroup.test.ts @@ -12,6 +12,7 @@ import { import { containsRect } from '@/lib/litegraph/src/measure' import { layoutStore } from '@/renderer/core/layout/store/layoutStore' import { LayoutSource } from '@/renderer/core/layout/types' +import { useGraphDefinitionStore } from '@/stores/graphDefinitionStore' import type { GroupId } from '@/types/groupId' import { toGroupId } from '@/types/groupId' import * as colorUtil from '@/utils/colorUtil' @@ -52,6 +53,65 @@ describe('LGraphGroup', () => { expect(link.serialize()).toMatchSnapshot('Basic') }) + test('projects mutable presentation fields from graph definition state', () => { + const graph = new LGraph() + const group = new LGraphGroup('initial', toGroupId(930)) + graph.add(group) + + const presentation = useGraphDefinitionStore() + .membership(graph.rootGraph.id, graph.id) + .groupPresentation.get(group.id) + + expect(presentation).toBe(group._presentation) + group.title = 'updated' + group.color = '#123456' + group.font = 'serif' + group.font_size = 42 + group.flags.pinned = true + expect(presentation).toEqual({ + title: 'updated', + color: '#123456', + font: 'serif', + font_size: 42, + flags: { pinned: true } + }) + expect(group.serialize()).toMatchObject({ + title: 'updated', + color: '#123456', + flags: { pinned: true } + }) + }) + + test('clears a store-backed color option', () => { + const graph = new LGraph() + const group = new LGraphGroup('group', toGroupId(932)) + graph.add(group) + group.color = '#123456' + + group.setColorOption(null) + + expect(group.color).toBeUndefined() + expect(group.serialize().color).toBeUndefined() + }) + + test('detaches presentation state when removed', () => { + const graph = new LGraph() + const group = new LGraphGroup('group', toGroupId(931)) + graph.add(group) + const presentation = group._presentation + + graph.remove(group) + group.flags.pinned = true + + expect(group._presentation).not.toBe(presentation) + expect(group.pinned).toBe(true) + expect( + useGraphDefinitionStore() + .membership(graph.rootGraph.id, graph.id) + .groupPresentation.has(group.id) + ).toBe(false) + }) + describe('recomputeInsideNodes', () => { test('uses visited set to avoid redundant computation', () => { const graph = new LGraph() diff --git a/src/lib/litegraph/src/LGraphGroup.ts b/src/lib/litegraph/src/LGraphGroup.ts index 8b7b4ab0cf7..56ca7d0fefc 100644 --- a/src/lib/litegraph/src/LGraphGroup.ts +++ b/src/lib/litegraph/src/LGraphGroup.ts @@ -1,6 +1,8 @@ import { NullGraphError } from '@/lib/litegraph/src/infrastructure/NullGraphError' import { setGroupBoundsLayout } from '@/renderer/core/layout/operations/graphLayoutAttachment' import { layoutStore } from '@/renderer/core/layout/store/layoutStore' +import type { GroupPresentation } from '@/stores/graphDefinitionStore' +import { useGraphDefinitionStore } from '@/stores/graphDefinitionStore' import type { GroupId } from '@/types/groupId' import { toGroupId } from '@/types/groupId' import { hexToRgb, luminance, readableTextColor } from '@/utils/colorUtil' @@ -52,10 +54,7 @@ export class LGraphGroup implements Positionable, IPinnable, IColorable { static darkBgLuminanceThreshold = 80 id: GroupId - color?: string - title: string - font?: string - font_size: number = LiteGraph.GROUP_TEXT_SIZE + _presentation: GroupPresentation private readonly bounds = new Rectangle( 10, 10, @@ -66,7 +65,6 @@ export class LGraphGroup implements Positionable, IPinnable, IColorable { _nodes: LGraphNode[] = [] _children: Set = new Set() graph?: LGraph - flags: IGraphGroupFlags = {} selected?: boolean /** Background colour last used to compute {@link _titleTextColor} */ @@ -97,16 +95,71 @@ export class LGraphGroup implements Positionable, IPinnable, IColorable { constructor(title?: string, id?: GroupId) { // TODO: Object instantiation pattern requires too much boilerplate and null checking. ID should be passed in via constructor. this.id = toGroupId(id ?? -1) - this.title = title || 'Group' - const { pale_blue } = LGraphCanvas.node_colors - this.color = pale_blue ? pale_blue.groupcolor : '#AAA' + this._presentation = { + title: title || 'Group', + color: pale_blue ? pale_blue.groupcolor : '#AAA', + font_size: LiteGraph.GROUP_TEXT_SIZE, + flags: {} + } + for (const property of [ + 'title', + 'color', + 'font', + 'font_size', + 'flags' + ] as const) { + Object.defineProperty(this, property, { + ...Object.getOwnPropertyDescriptor(LGraphGroup.prototype, property), + enumerable: true + }) + } + } + + get title(): string { + return this._presentation.title + } + + set title(value: string) { + this._presentation.title = value + } + + get color(): string | undefined { + return this._presentation.color + } + + set color(value: string | undefined) { + this._presentation.color = value + } + + get font(): string | undefined { + return this._presentation.font + } + + set font(value: string | undefined) { + this._presentation.font = value + } + + get font_size(): number { + return this._presentation.font_size + } + + set font_size(value: number) { + this._presentation.font_size = value + } + + get flags(): IGraphGroupFlags { + return this._presentation.flags + } + + set flags(value: IGraphGroupFlags) { + this._presentation.flags = value } /** @inheritdoc {@link IColorable.setColorOption} */ setColorOption(colorOption: ColorOption | null): void { if (colorOption == null) { - delete this.color + this.color = undefined } else { this.color = colorOption.groupcolor } @@ -478,3 +531,30 @@ export class LGraphGroup implements Positionable, IPinnable, IColorable { } setDirtyCanvas = LGraphNode.prototype.setDirtyCanvas } + +export function registerGroupPresentation( + graph: Pick, + group: LGraphGroup +): void { + group._presentation = useGraphDefinitionStore().registerGroupPresentation( + graph.rootGraph.id, + graph.id, + group.id, + group._presentation + ) +} + +export function unregisterGroupPresentation(group: LGraphGroup): void { + const graph = group.graph + if (!graph) return + const detached = { + ...group._presentation, + flags: { ...group.flags } + } + useGraphDefinitionStore().deleteGroupPresentation( + graph.rootGraph.id, + graph.id, + group.id + ) + group._presentation = detached +} diff --git a/src/lib/litegraph/src/LGraphNode.nodeState.test.ts b/src/lib/litegraph/src/LGraphNode.nodeState.test.ts index 777667bffa3..b2a60d93ece 100644 --- a/src/lib/litegraph/src/LGraphNode.nodeState.test.ts +++ b/src/lib/litegraph/src/LGraphNode.nodeState.test.ts @@ -1,7 +1,7 @@ import { createTestingPinia } from '@pinia/testing' import { setActivePinia } from 'pinia' import { beforeEach, describe, expect, it, vi } from 'vitest' -import { computed } from 'vue' +import { computed, toRaw } from 'vue' import { useNodeDataStore } from '@/stores/nodeDataStore' import { toNodeId } from '@/types/nodeId' @@ -10,6 +10,8 @@ import type { NodeState } from '@/types/nodeState' import { LGraphNode } from './litegraph' import type { Subgraph } from './litegraph' +import { NodeInputSlot } from './node/NodeInputSlot' +import { NodeOutputSlot } from './node/NodeOutputSlot' import { createTestSubgraph } from './subgraph/__fixtures__/subgraphHelpers' describe('LGraphNode node-data adoption', () => { @@ -36,15 +38,90 @@ describe('LGraphNode node-data adoption', () => { const { subgraph, node } = addNodeToSubgraph() const title = computed(() => statesIn(subgraph)[0]?.title) + const boxcolor = computed(() => statesIn(subgraph)[0]?.boxcolor) node.title = 'Renamed' + node.boxcolor = '#778899' expect(title.value).toBe('Renamed') + expect(boxcolor.value).toBe('#778899') expect(node.title).toBe('Renamed') + expect(node.boxcolor).toBe('#778899') node.flags.collapsed = true expect(statesIn(subgraph)[0]?.flags.collapsed).toBe(true) }) + it('keeps direct property mutation and assignment store-backed', () => { + const { subgraph, node } = addNodeToSubgraph() + const properties = node.properties + + node.properties.settings = { strength: 0.5 } + node.properties = { model: 'flux.safetensors' } + + expect(node.properties).toBe(properties) + expect(statesIn(subgraph)[0]?.properties).toBe(properties) + expect(properties).toEqual({ model: 'flux.safetensors' }) + }) + + it('orchestrates setProperty callbacks and property-backed widgets', () => { + const node = new LGraphNode('Node') + const widget = node.addWidget('text', 'model', '', () => undefined, { + property: 'model' + }) + node.properties.model = 'old' + node.onPropertyChanged = vi.fn((_name, value) => value !== 'rejected') + + node.setProperty('model', 'new') + expect(node.properties.model).toBe('new') + expect(widget.value).toBe('new') + expect(node.onPropertyChanged).toHaveBeenCalledWith('model', 'new', 'old') + + node.setProperty('model', 'rejected') + expect(node.properties.model).toBe('new') + }) + + it('configures and round-trips properties through the store authority', () => { + const { subgraph, node } = addNodeToSubgraph() + const properties = node.properties + node.onPropertyChanged = vi.fn() + const serialised = { + ...node.serialize(), + properties: { nested: { value: 1 } } + } + + node.configure(serialised) + const roundTrip = node.serialize() + + expect(node.properties).toBe(properties) + expect(statesIn(subgraph)[0]?.properties).toBe(properties) + expect(node.onPropertyChanged).toHaveBeenCalledWith('nested', { + value: 1 + }) + expect(roundTrip.properties).toEqual(serialised.properties) + expect(roundTrip.properties).not.toBe(node.properties) + }) + + it('stores plain slot descriptors behind stable class projections', () => { + const { subgraph, node } = addNodeToSubgraph() + const input = node.addInput('prompt', 'STRING') + const output = node.addOutput('result', 'STRING') + const [state] = statesIn(subgraph) + + expect(Object.getPrototypeOf(toRaw(state.inputs[0]))).toBe(Object.prototype) + expect(Object.getPrototypeOf(toRaw(state.outputs[0]))).toBe( + Object.prototype + ) + expect(node.inputs[0]).toBe(input) + expect(node.outputs[0]).toBe(output) + expect(input).toBeInstanceOf(NodeInputSlot) + expect(output).toBeInstanceOf(NodeOutputSlot) + + input.label = 'Prompt' + output.label = 'Result' + expect(state.inputs[0].label).toBe('Prompt') + expect(state.outputs[0].label).toBe('Result') + }) + it('exposes enumerable own collection fields without replacing their views', () => { const { node } = addNodeToSubgraph() node.addWidget('text', 'prompt', '', () => undefined) @@ -59,8 +136,15 @@ describe('LGraphNode node-data adoption', () => { expect(Object.hasOwn(node, 'inputs')).toBe(true) expect(Object.hasOwn(node, 'outputs')).toBe(true) expect(Object.hasOwn(node, 'widgets')).toBe(true) + expect(Object.hasOwn(node, 'boxcolor')).toBe(true) expect(Object.keys(node)).toEqual( - expect.arrayContaining(['inputs', 'outputs', 'widgets']) + expect.arrayContaining([ + 'inputs', + 'outputs', + 'properties', + 'widgets', + 'boxcolor' + ]) ) expect(node.inputs).toBe(inputs) expect(node.outputs).toBe(outputs) diff --git a/src/lib/litegraph/src/LGraphNode.test.ts b/src/lib/litegraph/src/LGraphNode.test.ts index f6beb7e35be..49bb6277188 100644 --- a/src/lib/litegraph/src/LGraphNode.test.ts +++ b/src/lib/litegraph/src/LGraphNode.test.ts @@ -1028,6 +1028,34 @@ describe('layout geometry projection', () => { }) }) + test('keeps legacy buffers and views stable across store updates', () => { + const graph = new LGraph() + const node = new LGraphNode('test') + graph.add(node) + const pos = node.pos + const size = node.size + const posBuffer = node._pos + const sizeBuffer = node._size + + layoutStore.batchUpdateNodeBounds( + graph.rootGraph.id, + [ + { + nodeId: node.id, + bounds: { x: 30, y: 40, width: 200, height: 80 } + } + ], + { source: LayoutSource.Canvas } + ) + + expect([...node.pos]).toEqual([30, 40]) + expect([...node.size]).toEqual([200, 80]) + expect(node.pos).toBe(pos) + expect(node.size).toBe(size) + expect(node._pos).toBe(posBuffer) + expect(node._size).toBe(sizeBuffer) + }) + test('preserves stored size when assigning position', () => { const graph = new LGraph() const node = new LGraphNode('test') diff --git a/src/lib/litegraph/src/LGraphNode.ts b/src/lib/litegraph/src/LGraphNode.ts index 196004c771a..676716ab72d 100644 --- a/src/lib/litegraph/src/LGraphNode.ts +++ b/src/lib/litegraph/src/LGraphNode.ts @@ -10,18 +10,28 @@ import { } from '@/renderer/core/canvas/litegraph/slotCalculations' import type { SlotPositionContext } from '@/renderer/core/canvas/litegraph/slotCalculations' import { - moveNodeLayout, - resizeNodeLayout + nodeGeometryBuffer, + nodePositionBuffer, + nodePositionView, + nodeSizeBuffer, + nodeSizeView, + refreshNodeGeometry, + setNodePosition, + setNodeSize } from '@/renderer/core/layout/operations/graphLayoutAttachment' import { layoutStore } from '@/renderer/core/layout/store/layoutStore' +import { useExecutionOrderStore } from '@/stores/executionOrderStore' +import { useWidgetValueStore } from '@/stores/widgetValueStore' +import { graphScopeOf } from '@/types/graphScopeId' import { toLinkId } from '@/types/linkId' import type { GraphScope } from '@/types/graphScopeId' import { mintLinkId } from './idAllocation' import { UNASSIGNED_NODE_ID, toNodeId, serializeNodeId } from '@/types/nodeId' import type { NodeId } from '@/types/nodeId' -import type { NodeState } from '@/types/nodeState' +import type { NodeProperty, NodeState } from '@/types/nodeState' import { adjustColor } from '@/utils/colorUtil' import type { ColorAdjustOptions } from '@/utils/colorUtil' +import { zeroUuid } from '@/utils/uuid' import { commonType, isNodeBindable, @@ -52,11 +62,17 @@ import { outputLinks } from './node/slotLinks' import { initializeWidgetsView } from './node/widgetsView' +import { + extensionConfigureView, + hydrateExtensionPayload, + NODE_CANONICAL_FIELDS, + runExtensionSerializeHook +} from './extensionPersistence' +import { inputSlotView, outputSlotView } from './node/slotDescriptorView' import { anchorRerouteChain } from './Reroute' import type { Reroute, RerouteId } from './Reroute' import { getNodeInputOnPos, getNodeOutputOnPos } from './canvas/measureSlots' import type { IDrawBoundingOptions } from './draw' -import { createMutationView } from './infrastructure/createMutationView' import { NullGraphError } from './infrastructure/NullGraphError' import type { ReadOnlyRectangle } from './infrastructure/Rectangle' import { Rectangle } from './infrastructure/Rectangle' @@ -131,9 +147,7 @@ import type { WidgetTypeMap } from './widgets/widgetMap' // #region Types -export type NodeProperty = string | number | boolean | object | null - -const storedRectScratch = new Float64Array(4) +export type { NodeProperty } from '@/types/nodeState' interface INodePropertyInfo { name?: string @@ -266,6 +280,8 @@ const NAMED_SHAPES = { * @param type a type for the node */ +const wireExecutionOrders = new WeakMap() + export class LGraphNode implements NodeLike, Positionable, IPinnable, IColorable { @@ -350,28 +366,39 @@ export class LGraphNode /** Assignment splices in place: the `shallowReactive` array identity is what the renderer tracks. */ get inputs(): INodeInputSlot[] { - return this._state.inputs + return this._inputs } set inputs(value: INodeInputSlot[] | null | undefined) { - const { inputs } = this._state - inputs.splice(0, inputs.length, ...(value ?? [])) + this._inputs.splice(0, this._inputs.length, ...(value ?? [])) } /** @see {@link inputs} */ get outputs(): INodeOutputSlot[] { - return this._state.outputs + return this._outputs } set outputs(value: INodeOutputSlot[] | null | undefined) { - const { outputs } = this._state - outputs.splice(0, outputs.length, ...(value ?? [])) + this._outputs.splice(0, this._outputs.length, ...(value ?? [])) } + private readonly _inputs: INodeInputSlot[] + private readonly _outputs: INodeOutputSlot[] + private _concreteInputs: NodeInputSlot[] = [] private _concreteOutputs: NodeOutputSlot[] = [] - properties: Dictionary = {} + get properties(): Dictionary { + return this._state.properties + } + + set properties(value: Dictionary) { + if (value === this._state.properties) return + for (const key of Object.keys(this._state.properties)) + delete this._state.properties[key] + Object.assign(this._state.properties, value) + } + properties_info: INodePropertyInfo[] = [] get flags(): INodeFlags { @@ -399,7 +426,17 @@ export class LGraphNode locked?: boolean /** Execution order, automatically computed during run @see {@link LGraph.computeExecutionOrder} */ - order: number = 0 + get order(): number { + return this.graph + ? (useExecutionOrderStore().get(graphScopeOf(this.graph), this.id) ?? + wireExecutionOrders.get(this) ?? + 0) + : (wireExecutionOrders.get(this) ?? 0) + } + + set order(value: number) { + wireExecutionOrders.set(this, value) + } get mode(): LGraphEventMode { return this._state.mode } @@ -407,7 +444,13 @@ export class LGraphNode set mode(value: LGraphEventMode) { setTrackedNodeState(this, 'mode', value) } - last_serialization?: ISerialisedNode + get last_serialization(): ISerialisedNode | undefined { + return this._state.lastSerialization + } + + set last_serialization(value: ISerialisedNode | undefined) { + this._state.lastSerialization = value + } serialize_widgets?: boolean /** * The overridden fg color used to render the node. @@ -435,7 +478,13 @@ export class LGraphNode * The overridden box color used to render the node. * @see {@link renderingBoxColor} */ - boxcolor?: string + get boxcolor(): string | undefined { + return this._state.boxcolor + } + + set boxcolor(value: string | undefined) { + setTrackedNodeState(this, 'boxcolor', value) + } /** The fg color used to render the node. */ get renderingColor(): string { @@ -623,69 +672,27 @@ export class LGraphNode return [posX - bX, posY - bY] } - _posSize = new Rectangle() - _pos: Point = this._posSize.pos - _size: Size = this._posSize.size - private readonly _renderedSize: Size = [0, 0] - private readonly posView = createMutationView(this._pos, { - commit: () => this._positionUpdated(), - synchronize: () => this.refreshGeometry() - }) - private readonly sizeView = createMutationView(this._size, { - commit: () => this._sizeUpdated(), - synchronize: () => this.refreshGeometry() - }) - - _geometryVersion = -1 - - _layoutRegistered = false - - private refreshGeometry(): void { - const rootGraphId = this.graph?.rootGraph.id - if (this._layoutRegistered && rootGraphId) { - const { geometryVersion } = layoutStore - if (geometryVersion !== this._geometryVersion) { - this._geometryVersion = geometryVersion - layoutStore.readNodeRect(rootGraphId, this.id, this._posSize) - } - } + get _posSize(): Rectangle { + return nodeGeometryBuffer(this) + } - const contentSize = rootGraphId - ? layoutStore.contentSizeOf(rootGraphId, this.id) - : undefined - this._renderedSize[0] = Math.max(this._size[0], contentSize?.width ?? 0) - this._renderedSize[1] = Math.max(this._size[1], contentSize?.height ?? 0) + get _pos(): Point { + return nodePositionBuffer(this) + } + + get _size(): Size { + return nodeSizeBuffer(this) } - public get pos() { - return this.posView + public get pos(): Point { + return nodePositionView(this) } /** Node position does not necessarily correlate to the top-left corner. */ public set pos(value) { if (!value || value.length < 2) return - this._pos[0] = value[0] - this._pos[1] = value[1] - this._positionUpdated() - } - - private _positionUpdated(): void { - if (this.id === UNASSIGNED_NODE_ID || !this.graph) return - - const rootGraphId = this.graph.rootGraph.id - const position = { x: this._pos[0], y: this._pos[1] } - if ( - layoutStore.readNodeRect(rootGraphId, this.id, storedRectScratch) && - storedRectScratch[0] === position.x && - storedRectScratch[1] === position.y - ) { - return - } - - moveNodeLayout(this, position) - this._geometryVersion = -1 - this.refreshGeometry() + setNodePosition(this, value) } /** @@ -696,44 +703,21 @@ export class LGraphNode } public get size(): Size { - return this.sizeView + return nodeSizeView(this) } public set size(value) { if (!value || value.length < 2) return - this._size[0] = value[0] - this._size[1] = value[1] - this._sizeUpdated() - } - - /** Mirror the current {@link _size} into the layout store for Vue nodes. */ - private _sizeUpdated(): void { - if (this.id === UNASSIGNED_NODE_ID || !this.graph) return - - const rootGraphId = this.graph.rootGraph.id - if ( - layoutStore.readNodeRect(rootGraphId, this.id, storedRectScratch) && - storedRectScratch[2] === this._size[0] && - storedRectScratch[3] === this._size[1] - ) { - return - } - - resizeNodeLayout(this, { - width: this._size[0], - height: this._size[1] - }) - this._geometryVersion = -1 - this.refreshGeometry() + setNodeSize(this, value) } /** * The size of the node used for rendering. */ get renderingSize(): Size { - this.refreshGeometry() - if (!this.flags.collapsed) return this._renderedSize + const renderedSize = refreshNodeGeometry(this) + if (!this.flags.collapsed) return renderedSize const contentSize = this.graph ? layoutStore.contentSizeOf(this.graph.rootGraph.id, this.id) @@ -1003,7 +987,16 @@ export class LGraphNode constructor(title: string, type?: string) { initializeWidgetsView(this) this._state = createNodeShellState(title, type, this.title_mode) - for (const property of ['inputs', 'outputs'] as const) { + this._inputs = inputSlotView(this._state.inputs, this) + this._outputs = outputSlotView(this._state.outputs, this) + for (const property of [ + 'inputs', + 'outputs', + 'properties', + 'order', + 'boxcolor', + 'last_serialization' + ] as const) { Object.defineProperty(this, property, { ...Object.getOwnPropertyDescriptor(LGraphNode.prototype, property), enumerable: true @@ -1024,10 +1017,12 @@ export class LGraphNode * configure a node from an object containing the serialized info */ configure(info: ISerialisedNode): void { + hydrateExtensionPayload(this, info, NODE_CANONICAL_FIELDS) if (this.graph) { this.graph.incrementVersion() } for (const j in info) { + if (!NODE_CANONICAL_FIELDS.has(j)) continue if (j == 'properties') { // i don't want to clone properties, I want to reuse the old container for (const k in info.properties) { @@ -1103,6 +1098,25 @@ export class LGraphNode // SubgraphNode callback. this._internalConfigureAfterSlots?.() + const positionalValues = Array.from(info.widgets_values ?? []) + const getNamedValues = () => { + if (info.widgets_values_named) return info.widgets_values_named + + const map = this.constructor.nodeData?.fallbackWidgetsValuesNames + if (!info.widgets_values || !map) return + + return Object.fromEntries( + positionalValues.flatMap((v, i) => (map[i] ? [[map[i], v]] : [])) + ) + } + const namedValues = getNamedValues() + const graphId = this.graph?.rootGraph.id ?? zeroUuid + useWidgetValueStore().setNodeWidgetRestoration(graphId, this.id, { + positional: positionalValues, + named: namedValues ? { ...namedValues } : undefined, + restoreNamed: Boolean(namedValues && LiteGraph.namedValuesRestore) + }) + if (this.widgets) { for (const w of this.widgets) { if (!w) continue @@ -1119,18 +1133,6 @@ export class LGraphNode ) } - const getNamedValues = () => { - if (info.widgets_values_named) return info.widgets_values_named - - const map = this.constructor.nodeData?.fallbackWidgetsValuesNames - if (!info.widgets_values || !map) return - - return Object.fromEntries( - info.widgets_values.flatMap((v, i) => (map[i] ? [[map[i], v]] : [])) - ) - } - - const namedValues = getNamedValues() if (namedValues) { const legacyShadow = computeLegacyWidgetShadow( this.widgets, @@ -1143,20 +1145,16 @@ export class LGraphNode ) } - if (namedValues && LiteGraph.namedValuesRestore) { - for (const widget of this.widgets) { - if (widget.serialize === false || !(widget.name in namedValues)) - continue - - widget.value = namedValues[widget.name] - } - } else if (info.widgets_values) { - let i = 0 - for (const widget of this.widgets ?? []) { - if (widget.serialize === false) continue - if (i >= info.widgets_values.length) break - widget.value = info.widgets_values[i++] - } + let positionalIndex = 0 + for (const widget of this.widgets) { + if (widget.serialize === false) continue + const restored = useWidgetValueStore().getRestoredWidgetValue( + graphId, + this.id, + widget.name, + positionalIndex++ + ) + if (restored) widget.value = restored.value } } @@ -1171,41 +1169,46 @@ export class LGraphNode ) } - this.onConfigure?.(info) + this.onConfigure?.(extensionConfigureView(this, info)) } /** * serialize the content */ serialize(): ISerialisedNode { + return this.serializeFromStoreState(this._state) + } + + serializeFromStoreState(state: NodeState): ISerialisedNode { // create serialization object const o: ISerialisedNode = { - id: serializeNodeId(this.id), - type: this.type, + id: serializeNodeId(state.id), + type: state.type, pos: [this.pos[0], this.pos[1]], size: [this.size[0], this.size[1]], - flags: LiteGraph.cloneObject(this.flags), + flags: LiteGraph.cloneObject(state.flags), order: this.order, - mode: this.mode, - showAdvanced: this.showAdvanced + mode: state.mode, + showAdvanced: state.showAdvanced } // special case for when there were errors - if (this.constructor === LGraphNode && this.last_serialization) - return { ...this.last_serialization, mode: o.mode, pos: o.pos } + if (this.constructor === LGraphNode && state.lastSerialization) + return { ...state.lastSerialization, mode: o.mode, pos: o.pos } - if (this.inputs) - o.inputs = this.inputs.map((input, i) => + if (state.inputs) + o.inputs = state.inputs.map((input, i) => inputAsSerialisable(input, this, i) ) - if (this.outputs) - o.outputs = this.outputs.map((output, i) => + if (state.outputs) + o.outputs = state.outputs.map((output, i) => outputAsSerialisable(output, this, i) ) - if (this.title && this.title != this.constructor.title) o.title = this.title + if (state.title && state.title != this.constructor.title) + o.title = state.title - if (this.properties) o.properties = LiteGraph.cloneObject(this.properties) + if (state.properties) o.properties = LiteGraph.cloneObject(state.properties) const { widgets } = this if (widgets?.length && this.serialize_widgets) { @@ -1226,17 +1229,26 @@ export class LGraphNode if (!o.type && this.constructor.type) o.type = this.constructor.type - if (this.color) o.color = this.color - if (this.bgcolor) o.bgcolor = this.bgcolor - if (this.boxcolor) o.boxcolor = this.boxcolor - if (this.shape) o.shape = this.shape + if (state.color) o.color = state.color + if (state.bgcolor) o.bgcolor = state.bgcolor + if (state.boxcolor) o.boxcolor = state.boxcolor + if (state.shape) o.shape = state.shape - if (this.onSerialize?.(o)) + let hookResult: unknown + const serialised = runExtensionSerializeHook( + this, + o, + NODE_CANONICAL_FIELDS, + (data) => { + hookResult = this.onSerialize?.(data) + } + ) + if (hookResult) console.warn( "node onSerialize shouldn't return anything, data should be stored in the object pass in the first parameter" ) - return o + return serialised } clone(): LGraphNode | null { @@ -1853,14 +1865,15 @@ export class LGraphNode this.outputs ||= [] this.outputs.push(output) - this.onOutputAdded?.(output) + const added = this.outputs.at(-1) as NodeOutputSlot & TProperties + this.onOutputAdded?.(added) if (LiteGraph.auto_load_slot_types) LiteGraph.registerNodeAndSlotType(this, type, true) this.expandToFitContent() this.setDirtyCanvas(true, true) - return output + return added } /** @@ -1919,13 +1932,14 @@ export class LGraphNode this.inputs ||= [] this.inputs.push(input) + const added = this.inputs.at(-1) as NodeInputSlot & TProperties this.expandToFitContent() - this.onInputAdded?.(input) + this.onInputAdded?.(added) LiteGraph.registerNodeAndSlotType(this, type) this.setDirtyCanvas(true, true) - return input + return added } /** @@ -2231,6 +2245,19 @@ export class LGraphNode widget.setNodeId(this.id) } + if (widget.serialize === false) return widget + + const positionalIndex = + this.widgets.filter((candidate) => candidate.serialize !== false).length - + 1 + const restored = useWidgetValueStore().getRestoredWidgetValue( + this.graph?.rootGraph.id ?? zeroUuid, + this.id, + widget.name, + positionalIndex + ) + if (restored) widget.value = restored.value + return widget } @@ -2282,7 +2309,7 @@ export class LGraphNode move(deltaX: number, deltaY: number): void { if (this.pinned) return - this.refreshGeometry() + refreshNodeGeometry(this) this.pos = [this._pos[0] + deltaX, this._pos[1] + deltaY] } @@ -3604,7 +3631,7 @@ export class LGraphNode snapToGrid(snapTo: number): boolean { if (this.pinned || !snapTo) return false - this.refreshGeometry() + refreshNodeGeometry(this) const snapped: Point = [this._pos[0], this._pos[1]] snapPoint(snapped, snapTo) if (snapped[0] === this._pos[0] && snapped[1] === this._pos[1]) return false @@ -4394,12 +4421,6 @@ export class LGraphNode this._concreteOutputs = this.outputs.map((slot) => toClass(NodeOutputSlot, slot, this) ) - for (const [i, slot] of this._concreteInputs.entries()) { - this.inputs[i] = slot - } - for (const [i, slot] of this._concreteOutputs.entries()) { - this.outputs[i] = slot - } } /** diff --git a/src/lib/litegraph/src/LGraphNode.widgetOrder.test.ts b/src/lib/litegraph/src/LGraphNode.widgetOrder.test.ts index 77fd288b087..9748703e721 100644 --- a/src/lib/litegraph/src/LGraphNode.widgetOrder.test.ts +++ b/src/lib/litegraph/src/LGraphNode.widgetOrder.test.ts @@ -4,7 +4,10 @@ import { beforeEach, describe, expect, it } from 'vitest' import { addDynamicCombo } from '@/core/graph/widgets/__fixtures__/dynamicInputHelpers' import { LGraphNode, LiteGraph } from '@/lib/litegraph/src/litegraph' import type { ISerialisedNode } from '@/lib/litegraph/src/types/serialisation' -import type { TWidgetValue } from '@/lib/litegraph/src/types/widgets' +import type { + IBaseWidget, + TWidgetValue +} from '@/lib/litegraph/src/types/widgets' import { sortWidgetValuesByInputOrder } from '@/workbench/utils/nodeDefOrderingUtil' describe('LGraphNode widget ordering', () => { @@ -100,6 +103,26 @@ describe('LGraphNode widget ordering', () => { expect(node.widgets![1].value).toBe('Click') // button unchanged expect(node.widgets![2].value).toBe(12345) // seed }) + + it('restores positional values for widgets created after configure', () => { + node.configure({ + id: 1, + type: 'TestNode', + pos: [0, 0], + size: [200, 100], + flags: {}, + order: 0, + mode: 0, + widgets_values: [30, 12345] + }) + + node.addWidget('number', 'steps', 0, null, {}) + node.addWidget('number', 'seed', 0, null, {}) + + expect(node.widgets!.map((widget) => widget.value)).toStrictEqual([ + 30, 12345 + ]) + }) }) describe('configure with widgets_values_named', () => { @@ -161,6 +184,22 @@ describe('LGraphNode widget ordering', () => { expect(node.widgets!.map((w) => w.value)).toStrictEqual([1, 5, 'test']) }) + it('restores delayed widgets by name and preserves the wire roundtrip', () => { + node.configure(mockNode([30, 12345], { steps: 30, seed: 12345 })) + + node.addWidget('number', 'seed', 0, null, {}) + node.addWidget('number', 'steps', 0, null, {}) + node.serialize_widgets = true + + expect(node.widgets!.map((widget) => widget.value)).toStrictEqual([ + 12345, 30 + ]) + expect(node.serialize()).toMatchObject({ + widgets_values: [12345, 30], + widgets_values_named: { seed: 12345, steps: 30 } + }) + }) + it('should support restoration even when order has changed', () => { node.addWidget('number', 'steps', 20, null, {}) node.addWidget('number', 'seed', 5, null, {}) @@ -186,6 +225,46 @@ describe('LGraphNode widget ordering', () => { expect(node.widgets!.map((w) => w.value)).toStrictEqual([5, 20]) }) + + it('supports fallback names for non-iterable array-like values', () => { + node.addWidget('number', 'steps', 0, null, {}) + node.addWidget('number', 'seed', 0, null, {}) + const nodeData = fromPartial({ + fallbackWidgetsValuesNames: ['seed', 'steps'] + }) + node.constructor = Object.assign({}, node.constructor, { nodeData }) + + const info = mockNode() + info.widgets_values = [20, 5] + Object.defineProperties(info.widgets_values, { + [Symbol.iterator]: { value: undefined }, + flatMap: { value: undefined } + }) + node.configure(info) + + expect(node.widgets!.map((widget) => widget.value)).toStrictEqual([5, 20]) + }) + + it('does not restore dynamically added non-serializable widgets', () => { + node.onConfigure = function () { + this.addCustomWidget( + fromPartial({ + type: 'button', + name: 'action', + value: 'Click', + serialize: false + }) + ) + this.addWidget('number', 'seed', 0, null, {}) + } + + node.configure(mockNode([12345], { action: 'Restored', seed: 12345 })) + + expect(node.widgets!.map((widget) => widget.value)).toStrictEqual([ + 'Click', + 12345 + ]) + }) }) }) diff --git a/src/lib/litegraph/src/LLink.store.test.ts b/src/lib/litegraph/src/LLink.store.test.ts index 1443e2e93a2..ad64dd14836 100644 --- a/src/lib/litegraph/src/LLink.store.test.ts +++ b/src/lib/litegraph/src/LLink.store.test.ts @@ -5,6 +5,7 @@ import { computed } from 'vue' import { LGraph, LGraphNode, LLink } from '@/lib/litegraph/src/litegraph' import { useLinkStore } from '@/stores/linkStore' +import { useLinkStateStore } from '@/stores/linkStateStore' import { graphScopeOf, toOwningGraphId } from '@/types/graphScopeId' import { toLinkId } from '@/types/linkId' import { UNASSIGNED_NODE_ID, toNodeId } from '@/types/nodeId' @@ -136,6 +137,63 @@ describe('LLink ↔ linkStore integration', () => { expect(store.isInputSlotConnected(graphScopeOf(graph), b.id, 0)).toBe(false) }) + it('registers non-topological state and removes it with the link', () => { + const graph = new LGraph() + const a = new LGraphNode('A') + const b = new LGraphNode('B') + a.addOutput('out', 'INT') + b.addInput('in', 'INT') + graph.add(a) + graph.add(b) + + const link = a.connect(0, b, 0)! + const scope = graphScopeOf(graph) + const state = useLinkStateStore().get(scope, link.id) + expect(state).toBe(link._linkState) + + graph.removeLink(link.id) + + expect(useLinkStateStore().get(scope, link.id)).toBeUndefined() + expect(link._linkState).not.toBe(state) + }) + + it('preserves extension-visible link fields through store-backed state', () => { + vi.stubGlobal('Path2D', class {}) + const graph = new LGraph() + const link = new LLink( + toLinkId(1), + '*', + UNASSIGNED_NODE_ID, + -1, + UNASSIGNED_NODE_ID, + -1 + ) + graph.addFloatingLink(link) + const path = new Path2D() + + link.data = 42 + link._data = { output: true } + link._pos = [10, 20] + link._last_time = 30 + link.path = path + link._centreAngle = 0.5 + link._dragging = true + link.color = '' + + const state = useLinkStateStore().get(graphScopeOf(graph), link.id)! + expect(state.runtime).toEqual({ + data: 42, + outputData: { output: true }, + position: [10, 20], + lastTime: 30, + path, + centreAngle: 0.5, + dragging: true + }) + expect(state.persistent.color).toBeNull() + expect(link.color).toBeNull() + }) + it('commits a replacement before disconnect callbacks run', () => { const graph = new LGraph() const first = new LGraphNode('first') diff --git a/src/lib/litegraph/src/LLink.ts b/src/lib/litegraph/src/LLink.ts index e1fd6c8cec1..5c4e0ce9b71 100644 --- a/src/lib/litegraph/src/LLink.ts +++ b/src/lib/litegraph/src/LLink.ts @@ -6,6 +6,7 @@ import type { SubgraphInput } from '@/lib/litegraph/src/subgraph/SubgraphInput' import type { SubgraphOutput } from '@/lib/litegraph/src/subgraph/SubgraphOutput' import { layoutStore } from '@/renderer/core/layout/store/layoutStore' import { useLinkStore } from '@/stores/linkStore' +import { createLinkState, useLinkStateStore } from '@/stores/linkStateStore' import { graphScopeOf, toOwningGraphId } from '@/types/graphScopeId' import type { GraphScope } from '@/types/graphScopeId' import { zeroUuid } from '@/utils/uuid' @@ -14,6 +15,7 @@ import { UNASSIGNED_NODE_ID, toNodeId, serializeNodeId } from '@/types/nodeId' import { toRerouteId } from '@/types/rerouteId' import type { EndpointPatch } from '@/stores/linkStore' +import type { LinkExecutionData, LinkState } from '@/stores/linkStateStore' import type { LinkId } from '@/types/linkId' import type { LinkTopology } from '@/types/linkTopology' import type { RerouteId } from '@/types/rerouteId' @@ -200,28 +202,76 @@ export class LLink implements LinkSegment, Serialisable { this._state.parentId = value } - data?: number | string | boolean | { toToolTip?(): string } - _data?: unknown + _linkState: LinkState + + get data(): LinkExecutionData | undefined { + return this._linkState.runtime.data + } + + set data(value: LinkExecutionData | undefined) { + this._linkState.runtime.data = value + } + + get _data(): unknown { + return this._linkState.runtime.outputData + } + + set _data(value: unknown) { + this._linkState.runtime.outputData = value + } + /** Centre point of the link, calculated during render only - can be inaccurate */ - _pos: Point + get _pos(): Point { + return this._linkState.runtime.position + } + + set _pos(value: Point) { + this._linkState.runtime.position = value + } + /** @todo Clean up - never implemented in comfy. */ - _last_time?: number + get _last_time(): number | undefined { + return this._linkState.runtime.lastTime + } + + set _last_time(value: number | undefined) { + this._linkState.runtime.lastTime = value + } + /** The last canvas 2D path that was used to render this link */ - path?: Path2D + get path(): Path2D | undefined { + return this._linkState.runtime.path + } + + set path(value: Path2D | undefined) { + this._linkState.runtime.path = value + } + /** @inheritdoc */ - _centreAngle?: number + get _centreAngle(): number | undefined { + return this._linkState.runtime.centreAngle + } + + set _centreAngle(value: number | undefined) { + this._linkState.runtime.centreAngle = value + } /** @inheritdoc */ - _dragging?: boolean + get _dragging(): boolean | undefined { + return this._linkState.runtime.dragging + } + + set _dragging(value: boolean | undefined) { + this._linkState.runtime.dragging = value + } - private _color?: CanvasColour | null /** Custom colour for this link only */ public get color(): CanvasColour | null | undefined { - return this._color + return this._linkState.persistent.color } public set color(value: CanvasColour) { - this._color = value === '' ? null : value + this._linkState.persistent.color = value === '' ? null : value } public get isFloatingOutput(): boolean { @@ -255,6 +305,7 @@ export class LLink implements LinkSegment, Serialisable { target_slot: number, parentId?: RerouteId ) { + this._linkState = createLinkState() this._state = { id, graphId: toOwningGraphId(zeroUuid), @@ -265,10 +316,6 @@ export class LLink implements LinkSegment, Serialisable { targetSlot: target_slot, parentId } - - this._data = null - // center - this._pos = [0, 0] } /** @deprecated Use {@link LLink.create} */ @@ -661,6 +708,7 @@ export function replaceLinkTopology( ) if (!registered) return false if (incumbent) { + unregisterLinkState(incumbent) linkByTopology.delete(toRaw(incumbent._state)) incumbent._graphScope = undefined } @@ -675,6 +723,11 @@ function adoptLinkTopology( ): void { link._state = registered link._graphScope = scope + link._linkState = useLinkStateStore().register( + scope, + link.id, + link._linkState + ) linkByTopology.set(toRaw(registered), link) } @@ -686,11 +739,26 @@ function adoptLinkTopology( */ export function unregisterLinkTopology(link: LLink): void { if (!link._graphScope) return + unregisterLinkState(link) useLinkStore().deleteLink(link._graphScope, link._state) linkByTopology.delete(toRaw(link._state)) link._graphScope = undefined } +function unregisterLinkState(link: LLink): void { + if (!link._graphScope) return + const registered = link._linkState + if (!useLinkStateStore().unregister(link._graphScope, link.id, registered)) + return + link._linkState = { + persistent: { ...registered.persistent }, + runtime: { + ...registered.runtime, + position: [...registered.runtime.position] + } + } +} + /** * Unregisters every link and floating link a graph owns. Used when a graph's * links leave the store without a whole-bucket wipe: subgraph-definition diff --git a/src/lib/litegraph/src/canvas/LinkConnector.ts b/src/lib/litegraph/src/canvas/LinkConnector.ts index 613a5cf18b4..6ef17153b6b 100644 --- a/src/lib/litegraph/src/canvas/LinkConnector.ts +++ b/src/lib/litegraph/src/canvas/LinkConnector.ts @@ -1117,9 +1117,9 @@ export class LinkConnector { if (!force && state.connectingTo === undefined) return state.connectingTo = undefined - for (const link of outputLinks) delete link._dragging - for (const link of inputLinks) delete link._dragging - for (const link of floatingLinks) delete link._dragging + for (const link of outputLinks) link._dragging = undefined + for (const link of inputLinks) link._dragging = undefined + for (const link of floatingLinks) link._dragging = undefined for (const reroute of hiddenReroutes) delete reroute._dragging renderLinks.length = 0 diff --git a/src/lib/litegraph/src/extensionPersistence.ts b/src/lib/litegraph/src/extensionPersistence.ts new file mode 100644 index 00000000000..8bc72b89309 --- /dev/null +++ b/src/lib/litegraph/src/extensionPersistence.ts @@ -0,0 +1,141 @@ +type JsonValue = null | boolean | number | string | JsonValue[] | JsonObject +type JsonObject = { [key: string]: JsonValue } + +type ExtensionPayload = Record + +const payloads = new WeakMap() + +export const NODE_CANONICAL_FIELDS = new Set([ + 'title', + 'id', + 'type', + 'pos', + 'size', + 'flags', + 'order', + 'mode', + 'outputs', + 'inputs', + 'properties', + 'shape', + 'boxcolor', + 'color', + 'bgcolor', + 'showAdvanced', + 'widgets_values', + 'widgets_values_named' +]) + +export const GRAPH_CANONICAL_FIELDS = new Set([ + 'id', + 'revision', + 'config', + 'subgraphs', + 'definitions', + 'version', + 'state', + 'last_node_id', + 'last_link_id', + 'groups', + 'nodes', + 'links', + 'floatingLinks', + 'reroutes', + 'extra', + 'name', + 'category', + 'description', + 'inputNode', + 'outputNode', + 'inputs', + 'outputs', + 'widgets' +]) + +const isJsonValue = (value: unknown): value is JsonValue => { + if (value === null || typeof value === 'string' || typeof value === 'boolean') + return true + if (typeof value === 'number') return Number.isFinite(value) + if (Array.isArray(value)) return value.every(isJsonValue) + if (typeof value !== 'object') return false + + const prototype = Object.getPrototypeOf(value) + return ( + (prototype === Object.prototype || prototype === null) && + Object.values(value).every(isJsonValue) + ) +} + +const readPayload = (value: unknown): ExtensionPayload => { + if ( + !isJsonValue(value) || + value === null || + typeof value !== 'object' || + Array.isArray(value) + ) + return {} + return structuredClone(value) +} + +const cloneSerialisable = (value: T): T => + JSON.parse(JSON.stringify(value)) as T + +export const hydrateExtensionPayload = ( + owner: object, + data: object, + canonicalFields: ReadonlySet +): void => { + const record = Object.fromEntries(Object.entries(data)) + const payload = readPayload(record.extensions) + + for (const [key, value] of Object.entries(record)) { + if (canonicalFields.has(key) || key === 'extensions' || !isJsonValue(value)) + continue + payload[key] = structuredClone(value) + } + payloads.set(owner, payload) +} + +export const runExtensionSerializeHook = ( + owner: object, + canonical: T, + canonicalFields: ReadonlySet, + hook?: (data: T) => unknown +): T => { + const payload = payloads.get(owner) ?? {} + const view = cloneSerialisable(canonical) + Object.assign(view, { + extensions: structuredClone(payload) + }) + hook?.(view) + + for (const key of canonicalFields) { + if (Object.hasOwn(view, key)) { + Reflect.set(canonical, key, Reflect.get(view, key)) + } else { + Reflect.deleteProperty(canonical, key) + } + } + + const viewRecord = Object.fromEntries(Object.entries(view)) + const result = readPayload(viewRecord.extensions) + for (const [key, value] of Object.entries(viewRecord)) { + if (canonicalFields.has(key) || key === 'extensions' || !isJsonValue(value)) + continue + result[key] = structuredClone(value) + } + payloads.set(owner, result) + + return Object.keys(result).length + ? Object.assign(canonical, { extensions: structuredClone(result) }) + : canonical +} + +export const extensionConfigureView = ( + owner: object, + canonical: T +): T => + Object.assign( + cloneSerialisable(canonical), + structuredClone(payloads.get(owner)) + ) diff --git a/src/lib/litegraph/src/infrastructure/createMutationView.test.ts b/src/lib/litegraph/src/infrastructure/createMutationView.test.ts index 21f0691bf30..a855033f9f8 100644 --- a/src/lib/litegraph/src/infrastructure/createMutationView.test.ts +++ b/src/lib/litegraph/src/infrastructure/createMutationView.test.ts @@ -21,6 +21,19 @@ describe('createMutationView', () => { expect(commit).toHaveBeenCalledTimes(2) }) + it('commits record property assignments and deletions', () => { + const target: Record = { first: 1 } + const commit = vi.fn() + const view = createMutationView(target, { commit }) + + view.second = 2 + view.second = 2 + delete view.first + + expect(view).toEqual({ second: 2 }) + expect(commit).toHaveBeenCalledTimes(2) + }) + it('synchronizes before reads and preserves chained mutator identity', () => { const target = new Float64Array([1, 2]) const synchronize = vi.fn(() => target.set([3, 4])) diff --git a/src/lib/litegraph/src/infrastructure/createMutationView.ts b/src/lib/litegraph/src/infrastructure/createMutationView.ts index 19031be91bf..d3228c1b0f9 100644 --- a/src/lib/litegraph/src/infrastructure/createMutationView.ts +++ b/src/lib/litegraph/src/infrastructure/createMutationView.ts @@ -1,7 +1,7 @@ -interface MutationViewOptions { +interface MutationViewOptions { commit: () => void mapValue?: (property: PropertyKey, value: unknown) => unknown - observe?: ArrayLike + observe?: object shouldCommitMethod?: (property: PropertyKey) => boolean synchronize?: () => void } @@ -18,10 +18,14 @@ const arrayMutationMethods = new Set([ 'unshift' ]) -export function createMutationView< - TValue, - TTarget extends object & ArrayLike ->( +function snapshot(target: object): unknown[] { + return Reflect.ownKeys(target).flatMap((property) => [ + property, + Reflect.get(target, property) + ]) +} + +export function createMutationView( target: TTarget, { synchronize, @@ -29,12 +33,13 @@ export function createMutationView< observe = target, mapValue = (_property, value) => value, shouldCommitMethod = () => true - }: MutationViewOptions + }: MutationViewOptions ): TTarget { - function commitIfChanged(previous: TValue[]): void { + function commitIfChanged(previous: unknown[]): void { + const current = snapshot(observe) if ( - previous.some((value, index) => value !== observe[index]) || - previous.length !== observe.length + previous.some((value, index) => value !== current[index]) || + previous.length !== current.length ) commit() } @@ -49,7 +54,7 @@ export function createMutationView< return (...args: unknown[]) => { synchronize?.() - const previous = Array.from(observe) + const previous = snapshot(observe) try { const result = Reflect.apply(value, target, args) return result === target ? receiver : result @@ -60,7 +65,7 @@ export function createMutationView< }, set(target, property, value) { synchronize?.() - const previous = Array.from(observe) + const previous = snapshot(observe) try { return Reflect.set(target, property, value, target) } finally { @@ -69,7 +74,7 @@ export function createMutationView< }, deleteProperty(target, property) { synchronize?.() - const previous = Array.from(observe) + const previous = snapshot(observe) try { return Reflect.deleteProperty(target, property) } finally { @@ -78,7 +83,7 @@ export function createMutationView< }, defineProperty(target, property, attributes) { synchronize?.() - const previous = Array.from(observe) + const previous = snapshot(observe) try { return Reflect.defineProperty(target, property, attributes) } finally { diff --git a/src/lib/litegraph/src/litegraph.ts b/src/lib/litegraph/src/litegraph.ts index 9ed40651c2e..deb37e73144 100644 --- a/src/lib/litegraph/src/litegraph.ts +++ b/src/lib/litegraph/src/litegraph.ts @@ -108,6 +108,7 @@ export type { } from './interfaces' export { LGraph, + serialiseMutableGraphParts, type LGraphTriggerAction, type LGraphTriggerParam, type GraphAddOptions, diff --git a/src/lib/litegraph/src/node/NodeOutputSlot.ts b/src/lib/litegraph/src/node/NodeOutputSlot.ts index 47fd84e14fd..00cb51a6879 100644 --- a/src/lib/litegraph/src/node/NodeOutputSlot.ts +++ b/src/lib/litegraph/src/node/NodeOutputSlot.ts @@ -165,7 +165,13 @@ export class NodeOutputSlot extends NodeSlot implements INodeOutputSlot { * happens to carry the same key. */ function indexOf(slot: NodeOutputSlot): number { - return slot.node.outputs.indexOf(slot) + const index = slot.node.outputs.indexOf(slot) + return index !== -1 + ? index + : slot.node.outputs.findIndex( + (candidate) => + candidate.name === slot.name && candidate.type === slot.type + ) } function linkIdsOf(slot: NodeOutputSlot): LinkId[] { diff --git a/src/lib/litegraph/src/node/slotDescriptorView.ts b/src/lib/litegraph/src/node/slotDescriptorView.ts new file mode 100644 index 00000000000..1fa32cd1108 --- /dev/null +++ b/src/lib/litegraph/src/node/slotDescriptorView.ts @@ -0,0 +1,163 @@ +import { shallowReactive, toRaw } from 'vue' + +import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' +import type { + INodeInputSlot, + INodeOutputSlot, + INodeSlot +} from '@/lib/litegraph/src/interfaces' +import { NodeInputSlot } from '@/lib/litegraph/src/node/NodeInputSlot' +import { NodeOutputSlot } from '@/lib/litegraph/src/node/NodeOutputSlot' + +type SlotDescriptor = T + +export type InputSlotDescriptor = INodeInputSlot +export type OutputSlotDescriptor = INodeOutputSlot + +const slotProjection = Symbol('slotProjection') +const slotDescriptor = Symbol('slotDescriptor') +const sourceSlot = Symbol('sourceSlot') +const callbackArrayMethods = new Set([ + 'every', + 'filter', + 'find', + 'findIndex', + 'findLast', + 'findLastIndex', + 'flatMap', + 'forEach', + 'map', + 'reduce', + 'reduceRight', + 'some' +]) +const projectedArrayMethods = new Set([ + 'at', + 'entries', + 'flat', + 'includes', + 'keys', + 'slice', + 'toReversed', + 'toSorted', + 'toSpliced', + 'values' +]) + +function descriptorOf(slot: T): SlotDescriptor { + const raw = toRaw(slot) + const descriptor: T = { + ...raw, + boundingRect: raw.boundingRect ? [...raw.boundingRect] : [0, 0, 0, 0] + } + Reflect.deleteProperty(descriptor, '_node') + Object.defineProperty(descriptor, sourceSlot, { value: slot }) + return shallowReactive(descriptor) +} + +function project( + descriptor: SlotDescriptor, + create: (slot: T) => T +): T { + const projection = create(toRaw(descriptor)) + const view = new Proxy(projection, { + get(target, property, receiver) { + if ( + typeof property !== 'symbol' && + property !== 'boundingRect' && + !property.startsWith('_') && + property !== 'link' && + property !== 'links' && + Object.hasOwn(descriptor, property) + ) + return Reflect.get(descriptor, property) + return Reflect.get(target, property, receiver) + }, + set(target, property, value, receiver) { + if ( + typeof property !== 'symbol' && + property !== 'boundingRect' && + !property.startsWith('_') && + property !== 'link' && + property !== 'links' + ) + Reflect.set(descriptor, property, value) + return Reflect.set(target, property, value, receiver) + }, + deleteProperty(target, property) { + Reflect.deleteProperty(descriptor, property) + return Reflect.deleteProperty(target, property) + } + }) + Object.defineProperty(toRaw(projection), slotDescriptor, { + value: toRaw(descriptor) + }) + return view +} + +function slotView( + descriptors: SlotDescriptor[], + create: (slot: T) => T +): T[] { + return new Proxy(descriptors, { + get(target, property, receiver) { + if (property === Symbol.iterator) + return function* () { + for (let index = 0; index < target.length; index++) + yield Reflect.get(receiver, String(index)) + } + if (typeof property === 'string' && callbackArrayMethods.has(property)) + return Reflect.get(Array.prototype, property).bind(receiver) + if (typeof property === 'string' && projectedArrayMethods.has(property)) { + const projected = Array.from({ length: target.length }, (_, index) => + Reflect.get(receiver, String(index)) + ) + return Reflect.get(projected, property).bind(projected) + } + if (property === 'indexOf') + return (slot: T, fromIndex = 0) => { + const descriptor = Reflect.get(slot, slotDescriptor) + for (let index = fromIndex; index < target.length; index++) { + if ( + Reflect.get(receiver, String(index)) === slot || + toRaw(target[index]) === descriptor || + Reflect.get(toRaw(target[index]), sourceSlot) === slot + ) + return index + } + return -1 + } + const value = Reflect.get(target, property, receiver) + if (typeof property === 'symbol' || !/^\d+$/.test(property)) return value + if (!value) return value + const raw = toRaw(value) as T & { [slotProjection]?: T } + const existing = raw[slotProjection] + if (existing) return existing + const created = project(value, create) + Object.defineProperty(raw, slotProjection, { value: created }) + return created + }, + set(target, property, value, receiver) { + if (typeof property !== 'symbol' && /^\d+$/.test(property)) { + const descriptor = + Reflect.get(value, slotDescriptor) ?? descriptorOf(value as T) + return Reflect.set(target, property, descriptor, receiver) + } + return Reflect.set(target, property, value, receiver) + } + }) +} + +export function inputSlotView( + descriptors: InputSlotDescriptor[], + node: LGraphNode +): INodeInputSlot[] { + return slotView(descriptors, (slot) => new NodeInputSlot(slot, node)) +} + +export function outputSlotView( + descriptors: OutputSlotDescriptor[], + node: LGraphNode +): INodeOutputSlot[] { + return slotView(descriptors, (slot) => new NodeOutputSlot(slot, node)) +} diff --git a/src/lib/litegraph/src/node/slotEcosystemPatterns.test.ts b/src/lib/litegraph/src/node/slotEcosystemPatterns.test.ts index d4d19bd07aa..88b6d886974 100644 --- a/src/lib/litegraph/src/node/slotEcosystemPatterns.test.ts +++ b/src/lib/litegraph/src/node/slotEcosystemPatterns.test.ts @@ -63,6 +63,23 @@ describe('ecosystem slot patterns', () => { }) }) + it('preserves native callback array mutation semantics', () => { + const node = new LGraphNode('n') + node.addInput('first', 'INT') + node.addInput('second', 'INT') + const inputs = node.inputs + const visited: string[] = [] + + inputs.forEach((input, index, callbackInputs) => { + expect(callbackInputs).toBe(inputs) + if (index === 0) callbackInputs[1] = duckInputSlot() + visited.push(input.name) + }) + + expect(visited).toEqual(['first', 'in']) + expect(node.inputs[1]).toBe(inputs[1]) + }) + describe("input literals without a 'link' key", () => { it('does not report a free input as occupied because the same-index output is connected', () => { const { source, target } = createSourceAndTarget() diff --git a/src/lib/litegraph/src/subgraph/SubgraphNode.ts b/src/lib/litegraph/src/subgraph/SubgraphNode.ts index 6329a3e5833..3472fe74d7e 100644 --- a/src/lib/litegraph/src/subgraph/SubgraphNode.ts +++ b/src/lib/litegraph/src/subgraph/SubgraphNode.ts @@ -40,9 +40,13 @@ import { resolveConcretePromotedWidget } from '@/core/graph/subgraph/resolveConc import { resolveSubgraphInputTarget } from '@/core/graph/subgraph/resolveSubgraphInputTarget' import { parsePreviewExposures } from '@/core/schemas/previewExposureSchema' import { parseProxyWidgetErrorQuarantine } from '@/core/schemas/proxyWidgetQuarantineSchema' -import { usePreviewExposureStore } from '@/stores/previewExposureStore' +import { + getPreviewExposureHostLocator, + usePreviewExposureStore +} from '@/stores/previewExposureStore' import { useWidgetValueStore } from '@/stores/widgetValueStore' import { createNodeLocatorId } from '@/types/nodeIdentification' +import type { NodeState } from '@/types/nodeState' import type { WidgetId } from '@/types/widgetId' import { widgetId } from '@/types/widgetId' @@ -556,7 +560,7 @@ export class SubgraphNode extends LGraphNode implements BaseLGraph { private _hydratePreviewExposures() { const store = usePreviewExposureStore() const rootGraphId = this.rootGraph.id - const hostLocator = String(this.id) + const hostLocator = getPreviewExposureHostLocator(this) const rawProperty = this.properties.previewExposures const hasExplicitProperty = Array.isArray(rawProperty) const fromProperty = parsePreviewExposures(rawProperty) @@ -572,6 +576,9 @@ export class SubgraphNode extends LGraphNode implements BaseLGraph { const legacy = store.getExposures(rootGraphId, legacyKey) if (legacy.length) { store.setExposures(rootGraphId, hostLocator, [...legacy]) + if (legacyKey !== hostLocator) { + store.setExposures(rootGraphId, legacyKey, []) + } return } store.setExposures(rootGraphId, hostLocator, []) @@ -931,10 +938,14 @@ export class SubgraphNode extends LGraphNode implements BaseLGraph { } override serialize(): ISerialisedNode { - const serialized = super.serialize() + return this.serializeFromStoreState(this._state) + } + + override serializeFromStoreState(state: NodeState): ISerialisedNode { + const serialized = super.serializeFromStoreState(state) const serializedProperties = { ...(serialized.properties ?? {}) } const rootGraphId = this.rootGraph.id - const hostLocator = String(this.id) + const hostLocator = getPreviewExposureHostLocator(this) const previewExposures = usePreviewExposureStore().getExposures( rootGraphId, diff --git a/src/lib/litegraph/src/subgraph/SubgraphSerialization.test.ts b/src/lib/litegraph/src/subgraph/SubgraphSerialization.test.ts index 4f0110c10f8..fac55d6b579 100644 --- a/src/lib/litegraph/src/subgraph/SubgraphSerialization.test.ts +++ b/src/lib/litegraph/src/subgraph/SubgraphSerialization.test.ts @@ -19,9 +19,11 @@ import { LiteGraph, Subgraph } from '@/lib/litegraph/src/litegraph' +import { useGraphDefinitionStore } from '@/stores/graphDefinitionStore' import { toLinkId } from '@/types/linkId' import { toNodeId, UNASSIGNED_NODE_ID } from '@/types/nodeId' +import { createUuidv4 } from '@/utils/uuid' import { createTestSubgraph, createTestSubgraphNode, @@ -45,6 +47,35 @@ afterEach(() => { }) describe('SubgraphSerialization - Basic Serialization', () => { + it('projects registry and interface metadata from root-scoped records', () => { + const root = new LGraph() + const subgraph = createTestSubgraph({ rootGraph: root, name: 'Stored' }) + root.subgraphs.set(subgraph.id, subgraph) + + const store = useGraphDefinitionStore() + expect(root.subgraphs).toBe(store.subgraphs(root.id)) + expect(root.subgraphs.get(subgraph.id)).toBe(subgraph) + + const previousId = subgraph.id + subgraph.id = createUuidv4() + const definition = store.definition(root.id, subgraph.id) + const input = subgraph.addInput('input', 'number') + const output = subgraph.addOutput('output', 'number') + expect(subgraph.inputs).toBe(definition.inputs) + expect(subgraph.outputs).toBe(definition.outputs) + expect(definition).toMatchObject({ + name: 'Stored', + inputs: [input], + outputs: [output] + }) + + expect(() => { + subgraph.id = createUuidv4() + }).toThrow("Cannot change a populated graph's ID") + expect(root.subgraphs.get(subgraph.id)).toBe(subgraph) + expect(root.subgraphs.has(previousId)).toBe(false) + }) + it('should save and load simple subgraphs', () => { const original = createTestSubgraph({ name: 'Simple Test', diff --git a/src/lib/litegraph/src/subgraph/SubgraphWidgetPromotion.test.ts b/src/lib/litegraph/src/subgraph/SubgraphWidgetPromotion.test.ts index bda7b58e3a1..0cc58932361 100644 --- a/src/lib/litegraph/src/subgraph/SubgraphWidgetPromotion.test.ts +++ b/src/lib/litegraph/src/subgraph/SubgraphWidgetPromotion.test.ts @@ -21,7 +21,10 @@ import { import { reorderSubgraphInputsByName } from '@/core/graph/subgraph/promotionUtils' import type { SerializedProxyWidgetTuple } from '@/core/schemas/promotionSchema' import { IS_CONTROL_WIDGET } from '@/scripts/controlWidgetMarker' -import { usePreviewExposureStore } from '@/stores/previewExposureStore' +import { + getPreviewExposureHostLocator, + usePreviewExposureStore +} from '@/stores/previewExposureStore' import { useWidgetValueStore } from '@/stores/widgetValueStore' import { toNodeId } from '@/types/nodeId' import type { WidgetId } from '@/types/widgetId' @@ -1275,6 +1278,31 @@ describe('SubgraphWidgetPromotion', () => { ).toEqual([{ name: 'preview', ...exposure12 }]) }) + it('moves a nested host raw-ID entry to its owner-scoped locator', () => { + const outer = createTestSubgraph() + const hostNode = createTestSubgraphNode(createTestSubgraph(), { + id: 21 + }) + outer.add(hostNode) + const rootGraphId = hostNode.rootGraph.id + const rawLocator = createNodeLocatorId(null, hostNode.id) + const scopedLocator = getPreviewExposureHostLocator(hostNode) + const store = usePreviewExposureStore() + store.setExposures(rootGraphId, rawLocator, [ + { name: 'preview', ...exposure12 } + ]) + + hostNode._internalConfigureAfterSlots() + + expect(store.getExposures(rootGraphId, scopedLocator)).toEqual([ + { name: 'preview', ...exposure12 } + ]) + expect(store.getExposures(rootGraphId, rawLocator)).toEqual([]) + expect(hostNode.serialize().properties?.previewExposures).toEqual([ + { name: 'preview', ...serializedExposure12 } + ]) + }) + type SerializeCase = { name: string addExposures: (typeof exposure12)[] diff --git a/src/lib/litegraph/src/types/serialisation.ts b/src/lib/litegraph/src/types/serialisation.ts index ff78823c18a..96178a26dfe 100644 --- a/src/lib/litegraph/src/types/serialisation.ts +++ b/src/lib/litegraph/src/types/serialisation.ts @@ -2,7 +2,7 @@ import type { UUID } from '@/utils/uuid' import type { LGraphConfig, LGraphExtra, SubgraphId } from '../LGraph' import type { IGraphGroupFlags } from '../LGraphGroup' -import type { NodeProperty } from '../LGraphNode' +import type { NodeProperty } from '@/types/nodeState' import type { SerializedNodeId } from '@/types/nodeId' import type { SerialisedLLinkArray } from '../LLink' import type { FloatingRerouteSlot } from '../Reroute' @@ -45,6 +45,7 @@ interface BaseExportedGraph { /** The base definition of subgraphs used in this workflow. That is, what you see when you open / edit a subgraph. */ subgraphs?: ExportedSubgraph[] } + extensions?: ExtensionPayload } interface SerialisableGraphState { @@ -67,6 +68,8 @@ export interface SerialisableGraph extends BaseExportedGraph { extra?: LGraphExtra } +type ExtensionPayload = Record + export type ISerialisableNodeInput = Omit< INodeInputSlot, 'boundingRect' | 'widget' | 'link' @@ -108,6 +111,7 @@ export interface ISerialisedNode { */ widgets_values?: TWidgetValue[] widgets_values_named?: Record + extensions?: ExtensionPayload } /** Properties of nodes that are used by subgraph instances. */ diff --git a/src/platform/nodeReplacement/useNodeReplacement.test.ts b/src/platform/nodeReplacement/useNodeReplacement.test.ts index b4416deeb68..4cd78d35c01 100644 --- a/src/platform/nodeReplacement/useNodeReplacement.test.ts +++ b/src/platform/nodeReplacement/useNodeReplacement.test.ts @@ -10,10 +10,13 @@ import { import type { LGraph, LGraphNode } from '@/lib/litegraph/src/litegraph' import { LiteGraph } from '@/lib/litegraph/src/litegraph' import { useLinkStore } from '@/stores/linkStore' +import { usePreviewExposureStore } from '@/stores/previewExposureStore' +import { useWidgetValueStore } from '@/stores/widgetValueStore' import type { MissingNodeType } from '@/types/comfy' import { toOwningGraphId, toRootGraphId } from '@/types/graphScopeId' import { toLinkId } from '@/types/linkId' import { toNodeId } from '@/types/nodeId' +import { widgetId } from '@/types/widgetId' import type { UUID } from '@/utils/uuid' import type { NodeReplacement } from './types' @@ -247,6 +250,38 @@ describe('useNodeReplacement', () => { expect(newNode.has_errors).toBe(false) }) + it('clears stale node-owned records before binding the replacement', () => { + const placeholder = createPlaceholderNode(1, 'MissingNode') + const graph = createMockGraph([placeholder]) + placeholder.graph = graph + Object.assign(app, { rootGraph: graph }) + vi.mocked(collectAllNodes).mockReturnValue([placeholder]) + vi.mocked(LiteGraph.createNode).mockReturnValue(createNewNode()) + const id = widgetId(GRAPH_ID, toNodeId(1), 'stale') + useWidgetValueStore().registerWidget(id, { + type: 'number', + value: 1, + options: {} + }) + usePreviewExposureStore().addExposure(GRAPH_ID, '1', { + sourceNodeId: '2', + sourcePreviewName: 'preview' + }) + + useNodeReplacement().replaceNodesInPlace([ + makeMissingNodeType('MissingNode', { + new_node_id: 'Replacement', + old_node_id: 'MissingNode', + old_widget_ids: null, + input_mapping: null, + output_mapping: null + }) + ]) + + expect(useWidgetValueStore().getWidget(id)).toBeUndefined() + expect(usePreviewExposureStore().getExposures(GRAPH_ID, '1')).toEqual([]) + }) + it('should transfer input connections using input_mapping', () => { const link = createMockLink(10, 5, 0, 1, 0) const placeholder = createPlaceholderNode( @@ -532,6 +567,7 @@ describe('useNodeReplacement', () => { const graph = createMockGraph([placeholder], [link]) placeholder.graph = graph + placeholder.order = 6 Object.assign(app, { rootGraph: graph }) vi.mocked(collectAllNodes).mockReturnValue([placeholder]) @@ -558,6 +594,8 @@ describe('useNodeReplacement', () => { expect(newNode.id).toBe(42) expect(newNode.pos).toEqual([300, 400]) expect(newNode.size).toEqual([250, 150]) + expect(newNode.order).toBe(6) + expect(placeholder.order).toBe(6) expect(graph._nodes[0]).toBe(newNode) expect(placeholder.onRemoved).toHaveBeenCalledOnce() }) diff --git a/src/renderer/core/canvas/canvasStore.ts b/src/renderer/core/canvas/canvasStore.ts index 6a26dd56edb..80bc23f8956 100644 --- a/src/renderer/core/canvas/canvasStore.ts +++ b/src/renderer/core/canvas/canvasStore.ts @@ -178,10 +178,10 @@ export const useCanvasStore = defineStore('canvas', () => { 'litegraph:ghost-placement', (e: CustomEvent<{ active: boolean; nodeId: NodeId }>) => { isGhostPlacing.value = e.detail.active - const graphId = rootGraphId.value - if (e.detail.active && graphId) { + const graph = currentGraph.value + if (e.detail.active && graph) { const mutations = useLayoutMutations(LayoutSource.Canvas) - mutations.bringNodeToFront(graphId, e.detail.nodeId) + mutations.setNodeOrder(graph, e.detail.nodeId, 'front') } } ) diff --git a/src/renderer/core/canvas/litegraph/slotCalculations.ts b/src/renderer/core/canvas/litegraph/slotCalculations.ts index 7b29757c1d0..6eb2a5a106f 100644 --- a/src/renderer/core/canvas/litegraph/slotCalculations.ts +++ b/src/renderer/core/canvas/litegraph/slotCalculations.ts @@ -80,8 +80,13 @@ export function calculateInputSlotPosFromSlot( // Default vertical slots const offsetX = LiteGraph.NODE_SLOT_HEIGHT * 0.5 const nodeOffsetY = context.slotStartY || 0 - const defaultVerticalInputs = getDefaultVerticalInputs(context) - const slotIndex = defaultVerticalInputs.indexOf(input) + const inputIndex = context.inputs.indexOf(input) + const slotIndex = context.inputs + .slice(0, inputIndex) + .filter( + (slot) => + !slot.pos && !(context.widgets?.length && isWidgetInputSlot(slot)) + ).length const slotY = (slotIndex + 0.7) * LiteGraph.NODE_SLOT_HEIGHT return [nodeX + offsetX, nodeY + slotY + nodeOffsetY] @@ -195,17 +200,6 @@ export function getSlotPosition( : calculateOutputSlotPos(context, slotIndex) } -/** - * Get the inputs that are not positioned with absolute coordinates - */ -function getDefaultVerticalInputs( - context: SlotPositionContext -): INodeInputSlot[] { - return context.inputs.filter( - (slot) => !slot.pos && !(context.widgets?.length && isWidgetInputSlot(slot)) - ) -} - /** * Get the outputs that are not positioned with absolute coordinates */ diff --git a/src/renderer/core/layout/operations/graphLayoutAttachment.ts b/src/renderer/core/layout/operations/graphLayoutAttachment.ts index 1c10c9eb1db..e28f624a7ef 100644 --- a/src/renderer/core/layout/operations/graphLayoutAttachment.ts +++ b/src/renderer/core/layout/operations/graphLayoutAttachment.ts @@ -2,9 +2,16 @@ import type { LGraph } from '@/lib/litegraph/src/LGraph' import type { LGraphGroup } from '@/lib/litegraph/src/LGraphGroup' import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' import type { Reroute } from '@/lib/litegraph/src/Reroute' +import { createMutationView } from '@/lib/litegraph/src/infrastructure/createMutationView' +import { Rectangle } from '@/lib/litegraph/src/infrastructure/Rectangle' +import type { + Point as LegacyPoint, + Size as LegacySize +} from '@/lib/litegraph/src/interfaces' import { layoutStore } from '@/renderer/core/layout/store/layoutStore' import { LayoutSource } from '@/renderer/core/layout/types' import type { LayoutOperation, Point, Size } from '@/renderer/core/layout/types' +import { UNASSIGNED_NODE_ID } from '@/types/nodeId' import type { UUID } from '@/utils/uuid' type GraphLayoutOwner = Pick< @@ -22,6 +29,153 @@ const nodeAttachments = new WeakMap< LGraphNode, LayoutAttachment >() +interface NodeGeometryProjection { + buffer: Rectangle + geometryVersion: number + position: LegacyPoint + positionView: LegacyPoint + renderedSize: LegacySize + size: LegacySize + sizeView: LegacySize +} + +const nodeGeometryProjections = new WeakMap< + LGraphNode, + NodeGeometryProjection +>() +const storedRectScratch = new Float64Array(4) + +function nodeGeometryProjection(node: LGraphNode): NodeGeometryProjection { + const existing = nodeGeometryProjections.get(node) + if (existing) return existing + + const buffer = new Rectangle() + const position = buffer.pos + const size = buffer.size + const projection: NodeGeometryProjection = { + buffer, + geometryVersion: -1, + position, + positionView: createMutationView(position, { + commit: () => commitNodePosition(node), + synchronize: () => refreshNodeGeometry(node) + }), + renderedSize: [0, 0], + size, + sizeView: createMutationView(size, { + commit: () => commitNodeSize(node), + synchronize: () => refreshNodeGeometry(node) + }) + } + nodeGeometryProjections.set(node, projection) + return projection +} + +export function nodeGeometryBuffer(node: LGraphNode): Rectangle { + return nodeGeometryProjection(node).buffer +} + +export function nodePositionBuffer(node: LGraphNode): LegacyPoint { + return nodeGeometryProjection(node).position +} + +export function nodeSizeBuffer(node: LGraphNode): LegacySize { + return nodeGeometryProjection(node).size +} + +export function nodePositionView(node: LGraphNode): LegacyPoint { + return nodeGeometryProjection(node).positionView +} + +export function nodeSizeView(node: LGraphNode): LegacySize { + return nodeGeometryProjection(node).sizeView +} + +export function refreshNodeGeometry(node: LGraphNode): LegacySize { + const projection = nodeGeometryProjection(node) + const attachment = nodeAttachments.get(node) + if ( + attachment && + projection.geometryVersion !== layoutStore.geometryVersion + ) { + projection.geometryVersion = layoutStore.geometryVersion + layoutStore.readNodeRect( + attachment.graphId, + attachment.id, + projection.buffer + ) + } + + const rootGraphId = node.graph?.rootGraph.id + const contentSize = rootGraphId + ? layoutStore.contentSizeOf(rootGraphId, node.id) + : undefined + projection.renderedSize[0] = Math.max( + projection.size[0], + contentSize?.width ?? 0 + ) + projection.renderedSize[1] = Math.max( + projection.size[1], + contentSize?.height ?? 0 + ) + return projection.renderedSize +} + +export function setNodePosition(node: LGraphNode, value: LegacyPoint): void { + const position = nodeGeometryProjection(node).position + position[0] = value[0] + position[1] = value[1] + commitNodePosition(node) +} + +function commitNodePosition(node: LGraphNode): void { + if (node.id === UNASSIGNED_NODE_ID || !node.graph) return + const projection = nodeGeometryProjection(node) + const position = { x: projection.position[0], y: projection.position[1] } + if ( + layoutStore.readNodeRect( + node.graph.rootGraph.id, + node.id, + storedRectScratch + ) && + storedRectScratch[0] === position.x && + storedRectScratch[1] === position.y + ) + return + + moveNodeLayout(node, position) + projection.geometryVersion = -1 + refreshNodeGeometry(node) +} + +export function setNodeSize(node: LGraphNode, value: LegacySize): void { + const size = nodeGeometryProjection(node).size + size[0] = value[0] + size[1] = value[1] + commitNodeSize(node) +} + +function commitNodeSize(node: LGraphNode): void { + if (node.id === UNASSIGNED_NODE_ID || !node.graph) return + const projection = nodeGeometryProjection(node) + if ( + layoutStore.readNodeRect( + node.graph.rootGraph.id, + node.id, + storedRectScratch + ) && + storedRectScratch[2] === projection.size[0] && + storedRectScratch[3] === projection.size[1] + ) + return + + resizeNodeLayout(node, { + width: projection.size[0], + height: projection.size[1] + }) + projection.geometryVersion = -1 + refreshNodeGeometry(node) +} const groupAttachments = new WeakMap< LGraphGroup, LayoutAttachment @@ -69,15 +223,15 @@ export function attachNodeLayout(graph: LayoutGraph, node: LGraphNode): void { } function adoptNodeAttachment(graphId: UUID, node: LGraphNode): void { + const projection = nodeGeometryProjection(node) const geometrySynchronized = layoutStore.readNodeRect( graphId, node.id, - node._posSize + projection.buffer ) nodeAttachments.set(node, { graphId, id: node.id }) - node._layoutRegistered = true if (geometrySynchronized) { - node._geometryVersion = layoutStore.geometryVersion + projection.geometryVersion = layoutStore.geometryVersion } } @@ -113,15 +267,14 @@ export function transferLayoutAttachment( const geometrySynchronized = layoutStore.readNodeRect( attachment.graphId, attachment.id, - replacement._posSize + nodeGeometryProjection(replacement).buffer ) nodeAttachments.delete(node) - node._layoutRegistered = false nodeAttachments.set(replacement, attachment) - replacement._layoutRegistered = true if (geometrySynchronized) { - replacement._geometryVersion = layoutStore.geometryVersion + nodeGeometryProjection(replacement).geometryVersion = + layoutStore.geometryVersion } return true } @@ -131,9 +284,8 @@ export function detachNodeLayout(node: LGraphNode): void { if (!attachment) return const { graphId, id: nodeId } = attachment - layoutStore.readNodeRect(graphId, nodeId, node._posSize) + layoutStore.readNodeRect(graphId, nodeId, nodeGeometryProjection(node).buffer) nodeAttachments.delete(node) - node._layoutRegistered = false layoutStore.applyOperation({ ...canvasOperationMeta(), graphId, @@ -214,7 +366,7 @@ export function detachRerouteLayout(reroute: Reroute): void { }) } -export function moveNodeLayout(node: LGraphNode, position: Point): void { +function moveNodeLayout(node: LGraphNode, position: Point): void { const attachment = nodeAttachments.get(node) if (!attachment) return layoutStore.applyOperation({ @@ -307,9 +459,12 @@ export function detachGraphLayouts( for (const node of owner._nodes) { const attachment = nodeAttachments.get(node) if (!attachment) continue - layoutStore.readNodeRect(attachment.graphId, attachment.id, node._posSize) + layoutStore.readNodeRect( + attachment.graphId, + attachment.id, + nodeGeometryProjection(node).buffer + ) nodeAttachments.delete(node) - node._layoutRegistered = false if (meta) { operations.push({ ...meta, diff --git a/src/renderer/core/layout/operations/layoutMutations.test.ts b/src/renderer/core/layout/operations/layoutMutations.test.ts index b2932afc6fd..89d01b66c23 100644 --- a/src/renderer/core/layout/operations/layoutMutations.test.ts +++ b/src/renderer/core/layout/operations/layoutMutations.test.ts @@ -2,6 +2,7 @@ import { createTestingPinia } from '@pinia/testing' import { setActivePinia } from 'pinia' import { beforeEach, describe, expect, it, onTestFinished, vi } from 'vitest' +import { LGraph, LGraphNode } from '@/lib/litegraph/src/litegraph' import { layoutStore } from '@/renderer/core/layout/store/layoutStore' import { LayoutSource } from '@/renderer/core/layout/types' import type { NodeId } from '@/types/nodeId' @@ -188,3 +189,31 @@ describe('bringNodeToFront', () => { expect(z1).toBeGreaterThan(z2) }) }) + +describe('setNodeOrder', () => { + it.for([ + ['front', 1, 0], + ['back', 0, 1] + ] as const)( + 'projects %s order to layout and the legacy node array', + ([order, expectedFirstIndex, expectedSecondIndex]) => { + const graph = new LGraph() + const first = graph.add(new LGraphNode('first'))! + const second = graph.add(new LGraphNode('second'))! + + useLayoutMutations(LayoutSource.Canvas).setNodeOrder( + graph, + first.id, + order + ) + + expect(graph._nodes[expectedFirstIndex]).toBe(first) + expect(graph._nodes[expectedSecondIndex]).toBe(second) + const firstZ = layoutStore.getNodeLayout(graph.id, first.id)?.zIndex ?? 0 + const secondZ = + layoutStore.getNodeLayout(graph.id, second.id)?.zIndex ?? 0 + if (order === 'front') expect(firstZ).toBeGreaterThan(secondZ) + else expect(firstZ).toBeLessThan(secondZ) + } + ) +}) diff --git a/src/renderer/core/layout/operations/layoutMutations.ts b/src/renderer/core/layout/operations/layoutMutations.ts index 9411a66f4f1..14f4adc5e3f 100644 --- a/src/renderer/core/layout/operations/layoutMutations.ts +++ b/src/renderer/core/layout/operations/layoutMutations.ts @@ -1,5 +1,6 @@ import type { NodeId } from '@/types/nodeId' import type { UUID } from '@/utils/uuid' +import type { LGraph } from '@/lib/litegraph/src/litegraph' import { layoutStore } from '@/renderer/core/layout/store/layoutStore' import type { LayoutSource, Point } from '@/renderer/core/layout/types' @@ -73,10 +74,36 @@ export function useLayoutMutations(source: LayoutSource) { setNodeZIndex(rootGraphId, nodeId, layoutStore.allocateZIndex()) } + const setNodeOrder = ( + graph: LGraph, + nodeId: NodeId, + order: 'front' | 'back' + ): void => { + const index = graph._nodes.findIndex((node) => node.id === nodeId) + if (index === -1) return + + const rootGraphId = graph.rootGraph.id + const zIndex = + order === 'front' + ? layoutStore.allocateZIndex() + : Math.min( + ...graph._nodes.map( + (node) => + layoutStore.getNodeLayout(rootGraphId, node.id)?.zIndex ?? 0 + ) + ) - 1 + setNodeZIndex(rootGraphId, nodeId, zIndex) + + const [node] = graph._nodes.splice(index, 1) + if (order === 'front') graph._nodes.push(node) + else graph._nodes.unshift(node) + } + return { moveNode, batchMoveNodes, setNodeZIndex, - bringNodeToFront + bringNodeToFront, + setNodeOrder } } diff --git a/src/renderer/extensions/minimap/composables/useMinimap.test.ts b/src/renderer/extensions/minimap/composables/useMinimap.test.ts index 1588865c569..3589126d35c 100644 --- a/src/renderer/extensions/minimap/composables/useMinimap.test.ts +++ b/src/renderer/extensions/minimap/composables/useMinimap.test.ts @@ -298,9 +298,6 @@ describe('useMinimap', () => { setActivePinia(createTestingPinia({ stubActions: false })) registerMockLink(1, 'node2') - mockPause.mockClear() - mockResume.mockClear() - mockContext2D = createMockCanvas2DContext() moduleMockCanvasElement = createMockMinimapCanvas({ diff --git a/src/renderer/extensions/vueNodes/components/LGraphNode.subgraph.test.ts b/src/renderer/extensions/vueNodes/components/LGraphNode.subgraph.test.ts index 4530ef5a8d8..35da5f97c8d 100644 --- a/src/renderer/extensions/vueNodes/components/LGraphNode.subgraph.test.ts +++ b/src/renderer/extensions/vueNodes/components/LGraphNode.subgraph.test.ts @@ -89,7 +89,8 @@ describe('Vue Node - Subgraph Functionality', () => { mode: 0, flags: {}, inputs: [], - outputs: [] + outputs: [], + properties: {} }) const renderComponent = (props: { nodeData: NodeState }) => { diff --git a/src/renderer/extensions/vueNodes/components/LGraphNode.test.ts b/src/renderer/extensions/vueNodes/components/LGraphNode.test.ts index f7a3cb25483..c76552f1fc5 100644 --- a/src/renderer/extensions/vueNodes/components/LGraphNode.test.ts +++ b/src/renderer/extensions/vueNodes/components/LGraphNode.test.ts @@ -167,7 +167,8 @@ const mockNodeData: NodeState = { mode: 0, flags: {}, inputs: [], - outputs: [] + outputs: [], + properties: {} } const mockRerouteNodeData: NodeState = { diff --git a/src/renderer/extensions/vueNodes/components/LGraphNodePreview.vue b/src/renderer/extensions/vueNodes/components/LGraphNodePreview.vue index 02378d35731..38709d93c94 100644 --- a/src/renderer/extensions/vueNodes/components/LGraphNodePreview.vue +++ b/src/renderer/extensions/vueNodes/components/LGraphNodePreview.vue @@ -104,7 +104,8 @@ const nodeData = computed(() => ({ collapsed: false }, inputs: previewInputs.value, - outputs: previewOutputs.value + outputs: previewOutputs.value, + properties: {} })) const previewWidgets = computed(() => diff --git a/src/renderer/extensions/vueNodes/components/NodeHeader.test.ts b/src/renderer/extensions/vueNodes/components/NodeHeader.test.ts index 0da3fad6ba6..e153a75e7d7 100644 --- a/src/renderer/extensions/vueNodes/components/NodeHeader.test.ts +++ b/src/renderer/extensions/vueNodes/components/NodeHeader.test.ts @@ -28,7 +28,8 @@ const makeNodeData = (overrides: Partial = {}): NodeState => ({ type: 'KSampler', mode: 0, flags: { collapsed: false }, - ...overrides + ...overrides, + properties: overrides.properties ?? {} }) const setupMockStores = () => { diff --git a/src/renderer/extensions/vueNodes/components/NodeSlots.test.ts b/src/renderer/extensions/vueNodes/components/NodeSlots.test.ts index 426c2b6bb92..274edf598d3 100644 --- a/src/renderer/extensions/vueNodes/components/NodeSlots.test.ts +++ b/src/renderer/extensions/vueNodes/components/NodeSlots.test.ts @@ -44,7 +44,8 @@ const makeNodeData = (overrides: Partial = {}): NodeState => ({ inputs: [], outputs: [], flags: { collapsed: false }, - ...overrides + ...overrides, + properties: overrides.properties ?? {} }) // Explicit stubs to capture props for assertions diff --git a/src/renderer/extensions/vueNodes/components/NodeWidgets.test.ts b/src/renderer/extensions/vueNodes/components/NodeWidgets.test.ts index ea582ec3911..4080bdf9d3d 100644 --- a/src/renderer/extensions/vueNodes/components/NodeWidgets.test.ts +++ b/src/renderer/extensions/vueNodes/components/NodeWidgets.test.ts @@ -59,7 +59,8 @@ function createMockNodeData( mode: 0, flags: {}, inputs: [], - outputs: [] + outputs: [], + properties: {} } } diff --git a/src/renderer/extensions/vueNodes/composables/useNodeEventHandlers.test.ts b/src/renderer/extensions/vueNodes/composables/useNodeEventHandlers.test.ts index a6f150fc123..2a30cd0eb07 100644 --- a/src/renderer/extensions/vueNodes/composables/useNodeEventHandlers.test.ts +++ b/src/renderer/extensions/vueNodes/composables/useNodeEventHandlers.test.ts @@ -50,10 +50,10 @@ vi.mock('@/renderer/core/canvas/useCanvasInteractions', () => ({ })) vi.mock('@/renderer/core/layout/operations/layoutMutations', () => { - const bringNodeToFront = vi.fn() + const setNodeOrder = vi.fn() return { useLayoutMutations: vi.fn(() => ({ - bringNodeToFront + setNodeOrder })) } }) @@ -101,9 +101,10 @@ describe('useNodeEventHandlers', () => { handleNodeSelect(ctrlClickEvent, testNodeId) // On pointer down with multi-select: bring to front - expect(mockLayoutMutations.bringNodeToFront).toHaveBeenCalledWith( - ROOT_GRAPH_ID, - 'node-1' + expect(mockLayoutMutations.setNodeOrder).toHaveBeenCalledWith( + useCanvasStore().currentGraph, + 'node-1', + 'front' ) // Selection happens immediately so dragging includes this node @@ -128,9 +129,10 @@ describe('useNodeEventHandlers', () => { handleNodeSelect(ctrlClickEvent, testNodeId) // On pointer down: bring to front - expect(mockLayoutMutations.bringNodeToFront).toHaveBeenCalledWith( - ROOT_GRAPH_ID, - 'node-1' + expect(mockLayoutMutations.setNodeOrder).toHaveBeenCalledWith( + useCanvasStore().currentGraph, + 'node-1', + 'front' ) // But don't deselect yet (deferred to pointer up) @@ -154,9 +156,10 @@ describe('useNodeEventHandlers', () => { handleNodeSelect(metaClickEvent, testNodeId) // On pointer down with meta key: bring to front - expect(mockLayoutMutations.bringNodeToFront).toHaveBeenCalledWith( - ROOT_GRAPH_ID, - 'node-1' + expect(mockLayoutMutations.setNodeOrder).toHaveBeenCalledWith( + useCanvasStore().currentGraph, + 'node-1', + 'front' ) // Selection happens immediately @@ -180,9 +183,10 @@ describe('useNodeEventHandlers', () => { handleNodeSelect(shiftClickEvent, testNodeId) // On pointer down with shift: bring to front - expect(mockLayoutMutations.bringNodeToFront).toHaveBeenCalledWith( - ROOT_GRAPH_ID, - 'node-1' + expect(mockLayoutMutations.setNodeOrder).toHaveBeenCalledWith( + useCanvasStore().currentGraph, + 'node-1', + 'front' ) // Selection happens immediately for shift-click as well @@ -218,9 +222,10 @@ describe('useNodeEventHandlers', () => { const event = new PointerEvent('pointerdown') handleNodeSelect(event, testNodeId) - expect(mockLayoutMutations.bringNodeToFront).toHaveBeenCalledWith( - ROOT_GRAPH_ID, - 'node-1' + expect(mockLayoutMutations.setNodeOrder).toHaveBeenCalledWith( + useCanvasStore().currentGraph, + 'node-1', + 'front' ) }) @@ -232,7 +237,7 @@ describe('useNodeEventHandlers', () => { const event = new PointerEvent('pointerdown') handleNodeSelect(event, testNodeId) - expect(mockLayoutMutations.bringNodeToFront).not.toHaveBeenCalled() + expect(mockLayoutMutations.setNodeOrder).not.toHaveBeenCalled() }) }) diff --git a/src/renderer/extensions/vueNodes/composables/useNodeZIndex.test.ts b/src/renderer/extensions/vueNodes/composables/useNodeZIndex.test.ts index 46d54a4bc0d..bc43f0bb434 100644 --- a/src/renderer/extensions/vueNodes/composables/useNodeZIndex.test.ts +++ b/src/renderer/extensions/vueNodes/composables/useNodeZIndex.test.ts @@ -12,20 +12,20 @@ vi.mock('@/renderer/core/layout/operations/layoutMutations', () => ({ useLayoutMutations: vi.fn() })) -const ROOT_GRAPH_ID = createUuidv4() +const CURRENT_GRAPH = fromPartial({ id: createUuidv4() }) vi.mock('@/renderer/core/canvas/canvasStore', () => ({ - useCanvasStore: () => ({ rootGraphId: ROOT_GRAPH_ID }) + useCanvasStore: () => ({ currentGraph: CURRENT_GRAPH }) })) const mockedUseLayoutMutations = vi.mocked(useLayoutMutations) describe('useNodeZIndex', () => { it('scopes the mutation to the viewed root graph, attributed to Vue', () => { - const mockBringNodeToFront = vi.fn() + const mockSetNodeOrder = vi.fn() mockedUseLayoutMutations.mockReturnValue( fromPartial({ - bringNodeToFront: mockBringNodeToFront + setNodeOrder: mockSetNodeOrder }) ) @@ -34,6 +34,10 @@ describe('useNodeZIndex', () => { bringNodeToFront(toNodeId('node1')) expect(mockedUseLayoutMutations).toHaveBeenCalledWith(LayoutSource.Vue) - expect(mockBringNodeToFront).toHaveBeenCalledWith(ROOT_GRAPH_ID, 'node1') + expect(mockSetNodeOrder).toHaveBeenCalledWith( + CURRENT_GRAPH, + 'node1', + 'front' + ) }) }) diff --git a/src/renderer/extensions/vueNodes/composables/useNodeZIndex.ts b/src/renderer/extensions/vueNodes/composables/useNodeZIndex.ts index 55613af9b3d..4097707c1b1 100644 --- a/src/renderer/extensions/vueNodes/composables/useNodeZIndex.ts +++ b/src/renderer/extensions/vueNodes/composables/useNodeZIndex.ts @@ -14,10 +14,10 @@ export function useNodeZIndex() { const canvasStore = useCanvasStore() function bringNodeToFront(nodeId: NodeId) { - const { rootGraphId } = canvasStore - if (!rootGraphId) return + const { currentGraph } = canvasStore + if (!currentGraph) return - layoutMutations.bringNodeToFront(rootGraphId, nodeId) + layoutMutations.setNodeOrder(currentGraph, nodeId, 'front') } return { diff --git a/src/renderer/extensions/vueNodes/composables/useProcessedWidgets.test.ts b/src/renderer/extensions/vueNodes/composables/useProcessedWidgets.test.ts index 5a5f3787fe6..2f870f9bcb7 100644 --- a/src/renderer/extensions/vueNodes/composables/useProcessedWidgets.test.ts +++ b/src/renderer/extensions/vueNodes/composables/useProcessedWidgets.test.ts @@ -133,7 +133,8 @@ function processWidgets({ mode: 0, flags: {}, inputs: [], - outputs: [] + outputs: [], + properties: {} }, widgetIds, graphId: GRAPH_ID, diff --git a/src/scripts/app.test.ts b/src/scripts/app.test.ts index 9bc3442eae2..1a18a5a83b3 100644 --- a/src/scripts/app.test.ts +++ b/src/scripts/app.test.ts @@ -87,6 +87,7 @@ const { }, mockNodeOutputStore: { refreshNodeOutputs: vi.fn(), + replaceOutputsFromLegacy: vi.fn(), resetAllOutputsAndPreviews: vi.fn(), stashPreviewsForWorkflow: vi.fn(), restorePreviewsForWorkflow: vi.fn(), @@ -307,6 +308,50 @@ describe('ComfyApp', () => { ) }) + describe('nodeOutputs', () => { + it('commits legacy property mutations to the output store', () => { + app.vueAppReady = true + const output = { images: [{ filename: 'legacy.png' }] } + + app.nodeOutputs['1'] = output + expect( + mockNodeOutputStore.replaceOutputsFromLegacy + ).toHaveBeenNthCalledWith(1, { '1': output }) + + delete app.nodeOutputs['1'] + expect( + mockNodeOutputStore.replaceOutputsFromLegacy + ).toHaveBeenNthCalledWith(2, {}) + }) + + it('commits nested legacy output mutations to the output store', () => { + app.vueAppReady = true + app.nodeOutputs['1'] = { images: [{ filename: 'first.png' }] } + mockNodeOutputStore.replaceOutputsFromLegacy.mockClear() + + const output = app.nodeOutputs['1'] + output.images = [{ filename: 'second.png' }] + expect(mockNodeOutputStore.replaceOutputsFromLegacy).toHaveBeenCalledWith( + { + '1': { images: [{ filename: 'second.png' }] } + } + ) + + mockNodeOutputStore.replaceOutputsFromLegacy.mockClear() + const images = output.images + images?.push({ filename: 'third.png' }) + expect(mockNodeOutputStore.replaceOutputsFromLegacy).toHaveBeenCalledWith( + { + '1': { + images: [{ filename: 'second.png' }, { filename: 'third.png' }] + } + } + ) + expect(app.nodeOutputs['1']).toBe(output) + expect(output.images).toBe(images) + }) + }) + describe('queuePrompt', () => { function prepareEmptyPromptQueue() { const workflow = new ComfyWorkflow({ diff --git a/src/scripts/app.ts b/src/scripts/app.ts index 4ac6338abeb..6eefe91a056 100644 --- a/src/scripts/app.ts +++ b/src/scripts/app.ts @@ -14,6 +14,10 @@ import { setBackendNodeText, st, t } from '@/i18n' import { normalizeI18nKey } from '@/utils/formatUtil' import { ChangeTracker } from '@/scripts/changeTracker' import type { IContextMenuValue } from '@/lib/litegraph/src/interfaces' +import { + createArrayMutationView, + createMutationView +} from '@/lib/litegraph/src/infrastructure/createMutationView' import { inputAsSerialisable, LGraph, @@ -247,6 +251,32 @@ export interface QueuePromptOptions { intent?: WorkflowQueueIntent } +function createNodeOutputsMutationView( + outputs: Record, + commit: () => void +): Record { + const outputViews = new WeakMap() + const arrayViews = new WeakMap() + const mapArray = (_property: PropertyKey, value: unknown): unknown => { + if (!Array.isArray(value)) return value + const existing = arrayViews.get(value) + if (existing) return existing + const view = createArrayMutationView(value, commit) + arrayViews.set(value, view) + return view + } + const mapOutput = (_property: PropertyKey, value: unknown): unknown => { + if (value === null || typeof value !== 'object' || Array.isArray(value)) + return value + const existing = outputViews.get(value) + if (existing) return existing + const view = createMutationView(value, { commit, mapValue: mapArray }) + outputViews.set(value, view) + return view + } + return createMutationView(outputs, { commit, mapValue: mapOutput }) +} + export class ComfyApp { /** * List of entries to queue @@ -278,7 +308,14 @@ export class ComfyApp { api: ComfyApi ui: ComfyUI extensionManager!: ExtensionManager - private _nodeOutputs!: Record + private readonly nodeOutputsData: Record = {} + private readonly _nodeOutputs = createNodeOutputsMutationView( + this.nodeOutputsData, + () => { + if (this.vueAppReady) + useNodeOutputStore().replaceOutputsFromLegacy(this.nodeOutputsData) + } + ) nodePreviewImages: Record private rootGraphInternal: LGraph | undefined @@ -443,9 +480,15 @@ export class ComfyApp { } set nodeOutputs(value) { - this._nodeOutputs = value - if (this.vueAppReady) + if (value !== this._nodeOutputs) { + for (const key of Object.keys(this.nodeOutputsData)) + delete this.nodeOutputsData[key] + Object.assign(this.nodeOutputsData, value) + } + if (this.vueAppReady) { + useNodeOutputStore().replaceOutputsFromLegacy(this.nodeOutputsData) useExtensionService().invokeExtensions('onNodeOutputsUpdated', value) + } } /** @@ -507,8 +550,7 @@ export class ComfyApp { const combinedIndex = imgs ? imgs.length + 2 : 2 // for vueNodes mode - const images = - node.images ?? useNodeOutputStore().getNodeOutputs(node)?.images + const images = useNodeOutputStore().getNodeOutputs(node)?.images ComfyApp.clipspace = { widgets: widgets, @@ -543,16 +585,11 @@ export class ComfyApp { if (ComfyApp.clipspace.imgs && node.imgs) { // Update node.images even if it's initially undefined (vueNodes mode) if (ComfyApp.clipspace.images) { - if (ComfyApp.clipspace['img_paste_mode'] == 'selected') { - node.images = [ - ComfyApp.clipspace.images[ComfyApp.clipspace['selectedIndex']] - ] - } else { - node.images = ComfyApp.clipspace.images - } - - if (app.nodeOutputs[node.id + '']) - app.nodeOutputs[node.id + ''].images = node.images + const images = + ComfyApp.clipspace['img_paste_mode'] == 'selected' + ? [ComfyApp.clipspace.images[ComfyApp.clipspace['selectedIndex']]] + : ComfyApp.clipspace.images + useNodeOutputStore().setNodeOutputImages(node, images) } if (ComfyApp.clipspace.imgs) { @@ -643,8 +680,6 @@ export class ComfyApp { } app.canvas.setDirty(true) - - useNodeOutputStore().updateNodeImages(node) } } diff --git a/src/scripts/ui.ts b/src/scripts/ui.ts index 36559935138..0d22543cfdc 100644 --- a/src/scripts/ui.ts +++ b/src/scripts/ui.ts @@ -8,6 +8,7 @@ import { WORKFLOW_ACCEPT_STRING } from '@/platform/workflow/core/types/formats' import { type StatusWsMessageStatus } from '@/schemas/apiSchema' import { useLitegraphService } from '@/services/litegraphService' import { useCommandStore } from '@/stores/commandStore' +import { useNodeOutputStore } from '@/stores/nodeOutputStore' import { useWorkspaceStore } from '@/stores/workspaceStore' import { api } from './api' @@ -308,10 +309,7 @@ class ComfyList { const workflow = await extractWorkflow(job) await app.loadGraphData(workflow, true, false) if ('outputs' in job && job.outputs) { - app.nodeOutputs = {} - for (const [key, value] of Object.entries(job.outputs)) { - app.nodeOutputs[key] = value - } + useNodeOutputStore().restoreOutputs(job.outputs) } } }), diff --git a/src/services/litegraphService.ts b/src/services/litegraphService.ts index ec387446c2c..db8ba1c0535 100644 --- a/src/services/litegraphService.ts +++ b/src/services/litegraphService.ts @@ -59,7 +59,10 @@ import { $el } from '@/scripts/ui' import { useExecutionStore } from '@/stores/executionStore' import { useNodeOutputStore } from '@/stores/nodeOutputStore' import { ComfyNodeDefImpl } from '@/stores/nodeDefStore' -import { usePreviewExposureStore } from '@/stores/previewExposureStore' +import { + getPreviewExposureHostLocator, + usePreviewExposureStore +} from '@/stores/previewExposureStore' import { useSubgraphStore } from '@/stores/subgraphStore' import { useFavoritedWidgetsStore } from '@/stores/workspace/favoritedWidgetsStore' import { useRightSidePanelStore } from '@/stores/workspace/rightSidePanelStore' @@ -194,7 +197,7 @@ export const useLitegraphService = () => { } function getPseudoWidgetPreviewTargets(node: SubgraphNode): LGraphNode[] { - const hostLocator = String(node.id) + const hostLocator = getPreviewExposureHostLocator(node) const promotions = usePreviewExposureStore().getExposuresAsPromotionShape( node.rootGraph.id, hostLocator diff --git a/src/stores/appModeStore.test.ts b/src/stores/appModeStore.test.ts index a85d940970d..54f23be967e 100644 --- a/src/stores/appModeStore.test.ts +++ b/src/stores/appModeStore.test.ts @@ -1112,7 +1112,7 @@ describe('appModeStore', () => { const hostState = createNodeState({ id: toNodeId(hostId) }) const hostNode = Object.assign(Object.create(SubgraphNode.prototype), { _state: hostState, - inputs: [{ name: 'Prompt', _widget: hostWidget }], + _inputs: [{ name: 'Prompt', _widget: hostWidget }], widgets: [hostWidget], isSubgraphNode: () => true }) as SubgraphNode diff --git a/src/stores/clearNodeOwnedStoreState.ts b/src/stores/clearNodeOwnedStoreState.ts new file mode 100644 index 00000000000..241ed46d970 --- /dev/null +++ b/src/stores/clearNodeOwnedStoreState.ts @@ -0,0 +1,22 @@ +import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' +import { createNodeLocatorId } from '@/types/nodeIdentification' +import { parseNodeId } from '@/types/nodeId' + +import { usePreviewExposureStore } from './previewExposureStore' +import { useWidgetValueStore } from './widgetValueStore' + +export function clearNodeOwnedStoreState(node: LGraphNode): void { + const graph = node.graph + if (!graph) return + + const rootGraphId = graph.isRootGraph ? graph.id : graph.rootGraph.id + const nodeId = parseNodeId(node.id) ?? node.id + useWidgetValueStore().clearNode(rootGraphId, nodeId) + usePreviewExposureStore().clearHost( + rootGraphId, + createNodeLocatorId( + graph.isRootGraph || graph.id === rootGraphId ? null : graph.id, + nodeId + ) + ) +} diff --git a/src/stores/entityIdStore.test.ts b/src/stores/entityIdStore.test.ts new file mode 100644 index 00000000000..cf25bfe81d1 --- /dev/null +++ b/src/stores/entityIdStore.test.ts @@ -0,0 +1,33 @@ +import { describe, expect, it } from 'vitest' + +import { createLGraphState, mintNodeId } from '@/lib/litegraph/src/idAllocation' +import type { UUID } from '@/utils/uuid' + +import { useEntityIdStore } from './entityIdStore' + +describe(useEntityIdStore, () => { + const first = '00000000-0000-4000-8000-000000000001' as UUID + const second = '00000000-0000-4000-8000-000000000002' as UUID + + it('keeps allocation state when a root graph is rekeyed', () => { + const store = useEntityIdStore() + const state = store.get(first) + mintNodeId(state) + + store.rekey(first, second) + + expect(store.get(second)).toBe(state) + expect(Number(mintNodeId(store.get(second)))).toBe(2) + }) + + it('replaces compatibility state without sharing the caller object', () => { + const store = useEntityIdStore() + const state = createLGraphState() + state.lastNodeId = 2 + + store.set(first, state) + state.lastNodeId = 99 + + expect(store.get(first).lastNodeId).toBe(2) + }) +}) diff --git a/src/stores/entityIdStore.ts b/src/stores/entityIdStore.ts new file mode 100644 index 00000000000..0ccce0289f3 --- /dev/null +++ b/src/stores/entityIdStore.ts @@ -0,0 +1,37 @@ +import { defineStore } from 'pinia' +import { shallowRef } from 'vue' + +import { createLGraphState } from '@/lib/litegraph/src/idAllocation' +import type { LGraphState } from '@/lib/litegraph/src/idAllocation' +import type { UUID } from '@/utils/uuid' + +export const useEntityIdStore = defineStore('entityId', () => { + const states = shallowRef(new Map()) + + function get(rootGraphId: UUID): LGraphState { + const existing = states.value.get(rootGraphId) + if (existing) return existing + + const created = createLGraphState() + states.value.set(rootGraphId, created) + return created + } + + function set(rootGraphId: UUID, state: LGraphState): void { + states.value.set(rootGraphId, { ...state }) + } + + function rekey(previousId: UUID, nextId: UUID): void { + if (previousId === nextId) return + const existing = states.value.get(previousId) + if (!existing) return + states.value.delete(previousId) + states.value.set(nextId, existing) + } + + function clear(rootGraphId: UUID): void { + states.value.delete(rootGraphId) + } + + return { get, set, rekey, clear } +}) diff --git a/src/stores/executionOrderStore.ts b/src/stores/executionOrderStore.ts new file mode 100644 index 00000000000..96d1d170d6e --- /dev/null +++ b/src/stores/executionOrderStore.ts @@ -0,0 +1,50 @@ +import { defineStore } from 'pinia' + +import type { GraphScope } from '@/types/graphScopeId' +import type { NodeId } from '@/types/nodeId' + +export const useExecutionOrderStore = defineStore('executionOrder', () => { + const orders = new Map>>() + + function graphOrders(scope: GraphScope): Map { + let root = orders.get(scope.rootGraphId) + if (!root) { + root = new Map() + orders.set(scope.rootGraphId, root) + } + let graph = root.get(scope.owningGraphId) + if (!graph) { + graph = new Map() + root.set(scope.owningGraphId, graph) + } + return graph + } + + function get(scope: GraphScope, nodeId: NodeId): number | undefined { + return orders.get(scope.rootGraphId)?.get(scope.owningGraphId)?.get(nodeId) + } + + function set(scope: GraphScope, nodeId: NodeId, order: number): void { + graphOrders(scope).set(nodeId, order) + } + + function replace(scope: GraphScope, nodeIds: readonly NodeId[]): void { + const graph = graphOrders(scope) + graph.clear() + nodeIds.forEach((nodeId, order) => graph.set(nodeId, order)) + } + + function remove(scope: GraphScope, nodeId: NodeId): void { + orders.get(scope.rootGraphId)?.get(scope.owningGraphId)?.delete(nodeId) + } + + function clearGraph(scope: GraphScope): void { + orders.get(scope.rootGraphId)?.delete(scope.owningGraphId) + } + + function clearRoot(rootGraphId: string): void { + orders.delete(rootGraphId) + } + + return { clearGraph, clearRoot, get, remove, replace, set } +}) diff --git a/src/stores/graphDefinitionStore.ts b/src/stores/graphDefinitionStore.ts new file mode 100644 index 00000000000..f660ef96ba2 --- /dev/null +++ b/src/stores/graphDefinitionStore.ts @@ -0,0 +1,171 @@ +import { defineStore } from 'pinia' + +import type { + IGraphGroupFlags, + LGraphGroup +} from '@/lib/litegraph/src/LGraphGroup' +import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' +import type { Subgraph, SubgraphId } from '@/lib/litegraph/src/LGraph' +import type { SubgraphInput } from '@/lib/litegraph/src/subgraph/SubgraphInput' +import type { SubgraphNode } from '@/lib/litegraph/src/subgraph/SubgraphNode' +import type { SubgraphOutput } from '@/lib/litegraph/src/subgraph/SubgraphOutput' +import type { ExposedWidget } from '@/lib/litegraph/src/types/serialisation' +import type { GroupId } from '@/types/groupId' +import type { UUID } from '@/utils/uuid' + +export interface GroupPresentation { + title: string + color?: string + font?: string + font_size: number + flags: IGraphGroupFlags +} + +interface GraphMembership { + nodes: (LGraphNode | SubgraphNode)[] + groups: LGraphGroup[] + groupPresentation: Map +} + +interface SubgraphDefinition { + name: string + description?: string + inputs: SubgraphInput[] + outputs: SubgraphOutput[] + widgets: ExposedWidget[] +} + +interface RootDefinitions { + membershipByGraph: Map + subgraphs: Map + definitions: Map +} + +export const useGraphDefinitionStore = defineStore('graphDefinition', () => { + const roots = new Map() + + function root(rootGraphId: UUID): RootDefinitions { + const existing = roots.get(rootGraphId) + if (existing) return existing + const created: RootDefinitions = { + membershipByGraph: new Map(), + subgraphs: new Map(), + definitions: new Map() + } + roots.set(rootGraphId, created) + return created + } + + function membership(rootGraphId: UUID, graphId: UUID): GraphMembership { + const definitions = root(rootGraphId) + const existing = definitions.membershipByGraph.get(graphId) + if (existing) return existing + const created: GraphMembership = { + nodes: [], + groups: [], + groupPresentation: new Map() + } + definitions.membershipByGraph.set(graphId, created) + return created + } + + function subgraphs(rootGraphId: UUID) { + return root(rootGraphId).subgraphs + } + + function registerGroupPresentation( + rootGraphId: UUID, + graphId: UUID, + groupId: GroupId, + presentation: GroupPresentation + ): GroupPresentation { + const presentations = membership(rootGraphId, graphId).groupPresentation + presentations.set(groupId, presentation) + return presentations.get(groupId)! + } + + function deleteGroupPresentation( + rootGraphId: UUID, + graphId: UUID, + groupId: GroupId + ): void { + membership(rootGraphId, graphId).groupPresentation.delete(groupId) + } + + function definition( + rootGraphId: UUID, + subgraphId: SubgraphId + ): SubgraphDefinition { + const definitions = root(rootGraphId) + const existing = definitions.definitions.get(subgraphId) + if (existing) return existing + const created: SubgraphDefinition = { + name: 'Unnamed Subgraph', + inputs: [], + outputs: [], + widgets: [] + } + definitions.definitions.set(subgraphId, created) + return created + } + + function rekeyRoot(previousId: UUID, nextId: UUID): void { + if (previousId === nextId) return + const existing = roots.get(previousId) + if (!existing) return + roots.delete(previousId) + const rootMembership = existing.membershipByGraph.get(previousId) + if (rootMembership) { + existing.membershipByGraph.delete(previousId) + existing.membershipByGraph.set(nextId, rootMembership) + } + roots.set(nextId, existing) + } + + function rekeyGraph( + rootGraphId: UUID, + previousId: SubgraphId, + nextId: SubgraphId + ): void { + if (previousId === nextId) return + const definitions = roots.get(rootGraphId) + if (!definitions) return + const subgraph = definitions.subgraphs.get(previousId) + if (subgraph) { + definitions.subgraphs.delete(previousId) + definitions.subgraphs.set(nextId, subgraph) + } + const membership = definitions.membershipByGraph.get(previousId) + if (membership) { + definitions.membershipByGraph.delete(previousId) + definitions.membershipByGraph.set(nextId, membership) + } + const definition = definitions.definitions.get(previousId) + if (definition) { + definitions.definitions.delete(previousId) + definitions.definitions.set(nextId, definition) + } + } + + function clearRoot(rootGraphId: UUID): void { + roots.delete(rootGraphId) + } + + function clearGraph(rootGraphId: UUID, graphId: UUID): void { + const definitions = roots.get(rootGraphId) + definitions?.membershipByGraph.delete(graphId) + definitions?.definitions.delete(graphId) + } + + return { + clearGraph, + clearRoot, + deleteGroupPresentation, + definition, + membership, + registerGroupPresentation, + rekeyGraph, + rekeyRoot, + subgraphs + } +}) diff --git a/src/stores/graphMetadataStore.test.ts b/src/stores/graphMetadataStore.test.ts new file mode 100644 index 00000000000..02ec93e617c --- /dev/null +++ b/src/stores/graphMetadataStore.test.ts @@ -0,0 +1,39 @@ +import { describe, expect, it } from 'vitest' + +import type { UUID } from '@/utils/uuid' + +import { useGraphMetadataStore } from './graphMetadataStore' + +describe(useGraphMetadataStore, () => { + const first = '00000000-0000-4000-8000-000000000001' as UUID + const second = '00000000-0000-4000-8000-000000000002' as UUID + + it('rekeys one canonical metadata record', () => { + const store = useGraphMetadataStore() + const metadata = store.get(first) + metadata.revision = 3 + metadata.config = { links_ontop: true } + metadata.extra = { workflowRendererVersion: 'Vue' } + + store.rekey(first, second) + + expect(store.get(second)).toBe(metadata) + expect(store.get(second)).toMatchObject({ + revision: 3, + config: { links_ontop: true }, + extra: { workflowRendererVersion: 'Vue' } + }) + expect(store.get(first)).not.toBe(metadata) + }) + + it('clears only the selected graph', () => { + const store = useGraphMetadataStore() + const firstMetadata = store.get(first) + const secondMetadata = store.get(second) + + store.clear(first) + + expect(store.get(first)).not.toBe(firstMetadata) + expect(store.get(second)).toBe(secondMetadata) + }) +}) diff --git a/src/stores/graphMetadataStore.ts b/src/stores/graphMetadataStore.ts new file mode 100644 index 00000000000..52e766ce6f1 --- /dev/null +++ b/src/stores/graphMetadataStore.ts @@ -0,0 +1,42 @@ +import { defineStore } from 'pinia' +import { ref, shallowReactive } from 'vue' + +import type { LGraphConfig, LGraphExtra } from '@/lib/litegraph/src/LGraph' +import type { UUID } from '@/utils/uuid' + +interface GraphMetadata { + revision: number + config: LGraphConfig + extra: LGraphExtra +} + +export const useGraphMetadataStore = defineStore('graphMetadata', () => { + const metadata = ref(new Map()) + + function get(graphId: UUID): GraphMetadata { + const existing = metadata.value.get(graphId) + if (existing) return existing + + const created = shallowReactive({ + revision: 0, + config: {}, + extra: {} + }) + metadata.value.set(graphId, created) + return created + } + + function rekey(previousId: UUID, nextId: UUID): void { + if (previousId === nextId) return + const existing = metadata.value.get(previousId) + if (!existing) return + metadata.value.delete(previousId) + metadata.value.set(nextId, existing) + } + + function clear(graphId: UUID): void { + metadata.value.delete(graphId) + } + + return { get, rekey, clear } +}) diff --git a/src/stores/linkStateStore.ts b/src/stores/linkStateStore.ts new file mode 100644 index 00000000000..80926d477e3 --- /dev/null +++ b/src/stores/linkStateStore.ts @@ -0,0 +1,88 @@ +import { defineStore } from 'pinia' + +import type { CanvasColour, Point } from '@/lib/litegraph/src/interfaces' +import type { + GraphScope, + OwningGraphId, + RootGraphId +} from '@/types/graphScopeId' +import type { LinkId } from '@/types/linkId' + +export type LinkExecutionData = + | number + | string + | boolean + | { toToolTip?(): string } + +interface LinkPersistentState { + color?: CanvasColour | null +} + +interface LinkRuntimeState { + data?: LinkExecutionData + outputData?: unknown + position: Point + lastTime?: number + path?: Path2D + centreAngle?: number + dragging?: boolean +} + +export interface LinkState { + persistent: LinkPersistentState + runtime: LinkRuntimeState +} + +type OwnerStates = Map> + +export function createLinkState(): LinkState { + return { + persistent: {}, + runtime: { outputData: null, position: [0, 0] } + } +} + +export const useLinkStateStore = defineStore('linkState', () => { + const roots = new Map() + + function register( + scope: GraphScope, + id: LinkId, + state: LinkState + ): LinkState { + let owners = roots.get(scope.rootGraphId) + if (!owners) { + owners = new Map() + roots.set(scope.rootGraphId, owners) + } + let states = owners.get(scope.owningGraphId) + if (!states) { + states = new Map() + owners.set(scope.owningGraphId, states) + } + const registered = states.get(id) + if (registered) return registered + states.set(id, state) + return state + } + + function unregister( + scope: GraphScope, + id: LinkId, + state: LinkState + ): boolean { + const owners = roots.get(scope.rootGraphId) + const states = owners?.get(scope.owningGraphId) + if (states?.get(id) !== state) return false + states.delete(id) + if (!states.size) owners?.delete(scope.owningGraphId) + if (!owners?.size) roots.delete(scope.rootGraphId) + return true + } + + function get(scope: GraphScope, id: LinkId): LinkState | undefined { + return roots.get(scope.rootGraphId)?.get(scope.owningGraphId)?.get(id) + } + + return { get, register, unregister } +}) diff --git a/src/stores/nodeOutputStore.test.ts b/src/stores/nodeOutputStore.test.ts index e3d213ccd60..970b94731df 100644 --- a/src/stores/nodeOutputStore.test.ts +++ b/src/stores/nodeOutputStore.test.ts @@ -6,7 +6,10 @@ import { LiteGraph } from '@/lib/litegraph/src/litegraph' import type { ExecutedWsMessage } from '@/schemas/apiSchema' import { app } from '@/scripts/app' import { useNodeOutputStore } from '@/stores/nodeOutputStore' -import { createNodeExecutionId } from '@/types/nodeIdentification' +import { + createNodeExecutionId, + createNodeLocatorId +} from '@/types/nodeIdentification' import { toNodeId } from '@/types/nodeId' import * as litegraphUtil from '@/utils/litegraphUtil' @@ -188,6 +191,29 @@ describe('nodeOutputStore setNodeOutputsByExecutionId with merge', () => { expect(refAfter).not.toBe(refBefore) expect(refAfter?.images).toHaveLength(2) }) + + it('replaces outputs written through the legacy map', () => { + const store = useNodeOutputStore() + const node = createMockNode({ id: 5 }) + + store.setNodeOutputs(node, 'canonical.png') + const legacyOutput = createMockOutputs([{ filename: 'legacy.png' }]) + store.replaceOutputsFromLegacy({ '5': legacyOutput }) + + expect(store.getNodeOutputs(node)).toEqual(legacyOutput) + }) + + it('projects previews without reading legacy map mutations back', () => { + const store = useNodeOutputStore() + const node = createMockNode({ id: 5 }) + + store.setNodePreviewsByLocatorId(createNodeLocatorId(null, node.id), [ + 'blob:canonical' + ]) + app.nodePreviewImages['5'] = ['blob:legacy'] + + expect(store.getNodePreviews(node)).toEqual(['blob:canonical']) + }) }) describe('nodeOutputStore restoreOutputs', () => { @@ -760,6 +786,17 @@ describe('nodeOutputStore setNodeOutputs (widget path)', () => { expect(store.nodeOutputs['5']?.images?.[0]?.type).toBe('input') }) + it('leaves node images unchanged for preview change detection', () => { + const store = useNodeOutputStore() + const images = [{ filename: 'previous.png' }] + const node = createMockNode({ id: 5, images }) + + store.setNodeOutputs(node, 'test.png') + + expect(node.images).toBe(images) + expect(node.images).not.toBe(store.nodeOutputs['5']?.images) + }) + it('should skip empty array of filenames after createOutputs', () => { const store = useNodeOutputStore() const node = createMockNode({ id: 5 }) diff --git a/src/stores/nodeOutputStore.ts b/src/stores/nodeOutputStore.ts index 1d1a967a885..e7789d1ee2c 100644 --- a/src/stores/nodeOutputStore.ts +++ b/src/stores/nodeOutputStore.ts @@ -71,19 +71,26 @@ export const useNodeOutputStore = defineStore('nodeOutput', () => { scheduledRevoke[locator] = { stop } } - const nodeOutputs = ref>({}) + const nodeOutputs = ref>({ + ...app.nodeOutputs + }) const nodePreviewImages = ref>( - app.nodePreviewImages || {} + Object.fromEntries( + Object.entries(app.nodePreviewImages || {}).map(([id, urls]) => [ + id, + [...urls] + ]) + ) ) function getNodeOutputs( node: LGraphNode ): ExecutedWsMessage['output'] | undefined { - return app.nodeOutputs[nodeToNodeLocatorId(node)] + return nodeOutputs.value[nodeToNodeLocatorId(node)] } function getNodePreviews(node: LGraphNode): string[] | undefined { - return app.nodePreviewImages[nodeToNodeLocatorId(node)] + return nodePreviewImages.value[nodeToNodeLocatorId(node)] } const isImageOutputs = ( @@ -183,34 +190,36 @@ export const useNodeOutputStore = defineStore('nodeOutput', () => { Array.isArray(incomingImages) && incomingImages.length > 0 if ( !hasIncomingImages && - isInputPreviewOutput(app.nodeOutputs[nodeLocatorId]) + isInputPreviewOutput(nodeOutputs.value[nodeLocatorId]) ) { outputs = { ...outputs, - images: app.nodeOutputs[nodeLocatorId].images + images: nodeOutputs.value[nodeLocatorId].images } } if (options.merge) { - const existingOutput = app.nodeOutputs[nodeLocatorId] + const existingOutput = nodeOutputs.value[nodeLocatorId] if (existingOutput && outputs) { + const mergedOutput = { ...existingOutput } for (const k in outputs) { const existingValue = existingOutput[k] const newValue = (outputs as Record)[k] if (Array.isArray(existingValue) && Array.isArray(newValue)) { - existingOutput[k] = existingValue.concat(newValue) + mergedOutput[k] = existingValue.concat(newValue) } else { - existingOutput[k] = newValue + mergedOutput[k] = newValue } } - nodeOutputs.value[nodeLocatorId] = { ...existingOutput } + nodeOutputs.value[nodeLocatorId] = mergedOutput + app.nodeOutputs[nodeLocatorId] = clone(mergedOutput) return } } - app.nodeOutputs[nodeLocatorId] = outputs nodeOutputs.value[nodeLocatorId] = outputs + app.nodeOutputs[nodeLocatorId] = clone(outputs) } function setNodeOutputs( @@ -239,6 +248,20 @@ export const useNodeOutputStore = defineStore('nodeOutput', () => { } } + function setNodeOutputImages( + node: LGraphNode, + images: NonNullable + ) { + const locatorId = nodeToNodeLocatorId(node) + if (!locatorId) return + + setOutputsByLocatorId(locatorId, { + ...nodeOutputs.value[locatorId], + images + }) + node.images = images + } + function setNodeOutputsByExecutionId( executionId: NodeExecutionId, outputs: ExecutedWsMessage['output'] | ResultItem, @@ -263,7 +286,7 @@ export const useNodeOutputStore = defineStore('nodeOutput', () => { nodeLocatorId: NodeLocatorId, previewImages: string[] ) { - const existingPreviews = app.nodePreviewImages[nodeLocatorId] + const existingPreviews = nodePreviewImages.value[nodeLocatorId] if (scheduledRevoke[nodeLocatorId]) { scheduledRevoke[nodeLocatorId].stop() delete scheduledRevoke[nodeLocatorId] @@ -276,8 +299,8 @@ export const useNodeOutputStore = defineStore('nodeOutput', () => { for (const url of previewImages) { retainSharedObjectUrl(url) } - app.nodePreviewImages[nodeLocatorId] = previewImages nodePreviewImages.value[nodeLocatorId] = previewImages + app.nodePreviewImages[nodeLocatorId] = [...previewImages] } function setNodePreviewsByNodeId(nodeId: NodeId, previewImages: string[]) { @@ -293,15 +316,15 @@ export const useNodeOutputStore = defineStore('nodeOutput', () => { } function revokePreviewsByLocatorId(nodeLocatorId: NodeLocatorId) { - const previews = app.nodePreviewImages[nodeLocatorId] + const previews = nodePreviewImages.value[nodeLocatorId] if (!previews?.[Symbol.iterator]) return for (const url of previews) { releaseSharedObjectUrl(url) } - delete app.nodePreviewImages[nodeLocatorId] delete nodePreviewImages.value[nodeLocatorId] + delete app.nodePreviewImages[nodeLocatorId] } function releasePreviewUrls(previews: Record) { @@ -315,9 +338,9 @@ export const useNodeOutputStore = defineStore('nodeOutput', () => { } function revokeAllPreviews() { - releasePreviewUrls(app.nodePreviewImages) - app.nodePreviewImages = {} + releasePreviewUrls(nodePreviewImages.value) nodePreviewImages.value = {} + app.nodePreviewImages = {} } /** Release the previews held for a workflow that is going away. */ @@ -346,12 +369,12 @@ export const useNodeOutputStore = defineStore('nodeOutput', () => { discardClosedWorkflowPreviews() discardPreviewsForWorkflow(workflowPath) - const previews = app.nodePreviewImages + const previews = nodePreviewImages.value if (Object.keys(previews).length) { stashedPreviews.set(workflowPath, previews) } - app.nodePreviewImages = {} nodePreviewImages.value = {} + app.nodePreviewImages = {} } function restorePreviewsForWorkflow(workflowPath: string | undefined) { @@ -361,7 +384,7 @@ export const useNodeOutputStore = defineStore('nodeOutput', () => { if (!stashed) return stashedPreviews.delete(workflowPath) - const live = app.nodePreviewImages + const live = nodePreviewImages.value const superseded: Record = {} for (const [nodeLocatorId, urls] of Object.entries(stashed)) { // A preview that arrived while the graph was loading wins; the stashed @@ -371,6 +394,7 @@ export const useNodeOutputStore = defineStore('nodeOutput', () => { } releasePreviewUrls(superseded) nodePreviewImages.value = { ...live } + app.nodePreviewImages = clone(nodePreviewImages.value) } function revokeSubgraphPreviews(subgraphNode: SubgraphNode) { @@ -388,19 +412,19 @@ export const useNodeOutputStore = defineStore('nodeOutput', () => { } function removeOutputsByLocatorId(nodeLocatorId: NodeLocatorId) { - const hadOutputs = !!app.nodeOutputs[nodeLocatorId] - delete app.nodeOutputs[nodeLocatorId] + const hadOutputs = !!nodeOutputs.value[nodeLocatorId] delete nodeOutputs.value[nodeLocatorId] + delete app.nodeOutputs[nodeLocatorId] - if (app.nodePreviewImages[nodeLocatorId]) { - const previews = app.nodePreviewImages[nodeLocatorId] + if (nodePreviewImages.value[nodeLocatorId]) { + const previews = nodePreviewImages.value[nodeLocatorId] if (previews?.[Symbol.iterator]) { for (const url of previews) { releaseSharedObjectUrl(url) } } - delete app.nodePreviewImages[nodeLocatorId] delete nodePreviewImages.value[nodeLocatorId] + delete app.nodePreviewImages[nodeLocatorId] } return hadOutputs @@ -417,53 +441,39 @@ export const useNodeOutputStore = defineStore('nodeOutput', () => { } function snapshotOutputs(): Record { - return clone(app.nodeOutputs) + return clone(nodeOutputs.value) } - function restoreOutputs( + function replaceOutputsFromLegacy( outputs: Record ) { const parsedOutputs = mapKeys( outputs, (_, id) => executionIdToNodeLocatorId(app.rootGraph, id) ?? id ) - app.nodeOutputs = parsedOutputs nodeOutputs.value = { ...parsedOutputs } } - function updateNodeImages(node: LGraphNode) { - if (!node.images?.length) return - - const nodeLocatorId = nodeIdToNodeLocatorId(node.id) - - if (nodeLocatorId) { - const existingOutputs = app.nodeOutputs[nodeLocatorId] - - if (existingOutputs) { - const updatedOutputs = { - ...existingOutputs, - images: node.images - } - - app.nodeOutputs[nodeLocatorId] = updatedOutputs - nodeOutputs.value[nodeLocatorId] = updatedOutputs - } - } + function restoreOutputs( + outputs: Record + ) { + replaceOutputsFromLegacy(outputs) + app.nodeOutputs = clone(nodeOutputs.value) } function refreshNodeOutputs(node: LGraphNode) { const locatorId = nodeToNodeLocatorId(node) if (!locatorId) return - const outputs = app.nodeOutputs[locatorId] + const outputs = nodeOutputs.value[locatorId] if (!outputs) return nodeOutputs.value[locatorId] = { ...outputs } } function resetAllOutputsAndPreviews() { - app.nodeOutputs = {} nodeOutputs.value = {} + app.nodeOutputs = {} revokeAllPreviews() } @@ -494,11 +504,11 @@ export const useNodeOutputStore = defineStore('nodeOutput', () => { getPreviewParam, setNodeOutputs, + setNodeOutputImages, setNodeOutputsByExecutionId, setNodePreviewsByExecutionId, setNodePreviewsByLocatorId, setNodePreviewsByNodeId, - updateNodeImages, refreshNodeOutputs, syncLegacyNodeImgs, @@ -512,6 +522,7 @@ export const useNodeOutputStore = defineStore('nodeOutput', () => { removeNodeOutputs, removeNodeOutputsForNode, snapshotOutputs, + replaceOutputsFromLegacy, restoreOutputs, resetAllOutputsAndPreviews, diff --git a/src/stores/previewExposureStore.test.ts b/src/stores/previewExposureStore.test.ts index 0bbb0299ff7..fec6b791ab3 100644 --- a/src/stores/previewExposureStore.test.ts +++ b/src/stores/previewExposureStore.test.ts @@ -167,6 +167,24 @@ describe(usePreviewExposureStore, () => { }) }) + describe('clearHost', () => { + it('removes only the target host', () => { + store.addExposure(rootGraphA, hostA, { + sourceNodeId: '1', + sourcePreviewName: 'p' + }) + store.addExposure(rootGraphA, hostB, { + sourceNodeId: '2', + sourcePreviewName: 'p' + }) + + store.clearHost(rootGraphA, hostA) + + expect(store.getExposures(rootGraphA, hostA)).toEqual([]) + expect(store.getExposures(rootGraphA, hostB)).toHaveLength(1) + }) + }) + describe('isolation between (rootGraphId, hostNodeLocator) pairs', () => { it('keeps separate buckets per host and per root graph', () => { store.addExposure(rootGraphA, hostA, { diff --git a/src/stores/previewExposureStore.ts b/src/stores/previewExposureStore.ts index 7e55bc67145..1b81b2dc009 100644 --- a/src/stores/previewExposureStore.ts +++ b/src/stores/previewExposureStore.ts @@ -8,7 +8,10 @@ import type { import { resolvePreviewExposureChain } from '@/core/graph/subgraph/preview/previewExposureChain' import type { PromotedWidgetSource } from '@/core/graph/subgraph/promotedWidgetTypes' import type { PreviewExposure } from '@/core/schemas/previewExposureSchema' +import type { SubgraphNode } from '@/lib/litegraph/src/litegraph' import { nextUniqueName } from '@/lib/litegraph/src/strings' +import { createNodeLocatorId } from '@/types/nodeIdentification' +import type { NodeLocatorId } from '@/types/nodeIdentification' import { toNodeId } from '@/types/nodeId' import type { SerializedNodeId } from '@/types/nodeId' import type { UUID } from '@/utils/uuid' @@ -32,6 +35,13 @@ function normalizePreviewExposure( } } +export function getPreviewExposureHostLocator( + host: SubgraphNode +): NodeLocatorId { + const graphId = host.graph?.isRootGraph ? null : (host.graph?.id ?? null) + return createNodeLocatorId(graphId, host.id) +} + export const usePreviewExposureStore = defineStore('previewExposure', () => { const exposures = ref(new Map>()) @@ -104,6 +114,13 @@ export const usePreviewExposureStore = defineStore('previewExposure', () => { setExposures(rootGraphId, hostNodeLocator, next) } + function clearHost(rootGraphId: UUID, hostNodeLocator: string): void { + const hosts = exposures.value.get(rootGraphId) + if (!hosts) return + hosts.delete(hostNodeLocator) + if (hosts.size === 0) exposures.value.delete(rootGraphId) + } + function clearGraph(rootGraphId: UUID): void { exposures.value.delete(rootGraphId) } @@ -141,6 +158,7 @@ export const usePreviewExposureStore = defineStore('previewExposure', () => { setExposures, addExposure, removeExposure, + clearHost, clearGraph, resolveChain } diff --git a/src/stores/rerouteStore.ts b/src/stores/rerouteStore.ts index 1127c35bbbe..0387a649ad7 100644 --- a/src/stores/rerouteStore.ts +++ b/src/stores/rerouteStore.ts @@ -151,6 +151,16 @@ export const useRerouteStore = defineStore('reroute', () => { return chain?.graphId === scope.owningGraphId ? chain : undefined } + function* graphChains(scope: GraphScope): Generator { + const bucket = roots.get(scope.rootGraphId) + const ids = bucket?.idsByOwner.get(scope.owningGraphId) + if (!bucket || !ids) return + for (const id of ids) { + const chain = bucket.chains.get(id) + if (chain) yield chain + } + } + /** Removes a chain's registration; only the registered state may vacate it. */ function deleteReroute(scope: GraphScope, chain: RerouteChain): boolean { const bucket = roots.get(scope.rootGraphId) @@ -185,6 +195,7 @@ export const useRerouteStore = defineStore('reroute', () => { return { registerReroute, getReroute, + graphChains, deleteReroute, getMembership, clearOwner, diff --git a/src/stores/widgetValueStore.test.ts b/src/stores/widgetValueStore.test.ts index e0d2e943bbd..cb90dc6a48d 100644 --- a/src/stores/widgetValueStore.test.ts +++ b/src/stores/widgetValueStore.test.ts @@ -318,6 +318,20 @@ describe('useWidgetValueStore', () => { expect(store.getWidget(seedA)).toBeUndefined() expect(store.getWidget(seedB)?.value).toBe(2) }) + + it('clearNode removes only the target node values, render state, and order', () => { + const store = useWidgetValueStore() + const sibling = widgetId(graphA, toNodeId('node-2'), 'seed') + store.registerWidget(seedA, state('number', 1), { advanced: true }) + store.registerWidget(sibling, state('number', 2)) + + store.clearNode(graphA, toNodeId('node-1')) + + expect(store.getWidget(seedA)).toBeUndefined() + expect(store.getWidgetRenderState(seedA)).toBeUndefined() + expect(store.getNodeWidgetIds(graphA, toNodeId('node-1'))).toEqual([]) + expect(store.getWidget(sibling)?.value).toBe(2) + }) }) describe('un-keyable widget ids', () => { diff --git a/src/stores/widgetValueStore.ts b/src/stores/widgetValueStore.ts index 11bd1f8fd0e..8c3815de59c 100644 --- a/src/stores/widgetValueStore.ts +++ b/src/stores/widgetValueStore.ts @@ -16,6 +16,12 @@ export interface WidgetRenderState { tooltip?: string } +interface WidgetRestorationState { + positional: readonly WidgetValue[] + named?: Readonly> + restoreNamed: boolean +} + export function stripGraphPrefix(scopedId: SerializedNodeId): NodeId | null { return parseNodeId(String(scopedId).replace(/^(.*:)+/, '')) } @@ -26,6 +32,51 @@ export const useWidgetValueStore = defineStore('widgetValue', () => { new Map>() ) const graphNodeWidgetOrders = ref(new Map>()) + const graphWidgetRestorations = new Map< + UUID, + Map + >() + + function setNodeWidgetRestoration( + graphId: UUID, + nodeId: NodeId, + restoration: WidgetRestorationState + ): void { + let graphRestorations = graphWidgetRestorations.get(graphId) + if (!graphRestorations) { + graphRestorations = new Map() + graphWidgetRestorations.set(graphId, graphRestorations) + } + graphRestorations.set(nodeId, restoration) + } + + function getRestoredWidgetValue( + graphId: UUID, + nodeId: NodeId, + name: string, + positionalIndex: number + ): { value: WidgetValue } | undefined { + const restoration = graphWidgetRestorations.get(graphId)?.get(nodeId) + if (!restoration) return + if (restoration.restoreNamed && restoration.named) { + return Object.hasOwn(restoration.named, name) + ? { value: restoration.named[name] } + : undefined + } + return positionalIndex < restoration.positional.length + ? { value: restoration.positional[positionalIndex] } + : undefined + } + + function getPositionalRestoredWidgetValue( + graphId: UUID, + nodeId: NodeId, + positionalIndex: number + ): WidgetValue | undefined { + return graphWidgetRestorations.get(graphId)?.get(nodeId)?.positional[ + positionalIndex + ] + } function getGraphWidgetStates(graphId: UUID): Map { const widgetStates = graphWidgetStates.value.get(graphId) @@ -272,14 +323,39 @@ export const useWidgetValueStore = defineStore('widgetValue', () => { graphOrders.delete(localNodeId) } + function clearNode(graphId: UUID, nodeId: NodeId): void { + graphWidgetRestorations.get(graphId)?.delete(nodeId) + const widgetStates = graphWidgetStates.value.get(graphId) + const widgetRenderStates = graphWidgetRenderStates.value.get(graphId) + if (widgetStates) { + for (const [id, state] of widgetStates) { + if (state.nodeId !== nodeId) continue + widgetStates.delete(id) + widgetRenderStates?.delete(id) + } + if (widgetStates.size === 0) graphWidgetStates.value.delete(graphId) + } + if (widgetRenderStates?.size === 0) { + graphWidgetRenderStates.value.delete(graphId) + } + + const widgetOrders = graphNodeWidgetOrders.value.get(graphId) + widgetOrders?.delete(nodeId) + if (widgetOrders?.size === 0) graphNodeWidgetOrders.value.delete(graphId) + } + function clearGraph(graphId: UUID): void { graphWidgetStates.value.delete(graphId) graphWidgetRenderStates.value.delete(graphId) graphNodeWidgetOrders.value.delete(graphId) + graphWidgetRestorations.delete(graphId) } return { registerWidget, + setNodeWidgetRestoration, + getRestoredWidgetValue, + getPositionalRestoredWidgetValue, getWidget, getWidgetRenderState, setValue, @@ -291,6 +367,7 @@ export const useWidgetValueStore = defineStore('widgetValue', () => { replaceNodeWidgetOrder, removeNodeWidgetOrder, releaseNodeWidgets, + clearNode, clearGraph } }) diff --git a/src/types/nodeState.ts b/src/types/nodeState.ts index 0ec32f63814..9a0af949de8 100644 --- a/src/types/nodeState.ts +++ b/src/types/nodeState.ts @@ -1,16 +1,19 @@ +import type { INodeFlags } from '@/lib/litegraph/src/interfaces' import type { - INodeFlags, - INodeInputSlot, - INodeOutputSlot -} from '@/lib/litegraph/src/interfaces' + InputSlotDescriptor, + OutputSlotDescriptor +} from '@/lib/litegraph/src/node/slotDescriptorView' import type { LGraphEventMode, RenderShape, TitleMode } from '@/lib/litegraph/src/types/globalEnums' +import type { ISerialisedNode } from '@/lib/litegraph/src/types/serialisation' import type { NodeId } from '@/types/nodeId' import type { UUID } from '@/utils/uuid' +export type NodeProperty = string | number | boolean | object | null + /** * The fields the renderer draws. Selection, execution, errors, geometry, widget * values and links live elsewhere — see docs/architecture/node-data-store.md. @@ -20,14 +23,15 @@ export interface NodeState { /** Owning (sub)graph id — partitioning + locator ids. */ graphId: UUID readonly id: NodeId - /** The node's own `shallowReactive` array; slots are still class instances. */ - inputs: INodeInputSlot[] + inputs: InputSlotDescriptor[] + lastSerialization?: ISerialisedNode mode: LGraphEventMode - /** @see {@link inputs} */ - outputs: INodeOutputSlot[] + outputs: OutputSlotDescriptor[] + properties: Record title: string type: string bgcolor?: string + boxcolor?: string color?: string resizable?: boolean shape?: RenderShape diff --git a/src/utils/__tests__/litegraphTestUtils.ts b/src/utils/__tests__/litegraphTestUtils.ts index 14f701a809a..0cba675530f 100644 --- a/src/utils/__tests__/litegraphTestUtils.ts +++ b/src/utils/__tests__/litegraphTestUtils.ts @@ -42,7 +42,8 @@ export function createNodeState(overrides: Partial = {}): NodeState { outputs: [], title: 'Test Node', type: 'TestNode', - ...overrides + ...overrides, + properties: overrides.properties ?? {} } } From 8233c71b03a5f9becec772a8df509eb75a3635a8 Mon Sep 17 00:00:00 2001 From: Amp Date: Sat, 22 Aug 2026 09:28:31 +0000 Subject: [PATCH 02/14] fix: address ECS migration review feedback Amp-Thread-ID: https://ampcode.com/threads/T-01a02889-c911-708b-bf1a-11dfb0904fff --- .../ecs/ecs-component-entity-data-audit.md | 84 ++++++++-------- .../ecs/ecs-extension-compatibility-audit.md | 20 ++-- docs/architecture/ecs/ecs-migration-plan.md | 32 +++---- src/components/maskeditor/BrushCursor.vue | 46 ++++----- .../widgets/matchTypeConfiguring.test.ts | 15 +-- .../litegraph/src/LGraph.serialise.test.ts | 4 +- src/lib/litegraph/src/LGraph.test.ts | 95 ++++++++++++++++++- src/lib/litegraph/src/LGraph.ts | 35 +++++-- src/lib/litegraph/src/LGraphGroup.test.ts | 18 ++++ src/lib/litegraph/src/LGraphGroup.ts | 16 +++- src/lib/litegraph/src/LGraphNode.test.ts | 17 ++++ src/lib/litegraph/src/LGraphNode.ts | 29 +++--- .../src/LGraphNode.widgetOrder.test.ts | 22 +++++ src/lib/litegraph/src/extensionPersistence.ts | 2 + src/lib/litegraph/src/litegraph.ts | 1 - .../litegraph/src/node/NodeOutputSlot.test.ts | 15 +++ src/lib/litegraph/src/node/NodeOutputSlot.ts | 8 +- .../src/node/slotDescriptorView.test.ts | 43 +++++++++ .../litegraph/src/node/slotDescriptorView.ts | 8 +- .../canvas/litegraph/slotCalculations.test.ts | 21 ++++ .../core/canvas/litegraph/slotCalculations.ts | 20 ++-- src/scripts/app.test.ts | 13 +++ src/scripts/app.ts | 32 +++---- src/stores/executionOrderStore.ts | 10 +- src/stores/nodeOutputStore.test.ts | 14 +++ src/stores/nodeOutputStore.ts | 4 +- src/stores/previewExposureStore.test.ts | 21 +++- src/stores/previewExposureStore.ts | 8 +- 28 files changed, 480 insertions(+), 173 deletions(-) create mode 100644 src/lib/litegraph/src/node/slotDescriptorView.test.ts diff --git a/docs/architecture/ecs/ecs-component-entity-data-audit.md b/docs/architecture/ecs/ecs-component-entity-data-audit.md index 18a47ec96fc..8b4b1d4e0ce 100644 --- a/docs/architecture/ecs/ecs-component-entity-data-audit.md +++ b/docs/architecture/ecs/ecs-component-entity-data-audit.md @@ -16,27 +16,27 @@ All 19 scoped concerns are implemented. Compatibility facades remain where extensions or legacy rendering require their object identity and mutation behavior. Five larger structural slices remain and are listed separately below. -| Concern | Status | Current boundary | -| ----------------------------------- | ----------- | --------------------------------------------------------------------------------------------- | -| Graph and subgraph definitions | Implemented | Root-scoped records own ordered membership, registries, and subgraph definition metadata. | -| Remaining node visuals | Implemented | `NodeState` owns `boxcolor` behind an enumerable compatibility accessor. | -| Outputs and transient previews | Implemented | Store maps own reads; legacy output-map writes commit through stable nested mutation views. | -| Node ordering | Implemented | One action updates canonical layout z-index and legacy node-array order. | -| Store-driven serialization | Implemented | Graph, node, group, topology, reroute, and subgraph DTOs enumerate store authorities. | -| Legacy node geometry projection | Implemented | The layout adapter owns stable mutable geometry views and write-through. | -| Link non-topological state | Implemented | Separate persistent and runtime records follow topology lifecycle. | -| Plain slot descriptors | Implemented | Plain reactive descriptors back stable slot-class projections with native callback semantics. | -| Node properties | Implemented | `NodeState` owns the stable mutable property dictionary behind compatibility behavior. | -| Group presentation | Implemented | Graph-definition records own presentation while layout owns geometry. | -| Preview-exposure persistence | Implemented | Owner-scoped locators key runtime lookup and serialization; raw IDs are hydration-only input. | -| Extension persistence adapter | Implemented | Validated namespaced payloads and isolated hook views preserve canonical fields. | -| Graph metadata | Implemented | A graph-keyed store owns revision, config, and extra behind compatibility accessors. | -| Graph invalidation | Implemented | Graph-scoped batching coalesces composite mutations behind `_version`. | -| Unknown-node fallback | Implemented | `NodeState` owns opaque fallback DTOs behind `last_serialization`. | -| Execution order | Implemented | Graph-scoped derived records own execution order behind the node projection. | -| Entity ID allocation | Implemented | Root-keyed allocation state and shared helpers own minting and observation. | -| Delayed widget restoration | Implemented | Store-owned restoration state feeds registered and delayed widgets. | -| Widget and preview-exposure cleanup | Implemented | Removal, replacement, teardown, and failed configure clear node-owned records. | +| Concern | Status | Current boundary | +| ----------------------------------- | ----------- | ------------------------------------------------------------------------------------------------------ | +| Graph and subgraph definitions | Implemented | Root-scoped records own ordered membership, registries, and subgraph definition metadata. | +| Remaining node visuals | Implemented | `NodeState` owns `boxcolor` behind an enumerable compatibility accessor. | +| Outputs and transient previews | Implemented | Store maps own reads; legacy output-map writes commit through stable nested mutation views. | +| Node ordering | Implemented | One action updates canonical layout z-index and legacy node-array order. | +| Store-driven serialization | Implemented | Graph, node, group, topology, reroute, and subgraph DTOs enumerate store authorities. | +| Legacy node geometry projection | Implemented | The layout adapter owns stable mutable geometry views and write-through. | +| Link non-topological state | Implemented | Separate persistent and runtime records follow topology lifecycle. | +| Plain slot descriptors | Implemented | Plain reactive descriptors back stable slot-class projections with native callback semantics. | +| Node properties | Implemented | `NodeState` owns the stable mutable property dictionary behind compatibility behavior. | +| Group presentation | Implemented | Graph-definition records own presentation while layout owns geometry. | +| Preview-exposure persistence | Implemented | Owner-scoped locators key runtime lookup and serialization; raw IDs are hydration-only input. | +| Extension persistence adapter | Implemented | Validated namespaced payloads and isolated hook views preserve canonical fields. | +| Graph metadata | Implemented | A graph-keyed store owns revision, config, and extra behind compatibility accessors. | +| Graph invalidation | Implemented | Graph-scoped batching coalesces composite mutations behind `_version`. | +| Unknown-node fallback | Implemented | `NodeState` owns opaque fallback DTOs behind `last_serialization`. | +| Execution order | Implemented | Graph-scoped derived records own execution order behind the node projection. | +| Entity ID allocation | Implemented | Root-keyed allocation state and shared helpers own minting and observation. | +| Delayed widget restoration | Implemented | Store-owned restoration state feeds registered and delayed widgets. | +| Widget and preview-exposure cleanup | Implemented | Removal, replacement, and teardown clear node-owned records; failed configure cleanup remains limited. | ## Pre-migration baseline findings @@ -228,27 +228,27 @@ undo model. Each concern is completed in its own commit. The order follows the dependency sequence above rather than the summary-table order. -| Sequence | Concern | Status | Commit | Result | -| -------- | ----------------------------------- | -------- | ------------ | ------------------------------------------------------------------------------------------------ | -| 1 | Remaining node visuals | Complete | `fa5dcd8ca` | `boxcolor` moved into `NodeState` with an enumerable tracked compatibility accessor. | -| 2 | Node ordering | Complete | `06ae14e8b` | One action now updates layout z-index and the legacy node array for front/back order. | -| 3 | Outputs and transient previews | Complete | `570b38c2a` | Store maps own reads; the legacy output map commits mutations through a compatibility view. | -| 4 | Preview-exposure persistence | Complete | `2212ed302` | Host exposures use owner-scoped locators; raw node-ID entries are hydration-only input. | -| 5 | Widget and preview-exposure cleanup | Complete | `ecd148fa0` | Node removal, replacement, teardown, and failed configure clear node-owned store records. | -| 6 | Graph metadata | Complete | `298bf3da0` | Revision, config, and extra now use graph-keyed store records behind compatibility accessors. | -| 7 | Entity ID allocation | Complete | `7fbd10624` | Root-keyed allocation state now backs graph accessors; clipboard allocation uses shared helpers. | -| 8 | Graph and subgraph definitions | Complete | `8923225fb` | Root-scoped records now own ordered membership, the registry, and subgraph definition metadata. | -| 9 | Legacy node geometry projection | Complete | `854f68fb2` | The layout adapter now owns stable legacy views, synchronization, and geometry write-through. | -| 10 | Plain slot descriptors | Complete | `cc53dad291` | Plain store descriptors now back stable extension-visible slot class projections. | -| 11 | Group presentation | Complete | `3ac7b726a` | Graph-definition records now back mutable presentation fields and serialization. | -| 12 | Link non-topological state | Complete | `38b40988a` | Separate persistent and runtime records now back compatible link fields with topology lifecycle. | -| 13 | Node properties | Complete | `f75b30dbd` | Node state now owns stable mutable properties across lifecycle, callbacks, and serialization. | -| 14 | Unknown-node fallback | Complete | `ba4757f4c` | Node state now owns unknown-node fallback records behind the compatibility accessor. | -| 15 | Delayed widget restoration | Complete | `972947d939` | Store-owned restoration state now hydrates configured and delayed widgets from wire shadows. | -| 16 | Extension persistence adapter | Complete | `287bf33f38` | Validated payloads round trip through a namespace while legacy hooks mutate isolated DTO views. | -| 17 | Execution order | Complete | `7f6c2783dd` | Graph-scoped derived records own execution order behind the compatible node projection. | -| 18 | Graph invalidation | Complete | `335b69420c` | Graph-scoped batching coalesces composite invalidations behind the compatible version counter. | -| 19 | Store-driven serialization | Complete | `458d292f1` | Store authorities now build wire DTOs after normalized parity against mutable serialization. | +| Sequence | Concern | Status | Commit | Result | +| -------- | ----------------------------------- | -------- | ------------ | ------------------------------------------------------------------------------------------------------ | +| 1 | Remaining node visuals | Complete | `fa5dcd8ca` | `boxcolor` moved into `NodeState` with an enumerable tracked compatibility accessor. | +| 2 | Node ordering | Complete | `06ae14e8b` | One action now updates layout z-index and the legacy node array for front/back order. | +| 3 | Outputs and transient previews | Complete | `570b38c2a` | Store maps own reads; the legacy output map commits mutations through a compatibility view. | +| 4 | Preview-exposure persistence | Complete | `2212ed302` | Host exposures use owner-scoped locators; raw node-ID entries are hydration-only input. | +| 5 | Widget and preview-exposure cleanup | Complete | `ecd148fa0` | Removal, replacement, and teardown clear node-owned records; failed configure cleanup remains limited. | +| 6 | Graph metadata | Complete | `298bf3da0` | Revision, config, and extra now use graph-keyed store records behind compatibility accessors. | +| 7 | Entity ID allocation | Complete | `7fbd10624` | Root-keyed allocation state now backs graph accessors; clipboard allocation uses shared helpers. | +| 8 | Graph and subgraph definitions | Complete | `8923225fb` | Root-scoped records now own ordered membership, the registry, and subgraph definition metadata. | +| 9 | Legacy node geometry projection | Complete | `854f68fb2` | The layout adapter now owns stable legacy views, synchronization, and geometry write-through. | +| 10 | Plain slot descriptors | Complete | `cc53dad291` | Plain store descriptors now back stable extension-visible slot class projections. | +| 11 | Group presentation | Complete | `3ac7b726a` | Graph-definition records now back mutable presentation fields and serialization. | +| 12 | Link non-topological state | Complete | `38b40988a` | Separate persistent and runtime records now back compatible link fields with topology lifecycle. | +| 13 | Node properties | Complete | `f75b30dbd` | Node state now owns stable mutable properties across lifecycle, callbacks, and serialization. | +| 14 | Unknown-node fallback | Complete | `ba4757f4c` | Node state now owns unknown-node fallback records behind the compatibility accessor. | +| 15 | Delayed widget restoration | Complete | `972947d939` | Store-owned restoration state now hydrates configured and delayed widgets from wire shadows. | +| 16 | Extension persistence adapter | Complete | `287bf33f38` | Validated payloads round trip through a namespace while legacy hooks mutate isolated DTO views. | +| 17 | Execution order | Complete | `7f6c2783dd` | Graph-scoped derived records own execution order behind the compatible node projection. | +| 18 | Graph invalidation | Complete | `335b69420c` | Graph-scoped batching coalesces composite invalidations behind the compatible version counter. | +| 19 | Store-driven serialization | Complete | `458d292f1` | Store authorities now build wire DTOs after normalized parity against mutable serialization. | Final full-suite verification also tightened slot-projection deletion and updated test fixtures to preserve their store scope and projected-array diff --git a/docs/architecture/ecs/ecs-extension-compatibility-audit.md b/docs/architecture/ecs/ecs-extension-compatibility-audit.md index 62dae0b77f7..ffe6998d71b 100644 --- a/docs/architecture/ecs/ecs-extension-compatibility-audit.md +++ b/docs/architecture/ecs/ecs-extension-compatibility-audit.md @@ -127,14 +127,18 @@ Derived first-party rows are recomputed on read; extension entries remain on ### Persistence hooks `node.onSerialize`, `node.onConfigure`, `graph.onSerialize`, and -`graph.onConfigure` remain compatibility surfaces, but currently expose full -node/workflow DTOs or live objects. Extensions can therefore mutate canonical -store-backed persistence fields outside store actions. -The migration must measure this usage before replacing it with a controlled -adapter for validated, namespaced plain-data payloads. This phase requires the -adapter to define serialization and configuration behavior without allowing -extension payload hooks to rewrite canonical fields. Replay and command-based -undo contracts are later architecture work. +`graph.onConfigure` remain compatibility surfaces. Serialize hooks receive a +mutable DTO view: changes to canonical fields are applied to that serialization, +while JSON-compatible noncanonical fields are persisted under `extensions`. +Configure hooks receive an isolated clone containing canonical data plus the +owner's extension payload; mutating that clone does not reconfigure the graph. + +Extensions should persist JSON-compatible data under a unique key in +`data.extensions` and read it from the same key in `onConfigure`. Legacy flat +fields are still projected into configure views, but migrate them to a +namespaced key. Do not use `onConfigure` to rewrite nodes, links, or other +canonical workflow fields; perform graph mutations through supported graph APIs +after configuration instead. ### Custom-widget constructors diff --git a/docs/architecture/ecs/ecs-migration-plan.md b/docs/architecture/ecs/ecs-migration-plan.md index 5e24ae8b2bd..3d2aa31aef3 100644 --- a/docs/architecture/ecs/ecs-migration-plan.md +++ b/docs/architecture/ecs/ecs-migration-plan.md @@ -38,22 +38,22 @@ this phase. ## Work completed -| Concern | Current result | -| -------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | -| Node shell | `nodeDataStore` owns `NodeState`. `LGraphNode` adopts the registered reactive proxy; renderer-only node mirrors and `useGraphNodeManager` were removed. | -| Link topology | `linkStore` owns `LinkTopology` by root-wide `LinkId`, with owner, target, and origin indexes. `LLink` endpoint properties are compatibility accessors over the registered state. | -| Slot connectivity | Input and output connectivity is derived from `linkStore`. `input.link` and `output.links` are deprecated read-only compatibility accessors; writes are ignored. | -| Reroute chains | `rerouteStore` owns parent/floating chain state. Link membership is derived from link parent chains rather than stored on reroutes. | -| Widget state | `widgetValueStore` owns widget values, render metadata, and root-scoped per-node order. `BaseWidget` adopts registered state. | -| Layout | Yjs-backed `layoutStore` owns persistent node, group, and reroute geometry. Entity lifecycle attaches geometry independently of renderer lifecycle, and layout writes use `LayoutOperation`. | -| Renderer integration | Vue and legacy renderers read the same persistent geometry. Vue slot endpoints reactively derive from store-owned node layout plus measured offsets. Renderer switches clear transient view geometry rather than reseeding entity layout. | -| Renderer consumers | `GraphCanvas` reads node shells from `nodeDataStore`; minimap topology reads `linkStore`; selection, arrangement, traversal, pricing, and widget consumers use store-backed topology or geometry where migrated. Live classes remain adapters for schema, callbacks, and legacy rendering. | -| Badges | Badge rows are derived by `badgeSystem`; the temporary badge store was removed. Badge data is transient and is not serialized or independently mutated. | -| Error presentation | Error stores are authoritative inputs for Vue error rendering. `node.has_errors` remains a legacy-canvas projection, including promoted-widget and container error resolution, rather than an independent persisted authority. | -| Identity | Root graph allocation and import normalization enforce unique node, link, group, and reroute IDs across nested definitions. Owner indexes retain graph-local queries and teardown. | -| Lifecycle | Graph add, configure, replace, remove, subgraph release, and clear register, transfer, or release migrated state. Typed graph lifecycle events let internal observers avoid replacing extension callback slots. Clear handles reentrant and failing removal callbacks; normal node removal can retain widget entries, and `configure(keep_old)` provides weaker cleanup. | -| Persistence | Serialization, subgraph conversion, copy/paste, and workflow insertion preserve the existing workflow format while normalizing conflicting identities. | -| Compatibility | Legacy graph, node, link, slot, widget, geometry, and callback surfaces remain available where practical. Changed behavior is documented in the extension migration references. | +| Concern | Current result | +| -------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| Node shell | `nodeDataStore` owns `NodeState`. `LGraphNode` adopts the registered reactive proxy; renderer-only node mirrors and `useGraphNodeManager` were removed. | +| Link topology | `linkStore` owns `LinkTopology` by root-wide `LinkId`, with owner, target, and origin indexes. `LLink` endpoint properties are compatibility accessors over the registered state. | +| Slot connectivity | Input and output connectivity is derived from `linkStore`. `input.link` and `output.links` are deprecated read-only compatibility accessors; writes are ignored. | +| Reroute chains | `rerouteStore` owns parent/floating chain state. Link membership is derived from link parent chains rather than stored on reroutes. | +| Widget state | `widgetValueStore` owns widget values, render metadata, and root-scoped per-node order. `BaseWidget` adopts registered state. | +| Layout | Yjs-backed `layoutStore` owns persistent node, group, and reroute geometry. Entity lifecycle attaches geometry independently of renderer lifecycle, and layout writes use `LayoutOperation`. | +| Renderer integration | Vue and legacy renderers read the same persistent geometry. Vue slot endpoints reactively derive from store-owned node layout plus measured offsets. Renderer switches clear transient view geometry rather than reseeding entity layout. | +| Renderer consumers | `GraphCanvas` reads node shells from `nodeDataStore`; minimap topology reads `linkStore`; selection, arrangement, traversal, pricing, and widget consumers use store-backed topology or geometry where migrated. Live classes remain adapters for schema, callbacks, and legacy rendering. | +| Badges | Badge rows are derived by `badgeSystem`; the temporary badge store was removed. Badge data is transient and is not serialized or independently mutated. | +| Error presentation | Error stores are authoritative inputs for Vue error rendering. `node.has_errors` remains a legacy-canvas projection, including promoted-widget and container error resolution, rather than an independent persisted authority. | +| Identity | Root graph allocation and import normalization enforce unique node, link, group, and reroute IDs across nested definitions. Owner indexes retain graph-local queries and teardown. | +| Lifecycle | Graph add, configure, replace, remove, subgraph release, and clear register, transfer, or release migrated state. Typed graph lifecycle events let internal observers avoid replacing extension callback slots. Clear handles reentrant and failing removal callbacks; failed and additive configure still have the cleanup limits described below. | +| Persistence | Serialization, subgraph conversion, copy/paste, and workflow insertion preserve the existing workflow format while normalizing conflicting identities. | +| Compatibility | Legacy graph, node, link, slot, widget, geometry, and callback surfaces remain available where practical. Changed behavior is documented in the extension migration references. | ## PR 14246 change traceability diff --git a/src/components/maskeditor/BrushCursor.vue b/src/components/maskeditor/BrushCursor.vue index f135a031fe7..2d7ca40f196 100644 --- a/src/components/maskeditor/BrushCursor.vue +++ b/src/components/maskeditor/BrushCursor.vue @@ -26,6 +26,7 @@