diff --git a/src/dispatch/plugins/dispatch_slack/case/messages.py b/src/dispatch/plugins/dispatch_slack/case/messages.py index 2ab74117badec..03851c8b4c0d8 100644 --- a/src/dispatch/plugins/dispatch_slack/case/messages.py +++ b/src/dispatch/plugins/dispatch_slack/case/messages.py @@ -20,12 +20,16 @@ from dispatch.config import DISPATCH_UI_URL from dispatch.messaging.strings import CASE_STATUS_DESCRIPTIONS, CASE_VISIBILITY_DESCRIPTIONS from dispatch.plugin import service as plugin_service +from dispatch.plugins.dispatch_slack import service as dispatch_slack_service from dispatch.plugins.dispatch_slack.case.enums import ( CaseNotificationActions, SignalEngagementActions, SignalNotificationActions, ) -from dispatch.plugins.dispatch_slack.config import MAX_SECTION_TEXT_LENGTH +from dispatch.plugins.dispatch_slack.config import ( + MAX_SECTION_TEXT_LENGTH, + SlackConversationConfiguration, +) from dispatch.plugins.dispatch_slack.models import ( CaseSubjects, EngagementMetadata, @@ -303,11 +307,12 @@ def create_genai_signal_analysis_message( channel_id: str, db_session: Session, client: WebClient, + config: SlackConversationConfiguration, ) -> list[Block]: """ Creates a signal analysis using a generative AI plugin. - This function generates a analysis for a given case by leveraging historical context and + This function generates an analysis for a given case by leveraging historical context and a generative AI plugin. It fetches related cases, their resolutions, and relevant Slack messages to provide a comprehensive analysis. @@ -316,6 +321,7 @@ def create_genai_signal_analysis_message( channel_id (str): The ID of the Slack channel where the analysis will be sent. db_session (Session): The database session to use for querying signal instances and related cases. client (WebClient): The Slack WebClient to fetch threaded messages. + config (SlackConversationConfiguration): The Slack conversation configuration. Returns: list[Block]: A list of Block objects representing the structure of the Slack message. @@ -355,9 +361,12 @@ def create_genai_signal_analysis_message( for related_case in related_cases: historical_context.append("") historical_context.append(f"{related_case.name}") - historical_context.append(f"{related_case.resolution}{related_case.resolution}{related_case.resolution_reason}" + ) historical_context.append( - f"{related_case.resolution_reason}" + f"{related_case.signal_instances[0].raw}" ) # we fetch Slack messages for the related case @@ -368,10 +377,12 @@ def create_genai_signal_analysis_message( channel=related_case.conversation.channel_id, ts=related_case.conversation.thread_id, ) - - # we add relevant messages to the context (e.g., first 10 messages) - for message in thread_messages["messages"][:10]: - historical_context.append(f"{message['text']}") + for message in thread_messages["messages"]: + if dispatch_slack_service.is_user(config=config, user_id=message.get("user")): + # we only include messages from users + historical_context.append( + f"{message['text']}" + ) except SlackApiError as e: log.error( f"Unable to generate GenAI signal analysis. Error fetching Slack messages for case {related_case.name}: {e}" diff --git a/src/dispatch/plugins/dispatch_slack/plugin.py b/src/dispatch/plugins/dispatch_slack/plugin.py index b8e9ce96aa46b..8682958380dc3 100644 --- a/src/dispatch/plugins/dispatch_slack/plugin.py +++ b/src/dispatch/plugins/dispatch_slack/plugin.py @@ -105,6 +105,7 @@ def create_threaded(self, case: Case, conversation_id: str, db_session: Session) channel_id=conversation_id, db_session=db_session, client=client, + config=self.configuration, ): signal_response = send_message( client=client, diff --git a/src/dispatch/plugins/dispatch_slack/service.py b/src/dispatch/plugins/dispatch_slack/service.py index 4c323e7b220ef..433023af5d845 100644 --- a/src/dispatch/plugins/dispatch_slack/service.py +++ b/src/dispatch/plugins/dispatch_slack/service.py @@ -1,27 +1,25 @@ -from datetime import datetime import functools import heapq import logging -from requests import Timeout +from datetime import datetime +from typing import Dict, List, Optional from blockkit import Message, Section +from requests import Timeout from slack_sdk.errors import SlackApiError from slack_sdk.web.client import WebClient from slack_sdk.web.slack_response import SlackResponse from tenacity import ( + RetryCallState, retry, retry_if_exception, - RetryCallState, - wait_exponential, stop_after_attempt, + wait_exponential, ) -from typing import Dict, List, Optional - from .config import SlackConversationConfiguration from .enums import SlackAPIErrorCode, SlackAPIGetEndpoints, SlackAPIPostEndpoints - Conversation = dict[str, str] log = logging.getLogger(__name__) diff --git a/src/dispatch/signal/flows.py b/src/dispatch/signal/flows.py index fdda25f909ca1..4010cb1f104c3 100644 --- a/src/dispatch/signal/flows.py +++ b/src/dispatch/signal/flows.py @@ -1,7 +1,7 @@ import logging +import time from datetime import timedelta from queue import Queue -import time from cachetools import TTLCache from email_validator import EmailNotValidError, validate_email @@ -15,6 +15,7 @@ from dispatch.database.core import get_organization_session, get_session from dispatch.entity import service as entity_service from dispatch.entity_type import service as entity_type_service +from dispatch.entity_type.models import EntityScopeEnum from dispatch.exceptions import DispatchException from dispatch.organization.service import get_all as get_all_organizations from dispatch.plugin import service as plugin_service @@ -25,7 +26,6 @@ from dispatch.signal.enums import SignalEngagementStatus from dispatch.signal.models import SignalFilterAction, SignalInstance, SignalInstanceCreate from dispatch.workflow import flows as workflow_flows -from dispatch.entity_type.models import EntityScopeEnum log = logging.getLogger(__name__)