mirror of
https://github.com/open-webui/open-webui.git
synced 2026-11-30 08:55:12 -05:00
fix: database upgrade breaks the reply chain of old chats whose first message has no id (#35012)
In a chat from an older version whose saved message list is broken, the upgrade falls back to the older plain list of its messages. When that list's first entry has no id but is already saved, the message after it kept pointing at a first message that does not exist, so the conversation could not be followed back to its start. The next message now points at the saved first message. Fixes #35004
This commit is contained in:
1 parent
c6dde2a4e6
commit
460bf0e42b
1 file changed
+20
-11
+20
-11
@@ -145,6 +145,24 @@ def _without_nul(value: Any) -> Any:
|
||||
return value
|
||||
|
||||
|
||||
def _drop_saved_list_entries(chat_id: str, messages: dict, existing: dict) -> None:
|
||||
"""Old lists give an entry no id of its own although its row exists; its children move to that row."""
|
||||
unclaimed = [row for row in existing.values() if row.id.removeprefix(f'{chat_id}-') not in messages]
|
||||
for message_id, message in list(messages.items()):
|
||||
if 'id' in message or f'{chat_id}-{message_id}' in existing:
|
||||
continue
|
||||
saved = next(
|
||||
(row for row in unclaimed if row.role == message.get('role') and row.content == message.get('content')),
|
||||
None,
|
||||
)
|
||||
if saved:
|
||||
unclaimed.remove(saved)
|
||||
del messages[message_id]
|
||||
for child in messages.values():
|
||||
if child.get('parentId') == message_id:
|
||||
child['parentId'] = saved.id.removeprefix(f'{chat_id}-')
|
||||
|
||||
|
||||
def _move_chat_history(
|
||||
conn: sa.Connection, chat_id: str, owner_id: str, chat: dict, current_message_id: str | None
|
||||
) -> None:
|
||||
@@ -171,22 +189,13 @@ def _move_chat_history(
|
||||
deleted_ids = existing.keys() - {f'{chat_id}-{message_id}' for message_id in history['messages']}
|
||||
if deleted_ids:
|
||||
conn.execute(sa.delete(chat_message_table).where(columns.id.in_(deleted_ids)))
|
||||
else:
|
||||
_drop_saved_list_entries(chat_id, messages, existing)
|
||||
|
||||
now = int(time.time())
|
||||
updates, inserts = [], []
|
||||
for message_id, message in messages.items():
|
||||
row_id = f'{chat_id}-{message_id}'
|
||||
# An old list entry without an id of its own is already saved when a row has its role and content.
|
||||
if (
|
||||
row_id not in existing
|
||||
and not isinstance(history.get('messages'), dict)
|
||||
and 'id' not in message
|
||||
and any(
|
||||
stored.role == message.get('role') and stored.content == message.get('content')
|
||||
for stored in existing.values()
|
||||
)
|
||||
):
|
||||
continue
|
||||
created_at = _recorded_timestamp(message.get('timestamp'))
|
||||
author = message.get('user') if isinstance(message.get('user'), dict) else {}
|
||||
author_id = message.get('user_id') or author.get('id')
|
||||
|
||||
Reference in new issue
Block a user