```plaintext

Prevent duplicate message storage in Chat components

Introduce `_message_stored` flag to `ChatInput` and `ChatOutput` components to prevent storing the same message multiple times during a single session. This change ensures that each message is stored only once, enhancing data integrity and reducing redundant operations.
```
This commit is contained in:
Rodrigo 2024-06-16 20:30:12 -03:00
commit cb1cc99c91
2 changed files with 10 additions and 2 deletions

View file

@ -44,6 +44,8 @@ class ChatInput(ChatComponent):
Output(display_name="Text", name="text", method="text_response"),
]
_message_stored = False
def message_response(self) -> Message:
message = Message(
text=self.input_value,
@ -51,8 +53,10 @@ class ChatInput(ChatComponent):
sender_name=self.sender_name,
session_id=self.session_id,
)
if self.session_id and isinstance(message, (Message, str)) and isinstance(message.text, str):
if self.session_id and isinstance(message, Message) and isinstance(message.text, str) and not self._message_stored:
self.store_message(message)
self._message_stored = True
self.status = message
return message

View file

@ -39,6 +39,8 @@ class ChatOutput(ChatComponent):
Output(display_name="Text", name="text", method="text_response"),
]
_message_stored = False
def message_response(self) -> Message:
message = Message(
text=self.input_value,
@ -46,8 +48,10 @@ class ChatOutput(ChatComponent):
sender_name=self.sender_name,
session_id=self.session_id,
)
if self.session_id and isinstance(message, Message) and isinstance(message.text, str):
if self.session_id and isinstance(message, Message) and isinstance(message.text, str) and not self._message_stored:
self.store_message(message)
self._message_stored = True
self.status = message
return message