Line data Source code
1 : import { t } from '@/i18n'
2 : import type { ComfyNodeDefImpl } from '@/core/graph/nodeDef/ComfyNodeDefImpl'
3 : import { buildNodeDefTree } from '@/stores/nodeDefStore'
4 : import type {
5 : NodeGroupingStrategy,
6 : NodeOrganizationOptions,
7 : NodeSection,
8 : NodeSortStrategy
9 : } from '@/types/nodeOrganizationTypes'
10 : import { NodeSourceType } from '@/types/nodeSource'
11 : import type { TreeNode } from '@/types/treeExplorerTypes'
12 : import { sortedTree, unwrapTreeRoot } from '@/utils/treeUtil'
13 :
14 2355 : const DEFAULT_ICON = 'icon-[lucide--arrow-down-up]'
15 :
16 4621219 : function categoryPathExtractor(nodeDef: ComfyNodeDefImpl): string[] {
17 4621219 : const category = nodeDef.category || ''
18 : const categoryParts = category ? category.split('/').filter(Boolean) : []
19 4621219 : return [...categoryParts, nodeDef.name]
20 : }
21 :
22 265840 : function isBlueprint(node: ComfyNodeDefImpl): boolean {
23 265840 : return (
24 : node.nodeSource.type === NodeSourceType.Blueprint ||
25 : node.python_module.startsWith('blueprint')
26 : )
27 : }
28 :
29 2355 : export const DEFAULT_GROUPING_ID = 'category' as const
30 2355 : export const DEFAULT_SORTING_ID = 'original' as const
31 : export const DEFAULT_TAB_ID = 'all' as const
32 :
33 2355 : class NodeOrganizationService {
34 : private readonly groupingStrategies: NodeGroupingStrategy[] = [
35 : {
36 : id: 'category',
37 : label: 'sideToolbar.nodeLibraryTab.groupStrategies.category',
38 : icon: 'icon-[lucide--folder]',
39 : description: 'sideToolbar.nodeLibraryTab.groupStrategies.categoryDesc',
40 : getNodePath: categoryPathExtractor
41 : },
42 : {
43 : id: 'module',
44 : label: 'sideToolbar.nodeLibraryTab.groupStrategies.module',
45 : icon: 'icon-[lucide--box]',
46 : description: 'sideToolbar.nodeLibraryTab.groupStrategies.moduleDesc',
47 : getNodePath: (nodeDef: ComfyNodeDefImpl) => {
48 0 : const pythonModule = nodeDef.python_module || ''
49 :
50 0 : if (!pythonModule) {
51 0 : return ['unknown_module', nodeDef.name]
52 : }
53 :
54 : // Split the module path into components
55 0 : const parts = pythonModule.split('.')
56 :
57 : // Remove common prefixes and organize
58 0 : if (parts[0] === 'nodes') {
59 : // Core nodes - just use 'core'
60 0 : return ['core', nodeDef.name]
61 0 : } else if (parts[0] === 'custom_nodes') {
62 : // Custom nodes - use the package name as the folder
63 0 : if (parts.length > 1) {
64 : // Return the custom node package name
65 0 : return [parts[1], nodeDef.name]
66 : }
67 0 : return ['custom_nodes', nodeDef.name]
68 : }
69 :
70 : // For other modules, use the full path structure plus node name
71 0 : return [...parts, nodeDef.name]
72 : }
73 : },
74 : {
75 : id: 'source',
76 : label: 'sideToolbar.nodeLibraryTab.groupStrategies.source',
77 : icon: 'icon-[lucide--server]',
78 : description: 'sideToolbar.nodeLibraryTab.groupStrategies.sourceDesc',
79 : getNodePath: (nodeDef: ComfyNodeDefImpl) => {
80 0 : if (nodeDef.api_node) {
81 0 : return ['API nodes', nodeDef.name]
82 0 : } else if (nodeDef.nodeSource.type === NodeSourceType.Core) {
83 0 : return ['Core', nodeDef.name]
84 0 : } else if (nodeDef.nodeSource.type === NodeSourceType.CustomNodes) {
85 0 : return ['Custom nodes', nodeDef.name]
86 : } else {
87 0 : return ['Unknown', nodeDef.name]
88 : }
89 : }
90 : }
91 : ]
92 :
93 : private readonly sortingStrategies: NodeSortStrategy[] = [
94 : {
95 : id: 'original',
96 : label: 'sideToolbar.nodeLibraryTab.sortBy.original',
97 : icon: 'icon-[lucide--arrow-down-up]',
98 : description: 'sideToolbar.nodeLibraryTab.sortBy.originalDesc',
99 : compare: () => 0
100 : },
101 : {
102 : id: 'alphabetical',
103 : label: 'sideToolbar.nodeLibraryTab.sortBy.alphabetical',
104 : icon: 'icon-[lucide--arrow-down-a-z]',
105 : description: 'sideToolbar.nodeLibraryTab.sortBy.alphabeticalDesc',
106 : compare: (a: ComfyNodeDefImpl, b: ComfyNodeDefImpl) =>
107 : a.display_name.localeCompare(b.display_name)
108 : }
109 : ]
110 :
111 : getGroupingStrategies(): NodeGroupingStrategy[] {
112 0 : return [...this.groupingStrategies]
113 : }
114 :
115 : getGroupingStrategy(id: string): NodeGroupingStrategy | undefined {
116 4206 : return this.groupingStrategies.find((strategy) => strategy.id === id)
117 : }
118 :
119 : getSortingStrategies(): NodeSortStrategy[] {
120 1 : return [...this.sortingStrategies]
121 : }
122 :
123 : getSortingStrategy(id: string): NodeSortStrategy | undefined {
124 4206 : return this.sortingStrategies.find((strategy) => strategy.id === id)
125 : }
126 :
127 : organizeNodesTab(nodes: ComfyNodeDefImpl[]): NodeSection[] {
128 252 : const {
129 : myBlueprints,
130 : comfyBlueprints,
131 : partnerNodes,
132 : comfyNodes,
133 : extensions
134 : } = this.classifyNodes(nodes)
135 :
136 252 : const blueprintTree = this.buildBlueprintTree(
137 : myBlueprints,
138 : comfyBlueprints,
139 : categoryPathExtractor
140 : )
141 :
142 252 : const sections: NodeSection[] = []
143 :
144 252 : if (blueprintTree.children?.length) {
145 249 : sections.push({ category: 'blueprints', tree: blueprintTree })
146 : }
147 252 : if (comfyNodes.length > 0) {
148 251 : sections.push({
149 : category: 'comfyNodes',
150 : tree: buildNodeDefTree(comfyNodes, {
151 : pathExtractor: categoryPathExtractor
152 : })
153 : })
154 : }
155 252 : if (partnerNodes.length > 0) {
156 248 : sections.push({
157 : category: 'partnerNodes',
158 : tree: unwrapTreeRoot(
159 : buildNodeDefTree(partnerNodes, {
160 : pathExtractor: categoryPathExtractor
161 : })
162 : )
163 : })
164 : }
165 252 : if (extensions.length > 0) {
166 248 : sections.push({
167 : category: 'extensions',
168 : tree: buildNodeDefTree(extensions, {
169 : pathExtractor: categoryPathExtractor
170 : })
171 : })
172 : }
173 :
174 252 : return sections
175 : }
176 :
177 : private classifyNodes(nodes: ComfyNodeDefImpl[]): {
178 : myBlueprints: ComfyNodeDefImpl[]
179 : comfyBlueprints: ComfyNodeDefImpl[]
180 : partnerNodes: ComfyNodeDefImpl[]
181 : comfyNodes: ComfyNodeDefImpl[]
182 : extensions: ComfyNodeDefImpl[]
183 : } {
184 252 : const myBlueprints: ComfyNodeDefImpl[] = []
185 252 : const comfyBlueprints: ComfyNodeDefImpl[] = []
186 252 : const partnerNodes: ComfyNodeDefImpl[] = []
187 252 : const comfyNodes: ComfyNodeDefImpl[] = []
188 252 : const extensions: ComfyNodeDefImpl[] = []
189 :
190 252 : for (const node of nodes) {
191 265840 : if (isBlueprint(node)) {
192 23023 : if (node.isGlobal) comfyBlueprints.push(node)
193 11 : else myBlueprints.push(node)
194 242817 : } else if (node.api_node || node.category.startsWith('api node')) {
195 65472 : partnerNodes.push(node)
196 177345 : } else if (
197 : node.nodeSource.type === NodeSourceType.Core ||
198 : node.nodeSource.type === NodeSourceType.Essentials
199 : ) {
200 167425 : comfyNodes.push(node)
201 : } else {
202 9920 : extensions.push(node)
203 : }
204 : }
205 :
206 252 : return {
207 : myBlueprints,
208 : comfyBlueprints,
209 : partnerNodes,
210 : comfyNodes,
211 : extensions
212 : }
213 : }
214 :
215 : private buildBlueprintTree(
216 : myBlueprints: ComfyNodeDefImpl[],
217 : comfyBlueprints: ComfyNodeDefImpl[],
218 : pathExtractor: (nodeDef: ComfyNodeDefImpl) => string[]
219 : ): TreeNode<ComfyNodeDefImpl> {
220 252 : const children: TreeNode<ComfyNodeDefImpl>[] = []
221 252 : if (myBlueprints.length > 0) {
222 11 : const tree = unwrapTreeRoot(
223 : buildNodeDefTree(myBlueprints, { pathExtractor })
224 : )
225 11 : children.push({
226 : key: 'root/my-blueprints',
227 : label: t('sideToolbar.nodeLibraryTab.sections.myBlueprints'),
228 : children: tree.children
229 : })
230 : }
231 252 : if (comfyBlueprints.length > 0) {
232 248 : const tree = unwrapTreeRoot(
233 : buildNodeDefTree(comfyBlueprints, { pathExtractor })
234 : )
235 248 : children.push({
236 : key: 'root/comfy-blueprints',
237 : label: t('sideToolbar.nodeLibraryTab.sections.comfyBlueprints'),
238 : children: tree.children
239 : })
240 : }
241 252 : return { key: 'root', label: '', children }
242 : }
243 :
244 : organizeNodes(
245 : nodes: ComfyNodeDefImpl[],
246 : options: NodeOrganizationOptions = {}
247 : ): TreeNode<ComfyNodeDefImpl> {
248 4151 : const { groupBy = DEFAULT_GROUPING_ID, sortBy = DEFAULT_SORTING_ID } =
249 : options
250 :
251 4151 : const groupingStrategy = this.getGroupingStrategy(groupBy)
252 4151 : const sortingStrategy = this.getSortingStrategy(sortBy)
253 :
254 4151 : if (!groupingStrategy) {
255 0 : throw new Error(`Unknown grouping strategy: ${groupBy}`)
256 : }
257 :
258 4151 : if (!sortingStrategy) {
259 0 : throw new Error(`Unknown sorting strategy: ${sortBy}`)
260 : }
261 :
262 4151 : const sortedNodes =
263 : sortingStrategy.id === 'original'
264 : ? nodes
265 : : [...nodes].sort(sortingStrategy.compare)
266 :
267 4151 : const tree = buildNodeDefTree(sortedNodes, {
268 : pathExtractor: groupingStrategy.getNodePath
269 : })
270 :
271 4151 : if (sortBy === 'alphabetical') {
272 0 : return sortedTree(tree, { groupLeaf: true })
273 : }
274 :
275 4151 : return tree
276 : }
277 :
278 : getGroupingIcon(groupingId: string): string {
279 : const strategy = this.getGroupingStrategy(groupingId)
280 55 : return strategy?.icon || DEFAULT_ICON
281 : }
282 :
283 : getSortingIcon(sortingId: string): string {
284 : const strategy = this.getSortingStrategy(sortingId)
285 55 : return strategy?.icon || DEFAULT_ICON
286 : }
287 : }
288 :
289 2355 : export const nodeOrganizationService = new NodeOrganizationService()
|