@graphen.ai/aiia-sdk 1.0.11 → 1.0.13

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/node.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";var e,t=require("lodash-es"),s=require("moment"),o=require("socket.io"),i=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:.3}get llmSampleRate(){return"number"==typeof this.config.llmSampleRate&&this.config.llmSampleRate>0?this.config.llmSampleRate:16e3}get faceDetection(){return t.assign({perSecond:3,confidence:80},this.config.faceDetection)}get autoClearSubtitle(){const{user:e,aiia:s}=t.assign({},this.config.autoClearSubtitle);let o=5e3,i=5e3;return"number"==typeof e?o=e>=0?e:o:!1===e&&(o=NaN),"number"==typeof s?i=s>=0?s:i:!1===s&&(i=NaN),{userDelayTime:o,aiiaDelayTime:i}}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 g={code:console.log,debug:console.log,info:console.log,warn:console.warn,error:console.error,fatal:console.error},p=[];function E(e){return p.push(e),E}const _=new Proxy(g,{get(e,o,i){if("string"==typeof o)switch(o){case"debug":case"info":case"error":case"warn":case"fatal":case"code":return(...i)=>{const r=s().format("yyyy-MM-DD[T]HH:mm:ss");t.forEach(p,e=>{e.next(o,r,...i)}),e[o](`[Aiia::${o}_${r}_]`,...i)};default:return}},set:(e,t,s,o)=>!0});class f{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 i.Subject}start(){if(this.server=new o.Server(this.webserver,{cors:{origin:"*"}}),this.server.on("connection",e=>{this.memberSub.next({type:"in",socket:e}),_.debug("client on",e.id),e.on("disconnect",t=>{this.memberSub.next({type:"out",socket:e}),_.debug("client off",t)})}),void 0===this.webserver){const e=this.port;this.server.listen(e),_.info("Websocket server run on",e)}else _.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,o){const i={license_number:s,token_value:o??""},r=await this.instance.post("/api/v1/license/agent_token",i).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 o=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===o)throw new Error(e["網絡錯誤"]);if(0===o.length)throw new Error(e["沒有可用專案"]);return o}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.WHEN_BROWSER_CLOSE=4001]="WHEN_BROWSER_CLOSE",e[e.MANUAL_CLOSE=4002]="MANUAL_CLOSE",e[e.WS_5000_NORAML_CLOSURE=4100]="WS_5000_NORAML_CLOSURE",e[e.WS_5001_UNAUTHORIZED=4101]="WS_5001_UNAUTHORIZED",e[e.WS_5002_LICENSE_EXPIRED=4102]="WS_5002_LICENSE_EXPIRED",e[e.WS_5003_TOKEN_EXPIRED=4103]="WS_5003_TOKEN_EXPIRED"}(m||(m={}));class O{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 i.Subject,this.linkStartSub=new i.Subject,this.closeSub=new i.Subject,this.destorySub=new i.Subject}start(e){this.socket=new n(e,{autoPong:!0}),i.fromEvent(this.socket,"open").pipe(i.takeUntil(this.destorySub)).subscribe(()=>{this.isConnected=!0,_.code(b["啟動服務"],"proxy"),this.flushMessageQueue(),this.linkStartSub.next()}),i.fromEvent(this.socket,"message").pipe(i.takeUntil(this.destorySub)).subscribe(e=>{const{data:t}=e;this.messageSub.next(t)}),i.fromEvent(this.socket,"error").pipe(i.takeUntil(this.destorySub)).subscribe(e=>{const{error:t}=e;_.error("Proxy has some problem",t)}),i.fromEvent(this.socket,"close").pipe(i.takeUntil(this.destorySub)).subscribe(e=>{this.isConnected=!1;const{code:t,reason:s,type:o,wasClean:i}=e;switch(t){case m.WHEN_BROWSER_CLOSE:_.code(b["無副作用的關閉連線"]);break;case m.MDN_NORMAL_CLOSURE:case m.MDN_GOING_AWAY:this.closeSub.next("close"),_.code(b["安全的關閉連線"]);break;case m.MDN_NO_STATUS_RECEIVED:case m.MDN_ABNORMAL_CLOSURE:this.closeSub.next("close"),_.code(b["與雲端的服務中斷"],{code:t,reason:s,type:o,wasClean:i});break;case m.WS_5001_UNAUTHORIZED:case m.WS_5002_LICENSE_EXPIRED:this.closeSub.next("no_permissions"),_.code(b["沒有授權,請聯繫Graphen"]);break;case m.WS_5000_NORAML_CLOSURE:case m.WS_5003_TOKEN_EXPIRED:this.closeSub.next("reconnect"),_.code(b["自動重新連線連端服務"]);break;default:this.closeSub.next("close"),_.code(b["未預期的斷線,請聯繫Graphen"],{code:t,reason:s,type:o,wasClean:i})}})}reconnect(e){this.onDestroy(),this.destorySub=new i.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,e=>{void 0!==e&&_.error("send message fail",e?.message,e)})}}send(e){this.isConnected&&void 0!==this.socket?this.socket.send(e,e=>{void 0!==e&&_.error("send message fail",e?.message,e)}):this.messageQueue.push(e)}onClose(){this.socket?.close(m.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(),o=t.omit(s,e);await this.setFileData(o)}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,_.info("UEProxy, config=",e)}start(){_.info("UEProxy, start proxy")}send(e){let t=this.cloudToObj(e);if(t&&t.signal&&"osc"===t.signal){_.info("UEProxy, payload=",t);try{let e=t.content;const s=new l.Client(e.host,e.port);_.debug("UEProxy, client open to port "+e.port),s.send(e.message,e=>{e&&_.error(e),s.close(),_.debug("UEProxy, client closed")})}catch(e){_.error(e)}}}cloudToObj(e){try{return JSON.parse(e)}catch(e){_.error("message transfer fail",e)}}onDestroy(){_.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 i.Subject,this.linkStartSub=new i.Subject,this.closeSub=new i.Subject,this.destorySub=new i.Subject}url="ws://localhost:8080";start(){this.socket=new n(this.url,{autoPong:!0}),i.fromEvent(this.socket,"open").pipe(i.takeUntil(this.destorySub)).subscribe(()=>{this.isConnected=!0,_.code(b["啟動服務"],"proxy"),this.flushMessageQueue(),this.linkStartSub.next()}),i.fromEvent(this.socket,"message").pipe(i.takeUntil(this.destorySub)).subscribe(e=>{const{data:t}=e;this.messageSub.next(t)}),i.fromEvent(this.socket,"error").pipe(i.takeUntil(this.destorySub)).subscribe(e=>{const{error:t}=e;_.error("Proxy has some problem",t)}),i.fromEvent(this.socket,"close").pipe(i.takeUntil(this.destorySub)).subscribe(e=>{this.isConnected=!1;const{code:t,reason:s}=e;switch(t){case m.MANUAL_CLOSE:_.code(b["無副作用的關閉連線"]);break;case m.WS_5000_NORAML_CLOSURE:case m.MDN_NORMAL_CLOSURE:case m.MDN_GOING_AWAY:this.closeSub.next("close"),_.code(b["安全的關閉連線"]);break;case m.MDN_NO_STATUS_RECEIVED:case m.MDN_ABNORMAL_CLOSURE:this.closeSub.next("close"),_.code(b["與雲端的服務中斷"],{code:t,reason:s});break;case m.WS_5001_UNAUTHORIZED:case m.WS_5002_LICENSE_EXPIRED:this.closeSub.next("no_permissions"),_.code(b["沒有授權,請聯繫Graphen"]);break;case m.WS_5003_TOKEN_EXPIRED:this.closeSub.next("reconnect"),_.code(b["自動重新連線連端服務"]);break;default:this.closeSub.next("close"),_.code(b["未預期的斷線,請聯繫Graphen"],{code:t,reason:s})}})}reconnect(){this.onDestroy(),this.destorySub=new i.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&&(_.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){_.error(e)}}cloudToObj(e){try{return JSON.parse(e)}catch(e){_.error("message transfer fail",e)}}onClose(){this.socket?.close(m.MANUAL_CLOSE)}onDestroy(){this.destorySub.next(),this.destorySub.complete()}}class w{next(e,t,...o){const i=`log_${s().format("yyyy-MM-DD")}.txt`,r=D.addPath(D.appFolder,i);try{const s=o.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(r,`LOG(${e})__${t}__::${s}${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 i.Subject,this.channelMap=new Map,this.access_token=t.getItemSync(u),this.apiProxy=new y(this.config.endPoint.api),_.code(b["指定模組初始化完成"],"api"),this.chat=new f(this.config),_.code(b["指定模組初始化完成"],"chat"),this.udpProxy=new R(this.config),_.code(b["指定模組初始化完成"],"udp"),this.audioProxy=new k,_.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(i.takeUntil(this.destorySub)).subscribe(({type:e,socket:s})=>{switch(e){case"in":{const e=new O,r=new i.Subject,n=(o=s.handshake.query.uuid,t.isArray(o)?t.get(o,"[0]",void 0):o??void 0);void 0===n?(this.getProjectList().then(e=>{s.emit("list",e)}),i.fromEvent(s,"uuid").pipe(i.take(1),i.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 o}),this.chat.start(),_.code(b["啟動服務"],"chat"),this.udpProxy.start(),_.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,o){i.fromEvent(t,"cloud").pipe(i.takeUntil(o)).subscribe(e=>{s.send(e)}),s.message.pipe(i.takeUntil(o)).subscribe(e=>{t.emit("cloud",e),this.udpProxy.send(e),this.audioProxy.send(e)}),s.close.pipe(i.takeUntil(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(i.takeUntil(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(),_.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: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||(g.code=()=>{}),i||(g.debug=()=>{}),t||(g.info=()=>{}),o||(g.warn=()=>{}),s||(g.error=()=>{}),r||(g.fatal=()=>{}),E}(t.get(s,"debug",!0))(new w),_.code(b["開始初始化必要模組"]);const o=new S(t.assign({},s));_.code(b["指定模組初始化完成"],"config");const i=new N;_.code(b["指定模組初始化完成"],"storage");const r=new A(o,i);return _.code(b["指定模組初始化完成"],"core"),r.start(),_.code(b["啟動服務"]),()=>{r.onDestroy()}}throw new Error(e["環境錯誤(nodejs)"])};
1
+ "use strict";var e,t=require("lodash-es"),s=require("moment"),o=require("socket.io"),i=require("rxjs"),r=require("axios"),n=require("ws"),c=require("fs"),a=require("os"),h=require("path"),u=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 l="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:.3}get llmSampleRate(){return"number"==typeof this.config.llmSampleRate&&this.config.llmSampleRate>0?this.config.llmSampleRate:16e3}get faceDetection(){return t.assign({perSecond:3,confidence:80},this.config.faceDetection)}get autoClearSubtitle(){const{user:e,aiia:s}=t.assign({},this.config.autoClearSubtitle);let o=5e3,i=5e3;return"number"==typeof e?o=e>=0?e:o:!1===e&&(o=NaN),"number"==typeof s?i=s>=0?s:i:!1===s&&(i=NaN),{userDelayTime:o,aiiaDelayTime:i}}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 g={code:console.log,debug:console.log,info:console.log,warn:console.warn,error:console.error,fatal:console.error},p=[];function _(e){return p.push(e),_}const E=new Proxy(g,{get(e,o,i){if("string"==typeof o)switch(o){case"debug":case"info":case"error":case"warn":case"fatal":case"code":return(...i)=>{const r=s().format("yyyy-MM-DD[T]HH:mm:ss");t.forEach(p,e=>{e.next(o,r,...i)}),e[o](`[Aiia::${o}_${r}_]`,...i)};default:return}},set:(e,t,s,o)=>!0});class f{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 i.Subject}start(){if(this.server=new o.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,o){const i={license_number:s,token_value:o??""},r=await this.instance.post("/api/v1/license/agent_token",i).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 o=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===o)throw new Error(e["網絡錯誤"]);if(0===o.length)throw new Error(e["沒有可用專案"]);return o}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.WHEN_BROWSER_CLOSE=4001]="WHEN_BROWSER_CLOSE",e[e.MANUAL_CLOSE=4002]="MANUAL_CLOSE",e[e.WS_5000_NORAML_CLOSURE=4100]="WS_5000_NORAML_CLOSURE",e[e.WS_5001_UNAUTHORIZED=4101]="WS_5001_UNAUTHORIZED",e[e.WS_5002_LICENSE_EXPIRED=4102]="WS_5002_LICENSE_EXPIRED",e[e.WS_5003_TOKEN_EXPIRED=4103]="WS_5003_TOKEN_EXPIRED"}(m||(m={}));class O{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 i.Subject,this.linkStartSub=new i.Subject,this.closeSub=new i.Subject,this.destorySub=new i.Subject}start(e){this.socket=new n(e,{autoPong:!0}),i.fromEvent(this.socket,"open").pipe(i.takeUntil(this.destorySub)).subscribe(()=>{this.isConnected=!0,E.code(b["啟動服務"],"proxy"),this.flushMessageQueue(),this.linkStartSub.next()}),i.fromEvent(this.socket,"message").pipe(i.takeUntil(this.destorySub)).subscribe(e=>{const{data:t}=e;this.messageSub.next(t)}),i.fromEvent(this.socket,"error").pipe(i.takeUntil(this.destorySub)).subscribe(e=>{const{error:t}=e;E.error("Proxy has some problem",t)}),i.fromEvent(this.socket,"close").pipe(i.takeUntil(this.destorySub)).subscribe(e=>{this.isConnected=!1;const{code:t,reason:s,type:o,wasClean:i}=e;switch(t){case m.WHEN_BROWSER_CLOSE:E.code(b["無副作用的關閉連線"]);break;case m.MDN_NORMAL_CLOSURE:case m.MDN_GOING_AWAY:this.closeSub.next("close"),E.code(b["安全的關閉連線"]);break;case m.MDN_NO_STATUS_RECEIVED:case m.MDN_ABNORMAL_CLOSURE:this.closeSub.next("close"),E.code(b["與雲端的服務中斷"],{code:t,reason:s,type:o,wasClean:i});break;case m.WS_5001_UNAUTHORIZED:case m.WS_5002_LICENSE_EXPIRED:this.closeSub.next("no_permissions"),E.code(b["沒有授權,請聯繫Graphen"]);break;case m.WS_5000_NORAML_CLOSURE:case m.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:o,wasClean:i})}})}reconnect(e){this.onDestroy(),this.destorySub=new i.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,e=>{e&&E.error("send message fail",e?.message,e)})}}send(e){this.isConnected&&void 0!==this.socket?this.socket.send(e,e=>{e&&E.error("send message fail",e?.message,e)}):this.messageQueue.push(e)}onClose(){this.socket?.close(m.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(),o=t.omit(s,e);await this.setFileData(o)}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 u.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 i.Subject,this.linkStartSub=new i.Subject,this.closeSub=new i.Subject,this.destorySub=new i.Subject}url="ws://localhost:8080";start(){this.socket=new n(this.url,{autoPong:!0}),i.fromEvent(this.socket,"open").pipe(i.takeUntil(this.destorySub)).subscribe(()=>{this.isConnected=!0,E.code(b["啟動服務"],"proxy"),this.flushMessageQueue(),this.linkStartSub.next()}),i.fromEvent(this.socket,"message").pipe(i.takeUntil(this.destorySub)).subscribe(e=>{const{data:t}=e;this.messageSub.next(t)}),i.fromEvent(this.socket,"error").pipe(i.takeUntil(this.destorySub)).subscribe(e=>{const{error:t}=e;E.error("Proxy has some problem",t)}),i.fromEvent(this.socket,"close").pipe(i.takeUntil(this.destorySub)).subscribe(e=>{this.isConnected=!1;const{code:t,reason:s}=e;switch(t){case m.MANUAL_CLOSE:E.code(b["無副作用的關閉連線"]);break;case m.WS_5000_NORAML_CLOSURE:case m.MDN_NORMAL_CLOSURE:case m.MDN_GOING_AWAY:this.closeSub.next("close"),E.code(b["安全的關閉連線"]);break;case m.MDN_NO_STATUS_RECEIVED:case m.MDN_ABNORMAL_CLOSURE:this.closeSub.next("close"),E.code(b["與雲端的服務中斷"],{code:t,reason:s});break;case m.WS_5001_UNAUTHORIZED:case m.WS_5002_LICENSE_EXPIRED:this.closeSub.next("no_permissions"),E.code(b["沒有授權,請聯繫Graphen"]);break;case m.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 i.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&&"pcm"===t.command&&(E.info("AudioSocketProxy, payload=",t),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(m.MANUAL_CLOSE)}onDestroy(){this.destorySub.next(),this.destorySub.complete()}}class C{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 i.Subject,this.linkStartSub=new i.Subject,this.closeSub=new i.Subject,this.destorySub=new i.Subject}url="ws://localhost:8081";start(){this.socket=new n(this.url,{autoPong:!0}),i.fromEvent(this.socket,"open").pipe(i.takeUntil(this.destorySub)).subscribe(()=>{this.isConnected=!0,E.code(b["啟動服務"],"proxy"),this.flushMessageQueue(),this.linkStartSub.next()}),i.fromEvent(this.socket,"message").pipe(i.takeUntil(this.destorySub)).subscribe(e=>{const{data:t}=e;this.messageSub.next(t)}),i.fromEvent(this.socket,"error").pipe(i.takeUntil(this.destorySub)).subscribe(e=>{const{error:t}=e;E.error("Proxy has some problem",t)}),i.fromEvent(this.socket,"close").pipe(i.takeUntil(this.destorySub)).subscribe(e=>{this.isConnected=!1;const{code:t,reason:s}=e;switch(t){case m.MANUAL_CLOSE:E.code(b["無副作用的關閉連線"]);break;case m.WS_5000_NORAML_CLOSURE:case m.MDN_NORMAL_CLOSURE:case m.MDN_GOING_AWAY:this.closeSub.next("close"),E.code(b["安全的關閉連線"]);break;case m.MDN_NO_STATUS_RECEIVED:case m.MDN_ABNORMAL_CLOSURE:this.closeSub.next("close"),E.code(b["與雲端的服務中斷"],{code:t,reason:s});break;case m.WS_5001_UNAUTHORIZED:case m.WS_5002_LICENSE_EXPIRED:this.closeSub.next("no_permissions"),E.code(b["沒有授權,請聯繫Graphen"]);break;case m.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 i.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&&"interrupted"===t.command&&(E.info("AudioInterruptSocketProxy, payload=",t),this.isConnected&&void 0!==this.socket))try{this.socket.send(t.command)}catch(e){E.error(e)}}cloudToObj(e){try{return JSON.parse(e)}catch(e){E.error("message transfer fail",e)}}onClose(){this.socket?.close(m.MANUAL_CLOSE)}onDestroy(){this.destorySub.next(),this.destorySub.complete()}}class A{next(e,t,...o){const i=`log_${s().format("yyyy-MM-DD")}.txt`,r=D.addPath(D.appFolder,i);try{const s=o.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(r,`LOG(${e})__${t}__::${s}${D.EOL}`,{encoding:"utf8"})}catch(e){}}}class w{config;stroage;apiProxy;chat;udpProxy;audioProxy;audioIntrProxy;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 i.Subject,this.channelMap=new Map,this.access_token=t.getItemSync(l),this.apiProxy=new y(this.config.endPoint.api),E.code(b["指定模組初始化完成"],"api"),this.chat=new f(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.audioIntrProxy=new C,E.code(b["指定模組初始化完成"],"audio intr"),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(i.takeUntil(this.destorySub)).subscribe(({type:e,socket:s})=>{switch(e){case"in":{const e=new O,r=new i.Subject,n=(o=s.handshake.query.uuid,t.isArray(o)?t.get(o,"[0]",void 0):o??void 0);void 0===n?(this.getProjectList().then(e=>{s.emit("list",e)}),i.fromEvent(s,"uuid").pipe(i.take(1),i.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 o}),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(l,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){i.fromEvent(t,"cloud").pipe(i.takeUntil(o)).subscribe(e=>{s.send(e)}),s.message.pipe(i.takeUntil(o)).subscribe(e=>{t.emit("cloud",e),this.udpProxy.send(e),this.audioProxy.send(e),this.audioIntrProxy.send(e)}),s.close.pipe(i.takeUntil(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(i.takeUntil(o)).subscribe(()=>{t.emit("channel","open")}),o.subscribe(()=>{s.onClose(),this.audioProxy.onClose(),this.audioIntrProxy.onClose()}),s.start(this.getProxySocketURL(e)),this.audioProxy.start(),this.audioIntrProxy.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: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||(g.code=()=>{}),i||(g.debug=()=>{}),t||(g.info=()=>{}),o||(g.warn=()=>{}),s||(g.error=()=>{}),r||(g.fatal=()=>{}),_}(t.get(s,"debug",!0))(new A),E.code(b["開始初始化必要模組"]);const o=new S(t.assign({},s));E.code(b["指定模組初始化完成"],"config");const i=new N;E.code(b["指定模組初始化完成"],"storage");const r=new w(o,i);return E.code(b["指定模組初始化完成"],"core"),r.start(),E.code(b["啟動服務"]),()=>{r.onDestroy()}}throw new Error(e["環境錯誤(nodejs)"])};
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-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 _ from"path";import{Client as f}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:.3}get llmSampleRate(){return"number"==typeof this.config.llmSampleRate&&this.config.llmSampleRate>0?this.config.llmSampleRate:16e3}get faceDetection(){return e({perSecond:3,confidence:80},this.config.faceDetection)}get autoClearSubtitle(){const{user:t,aiia:s}=e({},this.config.autoClearSubtitle);let o=5e3,i=5e3;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=4100]="WS_5000_NORAML_CLOSURE",e[e.WS_5001_UNAUTHORIZED=4101]="WS_5001_UNAUTHORIZED",e[e.WS_5002_LICENSE_EXPIRED=4102]="WS_5002_LICENSE_EXPIRED",e[e.WS_5003_TOKEN_EXPIRED=4103]="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.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_5000_NORAML_CLOSURE: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,e=>{void 0!==e&&w.error("send message fail",e?.message,e)})}}send(e){this.isConnected&&void 0!==this.socket?this.socket.send(e,e=>{void 0!==e&&w.error("send message fail",e?.message,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=_.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){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 f(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 v{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 T{next(e,t,...s){const o=`log_${n().format("yyyy-MM-DD")}.txt`,i=L.addPath(L.appFolder,o);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(i,`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 v,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 T),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};
1
+ import{assign as e,forEach as t,get as s,map as o,omit as r,isArray as i}from"lodash-es";import n from"moment";import{Server as c}from"socket.io";import{Subject as a,fromEvent as h,takeUntil as u,take as l}from"rxjs";import d from"axios";import S from"ws";import b from"fs";import p from"os";import _ from"path";import{Client as g}from"node-osc";var f;!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"}(f||(f={}));const E="access_token",y="undefined"!=typeof process&&null!=process.versions&&null!=process.versions.node;class m{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:.3}get llmSampleRate(){return"number"==typeof this.config.llmSampleRate&&this.config.llmSampleRate>0?this.config.llmSampleRate:16e3}get faceDetection(){return e({perSecond:3,confidence:80},this.config.faceDetection)}get autoClearSubtitle(){const{user:t,aiia:s}=e({},this.config.autoClearSubtitle);let o=5e3,r=5e3;return"number"==typeof t?o=t>=0?t:o:!1===t&&(o=NaN),"number"==typeof s?r=s>=0?s:r:!1===s&&(r=NaN),{userDelayTime:o,aiiaDelayTime:r}}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 C=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 r=n().format("yyyy-MM-DD[T]HH:mm:ss");t(N,e=>{e.next(s,r,...o)}),e[s](`[Aiia::${s}_${r}_]`,...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}),C.debug("client on",e.id),e.on("disconnect",t=>{this.memberSub.next({type:"out",socket:e}),C.debug("client off",t)})}),void 0===this.webserver){const e=this.port;this.server.listen(e),C.info("Websocket server run on",e)}else C.info("Websocket server rely your webserver.")}onDestroy(){this.server&&this.server.close()}}class k{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??""},r=await this.instance.post("/api/v1/license/agent_token",o).then(e=>s(e.data,"token_value","")).catch(()=>null);if(null===r)throw new Error(f["網絡錯誤"]);if(""===r)throw new Error(f["獲取權杖失敗"]);return r}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(f["網絡錯誤"]);if(0===t.length)throw new Error(f["沒有可用專案"]);return t}onDestroy(){this.controller.abort()}}var w;!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=4100]="WS_5000_NORAML_CLOSURE",e[e.WS_5001_UNAUTHORIZED=4101]="WS_5001_UNAUTHORIZED",e[e.WS_5002_LICENSE_EXPIRED=4102]="WS_5002_LICENSE_EXPIRED",e[e.WS_5003_TOKEN_EXPIRED=4103]="WS_5003_TOKEN_EXPIRED"}(w||(w={}));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(u(this.destorySub)).subscribe(()=>{this.isConnected=!0,C.code(O["啟動服務"],"proxy"),this.flushMessageQueue(),this.linkStartSub.next()}),h(this.socket,"message").pipe(u(this.destorySub)).subscribe(e=>{const{data:t}=e;this.messageSub.next(t)}),h(this.socket,"error").pipe(u(this.destorySub)).subscribe(e=>{const{error:t}=e;C.error("Proxy has some problem",t)}),h(this.socket,"close").pipe(u(this.destorySub)).subscribe(e=>{this.isConnected=!1;const{code:t,reason:s,type:o,wasClean:r}=e;switch(t){case w.WHEN_BROWSER_CLOSE:C.code(O["無副作用的關閉連線"]);break;case w.MDN_NORMAL_CLOSURE:case w.MDN_GOING_AWAY:this.closeSub.next("close"),C.code(O["安全的關閉連線"]);break;case w.MDN_NO_STATUS_RECEIVED:case w.MDN_ABNORMAL_CLOSURE:this.closeSub.next("close"),C.code(O["與雲端的服務中斷"],{code:t,reason:s,type:o,wasClean:r});break;case w.WS_5001_UNAUTHORIZED:case w.WS_5002_LICENSE_EXPIRED:this.closeSub.next("no_permissions"),C.code(O["沒有授權,請聯繫Graphen"]);break;case w.WS_5000_NORAML_CLOSURE:case w.WS_5003_TOKEN_EXPIRED:this.closeSub.next("reconnect"),C.code(O["自動重新連線連端服務"]);break;default:this.closeSub.next("close"),C.code(O["未預期的斷線,請聯繫Graphen"],{code:t,reason:s,type:o,wasClean:r})}})}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,e=>{e&&C.error("send message fail",e?.message,e)})}}send(e){this.isConnected&&void 0!==this.socket?this.socket.send(e,e=>{e&&C.error("send message fail",e?.message,e)}):this.messageQueue.push(e)}onClose(){this.socket?.close(w.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=p.homedir(),this.EOL=p.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){b.mkdirSync(e,{recursive:!0})}fileCheck(e){b.existsSync(e)||b.writeFileSync(e,"",{encoding:"utf8"})}}("aiia-sdk");class I{type="node";filePath;fileName;constructor(){this.fileName="storage.json",this.filePath=L.addPath(L.appFolder,this.fileName),L.fileCheck(this.filePath)}getFileDataSync(){const e=b.readFileSync(this.filePath,"utf8");try{return JSON.parse(e)}catch(e){return{}}}setFileDataSync(e){try{const t=JSON.stringify(e);b.writeFileSync(this.filePath,t,"utf8")}catch(e){}}async getFileData(){return new Promise(e=>{b.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);b.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=r(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=r(this.getFileDataSync(),e);this.setFileDataSync(t)}clearSync(){this.setFileDataSync({})}}class P{config;constructor(e){this.config=e,C.info("UEProxy, config=",e)}start(){C.info("UEProxy, start proxy")}send(e){let t=this.cloudToObj(e);if(t&&t.signal&&"osc"===t.signal){C.info("UEProxy, payload=",t);try{let e=t.content;const s=new g(e.host,e.port);C.debug("UEProxy, client open to port "+e.port),s.send(e.message,e=>{e&&C.error(e),s.close(),C.debug("UEProxy, client closed")})}catch(e){C.error(e)}}}cloudToObj(e){try{return JSON.parse(e)}catch(e){C.error("message transfer fail",e)}}onDestroy(){C.info("UEProxy, destroy proxy")}}class x{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(u(this.destorySub)).subscribe(()=>{this.isConnected=!0,C.code(O["啟動服務"],"proxy"),this.flushMessageQueue(),this.linkStartSub.next()}),h(this.socket,"message").pipe(u(this.destorySub)).subscribe(e=>{const{data:t}=e;this.messageSub.next(t)}),h(this.socket,"error").pipe(u(this.destorySub)).subscribe(e=>{const{error:t}=e;C.error("Proxy has some problem",t)}),h(this.socket,"close").pipe(u(this.destorySub)).subscribe(e=>{this.isConnected=!1;const{code:t,reason:s}=e;switch(t){case w.MANUAL_CLOSE:C.code(O["無副作用的關閉連線"]);break;case w.WS_5000_NORAML_CLOSURE:case w.MDN_NORMAL_CLOSURE:case w.MDN_GOING_AWAY:this.closeSub.next("close"),C.code(O["安全的關閉連線"]);break;case w.MDN_NO_STATUS_RECEIVED:case w.MDN_ABNORMAL_CLOSURE:this.closeSub.next("close"),C.code(O["與雲端的服務中斷"],{code:t,reason:s});break;case w.WS_5001_UNAUTHORIZED:case w.WS_5002_LICENSE_EXPIRED:this.closeSub.next("no_permissions"),C.code(O["沒有授權,請聯繫Graphen"]);break;case w.WS_5003_TOKEN_EXPIRED:this.closeSub.next("reconnect"),C.code(O["自動重新連線連端服務"]);break;default:this.closeSub.next("close"),C.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&&"pcm"===t.command&&(C.info("AudioSocketProxy, payload=",t),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){C.error(e)}}cloudToObj(e){try{return JSON.parse(e)}catch(e){C.error("message transfer fail",e)}}onClose(){this.socket?.close(w.MANUAL_CLOSE)}onDestroy(){this.destorySub.next(),this.destorySub.complete()}}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:8081";start(){this.socket=new S(this.url,{autoPong:!0}),h(this.socket,"open").pipe(u(this.destorySub)).subscribe(()=>{this.isConnected=!0,C.code(O["啟動服務"],"proxy"),this.flushMessageQueue(),this.linkStartSub.next()}),h(this.socket,"message").pipe(u(this.destorySub)).subscribe(e=>{const{data:t}=e;this.messageSub.next(t)}),h(this.socket,"error").pipe(u(this.destorySub)).subscribe(e=>{const{error:t}=e;C.error("Proxy has some problem",t)}),h(this.socket,"close").pipe(u(this.destorySub)).subscribe(e=>{this.isConnected=!1;const{code:t,reason:s}=e;switch(t){case w.MANUAL_CLOSE:C.code(O["無副作用的關閉連線"]);break;case w.WS_5000_NORAML_CLOSURE:case w.MDN_NORMAL_CLOSURE:case w.MDN_GOING_AWAY:this.closeSub.next("close"),C.code(O["安全的關閉連線"]);break;case w.MDN_NO_STATUS_RECEIVED:case w.MDN_ABNORMAL_CLOSURE:this.closeSub.next("close"),C.code(O["與雲端的服務中斷"],{code:t,reason:s});break;case w.WS_5001_UNAUTHORIZED:case w.WS_5002_LICENSE_EXPIRED:this.closeSub.next("no_permissions"),C.code(O["沒有授權,請聯繫Graphen"]);break;case w.WS_5003_TOKEN_EXPIRED:this.closeSub.next("reconnect"),C.code(O["自動重新連線連端服務"]);break;default:this.closeSub.next("close"),C.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&&"interrupted"===t.command&&(C.info("AudioInterruptSocketProxy, payload=",t),this.isConnected&&void 0!==this.socket))try{this.socket.send(t.command)}catch(e){C.error(e)}}cloudToObj(e){try{return JSON.parse(e)}catch(e){C.error("message transfer fail",e)}}onClose(){this.socket?.close(w.MANUAL_CLOSE)}onDestroy(){this.destorySub.next(),this.destorySub.complete()}}class v{next(e,t,...s){const o=`log_${n().format("yyyy-MM-DD")}.txt`,r=L.addPath(L.appFolder,o);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}]`}},"");b.appendFileSync(r,`LOG(${e})__${t}__::${o}${L.EOL}`,{encoding:"utf8"})}catch(e){}}}class U{config;stroage;apiProxy;chat;udpProxy;audioProxy;audioIntrProxy;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 k(this.config.endPoint.api),C.code(O["指定模組初始化完成"],"api"),this.chat=new A(this.config),C.code(O["指定模組初始化完成"],"chat"),this.udpProxy=new P(this.config),C.code(O["指定模組初始化完成"],"udp"),this.audioProxy=new x,C.code(O["指定模組初始化完成"],"audio"),this.audioIntrProxy=new T,C.code(O["指定模組初始化完成"],"audio intr"),this.openChannel=this.openChannel.bind(this)}async start(){if(this.isStart)throw new Error(f["重複初始化"]);this.isStart=!0,await this.replaceAccessToken(),this.chat.memberChange.pipe(u(this.destorySub)).subscribe(({type:e,socket:t})=>{switch(e){case"in":{const e=new M,r=new a,n=(o=t.handshake.query.uuid,i(o)?s(o,"[0]",void 0):o??void 0);void 0===n?(this.getProjectList().then(e=>{t.emit("list",e)}),h(t,"uuid").pipe(l(1),u(r)).subscribe(s=>{this.openChannel(s,t,e,r)})):this.openChannel(n,t,e,r),this.channelMap.set(t.id,()=>{r.next(),r.complete()});break}case"out":this.channelMap.has(t.id)&&(this.channelMap.get(t.id)(),this.channelMap.delete(t.id))}var o}),this.chat.start(),C.code(O["啟動服務"],"chat"),this.udpProxy.start(),C.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(u(o)).subscribe(e=>{s.send(e)}),s.message.pipe(u(o)).subscribe(e=>{t.emit("cloud",e),this.udpProxy.send(e),this.audioProxy.send(e),this.audioIntrProxy.send(e)}),s.close.pipe(u(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(u(o)).subscribe(()=>{t.emit("channel","open")}),o.subscribe(()=>{s.onClose(),this.audioProxy.onClose(),this.audioIntrProxy.onClose()}),s.start(this.getProxySocketURL(e)),this.audioProxy.start(),this.audioIntrProxy.start()}onDestroy(){this.apiProxy.onDestroy(),this.chat.onDestroy(),this.udpProxy.onDestroy(),this.destorySub.next(),this.destorySub.complete(),C.code(O["服務結束"])}}function W(t){if(y){if(void 0===s(t,"license",void 0))throw new Error(f["未提供憑證"]);!function(e){const{info:t,error:s,warn:o,debug:r,fatal:i,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=()=>{}),r||(D.debug=()=>{}),t||(D.info=()=>{}),o||(D.warn=()=>{}),s||(D.error=()=>{}),i||(D.fatal=()=>{}),R}(s(t,"debug",!0))(new v),C.code(O["開始初始化必要模組"]);const o=new m(e({},t));C.code(O["指定模組初始化完成"],"config");const r=new I;C.code(O["指定模組初始化完成"],"storage");const i=new U(o,r);return C.code(O["指定模組初始化完成"],"core"),i.start(),C.code(O["啟動服務"]),()=>{i.onDestroy()}}throw new Error(f["環境錯誤(nodejs)"])}export{W as aiiaCore};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphen.ai/aiia-sdk",
3
- "version": "1.0.11",
3
+ "version": "1.0.13",
4
4
  "keywords": [],
5
5
  "author": {
6
6
  "name": "Jay Huang",
@@ -53,6 +53,7 @@
53
53
  "@rollup/plugin-node-resolve": "^15.3.1",
54
54
  "@rollup/plugin-terser": "^0.4.4",
55
55
  "@rollup/plugin-typescript": "^11.1.6",
56
+ "@rollup/rollup-darwin-arm64": "^4.52.3",
56
57
  "@types/audioworklet": "^0.0.83",
57
58
  "@types/jest": "^30.0.0",
58
59
  "@types/lodash-es": "^4.17.12",
@@ -5,7 +5,7 @@
5
5
  "toolPackages": [
6
6
  {
7
7
  "packageName": "@microsoft/api-extractor",
8
- "packageVersion": "7.52.13"
8
+ "packageVersion": "7.52.11"
9
9
  }
10
10
  ]
11
11
  }