فهرست منبع

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

Hash Brown 7 ماه پیش
والد
کامیت
11d09a92d0

+ 4 - 9
web/app/components/app/configuration/debug/debug-with-single-model/index.tsx

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

+ 4 - 9
web/app/components/base/chat/chat-with-history/chat-wrapper.tsx

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

+ 4 - 9
web/app/components/base/chat/embedded-chatbot/chat-wrapper.tsx

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

+ 1 - 1
web/app/components/base/chat/types.ts

@@ -63,7 +63,7 @@ export type ChatItem = IChatItem & {
   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
 

+ 10 - 0
web/app/components/base/chat/utils.ts

@@ -19,6 +19,15 @@ function getProcessedInputsFromUrlParams(): Record<string, any> {
   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) {
   // we append answer first and then question since will reverse the whole chatList later
   chatList.push({
@@ -71,5 +80,6 @@ function getPrevChatList(fetchedMessages: any[]) {
 
 export {
   getProcessedInputsFromUrlParams,
+  getLastAnswer,
   getPrevChatList,
 }

+ 4 - 9
web/app/components/workflow/panel/debug-and-preview/chat-wrapper.tsx

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