@@ -7,6 +7,7 @@ import { canonicalJson } from '@sim/utils/object'
77import { compareStrings } from '@sim/utils/string'
88import { z } from 'zod'
99import type { WorkspaceSearchFilters } from '@/lib/api/contracts/knowledge'
10+ import { liveSearchInvolvingSchema } from '@/lib/api/contracts/knowledge/search'
1011import type {
1112 WorkspaceKnowledgeSearchData ,
1213 WorkspaceKnowledgeSearchResult ,
@@ -82,7 +83,7 @@ import { isManagedSearchMcpProvider } from '@/lib/sim-search/live/managed-mcp-co
8283import { joinMessages } from '@/lib/sim-search/live/pages'
8384import { loadLiveSearchPolicies } from '@/lib/sim-search/live/policy-store'
8485import { LIVE_SEARCH_PROVIDER_IDS } from '@/lib/sim-search/live/provider-catalog'
85- import { liveSearchGuidance } from '@/lib/sim-search/live/providers'
86+ import { filtersByPerson , liveSearchGuidance } from '@/lib/sim-search/live/providers'
8687import { LiveReadError } from '@/lib/sim-search/live/read-error'
8788import { encodedWindowEnd , READ_WINDOW_CHARACTERS } from '@/lib/sim-search/live/read-window'
8889import {
@@ -162,6 +163,8 @@ interface LiveSearchOptions {
162163 topK : number
163164 filters ?: WorkspaceSearchFilters
164165 nativeQueries ?: NativeSearchQuery [ ]
166+ /** Only items this person took part in: `me` or another person's lowercase email. */
167+ involving ?: string
165168 signal ?: AbortSignal
166169 resultSecretRegistry ?: ResolvedSecretTraceRegistry
167170}
@@ -295,6 +298,9 @@ export const LIVE_NOT_STARTED_MESSAGE =
295298 'Not searched before the search deadline. Search this account alone or narrow the query.'
296299/** Account status for each eligible account beyond {@link MAX_ACCOUNTS}. */
297300export const LIVE_OVER_ACCOUNT_LIMIT_MESSAGE = `Not searched: one search covers at most ${ MAX_ACCOUNTS } accounts. Search this source or account alone.`
301+ /** Account status when a search filtered by person reaches a provider that cannot apply it. */
302+ export const LIVE_NO_PERSON_FILTER_MESSAGE =
303+ 'Not searched: this source cannot filter by person. Search it without involving.'
298304/** Previews center on the query's longest matching term, like indexed passages. */
299305const PREVIEW_CHARACTERS = 1800
300306/**
@@ -486,14 +492,23 @@ const liveKnowledgeSearch = defineAuthorizedKnowledgeUseCase({
486492 const requestedFilters = input . filters
487493 ? workspaceSearchFiltersSchema . parse ( input . filters )
488494 : undefined
489- input = { ...input , filters : requestedFilters }
495+ input = {
496+ ...input ,
497+ filters : requestedFilters ,
498+ involving : liveSearchInvolvingSchema . optional ( ) . parse ( input . involving ) ,
499+ }
490500 if (
491501 ( ! input . query . trim ( ) && ! queries ?. some ( ( query ) => query . query ) ) ||
492502 queries ?. some ( ( query ) => ! query . query )
493503 )
494504 input = { ...input , filters : withImpliedListingBound ( input . filters , new Date ( ) ) }
495505 annotateSearchDiagnostics (
496- describeSearchRequest ( { query : input . query , filters : input . filters , nativeQueries : queries } )
506+ describeSearchRequest ( {
507+ query : input . query ,
508+ filters : input . filters ,
509+ nativeQueries : queries ,
510+ involving : input . involving ,
511+ } )
497512 )
498513 if (
499514 ( ! input . query . trim ( ) &&
@@ -530,14 +545,18 @@ const liveKnowledgeSearch = defineAuthorizedKnowledgeUseCase({
530545 measureSearchStage ( 'live.policies' , ( ) => loadLiveSearchPolicies ( input ) ) ,
531546 measureSearchStage ( 'live.accounts' , ( ) => listLiveAccounts ( input , userId ) ) ,
532547 ] )
533- const eligible = allAccounts
548+ const reachable = allAccounts
534549 . filter (
535550 ( account ) =>
536551 ( ! filters ?. source || filters . source === account . provider ) &&
537552 nativesFor ( account ) . length > 0
538553 )
539554 . sort ( compareAccounts )
540- const nothingSearched = ! queries && eligible . length === 0
555+ /** A person filter skips the sources that cannot apply it rather than return them unfiltered. */
556+ const eligible = reachable . filter (
557+ ( account ) => ! input . involving || filtersByPerson ( account . provider )
558+ )
559+ const nothingSearched = ! queries && reachable . length === 0
541560 const selected = eligible . slice ( 0 , MAX_ACCOUNTS )
542561 const openingAccount = createConcurrencyLimit ( ACCOUNT_OPEN_CONCURRENCY )
543562 annotateSearchDiagnostics ( {
@@ -615,6 +634,7 @@ const liveKnowledgeSearch = defineAuthorizedKnowledgeUseCase({
615634 policy : session . policy ,
616635 query : input . query ,
617636 native : queryNative ,
637+ involving : input . involving ,
618638 limit : input . topK ,
619639 scopes : resolved . account . scopes ,
620640 } )
@@ -944,22 +964,29 @@ const liveKnowledgeSearch = defineAuthorizedKnowledgeUseCase({
944964 ? [ nothingSearchedGuidance ( input , filters ?. source , allAccounts . length > 0 ) ]
945965 : [ ] ) ,
946966 liveSearchGuidance (
947- accounts . filter ( ( account ) => account . status !== 'reconnect' ) . map ( ( { provider } ) => provider )
967+ accounts
968+ . filter ( ( account ) => account . status !== 'reconnect' )
969+ . map ( ( { provider } ) => provider ) ,
970+ Boolean ( input . involving )
948971 ) ,
949972 ] . join ( '\n' )
950- for ( const account of eligible . slice ( MAX_ACCOUNTS ) ) {
951- for ( const { queryIndex } of nativesFor ( account ) )
952- accounts . push ( {
953- accountId : account . id ,
954- provider : account . provider ,
955- displayName : account . displayName ,
956- ...( queryIndex === undefined ? { } : { queryIndex } ) ,
957- status : 'unavailable' ,
958- message : LIVE_OVER_ACCOUNT_LIMIT_MESSAGE ,
959- } )
960- }
973+ const unsearched : [ LiveAccount [ ] , string ] [ ] = [
974+ [ eligible . slice ( MAX_ACCOUNTS ) , LIVE_OVER_ACCOUNT_LIMIT_MESSAGE ] ,
975+ [ reachable . filter ( ( account ) => ! eligible . includes ( account ) ) , LIVE_NO_PERSON_FILTER_MESSAGE ] ,
976+ ]
977+ for ( const [ skipped , message ] of unsearched )
978+ for ( const account of skipped )
979+ for ( const { queryIndex } of nativesFor ( account ) )
980+ accounts . push ( {
981+ accountId : account . id ,
982+ provider : account . provider ,
983+ displayName : account . displayName ,
984+ ...( queryIndex === undefined ? { } : { queryIndex } ) ,
985+ status : 'unavailable' ,
986+ message,
987+ } )
961988 for ( const [ queryIndex , query ] of ( queries ?? [ ] ) . entries ( ) ) {
962- if ( ! eligible . some ( ( account ) => targetsAccount ( query , account ) ) )
989+ if ( ! reachable . some ( ( account ) => targetsAccount ( query , account ) ) )
963990 accounts . push ( {
964991 accountId : query . accountId ?? '' ,
965992 provider : query . provider ,
0 commit comments