11import { useMemo } from 'react'
2- import { useQueries } from '@tanstack/react-query'
2+ import { useQueries , useQuery } from '@tanstack/react-query'
33import { requestJson } from '@/lib/api/client/request'
44import type { KnowledgeBaseData } from '@/lib/api/contracts/knowledge'
5- import { discoverMcpToolsContract , listMcpServersContract } from '@/lib/api/contracts/mcp'
6- import { getTableContract , listTablesContract } from '@/lib/api/contracts/tables'
7- import { listWorkspaceFilesContract } from '@/lib/api/contracts/workspace-files'
5+ import {
6+ type DiscoverMcpToolsResponse ,
7+ discoverMcpToolsContract ,
8+ type ListMcpServersResponse ,
9+ listMcpServersContract ,
10+ } from '@/lib/api/contracts/mcp'
11+ import {
12+ type GetTableResponse ,
13+ getTableContract ,
14+ type ListTablesResponse ,
15+ listTablesContract ,
16+ } from '@/lib/api/contracts/tables'
17+ import {
18+ type ListWorkspaceFilesResponse ,
19+ listWorkspaceFilesContract ,
20+ } from '@/lib/api/contracts/workspace-files'
821import { createMcpToolId } from '@/lib/mcp/shared'
922import type { Credential } from '@/lib/oauth'
1023import { stableStringifyWorkflowSearchValue } from '@/lib/workflows/search-replace/resource-resolvers'
@@ -59,16 +72,22 @@ export const workflowSearchReplaceKeys = {
5972 fileDetails : ( ) => [ ...workflowSearchReplaceKeys . resourceDetails ( ) , 'file' ] as const ,
6073 fileDetail : ( workspaceId ?: string , fileKey ?: string ) =>
6174 [ ...workflowSearchReplaceKeys . fileDetails ( ) , workspaceId ?? '' , fileKey ?? '' ] as const ,
75+ fileListDetails : ( workspaceId ?: string ) =>
76+ [ ...workflowSearchReplaceKeys . fileDetails ( ) , 'list' , workspaceId ?? '' ] as const ,
6277 fileReplacementOptions : ( workspaceId ?: string ) =>
6378 [ ...workflowSearchReplaceKeys . replacementOptions ( ) , 'file' , workspaceId ?? '' ] as const ,
6479 mcpServerDetails : ( ) => [ ...workflowSearchReplaceKeys . resourceDetails ( ) , 'mcp-server' ] as const ,
6580 mcpServerDetail : ( workspaceId ?: string , serverId ?: string ) =>
6681 [ ...workflowSearchReplaceKeys . mcpServerDetails ( ) , workspaceId ?? '' , serverId ?? '' ] as const ,
82+ mcpServerListDetails : ( workspaceId ?: string ) =>
83+ [ ...workflowSearchReplaceKeys . mcpServerDetails ( ) , 'list' , workspaceId ?? '' ] as const ,
6784 mcpServerReplacementOptions : ( workspaceId ?: string ) =>
6885 [ ...workflowSearchReplaceKeys . replacementOptions ( ) , 'mcp-server' , workspaceId ?? '' ] as const ,
6986 mcpToolDetails : ( ) => [ ...workflowSearchReplaceKeys . resourceDetails ( ) , 'mcp-tool' ] as const ,
7087 mcpToolDetail : ( workspaceId ?: string , toolId ?: string ) =>
7188 [ ...workflowSearchReplaceKeys . mcpToolDetails ( ) , workspaceId ?? '' , toolId ?? '' ] as const ,
89+ mcpToolListDetails : ( workspaceId ?: string ) =>
90+ [ ...workflowSearchReplaceKeys . mcpToolDetails ( ) , 'list' , workspaceId ?? '' ] as const ,
7291 mcpToolReplacementOptions : ( workspaceId ?: string ) =>
7392 [ ...workflowSearchReplaceKeys . replacementOptions ( ) , 'mcp-tool' , workspaceId ?? '' ] as const ,
7493 knowledgeReplacementOptions : ( workspaceId ?: string ) =>
@@ -196,7 +215,7 @@ export function useWorkflowSearchTableDetails(
196215 } ) ,
197216 enabled : Boolean ( workspaceId && match . rawValue ) ,
198217 staleTime : 60 * 1000 ,
199- select : ( response ) : WorkflowSearchResolvedResource => ( {
218+ select : ( response : GetTableResponse ) : WorkflowSearchResolvedResource => ( {
200219 matchRawValue : match . rawValue ,
201220 resourceGroupKey : match . resource ?. resourceGroupKey ,
202221 label : response . data . table . name ,
@@ -217,31 +236,38 @@ export function useWorkflowSearchFileDetails(matches: WorkflowSearchMatch[], wor
217236 [ matches ]
218237 )
219238
220- return useQueries ( {
221- queries : fileMatches . map ( ( match ) => ( {
222- queryKey : workflowSearchReplaceKeys . fileDetail ( workspaceId , match . rawValue ) ,
223- queryFn : ( { signal } : { signal : AbortSignal } ) =>
224- requestJson ( listWorkspaceFilesContract , {
225- params : { id : workspaceId as string } ,
226- query : { scope : 'active' } ,
227- signal,
228- } ) ,
229- enabled : Boolean ( workspaceId && match . rawValue ) ,
230- staleTime : 60 * 1000 ,
231- select : ( response ) : WorkflowSearchResolvedResource => {
232- const file = response . files . find ( ( item ) =>
239+ const filesQuery = useQuery ( {
240+ queryKey : workflowSearchReplaceKeys . fileListDetails ( workspaceId ) ,
241+ queryFn : ( { signal } : { signal : AbortSignal } ) =>
242+ requestJson ( listWorkspaceFilesContract , {
243+ params : { id : workspaceId as string } ,
244+ query : { scope : 'active' } ,
245+ signal,
246+ } ) ,
247+ enabled : Boolean ( workspaceId && fileMatches . length > 0 ) ,
248+ staleTime : 60 * 1000 ,
249+ } )
250+
251+ return useMemo (
252+ ( ) =>
253+ fileMatches . map ( ( match ) => {
254+ const file = filesQuery . data ?. files . find ( ( item ) =>
233255 [ item . id , item . key , item . path , item . name ] . includes ( match . rawValue )
234256 )
235257 return {
236- matchRawValue : match . rawValue ,
237- resourceGroupKey : match . resource ?. resourceGroupKey ,
238- label : file ?. name ?? match . rawValue ,
239- resolved : Boolean ( file ) ,
240- inaccessible : false ,
258+ data : filesQuery . data
259+ ? {
260+ matchRawValue : match . rawValue ,
261+ resourceGroupKey : match . resource ?. resourceGroupKey ,
262+ label : file ?. name ?? match . rawValue ,
263+ resolved : Boolean ( file ) ,
264+ inaccessible : false ,
265+ }
266+ : undefined ,
241267 }
242- } ,
243- } ) ) ,
244- } )
268+ } ) ,
269+ [ fileMatches , filesQuery . data ]
270+ )
245271}
246272
247273export function useWorkflowSearchMcpServerDetails (
@@ -250,28 +276,35 @@ export function useWorkflowSearchMcpServerDetails(
250276) {
251277 const serverMatches = useMemo ( ( ) => uniqueMatches ( matches , 'mcp-server' ) , [ matches ] )
252278
253- return useQueries ( {
254- queries : serverMatches . map ( ( match ) => ( {
255- queryKey : workflowSearchReplaceKeys . mcpServerDetail ( workspaceId , match . rawValue ) ,
256- queryFn : ( { signal } : { signal : AbortSignal } ) =>
257- requestJson ( listMcpServersContract , {
258- query : { workspaceId : workspaceId as string } ,
259- signal,
260- } ) ,
261- enabled : Boolean ( workspaceId && match . rawValue ) ,
262- staleTime : 60 * 1000 ,
263- select : ( response ) : WorkflowSearchResolvedResource => {
264- const server = response . data . servers . find ( ( item ) => item . id === match . rawValue )
279+ const serversQuery = useQuery ( {
280+ queryKey : workflowSearchReplaceKeys . mcpServerListDetails ( workspaceId ) ,
281+ queryFn : ( { signal } : { signal : AbortSignal } ) =>
282+ requestJson ( listMcpServersContract , {
283+ query : { workspaceId : workspaceId as string } ,
284+ signal,
285+ } ) ,
286+ enabled : Boolean ( workspaceId && serverMatches . length > 0 ) ,
287+ staleTime : 60 * 1000 ,
288+ } )
289+
290+ return useMemo (
291+ ( ) =>
292+ serverMatches . map ( ( match ) => {
293+ const server = serversQuery . data ?. data . servers . find ( ( item ) => item . id === match . rawValue )
265294 return {
266- matchRawValue : match . rawValue ,
267- resourceGroupKey : match . resource ?. resourceGroupKey ,
268- label : server ?. name ?? match . rawValue ,
269- resolved : Boolean ( server ) ,
270- inaccessible : false ,
295+ data : serversQuery . data
296+ ? {
297+ matchRawValue : match . rawValue ,
298+ resourceGroupKey : match . resource ?. resourceGroupKey ,
299+ label : server ?. name ?? match . rawValue ,
300+ resolved : Boolean ( server ) ,
301+ inaccessible : false ,
302+ }
303+ : undefined ,
271304 }
272- } ,
273- } ) ) ,
274- } )
305+ } ) ,
306+ [ serverMatches , serversQuery . data ]
307+ )
275308}
276309
277310export function useWorkflowSearchMcpToolDetails (
@@ -280,30 +313,37 @@ export function useWorkflowSearchMcpToolDetails(
280313) {
281314 const toolMatches = useMemo ( ( ) => uniqueMatches ( matches , 'mcp-tool' ) , [ matches ] )
282315
283- return useQueries ( {
284- queries : toolMatches . map ( ( match ) => ( {
285- queryKey : workflowSearchReplaceKeys . mcpToolDetail ( workspaceId , match . rawValue ) ,
286- queryFn : ( { signal } : { signal : AbortSignal } ) =>
287- requestJson ( discoverMcpToolsContract , {
288- query : { workspaceId : workspaceId as string } ,
289- signal,
290- } ) ,
291- enabled : Boolean ( workspaceId && match . rawValue ) ,
292- staleTime : 60 * 1000 ,
293- select : ( response ) : WorkflowSearchResolvedResource => {
294- const tool = response . data . tools . find (
316+ const toolsQuery = useQuery ( {
317+ queryKey : workflowSearchReplaceKeys . mcpToolListDetails ( workspaceId ) ,
318+ queryFn : ( { signal } : { signal : AbortSignal } ) =>
319+ requestJson ( discoverMcpToolsContract , {
320+ query : { workspaceId : workspaceId as string } ,
321+ signal,
322+ } ) ,
323+ enabled : Boolean ( workspaceId && toolMatches . length > 0 ) ,
324+ staleTime : 60 * 1000 ,
325+ } )
326+
327+ return useMemo (
328+ ( ) =>
329+ toolMatches . map ( ( match ) => {
330+ const tool = toolsQuery . data ?. data . tools . find (
295331 ( item ) => createMcpToolId ( item . serverId , item . name ) === match . rawValue
296332 )
297333 return {
298- matchRawValue : match . rawValue ,
299- resourceGroupKey : match . resource ?. resourceGroupKey ,
300- label : tool ? `${ tool . serverName } : ${ tool . name } ` : match . rawValue ,
301- resolved : Boolean ( tool ) ,
302- inaccessible : false ,
334+ data : toolsQuery . data
335+ ? {
336+ matchRawValue : match . rawValue ,
337+ resourceGroupKey : match . resource ?. resourceGroupKey ,
338+ label : tool ? `${ tool . serverName } : ${ tool . name } ` : match . rawValue ,
339+ resolved : Boolean ( tool ) ,
340+ inaccessible : false ,
341+ }
342+ : undefined ,
303343 }
304- } ,
305- } ) ) ,
306- } )
344+ } ) ,
345+ [ toolMatches , toolsQuery . data ]
346+ )
307347}
308348
309349export function useWorkflowSearchSelectorDetails ( matches : WorkflowSearchMatch [ ] ) {
@@ -418,7 +458,7 @@ export function useWorkflowSearchTableReplacementOptions(
418458 } ) ,
419459 enabled : Boolean ( workspaceId && tableMatch ) ,
420460 staleTime : 60 * 1000 ,
421- select : ( response ) : WorkflowSearchReplacementOption [ ] =>
461+ select : ( response : ListTablesResponse ) : WorkflowSearchReplacementOption [ ] =>
422462 response . data . tables . map ( ( table ) => ( {
423463 kind : 'table' ,
424464 value : table . id ,
@@ -451,7 +491,7 @@ export function useWorkflowSearchFileReplacementOptions(
451491 } ) ,
452492 enabled : Boolean ( workspaceId && fileMatch ) ,
453493 staleTime : 60 * 1000 ,
454- select : ( response ) : WorkflowSearchReplacementOption [ ] =>
494+ select : ( response : ListWorkspaceFilesResponse ) : WorkflowSearchReplacementOption [ ] =>
455495 response . files . map ( ( file ) => ( {
456496 kind : 'file' ,
457497 value : JSON . stringify ( {
@@ -486,7 +526,7 @@ export function useWorkflowSearchMcpServerReplacementOptions(
486526 } ) ,
487527 enabled : Boolean ( workspaceId && serverMatch ) ,
488528 staleTime : 60 * 1000 ,
489- select : ( response ) : WorkflowSearchReplacementOption [ ] =>
529+ select : ( response : ListMcpServersResponse ) : WorkflowSearchReplacementOption [ ] =>
490530 response . data . servers . map ( ( server ) => ( {
491531 kind : 'mcp-server' ,
492532 value : server . id ,
@@ -515,7 +555,7 @@ export function useWorkflowSearchMcpToolReplacementOptions(
515555 } ) ,
516556 enabled : Boolean ( workspaceId && toolMatch ) ,
517557 staleTime : 60 * 1000 ,
518- select : ( response ) : WorkflowSearchReplacementOption [ ] =>
558+ select : ( response : DiscoverMcpToolsResponse ) : WorkflowSearchReplacementOption [ ] =>
519559 response . data . tools . map ( ( tool ) => ( {
520560 kind : 'mcp-tool' ,
521561 value : createMcpToolId ( tool . serverId , tool . name ) ,
0 commit comments