Skip to content

πŸ™Œ OpenHands: AI-Driven Development

Make SlackTeamStore fully async (#13160)

Tim O'Farrell501bf643122e
diff --git a/enterprise/integrations/slack/slack_manager.py b/enterprise/integrations/slack/slack_manager.py
index 9325637178..16a60d9e3a 100644
--- a/enterprise/integrations/slack/slack_manager.py
+++ b/enterprise/integrations/slack/slack_manager.py
@@ -181,7 +181,7 @@ class SlackManager(Manager):
)
try:
- slack_view = SlackFactory.create_slack_view_from_payload(
+ slack_view = await SlackFactory.create_slack_view_from_payload(
message, slack_user, saas_user_auth
)
except Exception as e:
diff --git a/enterprise/integrations/slack/slack_v1_callback_processor.py b/enterprise/integrations/slack/slack_v1_callback_processor.py
index a20ef5fd52..05562aa1a8 100644
--- a/enterprise/integrations/slack/slack_v1_callback_processor.py
+++ b/enterprise/integrations/slack/slack_v1_callback_processor.py
@@ -88,9 +88,9 @@ class SlackV1CallbackProcessor(EventCallbackProcessor):
# Slack helpers
# -------------------------------------------------------------------------
- def _get_bot_access_token(self):
+ async def _get_bot_access_token(self) -> str | None:
slack_team_store = SlackTeamStore.get_instance()
- bot_access_token = slack_team_store.get_team_bot_token(
+ bot_access_token = await slack_team_store.get_team_bot_token(
self.slack_view_data['team_id']
)
@@ -98,7 +98,7 @@ class SlackV1CallbackProcessor(EventCallbackProcessor):
async def _post_summary_to_slack(self, summary: str) -> None:
"""Post a summary message to the configured Slack channel."""
- bot_access_token = self._get_bot_access_token()
+ bot_access_token = await self._get_bot_access_token()
if not bot_access_token:
raise RuntimeError('Missing Slack bot access token')
diff --git a/enterprise/integrations/slack/slack_view.py b/enterprise/integrations/slack/slack_view.py
index 85c2465b79..6c94dfdae5 100644
--- a/enterprise/integrations/slack/slack_view.py
+++ b/enterprise/integrations/slack/slack_view.py
@@ -1,3 +1,4 @@
+import asyncio
from dataclasses import dataclass
from uuid import UUID, uuid4
@@ -42,7 +43,7 @@ from openhands.server.user_auth.user_auth import UserAuth
from openhands.storage.data_models.conversation_metadata import (
ConversationTrigger,
)
-from openhands.utils.async_utils import GENERAL_TIMEOUT, call_async_from_sync
+from openhands.utils.async_utils import GENERAL_TIMEOUT
# =================================================
# SECTION: Slack view types
@@ -553,7 +554,8 @@ class SlackFactory:
channel_id, thread_ts
)
- def create_slack_view_from_payload(
+ @staticmethod
+ async def create_slack_view_from_payload(
message: Message, slack_user: SlackUser | None, saas_user_auth: UserAuth | None
):
payload = message.message
@@ -564,7 +566,7 @@ class SlackFactory:
team_id = payload['team_id']
user_msg = payload.get('user_msg')
- bot_access_token = slack_team_store.get_team_bot_token(team_id)
+ bot_access_token = await slack_team_store.get_team_bot_token(team_id)
if not bot_access_token:
logger.error(
'Did not find slack team',
@@ -594,10 +596,9 @@ class SlackFactory:
v1_enabled=False,
)
- conversation: SlackConversation | None = call_async_from_sync(
- SlackFactory.determine_if_updating_existing_conversation,
- GENERAL_TIMEOUT,
- message,
+ conversation = await asyncio.wait_for(
+ SlackFactory.determine_if_updating_existing_conversation(message),
+ timeout=GENERAL_TIMEOUT,
)
if conversation:
logger.info(
diff --git a/enterprise/server/conversation_callback_processor/slack_callback_processor.py b/enterprise/server/conversation_callback_processor/slack_callback_processor.py
index d29a39e9a6..ce7f06245c 100644
--- a/enterprise/server/conversation_callback_processor/slack_callback_processor.py
+++ b/enterprise/server/conversation_callback_processor/slack_callback_processor.py
@@ -62,7 +62,7 @@ class SlackCallbackProcessor(ConversationCallbackProcessor):
slack_user, saas_user_auth = await slack_manager.authenticate_user(
self.slack_user_id
)
- slack_view = SlackFactory.create_slack_view_from_payload(
+ slack_view = await SlackFactory.create_slack_view_from_payload(
message_obj, slack_user, saas_user_auth
)
# Send the message directly as a string
diff --git a/enterprise/server/routes/integration/slack.py b/enterprise/server/routes/integration/slack.py
index e0d7f53f46..c39d8ac838 100644
--- a/enterprise/server/routes/integration/slack.py
+++ b/enterprise/server/routes/integration/slack.py
@@ -219,9 +219,9 @@ async def keycloak_callback(
# Retrieve bot token
if team_id and bot_access_token:
- slack_team_store.create_team(team_id, bot_access_token)
+ await slack_team_store.create_team(team_id, bot_access_token)
else:
- bot_access_token = slack_team_store.get_team_bot_token(team_id)
+ bot_access_token = await slack_team_store.get_team_bot_token(team_id)
if not bot_access_token:
logger.error(
diff --git a/enterprise/storage/slack_team_store.py b/enterprise/storage/slack_team_store.py
index 95924e769a..42ce75c4d4 100644
--- a/enterprise/storage/slack_team_store.py
+++ b/enterprise/storage/slack_team_store.py
@@ -1,23 +1,26 @@
+from __future__ import annotations
+
from dataclasses import dataclass
-from sqlalchemy.orm import sessionmaker
-from storage.database import session_maker
+from sqlalchemy import delete, select
+from storage.database import a_session_maker
from storage.slack_team import SlackTeam
@dataclass
class SlackTeamStore:
- session_maker: sessionmaker
-
- def get_team_bot_token(self, team_id: str) -> str | None:
+ async def get_team_bot_token(self, team_id: str) -> str | None:
"""
Get a team's bot access token by team_id
"""
- with session_maker() as session:
- team = session.query(SlackTeam).filter(SlackTeam.team_id == team_id).first()
+ async with a_session_maker() as session:
+ result = await session.execute(
+ select(SlackTeam).where(SlackTeam.team_id == team_id)
+ )
+ team = result.scalar_one_or_none()
return team.bot_access_token if team else None
- def create_team(
+ async def create_team(
self,
team_id: str,
bot_access_token: str,
@@ -26,14 +29,12 @@ class SlackTeamStore:
Create a new SlackTeam
"""
slack_team = SlackTeam(team_id=team_id, bot_access_token=bot_access_token)
- with session_maker() as session:
- session.query(SlackTeam).filter(SlackTeam.team_id == team_id).delete()
-
- # Store the token
+ async with a_session_maker() as session:
+ await session.execute(delete(SlackTeam).where(SlackTeam.team_id == team_id))
session.add(slack_team)
- session.commit()
+ await session.commit()
return slack_team
@classmethod
- def get_instance(cls):
- return SlackTeamStore(session_maker)
+ def get_instance(cls) -> SlackTeamStore:
+ return SlackTeamStore()
diff --git a/enterprise/tests/unit/integrations/slack/test_slack_v1_callback_processor.py b/enterprise/tests/unit/integrations/slack/test_slack_v1_callback_processor.py
index 6aa03c408d..e72e89233e 100644
--- a/enterprise/tests/unit/integrations/slack/test_slack_v1_callback_processor.py
+++ b/enterprise/tests/unit/integrations/slack/test_slack_v1_callback_processor.py
@@ -145,9 +145,9 @@ class TestSlackV1CallbackProcessor:
"""Test that processor handles double callback correctly and processes both times."""
conversation_id = uuid4()
- # Mock SlackTeamStore
+ # Mock SlackTeamStore (async method)
mock_store = MagicMock()
- mock_store.get_team_bot_token.return_value = 'xoxb-test-token'
+ mock_store.get_team_bot_token = AsyncMock(return_value='xoxb-test-token')
mock_slack_team_store.return_value = mock_store
# Mock successful summary generation
@@ -208,9 +208,9 @@ class TestSlackV1CallbackProcessor:
"""Test successful end-to-end callback execution."""
conversation_id = uuid4()
- # Mock SlackTeamStore
+ # Mock SlackTeamStore (async method)
mock_store = MagicMock()
- mock_store.get_team_bot_token.return_value = 'xoxb-test-token'
+ mock_store.get_team_bot_token = AsyncMock(return_value='xoxb-test-token')
mock_slack_team_store.return_value = mock_store
# Mock summary instruction
@@ -287,9 +287,9 @@ class TestSlackV1CallbackProcessor:
expected_error,
):
"""Test error handling when bot access token is missing or empty."""
- # Mock SlackTeamStore to return the test token
+ # Mock SlackTeamStore to return the test token (async method)
mock_store = MagicMock()
- mock_store.get_team_bot_token.return_value = bot_token
+ mock_store.get_team_bot_token = AsyncMock(return_value=bot_token)
mock_slack_team_store.return_value = mock_store
# Mock successful summary generation
@@ -327,9 +327,9 @@ class TestSlackV1CallbackProcessor:
expected_error,
):
"""Test error handling for various Slack API errors."""
- # Mock SlackTeamStore
+ # Mock SlackTeamStore (async method)
mock_store = MagicMock()
- mock_store.get_team_bot_token.return_value = 'xoxb-test-token'
+ mock_store.get_team_bot_token = AsyncMock(return_value='xoxb-test-token')
mock_slack_team_store.return_value = mock_store
# Mock successful summary generation
@@ -392,9 +392,9 @@ class TestSlackV1CallbackProcessor:
"""Test error handling for various agent server errors."""
conversation_id = uuid4()
- # Mock SlackTeamStore
+ # Mock SlackTeamStore (async method)
mock_store = MagicMock()
- mock_store.get_team_bot_token.return_value = 'xoxb-test-token'
+ mock_store.get_team_bot_token = AsyncMock(return_value='xoxb-test-token')
mock_slack_team_store.return_value = mock_store
# Mock summary instruction
diff --git a/enterprise/tests/unit/test_slack_callback_processor.py b/enterprise/tests/unit/test_slack_callback_processor.py
index 79475d4f2a..5cfb81e0b9 100644
--- a/enterprise/tests/unit/test_slack_callback_processor.py
+++ b/enterprise/tests/unit/test_slack_callback_processor.py
@@ -240,12 +240,12 @@ class TestSlackCallbackProcessor:
return_value=(mock_slack_user, mock_saas_user_auth)
)
- # Mock the SlackFactory
+ # Mock the SlackFactory (async method)
with patch(
'server.conversation_callback_processor.slack_callback_processor.SlackFactory'
) as mock_slack_factory:
- mock_slack_factory.create_slack_view_from_payload.return_value = (
- mock_slack_view
+ mock_slack_factory.create_slack_view_from_payload = AsyncMock(
+ return_value=mock_slack_view
)
mock_slack_manager.send_message = AsyncMock()