diff --git a/api/chat_main_handler.go b/api/chat_main_handler.go index 1c814351..1a0535b8 100644 --- a/api/chat_main_handler.go +++ b/api/chat_main_handler.go @@ -221,12 +221,16 @@ func genAnswer(h *ChatHandler, w http.ResponseWriter, chatSessionUuid string, ch } if existingPrompt { - _, err := h.service.CreateChatMessageSimple(ctx, chatSession.Uuid, chatUuid, "user", newQuestion, chatSession.Model, userID, baseURL, chatSession.SummarizeMode) - if err != nil { - http.Error(w, - eris.Wrap(err, "fail to create message: ").Error(), - http.StatusInternalServerError, - ) + if newQuestion != "" { + _, err := h.service.CreateChatMessageSimple(ctx, chatSession.Uuid, chatUuid, "user", newQuestion, chatSession.Model, userID, baseURL, chatSession.SummarizeMode) + if err != nil { + http.Error(w, + eris.Wrap(err, "fail to create message: ").Error(), + http.StatusInternalServerError, + ) + } + } else { + log.Println("no new question, regenerate answer") } } else { chatPrompt, err := h.service.CreateChatPromptSimple(chatSessionUuid, newQuestion, userID) diff --git a/web/src/api/content.ts b/web/src/api/content.ts index d49cfb0d..681d8ca8 100644 --- a/web/src/api/content.ts +++ b/web/src/api/content.ts @@ -6,6 +6,7 @@ export const deleteChatData = async (chat: Chat.Message) => { await deleteChatPrompt(chat.uuid) else await deleteChatMessage(chat.uuid) + } export const updateChatData = async (chat: Chat.Message) => { diff --git a/web/src/views/chat/components/Conversation.vue b/web/src/views/chat/components/Conversation.vue index 33bd606b..e25a7d1a 100644 --- a/web/src/views/chat/components/Conversation.vue +++ b/web/src/views/chat/components/Conversation.vue @@ -10,7 +10,7 @@ import { useScroll } from '@/views/chat/hooks/useScroll' import { useChat } from '@/views/chat/hooks/useChat' import HeaderComponent from '@/views/chat/components/Header/index.vue' import SessionConfig from '@/views/chat/components/Session/SessionConfig.vue' -import { createChatBot, createChatSnapshot, fetchChatStream } from '@/api' +import { createChatBot, createChatSnapshot, deleteChatMessage, fetchChatStream } from '@/api' import { HoverButton, SvgIcon } from '@/components/common' import { useBasicLayout } from '@/hooks/useBasicLayout' import { useChatStore, usePromptStore } from '@/store' @@ -41,7 +41,7 @@ const { sessionUuid } = defineProps({ const { isMobile } = useBasicLayout() -const { addChat, updateChat, updateChatPartial } = useChat() +const { addChat, deleteChat, updateChat, updateChatPartial } = useChat() const { scrollRef, scrollToBottom, scrollToBottomIfAtBottom } = useScroll() // session uuid chatStore.syncChatMessages(sessionUuid) @@ -234,23 +234,74 @@ async function onRegenerate(index: number) { const chat = dataSources.value[index] + console.log("regen", chat) + const chatUuid = chat.uuid - const message = chat.text + // from user + const inversion = chat.inversion loading.value = true - updateChat( - sessionUuid, - index, - { - uuid: chatUuid, - dateTime: nowISO(), - text: '', - inversion: false, - error: false, - loading: true, - }, - ) + + let updateIndex = index; + let isRegenerate = true; + + if (inversion) { + // trigger from user message + const chatNext = dataSources.value[index + 1] + if (chatNext) { + updateIndex = index + 1 + isRegenerate = false + // if there are answer below. then clear + await deleteChatMessage(chatNext.uuid) + updateChat( + sessionUuid, + updateIndex, + { + uuid: chatNext.uuid, + dateTime: nowISO(), + text: '', + inversion: false, + error: false, + loading: true, + }, + ) + + } else { + // add a blank response + updateIndex = index + 1 + isRegenerate = false + addChat( + sessionUuid, + { + uuid: '', + dateTime: nowISO(), + text: '', + loading: true, + inversion: false, + error: false, + }, + ) + } + // if there are answer below. then clear + // if not, add answer + + } else { + // clear the old answer for regenerating + updateChat( + sessionUuid, + index, + { + uuid: chatUuid, + dateTime: nowISO(), + text: '', + inversion: false, + error: false, + loading: true, + }, + ) + + } try { const subscribleStrem = async () => { try { @@ -258,8 +309,8 @@ async function onRegenerate(index: number) { const response = fetchChatStream( sessionUuid, chatUuid, - true, - message, + isRegenerate, + "", (progress: any) => { const xhr = progress.event.target const { @@ -267,6 +318,7 @@ async function onRegenerate(index: number) { status } = xhr + if (status >= 400) { const error_json: { code: number; message: string; details: any } = JSON.parse(responseText) nui_msg.error(t(error_json.message), { @@ -300,6 +352,7 @@ async function onRegenerate(index: number) { }, ) } + } }, ) diff --git a/web/src/views/chat/components/Message/index.vue b/web/src/views/chat/components/Message/index.vue index 07e9bd45..8fdb3a44 100644 --- a/web/src/views/chat/components/Message/index.vue +++ b/web/src/views/chat/components/Message/index.vue @@ -84,18 +84,18 @@ function handleDelete() {