Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.
Python agentagent-osaiai-agentsai-partnerchatopsclaudeclaude-codecodexdevtoolsdiscord-botlark-botllmlocal-firstlong-term-memoryopencodeslack-botvibe-codingvibeywechat
fix(slack): preserve raw mention text for agents
cyh01a1e8ae1b52
diff --git a/core/handlers/message_handler.py b/core/handlers/message_handler.pyindex 1a45f79d0..11a62232a 100644--- a/core/handlers/message_handler.py+++ b/core/handlers/message_handler.py@@ -336,6 +336,8 @@ class MessageHandler(BaseHandler):# Prepend user identity when include_user_info is enabledif is_human and self.config.include_user_info:message = await self._prepend_user_info(context, message)+ if is_human:+ message = self._prepend_agent_identity(context, message)message = self._append_attachment_errors(message, attachment_errors)@@ -422,6 +424,14 @@ class MessageHandler(BaseHandler):uid = self._sanitize_identity(context.user_id)return f"[{name}<{uid}>] {message}"+ def _prepend_agent_identity(self, context: MessageContext, message: str) -> str:+ """Add platform identity context without rewriting the user's message."""+ payload = context.platform_specific or {}+ bot_mention = payload.get("bot_mention")+ if isinstance(bot_mention, str) and bot_mention.strip():+ return f"[Agent Identity] Slack bot mention: {bot_mention.strip()}\n{message}"+ return message+async def handle_callback_query(self, context: MessageContext, callback_data: str):"""Route callback queries to appropriate handlers"""try:diff --git a/modules/im/slack.py b/modules/im/slack.pyindex 1649e5ef9..2203e3e34 100644--- a/modules/im/slack.py+++ b/modules/im/slack.py@@ -351,6 +351,15 @@ class SlackBot(BaseIMClient):return Truereturn self._channel_looks_like_dm(context.channel_id)+ def _is_own_bot_message(self, event: Dict[str, Any], bot_user_id: Optional[str]) -> bool:+ bot_profile = event.get("bot_profile") if isinstance(event.get("bot_profile"), dict) else {}+ if bot_user_id and event.get("user") == bot_user_id:+ return True+ if bot_user_id and bot_profile.get("user_id") == bot_user_id:+ return True+ config_app_id = getattr(self.config, "app_id", None)+ return bool(config_app_id and bot_profile.get("app_id") == config_app_id)+@staticmethoddef _slack_response_get(response: Any, key: str) -> Any:getter = getattr(response, "get", None)@@ -1358,10 +1367,6 @@ class SlackBot(BaseIMClient):if self._controller and hasattr(self._controller, "_refresh_config_from_disk"):self._controller._refresh_config_from_disk()- # Ignore bot messages- if event.get("bot_id"):- return-# Check for file attachments firstslack_files = event.get("files", [])has_files = bool(slack_files)@@ -1376,7 +1381,8 @@ class SlackBot(BaseIMClient):returnchannel_id = event.get("channel")- user_id = event.get("user")+ bot_profile = event.get("bot_profile") if isinstance(event.get("bot_profile"), dict) else {}+ user_id = event.get("user") or bot_profile.get("user_id") or event.get("bot_id")is_dm = self._channel_looks_like_dm(channel_id)if is_dm:dm_match = await self._match_bound_dm_channel(user_id, channel_id)@@ -1388,26 +1394,34 @@ class SlackBot(BaseIMClient):)return- # Check if this message contains a bot mention.- # In channels, app_mention will handle it. In DMs, Slack does not- # emit app_mention, so we strip the mention and continue.- text = (event.get("text") or "").strip()+ # Check if this message contains a bot mention. Use a normalized+ # copy for routing/commands while preserving the raw Slack text for+ # the agent.+ raw_text = (event.get("text") or "").strip()+ route_text = raw_text+ agent_text = raw_texthad_dm_mention_only = Falsebot_user_id = await self._get_bot_user_id(payload)- has_bot_mention = self._has_specific_mention(text, bot_user_id)- cleaned_text = self._strip_specific_mention(text, bot_user_id)+ bot_mention = f"<@{bot_user_id}>" if bot_user_id else None+ has_bot_mention = self._has_specific_mention(raw_text, bot_user_id)+ cleaned_text = self._strip_specific_mention(raw_text, bot_user_id)handled_bot_mention_in_message_event = False+ if event.get("bot_id"):+ if self._is_own_bot_message(event, bot_user_id):+ return+ if not has_bot_mention:+ returnif has_bot_mention:if is_dm:- text = cleaned_text- had_dm_mention_only = not text+ route_text = cleaned_text+ had_dm_mention_only = not route_textelif await self._is_slack_connect_channel(channel_id):- text = self._strip_specific_mention(text, bot_user_id, anywhere=True)+ route_text = self._strip_specific_mention(raw_text, bot_user_id, anywhere=True)handled_bot_mention_in_message_event = Truelogger.info("Processing Slack Connect message event with bot mention: '%s'", event.get("text"))else:- logger.info(f"Skipping message event with bot mention: '{text}'")+ logger.info(f"Skipping message event with bot mention: '{raw_text}'")return# Extract file attachments (slack_files already checked above)@@ -1422,7 +1436,7 @@ class SlackBot(BaseIMClient):if not user_id:logger.debug("Ignoring Slack message without user id")return- if not text and not file_attachments and not has_shared_content and not had_dm_mention_only:+ if not route_text and not file_attachments and not has_shared_content and not had_dm_mention_only:logger.debug("Ignoring Slack message with empty text and no files")return@@ -1447,7 +1461,7 @@ class SlackBot(BaseIMClient):if handled_bot_mention_in_message_event:logger.debug("Processing message event because Slack Connect bot mention was already detected")elif not is_thread_reply:- logger.debug(f"Ignoring non-mention message in channel: '{text}'")+ logger.debug(f"Ignoring non-mention message in channel: '{route_text}'")return# In thread: check if bot is active in this thread@@ -1464,18 +1478,18 @@ class SlackBot(BaseIMClient):else False)if not thread_active and not scheduled_thread_active:- logger.debug(f"Ignoring message in inactive thread {thread_ts}: '{text}'")+ logger.debug(f"Ignoring message in inactive thread {thread_ts}: '{route_text}'")returnelse:# Without settings_manager, fall back to ignoring non-mention in threads- logger.debug(f"No settings_manager, ignoring thread message: '{text}'")+ logger.debug(f"No settings_manager, ignoring thread message: '{route_text}'")returnauth = self.check_authorization(user_id=user_id,channel_id=channel_id,is_dm=is_dm,- text=text,+ text=route_text,settings_manager=self.settings_manager,)if not auth.allowed:@@ -1489,7 +1503,7 @@ class SlackBot(BaseIMClient):shared_text = await self._extract_shared_message_content(event)# If shared content extraction found nothing and we have no text/files,# there's nothing to process- if not shared_text and not text and not file_attachments:+ if not shared_text and not route_text and not file_attachments:logger.debug("Ignoring shared message with no extractable content")return@@ -1506,6 +1520,8 @@ class SlackBot(BaseIMClient):"team_id": payload.get("team_id"),"event": event,"is_dm": is_dm,+ "bot_user_id": bot_user_id,+ "bot_mention": bot_mention,},files=file_attachments,)@@ -1516,10 +1532,11 @@ class SlackBot(BaseIMClient):logger.info(f"Marked thread {thread_id} as active due to Slack Connect @mention")# Handle slash commands in regular messages (before appending shared content)- # Use only the user's original text for command detection+ # Use mention-normalized text for internal command detection;+ # keep agent_text as the original Slack message.if await self.dispatch_text_command(context,- text,+ route_text,allow_plain_bind=self.should_allow_plain_bind(user_id=user_id,is_dm=is_dm,@@ -1530,14 +1547,14 @@ class SlackBot(BaseIMClient):# Append shared content to user text (after command parsing)if shared_text:- if text:- text = f"{text}\n\n{shared_text}"+ if agent_text:+ agent_text = f"{agent_text}\n\n{shared_text}"else:- text = shared_text+ agent_text = shared_text# Handle as regular messageif self.on_message_callback:- await self.on_message_callback(context, text)+ await self.on_message_callback(context, agent_text)elif event_type == "app_mention":# Handle @mentions@@ -1546,15 +1563,18 @@ class SlackBot(BaseIMClient):if not self._mark_mention_event_processed(channel_id, event.get("ts")):return+ raw_text = (event.get("text") or "").strip()+ bot_user_id = await self._get_bot_user_id(payload)+ bot_mention = f"<@{bot_user_id}>" if bot_user_id else None# Remove the mention from the text first so we can parse commands for auth- text = self._strip_specific_mention(- event.get("text", ""),- await self._get_bot_user_id(payload),+ route_text = self._strip_specific_mention(+ raw_text,+ bot_user_id,anywhere=True,).strip()# Parse command action for proper admin-protected auth check- parsed_command = self.parse_text_command(text)+ parsed_command = self.parse_text_command(route_text)auth_action = parsed_command[0] if parsed_command else ""# Centralized auth gate (app_mention is always in a channel, not DM)@@ -1579,7 +1599,7 @@ class SlackBot(BaseIMClient):# Extract shared/forwarded message content (defer appending until after command check)shared_text = await self._extract_shared_message_content(event)- if not text and not file_attachments and not shared_text:+ if not route_text and not file_attachments and not shared_text:logger.debug("Ignoring Slack app_mention with empty text and no files")return@@ -1592,6 +1612,8 @@ class SlackBot(BaseIMClient):"team_id": payload.get("team_id"),"event": event,"is_dm": channel_id.startswith("D"),+ "bot_user_id": bot_user_id,+ "bot_mention": bot_mention,},files=file_attachments,)@@ -1602,7 +1624,7 @@ class SlackBot(BaseIMClient):self.sessions.mark_thread_active(event.get("user"), channel_id, thread_id)logger.info(f"Marked thread {thread_id} as active due to @mention")- logger.info(f"App mention processed: original='{event.get('text')}', cleaned='{text}'")+ logger.info(f"App mention processed: original='{event.get('text')}', cleaned='{route_text}'")# Check if this is a command after mention (command already parsed above for auth)if parsed_command:@@ -1617,16 +1639,17 @@ class SlackBot(BaseIMClient):logger.warning(f"Command '{command}' not found in callbacks")# Append shared content to user text (after command parsing)+ agent_text = raw_textif shared_text:- if text:- text = f"{text}\n\n{shared_text}"+ if agent_text:+ agent_text = f"{agent_text}\n\n{shared_text}"else:- text = shared_text+ agent_text = shared_text# Handle as regular message- logger.info(f"Handling as regular message: '{text}'")+ logger.info(f"Handling as regular message: '{agent_text}'")if self.on_message_callback:- await self.on_message_callback(context, text)+ await self.on_message_callback(context, agent_text)async def _handle_slash_command(self, payload: Dict[str, Any]):"""Handle native Slack slash commands"""diff --git a/tests/test_message_handler_attachments.py b/tests/test_message_handler_attachments.pyindex 6723b2b84..7ab0f09e0 100644--- a/tests/test_message_handler_attachments.py+++ b/tests/test_message_handler_attachments.py@@ -249,6 +249,21 @@ class MessageHandlerAttachmentTests(unittest.IsolatedAsyncioTestCase):self.assertIn("Download failed with HTTP 403", message)self.assertNotIn("/.vibe_remote/attachments/", message)+ def test_prepend_agent_identity_keeps_original_message_text(self):+ handler = MessageHandler(_StubController(_StubIMClient()))+ context = MessageContext(+ user_id="U1",+ channel_id="C1",+ platform_specific={"bot_mention": "<@U_BOT>"},+ )++ message = handler._prepend_agent_identity(context, "please <@U_BOT> help <@U_OTHER>")++ self.assertEqual(+ message,+ "[Agent Identity] Slack bot mention: <@U_BOT>\nplease <@U_BOT> help <@U_OTHER>",+ )+if __name__ == "__main__":unittest.main()diff --git a/tests/test_slack_dm_mentions.py b/tests/test_slack_dm_mentions.pyindex 28eafd2eb..60e944d62 100644--- a/tests/test_slack_dm_mentions.py+++ b/tests/test_slack_dm_mentions.py@@ -333,7 +333,7 @@ class SlackDmMentionTests(unittest.IsolatedAsyncioTestCase):self.assertEqual(user_info["real_name"], "Alex")self.assertEqual(user_info["name"], "cyh")- async def test_dm_mention_only_falls_through_as_empty_message(self):+ async def test_dm_mention_only_preserves_raw_text_for_agent(self):slack = SlackBot(SlackConfig(bot_token="xoxb-test"))received = {}@@ -364,11 +364,11 @@ class SlackDmMentionTests(unittest.IsolatedAsyncioTestCase):{"channel_id": "D123","thread_id": "1710000000.000100",- "text": "",+ "text": "<@U_BOT>",},)- async def test_dm_mention_with_text_strips_mention(self):+ async def test_dm_mention_with_text_preserves_raw_text_for_agent(self):slack = SlackBot(SlackConfig(bot_token="xoxb-test"))received = {}@@ -392,7 +392,7 @@ class SlackDmMentionTests(unittest.IsolatedAsyncioTestCase):await slack._handle_event(payload)- self.assertEqual(received, {"text": "hello"})+ self.assertEqual(received, {"text": "<@U_BOT> hello"})async def test_bound_user_message_from_mismatched_dm_channel_is_ignored(self):slack = SlackBot(SlackConfig(bot_token="xoxb-test"))@@ -802,6 +802,7 @@ class SlackDmMentionTests(unittest.IsolatedAsyncioTestCase):async def _on_message(context, text):received["text"] = textreceived["thread_id"] = context.thread_id+ received["bot_mention"] = (context.platform_specific or {}).get("bot_mention")slack.web_client = _WebClient()slack.register_callbacks(on_message=_on_message)@@ -821,9 +822,16 @@ class SlackDmMentionTests(unittest.IsolatedAsyncioTestCase):await slack._handle_event(payload)- self.assertEqual(received, {"text": "please help", "thread_id": "1710000000.000350"})+ self.assertEqual(+ received,+ {+ "text": "<@U_BOT> please help",+ "thread_id": "1710000000.000350",+ "bot_mention": "<@U_BOT>",+ },+ )- async def test_slack_connect_mid_text_mention_is_stripped_in_message_fallback(self):+ async def test_slack_connect_mid_text_mention_is_forwarded_raw_in_message_fallback(self):slack = SlackBot(SlackConfig(bot_token="xoxb-test", require_mention=True))received = {}@@ -852,7 +860,67 @@ class SlackDmMentionTests(unittest.IsolatedAsyncioTestCase):await slack._handle_event(payload)- self.assertEqual(received, {"text": "please help"})+ self.assertEqual(received, {"text": "please <@U_BOT> help"})++ async def test_slack_connect_other_bot_mention_is_forwarded_raw(self):+ slack = SlackBot(SlackConfig(bot_token="xoxb-test", require_mention=True))+ received = {}++ class _WebClient:+ async def conversations_info(self, channel):+ return {"channel": {"id": channel, "is_ext_shared": True}}++ async def _on_message(context, text):+ received["user_id"] = context.user_id+ received["text"] = text++ slack.web_client = _WebClient()+ slack.register_callbacks(on_message=_on_message)++ payload = {+ "event_id": "evt-slack-connect-other-bot-mention",+ "team_id": "T1",+ "authorizations": [{"user_id": "U_BOT"}],+ "event": {+ "type": "message",+ "channel": "C_CONNECT",+ "bot_id": "B_OTHER",+ "bot_profile": {"user_id": "U_OTHER_BOT"},+ "text": "handoff to <@U_BOT> please",+ "ts": "1710000000.000356",+ },+ }++ await slack._handle_event(payload)++ self.assertEqual(received, {"user_id": "U_OTHER_BOT", "text": "handoff to <@U_BOT> please"})++ async def test_own_bot_message_is_ignored_even_when_mentioned(self):+ slack = SlackBot(SlackConfig(bot_token="xoxb-test", app_id="A_SELF"))+ received = {"called": False}++ async def _on_message(_context, _text):+ received["called"] = True++ slack.register_callbacks(on_message=_on_message)++ payload = {+ "event_id": "evt-own-bot-mention",+ "team_id": "T1",+ "authorizations": [{"user_id": "U_BOT"}],+ "event": {+ "type": "message",+ "channel": "C_CONNECT",+ "bot_id": "B_SELF",+ "bot_profile": {"user_id": "U_BOT", "app_id": "A_SELF"},+ "text": "<@U_BOT> loop",+ "ts": "1710000000.000357",+ },+ }++ await slack._handle_event(payload)++ self.assertFalse(received["called"])async def test_slack_connect_channel_mention_marks_thread_active(self):slack = SlackBot(SlackConfig(bot_token="xoxb-test", require_mention=True))@@ -938,7 +1006,7 @@ class SlackDmMentionTests(unittest.IsolatedAsyncioTestCase):await slack._handle_event(message_payload)await slack._handle_event(app_mention_payload)- self.assertEqual(received, ["please help"])+ self.assertEqual(received, ["<@U_BOT> please help"])async def test_slack_connect_app_mention_then_message_is_handled_once(self):slack = SlackBot(SlackConfig(bot_token="xoxb-test", require_mention=True))@@ -982,7 +1050,7 @@ class SlackDmMentionTests(unittest.IsolatedAsyncioTestCase):await slack._handle_event(app_mention_payload)await slack._handle_event(message_payload)- self.assertEqual(received, ["please help"])+ self.assertEqual(received, ["<@U_BOT> please help"])async def test_dm_preserves_non_bot_mentions(self):slack = SlackBot(SlackConfig(bot_token="xoxb-test"))@@ -1008,7 +1076,7 @@ class SlackDmMentionTests(unittest.IsolatedAsyncioTestCase):await slack._handle_event(payload)- self.assertEqual(received, {"text": "summarize what <@U_OTHER> said"})+ self.assertEqual(received, {"text": "<@U_BOT> summarize what <@U_OTHER> said"})async def test_channel_message_with_other_mentions_is_not_skipped(self):slack = SlackBot(SlackConfig(bot_token="xoxb-test"))@@ -1086,7 +1154,7 @@ class SlackDmMentionTests(unittest.IsolatedAsyncioTestCase):await slack._handle_event(payload)- self.assertEqual(received, {"text": "summarize what <@U_OTHER> said"})+ self.assertEqual(received, {"text": "<@U_BOT> summarize what <@U_OTHER> said"})if __name__ == "__main__":