import { MessageItem, SubmitData, MESSAGE_STATUS, AI_MODELS } from '../type' import { createUserMessage, createAiMessage } from '../chat-embed-message' import axios from 'axios' import { getToken } from '../../../../src/utils/auth' export default { async sendQmsReportRequest(submitData: SubmitData, sessionId: string) { axios.defaults.headers.common.Authorization = getToken() const baseAPI = window.$vueApp.config.globalProperties.baseAPI try { const requestParams = { question: submitData.question, messageId: sessionId, systemCode: submitData.systemCode } const questionsRes = await axios.post(`${baseAPI}/component/ai/qms-report/get-questions`, requestParams) const questions = questionsRes.data if (!questions || !Array.isArray(questions) || questions.length === 0) { return { errorMessage: '未能返回有效问题列表' } } const dataRequests = questions.map((question: string) => { return axios.post(`${baseAPI}/component/ai/qms-report/get-data`, { ...requestParams, question: question }) }) const dataResponses = await Promise.all(dataRequests) const chartData = dataResponses.map(res => res.data) await axios.post(`${baseAPI}/component/ai/qms-report/save-history`, { ...requestParams, chartData: chartData }) return handleQmsReportMessage(chartData) } catch (error: any) { if (error.response?.data?.code) { return { errorMessage: error.response.data.code } } else { return { errorMessage: error.message } } } } } function encodeAttr(val: any): string { return encodeURIComponent(JSON.stringify(val)).replace(/'/g, '%27') } export function handleQmsReportMessage(response: any): any { let exportChart = false if (Array.isArray(response)) { // 如果response是数组,则遍历数组,响应多个消息 let answerMessage = '' for (let i = 0; i < response.length; i++) { const item = response[i] if (item.code === 500) { answerMessage = item.message || '请求失败' continue } if (item.answer?.sqlInfo?.sql) { delete item.answer.sqlInfo.sql } if (item.type == 'markdown') { answerMessage += item.answer + '\n' } else if (item.type === 'chartSettingList') { if (item.answer?.length > 0) { const realMessahgeList: string[] = [] item.answer.forEach((subItem: any) => { realMessahgeList.push( `` ) }) answerMessage += realMessahgeList.join('\n') + '\n' } } else if (item.type === 'sourceData') { answerMessage += `` } else if (item.type === 'chartData') { if (item.answer?.sqlInfo) { delete item.answer.sqlInfo } exportChart = true answerMessage += `
` + '
' } } return { answerMessage: answerMessage, exportType: exportChart ? 'chart' : null } } else { if (response.code === 200) { if (!response.answer) { return { errorMessage: '未能返回有效信息' } } if (response.answer?.sqlInfo?.sql) { delete response.answer.sqlInfo.sql } const message = handleSingleQmsReportMessage(response) if (message) { return message } else { return { answerMessage: '未能返回有效信息' } } } else if (response.code === 500) { return { errorMessage: response.message } } else { if (response.message) { const msg = JSON.parse(response.message) return { errorMessage: msg.message } } } } } export function handleSingleQmsReportMessage(response: any): any { if (response.type == 'markdown') { return { answerMessage: response.answer } } else if (response.type === 'chartSettingList') { if (response.answer?.length > 0) { const realMessahgeList: string[] = [] response.answer.forEach((item: any) => { realMessahgeList.push( `` ) }) return { answerMessage: realMessahgeList.join('\n') } } return { errorMessage: '未能返回有效信息' } } else if (response.type === 'sourceData') { return { answerMessage: `` } } else if (response.type === 'chartData') { if (response.answer?.sqlInfo) { delete response.answer.sqlInfo } return { answerMessage: `
` } } }