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
//! Decide how agent-session broker events are handled by this deployment.
use agent_session::domain::model::AgentSessionId;
use agent_trigger::domain::broker_events::{
AgentTriggerTopicEvent, NewAgentSessionEvent, OpeningMention, SessionMessage,
};
use agent_trigger::domain::task_assignment::assignment_instructions;
use bot_id::BotId;
use agent_runtime_protocol::domain::action::AgentAction;
use super::model::{
AgentKind, AgentRuntimeConfig, AnnounceOrigin, AnnouncePrompt, DeliverAction, HarnessCommand,
MentionOrigin, OpenSession, SessionOrigin, StaticFileLinks, TaskAssignmentOrigin,
};
/// What one trigger event asks this deployment to do.
#[derive(Debug, Clone)]
#[allow(
clippy::large_enum_variant,
reason = "a command carries its prompt's context; boxing would only move the size"
)]
pub enum RoutedTrigger {
/// Run a harness command for the managed bot's session.
Command(AgentSessionId, HarnessCommand),
/// Post the chip for a prompt an external bot's runtime delivers itself.
Announce(AgentSessionId, AnnouncePrompt),
}
/// Why an event yielded no work. Only for logging - none of these are
/// errors, and the consumer commits the offset either way.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Skipped {
/// The event is another deployment's to act on: an open for a bot whose
/// runtime opens its own sessions, or managed traffic that is not ours.
ForeignBot,
/// Neither a user sender nor trusted bot attribution names a session owner.
MissingInvokingUser,
/// An event shape this harness does not recognise yet - the trigger's
/// vocabulary is non-exhaustive on purpose, and unknown shapes are
/// skipped rather than wedging the partition.
Unrecognized,
}
/// Bot targeted by a recognized trigger event shape.
#[must_use]
pub fn agent_trigger_bot_id(event: &AgentTriggerTopicEvent) -> Option<BotId> {
event.bot_id()
}
/// Route one trigger event: work for this deployment, or a reason it was
/// skipped.
///
/// Opens are only ours when `runtime` resolves to a managed profile. External
/// bots' runtimes open their own sessions over the API. Events for sessions
/// that already exist always carry work: a prompt to deliver when the session
/// is managed here, or just its announcement when the bot's own runtime
/// delivers the prompt.
pub fn route_agent_trigger(
event: AgentTriggerTopicEvent,
runtime: Option<AgentRuntimeConfig>,
links: &StaticFileLinks,
) -> Result<RoutedTrigger, Skipped> {
let trigger_origin = match &event {
AgentTriggerTopicEvent::New(new) => new.origin(),
AgentTriggerTopicEvent::Existing(_) => trigger_context::TriggerOrigin::Unknown,
};
match event {
AgentTriggerTopicEvent::New(NewAgentSessionEvent::ConversationMessage(event)) => {
let Some(runtime) = runtime else {
return Err(Skipped::ForeignBot);
};
let sender = event
.message
.sender
.as_user()
.cloned()
.ok_or(Skipped::MissingInvokingUser)?;
let mut context = event.context;
links.link_context_attachments(context.as_mut());
let attachments =
links.prompt_attachments_for(&event.message.attachments, context.as_ref());
Ok(RoutedTrigger::Command(
event.session_id,
HarnessCommand::ConversationMessage(OpenSession {
bot_id: event.bot_id,
runtime,
origin: SessionOrigin::Mention(MentionOrigin {
trigger_origin,
reply_placement: super::model::ReplyPlacement::Timeline,
parent: event.message.parent,
thread_id: event.message.thread_id.unwrap_or(event.message.message_id),
message_id: event.message.message_id,
sender,
content: event.message.content,
attachments,
context,
}),
}),
))
}
AgentTriggerTopicEvent::New(NewAgentSessionEvent::AssignedToTask(assigned)) => {
let Some(runtime) = runtime.filter(|runtime| runtime.kind.is_managed()) else {
return Err(Skipped::ForeignBot);
};
Ok(RoutedTrigger::Command(
AgentSessionId::new(),
HarnessCommand::Open(OpenSession {
bot_id: assigned.bot_id,
runtime,
origin: SessionOrigin::TaskAssignment(TaskAssignmentOrigin {
trigger_origin,
// The task travels in the context; the prompt is only
// what to do with it. Events from producers that
// predate the context carry the whole brief instead.
prompt: match &assigned.context {
Some(_) => assignment_instructions(&assigned.parent),
None => assigned.prompt,
},
parent: assigned.parent,
discussion_id: assigned.discussion_id,
actor: assigned.actor,
context: assigned.context,
}),
}),
))
}
AgentTriggerTopicEvent::New(event) => {
let Some(OpeningMention {
bot_id,
message,
mut context,
}) = event.mention()
else {
return Err(Skipped::Unrecognized);
};
let Some(runtime) = runtime.filter(|runtime| runtime.kind.is_managed()) else {
return Err(Skipped::ForeignBot);
};
let sender = message
.invoking_user()
.ok_or(Skipped::MissingInvokingUser)?;
links.link_context_attachments(context.as_mut());
let attachments = links.prompt_attachments_for(&message.attachments, context.as_ref());
Ok(RoutedTrigger::Command(
AgentSessionId::new(),
HarnessCommand::Open(OpenSession {
bot_id,
runtime,
origin: SessionOrigin::Mention(MentionOrigin {
trigger_origin,
reply_placement: Default::default(),
parent: message.parent,
// A top-level mention roots its own thread; a mention
// inside a thread answers into that thread.
thread_id: message.thread_id.unwrap_or(message.message_id),
message_id: message.message_id,
sender,
content: message.content,
attachments,
context,
}),
}),
))
}
AgentTriggerTopicEvent::Existing(event) => {
let Some(SessionMessage {
bot_id,
session_id,
kind: _,
message,
mut context,
}) = event.session_message()
else {
return Err(Skipped::Unrecognized);
};
let sender = message.invoking_user();
let origin = AnnounceOrigin {
reply_placement: Default::default(),
reuse_origin_message: false,
parent: message.parent,
thread_id: message.thread_id.unwrap_or(message.message_id),
message_id: message.message_id,
};
let kind = runtime
.as_ref()
.map_or(AgentKind::External, |runtime| runtime.kind);
links.link_context_attachments(context.as_mut());
if kind.is_managed() {
// The session already saw its thread's files on its first turn.
let attachments = links.prompt_attachments_for(&message.attachments, None);
return Ok(RoutedTrigger::Command(
session_id,
HarnessCommand::Deliver(DeliverAction {
context,
..DeliverAction::prompt(
AgentAction::prompt_with_attachments(message.content, attachments),
sender.clone(),
Some(origin),
)
}),
));
}
let sender = sender.ok_or(Skipped::MissingInvokingUser)?;
Ok(RoutedTrigger::Announce(
session_id,
AnnouncePrompt {
bot_id,
origin,
content: message.content,
sender,
},
))
}
}
}