Skip to content

Commit 48187ca

Browse files
committed
Refactor MessageHandler to consolidate message analysis and response handling, removing the MessageAnalysisResult class and integrating its logic directly into the response generation process for improved clarity and efficiency.
1 parent 0f3932a commit 48187ca

1 file changed

Lines changed: 58 additions & 79 deletions

File tree

bot/rp_bot/messages/message_handler.py

Lines changed: 58 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from typing import Optional, AsyncIterator, List
22
from datetime import datetime
3-
from pydantic import BaseModel
43

54
from ...models.handlers_response import CommandResponse
65
from ...models.handlers_input import Person, Context, Message, TranscribedMessage
@@ -9,11 +8,6 @@
98
from ..ai_agent.agent_tools.agent import AIAgent
109

1110

12-
class MessageAnalysisResult(BaseModel):
13-
engage_is_needed: bool
14-
save_message_to_db: bool
15-
16-
1711
class MessageHandler(RPBotMessageHandler):
1812
permission_classes = (AllowedUser, BotAdmin, NotBanned)
1913

@@ -65,50 +59,6 @@ async def _get_transcribed_message(self, message: Message) -> TranscribedMessage
6559
voice_description=voice_description,
6660
)
6761

68-
async def prepare_get_reply(
69-
self,
70-
person: Person,
71-
context: Context,
72-
message: Message,
73-
) -> MessageAnalysisResult:
74-
conversation_tracker_enabled = (
75-
await self.db.chats.get_conversation_tracker_state(context)
76-
)
77-
chat_is_started = await self.db.chats.chat_is_started(context)
78-
if not chat_is_started or (
79-
not conversation_tracker_enabled and not context.is_bot_mentioned
80-
):
81-
self.logger.info(
82-
f"Ignoring message from {person.user_handle} in chat {context.chat_id}"
83-
)
84-
return MessageAnalysisResult(
85-
engage_is_needed=False,
86-
save_message_to_db=False,
87-
)
88-
autoengage_state = await self.db.chats.get_autoengage_state(context)
89-
engage_is_needed = False
90-
if autoengage_state:
91-
prompt = await self.prompt_manager.compose_engage_needed_prompt(
92-
message.message_text
93-
)
94-
engage_is_needed = (
95-
await self.models_toolkit.text_model.async_ask_yes_no_question(prompt)
96-
)
97-
if not context.is_bot_mentioned and not engage_is_needed:
98-
self.logger.info(
99-
f"Saving the message from {person.user_handle} in chat {context.chat_id} "
100-
"as context for future responses and not generating a response"
101-
)
102-
elif not context.is_bot_mentioned and engage_is_needed:
103-
self.logger.info(
104-
f"Engaging with the message from {person.user_handle} in chat {context.chat_id} "
105-
"as the agent detected a question or a request for information"
106-
)
107-
return MessageAnalysisResult(
108-
engage_is_needed=engage_is_needed or context.is_bot_mentioned,
109-
save_message_to_db=True,
110-
)
111-
11262
async def is_usage_under_limit(
11363
self, person: Person, context: Context, transcribed_message: TranscribedMessage
11464
) -> bool:
@@ -134,28 +84,6 @@ async def get_prompt_from_transcribed_message(
13484
)
13585
return prompt
13686

137-
async def finish_get_reply(
138-
self,
139-
person: Person,
140-
context: Context,
141-
message: Message,
142-
response_message: str,
143-
) -> None:
144-
user_usage = await self._get_user_usage(message, response_message)
145-
await self.db.user_usage.add_usage_points(person, user_usage)
146-
await self.db.dialogs.add_message_to_dialog(
147-
context,
148-
"bot",
149-
TranscribedMessage(
150-
message_text=response_message,
151-
timestamp=datetime.now(),
152-
),
153-
)
154-
self.logger.info(
155-
f"Generated a response for the message from {person.user_handle} in chat {context.chat_id}"
156-
f"with usage of {user_usage}"
157-
)
158-
15987
async def get_response(
16088
self,
16189
person: Person,
@@ -175,11 +103,34 @@ async def get_response(
175103
async def stream_get_response(
176104
self, person: Person, context: Context, message: Message, args: List[str]
177105
) -> AsyncIterator[CommandResponse]:
178-
message_analysis_result = await self.prepare_get_reply(person, context, message)
179-
if (
180-
message_analysis_result.save_message_to_db
181-
and not message_analysis_result.engage_is_needed
106+
# Prepare and analyze the message (formerly prepare_get_reply logic)
107+
conversation_tracker_enabled = (
108+
await self.db.chats.get_conversation_tracker_state(context)
109+
)
110+
chat_is_started = await self.db.chats.chat_is_started(context)
111+
if not chat_is_started or (
112+
not conversation_tracker_enabled and not context.is_bot_mentioned
182113
):
114+
self.logger.info(
115+
f"Ignoring message from {person.user_handle} in chat {context.chat_id}"
116+
)
117+
return
118+
119+
autoengage_state = await self.db.chats.get_autoengage_state(context)
120+
engage_is_needed = False
121+
if autoengage_state:
122+
prompt = await self.prompt_manager.compose_engage_needed_prompt(
123+
message.message_text
124+
)
125+
engage_is_needed = (
126+
await self.models_toolkit.text_model.async_ask_yes_no_question(prompt)
127+
)
128+
129+
# Determine if we should engage or just save to DB
130+
should_engage = engage_is_needed or context.is_bot_mentioned
131+
save_message_to_db = True
132+
133+
if save_message_to_db and not should_engage:
183134
# Save message without transcribing to save resources
184135
await self.db.dialogs.add_message_to_dialog(
185136
context=context,
@@ -189,10 +140,21 @@ async def stream_get_response(
189140
timestamp=message.timestamp,
190141
),
191142
)
192-
return None
193-
if not message_analysis_result.engage_is_needed:
143+
self.logger.info(
144+
f"Saving the message from {person.user_handle} in chat {context.chat_id} "
145+
"as context for future responses and not generating a response"
146+
)
194147
return
195148

149+
if not should_engage:
150+
return
151+
152+
if not context.is_bot_mentioned and engage_is_needed:
153+
self.logger.info(
154+
f"Engaging with the message from {person.user_handle} in chat {context.chat_id} "
155+
"as the agent detected a question or a request for information"
156+
)
157+
196158
if not self.is_usage_under_limit(person, context, message):
197159
yield await self.get_usage_over_limit_response(person)
198160
return
@@ -220,6 +182,8 @@ async def stream_get_response(
220182
text="streaming_message_response",
221183
kwargs={"response_text": response_message},
222184
)
185+
186+
# Save the user's message to dialog
223187
await self.db.dialogs.add_message_to_dialog(
224188
context=context,
225189
person=person,
@@ -228,4 +192,19 @@ async def stream_get_response(
228192
timestamp=message.timestamp,
229193
),
230194
)
231-
await self.finish_get_reply(person, context, message, response_message)
195+
196+
# Finish processing (formerly finish_get_reply logic)
197+
user_usage = await self._get_user_usage(message, response_message)
198+
await self.db.user_usage.add_usage_points(person, user_usage)
199+
await self.db.dialogs.add_message_to_dialog(
200+
context,
201+
"bot",
202+
TranscribedMessage(
203+
message_text=response_message,
204+
timestamp=datetime.now(),
205+
),
206+
)
207+
self.logger.info(
208+
f"Generated a response for the message from {person.user_handle} in chat {context.chat_id} "
209+
f"with usage of {user_usage}"
210+
)

0 commit comments

Comments
 (0)