Skip to content

πŸ™Œ OpenHands: AI-Driven Development

Fix Slack integration mypy type errors (#13177)

Rohit Malhotra46887413249a
diff --git a/enterprise/integrations/slack/slack_manager.py b/enterprise/integrations/slack/slack_manager.py
index 34ee33b535..27a892e8c4 100644
--- a/enterprise/integrations/slack/slack_manager.py
+++ b/enterprise/integrations/slack/slack_manager.py
@@ -4,7 +4,11 @@ from typing import Any
import jwt
from integrations.manager import Manager
from integrations.models import Message, SourceType
-from integrations.slack.slack_types import SlackViewInterface, StartingConvoException
+from integrations.slack.slack_types import (
+ SlackMessageView,
+ SlackViewInterface,
+ StartingConvoException,
+)
from integrations.slack.slack_view import (
SlackFactory,
SlackNewConversationFromRepoFormView,
@@ -214,7 +218,7 @@ class SlackManager(Manager[SlackViewInterface]):
async def send_message(
self,
message: str | dict[str, Any],
- slack_view: SlackViewInterface,
+ slack_view: SlackMessageView,
ephemeral: bool = False,
):
"""Send a message to Slack.
@@ -223,6 +227,8 @@ class SlackManager(Manager[SlackViewInterface]):
message: The message content. Can be a string (for simple text) or
a dict with 'text' and 'blocks' keys (for structured messages).
slack_view: The Slack view object containing channel/thread info.
+ Can be either SlackMessageView (for unauthenticated users)
+ or SlackViewInterface (for authenticated users).
ephemeral: If True, send as an ephemeral message visible only to the user.
"""
client = AsyncWebClient(token=slack_view.bot_access_token)
@@ -305,7 +311,7 @@ class SlackManager(Manager[SlackViewInterface]):
try:
msg_info = None
- user_info: SlackUser = slack_view.slack_to_openhands_user
+ user_info = slack_view.slack_to_openhands_user
try:
logger.info(
f'[Slack] Starting job for user {user_info.slack_display_name} (id={user_info.slack_user_id})',
diff --git a/enterprise/integrations/slack/slack_types.py b/enterprise/integrations/slack/slack_types.py
index 17c375a24c..efa355da4f 100644
--- a/enterprise/integrations/slack/slack_types.py
+++ b/enterprise/integrations/slack/slack_types.py
@@ -7,15 +7,31 @@ from storage.slack_user import SlackUser
from openhands.server.user_auth.user_auth import UserAuth
-class SlackViewInterface(SummaryExtractionTracker, ABC):
+class SlackMessageView(ABC):
+ """Minimal interface for sending messages to Slack.
+
+ This base class contains only the fields needed to send messages,
+ without requiring user authentication. Used by both authenticated
+ and unauthenticated Slack views.
+ """
+
bot_access_token: str
- user_msg: str | None
slack_user_id: str
- slack_to_openhands_user: SlackUser | None
- saas_user_auth: UserAuth | None
channel_id: str
message_ts: str
thread_ts: str | None
+
+
+class SlackViewInterface(SlackMessageView, SummaryExtractionTracker, ABC):
+ """Interface for authenticated Slack views that can create conversations.
+
+ All fields are required (non-None) because this interface is only used
+ for users who have linked their Slack account to OpenHands.
+ """
+
+ user_msg: str
+ slack_to_openhands_user: SlackUser
+ saas_user_auth: UserAuth
selected_repo: str | None
should_extract: bool
send_summary_instruction: bool
diff --git a/enterprise/integrations/slack/slack_v1_callback_processor.py b/enterprise/integrations/slack/slack_v1_callback_processor.py
index 05562aa1a8..e5724b1df5 100644
--- a/enterprise/integrations/slack/slack_v1_callback_processor.py
+++ b/enterprise/integrations/slack/slack_v1_callback_processor.py
@@ -89,10 +89,11 @@ class SlackV1CallbackProcessor(EventCallbackProcessor):
# -------------------------------------------------------------------------
async def _get_bot_access_token(self) -> str | None:
+ team_id = self.slack_view_data.get('team_id')
+ if team_id is None:
+ return None
slack_team_store = SlackTeamStore.get_instance()
- bot_access_token = await slack_team_store.get_team_bot_token(
- self.slack_view_data['team_id']
- )
+ bot_access_token = await slack_team_store.get_team_bot_token(team_id)
return bot_access_token
diff --git a/enterprise/integrations/slack/slack_view.py b/enterprise/integrations/slack/slack_view.py
index 6c94dfdae5..cd14137ebf 100644
--- a/enterprise/integrations/slack/slack_view.py
+++ b/enterprise/integrations/slack/slack_view.py
@@ -4,7 +4,11 @@ from uuid import UUID, uuid4
from integrations.models import Message
from integrations.resolver_context import ResolverUserContext
-from integrations.slack.slack_types import SlackViewInterface, StartingConvoException
+from integrations.slack.slack_types import (
+ SlackMessageView,
+ SlackViewInterface,
+ StartingConvoException,
+)
from integrations.slack.slack_v1_callback_processor import SlackV1CallbackProcessor
from integrations.utils import (
CONVERSATION_URL,
@@ -60,36 +64,25 @@ async def is_v1_enabled_for_slack_resolver(user_id: str) -> bool:
@dataclass
-class SlackUnkownUserView(SlackViewInterface):
+class SlackUnkownUserView(SlackMessageView):
+ """View for unauthenticated Slack users who haven't linked their account.
+
+ This view only contains the minimal fields needed to send a login link
+ message back to the user. It does not implement SlackViewInterface
+ because it cannot create conversations without user authentication.
+ """
+
bot_access_token: str
- user_msg: str | None
slack_user_id: str
- slack_to_openhands_user: SlackUser | None
- saas_user_auth: UserAuth | None
channel_id: str
message_ts: str
thread_ts: str | None
- selected_repo: str | None
- should_extract: bool
- send_summary_instruction: bool
- conversation_id: str
- team_id: str
- v1_enabled: bool
-
- async def _get_instructions(self, jinja_env: Environment) -> tuple[str, str]:
- raise NotImplementedError
-
- async def create_or_update_conversation(self, jinja_env: Environment):
- raise NotImplementedError
-
- def get_response_msg(self) -> str:
- raise NotImplementedError
@dataclass
class SlackNewConversationView(SlackViewInterface):
bot_access_token: str
- user_msg: str | None
+ user_msg: str
slack_user_id: str
slack_to_openhands_user: SlackUser
saas_user_auth: UserAuth
@@ -394,6 +387,9 @@ class SlackUpdateExistingConversationView(SlackNewConversationView):
self.conversation_id, conversation_init_data, user_id
)
+ if agent_loop_info.event_store is None:
+ raise StartingConvoException('Event store not available')
+
final_agent_observation = get_final_agent_observation(
agent_loop_info.event_store
)
@@ -550,6 +546,8 @@ class SlackFactory:
return None
# thread_ts in slack payloads in the parent's (root level msg's) message ID
+ if channel_id is None:
+ return None
return await slack_conversation_store.get_slack_conversation(
channel_id, thread_ts
)
@@ -578,24 +576,22 @@ class SlackFactory:
raise Exception('Did not find slack team')
# Determine if this is a known slack user by openhands
- if not slack_user or not saas_user_auth or not channel_id:
+ if not slack_user or not saas_user_auth or not channel_id or not message_ts:
return SlackUnkownUserView(
bot_access_token=bot_access_token,
- user_msg=user_msg,
slack_user_id=slack_user_id,
- slack_to_openhands_user=slack_user,
- saas_user_auth=saas_user_auth,
- channel_id=channel_id,
- message_ts=message_ts,
+ channel_id=channel_id or '',
+ message_ts=message_ts or '',
thread_ts=thread_ts,
- selected_repo=None,
- should_extract=False,
- send_summary_instruction=False,
- conversation_id='',
- team_id=team_id,
- v1_enabled=False,
)
+ # At this point, we've verified slack_user, saas_user_auth, channel_id, and message_ts are set
+ # user_msg should always be present in Slack payloads
+ if not user_msg:
+ raise ValueError('user_msg is required but was not provided in payload')
+ assert channel_id is not None
+ assert message_ts is not None
+
conversation = await asyncio.wait_for(
SlackFactory.determine_if_updating_existing_conversation(message),
timeout=GENERAL_TIMEOUT,
diff --git a/enterprise/server/routes/integration/slack.py b/enterprise/server/routes/integration/slack.py
index c39d8ac838..8ada1a9679 100644
--- a/enterprise/server/routes/integration/slack.py
+++ b/enterprise/server/routes/integration/slack.py
@@ -171,7 +171,7 @@ async def keycloak_callback(
state, config.jwt_secret.get_secret_value(), algorithms=['HS256']
)
slack_user_id = payload['slack_user_id']
- bot_access_token = payload['bot_access_token']
+ bot_access_token: str | None = payload['bot_access_token']
team_id = payload['team_id']
# Retrieve the keycloak_user_id