|
@@ -1,187 +1,187 @@
|
|
|
// 消息数组
|
|
|
-import { ref, reactive } from 'vue';
|
|
|
+import {ref, reactive, nextTick, inject} from 'vue';
|
|
|
import avator from '@/public/icon/32.png';
|
|
|
import moment from 'moment'
|
|
|
+
|
|
|
// 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<any>(false);
|
|
|
- const sendLoading = ref(false);
|
|
|
- const pageInfo = ref<any>({});
|
|
|
- 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
|
|
|
- }]);
|
|
|
+export function useMsg(scrollbar?: any, xlsxData?: any, fetchDataAndProcess?: Function) {
|
|
|
+ const msgUuid = ref<string>();
|
|
|
+ const inputMessage = ref('');
|
|
|
+ const indexTemp = ref(0);
|
|
|
+ const taklToHtml = ref<any>(false);
|
|
|
+ const sendLoading = ref(false);
|
|
|
+ const pageInfo = ref<any>({});
|
|
|
+ const type = ref('');
|
|
|
+ const formMap = ref([]);
|
|
|
+ const messages = ref<any>([]);
|
|
|
+ // 获取父组件提供的 Hook 实例
|
|
|
+ const {useStore} = inject('indexedDBHook') as any;
|
|
|
+
|
|
|
+ // 发送消息
|
|
|
+ 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)
|
|
|
+ useStore(msgUuid.value).add(newMessage)
|
|
|
|
|
|
- // 发送消息
|
|
|
- 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
|
|
|
+ // 滚动到底部
|
|
|
+ nextTick(() => {
|
|
|
+ scrollbar.value?.setScrollTop(99999);
|
|
|
+ fetch && sendRequese(msg, 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 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)
|
|
|
+ const sendRequese = async (msg: any, addHtml = false) => {
|
|
|
+ const res: any = await getPageInfo()
|
|
|
+ if (type.value === '2' && msg.startsWith('/')) {
|
|
|
+ indexTemp.value = 0
|
|
|
+ await fetchRes(msg)
|
|
|
} else {
|
|
|
- pageInfo.value = response.data
|
|
|
- res(response.data)
|
|
|
+ if (!addHtml) msg = getSummaryPrompt(res.content)
|
|
|
+ await streamRes(msg, addHtml)
|
|
|
}
|
|
|
- });
|
|
|
- })
|
|
|
- }
|
|
|
-
|
|
|
- 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)
|
|
|
- scrollbar.value?.setScrollTop(99999);
|
|
|
- if (type.value === '2') {
|
|
|
- await fetchDataAndProcess(msg, obj)
|
|
|
- sendLoading.value = false
|
|
|
+ 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)
|
|
|
+ scrollbar.value?.setScrollTop(99999);
|
|
|
+ if (type.value === '2') {
|
|
|
+ fetchDataAndProcess && await fetchDataAndProcess(msg, obj)
|
|
|
+ sendLoading.value = false
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
+ const streamRes = async (msg: any, addHtml: any) => {
|
|
|
+ sendLoading.value = true;
|
|
|
+ const obj = reactive<any>({
|
|
|
+ id:messages.value.length + 1,
|
|
|
+ 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
|
|
|
+ }))
|
|
|
+ }
|
|
|
|
|
|
- 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}`
|
|
|
+ messages.value.push(obj)
|
|
|
+ nextTick(() => {
|
|
|
+ scrollbar.value?.setScrollTop(99999)
|
|
|
})
|
|
|
- }
|
|
|
- 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
|
|
|
- }))
|
|
|
- }
|
|
|
+ const iterator = await sendMessage(history, addHtml)
|
|
|
|
|
|
- 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)
|
|
|
+ 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)
|
|
|
- }
|
|
|
- scrollbar.value?.setScrollTop(99999)
|
|
|
+ scrollbar.value?.setScrollTop(99999)
|
|
|
+ //添加到存储历史
|
|
|
+ useStore(msgUuid.value).add({...obj})
|
|
|
|
|
|
- // 处理最终内容
|
|
|
- if (type.value === '2') {
|
|
|
- try {
|
|
|
- formMap.value = JSON.parse(obj.rawContent.split('json')[1].split('```')[0])
|
|
|
+ // 处理最终内容
|
|
|
+ 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)
|
|
|
- }
|
|
|
+ handleInput()
|
|
|
+ type.value = ''
|
|
|
+ } catch (e) {
|
|
|
+ console.error('解析JSON失败:', e)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ sendLoading.value = false
|
|
|
+ nextTick(() => {
|
|
|
+ scrollbar.value?.setScrollTop(99999)
|
|
|
+ })
|
|
|
}
|
|
|
- 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
|
|
|
- }
|
|
|
- });
|
|
|
- }
|
|
|
+ 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,
|
|
|
- type,
|
|
|
- addMessage,
|
|
|
- sendRequese,
|
|
|
- getPageInfo,
|
|
|
- streamRes,
|
|
|
- handleInput
|
|
|
- }
|
|
|
+ return {
|
|
|
+ msgUuid,
|
|
|
+ messages,
|
|
|
+ inputMessage,
|
|
|
+ indexTemp,
|
|
|
+ taklToHtml,
|
|
|
+ pageInfo,
|
|
|
+ sendLoading,
|
|
|
+ formMap,
|
|
|
+ type,
|
|
|
+ addMessage,
|
|
|
+ sendRequese,
|
|
|
+ getPageInfo,
|
|
|
+ streamRes,
|
|
|
+ handleInput
|
|
|
+ }
|
|
|
}
|