Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
use crate::{MatchType, SearchHighlight, SearchOn, SearchResponseItem};
use chrono::{DateTime, Utc};
use item_filters::ChannelFilters;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;
/// Sort key for channel content search results.
#[derive(
Debug, Serialize, Deserialize, Default, Clone, Copy, PartialEq, Eq, ToSchema, JsonSchema,
)]
#[serde(rename_all = "snake_case")]
pub enum ChannelSortTimestamp {
/// Sort by message created_at DESC, message_id DESC tiebreaker.
#[default]
Message,
/// Sort by thread_id DESC, message_id DESC tiebreaker.
Thread,
}
/// A channel message match for a given channel id
#[derive(Debug, Serialize, Deserialize, ToSchema, JsonSchema)]
pub struct ChannelSearchResult {
/// The channel message id
/// This is only prsent if the search result is on the message content
pub message_id: Option<uuid::Uuid>,
/// The channel message thread id
pub thread_id: Option<uuid::Uuid>,
/// The sender id
/// This is only prsent if the search result is on the message content
pub sender_id: Option<String>,
/// When the channel message was created
/// This is only prsent if the search result is on the message content
pub created_at: Option<DateTime<Utc>>,
/// When the channel message was last updated
/// This is only prsent if the search result is on the message content
pub updated_at: Option<DateTime<Utc>>,
/// When the channel message was deleted, if it has been
pub deleted_at: Option<DateTime<Utc>>,
/// The highlights for the channel message
pub highlight: SearchHighlight,
/// The score of the result
#[serde(skip_serializing_if = "Option::is_none")]
pub score: Option<f64>,
}
/// A single response item, part of the ChannelSearchResponse object
#[derive(Debug, Serialize, Deserialize, ToSchema, JsonSchema)]
pub struct ChannelSearchResponseItem {
/// Standardized fields that all item types will share.
/// These field names are being aligned across all item types
/// for consistency in our data model.
pub id: uuid::Uuid,
/// we don't store this for channels atm but keeping it here for consistency
pub owner_id: Option<String>,
/// The type of channel
pub channel_type: String,
/// The id of the channel
pub channel_id: uuid::Uuid,
/// The search results for the channel
/// This may be empty if the search result match was not on content
pub channel_message_search_results: Vec<ChannelSearchResult>,
}
/// Metadata for a channel fetched from the database
#[derive(Debug, Serialize, Deserialize, ToSchema, JsonSchema)]
pub struct ChannelMetadata {
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
pub viewed_at: Option<DateTime<Utc>>,
pub interacted_at: Option<DateTime<Utc>>,
}
/// ChannelSearchResponseItem object with channel metadata we fetch from macrodb. we don't store these
/// timestamps in opensearch as they would require us to update each chat message record for the chat
/// every time the chat updates (specifically for updated_at and viewed_at and interacted_at)
#[derive(Debug, Serialize, Deserialize, ToSchema, JsonSchema)]
pub struct ChannelSearchResponseItemWithMetadata {
/// Metadata from the database. None if the channel doesn't exist in the database.
pub metadata: Option<ChannelMetadata>,
#[serde(flatten)]
pub extra: ChannelSearchResponseItem,
}
/// Request for viewer-aware channel name search.
#[derive(Debug, Serialize, Deserialize, ToSchema, JsonSchema)]
pub struct ChannelNameSearchRequest {
/// Channel name text to search for; every whitespace- or comma-separated
/// term must match the name the viewer sees.
pub query: String,
/// Whether terms match as substrings or as whole tokens.
pub match_type: MatchType,
}
/// A channel-name search hit.
#[derive(Debug, Serialize, Deserialize, ToSchema, JsonSchema)]
pub struct ChannelNameSearchResponseItem {
/// Metadata for the channel.
pub metadata: Option<ChannelMetadata>,
/// Standardized id field shared by all item types; the channel id.
pub id: uuid::Uuid,
/// The channel owner.
pub owner_id: Option<String>,
/// The type of channel.
pub channel_type: String,
/// The channel id.
pub channel_id: uuid::Uuid,
/// The matched channel-name highlight.
pub highlight: SearchHighlight,
/// The score of the result.
#[serde(skip_serializing_if = "Option::is_none")]
pub score: Option<f64>,
}
/// Response from viewer-aware channel name search.
#[derive(Debug, Serialize, Deserialize, ToSchema, JsonSchema)]
pub struct ChannelNameSearchResponse {
/// Matching channels in descending update order.
pub results: Vec<ChannelNameSearchResponseItem>,
/// Cursor for the next page, if one exists.
pub next_cursor: Option<String>,
}
/// A single channel-message content hit in the unified search response.
/// One item per matching message, timestamped by the message itself, so the
/// unified sort interleaves channel messages with other entity types by
/// their own recency.
#[derive(Debug, Serialize, Deserialize, ToSchema, JsonSchema)]
pub struct ChannelMessageSearchResponseItem {
/// Standardized id field shared by all item types; the channel id.
pub id: uuid::Uuid,
/// we don't store this for channels atm but keeping it here for consistency
pub owner_id: Option<String>,
/// The type of channel
pub channel_type: String,
/// The id of the channel the message belongs to
pub channel_id: uuid::Uuid,
/// The channel message id
pub message_id: uuid::Uuid,
/// The channel message thread id
pub thread_id: Option<uuid::Uuid>,
/// The sender id
pub sender_id: String,
/// When the channel message was created
pub created_at: DateTime<Utc>,
/// When the channel message was last updated
pub updated_at: DateTime<Utc>,
/// When the channel message was deleted, if it has been
pub deleted_at: Option<DateTime<Utc>>,
/// The highlights for the channel message
pub highlight: SearchHighlight,
/// The score of the result
#[serde(skip_serializing_if = "Option::is_none")]
pub score: Option<f64>,
}
/// Metadata associated with Channel Search
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct ChannelSearchMetadata {
/// The id of the channel
pub channel_id: uuid::Uuid,
/// The type of channel
pub channel_type: String,
}
impl From<SearchResponseItem<ChannelSearchResult, ChannelSearchMetadata>>
for ChannelSearchResponseItem
{
fn from(response: SearchResponseItem<ChannelSearchResult, ChannelSearchMetadata>) -> Self {
ChannelSearchResponseItem {
id: response.metadata.channel_id,
// we don't store this for channels atm but keeping it here for consistency
owner_id: None,
channel_type: response.metadata.channel_type.clone(),
channel_id: response.metadata.channel_id,
channel_message_search_results: response.results,
}
}
}
/// The document search response object
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct ChannelSearchResponse {
/// List containing results from email threads
pub results: Vec<ChannelSearchResponseItemWithMetadata>,
/// Base64-encoded cursor for the next page; `None` when exhausted.
#[serde(skip_serializing_if = "Option::is_none")]
pub next_cursor: Option<String>,
/// Total number of matching channel messages across all pages.
pub total_count: i64,
}
#[derive(Serialize, Deserialize, Debug, ToSchema, JsonSchema)]
pub struct ChannelSearchRequest {
/// The query to search for
pub query: Option<String>,
/// Multiple terms to search over
pub terms: Option<Vec<String>>,
/// The match type to use when searching
pub match_type: MatchType,
/// Search filters for channels
#[serde(flatten)]
pub filters: Option<ChannelFilters>,
/// Fields to search on (Name, Content, NameContent). Defaults to Content
#[serde(default)]
pub search_on: SearchOn,
/// If true, returns only 1 result per entity. False by default.
#[serde(skip_serializing_if = "Option::is_none")]
pub collapse: Option<bool>,
/// Sort key for results. Defaults to `message`.
#[serde(default)]
pub sort: ChannelSortTimestamp,
}
#[derive(Debug, Serialize, Deserialize, ToSchema, JsonSchema)]
pub struct SimpleChannelSearchReponseBaseItem<T> {
/// The channel id
pub channel_id: String,
/// The channel type
pub channel_type: String,
/// The org id
pub org_id: Option<i64>,
/// The message id
pub message_id: String,
/// The thread id
pub thread_id: Option<String>,
/// The sender id
pub sender_id: String,
/// The mentions
pub mentions: Vec<String>,
#[schema(inline)]
/// The time the channel message was created
pub created_at: T,
#[schema(inline)]
/// The time the channel message was last updated
pub updated_at: T,
/// The highlights on the channel message
pub highlight: SearchHighlight,
}
pub type SimpleChannelSearchReponseItem =
SimpleChannelSearchReponseBaseItem<crate::HumanReadableTimestamp>;
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct SimpleChannelSearchResponse {
/// List containing results from channels.
/// Each item in the list is for a specific message in a channel.
pub results: Vec<SimpleChannelSearchReponseItem>,
}
#[cfg(test)]
mod test;