Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion apps/sim/lib/sim-search/live/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,8 @@ const liveKnowledgeSearch = defineAuthorizedKnowledgeUseCase({
.sort(compareAccounts)
/** A person filter skips the sources that cannot apply it rather than return them unfiltered. */
const eligible = reachable.filter(
(account) => !input.involving || filtersByPerson(account.provider, input.involving)
(account) =>
!input.involving || filtersByPerson(account.provider, input.involving, account.scopes)
)
const nothingSearched = !queries && reachable.length === 0
const selected = eligible.slice(0, MAX_ACCOUNTS)
Expand Down
4 changes: 3 additions & 1 deletion apps/sim/lib/sim-search/live/google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,9 @@ function gmailDocument(row: Record<string, unknown>): NativeDocument {
*/
function gmailPersonClause(involving: string | undefined): string {
if (!involving) return ''
return involving === 'me' ? 'from:me' : `{from:${involving} to:${involving} cc:${involving}}`
if (involving === 'me') return 'from:me'
const address = `"${involving}"`
return `{from:${address} to:${address} cc:${address}}`
}

export async function searchGmail(
Expand Down
17 changes: 16 additions & 1 deletion apps/sim/lib/sim-search/live/providers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import {
withSearchProviderDiagnostics,
} from '@/lib/knowledge/search/diagnostics'
import { readGitHub, searchGitHub } from '@/lib/sim-search/live/github'
import { searchCalendar, searchDrive } from '@/lib/sim-search/live/google'
import { searchCalendar, searchDrive, searchGmail } from '@/lib/sim-search/live/google'
import { defaultLiveSearchPolicy } from '@/lib/sim-search/live/policy-schema'
import { filtersByPerson } from '@/lib/sim-search/live/providers'
import { searchSlack } from '@/lib/sim-search/live/slack'
import type { NativeClient } from '@/lib/sim-search/live/types'

Expand Down Expand Up @@ -116,6 +117,20 @@ describe('native search endpoints', () => {
).rejects.toThrow('ORs repeated involves:')
expect(api.json.mock.calls.some(([path]) => path.startsWith('/search/'))).toBe(false)
})
it('keeps a Gmail person group intact when the address holds Gmail operator characters', async () => {
const api = client()
api.json.mockResolvedValue({ messages: [] })
await searchGmail(api, { ...input, query: '', involving: 'a}{b|c@example.com' })
const address = '"a}{b|c@example.com"'
expect(api.json.mock.calls[0]?.[1]?.query?.q).toBe(
`{from:${address} to:${address} cc:${address}}`
)
})
it('skips Slack for a person found by email unless the account can look members up by email', () => {
expect(filtersByPerson('slack', 'me', [])).toBe(true)
expect(filtersByPerson('slack', 'jane@example.com', [])).toBe(false)
expect(filtersByPerson('slack', 'jane@example.com', ['users:read.email'])).toBe(true)
})
it('keeps only calendar events a person organizes or attends without declining', async () => {
const api = client()
const event = (id: string, extra: Record<string, unknown>) => ({
Expand Down
20 changes: 17 additions & 3 deletions apps/sim/lib/sim-search/live/providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ interface NativeProvider {
involving?: string
/** The involving filter accepts only `me`: the provider cannot find anyone else by email. */
involvingMeOnly?: true
/** The permission finding someone other than `me` by email needs; without it only `me` applies. */
involvingByEmailScope?: string
/** Plain queries must contain every word, so an underfilled search retries with any term. */
requiresEveryTerm?: true
/**
Expand Down Expand Up @@ -175,6 +177,7 @@ export const LIVE_SEARCH_PROVIDERS = {
},
slack: {
involving: 'messages they sent',
involvingByEmailScope: 'users:read.email',
guide: {
syntax:
'Real-time Search: a question (what/how/…?) enables meaning-based matching where Slack AI is on; keyword retrieval (keywordOnly, sortBy newest or oldest, or no Slack AI) requires every word and does not support OR. "exact phrase" and prefix matching such as psca* work.',
Expand Down Expand Up @@ -375,10 +378,21 @@ export function searchNativeProvider(
return adapter.search(client, input, origin)
}

/** True when the provider can limit a search to the items this person (`me` or an email) took part in. */
export function filtersByPerson(provider: LiveSearchProviderId, involving: string): boolean {
/**
* True when the account can limit a search to the items this person (`me` or an email) took part
* in: the provider supports the filter and, for an email, the account holds the scope finding
* someone by email needs.
*/
export function filtersByPerson(
provider: LiveSearchProviderId,
involving: string,
scopes: readonly string[]
): boolean {
const adapter: NativeProvider | ManagedMcpProvider = LIVE_SEARCH_PROVIDERS[provider]
return 'involving' in adapter && (involving === 'me' || !('involvingMeOnly' in adapter))
if (!('involving' in adapter)) return false
if (involving === 'me') return true
if ('involvingMeOnly' in adapter) return false
return !adapter.involvingByEmailScope || scopes.includes(adapter.involvingByEmailScope)
}

/** True when the provider's plain-text search matches only items containing every query word. */
Expand Down
Loading