|
|
|
|
@ -1,6 +1,8 @@
|
|
|
|
|
import { UUID_NIL } from './constants'
|
|
|
|
|
import type { IChatItem } from './chat/type'
|
|
|
|
|
import type { ChatItem, ChatItemInTree } from './types'
|
|
|
|
|
import { addFileInfos, sortAgentSorts } from '../../tools/utils'
|
|
|
|
|
import { getProcessedFilesFromResponse } from '../file-uploader/utils'
|
|
|
|
|
|
|
|
|
|
async function decodeBase64AndDecompress(base64String: string) {
|
|
|
|
|
const binaryString = atob(base64String)
|
|
|
|
|
@ -19,6 +21,60 @@ function getProcessedInputsFromUrlParams(): Record<string, any> {
|
|
|
|
|
return inputs
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function appendQAToChatList(chatList: ChatItem[], item: any) {
|
|
|
|
|
// we append answer first and then question since will reverse the whole chatList later
|
|
|
|
|
const answerFiles = item.message_files?.filter((file: any) => file.belongs_to === 'assistant') || []
|
|
|
|
|
chatList.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 }))),
|
|
|
|
|
})
|
|
|
|
|
const questionFiles = item.message_files?.filter((file: any) => file.belongs_to === 'user') || []
|
|
|
|
|
chatList.push({
|
|
|
|
|
id: `question-${item.id}`,
|
|
|
|
|
content: item.query,
|
|
|
|
|
isAnswer: false,
|
|
|
|
|
message_files: getProcessedFilesFromResponse(questionFiles.map((item: any) => ({ ...item, related_id: item.id }))),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Computes the latest thread messages from all messages of the conversation.
|
|
|
|
|
* Same logic as backend codebase `api/core/prompt/utils/extract_thread_messages.py`
|
|
|
|
|
*
|
|
|
|
|
* @param fetchedMessages - The history chat list data from the backend, sorted by created_at in descending order. This includes all flattened history messages of the conversation.
|
|
|
|
|
* @returns An array of ChatItems representing the latest thread.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function getPrevChatList(fetchedMessages: any[]) {
|
|
|
|
|
const ret: ChatItem[] = []
|
|
|
|
|
let nextMessageId = null
|
|
|
|
|
|
|
|
|
|
for (const item of fetchedMessages) {
|
|
|
|
|
if (!item.parent_message_id) {
|
|
|
|
|
appendQAToChatList(ret, item)
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!nextMessageId) {
|
|
|
|
|
appendQAToChatList(ret, item)
|
|
|
|
|
nextMessageId = item.parent_message_id
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
if (item.id === nextMessageId || nextMessageId === UUID_NIL) {
|
|
|
|
|
appendQAToChatList(ret, item)
|
|
|
|
|
nextMessageId = item.parent_message_id
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ret.reverse()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isValidGeneratedAnswer(item?: ChatItem | ChatItemInTree): boolean {
|
|
|
|
|
return !!item && item.isAnswer && !item.id.startsWith('answer-placeholder-') && !item.isOpeningStatement
|
|
|
|
|
}
|
|
|
|
|
@ -164,6 +220,7 @@ function getThreadMessages(tree: ChatItemInTree[], targetMessageId?: string): Ch
|
|
|
|
|
export {
|
|
|
|
|
getProcessedInputsFromUrlParams,
|
|
|
|
|
isValidGeneratedAnswer,
|
|
|
|
|
getPrevChatList,
|
|
|
|
|
getLastAnswer,
|
|
|
|
|
buildChatItemTree,
|
|
|
|
|
getThreadMessages,
|
|
|
|
|
|