import sse from './request.js'; interface sendCOnfig { $source: any; $data: any; $sendPrompt: (data: any, callback?: any) => Promise; $closeSend: () => void | Promise; } export const createSend: sendCOnfig = { $source: null, $data: null, $sendPrompt(data: any, callback: any) { this.$data = data; const { prompt, // fileList, aiOptions, } = data; const msgId = `${aiOptions.botId}${new Date().getTime()}`; // const msgType = 'text'; if (this.$data) this.$data.msgId = msgId; // let _fileListFormat = []; // if (fileList && fileList.length) { // _fileListFormat = fileList.map(item => { // return { // ...item, // raw: null // }; // }) // } return new Promise(async (resolve, reject) => { try { // const QA = this.getChatQA(callbackParam); // const fileListJSONStr = await handleUpload(fileList, 'chat'); // if (fileListJSONStr) { // store.dispatch('updateMessage', { // indexes, // message: { // type: "prompt", // fileList: JSON.parse(fileListJSONStr), // done: true // } // }); // } // return; const jsonData = { prompt, roleId: aiOptions.botId, role: aiOptions.botId, }; // if (fileListJSONStr) jsonData.chatFileUrl = JSON.parse(fileListJSONStr); let currentMessage = ''; this.$source = sse({ aiOptions: aiOptions, url: aiOptions.sse.url, method: aiOptions.sse.method, data: jsonData, callback: (content, error) => { if (error) { this.$source = null; reject(error); } if (content) { if (content.code == 3001) { // let text = content.data.replaceAll('{appCode}', store.getters // .appCode) // .replaceAll('{token}', store.getters.getToken + // `&lang=${lang}`); // content.data = text; } let text = content.data || ''; if (content.end === true) { this.$source = null; if (content.code == 3001) { callback({ content: text, done: false, }); } else if (content.docs) { } else { } resolve({ result: currentMessage, done: true, }); } else { currentMessage += text; let words = currentMessage.split(''); text = words.join(''); callback({ content: text, done: false, }); } } }, }); if (callback && this.$source) callback({ start: true }); if (!this.$source) { this.$source = null; reject(); } } catch (error) { this.$source = null; reject(error); } }); }, $closeSend() { if (this.$source) { this.$source.close(); this.$source = null; } if (this.$data) { // const indexes = { // roleId: this.$data.botId, // msgId: this.$data.msgId, // }; // store.dispatch('updateMessage', { // indexes, // message: { // done: true // } // }); // store.dispatch('getChatCount', { // botId: indexes.roleId // }); } }, };