From 65bac701063768007ceaa58c15f4cc8d8d6315a6 Mon Sep 17 00:00:00 2001 From: jinyang628 Date: Sat, 28 Sep 2024 16:51:53 -0700 Subject: [PATCH 1/2] Update --- backend/app/connectors/client/slack.py | 28 ++++++++++++++++++---- backend/app/sandbox/integrations/slack.py | 29 ++++------------------- backend/app/utils/levenshtein.py | 4 ++-- 3 files changed, 30 insertions(+), 31 deletions(-) diff --git a/backend/app/connectors/client/slack.py b/backend/app/connectors/client/slack.py index ca36244..7be53d5 100644 --- a/backend/app/connectors/client/slack.py +++ b/backend/app/connectors/client/slack.py @@ -7,6 +7,7 @@ SlackGetChannelIdRequest, SlackSendMessageRequest, ) +from app.utils.levenshtein import get_most_similar_string logging.basicConfig(level=logging.INFO) @@ -17,22 +18,39 @@ class SlackClient: def __init__(self, access_token: str): self.client = AsyncWebClient(token=access_token) + async def get_all_channel_names(self) -> list[str]: + response = await self.client.conversations_list() + channels = response["channels"] + channel_names = [channel["name"] for channel in channels] + return channel_names + async def get_all_channel_ids( self, request: SlackGetChannelIdRequest ) -> list[dict[str, Any]]: response = await self.client.conversations_list() channels = response["channels"] - request_channel_names_set: set[str] = { - name.lower() for name in request.channel_names - } + request = await self._repair_channel_names(request=request) + channel_info = [ {"channel_name": channel["name"], "channel_id": channel["id"]} for channel in channels - if channel["name"].lower() - in request_channel_names_set # Slack channel names are always lower case + if channel["name"] + in request.channel_names ] return channel_info + async def _repair_channel_names(self, request: SlackGetChannelIdRequest) -> SlackGetChannelIdRequest: + possible_channel_names: list[str] = await self.get_all_channel_names() + updated_channel_names: list[str] = [] + for channel_name in request.channel_names: + updated_channel_names.append( + get_most_similar_string( + target=channel_name, + candidates=possible_channel_names + ) + ) + return SlackGetChannelIdRequest(channel_names=updated_channel_names) + async def send_message(self, request: SlackSendMessageRequest): response = await self.client.chat_postMessage( channel=request.channel_id, text=request.text diff --git a/backend/app/sandbox/integrations/slack.py b/backend/app/sandbox/integrations/slack.py index 3d5b07f..a1a0510 100644 --- a/backend/app/sandbox/integrations/slack.py +++ b/backend/app/sandbox/integrations/slack.py @@ -1,3 +1,4 @@ +import asyncio import os from dotenv import load_dotenv @@ -18,35 +19,15 @@ client = SlackClient(access_token=SLACK_ACCESS_TOKEN) -def main(): +async def main(): # HARD CODE TEST - + print(await client.get_all_channel_names()) ## AGENT TEST - chat_history: list[Message] = [] - message = Message( - role=Role.USER, - content="I am the new intern and I want to send an introductory message to the channel named startup. You can write the introductory message for me and send it directly", - ).model_dump() - chat_history.append(message) - response = AgentResponse(agent=MAIN_TRIAGE_AGENT, message=message) - while response.agent: - prev_agent: Agent = response.agent - response = response.agent.query( - chat_history=chat_history, - access_token=SLACK_ACCESS_TOKEN, - refresh_token=None, - client_id=None, - client_secret=None, - ) - if isinstance(prev_agent, TriageAgent): - continue - chat_history.append(Message(role=Role.ASSISTANT, content=str(response.message))) - print("CHAT HISTORY", chat_history) - print("Final chat history", chat_history) + if __name__ == "__main__": - main() + asyncio.run(main()) ### HARD CODE TEST diff --git a/backend/app/utils/levenshtein.py b/backend/app/utils/levenshtein.py index 4d203e1..d688478 100644 --- a/backend/app/utils/levenshtein.py +++ b/backend/app/utils/levenshtein.py @@ -26,7 +26,7 @@ def get_most_similar_string(target: str, candidates: list[str]) -> str: _process_string(candidate), _process_string(target) ), ) - + # Return the original value if the similarity is less than the threshold if ratio(most_similar, target) < THRESHOLD: - return None + return target return most_similar From 6ed507741157d0594149cd0fe7f02e1c7c8c7e03 Mon Sep 17 00:00:00 2001 From: jinyang628 Date: Sat, 28 Sep 2024 16:52:02 -0700 Subject: [PATCH 2/2] Update --- backend/app/connectors/client/slack.py | 16 ++++++++-------- backend/app/sandbox/integrations/slack.py | 1 - 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/backend/app/connectors/client/slack.py b/backend/app/connectors/client/slack.py index 7be53d5..8e9aa50 100644 --- a/backend/app/connectors/client/slack.py +++ b/backend/app/connectors/client/slack.py @@ -23,34 +23,34 @@ async def get_all_channel_names(self) -> list[str]: channels = response["channels"] channel_names = [channel["name"] for channel in channels] return channel_names - + async def get_all_channel_ids( self, request: SlackGetChannelIdRequest ) -> list[dict[str, Any]]: response = await self.client.conversations_list() channels = response["channels"] request = await self._repair_channel_names(request=request) - + channel_info = [ {"channel_name": channel["name"], "channel_id": channel["id"]} for channel in channels - if channel["name"] - in request.channel_names + if channel["name"] in request.channel_names ] return channel_info - async def _repair_channel_names(self, request: SlackGetChannelIdRequest) -> SlackGetChannelIdRequest: + async def _repair_channel_names( + self, request: SlackGetChannelIdRequest + ) -> SlackGetChannelIdRequest: possible_channel_names: list[str] = await self.get_all_channel_names() updated_channel_names: list[str] = [] for channel_name in request.channel_names: updated_channel_names.append( get_most_similar_string( - target=channel_name, - candidates=possible_channel_names + target=channel_name, candidates=possible_channel_names ) ) return SlackGetChannelIdRequest(channel_names=updated_channel_names) - + async def send_message(self, request: SlackSendMessageRequest): response = await self.client.chat_postMessage( channel=request.channel_id, text=request.text diff --git a/backend/app/sandbox/integrations/slack.py b/backend/app/sandbox/integrations/slack.py index a1a0510..a959654 100644 --- a/backend/app/sandbox/integrations/slack.py +++ b/backend/app/sandbox/integrations/slack.py @@ -25,7 +25,6 @@ async def main(): ## AGENT TEST - if __name__ == "__main__": asyncio.run(main())