123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- // 消息数组
- import { ref, reactive } from 'vue';
- import avator from '@/public/icon/32.png';
- import moment from 'moment'
- export function useMsg(scrollbar: any, type: any, xlsxData: any, fetchDataAndProcess: Function) {
- const inputMessage = ref('');
- const indexTemp = ref(0);
- const taklToHtml = ref<any>(false);
- const sendLoading = ref(false);
- const pageInfo = ref<any>({});
- const formMap = ref([]);
- const messages = ref([{
- username: '用户1',
- content: '你好!有什么我可以帮助你的吗?',
- rawContent: '你好!有什么我可以帮助你的吗?',
- timestamp: moment().format('YYYY-MM-DD HH:mm:ss'),
- isSelf: false,
- avatar: avator
- }]);
- // 发送消息
- 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('/')) {
- 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)
- if (type.value === '2') {
- 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)
- // 处理最终内容
- if (type.value === '2') {
- try {
- formMap.value = JSON.parse(obj.rawContent.split('json')[1].split('```')[0])
- handleInput()
- } catch (e) {
- console.error('解析JSON失败:', e)
- }
- }
- sendLoading.value = false
- nextTick(() => {
- scrollbar.value?.setScrollTop(99999)
- })
- }
- const handleInput = () => {
- const arr = xlsxData.value;
- chrome.runtime.sendMessage({
- type: 'FROM_SIDE_PANEL_TO_INPUT_FORM',
- data: {
- excelData: arr,
- formData: formMap.value
- }
- });
- }
- return {
- messages,
- inputMessage,
- indexTemp,
- taklToHtml,
- pageInfo,
- sendLoading,
- formMap,
- addMessage,
- sendRequese,
- getPageInfo,
- streamRes,
- handleInput
- }
- }
|