|
1 | 1 | from logging import Logger |
2 | 2 | from slack_sdk import WebClient |
3 | 3 | from slack_bolt import Say |
4 | | -from typing import List, Dict |
5 | 4 |
|
6 | 5 | from ..llm_caller import call_llm |
7 | 6 | from ..views.feedback_block import create_feedback_block |
8 | | -from ..listeners_constants import loading_messages |
9 | 7 |
|
10 | 8 | """ |
11 | 9 | Handles the event when the app is mentioned in a Slack channel, retrieves the conversation context, |
|
15 | 13 |
|
16 | 14 | def app_mentioned_callback(client: WebClient, event: dict, logger: Logger, say: Say): |
17 | 15 | try: |
18 | | - |
19 | 16 | channel_id = event.get("channel") |
20 | | - thread_ts = event.get("thread_ts") |
21 | | - user_id = event.get("user") |
22 | 17 | team_id = event.get("team") |
23 | 18 | text = event.get("text") |
24 | | - |
25 | | - if thread_ts: |
26 | | - conversation_context = client.conversations_replies(channel=channel_id, ts=thread_ts, limit=10) |
27 | | - else: |
28 | | - conversation_context = client.conversations_history(channel=channel_id, limit=50) |
29 | | - thread_ts = event["ts"] |
30 | | - |
31 | | - messages_in_thread: List[Dict[str, str]] = [] |
32 | | - for message in conversation_context["messages"]: |
33 | | - role = "user" if message.get("bot_id") is None else "assistant" |
34 | | - messages_in_thread.append({"role": role, "content": message["text"]}) |
35 | | - if text: |
36 | | - returned_message = call_llm(messages_in_thread) |
| 19 | + thread_ts = event.get("thread_ts") or event.get("ts") |
| 20 | + user_id = event.get("user") |
37 | 21 |
|
38 | 22 | client.assistant_threads_setStatus( |
39 | | - channel_id=channel_id, thread_ts=thread_ts, status="Bolt is typing", loading_messages=loading_messages |
| 23 | + channel_id=channel_id, |
| 24 | + thread_ts=thread_ts, |
| 25 | + status="thinking...", |
| 26 | + loading_messages=[ |
| 27 | + "Teaching the hamsters to type faster…", |
| 28 | + "Untangling the internet cables…", |
| 29 | + "Consulting the office goldfish…", |
| 30 | + "Polishing up the response just for you…", |
| 31 | + "Convincing the AI to stop overthinking…", |
| 32 | + ], |
40 | 33 | ) |
| 34 | + |
| 35 | + returned_message = call_llm([{"role": "user", "content": text}]) |
| 36 | + |
41 | 37 | stream_response = client.chat_startStream( |
42 | 38 | channel=channel_id, recipient_team_id=team_id, recipient_user_id=user_id, thread_ts=thread_ts |
43 | 39 | ) |
44 | 40 |
|
45 | 41 | stream_ts = stream_response["ts"] |
46 | 42 |
|
47 | | - # use of this for loop is specific to openai response method |
| 43 | + # Loop over OpenAI response stream |
| 44 | + # https://platform.openai.com/docs/api-reference/responses/create |
48 | 45 | for event in returned_message: |
49 | 46 | if event.type == "response.output_text.delta": |
50 | 47 | client.chat_appendStream(channel=channel_id, ts=stream_ts, markdown_text=f"{event.delta}") |
|
0 commit comments