Skip to content

Commit

Permalink
Slack fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Weves committed Jan 1, 2025
1 parent d64464c commit 92add65
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 99 deletions.
18 changes: 0 additions & 18 deletions backend/onyx/connectors/slack/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,24 +264,6 @@ def replace_special_mentions(message: str) -> str:
message = message.replace("<!everyone>", "@everyone")
return message

@staticmethod
def replace_links(message: str) -> str:
"""Replaces slack links e.g. `<URL>` -> `URL` and `<URL|DISPLAY>` -> `DISPLAY`"""
# Find user IDs in the message
possible_link_matches = re.findall(r"<(.*?)>", message)
for possible_link in possible_link_matches:
if not possible_link:
continue
# Special slack patterns that aren't for links
if possible_link[0] not in ["#", "@", "!"]:
link_display = (
possible_link
if "|" not in possible_link
else possible_link.split("|")[1]
)
message = message.replace(f"<{possible_link}>", link_display)
return message

@staticmethod
def replace_special_catchall(message: str) -> str:
"""Replaces pattern of <!something|another-thing> with another-thing
Expand Down
39 changes: 3 additions & 36 deletions backend/onyx/onyxbot/slack/blocks.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import re
from datetime import datetime
from re import Match

import pytz
import timeago # type: ignore
Expand Down Expand Up @@ -59,33 +57,6 @@ def get_feedback_reminder_blocks(thread_link: str, include_followup: bool) -> Bl
return SectionBlock(text=text)


def _process_citations_for_slack(text: str) -> str:
"""
Converts instances of [[x]](LINK) in the input text to Slack's link format <LINK|[x]>.
Args:
- text (str): The input string containing markdown links.
Returns:
- str: The string with markdown links converted to Slack format.
"""
# Regular expression to find all instances of [[x]](LINK)
pattern = r"\[\[(.*?)\]\]\((.*?)\)"

# Function to replace each found instance with Slack's format
def slack_link_format(match: Match) -> str:
link_text = match.group(1)
link_url = match.group(2)

# Account for empty link citations
if link_url == "":
return f"[{link_text}]"
return f"<{link_url}|[{link_text}]>"

# Substitute all matches in the input text
return re.sub(pattern, slack_link_format, text)


def _split_text(text: str, limit: int = 3000) -> list[str]:
if len(text) <= limit:
return [text]
Expand Down Expand Up @@ -369,15 +340,12 @@ def _build_citations_blocks(

def _build_qa_response_blocks(
answer: ChatOnyxBotResponse,
process_message_for_citations: bool = False,
) -> list[Block]:
retrieval_info = answer.docs
if not retrieval_info:
# This should not happen, even with no docs retrieved, there is still info returned
raise RuntimeError("Failed to retrieve docs, cannot answer question.")

formatted_answer = format_slack_message(answer.answer) if answer.answer else None

if DISABLE_GENERATIVE_AI:
return []

Expand Down Expand Up @@ -408,18 +376,18 @@ def _build_qa_response_blocks(

filter_block = SectionBlock(text=f"_{filter_text}_")

if not formatted_answer:
if not answer.answer:
answer_blocks = [
SectionBlock(
text="Sorry, I was unable to find an answer, but I did find some potentially relevant docs 🤓"
)
]
else:
# replaces markdown links with slack format links
formatted_answer = format_slack_message(answer.answer)
answer_processed = decode_escapes(
remove_slack_text_interactions(formatted_answer)
)
if process_message_for_citations:
answer_processed = _process_citations_for_slack(answer_processed)
answer_blocks = [
SectionBlock(text=text) for text in _split_text(answer_processed)
]
Expand Down Expand Up @@ -525,7 +493,6 @@ def build_slack_response_blocks(

answer_blocks = _build_qa_response_blocks(
answer=answer,
process_message_for_citations=use_citations,
)

web_follow_up_block = []
Expand Down
70 changes: 26 additions & 44 deletions backend/onyx/onyxbot/slack/icons.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,73 +4,55 @@
def source_to_github_img_link(source: DocumentSource) -> str | None:
# TODO: store these images somewhere better
if source == DocumentSource.WEB.value:
return "https://raw.githubusercontent.com/onyx-ai/onyx/main/backend/slackbot_images/Web.png"
return "https://raw.githubusercontent.com/onyx-dot-app/onyx/main/backend/slackbot_images/Web.png"
if source == DocumentSource.FILE.value:
return "https://raw.githubusercontent.com/onyx-ai/onyx/main/backend/slackbot_images/File.png"
return "https://raw.githubusercontent.com/onyx-dot-app/onyx/main/backend/slackbot_images/File.png"
if source == DocumentSource.GOOGLE_SITES.value:
return "https://raw.githubusercontent.com/onyx-ai/onyx/main/web/public/GoogleSites.png"
return "https://raw.githubusercontent.com/onyx-dot-app/onyx/main/web/public/GoogleSites.png"
if source == DocumentSource.SLACK.value:
return (
"https://raw.githubusercontent.com/onyx-ai/onyx/main/web/public/Slack.png"
)
return "https://raw.githubusercontent.com/onyx-dot-app/onyx/main/web/public/Slack.png"
if source == DocumentSource.GMAIL.value:
return (
"https://raw.githubusercontent.com/onyx-ai/onyx/main/web/public/Gmail.png"
)
return "https://raw.githubusercontent.com/onyx-dot-app/onyx/main/web/public/Gmail.png"
if source == DocumentSource.GOOGLE_DRIVE.value:
return "https://raw.githubusercontent.com/onyx-ai/onyx/main/web/public/GoogleDrive.png"
return "https://raw.githubusercontent.com/onyx-dot-app/onyx/main/web/public/GoogleDrive.png"
if source == DocumentSource.GITHUB.value:
return (
"https://raw.githubusercontent.com/onyx-ai/onyx/main/web/public/Github.png"
)
return "https://raw.githubusercontent.com/onyx-dot-app/onyx/main/web/public/Github.png"
if source == DocumentSource.GITLAB.value:
return (
"https://raw.githubusercontent.com/onyx-ai/onyx/main/web/public/Gitlab.png"
)
return "https://raw.githubusercontent.com/onyx-dot-app/onyx/main/web/public/Gitlab.png"
if source == DocumentSource.CONFLUENCE.value:
return "https://raw.githubusercontent.com/onyx-ai/onyx/main/backend/slackbot_images/Confluence.png"
return "https://raw.githubusercontent.com/onyx-dot-app/onyx/main/backend/slackbot_images/Confluence.png"
if source == DocumentSource.JIRA.value:
return "https://raw.githubusercontent.com/onyx-ai/onyx/main/backend/slackbot_images/Jira.png"
return "https://raw.githubusercontent.com/onyx-dot-app/onyx/main/backend/slackbot_images/Jira.png"
if source == DocumentSource.NOTION.value:
return (
"https://raw.githubusercontent.com/onyx-ai/onyx/main/web/public/Notion.png"
)
return "https://raw.githubusercontent.com/onyx-dot-app/onyx/main/web/public/Notion.png"
if source == DocumentSource.ZENDESK.value:
return "https://raw.githubusercontent.com/onyx-ai/onyx/main/backend/slackbot_images/Zendesk.png"
return "https://raw.githubusercontent.com/onyx-dot-app/onyx/main/backend/slackbot_images/Zendesk.png"
if source == DocumentSource.GONG.value:
return "https://raw.githubusercontent.com/onyx-ai/onyx/main/web/public/Gong.png"
return "https://raw.githubusercontent.com/onyx-dot-app/onyx/main/web/public/Gong.png"
if source == DocumentSource.LINEAR.value:
return (
"https://raw.githubusercontent.com/onyx-ai/onyx/main/web/public/Linear.png"
)
return "https://raw.githubusercontent.com/onyx-dot-app/onyx/main/web/public/Linear.png"
if source == DocumentSource.PRODUCTBOARD.value:
return "https://raw.githubusercontent.com/onyx-ai/onyx/main/web/public/Productboard.webp"
return "https://raw.githubusercontent.com/onyx-dot-app/onyx/main/web/public/Productboard.webp"
if source == DocumentSource.SLAB.value:
return "https://raw.githubusercontent.com/onyx-ai/onyx/main/web/public/SlabLogo.png"
return "https://raw.githubusercontent.com/onyx-dot-app/onyx/main/web/public/SlabLogo.png"
if source == DocumentSource.ZULIP.value:
return (
"https://raw.githubusercontent.com/onyx-ai/onyx/main/web/public/Zulip.png"
)
return "https://raw.githubusercontent.com/onyx-dot-app/onyx/main/web/public/Zulip.png"
if source == DocumentSource.GURU.value:
return "https://raw.githubusercontent.com/onyx-ai/onyx/main/backend/slackbot_images/Guru.png"
return "https://raw.githubusercontent.com/onyx-dot-app/onyx/main/backend/slackbot_images/Guru.png"
if source == DocumentSource.HUBSPOT.value:
return (
"https://raw.githubusercontent.com/onyx-ai/onyx/main/web/public/HubSpot.png"
)
return "https://raw.githubusercontent.com/onyx-dot-app/onyx/main/web/public/HubSpot.png"
if source == DocumentSource.DOCUMENT360.value:
return "https://raw.githubusercontent.com/onyx-ai/onyx/main/web/public/Document360.png"
return "https://raw.githubusercontent.com/onyx-dot-app/onyx/main/web/public/Document360.png"
if source == DocumentSource.BOOKSTACK.value:
return "https://raw.githubusercontent.com/onyx-ai/onyx/main/web/public/Bookstack.png"
return "https://raw.githubusercontent.com/onyx-dot-app/onyx/main/web/public/Bookstack.png"
if source == DocumentSource.LOOPIO.value:
return (
"https://raw.githubusercontent.com/onyx-ai/onyx/main/web/public/Loopio.png"
)
return "https://raw.githubusercontent.com/onyx-dot-app/onyx/main/web/public/Loopio.png"
if source == DocumentSource.SHAREPOINT.value:
return "https://raw.githubusercontent.com/onyx-ai/onyx/main/web/public/Sharepoint.png"
return "https://raw.githubusercontent.com/onyx-dot-app/onyx/main/web/public/Sharepoint.png"
if source == DocumentSource.REQUESTTRACKER.value:
# just use file icon for now
return "https://raw.githubusercontent.com/onyx-ai/onyx/main/backend/slackbot_images/File.png"
return "https://raw.githubusercontent.com/onyx-dot-app/onyx/main/backend/slackbot_images/File.png"
if source == DocumentSource.INGESTION_API.value:
return "https://raw.githubusercontent.com/onyx-ai/onyx/main/backend/slackbot_images/File.png"
return "https://raw.githubusercontent.com/onyx-dot-app/onyx/main/backend/slackbot_images/File.png"

return "https://raw.githubusercontent.com/onyx-ai/onyx/main/backend/slackbot_images/File.png"
return "https://raw.githubusercontent.com/onyx-dot-app/onyx/main/backend/slackbot_images/File.png"
1 change: 0 additions & 1 deletion backend/onyx/onyxbot/slack/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,6 @@ def remove_slack_text_interactions(slack_str: str) -> str:
slack_str = SlackTextCleaner.replace_tags_basic(slack_str)
slack_str = SlackTextCleaner.replace_channels_basic(slack_str)
slack_str = SlackTextCleaner.replace_special_mentions(slack_str)
slack_str = SlackTextCleaner.replace_links(slack_str)
slack_str = SlackTextCleaner.replace_special_catchall(slack_str)
slack_str = SlackTextCleaner.add_zero_width_whitespace_after_tag(slack_str)
return slack_str
Expand Down

0 comments on commit 92add65

Please sign in to comment.