@@ -35,8 +35,19 @@ import {
3535 fetchOAuthCredentials ,
3636} from '@/hooks/queries/oauth/oauth-credentials'
3737import { collectDuplicateNames , disambiguateLabelByFolder } from '@/hooks/queries/utils/folder-tree'
38+ import {
39+ createSelectorCacheScopeRegistry ,
40+ type SelectorCacheScopeRegistry ,
41+ scopeServerResolvedSelectorContext ,
42+ } from '@/hooks/selectors/context-resolution'
3843import { getSelectorDefinition , loadAllSelectorOptions } from '@/hooks/selectors/registry'
39- import type { SelectorKey , SelectorOption } from '@/hooks/selectors/types'
44+ import type {
45+ SelectorContext ,
46+ SelectorDefinition ,
47+ SelectorKey ,
48+ SelectorOption ,
49+ } from '@/hooks/selectors/types'
50+ import { getScopedSelectorQueryKey } from '@/hooks/selectors/use-selector-query'
4051import type { WorkflowFolder } from '@/stores/folders/types'
4152
4253/** Stable identity while a folder list loads, so `select` isn't re-keyed on it. */
@@ -92,19 +103,26 @@ export const workflowSearchReplaceKeys = {
92103 knowledgeReplacementOptions : ( workspaceId ?: string ) =>
93104 [ ...workflowSearchReplaceKeys . replacementOptions ( ) , 'knowledge' , workspaceId ?? '' ] as const ,
94105 selectorDetails : ( ) => [ ...workflowSearchReplaceKeys . resourceDetails ( ) , 'selector' ] as const ,
95- selectorDetail : ( selectorKey ?: string , contextKey ?: string , value ?: string ) =>
106+ selectorDetail : (
107+ selectorKey ?: string ,
108+ contextIdentity ?: string | readonly unknown [ ] ,
109+ value ?: string
110+ ) =>
96111 [
97112 ...workflowSearchReplaceKeys . selectorDetails ( ) ,
98113 selectorKey ?? '' ,
99- contextKey ?? '' ,
114+ contextIdentity ?? '' ,
100115 value ?? '' ,
101116 ] as const ,
102- selectorReplacementOptions : ( selectorKey ?: string , contextKey ?: string ) =>
117+ selectorReplacementOptions : (
118+ selectorKey ?: string ,
119+ contextIdentity ?: string | readonly unknown [ ]
120+ ) =>
103121 [
104122 ...workflowSearchReplaceKeys . replacementOptions ( ) ,
105123 'selector' ,
106124 selectorKey ?? '' ,
107- contextKey ?? '' ,
125+ contextIdentity ?? '' ,
108126 ] as const ,
109127}
110128
@@ -139,6 +157,25 @@ function selectorContextKey(match: WorkflowSearchMatch): string {
139157 return stableStringifyWorkflowSearchValue ( match . resource ?. selectorContext ?? { } )
140158}
141159
160+ export function buildWorkflowSearchSelectorScope (
161+ definition : SelectorDefinition ,
162+ context : SelectorContext ,
163+ registry : SelectorCacheScopeRegistry
164+ ) : { context : SelectorContext ; identity : string | readonly unknown [ ] } {
165+ if ( ! definition . serverResolvedContextFields ?. length ) {
166+ return { context, identity : stableStringifyWorkflowSearchValue ( context ) }
167+ }
168+
169+ const scopedContext = scopeServerResolvedSelectorContext ( definition , context , registry )
170+ return {
171+ context : scopedContext ,
172+ identity : getScopedSelectorQueryKey ( definition , {
173+ key : definition . key ,
174+ context : scopedContext ,
175+ } ) ,
176+ }
177+ }
178+
142179function uniqueSelectorDetailMatches ( matches : WorkflowSearchMatch [ ] ) : WorkflowSearchMatch [ ] {
143180 const seen = new Set < string > ( )
144181 return matches . filter ( ( match ) => {
@@ -378,18 +415,22 @@ export function useWorkflowSearchMcpToolDetails(
378415
379416export function useWorkflowSearchSelectorDetails ( matches : WorkflowSearchMatch [ ] ) {
380417 const selectorMatches = useMemo ( ( ) => uniqueSelectorDetailMatches ( matches ) , [ matches ] )
418+ const cacheScopes = useMemo ( ( ) => createSelectorCacheScopeRegistry ( ) , [ ] )
381419
382420 return useQueries ( {
383421 queries : selectorMatches . map ( ( match ) => {
384422 const selectorKey = match . resource ?. selectorKey as SelectorKey
385- const context = match . resource ?. selectorContext ?? { }
386- const contextKey = selectorContextKey ( match )
387423 const definition = getSelectorDefinition ( selectorKey )
424+ const { context, identity } = buildWorkflowSearchSelectorScope (
425+ definition ,
426+ match . resource ?. selectorContext ?? { } ,
427+ cacheScopes
428+ )
388429 const queryArgs = { key : selectorKey , context, detailId : match . rawValue }
389430 const baseEnabled = definition . enabled ? definition . enabled ( queryArgs ) : true
390431
391432 return {
392- queryKey : workflowSearchReplaceKeys . selectorDetail ( selectorKey , contextKey , match . rawValue ) ,
433+ queryKey : workflowSearchReplaceKeys . selectorDetail ( selectorKey , identity , match . rawValue ) ,
393434 queryFn : async ( { signal } : { signal : AbortSignal } ) : Promise < SelectorOption | null > => {
394435 if ( definition . fetchById ) {
395436 return definition . fetchById ( { ...queryArgs , signal } )
@@ -652,18 +693,22 @@ export function useWorkflowSearchMcpToolReplacementOptions(
652693
653694export function useWorkflowSearchSelectorReplacementOptions ( matches : WorkflowSearchMatch [ ] ) {
654695 const selectorGroups = useMemo ( ( ) => uniqueSelectorOptionGroups ( matches ) , [ matches ] )
696+ const cacheScopes = useMemo ( ( ) => createSelectorCacheScopeRegistry ( ) , [ ] )
655697
656698 return useQueries ( {
657699 queries : selectorGroups . map ( ( match ) => {
658700 const selectorKey = match . resource ?. selectorKey as SelectorKey
659- const context = match . resource ?. selectorContext ?? { }
660- const contextKey = selectorContextKey ( match )
661701 const definition = getSelectorDefinition ( selectorKey )
702+ const { context, identity } = buildWorkflowSearchSelectorScope (
703+ definition ,
704+ match . resource ?. selectorContext ?? { } ,
705+ cacheScopes
706+ )
662707 const queryArgs = { key : selectorKey , context }
663708 const baseEnabled = definition . enabled ? definition . enabled ( queryArgs ) : true
664709
665710 return {
666- queryKey : workflowSearchReplaceKeys . selectorReplacementOptions ( selectorKey , contextKey ) ,
711+ queryKey : workflowSearchReplaceKeys . selectorReplacementOptions ( selectorKey , identity ) ,
667712 queryFn : ( { signal } : { signal : AbortSignal } ) =>
668713 loadAllSelectorOptions ( definition , { ...queryArgs , signal } ) ,
669714 enabled : Boolean ( selectorKey && baseEnabled ) ,
0 commit comments