// 消息数组 import { ref, reactive } from 'vue'; import avator from '@/public/icon/32.png'; import moment from 'moment' import { getFileContent, getFormKey, buildObjPrompt ,getFileSummaryPrompt} from '@/utils/ai-service' import { ElMessage } from 'element-plus'; // import { sendMessage } from '@/utils/ai-service'; export function useMsg(scrollbar: any, xlsxData: any, fetchDataAndProcess: Function) { const inputMessage = ref(''); const indexTemp = ref(0); const taklToHtml = ref(false); const sendLoading = ref(false); const pageInfo = ref({}); const type = ref(''); const formMap = ref([]); const messages = ref([{ username: '用户1', content: '你好!有什么我可以帮助你的吗?', rawContent: '你好!有什么我可以帮助你的吗?', timestamp: moment().format('YYYY-MM-DD HH:mm:ss'), isSelf: false, avatar: avator, addToHistory: false }]); const getFileValue = async (file: any, form: any) => { const obj = reactive({ id: moment(), username: '用户1', rawContent: '', content: '解析文件中', timestamp: moment().format('YYYY-MM-DD HH:mm:ss'), isSelf: false, avatar: avator, addToHistory: !taklToHtml.value }) try { sendLoading.value = true messages.value.push(obj) nextTick(() => scrollbar.value?.setScrollTop(99999)) let formData = new FormData(); formData.append("file", file); const res = await getFileContent(formData) if (type.value === '2') { const response = await getFormKey({ body: form, input_data: res.data }) xlsxData.value = response.data console.log(xlsxData.value); console.log(type.value); await streamRes(buildObjPrompt(response.data, form), false) } console.log(res.data,file); if (type.value === '') { console.log(getFileSummaryPrompt(res.data, file.name)); await streamRes(getFileSummaryPrompt(res.data, file.name), false) } } catch (error) { console.log(error); obj.content = '解析出错' } finally { } } // 发送消息 const addMessage = (msg: any, fetch: any) => { if (!msg) return const newMessage: any = { id: messages.value.length + 1, username: '我', content: msg, timestamp: moment().format('YYYY-MM-DD HH:mm:ss'), isSelf: true, avatar: 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png', addToHistory: !taklToHtml.value } messages.value.push(newMessage) // 滚动到底部 nextTick(() => { scrollbar.value?.setScrollTop(99999); fetch && sendRequese(msg, taklToHtml.value); }) } const getPageInfo = () => { return new Promise((res, rej) => { chrome.runtime.sendMessage({ type: 'FROM_SIDE_PANEL_TO_GET_PAGE_INFO', }, async (response) => { if (chrome.runtime.lastError) { console.error("消息发送错误:", chrome.runtime.lastError); rej(chrome.runtime.lastError) } else { pageInfo.value = response.data res(response.data) } }); }) } const sendRequese = async (msg: any, addHtml = false) => { const res: any = await getPageInfo() if ((type.value === '2' && msg.startsWith('/')) || msg.startsWith('请')) { if (!taklToHtml.value) return messages.value.push({ username: '用户1', content: '请打开与页面对话!', rawContent: '请打开与页面对话!', timestamp: moment().format('YYYY-MM-DD HH:mm:ss'), isSelf: false, avatar: avator, addToHistory: false }) indexTemp.value = 0 fetchRes(msg) } else { if (!addHtml) msg = getSummaryPrompt(res.content) streamRes(msg, addHtml) } } const fetchRes = async (msg: any) => { sendLoading.value = true const obj: any = reactive({ id: moment(), username: '用户1', content: '', timestamp: moment().format('YYYY-MM-DD HH:mm:ss'), isSelf: false, avatar: avator, addToHistory: !taklToHtml.value }) messages.value.push(obj) nextTick(() => scrollbar.value?.setScrollTop(99999)) await fetchDataAndProcess(msg, obj) sendLoading.value = false } const streamRes = async (msg: any, addHtml: any) => { sendLoading.value = true; const obj = reactive({ username: '用户1', content: '', rawContent: '', // 存储原始内容 timestamp: moment().format('YYYY-MM-DD HH:mm:ss'), isSelf: false, avatar: avator, addToHistory: !taklToHtml.value }); let history = [] if (taklToHtml.value) { if (addHtml) { history.push({ role: 'user', content: `页面主要内容${pageInfo.value.content.mainContent}` }) } history.push({ role: 'user', content: msg }) } else { history = messages.value .filter((item: any) => item.addToHistory).slice(-20) .map((item: any) => ({ role: item.isSelf ? 'user' : 'system', content: item.isSelf ? item.content : item.rawContent })) } messages.value.push(obj) nextTick(() => { scrollbar.value?.setScrollTop(99999) }) const iterator = await sendMessage(history, addHtml) for await (const chunk of iterator) { if (chunk) { const decodedChunk = chunk.choices[0].delta.content; if (decodedChunk) { // 保存原始内容 obj.rawContent += decodedChunk // 实时格式化显示内容 obj.content = formatMessage(obj.rawContent) } } scrollbar.value?.setScrollTop(99999) } scrollbar.value?.setScrollTop(99999) console.log(232344); // 处理最终内容 if (type.value === '2') { try { formMap.value = JSON.parse(obj.rawContent.split('json')[1].split('```')[0]) handleInput() type.value = '' } catch (e) { console.error('解析JSON失败:', e) } } type.value = '' sendLoading.value = false nextTick(() => { scrollbar.value?.setScrollTop(99999) }) } const handleInput = () => { chrome.runtime.sendMessage({ type: 'FROM_SIDE_PANEL_TO_INPUT_FORM', data: { excelData: xlsxData.value, formData: formMap.value } }); } return { getFileValue, messages, inputMessage, indexTemp, taklToHtml, pageInfo, sendLoading, formMap, type, addMessage, sendRequese, getPageInfo, streamRes, handleInput } }