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.

24.7 KB
Raw Blame
use crate::agent_session::SoupAgentSession;
use crate::calendar_event::SoupCalendarEvent;
use crate::call_record::SoupCallRecord;
use crate::crm_company::SoupCrmCompany;
use crate::crm_contact::SoupCrmContact;
use crate::database_row::SoupDatabaseRow;
use crate::document::SoupDocument;
use crate::email_thread::SoupEnrichedEmailThreadPreview;
use crate::foreign_entity::SoupForeignEntity;
use crate::initiative::SoupInitiative;
use crate::project::SoupProject;
use crate::{
    chat::SoupChat,
    comms::{SoupChannel, SoupChannelThread},
};
use chrono::{DateTime, Utc};
use model_entity::{Entity, EntityType};
use models_pagination::{Identify, SimpleSortMethod, SortOn};
use models_properties::{EntityReference, EntityType as PropertiesEntityType};
use serde::{Deserialize, Serialize};
use uuid::Uuid;

#[cfg(test)]
mod test;

/// A single item in the Soup feed.
#[derive(Clone, Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase", tag = "tag", content = "data")]
#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]
pub enum SoupItem<T = ()> {
    /// Document item.
    Document(SoupDocument<T>),
    /// Chat item.
    Chat(SoupChat<T>),
    /// Project item.
    Project(SoupProject<T>),
    /// Initiative entity.
    Initiative(SoupInitiative<T>),
    /// Email thread item.
    EmailThread(SoupEnrichedEmailThreadPreview<T>),
    /// Channel item.
    Channel(SoupChannel),
    /// Channel thread item.
    ChannelThread(SoupChannelThread),
    /// Call record item.
    Call(SoupCallRecord<T>),
    /// Calendar event item.
    CalendarEvent(SoupCalendarEvent<T>),
    /// CRM company item.
    CrmCompany(SoupCrmCompany<T>),
    /// Team-owned CRM contact.
    CrmContact(SoupCrmContact<T>),
    /// Foreign entity item.
    ForeignEntity(SoupForeignEntity),
    /// Agent session item.
    AgentSession(SoupAgentSession<T>),
    /// Database row item.
    DatabaseRow(SoupDatabaseRow<T>),
}

impl<T> SoupItem<T> {
    /// return the [Entity] for this soup item
    pub fn entity(&self) -> Entity<'static> {
        match self {
            SoupItem::Document(soup_document) => {
                EntityType::Document.with_entity_string(soup_document.id.to_string())
            }
            SoupItem::Chat(soup_chat) => {
                EntityType::Chat.with_entity_string(soup_chat.id.to_string())
            }
            SoupItem::Initiative(initiative) => {
                EntityType::Initiative.with_entity_string(initiative.id.to_string())
            }
            SoupItem::Project(soup_project) => {
                EntityType::Project.with_entity_string(soup_project.id.to_string())
            }
            SoupItem::EmailThread(email_thread) => {
                EntityType::EmailThread.with_entity_string(email_thread.thread.id.to_string())
            }
            SoupItem::Channel(channel) => {
                EntityType::Channel.with_entity_string(channel.channel.channel.id.0.to_string())
            }
            SoupItem::ChannelThread(thread) => {
                EntityType::ChannelMessage.with_entity_string(thread.id.to_string())
            }
            SoupItem::Call(record) => {
                EntityType::Call.with_entity_string(record.call_id.to_string())
            }
            SoupItem::CalendarEvent(event) => {
                EntityType::CalendarEvent.with_entity_string(event.id.to_string())
            }
            SoupItem::CrmContact(contact) => {
                EntityType::CrmContact.with_entity_string(contact.id.to_string())
            }
            SoupItem::CrmCompany(company) => {
                EntityType::CrmCompany.with_entity_string(company.id.to_string())
            }
            SoupItem::ForeignEntity(foreign_entity) => {
                EntityType::ForeignEntity.with_entity_string(foreign_entity.id.to_string())
            }
            SoupItem::AgentSession(session) => {
                EntityType::AgentSession.with_entity_string(session.id.to_string())
            }
            SoupItem::DatabaseRow(row) => {
                EntityType::DatabaseRow.with_entity_string(row.id.to_string())
            }
        }
    }

    /// Returns the timestamp used as this item's update time.
    pub fn updated_at(&self) -> DateTime<Utc> {
        match self {
            SoupItem::Document(soup_document) => soup_document.updated_at,
            SoupItem::Chat(soup_chat) => soup_chat.updated_at,
            SoupItem::Project(soup_project) => soup_project.updated_at,
            SoupItem::Initiative(initiative) => initiative.updated_at,
            SoupItem::EmailThread(soup_thread) => soup_thread.thread.updated_at,
            SoupItem::Channel(soup_channel) => soup_channel.channel.channel.updated_at,
            SoupItem::ChannelThread(thread) => thread.effective_updated_at(),
            // Calls intentionally lack `updated_at`; recency follows their lifecycle timestamps.
            SoupItem::Call(record) => record.ended_at.unwrap_or(record.started_at),
            // Includes the fired-reminder timestamp so the frecency fallback
            // cursor agrees with the GREATEST-based recency sort.
            SoupItem::CalendarEvent(event) => event
                .last_reminder_fired_at
                .map_or(event.updated_at, |fired| fired.max(event.updated_at)),
            SoupItem::CrmCompany(company) => company.updated_at,
            SoupItem::CrmContact(contact) => contact.last_interaction,
            SoupItem::ForeignEntity(foreign_entity) => foreign_entity.updated_at,
            SoupItem::AgentSession(session) => session.updated_at,
            SoupItem::DatabaseRow(row) => row.updated_at,
        }
    }

    fn cursor_timestamp(&self, sort: SimpleSortMethod) -> DateTime<Utc> {
        match (self, sort) {
            (SoupItem::Document(soup_document), SimpleSortMethod::ViewedAt) => {
                soup_document.viewed_at.unwrap_or_default()
            }
            (SoupItem::Document(soup_document), SimpleSortMethod::UpdatedAt) => {
                soup_document.updated_at
            }
            (SoupItem::Document(soup_document), SimpleSortMethod::CreatedAt) => {
                soup_document.created_at
            }
            (SoupItem::Document(soup_document), SimpleSortMethod::ViewedUpdated) => {
                soup_document.viewed_at.unwrap_or(soup_document.updated_at)
            }
            (SoupItem::Chat(soup_chat), SimpleSortMethod::ViewedAt) => {
                soup_chat.viewed_at.unwrap_or_default()
            }
            (SoupItem::Chat(soup_chat), SimpleSortMethod::UpdatedAt) => soup_chat.updated_at,
            (SoupItem::Chat(soup_chat), SimpleSortMethod::CreatedAt) => soup_chat.created_at,
            (SoupItem::Chat(soup_chat), SimpleSortMethod::ViewedUpdated) => {
                soup_chat.viewed_at.unwrap_or(soup_chat.updated_at)
            }
            (SoupItem::Initiative(initiative), SimpleSortMethod::ViewedAt) => {
                initiative.viewed_at.unwrap_or_default()
            }
            (SoupItem::Initiative(initiative), SimpleSortMethod::UpdatedAt) => {
                initiative.updated_at
            }
            (SoupItem::Initiative(initiative), SimpleSortMethod::CreatedAt) => {
                initiative.created_at
            }
            (SoupItem::Initiative(initiative), SimpleSortMethod::ViewedUpdated) => {
                initiative.viewed_at.unwrap_or(initiative.updated_at)
            }
            (SoupItem::Project(soup_project), SimpleSortMethod::ViewedAt) => {
                soup_project.viewed_at.unwrap_or_default()
            }
            (SoupItem::Project(soup_project), SimpleSortMethod::UpdatedAt) => {
                soup_project.updated_at
            }
            (SoupItem::Project(soup_project), SimpleSortMethod::CreatedAt) => {
                soup_project.created_at
            }
            (SoupItem::Project(soup_project), SimpleSortMethod::ViewedUpdated) => {
                soup_project.viewed_at.unwrap_or(soup_project.updated_at)
            }
            (SoupItem::EmailThread(thread), _) => {
                // Always use sort_ts for emails — this is the pre-computed effective_ts
                // from the email SQL query, which is also what the cursor offset logic
                // uses: (effective_ts, id) < (cursor_ts, cursor_id).
                thread.thread.sort_ts
            }
            (SoupItem::Channel(soup_channel), SimpleSortMethod::ViewedAt) => {
                soup_channel.viewed_at.unwrap_or_default()
            }
            (SoupItem::Channel(soup_channel), SimpleSortMethod::UpdatedAt) => {
                soup_channel.channel.channel.updated_at
            }
            (SoupItem::Channel(soup_channel), SimpleSortMethod::CreatedAt) => {
                soup_channel.channel.channel.created_at
            }
            (SoupItem::Channel(soup_channel), SimpleSortMethod::ViewedUpdated) => soup_channel
                .viewed_at
                .unwrap_or(soup_channel.channel.channel.updated_at),
            (SoupItem::ChannelThread(thread), SimpleSortMethod::CreatedAt) => thread.created_at,
            (SoupItem::ChannelThread(thread), _) => thread.effective_updated_at(),
            // Calls intentionally lack `updated_at`; recency follows their lifecycle timestamps.
            (SoupItem::Call(record), SimpleSortMethod::CreatedAt) => record.started_at,
            (SoupItem::Call(record), _) => record.ended_at.unwrap_or(record.started_at),
            (SoupItem::CalendarEvent(event), SimpleSortMethod::CreatedAt) => event.created_at,
            (SoupItem::CalendarEvent(_), SimpleSortMethod::ViewedAt) => DateTime::<Utc>::default(),
            // A fired alarm is the event's latest activity: without it the row
            // a reminder surfaces in the inbox would sort at the event's Google
            // last-modified time, i.e. into the past. Must mirror the SQL sort
            // expression GREATEST(updated_at, last_reminder_fired_at) or keyset
            // pagination breaks.
            (SoupItem::CalendarEvent(event), _) => event
                .last_reminder_fired_at
                .map_or(event.updated_at, |fired| fired.max(event.updated_at)),
            (SoupItem::CrmContact(contact), SimpleSortMethod::CreatedAt) => {
                contact.first_interaction
            }
            (SoupItem::CrmContact(contact), SimpleSortMethod::ViewedAt) => {
                contact.viewed_at.unwrap_or_default()
            }
            (SoupItem::CrmContact(contact), SimpleSortMethod::ViewedUpdated) => {
                contact.viewed_at.unwrap_or(contact.last_interaction)
            }
            (SoupItem::CrmContact(contact), _) => contact.last_interaction,
            (SoupItem::CrmCompany(company), SimpleSortMethod::CreatedAt) => company.created_at,
            (SoupItem::CrmCompany(company), SimpleSortMethod::ViewedAt) => {
                company.viewed_at.unwrap_or_default()
            }
            (SoupItem::CrmCompany(company), SimpleSortMethod::ViewedUpdated) => {
                company.viewed_at.unwrap_or(company.updated_at)
            }
            (SoupItem::CrmCompany(company), _) => company.updated_at,
            (SoupItem::ForeignEntity(foreign_entity), SimpleSortMethod::CreatedAt) => {
                foreign_entity.created_at
            }
            (SoupItem::ForeignEntity(foreign_entity), _) => foreign_entity.updated_at,
            (SoupItem::AgentSession(session), SimpleSortMethod::ViewedAt) => {
                session.viewed_at.unwrap_or_default()
            }
            (SoupItem::AgentSession(session), SimpleSortMethod::UpdatedAt) => session.updated_at,
            (SoupItem::AgentSession(session), SimpleSortMethod::CreatedAt) => session.created_at,
            (SoupItem::AgentSession(session), SimpleSortMethod::ViewedUpdated) => {
                session.viewed_at.unwrap_or(session.updated_at)
            }
            // Rows are never viewed on their own: ViewedAt sorts them last and
            // ViewedUpdated uses updated_at.
            (SoupItem::DatabaseRow(row), SimpleSortMethod::CreatedAt) => row.created_at,
            (SoupItem::DatabaseRow(_), SimpleSortMethod::ViewedAt) => DateTime::default(),
            (
                SoupItem::DatabaseRow(row),
                SimpleSortMethod::UpdatedAt | SimpleSortMethod::ViewedUpdated,
            ) => row.updated_at,
        }
    }

    /// Converts this item to an [`EntityReference`] for property lookups.
    ///
    /// Returns `None` for item types that don't support properties
    /// (e.g., channels, channel threads, foreign entities).
    pub fn to_entity_reference(&self) -> Option<EntityReference> {
        match self {
            SoupItem::Document(doc) => {
                Some(EntityReference::new(doc.id.to_string(), doc.entity_type()))
            }
            SoupItem::Initiative(initiative) => Some(EntityReference::new(
                initiative.id.to_string(),
                PropertiesEntityType::Initiative,
            )),
            SoupItem::Project(p) => Some(EntityReference::new(
                p.id.to_string(),
                PropertiesEntityType::Project,
            )),
            SoupItem::EmailThread(e) => Some(EntityReference::new(
                e.thread.id.to_string(),
                PropertiesEntityType::Thread,
            )),
            SoupItem::Chat(c) => Some(EntityReference::new(
                c.id.to_string(),
                PropertiesEntityType::Chat,
            )),
            SoupItem::Channel(_) => None,
            SoupItem::ChannelThread(_) => None,
            SoupItem::Call(c) => Some(EntityReference::new(
                c.call_id.to_string(),
                PropertiesEntityType::CallRecord,
            )),
            SoupItem::CalendarEvent(event) => Some(EntityReference::new(
                event.id.to_string(),
                PropertiesEntityType::CalendarEvent,
            )),
            SoupItem::CrmCompany(c) => Some(EntityReference::new(
                c.id.to_string(),
                PropertiesEntityType::Company,
            )),
            SoupItem::CrmContact(_) => None,
            SoupItem::ForeignEntity(_) => None,
            // Agent sessions have no properties entity type yet.
            SoupItem::AgentSession(_) => None,
            SoupItem::DatabaseRow(row) => Some(EntityReference::new(
                row.id.to_string(),
                PropertiesEntityType::DatabaseRow,
            )),
        }
    }

    /// Maps the extra fields attached to property-bearing Soup variants.
    pub fn map_extra<U, F>(self, f: F) -> SoupItem<U>
    where
        F: FnOnce(T) -> U,
    {
        match self {
            SoupItem::Document(SoupDocument {
                id,
                document_version_id,
                owner_id,
                name,
                file_type,
                sha,
                project_id,
                branched_from_id,
                branched_from_version_id,
                document_family_id,
                created_at,
                updated_at,
                viewed_at,
                sub_type,
                deleted_at,
                extra,
            }) => SoupItem::Document(SoupDocument {
                id,
                document_version_id,
                owner_id,
                name,
                file_type,
                sha,
                project_id,
                branched_from_id,
                branched_from_version_id,
                document_family_id,
                created_at,
                updated_at,
                viewed_at,
                sub_type,
                deleted_at,
                extra: f(extra),
            }),
            SoupItem::Chat(SoupChat {
                id,
                name,
                model,
                owner_id,
                project_id,
                is_persistent,
                created_at,
                updated_at,
                viewed_at,
                deleted_at,
                extra,
            }) => SoupItem::Chat(SoupChat {
                id,
                name,
                model,
                owner_id,
                project_id,
                is_persistent,
                created_at,
                updated_at,
                viewed_at,
                deleted_at,
                extra: f(extra),
            }),
            SoupItem::Initiative(SoupInitiative {
                id,
                name,
                owner_id,
                created_at,
                updated_at,
                viewed_at,
                extra,
            }) => SoupItem::Initiative(SoupInitiative {
                id,
                name,
                owner_id,
                created_at,
                updated_at,
                viewed_at,
                extra: f(extra),
            }),
            SoupItem::Project(SoupProject {
                id,
                name,
                owner_id,
                parent_id,
                created_at,
                updated_at,
                viewed_at,
                deleted_at,
                extra,
            }) => SoupItem::Project(SoupProject {
                id,
                name,
                owner_id,
                parent_id,
                created_at,
                updated_at,
                viewed_at,
                deleted_at,
                extra: f(extra),
            }),
            SoupItem::EmailThread(SoupEnrichedEmailThreadPreview {
                thread,
                attachments,
                participants,
                labels,
                extra,
            }) => SoupItem::EmailThread(SoupEnrichedEmailThreadPreview {
                thread,
                attachments,
                participants,
                labels,
                extra: f(extra),
            }),
            SoupItem::Channel(channel) => SoupItem::Channel(channel),
            SoupItem::ChannelThread(soup_channel_thread) => {
                SoupItem::ChannelThread(soup_channel_thread)
            }
            SoupItem::Call(SoupCallRecord {
                call_id,
                channel_id,
                created_by,
                started_at,
                ended_at,
                duration_ms,
                channel_name,
                custom_name,
                summary,
                is_active,
                status,
                attended,
                participants,
                guests,
                extra,
            }) => SoupItem::Call(SoupCallRecord {
                call_id,
                channel_id,
                created_by,
                started_at,
                ended_at,
                duration_ms,
                channel_name,
                custom_name,
                summary,
                is_active,
                status,
                attended,
                participants,
                guests,
                extra: f(extra),
            }),
            SoupItem::CalendarEvent(SoupCalendarEvent {
                id,
                owner_id,
                ical_uid,
                title,
                description,
                location,
                status,
                visibility,
                transparency,
                time,
                organizer_email,
                organizer_name,
                conference_url,
                conference_provider,
                is_read_only,
                created_at,
                updated_at,
                last_reminder_fired_at,
                extra,
            }) => SoupItem::CalendarEvent(SoupCalendarEvent {
                id,
                owner_id,
                ical_uid,
                title,
                description,
                location,
                status,
                visibility,
                transparency,
                time,
                organizer_email,
                organizer_name,
                conference_url,
                conference_provider,
                is_read_only,
                created_at,
                updated_at,
                last_reminder_fired_at,
                extra: f(extra),
            }),
            SoupItem::CrmContact(SoupCrmContact {
                id,
                team_id,
                company_id,
                company_name,
                email,
                name,
                hidden,
                first_interaction,
                last_interaction,
                created_at,
                updated_at,
                viewed_at,
                extra,
            }) => SoupItem::CrmContact(SoupCrmContact {
                id,
                team_id,
                company_id,
                company_name,
                email,
                name,
                hidden,
                first_interaction,
                last_interaction,
                created_at,
                updated_at,
                viewed_at,
                extra: f(extra),
            }),
            SoupItem::CrmCompany(SoupCrmCompany {
                id,
                team_id,
                name,
                description,
                email_sync,
                hidden,
                created_at,
                updated_at,
                viewed_at,
                domains,
                extra,
            }) => SoupItem::CrmCompany(SoupCrmCompany {
                id,
                team_id,
                name,
                description,
                email_sync,
                hidden,
                created_at,
                updated_at,
                viewed_at,
                domains,
                extra: f(extra),
            }),
            SoupItem::ForeignEntity(soup_foreign_entity) => {
                SoupItem::ForeignEntity(soup_foreign_entity)
            }
            SoupItem::AgentSession(SoupAgentSession {
                id,
                name,
                archived_by,
                archived_at,
                owner_id,
                bot_id,
                harness,
                repo_url,
                repo_branch,
                pull_request_url,
                working_branch,
                pull_request_state,
                pull_request_id,
                turn_state,
                thread_id,
                source,
                visibility,
                status,
                created_at,
                updated_at,
                viewed_at,
                extra,
            }) => SoupItem::AgentSession(SoupAgentSession {
                id,
                name,
                archived_by,
                archived_at,
                owner_id,
                bot_id,
                harness,
                repo_url,
                repo_branch,
                pull_request_url,
                working_branch,
                pull_request_state,
                pull_request_id,
                turn_state,
                thread_id,
                source,
                visibility,
                status,
                created_at,
                updated_at,
                viewed_at,
                extra: f(extra),
            }),
            SoupItem::DatabaseRow(SoupDatabaseRow {
                id,
                table_id,
                database_id,
                position,
                owner_id,
                created_by,
                created_at,
                updated_at,
                extra,
            }) => SoupItem::DatabaseRow(SoupDatabaseRow {
                id,
                table_id,
                database_id,
                position,
                owner_id,
                created_by,
                created_at,
                updated_at,
                extra: f(extra),
            }),
        }
    }
}

impl<T> Identify for SoupItem<T> {
    type Id = Uuid;

    fn id(&self) -> Self::Id {
        match self {
            SoupItem::Document(soup_document) => soup_document.id,
            SoupItem::Chat(soup_chat) => soup_chat.id,
            SoupItem::Project(soup_project) => soup_project.id,
            SoupItem::Initiative(initiative) => initiative.id,
            SoupItem::EmailThread(thread) => thread.thread.id,
            SoupItem::Channel(soup_channel) => soup_channel.channel.channel.id.0,
            SoupItem::ChannelThread(thread) => thread.id,
            SoupItem::Call(record) => record.call_id,
            SoupItem::CalendarEvent(event) => event.id,
            SoupItem::CrmCompany(company) => company.id,
            SoupItem::CrmContact(contact) => contact.id,
            SoupItem::ForeignEntity(foreign_entity) => foreign_entity.id,
            SoupItem::AgentSession(session) => session.id,
            SoupItem::DatabaseRow(row) => row.id,
        }
    }
}

impl<T> SortOn<SimpleSortMethod> for SoupItem<T> {
    fn sort_on(
        sort: SimpleSortMethod,
    ) -> impl FnMut(&Self) -> models_pagination::CursorVal<SimpleSortMethod> {
        move |v| {
            let last_val = v.cursor_timestamp(sort);
            models_pagination::CursorVal {
                sort_type: sort,
                last_val,
            }
        }
    }
}