| import produce from 'immer' | import produce from 'immer' | ||||
| import type { | import type { | ||||
| ChatConfig, | ChatConfig, | ||||
| ChatItem, | |||||
| Feedback, | Feedback, | ||||
| } from '../types' | } from '../types' | ||||
| import { CONVERSATION_ID_INFO } from '../constants' | import { CONVERSATION_ID_INFO } from '../constants' | ||||
| import { getPrevChatList, getProcessedInputsFromUrlParams } from '../utils' | |||||
| import { buildChatItemTree, getProcessedInputsFromUrlParams } from '../utils' | |||||
| import { getProcessedFilesFromResponse } from '../../file-uploader/utils' | |||||
| import { | import { | ||||
| fetchAppInfo, | fetchAppInfo, | ||||
| fetchAppMeta, | fetchAppMeta, | ||||
| import { changeLanguage } from '@/i18n/i18next-config' | import { changeLanguage } from '@/i18n/i18next-config' | ||||
| import { InputVarType } from '@/app/components/workflow/types' | import { InputVarType } from '@/app/components/workflow/types' | ||||
| import { TransferMethod } from '@/types/app' | import { TransferMethod } from '@/types/app' | ||||
| import { addFileInfos, sortAgentSorts } from '@/app/components/tools/utils' | |||||
| function getFormattedChatList(messages: any[]) { | |||||
| const newChatList: ChatItem[] = [] | |||||
| messages.forEach((item) => { | |||||
| const questionFiles = item.message_files?.filter((file: any) => file.belongs_to === 'user') || [] | |||||
| newChatList.push({ | |||||
| id: `question-${item.id}`, | |||||
| content: item.query, | |||||
| isAnswer: false, | |||||
| message_files: getProcessedFilesFromResponse(questionFiles.map((item: any) => ({ ...item, related_id: item.id }))), | |||||
| parentMessageId: item.parent_message_id || undefined, | |||||
| }) | |||||
| 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.feedback, | |||||
| isAnswer: true, | |||||
| citation: item.retriever_resources, | |||||
| message_files: getProcessedFilesFromResponse(answerFiles.map((item: any) => ({ ...item, related_id: item.id }))), | |||||
| parentMessageId: `question-${item.id}`, | |||||
| }) | |||||
| }) | |||||
| return newChatList | |||||
| } | |||||
| export const useEmbeddedChatbot = () => { | export const useEmbeddedChatbot = () => { | ||||
| const isInstalledApp = false | const isInstalledApp = false | ||||
| const appPrevChatList = useMemo( | const appPrevChatList = useMemo( | ||||
| () => (currentConversationId && appChatListData?.data.length) | () => (currentConversationId && appChatListData?.data.length) | ||||
| ? getPrevChatList(appChatListData.data) | |||||
| ? buildChatItemTree(getFormattedChatList(appChatListData.data)) | |||||
| : [], | : [], | ||||
| [appChatListData, currentConversationId], | [appChatListData, currentConversationId], | ||||
| ) | ) |