From 53de48d2b30f437fe1a4ca4d506610610030c166 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Sun, 5 Oct 2025 23:29:48 -0500 Subject: [PATCH] refac --- backend/open_webui/routers/channels.py | 43 +++++++++++++------ .../components/common/RichTextInput.svelte | 42 ++++++++++-------- 2 files changed, 54 insertions(+), 31 deletions(-) diff --git a/backend/open_webui/routers/channels.py b/backend/open_webui/routers/channels.py index 3b856e47f3..da6bff61e5 100644 --- a/backend/open_webui/routers/channels.py +++ b/backend/open_webui/routers/channels.py @@ -344,7 +344,7 @@ async def model_response_handler(request, channel, message, user): "role": "system", "content": f"You are {model.get('name', model_id)}, participating in a threaded conversation. Be concise and conversational." + ( - f"Here's the thread history:\n\n{''.join([f'{msg}' for msg in thread_history])}\n\nContinue the conversation naturally as {model.get('name', model_id)}, addressing the most recent message while being aware of the full context." + f"Here's the thread history:\n\n\n{''.join([f'{msg}\n\n' for msg in thread_history])}\n\n\nContinue the conversation naturally as {model.get('name', model_id)}, addressing the most recent message while being aware of the full context." if thread_history else "" ), @@ -384,19 +384,34 @@ async def model_response_handler(request, channel, message, user): ) if res: - await update_message_by_id( - channel.id, - response_message.id, - MessageForm( - **{ - "content": res["choices"][0]["message"]["content"], - "meta": { - "done": True, - }, - } - ), - user, - ) + if res.get("choices", []) and len(res["choices"]) > 0: + await update_message_by_id( + channel.id, + response_message.id, + MessageForm( + **{ + "content": res["choices"][0]["message"]["content"], + "meta": { + "done": True, + }, + } + ), + user, + ) + elif res.get("error", None): + await update_message_by_id( + channel.id, + response_message.id, + MessageForm( + **{ + "content": f"Error: {res['error']}", + "meta": { + "done": True, + }, + } + ), + user, + ) except Exception as e: log.info(e) pass diff --git a/src/lib/components/common/RichTextInput.svelte b/src/lib/components/common/RichTextInput.svelte index 6e770b7627..26ec540c67 100644 --- a/src/lib/components/common/RichTextInput.svelte +++ b/src/lib/components/common/RichTextInput.svelte @@ -775,28 +775,36 @@ htmlValue = editor.getHTML(); jsonValue = editor.getJSON(); - mdValue = turndownService - .turndown( - htmlValue - .replace(/

<\/p>/g, '
') - .replace(/ {2,}/g, (m) => m.replace(/ /g, '\u00a0')) - ) - .replace(/\u00a0/g, ' '); if (richText) { - onChange({ - html: htmlValue, - json: jsonValue, - md: mdValue - }); + mdValue = turndownService + .turndown( + htmlValue + .replace(/

<\/p>/g, '
') + .replace(/ {2,}/g, (m) => m.replace(/ /g, '\u00a0')) + ) + .replace(/\u00a0/g, ' '); } else { - // Plain text path: preserve \t and \n exactly - const doc = editor.view.state.doc; - const plain = doc.textBetween(0, doc.content.size, '\n\n', '\n'); // keeps \t intact - value = plain; - onChange({ html: null, json: null, md: plain }); + mdValue = turndownService + .turndown( + htmlValue + // Replace empty paragraphs with line breaks + .replace(/

<\/p>/g, '
') + // Replace multiple spaces with non-breaking spaces + .replace(/ {2,}/g, (m) => m.replace(/ /g, '\u00a0')) + // Replace tabs with non-breaking spaces (preserve indentation) + .replace(/\t/g, '\u00a0\u00a0\u00a0\u00a0') // 1 tab = 4 spaces + ) + // Convert non-breaking spaces back to regular spaces for markdown + .replace(/\u00a0/g, ' '); } + onChange({ + html: htmlValue, + json: jsonValue, + md: mdValue + }); + if (json) { value = jsonValue; } else {