Line data Source code
1 : import type {
2 : ExportedSubgraph,
3 : ExportedSubgraphInstance,
4 : Subgraph
5 : } from '@/lib/litegraph/src/litegraph'
6 : import { normalizeSubgraphDefinitionIds } from '@/lib/litegraph/src/subgraph/subgraphDeduplication'
7 : import type { ComfyWorkflowJSON } from '@comfyorg/graph-model/workflowSchema'
8 : import type { ComfyNodeDef as ComfyNodeDefV1 } from '@comfyorg/object-info-parser'
9 : import { useApp } from '@/scripts/appInstance'
10 : import { useNodeDefStore } from '@/stores/nodeDefStore'
11 :
12 : import { useLitegraphService } from './litegraphService'
13 :
14 2356 : export const useSubgraphService = () => {
15 4654 : const nodeDefStore = useNodeDefStore()
16 :
17 : /** Loads a single subgraph definition and registers it with the node def store */
18 1123 : function registerLitegraphNode(
19 : nodeDef: ComfyNodeDefV1,
20 : subgraph: Subgraph,
21 : exportedSubgraph: ExportedSubgraph
22 : ) {
23 1123 : const instanceData: ExportedSubgraphInstance = {
24 : id: -1,
25 : type: exportedSubgraph.id,
26 : pos: [0, 0],
27 : size: [100, 100],
28 : inputs: [],
29 : outputs: [],
30 : flags: {},
31 : order: 0,
32 : mode: 0
33 : }
34 :
35 1123 : useLitegraphService().registerSubgraphNodeDef(
36 : nodeDef,
37 : subgraph,
38 : instanceData
39 : )
40 : }
41 :
42 1123 : function createNodeDef(exportedSubgraph: ExportedSubgraph) {
43 1123 : const { id, name } = exportedSubgraph
44 :
45 1123 : const nodeDef: ComfyNodeDefV1 = {
46 : input: { required: {} },
47 : output: [],
48 : output_is_list: [],
49 : output_name: [],
50 : output_tooltips: [],
51 : name: id,
52 : display_name: name,
53 : description: exportedSubgraph.description || `Subgraph node for ${name}`,
54 : category: 'subgraph',
55 : output_node: false,
56 : python_module: 'nodes'
57 : }
58 :
59 1123 : nodeDefStore.addNodeDef(nodeDef)
60 1123 : return nodeDef
61 : }
62 :
63 : /** Loads all exported subgraph definitions from workflow */
64 3887 : function loadSubgraphs(graphData: ComfyWorkflowJSON) {
65 3887 : const subgraphs = graphData.definitions?.subgraphs
66 3887 : if (!subgraphs) return
67 :
68 273 : const normalized = normalizeSubgraphDefinitionIds(
69 : subgraphs,
70 : graphData.nodes
71 : )
72 273 : graphData.definitions!.subgraphs = normalized.subgraphs
73 273 : if (normalized.rootNodes) graphData.nodes = normalized.rootNodes
74 : // Assertion: overriding Zod schema
75 273 : const exportedSubgraphs = normalized.subgraphs as ExportedSubgraph[]
76 273 : const missingSubgraphs = exportedSubgraphs.filter(
77 : ({ id }) => !useApp().rootGraph.subgraphs.has(id)
78 : )
79 273 : const createdSubgraphs =
80 : useApp().rootGraph.createSubgraphs(missingSubgraphs)
81 273 : const loadedSubgraphs = new Map(useApp().rootGraph.subgraphs)
82 3755 : for (const [index, data] of missingSubgraphs.entries()) {
83 339 : loadedSubgraphs.set(data.id, createdSubgraphs[index])
84 : }
85 :
86 321 : for (const subgraphData of exportedSubgraphs) {
87 356 : const subgraph = loadedSubgraphs.get(subgraphData.id)
88 356 : if (!subgraph) continue
89 :
90 356 : registerNewSubgraph(subgraph, subgraphData)
91 : }
92 : }
93 :
94 : /** Registers a new subgraph (e.g. user converted from nodes) */
95 1123 : function registerNewSubgraph(
96 : subgraph: Subgraph,
97 : exportedSubgraph: ExportedSubgraph
98 : ) {
99 : const nodeDef = createNodeDef(exportedSubgraph)
100 1123 : registerLitegraphNode(nodeDef, subgraph, exportedSubgraph)
101 : }
102 :
103 4654 : return {
104 : loadSubgraphs,
105 : registerNewSubgraph
106 : }
107 : }
|