Skip to content

Commit 061c159

Browse files
committed
Only update the changed fields when updating a messge from the backend
1 parent 97e7208 commit 061c159

File tree

1 file changed

+10
-3
lines changed
  • python/jupyterlab-chat/jupyterlab_chat

1 file changed

+10
-3
lines changed

python/jupyterlab-chat/jupyterlab_chat/ychat.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def add_message(self, new_message: NewMessage) -> str:
137137
)
138138

139139
# find all mentioned users and add them as message mentions
140-
mention_pattern = re.compile("@([\w-]+):?")
140+
mention_pattern = re.compile(r"@([\w-]+):?")
141141
mentioned_names: Set[str] = set(re.findall(mention_pattern, message.body))
142142
users = self.get_users()
143143
mentioned_usernames = []
@@ -165,12 +165,19 @@ def update_message(self, update: Message, append: bool = False):
165165
index = self._indexes_by_id[update.id]
166166
message = self._ymessages[index]
167167
except (KeyError, IndexError) as e:
168-
print(e)
168+
print(f"Error while updating the message:\n{e}")
169169
return
170170
update_dict = asdict(update)
171171
if (update.body and append):
172172
update_dict["body"] = message.get("body") + update.body
173-
message.update(update_dict)
173+
174+
# Only update the changed values.
175+
for key in update_dict:
176+
if key in message:
177+
if message[key] != update_dict[key]:
178+
message.update({ key: update_dict[key] })
179+
elif update_dict[key] is not None:
180+
message.update({ key: update_dict[key] })
174181

175182
def get_attachments(self) -> dict[str, Union[FileAttachment, NotebookAttachment]]:
176183
"""

0 commit comments

Comments
 (0)