Skip to content

Commit e6260ac

Browse files
srtaalejzimeg
andauthored
refactor: breakup assistant.py into separate files (#25)
Co-authored-by: Eden Zimbelman <eden.zimbelman@salesforce.com>
1 parent 62122da commit e6260ac

File tree

5 files changed

+56
-53
lines changed

5 files changed

+56
-53
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,11 @@ Every incoming request is routed to a "listener". This directory groups each lis
8585

8686
Configures the new Slack Assistant features, providing a dedicated side panel UI for users to interact with the AI chatbot. This module includes:
8787

88-
`assistant.py`, which contains two listeners:
89-
* The `@assistant.thread_started` listener receives an event when users start new app thread.
90-
* The `@assistant.user_message` listener processes user messages in app threads or from the app **Chat** and **History** tab.
88+
- The `assistant_thread_started.py` file, which responds to new app threads with a list of suggested prompts.
89+
- The `message.py` file, which responds to user messages sent to app threads or from the **Chat** and **History** tab with an LLM generated response.
9190

92-
`ai/llm_caller.py`, which handles OpenAI API integration and message formatting. It includes the `call_llm()` function that sends conversation threads to OpenAI's models.
91+
### `ai/`
92+
The `llm_caller.py` file, which handles OpenAI API integration and message formatting. It includes the `call_llm()` function that sends conversation threads to OpenAI's models.
9393

9494
## App Distribution / OAuth
9595

listeners/assistant/__init__.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1-
from slack_bolt import App
1+
from slack_bolt import App, Assistant
22

3-
from .assistant import assistant
3+
from .assistant_thread_started import assistant_thread_started
4+
from .message import message
45

56

7+
# Refer to https://docs.slack.dev/tools/bolt-python/concepts/ai-apps#assistant for more details on the Assistant class
68
def register(app: App):
9+
assistant = Assistant()
10+
11+
assistant.thread_started(assistant_thread_started)
12+
assistant.user_message(message)
13+
714
app.assistant(assistant)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from logging import Logger
2+
from typing import Dict, List
3+
4+
from slack_bolt import Say, SetSuggestedPrompts
5+
6+
7+
def assistant_thread_started(
8+
say: Say,
9+
set_suggested_prompts: SetSuggestedPrompts,
10+
logger: Logger,
11+
):
12+
"""
13+
Handle the assistant thread start event by greeting the user and setting suggested prompts.
14+
15+
Args:
16+
say: Function to send messages to the thread from the app
17+
set_suggested_prompts: Function to configure suggested prompt options
18+
logger: Logger instance for error tracking
19+
"""
20+
try:
21+
say("How can I help you?")
22+
23+
prompts: List[Dict[str, str]] = [
24+
{
25+
"title": "What does Slack stand for?",
26+
"message": "Slack, a business communication service, was named after an acronym. Can you guess what it stands for?",
27+
},
28+
{
29+
"title": "Write a draft announcement",
30+
"message": "Can you write a draft announcement about a new feature my team just released? It must include how impactful it is.",
31+
},
32+
{
33+
"title": "Suggest names for my Slack app",
34+
"message": "Can you suggest a few names for my Slack app? The app helps my teammates better organize information and plan priorities and action items.",
35+
},
36+
]
37+
38+
set_suggested_prompts(prompts=prompts)
39+
except Exception as e:
40+
logger.exception(f"Failed to handle an assistant_thread_started event: {e}", e)
41+
say(f":warning: Something went wrong! ({e})")
Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,15 @@
11
from logging import Logger
22
from typing import Dict, List
33

4-
from slack_bolt import Assistant, BoltContext, Say, SetStatus, SetSuggestedPrompts
4+
from slack_bolt import BoltContext, Say, SetStatus
55
from slack_sdk import WebClient
66

77
from ai.llm_caller import call_llm
88

99
from ..views.feedback_block import create_feedback_block
1010

11-
# Refer to https://tools.slack.dev/bolt-python/concepts/assistant/ for more details
12-
assistant = Assistant()
1311

14-
15-
@assistant.thread_started
16-
def start_assistant_thread(
17-
say: Say,
18-
set_suggested_prompts: SetSuggestedPrompts,
19-
logger: Logger,
20-
):
21-
"""
22-
Handle the assistant thread start event by greeting the user and setting suggested prompts.
23-
24-
Args:
25-
say: Function to send messages to the thread from the app
26-
set_suggested_prompts: Function to configure suggested prompt options
27-
logger: Logger instance for error tracking
28-
"""
29-
try:
30-
say("How can I help you?")
31-
32-
prompts: List[Dict[str, str]] = [
33-
{
34-
"title": "What does Slack stand for?",
35-
"message": "Slack, a business communication service, was named after an acronym. Can you guess what it stands for?",
36-
},
37-
{
38-
"title": "Write a draft announcement",
39-
"message": "Can you write a draft announcement about a new feature my team just released? It must include how impactful it is.",
40-
},
41-
{
42-
"title": "Suggest names for my Slack app",
43-
"message": "Can you suggest a few names for my Slack app? The app helps my teammates better organize information and plan priorities and action items.",
44-
},
45-
]
46-
47-
set_suggested_prompts(prompts=prompts)
48-
except Exception as e:
49-
logger.exception(f"Failed to handle an assistant_thread_started event: {e}", e)
50-
say(f":warning: Something went wrong! ({e})")
51-
52-
53-
# This listener is invoked when the human user sends a reply in the assistant thread
54-
@assistant.user_message
55-
def respond_in_assistant_thread(
12+
def message(
5613
client: WebClient,
5714
context: BoltContext,
5815
logger: Logger,

manifest.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,13 @@
2323
"app_mentions:read",
2424
"assistant:write",
2525
"im:history",
26-
"channels:read",
2726
"chat:write"
2827
]
2928
}
3029
},
3130
"settings": {
3231
"event_subscriptions": {
3332
"bot_events": [
34-
"assistant_thread_context_changed",
3533
"assistant_thread_started",
3634
"message.im",
3735
"app_mention"

0 commit comments

Comments
 (0)