@graphen.ai/aiia-sdk 1.0.7 → 1.0.9
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/dist/browser.cjs +1 -1
- package/dist/browser.d.ts +20 -16
- package/dist/browser.mjs +1 -1
- package/dist/node.cjs +1 -1
- package/dist/node.d.ts +4 -4
- package/dist/node.mjs +1 -1
- package/package.json +3 -3
package/dist/browser.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var e,t=require("lodash"),i=require("moment"),s=require("rxjs"),a=require("@mediapipe/tasks-vision"),n=require("socket.io-client");!function(e){e["網絡錯誤"]="ERROR CODE: 500",e["環境錯誤(browser)"]="ERROR CODE: 1001",e["Worklet模組載入失敗"]="ERROR CODE: 1002",e["未獲得媒體裝置權限"]="ERROR CODE: 1003",e["實例已摧毀"]="ERROR CODE: 1004",e["環境錯誤(nodejs)"]="ERROR CODE: 2001",e["未提供憑證"]="ERROR CODE: 2002",e["獲取權杖失敗"]="ERROR CODE: 2003",e["沒有可用專案"]="ERROR CODE: 2004",e["重複初始化"]="ERROR CODE: 2005"}(e||(e={}));const o="undefined"!=typeof window;"undefined"!=typeof process&&null!=process.versions&&process.versions.node;class r{config;get webserver(){return this.config.webserver}get environment(){return this.config.env}get license(){return this.config.license??""}get port(){return function(e,t){const i=t??NaN;if(e){const t=parseInt(e);return isNaN(t)?i:t}return i}(process.env.PORT,this.config.port??3e3)}get ws_url(){return this.config.ws_url??""}get worklet_url(){return this.config.worklet_url??""}get project(){const{project:e}=t.assign({},this.config);return void 0===e?{specific:!1}:"string"==typeof e?{name:e,id:e,specific:!1}:t.assign({},e,{specific:!0})}get endPoint(){return this.environment,{api:"https://aiia-content-management-dev-21193779403.asia-east1.run.app",socket:"wss://graphen-agentic-workflow-dev-21193779403.asia-east1.run.app"}}get debug(){return this.config.debug??!0}get mediaStream(){return this.config.mediaStream}get chunkTimeInSeconds(){return"number"==typeof this.config.chunkTimeInSeconds&&this.config.chunkTimeInSeconds>0?this.config.chunkTimeInSeconds:1}get llmSampleRate(){return"number"==typeof this.config.llmSampleRate&&this.config.llmSampleRate>0?this.config.llmSampleRate:16e3}get faceDetection(){return t.assign({frame:3,confidence:80},this.config.faceDetection)}get autoClearSubtitle(){const{user:e,aiia:i}=t.assign({},this.config.autoClearSubtitle);let s=3e3,a=3e3;return"number"==typeof e?s=e>=0?e:s:!1===e&&(s=NaN),"number"==typeof i?a=i>=0?i:a:!1===i&&(a=NaN),{userDelayTime:s,aiiaDelayTime:a}}constructor(e){this.config=e}}var c;!function(e){e["通道、雲端服務、麥克風都已就緒"]="CODE: 1000",e["未取得媒體裝置權限,請重新設定或忽略此訊息"]="CODE: 1001",e["請訂閱專案列表"]="CODE: 1002",e["初始化必要依賴"]="CODE: 1010",e["指定依賴已完成"]="CODE: 1011",e["啟動SDK"]="CODE: 1012",e["結束SDK"]="CODE: 1013",e["安全的關閉連線"]="CODE: 2000",e["無副作用的關閉連線"]="CODE: 2001",e["與雲端的服務中斷"]="CODE: 2002",e["沒有授權,請聯繫Graphen"]="CODE: 2003",e["自動重新連線連端服務"]="CODE: 2004",e["未預期的斷線,請聯繫Graphen"]="CODE: 2005",e["開始初始化必要模組"]="CODE: 2010",e["指定模組初始化完成"]="CODE: 2011",e["啟動服務"]="CODE: 2012",e["服務結束"]="CODE: 2013"}(c||(c={}));const u={code:console.log,debug:console.log,info:console.log,warn:console.warn,error:console.error,fatal:console.error},h=[];const d=new Proxy(u,{get(e,s,a){if("string"==typeof s)switch(s){case"debug":case"info":case"error":case"warn":case"fatal":case"code":return(...a)=>{const n=i().format("yyyy-MM-DD[T]HH:mm:ss");t.forEach(h,e=>{e.next(s,n,...a)}),e[s](`[Aiia::${s}_${n}_]`,...a)};default:return}},set:(e,t,i,s)=>!0});var l,b;function p(e,t,i){return Math.max(i,Math.min(e,t))}function m(e){const t=new DataView(e.buffer),i=[];for(let s=0;s<e.length;s++){const e=t.getInt16(2*s,!0)/32768;i.push(e)}return new Float32Array(i)}function f(e){const t=new DataView(e.buffer),i=[];for(let s=0;s<e.length;s++){const e=p(32768*t.getFloat32(4*s,!0),32767,-32768);i.push(e)}return i}!function(e){e[e.unknow=0]="unknow",e[e.allowed=1]="allowed",e[e.rejected=2]="rejected"}(l||(l={}));class g{audioContext;type="audio";currentNode;bufferQueue;gainNode;set volume(e){e<0&&(e=0),e>100&&(e=100),this.gainNode.gain.value=e/100}get volume(){return Math.round(100*this.gainNode.gain.value)}constructor(e){this.audioContext=e,this.bufferQueue=[],this.currentNode=null,this.gainNode=this.audioContext.createGain(),this.gainNode.connect(this.audioContext.destination)}addBuffer(e){this.bufferQueue.push(e)}addBufferByFloat32(e,t=1){const i=e.length,s=this.audioContext.sampleRate,a=this.audioContext.createBuffer(t,i,s),n=e.slice();for(let e=0;e<t;e++)a.copyToChannel(n,e);this.addBuffer(a)}addBufferByInt16(e,t=1){this.addBufferByFloat32(m(e),t)}startSpeech(){null===this.currentNode&&this.continue()}pauseSpeech(){this.audioContext.suspend().catch(e=>{d.error("pause speech fail",e)})}resumeSpeech(){this.audioContext.resume().catch(e=>{d.error("resume speech fail",e)})}nextSpeech(){this.bufferQueue.length>0&&(this.clearNode(),this.continue())}stopSpeech(){this.bufferQueue=[],this.clearNode()}clearNode(){this.currentNode&&(this.currentNode.onended=function(){},this.currentNode.stop(),this.currentNode.disconnect(),this.currentNode=null)}continue(){const e=this.bufferQueue.shift();if(e){this.currentNode=this.audioContext.createBufferSource(),this.currentNode.buffer=e,this.currentNode.connect(this.gainNode);const t=this.continue.bind(this);this.currentNode.onended=t,this.currentNode.start()}else this.clearNode()}}class S{stream;audioContext;type="vad";workletNode;source;isRecording=!1;outputSampleRate;chunkTimeInSeconds;pcmSub;get pcm(){return this.pcmSub.asObservable()}constructor(e,t,i){this.stream=e,this.audioContext=t,this.outputSampleRate=i?.llmSampleRate??16e3,this.chunkTimeInSeconds=i?.chunkTimeInSeconds??1,this.pcmSub=new s.Subject}async startRecord(){this.isRecording||(this.source=this.audioContext.createMediaStreamSource(this.stream),this.workletNode=new AudioWorkletNode(this.audioContext,"aiia-vad",{processorOptions:{outputSampleRate:this.outputSampleRate,chunkTimeInSeconds:this.chunkTimeInSeconds}}),this.source.connect(this.workletNode),this.workletNode.port.onmessage=e=>{const{float32:t}=e.data;t&&this.pcmSub.next(new Float32Array(t))},this.isRecording=!0)}stopRecord(){this.isRecording&&(this.workletNode&&(this.workletNode.port.onmessage=null,this.workletNode.disconnect()),this.source&&this.source.disconnect(),this.isRecording=!1)}}class y{type="camera";confidenceScore;aiiaCamera;get detections(){return this.aiiaCamera.detections}get faceResult(){const e=this.confidenceScore;return this.aiiaCamera.detections.pipe(s.map(i=>function(e,i){const s=t.filter(e,e=>{const s=e.categories[0]?.score??0,a=e.keypoints,n=100*s>=i,o=t.some(a,e=>"leftEye"===e.label||"rightEye"===e.label);return n&&o});if(0===s.length)return null;const a=t.compact(t.map(s,e=>{if(void 0!==e.boundingBox){const t=e.boundingBox,i=t.width*t.height,s=e.categories[0]?.score??0;return{...t,area:i,confidenceScore:100*s}}return null}));return t.sortBy(a,["area","confidenceScore"])[0]??null}(i,e)))}constructor(e,i,s){const{frame:a,confidence:n}=t.assign({frame:3,confidence:80},s);this.confidenceScore=n,this.aiiaCamera=new C,this.aiiaCamera.setAttribute("frequency",`${a}`),this.aiiaCamera.setStream(e),this.aiiaCamera.setFaceDetector(i),document.body.append(this.aiiaCamera)}startCapture(){this.aiiaCamera.startCapture()}stopCapture(){this.aiiaCamera.stopCapture()}onDestroy(){this.aiiaCamera.remove()}}class C extends HTMLElement{static get observedAttributes(){return["debug","frequency"]}_shadow;_containerEl;_styleEl;_videoEl;_isDebug;_debugElements;_analyzeInterval;_frameCount;_loopFrameID;_faceDetector;_detectionsSub;_isReady;get detections(){return this._detectionsSub.asObservable()}constructor(){super(),this._isDebug=!1,this._isReady=!1,this._shadow=this.attachShadow({mode:"open"}),this._containerEl=document.createElement("div"),this._videoEl=document.createElement("video"),this._styleEl=document.createElement("style"),this._debugElements=[],this._loopFrameID=null,this._analyzeInterval=3,this._frameCount=this._analyzeInterval,this._detectionsSub=new s.Subject,this.predictWebcam=this.predictWebcam.bind(this),this._settingContainer(),this._settingStyle(),this._settingVideo(),this._containerEl.appendChild(this._videoEl),this._shadow.appendChild(this._styleEl),this._shadow.appendChild(this._containerEl)}connectedCallback(){this.startCapture()}disconnectedCallback(){this.stopCapture()}attributeChangedCallback(e,t,i){switch(e){case"debug":this._isDebug=null!==i&&("true"===i||""===i),this._containerEl.style.visibility=this._isDebug?"visible":"hidden";break;case"frequency":{const e=Number(i);!isNaN(e)&&e>0&&(this._analyzeInterval=e,this._frameCount=this._analyzeInterval)}}}setStream(e){this._videoEl.srcObject=e,this._videoEl.onloadedmetadata=()=>{this._isReady=!0}}setFaceDetector(e){this._faceDetector=e}_settingStyle(){this._styleEl.textContent="\n .aiia_container {\n visibility: hidden;\n position: fixed;\n top: 0;\n left: 0;\n }\n .aiia_container .highlighter {\n background: rgba(0, 255, 0, 0.25);\n border: 1px dashed #fff;\n z-index: 1;\n position: absolute;\n }\n .aiia_container .confidence {\n position: absolute;\n padding-bottom: 5px;\n padding-top: 5px;\n background-color: #007f8b;\n color: #fff;\n border: 1px dashed rgba(255, 255, 255, 0.7);\n z-index: 2;\n font-size: 12px;\n margin: 0;\n }\n "}_settingContainer(){this._containerEl.classList.add("aiia_container")}_settingVideo(){this._videoEl.autoplay=!0,this._videoEl.playsInline=!0,this._videoEl.muted=!0}getVideo(){return this._videoEl}startCapture(){this._videoEl.play(),this._loopFrameID=window.requestAnimationFrame(this.predictWebcam)}stopCapture(){null!==this._loopFrameID&&(window.cancelAnimationFrame(this._loopFrameID),this._loopFrameID=null),this._videoEl.pause()}async predictWebcam(){if(this._loopFrameID=window.requestAnimationFrame(this.predictWebcam),void 0!==this._faceDetector&&this._isReady){if(this._frameCount===this._analyzeInterval){const e=this._faceDetector.detectForVideo(this._videoEl,performance.now()).detections;this._isDebug&&this._drawRect(e),this._detectionsSub.next(e),this._frameCount=0}this._frameCount++}}_drawRect(e){t.forEach(this._debugElements,e=>{e.style.display="none"}),t.forEach(e,(e,t)=>{const{categories:i,boundingBox:s}=e;if(s){let e=this._debugElements[2*t],a=this._debugElements[2*t+1];e&&a||(e=document.createElement("div"),e.classList.add("highlighter"),a=document.createElement("p"),a.classList.add("confidence"),this._containerEl.append(a,e),this._debugElements.push(e,a)),e.style.display="block",a.style.display="block";const n=100*i[0].score;a.innerText="Confidence: "+Math.round(n)+"% .",a.style.right=this._videoEl.offsetWidth-s.width-s.originX+"px",a.style.top=s.originY-30+"px",a.style.width=s.width-10+"px",e.style.right=this._videoEl.offsetWidth-s.width-s.originX+"px",e.style.top=`${s.originY}px`,e.style.width=s.width-10+"px",e.style.height=`${s.height}px`}});for(let t=2*e.length;t<this._debugElements.length;t++)this._debugElements[t].style.display="none"}destroy(){this._detectionsSub.complete(),this.remove()}}customElements.define("aiia-camera",C),function(e){e[e.padding=0]="padding",e[e.rejected=1]="rejected",e[e.loadfail=2]="loadfail",e[e.allowed=3]="allowed"}(b||(b={}));class w{stream;_state;stateSub;audioCtx;audioManager;vadManager;camManager;destory;pcmSub;faceResultSub;get sampleRate(){return this.audioCtx?this.audioCtx.sampleRate:16e3}get pcm(){return this.pcmSub.asObservable()}get faceResult(){return this.faceResultSub.asObservable()}get state(){return this._state}set state(e){e!==this._state&&(this._state=e,this.stateSub.next(e))}get stateObs(){return this.stateSub.asObservable()}constructor(e){this.stream=e,this.stateSub=new s.Subject,this.pcmSub=new s.Subject,this.destory=new s.Subject,this.faceResultSub=new s.Subject,this.state=b.padding}async init(t,i){const n=this.stream??await async function(e){try{return await navigator.mediaDevices.getUserMedia(e)}catch(e){return null}}({audio:{echoCancellation:!0,noiseSuppression:!0},video:!0});if(null===n)throw this.state=b.rejected,new Error(e["未獲得媒體裝置權限"]);this.stream=n,this.audioCtx=new AudioContext;try{await this.audioCtx.audioWorklet.addModule(t)}catch(t){throw d.fatal(t),this.state=b.loadfail,new Error(e["Worklet模組載入失敗"])}this.state=b.allowed;const o=await async function(){const e=await a.FilesetResolver.forVisionTasks("https://cdn.jsdelivr.net/npm/@mediapipe/tasks-vision@0.10.0/wasm");return await a.FaceDetector.createFromOptions(e,{baseOptions:{modelAssetPath:"https://storage.googleapis.com/mediapipe-models/face_detector/blaze_face_short_range/float16/1/blaze_face_short_range.tflite",delegate:"GPU"},runningMode:"VIDEO"})}();this.audioManager=new g(this.audioCtx),this.vadManager=new S(n,this.audioCtx,i),this.vadManager.pcm.pipe(s.takeUntil(this.destory)).subscribe(e=>{this.pcmSub.next(e)}),this.camManager=new y(n,o,i?.faceDetection),this.camManager.faceResult.pipe(s.takeUntil(this.destory)).subscribe(e=>{this.faceResultSub.next(e)})}addAudioQueue(e){const i=t.get(e,"buffer",void 0),s=t.get(e,"float32",void 0),a=t.get(e,"int16",void 0),n=t.get(e,"numberOfChannels",void 0);i?this.audioManager?.addBuffer(i):s?this.audioManager?.addBufferByFloat32(s,n):a&&this.audioManager?.addBufferByInt16(a,n)}playAudio(){this.audioManager?.startSpeech()}stopAudio(){this.audioManager?.stopSpeech()}startRecord(){this.vadManager?.startRecord()}stopRecord(){this.vadManager?.stopRecord()}setVolume(e){this.audioManager&&(this.audioManager.volume=e)}getVolume(){return this.audioManager?this.audioManager.volume:0}startCapture(){this.camManager?.startCapture()}stopCapture(){this.camManager?.stopCapture()}onDestroy(){this.camManager?.onDestroy(),this.audioCtx?.close(),this.destory.next(),this.destory.complete()}}class _{socket;messageSub;destorySub;get message(){return this.messageSub.asObservable()}constructor(e){this.destorySub=new s.Subject,this.messageSub=new s.Subject;const{specific:i,id:a}=e.project;this.socket=n.io(e.ws_url,t.assign({autoConnect:!1},i&&void 0!==a?{query:{uuid:a}}:{}))}start(){const e="list",t="cloud",i="channel";s.fromEvent(this.socket,e).pipe(s.take(1),s.takeUntil(this.destorySub)).subscribe(t=>{this.messageSub.next({event:e,data:t})}),s.fromEvent(this.socket,t).pipe(s.takeUntil(this.destorySub)).subscribe(e=>{this.messageSub.next({event:t,data:e})}),s.fromEvent(this.socket,i).pipe(s.takeUntil(this.destorySub)).subscribe(e=>{this.messageSub.next({event:i,data:e})}),this.socket.connect()}send(e,t){this.socket.emit(e,t)}sendToCloud(e){this.send("cloud",JSON.stringify(e))}onDestroy(){this.destorySub.next(),this.destorySub.complete(),this.messageSub.complete(),this.socket.disconnect()}}class v{config;media;chat;destorySub;projectsSub;layoutSub;aiiaSubtitleSub;userSubtitleSub;sampleRateOfWhisper;channelState;stateSub;isMuteAiia;aiiaSubtitleClearSub;userSubtitleClearSub;get state(){return this.stateSub.asObservable()}get layout(){return this.layoutSub.asObservable()}get projects(){return this.projectsSub.asObservable()}get aiiaSubtitle(){return this.aiiaSubtitleSub.asObservable()}get userSubtitle(){return this.userSubtitleSub.asObservable()}set volume(e){this.media.setVolume(e)}get volume(){return this.media.getVolume()}constructor(e,t){this.config=e,this.media=t,this.layoutSub=new s.Subject,this.projectsSub=new s.ReplaySubject(1),this.destorySub=new s.Subject,this.aiiaSubtitleSub=new s.Subject,this.userSubtitleSub=new s.Subject,this.aiiaSubtitleClearSub=new s.Subject,this.userSubtitleClearSub=new s.Subject,this.stateSub=new s.BehaviorSubject("NotStart"),this.sampleRateOfWhisper={input:e.llmSampleRate,output:16e3},this.channelState={proxy:!1,cloud:!1},this.isMuteAiia=!1,this.chat=new _(e),d.code(c["指定依賴已完成"],"chat"),this.close=this.onDestroy,this.behavior=this.behavior.bind(this),this.cloudToObj=this.cloudToObj.bind(this);const{userDelayTime:i,aiiaDelayTime:a}=e.autoClearSubtitle;isNaN(a)||this.aiiaSubtitleClearSub.pipe(s.debounceTime(a),s.takeUntil(this.destorySub)).subscribe(()=>{this.aiiaSubtitleSub.next("")}),isNaN(i)||this.userSubtitleClearSub.pipe(s.debounceTime(i),s.takeUntil(this.destorySub)).subscribe(()=>{this.userSubtitleSub.next("")})}start(){this.media.stateObs.pipe(s.takeUntil(this.destorySub)).subscribe(()=>{this.channelCheck()}),this.chat.message.pipe(s.takeUntil(this.destorySub)).subscribe(({event:e,data:i})=>{switch(e){case"cloud":this.cloudToObj(i);break;case"list":{const{id:e,name:s,specific:a}=this.config.project;let n=null;if(a&&void 0!==e)n=e;else{const a=t.find(i,t=>t.name===s||t.id===e);a&&(n=a.id)}null!==n?this.chooseProject(n):(this.projectsSub.next(i),this.stateSub.next("WattingProjectID"),d.code(c["請訂閱專案列表"]));break}case"channel":switch(i){case"open":this.channelState.proxy=!0,this.channelCheck();break;case"close":this.channelState.proxy=!1,this.media.stopRecord(),this.stateSub.next("CloseService");break;case"no_permissions":this.channelState.proxy=!1,this.stateSub.next("NoPermissions");break;case"reconnect":this.channelState.proxy=!1,this.stateSub.next("ReconnectingCloud")}}}),this.chat.start(),this.stateSub.next("ConnectingCloud"),d.code(c["啟動SDK"])}close;resetSession(){this.chat.sendToCloud({request:"reset"}),this.aiiaSubtitleSub.next(""),this.userSubtitleSub.next(""),this.media.stopAudio(),this.layoutSub.next({type:"resetLayout"})}chooseProject(e){this.chat.send("uuid",e),this.stateSub.next("ConnectingCloud")}mute(){this.isMuteAiia=!0,this.media.stopAudio(),this.media.stopRecord()}unmute(){this.isMuteAiia=!1,this.media.startRecord()}cloudToObj(e){try{const i=JSON.parse(e);void 0!==t.get(i,"signal")?this.behavior(i):t.isArray(i)?t.map(i,this.behavior):t.map(t.values(i),this.behavior)}catch(e){d.error("message transfer fail",e)}}behavior(e){switch(e.signal){case"layout":this.layoutSub.next(e.content);break;case"audio":if("sampleRate"===e.command)this.sampleRateOfWhisper.output=e.content;else switch(e.command){case"pcm":if(!this.isMuteAiia){const t=function(e,t,i){if(t===i)return e;const s=i/t,a=Math.ceil(e.length*s),n=new Float32Array(a);for(let t=0;t<a;t++){const i=t/s,a=Math.floor(i),o=i-a,r=e[a],c=e[Math.min(a+1,e.length-1)];n[t]=r+(c-r)*o}return n}(m(new Int16Array(e.content)),this.sampleRateOfWhisper.output,this.media.sampleRate);this.media.addAudioQueue({float32:t}),this.media.playAudio()}break;case"interrupted":this.media.stopAudio(),this.aiiaSubtitleSub.next("")}break;case"status":"Connected"===t.get(e,"content")&&(this.channelState.cloud=!0,this.channelCheck(),this.layoutSub.next({type:"resetLayout"}));break;case"agent":if(!this.isMuteAiia){const i=t.get(e,"content.function_result.response");void 0!==i&&(this.aiiaSubtitleSub.next(i),this.aiiaSubtitleClearSub.next())}break;case"asr":if(!this.isMuteAiia&&"ASR"===t.get(e,"content.function_type")){const i=t.get(e,"content.function_result.result");void 0!==i&&(this.userSubtitleSub.next(i),this.userSubtitleClearSub.next())}break;default:d.debug(e.signal)}}channelCheck(){const{proxy:e,cloud:t}=this.channelState;e&&t&&this.media.state===b.allowed?(this.chat.sendToCloud({request:"audio",command:"sampleRate",content:this.sampleRateOfWhisper.input}),this.media.pcm.pipe(s.takeUntil(this.destorySub)).subscribe(e=>{this.chat.sendToCloud({request:"audio",command:"pcm",content:f(e)})}),this.media.faceResult.pipe(s.takeUntil(this.destorySub)).subscribe(e=>{this.chat.send("face",e)}),this.media.startRecord(),this.media.startCapture(),this.stateSub.next("InService"),d.code(c["通道、雲端服務、麥克風都已就緒"])):e&&t&&this.media.state!==b.allowed&&(this.stateSub.next("InServiceNoMedia"),d.code(c["未取得媒體裝置權限,請重新設定或忽略此訊息"]))}onDestroy(){this.chat.onDestroy(),this.media.onDestroy(),this.destorySub.next(),this.destorySub.complete(),this.stateSub.next("Destroy"),this.stateSub.complete(),d.code(c["結束SDK"])}}exports.initSdk=function(i){if(o){!function(e){const{info:t,error:i,warn:s,debug:a,fatal:n,code:o}=function(e){if(void 0===e||"all"===e)return{code:!0,error:!0,warn:!0,info:!0,debug:!0,fatal:!0};if("none"===e)return{error:!1,code:!1,warn:!1,info:!1,debug:!1,fatal:!1};if("boolean"==typeof e)return{code:e,error:e,warn:e,info:e,debug:e,fatal:e};{const t=new Set(e);return{code:t.has("code"),debug:t.has("debug"),fatal:t.has("fatal"),error:t.has("error"),warn:t.has("warn"),info:t.has("info")}}}(e);o||(u.code=()=>{}),a||(u.debug=()=>{}),t||(u.info=()=>{}),s||(u.warn=()=>{}),i||(u.error=()=>{}),n||(u.fatal=()=>{})}(t.get(i,"debug",!0)),d.code(c["初始化必要依賴"]);const e=new r(t.assign({},i));d.code(c["指定依賴已完成"],"config");const s=new w(e.mediaStream);d.code(c["指定依賴已完成"],"config");const a=new v(e,s);return d.code(c["指定依賴已完成"],"sdk"),""!==e.worklet_url&&s.init(e.worklet_url,e),a}throw new Error(e["環境錯誤(browser)"])};
|
|
1
|
+
"use strict";var e,t=require("lodash-es"),i=require("moment"),s=require("rxjs"),a=require("@mediapipe/tasks-vision"),n=require("socket.io-client");!function(e){e["網絡錯誤"]="ERROR CODE: 500",e["環境錯誤(browser)"]="ERROR CODE: 1001",e["Worklet模組載入失敗"]="ERROR CODE: 1002",e["未獲得媒體裝置權限"]="ERROR CODE: 1003",e["實例已摧毀"]="ERROR CODE: 1004",e["環境錯誤(nodejs)"]="ERROR CODE: 2001",e["未提供憑證"]="ERROR CODE: 2002",e["獲取權杖失敗"]="ERROR CODE: 2003",e["沒有可用專案"]="ERROR CODE: 2004",e["重複初始化"]="ERROR CODE: 2005"}(e||(e={}));const o="undefined"!=typeof window;"undefined"!=typeof process&&null!=process.versions&&process.versions.node;class r{config;get webserver(){return this.config.webserver}get environment(){return this.config.env}get license(){return this.config.license??""}get port(){return function(e,t){const i=t??NaN;if(e){const t=parseInt(e);return isNaN(t)?i:t}return i}(process.env.PORT,this.config.port??3e3)}get ws_url(){return this.config.ws_url??""}get worklet_url(){return this.config.worklet_url??""}get project(){const{project:e}=t.assign({},this.config);return void 0===e?{specific:!1}:"string"==typeof e?{name:e,id:e,specific:!1}:t.assign({},e,{specific:!0})}get endPoint(){return this.environment,{api:"https://aiia-content-management-dev-21193779403.asia-east1.run.app",socket:"wss://graphen-agentic-workflow-dev-21193779403.asia-east1.run.app"}}get debug(){return this.config.debug??!0}get mediaStream(){return this.config.mediaStream}get chunkTimeInSeconds(){return"number"==typeof this.config.chunkTimeInSeconds&&this.config.chunkTimeInSeconds>0?this.config.chunkTimeInSeconds:1}get llmSampleRate(){return"number"==typeof this.config.llmSampleRate&&this.config.llmSampleRate>0?this.config.llmSampleRate:16e3}get faceDetection(){return t.assign({frequency:3,confidence:80},this.config.faceDetection)}get autoClearSubtitle(){const{user:e,aiia:i}=t.assign({},this.config.autoClearSubtitle);let s=3e3,a=3e3;return"number"==typeof e?s=e>=0?e:s:!1===e&&(s=NaN),"number"==typeof i?a=i>=0?i:a:!1===i&&(a=NaN),{userDelayTime:s,aiiaDelayTime:a}}constructor(e){this.config=e}}var c;!function(e){e["通道、雲端服務、麥克風都已就緒"]="CODE: 1000",e["未取得媒體裝置權限,請重新設定或忽略此訊息"]="CODE: 1001",e["請訂閱專案列表"]="CODE: 1002",e["初始化必要依賴"]="CODE: 1010",e["指定依賴已完成"]="CODE: 1011",e["啟動SDK"]="CODE: 1012",e["結束SDK"]="CODE: 1013",e["安全的關閉連線"]="CODE: 2000",e["無副作用的關閉連線"]="CODE: 2001",e["與雲端的服務中斷"]="CODE: 2002",e["沒有授權,請聯繫Graphen"]="CODE: 2003",e["自動重新連線連端服務"]="CODE: 2004",e["未預期的斷線,請聯繫Graphen"]="CODE: 2005",e["開始初始化必要模組"]="CODE: 2010",e["指定模組初始化完成"]="CODE: 2011",e["啟動服務"]="CODE: 2012",e["服務結束"]="CODE: 2013"}(c||(c={}));const u={code:console.log,debug:console.log,info:console.log,warn:console.warn,error:console.error,fatal:console.error},h=[];const d=new Proxy(u,{get(e,s,a){if("string"==typeof s)switch(s){case"debug":case"info":case"error":case"warn":case"fatal":case"code":return(...a)=>{const n=i().format("yyyy-MM-DD[T]HH:mm:ss");t.forEach(h,e=>{e.next(s,n,...a)}),e[s](`[Aiia::${s}_${n}_]`,...a)};default:return}},set:(e,t,i,s)=>!0});var l,b;function p(e,t,i){return Math.max(i,Math.min(e,t))}function m(e){const t=new DataView(e.buffer),i=[];for(let s=0;s<e.length;s++){const e=t.getInt16(2*s,!0)/32768;i.push(e)}return new Float32Array(i)}function g(e){const t=new DataView(e.buffer),i=[];for(let s=0;s<e.length;s++){const e=p(32768*t.getFloat32(4*s,!0),32767,-32768);i.push(e)}return i}!function(e){e[e.unknow=0]="unknow",e[e.allowed=1]="allowed",e[e.rejected=2]="rejected"}(l||(l={}));class f{audioContext;type="audio";currentNode;bufferQueue;gainNode;set volume(e){e<0&&(e=0),e>100&&(e=100),this.gainNode.gain.value=e/100}get volume(){return Math.round(100*this.gainNode.gain.value)}constructor(e){this.audioContext=e,this.bufferQueue=[],this.currentNode=null,this.gainNode=this.audioContext.createGain(),this.gainNode.connect(this.audioContext.destination)}addBuffer(e){this.bufferQueue.push(e)}addBufferByFloat32(e,t=1){const i=e.length,s=this.audioContext.sampleRate,a=this.audioContext.createBuffer(t,i,s),n=e.slice();for(let e=0;e<t;e++)a.copyToChannel(n,e);this.addBuffer(a)}addBufferByInt16(e,t=1){this.addBufferByFloat32(m(e),t)}startSpeech(){null===this.currentNode&&this.continue()}pauseSpeech(){this.audioContext.suspend().catch(e=>{d.error("pause speech fail",e)})}resumeSpeech(){this.audioContext.resume().catch(e=>{d.error("resume speech fail",e)})}nextSpeech(){this.bufferQueue.length>0&&(this.clearNode(),this.continue())}stopSpeech(){this.bufferQueue=[],this.clearNode()}clearNode(){this.currentNode&&(this.currentNode.onended=function(){},this.currentNode.stop(),this.currentNode.disconnect(),this.currentNode=null)}continue(){const e=this.bufferQueue.shift();if(e){this.currentNode=this.audioContext.createBufferSource(),this.currentNode.buffer=e,this.currentNode.connect(this.gainNode);const t=this.continue.bind(this);this.currentNode.onended=t,this.currentNode.start()}else this.clearNode()}}class S{stream;audioContext;type="vad";workletNode;source;isRecording=!1;outputSampleRate;chunkTimeInSeconds;pcmSub;get pcm(){return this.pcmSub.asObservable()}constructor(e,t,i){this.stream=e,this.audioContext=t,this.outputSampleRate=i?.llmSampleRate??16e3,this.chunkTimeInSeconds=i?.chunkTimeInSeconds??1,this.pcmSub=new s.Subject}async startRecord(){this.isRecording||(this.source=this.audioContext.createMediaStreamSource(this.stream),this.workletNode=new AudioWorkletNode(this.audioContext,"aiia-vad",{processorOptions:{outputSampleRate:this.outputSampleRate,chunkTimeInSeconds:this.chunkTimeInSeconds}}),this.source.connect(this.workletNode),this.workletNode.port.onmessage=e=>{const{float32:t}=e.data;t&&this.pcmSub.next(new Float32Array(t))},this.isRecording=!0)}stopRecord(){this.isRecording&&(this.workletNode&&(this.workletNode.port.onmessage=null,this.workletNode.disconnect()),this.source&&this.source.disconnect(),this.isRecording=!1)}}class y{type="camera";aiiaCamera;get detections(){return this.aiiaCamera.detections}constructor(e){const{frequency:i}=t.assign({frequency:3},e);this.aiiaCamera=new C,this.aiiaCamera.setAttribute("frequency",`${i}`),document.body.append(this.aiiaCamera)}async init(e){try{const t=await a.FilesetResolver.forVisionTasks("https://cdn.jsdelivr.net/npm/@mediapipe/tasks-vision@0.10.0/wasm"),i=await a.FaceDetector.createFromOptions(t,{baseOptions:{modelAssetPath:"https://storage.googleapis.com/mediapipe-models/face_detector/blaze_face_short_range/float16/1/blaze_face_short_range.tflite",delegate:"GPU"},runningMode:"VIDEO"});this.aiiaCamera.setStream(e),this.aiiaCamera.setFaceDetector(i)}catch(e){d.error("Camera init fail",e)}}startCapture(){this.aiiaCamera.startCapture()}stopCapture(){this.aiiaCamera.stopCapture()}onDestroy(){this.aiiaCamera.remove()}}class C extends HTMLElement{static get observedAttributes(){return["debug","frequency"]}_shadow;_containerEl;_styleEl;_videoEl;_isDebug;_debugElements;_analyzeInterval;_frameCount;_loopFrameID;_faceDetector;_detectionsSub;_isReady;get detections(){return this._detectionsSub.asObservable()}constructor(){super(),this._isDebug=!1,this._isReady=!1,this._shadow=this.attachShadow({mode:"open"}),this._containerEl=document.createElement("div"),this._videoEl=document.createElement("video"),this._styleEl=document.createElement("style"),this._debugElements=[],this._loopFrameID=null,this._analyzeInterval=3,this._frameCount=this._analyzeInterval,this._detectionsSub=new s.Subject,this.predictWebcam=this.predictWebcam.bind(this),this._settingContainer(),this._settingStyle(),this._settingVideo(),this._containerEl.appendChild(this._videoEl),this._shadow.appendChild(this._styleEl),this._shadow.appendChild(this._containerEl)}connectedCallback(){this.startCapture()}disconnectedCallback(){this.stopCapture()}attributeChangedCallback(e,t,i){switch(e){case"debug":this._isDebug=null!==i&&("true"===i||""===i),this._containerEl.style.visibility=this._isDebug?"visible":"hidden";break;case"frequency":{const e=Number(i);!isNaN(e)&&e>0&&(this._analyzeInterval=e,this._frameCount=this._analyzeInterval);break}}}setStream(e){this._videoEl.onloadedmetadata=()=>{this._isReady=!0},this._videoEl.srcObject=e}setFaceDetector(e){this._faceDetector=e}_settingStyle(){this._styleEl.textContent="\n .aiia_container {\n visibility: hidden;\n position: fixed;\n top: 0;\n left: 0;\n }\n .aiia_container .highlighter {\n background: rgba(0, 255, 0, 0.25);\n border: 1px dashed #fff;\n z-index: 1;\n position: absolute;\n }\n .aiia_container .confidence {\n position: absolute;\n padding-bottom: 5px;\n padding-top: 5px;\n background-color: #007f8b;\n color: #fff;\n border: 1px dashed rgba(255, 255, 255, 0.7);\n z-index: 2;\n font-size: 12px;\n margin: 0;\n }\n "}_settingContainer(){this._containerEl.classList.add("aiia_container")}_settingVideo(){this._videoEl.autoplay=!0,this._videoEl.playsInline=!0,this._videoEl.muted=!0}getVideo(){return this._videoEl}startCapture(){null===this._loopFrameID&&(this._videoEl.play(),this._loopFrameID=window.requestAnimationFrame(this.predictWebcam))}stopCapture(){null!==this._loopFrameID&&(window.cancelAnimationFrame(this._loopFrameID),this._loopFrameID=null),this._videoEl.pause()}async predictWebcam(){if(this._loopFrameID=window.requestAnimationFrame(this.predictWebcam),void 0!==this._faceDetector&&this._isReady){if(this._frameCount===this._analyzeInterval){const e=this._faceDetector.detectForVideo(this._videoEl,performance.now()).detections;this._isDebug&&this._drawRect(e),this._detectionsSub.next(e),this._frameCount=0}this._frameCount++}}_drawRect(e){t.forEach(e,(e,t)=>{const{categories:i,boundingBox:s}=e;if(s){let e=this._debugElements[2*t],a=this._debugElements[2*t+1];e&&a||(e=document.createElement("div"),e.classList.add("highlighter"),a=document.createElement("p"),a.classList.add("confidence"),this._containerEl.append(a,e),this._debugElements.push(e,a)),e.style.display="block",a.style.display="block";const n=100*i[0].score;a.innerText=`Confidence: ${Math.round(n)} %`,a.style.right=this._videoEl.offsetWidth-s.width-s.originX+"px",a.style.top=s.originY-30+"px",a.style.width=s.width-10+"px",e.style.right=this._videoEl.offsetWidth-s.width-s.originX+"px",e.style.top=`${s.originY}px`,e.style.width=s.width-10+"px",e.style.height=`${s.height}px`}});for(let t=2*e.length;t<this._debugElements.length;t++)this._debugElements[t].style.display="none"}destroy(){this._detectionsSub.complete(),this.remove()}}customElements.define("aiia-camera",C),function(e){e[e.padding=0]="padding",e[e.rejected=1]="rejected",e[e.loadfail=2]="loadfail",e[e.allowed=3]="allowed"}(b||(b={}));class w{stream;_state;stateSub;audioCtx;audioManager;vadManager;camManager;destory;pcmSub;detectionsSub;get sampleRate(){return this.audioCtx?this.audioCtx.sampleRate:16e3}get pcm(){return this.pcmSub.asObservable()}get detections(){return this.detectionsSub.asObservable()}get state(){return this._state}set state(e){e!==this._state&&(this._state=e,this.stateSub.next(e))}get stateObs(){return this.stateSub.asObservable()}constructor(e){this.stream=e,this.stateSub=new s.Subject,this.pcmSub=new s.Subject,this.destory=new s.Subject,this.detectionsSub=new s.Subject,this.state=b.padding}async init(t,i){const a=this.stream??await async function(e){try{return await navigator.mediaDevices.getUserMedia(e)}catch(e){return null}}({audio:{echoCancellation:!0,noiseSuppression:!0},video:!0});if(null===a)throw this.state=b.rejected,new Error(e["未獲得媒體裝置權限"]);this.stream=a,this.audioCtx=new AudioContext;try{await this.audioCtx.audioWorklet.addModule(t)}catch(t){throw d.fatal(t),this.state=b.loadfail,new Error(e["Worklet模組載入失敗"])}this.state=b.allowed,this.audioManager=new f(this.audioCtx),this.vadManager=new S(a,this.audioCtx,i),this.vadManager.pcm.pipe(s.takeUntil(this.destory)).subscribe(e=>{this.pcmSub.next(e)}),this.camManager=new y(i?.faceDetection),this.camManager.detections.pipe(s.takeUntil(this.destory)).subscribe(e=>{this.detectionsSub.next(e)}),this.camManager.init(a)}addAudioQueue(e){const i=t.get(e,"buffer",void 0),s=t.get(e,"float32",void 0),a=t.get(e,"int16",void 0),n=t.get(e,"numberOfChannels",void 0);i?this.audioManager?.addBuffer(i):s?this.audioManager?.addBufferByFloat32(s,n):a&&this.audioManager?.addBufferByInt16(a,n)}playAudio(){this.audioManager?.startSpeech()}stopAudio(){this.audioManager?.stopSpeech()}startRecord(){this.vadManager?.startRecord()}stopRecord(){this.vadManager?.stopRecord()}setVolume(e){this.audioManager&&(this.audioManager.volume=e)}getVolume(){return this.audioManager?this.audioManager.volume:0}startCapture(){this.camManager?.startCapture()}stopCapture(){this.camManager?.stopCapture()}onDestroy(){this.camManager?.onDestroy(),this.audioCtx?.close(),this.destory.next(),this.destory.complete()}}class _{socket;messageSub;destorySub;get message(){return this.messageSub.asObservable()}constructor(e){this.destorySub=new s.Subject,this.messageSub=new s.Subject;const{specific:i,id:a}=e.project;this.socket=n.io(e.ws_url,t.assign({autoConnect:!1},i&&void 0!==a?{query:{uuid:a}}:{}))}start(){const e="list",t="cloud",i="channel";s.fromEvent(this.socket,"connect").pipe(s.takeUntil(this.destorySub)).subscribe(()=>{d.debug("Client ID: ",this.socket.id)}),s.fromEvent(this.socket,e).pipe(s.take(1),s.takeUntil(this.destorySub)).subscribe(t=>{this.messageSub.next({event:e,data:t})}),s.fromEvent(this.socket,t).pipe(s.takeUntil(this.destorySub)).subscribe(e=>{this.messageSub.next({event:t,data:e})}),s.fromEvent(this.socket,i).pipe(s.takeUntil(this.destorySub)).subscribe(e=>{this.messageSub.next({event:i,data:e})}),this.socket.connect()}send(e,t){this.socket.emit(e,t)}sendToCloud(e){this.send("cloud",JSON.stringify(e))}onDestroy(){this.destorySub.next(),this.destorySub.complete(),this.messageSub.complete(),this.socket.disconnect()}}class v{config;media;chat;destorySub;projectsSub;layoutSub;aiiaSubtitleSub;userSubtitleSub;sampleRateOfWhisper;channelState;stateSub;_currentState;isMuteAiia;aiiaSubtitleClearSub;userSubtitleClearSub;get state(){return this.stateSub.asObservable()}get currentState(){return this._currentState}get layout(){return this.layoutSub.asObservable()}get projects(){return this.projectsSub.asObservable()}get aiiaSubtitle(){return this.aiiaSubtitleSub.asObservable()}get userSubtitle(){return this.userSubtitleSub.asObservable()}set volume(e){this.media.setVolume(e)}get volume(){return this.media.getVolume()}constructor(e,t){this.config=e,this.media=t,this.layoutSub=new s.Subject,this.projectsSub=new s.ReplaySubject(1),this.destorySub=new s.Subject,this.aiiaSubtitleSub=new s.Subject,this.userSubtitleSub=new s.Subject,this.aiiaSubtitleClearSub=new s.Subject,this.userSubtitleClearSub=new s.Subject,this.stateSub=new s.BehaviorSubject("NotStart"),this.sampleRateOfWhisper={input:e.llmSampleRate,output:16e3},this.channelState={proxy:!1,cloud:!1},this.isMuteAiia=!1,this.chat=new _(e),d.code(c["指定依賴已完成"],"chat"),this.close=this.onDestroy,this.behavior=this.behavior.bind(this),this.cloudToObj=this.cloudToObj.bind(this);const{userDelayTime:i,aiiaDelayTime:a}=e.autoClearSubtitle;isNaN(a)||this.aiiaSubtitleClearSub.pipe(s.debounceTime(a),s.takeUntil(this.destorySub)).subscribe(()=>{this.aiiaSubtitleSub.next("")}),isNaN(i)||this.userSubtitleClearSub.pipe(s.debounceTime(i),s.takeUntil(this.destorySub)).subscribe(()=>{this.userSubtitleSub.next("")}),this.stateSub.pipe(s.takeUntil(this.destorySub)).subscribe(e=>{this._currentState=e})}start(){this.media.stateObs.pipe(s.takeUntil(this.destorySub)).subscribe(()=>{this.channelCheck()}),this.chat.message.pipe(s.takeUntil(this.destorySub)).subscribe(({event:e,data:i})=>{switch(e){case"cloud":this.cloudToObj(i);break;case"list":{const{id:e,name:s,specific:a}=this.config.project;let n=null;if(a&&void 0!==e)n=e;else{const a=t.find(i,t=>t.name===s||t.id===e);a&&(n=a.id)}null!==n?this.chooseProject(n):(this.projectsSub.next(i),this.stateSub.next("WattingProjectID"),d.code(c["請訂閱專案列表"]));break}case"channel":switch(i){case"open":this.channelState.proxy=!0,this.channelCheck();break;case"close":this.channelState.proxy=!1,this.media.stopRecord(),this.media.stopCapture(),this.stateSub.next("CloseService");break;case"no_permissions":this.channelState.proxy=!1,this.stateSub.next("NoPermissions");break;case"reconnect":this.channelState.proxy=!1,this.stateSub.next("ReconnectingCloud")}}}),this.chat.start(),this.stateSub.next("ConnectingCloud"),d.code(c["啟動SDK"])}close;resetSession(){this.chat.sendToCloud({request:"reset"}),this.aiiaSubtitleSub.next(""),this.userSubtitleSub.next(""),this.media.stopAudio(),this.layoutSub.next({type:"resetLayout"})}chooseProject(e){this.chat.send("uuid",e),this.stateSub.next("ConnectingCloud")}mute(){this.isMuteAiia=!0,this.media.stopAudio(),this.media.stopRecord()}unmute(){this.isMuteAiia=!1,this.media.startRecord()}cloudToObj(e){try{const i=JSON.parse(e);void 0!==t.get(i,"signal")?this.behavior(i):t.isArray(i)?t.map(i,this.behavior):t.map(t.values(i),this.behavior)}catch(e){d.error("message transfer fail",e)}}behavior(e){switch(e.signal){case"layout":this.layoutSub.next(e.content);break;case"audio":if("sampleRate"===e.command)this.sampleRateOfWhisper.output=e.content;else switch(e.command){case"pcm":if(!this.isMuteAiia){const t=function(e,t,i){if(t===i)return e;const s=i/t,a=Math.ceil(e.length*s),n=new Float32Array(a);for(let t=0;t<a;t++){const i=t/s,a=Math.floor(i),o=i-a,r=e[a],c=e[Math.min(a+1,e.length-1)];n[t]=r+(c-r)*o}return n}(m(new Int16Array(e.content)),this.sampleRateOfWhisper.output,this.media.sampleRate);this.media.addAudioQueue({float32:t}),this.media.playAudio()}break;case"interrupted":this.media.stopAudio(),this.aiiaSubtitleSub.next("")}break;case"status":"Connected"===t.get(e,"content")&&(this.channelState.cloud=!0,this.channelCheck(),this.layoutSub.next({type:"resetLayout"}));break;case"agent":if(!this.isMuteAiia){const i=t.get(e,"content.function_result.response");void 0!==i&&(this.aiiaSubtitleSub.next(i),this.aiiaSubtitleClearSub.next())}break;case"asr":if(!this.isMuteAiia&&"ASR"===t.get(e,"content.function_type")){const i=t.get(e,"content.function_result.result");void 0!==i&&(this.userSubtitleSub.next(i),this.userSubtitleClearSub.next())}break;default:d.debug(e.signal)}}channelCheck(){const{proxy:e,cloud:i}=this.channelState;if(e&&i&&this.media.state===b.allowed){this.chat.sendToCloud({request:"audio",command:"sampleRate",content:this.sampleRateOfWhisper.input}),this.media.pcm.pipe(s.takeUntil(this.destorySub)).subscribe(e=>{this.chat.sendToCloud({request:"audio",command:"pcm",content:g(e)})});const e=(a=this.config.faceDetection.confidence,function(e){const i=t.filter(e,e=>100*(e.categories[0]?.score??0)>=a),s=t.compact(t.map(i,e=>{if(void 0!==e.boundingBox){const t=e.boundingBox,i=t.width*t.height,s=e.categories[0]?.score??0;return{...t,area:i,score:100*s}}return null}));return t.sortBy(s,["area","confidenceScore"])[0]??null});this.media.detections.pipe(s.takeUntil(this.destorySub),s.map(e)).subscribe(e=>{this.chat.sendToCloud({request:"face_detect",content:null!==e})}),this.media.startRecord(),this.media.startCapture(),this.stateSub.next("InService"),d.code(c["通道、雲端服務、麥克風都已就緒"])}else e&&i&&this.media.state!==b.allowed&&(this.stateSub.next("InServiceNoMedia"),d.code(c["未取得媒體裝置權限,請重新設定或忽略此訊息"]));var a}onDestroy(){this.chat.onDestroy(),this.media.onDestroy(),this.destorySub.next(),this.destorySub.complete(),this.stateSub.next("Destroy"),this.stateSub.complete(),d.code(c["結束SDK"])}}exports.initSdk=function(i){if(o){!function(e){const{info:t,error:i,warn:s,debug:a,fatal:n,code:o}=function(e){if(void 0===e||"all"===e)return{code:!0,error:!0,warn:!0,info:!0,debug:!0,fatal:!0};if("none"===e)return{error:!1,code:!1,warn:!1,info:!1,debug:!1,fatal:!1};if("boolean"==typeof e)return{code:e,error:e,warn:e,info:e,debug:e,fatal:e};{const t=new Set(e);return{code:t.has("code"),debug:t.has("debug"),fatal:t.has("fatal"),error:t.has("error"),warn:t.has("warn"),info:t.has("info")}}}(e);o||(u.code=()=>{}),a||(u.debug=()=>{}),t||(u.info=()=>{}),s||(u.warn=()=>{}),i||(u.error=()=>{}),n||(u.fatal=()=>{})}(t.get(i,"debug",!0)),d.code(c["初始化必要依賴"]);const e=new r(t.assign({},i));d.code(c["指定依賴已完成"],"config");const s=new w(e.mediaStream);d.code(c["指定依賴已完成"],"config");const a=new v(e,s);return d.code(c["指定依賴已完成"],"sdk"),""!==e.worklet_url&&s.init(e.worklet_url,e),a}throw new Error(e["環境錯誤(browser)"])};
|
package/dist/browser.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Detection } from '@mediapipe/tasks-vision';
|
|
2
2
|
import { Http2SecureServer } from 'http2';
|
|
3
3
|
import { Http2Server } from 'http2';
|
|
4
4
|
import { Observable } from 'rxjs';
|
|
@@ -25,7 +25,7 @@ declare class AiiaConfig {
|
|
|
25
25
|
get chunkTimeInSeconds(): number;
|
|
26
26
|
get llmSampleRate(): number;
|
|
27
27
|
get faceDetection(): {
|
|
28
|
-
|
|
28
|
+
frequency: number;
|
|
29
29
|
confidence: number;
|
|
30
30
|
};
|
|
31
31
|
get autoClearSubtitle(): {
|
|
@@ -52,23 +52,32 @@ declare class AiiaSdk {
|
|
|
52
52
|
private sampleRateOfWhisper;
|
|
53
53
|
private channelState;
|
|
54
54
|
private stateSub;
|
|
55
|
+
private _currentState;
|
|
55
56
|
private isMuteAiia;
|
|
56
57
|
private aiiaSubtitleClearSub;
|
|
57
58
|
private userSubtitleClearSub;
|
|
58
59
|
/**
|
|
59
60
|
* [zh]
|
|
60
|
-
*
|
|
61
|
+
* 可訂閱的服務狀態
|
|
61
62
|
*
|
|
62
63
|
* [en]
|
|
63
|
-
*
|
|
64
|
+
* Status of subscribing services
|
|
64
65
|
*/
|
|
65
66
|
get state(): Observable<AiiaState>;
|
|
66
67
|
/**
|
|
67
68
|
* [zh]
|
|
68
|
-
*
|
|
69
|
+
* 服務狀態
|
|
69
70
|
*
|
|
70
71
|
* [en]
|
|
71
|
-
*
|
|
72
|
+
* State of service
|
|
73
|
+
*/
|
|
74
|
+
get currentState(): AiiaState;
|
|
75
|
+
/**
|
|
76
|
+
* [zh]
|
|
77
|
+
* 可訂閱的控制組件
|
|
78
|
+
*
|
|
79
|
+
* [en]
|
|
80
|
+
* Subscribeable Control Layout
|
|
72
81
|
*/
|
|
73
82
|
get layout(): Observable< {
|
|
74
83
|
type: "mount";
|
|
@@ -199,11 +208,6 @@ export declare type AiiaState = "NotStart" | "WattingProjectID" | "ConnectingClo
|
|
|
199
208
|
|
|
200
209
|
export declare type BrowserInitOptions = Omit<InitOptions, "webserver" | "port" | "license">;
|
|
201
210
|
|
|
202
|
-
declare type FaceDetectorResult = ({
|
|
203
|
-
area: number;
|
|
204
|
-
confidenceScore: number;
|
|
205
|
-
} & BoundingBox) | null;
|
|
206
|
-
|
|
207
211
|
declare interface InitOptions {
|
|
208
212
|
/**
|
|
209
213
|
* [zh]
|
|
@@ -301,19 +305,19 @@ declare interface InitOptions {
|
|
|
301
305
|
* [zh]
|
|
302
306
|
* 人臉辨識
|
|
303
307
|
*
|
|
304
|
-
*
|
|
308
|
+
* frequency: 每幾幀更新一次, 預設: 3
|
|
305
309
|
*
|
|
306
310
|
* confidence: 分數(0 ~ 100), 預設: 80
|
|
307
311
|
*
|
|
308
312
|
* [en]
|
|
309
313
|
* facial recognition
|
|
310
314
|
*
|
|
311
|
-
*
|
|
315
|
+
* frequency: Update every few frames, default: 3
|
|
312
316
|
*
|
|
313
317
|
* confidence: score(0 ~ 100), default: 80
|
|
314
318
|
*/
|
|
315
319
|
faceDetection?: {
|
|
316
|
-
|
|
320
|
+
frequency?: number;
|
|
317
321
|
confidence?: number;
|
|
318
322
|
};
|
|
319
323
|
/**
|
|
@@ -345,10 +349,10 @@ declare class MediaManager {
|
|
|
345
349
|
private camManager?;
|
|
346
350
|
private destory;
|
|
347
351
|
private pcmSub;
|
|
348
|
-
private
|
|
352
|
+
private detectionsSub;
|
|
349
353
|
get sampleRate(): number;
|
|
350
354
|
get pcm(): Observable<Float32Array<ArrayBufferLike>>;
|
|
351
|
-
get
|
|
355
|
+
get detections(): Observable<Detection[]>;
|
|
352
356
|
get state(): MediaStateEnum;
|
|
353
357
|
set state(v: MediaStateEnum);
|
|
354
358
|
get stateObs(): Observable<MediaStateEnum>;
|
package/dist/browser.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{assign as e,forEach as t,filter as i,some as s,compact as a,map as n,sortBy as o,get as r,find as c,isArray as u,values as h}from"lodash";import d from"moment";import{Subject as l,map as b,takeUntil as p,fromEvent as m,take as f,ReplaySubject as g,BehaviorSubject as S,debounceTime as y}from"rxjs";import{FilesetResolver as C,FaceDetector as w}from"@mediapipe/tasks-vision";import{io as _}from"socket.io-client";var v;!function(e){e["網絡錯誤"]="ERROR CODE: 500",e["環境錯誤(browser)"]="ERROR CODE: 1001",e["Worklet模組載入失敗"]="ERROR CODE: 1002",e["未獲得媒體裝置權限"]="ERROR CODE: 1003",e["實例已摧毀"]="ERROR CODE: 1004",e["環境錯誤(nodejs)"]="ERROR CODE: 2001",e["未提供憑證"]="ERROR CODE: 2002",e["獲取權杖失敗"]="ERROR CODE: 2003",e["沒有可用專案"]="ERROR CODE: 2004",e["重複初始化"]="ERROR CODE: 2005"}(v||(v={}));const x="undefined"!=typeof window;"undefined"!=typeof process&&null!=process.versions&&process.versions.node;class E{config;get webserver(){return this.config.webserver}get environment(){return this.config.env}get license(){return this.config.license??""}get port(){return function(e,t){const i=t??NaN;if(e){const t=parseInt(e);return isNaN(t)?i:t}return i}(process.env.PORT,this.config.port??3e3)}get ws_url(){return this.config.ws_url??""}get worklet_url(){return this.config.worklet_url??""}get project(){const{project:t}=e({},this.config);return void 0===t?{specific:!1}:"string"==typeof t?{name:t,id:t,specific:!1}:e({},t,{specific:!0})}get endPoint(){return this.environment,{api:"https://aiia-content-management-dev-21193779403.asia-east1.run.app",socket:"wss://graphen-agentic-workflow-dev-21193779403.asia-east1.run.app"}}get debug(){return this.config.debug??!0}get mediaStream(){return this.config.mediaStream}get chunkTimeInSeconds(){return"number"==typeof this.config.chunkTimeInSeconds&&this.config.chunkTimeInSeconds>0?this.config.chunkTimeInSeconds:1}get llmSampleRate(){return"number"==typeof this.config.llmSampleRate&&this.config.llmSampleRate>0?this.config.llmSampleRate:16e3}get faceDetection(){return e({frame:3,confidence:80},this.config.faceDetection)}get autoClearSubtitle(){const{user:t,aiia:i}=e({},this.config.autoClearSubtitle);let s=3e3,a=3e3;return"number"==typeof t?s=t>=0?t:s:!1===t&&(s=NaN),"number"==typeof i?a=i>=0?i:a:!1===i&&(a=NaN),{userDelayTime:s,aiiaDelayTime:a}}constructor(e){this.config=e}}var R;!function(e){e["通道、雲端服務、麥克風都已就緒"]="CODE: 1000",e["未取得媒體裝置權限,請重新設定或忽略此訊息"]="CODE: 1001",e["請訂閱專案列表"]="CODE: 1002",e["初始化必要依賴"]="CODE: 1010",e["指定依賴已完成"]="CODE: 1011",e["啟動SDK"]="CODE: 1012",e["結束SDK"]="CODE: 1013",e["安全的關閉連線"]="CODE: 2000",e["無副作用的關閉連線"]="CODE: 2001",e["與雲端的服務中斷"]="CODE: 2002",e["沒有授權,請聯繫Graphen"]="CODE: 2003",e["自動重新連線連端服務"]="CODE: 2004",e["未預期的斷線,請聯繫Graphen"]="CODE: 2005",e["開始初始化必要模組"]="CODE: 2010",e["指定模組初始化完成"]="CODE: 2011",e["啟動服務"]="CODE: 2012",e["服務結束"]="CODE: 2013"}(R||(R={}));const D={code:console.log,debug:console.log,info:console.log,warn:console.warn,error:console.error,fatal:console.error},O=[];const k=new Proxy(D,{get(e,i,s){if("string"==typeof i)switch(i){case"debug":case"info":case"error":case"warn":case"fatal":case"code":return(...s)=>{const a=d().format("yyyy-MM-DD[T]HH:mm:ss");t(O,e=>{e.next(i,a,...s)}),e[i](`[Aiia::${i}_${a}_]`,...s)};default:return}},set:(e,t,i,s)=>!0});var N,M;function I(e,t,i){return Math.max(i,Math.min(e,t))}function A(e){const t=new DataView(e.buffer),i=[];for(let s=0;s<e.length;s++){const e=t.getInt16(2*s,!0)/32768;i.push(e)}return new Float32Array(i)}function T(e){const t=new DataView(e.buffer),i=[];for(let s=0;s<e.length;s++){const e=I(32768*t.getFloat32(4*s,!0),32767,-32768);i.push(e)}return i}!function(e){e[e.unknow=0]="unknow",e[e.allowed=1]="allowed",e[e.rejected=2]="rejected"}(N||(N={}));class j{audioContext;type="audio";currentNode;bufferQueue;gainNode;set volume(e){e<0&&(e=0),e>100&&(e=100),this.gainNode.gain.value=e/100}get volume(){return Math.round(100*this.gainNode.gain.value)}constructor(e){this.audioContext=e,this.bufferQueue=[],this.currentNode=null,this.gainNode=this.audioContext.createGain(),this.gainNode.connect(this.audioContext.destination)}addBuffer(e){this.bufferQueue.push(e)}addBufferByFloat32(e,t=1){const i=e.length,s=this.audioContext.sampleRate,a=this.audioContext.createBuffer(t,i,s),n=e.slice();for(let e=0;e<t;e++)a.copyToChannel(n,e);this.addBuffer(a)}addBufferByInt16(e,t=1){this.addBufferByFloat32(A(e),t)}startSpeech(){null===this.currentNode&&this.continue()}pauseSpeech(){this.audioContext.suspend().catch(e=>{k.error("pause speech fail",e)})}resumeSpeech(){this.audioContext.resume().catch(e=>{k.error("resume speech fail",e)})}nextSpeech(){this.bufferQueue.length>0&&(this.clearNode(),this.continue())}stopSpeech(){this.bufferQueue=[],this.clearNode()}clearNode(){this.currentNode&&(this.currentNode.onended=function(){},this.currentNode.stop(),this.currentNode.disconnect(),this.currentNode=null)}continue(){const e=this.bufferQueue.shift();if(e){this.currentNode=this.audioContext.createBufferSource(),this.currentNode.buffer=e,this.currentNode.connect(this.gainNode);const t=this.continue.bind(this);this.currentNode.onended=t,this.currentNode.start()}else this.clearNode()}}class F{stream;audioContext;type="vad";workletNode;source;isRecording=!1;outputSampleRate;chunkTimeInSeconds;pcmSub;get pcm(){return this.pcmSub.asObservable()}constructor(e,t,i){this.stream=e,this.audioContext=t,this.outputSampleRate=i?.llmSampleRate??16e3,this.chunkTimeInSeconds=i?.chunkTimeInSeconds??1,this.pcmSub=new l}async startRecord(){this.isRecording||(this.source=this.audioContext.createMediaStreamSource(this.stream),this.workletNode=new AudioWorkletNode(this.audioContext,"aiia-vad",{processorOptions:{outputSampleRate:this.outputSampleRate,chunkTimeInSeconds:this.chunkTimeInSeconds}}),this.source.connect(this.workletNode),this.workletNode.port.onmessage=e=>{const{float32:t}=e.data;t&&this.pcmSub.next(new Float32Array(t))},this.isRecording=!0)}stopRecord(){this.isRecording&&(this.workletNode&&(this.workletNode.port.onmessage=null,this.workletNode.disconnect()),this.source&&this.source.disconnect(),this.isRecording=!1)}}class B{type="camera";confidenceScore;aiiaCamera;get detections(){return this.aiiaCamera.detections}get faceResult(){const e=this.confidenceScore;return this.aiiaCamera.detections.pipe(b(t=>function(e,t){const r=i(e,e=>{const i=e.categories[0]?.score??0,a=e.keypoints,n=100*i>=t,o=s(a,e=>"leftEye"===e.label||"rightEye"===e.label);return n&&o});if(0===r.length)return null;const c=a(n(r,e=>{if(void 0!==e.boundingBox){const t=e.boundingBox,i=t.width*t.height,s=e.categories[0]?.score??0;return{...t,area:i,confidenceScore:100*s}}return null}));return o(c,["area","confidenceScore"])[0]??null}(t,e)))}constructor(t,i,s){const{frame:a,confidence:n}=e({frame:3,confidence:80},s);this.confidenceScore=n,this.aiiaCamera=new W,this.aiiaCamera.setAttribute("frequency",`${a}`),this.aiiaCamera.setStream(t),this.aiiaCamera.setFaceDetector(i),document.body.append(this.aiiaCamera)}startCapture(){this.aiiaCamera.startCapture()}stopCapture(){this.aiiaCamera.stopCapture()}onDestroy(){this.aiiaCamera.remove()}}class W extends HTMLElement{static get observedAttributes(){return["debug","frequency"]}_shadow;_containerEl;_styleEl;_videoEl;_isDebug;_debugElements;_analyzeInterval;_frameCount;_loopFrameID;_faceDetector;_detectionsSub;_isReady;get detections(){return this._detectionsSub.asObservable()}constructor(){super(),this._isDebug=!1,this._isReady=!1,this._shadow=this.attachShadow({mode:"open"}),this._containerEl=document.createElement("div"),this._videoEl=document.createElement("video"),this._styleEl=document.createElement("style"),this._debugElements=[],this._loopFrameID=null,this._analyzeInterval=3,this._frameCount=this._analyzeInterval,this._detectionsSub=new l,this.predictWebcam=this.predictWebcam.bind(this),this._settingContainer(),this._settingStyle(),this._settingVideo(),this._containerEl.appendChild(this._videoEl),this._shadow.appendChild(this._styleEl),this._shadow.appendChild(this._containerEl)}connectedCallback(){this.startCapture()}disconnectedCallback(){this.stopCapture()}attributeChangedCallback(e,t,i){switch(e){case"debug":this._isDebug=null!==i&&("true"===i||""===i),this._containerEl.style.visibility=this._isDebug?"visible":"hidden";break;case"frequency":{const e=Number(i);!isNaN(e)&&e>0&&(this._analyzeInterval=e,this._frameCount=this._analyzeInterval)}}}setStream(e){this._videoEl.srcObject=e,this._videoEl.onloadedmetadata=()=>{this._isReady=!0}}setFaceDetector(e){this._faceDetector=e}_settingStyle(){this._styleEl.textContent="\n .aiia_container {\n visibility: hidden;\n position: fixed;\n top: 0;\n left: 0;\n }\n .aiia_container .highlighter {\n background: rgba(0, 255, 0, 0.25);\n border: 1px dashed #fff;\n z-index: 1;\n position: absolute;\n }\n .aiia_container .confidence {\n position: absolute;\n padding-bottom: 5px;\n padding-top: 5px;\n background-color: #007f8b;\n color: #fff;\n border: 1px dashed rgba(255, 255, 255, 0.7);\n z-index: 2;\n font-size: 12px;\n margin: 0;\n }\n "}_settingContainer(){this._containerEl.classList.add("aiia_container")}_settingVideo(){this._videoEl.autoplay=!0,this._videoEl.playsInline=!0,this._videoEl.muted=!0}getVideo(){return this._videoEl}startCapture(){this._videoEl.play(),this._loopFrameID=window.requestAnimationFrame(this.predictWebcam)}stopCapture(){null!==this._loopFrameID&&(window.cancelAnimationFrame(this._loopFrameID),this._loopFrameID=null),this._videoEl.pause()}async predictWebcam(){if(this._loopFrameID=window.requestAnimationFrame(this.predictWebcam),void 0!==this._faceDetector&&this._isReady){if(this._frameCount===this._analyzeInterval){const e=this._faceDetector.detectForVideo(this._videoEl,performance.now()).detections;this._isDebug&&this._drawRect(e),this._detectionsSub.next(e),this._frameCount=0}this._frameCount++}}_drawRect(e){t(this._debugElements,e=>{e.style.display="none"}),t(e,(e,t)=>{const{categories:i,boundingBox:s}=e;if(s){let e=this._debugElements[2*t],a=this._debugElements[2*t+1];e&&a||(e=document.createElement("div"),e.classList.add("highlighter"),a=document.createElement("p"),a.classList.add("confidence"),this._containerEl.append(a,e),this._debugElements.push(e,a)),e.style.display="block",a.style.display="block";const n=100*i[0].score;a.innerText="Confidence: "+Math.round(n)+"% .",a.style.right=this._videoEl.offsetWidth-s.width-s.originX+"px",a.style.top=s.originY-30+"px",a.style.width=s.width-10+"px",e.style.right=this._videoEl.offsetWidth-s.width-s.originX+"px",e.style.top=`${s.originY}px`,e.style.width=s.width-10+"px",e.style.height=`${s.height}px`}});for(let t=2*e.length;t<this._debugElements.length;t++)this._debugElements[t].style.display="none"}destroy(){this._detectionsSub.complete(),this.remove()}}customElements.define("aiia-camera",W),function(e){e[e.padding=0]="padding",e[e.rejected=1]="rejected",e[e.loadfail=2]="loadfail",e[e.allowed=3]="allowed"}(M||(M={}));class V{stream;_state;stateSub;audioCtx;audioManager;vadManager;camManager;destory;pcmSub;faceResultSub;get sampleRate(){return this.audioCtx?this.audioCtx.sampleRate:16e3}get pcm(){return this.pcmSub.asObservable()}get faceResult(){return this.faceResultSub.asObservable()}get state(){return this._state}set state(e){e!==this._state&&(this._state=e,this.stateSub.next(e))}get stateObs(){return this.stateSub.asObservable()}constructor(e){this.stream=e,this.stateSub=new l,this.pcmSub=new l,this.destory=new l,this.faceResultSub=new l,this.state=M.padding}async init(e,t){const i=this.stream??await async function(e){try{return await navigator.mediaDevices.getUserMedia(e)}catch(e){return null}}({audio:{echoCancellation:!0,noiseSuppression:!0},video:!0});if(null===i)throw this.state=M.rejected,new Error(v["未獲得媒體裝置權限"]);this.stream=i,this.audioCtx=new AudioContext;try{await this.audioCtx.audioWorklet.addModule(e)}catch(e){throw k.fatal(e),this.state=M.loadfail,new Error(v["Worklet模組載入失敗"])}this.state=M.allowed;const s=await async function(){const e=await C.forVisionTasks("https://cdn.jsdelivr.net/npm/@mediapipe/tasks-vision@0.10.0/wasm");return await w.createFromOptions(e,{baseOptions:{modelAssetPath:"https://storage.googleapis.com/mediapipe-models/face_detector/blaze_face_short_range/float16/1/blaze_face_short_range.tflite",delegate:"GPU"},runningMode:"VIDEO"})}();this.audioManager=new j(this.audioCtx),this.vadManager=new F(i,this.audioCtx,t),this.vadManager.pcm.pipe(p(this.destory)).subscribe(e=>{this.pcmSub.next(e)}),this.camManager=new B(i,s,t?.faceDetection),this.camManager.faceResult.pipe(p(this.destory)).subscribe(e=>{this.faceResultSub.next(e)})}addAudioQueue(e){const t=r(e,"buffer",void 0),i=r(e,"float32",void 0),s=r(e,"int16",void 0),a=r(e,"numberOfChannels",void 0);t?this.audioManager?.addBuffer(t):i?this.audioManager?.addBufferByFloat32(i,a):s&&this.audioManager?.addBufferByInt16(s,a)}playAudio(){this.audioManager?.startSpeech()}stopAudio(){this.audioManager?.stopSpeech()}startRecord(){this.vadManager?.startRecord()}stopRecord(){this.vadManager?.stopRecord()}setVolume(e){this.audioManager&&(this.audioManager.volume=e)}getVolume(){return this.audioManager?this.audioManager.volume:0}startCapture(){this.camManager?.startCapture()}stopCapture(){this.camManager?.stopCapture()}onDestroy(){this.camManager?.onDestroy(),this.audioCtx?.close(),this.destory.next(),this.destory.complete()}}class z{socket;messageSub;destorySub;get message(){return this.messageSub.asObservable()}constructor(t){this.destorySub=new l,this.messageSub=new l;const{specific:i,id:s}=t.project;this.socket=_(t.ws_url,e({autoConnect:!1},i&&void 0!==s?{query:{uuid:s}}:{}))}start(){const e="list",t="cloud",i="channel";m(this.socket,e).pipe(f(1),p(this.destorySub)).subscribe(t=>{this.messageSub.next({event:e,data:t})}),m(this.socket,t).pipe(p(this.destorySub)).subscribe(e=>{this.messageSub.next({event:t,data:e})}),m(this.socket,i).pipe(p(this.destorySub)).subscribe(e=>{this.messageSub.next({event:i,data:e})}),this.socket.connect()}send(e,t){this.socket.emit(e,t)}sendToCloud(e){this.send("cloud",JSON.stringify(e))}onDestroy(){this.destorySub.next(),this.destorySub.complete(),this.messageSub.complete(),this.socket.disconnect()}}class q{config;media;chat;destorySub;projectsSub;layoutSub;aiiaSubtitleSub;userSubtitleSub;sampleRateOfWhisper;channelState;stateSub;isMuteAiia;aiiaSubtitleClearSub;userSubtitleClearSub;get state(){return this.stateSub.asObservable()}get layout(){return this.layoutSub.asObservable()}get projects(){return this.projectsSub.asObservable()}get aiiaSubtitle(){return this.aiiaSubtitleSub.asObservable()}get userSubtitle(){return this.userSubtitleSub.asObservable()}set volume(e){this.media.setVolume(e)}get volume(){return this.media.getVolume()}constructor(e,t){this.config=e,this.media=t,this.layoutSub=new l,this.projectsSub=new g(1),this.destorySub=new l,this.aiiaSubtitleSub=new l,this.userSubtitleSub=new l,this.aiiaSubtitleClearSub=new l,this.userSubtitleClearSub=new l,this.stateSub=new S("NotStart"),this.sampleRateOfWhisper={input:e.llmSampleRate,output:16e3},this.channelState={proxy:!1,cloud:!1},this.isMuteAiia=!1,this.chat=new z(e),k.code(R["指定依賴已完成"],"chat"),this.close=this.onDestroy,this.behavior=this.behavior.bind(this),this.cloudToObj=this.cloudToObj.bind(this);const{userDelayTime:i,aiiaDelayTime:s}=e.autoClearSubtitle;isNaN(s)||this.aiiaSubtitleClearSub.pipe(y(s),p(this.destorySub)).subscribe(()=>{this.aiiaSubtitleSub.next("")}),isNaN(i)||this.userSubtitleClearSub.pipe(y(i),p(this.destorySub)).subscribe(()=>{this.userSubtitleSub.next("")})}start(){this.media.stateObs.pipe(p(this.destorySub)).subscribe(()=>{this.channelCheck()}),this.chat.message.pipe(p(this.destorySub)).subscribe(({event:e,data:t})=>{switch(e){case"cloud":this.cloudToObj(t);break;case"list":{const{id:e,name:i,specific:s}=this.config.project;let a=null;if(s&&void 0!==e)a=e;else{const s=c(t,t=>t.name===i||t.id===e);s&&(a=s.id)}null!==a?this.chooseProject(a):(this.projectsSub.next(t),this.stateSub.next("WattingProjectID"),k.code(R["請訂閱專案列表"]));break}case"channel":switch(t){case"open":this.channelState.proxy=!0,this.channelCheck();break;case"close":this.channelState.proxy=!1,this.media.stopRecord(),this.stateSub.next("CloseService");break;case"no_permissions":this.channelState.proxy=!1,this.stateSub.next("NoPermissions");break;case"reconnect":this.channelState.proxy=!1,this.stateSub.next("ReconnectingCloud")}}}),this.chat.start(),this.stateSub.next("ConnectingCloud"),k.code(R["啟動SDK"])}close;resetSession(){this.chat.sendToCloud({request:"reset"}),this.aiiaSubtitleSub.next(""),this.userSubtitleSub.next(""),this.media.stopAudio(),this.layoutSub.next({type:"resetLayout"})}chooseProject(e){this.chat.send("uuid",e),this.stateSub.next("ConnectingCloud")}mute(){this.isMuteAiia=!0,this.media.stopAudio(),this.media.stopRecord()}unmute(){this.isMuteAiia=!1,this.media.startRecord()}cloudToObj(e){try{const t=JSON.parse(e);void 0!==r(t,"signal")?this.behavior(t):u(t)?n(t,this.behavior):n(h(t),this.behavior)}catch(e){k.error("message transfer fail",e)}}behavior(e){switch(e.signal){case"layout":this.layoutSub.next(e.content);break;case"audio":if("sampleRate"===e.command)this.sampleRateOfWhisper.output=e.content;else switch(e.command){case"pcm":if(!this.isMuteAiia){const t=function(e,t,i){if(t===i)return e;const s=i/t,a=Math.ceil(e.length*s),n=new Float32Array(a);for(let t=0;t<a;t++){const i=t/s,a=Math.floor(i),o=i-a,r=e[a],c=e[Math.min(a+1,e.length-1)];n[t]=r+(c-r)*o}return n}(A(new Int16Array(e.content)),this.sampleRateOfWhisper.output,this.media.sampleRate);this.media.addAudioQueue({float32:t}),this.media.playAudio()}break;case"interrupted":this.media.stopAudio(),this.aiiaSubtitleSub.next("")}break;case"status":"Connected"===r(e,"content")&&(this.channelState.cloud=!0,this.channelCheck(),this.layoutSub.next({type:"resetLayout"}));break;case"agent":if(!this.isMuteAiia){const t=r(e,"content.function_result.response");void 0!==t&&(this.aiiaSubtitleSub.next(t),this.aiiaSubtitleClearSub.next())}break;case"asr":if(!this.isMuteAiia&&"ASR"===r(e,"content.function_type")){const t=r(e,"content.function_result.result");void 0!==t&&(this.userSubtitleSub.next(t),this.userSubtitleClearSub.next())}break;default:k.debug(e.signal)}}channelCheck(){const{proxy:e,cloud:t}=this.channelState;e&&t&&this.media.state===M.allowed?(this.chat.sendToCloud({request:"audio",command:"sampleRate",content:this.sampleRateOfWhisper.input}),this.media.pcm.pipe(p(this.destorySub)).subscribe(e=>{this.chat.sendToCloud({request:"audio",command:"pcm",content:T(e)})}),this.media.faceResult.pipe(p(this.destorySub)).subscribe(e=>{this.chat.send("face",e)}),this.media.startRecord(),this.media.startCapture(),this.stateSub.next("InService"),k.code(R["通道、雲端服務、麥克風都已就緒"])):e&&t&&this.media.state!==M.allowed&&(this.stateSub.next("InServiceNoMedia"),k.code(R["未取得媒體裝置權限,請重新設定或忽略此訊息"]))}onDestroy(){this.chat.onDestroy(),this.media.onDestroy(),this.destorySub.next(),this.destorySub.complete(),this.stateSub.next("Destroy"),this.stateSub.complete(),k.code(R["結束SDK"])}}function P(t){if(x){!function(e){const{info:t,error:i,warn:s,debug:a,fatal:n,code:o}=function(e){if(void 0===e||"all"===e)return{code:!0,error:!0,warn:!0,info:!0,debug:!0,fatal:!0};if("none"===e)return{error:!1,code:!1,warn:!1,info:!1,debug:!1,fatal:!1};if("boolean"==typeof e)return{code:e,error:e,warn:e,info:e,debug:e,fatal:e};{const t=new Set(e);return{code:t.has("code"),debug:t.has("debug"),fatal:t.has("fatal"),error:t.has("error"),warn:t.has("warn"),info:t.has("info")}}}(e);o||(D.code=()=>{}),a||(D.debug=()=>{}),t||(D.info=()=>{}),s||(D.warn=()=>{}),i||(D.error=()=>{}),n||(D.fatal=()=>{})}(r(t,"debug",!0)),k.code(R["初始化必要依賴"]);const i=new E(e({},t));k.code(R["指定依賴已完成"],"config");const s=new V(i.mediaStream);k.code(R["指定依賴已完成"],"config");const a=new q(i,s);return k.code(R["指定依賴已完成"],"sdk"),""!==i.worklet_url&&s.init(i.worklet_url,i),a}throw new Error(v["環境錯誤(browser)"])}export{P as initSdk};
|
|
1
|
+
import{assign as e,forEach as t,filter as i,compact as s,map as a,sortBy as n,get as o,find as r,isArray as c,values as u}from"lodash-es";import h from"moment";import{Subject as d,takeUntil as l,fromEvent as b,take as p,ReplaySubject as m,BehaviorSubject as f,debounceTime as g,map as S}from"rxjs";import{FilesetResolver as y,FaceDetector as C}from"@mediapipe/tasks-vision";import{io as w}from"socket.io-client";var _;!function(e){e["網絡錯誤"]="ERROR CODE: 500",e["環境錯誤(browser)"]="ERROR CODE: 1001",e["Worklet模組載入失敗"]="ERROR CODE: 1002",e["未獲得媒體裝置權限"]="ERROR CODE: 1003",e["實例已摧毀"]="ERROR CODE: 1004",e["環境錯誤(nodejs)"]="ERROR CODE: 2001",e["未提供憑證"]="ERROR CODE: 2002",e["獲取權杖失敗"]="ERROR CODE: 2003",e["沒有可用專案"]="ERROR CODE: 2004",e["重複初始化"]="ERROR CODE: 2005"}(_||(_={}));const v="undefined"!=typeof window;"undefined"!=typeof process&&null!=process.versions&&process.versions.node;class x{config;get webserver(){return this.config.webserver}get environment(){return this.config.env}get license(){return this.config.license??""}get port(){return function(e,t){const i=t??NaN;if(e){const t=parseInt(e);return isNaN(t)?i:t}return i}(process.env.PORT,this.config.port??3e3)}get ws_url(){return this.config.ws_url??""}get worklet_url(){return this.config.worklet_url??""}get project(){const{project:t}=e({},this.config);return void 0===t?{specific:!1}:"string"==typeof t?{name:t,id:t,specific:!1}:e({},t,{specific:!0})}get endPoint(){return this.environment,{api:"https://aiia-content-management-dev-21193779403.asia-east1.run.app",socket:"wss://graphen-agentic-workflow-dev-21193779403.asia-east1.run.app"}}get debug(){return this.config.debug??!0}get mediaStream(){return this.config.mediaStream}get chunkTimeInSeconds(){return"number"==typeof this.config.chunkTimeInSeconds&&this.config.chunkTimeInSeconds>0?this.config.chunkTimeInSeconds:1}get llmSampleRate(){return"number"==typeof this.config.llmSampleRate&&this.config.llmSampleRate>0?this.config.llmSampleRate:16e3}get faceDetection(){return e({frequency:3,confidence:80},this.config.faceDetection)}get autoClearSubtitle(){const{user:t,aiia:i}=e({},this.config.autoClearSubtitle);let s=3e3,a=3e3;return"number"==typeof t?s=t>=0?t:s:!1===t&&(s=NaN),"number"==typeof i?a=i>=0?i:a:!1===i&&(a=NaN),{userDelayTime:s,aiiaDelayTime:a}}constructor(e){this.config=e}}var E;!function(e){e["通道、雲端服務、麥克風都已就緒"]="CODE: 1000",e["未取得媒體裝置權限,請重新設定或忽略此訊息"]="CODE: 1001",e["請訂閱專案列表"]="CODE: 1002",e["初始化必要依賴"]="CODE: 1010",e["指定依賴已完成"]="CODE: 1011",e["啟動SDK"]="CODE: 1012",e["結束SDK"]="CODE: 1013",e["安全的關閉連線"]="CODE: 2000",e["無副作用的關閉連線"]="CODE: 2001",e["與雲端的服務中斷"]="CODE: 2002",e["沒有授權,請聯繫Graphen"]="CODE: 2003",e["自動重新連線連端服務"]="CODE: 2004",e["未預期的斷線,請聯繫Graphen"]="CODE: 2005",e["開始初始化必要模組"]="CODE: 2010",e["指定模組初始化完成"]="CODE: 2011",e["啟動服務"]="CODE: 2012",e["服務結束"]="CODE: 2013"}(E||(E={}));const D={code:console.log,debug:console.log,info:console.log,warn:console.warn,error:console.error,fatal:console.error},R=[];const O=new Proxy(D,{get(e,i,s){if("string"==typeof i)switch(i){case"debug":case"info":case"error":case"warn":case"fatal":case"code":return(...s)=>{const a=h().format("yyyy-MM-DD[T]HH:mm:ss");t(R,e=>{e.next(i,a,...s)}),e[i](`[Aiia::${i}_${a}_]`,...s)};default:return}},set:(e,t,i,s)=>!0});var k,N;function M(e,t,i){return Math.max(i,Math.min(e,t))}function I(e){const t=new DataView(e.buffer),i=[];for(let s=0;s<e.length;s++){const e=t.getInt16(2*s,!0)/32768;i.push(e)}return new Float32Array(i)}function A(e){const t=new DataView(e.buffer),i=[];for(let s=0;s<e.length;s++){const e=M(32768*t.getFloat32(4*s,!0),32767,-32768);i.push(e)}return i}!function(e){e[e.unknow=0]="unknow",e[e.allowed=1]="allowed",e[e.rejected=2]="rejected"}(k||(k={}));class T{audioContext;type="audio";currentNode;bufferQueue;gainNode;set volume(e){e<0&&(e=0),e>100&&(e=100),this.gainNode.gain.value=e/100}get volume(){return Math.round(100*this.gainNode.gain.value)}constructor(e){this.audioContext=e,this.bufferQueue=[],this.currentNode=null,this.gainNode=this.audioContext.createGain(),this.gainNode.connect(this.audioContext.destination)}addBuffer(e){this.bufferQueue.push(e)}addBufferByFloat32(e,t=1){const i=e.length,s=this.audioContext.sampleRate,a=this.audioContext.createBuffer(t,i,s),n=e.slice();for(let e=0;e<t;e++)a.copyToChannel(n,e);this.addBuffer(a)}addBufferByInt16(e,t=1){this.addBufferByFloat32(I(e),t)}startSpeech(){null===this.currentNode&&this.continue()}pauseSpeech(){this.audioContext.suspend().catch(e=>{O.error("pause speech fail",e)})}resumeSpeech(){this.audioContext.resume().catch(e=>{O.error("resume speech fail",e)})}nextSpeech(){this.bufferQueue.length>0&&(this.clearNode(),this.continue())}stopSpeech(){this.bufferQueue=[],this.clearNode()}clearNode(){this.currentNode&&(this.currentNode.onended=function(){},this.currentNode.stop(),this.currentNode.disconnect(),this.currentNode=null)}continue(){const e=this.bufferQueue.shift();if(e){this.currentNode=this.audioContext.createBufferSource(),this.currentNode.buffer=e,this.currentNode.connect(this.gainNode);const t=this.continue.bind(this);this.currentNode.onended=t,this.currentNode.start()}else this.clearNode()}}class j{stream;audioContext;type="vad";workletNode;source;isRecording=!1;outputSampleRate;chunkTimeInSeconds;pcmSub;get pcm(){return this.pcmSub.asObservable()}constructor(e,t,i){this.stream=e,this.audioContext=t,this.outputSampleRate=i?.llmSampleRate??16e3,this.chunkTimeInSeconds=i?.chunkTimeInSeconds??1,this.pcmSub=new d}async startRecord(){this.isRecording||(this.source=this.audioContext.createMediaStreamSource(this.stream),this.workletNode=new AudioWorkletNode(this.audioContext,"aiia-vad",{processorOptions:{outputSampleRate:this.outputSampleRate,chunkTimeInSeconds:this.chunkTimeInSeconds}}),this.source.connect(this.workletNode),this.workletNode.port.onmessage=e=>{const{float32:t}=e.data;t&&this.pcmSub.next(new Float32Array(t))},this.isRecording=!0)}stopRecord(){this.isRecording&&(this.workletNode&&(this.workletNode.port.onmessage=null,this.workletNode.disconnect()),this.source&&this.source.disconnect(),this.isRecording=!1)}}class F{type="camera";aiiaCamera;get detections(){return this.aiiaCamera.detections}constructor(t){const{frequency:i}=e({frequency:3},t);this.aiiaCamera=new B,this.aiiaCamera.setAttribute("frequency",`${i}`),document.body.append(this.aiiaCamera)}async init(e){try{const t=await y.forVisionTasks("https://cdn.jsdelivr.net/npm/@mediapipe/tasks-vision@0.10.0/wasm"),i=await C.createFromOptions(t,{baseOptions:{modelAssetPath:"https://storage.googleapis.com/mediapipe-models/face_detector/blaze_face_short_range/float16/1/blaze_face_short_range.tflite",delegate:"GPU"},runningMode:"VIDEO"});this.aiiaCamera.setStream(e),this.aiiaCamera.setFaceDetector(i)}catch(e){O.error("Camera init fail",e)}}startCapture(){this.aiiaCamera.startCapture()}stopCapture(){this.aiiaCamera.stopCapture()}onDestroy(){this.aiiaCamera.remove()}}class B extends HTMLElement{static get observedAttributes(){return["debug","frequency"]}_shadow;_containerEl;_styleEl;_videoEl;_isDebug;_debugElements;_analyzeInterval;_frameCount;_loopFrameID;_faceDetector;_detectionsSub;_isReady;get detections(){return this._detectionsSub.asObservable()}constructor(){super(),this._isDebug=!1,this._isReady=!1,this._shadow=this.attachShadow({mode:"open"}),this._containerEl=document.createElement("div"),this._videoEl=document.createElement("video"),this._styleEl=document.createElement("style"),this._debugElements=[],this._loopFrameID=null,this._analyzeInterval=3,this._frameCount=this._analyzeInterval,this._detectionsSub=new d,this.predictWebcam=this.predictWebcam.bind(this),this._settingContainer(),this._settingStyle(),this._settingVideo(),this._containerEl.appendChild(this._videoEl),this._shadow.appendChild(this._styleEl),this._shadow.appendChild(this._containerEl)}connectedCallback(){this.startCapture()}disconnectedCallback(){this.stopCapture()}attributeChangedCallback(e,t,i){switch(e){case"debug":this._isDebug=null!==i&&("true"===i||""===i),this._containerEl.style.visibility=this._isDebug?"visible":"hidden";break;case"frequency":{const e=Number(i);!isNaN(e)&&e>0&&(this._analyzeInterval=e,this._frameCount=this._analyzeInterval);break}}}setStream(e){this._videoEl.onloadedmetadata=()=>{this._isReady=!0},this._videoEl.srcObject=e}setFaceDetector(e){this._faceDetector=e}_settingStyle(){this._styleEl.textContent="\n .aiia_container {\n visibility: hidden;\n position: fixed;\n top: 0;\n left: 0;\n }\n .aiia_container .highlighter {\n background: rgba(0, 255, 0, 0.25);\n border: 1px dashed #fff;\n z-index: 1;\n position: absolute;\n }\n .aiia_container .confidence {\n position: absolute;\n padding-bottom: 5px;\n padding-top: 5px;\n background-color: #007f8b;\n color: #fff;\n border: 1px dashed rgba(255, 255, 255, 0.7);\n z-index: 2;\n font-size: 12px;\n margin: 0;\n }\n "}_settingContainer(){this._containerEl.classList.add("aiia_container")}_settingVideo(){this._videoEl.autoplay=!0,this._videoEl.playsInline=!0,this._videoEl.muted=!0}getVideo(){return this._videoEl}startCapture(){null===this._loopFrameID&&(this._videoEl.play(),this._loopFrameID=window.requestAnimationFrame(this.predictWebcam))}stopCapture(){null!==this._loopFrameID&&(window.cancelAnimationFrame(this._loopFrameID),this._loopFrameID=null),this._videoEl.pause()}async predictWebcam(){if(this._loopFrameID=window.requestAnimationFrame(this.predictWebcam),void 0!==this._faceDetector&&this._isReady){if(this._frameCount===this._analyzeInterval){const e=this._faceDetector.detectForVideo(this._videoEl,performance.now()).detections;this._isDebug&&this._drawRect(e),this._detectionsSub.next(e),this._frameCount=0}this._frameCount++}}_drawRect(e){t(e,(e,t)=>{const{categories:i,boundingBox:s}=e;if(s){let e=this._debugElements[2*t],a=this._debugElements[2*t+1];e&&a||(e=document.createElement("div"),e.classList.add("highlighter"),a=document.createElement("p"),a.classList.add("confidence"),this._containerEl.append(a,e),this._debugElements.push(e,a)),e.style.display="block",a.style.display="block";const n=100*i[0].score;a.innerText=`Confidence: ${Math.round(n)} %`,a.style.right=this._videoEl.offsetWidth-s.width-s.originX+"px",a.style.top=s.originY-30+"px",a.style.width=s.width-10+"px",e.style.right=this._videoEl.offsetWidth-s.width-s.originX+"px",e.style.top=`${s.originY}px`,e.style.width=s.width-10+"px",e.style.height=`${s.height}px`}});for(let t=2*e.length;t<this._debugElements.length;t++)this._debugElements[t].style.display="none"}destroy(){this._detectionsSub.complete(),this.remove()}}customElements.define("aiia-camera",B),function(e){e[e.padding=0]="padding",e[e.rejected=1]="rejected",e[e.loadfail=2]="loadfail",e[e.allowed=3]="allowed"}(N||(N={}));class W{stream;_state;stateSub;audioCtx;audioManager;vadManager;camManager;destory;pcmSub;detectionsSub;get sampleRate(){return this.audioCtx?this.audioCtx.sampleRate:16e3}get pcm(){return this.pcmSub.asObservable()}get detections(){return this.detectionsSub.asObservable()}get state(){return this._state}set state(e){e!==this._state&&(this._state=e,this.stateSub.next(e))}get stateObs(){return this.stateSub.asObservable()}constructor(e){this.stream=e,this.stateSub=new d,this.pcmSub=new d,this.destory=new d,this.detectionsSub=new d,this.state=N.padding}async init(e,t){const i=this.stream??await async function(e){try{return await navigator.mediaDevices.getUserMedia(e)}catch(e){return null}}({audio:{echoCancellation:!0,noiseSuppression:!0},video:!0});if(null===i)throw this.state=N.rejected,new Error(_["未獲得媒體裝置權限"]);this.stream=i,this.audioCtx=new AudioContext;try{await this.audioCtx.audioWorklet.addModule(e)}catch(e){throw O.fatal(e),this.state=N.loadfail,new Error(_["Worklet模組載入失敗"])}this.state=N.allowed,this.audioManager=new T(this.audioCtx),this.vadManager=new j(i,this.audioCtx,t),this.vadManager.pcm.pipe(l(this.destory)).subscribe(e=>{this.pcmSub.next(e)}),this.camManager=new F(t?.faceDetection),this.camManager.detections.pipe(l(this.destory)).subscribe(e=>{this.detectionsSub.next(e)}),this.camManager.init(i)}addAudioQueue(e){const t=o(e,"buffer",void 0),i=o(e,"float32",void 0),s=o(e,"int16",void 0),a=o(e,"numberOfChannels",void 0);t?this.audioManager?.addBuffer(t):i?this.audioManager?.addBufferByFloat32(i,a):s&&this.audioManager?.addBufferByInt16(s,a)}playAudio(){this.audioManager?.startSpeech()}stopAudio(){this.audioManager?.stopSpeech()}startRecord(){this.vadManager?.startRecord()}stopRecord(){this.vadManager?.stopRecord()}setVolume(e){this.audioManager&&(this.audioManager.volume=e)}getVolume(){return this.audioManager?this.audioManager.volume:0}startCapture(){this.camManager?.startCapture()}stopCapture(){this.camManager?.stopCapture()}onDestroy(){this.camManager?.onDestroy(),this.audioCtx?.close(),this.destory.next(),this.destory.complete()}}class q{socket;messageSub;destorySub;get message(){return this.messageSub.asObservable()}constructor(t){this.destorySub=new d,this.messageSub=new d;const{specific:i,id:s}=t.project;this.socket=w(t.ws_url,e({autoConnect:!1},i&&void 0!==s?{query:{uuid:s}}:{}))}start(){const e="list",t="cloud",i="channel";b(this.socket,"connect").pipe(l(this.destorySub)).subscribe(()=>{O.debug("Client ID: ",this.socket.id)}),b(this.socket,e).pipe(p(1),l(this.destorySub)).subscribe(t=>{this.messageSub.next({event:e,data:t})}),b(this.socket,t).pipe(l(this.destorySub)).subscribe(e=>{this.messageSub.next({event:t,data:e})}),b(this.socket,i).pipe(l(this.destorySub)).subscribe(e=>{this.messageSub.next({event:i,data:e})}),this.socket.connect()}send(e,t){this.socket.emit(e,t)}sendToCloud(e){this.send("cloud",JSON.stringify(e))}onDestroy(){this.destorySub.next(),this.destorySub.complete(),this.messageSub.complete(),this.socket.disconnect()}}class V{config;media;chat;destorySub;projectsSub;layoutSub;aiiaSubtitleSub;userSubtitleSub;sampleRateOfWhisper;channelState;stateSub;_currentState;isMuteAiia;aiiaSubtitleClearSub;userSubtitleClearSub;get state(){return this.stateSub.asObservable()}get currentState(){return this._currentState}get layout(){return this.layoutSub.asObservable()}get projects(){return this.projectsSub.asObservable()}get aiiaSubtitle(){return this.aiiaSubtitleSub.asObservable()}get userSubtitle(){return this.userSubtitleSub.asObservable()}set volume(e){this.media.setVolume(e)}get volume(){return this.media.getVolume()}constructor(e,t){this.config=e,this.media=t,this.layoutSub=new d,this.projectsSub=new m(1),this.destorySub=new d,this.aiiaSubtitleSub=new d,this.userSubtitleSub=new d,this.aiiaSubtitleClearSub=new d,this.userSubtitleClearSub=new d,this.stateSub=new f("NotStart"),this.sampleRateOfWhisper={input:e.llmSampleRate,output:16e3},this.channelState={proxy:!1,cloud:!1},this.isMuteAiia=!1,this.chat=new q(e),O.code(E["指定依賴已完成"],"chat"),this.close=this.onDestroy,this.behavior=this.behavior.bind(this),this.cloudToObj=this.cloudToObj.bind(this);const{userDelayTime:i,aiiaDelayTime:s}=e.autoClearSubtitle;isNaN(s)||this.aiiaSubtitleClearSub.pipe(g(s),l(this.destorySub)).subscribe(()=>{this.aiiaSubtitleSub.next("")}),isNaN(i)||this.userSubtitleClearSub.pipe(g(i),l(this.destorySub)).subscribe(()=>{this.userSubtitleSub.next("")}),this.stateSub.pipe(l(this.destorySub)).subscribe(e=>{this._currentState=e})}start(){this.media.stateObs.pipe(l(this.destorySub)).subscribe(()=>{this.channelCheck()}),this.chat.message.pipe(l(this.destorySub)).subscribe(({event:e,data:t})=>{switch(e){case"cloud":this.cloudToObj(t);break;case"list":{const{id:e,name:i,specific:s}=this.config.project;let a=null;if(s&&void 0!==e)a=e;else{const s=r(t,t=>t.name===i||t.id===e);s&&(a=s.id)}null!==a?this.chooseProject(a):(this.projectsSub.next(t),this.stateSub.next("WattingProjectID"),O.code(E["請訂閱專案列表"]));break}case"channel":switch(t){case"open":this.channelState.proxy=!0,this.channelCheck();break;case"close":this.channelState.proxy=!1,this.media.stopRecord(),this.media.stopCapture(),this.stateSub.next("CloseService");break;case"no_permissions":this.channelState.proxy=!1,this.stateSub.next("NoPermissions");break;case"reconnect":this.channelState.proxy=!1,this.stateSub.next("ReconnectingCloud")}}}),this.chat.start(),this.stateSub.next("ConnectingCloud"),O.code(E["啟動SDK"])}close;resetSession(){this.chat.sendToCloud({request:"reset"}),this.aiiaSubtitleSub.next(""),this.userSubtitleSub.next(""),this.media.stopAudio(),this.layoutSub.next({type:"resetLayout"})}chooseProject(e){this.chat.send("uuid",e),this.stateSub.next("ConnectingCloud")}mute(){this.isMuteAiia=!0,this.media.stopAudio(),this.media.stopRecord()}unmute(){this.isMuteAiia=!1,this.media.startRecord()}cloudToObj(e){try{const t=JSON.parse(e);void 0!==o(t,"signal")?this.behavior(t):c(t)?a(t,this.behavior):a(u(t),this.behavior)}catch(e){O.error("message transfer fail",e)}}behavior(e){switch(e.signal){case"layout":this.layoutSub.next(e.content);break;case"audio":if("sampleRate"===e.command)this.sampleRateOfWhisper.output=e.content;else switch(e.command){case"pcm":if(!this.isMuteAiia){const t=function(e,t,i){if(t===i)return e;const s=i/t,a=Math.ceil(e.length*s),n=new Float32Array(a);for(let t=0;t<a;t++){const i=t/s,a=Math.floor(i),o=i-a,r=e[a],c=e[Math.min(a+1,e.length-1)];n[t]=r+(c-r)*o}return n}(I(new Int16Array(e.content)),this.sampleRateOfWhisper.output,this.media.sampleRate);this.media.addAudioQueue({float32:t}),this.media.playAudio()}break;case"interrupted":this.media.stopAudio(),this.aiiaSubtitleSub.next("")}break;case"status":"Connected"===o(e,"content")&&(this.channelState.cloud=!0,this.channelCheck(),this.layoutSub.next({type:"resetLayout"}));break;case"agent":if(!this.isMuteAiia){const t=o(e,"content.function_result.response");void 0!==t&&(this.aiiaSubtitleSub.next(t),this.aiiaSubtitleClearSub.next())}break;case"asr":if(!this.isMuteAiia&&"ASR"===o(e,"content.function_type")){const t=o(e,"content.function_result.result");void 0!==t&&(this.userSubtitleSub.next(t),this.userSubtitleClearSub.next())}break;default:O.debug(e.signal)}}channelCheck(){const{proxy:e,cloud:t}=this.channelState;if(e&&t&&this.media.state===N.allowed){this.chat.sendToCloud({request:"audio",command:"sampleRate",content:this.sampleRateOfWhisper.input}),this.media.pcm.pipe(l(this.destorySub)).subscribe(e=>{this.chat.sendToCloud({request:"audio",command:"pcm",content:A(e)})});const e=(o=this.config.faceDetection.confidence,function(e){const t=i(e,e=>100*(e.categories[0]?.score??0)>=o),r=s(a(t,e=>{if(void 0!==e.boundingBox){const t=e.boundingBox,i=t.width*t.height,s=e.categories[0]?.score??0;return{...t,area:i,score:100*s}}return null}));return n(r,["area","confidenceScore"])[0]??null});this.media.detections.pipe(l(this.destorySub),S(e)).subscribe(e=>{this.chat.sendToCloud({request:"face_detect",content:null!==e})}),this.media.startRecord(),this.media.startCapture(),this.stateSub.next("InService"),O.code(E["通道、雲端服務、麥克風都已就緒"])}else e&&t&&this.media.state!==N.allowed&&(this.stateSub.next("InServiceNoMedia"),O.code(E["未取得媒體裝置權限,請重新設定或忽略此訊息"]));var o}onDestroy(){this.chat.onDestroy(),this.media.onDestroy(),this.destorySub.next(),this.destorySub.complete(),this.stateSub.next("Destroy"),this.stateSub.complete(),O.code(E["結束SDK"])}}function z(t){if(v){!function(e){const{info:t,error:i,warn:s,debug:a,fatal:n,code:o}=function(e){if(void 0===e||"all"===e)return{code:!0,error:!0,warn:!0,info:!0,debug:!0,fatal:!0};if("none"===e)return{error:!1,code:!1,warn:!1,info:!1,debug:!1,fatal:!1};if("boolean"==typeof e)return{code:e,error:e,warn:e,info:e,debug:e,fatal:e};{const t=new Set(e);return{code:t.has("code"),debug:t.has("debug"),fatal:t.has("fatal"),error:t.has("error"),warn:t.has("warn"),info:t.has("info")}}}(e);o||(D.code=()=>{}),a||(D.debug=()=>{}),t||(D.info=()=>{}),s||(D.warn=()=>{}),i||(D.error=()=>{}),n||(D.fatal=()=>{})}(o(t,"debug",!0)),O.code(E["初始化必要依賴"]);const i=new x(e({},t));O.code(E["指定依賴已完成"],"config");const s=new W(i.mediaStream);O.code(E["指定依賴已完成"],"config");const a=new V(i,s);return O.code(E["指定依賴已完成"],"sdk"),""!==i.worklet_url&&s.init(i.worklet_url,i),a}throw new Error(_["環境錯誤(browser)"])}export{z as initSdk};
|
package/dist/node.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var e,t=require("lodash"),s=require("moment"),i=require("socket.io"),o=require("rxjs"),r=require("axios"),n=require("ws"),c=require("fs"),a=require("os"),h=require("path"),l=require("node-osc");!function(e){e["網絡錯誤"]="ERROR CODE: 500",e["環境錯誤(browser)"]="ERROR CODE: 1001",e["Worklet模組載入失敗"]="ERROR CODE: 1002",e["未獲得媒體裝置權限"]="ERROR CODE: 1003",e["實例已摧毀"]="ERROR CODE: 1004",e["環境錯誤(nodejs)"]="ERROR CODE: 2001",e["未提供憑證"]="ERROR CODE: 2002",e["獲取權杖失敗"]="ERROR CODE: 2003",e["沒有可用專案"]="ERROR CODE: 2004",e["重複初始化"]="ERROR CODE: 2005"}(e||(e={}));const u="access_token",d="undefined"!=typeof process&&null!=process.versions&&null!=process.versions.node;class g{config;get webserver(){return this.config.webserver}get environment(){return this.config.env}get license(){return this.config.license??""}get port(){return function(e,t){const s=t??NaN;if(e){const t=parseInt(e);return isNaN(t)?s:t}return s}(process.env.PORT,this.config.port??3e3)}get ws_url(){return this.config.ws_url??""}get worklet_url(){return this.config.worklet_url??""}get project(){const{project:e}=t.assign({},this.config);return void 0===e?{specific:!1}:"string"==typeof e?{name:e,id:e,specific:!1}:t.assign({},e,{specific:!0})}get endPoint(){return this.environment,{api:"https://aiia-content-management-dev-21193779403.asia-east1.run.app",socket:"wss://graphen-agentic-workflow-dev-21193779403.asia-east1.run.app"}}get debug(){return this.config.debug??!0}get mediaStream(){return this.config.mediaStream}get chunkTimeInSeconds(){return"number"==typeof this.config.chunkTimeInSeconds&&this.config.chunkTimeInSeconds>0?this.config.chunkTimeInSeconds:1}get llmSampleRate(){return"number"==typeof this.config.llmSampleRate&&this.config.llmSampleRate>0?this.config.llmSampleRate:16e3}get faceDetection(){return t.assign({frame:3,confidence:80},this.config.faceDetection)}get autoClearSubtitle(){const{user:e,aiia:s}=t.assign({},this.config.autoClearSubtitle);let i=3e3,o=3e3;return"number"==typeof e?i=e>=0?e:i:!1===e&&(i=NaN),"number"==typeof s?o=s>=0?s:o:!1===s&&(o=NaN),{userDelayTime:i,aiiaDelayTime:o}}constructor(e){this.config=e}}var f;!function(e){e["通道、雲端服務、麥克風都已就緒"]="CODE: 1000",e["未取得媒體裝置權限,請重新設定或忽略此訊息"]="CODE: 1001",e["請訂閱專案列表"]="CODE: 1002",e["初始化必要依賴"]="CODE: 1010",e["指定依賴已完成"]="CODE: 1011",e["啟動SDK"]="CODE: 1012",e["結束SDK"]="CODE: 1013",e["安全的關閉連線"]="CODE: 2000",e["無副作用的關閉連線"]="CODE: 2001",e["與雲端的服務中斷"]="CODE: 2002",e["沒有授權,請聯繫Graphen"]="CODE: 2003",e["自動重新連線連端服務"]="CODE: 2004",e["未預期的斷線,請聯繫Graphen"]="CODE: 2005",e["開始初始化必要模組"]="CODE: 2010",e["指定模組初始化完成"]="CODE: 2011",e["啟動服務"]="CODE: 2012",e["服務結束"]="CODE: 2013"}(f||(f={}));const p={code:console.log,debug:console.log,info:console.log,warn:console.warn,error:console.error,fatal:console.error},S=[];function _(e){return S.push(e),_}const E=new Proxy(p,{get(e,i,o){if("string"==typeof i)switch(i){case"debug":case"info":case"error":case"warn":case"fatal":case"code":return(...o)=>{const r=s().format("yyyy-MM-DD[T]HH:mm:ss");t.forEach(S,e=>{e.next(i,r,...o)}),e[i](`[Aiia::${i}_${r}_]`,...o)};default:return}},set:(e,t,s,i)=>!0});class b{config;server;memberSub;get port(){return this.config.port}get webserver(){return this.config.webserver}get memberChange(){return this.memberSub.asObservable()}constructor(e){this.config=e,this.memberSub=new o.Subject}start(){if(this.server=new i.Server(this.webserver,{cors:{origin:"*"}}),this.server.on("connection",e=>{this.memberSub.next({type:"in",socket:e}),E.debug("client on",e.id),e.on("disconnect",t=>{this.memberSub.next({type:"out",socket:e}),E.debug("client off",t)})}),void 0===this.webserver){const e=this.port;this.server.listen(e),E.info("Websocket server run on",e)}else E.info("Websocket server rely your webserver.")}onDestroy(){this.server&&this.server.close()}}class D{instance;controller;constructor(e){this.controller=new AbortController,this.instance=r.create({baseURL:e,signal:this.controller.signal})}async getAccessToken(s,i){const o={license_number:s,token_value:i??""},r=await this.instance.post("/api/v1/license/agent_token",o).then(e=>t.get(e.data,"token_value","")).catch(()=>null);if(null===r)throw new Error(e["網絡錯誤"]);if(""===r)throw new Error(e["獲取權杖失敗"]);return r}async getProjectList(s){const i=await this.instance.post("/api/v1/license/projects",{license_number:s}).then(e=>t.map(t.get(e,"data.projects",[]),({id:e,sub_project_name:t})=>({id:e,name:t}))).catch(e=>null);if(null===i)throw new Error(e["網絡錯誤"]);if(0===i.length)throw new Error(e["沒有可用專案"]);return i}onDestroy(){this.controller.abort()}}var m;!function(e){e[e.MDN_NORMAL_CLOSURE=1e3]="MDN_NORMAL_CLOSURE",e[e.MDN_GOING_AWAY=1001]="MDN_GOING_AWAY",e[e.MDN_PROTOCOL_ERROR=1002]="MDN_PROTOCOL_ERROR",e[e.MDN_UNSUPPORTED_DATA=1003]="MDN_UNSUPPORTED_DATA",e[e.MDN_RESERVED_1004=1004]="MDN_RESERVED_1004",e[e.MDN_NO_STATUS_RECEIVED=1005]="MDN_NO_STATUS_RECEIVED",e[e.MDN_ABNORMAL_CLOSURE=1006]="MDN_ABNORMAL_CLOSURE",e[e.MDN_INVALID_FRAME_PAYLOAD=1007]="MDN_INVALID_FRAME_PAYLOAD",e[e.MDN_POLICY_VIOLATION=1008]="MDN_POLICY_VIOLATION",e[e.MDN_MESSAGE_TOO_BIG=1009]="MDN_MESSAGE_TOO_BIG",e[e.MDN_MANDATORY_EXTENSION=1010]="MDN_MANDATORY_EXTENSION",e[e.MDN_INTERNAL_ERROR=1011]="MDN_INTERNAL_ERROR",e[e.MDN_SERVICE_RESTART=1012]="MDN_SERVICE_RESTART",e[e.MDN_TRY_AGAIN_LATER=1013]="MDN_TRY_AGAIN_LATER",e[e.MDN_BAD_GATEWAY=1014]="MDN_BAD_GATEWAY",e[e.MDN_TLS_HANDSHAKE=1015]="MDN_TLS_HANDSHAKE",e[e.MANUAL_CLOSE=4001]="MANUAL_CLOSE",e[e.WS_5000_NORAML_CLOSURE=5e3]="WS_5000_NORAML_CLOSURE",e[e.WS_5001_UNAUTHORIZED=5001]="WS_5001_UNAUTHORIZED",e[e.WS_5002_LICENSE_EXPIRED=5002]="WS_5002_LICENSE_EXPIRED",e[e.WS_5003_TOKEN_EXPIRED=5003]="WS_5003_TOKEN_EXPIRED"}(m||(m={}));class y{socket;messageSub;linkStartSub;destorySub;closeSub;get message(){return this.messageSub.asObservable()}get close(){return this.closeSub.asObservable()}get linkStart(){return this.linkStartSub.asObservable()}messageQueue=[];isConnected;constructor(){this.isConnected=!1,this.messageSub=new o.Subject,this.linkStartSub=new o.Subject,this.closeSub=new o.Subject,this.destorySub=new o.Subject}start(e){this.socket=new n(e,{autoPong:!0}),o.fromEvent(this.socket,"open").pipe(o.takeUntil(this.destorySub)).subscribe(()=>{this.isConnected=!0,E.code(f["啟動服務"],"proxy"),this.flushMessageQueue(),this.linkStartSub.next()}),o.fromEvent(this.socket,"message").pipe(o.takeUntil(this.destorySub)).subscribe(e=>{const{data:t}=e;this.messageSub.next(t)}),o.fromEvent(this.socket,"error").pipe(o.takeUntil(this.destorySub)).subscribe(e=>{const{error:t}=e;E.error("Proxy has some problem",t)}),o.fromEvent(this.socket,"close").pipe(o.takeUntil(this.destorySub)).subscribe(e=>{this.isConnected=!1;const{code:t,reason:s}=e;switch(t){case m.MANUAL_CLOSE:E.code(f["無副作用的關閉連線"]);break;case m.WS_5000_NORAML_CLOSURE:case m.MDN_NORMAL_CLOSURE:case m.MDN_GOING_AWAY:this.closeSub.next("close"),E.code(f["安全的關閉連線"]);break;case m.MDN_NO_STATUS_RECEIVED:case m.MDN_ABNORMAL_CLOSURE:this.closeSub.next("close"),E.code(f["與雲端的服務中斷"],{code:t,reason:s});break;case m.WS_5001_UNAUTHORIZED:case m.WS_5002_LICENSE_EXPIRED:this.closeSub.next("no_permissions"),E.code(f["沒有授權,請聯繫Graphen"]);break;case m.WS_5003_TOKEN_EXPIRED:this.closeSub.next("reconnect"),E.code(f["自動重新連線連端服務"]);break;default:this.closeSub.next("close"),E.code(f["未預期的斷線,請聯繫Graphen"],{code:t,reason:s})}})}reconnect(e){this.onDestroy(),this.destorySub=new o.Subject,this.start(e)}flushMessageQueue(){for(;this.messageQueue.length>0&&this.isConnected&&void 0!==this.socket;){const e=this.messageQueue.shift();e&&this.socket.send(e)}}send(e){this.isConnected&&void 0!==this.socket?this.socket.send(e):this.messageQueue.push(e)}onClose(){this.socket?.close(m.MANUAL_CLOSE)}onDestroy(){this.destorySub.next(),this.destorySub.complete()}}const O=new class{AppName;platform;isMAC;isWIN;isLINUX;saveFolder;homedir;appFolder;EOL;constructor(e){switch(this.AppName=e,this.platform=process.platform,this.homedir=a.homedir(),this.EOL=a.EOL,this.platform){case"darwin":this.isMAC=!0,this.isWIN=!1,this.isLINUX=!1,this.saveFolder=h.join(this.homedir,"Library","Application Support");break;case"win32":this.isMAC=!1,this.isWIN=!0,this.isLINUX=!1,this.saveFolder=process.env.APPDATA||h.join(this.homedir,"AppData","Roaming");break;case"linux":this.isMAC=!1,this.isWIN=!1,this.isLINUX=!0,this.saveFolder=process.env.XDG_CONFIG_HOME||h.join(this.homedir,".config");break;default:console.log(`當前系統是未知的平台: ${this.platform}`),this.isMAC=!1,this.isWIN=!1,this.isLINUX=!1,this.saveFolder=this.homedir}this.appFolder=h.join(this.saveFolder,this.AppName),this.folderCheck(this.appFolder)}addPath(...e){return h.join(...e)}folderCheck(e){c.mkdirSync(e,{recursive:!0})}fileCheck(e){c.existsSync(e)||c.writeFileSync(e,"",{encoding:"utf8"})}}("aiia-sdk");class N{type="node";filePath;fileName;constructor(){this.fileName="storage.json",this.filePath=O.addPath(O.appFolder,this.fileName),O.fileCheck(this.filePath)}getFileDataSync(){const e=c.readFileSync(this.filePath,"utf8");try{return JSON.parse(e)}catch(e){return{}}}setFileDataSync(e){try{const t=JSON.stringify(e);c.writeFileSync(this.filePath,t,"utf8")}catch(e){}}async getFileData(){return new Promise(e=>{c.readFile(this.filePath,"utf8",(t,s)=>{if(t)e({});else try{e(JSON.parse(s))}catch(t){e({})}})})}async setFileData(e){return new Promise(t=>{try{const s=JSON.stringify(e);c.writeFile(this.filePath,s,"utf8",e=>{t()})}catch(e){}})}async getItem(e){return(await this.getFileData())[e]??null}async setItem(e,t){const s=await this.getFileData();s[e]=t,await this.setFileData(s)}async removeItem(e){const s=await this.getFileData(),i=t.omit(s,e);await this.setFileData(i)}async clear(){await this.setFileData({})}getItemSync(e){return this.getFileDataSync()[e]??null}setItemSync(e,t){const s=this.getFileDataSync();s[e]=t,this.setFileDataSync(s)}removeItemSync(e){const s=t.omit(this.getFileDataSync(),e);this.setFileDataSync(s)}clearSync(){this.setFileDataSync({})}}class R{config;constructor(e){this.config=e,console.log("UEProxy, config=",e)}start(){console.log("UEProxy, start proxy")}send(e){let t=this.cloudToObj(e);if(t&&t.signal&&"osc"==t.signal){console.log("UEProxy, payload=",t);try{let e=t.content;const s=new l.Client(e.host,e.port);console.debug("UEProxy, client open to port "+e.port),s.send(e.message,e=>{e&&console.error(e),s.close(),console.debug("UEProxy, client closed")})}catch(e){console.log(e)}}}cloudToObj(e){try{return JSON.parse(e)}catch(e){console.error("message transfer fail",e)}}onDestroy(){console.log("UEProxy, destroy proxy")}}class A{filePath;fileName;constructor(){this.fileName="log.txt",this.filePath=O.addPath(O.appFolder,this.fileName)}next(e,t,...s){try{const i=s.reduce((e,t)=>{switch(typeof t){case"string":case"number":case"bigint":return`${e} ${t.toString()}`;case"boolean":return`${e} ${t?"true":"false"}`;case"object":try{return`${e} ${JSON.stringify(t)}`}catch(t){return e}case"symbol":case"undefined":case"function":return`${e} [${typeof t}]`}},"");c.appendFileSync(this.filePath,`LOG(${e})__${t}__::${i}${O.EOL}`,{encoding:"utf8"})}catch(e){}}}class w{config;stroage;apiProxy;chat;udpProxy;isStart;channelMap;destorySub;access_token;get accessToken(){return this.access_token}constructor(e,t){this.config=e,this.stroage=t,this.isStart=!1,this.destorySub=new o.Subject,this.channelMap=new Map,this.access_token=t.getItemSync(u),this.apiProxy=new D(this.config.endPoint.api),E.code(f["指定模組初始化完成"],"api"),this.chat=new b(this.config),E.code(f["指定模組初始化完成"],"chat"),this.udpProxy=new R(this.config),E.code(f["指定模組初始化完成"],"udp")}async start(){if(this.isStart)throw new Error(e["重複初始化"]);this.isStart=!0,await this.replaceAccessToken(),this.chat.memberChange.pipe(o.takeUntil(this.destorySub)).subscribe(({type:e,socket:s})=>{switch(e){case"in":{E.debug("member join",s.id);const e=new y,r=new o.Subject,n=(i=s.handshake.query.uuid,t.isArray(i)?t.get(i,"[0]",void 0):i??void 0);void 0===n?(this.getProjectList().then(e=>{s.emit("list",e)}),o.fromEvent(s,"uuid").pipe(o.take(1),o.takeUntil(r)).subscribe(t=>{this.openChannel(t,s,e,r)})):this.openChannel(n,s,e,r),this.channelMap.set(s.id,()=>{r.next(),r.complete()});break}case"out":E.debug("member leave",s.id),this.channelMap.has(s.id)&&(this.channelMap.get(s.id)(),this.channelMap.delete(s.id))}var i}),this.chat.start(),E.code(f["啟動服務"],"chat"),this.udpProxy.start(),E.code(f["啟動服務"],"udp")}async replaceAccessToken(){const e=await this.apiProxy.getAccessToken(this.config.license,this.access_token);return this.access_token=e,this.stroage.setItemSync(u,e),e}async getProjectList(){return await this.apiProxy.getProjectList(this.config.license)}getProxySocketURL(e){return`${this.config.endPoint.socket}/v1/chat/project/${this.accessToken}/${e}`}openChannel(e,t,s,i){o.fromEvent(t,"cloud").pipe(o.takeUntil(i)).subscribe(e=>{s.send(e)}),s.message.pipe(o.takeUntil(i)).subscribe(e=>{t.emit("cloud",e),this.udpProxy.send(e)}),s.close.pipe(o.takeUntil(i)).subscribe(async i=>{switch(t.emit("channel",i),i){case"close":case"no_permissions":s.onDestroy();break;case"reconnect":await this.replaceAccessToken(),s.reconnect(this.getProxySocketURL(e))}}),s.linkStart.pipe(o.takeUntil(i)).subscribe(()=>{t.emit("channel","open")}),i.subscribe(()=>{s.onClose()}),s.start(this.getProxySocketURL(e))}onDestroy(){this.apiProxy.onDestroy(),this.chat.onDestroy(),this.udpProxy.onDestroy(),this.destorySub.next(),this.destorySub.complete(),E.code(f["服務結束"])}}exports.aiiaCore=function(s){if(d){if(void 0===t.get(s,"license",void 0))throw new Error(e["未提供憑證"]);!function(e){const{info:t,error:s,warn:i,debug:o,fatal:r,code:n}=function(e){if(void 0===e||"all"===e)return{code:!0,error:!0,warn:!0,info:!0,debug:!0,fatal:!0};if("none"===e)return{error:!1,code:!1,warn:!1,info:!1,debug:!1,fatal:!1};if("boolean"==typeof e)return{code:e,error:e,warn:e,info:e,debug:e,fatal:e};{const t=new Set(e);return{code:t.has("code"),debug:t.has("debug"),fatal:t.has("fatal"),error:t.has("error"),warn:t.has("warn"),info:t.has("info")}}}(e);return n||(p.code=()=>{}),o||(p.debug=()=>{}),t||(p.info=()=>{}),i||(p.warn=()=>{}),s||(p.error=()=>{}),r||(p.fatal=()=>{}),_}(t.get(s,"debug",!0))(new A),E.code(f["開始初始化必要模組"]);const i=new g(t.assign({},s));E.code(f["指定模組初始化完成"],"config");const o=new N;E.code(f["指定模組初始化完成"],"storage");const r=new w(i,o);return E.code(f["指定模組初始化完成"],"core"),r.start(),E.code(f["啟動服務"]),()=>{r.onDestroy()}}throw new Error(e["環境錯誤(nodejs)"])};
|
|
1
|
+
"use strict";var e,t=require("lodash-es"),s=require("moment"),i=require("socket.io"),o=require("rxjs"),r=require("axios"),n=require("ws"),c=require("fs"),a=require("os"),h=require("path"),l=require("node-osc");!function(e){e["網絡錯誤"]="ERROR CODE: 500",e["環境錯誤(browser)"]="ERROR CODE: 1001",e["Worklet模組載入失敗"]="ERROR CODE: 1002",e["未獲得媒體裝置權限"]="ERROR CODE: 1003",e["實例已摧毀"]="ERROR CODE: 1004",e["環境錯誤(nodejs)"]="ERROR CODE: 2001",e["未提供憑證"]="ERROR CODE: 2002",e["獲取權杖失敗"]="ERROR CODE: 2003",e["沒有可用專案"]="ERROR CODE: 2004",e["重複初始化"]="ERROR CODE: 2005"}(e||(e={}));const u="access_token",d="undefined"!=typeof process&&null!=process.versions&&null!=process.versions.node;class S{config;get webserver(){return this.config.webserver}get environment(){return this.config.env}get license(){return this.config.license??""}get port(){return function(e,t){const s=t??NaN;if(e){const t=parseInt(e);return isNaN(t)?s:t}return s}(process.env.PORT,this.config.port??3e3)}get ws_url(){return this.config.ws_url??""}get worklet_url(){return this.config.worklet_url??""}get project(){const{project:e}=t.assign({},this.config);return void 0===e?{specific:!1}:"string"==typeof e?{name:e,id:e,specific:!1}:t.assign({},e,{specific:!0})}get endPoint(){return this.environment,{api:"https://aiia-content-management-dev-21193779403.asia-east1.run.app",socket:"wss://graphen-agentic-workflow-dev-21193779403.asia-east1.run.app"}}get debug(){return this.config.debug??!0}get mediaStream(){return this.config.mediaStream}get chunkTimeInSeconds(){return"number"==typeof this.config.chunkTimeInSeconds&&this.config.chunkTimeInSeconds>0?this.config.chunkTimeInSeconds:1}get llmSampleRate(){return"number"==typeof this.config.llmSampleRate&&this.config.llmSampleRate>0?this.config.llmSampleRate:16e3}get faceDetection(){return t.assign({frequency:3,confidence:80},this.config.faceDetection)}get autoClearSubtitle(){const{user:e,aiia:s}=t.assign({},this.config.autoClearSubtitle);let i=3e3,o=3e3;return"number"==typeof e?i=e>=0?e:i:!1===e&&(i=NaN),"number"==typeof s?o=s>=0?s:o:!1===s&&(o=NaN),{userDelayTime:i,aiiaDelayTime:o}}constructor(e){this.config=e}}var b;!function(e){e["通道、雲端服務、麥克風都已就緒"]="CODE: 1000",e["未取得媒體裝置權限,請重新設定或忽略此訊息"]="CODE: 1001",e["請訂閱專案列表"]="CODE: 1002",e["初始化必要依賴"]="CODE: 1010",e["指定依賴已完成"]="CODE: 1011",e["啟動SDK"]="CODE: 1012",e["結束SDK"]="CODE: 1013",e["安全的關閉連線"]="CODE: 2000",e["無副作用的關閉連線"]="CODE: 2001",e["與雲端的服務中斷"]="CODE: 2002",e["沒有授權,請聯繫Graphen"]="CODE: 2003",e["自動重新連線連端服務"]="CODE: 2004",e["未預期的斷線,請聯繫Graphen"]="CODE: 2005",e["開始初始化必要模組"]="CODE: 2010",e["指定模組初始化完成"]="CODE: 2011",e["啟動服務"]="CODE: 2012",e["服務結束"]="CODE: 2013"}(b||(b={}));const p={code:console.log,debug:console.log,info:console.log,warn:console.warn,error:console.error,fatal:console.error},f=[];function g(e){return f.push(e),g}const E=new Proxy(p,{get(e,i,o){if("string"==typeof i)switch(i){case"debug":case"info":case"error":case"warn":case"fatal":case"code":return(...o)=>{const r=s().format("yyyy-MM-DD[T]HH:mm:ss");t.forEach(f,e=>{e.next(i,r,...o)}),e[i](`[Aiia::${i}_${r}_]`,...o)};default:return}},set:(e,t,s,i)=>!0});class _{config;server;memberSub;get port(){return this.config.port}get webserver(){return this.config.webserver}get memberChange(){return this.memberSub.asObservable()}constructor(e){this.config=e,this.memberSub=new o.Subject}start(){if(this.server=new i.Server(this.webserver,{cors:{origin:"*"}}),this.server.on("connection",e=>{this.memberSub.next({type:"in",socket:e}),E.debug("client on",e.id),e.on("disconnect",t=>{this.memberSub.next({type:"out",socket:e}),E.debug("client off",t)})}),void 0===this.webserver){const e=this.port;this.server.listen(e),E.info("Websocket server run on",e)}else E.info("Websocket server rely your webserver.")}onDestroy(){this.server&&this.server.close()}}class y{instance;controller;constructor(e){this.controller=new AbortController,this.instance=r.create({baseURL:e,signal:this.controller.signal})}async getAccessToken(s,i){const o={license_number:s,token_value:i??""},r=await this.instance.post("/api/v1/license/agent_token",o).then(e=>t.get(e.data,"token_value","")).catch(()=>null);if(null===r)throw new Error(e["網絡錯誤"]);if(""===r)throw new Error(e["獲取權杖失敗"]);return r}async getProjectList(s){const i=await this.instance.post("/api/v1/license/projects",{license_number:s}).then(e=>t.map(t.get(e,"data.projects",[]),({id:e,sub_project_name:t})=>({id:e,name:t}))).catch(e=>null);if(null===i)throw new Error(e["網絡錯誤"]);if(0===i.length)throw new Error(e["沒有可用專案"]);return i}onDestroy(){this.controller.abort()}}var O;!function(e){e[e.MDN_NORMAL_CLOSURE=1e3]="MDN_NORMAL_CLOSURE",e[e.MDN_GOING_AWAY=1001]="MDN_GOING_AWAY",e[e.MDN_PROTOCOL_ERROR=1002]="MDN_PROTOCOL_ERROR",e[e.MDN_UNSUPPORTED_DATA=1003]="MDN_UNSUPPORTED_DATA",e[e.MDN_RESERVED_1004=1004]="MDN_RESERVED_1004",e[e.MDN_NO_STATUS_RECEIVED=1005]="MDN_NO_STATUS_RECEIVED",e[e.MDN_ABNORMAL_CLOSURE=1006]="MDN_ABNORMAL_CLOSURE",e[e.MDN_INVALID_FRAME_PAYLOAD=1007]="MDN_INVALID_FRAME_PAYLOAD",e[e.MDN_POLICY_VIOLATION=1008]="MDN_POLICY_VIOLATION",e[e.MDN_MESSAGE_TOO_BIG=1009]="MDN_MESSAGE_TOO_BIG",e[e.MDN_MANDATORY_EXTENSION=1010]="MDN_MANDATORY_EXTENSION",e[e.MDN_INTERNAL_ERROR=1011]="MDN_INTERNAL_ERROR",e[e.MDN_SERVICE_RESTART=1012]="MDN_SERVICE_RESTART",e[e.MDN_TRY_AGAIN_LATER=1013]="MDN_TRY_AGAIN_LATER",e[e.MDN_BAD_GATEWAY=1014]="MDN_BAD_GATEWAY",e[e.MDN_TLS_HANDSHAKE=1015]="MDN_TLS_HANDSHAKE",e[e.WHEN_BROWSER_CLOSE=4001]="WHEN_BROWSER_CLOSE",e[e.MANUAL_CLOSE=4002]="MANUAL_CLOSE",e[e.WS_5000_NORAML_CLOSURE=5e3]="WS_5000_NORAML_CLOSURE",e[e.WS_5001_UNAUTHORIZED=5001]="WS_5001_UNAUTHORIZED",e[e.WS_5002_LICENSE_EXPIRED=5002]="WS_5002_LICENSE_EXPIRED",e[e.WS_5003_TOKEN_EXPIRED=5003]="WS_5003_TOKEN_EXPIRED"}(O||(O={}));class m{socket;messageSub;linkStartSub;destorySub;closeSub;get message(){return this.messageSub.asObservable()}get close(){return this.closeSub.asObservable()}get linkStart(){return this.linkStartSub.asObservable()}messageQueue=[];isConnected;constructor(){this.isConnected=!1,this.messageSub=new o.Subject,this.linkStartSub=new o.Subject,this.closeSub=new o.Subject,this.destorySub=new o.Subject}start(e){this.socket=new n(e,{autoPong:!0}),o.fromEvent(this.socket,"open").pipe(o.takeUntil(this.destorySub)).subscribe(()=>{this.isConnected=!0,E.code(b["啟動服務"],"proxy"),this.flushMessageQueue(),this.linkStartSub.next()}),o.fromEvent(this.socket,"message").pipe(o.takeUntil(this.destorySub)).subscribe(e=>{const{data:t}=e;this.messageSub.next(t)}),o.fromEvent(this.socket,"error").pipe(o.takeUntil(this.destorySub)).subscribe(e=>{const{error:t}=e;E.error("Proxy has some problem",t)}),o.fromEvent(this.socket,"close").pipe(o.takeUntil(this.destorySub)).subscribe(e=>{this.isConnected=!1;const{code:t,reason:s,type:i,wasClean:o}=e;switch(t){case O.WHEN_BROWSER_CLOSE:E.code(b["無副作用的關閉連線"]);break;case O.WS_5000_NORAML_CLOSURE:case O.MDN_NORMAL_CLOSURE:case O.MDN_GOING_AWAY:this.closeSub.next("close"),E.code(b["安全的關閉連線"]);break;case O.MDN_NO_STATUS_RECEIVED:case O.MDN_ABNORMAL_CLOSURE:this.closeSub.next("close"),E.code(b["與雲端的服務中斷"],{code:t,reason:s,type:i,wasClean:o});break;case O.WS_5001_UNAUTHORIZED:case O.WS_5002_LICENSE_EXPIRED:this.closeSub.next("no_permissions"),E.code(b["沒有授權,請聯繫Graphen"]);break;case O.WS_5003_TOKEN_EXPIRED:this.closeSub.next("reconnect"),E.code(b["自動重新連線連端服務"]);break;default:this.closeSub.next("close"),E.code(b["未預期的斷線,請聯繫Graphen"],{code:t,reason:s,type:i,wasClean:o})}})}reconnect(e){this.onDestroy(),this.destorySub=new o.Subject,this.start(e)}flushMessageQueue(){for(;this.messageQueue.length>0&&this.isConnected&&void 0!==this.socket;){const e=this.messageQueue.shift();e&&this.socket.send(e)}}send(e){this.isConnected&&void 0!==this.socket?this.socket.send(e):this.messageQueue.push(e)}onClose(){this.socket?.close(O.WHEN_BROWSER_CLOSE)}onDestroy(){this.destorySub.next(),this.destorySub.complete()}}const D=new class{AppName;platform;isMAC;isWIN;isLINUX;saveFolder;homedir;appFolder;EOL;constructor(e){switch(this.AppName=e,this.platform=process.platform,this.homedir=a.homedir(),this.EOL=a.EOL,this.platform){case"darwin":this.isMAC=!0,this.isWIN=!1,this.isLINUX=!1,this.saveFolder=h.join(this.homedir,"Library","Application Support");break;case"win32":this.isMAC=!1,this.isWIN=!0,this.isLINUX=!1,this.saveFolder=process.env.APPDATA||h.join(this.homedir,"AppData","Roaming");break;case"linux":this.isMAC=!1,this.isWIN=!1,this.isLINUX=!0,this.saveFolder=process.env.XDG_CONFIG_HOME||h.join(this.homedir,".config");break;default:console.log(`當前系統是未知的平台: ${this.platform}`),this.isMAC=!1,this.isWIN=!1,this.isLINUX=!1,this.saveFolder=this.homedir}this.appFolder=h.join(this.saveFolder,this.AppName),this.folderCheck(this.appFolder)}addPath(...e){return h.join(...e)}folderCheck(e){c.mkdirSync(e,{recursive:!0})}fileCheck(e){c.existsSync(e)||c.writeFileSync(e,"",{encoding:"utf8"})}}("aiia-sdk");class N{type="node";filePath;fileName;constructor(){this.fileName="storage.json",this.filePath=D.addPath(D.appFolder,this.fileName),D.fileCheck(this.filePath)}getFileDataSync(){const e=c.readFileSync(this.filePath,"utf8");try{return JSON.parse(e)}catch(e){return{}}}setFileDataSync(e){try{const t=JSON.stringify(e);c.writeFileSync(this.filePath,t,"utf8")}catch(e){}}async getFileData(){return new Promise(e=>{c.readFile(this.filePath,"utf8",(t,s)=>{if(t)e({});else try{e(JSON.parse(s))}catch(t){e({})}})})}async setFileData(e){return new Promise(t=>{try{const s=JSON.stringify(e);c.writeFile(this.filePath,s,"utf8",e=>{t()})}catch(e){}})}async getItem(e){return(await this.getFileData())[e]??null}async setItem(e,t){const s=await this.getFileData();s[e]=t,await this.setFileData(s)}async removeItem(e){const s=await this.getFileData(),i=t.omit(s,e);await this.setFileData(i)}async clear(){await this.setFileData({})}getItemSync(e){return this.getFileDataSync()[e]??null}setItemSync(e,t){const s=this.getFileDataSync();s[e]=t,this.setFileDataSync(s)}removeItemSync(e){const s=t.omit(this.getFileDataSync(),e);this.setFileDataSync(s)}clearSync(){this.setFileDataSync({})}}class R{config;constructor(e){this.config=e,E.info("UEProxy, config=",e)}start(){E.info("UEProxy, start proxy")}send(e){let t=this.cloudToObj(e);if(t&&t.signal&&"osc"===t.signal){E.info("UEProxy, payload=",t);try{let e=t.content;const s=new l.Client(e.host,e.port);E.debug("UEProxy, client open to port "+e.port),s.send(e.message,e=>{e&&E.error(e),s.close(),E.debug("UEProxy, client closed")})}catch(e){E.error(e)}}}cloudToObj(e){try{return JSON.parse(e)}catch(e){E.error("message transfer fail",e)}}onDestroy(){E.info("UEProxy, destroy proxy")}}class k{socket;messageSub;linkStartSub;destorySub;closeSub;get message(){return this.messageSub.asObservable()}get close(){return this.closeSub.asObservable()}get linkStart(){return this.linkStartSub.asObservable()}messageQueue=[];isConnected;constructor(){this.isConnected=!1,this.messageSub=new o.Subject,this.linkStartSub=new o.Subject,this.closeSub=new o.Subject,this.destorySub=new o.Subject}url="ws://localhost:8080";start(){this.socket=new n(this.url,{autoPong:!0}),o.fromEvent(this.socket,"open").pipe(o.takeUntil(this.destorySub)).subscribe(()=>{this.isConnected=!0,E.code(b["啟動服務"],"proxy"),this.flushMessageQueue(),this.linkStartSub.next()}),o.fromEvent(this.socket,"message").pipe(o.takeUntil(this.destorySub)).subscribe(e=>{const{data:t}=e;this.messageSub.next(t)}),o.fromEvent(this.socket,"error").pipe(o.takeUntil(this.destorySub)).subscribe(e=>{const{error:t}=e;E.error("Proxy has some problem",t)}),o.fromEvent(this.socket,"close").pipe(o.takeUntil(this.destorySub)).subscribe(e=>{this.isConnected=!1;const{code:t,reason:s}=e;switch(t){case O.MANUAL_CLOSE:E.code(b["無副作用的關閉連線"]);break;case O.WS_5000_NORAML_CLOSURE:case O.MDN_NORMAL_CLOSURE:case O.MDN_GOING_AWAY:this.closeSub.next("close"),E.code(b["安全的關閉連線"]);break;case O.MDN_NO_STATUS_RECEIVED:case O.MDN_ABNORMAL_CLOSURE:this.closeSub.next("close"),E.code(b["與雲端的服務中斷"],{code:t,reason:s});break;case O.WS_5001_UNAUTHORIZED:case O.WS_5002_LICENSE_EXPIRED:this.closeSub.next("no_permissions"),E.code(b["沒有授權,請聯繫Graphen"]);break;case O.WS_5003_TOKEN_EXPIRED:this.closeSub.next("reconnect"),E.code(b["自動重新連線連端服務"]);break;default:this.closeSub.next("close"),E.code(b["未預期的斷線,請聯繫Graphen"],{code:t,reason:s})}})}reconnect(){this.onDestroy(),this.destorySub=new o.Subject,this.start()}flushMessageQueue(){for(;this.messageQueue.length>0&&this.isConnected&&void 0!==this.socket;){const e=this.messageQueue.shift();e&&this.socket.send(e)}}send(e){let t=this.cloudToObj(e);if(t&&t.signal&&"audio"===t.signal&&(E.info("AudioSocketProxy, payload=",t),"pcm"===t.command&&this.isConnected&&void 0!==this.socket))try{const e=t.content,s=Buffer.allocUnsafe(2*e.length);for(let t=0;t<e.length;t++)s.writeInt16LE(e[t],2*t);this.socket.send(s)}catch(e){E.error(e)}}cloudToObj(e){try{return JSON.parse(e)}catch(e){E.error("message transfer fail",e)}}onClose(){this.socket?.close(O.MANUAL_CLOSE)}onDestroy(){this.destorySub.next(),this.destorySub.complete()}}class w{filePath;fileName;constructor(){this.fileName="log.txt",this.filePath=D.addPath(D.appFolder,this.fileName)}next(e,t,...s){try{const i=s.reduce((e,t)=>{switch(typeof t){case"string":case"number":case"bigint":return`${e} ${t.toString()}`;case"boolean":return`${e} ${t?"true":"false"}`;case"object":try{return`${e} ${JSON.stringify(t)}`}catch(t){return e}case"symbol":case"undefined":case"function":return`${e} [${typeof t}]`}},"");c.appendFileSync(this.filePath,`LOG(${e})__${t}__::${i}${D.EOL}`,{encoding:"utf8"})}catch(e){}}}class A{config;stroage;apiProxy;chat;udpProxy;audioProxy;isStart;channelMap;destorySub;access_token;get accessToken(){return this.access_token}constructor(e,t){this.config=e,this.stroage=t,this.isStart=!1,this.destorySub=new o.Subject,this.channelMap=new Map,this.access_token=t.getItemSync(u),this.apiProxy=new y(this.config.endPoint.api),E.code(b["指定模組初始化完成"],"api"),this.chat=new _(this.config),E.code(b["指定模組初始化完成"],"chat"),this.udpProxy=new R(this.config),E.code(b["指定模組初始化完成"],"udp"),this.audioProxy=new k,E.code(b["指定模組初始化完成"],"audio"),this.openChannel=this.openChannel.bind(this)}async start(){if(this.isStart)throw new Error(e["重複初始化"]);this.isStart=!0,await this.replaceAccessToken(),this.chat.memberChange.pipe(o.takeUntil(this.destorySub)).subscribe(({type:e,socket:s})=>{switch(e){case"in":{const e=new m,r=new o.Subject,n=(i=s.handshake.query.uuid,t.isArray(i)?t.get(i,"[0]",void 0):i??void 0);void 0===n?(this.getProjectList().then(e=>{s.emit("list",e)}),o.fromEvent(s,"uuid").pipe(o.take(1),o.takeUntil(r)).subscribe(t=>{this.openChannel(t,s,e,r)})):this.openChannel(n,s,e,r),this.channelMap.set(s.id,()=>{r.next(),r.complete()});break}case"out":this.channelMap.has(s.id)&&(this.channelMap.get(s.id)(),this.channelMap.delete(s.id))}var i}),this.chat.start(),E.code(b["啟動服務"],"chat"),this.udpProxy.start(),E.code(b["啟動服務"],"udp")}async replaceAccessToken(){const e=await this.apiProxy.getAccessToken(this.config.license,this.access_token);return this.access_token=e,this.stroage.setItemSync(u,e),e}async getProjectList(){return await this.apiProxy.getProjectList(this.config.license)}getProxySocketURL(e){return`${this.config.endPoint.socket}/v1/chat/project/${this.accessToken}/${e}`}openChannel(e,t,s,i){o.fromEvent(t,"cloud").pipe(o.takeUntil(i)).subscribe(e=>{s.send(e)}),s.message.pipe(o.takeUntil(i)).subscribe(e=>{t.emit("cloud",e),this.udpProxy.send(e),this.audioProxy.send(e)}),s.close.pipe(o.takeUntil(i)).subscribe(async i=>{switch(t.emit("channel",i),i){case"close":case"no_permissions":s.onDestroy();break;case"reconnect":await this.replaceAccessToken(),s.reconnect(this.getProxySocketURL(e))}}),s.linkStart.pipe(o.takeUntil(i)).subscribe(()=>{t.emit("channel","open")}),i.subscribe(()=>{s.onClose(),this.audioProxy.onClose()}),s.start(this.getProxySocketURL(e)),this.audioProxy.start()}onDestroy(){this.apiProxy.onDestroy(),this.chat.onDestroy(),this.udpProxy.onDestroy(),this.destorySub.next(),this.destorySub.complete(),E.code(b["服務結束"])}}exports.aiiaCore=function(s){if(d){if(void 0===t.get(s,"license",void 0))throw new Error(e["未提供憑證"]);!function(e){const{info:t,error:s,warn:i,debug:o,fatal:r,code:n}=function(e){if(void 0===e||"all"===e)return{code:!0,error:!0,warn:!0,info:!0,debug:!0,fatal:!0};if("none"===e)return{error:!1,code:!1,warn:!1,info:!1,debug:!1,fatal:!1};if("boolean"==typeof e)return{code:e,error:e,warn:e,info:e,debug:e,fatal:e};{const t=new Set(e);return{code:t.has("code"),debug:t.has("debug"),fatal:t.has("fatal"),error:t.has("error"),warn:t.has("warn"),info:t.has("info")}}}(e);return n||(p.code=()=>{}),o||(p.debug=()=>{}),t||(p.info=()=>{}),i||(p.warn=()=>{}),s||(p.error=()=>{}),r||(p.fatal=()=>{}),g}(t.get(s,"debug",!0))(new w),E.code(b["開始初始化必要模組"]);const i=new S(t.assign({},s));E.code(b["指定模組初始化完成"],"config");const o=new N;E.code(b["指定模組初始化完成"],"storage");const r=new A(i,o);return E.code(b["指定模組初始化完成"],"core"),r.start(),E.code(b["啟動服務"]),()=>{r.onDestroy()}}throw new Error(e["環境錯誤(nodejs)"])};
|
package/dist/node.d.ts
CHANGED
|
@@ -112,19 +112,19 @@ declare interface InitOptions {
|
|
|
112
112
|
* [zh]
|
|
113
113
|
* 人臉辨識
|
|
114
114
|
*
|
|
115
|
-
*
|
|
115
|
+
* frequency: 每幾幀更新一次, 預設: 3
|
|
116
116
|
*
|
|
117
117
|
* confidence: 分數(0 ~ 100), 預設: 80
|
|
118
118
|
*
|
|
119
119
|
* [en]
|
|
120
120
|
* facial recognition
|
|
121
121
|
*
|
|
122
|
-
*
|
|
122
|
+
* frequency: Update every few frames, default: 3
|
|
123
123
|
*
|
|
124
124
|
* confidence: score(0 ~ 100), default: 80
|
|
125
125
|
*/
|
|
126
126
|
faceDetection?: {
|
|
127
|
-
|
|
127
|
+
frequency?: number;
|
|
128
128
|
confidence?: number;
|
|
129
129
|
};
|
|
130
130
|
/**
|
|
@@ -142,7 +142,7 @@ declare interface InitOptions {
|
|
|
142
142
|
|
|
143
143
|
declare type LoggerLevel = "info" | "error" | "warn" | "debug" | "fatal" | "code";
|
|
144
144
|
|
|
145
|
-
export declare type NodeInitOptions = Omit<InitOptions, "project" | "ws_url" | "worklet_url" | "mediaStream" | "chunkTimeInSeconds" | "llmSampleRate" | "autoClearSubtitle">;
|
|
145
|
+
export declare type NodeInitOptions = Omit<InitOptions, "project" | "ws_url" | "worklet_url" | "mediaStream" | "chunkTimeInSeconds" | "llmSampleRate" | "autoClearSubtitle" | "faceDetection">;
|
|
146
146
|
|
|
147
147
|
declare type Webserver = Server<any, any> | Server_2<any, any> | Http2SecureServer<any, any, any, any> | Http2Server<any, any, any, any>;
|
|
148
148
|
|
package/dist/node.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{assign as e,forEach as t,get as s,map as o,omit as i,isArray as r}from"lodash";import n from"moment";import{Server as c}from"socket.io";import{Subject as a,fromEvent as h,takeUntil as l,take as u}from"rxjs";import d from"axios";import p from"ws";import f from"fs";import g from"os";import _ from"path";import{Client as S}from"node-osc";var E;!function(e){e["網絡錯誤"]="ERROR CODE: 500",e["環境錯誤(browser)"]="ERROR CODE: 1001",e["Worklet模組載入失敗"]="ERROR CODE: 1002",e["未獲得媒體裝置權限"]="ERROR CODE: 1003",e["實例已摧毀"]="ERROR CODE: 1004",e["環境錯誤(nodejs)"]="ERROR CODE: 2001",e["未提供憑證"]="ERROR CODE: 2002",e["獲取權杖失敗"]="ERROR CODE: 2003",e["沒有可用專案"]="ERROR CODE: 2004",e["重複初始化"]="ERROR CODE: 2005"}(E||(E={}));const m="access_token",b="undefined"!=typeof process&&null!=process.versions&&null!=process.versions.node;class D{config;get webserver(){return this.config.webserver}get environment(){return this.config.env}get license(){return this.config.license??""}get port(){return function(e,t){const s=t??NaN;if(e){const t=parseInt(e);return isNaN(t)?s:t}return s}(process.env.PORT,this.config.port??3e3)}get ws_url(){return this.config.ws_url??""}get worklet_url(){return this.config.worklet_url??""}get project(){const{project:t}=e({},this.config);return void 0===t?{specific:!1}:"string"==typeof t?{name:t,id:t,specific:!1}:e({},t,{specific:!0})}get endPoint(){return this.environment,{api:"https://aiia-content-management-dev-21193779403.asia-east1.run.app",socket:"wss://graphen-agentic-workflow-dev-21193779403.asia-east1.run.app"}}get debug(){return this.config.debug??!0}get mediaStream(){return this.config.mediaStream}get chunkTimeInSeconds(){return"number"==typeof this.config.chunkTimeInSeconds&&this.config.chunkTimeInSeconds>0?this.config.chunkTimeInSeconds:1}get llmSampleRate(){return"number"==typeof this.config.llmSampleRate&&this.config.llmSampleRate>0?this.config.llmSampleRate:16e3}get faceDetection(){return e({frame:3,confidence:80},this.config.faceDetection)}get autoClearSubtitle(){const{user:t,aiia:s}=e({},this.config.autoClearSubtitle);let o=3e3,i=3e3;return"number"==typeof t?o=t>=0?t:o:!1===t&&(o=NaN),"number"==typeof s?i=s>=0?s:i:!1===s&&(i=NaN),{userDelayTime:o,aiiaDelayTime:i}}constructor(e){this.config=e}}var y;!function(e){e["通道、雲端服務、麥克風都已就緒"]="CODE: 1000",e["未取得媒體裝置權限,請重新設定或忽略此訊息"]="CODE: 1001",e["請訂閱專案列表"]="CODE: 1002",e["初始化必要依賴"]="CODE: 1010",e["指定依賴已完成"]="CODE: 1011",e["啟動SDK"]="CODE: 1012",e["結束SDK"]="CODE: 1013",e["安全的關閉連線"]="CODE: 2000",e["無副作用的關閉連線"]="CODE: 2001",e["與雲端的服務中斷"]="CODE: 2002",e["沒有授權,請聯繫Graphen"]="CODE: 2003",e["自動重新連線連端服務"]="CODE: 2004",e["未預期的斷線,請聯繫Graphen"]="CODE: 2005",e["開始初始化必要模組"]="CODE: 2010",e["指定模組初始化完成"]="CODE: 2011",e["啟動服務"]="CODE: 2012",e["服務結束"]="CODE: 2013"}(y||(y={}));const O={code:console.log,debug:console.log,info:console.log,warn:console.warn,error:console.error,fatal:console.error},N=[];function R(e){return N.push(e),R}const A=new Proxy(O,{get(e,s,o){if("string"==typeof s)switch(s){case"debug":case"info":case"error":case"warn":case"fatal":case"code":return(...o)=>{const i=n().format("yyyy-MM-DD[T]HH:mm:ss");t(N,e=>{e.next(s,i,...o)}),e[s](`[Aiia::${s}_${i}_]`,...o)};default:return}},set:(e,t,s,o)=>!0});class w{config;server;memberSub;get port(){return this.config.port}get webserver(){return this.config.webserver}get memberChange(){return this.memberSub.asObservable()}constructor(e){this.config=e,this.memberSub=new a}start(){if(this.server=new c(this.webserver,{cors:{origin:"*"}}),this.server.on("connection",e=>{this.memberSub.next({type:"in",socket:e}),A.debug("client on",e.id),e.on("disconnect",t=>{this.memberSub.next({type:"out",socket:e}),A.debug("client off",t)})}),void 0===this.webserver){const e=this.port;this.server.listen(e),A.info("Websocket server run on",e)}else A.info("Websocket server rely your webserver.")}onDestroy(){this.server&&this.server.close()}}class C{instance;controller;constructor(e){this.controller=new AbortController,this.instance=d.create({baseURL:e,signal:this.controller.signal})}async getAccessToken(e,t){const o={license_number:e,token_value:t??""},i=await this.instance.post("/api/v1/license/agent_token",o).then(e=>s(e.data,"token_value","")).catch(()=>null);if(null===i)throw new Error(E["網絡錯誤"]);if(""===i)throw new Error(E["獲取權杖失敗"]);return i}async getProjectList(e){const t=await this.instance.post("/api/v1/license/projects",{license_number:e}).then(e=>o(s(e,"data.projects",[]),({id:e,sub_project_name:t})=>({id:e,name:t}))).catch(e=>null);if(null===t)throw new Error(E["網絡錯誤"]);if(0===t.length)throw new Error(E["沒有可用專案"]);return t}onDestroy(){this.controller.abort()}}var M;!function(e){e[e.MDN_NORMAL_CLOSURE=1e3]="MDN_NORMAL_CLOSURE",e[e.MDN_GOING_AWAY=1001]="MDN_GOING_AWAY",e[e.MDN_PROTOCOL_ERROR=1002]="MDN_PROTOCOL_ERROR",e[e.MDN_UNSUPPORTED_DATA=1003]="MDN_UNSUPPORTED_DATA",e[e.MDN_RESERVED_1004=1004]="MDN_RESERVED_1004",e[e.MDN_NO_STATUS_RECEIVED=1005]="MDN_NO_STATUS_RECEIVED",e[e.MDN_ABNORMAL_CLOSURE=1006]="MDN_ABNORMAL_CLOSURE",e[e.MDN_INVALID_FRAME_PAYLOAD=1007]="MDN_INVALID_FRAME_PAYLOAD",e[e.MDN_POLICY_VIOLATION=1008]="MDN_POLICY_VIOLATION",e[e.MDN_MESSAGE_TOO_BIG=1009]="MDN_MESSAGE_TOO_BIG",e[e.MDN_MANDATORY_EXTENSION=1010]="MDN_MANDATORY_EXTENSION",e[e.MDN_INTERNAL_ERROR=1011]="MDN_INTERNAL_ERROR",e[e.MDN_SERVICE_RESTART=1012]="MDN_SERVICE_RESTART",e[e.MDN_TRY_AGAIN_LATER=1013]="MDN_TRY_AGAIN_LATER",e[e.MDN_BAD_GATEWAY=1014]="MDN_BAD_GATEWAY",e[e.MDN_TLS_HANDSHAKE=1015]="MDN_TLS_HANDSHAKE",e[e.MANUAL_CLOSE=4001]="MANUAL_CLOSE",e[e.WS_5000_NORAML_CLOSURE=5e3]="WS_5000_NORAML_CLOSURE",e[e.WS_5001_UNAUTHORIZED=5001]="WS_5001_UNAUTHORIZED",e[e.WS_5002_LICENSE_EXPIRED=5002]="WS_5002_LICENSE_EXPIRED",e[e.WS_5003_TOKEN_EXPIRED=5003]="WS_5003_TOKEN_EXPIRED"}(M||(M={}));class k{socket;messageSub;linkStartSub;destorySub;closeSub;get message(){return this.messageSub.asObservable()}get close(){return this.closeSub.asObservable()}get linkStart(){return this.linkStartSub.asObservable()}messageQueue=[];isConnected;constructor(){this.isConnected=!1,this.messageSub=new a,this.linkStartSub=new a,this.closeSub=new a,this.destorySub=new a}start(e){this.socket=new p(e,{autoPong:!0}),h(this.socket,"open").pipe(l(this.destorySub)).subscribe(()=>{this.isConnected=!0,A.code(y["啟動服務"],"proxy"),this.flushMessageQueue(),this.linkStartSub.next()}),h(this.socket,"message").pipe(l(this.destorySub)).subscribe(e=>{const{data:t}=e;this.messageSub.next(t)}),h(this.socket,"error").pipe(l(this.destorySub)).subscribe(e=>{const{error:t}=e;A.error("Proxy has some problem",t)}),h(this.socket,"close").pipe(l(this.destorySub)).subscribe(e=>{this.isConnected=!1;const{code:t,reason:s}=e;switch(t){case M.MANUAL_CLOSE:A.code(y["無副作用的關閉連線"]);break;case M.WS_5000_NORAML_CLOSURE:case M.MDN_NORMAL_CLOSURE:case M.MDN_GOING_AWAY:this.closeSub.next("close"),A.code(y["安全的關閉連線"]);break;case M.MDN_NO_STATUS_RECEIVED:case M.MDN_ABNORMAL_CLOSURE:this.closeSub.next("close"),A.code(y["與雲端的服務中斷"],{code:t,reason:s});break;case M.WS_5001_UNAUTHORIZED:case M.WS_5002_LICENSE_EXPIRED:this.closeSub.next("no_permissions"),A.code(y["沒有授權,請聯繫Graphen"]);break;case M.WS_5003_TOKEN_EXPIRED:this.closeSub.next("reconnect"),A.code(y["自動重新連線連端服務"]);break;default:this.closeSub.next("close"),A.code(y["未預期的斷線,請聯繫Graphen"],{code:t,reason:s})}})}reconnect(e){this.onDestroy(),this.destorySub=new a,this.start(e)}flushMessageQueue(){for(;this.messageQueue.length>0&&this.isConnected&&void 0!==this.socket;){const e=this.messageQueue.shift();e&&this.socket.send(e)}}send(e){this.isConnected&&void 0!==this.socket?this.socket.send(e):this.messageQueue.push(e)}onClose(){this.socket?.close(M.MANUAL_CLOSE)}onDestroy(){this.destorySub.next(),this.destorySub.complete()}}const L=new class{AppName;platform;isMAC;isWIN;isLINUX;saveFolder;homedir;appFolder;EOL;constructor(e){switch(this.AppName=e,this.platform=process.platform,this.homedir=g.homedir(),this.EOL=g.EOL,this.platform){case"darwin":this.isMAC=!0,this.isWIN=!1,this.isLINUX=!1,this.saveFolder=_.join(this.homedir,"Library","Application Support");break;case"win32":this.isMAC=!1,this.isWIN=!0,this.isLINUX=!1,this.saveFolder=process.env.APPDATA||_.join(this.homedir,"AppData","Roaming");break;case"linux":this.isMAC=!1,this.isWIN=!1,this.isLINUX=!0,this.saveFolder=process.env.XDG_CONFIG_HOME||_.join(this.homedir,".config");break;default:console.log(`當前系統是未知的平台: ${this.platform}`),this.isMAC=!1,this.isWIN=!1,this.isLINUX=!1,this.saveFolder=this.homedir}this.appFolder=_.join(this.saveFolder,this.AppName),this.folderCheck(this.appFolder)}addPath(...e){return _.join(...e)}folderCheck(e){f.mkdirSync(e,{recursive:!0})}fileCheck(e){f.existsSync(e)||f.writeFileSync(e,"",{encoding:"utf8"})}}("aiia-sdk");class P{type="node";filePath;fileName;constructor(){this.fileName="storage.json",this.filePath=L.addPath(L.appFolder,this.fileName),L.fileCheck(this.filePath)}getFileDataSync(){const e=f.readFileSync(this.filePath,"utf8");try{return JSON.parse(e)}catch(e){return{}}}setFileDataSync(e){try{const t=JSON.stringify(e);f.writeFileSync(this.filePath,t,"utf8")}catch(e){}}async getFileData(){return new Promise(e=>{f.readFile(this.filePath,"utf8",(t,s)=>{if(t)e({});else try{e(JSON.parse(s))}catch(t){e({})}})})}async setFileData(e){return new Promise(t=>{try{const s=JSON.stringify(e);f.writeFile(this.filePath,s,"utf8",e=>{t()})}catch(e){}})}async getItem(e){return(await this.getFileData())[e]??null}async setItem(e,t){const s=await this.getFileData();s[e]=t,await this.setFileData(s)}async removeItem(e){const t=await this.getFileData(),s=i(t,e);await this.setFileData(s)}async clear(){await this.setFileData({})}getItemSync(e){return this.getFileDataSync()[e]??null}setItemSync(e,t){const s=this.getFileDataSync();s[e]=t,this.setFileDataSync(s)}removeItemSync(e){const t=i(this.getFileDataSync(),e);this.setFileDataSync(t)}clearSync(){this.setFileDataSync({})}}class I{config;constructor(e){this.config=e,console.log("UEProxy, config=",e)}start(){console.log("UEProxy, start proxy")}send(e){let t=this.cloudToObj(e);if(t&&t.signal&&"osc"==t.signal){console.log("UEProxy, payload=",t);try{let e=t.content;const s=new S(e.host,e.port);console.debug("UEProxy, client open to port "+e.port),s.send(e.message,e=>{e&&console.error(e),s.close(),console.debug("UEProxy, client closed")})}catch(e){console.log(e)}}}cloudToObj(e){try{return JSON.parse(e)}catch(e){console.error("message transfer fail",e)}}onDestroy(){console.log("UEProxy, destroy proxy")}}class v{filePath;fileName;constructor(){this.fileName="log.txt",this.filePath=L.addPath(L.appFolder,this.fileName)}next(e,t,...s){try{const o=s.reduce((e,t)=>{switch(typeof t){case"string":case"number":case"bigint":return`${e} ${t.toString()}`;case"boolean":return`${e} ${t?"true":"false"}`;case"object":try{return`${e} ${JSON.stringify(t)}`}catch(t){return e}case"symbol":case"undefined":case"function":return`${e} [${typeof t}]`}},"");f.appendFileSync(this.filePath,`LOG(${e})__${t}__::${o}${L.EOL}`,{encoding:"utf8"})}catch(e){}}}class T{config;stroage;apiProxy;chat;udpProxy;isStart;channelMap;destorySub;access_token;get accessToken(){return this.access_token}constructor(e,t){this.config=e,this.stroage=t,this.isStart=!1,this.destorySub=new a,this.channelMap=new Map,this.access_token=t.getItemSync(m),this.apiProxy=new C(this.config.endPoint.api),A.code(y["指定模組初始化完成"],"api"),this.chat=new w(this.config),A.code(y["指定模組初始化完成"],"chat"),this.udpProxy=new I(this.config),A.code(y["指定模組初始化完成"],"udp")}async start(){if(this.isStart)throw new Error(E["重複初始化"]);this.isStart=!0,await this.replaceAccessToken(),this.chat.memberChange.pipe(l(this.destorySub)).subscribe(({type:e,socket:t})=>{switch(e){case"in":{A.debug("member join",t.id);const e=new k,i=new a,n=(o=t.handshake.query.uuid,r(o)?s(o,"[0]",void 0):o??void 0);void 0===n?(this.getProjectList().then(e=>{t.emit("list",e)}),h(t,"uuid").pipe(u(1),l(i)).subscribe(s=>{this.openChannel(s,t,e,i)})):this.openChannel(n,t,e,i),this.channelMap.set(t.id,()=>{i.next(),i.complete()});break}case"out":A.debug("member leave",t.id),this.channelMap.has(t.id)&&(this.channelMap.get(t.id)(),this.channelMap.delete(t.id))}var o}),this.chat.start(),A.code(y["啟動服務"],"chat"),this.udpProxy.start(),A.code(y["啟動服務"],"udp")}async replaceAccessToken(){const e=await this.apiProxy.getAccessToken(this.config.license,this.access_token);return this.access_token=e,this.stroage.setItemSync(m,e),e}async getProjectList(){return await this.apiProxy.getProjectList(this.config.license)}getProxySocketURL(e){return`${this.config.endPoint.socket}/v1/chat/project/${this.accessToken}/${e}`}openChannel(e,t,s,o){h(t,"cloud").pipe(l(o)).subscribe(e=>{s.send(e)}),s.message.pipe(l(o)).subscribe(e=>{t.emit("cloud",e),this.udpProxy.send(e)}),s.close.pipe(l(o)).subscribe(async o=>{switch(t.emit("channel",o),o){case"close":case"no_permissions":s.onDestroy();break;case"reconnect":await this.replaceAccessToken(),s.reconnect(this.getProxySocketURL(e))}}),s.linkStart.pipe(l(o)).subscribe(()=>{t.emit("channel","open")}),o.subscribe(()=>{s.onClose()}),s.start(this.getProxySocketURL(e))}onDestroy(){this.apiProxy.onDestroy(),this.chat.onDestroy(),this.udpProxy.onDestroy(),this.destorySub.next(),this.destorySub.complete(),A.code(y["服務結束"])}}function x(t){if(b){if(void 0===s(t,"license",void 0))throw new Error(E["未提供憑證"]);!function(e){const{info:t,error:s,warn:o,debug:i,fatal:r,code:n}=function(e){if(void 0===e||"all"===e)return{code:!0,error:!0,warn:!0,info:!0,debug:!0,fatal:!0};if("none"===e)return{error:!1,code:!1,warn:!1,info:!1,debug:!1,fatal:!1};if("boolean"==typeof e)return{code:e,error:e,warn:e,info:e,debug:e,fatal:e};{const t=new Set(e);return{code:t.has("code"),debug:t.has("debug"),fatal:t.has("fatal"),error:t.has("error"),warn:t.has("warn"),info:t.has("info")}}}(e);return n||(O.code=()=>{}),i||(O.debug=()=>{}),t||(O.info=()=>{}),o||(O.warn=()=>{}),s||(O.error=()=>{}),r||(O.fatal=()=>{}),R}(s(t,"debug",!0))(new v),A.code(y["開始初始化必要模組"]);const o=new D(e({},t));A.code(y["指定模組初始化完成"],"config");const i=new P;A.code(y["指定模組初始化完成"],"storage");const r=new T(o,i);return A.code(y["指定模組初始化完成"],"core"),r.start(),A.code(y["啟動服務"]),()=>{r.onDestroy()}}throw new Error(E["環境錯誤(nodejs)"])}export{x as aiiaCore};
|
|
1
|
+
import{assign as e,forEach as t,get as s,map as o,omit as i,isArray as r}from"lodash-es";import n from"moment";import{Server as c}from"socket.io";import{Subject as a,fromEvent as h,takeUntil as l,take as u}from"rxjs";import d from"axios";import S from"ws";import p from"fs";import b from"os";import f from"path";import{Client as _}from"node-osc";var g;!function(e){e["網絡錯誤"]="ERROR CODE: 500",e["環境錯誤(browser)"]="ERROR CODE: 1001",e["Worklet模組載入失敗"]="ERROR CODE: 1002",e["未獲得媒體裝置權限"]="ERROR CODE: 1003",e["實例已摧毀"]="ERROR CODE: 1004",e["環境錯誤(nodejs)"]="ERROR CODE: 2001",e["未提供憑證"]="ERROR CODE: 2002",e["獲取權杖失敗"]="ERROR CODE: 2003",e["沒有可用專案"]="ERROR CODE: 2004",e["重複初始化"]="ERROR CODE: 2005"}(g||(g={}));const E="access_token",m="undefined"!=typeof process&&null!=process.versions&&null!=process.versions.node;class y{config;get webserver(){return this.config.webserver}get environment(){return this.config.env}get license(){return this.config.license??""}get port(){return function(e,t){const s=t??NaN;if(e){const t=parseInt(e);return isNaN(t)?s:t}return s}(process.env.PORT,this.config.port??3e3)}get ws_url(){return this.config.ws_url??""}get worklet_url(){return this.config.worklet_url??""}get project(){const{project:t}=e({},this.config);return void 0===t?{specific:!1}:"string"==typeof t?{name:t,id:t,specific:!1}:e({},t,{specific:!0})}get endPoint(){return this.environment,{api:"https://aiia-content-management-dev-21193779403.asia-east1.run.app",socket:"wss://graphen-agentic-workflow-dev-21193779403.asia-east1.run.app"}}get debug(){return this.config.debug??!0}get mediaStream(){return this.config.mediaStream}get chunkTimeInSeconds(){return"number"==typeof this.config.chunkTimeInSeconds&&this.config.chunkTimeInSeconds>0?this.config.chunkTimeInSeconds:1}get llmSampleRate(){return"number"==typeof this.config.llmSampleRate&&this.config.llmSampleRate>0?this.config.llmSampleRate:16e3}get faceDetection(){return e({frequency:3,confidence:80},this.config.faceDetection)}get autoClearSubtitle(){const{user:t,aiia:s}=e({},this.config.autoClearSubtitle);let o=3e3,i=3e3;return"number"==typeof t?o=t>=0?t:o:!1===t&&(o=NaN),"number"==typeof s?i=s>=0?s:i:!1===s&&(i=NaN),{userDelayTime:o,aiiaDelayTime:i}}constructor(e){this.config=e}}var O;!function(e){e["通道、雲端服務、麥克風都已就緒"]="CODE: 1000",e["未取得媒體裝置權限,請重新設定或忽略此訊息"]="CODE: 1001",e["請訂閱專案列表"]="CODE: 1002",e["初始化必要依賴"]="CODE: 1010",e["指定依賴已完成"]="CODE: 1011",e["啟動SDK"]="CODE: 1012",e["結束SDK"]="CODE: 1013",e["安全的關閉連線"]="CODE: 2000",e["無副作用的關閉連線"]="CODE: 2001",e["與雲端的服務中斷"]="CODE: 2002",e["沒有授權,請聯繫Graphen"]="CODE: 2003",e["自動重新連線連端服務"]="CODE: 2004",e["未預期的斷線,請聯繫Graphen"]="CODE: 2005",e["開始初始化必要模組"]="CODE: 2010",e["指定模組初始化完成"]="CODE: 2011",e["啟動服務"]="CODE: 2012",e["服務結束"]="CODE: 2013"}(O||(O={}));const D={code:console.log,debug:console.log,info:console.log,warn:console.warn,error:console.error,fatal:console.error},N=[];function R(e){return N.push(e),R}const w=new Proxy(D,{get(e,s,o){if("string"==typeof s)switch(s){case"debug":case"info":case"error":case"warn":case"fatal":case"code":return(...o)=>{const i=n().format("yyyy-MM-DD[T]HH:mm:ss");t(N,e=>{e.next(s,i,...o)}),e[s](`[Aiia::${s}_${i}_]`,...o)};default:return}},set:(e,t,s,o)=>!0});class A{config;server;memberSub;get port(){return this.config.port}get webserver(){return this.config.webserver}get memberChange(){return this.memberSub.asObservable()}constructor(e){this.config=e,this.memberSub=new a}start(){if(this.server=new c(this.webserver,{cors:{origin:"*"}}),this.server.on("connection",e=>{this.memberSub.next({type:"in",socket:e}),w.debug("client on",e.id),e.on("disconnect",t=>{this.memberSub.next({type:"out",socket:e}),w.debug("client off",t)})}),void 0===this.webserver){const e=this.port;this.server.listen(e),w.info("Websocket server run on",e)}else w.info("Websocket server rely your webserver.")}onDestroy(){this.server&&this.server.close()}}class C{instance;controller;constructor(e){this.controller=new AbortController,this.instance=d.create({baseURL:e,signal:this.controller.signal})}async getAccessToken(e,t){const o={license_number:e,token_value:t??""},i=await this.instance.post("/api/v1/license/agent_token",o).then(e=>s(e.data,"token_value","")).catch(()=>null);if(null===i)throw new Error(g["網絡錯誤"]);if(""===i)throw new Error(g["獲取權杖失敗"]);return i}async getProjectList(e){const t=await this.instance.post("/api/v1/license/projects",{license_number:e}).then(e=>o(s(e,"data.projects",[]),({id:e,sub_project_name:t})=>({id:e,name:t}))).catch(e=>null);if(null===t)throw new Error(g["網絡錯誤"]);if(0===t.length)throw new Error(g["沒有可用專案"]);return t}onDestroy(){this.controller.abort()}}var k;!function(e){e[e.MDN_NORMAL_CLOSURE=1e3]="MDN_NORMAL_CLOSURE",e[e.MDN_GOING_AWAY=1001]="MDN_GOING_AWAY",e[e.MDN_PROTOCOL_ERROR=1002]="MDN_PROTOCOL_ERROR",e[e.MDN_UNSUPPORTED_DATA=1003]="MDN_UNSUPPORTED_DATA",e[e.MDN_RESERVED_1004=1004]="MDN_RESERVED_1004",e[e.MDN_NO_STATUS_RECEIVED=1005]="MDN_NO_STATUS_RECEIVED",e[e.MDN_ABNORMAL_CLOSURE=1006]="MDN_ABNORMAL_CLOSURE",e[e.MDN_INVALID_FRAME_PAYLOAD=1007]="MDN_INVALID_FRAME_PAYLOAD",e[e.MDN_POLICY_VIOLATION=1008]="MDN_POLICY_VIOLATION",e[e.MDN_MESSAGE_TOO_BIG=1009]="MDN_MESSAGE_TOO_BIG",e[e.MDN_MANDATORY_EXTENSION=1010]="MDN_MANDATORY_EXTENSION",e[e.MDN_INTERNAL_ERROR=1011]="MDN_INTERNAL_ERROR",e[e.MDN_SERVICE_RESTART=1012]="MDN_SERVICE_RESTART",e[e.MDN_TRY_AGAIN_LATER=1013]="MDN_TRY_AGAIN_LATER",e[e.MDN_BAD_GATEWAY=1014]="MDN_BAD_GATEWAY",e[e.MDN_TLS_HANDSHAKE=1015]="MDN_TLS_HANDSHAKE",e[e.WHEN_BROWSER_CLOSE=4001]="WHEN_BROWSER_CLOSE",e[e.MANUAL_CLOSE=4002]="MANUAL_CLOSE",e[e.WS_5000_NORAML_CLOSURE=5e3]="WS_5000_NORAML_CLOSURE",e[e.WS_5001_UNAUTHORIZED=5001]="WS_5001_UNAUTHORIZED",e[e.WS_5002_LICENSE_EXPIRED=5002]="WS_5002_LICENSE_EXPIRED",e[e.WS_5003_TOKEN_EXPIRED=5003]="WS_5003_TOKEN_EXPIRED"}(k||(k={}));class M{socket;messageSub;linkStartSub;destorySub;closeSub;get message(){return this.messageSub.asObservable()}get close(){return this.closeSub.asObservable()}get linkStart(){return this.linkStartSub.asObservable()}messageQueue=[];isConnected;constructor(){this.isConnected=!1,this.messageSub=new a,this.linkStartSub=new a,this.closeSub=new a,this.destorySub=new a}start(e){this.socket=new S(e,{autoPong:!0}),h(this.socket,"open").pipe(l(this.destorySub)).subscribe(()=>{this.isConnected=!0,w.code(O["啟動服務"],"proxy"),this.flushMessageQueue(),this.linkStartSub.next()}),h(this.socket,"message").pipe(l(this.destorySub)).subscribe(e=>{const{data:t}=e;this.messageSub.next(t)}),h(this.socket,"error").pipe(l(this.destorySub)).subscribe(e=>{const{error:t}=e;w.error("Proxy has some problem",t)}),h(this.socket,"close").pipe(l(this.destorySub)).subscribe(e=>{this.isConnected=!1;const{code:t,reason:s,type:o,wasClean:i}=e;switch(t){case k.WHEN_BROWSER_CLOSE:w.code(O["無副作用的關閉連線"]);break;case k.WS_5000_NORAML_CLOSURE:case k.MDN_NORMAL_CLOSURE:case k.MDN_GOING_AWAY:this.closeSub.next("close"),w.code(O["安全的關閉連線"]);break;case k.MDN_NO_STATUS_RECEIVED:case k.MDN_ABNORMAL_CLOSURE:this.closeSub.next("close"),w.code(O["與雲端的服務中斷"],{code:t,reason:s,type:o,wasClean:i});break;case k.WS_5001_UNAUTHORIZED:case k.WS_5002_LICENSE_EXPIRED:this.closeSub.next("no_permissions"),w.code(O["沒有授權,請聯繫Graphen"]);break;case k.WS_5003_TOKEN_EXPIRED:this.closeSub.next("reconnect"),w.code(O["自動重新連線連端服務"]);break;default:this.closeSub.next("close"),w.code(O["未預期的斷線,請聯繫Graphen"],{code:t,reason:s,type:o,wasClean:i})}})}reconnect(e){this.onDestroy(),this.destorySub=new a,this.start(e)}flushMessageQueue(){for(;this.messageQueue.length>0&&this.isConnected&&void 0!==this.socket;){const e=this.messageQueue.shift();e&&this.socket.send(e)}}send(e){this.isConnected&&void 0!==this.socket?this.socket.send(e):this.messageQueue.push(e)}onClose(){this.socket?.close(k.WHEN_BROWSER_CLOSE)}onDestroy(){this.destorySub.next(),this.destorySub.complete()}}const L=new class{AppName;platform;isMAC;isWIN;isLINUX;saveFolder;homedir;appFolder;EOL;constructor(e){switch(this.AppName=e,this.platform=process.platform,this.homedir=b.homedir(),this.EOL=b.EOL,this.platform){case"darwin":this.isMAC=!0,this.isWIN=!1,this.isLINUX=!1,this.saveFolder=f.join(this.homedir,"Library","Application Support");break;case"win32":this.isMAC=!1,this.isWIN=!0,this.isLINUX=!1,this.saveFolder=process.env.APPDATA||f.join(this.homedir,"AppData","Roaming");break;case"linux":this.isMAC=!1,this.isWIN=!1,this.isLINUX=!0,this.saveFolder=process.env.XDG_CONFIG_HOME||f.join(this.homedir,".config");break;default:console.log(`當前系統是未知的平台: ${this.platform}`),this.isMAC=!1,this.isWIN=!1,this.isLINUX=!1,this.saveFolder=this.homedir}this.appFolder=f.join(this.saveFolder,this.AppName),this.folderCheck(this.appFolder)}addPath(...e){return f.join(...e)}folderCheck(e){p.mkdirSync(e,{recursive:!0})}fileCheck(e){p.existsSync(e)||p.writeFileSync(e,"",{encoding:"utf8"})}}("aiia-sdk");class P{type="node";filePath;fileName;constructor(){this.fileName="storage.json",this.filePath=L.addPath(L.appFolder,this.fileName),L.fileCheck(this.filePath)}getFileDataSync(){const e=p.readFileSync(this.filePath,"utf8");try{return JSON.parse(e)}catch(e){return{}}}setFileDataSync(e){try{const t=JSON.stringify(e);p.writeFileSync(this.filePath,t,"utf8")}catch(e){}}async getFileData(){return new Promise(e=>{p.readFile(this.filePath,"utf8",(t,s)=>{if(t)e({});else try{e(JSON.parse(s))}catch(t){e({})}})})}async setFileData(e){return new Promise(t=>{try{const s=JSON.stringify(e);p.writeFile(this.filePath,s,"utf8",e=>{t()})}catch(e){}})}async getItem(e){return(await this.getFileData())[e]??null}async setItem(e,t){const s=await this.getFileData();s[e]=t,await this.setFileData(s)}async removeItem(e){const t=await this.getFileData(),s=i(t,e);await this.setFileData(s)}async clear(){await this.setFileData({})}getItemSync(e){return this.getFileDataSync()[e]??null}setItemSync(e,t){const s=this.getFileDataSync();s[e]=t,this.setFileDataSync(s)}removeItemSync(e){const t=i(this.getFileDataSync(),e);this.setFileDataSync(t)}clearSync(){this.setFileDataSync({})}}class I{config;constructor(e){this.config=e,w.info("UEProxy, config=",e)}start(){w.info("UEProxy, start proxy")}send(e){let t=this.cloudToObj(e);if(t&&t.signal&&"osc"===t.signal){w.info("UEProxy, payload=",t);try{let e=t.content;const s=new _(e.host,e.port);w.debug("UEProxy, client open to port "+e.port),s.send(e.message,e=>{e&&w.error(e),s.close(),w.debug("UEProxy, client closed")})}catch(e){w.error(e)}}}cloudToObj(e){try{return JSON.parse(e)}catch(e){w.error("message transfer fail",e)}}onDestroy(){w.info("UEProxy, destroy proxy")}}class T{socket;messageSub;linkStartSub;destorySub;closeSub;get message(){return this.messageSub.asObservable()}get close(){return this.closeSub.asObservable()}get linkStart(){return this.linkStartSub.asObservable()}messageQueue=[];isConnected;constructor(){this.isConnected=!1,this.messageSub=new a,this.linkStartSub=new a,this.closeSub=new a,this.destorySub=new a}url="ws://localhost:8080";start(){this.socket=new S(this.url,{autoPong:!0}),h(this.socket,"open").pipe(l(this.destorySub)).subscribe(()=>{this.isConnected=!0,w.code(O["啟動服務"],"proxy"),this.flushMessageQueue(),this.linkStartSub.next()}),h(this.socket,"message").pipe(l(this.destorySub)).subscribe(e=>{const{data:t}=e;this.messageSub.next(t)}),h(this.socket,"error").pipe(l(this.destorySub)).subscribe(e=>{const{error:t}=e;w.error("Proxy has some problem",t)}),h(this.socket,"close").pipe(l(this.destorySub)).subscribe(e=>{this.isConnected=!1;const{code:t,reason:s}=e;switch(t){case k.MANUAL_CLOSE:w.code(O["無副作用的關閉連線"]);break;case k.WS_5000_NORAML_CLOSURE:case k.MDN_NORMAL_CLOSURE:case k.MDN_GOING_AWAY:this.closeSub.next("close"),w.code(O["安全的關閉連線"]);break;case k.MDN_NO_STATUS_RECEIVED:case k.MDN_ABNORMAL_CLOSURE:this.closeSub.next("close"),w.code(O["與雲端的服務中斷"],{code:t,reason:s});break;case k.WS_5001_UNAUTHORIZED:case k.WS_5002_LICENSE_EXPIRED:this.closeSub.next("no_permissions"),w.code(O["沒有授權,請聯繫Graphen"]);break;case k.WS_5003_TOKEN_EXPIRED:this.closeSub.next("reconnect"),w.code(O["自動重新連線連端服務"]);break;default:this.closeSub.next("close"),w.code(O["未預期的斷線,請聯繫Graphen"],{code:t,reason:s})}})}reconnect(){this.onDestroy(),this.destorySub=new a,this.start()}flushMessageQueue(){for(;this.messageQueue.length>0&&this.isConnected&&void 0!==this.socket;){const e=this.messageQueue.shift();e&&this.socket.send(e)}}send(e){let t=this.cloudToObj(e);if(t&&t.signal&&"audio"===t.signal&&(w.info("AudioSocketProxy, payload=",t),"pcm"===t.command&&this.isConnected&&void 0!==this.socket))try{const e=t.content,s=Buffer.allocUnsafe(2*e.length);for(let t=0;t<e.length;t++)s.writeInt16LE(e[t],2*t);this.socket.send(s)}catch(e){w.error(e)}}cloudToObj(e){try{return JSON.parse(e)}catch(e){w.error("message transfer fail",e)}}onClose(){this.socket?.close(k.MANUAL_CLOSE)}onDestroy(){this.destorySub.next(),this.destorySub.complete()}}class v{filePath;fileName;constructor(){this.fileName="log.txt",this.filePath=L.addPath(L.appFolder,this.fileName)}next(e,t,...s){try{const o=s.reduce((e,t)=>{switch(typeof t){case"string":case"number":case"bigint":return`${e} ${t.toString()}`;case"boolean":return`${e} ${t?"true":"false"}`;case"object":try{return`${e} ${JSON.stringify(t)}`}catch(t){return e}case"symbol":case"undefined":case"function":return`${e} [${typeof t}]`}},"");p.appendFileSync(this.filePath,`LOG(${e})__${t}__::${o}${L.EOL}`,{encoding:"utf8"})}catch(e){}}}class x{config;stroage;apiProxy;chat;udpProxy;audioProxy;isStart;channelMap;destorySub;access_token;get accessToken(){return this.access_token}constructor(e,t){this.config=e,this.stroage=t,this.isStart=!1,this.destorySub=new a,this.channelMap=new Map,this.access_token=t.getItemSync(E),this.apiProxy=new C(this.config.endPoint.api),w.code(O["指定模組初始化完成"],"api"),this.chat=new A(this.config),w.code(O["指定模組初始化完成"],"chat"),this.udpProxy=new I(this.config),w.code(O["指定模組初始化完成"],"udp"),this.audioProxy=new T,w.code(O["指定模組初始化完成"],"audio"),this.openChannel=this.openChannel.bind(this)}async start(){if(this.isStart)throw new Error(g["重複初始化"]);this.isStart=!0,await this.replaceAccessToken(),this.chat.memberChange.pipe(l(this.destorySub)).subscribe(({type:e,socket:t})=>{switch(e){case"in":{const e=new M,i=new a,n=(o=t.handshake.query.uuid,r(o)?s(o,"[0]",void 0):o??void 0);void 0===n?(this.getProjectList().then(e=>{t.emit("list",e)}),h(t,"uuid").pipe(u(1),l(i)).subscribe(s=>{this.openChannel(s,t,e,i)})):this.openChannel(n,t,e,i),this.channelMap.set(t.id,()=>{i.next(),i.complete()});break}case"out":this.channelMap.has(t.id)&&(this.channelMap.get(t.id)(),this.channelMap.delete(t.id))}var o}),this.chat.start(),w.code(O["啟動服務"],"chat"),this.udpProxy.start(),w.code(O["啟動服務"],"udp")}async replaceAccessToken(){const e=await this.apiProxy.getAccessToken(this.config.license,this.access_token);return this.access_token=e,this.stroage.setItemSync(E,e),e}async getProjectList(){return await this.apiProxy.getProjectList(this.config.license)}getProxySocketURL(e){return`${this.config.endPoint.socket}/v1/chat/project/${this.accessToken}/${e}`}openChannel(e,t,s,o){h(t,"cloud").pipe(l(o)).subscribe(e=>{s.send(e)}),s.message.pipe(l(o)).subscribe(e=>{t.emit("cloud",e),this.udpProxy.send(e),this.audioProxy.send(e)}),s.close.pipe(l(o)).subscribe(async o=>{switch(t.emit("channel",o),o){case"close":case"no_permissions":s.onDestroy();break;case"reconnect":await this.replaceAccessToken(),s.reconnect(this.getProxySocketURL(e))}}),s.linkStart.pipe(l(o)).subscribe(()=>{t.emit("channel","open")}),o.subscribe(()=>{s.onClose(),this.audioProxy.onClose()}),s.start(this.getProxySocketURL(e)),this.audioProxy.start()}onDestroy(){this.apiProxy.onDestroy(),this.chat.onDestroy(),this.udpProxy.onDestroy(),this.destorySub.next(),this.destorySub.complete(),w.code(O["服務結束"])}}function U(t){if(m){if(void 0===s(t,"license",void 0))throw new Error(g["未提供憑證"]);!function(e){const{info:t,error:s,warn:o,debug:i,fatal:r,code:n}=function(e){if(void 0===e||"all"===e)return{code:!0,error:!0,warn:!0,info:!0,debug:!0,fatal:!0};if("none"===e)return{error:!1,code:!1,warn:!1,info:!1,debug:!1,fatal:!1};if("boolean"==typeof e)return{code:e,error:e,warn:e,info:e,debug:e,fatal:e};{const t=new Set(e);return{code:t.has("code"),debug:t.has("debug"),fatal:t.has("fatal"),error:t.has("error"),warn:t.has("warn"),info:t.has("info")}}}(e);return n||(D.code=()=>{}),i||(D.debug=()=>{}),t||(D.info=()=>{}),o||(D.warn=()=>{}),s||(D.error=()=>{}),r||(D.fatal=()=>{}),R}(s(t,"debug",!0))(new v),w.code(O["開始初始化必要模組"]);const o=new y(e({},t));w.code(O["指定模組初始化完成"],"config");const i=new P;w.code(O["指定模組初始化完成"],"storage");const r=new x(o,i);return w.code(O["指定模組初始化完成"],"core"),r.start(),w.code(O["啟動服務"]),()=>{r.onDestroy()}}throw new Error(g["環境錯誤(nodejs)"])}export{U as aiiaCore};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@graphen.ai/aiia-sdk",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.9",
|
|
4
4
|
"keywords": [],
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Jay Huang",
|
|
@@ -55,7 +55,7 @@
|
|
|
55
55
|
"@rollup/plugin-typescript": "^11.1.6",
|
|
56
56
|
"@types/audioworklet": "^0.0.83",
|
|
57
57
|
"@types/jest": "^30.0.0",
|
|
58
|
-
"@types/lodash": "^4.17.
|
|
58
|
+
"@types/lodash-es": "^4.17.12",
|
|
59
59
|
"@types/node": "^24.2.1",
|
|
60
60
|
"@types/node-osc": "^9.1.0",
|
|
61
61
|
"@types/ws": "^8.18.1",
|
|
@@ -70,7 +70,7 @@
|
|
|
70
70
|
"dependencies": {
|
|
71
71
|
"@mediapipe/tasks-vision": "^0.10.22-rc.20250304",
|
|
72
72
|
"axios": "^1.11.0",
|
|
73
|
-
"lodash": "^4.17.21",
|
|
73
|
+
"lodash-es": "^4.17.21",
|
|
74
74
|
"moment": "^2.30.1",
|
|
75
75
|
"node-osc": "^11.0.0",
|
|
76
76
|
"rxjs": "^7.8.2",
|