Skip to content

Commit 3ccdd16

Browse files
committed
improvement(search): one provider setting for the any-term retry
Replace the parallel requiresEveryTerm and partialMatchesNeedHalfTheTerms flags and their two lookups with a single anyTermRetry setting that carries the fallback's options.
1 parent 6e8bad7 commit 3ccdd16

4 files changed

Lines changed: 27 additions & 31 deletions

File tree

‎apps/sim/lib/sim-search/live/account-session.ts‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ import { livePolicyFor, loadLiveSearchPolicies } from '@/lib/sim-search/live/pol
2323
import { LIVE_SEARCH_PROVIDER_CATALOG } from '@/lib/sim-search/live/provider-catalog'
2424
import { isSearchProviderEnabled } from '@/lib/sim-search/live/provider-rollout'
2525
import {
26-
partialMatchesNeedHalfTheTerms,
26+
anyTermRetry,
2727
previewNativeText,
2828
readNativeProvider,
29-
requiresEveryTerm,
3029
searchNativeProvider,
3130
} from '@/lib/sim-search/live/providers'
3231
import { searchWithinPolicy } from '@/lib/sim-search/live/scoped-search'
@@ -206,10 +205,9 @@ export async function openLiveAccountSession(
206205
client ? searchNativeProvider(provider, client, native) : searchMcp(native)
207206
)
208207
}
209-
return client && requiresEveryTerm(provider)
210-
? searchWithAnyTermFallback(searchOnce, search, signal, {
211-
requireHalfTheTerms: partialMatchesNeedHalfTheTerms(provider),
212-
})
208+
const retry = client ? anyTermRetry(provider) : undefined
209+
return retry
210+
? searchWithAnyTermFallback(searchOnce, search, signal, retry)
213211
: searchOnce(search)
214212
},
215213
async verify(document) {

‎apps/sim/lib/sim-search/live/application.test.ts‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const mocks = vi.hoisted(() => ({
2626
json: vi.fn(),
2727
service: vi.fn(),
2828
destroyPool: vi.fn(),
29-
requiresEveryTerm: vi.fn(),
29+
anyTermRetry: vi.fn(),
3030
preview: vi.fn(),
3131
}))
3232
vi.mock('@/lib/core/security/input-validation.server', () => inputValidationMock)
@@ -67,8 +67,7 @@ vi.mock('@/lib/sim-search/live/providers', () => ({
6767
liveSearchGuidance: (providers: string[]) => `Live coverage: ${[...new Set(providers)]}`,
6868
searchNativeProvider: mocks.search,
6969
readNativeProvider: mocks.read,
70-
requiresEveryTerm: mocks.requiresEveryTerm,
71-
partialMatchesNeedHalfTheTerms: () => false,
70+
anyTermRetry: mocks.anyTermRetry,
7271
previewNativeText: mocks.preview,
7372
}))
7473
vi.mock('@/executor/utils/resolved-secret-content-projection', () => ({
@@ -141,7 +140,7 @@ describe('authorized live retrieval', () => {
141140
documents: [{ ...document, id: 'src/a.ts', container: '42', kind: 'code' }],
142141
})
143142
mocks.adminVerify.mockResolvedValue(true)
144-
mocks.requiresEveryTerm.mockReset().mockReturnValue(false)
143+
mocks.anyTermRetry.mockReset().mockReturnValue(undefined)
145144
mocks.preview.mockReset().mockResolvedValue(undefined)
146145
})
147146
describe('Zoom continuation integrity', () => {
@@ -699,7 +698,7 @@ describe('authorized live retrieval', () => {
699698
})
700699

701700
it('retries an underfilled search with any term for providers that require every term', async () => {
702-
mocks.requiresEveryTerm.mockReturnValue(true)
701+
mocks.anyTermRetry.mockReturnValue({})
703702
const loose = { ...document, id: 'loose', url: 'https://docs.google.com/document/d/loose/edit' }
704703
mocks.search.mockImplementation(async (_provider, _client, search) => ({
705704
documents: search.textMatch === 'any' ? [loose, document] : [document],

‎apps/sim/lib/sim-search/live/providers.ts‎

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
type LiveSearchProviderId,
2121
} from '@/lib/sim-search/live/provider-catalog'
2222
import { readSlack, searchSlack } from '@/lib/sim-search/live/slack'
23+
import type { AnyTermFallbackOptions } from '@/lib/sim-search/live/term-fallback'
2324
import type {
2425
NativeClient,
2526
NativeDocument,
@@ -46,13 +47,12 @@ interface NativeQueryGuide {
4647

4748
interface NativeProvider {
4849
guide: NativeQueryGuide
49-
/** Plain queries must contain every word, so an underfilled search retries with any term. */
50-
requiresEveryTerm?: true
5150
/**
52-
* The any-term retry ranks items sharing just a couple of common words alongside close matches,
53-
* so its partial matches must contain at least half the query's terms.
51+
* Plain queries must contain every word, so an underfilled search retries with any term.
52+
* `requireHalfTheTerms` is for a provider whose any-term query ranks items sharing just a couple
53+
* of common words alongside close matches.
5454
*/
55-
partialMatchesNeedHalfTheTerms?: true
55+
anyTermRetry?: AnyTermFallbackOptions
5656
/**
5757
* Full text for a search preview, for providers whose search returns only an excerpt or
5858
* metadata. At most one request; undefined for kinds without a cheap text form.
@@ -181,8 +181,7 @@ export const LIVE_SEARCH_PROVIDERS = {
181181
avoid:
182182
'JQL with only an ORDER BY clause (Jira rejects unbounded queries), and identifying users other than currentUser() by display name or email; use their account ID.',
183183
},
184-
requiresEveryTerm: true,
185-
partialMatchesNeedHalfTheTerms: true,
184+
anyTermRetry: { requireHalfTheTerms: true },
186185
search: (client, input) => searchAtlassian(client, 'jira', input),
187186
read: (client, reference) => readAtlassian(client, 'jira', reference.id, reference.container),
188187
},
@@ -196,7 +195,7 @@ export const LIVE_SEARCH_PROVIDERS = {
196195
avoid:
197196
'starting the query with a negative clause (NOT, !=, !~ or NOT IN), which CQL rejects; lead with a positive clause such as type IN (page, blogpost).',
198197
},
199-
requiresEveryTerm: true,
198+
anyTermRetry: { requireHalfTheTerms: false },
200199
previewText: previewConfluence,
201200
search: (client, input) => searchAtlassian(client, 'confluence', input),
202201
read: (client, reference) =>
@@ -319,18 +318,13 @@ export function searchNativeProvider(
319318
return adapter.search(client, input)
320319
}
321320

322-
/** True when the provider's plain-text search matches only items containing every query word. */
323-
export function requiresEveryTerm(provider: LiveSearchProviderId): boolean {
324-
const adapter = LIVE_SEARCH_PROVIDERS[provider]
325-
return 'requiresEveryTerm' in adapter && adapter.requiresEveryTerm === true
326-
}
327-
328-
/** True when the provider's partial matches must contain at least half the query's terms. */
329-
export function partialMatchesNeedHalfTheTerms(provider: LiveSearchProviderId): boolean {
321+
/**
322+
* How the provider retries an underfilled plain search with any term; undefined when its
323+
* plain-text search does not require every query word.
324+
*/
325+
export function anyTermRetry(provider: LiveSearchProviderId): AnyTermFallbackOptions | undefined {
330326
const adapter = LIVE_SEARCH_PROVIDERS[provider]
331-
return (
332-
'partialMatchesNeedHalfTheTerms' in adapter && adapter.partialMatchesNeedHalfTheTerms === true
333-
)
327+
return 'anyTermRetry' in adapter ? adapter.anyTermRetry : undefined
334328
}
335329

336330
/**

‎apps/sim/lib/sim-search/live/term-fallback.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,16 @@ const documentKey = ({ container, kind, id }: NativeDocument) =>
5050
* With `requireHalfTheTerms`, a partial match must contain at least half the query's terms in its
5151
* title or text, for providers whose OR query matches items sharing only a couple of common words.
5252
*/
53+
export interface AnyTermFallbackOptions {
54+
/** Keep only partial matches holding at least half the query's terms. */
55+
requireHalfTheTerms?: boolean
56+
}
57+
5358
export async function searchWithAnyTermFallback(
5459
search: (input: NativeSearchInput) => Promise<NativePage>,
5560
input: NativeSearchInput,
5661
signal: AbortSignal,
57-
options: { requireHalfTheTerms?: boolean } = {}
62+
options: AnyTermFallbackOptions = {}
5863
): Promise<NativePage> {
5964
const strict = await search(input)
6065
const terms = significantTerms(input.query)

0 commit comments

Comments
 (0)