Skip to content
Merged
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
55 changes: 10 additions & 45 deletions listeners/assistant/assistant.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
from typing import Dict, List

from slack_bolt import Assistant, BoltContext, Say, SetStatus, SetSuggestedPrompts
from slack_bolt.context.get_thread_context import GetThreadContext
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError

from ai.llm_caller import call_llm

Expand All @@ -17,7 +15,6 @@
@assistant.thread_started
def start_assistant_thread(
say: Say,
get_thread_context: GetThreadContext,
set_suggested_prompts: SetSuggestedPrompts,
logger: Logger,
):
Expand All @@ -26,7 +23,6 @@ def start_assistant_thread(

Args:
say: Function to send messages to the thread from the app
get_thread_context: Function to retrieve thread context information
set_suggested_prompts: Function to configure suggested prompt options
logger: Logger instance for error tracking
"""
Expand All @@ -48,14 +44,6 @@ def start_assistant_thread(
},
]

thread_context = get_thread_context()
if thread_context is not None and thread_context.channel_id is not None:
summarize_channel = {
"title": "Summarize the referred channel",
"message": "Can you generate a brief summary of the referred channel?",
}
prompts.append(summarize_channel)

set_suggested_prompts(prompts=prompts)
except Exception as e:
logger.exception(f"Failed to handle an assistant_thread_started event: {e}", e)
Expand All @@ -67,7 +55,6 @@ def start_assistant_thread(
def respond_in_assistant_thread(
client: WebClient,
context: BoltContext,
get_thread_context: GetThreadContext,
logger: Logger,
payload: dict,
say: Say,
Expand All @@ -79,7 +66,6 @@ def respond_in_assistant_thread(
Args:
client: Slack WebClient for making API calls
context: Bolt context containing channel and thread information
get_thread_context: Function to retrieve thread context (e.g., referred channel)
logger: Logger instance for error tracking
payload: Event payload with message details (channel, user, text, etc.)
say: Function to send messages to the thread
Expand All @@ -90,7 +76,6 @@ def respond_in_assistant_thread(
team_id = context.team_id
thread_ts = payload["thread_ts"]
user_id = context.user_id
user_message = payload["text"]

set_status(
status="thinking...",
Expand All @@ -103,36 +88,16 @@ def respond_in_assistant_thread(
],
)

if user_message == "Can you generate a brief summary of the referred channel?":
thread_context = get_thread_context()
referred_channel_id = thread_context.get("channel_id")
try:
channel_history = client.conversations_history(channel=referred_channel_id, limit=50)
except SlackApiError as e:
if e.response["error"] == "not_in_channel":
# If this app's bot user is not in the public channel,
# we'll try joining the channel and then calling the same API again
client.conversations_join(channel=referred_channel_id)
Comment thread
srtaalej marked this conversation as resolved.
channel_history = client.conversations_history(channel=referred_channel_id, limit=50)
else:
raise e
prompt = f"Can you generate a brief summary of these messages in a Slack channel <#{referred_channel_id}>?\n\n"
for message in reversed(channel_history.get("messages")):
if message.get("user") is not None:
prompt += f"\n<@{message['user']}> says: {message['text']}\n"
messages_in_thread = [{"role": "user", "content": prompt}]

else:
replies = client.conversations_replies(
channel=context.channel_id,
ts=context.thread_ts,
oldest=context.thread_ts,
limit=10,
)
messages_in_thread: List[Dict[str, str]] = []
for message in replies["messages"]:
role = "user" if message.get("bot_id") is None else "assistant"
messages_in_thread.append({"role": role, "content": message["text"]})
replies = client.conversations_replies(
channel=context.channel_id,
ts=context.thread_ts,
oldest=context.thread_ts,
limit=10,
)
messages_in_thread: List[Dict[str, str]] = []
for message in replies["messages"]:
role = "user" if message.get("bot_id") is None else "assistant"
messages_in_thread.append({"role": role, "content": message["text"]})

returned_message = call_llm(messages_in_thread)

Expand Down
9 changes: 2 additions & 7 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,9 @@
"bot": [
"app_mentions:read",
"assistant:write",
"channels:join",
"im:history",
"channels:history",
"channels:read",
"groups:history",
"chat:write",
"users:read"
"chat:write"
]
}
},
Expand All @@ -38,8 +34,7 @@
"assistant_thread_context_changed",
"assistant_thread_started",
"message.im",
"app_mention",
"message.channels"
"app_mention"
]
},
"interactivity": {
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
slack-sdk==3.36.0.dev6
slack-bolt==1.26.0.dev3
slack-sdk==3.37.0
slack-bolt==1.26.0

# If you use a different LLM vendor, replace this dependency
openai
Expand Down