Skip to content

Commit adb146c

Browse files
committed
fix: sync chat_message rows on chat update
1 parent b5338bb commit adb146c

2 files changed

Lines changed: 14 additions & 15 deletions

File tree

backend/open_webui/models/chats.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,9 @@ async def update_chat_by_id(self, id: str, chat: dict, db: AsyncSession | None =
404404

405405
await db.commit()
406406

407+
new_messages = (chat.get('history', {}) or {}).get('messages', {}) or {}
408+
await self.reconcile_messages_by_chat_id(id, chat_item.user_id, new_messages)
409+
407410
return ChatModel.model_validate(chat_item)
408411
except Exception:
409412
return None
@@ -499,22 +502,25 @@ async def backfill_messages_by_chat_id(self, chat_id: str, user_id: str, message
499502
log.warning('Backfill failed for message %s in chat %s: %s', message_id, chat_id, e)
500503

501504
async def reconcile_messages_by_chat_id(
502-
self, chat_id: str, user_id: str, messages: dict[str, dict]
505+
self, chat_id: str, user_id: str, messages: dict[str, dict],
506+
*, delete_orphans: bool = False,
503507
) -> None:
504508
"""Sync ``chat_message`` rows with the committed JSON blob.
505509
506510
Upserts current messages via ``backfill_messages_by_chat_id``
507-
and deletes orphaned rows whose message_id no longer appears
508-
in the blob. Best-effort: errors are logged but never raised.
511+
and, when *delete_orphans* is True, deletes rows whose
512+
message_id no longer appears in the blob. Best-effort: errors
513+
are logged but never raised.
509514
"""
510515
try:
511516
await self.backfill_messages_by_chat_id(chat_id, user_id, messages)
512517

513-
existing_map = await ChatMessages.get_messages_map_by_chat_id(chat_id)
514-
if existing_map is not None:
515-
orphaned_ids = set(existing_map.keys()) - set(messages.keys())
516-
if orphaned_ids:
517-
await ChatMessages.delete_message_ids_by_chat_id(chat_id, orphaned_ids)
518+
if delete_orphans:
519+
existing_map = await ChatMessages.get_messages_map_by_chat_id(chat_id)
520+
if existing_map is not None:
521+
orphaned_ids = set(existing_map.keys()) - set(messages.keys())
522+
if orphaned_ids:
523+
await ChatMessages.delete_message_ids_by_chat_id(chat_id, orphaned_ids)
518524
except Exception as e:
519525
log.warning('Failed to reconcile chat_message rows for chat %s: %s', chat_id, e)
520526

backend/open_webui/routers/chats.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -988,13 +988,6 @@ async def update_chat_by_id(
988988

989989
chat = await Chats.update_chat_by_id(id, updated_chat, db=db)
990990

991-
# Reconcile chat_message rows with the committed blob.
992-
# This is the only caller where the frontend pushes a full
993-
# history with potential edits, deletions, or new branches.
994-
messages = (updated_chat.get('history') or {}).get('messages') or {}
995-
if messages:
996-
await Chats.reconcile_messages_by_chat_id(id, user.id, messages)
997-
998991
return ChatResponse(**chat.model_dump())
999992
else:
1000993
raise HTTPException(

0 commit comments

Comments
 (0)