Skip to content

Commit

Permalink
Merge branch 'master' into feat/slack-create-case-slash-command
Browse files Browse the repository at this point in the history
  • Loading branch information
bashbreakpoint authored Oct 8, 2024
2 parents 8462890 + 616f568 commit eca1e86
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 17 deletions.
27 changes: 19 additions & 8 deletions src/dispatch/plugins/dispatch_slack/case/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -355,9 +361,12 @@ def create_genai_signal_analysis_message(
for related_case in related_cases:
historical_context.append("<case>")
historical_context.append(f"<case_name>{related_case.name}</case_name>")
historical_context.append(f"<resolution>{related_case.resolution}</resolution")
historical_context.append(f"<case_resolution>{related_case.resolution}</case_resolution")
historical_context.append(
f"<case_resolution_reason>{related_case.resolution_reason}</case_resolution_reason>"
)
historical_context.append(
f"<resolution_reason>{related_case.resolution_reason}</resolution_reason>"
f"<case_alert_data>{related_case.signal_instances[0].raw}</case_alert_data>"
)

# we fetch Slack messages for the related case
Expand All @@ -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"<slack_message>{message['text']}</slack_message>")
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"<case_slack_message>{message['text']}</case_slack_message>"
)
except SlackApiError as e:
log.error(
f"Unable to generate GenAI signal analysis. Error fetching Slack messages for case {related_case.name}: {e}"
Expand Down
1 change: 1 addition & 0 deletions src/dispatch/plugins/dispatch_slack/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 5 additions & 7 deletions src/dispatch/plugins/dispatch_slack/service.py
Original file line number Diff line number Diff line change
@@ -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__)
Expand Down
4 changes: 2 additions & 2 deletions src/dispatch/signal/flows.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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__)

Expand Down

0 comments on commit eca1e86

Please sign in to comment.