fix: chat log not showing correctly (#9777)
This commit is contained in:
parent
b674c598f9
commit
fc63841169
@ -16,7 +16,6 @@ import timezone from 'dayjs/plugin/timezone'
|
|||||||
import { createContext, useContext } from 'use-context-selector'
|
import { createContext, useContext } from 'use-context-selector'
|
||||||
import { useShallow } from 'zustand/react/shallow'
|
import { useShallow } from 'zustand/react/shallow'
|
||||||
import { useTranslation } from 'react-i18next'
|
import { useTranslation } from 'react-i18next'
|
||||||
import { UUID_NIL } from '../../base/chat/constants'
|
|
||||||
import type { ChatItemInTree } from '../../base/chat/types'
|
import type { ChatItemInTree } from '../../base/chat/types'
|
||||||
import VarPanel from './var-panel'
|
import VarPanel from './var-panel'
|
||||||
import cn from '@/utils/classnames'
|
import cn from '@/utils/classnames'
|
||||||
@ -84,95 +83,76 @@ const PARAM_MAP = {
|
|||||||
frequency_penalty: 'Frequency Penalty',
|
frequency_penalty: 'Frequency Penalty',
|
||||||
}
|
}
|
||||||
|
|
||||||
function appendQAToChatList(newChatList: IChatItem[], item: any, conversationId: string, timezone: string, format: string) {
|
|
||||||
const answerFiles = item.message_files?.filter((file: any) => file.belongs_to === 'assistant') || []
|
|
||||||
newChatList.push({
|
|
||||||
id: item.id,
|
|
||||||
content: item.answer,
|
|
||||||
agent_thoughts: addFileInfos(item.agent_thoughts ? sortAgentSorts(item.agent_thoughts) : item.agent_thoughts, item.message_files),
|
|
||||||
feedback: item.feedbacks.find((item: any) => item.from_source === 'user'), // user feedback
|
|
||||||
adminFeedback: item.feedbacks.find((item: any) => item.from_source === 'admin'), // admin feedback
|
|
||||||
feedbackDisabled: false,
|
|
||||||
isAnswer: true,
|
|
||||||
message_files: getProcessedFilesFromResponse(answerFiles.map((item: any) => ({ ...item, related_id: item.id }))),
|
|
||||||
log: [
|
|
||||||
...item.message,
|
|
||||||
...(item.message[item.message.length - 1]?.role !== 'assistant'
|
|
||||||
? [
|
|
||||||
{
|
|
||||||
role: 'assistant',
|
|
||||||
text: item.answer,
|
|
||||||
files: item.message_files?.filter((file: any) => file.belongs_to === 'assistant') || [],
|
|
||||||
},
|
|
||||||
]
|
|
||||||
: []),
|
|
||||||
],
|
|
||||||
workflow_run_id: item.workflow_run_id,
|
|
||||||
conversationId,
|
|
||||||
input: {
|
|
||||||
inputs: item.inputs,
|
|
||||||
query: item.query,
|
|
||||||
},
|
|
||||||
more: {
|
|
||||||
time: dayjs.unix(item.created_at).tz(timezone).format(format),
|
|
||||||
tokens: item.answer_tokens + item.message_tokens,
|
|
||||||
latency: item.provider_response_latency.toFixed(2),
|
|
||||||
},
|
|
||||||
citation: item.metadata?.retriever_resources,
|
|
||||||
annotation: (() => {
|
|
||||||
if (item.annotation_hit_history) {
|
|
||||||
return {
|
|
||||||
id: item.annotation_hit_history.annotation_id,
|
|
||||||
authorName: item.annotation_hit_history.annotation_create_account?.name || 'N/A',
|
|
||||||
created_at: item.annotation_hit_history.created_at,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (item.annotation) {
|
|
||||||
return {
|
|
||||||
id: item.annotation.id,
|
|
||||||
authorName: item.annotation.account.name,
|
|
||||||
logAnnotation: item.annotation,
|
|
||||||
created_at: 0,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return undefined
|
|
||||||
})(),
|
|
||||||
parentMessageId: `question-${item.id}`,
|
|
||||||
})
|
|
||||||
|
|
||||||
const questionFiles = item.message_files?.filter((file: any) => file.belongs_to === 'user') || []
|
|
||||||
newChatList.push({
|
|
||||||
id: `question-${item.id}`,
|
|
||||||
content: item.inputs.query || item.inputs.default_input || item.query, // text generation: item.inputs.query; chat: item.query
|
|
||||||
isAnswer: false,
|
|
||||||
message_files: getProcessedFilesFromResponse(questionFiles.map((item: any) => ({ ...item, related_id: item.id }))),
|
|
||||||
parentMessageId: item.parent_message_id || undefined,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
const getFormattedChatList = (messages: ChatMessage[], conversationId: string, timezone: string, format: string) => {
|
const getFormattedChatList = (messages: ChatMessage[], conversationId: string, timezone: string, format: string) => {
|
||||||
const newChatList: IChatItem[] = []
|
const newChatList: IChatItem[] = []
|
||||||
let nextMessageId = null
|
messages.forEach((item: ChatMessage) => {
|
||||||
for (const item of messages) {
|
const questionFiles = item.message_files?.filter((file: any) => file.belongs_to === 'user') || []
|
||||||
if (!item.parent_message_id) {
|
newChatList.push({
|
||||||
appendQAToChatList(newChatList, item, conversationId, timezone, format)
|
id: `question-${item.id}`,
|
||||||
break
|
content: item.inputs.query || item.inputs.default_input || item.query, // text generation: item.inputs.query; chat: item.query
|
||||||
}
|
isAnswer: false,
|
||||||
|
message_files: getProcessedFilesFromResponse(questionFiles.map((item: any) => ({ ...item, related_id: item.id }))),
|
||||||
|
parentMessageId: item.parent_message_id || undefined,
|
||||||
|
})
|
||||||
|
|
||||||
if (!nextMessageId) {
|
const answerFiles = item.message_files?.filter((file: any) => file.belongs_to === 'assistant') || []
|
||||||
appendQAToChatList(newChatList, item, conversationId, timezone, format)
|
newChatList.push({
|
||||||
nextMessageId = item.parent_message_id
|
id: item.id,
|
||||||
}
|
content: item.answer,
|
||||||
else {
|
agent_thoughts: addFileInfos(item.agent_thoughts ? sortAgentSorts(item.agent_thoughts) : item.agent_thoughts, item.message_files),
|
||||||
if (item.id === nextMessageId || nextMessageId === UUID_NIL) {
|
feedback: item.feedbacks.find(item => item.from_source === 'user'), // user feedback
|
||||||
appendQAToChatList(newChatList, item, conversationId, timezone, format)
|
adminFeedback: item.feedbacks.find(item => item.from_source === 'admin'), // admin feedback
|
||||||
nextMessageId = item.parent_message_id
|
feedbackDisabled: false,
|
||||||
}
|
isAnswer: true,
|
||||||
}
|
message_files: getProcessedFilesFromResponse(answerFiles.map((item: any) => ({ ...item, related_id: item.id }))),
|
||||||
}
|
log: [
|
||||||
return newChatList.reverse()
|
...item.message,
|
||||||
|
...(item.message[item.message.length - 1]?.role !== 'assistant'
|
||||||
|
? [
|
||||||
|
{
|
||||||
|
role: 'assistant',
|
||||||
|
text: item.answer,
|
||||||
|
files: item.message_files?.filter((file: any) => file.belongs_to === 'assistant') || [],
|
||||||
|
},
|
||||||
|
]
|
||||||
|
: []),
|
||||||
|
] as IChatItem['log'],
|
||||||
|
workflow_run_id: item.workflow_run_id,
|
||||||
|
conversationId,
|
||||||
|
input: {
|
||||||
|
inputs: item.inputs,
|
||||||
|
query: item.query,
|
||||||
|
},
|
||||||
|
more: {
|
||||||
|
time: dayjs.unix(item.created_at).tz(timezone).format(format),
|
||||||
|
tokens: item.answer_tokens + item.message_tokens,
|
||||||
|
latency: item.provider_response_latency.toFixed(2),
|
||||||
|
},
|
||||||
|
citation: item.metadata?.retriever_resources,
|
||||||
|
annotation: (() => {
|
||||||
|
if (item.annotation_hit_history) {
|
||||||
|
return {
|
||||||
|
id: item.annotation_hit_history.annotation_id,
|
||||||
|
authorName: item.annotation_hit_history.annotation_create_account?.name || 'N/A',
|
||||||
|
created_at: item.annotation_hit_history.created_at,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (item.annotation) {
|
||||||
|
return {
|
||||||
|
id: item.annotation.id,
|
||||||
|
authorName: item.annotation.account.name,
|
||||||
|
logAnnotation: item.annotation,
|
||||||
|
created_at: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return undefined
|
||||||
|
})(),
|
||||||
|
parentMessageId: `question-${item.id}`,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
return newChatList
|
||||||
}
|
}
|
||||||
|
|
||||||
// const displayedParams = CompletionParams.slice(0, -2)
|
// const displayedParams = CompletionParams.slice(0, -2)
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import type { ThoughtItem } from '@/app/components/base/chat/chat/type'
|
import type { ThoughtItem } from '@/app/components/base/chat/chat/type'
|
||||||
import type { FileEntity } from '@/app/components/base/file-uploader/types'
|
import type { FileEntity } from '@/app/components/base/file-uploader/types'
|
||||||
|
import type { VisionFile } from '@/types/app'
|
||||||
|
|
||||||
export const sortAgentSorts = (list: ThoughtItem[]) => {
|
export const sortAgentSorts = (list: ThoughtItem[]) => {
|
||||||
if (!list)
|
if (!list)
|
||||||
@ -11,7 +12,7 @@ export const sortAgentSorts = (list: ThoughtItem[]) => {
|
|||||||
return temp
|
return temp
|
||||||
}
|
}
|
||||||
|
|
||||||
export const addFileInfos = (list: ThoughtItem[], messageFiles: FileEntity[]) => {
|
export const addFileInfos = (list: ThoughtItem[], messageFiles: (FileEntity | VisionFile)[]) => {
|
||||||
if (!list || !messageFiles)
|
if (!list || !messageFiles)
|
||||||
return list
|
return list
|
||||||
return list.map((item) => {
|
return list.map((item) => {
|
||||||
|
Loading…
Reference in New Issue
Block a user