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.

22.2 KB
Raw Blame
use crate::{
    domain::{
        models::{
            AdvancedSortParams, AttentionCandidate, AttentionCandidateRequest, CrmPropertyEdge,
            DynamicSort, GroupedSortRequest, NotifiedEntity, NotifiedSoupRequest, SimpleSortQuery,
            SimpleSortRequest, SoupProjectionHydration, SoupPropertiesField, TouchedEntity,
            TouchedSoupRequest, grouping::ItemGroupingInfo,
        },
        ports::SoupRepo,
    },
    outbound::pg_soup_repo::expanded::dynamic::{
        ExpandedDynamicCursorArgs, GroupedDynamicCursorArgs,
    },
};
use item_filters::ast::EntityFilterAst;
use macro_user_id::user_id::MacroUserIdStr;
use models_pagination::{Identify, SortOn};
use models_properties::service::property_definition_with_options::PropertyDefinitionWithOptions;
use models_soup::{SoupProperty, item::SoupItem};
use readonly_pool::ReadOnlyPool;
use system_properties::SystemPropertyKey;

mod agent_session;
mod attention;
mod calendar_event;
mod candidate_gates;
mod crm_association;
mod database_row;
mod expanded;
pub mod grouping;
mod initiative;
mod notified;
mod touched;
mod unexpanded;

/// PostgreSQL implementation of [`SoupRepo`].
pub struct PgSoupRepo {
    pool: ReadOnlyPool,
}

impl PgSoupRepo {
    /// Creates a PostgreSQL soup repository using the provided read-only pool.
    pub fn new(pool: ReadOnlyPool) -> Self {
        PgSoupRepo { pool }
    }
}

impl SoupRepo for PgSoupRepo {
    type Err = sqlx::Error;
    type GroupedItems = std::vec::IntoIter<ItemGroupingInfo>;

    async fn expanded_generic_cursor_soup<'a>(
        &self,
        req: SimpleSortRequest<'a>,
    ) -> Result<Vec<SoupItem<()>>, Self::Err> {
        Ok(self
            .expanded_generic_cursor_soup_with_projection(req)
            .await?
            .into_iter()
            .map(|hydration| hydration.item)
            .collect())
    }

    async fn expanded_generic_cursor_soup_with_projection<'a>(
        &self,
        req: SimpleSortRequest<'a>,
    ) -> Result<Vec<SoupProjectionHydration>, Self::Err> {
        let cursor_request = req.clone();
        let sort = *req.cursor.sort_method();
        let limit = req.limit;
        let mut items = match req.cursor {
            SimpleSortQuery::Property(cursor) => {
                return expanded::dynamic::expanded_dynamic_cursor_soup_with_projection(
                    &self.pool.0,
                    ExpandedDynamicCursorArgs {
                        user_id: req.user_id,
                        limit: req.limit,
                        cursor,
                        exclude_frecency: false,
                    },
                )
                .await;
            }
            SimpleSortQuery::ItemsAndFrecencyFilter(query) => {
                // Extract the EntityFilterAst from the tuple (Frecency, EntityFilterAst)
                expanded::dynamic::expanded_dynamic_cursor_soup_with_projection(
                    &self.pool.0,
                    ExpandedDynamicCursorArgs {
                        user_id: req.user_id,
                        limit: req.limit,
                        cursor: DynamicSort::from_timestamp_query(query.map_filter(|(_, ast)| ast)),
                        exclude_frecency: true,
                    },
                )
                .await?
            }
            SimpleSortQuery::ItemsFilter(ast) => {
                expanded::dynamic::expanded_dynamic_cursor_soup_with_projection(
                    &self.pool.0,
                    ExpandedDynamicCursorArgs {
                        user_id: req.user_id,
                        limit: req.limit,
                        cursor: DynamicSort::from_timestamp_query(ast),
                        exclude_frecency: false,
                    },
                )
                .await?
            }
            // The unfiltered arms route through the same dynamic builder as
            // the filtered ones. An empty `EntityFilterAst` folds to no
            // predicates and keeps all three entity arms, so the SQL is
            // semantically identical to the hand-written queries this
            // replaced — but it gets their shape: a lightweight `TopItems`
            // stage that applies the cursor and LIMIT before any detail join,
            // a flattenable per-arm `entity_access` semi-join instead of a
            // materialised whole-corpus CTE, and a sort expression fixed at
            // build time so the ORDER BY can be served by an index.
            SimpleSortQuery::FilterFrecency(f) => {
                expanded::dynamic::expanded_dynamic_cursor_soup_with_projection(
                    &self.pool.0,
                    ExpandedDynamicCursorArgs {
                        user_id: req.user_id,
                        limit: req.limit,
                        cursor: DynamicSort::from_timestamp_query(
                            f.map_filter(|_| EntityFilterAst::default()),
                        ),
                        exclude_frecency: true,
                    },
                )
                .await?
            }
            SimpleSortQuery::NoFilter(f) => {
                expanded::dynamic::expanded_dynamic_cursor_soup_with_projection(
                    &self.pool.0,
                    ExpandedDynamicCursorArgs {
                        user_id: req.user_id,
                        limit: req.limit,
                        cursor: DynamicSort::from_timestamp_query(
                            f.map_filter(|_| EntityFilterAst::default()),
                        ),
                        exclude_frecency: false,
                    },
                )
                .await?
            }
        };
        items.extend(
            calendar_event::cursor_soup(&self.pool.0, cursor_request.clone())
                .await?
                .into_iter()
                .map(|item| SoupProjectionHydration {
                    item,
                    document_server_facts: None,
                    sort_key: None,
                }),
        );
        items.extend(
            agent_session::cursor_soup(&self.pool.0, cursor_request)
                .await?
                .into_iter()
                .map(|item| SoupProjectionHydration {
                    item,
                    document_server_facts: None,
                    sort_key: None,
                }),
        );
        sort_and_truncate_hydrations(&mut items, sort, limit);
        Ok(items)
    }

    async fn unexpanded_generic_cursor_soup<'a>(
        &self,
        req: SimpleSortRequest<'a>,
    ) -> Result<Vec<SoupItem<()>>, Self::Err> {
        let cursor_request = req.clone();
        let sort = *req.cursor.sort_method();
        let limit = req.limit;
        let mut items = match req.cursor {
            SimpleSortQuery::Property(_) => return self.expanded_generic_cursor_soup(req).await,
            SimpleSortQuery::ItemsFilter(_) => not_implemented(req).await?,
            SimpleSortQuery::ItemsAndFrecencyFilter(_) => not_implemented(req).await?,
            // Same substitution as the expanded path above; this arm has
            // always answered with the expanded query.
            SimpleSortQuery::FilterFrecency(f) => {
                expanded::dynamic::expanded_dynamic_cursor_soup_with_projection(
                    &self.pool.0,
                    ExpandedDynamicCursorArgs {
                        user_id: req.user_id,
                        limit: req.limit,
                        cursor: DynamicSort::from_timestamp_query(
                            f.map_filter(|_| EntityFilterAst::default()),
                        ),
                        exclude_frecency: true,
                    },
                )
                .await?
                .into_iter()
                .map(|hydration| hydration.item)
                .collect()
            }
            SimpleSortQuery::NoFilter(f) => {
                unexpanded::by_cursor::unexpanded_generic_cursor_soup(
                    &self.pool.0,
                    req.user_id,
                    req.limit,
                    f,
                )
                .await?
            }
        };
        items.extend(calendar_event::cursor_soup(&self.pool.0, cursor_request.clone()).await?);
        items.extend(agent_session::cursor_soup(&self.pool.0, cursor_request).await?);
        sort_and_truncate(&mut items, sort, limit);
        Ok(items)
    }

    async fn expanded_soup_by_ids<'a>(
        &self,
        req: AdvancedSortParams<'a>,
    ) -> Result<Vec<SoupItem<()>>, Self::Err> {
        Ok(self
            .expanded_soup_by_ids_with_projection(req)
            .await?
            .into_iter()
            .map(|hydration| hydration.item)
            .collect())
    }

    async fn expanded_soup_by_ids_with_projection<'a>(
        &self,
        req: AdvancedSortParams<'a>,
    ) -> Result<Vec<SoupProjectionHydration>, Self::Err> {
        let by_ids_request = req.clone();
        let mut items = expanded::by_ids::expanded_soup_by_ids_with_projection(
            &self.pool.0,
            req.user_id,
            req.entities,
        )
        .await?;
        items.extend(
            calendar_event::by_ids(&self.pool.0, by_ids_request.clone())
                .await?
                .into_iter()
                .map(|item| SoupProjectionHydration {
                    item,
                    document_server_facts: None,
                    sort_key: None,
                }),
        );
        items.extend(
            agent_session::by_ids(&self.pool.0, by_ids_request.clone())
                .await?
                .into_iter()
                .map(|item| SoupProjectionHydration {
                    item,
                    document_server_facts: None,
                    sort_key: None,
                }),
        );
        items.extend(
            initiative::by_ids(&self.pool.0, by_ids_request.clone())
                .await?
                .into_iter()
                .map(|item| SoupProjectionHydration {
                    item,
                    document_server_facts: None,
                    sort_key: None,
                }),
        );
        items.extend(
            database_row::by_ids(&self.pool.0, by_ids_request)
                .await?
                .into_iter()
                .map(|item| SoupProjectionHydration {
                    item,
                    document_server_facts: None,
                    sort_key: None,
                }),
        );
        Ok(items)
    }

    async fn unexpanded_soup_by_ids<'a>(
        &self,
        req: AdvancedSortParams<'a>,
    ) -> Result<Vec<SoupItem<()>>, Self::Err> {
        let by_ids_request = req.clone();
        let mut items =
            unexpanded::by_ids::unexpanded_soup_by_ids(&self.pool.0, req.user_id, req.entities)
                .await?;
        items.extend(calendar_event::by_ids(&self.pool.0, by_ids_request.clone()).await?);
        items.extend(agent_session::by_ids(&self.pool.0, by_ids_request.clone()).await?);
        items.extend(initiative::by_ids(&self.pool.0, by_ids_request.clone()).await?);
        items.extend(database_row::by_ids(&self.pool.0, by_ids_request).await?);
        Ok(items)
    }

    fn populate_properties<'a>(
        &self,
        user_id: MacroUserIdStr<'a>,
        items: Vec<SoupItem<()>>,
    ) -> impl Future<Output = Result<Vec<SoupItem<SoupPropertiesField>>, Self::Err>> + Send {
        populate_properties(&self.pool.0, user_id, items)
    }

    async fn caller_tag_sets<'a>(
        &self,
        user_id: MacroUserIdStr<'a>,
    ) -> Result<Vec<PropertyDefinitionWithOptions>, Self::Err> {
        properties::outbound::property_definition_queries::get_caller_tag_definitions_with_options(
            &self.pool.0,
            user_id.as_ref(),
        )
        .await
        .map_err(|e| sqlx::Error::Decode(e.into()))
    }

    async fn expanded_grouped_cursor_soup<'a>(
        &self,
        req: GroupedSortRequest<'a>,
    ) -> Result<Self::GroupedItems, Self::Err> {
        expanded::dynamic::expanded_dynamic_cursor_soup_grouped(
            &self.pool.0,
            GroupedDynamicCursorArgs {
                user_id: req.user_id,
                limit: req.limit,
                cursor: req.cursor,
                exclude_frecency: false,
                grouping: req.grouping,
            },
        )
        .await
    }

    async fn crm_property_edges<'a>(
        &self,
        company_ids: &'a [uuid::Uuid],
        contact_ids: &'a [uuid::Uuid],
    ) -> Result<Vec<CrmPropertyEdge>, Self::Err> {
        crm_association::property_edges(&self.pool.0, company_ids, contact_ids).await
    }

    async fn touched_soup_page<'a>(
        &self,
        req: TouchedSoupRequest<'a>,
    ) -> Result<Vec<TouchedEntity>, Self::Err> {
        touched::touched_soup_page(&self.pool.0, req).await
    }

    async fn notified_soup_page<'a>(
        &self,
        req: NotifiedSoupRequest<'a>,
    ) -> Result<Vec<NotifiedEntity>, Self::Err> {
        notified::notified_soup_page(&self.pool.0, req).await
    }

    async fn attention_soup_page<'a>(
        &self,
        req: AttentionCandidateRequest<'a>,
    ) -> Result<Vec<AttentionCandidate>, Self::Err> {
        attention::attention_soup_page(&self.pool.0, req).await
    }
}

fn sort_and_truncate_hydrations(
    items: &mut Vec<SoupProjectionHydration>,
    sort: models_pagination::SimpleSortMethod,
    limit: u16,
) {
    let mut sort_on = SoupItem::sort_on(sort);
    items.sort_by(|left, right| {
        sort_on(&right.item)
            .last_val
            .cmp(&sort_on(&left.item).last_val)
            .then_with(|| right.item.id().cmp(&left.item.id()))
    });
    items.truncate(usize::from(limit));
}

fn sort_and_truncate(
    items: &mut Vec<SoupItem<()>>,
    sort: models_pagination::SimpleSortMethod,
    limit: u16,
) {
    let mut sort_on = SoupItem::sort_on(sort);
    items.sort_by(|left, right| {
        sort_on(right)
            .last_val
            .cmp(&sort_on(left).last_val)
            .then_with(|| right.id().cmp(&left.id()))
    });
    items.truncate(usize::from(limit));
}

#[tracing::instrument(err)]
async fn not_implemented<Ok>(_req: SimpleSortRequest<'_>) -> Result<Ok, sqlx::Error> {
    Err(sqlx::Error::InvalidArgument(
        "Unexpanded soup ast filters are not yet supported".to_string(),
    ))
}

/// utility fn for queries to create a sqlx err
fn type_err<E: std::fmt::Display>(e: E) -> sqlx::Error {
    sqlx::Error::TypeNotFound {
        type_name: e.to_string(),
    }
}

/// Fetches properties and attaches them to raw Soup items.
///
/// This helper collects entity references from items that support properties
/// and performs one bulk lookup. System properties are always included, plus
/// the caller's own and team tag properties and the team's CRM stage
/// definition. Tasks use `EntityType::Task` while regular documents use
/// `EntityType::Document`.
#[tracing::instrument(err, skip(db, items))]
pub(crate) async fn populate_properties(
    db: &sqlx::PgPool,
    user_id: MacroUserIdStr<'_>,
    items: Vec<SoupItem<()>>,
) -> Result<Vec<SoupItem<SoupPropertiesField>>, sqlx::Error> {
    let entity_refs = items
        .iter()
        .filter_map(SoupItem::to_entity_reference)
        .collect::<Vec<_>>();

    if entity_refs.is_empty() {
        return Ok(items
            .into_iter()
            .map(|item| item.map_extra(|()| SoupPropertiesField::default()))
            .collect());
    }

    // A database row's cells are its properties, so a row carries every one
    // of them; any other item carries the system properties and the
    // viewer's tags.
    let (row_refs, entity_refs): (Vec<_>, Vec<_>) = entity_refs
        .into_iter()
        .partition(|reference| reference.entity_type == models_properties::EntityType::DatabaseRow);
    let property_ids = SystemPropertyKey::all_system_property_keys();
    let mut properties_map =
        properties::outbound::entity_properties_get_query::get_bulk_entity_properties_values_filtered(
            db,
            &entity_refs,
            property_ids,
            Some(&user_id),
        )
        .await
        .map_err(|e| sqlx::Error::Decode(e.into()))?;
    properties_map.extend(
        properties::outbound::entity_properties_get_query::get_bulk_entity_properties_values(
            db, &row_refs,
        )
        .await
        .map_err(|e| sqlx::Error::Decode(e.into()))?,
    );

    // Items may repeat an id when grouped, so use `get` rather than `remove`.
    Ok(items
        .into_iter()
        .map(|item| {
            let properties = match &item {
                SoupItem::Document(x) => properties_map.get(&x.id.to_string()),
                SoupItem::Project(x) => properties_map.get(&x.id.to_string()),
                SoupItem::Initiative(x) => properties_map.get(&x.id.to_string()),
                SoupItem::EmailThread(x) => properties_map.get(&x.thread.id.to_string()),
                SoupItem::Chat(x) => properties_map.get(&x.id.to_string()),
                SoupItem::CrmCompany(x) => properties_map.get(&x.id.to_string()),
                SoupItem::CrmContact(_) => None,
                SoupItem::Call(x) => properties_map.get(&x.call_id.to_string()),
                SoupItem::CalendarEvent(x) => properties_map.get(&x.id.to_string()),
                SoupItem::Channel(_)
                | SoupItem::ChannelThread(_)
                | SoupItem::ForeignEntity(_)
                | SoupItem::AgentSession(_) => None,
                SoupItem::DatabaseRow(x) => properties_map.get(&x.id.to_string()),
            }
            .map(|properties| properties.iter().cloned().map(SoupProperty::from).collect())
            .unwrap_or_default();

            item.map_extra(|()| SoupPropertiesField::new(properties))
        })
        .collect())
}

/// this defines a macro which maps the soup query types for statically checked soup queries
/// This must be a macro because compile time queries cannot have a named type so we can't use a function
#[macro_export]
macro_rules! map_soup_type {
    () => {
        |r| $crate::map_soup_type!(@item r)
    };
    (@item $row:ident) => {{
        let r = $row;
        match r.item_type.as_ref() {
            "document" => Ok(::models_soup::item::SoupItem::Document(
                ::models_soup::document::SoupDocument {
                    id: Uuid::parse_str(&r.id).map_err(type_err)?,
                    document_version_id: r
                        .document_version_id
                        .ok_or_else(|| type_err("document version id must exist"))
                        .and_then(|s| FromStr::from_str(&s).map_err(type_err))?,
                    owner_id: ::model_owner::Owner::from_principal_str(&r.user_id)
                        .map_err(type_err)?,
                    name: r.name,
                    file_type: r.file_type,
                    sha: r.sha,
                    project_id: r
                        .project_id
                        .as_deref()
                        .map(Uuid::parse_str)
                        .transpose()
                        .map_err(type_err)?,
                    branched_from_id: r
                        .branched_from_id
                        .as_deref()
                        .map(Uuid::parse_str)
                        .transpose()
                        .map_err(type_err)?,
                    branched_from_version_id: r.branched_from_version_id,
                    document_family_id: r.document_family_id,
                    created_at: r.created_at,
                    updated_at: r.updated_at,
                    viewed_at: r.viewed_at,
                    sub_type: ::models_soup::document::SoupDocumentSubType::from_db(
                        r.sub_type,
                        r.is_completed,
                    ),
                    deleted_at: r.deleted_at,
                    extra: (),
                },
            )),
            "chat" => Ok(::models_soup::item::SoupItem::Chat(
                ::models_soup::chat::SoupChat {
                    id: Uuid::parse_str(&r.id).map_err(type_err)?,
                    name: r.name,
                    model: r.model,
                    owner_id: ::model_owner::Owner::from_principal_str(&r.user_id)
                        .map_err(type_err)?,
                    project_id: r
                        .project_id
                        .as_deref()
                        .map(Uuid::parse_str)
                        .transpose()
                        .map_err(type_err)?,
                    is_persistent: r.is_persistent.unwrap_or_default(),
                    created_at: r.created_at,
                    updated_at: r.updated_at,
                    viewed_at: r.viewed_at,
                    deleted_at: r.deleted_at,
                    extra: (),
                },
            )),
            "project" => Ok(::models_soup::item::SoupItem::Project(
                ::models_soup::project::SoupProject {
                    id: Uuid::parse_str(&r.id).map_err(type_err)?,
                    name: r.name,
                    owner_id: ::model_owner::Owner::from_principal_str(&r.user_id)
                        .map_err(type_err)?,
                    parent_id: r
                        .project_id
                        .as_deref()
                        .map(Uuid::parse_str)
                        .transpose()
                        .map_err(type_err)?,
                    created_at: r.created_at,
                    updated_at: r.updated_at,
                    viewed_at: r.viewed_at,
                    deleted_at: r.deleted_at,
                    extra: (),
                },
            )),
            _ => Err(sqlx::Error::TypeNotFound {
                type_name: r.item_type,
            }),
        }
    }};
}

/// Maps statically checked expanded Soup rows into an item plus authoritative
/// server-only document facts from that same row.
#[macro_export]
macro_rules! map_soup_projection_hydration {
    () => {
        |r| {
            let document_server_facts = match r.item_type.as_ref() {
                "document" => Some($crate::domain::models::SoupDocumentServerFacts {
                    is_email_attachment: r.is_email_attachment,
                    is_important: r.is_important,
                    status_option_ids: r.status_option_ids.clone(),
                }),
                _ => None,
            };
            $crate::map_soup_type!(@item r).map(|item| {
                $crate::domain::models::SoupProjectionHydration {
                    item,
                    document_server_facts,
                    sort_key: None,
                }
            })
        }
    };
}