Skip to content

Commit

Permalink
Refactor internal methods
Browse files Browse the repository at this point in the history
  • Loading branch information
seratch committed Jan 10, 2024
1 parent 83a366d commit ccab269
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
33 changes: 19 additions & 14 deletions app/bolt_listeners.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
)
from app.slack_ops import (
find_parent_message,
is_no_mention_thread,
is_this_app_mentioned,
post_wip_message,
update_wip_message,
extract_state_value,
Expand Down Expand Up @@ -69,10 +69,11 @@ def respond_to_app_mention(
parent_message = find_parent_message(
client, context.channel_id, payload.get("thread_ts")
)
if parent_message is not None:
if is_no_mention_thread(context, parent_message):
# The message event handler will reply to this
return
if parent_message is not None and is_this_app_mentioned(
context, parent_message
):
# The message event handler will reply to this
return

wip_reply = None
# Replace placeholder for Slack user ID in the system prompt
Expand Down Expand Up @@ -234,7 +235,7 @@ def respond_to_new_message(
wip_reply = None
try:
is_in_dm_with_bot = payload.get("channel_type") == "im"
is_no_mention_required = False
is_thread_for_this_app = False
thread_ts = payload.get("thread_ts")
if is_in_dm_with_bot is False and thread_ts is None:
return
Expand All @@ -245,7 +246,7 @@ def respond_to_new_message(

messages_in_context = []
if is_in_dm_with_bot is True and thread_ts is None:
# In the DM with the bot
# In the DM with the bot; this is not within a thread
past_messages = client.conversations_history(
channel=context.channel_id,
include_all_metadata=True,
Expand All @@ -257,30 +258,32 @@ def respond_to_new_message(
seconds = time.time() - float(message.get("ts"))
if seconds < 86400: # less than 1 day
messages_in_context.append(message)
is_no_mention_required = True
is_thread_for_this_app = True
else:
# In a thread with the bot in a channel
# Within a thread
messages_in_context = client.conversations_replies(
channel=context.channel_id,
ts=thread_ts,
include_all_metadata=True,
limit=1000,
).get("messages", [])
if is_in_dm_with_bot is True:
is_no_mention_required = True
# In the DM with this bot
is_thread_for_this_app = True
else:
# In a channel
the_parent_message_found = False
for message in messages_in_context:
if message.get("ts") == thread_ts:
the_parent_message_found = True
is_no_mention_required = is_no_mention_thread(context, message)
is_thread_for_this_app = is_this_app_mentioned(context, message)
break
if the_parent_message_found is False:
parent_message = find_parent_message(
client, context.channel_id, thread_ts
)
if parent_message is not None:
is_no_mention_required = is_no_mention_thread(
is_thread_for_this_app = is_this_app_mentioned(
context, parent_message
)

Expand Down Expand Up @@ -310,7 +313,7 @@ def respond_to_new_message(
messages = maybe_new_messages
last_assistant_idx = idx

if is_no_mention_required is False:
if is_thread_for_this_app is False:
return

if is_in_dm_with_bot is True or last_assistant_idx == -1:
Expand Down Expand Up @@ -342,7 +345,9 @@ def respond_to_new_message(
"content": f"<@{msg_user_id}>: "
+ format_openai_message_content(reply_text, TRANSLATE_MARKDOWN),
"role": (
"assistant" if "user" in reply and reply["user"] == context.bot_user_id else "user"
"assistant"
if "user" in reply and reply["user"] == context.bot_user_id
else "user"
),
}
)
Expand Down
2 changes: 1 addition & 1 deletion app/slack_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def find_parent_message(
return messages[0] if len(messages) > 0 else None


def is_no_mention_thread(context: BoltContext, parent_message: dict) -> bool:
def is_this_app_mentioned(context: BoltContext, parent_message: dict) -> bool:
parent_message_text = parent_message.get("text", "")
return f"<@{context.bot_user_id}>" in parent_message_text

Expand Down

0 comments on commit ccab269

Please sign in to comment.