Line data Source code
1 : import type { FuseSearchOptions } from 'fuse.js'
2 :
3 : import type { ComfyNodeDefImpl } from '@/core/graph/nodeDef/ComfyNodeDefImpl'
4 : import type { FuseFilterWithValue } from '@/utils/fuseUtil'
5 : import { FuseFilter, FuseSearch } from '@/utils/fuseUtil'
6 :
7 2356 : export class NodeSearchService {
8 : public readonly nodeFuseSearch: FuseSearch<ComfyNodeDefImpl>
9 : public readonly inputTypeFilter: FuseFilter<ComfyNodeDefImpl>
10 : public readonly outputTypeFilter: FuseFilter<ComfyNodeDefImpl>
11 : public readonly nodeCategoryFilter: FuseFilter<ComfyNodeDefImpl>
12 : public readonly nodeSourceFilter: FuseFilter<ComfyNodeDefImpl>
13 :
14 : constructor(data: ComfyNodeDefImpl[]) {
15 5963 : this.nodeFuseSearch = new FuseSearch(data, {
16 : fuseOptions: {
17 : keys: ['name', 'display_name', 'search_aliases'],
18 : includeScore: true,
19 : threshold: 0.3,
20 : shouldSort: false,
21 : useExtendedSearch: true
22 : },
23 : createIndex: true,
24 : advancedScoring: true
25 : })
26 :
27 5963 : const fuseOptions = {
28 : includeScore: true,
29 : threshold: 0.3,
30 : shouldSort: true
31 : }
32 :
33 5963 : this.inputTypeFilter = new FuseFilter<ComfyNodeDefImpl>(data, {
34 : id: 'input',
35 : name: 'Input Type',
36 : invokeSequence: 'i',
37 : getItemOptions: (node) => node.inputTypes,
38 : fuseOptions
39 : })
40 :
41 5963 : this.outputTypeFilter = new FuseFilter<ComfyNodeDefImpl>(data, {
42 : id: 'output',
43 : name: 'Output Type',
44 : invokeSequence: 'o',
45 : getItemOptions: (node) => node.outputTypes,
46 : fuseOptions
47 : })
48 :
49 5963 : this.nodeCategoryFilter = new FuseFilter<ComfyNodeDefImpl>(data, {
50 : id: 'category',
51 : name: 'Category',
52 : invokeSequence: 'c',
53 : getItemOptions: (node) => [node.category],
54 : fuseOptions
55 : })
56 :
57 5963 : this.nodeSourceFilter = new FuseFilter<ComfyNodeDefImpl>(data, {
58 : id: 'source',
59 : name: 'Source',
60 : invokeSequence: 's',
61 : getItemOptions: (node) => [node.nodeSource.displayText],
62 : fuseOptions
63 : })
64 : }
65 :
66 : public searchNode(
67 : query: string,
68 : filters: FuseFilterWithValue<ComfyNodeDefImpl>[] = [],
69 : options?: FuseSearchOptions,
70 : extraOptions: {
71 : matchWildcards?: boolean
72 : } = {}
73 : ): ComfyNodeDefImpl[] {
74 2849 : const { matchWildcards = true } = extraOptions
75 2849 : const wildcard = matchWildcards ? '*' : undefined
76 2849 : const matchedNodes = this.nodeFuseSearch.search(query)
77 :
78 2849 : const results = matchedNodes.filter((node) => {
79 2502496 : return filters.every((filterAndValue) => {
80 165674 : const { filterDef, value } = filterAndValue
81 165674 : return filterDef.matches(node, value)
82 : })
83 : })
84 2849 : if (matchWildcards) {
85 2842 : const alreadyValid = new Set(results.map((result) => result.name))
86 2842 : results.push(
87 : ...matchedNodes
88 : .filter((node) => !alreadyValid.has(node.name))
89 : .filter((node) => {
90 155872 : return filters.every((filterAndValue) => {
91 156033 : const { filterDef, value } = filterAndValue
92 156033 : return filterDef.matches(node, value, { wildcard })
93 : })
94 : })
95 : )
96 : }
97 :
98 2849 : return options?.limit ? results.slice(0, options.limit) : results
99 : }
100 :
101 : get nodeFilters(): FuseFilter<ComfyNodeDefImpl>[] {
102 117 : return [
103 : this.inputTypeFilter,
104 : this.outputTypeFilter,
105 : this.nodeCategoryFilter,
106 : this.nodeSourceFilter
107 : ]
108 : }
109 : }
|