import { getMessageTemplate, createAiMessage, createUserMessage } from '../chat-embed-message' import { AI_MODELS, SubmitData, MESSAGE_STATUS } from '../type' import { replaceAiTipTags, parseAnswerContent, extractFilenameFromUrl } from './chat-embed-util.ts' import { handleMcpMessage } from './mcp-utils.ts' import { handleQmsReportMessage } from './qms-report.ts' export function getBySessionId(sessionId: string): any { return window.$vueApp.config.globalProperties.$http.get( window.$vueApp.config.globalProperties.baseAPI + `/ai/ai-history/session/${sessionId}` ) } export function collectBySessionId(sessionId: string): any { return window.$vueApp.config.globalProperties.$http.put( window.$vueApp.config.globalProperties.baseAPI + `/ai/ai-history/collect/session/${sessionId}` ) } export function collectById(id: any): any { return window.$vueApp.config.globalProperties.$http.put( window.$vueApp.config.globalProperties.baseAPI + `/ai/ai-history/collect/${id}` ) } export function usefuleBySessionId(sessionId: string): any { return window.$vueApp.config.globalProperties.$http.put( window.$vueApp.config.globalProperties.baseAPI + `/ai/ai-history/useful/session/${sessionId}` ) } export function usefuleById(id: any): any { return window.$vueApp.config.globalProperties.$http.put( window.$vueApp.config.globalProperties.baseAPI + `/ai/ai-history/useful/${id}` ) } export function deleteByIds(ids: any): any { return window.$vueApp.config.globalProperties.$http.delete( window.$vueApp.config.globalProperties.baseAPI + `/ai/ai-history`, { data: ids } ) } export function formatRagflowHistoryMessage(history: any): any { const messageList: any = [] if (history) { history.forEach((item: any) => { if (item.role === 'USER') { if (item.submitParam) { const userMessage = createUserMessage(JSON.parse(item.submitParam)) delete userMessage.sendTime messageList.push(userMessage) } } else if (item.role === 'ASSISTANT') { const aiResult = JSON.parse(item.result) const aiMessage = createAiMessage({ question: '' } as SubmitData, true) delete aiMessage.sendTime const { thinkingContent, mainContent } = parseAnswerContent(aiResult.answer) if (aiResult.reference) { if (Array.isArray(aiResult.reference)) { aiMessage.referenceChunks = aiResult.reference const map = new Map() for (const chunk of aiResult.reference) { const docId = chunk.document_id const docName = chunk.document_name if (!docId) continue if (map.has(docId)) { map.get(docId).count++ } else { map.set(docId, { doc_id: docId, doc_name: docName, count: 1 }) } } aiMessage.docAggs = Array.from(map.values()) } else { if (aiResult.reference.doc_aggs?.length) { aiMessage.docAggs = aiResult.reference.doc_aggs } if (aiResult.reference.chunks?.length) { aiMessage.referenceChunks = aiResult.reference.chunks } } } if (aiResult.thinkingContent) { aiMessage.thinkingContent = aiResult.thinkingContent } else { aiMessage.thinkingContent = thinkingContent } aiMessage.content = mainContent aiMessage.thinkingStatus = MESSAGE_STATUS.END // 默认不展开思考内容 aiMessage.showThinkingContent = false aiMessage.content = replaceAiTipTags(aiMessage.content) aiMessage.messageType = 'history' messageList.push(aiMessage) } }) } return messageList } export function formatDatabaseHistoryMessage(history: any): any { const messageList: any = [] if (history) { history.forEach((item: any) => { if (item.role === 'USER') { if (item.submitParam) { const userMessage = createUserMessage(item.submitParam) delete userMessage.sendTime messageList.push(userMessage) } } else if (item.role === 'ASSISTANT') { // 创建AI响应消息 const aiMessage = createAiMessage({ question: '' } as SubmitData, true) delete aiMessage.sendTime const { answerMessage } = handleMcpMessage(item.result) aiMessage.content = answerMessage aiMessage.thinkingStatus = MESSAGE_STATUS.END aiMessage.showThinkingContent = false aiMessage.messageType = 'history' messageList.push(aiMessage) } }) } return messageList } export function formatBailianHistoryMessage(history: any): any { const messageList: any = [] if (history) { history.forEach((item: any) => { if (item.role === 'USER') { if (item.submitParam) { const userMessage = createUserMessage(item.submitParam) delete userMessage.sendTime messageList.push(userMessage) } } else if (item.role === 'ASSISTANT') { // 创建AI响应消息 const aiMessage = createAiMessage({ question: '' } as SubmitData, true) delete aiMessage.sendTime // const aiResult = JSON.parse(item.result) // const { thinkingContent, mainContent } = parseAnswerContent(aiResult.answer) aiMessage.content = item.result // aiMessage.thinkingContent = thinkingContent aiMessage.thinkingContent = '' aiMessage.thinkingStatus = MESSAGE_STATUS.END // aiMessage.showThinkingContent = true aiMessage.showThinkingContent = false aiMessage.messageType = 'history' messageList.push(aiMessage) } }) } return messageList } export function formatAiPptHistoryMessage(history: any): any { const messageList: any = [] if (history) { history.forEach((item: any) => { if (item.role === 'USER') { if (item.submitParam) { const userMessage = createUserMessage(item.submitParam) delete userMessage.sendTime messageList.push(userMessage) } } else if (item.role === 'ASSISTANT') { // 创建AI响应消息 const aiMessage = createAiMessage({ question: '' } as SubmitData, true) delete aiMessage.sendTime // const aiResult = JSON.parse(item.result) // const { thinkingContent, mainContent } = parseAnswerContent(aiResult.answer) if (item.result.fileUrl) { const fileName = extractFilenameFromUrl(item.result.fileUrl) || '点击下载' aiMessage.content = `PPT已生成:[${fileName}](${item.result.fileUrl})` } else { aiMessage.content = 'PPT生成失败,请稍后重试' } // aiMessage.thinkingContent = thinkingContent aiMessage.thinkingContent = '' aiMessage.thinkingStatus = MESSAGE_STATUS.END // aiMessage.showThinkingContent = true aiMessage.showThinkingContent = false aiMessage.messageType = 'history' messageList.push(aiMessage) } }) } return messageList } export function formatReportQmsHistoryMessage(history: any): any { const messageList: any = [] if (history) { history.forEach((item: any) => { if (item.role === 'USER') { if (item.submitParam) { const userMessage = createUserMessage(item.submitParam) delete userMessage.sendTime messageList.push(userMessage) } } else if (item.role === 'ASSISTANT') { // 创建AI响应消息 const aiMessage = createAiMessage({ question: '' } as SubmitData, true) delete aiMessage.sendTime const { answerMessage, exportType } = handleQmsReportMessage(item.result) aiMessage.exportType = exportType aiMessage.content = answerMessage aiMessage.thinkingStatus = MESSAGE_STATUS.END aiMessage.showThinkingContent = false aiMessage.messageType = 'history' messageList.push(aiMessage) } }) } return messageList }