From 7f1b8c9c602e2337e532fe51df7b4897973ef126 Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Tue, 19 Nov 2024 11:35:02 +0100 Subject: Fix generate messages on error in gui --- g4f/gui/client/static/js/chat.v1.js | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/g4f/gui/client/static/js/chat.v1.js b/g4f/gui/client/static/js/chat.v1.js index 11306cdc..9f6103fb 100644 --- a/g4f/gui/client/static/js/chat.v1.js +++ b/g4f/gui/client/static/js/chat.v1.js @@ -25,7 +25,8 @@ const optionElements = document.querySelectorAll(".settings input, .settings tex let provider_storage = {}; let message_storage = {}; let controller_storage = {}; -let content_storage = {} +let content_storage = {}; +let error_storage = {}; messageInput.addEventListener("blur", () => { window.scrollTo(0, 0); @@ -256,14 +257,14 @@ const delete_conversations = async () => { const handle_ask = async () => { messageInput.style.height = "82px"; messageInput.focus(); - window.scrollTo(0, 0); + await scroll_to_bottom(); let message = messageInput.value; if (message.length <= 0) { return; } messageInput.value = ""; - count_input() + await count_input() await add_conversation(window.conversation_id, message); if ("text" in fileInput.dataset) { @@ -396,7 +397,7 @@ async function add_message_chunk(message, message_index) { } else if (message.type == "message") { console.error(message.message) } else if (message.type == "error") { - window.error = message.error + error_storage[message_index] = message.error console.error(message.error); content_map.inner.innerHTML += `

An error occured: ${message.error}

`; } else if (message.type == "preview") { @@ -469,7 +470,6 @@ const ask_gpt = async (message_index = -1, message_id) => { `; controller_storage[message_index] = new AbortController(); - let error = false; let content_el = document.getElementById(`gpt_${message_id}`) let content_map = content_storage[message_index] = { @@ -478,8 +478,7 @@ const ask_gpt = async (message_index = -1, message_id) => { count: content_el.querySelector('.count'), } - message_box.scrollTop = message_box.scrollHeight; - window.scrollTo(0, 0); + await scroll_to_bottom(); try { const input = imageInput && imageInput.files.length > 0 ? imageInput : cameraInput; const file = input && input.files.length > 0 ? input.files[0] : null; @@ -501,7 +500,7 @@ const ask_gpt = async (message_index = -1, message_id) => { auto_continue: auto_continue, api_key: api_key }, file, message_index); - if (!error) { + if (!error_storage[message_index]) { html = markdown_render(message_storage[message_index]); content_map.inner.innerHTML = html; highlight(content_map.inner); @@ -513,7 +512,7 @@ const ask_gpt = async (message_index = -1, message_id) => { } catch (e) { console.error(e); if (e.name != "AbortError") { - error = true; + error_storage[message_index] = true; content_map.inner.innerHTML += `

An error occured: ${e}

`; } } @@ -526,13 +525,18 @@ const ask_gpt = async (message_index = -1, message_id) => { let cursorDiv = message_box.querySelector(".cursor"); if (cursorDiv) cursorDiv.parentNode.removeChild(cursorDiv); } - window.scrollTo(0, 0); - message_box.scrollTop = message_box.scrollHeight; + await scroll_to_bottom(); await remove_cancel_button(); await register_message_buttons(); await load_conversations(); + regenerate.classList.remove("regenerate-hidden"); }; +async function scroll_to_bottom() { + window.scrollTo(0, 0); + message_box.scrollTop = message_box.scrollHeight; +} + const clear_conversations = async () => { const elements = box_conversations.childNodes; let index = elements.length; -- cgit v1.2.3