22from typing import Dict , List
33
44from slack_bolt import Assistant , BoltContext , Say , SetStatus , SetSuggestedPrompts
5- from slack_bolt .context .get_thread_context import GetThreadContext
65from slack_sdk import WebClient
7- from slack_sdk .errors import SlackApiError
86
97from ai .llm_caller import call_llm
108
1715@assistant .thread_started
1816def start_assistant_thread (
1917 say : Say ,
20- get_thread_context : GetThreadContext ,
2118 set_suggested_prompts : SetSuggestedPrompts ,
2219 logger : Logger ,
2320):
@@ -26,7 +23,6 @@ def start_assistant_thread(
2623
2724 Args:
2825 say: Function to send messages to the thread from the app
29- get_thread_context: Function to retrieve thread context information
3026 set_suggested_prompts: Function to configure suggested prompt options
3127 logger: Logger instance for error tracking
3228 """
@@ -48,14 +44,6 @@ def start_assistant_thread(
4844 },
4945 ]
5046
51- thread_context = get_thread_context ()
52- if thread_context is not None and thread_context .channel_id is not None :
53- summarize_channel = {
54- "title" : "Summarize the referred channel" ,
55- "message" : "Can you generate a brief summary of the referred channel?" ,
56- }
57- prompts .append (summarize_channel )
58-
5947 set_suggested_prompts (prompts = prompts )
6048 except Exception as e :
6149 logger .exception (f"Failed to handle an assistant_thread_started event: { e } " , e )
@@ -67,7 +55,6 @@ def start_assistant_thread(
6755def respond_in_assistant_thread (
6856 client : WebClient ,
6957 context : BoltContext ,
70- get_thread_context : GetThreadContext ,
7158 logger : Logger ,
7259 payload : dict ,
7360 say : Say ,
@@ -79,7 +66,6 @@ def respond_in_assistant_thread(
7966 Args:
8067 client: Slack WebClient for making API calls
8168 context: Bolt context containing channel and thread information
82- get_thread_context: Function to retrieve thread context (e.g., referred channel)
8369 logger: Logger instance for error tracking
8470 payload: Event payload with message details (channel, user, text, etc.)
8571 say: Function to send messages to the thread
@@ -90,7 +76,6 @@ def respond_in_assistant_thread(
9076 team_id = context .team_id
9177 thread_ts = payload ["thread_ts" ]
9278 user_id = context .user_id
93- user_message = payload ["text" ]
9479
9580 set_status (
9681 status = "thinking..." ,
@@ -103,36 +88,16 @@ def respond_in_assistant_thread(
10388 ],
10489 )
10590
106- if user_message == "Can you generate a brief summary of the referred channel?" :
107- thread_context = get_thread_context ()
108- referred_channel_id = thread_context .get ("channel_id" )
109- try :
110- channel_history = client .conversations_history (channel = referred_channel_id , limit = 50 )
111- except SlackApiError as e :
112- if e .response ["error" ] == "not_in_channel" :
113- # If this app's bot user is not in the public channel,
114- # we'll try joining the channel and then calling the same API again
115- client .conversations_join (channel = referred_channel_id )
116- channel_history = client .conversations_history (channel = referred_channel_id , limit = 50 )
117- else :
118- raise e
119- prompt = f"Can you generate a brief summary of these messages in a Slack channel <#{ referred_channel_id } >?\n \n "
120- for message in reversed (channel_history .get ("messages" )):
121- if message .get ("user" ) is not None :
122- prompt += f"\n <@{ message ['user' ]} > says: { message ['text' ]} \n "
123- messages_in_thread = [{"role" : "user" , "content" : prompt }]
124-
125- else :
126- replies = client .conversations_replies (
127- channel = context .channel_id ,
128- ts = context .thread_ts ,
129- oldest = context .thread_ts ,
130- limit = 10 ,
131- )
132- messages_in_thread : List [Dict [str , str ]] = []
133- for message in replies ["messages" ]:
134- role = "user" if message .get ("bot_id" ) is None else "assistant"
135- messages_in_thread .append ({"role" : role , "content" : message ["text" ]})
91+ replies = client .conversations_replies (
92+ channel = context .channel_id ,
93+ ts = context .thread_ts ,
94+ oldest = context .thread_ts ,
95+ limit = 10 ,
96+ )
97+ messages_in_thread : List [Dict [str , str ]] = []
98+ for message in replies ["messages" ]:
99+ role = "user" if message .get ("bot_id" ) is None else "assistant"
100+ messages_in_thread .append ({"role" : role , "content" : message ["text" ]})
136101
137102 returned_message = call_llm (messages_in_thread )
138103
0 commit comments