Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

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

Hash Brown11d09a92d0cd
diff --git a/web/app/components/app/configuration/debug/debug-with-single-model/index.tsx b/web/app/components/app/configuration/debug/debug-with-single-model/index.tsx
index 5faef46d988..2de500a3a62 100644
--- a/web/app/components/app/configuration/debug/debug-with-single-model/index.tsx
+++ b/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(() => {
diff --git a/web/app/components/base/chat/chat-with-history/chat-wrapper.tsx b/web/app/components/base/chat/chat-with-history/chat-wrapper.tsx
index 225bbac7140..9d7b360f38d 100644
--- a/web/app/components/base/chat/chat-with-history/chat-wrapper.tsx
+++ b/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(() => {
diff --git a/web/app/components/base/chat/embedded-chatbot/chat-wrapper.tsx b/web/app/components/base/chat/embedded-chatbot/chat-wrapper.tsx
index ed2f24274dc..b97c940eece 100644
--- a/web/app/components/base/chat/embedded-chatbot/chat-wrapper.tsx
+++ b/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(() => {
diff --git a/web/app/components/base/chat/types.ts b/web/app/components/base/chat/types.ts
index 489dbb44cf6..0bc50518ebb 100644
--- a/web/app/components/base/chat/types.ts
+++ b/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
diff --git a/web/app/components/base/chat/utils.ts b/web/app/components/base/chat/utils.ts
index e851c4c4633..305df5995d1 100644
--- a/web/app/components/base/chat/utils.ts
+++ b/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,
}
diff --git a/web/app/components/workflow/panel/debug-and-preview/chat-wrapper.tsx b/web/app/components/workflow/panel/debug-and-preview/chat-wrapper.tsx
index 86519af6035..230b2d7fa0c 100644
--- a/web/app/components/workflow/panel/debug-and-preview/chat-wrapper.tsx
+++ b/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, () => {