Line data Source code
1 : import { cloneDeep, groupBy, isEqual, omit } from 'es-toolkit'
2 :
3 : import { LiteGraph } from '@/lib/litegraph/src/litegraph'
4 : import type { LGraph } from '@/lib/litegraph/src/litegraph'
5 : import type { IBaseWidget } from '@comfyorg/graph-model/widgets'
6 : import {
7 : collectSubgraphDefinitions,
8 : parseFlattenableSubgraphDefinitions
9 : } from '@/platform/workflow/core/utils/workflowFlattening'
10 : import type { ComfyWorkflowJSON } from '@comfyorg/graph-model/workflowSchema'
11 : import { ownsNodeHistory } from '@/stores/widgetHistoryStore'
12 : import { parseWidgetId, widgetId } from '@comfyorg/graph-model/widgetId'
13 : import type { WidgetId } from '@comfyorg/graph-model/widgetId'
14 :
15 : interface SerializedWidgetValues {
16 : id?: unknown
17 : widgets_values?: unknown
18 : widgets_values_named?: unknown
19 : }
20 :
21 179 : function isRecord(value: unknown): value is Record<string, unknown> {
22 179 : return typeof value === 'object' && value !== null && !Array.isArray(value)
23 : }
24 :
25 6795 : function nodeHistoryOwnedWidgets(widgets: readonly IBaseWidget[]) {
26 6795 : const owned = widgets.filter(ownsNodeHistory)
27 6795 : const mirrored = LiteGraph.vueNodesMode
28 : ? owned.flatMap((widget) => widget.linkedWidgets ?? [])
29 : : []
30 6795 : return new Set([...owned, ...mirrored])
31 : }
32 :
33 1531 : export function collectNodeHistoryWidgets(
34 : rootGraph: LGraph
35 : ): Map<WidgetId, number> {
36 1531 : return new Map(
37 : [rootGraph, ...rootGraph.subgraphs.values()].flatMap((graph) =>
38 : graph.nodes.flatMap((node) => {
39 6795 : const widgets = node.widgets ?? []
40 6795 : const owned = nodeHistoryOwnedWidgets(widgets)
41 6795 : return widgets
42 : .filter((widget) => widget.serialize !== false)
43 : .flatMap((widget, slot): [WidgetId, number][] =>
44 : owned.has(widget)
45 : ? [[widgetId(graph.id, node.id, widget.name), slot]]
46 : : []
47 : )
48 : })
49 : )
50 : )
51 : }
52 :
53 319 : function nodeKey(graphId: string, nodeId: unknown): string {
54 319 : return `${graphId}:${String(nodeId)}`
55 : }
56 :
57 88 : function serializedNodes(
58 : state: ComfyWorkflowJSON,
59 : rootGraphId: string
60 : ): Map<string, SerializedWidgetValues> {
61 88 : const subgraphs = collectSubgraphDefinitions(
62 : parseFlattenableSubgraphDefinitions(state.definitions?.subgraphs ?? [])
63 : )
64 88 : const graphs: [string, readonly SerializedWidgetValues[]][] = [
65 : [rootGraphId, state.nodes],
66 : ...subgraphs.map(
67 : (subgraph): [string, readonly SerializedWidgetValues[]] => [
68 : subgraph.id,
69 : subgraph.nodes
70 : ]
71 : )
72 : ]
73 88 : return new Map(
74 : graphs.flatMap(([graphId, nodes]) =>
75 : nodes.map((node): [string, SerializedWidgetValues] => [
76 : nodeKey(graphId, node.id),
77 : node
78 : ])
79 : )
80 : )
81 : }
82 :
83 179 : function namedValues(
84 : node: SerializedWidgetValues
85 : ): Record<string, unknown> | undefined {
86 179 : return isRecord(node.widgets_values_named)
87 : ? node.widgets_values_named
88 : : undefined
89 : }
90 :
91 50 : function serializedWidgetValue(
92 : nodes: Map<string, SerializedWidgetValues>,
93 : widget: WidgetId
94 : ): unknown {
95 50 : const { graphId, nodeId, name } = parseWidgetId(widget)
96 50 : const node = nodes.get(nodeKey(graphId, nodeId))
97 50 : return node && namedValues(node)?.[name]
98 : }
99 :
100 19 : function slotCount(node: SerializedWidgetValues): number | undefined {
101 19 : return Array.isArray(node.widgets_values)
102 : ? node.widgets_values.length
103 : : undefined
104 : }
105 :
106 91 : function writeWidgetValue(
107 : node: SerializedWidgetValues,
108 : {
109 : name,
110 : slot,
111 : liveSlotCount
112 : }: { name: string; slot: number; liveSlotCount?: number },
113 : value: unknown
114 : ): void {
115 91 : const named = namedValues(node)
116 91 : const positional = node.widgets_values
117 91 : if (!named || !(name in named) || !Array.isArray(positional)) return
118 91 : if (positional.length !== liveSlotCount) return
119 91 : named[name] = cloneDeep(value)
120 91 : positional[slot] = cloneDeep(value)
121 : }
122 :
123 25 : export function nodeHistoryValuesChanged(
124 : previous: ComfyWorkflowJSON,
125 : current: ComfyWorkflowJSON,
126 : widgets: ReadonlyMap<WidgetId, number>,
127 : rootGraphId: string
128 : ): boolean {
129 25 : const previousNodes = serializedNodes(previous, rootGraphId)
130 25 : const currentNodes = serializedNodes(current, rootGraphId)
131 25 : return [...widgets.keys()].some(
132 : (widget) =>
133 : !isEqual(
134 : serializedWidgetValue(previousNodes, widget),
135 : serializedWidgetValue(currentNodes, widget)
136 : )
137 : )
138 : }
139 :
140 19 : export function withNodeHistoryValuesFrom(
141 : target: ComfyWorkflowJSON,
142 : source: ComfyWorkflowJSON,
143 : widgets: ReadonlyMap<WidgetId, number>,
144 : rootGraphId: string
145 : ): ComfyWorkflowJSON {
146 19 : if (!widgets.size) return target
147 19 : const result = cloneDeep(target)
148 19 : const targetNodes = serializedNodes(result, rootGraphId)
149 19 : const sourceNodes = serializedNodes(source, rootGraphId)
150 19 : const ownedByNode = groupBy(
151 : [...widgets].map(([widget, slot]) => ({ ...parseWidgetId(widget), slot })),
152 : ({ graphId, nodeId }) => nodeKey(graphId, nodeId)
153 : )
154 :
155 19 : for (const [key, nodeWidgets] of Object.entries(ownedByNode)) {
156 19 : const targetNode = targetNodes.get(key)
157 19 : const sourceNode = sourceNodes.get(key)
158 19 : const sourceValues = sourceNode && namedValues(sourceNode)
159 19 : if (!targetNode || !sourceNode || !sourceValues) continue
160 :
161 19 : const owned = nodeWidgets.map(({ name }) => name)
162 19 : if (
163 : !isEqual(
164 : omit(namedValues(targetNode) ?? {}, owned),
165 : omit(sourceValues, owned)
166 : )
167 : ) {
168 0 : continue
169 : }
170 19 : const liveSlotCount = slotCount(sourceNode)
171 19 : for (const { name, slot } of nodeWidgets) {
172 91 : if (name in sourceValues)
173 91 : writeWidgetValue(
174 : targetNode,
175 : { name, slot, liveSlotCount },
176 : sourceValues[name]
177 : )
178 : }
179 : }
180 19 : return result
181 : }
|