Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions backend/app/connectors/client/slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
SlackGetChannelIdRequest,
SlackSendMessageRequest,
)
from app.utils.levenshtein import get_most_similar_string

logging.basicConfig(level=logging.INFO)

Expand All @@ -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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: This comparison is now case-sensitive. Ensure this is intended behavior

]
return channel_info

async def _repair_channel_names(
self, request: SlackGetChannelIdRequest
) -> SlackGetChannelIdRequest:
possible_channel_names: list[str] = await self.get_all_channel_names()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: This call might be redundant if get_all_channel_ids() already fetched the channel list

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
Expand Down
28 changes: 4 additions & 24 deletions backend/app/sandbox/integrations/slack.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import os

from dotenv import load_dotenv
Expand All @@ -18,35 +19,14 @@
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
Expand Down
4 changes: 2 additions & 2 deletions backend/app/utils/levenshtein.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment on lines 30 to +31
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: This change could lead to unexpected behavior in calling code that expects None for no match. Ensure all usages of this function are updated accordingly.

return most_similar