From 856845e5f29bfa35b31e297429a2c4e1a547bb9a Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Thu, 11 Jul 2024 19:05:59 -0700 Subject: [PATCH] refac --- backend/main.py | 2 +- src/lib/components/chat/Chat.svelte | 36 +++++++++++++++---- src/lib/components/chat/Messages.svelte | 4 ++- .../chat/Messages/ResponseMessage.svelte | 3 +- 4 files changed, 35 insertions(+), 10 deletions(-) diff --git a/backend/main.py b/backend/main.py index aa0b6c956..875f262cc 100644 --- a/backend/main.py +++ b/backend/main.py @@ -1285,7 +1285,7 @@ async def chat_completed( status_code=status.HTTP_404_NOT_FOUND, detail="Action not found", ) - + data = form_data model_id = data["model"] if model_id not in app.state.MODELS: diff --git a/src/lib/components/chat/Chat.svelte b/src/lib/components/chat/Chat.svelte index 085eddbde..6fce08a8b 100644 --- a/src/lib/components/chat/Chat.svelte +++ b/src/lib/components/chat/Chat.svelte @@ -363,7 +363,7 @@ } }; - const chatCompletedHandler = async (modelId, responseMessageId, messages) => { + const chatCompletedHandler = async (chatId, modelId, responseMessageId, messages) => { await mermaid.run({ querySelector: '.mermaid' }); @@ -377,7 +377,7 @@ info: m.info ? m.info : undefined, timestamp: m.timestamp })), - chat_id: $chatId, + chat_id: chatId, session_id: $socket?.id, id: responseMessageId }).catch((error) => { @@ -399,9 +399,21 @@ }; } } + + if ($chatId == chatId) { + if ($settings.saveChatHistory ?? true) { + chat = await updateChatById(localStorage.token, chatId, { + models: selectedModels, + messages: messages, + history: history, + params: params + }); + await chats.set(await getChatList(localStorage.token)); + } + } }; - const chatActionHandler = async (actionId, modelId, responseMessageId) => { + const chatActionHandler = async (chatId, actionId, modelId, responseMessageId) => { const res = await chatAction(localStorage.token, actionId, { model: modelId, messages: messages.map((m) => ({ @@ -411,7 +423,7 @@ info: m.info ? m.info : undefined, timestamp: m.timestamp })), - chat_id: $chatId, + chat_id: chatId, session_id: $socket?.id, id: responseMessageId }).catch((error) => { @@ -432,6 +444,18 @@ }; } } + + if ($chatId == chatId) { + if ($settings.saveChatHistory ?? true) { + chat = await updateChatById(localStorage.token, chatId, { + models: selectedModels, + messages: messages, + history: history, + params: params + }); + await chats.set(await getChatList(localStorage.token)); + } + } }; const getChatEventEmitter = async (modelId: string, chatId: string = '') => { @@ -801,7 +825,7 @@ controller.abort('User: Stop Response'); } else { const messages = createMessagesList(responseMessageId); - await chatCompletedHandler(model.id, responseMessageId, messages); + await chatCompletedHandler(_chatId, model.id, responseMessageId, messages); } _response = responseMessage.content; @@ -1113,7 +1137,7 @@ } else { const messages = createMessagesList(responseMessageId); - await chatCompletedHandler(model.id, responseMessageId, messages); + await chatCompletedHandler(_chatId, model.id, responseMessageId, messages); } _response = responseMessage.content; diff --git a/src/lib/components/chat/Messages.svelte b/src/lib/components/chat/Messages.svelte index 6b71e6596..e46e93143 100644 --- a/src/lib/components/chat/Messages.svelte +++ b/src/lib/components/chat/Messages.svelte @@ -336,7 +336,9 @@ copyToClipboard={copyToClipboardWithToast} {continueGeneration} {regenerateResponse} - {chatActionHandler} + on:action={async (e) => { + await chatActionHandler(chatId, e.detail, message.model, message.id); + }} on:save={async (e) => { console.log('save', e); diff --git a/src/lib/components/chat/Messages/ResponseMessage.svelte b/src/lib/components/chat/Messages/ResponseMessage.svelte index 329b2b1cb..9342e6899 100644 --- a/src/lib/components/chat/Messages/ResponseMessage.svelte +++ b/src/lib/components/chat/Messages/ResponseMessage.svelte @@ -1031,8 +1031,7 @@ ? 'visible' : 'invisible group-hover:visible'} p-1.5 hover:bg-black/5 dark:hover:bg-white/5 rounded-lg dark:hover:text-white hover:text-black transition regenerate-response-button" on:click={() => { - chatActionHandler(action.id, message.model, message.id); - console.log('action'); + dispatch('action', action.id); }} >