Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

33.4 KB
Raw Blame
use crate::api::search::simple::filter::{FilterVariantToSearchArgs, ScopedCalendarEventFilters};

use crate::api::search::crm_company;
use crate::api::search::simple::{simple_channel, simple_chat};
use crate::api::{
    context::{SearchAuthorizationService, SearchHandlerState},
    search::{SearchPaginationParams, simple::SearchError},
};
use axum::{
    extract::{self, State},
    response::Json,
};
use crm::domain::auth::CrmTeamReceipt;
use entity_access::domain::models::MemberTeamRole;
use macro_authorization::{MacroAuthorizationExtractor, UserOrInternal};
use macro_user_id::user_id::MacroUserId;
use model::{response::ErrorResponse, user::UserContext};
use models_search::unified::SearchEntityFilters;
use models_search::{MatchType, SearchOn, SimpleSearchResponse, unified::UnifiedSearchRequest};
use models_search_cursor::{SearchCursor, SearchCursorOption, SearchMethodCursor};
use opensearch_client::search::agent_sessions::{AgentSessionSearchArgs, AgentSessionSearchMode};
use opensearch_client::search::call_records::CallRecordSearchMode;
use opensearch_client::search::documents::{
    DocumentSearchMode, PropertyDateRange, PropertyFilterArg,
};
use opensearch_client::search::model::SearchHit;
use opensearch_client::search::unified::UnifiedSearchArgs;

/// Whether calendar events may appear in this search.
///
/// The flag overrides the request, never the reverse. The AI search tools
/// default `entityTypes` to empty — which means every type — so an environment
/// whose calendar index has not been created and backfilled cannot rely on
/// callers opting out.
fn calendar_events_searchable(requested: bool, flag_enabled: bool) -> bool {
    requested && flag_enabled
}

/// Identifies the source of a search result for cursor regeneration
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum SearchSource {
    ChatName,
    ChannelName,
    Content,
    CrmCompany,
}

/// Wrapper for SearchHit that tracks its source for cursor regeneration
struct TaggedSearchHit {
    hit: SearchHit,
    source: SearchSource,
}

/// Find the last TaggedSearchHit matching a given source
fn find_last_of_source(
    results: &[TaggedSearchHit],
    source: SearchSource,
) -> Option<&TaggedSearchHit> {
    results.iter().rev().find(|h| h.source == source)
}

/// Generate a cursor from a TaggedSearchHit.
fn cursor_from_tagged(tagged: &TaggedSearchHit) -> Option<SearchMethodCursor> {
    tagged
        .hit
        .updated_at
        .map(|ts| SearchMethodCursor::UpdatedAt {
            entity_id: tagged.hit.entity_id,
            updated_at: ts,
        })
}

/// Trim the merged, sorted hit list to the first `page_size` distinct entities,
/// keeping every hit of an included entity. The page budget is entities, not
/// hits: a content document expands to one hit per matching chunk and an email
/// thread to one per matching message, so counting hits here lets a single
/// chunk-heavy entity exhaust the budget and short the page.
fn take_first_n_entities(combined: Vec<TaggedSearchHit>, page_size: usize) -> Vec<TaggedSearchHit> {
    let mut page_entities = std::collections::HashSet::new();
    combined
        .into_iter()
        .filter(|tagged| {
            if page_entities.contains(&tagged.hit.entity_id) {
                true
            } else if page_entities.len() < page_size {
                page_entities.insert(tagged.hit.entity_id);
                true
            } else {
                false
            }
        })
        .collect()
}

/// Count distinct entities among `hits` (multiple hits can share one entity_id —
/// content chunks of a document, messages of an email thread).
fn distinct_entity_count<'a>(hits: impl IntoIterator<Item = &'a SearchHit>) -> usize {
    hits.into_iter()
        .map(|h| h.entity_id)
        .collect::<std::collections::HashSet<_>>()
        .len()
}

/// Computes the next cursor for a search source based on pagination state.
///
/// `included_count`/`original_count` are measured in distinct entities, not hits,
/// so a chunk-heavy source is not falsely judged truncated.
fn compute_next_cursor(
    next_cursor_from_search: &SearchCursorOption,
    included_count: usize,
    original_count: usize,
    last_included_hit: Option<&TaggedSearchHit>,
    original_cursor: &SearchCursorOption,
) -> SearchCursorOption {
    // `included_count < original_count` means the cross-source merge dropped some
    // of this source's entities to stay within the page budget — they must be
    // re-surfaced next page even when the sub-search itself reported Done, so the
    // sub-search reporting Done is not on its own enough to end this source.
    let truncated_in_merge = included_count < original_count;

    if !truncated_in_merge && next_cursor_from_search.is_done() {
        SearchCursorOption::Done
    } else if truncated_in_merge || next_cursor_from_search.has_more() {
        if included_count > 0 {
            SearchCursorOption::NotDone(last_included_hit.and_then(cursor_from_tagged))
        } else {
            original_cursor.clone()
        }
    } else {
        SearchCursorOption::Done
    }
}

use crate::api::search::terms::{drop_wordless_terms, record_term_counts, split_search_terms};

/// Per-request cap on whitespace-delimited search terms. Each term expands
/// into its own `has_child` clause and is embedded in every clause's highlight
/// query, so query-build cost is quadratic in the term count across the
/// join-shape indices — an unbounded paste can OOM the service.
const MAX_SEARCH_TERMS: usize = 50;

/// Per-term character cap. Body size also scales with summed term length; real
/// tokens fall well under this, so longer ones are truncated rather than
/// rejected.
const MAX_TERM_CHARS: usize = 80;

/// Enforce [`MAX_SEARCH_TERMS`] and [`MAX_TERM_CHARS`] on tokenized terms
/// before they fan out into per-index OpenSearch queries.
fn enforce_term_limits(terms: Vec<String>) -> Result<Vec<String>, SearchError> {
    if terms.len() > MAX_SEARCH_TERMS {
        return Err(SearchError::TooManyTerms);
    }
    Ok(terms
        .into_iter()
        .map(|term| match term.char_indices().nth(MAX_TERM_CHARS) {
            Some((byte_idx, _)) => term[..byte_idx].to_string(),
            None => term,
        })
        .collect())
}

/// Convert request property filters into OpenSearch property-filter args.
///
/// `entity_type` is dropped: the indexed `properties` field carries only
/// `definition_id` + `values`, and definition ids are globally unique, so the
/// entity type adds nothing to the query. Select-option UUIDs and entity-ref
/// ids both match against `values`. Filters with no values are skipped.
fn to_property_filter_args(filters: &[item_filters::PropertyFilter]) -> Vec<PropertyFilterArg> {
    filters
        .iter()
        .filter_map(|f| {
            let values: Vec<String> = f
                .option_ids
                .iter()
                .chain(f.entity_ids.iter())
                .cloned()
                .collect();
            if values.is_empty() && f.date_range.is_none() && f.has_date.is_none() {
                return None;
            }
            Some(PropertyFilterArg {
                definition_id: f.property_definition_id.clone(),
                values,
                date_range: f.date_range.as_ref().map(|range| PropertyDateRange {
                    start: range.start,
                    end: range.end,
                }),
                has_date: f.has_date,
            })
        })
        .collect()
}

/// Creates a unified search request and performs the search
/// by calling individual simple search endpoints for each entity type
#[tracing::instrument(
    skip(ctx, user_context, query_params),
    fields(
        user_id = %user_context.user_id,
        search.match_type = %req.match_type,
        search.terms = tracing::field::Empty,
        search.dropped_terms = tracing::field::Empty,
    ),
    err
)]
pub(in crate::api::search) async fn perform_unified_search(
    ctx: &SearchHandlerState,
    user_context: &UserContext,
    crm_access: Option<&CrmTeamReceipt<MemberTeamRole>>,
    query_params: SearchPaginationParams,
    req: UnifiedSearchRequest,
) -> Result<
    (
        Vec<opensearch_client::search::model::SearchHit>,
        Option<String>,
    ),
    SearchError,
> {
    let user_id = user_context.user_id.clone();

    if user_id.is_empty() {
        return Err(SearchError::NoUserId);
    }
    let user_id = MacroUserId::parse_from_str(&user_id)
        .map_err(|_| SearchError::InvalidUserId(user_id.to_string()))?
        .lowercase();

    let search_on = req.search_on;
    let user_organization_id = user_context.organization_id;
    let collapse = req.collapse.unwrap_or(false);

    // Parse cursor from query params
    let cursor: Option<SearchCursor> = query_params
        .cursor
        .as_ref()
        .and_then(|c| SearchCursor::decode(c));

    if let Some(cursor) = cursor.as_ref()
        && cursor.is_exhausted()
    {
        return Err(SearchError::InvalidCursor);
    }

    let page_size = query_params.page_size.unwrap_or(10);
    if !(0..=100).contains(&page_size) {
        return Err(SearchError::InvalidPageSize);
    }

    let query = req.query.trim().to_string();
    if query.len() < 3 {
        return Err(SearchError::InvalidQuerySize);
    }
    let terms: Vec<String> = vec![query];
    let match_type = req.match_type;
    let split_terms = split_search_terms(&terms);
    let split_count = split_terms.len();
    let search_terms = enforce_term_limits(drop_wordless_terms(split_terms, match_type))?;
    record_term_counts(
        &tracing::Span::current(),
        search_terms.len(),
        split_count - search_terms.len(),
    );

    // Property filters live at the top level of the request and only apply to
    // the OpenSearch documents index, so capture them before the conversion
    // (which drops them) and attach them to the document search args below.
    // Tags, by contrast, are indexed on every taggable source, each of which
    // applies the tag filter itself (see the per-source gating below).
    let property_filter_args = to_property_filter_args(&req.filters.property_filters);
    let tag_option_ids = req.filters.tag_option_ids.clone();
    let match_all_tags = req.filters.tag_filter_mode == item_filters::TagFilterMode::All;
    // A tag filter keeps only the tag-indexed sources; channels and CRM drop
    // out below (see should_include_channels), so response pages contain only
    // rows the filter was actually applied to.
    let tags_active = !tag_option_ids.is_empty();

    // CRM is opt-in: it only runs when the caller resolved a team receipt
    // (set only when `include_crm` is true and the user has a qualifying
    // membership). `crm_company_filters` scopes/selects within the team.
    let crm_company_filters = req.filters.crm_company_filters.clone();
    let should_include_crm = crm_access.is_some() && !tags_active;

    let calendar_event_scope = req.calendar_event_scope;
    let search_filters = SearchEntityFilters::from(req.filters);
    let requested_sessions = search_filters
        .agent_session_filters
        .ids
        .iter()
        .map(|id| {
            uuid::Uuid::parse_str(id).map_err(|_| SearchError::InvalidAgentSessionId(id.clone()))
        })
        .collect::<Result<Vec<_>, _>>()?;
    let agent_sessions = ctx
        .agent_session_search()
        .scope(
            &user_id,
            &requested_sessions,
            &search_filters.agent_session_filters.owners,
            search_filters.should_include_agent_sessions,
            tags_active,
        )
        .await
        .map_err(|error| SearchError::InternalError(anyhow::anyhow!("{error}")))?;
    let channel_filters = search_filters.channel_filters;
    let email_filters = search_filters.email_filters;
    let chat_filters = search_filters.chat_filters;
    let doc_filters = search_filters.document_filters;
    let project_filters = search_filters.project_filters;
    let call_filters = search_filters.call_filters;
    let calendar_event_filters = ScopedCalendarEventFilters {
        filters: search_filters.calendar_event_filters,
        scope: calendar_event_scope,
    };

    let should_include_documents = search_filters.should_include_documents;
    // A tag filter drops channels (and CRM, below) because they aren't
    // tag-indexed. Every tag-indexed source — documents, chats, projects,
    // emails, call records, calendar events — stays included and applies the
    // filter itself.
    let should_include_channels = search_filters.should_include_channels && !tags_active;
    // Channel names are searched in Postgres below; only a content search
    // needs the viewer's channel ids for the OpenSearch messages index.
    let should_search_channel_messages =
        should_include_channels && !matches!(search_on, SearchOn::Name);
    let should_include_chats = search_filters.should_include_chats;
    let should_include_projects = search_filters.should_include_projects;
    let should_include_emails = search_filters.should_include_emails;
    let should_include_call_records = search_filters.should_include_call_records;
    let should_include_calendar_events = calendar_events_searchable(
        search_filters.should_include_calendar_events,
        ctx.calendar_search_enabled,
    );
    let email_terms = search_terms.clone();

    // Await all tasks in parallel
    let (
        mut filter_document_response,
        mut filter_channel_response,
        mut filter_chat_response,
        mut filter_email_response,
        mut filter_project_response,
        mut filter_call_record_response,
        mut filter_calendar_event_response,
    ) = tokio::try_join!(
        doc_filters.filter_to_search_args(
            ctx,
            user_id.as_ref(),
            user_organization_id,
            should_include_documents,
        ),
        channel_filters.filter_to_search_args(
            ctx,
            user_id.as_ref(),
            user_organization_id,
            should_search_channel_messages,
        ),
        chat_filters.filter_to_search_args(
            ctx,
            user_id.as_ref(),
            user_organization_id,
            should_include_chats,
        ),
        email_filters.filter_to_search_args(
            ctx,
            user_id.as_ref(),
            user_organization_id,
            should_include_emails,
        ),
        project_filters.filter_to_search_args(
            ctx,
            user_id.as_ref(),
            user_organization_id,
            should_include_projects
        ),
        call_filters.filter_to_search_args(
            ctx,
            user_id.as_ref(),
            user_organization_id,
            should_include_call_records,
        ),
        calendar_event_filters.filter_to_search_args(
            ctx,
            user_id.as_ref(),
            user_organization_id,
            should_include_calendar_events,
        )
    )
    .map_err(|e| SearchError::InternalError(anyhow::anyhow!("tokio error: {:?}", e)))?;

    // Set terms on each index's search args.
    //
    // Every content index gets whitespace-split terms. Emails match each
    // term independently across many fields ANDed inside OpenSearch;
    // channel messages are single-doc-per-message so each token must
    // appear in the same message via bool.must. Documents, chats, and
    // call records are join-shape, where each term becomes a separate
    // has_child clause ANDed via bool.must. Projects and calendar events are
    // flat and match terms against their name/title only.
    filter_document_response.terms = search_terms.clone();
    filter_document_response.property_filters = property_filter_args;
    filter_document_response.tag_option_ids = tag_option_ids.clone();
    filter_document_response.match_all_tags = match_all_tags;
    filter_channel_response.terms = search_terms.clone();
    filter_chat_response.terms = search_terms.clone();
    filter_chat_response.tag_option_ids = tag_option_ids.clone();
    filter_chat_response.match_all_tags = match_all_tags;
    filter_email_response.terms = email_terms.clone();
    filter_email_response.tag_option_ids = tag_option_ids.clone();
    filter_email_response.match_all_tags = match_all_tags;
    filter_call_record_response.terms = search_terms.clone();
    filter_call_record_response.tag_option_ids = tag_option_ids.clone();
    filter_call_record_response.match_all_tags = match_all_tags;
    filter_project_response.terms = search_terms.clone();
    filter_project_response.tag_option_ids = tag_option_ids.clone();
    filter_project_response.match_all_tags = match_all_tags;
    filter_calendar_event_response.terms = search_terms.clone();
    filter_calendar_event_response.tag_option_ids = tag_option_ids;
    filter_calendar_event_response.match_all_tags = match_all_tags;

    // Widen the email access filter to every inbox the caller can reach (their
    // own plus delegated). Connected secondary inboxes share the owner's
    // user_id; delegated inboxes index under their own, so collect the
    // distinct macro_ids the caller can search.
    if should_include_emails {
        let inboxes =
            email_db_client::links::get::fetch_inboxes_for_macro_id(&ctx.db, user_id.as_ref())
                .await
                .map_err(SearchError::InternalError)?;
        let mut email_user_ids: Vec<String> =
            inboxes.iter().map(|l| l.macro_id.to_string()).collect();
        email_user_ids.sort();
        email_user_ids.dedup();
        filter_email_response.user_ids = email_user_ids;
    }

    // Clone terms for use in name searches
    let name_search_term = terms[0].clone();

    // Extract individual cursors from the combined cursor (SearchCursorOption)
    // Clone for use in async blocks and for cursor regeneration
    let chat_cursor = cursor
        .as_ref()
        .map(|c| c.chat_name_cursor.clone())
        .unwrap_or_default();

    let content_cursor = cursor
        .as_ref()
        .map(|c| c.content_cursor.clone())
        .unwrap_or_default();

    let crm_cursor = cursor
        .as_ref()
        .map(|c| c.crm_company_cursor.clone())
        .unwrap_or_default();

    let channel_name_cursor = cursor
        .as_ref()
        .map(|c| c.channel_name_cursor.clone())
        .unwrap_or_default();

    // Clone cursors for passing to search functions (originals needed for cursor regeneration)
    let chat_cursor_for_search = chat_cursor.clone();
    let content_cursor_for_search = content_cursor.clone();
    let crm_cursor_for_search = crm_cursor.clone();
    let channel_name_cursor_for_search = channel_name_cursor.clone();

    let unified_search_args = UnifiedSearchArgs {
        agent_session_search_args: AgentSessionSearchArgs {
            terms: search_terms.clone(),
            session_ids: agent_sessions
                .iter()
                .map(|session| session.id.to_string())
                .collect(),
            mode: match search_on {
                SearchOn::Name => AgentSessionSearchMode::Name,
                SearchOn::Content => AgentSessionSearchMode::Content,
                SearchOn::NameContent => AgentSessionSearchMode::NameContent,
            },
        },
        user_id: user_id.as_ref().to_string(),
        page: 0, // With cursor-based pagination, we always start from "page 0" relative to cursor
        page_size,
        cursor: content_cursor_for_search,
        match_type: match_type.to_string(),
        collapse,
        search_indices: {
            let mut indices = std::collections::HashSet::new();
            if !agent_sessions.is_empty() {
                indices.insert(models_opensearch::OpenSearchEntityType::AgentSessions);
            }
            if should_include_documents
                && !(filter_document_response.ids_only
                    && filter_document_response.document_ids.is_empty())
            {
                indices.insert(models_opensearch::OpenSearchEntityType::Documents);
            }
            if should_include_chats
                && !(filter_chat_response.ids_only && filter_chat_response.chat_ids.is_empty())
            {
                indices.insert(models_opensearch::OpenSearchEntityType::Chats);
            }
            if should_include_emails {
                indices.insert(models_opensearch::OpenSearchEntityType::Emails);
            }
            if should_search_channel_messages && !filter_channel_response.channel_ids.is_empty() {
                indices.insert(models_opensearch::OpenSearchEntityType::Channels);
            }
            if should_include_call_records && !filter_call_record_response.call_ids.is_empty() {
                indices.insert(models_opensearch::OpenSearchEntityType::CallRecords);
            }
            if should_include_projects
                && !(filter_project_response.ids_only
                    && filter_project_response.project_ids.is_empty())
            {
                indices.insert(models_opensearch::OpenSearchEntityType::Projects);
            }
            // An event set narrowed to nothing would be rejected by the query
            // builder rather than matching everything, so drop the index.
            if should_include_calendar_events
                && !(filter_calendar_event_response.ids_only
                    && filter_calendar_event_response.calendar_event_ids.is_empty())
            {
                indices.insert(models_opensearch::OpenSearchEntityType::CalendarEvents);
            }
            indices
        },
        document_search_args: filter_document_response.clone(),
        email_search_args: filter_email_response.clone(),
        channel_message_search_args: filter_channel_response,
        chat_search_args: filter_chat_response.clone(),
        call_record_search_args: filter_call_record_response.clone(),
        project_search_args: filter_project_response,
        calendar_event_search_args: filter_calendar_event_response,
    };

    // Call search functions in parallel for included entity types
    // search_names handles Done cursors internally by returning early
    let (chat_results, channel_results, content_results, crm_results) = tokio::join!(
        async {
            if should_include_chats {
                match search_on {
                    SearchOn::Name | SearchOn::NameContent => {
                        simple_chat::search_names(
                            &ctx.db,
                            &user_id,
                            &simple_chat::FilterChatResponse {
                                ids_only: filter_chat_response.ids_only,
                                chat_ids: filter_chat_response.chat_ids,
                            },
                            name_search_term.clone(),
                            &filter_chat_response.tag_option_ids,
                            filter_chat_response.match_all_tags,
                            page_size,
                            chat_cursor_for_search,
                        )
                        .await
                    }
                    SearchOn::Content => Ok((vec![], SearchCursorOption::Done)),
                }
            } else {
                Ok((vec![], SearchCursorOption::Done))
            }
        },
        async {
            // Channel names depend on the viewer (a DM is named after the
            // other participant), so they are matched in Postgres rather than
            // the message-level channels index.
            match search_on {
                SearchOn::Name | SearchOn::NameContent if should_include_channels => {
                    simple_channel::search_names(
                        &ctx.db,
                        &user_id,
                        &channel_filters,
                        name_search_term.clone(),
                        match_type == MatchType::Exact,
                        page_size,
                        channel_name_cursor_for_search,
                    )
                    .await
                }
                _ => Ok((vec![], SearchCursorOption::Done)),
            }
        },
        async {
            // Documents, email subjects, project names, call names, and
            // calendar event titles are name-searched in OpenSearch. In Name
            // mode keep only those indices: chats and channels name-search in
            // Postgres above. Documents match `document_name`, emails their
            // subject, projects their `name`, calls their `name`, calendar
            // events their `title`. Projects and calendar events have no
            // content, so a content-only search drops them.
            let mut args = unified_search_args;
            match search_on {
                SearchOn::Name => {
                    args.document_search_args.mode = DocumentSearchMode::Name;
                    args.call_record_search_args.mode = CallRecordSearchMode::Name;
                    args.email_search_args.subject_only = true;
                    args.search_indices.retain(|i| {
                        *i == models_opensearch::OpenSearchEntityType::Documents
                            || *i == models_opensearch::OpenSearchEntityType::AgentSessions
                            || *i == models_opensearch::OpenSearchEntityType::Emails
                            || *i == models_opensearch::OpenSearchEntityType::Projects
                            || *i == models_opensearch::OpenSearchEntityType::CallRecords
                            || *i == models_opensearch::OpenSearchEntityType::CalendarEvents
                    });
                }
                SearchOn::NameContent => {
                    args.document_search_args.mode = DocumentSearchMode::NameContent;
                    args.call_record_search_args.mode = CallRecordSearchMode::NameContent;
                }
                SearchOn::Content => {
                    args.document_search_args.mode = DocumentSearchMode::Content;
                    args.search_indices
                        .remove(&models_opensearch::OpenSearchEntityType::Projects);
                    args.search_indices
                        .remove(&models_opensearch::OpenSearchEntityType::CalendarEvents);
                }
            }
            if args.search_indices.is_empty() {
                Ok((vec![], SearchCursorOption::Done))
            } else if let SearchCursorOption::Done = args.cursor {
                Ok((vec![], SearchCursorOption::Done))
            } else {
                ctx.opensearch_client.search_unified(args).await
            }
        },
        async {
            // CRM companies are name/domain only — no content index — so
            // they only participate under Name / NameContent.
            match (should_include_crm, crm_access, search_on) {
                (true, Some(access), SearchOn::Name | SearchOn::NameContent) => {
                    crm_company::search_company_names(
                        ctx,
                        access,
                        &crm_company_filters,
                        name_search_term.clone(),
                        page_size,
                        crm_cursor_for_search,
                    )
                    .await
                }
                _ => Ok((vec![], SearchCursorOption::Done)),
            }
        },
    );

    // Extract results and next cursors
    let (chat_hits, chat_next_cursor) = chat_results?;
    let (channel_hits, channel_next_cursor) = channel_results?;
    let (content_hits, content_next_cursor) = content_results?;
    let (crm_hits, crm_next_cursor) = crm_results?;

    // Track original counts before combining
    let chat_name_count = chat_hits.len();
    let channel_name_count = channel_hits.len();
    // Content hits are chunk-level (many per document), so the page budget counts
    // distinct documents. Name/crm sub-searches return one row per entity, so
    // their hit count already equals their entity count.
    let content_count = distinct_entity_count(&content_hits);
    let crm_count = crm_hits.len();

    let final_tagged = {
        // Wrap results with source tags
        let mut combined: Vec<TaggedSearchHit> = Vec::new();
        combined.extend(chat_hits.into_iter().map(|hit| TaggedSearchHit {
            hit,
            source: SearchSource::ChatName,
        }));
        combined.extend(channel_hits.into_iter().map(|hit| TaggedSearchHit {
            hit,
            source: SearchSource::ChannelName,
        }));
        combined.extend(content_hits.into_iter().map(|hit| TaggedSearchHit {
            hit,
            source: SearchSource::Content,
        }));
        combined.extend(crm_hits.into_iter().map(|hit| TaggedSearchHit {
            hit,
            source: SearchSource::CrmCompany,
        }));

        // Sort: updated_at DESC (None to bottom), entity_id DESC as tiebreaker
        combined.sort_by(|a, b| match (&b.hit.updated_at, &a.hit.updated_at) {
            (Some(b_ts), Some(a_ts)) => b_ts
                .cmp(a_ts)
                .then_with(|| b.hit.entity_id.cmp(&a.hit.entity_id)),
            (Some(_), None) => std::cmp::Ordering::Less,
            (None, Some(_)) => std::cmp::Ordering::Greater,
            (None, None) => b.hit.entity_id.cmp(&a.hit.entity_id),
        });

        // Take the first page_size distinct entities (keeping all of each
        // entity's hits), not the first page_size hits.
        let page_size_usize = page_size as usize;
        take_first_n_entities(combined, page_size_usize)
    };

    let next_cursor = {
        // Count included results by source
        let included_chat_names = final_tagged
            .iter()
            .filter(|h| h.source == SearchSource::ChatName)
            .count();
        let included_channel_names = final_tagged
            .iter()
            .filter(|h| h.source == SearchSource::ChannelName)
            .count();
        let included_content = distinct_entity_count(
            final_tagged
                .iter()
                .filter(|h| h.source == SearchSource::Content)
                .map(|h| &h.hit),
        );
        let included_crm = final_tagged
            .iter()
            .filter(|h| h.source == SearchSource::CrmCompany)
            .count();

        // Generate new cursors using helper function
        let new_chat_cursor = compute_next_cursor(
            &chat_next_cursor,
            included_chat_names,
            chat_name_count,
            find_last_of_source(&final_tagged, SearchSource::ChatName),
            &chat_cursor,
        );

        let new_channel_cursor = compute_next_cursor(
            &channel_next_cursor,
            included_channel_names,
            channel_name_count,
            find_last_of_source(&final_tagged, SearchSource::ChannelName),
            &channel_name_cursor,
        );

        let new_content_cursor = compute_next_cursor(
            &content_next_cursor,
            included_content,
            content_count,
            find_last_of_source(&final_tagged, SearchSource::Content),
            &content_cursor,
        );

        let new_crm_cursor = compute_next_cursor(
            &crm_next_cursor,
            included_crm,
            crm_count,
            find_last_of_source(&final_tagged, SearchSource::CrmCompany),
            &crm_cursor,
        );

        // Build next cursor if any source has more results
        let has_more = new_chat_cursor.has_more()
            || new_channel_cursor.has_more()
            || new_content_cursor.has_more()
            || new_crm_cursor.has_more();

        if has_more {
            let cursor = SearchCursor {
                chat_name_cursor: new_chat_cursor,
                content_cursor: new_content_cursor,
                // Projects ride the OpenSearch content cursor; the field
                // stays only so older cursors still decode.
                project_name_cursor: SearchCursorOption::Done,
                crm_company_cursor: new_crm_cursor,
                channel_name_cursor: new_channel_cursor,
            };
            cursor.encode()
        } else {
            None
        }
    };

    // Extract final SearchHits from tagged results
    let final_results: Vec<SearchHit> = final_tagged.into_iter().map(|t| t.hit).collect();

    Ok((final_results, next_cursor))
}

/// Perform a search through all items.
/// This is a simple search where we do not group your results by entity id.
#[utoipa::path(
    post,
    path = "/search/simple",
    operation_id = "simple_unified_search",
    params(
            ("page_size" = i64, Query, description = "The page size. Defaults to 10."),
            ("cursor" = Option<String>, Query, description = "Base64 encoded cursor for pagination."),
    ),
    responses(
            (status = 200, body=SimpleSearchResponse),
            (status = 400, body=ErrorResponse),
            (status = 401, body=ErrorResponse),
            (status = 500, body=ErrorResponse),
    )
)]
pub async fn handler(
    State(ctx): State<SearchHandlerState>,
    authorization: MacroAuthorizationExtractor<SearchAuthorizationService, UserOrInternal>,
    extract::Query(query_params): extract::Query<SearchPaginationParams>,
    extract::Json(req): extract::Json<UnifiedSearchRequest>,
) -> Result<Json<SimpleSearchResponse>, SearchError> {
    let user_context = &authorization.authorization.user.user_context;

    tracing::info!(
        user_id = user_context.user_id,
        query = ?req.query,
        search_on = ?req.search_on,
        "simple_unified_search"
    );

    // CRM opt-in is wired through the unified `/search` endpoint only.
    let (results, _next_cursor) =
        perform_unified_search(&ctx, user_context, None, query_params, req).await?;

    let results = results.into_iter().map(|a| a.into()).collect();

    // Note: SimpleSearchResponse doesn't have a next_cursor field
    // The cursor is returned by the unified search endpoint (/search)
    Ok(Json(SimpleSearchResponse { results }))
}

#[cfg(test)]
mod test;