Line data Source code
1 : import QuickLRU from '@alloc/quick-lru'
2 : import { partition } from 'es-toolkit/compat'
3 : import { defineStore } from 'pinia'
4 :
5 : import { useCachedRequest } from '@/composables/useCachedRequest'
6 : import { useComfyRegistryService } from '@/services/comfyRegistryService'
7 : import type { components, operations } from '@/types/comfyRegistryTypes'
8 :
9 2356 : const PACK_LIST_CACHE_SIZE = 20
10 2356 : const PACK_BY_ID_CACHE_SIZE = 64
11 :
12 : type NodePack = components['schemas']['Node']
13 : type ListPacksParams = operations['listAllNodes']['parameters']['query']
14 : type ListPacksResult =
15 : operations['listAllNodes']['responses'][200]['content']['application/json']
16 : type GetNodeDefsParams = operations['ListComfyNodes']['parameters']['query'] & {
17 : packId: components['schemas']['Node']['id']
18 : version: components['schemas']['NodeVersion']['version']
19 : }
20 : type GetPackByIdPath = operations['getNode']['parameters']['path']['nodeId']
21 :
22 2356 : const isNodePack = (pack: NodePack | undefined): pack is NodePack => {
23 58 : return pack !== undefined && 'id' in pack
24 : }
25 :
26 : /**
27 : * Store for managing remote custom nodes
28 : */
29 2356 : export const useComfyRegistryStore = defineStore('comfyRegistry', () => {
30 2355 : const registryService = useComfyRegistryService()
31 :
32 2355 : let getPacksByIdController: AbortController | null = null
33 2355 : const getPacksByIdCache = new QuickLRU<NodePack['id'], NodePack>({
34 : maxSize: PACK_BY_ID_CACHE_SIZE
35 : })
36 :
37 : /**
38 : * Get a list of all node packs from the registry
39 : */
40 2355 : const listAllPacks = useCachedRequest<ListPacksParams, ListPacksResult>(
41 : registryService.listAllPacks,
42 : { maxSize: PACK_LIST_CACHE_SIZE }
43 : )
44 :
45 : /**
46 : * Get a pack by its ID from the registry
47 : */
48 2355 : const getPackById = useCachedRequest<GetPackByIdPath, NodePack>(
49 : async (params) => {
50 3 : if (!params) return null
51 3 : return registryService.getPackById(params)
52 : },
53 : { maxSize: PACK_BY_ID_CACHE_SIZE }
54 : )
55 :
56 : /**
57 : * Get a list of packs by their IDs from the registry
58 : */
59 2355 : const getPacksByIds = async (ids: NodePack['id'][]): Promise<NodePack[]> => {
60 148 : const [cachedPacksIds, uncachedPacksIds] = partition(ids, (id) =>
61 : getPacksByIdCache.has(id)
62 : )
63 :
64 148 : const resolvedPacks = cachedPacksIds
65 : .map((id) => getPacksByIdCache.get(id))
66 : .filter(isNodePack)
67 :
68 148 : if (uncachedPacksIds.length) {
69 107 : getPacksByIdController = new AbortController()
70 : const uncachedPacks = await registryService.listAllPacks(
71 : {
72 : node_id: uncachedPacksIds.filter(
73 : (id): id is string => id !== undefined
74 : )
75 : },
76 : getPacksByIdController.signal
77 : )
78 :
79 107 : const { nodes = [] } = uncachedPacks ?? {}
80 107 : nodes.forEach((pack) => {
81 214 : if (pack.id) {
82 214 : getPacksByIdCache.set(pack.id, pack)
83 214 : resolvedPacks.push(pack)
84 : }
85 : })
86 : }
87 :
88 148 : return resolvedPacks
89 : }
90 :
91 : /**
92 : * Get the node definitions for a pack
93 : */
94 2355 : const getNodeDefs = useCachedRequest<
95 : GetNodeDefsParams,
96 : operations['ListComfyNodes']['responses'][200]['content']['application/json']
97 : >(registryService.getNodeDefs, { maxSize: PACK_BY_ID_CACHE_SIZE })
98 :
99 : /**
100 : * Search for packs by pack name or node names
101 : */
102 2355 : const search = useCachedRequest<
103 : operations['searchNodes']['parameters']['query'],
104 : ListPacksResult
105 : >(registryService.search, { maxSize: PACK_LIST_CACHE_SIZE })
106 :
107 : /**
108 : * Get the node pack that contains a specific ComfyUI node by its name.
109 : * Results are cached to avoid redundant API calls.
110 : *
111 : * @see {@link useComfyRegistryService.inferPackFromNodeName} for details on the ranking algorithm
112 : */
113 2355 : const inferPackFromNodeName = useCachedRequest<
114 : operations['getNodeByComfyNodeName']['parameters']['path']['comfyNodeName'],
115 : NodePack
116 : >(registryService.inferPackFromNodeName, { maxSize: PACK_BY_ID_CACHE_SIZE })
117 :
118 : /**
119 : * Clear all cached data
120 : */
121 2355 : const clearCache = () => {
122 0 : getNodeDefs.clear()
123 0 : listAllPacks.clear()
124 0 : getPackById.clear()
125 0 : inferPackFromNodeName.clear()
126 : }
127 :
128 : /**
129 : * Cancel all any in-flight requests.
130 : */
131 2355 : const cancelRequests = () => {
132 0 : getNodeDefs.cancel()
133 0 : listAllPacks.cancel()
134 0 : getPackById.cancel()
135 0 : inferPackFromNodeName.cancel()
136 0 : getPacksByIdController?.abort()
137 : }
138 :
139 2355 : return {
140 : listAllPacks,
141 : getPackById,
142 : getPacksByIds: {
143 : call: getPacksByIds,
144 : cancel: () => getPacksByIdController?.abort()
145 : },
146 : getNodeDefs,
147 : search,
148 : inferPackFromNodeName,
149 :
150 : clearCache,
151 : cancelRequests,
152 :
153 : isLoading: registryService.isLoading,
154 : error: registryService.error
155 : }
156 : })
|