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
fix(channels): attribute AI-created channels to Automation (#6197)
teo5bd51592bcf3
diff --git a/crates/channels/src/domain/ports.rs b/crates/channels/src/domain/ports.rsindex 8b2388a061..99f31fde48 100644--- a/crates/channels/src/domain/ports.rs+++ b/crates/channels/src/domain/ports.rs@@ -671,8 +671,19 @@ pub trait ChannelService: Send + Sync + 'static {/// signup). The activity is attributed to the system bot acting for/// `owner`; ownership and permissions are unchanged.fn create_system_channel(+ &self,+ owner: MacroUserIdStr<'static>,+ req: CreateChannelRequest,+ ) -> impl Future<Output = Result<CreateChannelResponse, ChannelMutationErr>> + Send {+ self.create_channel_on_behalf(owner, bot_id::MACRO_SYSTEM_BOT_ID, req)+ }++ /// Create a channel owned by `owner` with Created attributed to `actor`+ /// acting for that owner. Ownership and permissions stay with `owner`.+ fn create_channel_on_behalf(&self,_owner: MacroUserIdStr<'static>,+ _actor: BotId,_req: CreateChannelRequest,) -> impl Future<Output = Result<CreateChannelResponse, ChannelMutationErr>> + Send {async move {diff --git a/crates/channels/src/domain/service.rs b/crates/channels/src/domain/service.rsindex 031bac5a21..f4d3c26be9 100644--- a/crates/channels/src/domain/service.rs+++ b/crates/channels/src/domain/service.rs@@ -344,13 +344,19 @@ whereowner: MacroUserIdStr<'static>,req: crate::domain::models::CreateChannelRequest,) -> Result<crate::domain::models::CreateChannelResponse, ChannelMutationErr> {- self.create_owned_channel(- owner.clone(),- Sender::new_from_bot(bot_id::MACRO_SYSTEM_BOT_ID),- Some(owner),- req,- )- .await+ self.create_channel_on_behalf(owner, bot_id::MACRO_SYSTEM_BOT_ID, req)+ .await+ }++ #[tracing::instrument(err, skip(self, req))]+ async fn create_channel_on_behalf(+ &self,+ owner: MacroUserIdStr<'static>,+ actor: BotId,+ req: crate::domain::models::CreateChannelRequest,+ ) -> Result<crate::domain::models::CreateChannelResponse, ChannelMutationErr> {+ self.create_owned_channel(owner.clone(), Sender::new_from_bot(actor), Some(owner), req)+ .await}async fn create_owned_channel(@@ -1883,6 +1889,15 @@ whereChannelServiceImpl::create_system_channel(self, owner, req).await}+ async fn create_channel_on_behalf(+ &self,+ owner: MacroUserIdStr<'static>,+ actor: BotId,+ req: crate::domain::models::CreateChannelRequest,+ ) -> Result<crate::domain::models::CreateChannelResponse, ChannelMutationErr> {+ ChannelServiceImpl::create_channel_on_behalf(self, owner, actor, req).await+ }+#[tracing::instrument(err, skip(self, user_id))]async fn auto_join_by_team_id(&self,diff --git a/crates/channels/src/domain/service/test.rs b/crates/channels/src/domain/service/test.rsindex 31bc62c5b3..a14cc34ca7 100644--- a/crates/channels/src/domain/service/test.rs+++ b/crates/channels/src/domain/service/test.rs@@ -2303,6 +2303,41 @@ async fn create_system_channel_event_uses_system_actor() {));}+#[tokio::test]+async fn create_channel_on_behalf_attributes_created_to_the_bot() {+ let channel_id = Uuid::new_v4();+ let repo = FakeMutationRepo::new(channel_id, "macro|[email protected]");+ let events = FakeEvents::default();+ let svc = mutation_service(repo, events.clone(), FakeReferenceSharing::default());++ svc.create_channel_on_behalf(+ macro_id("macro|[email protected]"),+ bot_id::MACRO_AI_BOT_ID,+ crate::domain::models::CreateChannelRequest {+ name: Some("Planning".to_string()),+ channel_type: ChannelType::Private,+ team_id: None,+ auto_join_team: false,+ participants: HashSet::from([macro_id("macro|[email protected]")]),+ },+ )+ .await+ .unwrap();++ let events = events.events.lock().unwrap();+ assert!(matches!(+ events.as_slice(),+ [ChannelEvent::ChannelCreated {+ actor,+ on_behalf_of: Some(owner),+ channel_name: Some(name),+ ..+ }] if actor == &Sender::new_from_bot(bot_id::MACRO_AI_BOT_ID)+ && owner.as_ref() == "macro|[email protected]"+ && name == "Planning"+ ));+}+/// Signup on main called `create_channel(Sender::new_from_user(owner))`./// That is the path that made "Created # Macro Support x …" render as You.#[tokio::test]diff --git a/crates/channels/src/inbound/toolset/create_channel.rs b/crates/channels/src/inbound/toolset/create_channel.rsindex ac4d264820..0534e3317d 100644--- a/crates/channels/src/inbound/toolset/create_channel.rs+++ b/crates/channels/src/inbound/toolset/create_channel.rs@@ -2,7 +2,7 @@ use super::{ChannelToolContext, channel_mutation_error, channel_name, parse_participants,participant_id_strings,};-use crate::domain::models::{ChannelType, CreateChannelRequest, Sender};+use crate::domain::models::{ChannelType, CreateChannelRequest};use crate::domain::ports::ChannelService;use ai_toolset::{AsyncTool, RequestContext, ServiceContext, ToolAnnotated, ToolAnnotations, ToolCallError,@@ -118,9 +118,9 @@ wherelet participant_ids = participant_id_strings(&participants);let response = service_context.service- .create_channel(- Sender::new_from_user(request_context.user_id.clone()),- None,+ .create_channel_on_behalf(+ request_context.user_id.clone(),+ service_context.actor,CreateChannelRequest {name: Some(name.clone()),channel_type: self.channel_type.to_domain(),diff --git a/crates/channels/src/inbound/toolset/test.rs b/crates/channels/src/inbound/toolset/test.rsindex 002b597989..996c5b2ad4 100644--- a/crates/channels/src/inbound/toolset/test.rs+++ b/crates/channels/src/inbound/toolset/test.rs@@ -39,7 +39,11 @@ fn user_id() -> MacroUserIdStr<'static> {MacroUserIdStr::try_from(TEST_USER_ID.to_string()).expect("valid macro user id")}-type CreatedChannelCall = (Sender, Option<i64>, CreateChannelRequest);+type CreatedChannelCall = (+ Sender,+ Option<MacroUserIdStr<'static>>,+ CreateChannelRequest,+);type PatchChannelCall = (Sender, Uuid, PatchChannelRequest);type AddParticipantsCall = (Sender, Uuid, AddParticipantsRequest);type RemoveParticipantsCall = (Sender, Uuid, RemoveParticipantsRequest);@@ -58,6 +62,23 @@ struct ToolTestChannelService {metadata_name: Option<String>,}+impl ToolTestChannelService {+ fn record_created(+ &self,+ actor: Sender,+ on_behalf_of: Option<MacroUserIdStr<'static>>,+ req: CreateChannelRequest,+ ) -> Result<CreateChannelResponse, ChannelMutationErr> {+ if let Some(message) = &self.create_error {+ return Err(ChannelMutationErr::BadRequest(message.clone()));+ }+ *self.created.lock().expect("create lock") = Some((actor, on_behalf_of, req));+ Ok(CreateChannelResponse {+ id: self.created_id.unwrap_or_else(Uuid::new_v4).to_string(),+ })+ }+}+impl ChannelService for ToolTestChannelService {async fn get_channel_messages(&self,@@ -147,16 +168,20 @@ impl ChannelService for ToolTestChannelService {async fn create_channel(&self,actor: Sender,- actor_org_id: Option<i64>,+ _actor_org_id: Option<i64>,req: CreateChannelRequest,) -> Result<CreateChannelResponse, ChannelMutationErr> {- if let Some(message) = &self.create_error {- return Err(ChannelMutationErr::BadRequest(message.clone()));- }- *self.created.lock().expect("create lock") = Some((actor, actor_org_id, req));- Ok(CreateChannelResponse {- id: self.created_id.unwrap_or_else(Uuid::new_v4).to_string(),- })+ let on_behalf_of = actor.as_user().cloned();+ self.record_created(actor, on_behalf_of, req)+ }++ async fn create_channel_on_behalf(+ &self,+ owner: MacroUserIdStr<'static>,+ actor: BotId,+ req: CreateChannelRequest,+ ) -> Result<CreateChannelResponse, ChannelMutationErr> {+ self.record_created(Sender::new_from_bot(actor), Some(owner), req)}async fn patch_channel(@@ -494,13 +519,16 @@ async fn create_private_channel_does_not_resolve_a_team() {assert_eq!(response.name, "Planning");assert_eq!(response.channel_type, NewChannelType::Private);assert_eq!(response.participants, vec!["macro|[email protected]"]);- let (actor, org_id, req) = created+ let (actor, on_behalf_of, req) = created.lock().expect("create lock").clone().expect("called");- assert_eq!(actor.as_user(), Some(&user_id()));- assert_eq!(org_id, None);+ assert_eq!(+ actor.as_bot().map(|id| id.bot_id()),+ Some(bot_id::MACRO_AI_BOT_ID)+ );+ assert_eq!(on_behalf_of.as_ref(), Some(&user_id()));assert_eq!(req.channel_type, ChannelType::Private);assert_eq!(req.team_id, None);assert!(!req.auto_join_team);@@ -533,16 +561,75 @@ async fn create_team_channel_injects_the_caller_when_participants_are_empty() {.expect("team create injects the caller");assert_eq!(response.participants, vec![TEST_USER_ID]);- let (_, _, req) = created+ let (actor, on_behalf_of, req) = created.lock().expect("create lock").clone().expect("called");+ assert_eq!(+ actor.as_bot().map(|id| id.bot_id()),+ Some(bot_id::MACRO_AI_BOT_ID)+ );+ assert_eq!(on_behalf_of.as_ref(), Some(&user_id()));assert_eq!(req.channel_type, ChannelType::Team);assert_eq!(req.team_id, Some(team_id));assert!(req.participants.contains(&user_id()));}+#[tokio::test]+async fn create_private_channel_uses_the_context_actor_for_the_user() {+ let default_service = ToolTestChannelService::default();+ let default_created = default_service.created.clone();+ let custom_service = ToolTestChannelService::default();+ let custom_created = custom_service.created.clone();++ let tool = CreateChannel {+ name: "Planning".to_string(),+ channel_type: NewChannelType::Private,+ participants: Vec::new(),+ };++ tool.call(+ ServiceContext(ChannelToolContext::new(+ default_service,+ NoOpEntityAccessService,+ )),+ RequestContext::new(user_id()),+ )+ .await+ .expect("default actor can create");+ tool.call(+ ServiceContext(+ ChannelToolContext::new(custom_service, NoOpEntityAccessService)+ .with_actor(BotId::TEST_A),+ ),+ RequestContext::new(user_id()),+ )+ .await+ .expect("custom actor can create");++ let (default_actor, default_on_behalf_of, _) = default_created+ .lock()+ .expect("create lock")+ .clone()+ .expect("called");+ let (custom_actor, custom_on_behalf_of, _) = custom_created+ .lock()+ .expect("create lock")+ .clone()+ .expect("called");+ assert_eq!(+ default_actor.as_bot().map(|id| id.bot_id()),+ Some(bot_id::MACRO_AI_BOT_ID)+ );+ assert_eq!(default_on_behalf_of.as_ref(), Some(&user_id()));+ assert_eq!(+ custom_actor.as_bot().map(|id| id.bot_id()),+ Some(BotId::TEST_A)+ );+ assert_eq!(custom_on_behalf_of.as_ref(), Some(&user_id()));+}+#[tokio::test]async fn create_team_channel_fails_when_the_user_has_no_team() {let service = ToolTestChannelService::default();