From 7c398a625a8d51f79d80217f6d329fc30c72b782 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Sat, 9 May 2026 03:45:56 +0900 Subject: [PATCH] refac --- backend/open_webui/main.py | 11 +++++++++++ src/lib/components/chat/Chat.svelte | 18 ++++++------------ 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/backend/open_webui/main.py b/backend/open_webui/main.py index c437365312..cfb8cb3b1a 100644 --- a/backend/open_webui/main.py +++ b/backend/open_webui/main.py @@ -1829,6 +1829,7 @@ async def chat_completion( ] if user_message_id else [], + 'files': metadata.get('files') or [], 'tags': [], 'timestamp': int(time.time() * 1000), }, @@ -1861,6 +1862,16 @@ async def chat_completion( detail=ERROR_MESSAGES.DEFAULT(), ) + # Persist chat-level files (knowledge collections, docs, etc.) + # The old frontend saveChatHandler did this on every message; + # now the backend owns persistence. + chat_files = metadata.get('files') + if chat_files is not None: + existing_chat = await Chats.get_chat_by_id(chat_id) + if existing_chat: + updated = {**existing_chat.chat, 'files': chat_files} + await Chats.update_chat_by_id(chat_id, updated) + # Save user message to DB user_message = metadata.get('user_message') or {} if user_message and user_message.get('id'): diff --git a/src/lib/components/chat/Chat.svelte b/src/lib/components/chat/Chat.svelte index e7cf14a567..637596fdf8 100644 --- a/src/lib/components/chat/Chat.svelte +++ b/src/lib/components/chat/Chat.svelte @@ -2179,7 +2179,7 @@ files.push( ...(userMessage?.files ?? []).filter( (item) => - ['doc', 'text', 'note', 'chat', 'collection'].includes(item.type) || + ['doc', 'text', 'note', 'chat', 'collection', 'folder'].includes(item.type) || (item.type === 'file' && !(item?.content_type ?? '').startsWith('image/')) ) ); @@ -2433,18 +2433,12 @@ await chats.set(await getChatList(localStorage.token, $currentChatPage)); // Persist chat-level params (system prompt, advanced - // params) and files that the backend doesn't have when - // it creates the chat shell. Without this, reloading - // loses them. Only patch these fields — avoid writing - // history/messages which the backend is updating - // concurrently via streaming. - if ( - Object.keys(params).length > 0 || - chatFiles.length > 0 - ) { + // params) that the backend doesn't receive in the + // chat completion request. Files are now persisted + // by the backend at chat creation time. + if (Object.keys(params).length > 0) { await updateChatById(localStorage.token, res.chat_id, { - params: params, - files: chatFiles + params: params }); } }