fmode-ng 0.0.32 → 0.0.34
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/esm2022/lib/aigc/avatar/modal-chat-voice-input/modal-chat-voice-input.component.mjs +1 -1
- package/esm2022/lib/aigc/chat/chat-message-area/comp-message-area.component.mjs +1 -1
- package/esm2022/lib/aigc/chat/chat-message-card/comp-message-card.component.mjs +1 -1
- package/esm2022/lib/aigc/chat/chat-modal-input/modal-audio-message/modal-audio-message.component.mjs +1 -1
- package/esm2022/lib/aigc/chat/chat-modal-input/modal-input.component.mjs +1 -1
- package/esm2022/lib/aigc/chat/comp-role-prompt/comp-role-prompt.component.mjs +1 -1
- package/esm2022/lib/aigc/service-fmai/service-chat/chat-class.mjs +1 -1
- package/esm2022/lib/aigc/service-fmai/service-chat/chat.service.mjs +1 -1
- package/esm2022/lib/aigc/service-fmai/service-chat/pipes/chat-content.pipe.mjs +1 -1
- package/esm2022/lib/aigc/voice/fmode-voice.service.mjs +1 -1
- package/esm2022/lib/aigc/voice/lib/pcm2wav.mjs +1 -1
- package/esm2022/lib/aigc/voice/tts/fmode-tts-class.mjs +1 -1
- package/esm2022/lib/storage/service-upload/nova-upload.service.mjs +1 -1
- package/fesm2022/fmode-ng.mjs +1 -1
- package/fesm2022/fmode-ng.mjs.map +1 -1
- package/lib/aigc/chat/chat-message-card/comp-message-card.component.d.ts +3 -0
- package/lib/aigc/chat/chat-modal-input/modal-audio-message/modal-audio-message.component.d.ts +6 -1
- package/lib/aigc/service-fmai/service-chat/chat-class.d.ts +7 -3
- package/lib/aigc/service-fmai/service-chat/chat.service.d.ts +3 -1
- package/lib/aigc/voice/fmode-voice.service.d.ts +6 -2
- package/lib/aigc/voice/lib/pcm2wav.d.ts +1 -1
- package/lib/aigc/voice/tts/fmode-tts-class.d.ts +15 -3
- package/lib/storage/service-upload/nova-upload.service.d.ts +1 -0
- package/package.json +1 -1
|
@@ -5,6 +5,6 @@
|
|
|
5
5
|
* 保留所有权利 All Rights Reserved.
|
|
6
6
|
* /home/ryan/workspace/nova/nova-admin/dist/fmode-ng/esm2022/lib/aigc/service-fmai/service-chat/chat-class.mjs
|
|
7
7
|
*/
|
|
8
|
-
import{bufferTime,concatMap,Observable,delay,finalize}from"rxjs";import Parse from"parse";import{AgentPrompt}from"../../agent";import{FmodeTTS}from"../../voice/tts";const API_BASE="https://server.fmode.cn/api/apig/aigc/gpt",agentPrompt=new AgentPrompt,PromptTplTalkSSMLOutputCode="talk-ssml-output-tpl",PromptTplTalkTextSSMLCode="talk-text-ssml-tpl";export function getMessageContentText(t){let e="";return"string"==typeof t&&(e=t),"object"==typeof t&&(e=t?.find((t=>t?.text))?.text||""),e}export function getMessageImageUrl(t){return"object"==typeof t?t?.find((t=>t?.image_url))?.image_url?.url||"":null}export class FmodeChat{showAvatar(){this.avatarConfig=this.role?.get("avatarConfig"),this.avatarConfig&&(this.isAvatarShow=!0,this.avatarConfig?.image&&(this.avatarConfig.image.waiting=this.avatarConfig.image.waiting||this.role?.get("thumb")||this.role?.get("avatar"),this.avatarMode="image"),this.avatarConfig?.video&&(this.avatarConfig.video.waiting=this.avatarConfig.video.waiting,this.avatarMode="video"))}constructor(t,e,s,i,o,n){this.ChatSession=Parse.Object.extend("ChatSession"),this.messageList=[{role:"system",content:"系统提示:AI仅供参考"}],this.latestAIResponse="",this.userInput="",this.userImage="",this.isAvatarShow=!1,this.avatarMode="",this.isPromptModalOpen=!1,this.promptList=[],this.leftButtons=[{title:"灵感",icon:"color-wand-outline",onClick:()=>{this.isPromptModalOpen=!0},show:()=>this?.promptList?.length},{title:"角色",icon:"people-outline",onClick:()=>{this.navCtrl?.navigateRoot("/chat/pro/mask")},show:()=>!0},{title:"呼叫",icon:"call-outline",onClick:()=>{this.chatServ?.callRole(this.role)},show:()=>this?.role?.get("voiceConfig")}],this.isVoiceInputMode=!1,this.isTexting=!1,this.isTalkMode=!1,this.SSMLRoleVoice="zh-CN-XiaoxiaoNeural",this.playAnimation=t=>{console.log(t)},this.chatServ=i,this.role=e,this.sessionId=t,this.navCtrl=o,this.ncloud=n,s?.id&&(this.chatSession=s,this.messageList=this.chatSession.get("messageList"),this.sessionId=s?.id)}async loadTalkSystemPrompt(t){if(!this.isTalkMode)return;if(!t)return;"男"==t?.get("gender")?this.SSMLRoleVoice="zh-CN-YunyeNeural":this.SSMLRoleVoice="zh-CN-XiaoxiaoNeural",this.SSMLRoleVoice=t?.get("voiceConfig")?.voice||this.SSMLRoleVoice;let e=await agentPrompt.getFormatTpl("talk-ssml-output-tpl",{SSMLRoleVoice:this.SSMLRoleVoice}),s=t.get("prompt")||"请你扮演飞码AI的人工智能专家。";s+=e;let i={role:"user",content:s,hidden:!0},o=this.messageList?.map((t=>t?.content)).join();if(o.indexOf(s)>-1)return;let n=this.messageList?.findIndex((t=>"system"==t?.role)),a=n+1;this.messageList.splice(a,0,i)}loadRolePrompt(){let t=this.role?.get("prompt"),e={role:"user",content:t,hidden:!0};if(!t)return;let s=this.messageList?.map((t=>t?.content)).join();if(s.indexOf(t)>-1)return;let i=this.messageList?.findIndex((t=>"system"==t?.role)),o=i+1;this.messageList.splice(o,0,e)}async sendMessage(t="FmodeAiTest测试问题",e,s,i){this.loadRolePrompt(),e?this.messageList.push({role:"user",content:[{type:"image_url",image_url:{url:e}},{type:"text",text:t}],complete:!0,createdAt:new Date}):this.messageList.push({role:"user",content:t,complete:!0,createdAt:new Date});let o=new FmodeChatCompletion(this.fixMessageList(this.messageList),{model:this.chatServ?.currentModel?.get("code")||"fmode-4.5-128k"}),n=!1;this.isTalkMode&&(n=!0);let a=o.sendCompletion({isDirect:n,onComplete:s||null}).pipe(finalize((async()=>{if(this.isTalkMode){let t=this.messageList[o.indexOfList]?.content,e=await this.getVoiceByContentText(t,i);i?.onSSMLComplete&&i?.onSSMLComplete(e),this.messageList[o.indexOfList].voice=e}this.messageList[o.indexOfList].complete=!0}))).subscribe((t=>{this.messageList[o.indexOfList]=t,this.latestAIResponse=this.getContentText(t?.content);let e=this.chatSession?.get("messageList")?.length;this.messageList?.length>e&&this.saveChatSession(),t?.complete&&(this.saveChatSession(),a.unsubscribe())}))}getVoiceByContentText(t,e,s=!1){let i=this.getContentText(t),o=new(Parse.Object.extend("ChatVoice")),n="";return this.SSMLRoleVoice=this.role?.get("voiceConfig")?.voice||this.SSMLRoleVoice,new Promise((async(t,e)=>{let resolveChatVoice=async()=>{o.set("content",i),o.set("ssml",n),o=await o.save(),t({id:o?.id,ssml:n})};if(0==s&&(n=`<speak xmlns="http://www.w3.org/2001/10/synthesis" xmlns:mstts="http://www.w3.org/2001/mstts" xmlns:emo="http://www.w3.org/2009/10/emotionml" version="1.0" xml:lang="zh-CN"><voice name="${this.SSMLRoleVoice}">${i}</voice></speak>`,resolveChatVoice()),1==s){let t=await agentPrompt.getFormatTpl("talk-text-ssml-tpl",{content:i,SSMLRoleVoice:this.SSMLRoleVoice});new FmodeChatCompletion(this.fixMessageList([{role:"user",content:t}]),{model:this.chatServ?.currentModel?.get("code")||"fmode-4.5-128k"}).sendCompletion({isDirect:!0}).subscribe((async t=>{t?.complete&&(n=this.getContentText(t?.content),resolveChatVoice())}))}}))}getContentText(t){return"string"==typeof t?t:t?.[0]?.text||""}async initTTS(){let t=await this.ncloud.apig("voice/tts/token",{company:localStorage.getItem("company")});if(console.log(t),t?.token){let e=new FmodeTTS(t);this.tts=e}}async playTTS(t){if(await this.initTTS(),this.tts)try{await this.tts.speakAsync(t?.ssml)}catch(t){console.error(t)}}async saveChatSession(){if("new"==this.sessionId&&(this.chatSession=new this.ChatSession),this.chatSession.set("title",this.genTitle()),this.chatSession.set("role",this.role?.toPointer()),this.chatSession.set("messageList",this.messageList),this.chatSession.set("user",Parse.User.current()?.toPointer()),this.chatSession=await this.chatSession.save(),this.sessionId=this.chatSession?.id,this.sessionId){let t=`${window.location.origin}/chat/pro/chat/${this.sessionId}`;if(window.location?.pathname?.indexOf("chat/session")>-1){window.location.origin,this.sessionId}t=this.getInviteUrl(t),window.history.replaceState(null,null,t);let e={sid:this.chatSession?.id,rid:this.role?.id,name:this.role?.get("name"),message:this.chatSession?.get("messageList")?.[this.chatSession?.get("messageList")?.length-1]?.content?.slice(0,20),latest:this.chatSession?.createdAt};this.chatServ?.chatList?.length||(this.chatServ.chatList=[]);let s=this.chatServ?.chatList?.find((t=>t?.sid==e?.sid));s>-1?this.chatServ.chatList[s]=e:this.chatServ?.chatList.unshift(e)}}getInviteUrl(t){let e="?";e=t?.indexOf("?")>-1?"&":"?";let s=Parse.User?.current()?.id;if(-1==t?.indexOf("invite="+s)){if(!s)return t;t+=e+"invite="+s}return t}genTitle(){if(this.title)return this.title;let t=this.messageList.find((t=>"user"==t.role))?.content;return"string"==typeof t&&(this.title=t?.slice(0,15)||""),"object"==typeof t&&(this.title=t?.find((t=>t?.text))?.text||""),this.title}fixMessageList(t){return t.map((t=>({role:t.role,content:t.content})))}nowStr(){let t=new Date;return`${t.getFullYear()}/${t.getMonth()+1}/${t.getDate()} ${t.getHours()}:${t.getMinutes()}:${t.getSeconds()}`}}export class FmodeChatCompletion{constructor(t,e){this.content="",this.contentBuffer=[],this.isCompleted=!1,this.indexOfList=Number(t.length),this.messages=t,this.model=e?.model||"fmode-4.5-128k"}sendCompletion(t={}){t.intTime=t?.intTime||50,t.isDirect=t?.isDirect||!1,t?.isDirect&&(t.intTime=1);let e={messages:this.messages,stream:!0,model:this.model,temperature:.5,presence_penalty:0,frequency_penalty:0};return new Observable((s=>{let i=RequestFmodeChatApi("/v1/chat/completions",e).subscribe((e=>{let o=String(e);if("data: [DONE]"==o&&(this.isCompleted=!0,t?.isDirect&&this.isCompleted&&(s.next({role:"assistant",content:this.content,complete:!0,createdAt:new Date}),i.unsubscribe(),t?.onComplete&&t.onComplete({role:"assistant",content:this.content,complete:!0,createdAt:new Date}),s.complete())),o.indexOf("data: {")>-1){let e=chunkToJson(o),n=e?.choices?.[0]?.delta?.content||"";this.contentBuffer.push(n),t?.isDirect&&(this.content+=n||"",this.isCompleted||s.next({role:"assistant",cid:e?.id,content:this.content,createdAt:new Date})),t?.isDirect||this.contentPusher||(this.contentPusher=setInterval((()=>{this.isCompleted&&0==this.contentBuffer?.length&&(s.next({role:"assistant",cid:e?.id,content:this.content,complete:!0,createdAt:new Date}),i.unsubscribe(),clearInterval(this.contentPusher),s.complete()),this.contentBuffer?.length>=0&&(this.contentBuffer?.length>0&&(this.content+=this.contentBuffer.shift()),s.next({role:"assistant",cid:e?.id,content:this.content,createdAt:new Date}))}),t?.intTime))}}))})).pipe(bufferTime(100),concatMap((t=>t)),delay(200))}}function chunkToJson(t){let e;try{e=JSON.parse(t.replaceAll("data: ",""))}catch(t){console.error(t)}return e||{}}function RequestFmodeChatApi(t,e,s="POST"){return new Observable((i=>{let o=API_BASE+t,n=`Bearer ${Parse.User.current()?.getSessionToken()||localStorage.getItem("FMODE_AI_TOKEN")}`;return e.token=n,e&&(e=JSON.stringify(e)),fetch(o,{headers:{"Content-Type":"text/plain","Cache-Control":"no-cache"},body:e||null,method:s,credentials:"omit",mode:"cors"}).then((t=>{let e="";{let s=t.body?.getReader();const o=new TextDecoder;let n=new ReadableStream({start(t){!function read(){s.read().then((({done:e,value:s})=>{if(e)return t.close(),void i.complete();t.enqueue(s),read()}))}()}}).getReader();n.read().then((function processStream({done:t,value:s}){if(t)return;!function processData(t){let s=(e+t).split("\n");if(s?.length>1){for(let t=0;t<s.length-1;t++){let e=s[t];i.next(e)}e=s[s.length-1]}}(o.decode(s)),n.read().then(processStream)}))}})).catch((t=>i.error(t))),()=>{}}))}function JsonToFormData(t){const e=new FormData;return function appendFormData(t,s=""){Array.isArray(t)?t.forEach(((t,e)=>{appendFormData(t,`${s}[${e}]`)})):"object"==typeof t&&null!==t?Object.keys(t).forEach((e=>{const i=s?`${s}.${e}`:e;appendFormData(t[e],i)})):e.append(s,t)}(t),e}
|
|
8
|
+
import{bufferTime,concatMap,Observable,delay,finalize}from"rxjs";import Parse from"parse";import{AgentPrompt}from"../../agent";import{FmodeTTS}from"../../voice/tts";const API_BASE="https://server.fmode.cn/api/apig/aigc/gpt",agentPrompt=new AgentPrompt,PromptTplTalkSSMLOutputCode="talk-ssml-output-tpl",PromptTplTalkTextSSMLCode="talk-text-ssml-tpl";export function getMessageContentText(t){let e="";return"string"==typeof t&&(e=t),"object"==typeof t&&(e=t?.find((t=>t?.text))?.text||""),e}export function getMessageImageUrl(t){return"object"==typeof t?t?.find((t=>t?.image_url))?.image_url?.url||"":null}export class FmodeChat{showAvatar(){this.avatarConfig=this.role?.get("avatarConfig"),this.avatarConfig&&(this.isAvatarShow=!0,this.avatarConfig?.image&&(this.avatarConfig.image.waiting=this.avatarConfig.image.waiting||this.role?.get("thumb")||this.role?.get("avatar"),this.avatarMode="image"),this.avatarConfig?.video&&(this.avatarConfig.video.waiting=this.avatarConfig.video.waiting,this.avatarMode="video"))}constructor(t,e,s,i,o,n,a){this.ChatSession=Parse.Object.extend("ChatSession"),this.messageList=[{role:"system",content:"系统提示:AI仅供参考"}],this.latestAIResponse="",this.userInput="",this.userImage="",this.isAvatarShow=!1,this.avatarMode="",this.isPromptModalOpen=!1,this.promptList=[],this.leftButtons=[{title:"灵感",icon:"color-wand-outline",onClick:()=>{this.isPromptModalOpen=!0},show:()=>this?.promptList?.length},{title:"角色",icon:"people-outline",onClick:()=>{this.navCtrl?.navigateRoot("/chat/pro/mask")},show:()=>!0},{title:"呼叫",icon:"call-outline",onClick:()=>{this.chatServ?.callRole(this.role)},show:()=>this?.role?.get("voiceConfig")}],this.isVoiceInputMode=!1,this.isTexting=!1,this.isTalkMode=!1,this.SSMLRoleVoice="zh-CN-XiaoxiaoNeural",this.playAnimation=t=>{console.log(t)},this.voiceMap={},this.chatServ=i,this.role=e,this.sessionId=t,this.navCtrl=o,this.ncloud=n,this.uploadServ=a,s?.id&&(this.chatSession=s,this.messageList=this.chatSession.get("messageList"),this.sessionId=s?.id)}async loadTalkSystemPrompt(t){if(!this.isTalkMode)return;if(!t)return;"男"==t?.get("gender")?this.SSMLRoleVoice="zh-CN-YunyeNeural":this.SSMLRoleVoice="zh-CN-XiaoxiaoNeural",this.SSMLRoleVoice=t?.get("voiceConfig")?.voice||this.SSMLRoleVoice;let e=await agentPrompt.getFormatTpl("talk-ssml-output-tpl",{SSMLRoleVoice:this.SSMLRoleVoice}),s=t.get("prompt")||"请你扮演飞码AI的人工智能专家。";s+=e;let i={role:"user",content:s,hidden:!0},o=this.messageList?.map((t=>t?.content)).join();if(o.indexOf(s)>-1)return;let n=this.messageList?.findIndex((t=>"system"==t?.role)),a=n+1;this.messageList.splice(a,0,i)}loadRolePrompt(){let t=this.role?.get("prompt"),e={role:"user",content:t,hidden:!0};if(!t)return;let s=this.messageList?.map((t=>t?.content)).join();if(s.indexOf(t)>-1)return;let i=this.messageList?.findIndex((t=>"system"==t?.role)),o=i+1;this.messageList.splice(o,0,e)}async sendMessage(t="FmodeAiTest测试问题",e,s,i,o){if(this.loadRolePrompt(),e){let s={role:"user",content:[{type:"image_url",image_url:{url:e}},{type:"text",text:t}],complete:!0,createdAt:new Date};o&&(s.voice={id:o?.id}),this.messageList.push({role:"user",content:[{type:"image_url",image_url:{url:e}},{type:"text",text:t}],complete:!0,createdAt:new Date})}else{let e={role:"user",content:t,complete:!0,createdAt:new Date};o&&(e.voice={id:o?.id,duration:o?.duration}),this.messageList.push(e)}let n=new FmodeChatCompletion(this.fixMessageList(this.messageList),{model:this.chatServ?.currentModel?.get("code")||"fmode-4.5-128k"});this.userInput="",this.userImage="";let a=!1;this.isTalkMode&&(a=!0);let r=n.sendCompletion({isDirect:a,onComplete:s||null}).pipe(finalize((async()=>{if(this.isTalkMode){let t=this.messageList[n.indexOfList]?.content,e=await this.getVoiceByContentText(t,i);i?.onSSMLComplete&&i?.onSSMLComplete(e),this.messageList[n.indexOfList].voice=e,this.playChatVoice(this.voiceMap[e?.id])}this.messageList[n.indexOfList].complete=!0}))).subscribe((t=>{this.messageList[n.indexOfList]=t,this.latestAIResponse=this.getContentText(t?.content);let e=this.chatSession?.get("messageList")?.length;this.messageList?.length>e&&this.saveChatSession(),t?.complete&&(this.saveChatSession(),r.unsubscribe())}))}getVoiceByContentText(t,e,s=!1){let i=this.getContentText(t),o=new(Parse.Object.extend("ChatVoice")),n="";return this.SSMLRoleVoice=this.role?.get("voiceConfig")?.voice||this.SSMLRoleVoice,new Promise((async(t,e)=>{let resolveChatVoice=async()=>{o.set("content",i),o.set("ssml",n),o.set("role","assistant");let e=localStorage.getItem("company");e&&o.set("company",{__type:"Pointer",className:"Company",objectId:e}),Parse.User.current()?.id&&o.set("user",Parse.User.current().toPointer()),this.chatSession?.id&&o.set("session",this.chatSession?.toPointer()),o=await o.save(),this.voiceMap[o?.id]=o,t({id:o?.id})};if(0==s&&(n=`<speak xmlns="http://www.w3.org/2001/10/synthesis" xmlns:mstts="http://www.w3.org/2001/mstts" xmlns:emo="http://www.w3.org/2009/10/emotionml" version="1.0" xml:lang="zh-CN"><voice name="${this.SSMLRoleVoice}">${i}</voice></speak>`,resolveChatVoice()),1==s){let t=await agentPrompt.getFormatTpl("talk-text-ssml-tpl",{content:i,SSMLRoleVoice:this.SSMLRoleVoice});new FmodeChatCompletion(this.fixMessageList([{role:"user",content:t}]),{model:this.chatServ?.currentModel?.get("code")||"fmode-4.5-128k"}).sendCompletion({isDirect:!0}).subscribe((async t=>{t?.complete&&(n=this.getContentText(t?.content),resolveChatVoice())}))}}))}getContentText(t){return"string"==typeof t?t:t?.[0]?.text||""}async initTTS(){let t=await this.ncloud.apig("voice/tts/token",{company:localStorage.getItem("company")});if(console.log(t),t?.token){let e=new FmodeTTS(t,this.uploadServ);this.tts=e}}async playChatVoice(t,e){if(await this.initTTS(),this.tts)try{this.playAnimation("talking"),await this.tts.speakAsync(t?.get("ssml"),t,{onLoaded:t=>{e.onLoaded&&e.onLoaded(t)},onStop:()=>{e.onStop&&e.onStop(),this.playAnimation("waiting")}})}catch(t){console.error(t)}}async saveChatSession(){if("new"==this.sessionId&&(this.chatSession=new this.ChatSession),this.chatSession.set("title",this.genTitle()),this.chatSession.set("role",this.role?.toPointer()),this.chatSession.set("messageList",this.messageList),this.chatSession.set("user",Parse.User.current()?.toPointer()),this.chatSession=await this.chatSession.save(),this.sessionId=this.chatSession?.id,this.sessionId){let t=`${window.location.origin}/chat/pro/chat/${this.sessionId}`;if(window.location?.pathname?.indexOf("chat/session")>-1){window.location.origin,this.sessionId}t=this.getInviteUrl(t),window.history.replaceState(null,null,t);let e={sid:this.chatSession?.id,rid:this.role?.id,name:this.role?.get("name"),message:this.chatSession?.get("messageList")?.[this.chatSession?.get("messageList")?.length-1]?.content?.slice(0,20),latest:this.chatSession?.createdAt};this.chatServ?.chatList?.length||(this.chatServ.chatList=[]);let s=this.chatServ?.chatList?.find((t=>t?.sid==e?.sid));s>-1?this.chatServ.chatList[s]=e:this.chatServ?.chatList.unshift(e)}}getInviteUrl(t){let e="?";e=t?.indexOf("?")>-1?"&":"?";let s=Parse.User?.current()?.id;if(-1==t?.indexOf("invite="+s)){if(!s)return t;t+=e+"invite="+s}return t}genTitle(){if(this.title)return this.title;let t=this.messageList.find((t=>"user"==t.role))?.content;return"string"==typeof t&&(this.title=t?.slice(0,15)||""),"object"==typeof t&&(this.title=t?.find((t=>t?.text))?.text||""),this.title}fixMessageList(t){return t.map((t=>({role:t.role,content:t.content})))}nowStr(){let t=new Date;return`${t.getFullYear()}/${t.getMonth()+1}/${t.getDate()} ${t.getHours()}:${t.getMinutes()}:${t.getSeconds()}`}}export class FmodeChatCompletion{constructor(t,e){this.content="",this.contentBuffer=[],this.isCompleted=!1,this.indexOfList=Number(t.length),this.messages=t,this.model=e?.model||"fmode-4.5-128k"}sendCompletion(t={}){t.intTime=t?.intTime||50,t.isDirect=t?.isDirect||!1,t?.isDirect&&(t.intTime=1);let e={messages:this.messages,stream:!0,model:this.model,temperature:.5,presence_penalty:0,frequency_penalty:0};return new Observable((s=>{let i=RequestFmodeChatApi("/v1/chat/completions",e).subscribe((e=>{let o=String(e);if("data: [DONE]"==o&&(this.isCompleted=!0,t?.isDirect&&this.isCompleted&&(s.next({role:"assistant",content:this.content,complete:!0,createdAt:new Date}),i.unsubscribe(),t?.onComplete&&t.onComplete({role:"assistant",content:this.content,complete:!0,createdAt:new Date}),s.complete())),o.indexOf("data: {")>-1){let e=chunkToJson(o),n=e?.choices?.[0]?.delta?.content||"";this.contentBuffer.push(n),t?.isDirect&&(this.content+=n||"",this.isCompleted||s.next({role:"assistant",cid:e?.id,content:this.content,createdAt:new Date})),t?.isDirect||this.contentPusher||(this.contentPusher=setInterval((()=>{this.isCompleted&&0==this.contentBuffer?.length&&(s.next({role:"assistant",cid:e?.id,content:this.content,complete:!0,createdAt:new Date}),i.unsubscribe(),clearInterval(this.contentPusher),s.complete()),this.contentBuffer?.length>=0&&(this.contentBuffer?.length>0&&(this.content+=this.contentBuffer.shift()),s.next({role:"assistant",cid:e?.id,content:this.content,createdAt:new Date}))}),t?.intTime))}}))})).pipe(bufferTime(100),concatMap((t=>t)),delay(200))}}function chunkToJson(t){let e;try{e=JSON.parse(t.replaceAll("data: ",""))}catch(t){console.error(t)}return e||{}}function RequestFmodeChatApi(t,e,s="POST"){return new Observable((i=>{let o=API_BASE+t,n=`Bearer ${Parse.User.current()?.getSessionToken()||localStorage.getItem("FMODE_AI_TOKEN")}`;return e.token=n,e&&(e=JSON.stringify(e)),fetch(o,{headers:{"Content-Type":"text/plain","Cache-Control":"no-cache"},body:e||null,method:s,credentials:"omit",mode:"cors"}).then((t=>{let e="";{let s=t.body?.getReader();const o=new TextDecoder;let n=new ReadableStream({start(t){!function read(){s.read().then((({done:e,value:s})=>{if(e)return t.close(),void i.complete();t.enqueue(s),read()}))}()}}).getReader();n.read().then((function processStream({done:t,value:s}){if(t)return;!function processData(t){let s=(e+t).split("\n");if(s?.length>1){for(let t=0;t<s.length-1;t++){let e=s[t];i.next(e)}e=s[s.length-1]}}(o.decode(s)),n.read().then(processStream)}))}})).catch((t=>i.error(t))),()=>{}}))}function JsonToFormData(t){const e=new FormData;return function appendFormData(t,s=""){Array.isArray(t)?t.forEach(((t,e)=>{appendFormData(t,`${s}[${e}]`)})):"object"==typeof t&&null!==t?Object.keys(t).forEach((e=>{const i=s?`${s}.${e}`:e;appendFormData(t[e],i)})):e.append(s,t)}(t),e}
|
|
9
9
|
var MODULE_PATH_NEED = `6K+l5paH5Lu25piv5pys6aG555uu55qE5LiA6YOo5YiGIFRoaXMgZmlsZSBpcyBwYXJ0IG9mIHRoZSBDb21wb25lbnRzIGluIEZtb2RlIEluYy4KICAgIOeJiOadg+aJgOaciSDCqSDmnKrmnaXpo57pqawgwqkg5rGf6KW/6ISR5o6n56eR5oqA5pyJ6ZmQ5YWs5Y+4IENvcHlyaWdodCDCqSBGbW9kZSBUZWNobm9sb2d5IENvLiwgTHRkLgogICAg5L+d55WZ5omA5pyJ5p2D5YipIEFsbCBSaWdodHMgUmVzZXJ2ZWQuCiAgICDkuKXnpoHlnKjmnKrnu4/mjojmnYPnmoTmg4XlhrXkuIvvvIzpgJrov4fku7vkvZXlqpLku4vlpI3liLbmraTmlofku7YgVW5hdXRob3JpemVkIGNvcHlpbmcgb2YgdGhpcyBmaWxlLCB2aWEgYW55IG1lZGl1bSBpcyBzdHJpY3RseSBwcm9oaWJpdGVkCiAgICDor6Xmlofku7bmmK/kuJPmnInnmoTmnLrlr4bmlofku7YgUHJvcHJpZXRhcnkgYW5kIGNvbmZpZGVudGlhbAogICAKICAgIENvcHlyaWdodCAyMDIxLW5vdyBGbW9kZSBJbmMuIHN1cHBvcnRAZm1vZGUuY24uIDE4NjA3MDA3MDczLgogICAg5L+d55WZ5omA5pyJ5p2D5YipIEFsbCByaWdodHMgcmVzZXJ2ZWQuCgogICAgUEFUSDovaG9tZS9yeWFuL3dvcmtzcGFjZS9ub3ZhL25vdmEtYWRtaW4vZGlzdC9mbW9kZS1uZy9lc20yMDIyL2xpYi9haWdjL3NlcnZpY2UtZm1haS9zZXJ2aWNlLWNoYXQvY2hhdC1jbGFzcy5tanM=`
|
|
10
10
|
|
|
@@ -5,6 +5,6 @@
|
|
|
5
5
|
* 保留所有权利 All Rights Reserved.
|
|
6
6
|
* /home/ryan/workspace/nova/nova-admin/dist/fmode-ng/esm2022/lib/aigc/service-fmai/service-chat/chat.service.mjs
|
|
7
7
|
*/
|
|
8
|
-
import{Injectable}from"@angular/core";import{Router}from"@angular/router";import{FmodeChat}from"./chat-class";import Parse from"parse";import{NovaCloudService}from"../../../nova-cloud";import{AlertController,NavController,Platform}from"@ionic/angular";import{CrossService}from"../../../platform";import*as i0 from"@angular/core";import*as i1 from"@angular/router";import*as i2 from"../../../nova-cloud";import*as i3 from"@ionic/angular";import*as i4 from"../../../platform";export class ChatService{constructor(t,e,a,r,i,o){this.router=t,this.ncloud=e,this.platform=a,this.alertCtrl=r,this.navCtrl=i,this.cross=o,this.chatMap={},this.isCapacitor=!1,this.platformMap={pc:"电脑端",mobile:"移动端"},this.isCapacitor=this.platform.is("capacitor")}async loadModelList(t){if(this.modelList?.length)return;let e=new Parse.Query("ChatModel");e.notEqualTo("isDeleted",!0),e.equalTo("isEnabled",!0),e.addAscending("index"),this.modelList=await e.find(),this.currentModel=t||this.modelList?.find((t=>"fmode-4.5-128k"==t.get("code")))}async doButtonAction(t){let e=this.cross.navMenuType,a=t?.platform?.map((t=>this.platformMap[t])).join("、");if(t?.platform?.length>0&&-1==t?.platform?.indexOf(e)){(await this.alertCtrl.create({header:"注意",subHeader:"终端不符",message:`请您使用${a}开启本功能。`,buttons:[{role:"ok",text:"知道了"}]})).present()}else t?.path&&this.navCtrl.navigateRoot(t?.path)}async initChatMap(t){if(this.chatMap[t])return this.chatMap[t];let e=new Parse.Query("ChatSession");e.include("role","role.model");let a=await e.get(t),r=new FmodeChat(a?.id,a?.get("role"),a,this,this.navCtrl,this.ncloud);return this.chatMap[t]=r,this.chatMap[t]}async getChatSession(){if(!Parse?.User?.current()?.id)return;let t=new Parse.Query("ChatSession");t.include("role","role.model"),t.addDescending("updatedAt"),t.equalTo("user",Parse.User.current().toPointer()),t.notEqualTo("isDeleted",!0),t.limit(30);let e=await t.find();this.chatList=e.map((t=>(this.chatMap[t?.id]=new FmodeChat(t?.id,t?.get("role"),t,this,this.navCtrl,this.ncloud),{session:t,sid:t?.id,isHidden:!1,rid:t?.get("role")?.id,name:t?.get("role")?.get("name"),thumb:t?.get("role")?.get("thumb"),title:t?.get("title")||t?.get("role")?.get("name"),message:t?.get("messageList")?.[t?.get("messageList")?.length-1]?.content?.slice(0,20),latest:t?.createdAt})))}async getChatSessionDistinct(){let t=await this.ncloud.novaql('SELECT t1."objectId" as sid , "AvatarRole"."objectId" as rid, * FROM (\n SELECT *,ROW_NUMBER() OVER (PARTITION BY "user", "role" ORDER BY "createdAt" DESC) AS rn\n FROM "ChatSession" WHERE "user"=$1\n ) as t1\n LEFT JOIN "AvatarRole" ON "AvatarRole"."objectId" = t1."role"\n WHERE t1.rn=1\n LIMIT $2\n ;',[Parse.User.current()?.id,10]),e=t?.map((t=>({sid:t?.sid,rid:t?.rid,name:t?.name,message:t?.messageList?.[t?.messageList?.length-1]?.content?.slice(0,20),latest:t?.createdAt})));return this.chatList=e,this.chatList}createChatPanel(t,e){let a=e?.id||"new";e=new FmodeChat(a,t,e,this,this.navCtrl,this.ncloud),this.chatMap[a]=e,this.router.navigate(["/chat/pro/chat/"+a])}async createNewRoleChat(t){let e=new Parse.Query("AvatarRole");e.include("model");let a=await e.get(t);return new FmodeChat("new",a,null,this,this.navCtrl,this.ncloud)}async restoreChatPanel(t){let e=new Parse.Query("AvatarRole"),a=new Parse.Query("ChatSession"),r=await e.get(t?.rid),i=await a.get(t?.sid),o=new FmodeChat(t?.sid,r,i,this,this.navCtrl,this.ncloud);this.chatMap[t?.sid]=o,this.router.navigate(["/chat/pro/chat/"+t?.sid])}async callRole(t){document.body.classList.add("dark"),this.router.navigate([`/avatar/role/${t.id}`,{type:"phone"}])}static{this.ɵfac=i0.ɵɵngDeclareFactory({minVersion:"12.0.0",version:"17.3.12",ngImport:i0,type:ChatService,deps:[{token:i1.Router},{token:i2.NovaCloudService},{token:i3.Platform},{token:i3.AlertController},{token:i3.NavController},{token:i4.CrossService}],target:i0.ɵɵFactoryTarget.Injectable})}static{this.ɵprov=i0.ɵɵngDeclareInjectable({minVersion:"12.0.0",version:"17.3.12",ngImport:i0,type:ChatService,providedIn:"root"})}}i0.ɵɵngDeclareClassMetadata({minVersion:"12.0.0",version:"17.3.12",ngImport:i0,type:ChatService,decorators:[{type:Injectable,args:[{providedIn:"root"}]}],ctorParameters:()=>[{type:i1.Router},{type:i2.NovaCloudService},{type:i3.Platform},{type:i3.AlertController},{type:i3.NavController},{type:i4.CrossService}]});
|
|
8
|
+
import{Injectable}from"@angular/core";import{Router}from"@angular/router";import{FmodeChat}from"./chat-class";import Parse from"parse";import{NovaCloudService}from"../../../nova-cloud";import{AlertController,NavController,Platform}from"@ionic/angular";import{CrossService}from"../../../platform";import{NovaUploadService}from"../../../storage/service-upload/nova-upload.service";import*as i0 from"@angular/core";import*as i1 from"@angular/router";import*as i2 from"../../../nova-cloud";import*as i3 from"@ionic/angular";import*as i4 from"../../../platform";import*as i5 from"../../../storage/service-upload/nova-upload.service";export class ChatService{constructor(t,e,a,r,i,o,s){this.router=t,this.ncloud=e,this.platform=a,this.alertCtrl=r,this.navCtrl=i,this.cross=o,this.uploadServ=s,this.chatMap={},this.isCapacitor=!1,this.platformMap={pc:"电脑端",mobile:"移动端"},this.isCapacitor=this.platform.is("capacitor")}async loadModelList(t){if(this.modelList?.length)return;let e=new Parse.Query("ChatModel");e.notEqualTo("isDeleted",!0),e.equalTo("isEnabled",!0),e.addAscending("index"),this.modelList=await e.find(),this.currentModel=t||this.modelList?.find((t=>"fmode-4.5-128k"==t.get("code")))}async doButtonAction(t){let e=this.cross.navMenuType,a=t?.platform?.map((t=>this.platformMap[t])).join("、");if(t?.platform?.length>0&&-1==t?.platform?.indexOf(e)){(await this.alertCtrl.create({header:"注意",subHeader:"终端不符",message:`请您使用${a}开启本功能。`,buttons:[{role:"ok",text:"知道了"}]})).present()}else t?.path&&this.navCtrl.navigateRoot(t?.path)}async initChatMap(t){if(this.chatMap[t])return this.chatMap[t];let e=new Parse.Query("ChatSession");e.include("role","role.model");let a=await e.get(t),r=new FmodeChat(a?.id,a?.get("role"),a,this,this.navCtrl,this.ncloud,this.uploadServ);return this.chatMap[t]=r,this.chatMap[t]}async getChatSession(){if(!Parse?.User?.current()?.id)return;let t=new Parse.Query("ChatSession");t.include("role","role.model"),t.addDescending("updatedAt"),t.equalTo("user",Parse.User.current().toPointer()),t.notEqualTo("isDeleted",!0),t.limit(30);let e=await t.find();this.chatList=e.map((t=>(this.chatMap[t?.id]=new FmodeChat(t?.id,t?.get("role"),t,this,this.navCtrl,this.ncloud,this.uploadServ),{session:t,sid:t?.id,isHidden:!1,rid:t?.get("role")?.id,name:t?.get("role")?.get("name"),thumb:t?.get("role")?.get("thumb"),title:t?.get("title")||t?.get("role")?.get("name"),message:t?.get("messageList")?.[t?.get("messageList")?.length-1]?.content?.slice(0,20),latest:t?.createdAt})))}async getChatSessionDistinct(){let t=await this.ncloud.novaql('SELECT t1."objectId" as sid , "AvatarRole"."objectId" as rid, * FROM (\n SELECT *,ROW_NUMBER() OVER (PARTITION BY "user", "role" ORDER BY "createdAt" DESC) AS rn\n FROM "ChatSession" WHERE "user"=$1\n ) as t1\n LEFT JOIN "AvatarRole" ON "AvatarRole"."objectId" = t1."role"\n WHERE t1.rn=1\n LIMIT $2\n ;',[Parse.User.current()?.id,10]),e=t?.map((t=>({sid:t?.sid,rid:t?.rid,name:t?.name,message:t?.messageList?.[t?.messageList?.length-1]?.content?.slice(0,20),latest:t?.createdAt})));return this.chatList=e,this.chatList}createChatPanel(t,e){let a=e?.id||"new";e=new FmodeChat(a,t,e,this,this.navCtrl,this.ncloud,this.uploadServ),this.chatMap[a]=e,this.router.navigate(["/chat/pro/chat/"+a])}async createNewRoleChat(t){let e=new Parse.Query("AvatarRole");e.include("model");let a=await e.get(t);return new FmodeChat("new",a,null,this,this.navCtrl,this.ncloud,this.uploadServ)}async restoreChatPanel(t){let e=new Parse.Query("AvatarRole"),a=new Parse.Query("ChatSession"),r=await e.get(t?.rid),i=await a.get(t?.sid),o=new FmodeChat(t?.sid,r,i,this,this.navCtrl,this.ncloud,this.uploadServ);this.chatMap[t?.sid]=o,this.router.navigate(["/chat/pro/chat/"+t?.sid])}async callRole(t){document.body.classList.add("dark"),this.router.navigate([`/avatar/role/${t.id}`,{type:"phone"}])}static{this.ɵfac=i0.ɵɵngDeclareFactory({minVersion:"12.0.0",version:"17.3.12",ngImport:i0,type:ChatService,deps:[{token:i1.Router},{token:i2.NovaCloudService},{token:i3.Platform},{token:i3.AlertController},{token:i3.NavController},{token:i4.CrossService},{token:i5.NovaUploadService}],target:i0.ɵɵFactoryTarget.Injectable})}static{this.ɵprov=i0.ɵɵngDeclareInjectable({minVersion:"12.0.0",version:"17.3.12",ngImport:i0,type:ChatService,providedIn:"root"})}}i0.ɵɵngDeclareClassMetadata({minVersion:"12.0.0",version:"17.3.12",ngImport:i0,type:ChatService,decorators:[{type:Injectable,args:[{providedIn:"root"}]}],ctorParameters:()=>[{type:i1.Router},{type:i2.NovaCloudService},{type:i3.Platform},{type:i3.AlertController},{type:i3.NavController},{type:i4.CrossService},{type:i5.NovaUploadService}]});
|
|
9
9
|
var MODULE_PATH_NEED = `6K+l5paH5Lu25piv5pys6aG555uu55qE5LiA6YOo5YiGIFRoaXMgZmlsZSBpcyBwYXJ0IG9mIHRoZSBDb21wb25lbnRzIGluIEZtb2RlIEluYy4KICAgIOeJiOadg+aJgOaciSDCqSDmnKrmnaXpo57pqawgwqkg5rGf6KW/6ISR5o6n56eR5oqA5pyJ6ZmQ5YWs5Y+4IENvcHlyaWdodCDCqSBGbW9kZSBUZWNobm9sb2d5IENvLiwgTHRkLgogICAg5L+d55WZ5omA5pyJ5p2D5YipIEFsbCBSaWdodHMgUmVzZXJ2ZWQuCiAgICDkuKXnpoHlnKjmnKrnu4/mjojmnYPnmoTmg4XlhrXkuIvvvIzpgJrov4fku7vkvZXlqpLku4vlpI3liLbmraTmlofku7YgVW5hdXRob3JpemVkIGNvcHlpbmcgb2YgdGhpcyBmaWxlLCB2aWEgYW55IG1lZGl1bSBpcyBzdHJpY3RseSBwcm9oaWJpdGVkCiAgICDor6Xmlofku7bmmK/kuJPmnInnmoTmnLrlr4bmlofku7YgUHJvcHJpZXRhcnkgYW5kIGNvbmZpZGVudGlhbAogICAKICAgIENvcHlyaWdodCAyMDIxLW5vdyBGbW9kZSBJbmMuIHN1cHBvcnRAZm1vZGUuY24uIDE4NjA3MDA3MDczLgogICAg5L+d55WZ5omA5pyJ5p2D5YipIEFsbCByaWdodHMgcmVzZXJ2ZWQuCgogICAgUEFUSDovaG9tZS9yeWFuL3dvcmtzcGFjZS9ub3ZhL25vdmEtYWRtaW4vZGlzdC9mbW9kZS1uZy9lc20yMDIyL2xpYi9haWdjL3NlcnZpY2UtZm1haS9zZXJ2aWNlLWNoYXQvY2hhdC5zZXJ2aWNlLm1qcw==`
|
|
10
10
|
|
|
@@ -5,6 +5,6 @@
|
|
|
5
5
|
* 保留所有权利 All Rights Reserved.
|
|
6
6
|
* /home/ryan/workspace/nova/nova-admin/dist/fmode-ng/esm2022/lib/aigc/service-fmai/service-chat/pipes/chat-content.pipe.mjs
|
|
7
7
|
*/
|
|
8
|
-
import{Pipe}from"@angular/core";import{getMessageContentText,getMessageImageUrl}from"../chat-class";import*as i0 from"@angular/core";export class ChatContentPipe{transform(e,...t){
|
|
8
|
+
import{Pipe}from"@angular/core";import{getMessageContentText,getMessageImageUrl}from"../chat-class";import*as i0 from"@angular/core";export class ChatContentPipe{transform(e,...t){let a=t?.[0]||"text";return"text"==a?getMessageContentText(e):"image_url"==a?getMessageImageUrl(e):e}static{this.ɵfac=i0.ɵɵngDeclareFactory({minVersion:"12.0.0",version:"17.3.12",ngImport:i0,type:ChatContentPipe,deps:[],target:i0.ɵɵFactoryTarget.Pipe})}static{this.ɵpipe=i0.ɵɵngDeclarePipe({minVersion:"14.0.0",version:"17.3.12",ngImport:i0,type:ChatContentPipe,isStandalone:!0,name:"chatContent"})}}i0.ɵɵngDeclareClassMetadata({minVersion:"12.0.0",version:"17.3.12",ngImport:i0,type:ChatContentPipe,decorators:[{type:Pipe,args:[{name:"chatContent",pure:!0,standalone:!0}]}]});
|
|
9
9
|
var MODULE_PATH_NEED = `6K+l5paH5Lu25piv5pys6aG555uu55qE5LiA6YOo5YiGIFRoaXMgZmlsZSBpcyBwYXJ0IG9mIHRoZSBDb21wb25lbnRzIGluIEZtb2RlIEluYy4KICAgIOeJiOadg+aJgOaciSDCqSDmnKrmnaXpo57pqawgwqkg5rGf6KW/6ISR5o6n56eR5oqA5pyJ6ZmQ5YWs5Y+4IENvcHlyaWdodCDCqSBGbW9kZSBUZWNobm9sb2d5IENvLiwgTHRkLgogICAg5L+d55WZ5omA5pyJ5p2D5YipIEFsbCBSaWdodHMgUmVzZXJ2ZWQuCiAgICDkuKXnpoHlnKjmnKrnu4/mjojmnYPnmoTmg4XlhrXkuIvvvIzpgJrov4fku7vkvZXlqpLku4vlpI3liLbmraTmlofku7YgVW5hdXRob3JpemVkIGNvcHlpbmcgb2YgdGhpcyBmaWxlLCB2aWEgYW55IG1lZGl1bSBpcyBzdHJpY3RseSBwcm9oaWJpdGVkCiAgICDor6Xmlofku7bmmK/kuJPmnInnmoTmnLrlr4bmlofku7YgUHJvcHJpZXRhcnkgYW5kIGNvbmZpZGVudGlhbAogICAKICAgIENvcHlyaWdodCAyMDIxLW5vdyBGbW9kZSBJbmMuIHN1cHBvcnRAZm1vZGUuY24uIDE4NjA3MDA3MDczLgogICAg5L+d55WZ5omA5pyJ5p2D5YipIEFsbCByaWdodHMgcmVzZXJ2ZWQuCgogICAgUEFUSDovaG9tZS9yeWFuL3dvcmtzcGFjZS9ub3ZhL25vdmEtYWRtaW4vZGlzdC9mbW9kZS1uZy9lc20yMDIyL2xpYi9haWdjL3NlcnZpY2UtZm1haS9zZXJ2aWNlLWNoYXQvcGlwZXMvY2hhdC1jb250ZW50LnBpcGUubWpz`
|
|
10
10
|
|
|
@@ -5,6 +5,6 @@
|
|
|
5
5
|
* 保留所有权利 All Rights Reserved.
|
|
6
6
|
* /home/ryan/workspace/nova/nova-admin/dist/fmode-ng/esm2022/lib/aigc/voice/fmode-voice.service.mjs
|
|
7
7
|
*/
|
|
8
|
-
import{Injectable}from"@angular/core";import Recorder from"recorder-core";import"recorder-core/src/engine/pcm";import"recorder-core/src/engine/wav";import"recorder-core/src/extensions/waveview";import CryptoJS from"crypto-js";import{pcmtoWav}from"./lib/pcm2wav";import{convertFrameBufferToBase64,resampleBuffer}from"./lib/resample";import{WebSpeech}from"./class-asr";import{Platform}from"@ionic/angular";import{Diagnostic}from"@awesome-cordova-plugins/diagnostic/ngx";import*as i0 from"@angular/core";import*as i1 from"@ionic/angular";import*as i2 from"@awesome-cordova-plugins/diagnostic/ngx";export class FmodeVoiceService{constructor(t,e){this.platform=t,this.diagnostic=e,this.webSpeech=WebSpeech,this.recordPcmBlob=null,this.recordType="pcm",this.encodingType="raw",this.connStatus="",this.btnStatus="UNDEFINED",this.resultText="",this.resultTextTemp="",this.APPID="50f4a46c",this.API_SECRET="NzFlNmFhZDJjMDNkZGM3NzI0Mzg2OGNm",this.API_KEY="106ddc40dfd4b9ca6d7b47c70fada749",this.requestPermission()}toggleRecord(){console.log(this.btnStatus),"UNDEFINED"===this.btnStatus||"CLOSED"===this.btnStatus?this.startTalk():"CONNECTING"!==this.btnStatus&&"OPEN"!==this.btnStatus||this.finishTalk()}finishTalk(){this.onBeforeFinishTalk&&this.onBeforeFinishTalk(),this.recordStop()
|
|
8
|
+
import{Injectable}from"@angular/core";import Recorder from"recorder-core";import"recorder-core/src/engine/pcm";import"recorder-core/src/engine/wav";import"recorder-core/src/extensions/waveview";import CryptoJS from"crypto-js";import{pcmtoWav}from"./lib/pcm2wav";import{convertFrameBufferToBase64,resampleBuffer}from"./lib/resample";import{WebSpeech}from"./class-asr";import{Platform}from"@ionic/angular";import{Diagnostic}from"@awesome-cordova-plugins/diagnostic/ngx";import*as i0 from"@angular/core";import*as i1 from"@ionic/angular";import*as i2 from"@awesome-cordova-plugins/diagnostic/ngx";export class FmodeVoiceService{constructor(t,e){this.platform=t,this.diagnostic=e,this.webSpeech=WebSpeech,this.isUserFinish=!1,this.recordWavBlob=null,this.recordPcmBlob=null,this.recordDuration=0,this.recordType="pcm",this.encodingType="raw",this.connStatus="",this.btnStatus="UNDEFINED",this.resultText="",this.resultTextTemp="",this.APPID="50f4a46c",this.API_SECRET="NzFlNmFhZDJjMDNkZGM3NzI0Mzg2OGNm",this.API_KEY="106ddc40dfd4b9ca6d7b47c70fada749",this.requestPermission()}toggleRecord(){console.log(this.btnStatus),"UNDEFINED"===this.btnStatus||"CLOSED"===this.btnStatus?this.startTalk():"CONNECTING"!==this.btnStatus&&"OPEN"!==this.btnStatus||this.finishTalk()}finishTalk(){this.isUserFinish=!0,this.onBeforeFinishTalk&&this.onBeforeFinishTalk(),this.recordStop()}async startTalk(t){this.resultText="",this.resultTextTemp="",this.onBeforeStartTalk&&this.onBeforeStartTalk(),event?.preventDefault(),await this.openWithPriviledge(),setTimeout((()=>{this.connectWebSocket()}),100),this.onAfterStartTalk&&this.onAfterStartTalk()}cancelTalk(){this.onBeforeCancelTalk&&this.onBeforeCancelTalk(),this.recordStop(),this.iatWS?.close(),this.resultText=null,this.onAfterCancelTalk&&this.onAfterCancelTalk()}async recordStart(){this.createRecorder(),await this.openWithPriviledge(),this.recorder.start(),this.changeBtnStatus("OPEN"),this.onAfterRecordStart&&this.onAfterRecordStart()}recordStop(){return new Promise((t=>{clearInterval(this.countdownInterval),this.changeBtnStatus("CLOSED"),this.recorder?.stop((async(e,i)=>{try{this.iatWS.send(JSON.stringify({data:{status:2,format:"audio/L16;rate=16000",encoding:this.encodingType,audio:""}}))}catch(t){}let s=(window.URL||webkitURL).createObjectURL(e);console.log(e,s,"时长:"+i+"ms"),this.recordPcmBlob=e,this.recordWavBlob=await this.pcmBlobToWavBlob(e,44100),this.recordDuration=i,console.log("this.recordWavBlob",this.recordWavBlob),setTimeout((()=>{this.isUserFinish&&(this.onAfterFinishTalk&&this.onAfterFinishTalk(),this.isUserFinish=!1)}),2e3),this.recorder?.close(),this.recorder=null,console.log("localUrl",s),t(!0)}),(e=>{console.log("录音失败:"+e),this.recorder.close(),this.recorder=null,t(null)}))}))}playRecord(){this.playPCM(this.recordPcmBlob,44100)}async pcmBlobToWavBlob(t,e){return new Promise((i=>{let s=new FileReader;s.onload=function(t){let s=t.target.result,o=pcmtoWav(s,e,1,16);i(o)},s.readAsArrayBuffer(t)}))}async playPCM(t,e){let i=await this.pcmBlobToWavBlob(t,e),s=window.URL.createObjectURL(i),o=new Audio;o.src=s,o.play()}async playBuffers(){let t=await this.BuffersToBlob(this.buffers);this.playPCM(t,44100)}BuffersToBlob(t){let e=[];return t.forEach((t=>{t.forEach((t=>{e.push(t)}))})),new Blob([e],{type:"audio/pcm"})}splitAudioData(t){const e=1280,i=Math.ceil(t.length/e),s=[];for(let o=0;o<i;o++){const i=o*e,r=i+e,a=t.slice(i,r);s.push(a)}return s}BufferToBlob(t){return new Blob([t],{type:"audio/pcm"})}createRecorder(){this.recorder||(this.recorder=Recorder({type:this.recordType,sampleRate:44100,bitRate:16,onProcess:(t,e,i,s,o,r)=>{let a=t.length&&t[t.length-1];if(this.buffers=t,a=resampleBuffer(a,44100,16e3),this.iatWS.readyState===this.iatWS.OPEN){if(this.disableASR)return;this.iatWS.send(JSON.stringify({data:{status:1,format:"audio/L16;rate=16000",encoding:this.encodingType,audio:convertFrameBufferToBase64(a)}}))}this.waveClient?.input(t[t.length-1],e,s)}}))}async openWithPriviledge(){return console.log(this.btnStatus),await this.requestPermission(),this.createRecorder(),!!Recorder.IsOpen()||new Promise((t=>{this.recorder.open((()=>{let e=document.querySelector(".record-wave");e&&(console.log(e),Recorder.WaveView&&(this.waveClient=Recorder.WaveView({elem:".record-wave"}))),t(!0)}),((t,e)=>{console.log((e?"UserNotAllow,":"")+"无法录音:"+t)}))}))}getWebSocketUrl(){let t="wss://iat-api.xfyun.cn/v2/iat",e="iat-api.xfyun.cn",i=this.API_KEY,s=this.API_SECRET,o=(new Date).toUTCString(),r=`host: ${e}\ndate: ${o}\nGET /v2/iat HTTP/1.1`,a=CryptoJS.HmacSHA256(r,s),n=CryptoJS.enc.Base64.stringify(a);return t=`${t}?authorization=${btoa(`api_key="${i}", algorithm="hmac-sha256", headers="host date request-line", signature="${n}"`)}&date=${o}&host=${e}`,t}toBase64(t){for(var e="",i=new Uint8Array(t),s=i.byteLength,o=0;o<s;o++)e+=String.fromCharCode(i[o]);return window.btoa(e)}countdown(){let t=60;this.connStatus=`录音中(${t}s)`,this.countdownInterval=setInterval((()=>{t-=1,console.log(t),t<=0?(clearInterval(this.countdownInterval),this.recordStop()):this.connStatus=`录音中(${t}s)`}),1e3)}changeBtnStatus(t){this.btnStatus=t,"CONNECTING"===t?this.connStatus="建立连接中":"OPEN"===t?this.countdown():"CLOSING"===t?this.connStatus="关闭连接中":"CLOSED"===t&&(this.connStatus="开始录音")}renderResult(t){let e=JSON.parse(t);if(e.data&&e.data.result){let t=e.data.result,i="",s=t.ws;for(let t=0;t<s.length;t++)i+=s[t].cw[0].w,console.log(i);t.pgs?("apd"===t.pgs&&(this.resultText=this.resultTextTemp),this.resultTextTemp=this.resultText+i):this.resultText=this.resultText+i,this.resultTextTemp||this.resultText,console.log("diff temp",this.resultTextTemp),console.log("diff result",this.resultText)}0===e.code&&2===e.data.status&&this.iatWS.close(),0!==e.code&&(this.iatWS.close(),console.error(e))}connectWebSocket(){console.log("connectWebSocket");const t=this.getWebSocketUrl();if("WebSocket"in window)this.iatWS=new WebSocket(t);else if(!("MozWebSocket"in window))return void alert("浏览器不支持WebSocket");console.log("connectWebSocket",this.btnStatus),this.changeBtnStatus("CONNECTING"),this.iatWS.onopen=t=>{this.recordStart();var e={common:{app_id:this.APPID},business:{language:"zh_cn",domain:"iat",accent:"mandarin",vad_eos:5e3,dwa:"wpgs"},data:{status:0,format:"audio/L16;rate=16000",encoding:this.encodingType}};this.iatWS.send(JSON.stringify(e))},this.iatWS.onmessage=t=>{console.log("onmessage"+this.resultText),this.renderResult(t.data)},this.iatWS.onerror=t=>{console.error("error",t),this.recordStop(),this.changeBtnStatus("CLOSED")},this.iatWS.onclose=async t=>{console.log("onclose"+this.resultText),console.error("close",t),await this.recordStop(),this.changeBtnStatus("CLOSED")}}isCapacitor(){return this.platform.is("capacitor")||this.platform.is("cordova")}async requestPermission(){if(this.isCapacitor())try{await this.requestStoagePermission(),await this.requestCameraPermission(),await this.requestMicPermission(),await this.requestRecordAudioPermission()}catch(t){console.error(t)}}async requestRecordAudioPermission(){let t=await this.diagnostic.requestRuntimePermissions([this.diagnostic.permission.RECORD_AUDIO]);console.log("record permission request:",t)}async requestMicPermission(){let t=await this.diagnostic.isMicrophoneAuthorized();if(console.log("permisson_MIC:",t),!t){await this.diagnostic.requestMicrophoneAuthorization()}}async requestStoagePermission(){let t=await this.diagnostic.isExternalStorageAuthorized();if(console.log("permisson_STORAGE:",t),!t){await this.diagnostic.requestExternalStorageAuthorization()}}async requestCameraPermission(){let t=await this.diagnostic.isCameraAuthorized();if(console.log("permisson_Camera:",t),!t){await this.diagnostic.requestCameraAuthorization()}}static{this.ɵfac=i0.ɵɵngDeclareFactory({minVersion:"12.0.0",version:"17.3.12",ngImport:i0,type:FmodeVoiceService,deps:[{token:i1.Platform},{token:i2.Diagnostic}],target:i0.ɵɵFactoryTarget.Injectable})}static{this.ɵprov=i0.ɵɵngDeclareInjectable({minVersion:"12.0.0",version:"17.3.12",ngImport:i0,type:FmodeVoiceService,providedIn:"root"})}}i0.ɵɵngDeclareClassMetadata({minVersion:"12.0.0",version:"17.3.12",ngImport:i0,type:FmodeVoiceService,decorators:[{type:Injectable,args:[{providedIn:"root"}]}],ctorParameters:()=>[{type:i1.Platform},{type:i2.Diagnostic}]});
|
|
9
9
|
var MODULE_PATH_NEED = `6K+l5paH5Lu25piv5pys6aG555uu55qE5LiA6YOo5YiGIFRoaXMgZmlsZSBpcyBwYXJ0IG9mIHRoZSBDb21wb25lbnRzIGluIEZtb2RlIEluYy4KICAgIOeJiOadg+aJgOaciSDCqSDmnKrmnaXpo57pqawgwqkg5rGf6KW/6ISR5o6n56eR5oqA5pyJ6ZmQ5YWs5Y+4IENvcHlyaWdodCDCqSBGbW9kZSBUZWNobm9sb2d5IENvLiwgTHRkLgogICAg5L+d55WZ5omA5pyJ5p2D5YipIEFsbCBSaWdodHMgUmVzZXJ2ZWQuCiAgICDkuKXnpoHlnKjmnKrnu4/mjojmnYPnmoTmg4XlhrXkuIvvvIzpgJrov4fku7vkvZXlqpLku4vlpI3liLbmraTmlofku7YgVW5hdXRob3JpemVkIGNvcHlpbmcgb2YgdGhpcyBmaWxlLCB2aWEgYW55IG1lZGl1bSBpcyBzdHJpY3RseSBwcm9oaWJpdGVkCiAgICDor6Xmlofku7bmmK/kuJPmnInnmoTmnLrlr4bmlofku7YgUHJvcHJpZXRhcnkgYW5kIGNvbmZpZGVudGlhbAogICAKICAgIENvcHlyaWdodCAyMDIxLW5vdyBGbW9kZSBJbmMuIHN1cHBvcnRAZm1vZGUuY24uIDE4NjA3MDA3MDczLgogICAg5L+d55WZ5omA5pyJ5p2D5YipIEFsbCByaWdodHMgcmVzZXJ2ZWQuCgogICAgUEFUSDovaG9tZS9yeWFuL3dvcmtzcGFjZS9ub3ZhL25vdmEtYWRtaW4vZGlzdC9mbW9kZS1uZy9lc20yMDIyL2xpYi9haWdjL3ZvaWNlL2Ztb2RlLXZvaWNlLnNlcnZpY2UubWpz`
|
|
10
10
|
|
|
@@ -5,6 +5,6 @@
|
|
|
5
5
|
* 保留所有权利 All Rights Reserved.
|
|
6
6
|
* /home/ryan/workspace/nova/nova-admin/dist/fmode-ng/esm2022/lib/aigc/voice/lib/pcm2wav.mjs
|
|
7
7
|
*/
|
|
8
|
-
export function pcmtoWav(e,n,u,r){let
|
|
8
|
+
export function pcmtoWav(e,n,u,r){let a={chunkId:[82,73,70,70],chunkSize:0,format:[87,65,86,69],subChunk1Id:[102,109,116,32],subChunk1Size:16,audioFormat:1,numChannels:u||1,sampleRate:n||16e3,byteRate:0,blockAlign:0,bitsPerSample:r||16,subChunk2Id:[100,97,116,97],subChunk2Size:0};function u32ToArray(e){return[255&e,e>>8&255,e>>16&255,e>>24&255]}function u16ToArray(e){return[255&e,e>>8&255]}let t=new Uint8Array(e);a.blockAlign=a.numChannels*a.bitsPerSample>>3,a.byteRate=a.blockAlign*a.sampleRate,a.subChunk2Size=t.length*(a.bitsPerSample>>3),a.chunkSize=36+a.subChunk2Size;let o=a.chunkId.concat(u32ToArray(a.chunkSize),a.format,a.subChunk1Id,u32ToArray(a.subChunk1Size),u16ToArray(a.audioFormat),u16ToArray(a.numChannels),u32ToArray(a.sampleRate),u32ToArray(a.byteRate),u16ToArray(a.blockAlign),u16ToArray(a.bitsPerSample),a.subChunk2Id,u32ToArray(a.subChunk2Size)),i=new Uint8Array(o),l=new Uint8Array(i.length+t.length);return l.set(i),l.set(t,i.length),new Blob([l],{type:"audio/wav"})}
|
|
9
9
|
var MODULE_PATH_NEED = `6K+l5paH5Lu25piv5pys6aG555uu55qE5LiA6YOo5YiGIFRoaXMgZmlsZSBpcyBwYXJ0IG9mIHRoZSBDb21wb25lbnRzIGluIEZtb2RlIEluYy4KICAgIOeJiOadg+aJgOaciSDCqSDmnKrmnaXpo57pqawgwqkg5rGf6KW/6ISR5o6n56eR5oqA5pyJ6ZmQ5YWs5Y+4IENvcHlyaWdodCDCqSBGbW9kZSBUZWNobm9sb2d5IENvLiwgTHRkLgogICAg5L+d55WZ5omA5pyJ5p2D5YipIEFsbCBSaWdodHMgUmVzZXJ2ZWQuCiAgICDkuKXnpoHlnKjmnKrnu4/mjojmnYPnmoTmg4XlhrXkuIvvvIzpgJrov4fku7vkvZXlqpLku4vlpI3liLbmraTmlofku7YgVW5hdXRob3JpemVkIGNvcHlpbmcgb2YgdGhpcyBmaWxlLCB2aWEgYW55IG1lZGl1bSBpcyBzdHJpY3RseSBwcm9oaWJpdGVkCiAgICDor6Xmlofku7bmmK/kuJPmnInnmoTmnLrlr4bmlofku7YgUHJvcHJpZXRhcnkgYW5kIGNvbmZpZGVudGlhbAogICAKICAgIENvcHlyaWdodCAyMDIxLW5vdyBGbW9kZSBJbmMuIHN1cHBvcnRAZm1vZGUuY24uIDE4NjA3MDA3MDczLgogICAg5L+d55WZ5omA5pyJ5p2D5YipIEFsbCByaWdodHMgcmVzZXJ2ZWQuCgogICAgUEFUSDovaG9tZS9yeWFuL3dvcmtzcGFjZS9ub3ZhL25vdmEtYWRtaW4vZGlzdC9mbW9kZS1uZy9lc20yMDIyL2xpYi9haWdjL3ZvaWNlL2xpYi9wY20yd2F2Lm1qcw==`
|
|
10
10
|
|
|
@@ -5,6 +5,6 @@
|
|
|
5
5
|
* 保留所有权利 All Rights Reserved.
|
|
6
6
|
* /home/ryan/workspace/nova/nova-admin/dist/fmode-ng/esm2022/lib/aigc/voice/tts/fmode-tts-class.mjs
|
|
7
7
|
*/
|
|
8
|
-
import{SpeechSynthesizer}from"microsoft-cognitiveservices-speech-sdk/distrib/lib/src/sdk/SpeechSynthesizer";import{SpeechConfig}from"microsoft-cognitiveservices-speech-sdk/distrib/lib/src/sdk/SpeechConfig";import{ResultReason}from"microsoft-cognitiveservices-speech-sdk/distrib/lib/src/sdk/ResultReason";export class FmodeTTS{constructor(e){this.subscriptionKey=e?.subscriptionKey,this.authorizationToken=e?.token,this.region=e?.region,this.subscriptionKey&&(this.speechConfig=SpeechConfig.fromSubscription(this.subscriptionKey,this.region)),this.authorizationToken&&(this.speechConfig=SpeechConfig.fromAuthorizationToken(this.authorizationToken,this.region)),this.synthesizer=new SpeechSynthesizer(this.speechConfig)}extractSSMLContent(e){var
|
|
8
|
+
import{SpeechSynthesizer}from"microsoft-cognitiveservices-speech-sdk/distrib/lib/src/sdk/SpeechSynthesizer";import{SpeechConfig}from"microsoft-cognitiveservices-speech-sdk/distrib/lib/src/sdk/SpeechConfig";import{ResultReason}from"microsoft-cognitiveservices-speech-sdk/distrib/lib/src/sdk/ResultReason";import Parse from"parse";export class FmodeTTS{constructor(e,t){this.uploadServ=t,this.subscriptionKey=e?.subscriptionKey,this.authorizationToken=e?.token,this.region=e?.region,this.subscriptionKey&&(this.speechConfig=SpeechConfig.fromSubscription(this.subscriptionKey,this.region)),this.authorizationToken&&(this.speechConfig=SpeechConfig.fromAuthorizationToken(this.authorizationToken,this.region)),this.synthesizer=new SpeechSynthesizer(this.speechConfig)}extractTextFromXML(e){const t=/>([^<]+)</g;let o,i=[];for(;null!==(o=t.exec(e));){const e=o[1].trim();e&&i.push(e)}return i.join(" ")}extractSSMLContent(e){var t=e.match(/<speak.*?<\/speak>/s);return t?t[0]:e}async speakAsync(e,t,o){if(!t){let o=Parse.Object.extend("ChatVoice");(t=new o).set("ssml",e),t.set("content",this.extractTextFromXML(e));let i=localStorage.getItem("company");i&&t.set("company",{__type:"Pointer",className:"Company",objectId:i}),Parse.User.current()?.id&&t.set("user",Parse.User.current().toPointer())}if(!t?.get("voiceFile")){let o=[];t?.get("content")&&o.push({content:t?.get("content")}),e&&o.push({ssml:e});let i=Parse.Query.fromJSON("ChatVoice",{include:"voiceFile",where:{$or:o}}),s=await i.first();t.set("voiceFile",s?.get("voiceFile")?.toPointer())}return t?.get("voiceFile")?(this.playAudioData(t?.get("voiceFile")?.get("url"),t,o),t):(e=this.extractSSMLContent(e),new Promise(((i,s)=>{const n=Date.now();let r="speakTextAsync";e?.indexOf("<")>-1&&(r="speakSsmlAsync"),this.eventMap?.onSpeakBefore&&this.eventMap?.onSpeakBefore(),this.synthesizer[r](e,(e=>{if(console.log(e),e.reason===ResultReason.SynthesizingAudioCompleted){this.eventMap?.onAudioCompleted&&this.eventMap?.onAudioCompleted();const s=Date.now();let r=e?.audioData;t.set("duration",Number(e?.audioDuration)/1e4),this.playAudioData(r,t,o),console.log(`Audio synthesis finished. Duration: ${s-n} ms`),i(t)}else s(`Speech synthesis failed. Reason: ${e.errorDetails}`)}),(e=>{s(`Error occurred during synthesis: ${e}`)}))})))}onBreak(e){}playAudioData(e,t,o){let i;if(console.log("audioData",e),!(e?.indexOf&&e?.indexOf("http")>=-1)){let o=new Blob([e],{type:"audio/wav"});return i=URL.createObjectURL(o),void this.uploadAndSaveVoice(o,t)}i=e;let s=new Audio(i);s.onloadeddata=()=>{let e=1e3*s.duration;console.log("duration",e),t?.get("duration")||(t?.set("duration",e),t?.save()),o?.onLoaded&&o?.onLoaded(s)},s.onpause=()=>{o?.onStop&&o?.onStop()},s.onended=()=>{o?.onStop&&o?.onStop()},s.onclose=()=>{o?.onStop&&o?.onStop()},s.play()}async uploadAndSaveVoice(e,t){if(this.uploadServ){let o=t?.id||this.uploadServ?.genMd5(t?.get("content")||t?.get("ssml")),i=new Date,s=o+i.getFullYear()+(i.getMonth()+1)+i.getDate()+i.getHours()+i.getMinutes()+i.getSeconds()+".wav",n=new File([e],s,{type:"audio/wav"}),r=await this.uploadServ.upload(n,(e=>{console.log(e)})),a={__type:"Pointer",className:"Attachment",objectId:r?.id};a?.objectId&&(t.set("voiceFile",a),t=await t.save())}}}
|
|
9
9
|
var MODULE_PATH_NEED = `6K+l5paH5Lu25piv5pys6aG555uu55qE5LiA6YOo5YiGIFRoaXMgZmlsZSBpcyBwYXJ0IG9mIHRoZSBDb21wb25lbnRzIGluIEZtb2RlIEluYy4KICAgIOeJiOadg+aJgOaciSDCqSDmnKrmnaXpo57pqawgwqkg5rGf6KW/6ISR5o6n56eR5oqA5pyJ6ZmQ5YWs5Y+4IENvcHlyaWdodCDCqSBGbW9kZSBUZWNobm9sb2d5IENvLiwgTHRkLgogICAg5L+d55WZ5omA5pyJ5p2D5YipIEFsbCBSaWdodHMgUmVzZXJ2ZWQuCiAgICDkuKXnpoHlnKjmnKrnu4/mjojmnYPnmoTmg4XlhrXkuIvvvIzpgJrov4fku7vkvZXlqpLku4vlpI3liLbmraTmlofku7YgVW5hdXRob3JpemVkIGNvcHlpbmcgb2YgdGhpcyBmaWxlLCB2aWEgYW55IG1lZGl1bSBpcyBzdHJpY3RseSBwcm9oaWJpdGVkCiAgICDor6Xmlofku7bmmK/kuJPmnInnmoTmnLrlr4bmlofku7YgUHJvcHJpZXRhcnkgYW5kIGNvbmZpZGVudGlhbAogICAKICAgIENvcHlyaWdodCAyMDIxLW5vdyBGbW9kZSBJbmMuIHN1cHBvcnRAZm1vZGUuY24uIDE4NjA3MDA3MDczLgogICAg5L+d55WZ5omA5pyJ5p2D5YipIEFsbCByaWdodHMgcmVzZXJ2ZWQuCgogICAgUEFUSDovaG9tZS9yeWFuL3dvcmtzcGFjZS9ub3ZhL25vdmEtYWRtaW4vZGlzdC9mbW9kZS1uZy9lc20yMDIyL2xpYi9haWdjL3ZvaWNlL3R0cy9mbW9kZS10dHMtY2xhc3MubWpz`
|
|
10
10
|
|
|
@@ -5,6 +5,6 @@
|
|
|
5
5
|
* 保留所有权利 All Rights Reserved.
|
|
6
6
|
* /home/ryan/workspace/nova/nova-admin/dist/fmode-ng/esm2022/lib/storage/service-upload/nova-upload.service.mjs
|
|
7
7
|
*/
|
|
8
|
-
import{DatePipe}from"@angular/common";import{Injectable}from"@angular/core";import{Platform}from"@ionic/angular";import{Camera,CameraResultType,CameraSource}from"@capacitor/camera";import{Capacitor}from"@capacitor/core";import{Filesystem}from"@capacitor/filesystem";import{FilesystemWeb}from"@capacitor/filesystem/dist/esm/web.js";import*as qiniu from"qiniu-js";import Parse from"parse";import{Diagnostic}from"@awesome-cordova-plugins/diagnostic/ngx";import{MediaCapture}from"@awesome-cordova-plugins/media-capture";import{calcFileMd5}from"./util-file-md5";import*as i0 from"@angular/core";import*as i1 from"@ionic/angular";import*as i2 from"@awesome-cordova-plugins/diagnostic/ngx";export class NovaUploadService{constructor(e,i){this.platform=e,this.diagnostic=i,this.maxSize=5242880,this.getUptoken(!0),this.requestPermission(),this.queryDomain()}async upload(e,i){let t,a=e.type,o=e.name?.split("."),
|
|
8
|
+
import{DatePipe}from"@angular/common";import{Injectable}from"@angular/core";import{Platform}from"@ionic/angular";import{Camera,CameraResultType,CameraSource}from"@capacitor/camera";import{Capacitor}from"@capacitor/core";import{Filesystem}from"@capacitor/filesystem";import{FilesystemWeb}from"@capacitor/filesystem/dist/esm/web.js";import*as qiniu from"qiniu-js";import Parse from"parse";import{Diagnostic}from"@awesome-cordova-plugins/diagnostic/ngx";import CryptoJS from"crypto-js";import{MediaCapture}from"@awesome-cordova-plugins/media-capture";import{calcFileMd5}from"./util-file-md5";import*as i0 from"@angular/core";import*as i1 from"@ionic/angular";import*as i2 from"@awesome-cordova-plugins/diagnostic/ngx";export class NovaUploadService{constructor(e,i){this.platform=e,this.diagnostic=i,this.maxSize=5242880,this.getUptoken(!0),this.requestPermission(),this.queryDomain()}async upload(e,i){let t,a=e.type,o=e.name?.split("."),r=o[o.length-1];try{t=await calcFileMd5(e)}catch(e){}let n,s,l=this.fileToBlob(e);return n=a.indexOf("image")>-1?await this.saveQiniuImageFile(l,r,null,i):await this.saveQiniuMediaFile(e,l,null,i),n.md5=t,n?.url?.indexOf("undefined")>-1&&(n.url=(this.qiniuDomain||"https://file-cloud.fmode.cn/")+n.url.replace("undefined/","")),n?.url&&(s=await this.saveAttachment(n,this.qiniuDomain,null,this.getCompanyId())),s?.id&&(n.id=s?.id),n}isCapacitor(){return this.platform.is("capacitor")||this.platform.is("cordova")}async requestPermission(){this.isCapacitor()&&(await this.requestStoagePermission(),await this.requestCameraPermission())}async requestStoagePermission(){let e=await this.diagnostic.isExternalStorageAuthorized();if(console.log("permisson_STORAGE:",e),!e){await this.diagnostic.requestExternalStorageAuthorization()}}async requestCameraPermission(){let e=await this.diagnostic.isCameraAuthorized();if(console.log("permisson_Camera:",e),!e){await this.diagnostic.requestCameraAuthorization()}}async getUptoken(e=!1){if(console.log("getUptoken"),this.qiniuConf||e)try{console.log(this.getCompanyId());let e=await Parse.Cloud.run("qiniu_uptoken",{company:this.getCompanyId()});console.log(e),this.qiniuConf=e}catch(e){console.error(e)}}genFileKey(e,i){let t=new Date,a=new DatePipe("en");return i||(i=this.getCompanyId()),i+"/"+a.transform(t,"yMMdd")+"/"+String(e.id).substr(20,6)+a.transform(t,"hhmmssSSS")+/\.[^\.]+/.exec(e.name)}getCompanyId(){if(this.company)return this.company;return localStorage.getItem("company")}async queryDomain(){let e=new Parse.Query("Company"),i=await e.get(this.getCompanyId());i.get("configQiniu")&&i.get("configQiniu").domain?(console.log(i.get("configQiniu").domain),this.qiniuDomain=i.get("configQiniu").domain):this.qiniuDomain="https://file-cloud.fmode.cn"}async saveAttachment(e,i,t,a){let o=e.url;o.startsWith("http")||(o=i+o),o=o.replace(/undefined\//,""),a||(a=localStorage.getItem("company"));let r=Parse.User.current(),n=new Parse.Query("Attachment");n.equalTo("url",o);let s=await n.first();if(s&&s.id)return console.error("该文件已存在,无需重复上传"),s;return s=new(Parse.Object.extend("Attachment")),s.set("size",e.size),s.set("url",o),s.set("name",e.name),s.set("mime",e.type),s.set("md5",e?.md5),r?.id&&s.set("user",r.toPointer()),a&&s.set("company",{__type:"Pointer",className:"Company",objectId:localStorage.getItem("company")}),t&&s.set("category",{__type:"Pointer",className:"Category",objectId:t}),await s.save()}async captureVideo(e){if(this.qiniuConf=e,!this.isCapacitor())return;let i=await this.cameraCaptureVideoFile(),t=await this.getMediaFileDataString(i);return await this.saveQiniuMediaFile(i,t)}async cameraCaptureVideoFile(){let e=MediaCapture,i=await e.captureVideo({limit:1});return i?.length>0?i[0]:null}async getMediaFileDataString(e){new FilesystemWeb,e.fullPath.replaceAll("///","//");let i=await Filesystem.stat({path:e.fullPath}),t=Capacitor.convertFileSrc(i.uri),a=await fetch(t),o=await a.blob();if(console.log(o.size),console.log(JSON.stringify(i)),console.log(JSON.stringify(a)),console.log(JSON.stringify(Object.keys(a))),o)return o;throw"读取文件失败"}async takePicture(e){if(this.qiniuConf=e,!this.isCapacitor())return;await this.getUptoken();let i=await this.cameraTakePictureDataUrl();if(!i?.dataUrl)return;let t=await this.base64ToBlob(i?.dataUrl),a=await this.saveQiniuImageFile(t,i?.format);return console.log(JSON.stringify(a)),a}async cameraTakePictureDataUrl(){if(!this.isCapacitor())return;return await Camera.getPhoto({quality:90,allowEditing:!1,source:CameraSource.Camera,resultType:CameraResultType.DataUrl})}async saveQiniuImageFile(e,i,t,a){let o=this.maxSize;if(e.size>o)throw await console.log("照片过大,超出限制5MB"),"超出文件大小";let r=new DatePipe("en").transform(new Date,"yyyyMMddHHmmss"),n=`${r}.${i}`,s=`image/${i}`,l={fname:n,params:{},mimeType:"image/*"},c={useCdnDomain:!0,forceDirect:!0};console.log(this.qiniuConf);let m=this.genFileKey({id:r,name:n});return console.log("图片上传前"),new Promise(((i,t)=>{console.log("进入了上传"),qiniu.upload(e,m,this.qiniuConf?.uptoken,l,c).subscribe({next:e=>{console.log(e),a&&a(e)},error:async e=>{console.log(e)},complete:t=>{console.log("上传完成"),console.log(`${this.qiniuConf?.domain}${t.key}`),t.url=`${this.qiniuConf?.domain}${t.key}`,t.name=n,t.type=s,t.size=e.size,i(t)}})}))}async saveQiniuMediaFile(e,i,t,a){let o=e.name,r=e.type;if(e.size>104857600)throw console.log("视频过大,超出限制100MB"),"超出文件大小";let n=new DatePipe("en").transform(new Date,"yyyyMMddHHmmss"),s={fname:o,params:{},mimeType:r},l={useCdnDomain:!0,forceDirect:!0},c=this.genFileKey({id:n,name:o});return console.log("图片上传前"),console.log(e.name,i.size),console.log(i.size),console.log(i.size/1024/1024),new Promise(((t,o)=>{console.log("进入了上传"),qiniu.upload(i,c,this.qiniuConf?.uptoken,s,l).subscribe({next:e=>{console.log("主要用来展示进度"),a&&a(e),console.log(JSON.stringify(e))},error:async e=>{console.log("上传失败"),console.log(JSON.stringify(e))},complete:i=>{console.log("上传完成"),console.log(JSON.stringify(i)),e.key=i.key,console.log(e.type),e.url=`${this.qiniuConf?.domain}${i.key}`,console.log(e.url),t(e)}})}))}async base64ToBlobType(e,i){let t=await fetch(`data:${i};base64,${e}`);return await t.blob()}async base64ToBlob(e){let i=await fetch(e);return await i.blob()}fileToBlob(e){const i=e.slice(0,e.size,e.type);return new Blob([i],{type:e.type})}genMd5(e){return CryptoJS.MD5(e).toString()}static{this.ɵfac=i0.ɵɵngDeclareFactory({minVersion:"12.0.0",version:"17.3.12",ngImport:i0,type:NovaUploadService,deps:[{token:i1.Platform},{token:i2.Diagnostic}],target:i0.ɵɵFactoryTarget.Injectable})}static{this.ɵprov=i0.ɵɵngDeclareInjectable({minVersion:"12.0.0",version:"17.3.12",ngImport:i0,type:NovaUploadService,providedIn:"root"})}}i0.ɵɵngDeclareClassMetadata({minVersion:"12.0.0",version:"17.3.12",ngImport:i0,type:NovaUploadService,decorators:[{type:Injectable,args:[{providedIn:"root"}]}],ctorParameters:()=>[{type:i1.Platform},{type:i2.Diagnostic}]});
|
|
9
9
|
var MODULE_PATH_NEED = `6K+l5paH5Lu25piv5pys6aG555uu55qE5LiA6YOo5YiGIFRoaXMgZmlsZSBpcyBwYXJ0IG9mIHRoZSBDb21wb25lbnRzIGluIEZtb2RlIEluYy4KICAgIOeJiOadg+aJgOaciSDCqSDmnKrmnaXpo57pqawgwqkg5rGf6KW/6ISR5o6n56eR5oqA5pyJ6ZmQ5YWs5Y+4IENvcHlyaWdodCDCqSBGbW9kZSBUZWNobm9sb2d5IENvLiwgTHRkLgogICAg5L+d55WZ5omA5pyJ5p2D5YipIEFsbCBSaWdodHMgUmVzZXJ2ZWQuCiAgICDkuKXnpoHlnKjmnKrnu4/mjojmnYPnmoTmg4XlhrXkuIvvvIzpgJrov4fku7vkvZXlqpLku4vlpI3liLbmraTmlofku7YgVW5hdXRob3JpemVkIGNvcHlpbmcgb2YgdGhpcyBmaWxlLCB2aWEgYW55IG1lZGl1bSBpcyBzdHJpY3RseSBwcm9oaWJpdGVkCiAgICDor6Xmlofku7bmmK/kuJPmnInnmoTmnLrlr4bmlofku7YgUHJvcHJpZXRhcnkgYW5kIGNvbmZpZGVudGlhbAogICAKICAgIENvcHlyaWdodCAyMDIxLW5vdyBGbW9kZSBJbmMuIHN1cHBvcnRAZm1vZGUuY24uIDE4NjA3MDA3MDczLgogICAg5L+d55WZ5omA5pyJ5p2D5YipIEFsbCByaWdodHMgcmVzZXJ2ZWQuCgogICAgUEFUSDovaG9tZS9yeWFuL3dvcmtzcGFjZS9ub3ZhL25vdmEtYWRtaW4vZGlzdC9mbW9kZS1uZy9lc20yMDIyL2xpYi9zdG9yYWdlL3NlcnZpY2UtdXBsb2FkL25vdmEtdXBsb2FkLnNlcnZpY2UubWpz`
|
|
10
10
|
|