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.

Render bot mentions by their own name in notification text (#7055)

teoa242c0fdac46
diff --git a/crates/mention_utils/src/parse.rs b/crates/mention_utils/src/parse.rs
index 186cd52d80..68ad3ab247 100644
--- a/crates/mention_utils/src/parse.rs
+++ b/crates/mention_utils/src/parse.rs
@@ -1,3 +1,4 @@
+use bot_id::BotIdStr;
use channel_sender::ChannelSender;
use either::Either;
use nom::{
@@ -59,6 +60,26 @@ pub trait XmlTaggedParsed<'de>: Deserialize<'de> {
pub struct ParsedUserMention<'a> {
#[serde(borrow)]
pub user_id: ChannelSender<'a>,
+ /// For a bot mention this carries the bot's display name; see
+ /// [`crate::serialize::bot_mention`].
+ #[serde(borrow, default)]
+ pub email: Option<Cow<'a, str>>,
+ #[serde(borrow, default)]
+ pub display_name: Option<Cow<'a, str>>,
+}
+
+impl ParsedUserMention<'_> {
+ /// Display name for a mentioned bot. Prefers the names written into the
+ /// tag by the composer, then the first-party bot registry, so a bot never
+ /// renders under another bot's name.
+ fn bot_display_name(&self, bot: &BotIdStr<'_>) -> &str {
+ [self.display_name.as_deref(), self.email.as_deref()]
+ .into_iter()
+ .flatten()
+ .find(|name| !name.trim().is_empty())
+ .or_else(|| bot_id::system_bot(bot.bot_id()).map(|system| system.name))
+ .unwrap_or("Bot")
+ }
}
impl<'de> XmlTaggedParsed<'de> for ParsedUserMention<'de> {
@@ -376,7 +397,7 @@ impl XmlFormatter for PlainTextFormatter {
fn format_user(user: &ParsedUserMention<'_>, f: &mut Formatter<'_>) -> std::fmt::Result {
match &user.user_id.0 {
- Either::Left(_l) => write!(f, "Macro"),
+ Either::Left(bot) => write!(f, "{}", user.bot_display_name(bot)),
Either::Right(r) => write!(f, "{}", r.email_part().as_ref()),
}
}
diff --git a/crates/mention_utils/src/tests.rs b/crates/mention_utils/src/tests.rs
index 953ef2926d..3fbb13c6fd 100644
--- a/crates/mention_utils/src/tests.rs
+++ b/crates/mention_utils/src/tests.rs
@@ -78,7 +78,7 @@ fn parse_single_user_mention() {
let input = r#"<m-user-mention>{"userId":"macro|[email protected]","email":"[email protected]"}</m-user-mention>"#;
let out = ParsedXmlText::parse(input).unwrap();
assert_matches!(out.0, [
- TextSegment::Xml(XmlTag::User(ParsedUserMention { user_id }))
+ TextSegment::Xml(XmlTag::User(ParsedUserMention { user_id, .. }))
] => {
assert_eq!(user_id.as_ref(), "macro|[email protected]");
});
@@ -90,9 +90,9 @@ fn parse_multiple_user_mentions() {
let out = ParsedXmlText::parse(input).unwrap();
assert_matches!(out.0, [
TextSegment::Plain("Hello "),
- TextSegment::Xml(XmlTag::User(ParsedUserMention { user_id: uid1 })),
+ TextSegment::Xml(XmlTag::User(ParsedUserMention { user_id: uid1, .. })),
TextSegment::Plain(" and "),
- TextSegment::Xml(XmlTag::User(ParsedUserMention { user_id: uid2 })),
+ TextSegment::Xml(XmlTag::User(ParsedUserMention { user_id: uid2, .. })),
] => {
assert_eq!(uid1.as_ref(), "macro|[email protected]");
assert_eq!(uid2.as_ref(), "macro|[email protected]");
@@ -115,7 +115,7 @@ fn parse_bare_uuid_bot_mention() {
let input = r#"<m-user-mention>{"userId":"bot|00000000-0000-0000-0000-00000000a1a1","email":"Macro"}</m-user-mention> there will come a day when I shall liberate you"#;
let out = ParsedXmlText::parse(input).unwrap();
assert_matches!(out.0, [
- TextSegment::Xml(XmlTag::User(ParsedUserMention { user_id })),
+ TextSegment::Xml(XmlTag::User(ParsedUserMention { user_id, .. })),
TextSegment::Plain(" there will come a day when I shall liberate you"),
] => {
assert_eq!(user_id.into_bot().unwrap().as_uuid().to_string(), "00000000-0000-0000-0000-00000000a1a1");
@@ -127,7 +127,7 @@ fn parse_prefixed_bot_mention() {
let input = r#"<m-user-mention>{"userId":"bot|00000000-0000-0000-0000-00000000a1a1","email":"Macro"}</m-user-mention>"#;
let out = ParsedXmlText::parse(input).unwrap();
assert_matches!(out.0, [
- TextSegment::Xml(XmlTag::User(ParsedUserMention { user_id })),
+ TextSegment::Xml(XmlTag::User(ParsedUserMention { user_id, .. })),
] => {
assert_eq!(user_id.as_ref(), "bot|00000000-0000-0000-0000-00000000a1a1");
});
@@ -141,6 +141,56 @@ fn bot_mention_renders_display_name() {
assert_eq!(rendered, "hi Macro ok");
}
+#[test]
+fn cursor_bot_mention_renders_cursor_not_macro() {
+ let input = r#"<m-user-mention>{"userId":"bot|00000000-0000-0000-0000-00000000c5c5","email":"Cursor","displayName":"Cursor"}</m-user-mention> investigate this"#;
+ let parsed = ParsedXmlText::parse(input).unwrap();
+ let rendered = PlainTextFormatter::format_xml_text(parsed).0;
+ assert_eq!(rendered, "Cursor investigate this");
+}
+
+#[test]
+fn bot_mention_prefers_display_name_over_email() {
+ let input = r#"<m-user-mention>{"userId":"bot|11111111-1111-1111-1111-111111111111","email":"Helper Bot","displayName":"Helper"}</m-user-mention>"#;
+ let parsed = ParsedXmlText::parse(input).unwrap();
+ let rendered = PlainTextFormatter::format_xml_text(parsed).0;
+ assert_eq!(rendered, "Helper");
+}
+
+#[test]
+fn team_bot_mention_renders_email_field_as_name() {
+ let input = r#"<m-user-mention>{"userId":"bot|11111111-1111-1111-1111-111111111111","email":"Helper Bot"}</m-user-mention>"#;
+ let parsed = ParsedXmlText::parse(input).unwrap();
+ let rendered = PlainTextFormatter::format_xml_text(parsed).0;
+ assert_eq!(rendered, "Helper Bot");
+}
+
+#[test]
+fn system_bot_mention_without_name_fields_uses_registry() {
+ let input =
+ r#"<m-user-mention>{"userId":"bot|00000000-0000-0000-0000-00000000c5c5"}</m-user-mention>"#;
+ let parsed = ParsedXmlText::parse(input).unwrap();
+ let rendered = PlainTextFormatter::format_xml_text(parsed).0;
+ assert_eq!(rendered, "Cursor");
+}
+
+#[test]
+fn bot_mention_ignores_blank_name_fields() {
+ let input = r#"<m-user-mention>{"userId":"bot|00000000-0000-0000-0000-00000000c5c5","email":"","displayName":" "}</m-user-mention>"#;
+ let parsed = ParsedXmlText::parse(input).unwrap();
+ let rendered = PlainTextFormatter::format_xml_text(parsed).0;
+ assert_eq!(rendered, "Cursor");
+}
+
+#[test]
+fn unknown_bot_mention_without_name_fields_uses_generic_label() {
+ let input =
+ r#"<m-user-mention>{"userId":"bot|11111111-1111-1111-1111-111111111111"}</m-user-mention>"#;
+ let parsed = ParsedXmlText::parse(input).unwrap();
+ let rendered = PlainTextFormatter::format_xml_text(parsed).0;
+ assert_eq!(rendered, "Bot");
+}
+
#[test]
fn bot_mention_null_formatter_strips_mention() {
let input = r#"hi <m-user-mention>{"userId":"bot|00000000-0000-0000-0000-00000000a1a1","email":"Macro"}</m-user-mention> ok"#;
diff --git a/crates/model_notifications/src/metadata/test.rs b/crates/model_notifications/src/metadata/test.rs
index dd4c91410e..9619696a31 100644
--- a/crates/model_notifications/src/metadata/test.rs
+++ b/crates/model_notifications/src/metadata/test.rs
@@ -574,6 +574,27 @@ fn channel_mention_title_falls_back_to_bot_display_name() {
assert_eq!(title, "Helper Bot mentioned you in #general");
}
+#[test]
+fn channel_message_body_renders_mentioned_agent_by_its_own_name() {
+ let notification = ChannelMessageSendMetadata {
+ sender: Some(uid("macro|[email protected]")),
+ sender_display_name: None,
+ message_content: r#"<m-user-mention>{"userId":"bot|00000000-0000-0000-0000-00000000c5c5","email":"Cursor","displayName":"Cursor"}</m-user-mention> push notifications resolve mentions wrong"#.to_string(),
+ message_id: Uuid::nil().to_string(),
+ has_attachments: false,
+ common: CommonChannelMetadata {
+ channel_type: ChannelType::Team,
+ channel_name: "AI Team".to_string(),
+ },
+ sender_profile_picture_url: None,
+ };
+
+ assert_eq!(
+ notification.format_body(None).unwrap(),
+ "Cursor push notifications resolve mentions wrong"
+ );
+}
+
fn document_mention(sub_type: Option<NotificationDocumentSubType>) -> DocumentMentionMetadata {
DocumentMentionMetadata {
document_name: "Q3 plan".to_string(),