fix: send message error when last sent message not succeeded (#8682)

This commit is contained in:
Hash Brown 2024-09-23 18:44:09 +08:00 committed by GitHub
parent c7eacd1aac
commit 11d09a92d0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 27 additions and 37 deletions

View File

@ -22,6 +22,7 @@ import {
import Avatar from '@/app/components/base/avatar' import Avatar from '@/app/components/base/avatar'
import { useAppContext } from '@/context/app-context' import { useAppContext } from '@/context/app-context'
import { ModelFeatureEnum } from '@/app/components/header/account-setting/model-provider-page/declarations' import { ModelFeatureEnum } from '@/app/components/header/account-setting/model-provider-page/declarations'
import { getLastAnswer } from '@/app/components/base/chat/utils'
type DebugWithSingleModelProps = { type DebugWithSingleModelProps = {
checkCanSend?: () => boolean checkCanSend?: () => boolean
@ -83,17 +84,11 @@ const DebugWithSingleModel = forwardRef<DebugWithSingleModelRefType, DebugWithSi
}, },
} }
const lastAnswer = chatListRef.current.at(-1)
const data: any = { const data: any = {
query: message, query: message,
inputs, inputs,
model_config: configData, model_config: configData,
parent_message_id: last_answer?.id || (lastAnswer parent_message_id: last_answer?.id || getLastAnswer(chatListRef.current)?.id || null,
? lastAnswer.isOpeningStatement
? null
: lastAnswer.id
: null),
} }
if (visionConfig.enabled && files?.length && supportVision) if (visionConfig.enabled && files?.length && supportVision)
@ -116,13 +111,13 @@ const DebugWithSingleModel = forwardRef<DebugWithSingleModelRefType, DebugWithSi
const prevMessages = chatList.slice(0, index) const prevMessages = chatList.slice(0, index)
const question = prevMessages.pop() const question = prevMessages.pop()
const lastAnswer = prevMessages.at(-1) const lastAnswer = getLastAnswer(prevMessages)
if (!question) if (!question)
return return
handleUpdateChatList(prevMessages) handleUpdateChatList(prevMessages)
doSend(question.content, question.message_files, (!lastAnswer || lastAnswer.isOpeningStatement) ? undefined : lastAnswer) doSend(question.content, question.message_files, lastAnswer)
}, [chatList, handleUpdateChatList, doSend]) }, [chatList, handleUpdateChatList, doSend])
const allToolIcons = useMemo(() => { const allToolIcons = useMemo(() => {

View File

@ -6,6 +6,7 @@ import type {
OnSend, OnSend,
} from '../types' } from '../types'
import { useChat } from '../chat/hooks' import { useChat } from '../chat/hooks'
import { getLastAnswer } from '../utils'
import { useChatWithHistoryContext } from './context' import { useChatWithHistoryContext } from './context'
import Header from './header' import Header from './header'
import ConfigPanel from './config-panel' import ConfigPanel from './config-panel'
@ -67,17 +68,11 @@ const ChatWrapper = () => {
}, []) }, [])
const doSend: OnSend = useCallback((message, files, last_answer) => { const doSend: OnSend = useCallback((message, files, last_answer) => {
const lastAnswer = chatListRef.current.at(-1)
const data: any = { const data: any = {
query: message, query: message,
inputs: currentConversationId ? currentConversationItem?.inputs : newConversationInputs, inputs: currentConversationId ? currentConversationItem?.inputs : newConversationInputs,
conversation_id: currentConversationId, conversation_id: currentConversationId,
parent_message_id: last_answer?.id || (lastAnswer parent_message_id: last_answer?.id || getLastAnswer(chatListRef.current)?.id || null,
? lastAnswer.isOpeningStatement
? null
: lastAnswer.id
: null),
} }
if (appConfig?.file_upload?.image.enabled && files?.length) if (appConfig?.file_upload?.image.enabled && files?.length)
@ -111,13 +106,13 @@ const ChatWrapper = () => {
const prevMessages = chatList.slice(0, index) const prevMessages = chatList.slice(0, index)
const question = prevMessages.pop() const question = prevMessages.pop()
const lastAnswer = prevMessages.at(-1) const lastAnswer = getLastAnswer(prevMessages)
if (!question) if (!question)
return return
handleUpdateChatList(prevMessages) handleUpdateChatList(prevMessages)
doSend(question.content, question.message_files, (!lastAnswer || lastAnswer.isOpeningStatement) ? undefined : lastAnswer) doSend(question.content, question.message_files, lastAnswer)
}, [chatList, handleUpdateChatList, doSend]) }, [chatList, handleUpdateChatList, doSend])
const chatNode = useMemo(() => { const chatNode = useMemo(() => {

View File

@ -6,6 +6,7 @@ import type {
OnSend, OnSend,
} from '../types' } from '../types'
import { useChat } from '../chat/hooks' import { useChat } from '../chat/hooks'
import { getLastAnswer } from '../utils'
import { useEmbeddedChatbotContext } from './context' import { useEmbeddedChatbotContext } from './context'
import ConfigPanel from './config-panel' import ConfigPanel from './config-panel'
import { isDify } from './utils' import { isDify } from './utils'
@ -69,17 +70,11 @@ const ChatWrapper = () => {
}, []) }, [])
const doSend: OnSend = useCallback((message, files, last_answer) => { const doSend: OnSend = useCallback((message, files, last_answer) => {
const lastAnswer = chatListRef.current.at(-1)
const data: any = { const data: any = {
query: message, query: message,
inputs: currentConversationId ? currentConversationItem?.inputs : newConversationInputs, inputs: currentConversationId ? currentConversationItem?.inputs : newConversationInputs,
conversation_id: currentConversationId, conversation_id: currentConversationId,
parent_message_id: last_answer?.id || (lastAnswer parent_message_id: last_answer?.id || getLastAnswer(chatListRef.current)?.id || null,
? lastAnswer.isOpeningStatement
? null
: lastAnswer.id
: null),
} }
if (appConfig?.file_upload?.image.enabled && files?.length) if (appConfig?.file_upload?.image.enabled && files?.length)
@ -113,13 +108,13 @@ const ChatWrapper = () => {
const prevMessages = chatList.slice(0, index) const prevMessages = chatList.slice(0, index)
const question = prevMessages.pop() const question = prevMessages.pop()
const lastAnswer = prevMessages.at(-1) const lastAnswer = getLastAnswer(prevMessages)
if (!question) if (!question)
return return
handleUpdateChatList(prevMessages) handleUpdateChatList(prevMessages)
doSend(question.content, question.message_files, (!lastAnswer || lastAnswer.isOpeningStatement) ? undefined : lastAnswer) doSend(question.content, question.message_files, lastAnswer)
}, [chatList, handleUpdateChatList, doSend]) }, [chatList, handleUpdateChatList, doSend])
const chatNode = useMemo(() => { const chatNode = useMemo(() => {

View File

@ -63,7 +63,7 @@ export type ChatItem = IChatItem & {
conversationId?: string conversationId?: string
} }
export type OnSend = (message: string, files?: VisionFile[], last_answer?: ChatItem) => void export type OnSend = (message: string, files?: VisionFile[], last_answer?: ChatItem | null) => void
export type OnRegenerate = (chatItem: ChatItem) => void export type OnRegenerate = (chatItem: ChatItem) => void

View File

@ -19,6 +19,15 @@ function getProcessedInputsFromUrlParams(): Record<string, any> {
return inputs return inputs
} }
function getLastAnswer(chatList: ChatItem[]) {
for (let i = chatList.length - 1; i >= 0; i--) {
const item = chatList[i]
if (item.isAnswer && !item.isOpeningStatement)
return item
}
return null
}
function appendQAToChatList(chatList: ChatItem[], item: any) { function appendQAToChatList(chatList: ChatItem[], item: any) {
// we append answer first and then question since will reverse the whole chatList later // we append answer first and then question since will reverse the whole chatList later
chatList.push({ chatList.push({
@ -71,5 +80,6 @@ function getPrevChatList(fetchedMessages: any[]) {
export { export {
getProcessedInputsFromUrlParams, getProcessedInputsFromUrlParams,
getLastAnswer,
getPrevChatList, getPrevChatList,
} }

View File

@ -25,6 +25,7 @@ import {
stopChatMessageResponding, stopChatMessageResponding,
} from '@/service/debug' } from '@/service/debug'
import { useStore as useAppStore } from '@/app/components/app/store' import { useStore as useAppStore } from '@/app/components/app/store'
import { getLastAnswer } from '@/app/components/base/chat/utils'
type ChatWrapperProps = { type ChatWrapperProps = {
showConversationVariableModal: boolean showConversationVariableModal: boolean
@ -76,19 +77,13 @@ const ChatWrapper = forwardRef<ChatWrapperRefType, ChatWrapperProps>(({ showConv
) )
const doSend = useCallback<OnSend>((query, files, last_answer) => { const doSend = useCallback<OnSend>((query, files, last_answer) => {
const lastAnswer = chatListRef.current.at(-1)
handleSend( handleSend(
{ {
query, query,
files, files,
inputs: workflowStore.getState().inputs, inputs: workflowStore.getState().inputs,
conversation_id: conversationId, conversation_id: conversationId,
parent_message_id: last_answer?.id || (lastAnswer parent_message_id: last_answer?.id || getLastAnswer(chatListRef.current)?.id || null,
? lastAnswer.isOpeningStatement
? null
: lastAnswer.id
: null),
}, },
{ {
onGetSuggestedQuestions: (messageId, getAbortController) => fetchSuggestedQuestions(appDetail!.id, messageId, getAbortController), onGetSuggestedQuestions: (messageId, getAbortController) => fetchSuggestedQuestions(appDetail!.id, messageId, getAbortController),
@ -103,13 +98,13 @@ const ChatWrapper = forwardRef<ChatWrapperRefType, ChatWrapperProps>(({ showConv
const prevMessages = chatList.slice(0, index) const prevMessages = chatList.slice(0, index)
const question = prevMessages.pop() const question = prevMessages.pop()
const lastAnswer = prevMessages.at(-1) const lastAnswer = getLastAnswer(prevMessages)
if (!question) if (!question)
return return
handleUpdateChatList(prevMessages) handleUpdateChatList(prevMessages)
doSend(question.content, question.message_files, (!lastAnswer || lastAnswer.isOpeningStatement) ? undefined : lastAnswer) doSend(question.content, question.message_files, lastAnswer)
}, [chatList, handleUpdateChatList, doSend]) }, [chatList, handleUpdateChatList, doSend])
useImperativeHandle(ref, () => { useImperativeHandle(ref, () => {