const api_key = 'app-NvKwdHvEK2UmJdmjTGDR0xu6' const url = 'http://192.168.2.24/v1' export default { data() { return { controller:null, } }, computed:{ userinfo(){ return this.$s.getObj('userinfo') } }, methods: { async getResult(){ if(!this.notice_file.guid){ this.$message.warning('请先上传审查意见书') return } this.cancelRun() this.controller = new AbortController(); let patent_fileUrls = {} // if(this.patent_fileUrls && this.patent_fileUrls.length){ // this.patent_fileUrls.forEach(item=>{ // patent_fileUrls[item.name] = item.guid // }) // } if(this.relevant_file.contrast_file && this.relevant_file.contrast_file.length){ this.relevant_file.contrast_file.forEach(item=>{ if(item.file && item.file.guid){ patent_fileUrls['D' + item.order + '-' + item.file.name] = item.file.guid } }) } // var params = { // inputs:{ // fileUrl:'http://192.168.2.24:8803/fileManager/downloadFile?fileId='+this.notice_file.guid, // patent_fileUrls:JSON.stringify(patent_fileUrls) // }, // query:'OA答辩', // response_mode:'streaming', // user:this.userinfo.id // } // this.loading = true // this.result = null // const response = await fetch('https://ai.xsip.cn/v1/chat-messages', { // method: 'POST', // headers: { // 'Content-Type': 'application/json', // // 'Accept': 'text/event-stream', // 'Authorization': `Bearer ${api_key}`, // }, // body:JSON.stringify(params), // signal: this.controller.signal // }); var params = { confessionSessionId:this.currentConversation.id, patentFileUrls:JSON.stringify(patent_fileUrls), appFileGuid:this.relevant_file.app_file.guid, modifyFileGuid:this.relevant_file.modify_file.guid } this.loading = true this.loading_text = '' var loading_text = '' this.result = null const response = await fetch('/api/xiaoshi/dify/sendOADefense', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Accept': 'text/event-stream', }, body:JSON.stringify(params), signal: this.controller.signal }); if (!response.ok) throw new Error('AI API 调用失败'); const reader = response.body.getReader(); const decoder = new TextDecoder('utf-8'); let noFinishMessage = '' while (true) { const { done, value } = await reader.read(); if (done){ this.loading = false break; } const chunk = decoder.decode(value); const lines2 = chunk.split('\n\n').filter(line => line.trim() !== '' && line.trim() !== 'data:event: ping') for(const line of lines2){ const jsonStr1 = line.replace(/^data:\s{0,}/, ''); const jsonStr = jsonStr1.trim() if(jsonStr == 'event: ping' || jsonStr ==''){ continue; } try { const json = JSON.parse(jsonStr); if(json.event == 'message'){ let answer = json.answer try{ let answer_json = JSON.parse(answer) if(answer_json.code == 1001){//缺少专利文件 this.result = answer_json this.$refs.errorTip.open(answer_json) }else if(answer_json.code == 200){ this.queryConfessionSession(false,false,true) }else if(answer_json.code == 1003){ this.loading_text = answer_json.data loading_text = answer_json.data } }catch(e){ if(loading_text!=''){ loading_text = '' this.loading_text = '' } this.result += answer; } }else if(json.event == 'message_replace'){ this.result = json.answer }else if(json.event == 'message_end'){ } } catch (e) { if(jsonStr.indexOf('{') == 0){ noFinishMessage = jsonStr }else{ noFinishMessage += jsonStr if(jsonStr.lastIndexOf('}') == jsonStr.length - 1){ try{ const json = JSON.parse(noFinishMessage); if(json.event == 'message'){ let answer = json.answer try{ let answer_json = JSON.parse(answer) if(answer_json.code == 1001){//缺少专利文件 this.result = answer_json this.$refs.errorTip.open(answer_json) }else if(answer_json.code == 1001){//请求超时 this.result = answer_json }else if(answer_json.code == 200){ this.queryConfessionSession(false,false,true) }else if(answer_json.code == 1003){ this.loading_text = answer_json.data loading_text = answer_json.data } }catch(e){ if(loading_text!=''){ loading_text = '' this.loading_text = '' } this.result += answer; } }else if(json.event == 'message_replace'){ this.result = json.answer }else if(json.event == 'message_end'){ } } catch(e){ console.error('解析响应数据出错:', e); }finally{ noFinishMessage = '' } } } } } } }, cancelRun(){ if (this.controller) { this.controller.abort(); } this.loading = false }, //格式化AI答复 formateAIAnswer(chat){ let answer = chat.replace('null','') // let regx1 = /[\s\S]*<\/think>/g // let regx2 = /
Thinking... <\/summary>[\s\S]*<\/details>/g // // let regx1 = new RegExp(".*", "g") // // let regx2 = new RegExp('
Thinking... .*
', "g") // let text = answer.replace(regx1,'').replace(regx2,'') // return [ // { // type:'text', // content:text.trim() // } // ] let len = answer.length let signs = [ { start:'', end:'' }, { start:'
Thinking... ', end:'
' }, ] let sign_obj = {} let start_index = -1 let end_index = -1 for(let i = 0;i { console.log('内容已复制到剪贴板'); this.$message.success('复制成功'); }); return } let textArea = document.createElement("textarea"); textArea.value = answer_text; // 防止滚动到页面底部 textArea.style.top = "0"; textArea.style.left = "0"; textArea.style.position = "fixed"; textArea.style.opacity = "0"; document.body.appendChild(textArea); textArea.select(); try { document.execCommand('copy'); this.$message.success('复制成功'); } catch (err) { this.$message.error('复制失败'); } document.body.removeChild(textArea); }, /** * 上传文件处理 * */ // 上传的文件监听 onchangeFile(file, fileList) { if(!this.patent_fileUrls){ this.$set(this,'patent_fileUrls',[]) } if (file.guid) { let index = this.patent_fileUrls.findIndex(item => { return item.uid == file.uid }) if (index != -1) { this.patent_fileUrls.splice(index, 1, file) } } else { this.patent_fileUrls.push(file.raw) } }, // 删除上传的文件 onRemove(file, fileList) { let index = this.patent_fileUrls.findIndex(item => { return item.uid == file.uid }) if (index != -1) { this.patent_fileUrls.splice(index, 1) } }, }, }