Skip to content

Commit cadd89d

Browse files
committed
address comments
1 parent 1030678 commit cadd89d

2 files changed

Lines changed: 45 additions & 4 deletions

File tree

apps/sim/lib/workflows/search-replace/resources/resolvers.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,32 @@ describe('dedupeOverlappingWorkflowSearchMatches', () => {
6969
])
7070
})
7171

72+
it('uses kind priority rather than iteration order for equal-span non-text matches', () => {
73+
const workflowReferenceMatch = createMatch({
74+
id: 'workflow-reference',
75+
kind: 'workflow-reference',
76+
rawValue: '{{API_KEY}}',
77+
searchText: 'API_KEY',
78+
range: { start: 0, end: 11 },
79+
resource: { kind: 'workflow-reference', token: '{{API_KEY}}', key: 'API_KEY' },
80+
})
81+
const environmentMatch = createMatch({
82+
id: 'environment',
83+
kind: 'environment',
84+
rawValue: '{{API_KEY}}',
85+
searchText: 'API_KEY',
86+
range: { start: 0, end: 11 },
87+
resource: { kind: 'environment', token: '{{API_KEY}}', key: 'API_KEY' },
88+
})
89+
90+
expect(
91+
dedupeOverlappingWorkflowSearchMatches([workflowReferenceMatch, environmentMatch])
92+
).toEqual([workflowReferenceMatch])
93+
expect(
94+
dedupeOverlappingWorkflowSearchMatches([environmentMatch, workflowReferenceMatch])
95+
).toEqual([workflowReferenceMatch])
96+
})
97+
7298
it('does not collapse matches from different fields', () => {
7399
const firstMatch = createMatch({
74100
id: 'first',

apps/sim/lib/workflows/search-replace/resources/resolvers.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
11
import type {
22
WorkflowSearchMatch,
3+
WorkflowSearchMatchKind,
34
WorkflowSearchReplacementOption,
45
WorkflowSearchResourceMeta,
56
WorkflowSearchValuePath,
67
} from '@/lib/workflows/search-replace/types'
78
import type { SelectorContext } from '@/hooks/selectors/types'
89

10+
const OVERLAPPING_MATCH_KIND_PRIORITY: Record<WorkflowSearchMatchKind, number> = {
11+
text: 0,
12+
environment: 1,
13+
'workflow-reference': 2,
14+
'oauth-credential': 3,
15+
'knowledge-base': 3,
16+
'knowledge-document': 3,
17+
workflow: 3,
18+
'mcp-server': 3,
19+
'mcp-tool': 3,
20+
table: 3,
21+
file: 3,
22+
'selector-resource': 3,
23+
}
24+
925
export function stableStringifyWorkflowSearchValue(value: unknown): string {
1026
if (!value || typeof value !== 'object') return JSON.stringify(value)
1127
if (Array.isArray(value)) {
@@ -118,10 +134,9 @@ function shouldPreferOverlappingMatch(
118134
const currentLength = getRangeLength(current)
119135
if (candidateLength !== currentLength) return candidateLength < currentLength
120136

121-
if (candidate.kind !== current.kind) {
122-
if (candidate.kind !== 'text') return true
123-
if (current.kind !== 'text') return false
124-
}
137+
const candidatePriority = OVERLAPPING_MATCH_KIND_PRIORITY[candidate.kind]
138+
const currentPriority = OVERLAPPING_MATCH_KIND_PRIORITY[current.kind]
139+
if (candidatePriority !== currentPriority) return candidatePriority > currentPriority
125140

126141
return false
127142
}

0 commit comments

Comments
 (0)