diff --git a/apps/sim/lib/sim-search/live/application.ts b/apps/sim/lib/sim-search/live/application.ts index 20055848734..c140678ee29 100644 --- a/apps/sim/lib/sim-search/live/application.ts +++ b/apps/sim/lib/sim-search/live/application.ts @@ -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) diff --git a/apps/sim/lib/sim-search/live/google.ts b/apps/sim/lib/sim-search/live/google.ts index 6c156e1d290..1942c5ec9df 100644 --- a/apps/sim/lib/sim-search/live/google.ts +++ b/apps/sim/lib/sim-search/live/google.ts @@ -258,7 +258,9 @@ function gmailDocument(row: Record): 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( diff --git a/apps/sim/lib/sim-search/live/providers.test.ts b/apps/sim/lib/sim-search/live/providers.test.ts index 6d8ce3d567d..41e6958fc58 100644 --- a/apps/sim/lib/sim-search/live/providers.test.ts +++ b/apps/sim/lib/sim-search/live/providers.test.ts @@ -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' @@ -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) => ({ diff --git a/apps/sim/lib/sim-search/live/providers.ts b/apps/sim/lib/sim-search/live/providers.ts index 2c5230628e5..16f372d2f94 100644 --- a/apps/sim/lib/sim-search/live/providers.ts +++ b/apps/sim/lib/sim-search/live/providers.ts @@ -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 /** @@ -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.', @@ -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. */