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
//! Stream-candidate adapters over shared webhook event normalization.
use super::{
LifecycleAudience, TriggerAudience, WebhookEventIngestionError, lifecycle_audience,
normalized_agent_session_lifecycle_event, normalized_agent_trigger_event,
normalized_call_notification_event, normalized_channel_event, normalized_document_event,
normalized_message_event, normalized_webhook_event,
};
use crate::domain::{
events::WebhookTopicEvent,
stream::{StreamAudience, StreamCandidateEvent},
};
use agent_session::domain::events::AgentSessionLifecycleEvent;
use agent_trigger::domain::broker_events::AgentTriggerTopicEvent;
use call::domain::notification_events::CallNotificationEvent;
use channels::domain::broker_events::ChannelTopicEvent;
use documents::domain::events::DocumentTopicEvent;
use entity_access::domain::models::EntityType;
use macro_event_broker::Event;
use messages::outbound::broker::MessageTopicEvent;
pub(crate) fn document_stream_candidate(
event: &Event<DocumentTopicEvent>,
) -> Result<Option<StreamCandidateEvent>, WebhookEventIngestionError> {
Ok(
normalized_document_event(event)?.map(|normalized| StreamCandidateEvent {
audience: StreamAudience::Entity {
entity_id: normalized.entity_id.clone(),
entity_type: EntityType::Document,
},
event: normalized,
}),
)
}
pub(crate) fn channel_stream_candidate(
event: &Event<ChannelTopicEvent>,
) -> Result<StreamCandidateEvent, WebhookEventIngestionError> {
let normalized = normalized_channel_event(event)?;
Ok(StreamCandidateEvent {
audience: StreamAudience::Entity {
entity_id: normalized.entity_id.clone(),
entity_type: EntityType::Channel,
},
event: normalized,
})
}
pub(crate) fn call_stream_candidate(
event: &Event<CallNotificationEvent>,
) -> Result<StreamCandidateEvent, WebhookEventIngestionError> {
let (normalized, recipient) = normalized_call_notification_event(event)?;
let audience = match recipient {
Some(user_id) => StreamAudience::User { user_id },
None => StreamAudience::Entity {
entity_id: normalized.entity_id.clone(),
entity_type: EntityType::Call,
},
};
Ok(StreamCandidateEvent {
event: normalized,
audience,
})
}
pub(crate) fn message_stream_candidate(
event: &Event<MessageTopicEvent>,
) -> Result<StreamCandidateEvent, WebhookEventIngestionError> {
let (normalized, entity_type) = normalized_message_event(event)?;
Ok(StreamCandidateEvent {
audience: StreamAudience::Entity {
entity_id: normalized.entity_id.clone(),
entity_type,
},
event: normalized,
})
}
pub(crate) fn webhook_stream_candidate(
event: &Event<WebhookTopicEvent>,
) -> Result<StreamCandidateEvent, WebhookEventIngestionError> {
let (normalized, workspace_id) = normalized_webhook_event(event)?;
Ok(StreamCandidateEvent {
audience: StreamAudience::Workspace { workspace_id },
event: normalized,
})
}
pub(crate) fn agent_trigger_stream_candidate(
event: &Event<AgentTriggerTopicEvent>,
) -> Result<StreamCandidateEvent, WebhookEventIngestionError> {
let (normalized, audience) = normalized_agent_trigger_event(event)?;
let audience = match audience {
TriggerAudience::Entity {
entity_id,
entity_type,
} => StreamAudience::Entity {
entity_id,
entity_type,
},
// Named people subscribe from their own workspaces, as a departed
// session's owner does.
TriggerAudience::People(people) => StreamAudience::Any(
people
.into_iter()
.map(|person| StreamAudience::Workspace {
workspace_id: person.to_string(),
})
.collect(),
),
};
Ok(StreamCandidateEvent {
audience,
event: normalized,
})
}
pub(crate) fn agent_session_lifecycle_stream_candidate(
event: &Event<AgentSessionLifecycleEvent>,
) -> Result<StreamCandidateEvent, WebhookEventIngestionError> {
let normalized = normalized_agent_session_lifecycle_event(event)?;
let audience = match lifecycle_audience(&event.event) {
LifecycleAudience::Session => StreamAudience::Entity {
entity_id: normalized.entity_id.clone(),
entity_type: EntityType::AgentSession,
},
// The session's grants are gone with it. The owner keeps ownership, so
// they must still receive the departure even after losing access to the
// parent; the channel or document it came from also still knows its
// audience. Deliver to the union of the two.
LifecycleAudience::Departed {
owner,
origin_parent: Some(parent),
} => StreamAudience::Any(vec![
StreamAudience::Workspace {
workspace_id: owner.to_string(),
},
StreamAudience::Entity {
entity_id: parent.entity_id(),
entity_type: parent.access_entity_type(),
},
]),
LifecycleAudience::Departed {
owner,
origin_parent: None,
} => StreamAudience::Workspace {
workspace_id: owner.to_string(),
},
};
Ok(StreamCandidateEvent {
audience,
event: normalized,
})
}