Skip to content

Commit 3e33f1d

Browse files
committed
Refactor message handling in MessageHandler: streamline the logic for adding messages to the dialog by ensuring both user and agent messages are recorded, improving the accuracy of conversation tracking and response generation.
1 parent 5030b38 commit 3e33f1d

1 file changed

Lines changed: 13 additions & 9 deletions

File tree

bot/rp_bot/messages/message_handler.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ async def get_response(
5555
async def stream_get_response(
5656
self, person: Person, context: Context, message: Message, args: List[str]
5757
) -> AsyncIterator[CommandResponse]:
58-
# Prepare and analyze the message (formerly prepare_get_reply logic)
5958
conversation_tracker_enabled = (
6059
await self.db.chats.get_conversation_tracker_state(context=context)
6160
)
@@ -149,15 +148,12 @@ async def stream_get_response(
149148
)
150149
yield response
151150

152-
await self.db.dialogs.add_message_to_dialog(
153-
context=context,
154-
person=person,
155-
transcribed_message=TranscribedMessage(
156-
message_text=message.message_text,
157-
timestamp=message.timestamp,
158-
),
159-
)
160151
if agent_response:
152+
await self.db.dialogs.add_message_to_dialog(
153+
context=context,
154+
person=person,
155+
transcribed_message=agent_response.transcribed_user_message,
156+
)
161157
await self.db.user_usage.add_usage_points(
162158
person=person, points=agent_response.total_price
163159
)
@@ -182,6 +178,14 @@ async def stream_get_response(
182178
f"with usage of {agent_response.total_price}"
183179
)
184180
else:
181+
await self.db.dialogs.add_message_to_dialog(
182+
context=context,
183+
person=person,
184+
transcribed_message=TranscribedMessage(
185+
message_text=message.message_text,
186+
timestamp=message.timestamp,
187+
),
188+
)
185189
self.logger.info(
186190
f"No response generated for the message from {person.user_handle} in chat {context.chat_id}"
187191
)

0 commit comments

Comments
 (0)