From ec3b8fab5b484017f68be5f0fb61f7313ea620c2 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Mon, 14 Apr 2025 01:08:16 -0700 Subject: [PATCH] refac --- backend/open_webui/socket/main.py | 19 +++---- backend/open_webui/utils/middleware.py | 11 +++- src/lib/components/chat/Chat.svelte | 54 ++++++++++--------- .../chat/Messages/ResponseMessage.svelte | 34 +++++++----- 4 files changed, 69 insertions(+), 49 deletions(-) diff --git a/backend/open_webui/socket/main.py b/backend/open_webui/socket/main.py index 2c64d4bf7..282f4db95 100644 --- a/backend/open_webui/socket/main.py +++ b/backend/open_webui/socket/main.py @@ -339,16 +339,17 @@ def get_event_emitter(request_info, update_db=True): request_info["message_id"], ) - content = message.get("content", "") - content += event_data.get("data", {}).get("content", "") + if message: + content = message.get("content", "") + content += event_data.get("data", {}).get("content", "") - Chats.upsert_message_to_chat_by_id_and_message_id( - request_info["chat_id"], - request_info["message_id"], - { - "content": content, - }, - ) + Chats.upsert_message_to_chat_by_id_and_message_id( + request_info["chat_id"], + request_info["message_id"], + { + "content": content, + }, + ) if "type" in event_data and event_data["type"] == "replace": content = event_data.get("data", {}).get("content", "") diff --git a/backend/open_webui/utils/middleware.py b/backend/open_webui/utils/middleware.py index 0f3dc67f5..f708fa170 100644 --- a/backend/open_webui/utils/middleware.py +++ b/backend/open_webui/utils/middleware.py @@ -537,8 +537,15 @@ async def chat_image_generation_handler( for image in images: await __event_emitter__( { - "type": "message", - "data": {"content": f"![Generated Image]({image['url']})\n"}, + "type": "files", + "data": { + "files": [ + { + "type": "image", + "url": image["url"], + } + ] + }, } ) diff --git a/src/lib/components/chat/Chat.svelte b/src/lib/components/chat/Chat.svelte index 8a8dd6fb7..fb9faa247 100644 --- a/src/lib/components/chat/Chat.svelte +++ b/src/lib/components/chat/Chat.svelte @@ -262,6 +262,21 @@ } else { message.statusHistory = [data]; } + } else if (type === 'chat:completion') { + chatCompletionEventHandler(data, message, event.chat_id); + } else if (type === 'chat:message:delta' || type === 'message') { + message.content += data.content; + } else if (type === 'chat:message' || type === 'replace') { + message.content = data.content; + } else if (type === 'chat:message:files' || type === 'files') { + message.files = data.files; + } else if (type === 'chat:title') { + chatTitle.set(data); + currentChatPage.set(1); + await chats.set(await getChatList(localStorage.token, $currentChatPage)); + } else if (type === 'chat:tags') { + chat = await getChatById(localStorage.token, $chatId); + allTags.set(await getAllTags(localStorage.token)); } else if (type === 'source' || type === 'citation') { if (data?.type === 'code_execution') { // Code execution; update existing code execution by ID, or add new one. @@ -288,19 +303,19 @@ message.sources = [data]; } } - } else if (type === 'chat:completion') { - chatCompletionEventHandler(data, message, event.chat_id); - } else if (type === 'chat:title') { - chatTitle.set(data); - currentChatPage.set(1); - await chats.set(await getChatList(localStorage.token, $currentChatPage)); - } else if (type === 'chat:tags') { - chat = await getChatById(localStorage.token, $chatId); - allTags.set(await getAllTags(localStorage.token)); - } else if (type === 'chat:message:delta' || type === 'message') { - message.content += data.content; - } else if (type === 'chat:message' || type === 'replace') { - message.content = data.content; + } else if (type === 'notification') { + const toastType = data?.type ?? 'info'; + const toastContent = data?.content ?? ''; + + if (toastType === 'success') { + toast.success(toastContent); + } else if (toastType === 'error') { + toast.error(toastContent); + } else if (toastType === 'warning') { + toast.warning(toastContent); + } else { + toast.info(toastContent); + } } else if (type === 'confirmation') { eventCallback = cb; @@ -333,19 +348,6 @@ eventConfirmationMessage = data.message; eventConfirmationInputPlaceholder = data.placeholder; eventConfirmationInputValue = data?.value ?? ''; - } else if (type === 'notification') { - const toastType = data?.type ?? 'info'; - const toastContent = data?.content ?? ''; - - if (toastType === 'success') { - toast.success(toastContent); - } else if (toastType === 'error') { - toast.error(toastContent); - } else if (toastType === 'warning') { - toast.warning(toastContent); - } else { - toast.info(toastContent); - } } else { console.log('Unknown message type', data); } diff --git a/src/lib/components/chat/Messages/ResponseMessage.svelte b/src/lib/components/chat/Messages/ResponseMessage.svelte index 5d6a1c3a8..027c40dae 100644 --- a/src/lib/components/chat/Messages/ResponseMessage.svelte +++ b/src/lib/components/chat/Messages/ResponseMessage.svelte @@ -47,6 +47,7 @@ import CodeExecutions from './CodeExecutions.svelte'; import ContentRenderer from './ContentRenderer.svelte'; import { KokoroWorker } from '$lib/workers/KokoroWorker'; + import FileItem from '$lib/components/common/FileItem.svelte'; interface MessageType { id: string; @@ -613,18 +614,6 @@
- {#if message?.files && message.files?.filter((f) => f.type === 'image').length > 0} -
- {#each message.files as file} -
- {#if file.type === 'image'} - {message.content} - {/if} -
- {/each} -
- {/if} -
{#if (message?.statusHistory ?? [...(message?.status ? [message?.status] : [])]).length > 0} @@ -703,6 +692,27 @@ {/if} {/if} + {#if message?.files && message.files?.filter((f) => f.type === 'image').length > 0} +
+ {#each message.files as file} +
+ {#if file.type === 'image'} + {message.content} + {:else} + + {/if} +
+ {/each} +
+ {/if} + {#if edit === true}