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.

fix(channels): extract bot message mentions via lexical service (#4771)

Eric Hayes136c60463621
diff --git a/.github/workspace-dep-closures.json b/.github/workspace-dep-closures.json
index e890ed9afa..9869105ed3 100644
--- a/.github/workspace-dep-closures.json
+++ b/.github/workspace-dep-closures.json
@@ -57,6 +57,7 @@
"crates/item_filters",
"crates/last_online_tracker",
"crates/lexical_client",
+ "crates/lexical_mention_extractor",
"crates/macro_auth",
"crates/macro_aws_config",
"crates/macro_db_client",
@@ -220,6 +221,7 @@
"crates/item_filters",
"crates/last_online_tracker",
"crates/lexical_client",
+ "crates/lexical_mention_extractor",
"crates/macro_auth",
"crates/macro_aws_config",
"crates/macro_db_client",
@@ -669,6 +671,7 @@
"crates/item_filters",
"crates/last_online_tracker",
"crates/lexical_client",
+ "crates/lexical_mention_extractor",
"crates/macro_auth",
"crates/macro_aws_config",
"crates/macro_db_client",
@@ -1435,6 +1438,7 @@
"crates/item_filters",
"crates/last_online_tracker",
"crates/lexical_client",
+ "crates/lexical_mention_extractor",
"crates/macro_auth",
"crates/macro_aws_config",
"crates/macro_config",
@@ -1558,6 +1562,7 @@
"crates/item_filters",
"crates/last_online_tracker",
"crates/lexical_client",
+ "crates/lexical_mention_extractor",
"crates/macro_auth",
"crates/macro_aws_config",
"crates/macro_axum_utils",
@@ -2960,6 +2965,86 @@
"crates/user_quota",
"crates/workspace-hack"
],
+ "lexical_mention_extractor": [
+ "crates/agent",
+ "crates/ai_toolset",
+ "crates/ai_usage",
+ "crates/attachment",
+ "crates/authentication_service_client",
+ "crates/bot_id",
+ "crates/call",
+ "crates/channel_sender",
+ "crates/channels",
+ "crates/connection",
+ "crates/connection_gateway_client",
+ "crates/connection_gateway_models",
+ "crates/contacts",
+ "crates/cowlike",
+ "crates/crm",
+ "crates/decode_jwt",
+ "crates/document_sub_type",
+ "crates/email",
+ "crates/email_db_client",
+ "crates/email_utils",
+ "crates/entity_access",
+ "crates/entity_access_db_utils",
+ "crates/entity_access_management",
+ "crates/filter_ast",
+ "crates/frecency",
+ "crates/invite_email",
+ "crates/ip_extractor",
+ "crates/item_filters",
+ "crates/last_online_tracker",
+ "crates/lexical_client",
+ "crates/lexical_mention_extractor",
+ "crates/macro_auth",
+ "crates/macro_aws_config",
+ "crates/macro_db_client",
+ "crates/macro_env",
+ "crates/macro_env_var",
+ "crates/macro_event_broker",
+ "crates/macro_event_topics",
+ "crates/macro_middleware",
+ "crates/macro_service_urls",
+ "crates/macro_user_id",
+ "crates/macro_uuid",
+ "crates/mention_utils",
+ "crates/model",
+ "crates/model-entity",
+ "crates/model-error-response",
+ "crates/model_file_type",
+ "crates/model_notifications",
+ "crates/model_user",
+ "crates/models_ai_projection",
+ "crates/models_bulk_upload",
+ "crates/models_dcs",
+ "crates/models_email",
+ "crates/models_entity_access_management",
+ "crates/models_opensearch",
+ "crates/models_pagination",
+ "crates/models_permissions",
+ "crates/models_properties",
+ "crates/models_search",
+ "crates/models_search_cursor",
+ "crates/models_soup",
+ "crates/models_team",
+ "crates/non_empty",
+ "crates/notification",
+ "crates/opensearch_client",
+ "crates/opensearch_query_builder",
+ "crates/prompt",
+ "crates/properties",
+ "crates/rate_limit",
+ "crates/remote_env_var",
+ "crates/s3_key",
+ "crates/share_permission_db_utils",
+ "crates/sqs_client",
+ "crates/sqs_worker",
+ "crates/system_properties",
+ "crates/unfurl",
+ "crates/user_quota",
+ "crates/workspace-hack"
+ ],
"load-test": [
"crates/macro_env_var",
"crates/workspace-hack",
@@ -3316,6 +3401,7 @@
"crates/item_filters",
"crates/last_online_tracker",
"crates/lexical_client",
+ "crates/lexical_mention_extractor",
"crates/macro_auth",
"crates/macro_aws_config",
"crates/macro_config",
@@ -3420,6 +3506,7 @@
"crates/item_filters",
"crates/last_online_tracker",
"crates/lexical_client",
+ "crates/lexical_mention_extractor",
"crates/macro_auth",
"crates/macro_aws_config",
"crates/macro_db_client",
@@ -4252,6 +4339,7 @@
"crates/item_filters",
"crates/last_online_tracker",
"crates/lexical_client",
+ "crates/lexical_mention_extractor",
"crates/macro_auth",
"crates/macro_aws_config",
"crates/macro_config",
diff --git a/Cargo.lock b/Cargo.lock
index fd27c0b290..677b2af1e3 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -198,6 +198,7 @@ dependencies = [
"item_filters",
"lazy_static",
"lexical_client",
+ "lexical_mention_extractor",
"macro_aws_config",
"macro_db_client",
"macro_env",
@@ -4223,6 +4224,7 @@ dependencies = [
"last_online_tracker",
"lazy_static",
"lexical_client",
+ "lexical_mention_extractor",
"macro_auth",
"macro_aws_config",
"macro_axum_utils",
@@ -7613,6 +7615,16 @@ dependencies = [
"workspace-hack",
]
+[[package]]
+name = "lexical_mention_extractor"
+version = "0.1.0"
+dependencies = [
+ "anyhow",
+ "channels",
+ "lexical_client",
+ "workspace-hack",
+]
+
[[package]]
name = "libbz2-rs-sys"
version = "0.1.3"
diff --git a/crates/ai_tools/Cargo.toml b/crates/ai_tools/Cargo.toml
index 22c7d77c47..2897fb8c68 100644
--- a/crates/ai_tools/Cargo.toml
+++ b/crates/ai_tools/Cargo.toml
@@ -38,6 +38,7 @@ futures.workspace = true
item_filters = { path = "../item_filters" }
lazy_static.workspace = true
lexical_client = { path = "../lexical_client" }
+lexical_mention_extractor = { path = "../lexical_mention_extractor" }
macro_aws_config = { path = "../macro_aws_config" }
macro_db_client = { path = "../macro_db_client" }
macro_env_var = { path = "../macro_env_var" }
diff --git a/crates/ai_tools/src/build_context.rs b/crates/ai_tools/src/build_context.rs
index 45799fe02a..da13fd4da8 100644
--- a/crates/ai_tools/src/build_context.rs
+++ b/crates/ai_tools/src/build_context.rs
@@ -203,7 +203,10 @@ pub async fn build_tool_service_context_from_env(
PgChannelsRepo::new(pool.clone()),
frecency_storage,
);
- let channel_tool_context = crate::tool_context::build_channel_tool_context(pool.clone());
+ let channel_tool_context = crate::tool_context::build_channel_tool_context(
+ pool.clone(),
+ Arc::new(lexical_client.clone()),
+ );
let email_service_for_tools: Arc<crate::tool_context::ToolEmailService> =
Arc::new(email_service.clone());
let foreign_entity_service =
diff --git a/crates/ai_tools/src/tool_context.rs b/crates/ai_tools/src/tool_context.rs
index f06ac15c89..f5615f9be8 100644
--- a/crates/ai_tools/src/tool_context.rs
+++ b/crates/ai_tools/src/tool_context.rs
@@ -26,6 +26,7 @@ use foreign_entity::{
domain::service::ForeignEntityServiceImpl,
outbound::pg_foreign_entity_repo::PgForeignEntityRepo,
};
+use lexical_mention_extractor::LexicalMentionExtractor;
use macro_user_id::user_id::MacroUserIdStr;
use notification::inbound::ai_tool::NotificationToolContext;
use properties::inbound::toolset::PropertiesToolContext;
@@ -96,8 +97,12 @@ pub type ToolCommsService = ChannelListServiceImpl<
pub type ToolChannelEventDispatcher = std::sync::Arc<dyn ChannelEventDispatcher>;
/// Type alias for the channel messages service implementation used by AI tools.
-pub type ToolChannelMessagesService =
- ChannelServiceImpl<PgChannelsRepo, ToolChannelEventDispatcher>;
+pub type ToolChannelMessagesService = ChannelServiceImpl<
+ PgChannelsRepo,
+ ToolChannelEventDispatcher,
+ NoopChannelReferenceSharePermissions,
+ LexicalMentionExtractor,
+>;
/// Type alias for the channel AI tool context.
pub type ToolChannelToolContext =
@@ -105,10 +110,16 @@ pub type ToolChannelToolContext =
/// Build the channel AI tool context from a Postgres pool, with no side
/// effects (no notifications/realtime) for hosts that lack those clients.
-pub fn build_channel_tool_context(pool: sqlx::PgPool) -> ToolChannelToolContext {
+/// `lexical_client` derives the mention list for messages the agent sends,
+/// since bot-authored content arrives without the editor-tracked mentions.
+pub fn build_channel_tool_context(
+ pool: sqlx::PgPool,
+ lexical_client: Arc<lexical_client::LexicalClient>,
+) -> ToolChannelToolContext {
build_channel_tool_context_with_dispatcher(
pool,
std::sync::Arc::new(NoopChannelEventDispatcher),
+ lexical_client,
)
}
@@ -118,13 +129,15 @@ pub fn build_channel_tool_context(pool: sqlx::PgPool) -> ToolChannelToolContext
pub fn build_channel_tool_context_with_dispatcher(
pool: sqlx::PgPool,
dispatcher: ToolChannelEventDispatcher,
+ lexical_client: Arc<lexical_client::LexicalClient>,
) -> ToolChannelToolContext {
ChannelToolContext::new(
ChannelServiceImpl::with_dependencies(
PgChannelsRepo::new(pool.clone()),
dispatcher,
NoopChannelReferenceSharePermissions,
- ),
+ )
+ .with_mention_extractor(LexicalMentionExtractor::new(lexical_client)),
entity_access::domain::service::EntityAccessServiceImpl::new(
entity_access::outbound::PgAccessRepository::new(pool),
),
diff --git a/crates/channels/src/domain/ports.rs b/crates/channels/src/domain/ports.rs
index 732ed472de..cfcc182152 100644
--- a/crates/channels/src/domain/ports.rs
+++ b/crates/channels/src/domain/ports.rs
@@ -1014,6 +1014,22 @@ pub trait ChannelReferenceSharePermissions: Send + Sync + 'static {
) -> impl Future<Output = Result<(), Self::Err>> + Send;
}
+/// Extractor of the mentions embedded in message content.
+///
+/// The web editor tracks mentions while a user composes a message and sends
+/// them alongside it; bot-authored content arrives as raw macro markdown, so
+/// the service uses this port to derive the equivalent mention list.
+pub trait ChannelMentionExtractor: Send + Sync + 'static {
+ /// Error type for mention extraction.
+ type Err: Into<anyhow::Error> + Send;
+
+ /// Extract the entity mentions embedded in `content`.
+ fn extract_mentions(
+ &self,
+ content: &str,
+ ) -> impl Future<Output = Result<Vec<SimpleMention>, Self::Err>> + Send;
+}
+
/// Errors that can occur while mutating channels.
#[derive(Debug, thiserror::Error)]
pub enum ChannelMutationErr {
diff --git a/crates/channels/src/domain/service.rs b/crates/channels/src/domain/service.rs
index c9e937cbc0..64ac7bd782 100644
--- a/crates/channels/src/domain/service.rs
+++ b/crates/channels/src/domain/service.rs
@@ -14,9 +14,9 @@ use crate::domain::{
WithChannelId,
},
ports::{
- ChannelAttachmentsPage, ChannelEventDispatcher, ChannelMessagesErr,
- ChannelMessagesQueryResult, ChannelMutationErr, ChannelReferenceSharePermissions,
- ChannelRepo, ChannelService,
+ ChannelAttachmentsPage, ChannelEventDispatcher, ChannelMentionExtractor,
+ ChannelMessagesErr, ChannelMessagesQueryResult, ChannelMutationErr,
+ ChannelReferenceSharePermissions, ChannelRepo, ChannelService,
},
};
use bot_id::BotIdStr;
@@ -39,10 +39,12 @@ pub struct ChannelServiceImpl<
R,
E = NoopChannelEventDispatcher,
P = NoopChannelReferenceSharePermissions,
+ M = NoopChannelMentionExtractor,
> {
repo: R,
events: E,
reference_share_permissions: P,
+ mention_extractor: M,
}
/// No-op event dispatcher used by read-only contexts.
@@ -70,6 +72,19 @@ impl ChannelReferenceSharePermissions for NoopChannelReferenceSharePermissions {
}
}
+/// No-op mention extractor used by contexts that don't derive mentions from
+/// message content.
+#[derive(Debug, Clone, Copy, Default)]
+pub struct NoopChannelMentionExtractor;
+
+impl ChannelMentionExtractor for NoopChannelMentionExtractor {
+ type Err = anyhow::Error;
+
+ async fn extract_mentions(&self, _content: &str) -> Result<Vec<SimpleMention>, Self::Err> {
+ Ok(Vec::new())
+ }
+}
+
impl<R> ChannelServiceImpl<R, NoopChannelEventDispatcher, NoopChannelReferenceSharePermissions>
where
R: ChannelRepo,
@@ -80,6 +95,7 @@ where
repo,
events: NoopChannelEventDispatcher,
reference_share_permissions: NoopChannelReferenceSharePermissions,
+ mention_extractor: NoopChannelMentionExtractor,
}
}
}
@@ -91,11 +107,28 @@ impl<R, E, P> ChannelServiceImpl<R, E, P> {
repo,
events,
reference_share_permissions,
+ mention_extractor: NoopChannelMentionExtractor,
+ }
+ }
+}
+
+impl<R, E, P, M> ChannelServiceImpl<R, E, P, M> {
+ /// Replace the mention extractor used to derive mentions from
+ /// bot-authored message content.
+ pub fn with_mention_extractor<M2>(
+ self,
+ mention_extractor: M2,
+ ) -> ChannelServiceImpl<R, E, P, M2> {
+ ChannelServiceImpl {
+ repo: self.repo,
+ events: self.events,
+ reference_share_permissions: self.reference_share_permissions,
+ mention_extractor,
}
}
}
-impl<R, E, P> ChannelServiceImpl<R, E, P>
+impl<R, E, P, M> ChannelServiceImpl<R, E, P, M>
where
R: ChannelRepo,
anyhow::Error: From<R::Err>,
@@ -294,11 +327,12 @@ fn created_channel_participant_ids<'a>(
.collect()
}
-impl<R, E, P> ChannelServiceImpl<R, E, P>
+impl<R, E, P, M> ChannelServiceImpl<R, E, P, M>
where
R: ChannelRepo,
E: ChannelEventDispatcher,
P: ChannelReferenceSharePermissions,
+ M: ChannelMentionExtractor,
{
#[tracing::instrument(err, skip(self, req))]
async fn create_channel(
@@ -481,6 +515,18 @@ where
Ok(())
}
+ /// Best-effort extraction of the mentions embedded in message content;
+ /// extraction failure yields no mentions rather than failing the send.
+ async fn extract_content_mentions(&self, content: &str) -> Vec<SimpleMention> {
+ match self.mention_extractor.extract_mentions(content).await {
+ Ok(mentions) => mentions,
+ Err(err) => {
+ tracing::error!(error=?err.into(), "unable to extract mentions from message content");
+ Vec::new()
+ }
+ }
+ }
+
#[tracing::instrument(err, skip(self, req))]
async fn post_message(
&self,
@@ -488,6 +534,14 @@ where
channel_id: Uuid,
req: PostMessageRequest,
) -> Result<PostMessageResponse, ChannelMutationErr> {
+ // Bots send raw macro markdown without a tracked mention list (the web
+ // editor builds that list for user-authored messages), so derive it
+ // from the content to keep bot-created references tracked.
+ let mut req = req;
+ if actor.as_bot().is_some() && req.mentions.is_empty() {
+ req.mentions = self.extract_content_mentions(&req.content).await;
+ }
+
let message = self
.repo
.create_message(
@@ -600,6 +654,16 @@ where
notification_policy,
} = req;
+ // As in post_message: bots don't track a mention list, so when a bot
+ // replaces message content (e.g. Macro AI swapping its "thinking"
+ // placeholder for the reply), derive the mentions from the new content.
+ let replacement_mentions = match (replacement_mentions, &content) {
+ (None, Some(content)) if actor.as_bot().is_some() => {
+ Some(self.extract_content_mentions(content).await)
+ }
+ (mentions, _) => mentions,
+ };
+
let owner = self
.repo
.get_message_owner(channel_id, message_id)
@@ -1056,11 +1120,12 @@ where
}
}
-impl<R, E, P> ChannelServiceImpl<R, E, P>
+impl<R, E, P, M> ChannelServiceImpl<R, E, P, M>
where
R: ChannelRepo,
E: ChannelEventDispatcher,
P: ChannelReferenceSharePermissions,
+ M: ChannelMentionExtractor,
{
async fn create_channel_record<'a>(
&self,
@@ -1281,11 +1346,12 @@ fn center_window(
}
}
-impl<R, E, P> ChannelService for ChannelServiceImpl<R, E, P>
+impl<R, E, P, M> ChannelService for ChannelServiceImpl<R, E, P, M>
where
R: ChannelRepo,
E: ChannelEventDispatcher,
P: ChannelReferenceSharePermissions,
+ M: ChannelMentionExtractor,
anyhow::Error: From<R::Err>,
{
#[tracing::instrument(err, skip(self))]
diff --git a/crates/channels/src/domain/service/test.rs b/crates/channels/src/domain/service/test.rs
index eb4ef81511..6495beaff7 100644
--- a/crates/channels/src/domain/service/test.rs
+++ b/crates/channels/src/domain/service/test.rs
@@ -12,8 +12,8 @@ use crate::domain::{
Sender, SimpleMention, ThreadData, ThreadReplyRow, TopLevelMessageRow,
},
ports::{
- ChannelEventDispatcher, ChannelReferenceSharePermissions, ChannelRepo, MockChannelRepo,
- TopLevelMessagesQueryResult,
+ ChannelEventDispatcher, ChannelMentionExtractor, ChannelReferenceSharePermissions,
+ ChannelRepo, MockChannelRepo, TopLevelMessagesQueryResult,
},
};
use channel_sender::ChannelSender;
@@ -291,6 +291,8 @@ struct FakeMutationRepoState {
patched_content: Option<String>,
activity_upserts: usize,
removed_participants: Vec<String>,
+ created_mentions: Vec<SimpleMention>,
+ synced_mentions: Vec<SimpleMention>,
}
impl FakeMutationRepo {
@@ -336,6 +338,8 @@ impl FakeMutationRepo {
patched_content: None,
activity_upserts: 0,
removed_participants: vec![],
+ created_mentions: vec![],
+ synced_mentions: vec![],
})),
}
}
@@ -572,16 +576,18 @@ impl ChannelRepo for FakeMutationRepo {
async fn create_message_mentions(
&self,
_message_id: Uuid,
- _mentions: Vec<SimpleMention>,
+ mentions: Vec<SimpleMention>,
) -> Result<(), Self::Err> {
+ self.state.lock().unwrap().created_mentions.extend(mentions);
Ok(())
}
async fn sync_message_mentions(
&self,
_message_id: Uuid,
- _mentions: Vec<SimpleMention>,
+ mentions: Vec<SimpleMention>,
) -> Result<(), Self::Err> {
+ self.state.lock().unwrap().synced_mentions.extend(mentions);
Ok(())
}
@@ -805,6 +811,30 @@ impl ChannelReferenceSharePermissions for FakeReferenceSharing {
}
}
+#[derive(Clone, Default)]
+struct FakeMentionExtractor {
+ mentions: Vec<SimpleMention>,
+ calls: Arc<Mutex<Vec<String>>>,
+}
+
+impl FakeMentionExtractor {
+ fn new(mentions: Vec<SimpleMention>) -> Self {
+ Self {
+ mentions,
+ calls: Arc::default(),
+ }
+ }
+}
+
+impl ChannelMentionExtractor for FakeMentionExtractor {
+ type Err = anyhow::Error;
+
+ async fn extract_mentions(&self, content: &str) -> Result<Vec<SimpleMention>, Self::Err> {
+ self.calls.lock().unwrap().push(content.to_string());
+ Ok(self.mentions.clone())
+ }
+}
+
fn mutation_service(
repo: FakeMutationRepo,
events: FakeEvents,
@@ -939,6 +969,174 @@ async fn bot_post_message_persists_bot_sender_and_skips_user_only_effects() {
);
}
+#[tokio::test]
+async fn bot_post_message_derives_mentions_from_content() {
+ let channel_id = Uuid::new_v4();
+ let actor = Sender::new_from_bot(BotId::new_from_uuid(Uuid::new_v4()));
+ let repo = FakeMutationRepo::new(channel_id, "macro|[email protected]");
+ let events = FakeEvents::default();
+ let expected = vec![SimpleMention {
+ entity_type: "document".to_string(),
+ entity_id: "doc-1".to_string(),
+ }];
+ let extractor = FakeMentionExtractor::new(expected.clone());
+ let svc = mutation_service(
+ repo.clone(),
+ events.clone(),
+ FakeReferenceSharing::default(),
+ )
+ .with_mention_extractor(extractor.clone());
+
+ svc.post_message(
+ actor,
+ channel_id,
+ PostMessageRequest {
+ content: "see the doc".to_string(),
+ mentions: vec![],
+ thread_id: None,
+ attachments: vec![],
+ nonce: None,
+ notification_policy: Default::default(),
+ triggered_by: None,
+ },
+ )
+ .await
+ .unwrap();
+
+ assert_eq!(
+ extractor.calls.lock().unwrap().clone(),
+ vec!["see the doc".to_string()]
+ );
+ assert_eq!(repo.state.lock().unwrap().created_mentions, expected);
+
+ let emitted = events.events.lock().unwrap();
+ let ChannelEvent::MessagePosted { mentions, .. } = &emitted[0] else {
+ panic!("expected MessagePosted event, got {:?}", emitted[0]);
+ };
+ assert_eq!(*mentions, expected);
+}
+
+#[tokio::test]
+async fn bot_post_message_with_explicit_mentions_skips_extraction() {
+ let channel_id = Uuid::new_v4();
+ let actor = Sender::new_from_bot(BotId::new_from_uuid(Uuid::new_v4()));
+ let repo = FakeMutationRepo::new(channel_id, "macro|[email protected]");
+ let extractor = FakeMentionExtractor::new(vec![SimpleMention {
+ entity_type: "document".to_string(),
+ entity_id: "derived".to_string(),
+ }]);
+ let svc = mutation_service(
+ repo.clone(),
+ FakeEvents::default(),
+ FakeReferenceSharing::default(),
+ )
+ .with_mention_extractor(extractor.clone());
+
+ let explicit = vec![SimpleMention {
+ entity_type: "document".to_string(),
+ entity_id: "explicit".to_string(),
+ }];
+ svc.post_message(
+ actor,
+ channel_id,
+ PostMessageRequest {
+ content: "bot update".to_string(),
+ mentions: explicit.clone(),
+ thread_id: None,
+ attachments: vec![],
+ nonce: None,
+ notification_policy: Default::default(),
+ triggered_by: None,
+ },
+ )
+ .await
+ .unwrap();
+
+ assert!(extractor.calls.lock().unwrap().is_empty());
+ assert_eq!(repo.state.lock().unwrap().created_mentions, explicit);
+}
+
+#[tokio::test]
+async fn user_post_message_does_not_derive_mentions_from_content() {
+ let channel_id = Uuid::new_v4();
+ let repo = FakeMutationRepo::new(channel_id, "macro|[email protected]");
+ let events = FakeEvents::default();
+ let extractor = FakeMentionExtractor::new(vec![SimpleMention {
+ entity_type: "document".to_string(),
+ entity_id: "doc-1".to_string(),
+ }]);
+ let svc = mutation_service(
+ repo.clone(),
+ events.clone(),
+ FakeReferenceSharing::default(),
+ )
+ .with_mention_extractor(extractor.clone());
+
+ svc.post_message(
+ sender("macro|[email protected]"),
+ channel_id,
+ PostMessageRequest {
+ content: "see the doc".to_string(),
+ mentions: vec![],
+ thread_id: None,
+ attachments: vec![],
+ nonce: None,
+ notification_policy: Default::default(),
+ triggered_by: None,
+ },
+ )
+ .await
+ .unwrap();
+
+ assert!(extractor.calls.lock().unwrap().is_empty());
+ assert!(repo.state.lock().unwrap().created_mentions.is_empty());
+}
+
+#[tokio::test]
+async fn bot_patch_message_derives_replacement_mentions_from_content() {
+ let channel_id = Uuid::new_v4();
+ let bot_id = BotId::new_from_uuid(Uuid::new_v4());
+ let bot_sender = bot_id.into_storage_id().to_string();
+ let repo = FakeMutationRepo::new(channel_id, &bot_sender);
+ repo.state.lock().unwrap().message.sender_id = Sender::new_from_bot(bot_id);
+ let message_id = repo.state.lock().unwrap().message.id;
+ let events = FakeEvents::default();
+ let expected = vec![SimpleMention {
+ entity_type: "chat".to_string(),
+ entity_id: "doc-2".to_string(),
+ }];
+ let extractor = FakeMentionExtractor::new(expected.clone());
+ let svc = mutation_service(
+ repo.clone(),
+ events.clone(),
+ FakeReferenceSharing::default(),
+ )
+ .with_mention_extractor(extractor.clone());
+
+ svc.patch_message(
+ Sender::new_from_bot(bot_id),
+ ParticipantRole::Member,
+ channel_id,
+ message_id,
+ PatchMessageRequest {
+ content: Some("final answer".to_string()),
+ mentions: None,
+ attachment_ids_to_delete: None,
+ attachments_to_add: None,
+ nonce: None,
+ notification_policy: PatchMessageNotificationPolicy::NotifyAsPostedMessage,
+ },
+ )
+ .await
+ .unwrap();
+
+ assert_eq!(
+ extractor.calls.lock().unwrap().clone(),
+ vec!["final answer".to_string()]
+ );
+ assert_eq!(repo.state.lock().unwrap().synced_mentions, expected);
+}
+
#[tokio::test]
async fn patch_message_content_emits_message_changed_event_to_thread_participants() {
let channel_id = Uuid::new_v4();
diff --git a/crates/lexical_client/src/parse_markdown.rs b/crates/lexical_client/src/parse_markdown.rs
index 570092b73e..a6444b247e 100644
--- a/crates/lexical_client/src/parse_markdown.rs
+++ b/crates/lexical_client/src/parse_markdown.rs
@@ -28,6 +28,27 @@ struct MarkdownResponse {
data: String,
}
+#[derive(Debug, serde::Serialize)]
+struct MentionsRequest<'a> {
+ markdown: &'a str,
+}
+
+/// An entity mention extracted from markdown by the lexical service
+/// `/mentions` endpoint, in the shape channel messages track them.
+#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
+#[serde(rename_all = "camelCase")]
+pub struct ExtractedMention {
+ /// Mentioned entity type (e.g. `document`, `channel`, `user`).
+ pub entity_type: String,
+ /// Mentioned entity id.
+ pub entity_id: String,
+}
+
+#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
+struct MentionsResponse {
+ mentions: Vec<ExtractedMention>,
+}
+
/// Rendering target supported by the lexical service `/markdown` endpoint.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MarkdownTarget {
@@ -196,6 +217,23 @@ impl LexicalClient {
Ok(bytes.to_vec())
}
+ /// Parses `markdown` via the lexical service and returns the entity
+ /// mentions it contains.
+ #[tracing::instrument(skip(self, markdown), err)]
+ pub async fn extract_mentions(&self, markdown: &str) -> Result<Vec<ExtractedMention>> {
+ let url = format!("{}/mentions", self.url);
+ let response = check_response(
+ self.client
+ .post(&url)
+ .json(&MentionsRequest { markdown })
+ .send()
+ .await?,
+ )
+ .await?;
+ let data: MentionsResponse = response.json().await.context("unexpected response")?;
+ Ok(data.mentions)
+ }
+
async fn get_json<T: DeserializeOwned>(&self, url: &str) -> Result<T> {
let response = check_response(self.client.get(url).send().await?).await?;
response.json().await.context("unexpected response")
diff --git a/crates/lexical_mention_extractor/Cargo.toml b/crates/lexical_mention_extractor/Cargo.toml
new file mode 100644
index 0000000000..fea575e6fa
--- /dev/null
+++ b/crates/lexical_mention_extractor/Cargo.toml
@@ -0,0 +1,13 @@
+[package]
+edition = "2024"
+name = "lexical_mention_extractor"
+publish = false
+version = "0.1.0"
+
+[dependencies]
+anyhow = { workspace = true }
+channels = { path = "../channels", default-features = false, features = [
+ "ports",
+] }
+lexical_client = { path = "../lexical_client" }
+workspace-hack = { version = "0.1", path = "../workspace-hack" }
diff --git a/crates/lexical_mention_extractor/src/lib.rs b/crates/lexical_mention_extractor/src/lib.rs
new file mode 100644
index 0000000000..b6345d287b
--- /dev/null
+++ b/crates/lexical_mention_extractor/src/lib.rs
@@ -0,0 +1,39 @@
+#![deny(missing_docs)]
+//! Lexical-service adapter for extracting the mentions embedded in channel
+//! message content, implementing the `channels` domain's
+//! [`ChannelMentionExtractor`] port.
+
+use channels::domain::models::SimpleMention;
+use channels::domain::ports::ChannelMentionExtractor;
+use lexical_client::LexicalClient;
+use std::sync::Arc;
+
+/// Mention extractor backed by the lexical service `/mentions` endpoint.
+#[derive(Clone)]
+pub struct LexicalMentionExtractor {
+ client: Arc<LexicalClient>,
+}
+
+impl LexicalMentionExtractor {
+ /// Create a new extractor backed by `client`.
+ pub fn new(client: Arc<LexicalClient>) -> Self {
+ Self { client }
+ }
+}
+
+impl ChannelMentionExtractor for LexicalMentionExtractor {
+ type Err = anyhow::Error;
+
+ async fn extract_mentions(&self, content: &str) -> Result<Vec<SimpleMention>, Self::Err> {
+ Ok(self
+ .client
+ .extract_mentions(content)
+ .await?
+ .into_iter()
+ .map(|mention| SimpleMention {
+ entity_type: mention.entity_type,
+ entity_id: mention.entity_id,
+ })
+ .collect())
+ }
+}
diff --git a/crates/memory/src/context.rs b/crates/memory/src/context.rs
index ced721b3cf..90e6bc9661 100644
--- a/crates/memory/src/context.rs
+++ b/crates/memory/src/context.rs
@@ -155,7 +155,7 @@ pub async fn build_tool_service_context(
let document_tool_context = DocumentToolContext::new(
document_service,
(*entity_access_service).clone(),
- lexical_client,
+ lexical_client.clone(),
sync_client.as_ref().clone(),
);
@@ -236,7 +236,10 @@ pub async fn build_tool_service_context(
call_tool_context,
notification_tool_context,
chat_tool_context,
- channel_tool_context: ai_tools::build_channel_tool_context(pool.clone()),
+ channel_tool_context: ai_tools::build_channel_tool_context(
+ pool.clone(),
+ Arc::new(lexical_client),
+ ),
team_tool_context: ai_tools::build_team_tool_context(pool.clone()),
crm_tool_context: ai_tools::build_crm_tool_context(pool.clone()),
schedule_tool_context: ai_tools::NoOpScheduleContext,
diff --git a/packages/lexical-core/tests/channel-mentions.test.ts b/packages/lexical-core/tests/channel-mentions.test.ts
new file mode 100644
index 0000000000..dd288534ec
--- /dev/null
+++ b/packages/lexical-core/tests/channel-mentions.test.ts
@@ -0,0 +1,73 @@
+import { describe, expect, it } from 'vitest';
+import { extractChannelMentionsFromMarkdown } from '../utils/markdown-mentions';
+
+function documentMention(id: string, blockName: string): string {
+ return `<m-document-mention>{"documentId":"${id}","blockName":"${blockName}","documentName":"Doc","blockParams":{}}</m-document-mention>`;
+}
+
+describe('extractChannelMentionsFromMarkdown', () => {
+ it('returns no mentions for plain text', () => {
+ expect(extractChannelMentionsFromMarkdown('just some text')).toEqual([]);
+ });
+
+ it('extracts a document mention', () => {
+ const markdown = `see ${documentMention('doc-1', 'md')}`;
+ expect(extractChannelMentionsFromMarkdown(markdown)).toEqual([
+ { entityType: 'document', entityId: 'doc-1' },
+ ]);
+ });
+
+ it('maps block names to entity types', () => {
+ const cases: [string, string][] = [
+ ['md', 'document'],
+ ['pdf', 'document'],
+ ['channel', 'channel'],
+ ['project', 'project'],
+ ['chat', 'chat'],
+ ['email', 'thread'],
+ ['call', 'call'],
+ ['automation', 'automation'],
+ ['company', 'crm_company'],
+ ['contact', 'crm_contact'],
+ ];
+ for (const [blockName, entityType] of cases) {
+ expect(
+ extractChannelMentionsFromMarkdown(documentMention('id-1', blockName)),
+ `block name ${blockName}`
+ ).toEqual([{ entityType, entityId: 'id-1' }]);
+ }
+ });
+
+ it('extracts user mentions and re-tags bot principals', () => {
+ const markdown =
+ '<m-user-mention>{"userId":"macro|[email protected]","email":"[email protected]"}</m-user-mention> and ' +
+ '<m-user-mention>{"userId":"bot|00000000-0000-0000-0000-00000000a1a1","email":"Macro"}</m-user-mention>';
+ expect(extractChannelMentionsFromMarkdown(markdown)).toEqual([
+ { entityType: 'user', entityId: 'macro|[email protected]' },
+ {
+ entityType: 'bot',
+ entityId: 'bot|00000000-0000-0000-0000-00000000a1a1',
+ },
+ ]);
+ });
+
+ it('deduplicates repeated mentions', () => {
+ const markdown = `${documentMention('doc-1', 'md')} twice ${documentMention('doc-1', 'md')}`;
+ expect(extractChannelMentionsFromMarkdown(markdown)).toHaveLength(1);
+ });
+
+ it('extracts mentions from content containing bare angle brackets', () => {
+ const markdown = `1 < 2 and ${documentMention('doc-1', 'md')}`;
+ expect(extractChannelMentionsFromMarkdown(markdown)).toEqual([
+ { entityType: 'document', entityId: 'doc-1' },
+ ]);
+ });
+
+ it('extracts mentions across multiple lines and formatting', () => {
+ const markdown = `# heading\n\n- a list item with ${documentMention('doc-9', 'email')}\n\nand a user <m-user-mention>{"userId":"macro|[email protected]","email":"[email protected]"}</m-user-mention>`;
+ expect(extractChannelMentionsFromMarkdown(markdown)).toEqual([
+ { entityType: 'thread', entityId: 'doc-9' },
+ { entityType: 'user', entityId: 'macro|[email protected]' },
+ ]);
+ });
+});
diff --git a/packages/lexical-core/utils/markdown-mentions.ts b/packages/lexical-core/utils/markdown-mentions.ts
new file mode 100644
index 0000000000..d5f7854517
--- /dev/null
+++ b/packages/lexical-core/utils/markdown-mentions.ts
@@ -0,0 +1,27 @@
+import { createHeadlessEditor } from '@lexical/headless';
+import { $convertFromMarkdownString } from '@lexical/markdown';
+import { NodeReplacements, SupportedNodeTypes } from '../node-list';
+import { ALL_TRANSFORMERS } from '../transformers';
+import { $extractChannelMentions, type ChannelMention } from './mentions';
+
+/**
+ * Parses a macro markdown string headlessly and extracts its mentions as
+ * `ChannelMention`s — the server-side equivalent of the mention tracking the
+ * web editor does while composing a channel message.
+ */
+export function extractChannelMentionsFromMarkdown(
+ markdown: string
+): ChannelMention[] {
+ const editor = createHeadlessEditor({
+ nodes: [...SupportedNodeTypes, ...NodeReplacements],
+ });
+
+ editor.update(
+ () => {
+ $convertFromMarkdownString(markdown, ALL_TRANSFORMERS);
+ },
+ { discrete: true }
+ );
+
+ return editor.getEditorState().read(() => $extractChannelMentions());
+}
diff --git a/packages/lexical-core/utils/mentions.ts b/packages/lexical-core/utils/mentions.ts
index b0cd5f6f2d..4d57ec78ab 100644
--- a/packages/lexical-core/utils/mentions.ts
+++ b/packages/lexical-core/utils/mentions.ts
@@ -69,6 +69,76 @@ export function $extractAllMentions(): MentionNode[] {
return out;
}
+/** An entity mention in the shape channel messages send to the backend. */
+export type ChannelMention = {
+ entityType: string;
+ entityId: string;
+};
+
+/**
+ * Entity type for a document-mention block name, mirroring
+ * `$mentionItemFromNode` in the web editor's mentions plugin. Plain file
+ * block names (md, pdf, csv, ...) and unknown block names are all
+ * `document`.
+ */
+function documentMentionEntityType(blockName: string): string {
+ switch (blockName) {
+ case 'channel':
+ return 'channel';
+ case 'project':
+ return 'project';
+ case 'chat':
+ return 'chat';
+ case 'email':
+ return 'thread';
+ case 'call':
+ return 'call';
+ case 'automation':
+ return 'automation';
+ case 'company':
+ return 'crm_company';
+ case 'contact':
+ return 'crm_contact';
+ default:
+ return 'document';
+ }
+}
+
+/**
+ * Extracts the mentions in the current editor state as `ChannelMention`s,
+ * the way the web editor tracks them while composing a channel message.
+ * Document mentions map by block name; user mentions are re-tagged `bot`
+ * when they target a bot principal. Contact, date, group, and PR mentions
+ * carry no referencable entity and are skipped. Duplicates are dropped.
+ */
+export function $extractChannelMentions(): ChannelMention[] {
+ const out: ChannelMention[] = [];
+ const seen = new Set<string>();
+ const push = (mention: ChannelMention) => {
+ if (!mention.entityId) return;
+ const key = `${mention.entityType}:${mention.entityId}`;
+ if (seen.has(key)) return;
+ seen.add(key);
+ out.push(mention);
+ };
+
+ for (const node of $extractAllMentions()) {
+ if ($isDocumentMentionNode(node)) {
+ push({
+ entityType: documentMentionEntityType(node.getBlockName()),
+ entityId: node.getDocumentId(),
+ });
+ } else if ($isUserMentionNode(node)) {
+ const userId = node.getUserId();
+ push({
+ entityType: userId.startsWith('bot|') ? 'bot' : 'user',
+ entityId: userId,
+ });
+ }
+ }
+ return out;
+}
+
export type MentionInfo =
| (UserMentionInfo & { type: 'user' })
| (DocumentMentionInfo & { type: 'document' })
diff --git a/services/document_cognition_service/src/api/context/test.rs b/services/document_cognition_service/src/api/context/test.rs
index 753cd6ade6..9d16bc0264 100644
--- a/services/document_cognition_service/src/api/context/test.rs
+++ b/services/document_cognition_service/src/api/context/test.rs
@@ -278,7 +278,7 @@ pub async fn test_api_context(pool: sqlx::Pool<sqlx::Postgres>) -> std::sync::Ar
let document_tool_context = documents::inbound::toolset::DocumentToolContext::new(
document_service,
(*entity_access_service).clone(),
- test_lexical_client,
+ test_lexical_client.clone(),
sync_service_client.as_ref().clone(),
test_editing_client,
"test-jwt-secret".to_string(),
@@ -351,7 +351,10 @@ pub async fn test_api_context(pool: sqlx::Pool<sqlx::Postgres>) -> std::sync::Ar
call_tool_context: call_tool_context.clone(),
notification_tool_context: notification_tool_context.clone(),
chat_tool_context,
- channel_tool_context: ai_tools::build_channel_tool_context(pool.clone()),
+ channel_tool_context: ai_tools::build_channel_tool_context(
+ pool.clone(),
+ std::sync::Arc::new(test_lexical_client),
+ ),
team_tool_context: ai_tools::build_team_tool_context(pool.clone()),
crm_tool_context: ai_tools::build_crm_tool_context(pool.clone()),
schedule_tool_context: ai_tools::no_op_schedule_context(),
diff --git a/services/document_cognition_service/src/main.rs b/services/document_cognition_service/src/main.rs
index ec213d56fc..7c1a2e0f0d 100644
--- a/services/document_cognition_service/src/main.rs
+++ b/services/document_cognition_service/src/main.rs
@@ -415,7 +415,10 @@ async fn main() -> anyhow::Result<()> {
call_tool_context: call_tool_context.clone(),
notification_tool_context: notification_tool_context.clone(),
chat_tool_context,
- channel_tool_context: ai_tools::build_channel_tool_context(db.clone()),
+ channel_tool_context: ai_tools::build_channel_tool_context(
+ db.clone(),
+ lexical_client.clone(),
+ ),
team_tool_context: ai_tools::build_team_tool_context(db.clone()),
crm_tool_context: ai_tools::build_crm_tool_context(db.clone()),
schedule_tool_context: ai_tools::NoOpScheduleContext,
diff --git a/services/document_storage_service/Cargo.toml b/services/document_storage_service/Cargo.toml
index 664d8dfae1..0e2cab1746 100644
--- a/services/document_storage_service/Cargo.toml
+++ b/services/document_storage_service/Cargo.toml
@@ -87,6 +87,7 @@ jsonwebtoken = { workspace = true }
last_online_tracker = { path = "../../crates/last_online_tracker" }
lazy_static = { workspace = true }
lexical_client = { path = "../../crates/lexical_client" }
+lexical_mention_extractor = { path = "../../crates/lexical_mention_extractor" }
macro_auth = { path = "../../crates/macro_auth" }
macro_aws_config = { path = "../../crates/macro_aws_config", features = ["s3"] }
macro_axum_utils = { path = "../../crates/macro_axum_utils" }
diff --git a/services/document_storage_service/src/api/context.rs b/services/document_storage_service/src/api/context.rs
index 4ddfc700df..2c821b92d0 100644
--- a/services/document_storage_service/src/api/context.rs
+++ b/services/document_storage_service/src/api/context.rs
@@ -270,6 +270,7 @@ pub(crate) type DssChannelService = ChannelServiceImpl<
>,
>,
PgChannelReferenceSharePermissions<EntityAccessService>,
+ lexical_mention_extractor::LexicalMentionExtractor,
>;
/// Type alias for the channels router state.
diff --git a/services/document_storage_service/src/main.rs b/services/document_storage_service/src/main.rs
index 79c4e7fac4..70832ede93 100644
--- a/services/document_storage_service/src/main.rs
+++ b/services/document_storage_service/src/main.rs
@@ -717,11 +717,16 @@ async fn main() -> anyhow::Result<()> {
.with_bot_trigger_sender(bot_trigger_sender)
.with_macro_event_broker(macro_event_broker.clone());
- let channels_service = Arc::new(ChannelServiceImpl::with_dependencies(
- channels_repo,
- SpawnedChannelEventDispatcher::new(channel_side_effects.clone()),
- PgChannelReferenceSharePermissions::new(db.clone(), entity_access_service.clone()),
- ));
+ let channels_service = Arc::new(
+ ChannelServiceImpl::with_dependencies(
+ channels_repo,
+ SpawnedChannelEventDispatcher::new(channel_side_effects.clone()),
+ PgChannelReferenceSharePermissions::new(db.clone(), entity_access_service.clone()),
+ )
+ .with_mention_extractor(lexical_mention_extractor::LexicalMentionExtractor::new(
+ lexical_client.clone(),
+ )),
+ );
// Wire Macro AI to react to mentions. The router posts replies through the
// channel service we just built and runs the agent loop in-process with the
@@ -736,6 +741,7 @@ async fn main() -> anyhow::Result<()> {
ai_tools::build_channel_tool_context_with_dispatcher(
db.clone(),
std::sync::Arc::new(SpawnedChannelEventDispatcher::new(channel_side_effects)),
+ lexical_client.clone(),
);
let macro_agent_tools = ai_tools::all_tools();
let bot_trigger_router = channel_bots::inbound::BotTriggerRouter::new(
diff --git a/services/lexical-service/src/endpoints/mentions.ts b/services/lexical-service/src/endpoints/mentions.ts
new file mode 100644
index 0000000000..13d94543e5
--- /dev/null
+++ b/services/lexical-service/src/endpoints/mentions.ts
@@ -0,0 +1,57 @@
+import { OpenAPIRoute } from 'chanfana';
+import type { Context } from 'hono';
+import { z } from 'zod';
+import { handleEndpointError } from '../lib/error-handler';
+import { standardErrorResponses } from '../lib/schemas';
+import { extractChannelMentionsFromMarkdown } from '@macro-inc/lexical-core/utils/markdown-mentions';
+
+const mentionsRequest = z.object({
+ markdown: z.string(),
+});
+
+const mentionsResponse = z.object({
+ mentions: z.array(
+ z.object({
+ entityType: z.string(),
+ entityId: z.string(),
+ })
+ ),
+});
+
+export class MentionsEndpoint extends OpenAPIRoute {
+ schema = {
+ summary: 'Extract mentions from markdown',
+ description:
+ 'Parses a macro markdown string as Lexical editor state and returns the entity mentions it contains, in the shape channel messages track them.',
+ request: {
+ body: {
+ content: {
+ 'application/json': {
+ schema: mentionsRequest,
+ },
+ },
+ },
+ },
+ responses: {
+ 200: {
+ description: 'Successfully extracted mentions',
+ content: {
+ 'application/json': {
+ schema: mentionsResponse,
+ },
+ },
+ },
+ ...standardErrorResponses,
+ },
+ };
+
+ async handle(c: Context) {
+ try {
+ const { body } = await this.getValidatedData<typeof this.schema>();
+ const mentions = extractChannelMentionsFromMarkdown(body.markdown);
+ return c.json({ mentions });
+ } catch (error) {
+ return handleEndpointError(error, c);
+ }
+ }
+}
diff --git a/services/lexical-service/src/index.ts b/services/lexical-service/src/index.ts
index f899b93728..ec11ac5a73 100644
--- a/services/lexical-service/src/index.ts
+++ b/services/lexical-service/src/index.ts
@@ -7,6 +7,7 @@ import { CognitionTextEndpoint } from './endpoints/cognition-text';
import { CognitionV2Endpoint } from './endpoints/cognition-v2';
import { MarkdownEndpoint } from './endpoints/markdown';
import { MarkdownSnapshotEndpoint } from './endpoints/markdown-snapshot';
+import { MentionsEndpoint } from './endpoints/mentions';
import { XmlEndpoint } from './endpoints/xml';
import { PlaintextEndpoint } from './endpoints/plaintext';
import { SearchTextEndpoint } from './endpoints/search-text';
@@ -77,6 +78,14 @@ app.use('/snapshot/*', async (c, next) => {
await next();
});
+app.use('/mentions', async (c, next) => {
+ const authKey = c.req.header('x-internal-auth-key');
+ if (!authKey || authKey !== c.env.INTERNAL_AUTH_KEY) {
+ return c.json({ error: 'Unauthorized' }, 401);
+ }
+ await next();
+});
+
app.use('/internal/health', async (c, next) => {
const authKey = c.req.header('x-internal-auth-key');
if (!authKey || authKey !== c.env.INTERNAL_AUTH_KEY) {
@@ -105,6 +114,7 @@ openapi.get('/search/:docId', SearchTextEndpoint);
openapi.get('/markdown/:docId', MarkdownEndpoint);
openapi.get('/xml/:docId', XmlEndpoint);
openapi.post('/snapshot/markdown', MarkdownSnapshotEndpoint);
+openapi.post('/mentions', MentionsEndpoint);
openapi.get('/internal/health', (c) => c.json({ status: 'healthy' }));
export default app;
diff --git a/services/mcp_service/src/context.rs b/services/mcp_service/src/context.rs
index 7210e2511f..bfbae8c02d 100644
--- a/services/mcp_service/src/context.rs
+++ b/services/mcp_service/src/context.rs
@@ -327,7 +327,10 @@ async fn build_tool_context(
call_tool_context,
notification_tool_context,
chat_tool_context,
- channel_tool_context: ai_tools::build_channel_tool_context(db.clone()),
+ channel_tool_context: ai_tools::build_channel_tool_context(
+ db.clone(),
+ lexical_client.clone(),
+ ),
team_tool_context: ai_tools::build_team_tool_context(db.clone()),
crm_tool_context: ai_tools::build_crm_tool_context(db.clone()),
schedule_tool_context: NoOpScheduleContext,