mirror of
https://github.com/langgenius/dify.git
synced 2026-05-05 18:08:07 +08:00
fix: chat generation render
This commit is contained in:
@ -12,7 +12,7 @@ import {
|
||||
} from 'react'
|
||||
import { useStore as useAppStore } from '@/app/components/app/store'
|
||||
import Chat from '@/app/components/base/chat/chat'
|
||||
import { buildChatItemTree, buildToolCallsFromHistorySequence, getThreadMessages } from '@/app/components/base/chat/utils'
|
||||
import { buildChatItemTree, buildLLMGenerationItemsFromHistorySequence, getThreadMessages } from '@/app/components/base/chat/utils'
|
||||
import { getProcessedFilesFromResponse } from '@/app/components/base/file-uploader/utils'
|
||||
import Loading from '@/app/components/base/loading'
|
||||
import { fetchConversationMessages } from '@/service/debug'
|
||||
@ -38,8 +38,8 @@ function getFormattedChatList(messages: ChatMessageRes[]) {
|
||||
const answerFiles = item.message_files?.filter((file: any) => file.belongs_to === 'assistant') || []
|
||||
res.push({
|
||||
id: item.id,
|
||||
content: buildToolCallsFromHistorySequence(item).message,
|
||||
toolCalls: buildToolCallsFromHistorySequence(item).toolCalls,
|
||||
content: buildLLMGenerationItemsFromHistorySequence(item).message,
|
||||
llmGenerationItems: buildLLMGenerationItemsFromHistorySequence(item).llmGenerationItems,
|
||||
feedback: item.feedback,
|
||||
isAnswer: true,
|
||||
citation: item.metadata?.retriever_resources,
|
||||
|
||||
@ -158,14 +158,37 @@ export function useChatMessageSender({
|
||||
}) => {
|
||||
if (!isCurrentRun())
|
||||
return
|
||||
if (chunk_type === 'text')
|
||||
if (chunk_type === 'text') {
|
||||
responseItem.content = responseItem.content + message
|
||||
|
||||
if (!responseItem.llmGenerationItems)
|
||||
responseItem.llmGenerationItems = []
|
||||
|
||||
const isNotCompletedTextItemIndex = responseItem.llmGenerationItems?.findIndex(item => item.type === 'text' && !item.textCompleted)
|
||||
|
||||
if (isNotCompletedTextItemIndex > -1) {
|
||||
responseItem.llmGenerationItems![isNotCompletedTextItemIndex].text += message
|
||||
}
|
||||
else {
|
||||
toolCallId = uuidV4()
|
||||
responseItem.llmGenerationItems?.push({
|
||||
id: toolCallId,
|
||||
type: 'text',
|
||||
text: message,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if (chunk_type === 'tool_call') {
|
||||
if (!responseItem.toolCalls)
|
||||
responseItem.toolCalls = []
|
||||
if (!responseItem.llmGenerationItems)
|
||||
responseItem.llmGenerationItems = []
|
||||
|
||||
const isNotCompletedTextItemIndex = responseItem.llmGenerationItems?.findIndex(item => item.type === 'text' && !item.textCompleted)
|
||||
if (isNotCompletedTextItemIndex > -1) {
|
||||
responseItem.llmGenerationItems![isNotCompletedTextItemIndex].textCompleted = true
|
||||
}
|
||||
toolCallId = uuidV4()
|
||||
responseItem.toolCalls?.push({
|
||||
responseItem.llmGenerationItems?.push({
|
||||
id: toolCallId,
|
||||
type: 'tool',
|
||||
toolName: tool_name,
|
||||
@ -176,21 +199,26 @@ export function useChatMessageSender({
|
||||
}
|
||||
|
||||
if (chunk_type === 'tool_result') {
|
||||
const currentToolCallIndex = responseItem.toolCalls?.findIndex(item => item.id === toolCallId) ?? -1
|
||||
const currentToolCallIndex = responseItem.llmGenerationItems?.findIndex(item => item.id === toolCallId) ?? -1
|
||||
|
||||
if (currentToolCallIndex > -1) {
|
||||
responseItem.toolCalls![currentToolCallIndex].toolError = tool_error
|
||||
responseItem.toolCalls![currentToolCallIndex].toolDuration = tool_elapsed_time
|
||||
responseItem.toolCalls![currentToolCallIndex].toolFiles = tool_files
|
||||
responseItem.toolCalls![currentToolCallIndex].toolOutput = message
|
||||
responseItem.llmGenerationItems![currentToolCallIndex].toolError = tool_error
|
||||
responseItem.llmGenerationItems![currentToolCallIndex].toolDuration = tool_elapsed_time
|
||||
responseItem.llmGenerationItems![currentToolCallIndex].toolFiles = tool_files
|
||||
responseItem.llmGenerationItems![currentToolCallIndex].toolOutput = message
|
||||
}
|
||||
}
|
||||
|
||||
if (chunk_type === 'thought_start') {
|
||||
if (!responseItem.toolCalls)
|
||||
responseItem.toolCalls = []
|
||||
if (!responseItem.llmGenerationItems)
|
||||
responseItem.llmGenerationItems = []
|
||||
|
||||
const isNotCompletedTextItemIndex = responseItem.llmGenerationItems?.findIndex(item => item.type === 'text' && !item.textCompleted)
|
||||
if (isNotCompletedTextItemIndex > -1) {
|
||||
responseItem.llmGenerationItems![isNotCompletedTextItemIndex].textCompleted = true
|
||||
}
|
||||
thoughtId = uuidV4()
|
||||
responseItem.toolCalls.push({
|
||||
responseItem.llmGenerationItems?.push({
|
||||
id: thoughtId,
|
||||
type: 'thought',
|
||||
thoughtOutput: '',
|
||||
@ -198,17 +226,17 @@ export function useChatMessageSender({
|
||||
}
|
||||
|
||||
if (chunk_type === 'thought') {
|
||||
const currentThoughtIndex = responseItem.toolCalls?.findIndex(item => item.id === thoughtId) ?? -1
|
||||
const currentThoughtIndex = responseItem.llmGenerationItems?.findIndex(item => item.id === thoughtId) ?? -1
|
||||
if (currentThoughtIndex > -1) {
|
||||
responseItem.toolCalls![currentThoughtIndex].thoughtOutput += message
|
||||
responseItem.llmGenerationItems![currentThoughtIndex].thoughtOutput += message
|
||||
}
|
||||
}
|
||||
|
||||
if (chunk_type === 'thought_end') {
|
||||
const currentThoughtIndex = responseItem.toolCalls?.findIndex(item => item.id === thoughtId) ?? -1
|
||||
const currentThoughtIndex = responseItem.llmGenerationItems?.findIndex(item => item.id === thoughtId) ?? -1
|
||||
if (currentThoughtIndex > -1) {
|
||||
responseItem.toolCalls![currentThoughtIndex].thoughtOutput += message
|
||||
responseItem.toolCalls![currentThoughtIndex].thoughtCompleted = true
|
||||
responseItem.llmGenerationItems![currentThoughtIndex].thoughtOutput += message
|
||||
responseItem.llmGenerationItems![currentThoughtIndex].thoughtCompleted = true
|
||||
}
|
||||
}
|
||||
|
||||
@ -245,6 +273,10 @@ export function useChatMessageSender({
|
||||
if (errorMessage) {
|
||||
responseItem.content = errorMessage
|
||||
responseItem.isError = true
|
||||
responseItem.llmGenerationItems?.forEach((item) => {
|
||||
if (item.type === 'text')
|
||||
item.isError = true
|
||||
})
|
||||
updateCurrentQAOnTree({
|
||||
placeholderQuestionId,
|
||||
questionItem,
|
||||
|
||||
Reference in New Issue
Block a user