Line data Source code
1 : import QuickLRU from '@alloc/quick-lru'
2 : import type {
3 : SearchQuery,
4 : SearchResponse
5 : } from 'algoliasearch/dist/lite/browser'
6 : import { liteClient as algoliasearch } from 'algoliasearch/dist/lite/builds/browser'
7 : import { uniqBy } from 'es-toolkit'
8 : import { memoize } from 'es-toolkit/compat'
9 : import { z } from 'zod'
10 : import { fromZodError } from 'zod-validation-error'
11 :
12 : import {
13 : MIN_CHARS_FOR_SUGGESTIONS_ALGOLIA,
14 : SEARCH_CACHE_MAX_SIZE,
15 : USER_SEARCH_ANALYTICS_TAG
16 : } from '@/constants/searchConstants'
17 : import { reportError } from '@comfyorg/telemetry/reportError'
18 : import { buildPackSearchRequest } from '@/services/providers/algoliaPackSearchRequest'
19 : import type {
20 : AlgoliaNodePack,
21 : NodesIndexSuggestion,
22 : SearchNodePacksParams
23 : } from '@/types/algoliaTypes'
24 : import type { components } from '@/types/comfyRegistryTypes'
25 : import type {
26 : NodePackSearchProvider,
27 : SearchPacksOutcome,
28 : SearchPacksResult
29 : } from '@/types/searchServiceTypes'
30 : import { paramsToCacheKey } from '@/utils/formatUtil'
31 : import { buildPackSearchFallbacks } from '@/utils/searchQueryUtil'
32 :
33 : type RegistryNodePack = components['schemas']['Node']
34 :
35 2355 : const REWRITE_ANALYTICS_TAG = 'rewrite'
36 :
37 2355 : const toRewriteParams = (
38 : params: SearchNodePacksParams
39 : ): SearchNodePacksParams => ({
40 : ...params,
41 : analyticsTags: [
42 : ...(params.analyticsTags ?? []).filter(
43 : (tag) => tag !== USER_SEARCH_ANALYTICS_TAG
44 : ),
45 : REWRITE_ANALYTICS_TAG
46 : ]
47 : })
48 :
49 2355 : const zPackSearchResult = z.looseObject({
50 : hits: z.array(
51 : z
52 : .looseObject({
53 : id: z.string().nullish(),
54 : objectID: z.string().optional()
55 : })
56 : .refine((hit) => (hit.id ?? hit.objectID) !== undefined, {
57 : message: 'expected an id or objectID'
58 : })
59 : ),
60 : nbPages: z.number().optional()
61 : })
62 :
63 2355 : const zSuggestionSearchResult = z.looseObject({
64 : hits: z.array(z.looseObject({ query: z.string(), popularity: z.number() }))
65 : })
66 :
67 2355 : const zPackSearchResults = z.tuple([zPackSearchResult], zSuggestionSearchResult)
68 :
69 2355 : const zFallbackSearchResults = z.array(zPackSearchResult)
70 :
71 2355 : const searchPacksCache = new QuickLRU<string, SearchPacksResult>({
72 : maxSize: SEARCH_CACHE_MAX_SIZE
73 : })
74 :
75 2355 : const toRegistryLatestVersion = (
76 : algoliaNode: AlgoliaNodePack
77 : ): RegistryNodePack['latest_version'] => {
78 0 : return {
79 : version: algoliaNode.latest_version,
80 : createdAt: algoliaNode.update_time,
81 : status: algoliaNode.latest_version_status,
82 : comfy_node_extract_status:
83 : algoliaNode.comfy_node_extract_status ?? undefined
84 : }
85 : }
86 :
87 2355 : const toRegistryPublisher = (
88 : algoliaNode: AlgoliaNodePack
89 : ): RegistryNodePack['publisher'] => {
90 0 : return {
91 : id: algoliaNode.publisher_id,
92 : name: algoliaNode.publisher_id
93 : }
94 : }
95 :
96 : /**
97 : * Convert from node pack in Algolia format to Comfy Registry format
98 : */
99 2355 : const toRegistryPack = memoize(
100 : (
101 : algoliaNode: AlgoliaNodePack
102 : ): RegistryNodePack & { comfy_nodes: string[] } => {
103 0 : return {
104 : id: algoliaNode.id ?? algoliaNode.objectID,
105 : name: algoliaNode.name,
106 : description: algoliaNode.description,
107 : repository: algoliaNode.repository_url,
108 : license: algoliaNode.license,
109 : downloads: algoliaNode.total_install,
110 : status: algoliaNode.status,
111 : icon: algoliaNode.icon_url,
112 : latest_version: toRegistryLatestVersion(algoliaNode),
113 : publisher: toRegistryPublisher(algoliaNode),
114 : created_at: algoliaNode.create_time,
115 : category: algoliaNode.category,
116 : author: algoliaNode.author,
117 : tags: algoliaNode.tags,
118 : github_stars: algoliaNode.github_stars,
119 : supported_os: algoliaNode.supported_os,
120 : supported_comfyui_version: algoliaNode.supported_comfyui_version,
121 : supported_comfyui_frontend_version:
122 : algoliaNode.supported_comfyui_frontend_version,
123 : supported_accelerators: algoliaNode.supported_accelerators,
124 : banner_url: algoliaNode.banner_url,
125 : comfy_nodes: algoliaNode.comfy_nodes
126 : }
127 : },
128 : (algoliaNode: AlgoliaNodePack) => algoliaNode.id ?? algoliaNode.objectID
129 : )
130 :
131 2355 : export const useAlgoliaSearchProvider = (): NodePackSearchProvider => {
132 15 : const searchClient = algoliasearch(__ALGOLIA_APP_ID__, __ALGOLIA_API_KEY__)
133 :
134 15 : const searchPacksInternal = async (
135 : query: string,
136 : params: SearchNodePacksParams
137 : ): Promise<
138 : | { ok: true; result: SearchPacksResult; fallbacksFailed: boolean }
139 : | { ok: false; cause: unknown }
140 : > => {
141 0 : const { pageSize, pageNumber } = params
142 :
143 0 : async function searchRawQuery<T>(
144 : requests: SearchQuery[],
145 : resultsSchema: z.ZodType
146 : ): Promise<
147 : { ok: true; results: SearchResponse<T>[] } | { ok: false; cause: unknown }
148 : > {
149 0 : let response: { results: unknown }
150 0 : try {
151 0 : response = await searchClient.search<T>({ requests, strategy: 'none' })
152 : } catch (cause) {
153 0 : return { ok: false, cause }
154 : }
155 0 : const parsed = resultsSchema.safeParse(response.results)
156 0 : if (!parsed.success) {
157 0 : return {
158 : ok: false,
159 : cause: fromZodError(parsed.error, {
160 : prefix: 'Malformed Algolia search response'
161 : })
162 : }
163 : }
164 0 : return { ok: true, results: response.results as SearchResponse<T>[] }
165 : }
166 :
167 0 : async function searchFallbacks(
168 : fallbackQueries: string[]
169 : ): Promise<AlgoliaNodePack[] | undefined> {
170 0 : if (fallbackQueries.length === 0) return []
171 0 : const response = await searchRawQuery<AlgoliaNodePack>(
172 : fallbackQueries.map((fallbackQuery) =>
173 : buildPackSearchRequest(fallbackQuery, toRewriteParams(params))
174 : ),
175 : zFallbackSearchResults
176 : )
177 0 : if (!response.ok) {
178 0 : reportError(response.cause, {
179 : errorType: 'error_searching_algolia_fallbacks',
180 : surface: 'platform',
181 : level: 'warning',
182 : tags: { outcome: 'degraded' }
183 : })
184 0 : return undefined
185 : }
186 0 : const hits = response.results.flatMap((result) => result.hits)
187 0 : return uniqBy(hits, (hit) => hit.objectID).slice(0, pageSize)
188 : }
189 :
190 0 : const shouldQuerySuggestions =
191 : query.length >= MIN_CHARS_FOR_SUGGESTIONS_ALGOLIA
192 0 : const requests: SearchQuery[] = [buildPackSearchRequest(query, params)]
193 0 : if (shouldQuerySuggestions) {
194 0 : requests.push({
195 : indexName: 'nodes_index_query_suggestions',
196 : query
197 : })
198 : }
199 :
200 0 : const response = await searchRawQuery<
201 : AlgoliaNodePack | NodesIndexSuggestion
202 : >(requests, zPackSearchResults)
203 0 : if (!response.ok) return response
204 0 : const { results } = response
205 :
206 0 : const nodePacks = results[0] as SearchResponse<AlgoliaNodePack>
207 0 : const querySuggestions = results[1] as
208 : | SearchResponse<NodesIndexSuggestion>
209 : | undefined
210 :
211 0 : const isNodeSearch =
212 : params.restrictSearchableAttributes?.includes('comfy_nodes')
213 : const needsFallbacks =
214 : pageNumber === 0 && !isNodeSearch && nodePacks.hits.length === 0
215 0 : const hits = needsFallbacks
216 : ? await searchFallbacks(buildPackSearchFallbacks(query))
217 : : nodePacks.hits
218 :
219 : const registryPacks = (hits ?? []).map(toRegistryPack)
220 :
221 : const suggestions =
222 : querySuggestions?.hits.map((suggestion) => ({
223 : query: suggestion.query,
224 : popularity: suggestion.popularity
225 : })) ?? []
226 :
227 0 : return {
228 : ok: true,
229 : result: {
230 : nodePacks: registryPacks,
231 : querySuggestions: suggestions,
232 : hasMore:
233 : nodePacks.nbPages === undefined
234 : ? undefined
235 : : pageNumber + 1 < nodePacks.nbPages,
236 : rawResultsEmpty: nodePacks.hits.length === 0
237 : },
238 : fallbacksFailed: hits === undefined
239 : }
240 : }
241 :
242 15 : const searchPacks = async (
243 : query: string,
244 : params: SearchNodePacksParams
245 : ): Promise<SearchPacksOutcome> => {
246 0 : const { analyticsTags: _analyticsTags, ...resultParams } = params
247 0 : const cacheKey = paramsToCacheKey({ query, ...resultParams })
248 0 : const cachedResult = searchPacksCache.get(cacheKey)
249 0 : if (cachedResult !== undefined) return { ok: true, result: cachedResult }
250 :
251 0 : const outcome = await searchPacksInternal(query, params)
252 0 : if (!outcome.ok) return outcome
253 0 : if (!outcome.fallbacksFailed) searchPacksCache.set(cacheKey, outcome.result)
254 0 : return { ok: true, result: outcome.result }
255 : }
256 :
257 15 : const clearSearchCache = () => {
258 0 : searchPacksCache.clear()
259 : }
260 :
261 15 : return {
262 : searchPacks,
263 : clearSearchCache
264 : }
265 : }
|