123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335 |
- 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 = /<think>[\s\S]*<\/think>/g
- // let regx2 = /<details style="color:gray;background-color: #f8f8f8;padding: 8px;border-radius: 4px;" open> <summary> Thinking... <\/summary>[\s\S]*<\/details>/g
- // // let regx1 = new RegExp("<think>.*</think>", "g")
- // // let regx2 = new RegExp('<details style="color:gray;background-color: #f8f8f8;padding: 8px;border-radius: 4px;" open> <summary> Thinking... </summary>.*</details>', "g")
- // let text = answer.replace(regx1,'').replace(regx2,'')
- // return [
- // {
- // type:'text',
- // content:text.trim()
- // }
- // ]
- let len = answer.length
- let signs = [
- {
- start:'<think>',
- end:'</think>'
- },
- {
- start:'<details style="color:gray;background-color: #f8f8f8;padding: 8px;border-radius: 4px;" open> <summary> Thinking... </summary>',
- end:'</details>'
- },
- ]
- let sign_obj = {}
- let start_index = -1
- let end_index = -1
- for(let i = 0;i<signs.length;i++){
- let item = signs[i]
- let index = answer.indexOf(item.start)
- if(index!=-1){
- sign_obj = item
- start_index = index
- end_index = answer.indexOf(item.end)
- break;
- }
- }
- var arr = []
- if(start_index == -1){
- let obj = {
- type:'text',
- content:answer.trim()
- }
- arr.push(obj)
- return arr
- }
- if(end_index == -1){
- let obj = {
- type:'text',
- content:answer.substring(0, start_index).trim()
- }
- arr.push(obj)
- obj = {
- type:'think',
- content:answer.substring(start_index + sign_obj.start.length,len).trim(),
- show:false
- }
- if(chat.id == this.currentMessage.id){
- obj.show = true
- }
- arr.push(obj)
- return arr
- }
- let obj = {
- type:'text',
- content:answer.substring(0, start_index).trim()
- }
- arr.push(obj)
- obj = {
- type:'think',
- show:false,
- content:answer.substring(start_index + sign_obj.start.length, end_index).trim()
- }
- arr.push(obj)
- obj = {
- type:'text',
- content:answer.substring(end_index + sign_obj.end.length,len).trim()
- }
- arr.push(obj)
- return arr
- },
- async copy(chat){
- let answerDomList = document.querySelectorAll('#answer')
-
- if(answerDomList.length == 0){
- return
- }
- let answerDom = answerDomList[answerDomList.length - 1]
- let answerHtml = answerDom.innerHTML
- let answer_text = answerDom.textContent || answerDom.innerText;
- if(navigator.clipboard && navigator.clipboard.write){
- // 复制复杂内容(纯文本 + HTML)
- navigator.clipboard.write([
- new ClipboardItem({
- 'text/html': new Blob([answerHtml], { type: 'text/html' }),
- 'text/plain': new Blob([answer_text], { type: 'text/plain' })
- })
- ]).then(() => {
- 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)
- }
- },
- },
- }
|