ysyt-agent-sdk 1.1.36 → 1.1.37
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.
|
@@ -911,4 +911,4 @@ Bs=Ts,function(){var t="input is invalid type",s="object"==typeof window,i=s?win
|
|
|
911
911
|
`:""}
|
|
912
912
|
</div>
|
|
913
913
|
</my-popover>
|
|
914
|
-
`;mt(s,this.rttHTML)}}class Ps{static requestPermission(){"Notification"in window&&"default"===Notification.permission&&Notification.requestPermission()}static async requestMediaPermissions(){try{const A=await navigator.mediaDevices.enumerateDevices();if(A.some(A=>"audioinput"===A.kind&&""!==A.label))return null;return await navigator.mediaDevices.getUserMedia({audio:!0})}catch(A){return console.warn("获取音频权限失败:",A),null}}static show(A,e,t,s){if("Notification"in window&&"granted"===Notification.permission){const i=new Notification(A,{body:e,tag:`xy-global-notification${(new Date).getTime()}`,renotify:!0});i.onclick=()=>{window.focus(),i.close(),t?.()},setTimeout(()=>i.close(),1e3*(s||3))}}static async checkMediaPermissions(){try{return(await navigator.mediaDevices.enumerateDevices()).some(A=>"audioinput"===A.kind&&""!==A.label)}catch(A){return console.warn("无法检查设备权限:",A),!1}}}const zs=console.log,Xs=(...A)=>{Ve.logBuiltinEnabled&&zs(...A)};let _s=null;class $s{constructor(A,e){this.url=A,this.onOpenCallback=e,this.ws=null,this.heartbeatWorker=null,this.reconnectDelay=3e3,this.manualClose=!1,this.apiClient=new Ks,this.sipClient=null,this.pendingMessages=new Map,this.initWebSocket()}static getInstance({url:A,sipClient:e,onOpen:t}){if(!_s){if(!A)throw new Error("WebSocket 尚未初始化");_s=new $s(A,t),_s.sipClient=e}return _s}setSipClient(A){this.sipClient=A}stateIdleChange(){const{state:A}=Ve.stateObject;A!==Ae.IDLE&&this.apiClient._changeState({data:{...Ve.stateBeforeChange,state_name:Ve.stateBeforeChange.state===Ae.IDLE?"空闲":"忙碌"}})}inCallAutoAnswer(){const{soft_device_auto_answer:A,auto_answer_time:e}=Ve.agentInfo,t=1===A,s=he.get("direction");if(t&&s===ee.INCOMING){const A=Number(e)||0;A>0&&(Ve.autoAnswerTimer=setTimeout(()=>{this.sipClient?.answerCall(),Ve.autoAnswerTimer=null},1e3*A))}}async getCustomerInfo(A){try{await this.apiClient._getInCallCrmDetail({data:{phone:A},success:A=>{he.updateCustomerInfo(A.data||{phone:A.data.phone,is_vip:0,customer_name:"未知客户",city:"未知",province:"未知",contact_count:0,in_contact_count:0,trace_id:A.data.trace_id,contact_count_month:0,in_contact_count_month:0})}})}catch(A){}}putAgentState(A,e){if(!A)return;if(5===e)return void this.stateIdleChange();Ve.sessionId="";const{post_call_process_time:t}=Ve.agentInfo,s=t||0;if(Ve.autoStateTimer&&(clearTimeout(Ve.autoStateTimer),Ve.autoStateTimer=null),he.updateCustomerInfo(void 0),s>0){const{state:A}=Ve.stateObject;if(A===Ae.IDLE)return;this.apiClient._changeState({data:{state:Ae.BUSY,state_name:"整理"}}),Ve.autoStateTimer=setTimeout(()=>{Ve.autoStateTimer=null,this.stateIdleChange()},1e3*s)}else this.stateIdleChange()}stateBusyChange(){Ve.sessionId="",clearTimeout(Ve.autoStateTimer),he.updateCustomerInfo(void 0),this.apiClient._changeState({data:{state:Ae.BUSY,state_name:"忙碌"}})}initWebSocket(){if(1===this.ws?.readyState)return;const A=Rs(Fs,Ys);this.ws=new WebSocket(this.url+A),this.ws.onopen=()=>{const A=(new Date).getTime();this.sendMessage({type:0,msg_id:A}),this.pendingMessages.set(A,performance.now()),this.startHeartbeat(),this.manualClose=!1,this.onOpenCallback&&this.onOpenCallback()},this.ws.onmessage=async A=>{const e=JSON.parse(A.data),{type:t,data:s,code:i,msg:n,msg_id:o}=e;if(0===t&&o){const A=this.pendingMessages.get(o),e=performance.now()-A;Ve.latency=parseFloat(e.toFixed(2)),this.pendingMessages.delete(o)}if(0!==t&&Xs("%cWebSocket 收到消息:","color: blue; font-weight: bold;",e),t!==te.OUT_CALL&&t!==te.PREVIEW_OUT_CALL||(0!==i?(he.reset(),ae.emit(ie.OUT_FAILED,n),ae.emit(ne.FAIL_NOTIFICATION,n),he.updateActionConfigs(ft),setTimeout(()=>{he.updateCustomerInfo(void 0)},500),this.stateIdleChange()):0===i&&(ae.emit(ie.OUT_SUCCESS),clearTimeout(Ve.autoStateTimer))),t===te.AGENT_STATE){const{state:A,state_name:e}=s;Ve.stateObject={state:A,state_name:e},Ve.stateBeforeChange?.state||Ve.stateObject.state!==Ae.IDLE&&(Ve.stateObject.state!==Ae.BUSY||"整理"===e)||(Ve.stateBeforeChange=Ve.stateObject),"振铃中"===e&&he.updateDisplayText(oe.AGENT_RINGING),A===Ae.UNREGISTERED&&he.updateActionConfigs(Mt),A===Ae.IDLE&&Ve.stateObject.state===Ae.UNREGISTERED&&he.updateActionConfigs(ft),ae.emit(ie.AGENT_STATE,{state:A,state_name:e})}if(t===te.OUT_CALL_INCOMING_CALL){try{await this.getCustomerInfo(s.trace_id||s.customer_phone)}catch(A){console.error(A)}s.preview_task_id?ae.emit(ie.PREVIEW_OUT_INCOMING_CALL,{customer_phone:s.customer_phone,session_id:s.session_id,preview_task_id:s.preview_task_id,trace_id:s.trace_id}):ae.emit(ie.OUT_INCOMING_CALL,{customer_phone:s.customer_phone,session_id:s.session_id,trace_id:s.trace_id}),Ve.sessionId=s.session_id,he.updateActionConfigs(Qt)}if(t===te.OUT_CALL_END)if(he.updateDisplayText(""),1===s.device_type&&s.agent_no===Ve.agentInfo.agent_no)he.reset(),ae.emit(ie.OUT_HANGUP,{session_id:s.session_id}),he.updateActionConfigs(ft),this.putAgentState(!0);else if(2===s.device_type&&s.agent_no===Ve.agentInfo.agent_no)he.reset(),ae.emit(ie.OUR_SIDE_CONSULT_HANGUP,{session_id:s.session_id}),he.updateActionConfigs(ft),this.putAgentState(!0);else if(2===s.device_type&&s.agent_no!==Ve.agentInfo.agent_no){he.get("isCalling")&&(he.updateDisplayText(oe.CALLING),ae.emit(ie.OTHER_SIDE_CONSULT_HANGUP,{session_id:s.session_id}),he.updateActionConfigs(St))}else if(3===s.device_type&&s.agent_no===Ve.agentInfo.agent_no)he.reset(),ae.emit(ie.THREE_WAY_OUR_SIDE_HANGUP,{session_id:s.session_id}),he.updateActionConfigs(ft),this.putAgentState(!0);else if(4===s.device_type&&s.agent_no===Ve.agentInfo.agent_no)he.reset(),ae.emit(ie.TRANSFER_END,{session_id:s.session_id}),he.updateActionConfigs(ft),this.putAgentState(!0);else if(4===s.device_type&&s.agent_no!==Ve.agentInfo.agent_no){ae.emit(ie.TRANSFER_FAILED,"未知原因"),ae.emit(ne.FAIL_NOTIFICATION,"转接失败");const A=he.get("direction");A===ee.OUTGOING?he.updateActionConfigs(St):A===ee.INCOMING&&he.updateActionConfigs(Ut)}else 5===s.device_type&&s.agent_no===Ve.agentInfo.agent_no&&(Ve.monitoredAgentNo="",Ve.earSpeakAgent="",he.reset(),ae.emit(ie.LISTEN_HANGUP,{session_id:s.session_id}),he.updateActionConfigs(ft),this.putAgentState(!0,5));if(t===te.OUT_CALL_RINGING&&1===s.type&&(ae.emit(ie.OUT_RINGING,{session_id:Ve.sessionId,type:s.type}),he.updateDisplayText(oe.RINGING)),t===te.OUT_CALL_ANSWER&&(clearTimeout(Ve.autoStateTimer),he.updateIsCalling(!0),ae.emit(ie.OUT_OFF_ANSWER,{session_id:Ve.sessionId}),he.updateOutCallIsAnswer(!0),he.updateDisplayText(oe.CALLING),he.updateActionConfigs(St)),t===te.CONSULT_RINGING&&(ae.emit(ie.CONSULT_RINGING,{session_id:s.session_id}),he.updateDisplayText(oe.AGENT_RINGING),he.updateActionConfigs([]),clearTimeout(Ve.autoStateTimer)),t===te.CONSULT_ANSWER&&(ae.emit(ie.CONSULT_OFF_HOOK,{session_id:s.session_id}),he.updateConsultIsAnswer(!0),he.updateDisplayText(oe.CONSULTING),he.updateActionConfigs(Tt)),t===te.CONSULT_FAIL&&(ae.emit(ie.CONSULT_FAILED,{custom_reason:s.custom_reason}),he.updateDisplayText(oe.CALLING),he.updateActionConfigs(St)),t===te.CONSULT_CALL_IN&&(ae.emit(ie.CONSULT_INCOMING,{agent_no:s.agent_no,agent_mane:s.agent_name,session_id:s.session_id}),Ve.sessionId=s.session_id,he.updateIsCalling(!0),he.updateDirection(ee.CONSULT),he.updateDisplayText(oe.CONSULT_CALL_IN),he.updateActionConfigs(Dt),Ve.enableBrowserAlert&&Ps.show("📞 咨询来电",`坐席 [${s.agent_name}] 正在咨询...`,()=>{ae.emit(ne.NOTIFICATION_CLICK,{agent_mane:s.agent_name,agent_no:s.agent_no})},Ve.browserAlertTime)),t===te.CONSULT_CALL_IN_SUCCESS&&(ae.emit(ie.CONSULT_CALL_IN_SUCCESS,{session_id:s.session_id}),he.updateConsultIsAnswer(!0),he.updateDisplayText(oe.CALLING),he.updateActionConfigs(yt)),t===te.CONSULT_RETURN_CALL_SUCCESS&&("success"===s.status?(he.updateConsultIsAnswer(!1),ae.emit(ie.CONSULT_RETURN_CALL_SUCCESS),he.updateDisplayText(oe.CALLING),ae.emit(ie.OTHER_SIDE_CONSULT_HANGUP),he.updateActionConfigs(St)):ae.emit(ne.FAIL_NOTIFICATION,"媒体失败")),t===te.MUTE&&(0===i?(he.updateIsMuted(!0),ae.emit(ie.MUTE_SUCCESS),he.updateDisplayText(oe.MUTING)):(ae.emit(ie.MUTE_FAILED,n),ae.emit(ne.FAIL_NOTIFICATION,n))),t===te.UNMUTE&&(0===i?(he.updateIsMuted(!1),ae.emit(ie.UNMUTE_SUCCESS),he.updateDisplayText(oe.CALLING)):(ae.emit(ie.UNMUTE_FAILED,n),ae.emit(ne.FAIL_NOTIFICATION,n))),t===te.CALL_HOLD&&(0===i?(he.updateIsHold(!0),he.updateDisplayText(oe.HOLDING),ae.emit(ie.HOLD_SUCCESS)):(ae.emit(ie.HOLD_FAILED,n),ae.emit(ne.FAIL_NOTIFICATION,n))),t===te.CALL_UNHOLD&&(0===i?(he.updateDisplayText(oe.CALLING),he.updateIsHold(!1),ae.emit(ie.UNHOLD_SUCCESS)):(ae.emit(ie.UNHOLD_FAILED,n),ae.emit(ne.FAIL_NOTIFICATION,n))),t===te.CONSULT_TRANSFER&&(0===i?ae.emit(ie.CONSULT_TRANSFER_SUCCESS):(ae.emit(ie.CONSULT_TRANSFER_FAILED,n),ae.emit(ne.FAIL_NOTIFICATION,n))),t===te.CONSULT_TRANSFER_FAILED&&ae.emit(ie.CONSULT_TRANSFER_FAILED,s.status),t===te.CONSULT_TRANSFER_OFF_HOOK&&(he.updateDirection(ee.INCOMING),he.updateDisplayText(oe.CALLING),ae.emit(ie.CONSULT_TRANSFER_OFF_HOOK),he.updateActionConfigs(Ut),he.updateIncomingIsAnswer(!0),he.updateConsultIsAnswer(!1)),t===te.INCOMING_CALL){clearTimeout(Ve.autoStateTimer),Ve.sessionId=s.session_id;try{await this.getCustomerInfo(s.trace_id||s.customer_phone)}catch(A){console.error(A)}he.updateIsCalling(!0),he.updateDirection(ee.INCOMING),this.inCallAutoAnswer(),he.updateDisplayText(oe.INCOMING_CALL),1===s.direction&&ae.emit(ie.INCOMING_CALL,{customer_phone:s.customer_phone,trace_id:s.trace_id,session_id:s.session_id}),3===s.direction&&(ae.emit(ie.PREDICT_INCOMING_CALL,{customer_phone:s.customer_phone,trace_id:s.trace_id,session_id:s.session_id}),this.sipClient?.answerCall()),he.updateActionConfigs(Dt),Ve.enableBrowserAlert&&Ps.show("📞 客户来电",`客户 [${s.customer_phone}] 正在呼入...`,()=>{ae.emit(ne.NOTIFICATION_CLICK,{customer_phone:s.customer_phone})},Ve.browserAlertTime)}if(t===te.INCOMING_CALL_OFF_HOOK&&(he.updateIncomingIsAnswer(!0),ae.emit(ie.INCOMING_CALL_OFF_HOOK,{session_id:Ve.sessionId}),he.updateActionConfigs(Ut),he.updateDisplayText(oe.CALLING)),t===te.INCOMING_CALL_END)if(he.updateDisplayText(""),1===s.device_type&&s.agent_no===Ve.agentInfo.agent_no)this.putAgentState(!0),he.reset(),ae.emit(ie.INCOMING_CALL_END,{session_id:s.session_id}),he.updateActionConfigs(ft);else if(2===s.device_type&&s.agent_no===Ve.agentInfo.agent_no)he.reset(),ae.emit(ie.OUR_SIDE_CONSULT_HANGUP,{session_id:s.session_id}),this.putAgentState(!0),he.updateActionConfigs(ft);else if(2===s.device_type&&s.agent_no!==Ve.agentInfo.agent_no)he.updateDisplayText(oe.CALLING),ae.emit(ie.OTHER_SIDE_CONSULT_HANGUP,{session_id:s.session_id}),he.updateActionConfigs(St);else if(3===s.device_type&&s.agent_no===Ve.agentInfo.agent_no)this.putAgentState(!0),he.reset(),ae.emit(ie.THREE_WAY_OUR_SIDE_HANGUP,{session_id:s.session_id}),he.updateActionConfigs(ft);else if(4===s.device_type&&s.agent_no===Ve.agentInfo.agent_no)this.putAgentState(!0),he.reset(),ae.emit(ie.TRANSFER_END,{session_id:s.session_id}),he.updateActionConfigs(ft);else if(4===s.device_type&&s.agent_no!==Ve.agentInfo.agent_no){ae.emit(ie.TRANSFER_FAILED,"未知原因"),ae.emit(ne.FAIL_NOTIFICATION,"转接失败");const A=he.get("direction");A===ee.OUTGOING?he.updateActionConfigs(St):A===ee.INCOMING&&he.updateActionConfigs(Ut)}else 5===s.device_type&&s.agent_no===Ve.agentInfo.agent_no&&(this.putAgentState(!0,5),Ve.monitoredAgentNo="",Ve.earSpeakAgent="",he.reset(),ae.emit(ie.LISTEN_HANGUP,{session_id:s.session_id}),he.updateActionConfigs(ft));if(t===te.SATISFACTION_EVALUATION&&(0===i?ae.emit(ie.SATISFACTION_EVALUATION_SUCCESS):(ae.emit(ie.SATISFACTION_EVALUATION_FAILED,n),ae.emit(ne.FAIL_NOTIFICATION,n))),t===te.CONSULT_THREE_WAY&&(0!==i?(ae.emit(ie.CONSULT_THREE_WAY_FAILED,n),ae.emit(ne.FAIL_NOTIFICATION,n)):0===i&&he.updateIsMeeting(!0)),t===te.CONSULT_THREE_WAY_RESULT&&("success"===s.status?(he.updateDirection(ee.MEETING),he.updateDisplayText(oe.MEETING),he.updateActionConfigs(Rt),ae.emit(ie.CONSULT_THREE_WAY_SUCCESS)):(ae.emit(ie.CONSULT_THREE_WAY_FAILED,{conf_name:s.conf_name}),he.updateIsMeeting(!1))),t===te.TRANSFER_INCOMING_CALL&&(clearTimeout(Ve.autoStateTimer),s.agent_no===Ve.agentInfo.agent_no?(he.updateDisplayText(oe.TRANSFERRING),ae.emit(ie.TRANSFER_PROGRESS),he.updateActionConfigs([]),he.updateTransferIsAnswer(!0)):(Ve.sessionId=s.session_id,ae.emit(ie.TRANSFER_INCOMING_CALL,{agent_no:s.agent_no,agent_name:s.agent_name,session_id:s.session_id}),he.updateIsCalling(!0),he.updateDirection(ee.TRANSFER),he.updateDisplayText(oe.TRANSFER_INCOMING_CALL),he.updateActionConfigs(Dt),Ve.enableBrowserAlert&&Ps.show("📞 转接来电",`坐席 [${s.agent_no}] 正在转接...`,()=>{ae.emit(ne.NOTIFICATION_CLICK,{agent_no:s.agent_no})},Ve.browserAlertTime))),t===te.TRANSFER_OFF_HOOK&&(ae.emit(ie.TRANSFER_OFF_HOOK,{session_id:Ve.sessionId}),he.updateTransferIsAnswer(!1),he.updateDisplayText(oe.CALLING),he.updateActionConfigs(Ut)),t===te.TRANSFER_FAILED&&"failed"===s.status){ae.emit(ie.TRANSFER_FAILED,"未知原因"),ae.emit(ne.FAIL_NOTIFICATION,"转接失败");const A=he.get("direction");A===ee.OUTGOING?he.updateActionConfigs(St):A===ee.INCOMING&&he.updateActionConfigs(Ut)}t===te.KICK_OFFLINE&&ae.emit(ne.KICK_OFF),t===te.CONTINUOUS_NOT_ANSWER&&(ae.emit(ne.FAIL_NOTIFICATION,`已连续 ${s.miss_count} 次未接, 设置忙碌`),this.stateBusyChange()),t===te.SIGN_OUT&&ae.emit(ne.SIGN_OUT),t===te.SIGN_IN_SWITCH_DEVICE&&ae.emit(ne.SIGN_IN_SWITCH_DEVICE,s),t===te.LISTEN_INCOMING_CALL&&(clearTimeout(Ve.autoStateTimer),Ve.sessionId=s.session_id,he.updateIsCalling(!0),he.updateDirection(ee.INCOMING),this.inCallAutoAnswer(),he.updateDisplayText(oe.LISTENING),he.updateActionConfigs(Dt),ae.emit(ie.LISTEN_INCOMING_CALL,{session_id:s.session_id}),Ve.enableBrowserAlert&&Ps.show("📞 监听来电","正在来电...",()=>{ae.emit(ne.NOTIFICATION_CLICK,{customer_phone:s.customer_phone})},Ve.browserAlertTime)),t===te.LISTEN_INCOMING_CALL_OFF_HOOK&&(Ve.monitoredAgentNo=s.monitored_agent_no,Ve.earSpeakAgent="",he.updateIncomingIsAnswer(!0),ae.emit(ie.LISTEN_INCOMING_CALL_OFF_HOOK,{session_id:Ve.sessionId,monitored_agent_no:s.monitored_agent_no}),he.updateActionConfigs(kt),he.updateDisplayText(oe.CALLING)),t===te.LISTEN_CALL_FAILED&&"failed"===s.status&&(ae.emit(ie.LISTEN_CALL_FAILED),Ve.monitoredAgentNo=""),t===te.EAR_SPEECH&&("success"===s.status?(s.monitored_agent_no===Ve.agentInfo.agent_no&&(he.updateActionConfigs(Gt),ae.emit(ie.EAR_SPEECH_BE_SUCCESS)),s.monitor_agent_no===Ve.agentInfo.agent_no&&(he.updateActionConfigs(Ft),ae.emit(ie.EAR_SPEECH_SUCCESS,{monitored_agent_no:s.monitored_agent_no,monitor_agent_no:s.monitor_agent_no}),Ve.earSpeakAgent=s.monitored_agent_no)):ae.emit(ie.EAR_SPEECH_FAILED)),t===te.STOP_EAR_SPEECH&&("success"===s.status?(s.monitored_agent_no===Ve.agentInfo.agent_no&&he.updateActionConfigs(Ut),s.monitor_agent_no===Ve.agentInfo.agent_no&&he.updateActionConfigs(kt),Ve.earSpeakAgent="",ae.emit(ie.STOP_EAR_SPEECH_SUCCESS)):ae.emit(ie.STOP_EAR_SPEECH_FAILED)),t===te.CALL_FORCE_INSERT&&("success"===s.status?(s.monitored_agent_no===Ve.agentInfo.agent_no&&(he.updateActionConfigs(Zt),ae.emit(ie.CALL_FORCE_INSERT_SUCCESS,{monitored_agent_no:s.monitored_agent_no,monitor_agent_no:s.monitor_agent_no})),s.monitor_agent_no===Ve.agentInfo.agent_no&&(he.updateActionConfigs(Yt),ae.emit(ie.CALL_FORCE_INSERT_SUCCESS_LISTENER,{monitored_agent_no:s.monitored_agent_no,monitor_agent_no:s.monitor_agent_no}))):ae.emit(ie.CALL_FORCE_INSERT_FAILED))},this.ws.onclose=()=>{this.stopHeartbeat(),this.manualClose||setTimeout(()=>this.initWebSocket(),this.reconnectDelay)},this.ws.onerror=()=>{this.ws?.close()}}startHeartbeat(){const A=new Blob(["\n let timer;\n self.onmessage = function (e) {\n if (e.data === 'start') {\n timer = setInterval(() => {\n self.postMessage('heartbeat');\n }, 5000);\n } else if (e.data === 'stop') {\n clearInterval(timer);\n }\n };\n "],{type:"application/javascript"}),e=URL.createObjectURL(A);this.heartbeatWorker=new Worker(e),this.heartbeatWorker.onmessage=()=>{const A=(new Date).getTime();this.sendMessage({type:0,msg_id:A}),this.pendingMessages.set(A,performance.now())},this.heartbeatWorker.postMessage("start")}stopHeartbeat(){this.heartbeatWorker&&(this.heartbeatWorker.postMessage("stop"),this.heartbeatWorker.terminate(),this.heartbeatWorker=null)}sendMessage(A){this.ws?.readyState===WebSocket.OPEN?(this.ws.send(JSON.stringify(A)),0!==A.type&&Xs("%cWebSocket 发送消息:","color: green; font-weight: bold;",A)):console.warn("WebSocket 未连接,消息未发送")}close(){this.manualClose=!0,this.stopHeartbeat(),this.ws?.close()}reconnect(){this.initWebSocket()}static __internalClose(){_s&&(_s.close(),_s=null)}static __internalSend(A){if(!_s)throw new Error("WebSocket 尚未初始化");_s.sendMessage(A)}}class Ai{constructor(A){this.sipClient=null,this.sipClient=A}roleMute(){const A=he.get("isCalling"),e=Ve.sessionId;if(!A)throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}));if(!e)throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}))}holdCall(){this.roleMute();const A=he.get("isHold"),{sessionId:e}=Ve;if(!e)return;if(!he.get("actionConfigs").includes("hold"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿保持"}));if(A)throw new Error(JSON.stringify({code:1,msg:"当前已处于保持状态"}));$s.__internalSend({type:te.CALL_HOLD,data:{session_id:e}})}unholdCall(){this.roleMute();const{isHold:A}=he.getState(),{sessionId:e}=Ve;if(!he.get("actionConfigs").includes("unhold"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿取消保持"}));if(e){if(!A)throw new Error(JSON.stringify({code:1,msg:"当前未处于保持状态"}));$s.__internalSend({type:te.CALL_UNHOLD,data:{session_id:e}})}}sendDTMF(A){if(1!==he.get("answerDevice"))throw new Error(JSON.stringify({code:1,msg:"当前设备不支持此功能"}));if(!he.get("actionConfigs").includes("dtmf"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿发送按键"}));const{isCalling:e}=he.getState();if(!e)throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}));this.sipClient.sendDTMF(A)}cancelCall(){if(!he.get("isCalling"))throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}));const A=Ve.sessionId;ae.emit(ie.REJECT,{session_id:A}),$s.__internalSend({type:te.AGENT_HANGUP,data:{session_id:A}})}async consultCall(A){this.roleMute();const{isCalling:e,outCallIsAnswer:t,consultIsAnswer:s,incomingIsAnswer:i}=he.getState(),n=Ve.sessionId;if(!he.get("actionConfigs").includes("consult"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用咨询"}));if(s)throw new Error(JSON.stringify({code:1,msg:"当前正在咨询"}));if(!(e||t||i))throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}));if(!n)throw new Error(JSON.stringify({code:1,msg:"sessionId不存在"}));$s.__internalSend({type:te.CONSULT_REQUEST,data:{session_id:n,consultation_agent_no:A}})}async answerCall(){if(1!==he.get("answerDevice"))throw new Error(JSON.stringify({code:1,msg:"当前设备不支持此功能"}));await this.sipClient.answerCall()}makeCall(A){if("string"!=typeof A||""===A.trim())throw new Error(JSON.stringify({code:1,msg:"外呼号码非法"}));if(!/^(?:1[3-9]\d{9}|0\d{2,3}-?\d{7,8})$/.test(A))throw new Error(JSON.stringify({code:1,msg:"外呼号码非法"}));const e={};Ds(A)?e.trace_id=A:e.customer_phone=A.replace(/[\s\r\n-]+/g,"");const t=he.get("isCalling"),s=he.get("selectOutNumber")||"";if(t)throw new Error(JSON.stringify({code:1,msg:"正在呼叫中,请勿重复点击"}));he.updateIsCalling(!0),he.updateDirection(ee.OUTGOING),$s.__internalSend({type:te.OUT_CALL,data:{...e,caller:s.replace(/[\s\r\n-]+/g,"")}})}previewCall(A,e){const t={};Ds(A)?t.trace_id=A:t.customer_phone=A.replace(/[\s\r\n-]+/g,"");const s=he.get("isCalling"),i=he.get("selectOutNumber")||"";if(s)throw new Error(JSON.stringify({code:1,msg:"正在呼叫中,请勿重复点击"}));if("string"!=typeof A||""===A.trim())throw new Error(JSON.stringify({code:1,msg:"外呼号码非法"}));he.updateIsCalling(!0),he.updateDirection(ee.OUTGOING),$s.__internalSend({type:te.PREVIEW_OUT_CALL,data:{...t,caller:i,preview_task_id:e}})}async hangup(){if(!he.get("isCalling"))throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}));const A=Ve.sessionId;$s.__internalSend({type:te.AGENT_HANGUP,data:{session_id:A}})}mute(){this.roleMute();const A=he.get("actionConfigs"),e=he.get("isMuted"),t=Ve.sessionId;if(e)throw new Error(JSON.stringify({code:1,msg:"当前未处于静音状态"}));if(!A.includes("mute"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用静音"}));$s.__internalSend({type:te.MUTE,data:{session_id:t}})}unmute(){this.roleMute();const A=he.get("actionConfigs"),e=he.get("isMuted"),t=Ve.sessionId;if(!e)throw new Error(JSON.stringify({code:1,msg:"当前未处于未静音状态"}));if(!A.includes("unmute"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用静音"}));$s.__internalSend({type:te.UNMUTE,data:{session_id:t}})}consultTransfer(){this.roleMute();const A=he.get("consultIsAnswer"),e=Ve.sessionId;if(!he.get("actionConfigs").includes("consult_transfer"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用咨询转移"}));if(!A)throw new Error(JSON.stringify({code:1,msg:"当前未处于咨询中"}));$s.__internalSend({type:te.CONSULT_TRANSFER,data:{session_id:e}})}satisfactionEvaluation(){this.roleMute();const A=Ve.sessionId;if(!he.get("actionConfigs").includes("satisfaction"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用满意度评价"}));$s.__internalSend({type:te.SATISFACTION_EVALUATION,data:{session_id:A}})}startConference(){this.roleMute();if(!he.get("actionConfigs").includes("conference"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用三方会议"}));const A=Ve.sessionId;if(!he.get("consultIsAnswer"))throw new Error(JSON.stringify({code:1,msg:"当前未处于咨询中"}));$s.__internalSend({type:te.CONSULT_THREE_WAY,data:{session_id:A}})}consultRetrieve(){this.roleMute();if(!he.get("actionConfigs").includes("consult_retrieve"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿取回通话"}));const A=Ve.sessionId;if(!he.get("consultIsAnswer"))throw new Error(JSON.stringify({code:1,msg:"当前未处于咨询中"}));$s.__internalSend({type:te.CONSULT_RETURN_CALL,data:{session_id:A}})}async transferAgent(A){this.roleMute();if(!he.get("actionConfigs").includes("transfer"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿转接"}));const{transferIsAnswer:e}=he.getState(),t=Ve.sessionId;if(e)throw new Error(JSON.stringify({code:1,msg:"当前正在转接"}));$s.__internalSend({type:te.TRANSFER,data:{session_id:t,transfer_type:0,transfer_dest:A}})}async transferOutline(A){this.roleMute();if(!he.get("actionConfigs").includes("transfer"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿转接"}));if(!A)throw new Error(JSON.stringify({code:1,msg:"号码不能为空"}));if(!/^\d+$/.test(A))throw new Error(JSON.stringify({code:1,msg:"号码格式错误"}));const{transferIsAnswer:e}=he.getState(),t=Ve.sessionId;if(e)throw new Error(JSON.stringify({code:1,msg:"当前正在转接"}));if(e)throw new Error(JSON.stringify({code:1,msg:"当前正在转接"}));$s.__internalSend({type:te.TRANSFER,data:{session_id:t,transfer_type:2,transfer_dest:A}})}}var ei;class ti{static{this._instance=null}#A=null;#e=new Ks;#t;#s=null;#i=!1;#n;#o;#r=!1;static{this._timer=null}#a=null;constructor(){if(!ei._creatingInstance)throw new Error("请使用 YSYTAgentSdk.getInstance() 获取实例");clearInterval(this.#s),this.#s=setInterval(async()=>{const A=await this.#V();le.updateRttObject(A)},2e3),Ps.requestMediaPermissions(),this.#n=he.subscribeKey("answerDevice",async A=>{1===A?(await this.initWebRtc(),Ve.stateObject.state===Ae.UNREGISTERED&&await this.#e._changeState({data:{...Ve.stateBeforeChange,state_name:Ve.stateBeforeChange.state===Ae.IDLE?"空闲":"忙碌"}})):await this.destroyRtc()}),this.#o=he.subscribeKey("isNextState",async A=>{if(A){await this.#e._agentLogin(Zs);1===he.get("answerDevice")&&await this.#A.reconnect();let A="";const e=new URL(Gs).host;A=Gs.includes("https")?`wss://${e}`:`ws://${e}`;const{agent_no:t}=Ve.agentInfo;$s.getInstance({url:`${A}/ccs-ws/api/v1/ws/call/${t}?Authorization=`,sipClient:this.#A}),Ve.stateObject.state===Ae.OFFLINE&&setTimeout(()=>{this.#e._changeState({data:{...this.#a,state_name:this.#a.state===Ae.BUSY&&"整理"===this.#a.state_name?"忙碌":this.#a.state_name},success:()=>{he.updateDisplayText(""),ae.emit(ie.AGENT_STATE,this.#a)}})},10)}else this.#a=Ve.stateObject,$s.__internalClose(),he.updateDisplayText("离线")}),this.on(ne.KICK_OFF,()=>{this.destroy()}),window.addEventListener("beforeunload",A=>{A.returnValue="",this.destroy()}),window.addEventListener("online",this.#c),window.addEventListener("offline",this.#h)}#c=()=>{he.updateIsNextState(!0)};#h=()=>{he.updateIsNextState(!1)};async#l(){const A=he.get("isNextState");try{await Ns(),A||he.updateIsNextState(!0)}catch(e){A&&he.updateIsNextState(!1)}}#g(){ei._timer||(ei._timer=setInterval(()=>this.#l(),3e3))}static get instance(){return this._instance}static async destroyInstance(){this._instance&&(await this._instance.destroy(),this._instance=null)}static getInstance(A){if(!this._instance){if(!A)throw new Error("首次调用必须传入配置参数");const{url:e,appKey:t,appSecret:s,agentNo:i}=A;Gs=e||"https://aicc-api.yescloudy.com",Fs=t,Ys=s,Zs=i,ei._creatingInstance=!0,this._instance=new ei,delete ei._creatingInstance}return this._instance}getInit(){return this.#r}async initWebRtc(){if(this.#A)return;const A=Ve.agentInfo.sip_account_list.find(A=>1===A.device_type);if(!A)throw new Error(JSON.stringify({code:1,msg:"未找到 sip_account_list"}));{const e=A.sip_register_addr;this.#A=new ge({server:e.split(":")[0],user:A.sip_account,password:A.sip_password,webSocket:`wss://${e}`,callTimeout:3e4},this.#q),await this.#A.start()}}async init({actionNodeParams:A,enableBrowserAlert:e,logBuiltinEnabled:t,browserAlertTime:s,initialState:i,initDevice:n}){try{let o="";const r=new URL(Gs).host;o=Gs.includes("https")?`wss://${r}`:`ws://${r}`;const a=$s.getInstance({url:`${o}/ccs-ws/api/v1/ws/call/${Zs}?Authorization=`,sipClient:null,onOpen:async()=>{const{viewHtmlElement:o,rttHTML:r}=A||{};Ve.logBuiltinEnabled=t||!1,Ve.browserAlertTime=s||5e3,await this.#e._agentLogin(Zs);const{current_answer_device:V,current_state:c,out_display_number:h,out_display_select_switch:l}=Ve.agentInfo;(i||c)===Ae.IDLE?(he.updateActionConfigs(ft),Ve.stateObject={state:i||c,state_name:"空闲"},Ve.stateBeforeChange=Ve.stateObject):(i||c)===Ae.BUSY?(he.updateActionConfigs(Bt),Ve.stateObject={state:c,state_name:"忙碌"},Ve.stateBeforeChange=Ve.stateObject):(i||c)===Ae.UNREGISTERED&&(he.updateActionConfigs(Bt),Ve.stateObject={state:c,state_name:"未注册"}),1===l?he.updateSelectOutNumber(h):he.updateSelectOutNumber(""),he.updateAnswerDevice(n||V),1===(n||V)&&(await this.initWebRtc(),a.setSipClient(this.#A)),this.#t=new Ai(this.#A),A&&o&&(this.#i=!0,new Ws({container:o,rttHTML:r,statusParams:{state:Ve.stateObject.state,statusName:Ve.stateObject.state_name}},this,this.#t)),Ve.enableBrowserAlert=e,e&&Ps.requestPermission(),ae.emit(ne.AGENT_INIT_STATUS,!0),this.#r=!0,clearInterval(ei._timer),ei._timer=null,this.#g()}})}catch(A){this.#i&&Hs("初始化失败"),ae.emit(ne.AGENT_INIT_STATUS,!1),this.#r=!1}}#q=A=>{const{type:e,data:t}=A;ae.emit(e,t)};on(A,e){return ae.on(A,e),this}off(A,e){ae.off(A,e)}offAll(){ae.clearAllListeners()}async destroyRtc(){await(this.#A?.destroy()),this.#A=null}async destroy(){ei._instance&&(await this.destroyRtc(),clearInterval(this.#s),$s.__internalClose(),ae.emit(ne.DESTROY),this.offAll(),this.#n?.(),this.#o?.(),this.#i=!1,ei._instance=null,he.reset(),he.updateSelectOutNumber(""),clearInterval(ei._timer),ei._timer=null)}async#V(){return this.#A?this.#A.getNetworkStats():{}}get call_api(){const A=A=>(...e)=>{if(!this.#i)return A.apply(this,e);console.warn("当前未使用个人开发模式")};return{answerCall:A(()=>this.#t.answerCall()),makeCall:A=>this.#t.makeCall(A),previewMakeCall:(A,e)=>this.#t.previewCall(A,e),cancelCall:A(()=>this.#t.cancelCall()),hangup:A(()=>this.#t.hangup()),sendDTMF:A(A=>this.#t.sendDTMF(A)),holdCall:A(()=>this.#t.holdCall()),unholdCall:A(()=>this.#t.unholdCall()),consultCall:A(async A=>{try{await this.#t.consultCall(A)}catch(A){throw console.warn(A),A}}),consultRetrieve:A(()=>this.#t.consultRetrieve()),consultTransfer:A(()=>this.#t.consultTransfer()),mute:A(()=>this.#t.mute()),unmute:A(()=>this.#t.unmute()),satisfactionEvaluation:A(()=>this.#t.satisfactionEvaluation()),startConference:A(()=>this.#t.startConference()),transferAgent:A(A=>this.#t.transferAgent(A)),transferOutline:A(A=>this.#t.transferOutline(A)),earSpeakStart:A(A=>this.#e.earCall(A)),earSpeakStop:A(A=>this.#e.stopEarCall(A)),forceInsert:A(A=>this.#e.forceInsert(A)),forceDisconnect:A(A=>this.#e.forceDisconnect(A))}}get agent_api(){const A=A=>(...e)=>{if(!this.#i)return A.apply(this,e);console.warn("当前未使用个人开发模式")};return{changeDevice:A(A=>this.#e.changeDevice(A)),changeState:A(A=>this.#e.changeState(A)),getIdleAgentList:A(A=>this.#e.getIdleAgentList(A)),getAgentState:A(A=>this.#e.getAgentState(A)),setAgentExplicit:A(A=>this.#e.setAgentExplicit(A)),checkAgentLogin:A=>this.#e.checkAgentLogin(A),getCallRecord:A=>this.#e.getCallRecord(A),agentLogout:async(A,e)=>{try{await this.#e.logout(A),await this.destroy(),e?.({code:0,msg:""})}catch(A){}},listenAgentCall:A=>this.#e.listenCall(A)}}getAgentData(){const{agent_no:A,agent_name:e,answer_devices:t,out_display_data:s,out_display_select_switch:i,current_state:n}=Ve.agentInfo,{stateObject:o}=Ve,r={1:"软电话",2:"手机模式",3:"SIP话机"};return{agent_no:A,agent_name:e,init_state:n,default_device:he.get("answerDevice"),out_display_data:s,out_display_select_switch:i,answer_devices:(t||"").split(",").map(A=>({value:Number(A),label:r[Number(A)]})),stateObject:o}}async getRealTimeCustomerInfo(){return he.get("customerObject")}setBrowserAlert(A){if("boolean"!=typeof A)throw new Error(JSON.stringify({code:1,msg:"参数类型错误"}));Ve.logBuiltinEnabled=A||!1}setBrowserAlertTime(A){if("number"!=typeof A)throw new Error(JSON.stringify({code:1,msg:"参数类型错误"}));Ve.browserAlertTime=A||3e3}}ei=ti,import.meta.hot&&import.meta.hot.dispose(()=>{window.location.reload()});export{Ae as AgentStatus,ie as CallSipCallEventEnum,ee as DirectionEnum,oe as DisplayTextEnum,ne as EventName,se as WebRtcCallSipEventEnum,te as WebsocketTypeEnum,ti as default};
|
|
914
|
+
`;mt(s,this.rttHTML)}}class Ps{static requestPermission(){"Notification"in window&&"default"===Notification.permission&&Notification.requestPermission()}static async requestMediaPermissions(){try{const A=await navigator.mediaDevices.enumerateDevices();if(A.some(A=>"audioinput"===A.kind&&""!==A.label))return null;return await navigator.mediaDevices.getUserMedia({audio:!0})}catch(A){return console.warn("获取音频权限失败:",A),null}}static show(A,e,t,s){if("Notification"in window&&"granted"===Notification.permission){const i=new Notification(A,{body:e,tag:`xy-global-notification${(new Date).getTime()}`,renotify:!0});i.onclick=()=>{window.focus(),i.close(),t?.()},setTimeout(()=>i.close(),1e3*(s||3))}}static async checkMediaPermissions(){try{return(await navigator.mediaDevices.enumerateDevices()).some(A=>"audioinput"===A.kind&&""!==A.label)}catch(A){return console.warn("无法检查设备权限:",A),!1}}}const zs=console.log,Xs=(...A)=>{Ve.logBuiltinEnabled&&zs(...A)};let _s=null;class $s{constructor(A,e){this.url=A,this.onOpenCallback=e,this.ws=null,this.heartbeatWorker=null,this.reconnectDelay=3e3,this.manualClose=!1,this.apiClient=new Ks,this.sipClient=null,this.pendingMessages=new Map,this.initWebSocket()}static getInstance({url:A,sipClient:e,onOpen:t}){if(!_s){if(!A)throw new Error("WebSocket 尚未初始化");_s=new $s(A,t),_s.sipClient=e}return _s}setSipClient(A){this.sipClient=A}stateIdleChange(){const{state:A}=Ve.stateObject;A!==Ae.IDLE&&this.apiClient._changeState({data:{...Ve.stateBeforeChange,state_name:Ve.stateBeforeChange.state===Ae.IDLE?"空闲":"忙碌"}})}inCallAutoAnswer(){const{soft_device_auto_answer:A,auto_answer_time:e}=Ve.agentInfo,t=1===A,s=he.get("direction");if(t&&s===ee.INCOMING){const A=Number(e)||0;A>0&&(Ve.autoAnswerTimer=setTimeout(()=>{this.sipClient?.answerCall(),Ve.autoAnswerTimer=null},1e3*A))}}async getCustomerInfo(A){try{await this.apiClient._getInCallCrmDetail({data:{phone:A},success:A=>{he.updateCustomerInfo(A.data||{phone:A.data.phone,is_vip:0,customer_name:"未知客户",city:"未知",province:"未知",contact_count:0,in_contact_count:0,trace_id:A.data.trace_id,contact_count_month:0,in_contact_count_month:0})}})}catch(A){}}putAgentState(A,e){if(!A)return;if(5===e)return void this.stateIdleChange();Ve.sessionId="";const{post_call_process_time:t}=Ve.agentInfo,s=t||0;if(Ve.autoStateTimer&&(clearTimeout(Ve.autoStateTimer),Ve.autoStateTimer=null),he.updateCustomerInfo(void 0),s>0){const{state:A}=Ve.stateObject;if(A===Ae.IDLE)return;this.apiClient._changeState({data:{state:Ae.BUSY,state_name:"整理"}}),Ve.autoStateTimer=setTimeout(()=>{Ve.autoStateTimer=null,this.stateIdleChange()},1e3*s)}else this.stateIdleChange()}stateBusyChange(){Ve.sessionId="",clearTimeout(Ve.autoStateTimer),he.updateCustomerInfo(void 0),this.apiClient._changeState({data:{state:Ae.BUSY,state_name:"忙碌"}})}initWebSocket(){if(1===this.ws?.readyState)return;const A=Rs(Fs,Ys);this.ws=new WebSocket(this.url+A),this.ws.onopen=()=>{const A=(new Date).getTime();this.sendMessage({type:0,msg_id:A}),this.pendingMessages.set(A,performance.now()),this.startHeartbeat(),this.manualClose=!1,this.onOpenCallback&&this.onOpenCallback()},this.ws.onmessage=async A=>{const e=JSON.parse(A.data),{type:t,data:s,code:i,msg:n,msg_id:o}=e;if(0===t&&o){const A=this.pendingMessages.get(o),e=performance.now()-A;Ve.latency=parseFloat(e.toFixed(2)),this.pendingMessages.delete(o)}if(0!==t&&Xs("%cWebSocket 收到消息:","color: blue; font-weight: bold;",e),t!==te.OUT_CALL&&t!==te.PREVIEW_OUT_CALL||(0!==i?(he.reset(),ae.emit(ie.OUT_FAILED,n),ae.emit(ne.FAIL_NOTIFICATION,n),he.updateActionConfigs(ft),setTimeout(()=>{he.updateCustomerInfo(void 0)},500),this.stateIdleChange()):0===i&&(ae.emit(ie.OUT_SUCCESS),clearTimeout(Ve.autoStateTimer))),t===te.AGENT_STATE){const{state:A,state_name:e}=s;Ve.stateObject={state:A,state_name:e},Ve.stateBeforeChange?.state||Ve.stateObject.state!==Ae.IDLE&&(Ve.stateObject.state!==Ae.BUSY||"整理"===e)||(Ve.stateBeforeChange=Ve.stateObject),"振铃中"===e&&he.updateDisplayText(oe.AGENT_RINGING),A===Ae.UNREGISTERED&&he.updateActionConfigs(Mt),A===Ae.IDLE&&Ve.stateObject.state===Ae.UNREGISTERED&&he.updateActionConfigs(ft),ae.emit(ie.AGENT_STATE,{state:A,state_name:e})}if(t===te.OUT_CALL_INCOMING_CALL){try{await this.getCustomerInfo(s.trace_id||s.customer_phone)}catch(A){console.error(A)}s.preview_task_id?ae.emit(ie.PREVIEW_OUT_INCOMING_CALL,{customer_phone:s.customer_phone,session_id:s.session_id,preview_task_id:s.preview_task_id,trace_id:s.trace_id}):ae.emit(ie.OUT_INCOMING_CALL,{customer_phone:s.customer_phone,session_id:s.session_id,trace_id:s.trace_id}),Ve.sessionId=s.session_id,he.updateActionConfigs(Qt)}if(t===te.OUT_CALL_END)if(he.updateDisplayText(""),1===s.device_type&&s.agent_no===Ve.agentInfo.agent_no)he.reset(),ae.emit(ie.OUT_HANGUP,{session_id:s.session_id}),he.updateActionConfigs(ft),this.putAgentState(!0);else if(2===s.device_type&&s.agent_no===Ve.agentInfo.agent_no)he.reset(),ae.emit(ie.OUR_SIDE_CONSULT_HANGUP,{session_id:s.session_id}),he.updateActionConfigs(ft),this.putAgentState(!0);else if(2===s.device_type&&s.agent_no!==Ve.agentInfo.agent_no){he.get("isCalling")&&(he.updateDisplayText(oe.CALLING),ae.emit(ie.OTHER_SIDE_CONSULT_HANGUP,{session_id:s.session_id}),he.updateActionConfigs(St))}else if(3===s.device_type&&s.agent_no===Ve.agentInfo.agent_no)he.reset(),ae.emit(ie.THREE_WAY_OUR_SIDE_HANGUP,{session_id:s.session_id}),he.updateActionConfigs(ft),this.putAgentState(!0);else if(4===s.device_type&&s.agent_no===Ve.agentInfo.agent_no)he.reset(),ae.emit(ie.TRANSFER_END,{session_id:s.session_id}),he.updateActionConfigs(ft),this.putAgentState(!0);else if(4===s.device_type&&s.agent_no!==Ve.agentInfo.agent_no){ae.emit(ie.TRANSFER_FAILED,"未知原因"),ae.emit(ne.FAIL_NOTIFICATION,"转接失败");const A=he.get("direction");A===ee.OUTGOING?he.updateActionConfigs(St):A===ee.INCOMING&&he.updateActionConfigs(Ut)}else 5===s.device_type&&s.agent_no===Ve.agentInfo.agent_no&&(Ve.monitoredAgentNo="",Ve.earSpeakAgent="",he.reset(),ae.emit(ie.LISTEN_HANGUP,{session_id:s.session_id}),he.updateActionConfigs(ft),this.putAgentState(!0,5));if(t===te.OUT_CALL_RINGING&&1===s.type&&(ae.emit(ie.OUT_RINGING,{session_id:Ve.sessionId,type:s.type}),he.updateDisplayText(oe.RINGING)),t===te.OUT_CALL_ANSWER&&(clearTimeout(Ve.autoStateTimer),he.updateIsCalling(!0),ae.emit(ie.OUT_OFF_ANSWER,{session_id:Ve.sessionId}),he.updateOutCallIsAnswer(!0),he.updateDisplayText(oe.CALLING),he.updateActionConfigs(St)),t===te.CONSULT_RINGING&&(ae.emit(ie.CONSULT_RINGING,{session_id:s.session_id}),he.updateDisplayText(oe.AGENT_RINGING),he.updateActionConfigs([]),clearTimeout(Ve.autoStateTimer)),t===te.CONSULT_ANSWER&&(ae.emit(ie.CONSULT_OFF_HOOK,{session_id:s.session_id}),he.updateConsultIsAnswer(!0),he.updateDisplayText(oe.CONSULTING),he.updateActionConfigs(Tt)),t===te.CONSULT_FAIL&&(ae.emit(ie.CONSULT_FAILED,{custom_reason:s.custom_reason}),he.updateDisplayText(oe.CALLING),he.updateActionConfigs(St)),t===te.CONSULT_CALL_IN&&(ae.emit(ie.CONSULT_INCOMING,{agent_no:s.agent_no,agent_mane:s.agent_name,session_id:s.session_id}),Ve.sessionId=s.session_id,he.updateIsCalling(!0),he.updateDirection(ee.CONSULT),he.updateDisplayText(oe.CONSULT_CALL_IN),he.updateActionConfigs(Dt),Ve.enableBrowserAlert&&Ps.show("📞 咨询来电",`坐席 [${s.agent_name}] 正在咨询...`,()=>{ae.emit(ne.NOTIFICATION_CLICK,{agent_mane:s.agent_name,agent_no:s.agent_no})},Ve.browserAlertTime)),t===te.CONSULT_CALL_IN_SUCCESS&&(ae.emit(ie.CONSULT_CALL_IN_SUCCESS,{session_id:s.session_id}),he.updateConsultIsAnswer(!0),he.updateDisplayText(oe.CALLING),he.updateActionConfigs(yt)),t===te.CONSULT_RETURN_CALL_SUCCESS&&("success"===s.status?(he.updateConsultIsAnswer(!1),ae.emit(ie.CONSULT_RETURN_CALL_SUCCESS),he.updateDisplayText(oe.CALLING),ae.emit(ie.OTHER_SIDE_CONSULT_HANGUP),he.updateActionConfigs(St)):ae.emit(ne.FAIL_NOTIFICATION,"媒体失败")),t===te.MUTE&&(0===i?(he.updateIsMuted(!0),ae.emit(ie.MUTE_SUCCESS),he.updateDisplayText(oe.MUTING)):(ae.emit(ie.MUTE_FAILED,n),ae.emit(ne.FAIL_NOTIFICATION,n))),t===te.UNMUTE&&(0===i?(he.updateIsMuted(!1),ae.emit(ie.UNMUTE_SUCCESS),he.updateDisplayText(oe.CALLING)):(ae.emit(ie.UNMUTE_FAILED,n),ae.emit(ne.FAIL_NOTIFICATION,n))),t===te.CALL_HOLD&&(0===i?(he.updateIsHold(!0),he.updateDisplayText(oe.HOLDING),ae.emit(ie.HOLD_SUCCESS)):(ae.emit(ie.HOLD_FAILED,n),ae.emit(ne.FAIL_NOTIFICATION,n))),t===te.CALL_UNHOLD&&(0===i?(he.updateDisplayText(oe.CALLING),he.updateIsHold(!1),ae.emit(ie.UNHOLD_SUCCESS)):(ae.emit(ie.UNHOLD_FAILED,n),ae.emit(ne.FAIL_NOTIFICATION,n))),t===te.CONSULT_TRANSFER&&(0===i?ae.emit(ie.CONSULT_TRANSFER_SUCCESS):(ae.emit(ie.CONSULT_TRANSFER_FAILED,n),ae.emit(ne.FAIL_NOTIFICATION,n))),t===te.CONSULT_TRANSFER_FAILED&&ae.emit(ie.CONSULT_TRANSFER_FAILED,s.status),t===te.CONSULT_TRANSFER_OFF_HOOK&&(he.updateDirection(ee.INCOMING),he.updateDisplayText(oe.CALLING),ae.emit(ie.CONSULT_TRANSFER_OFF_HOOK),he.updateActionConfigs(Ut),he.updateIncomingIsAnswer(!0),he.updateConsultIsAnswer(!1)),t===te.INCOMING_CALL){clearTimeout(Ve.autoStateTimer),Ve.sessionId=s.session_id;try{await this.getCustomerInfo(s.trace_id||s.customer_phone)}catch(A){console.error(A)}he.updateIsCalling(!0),he.updateDirection(ee.INCOMING),this.inCallAutoAnswer(),he.updateDisplayText(oe.INCOMING_CALL),1===s.direction&&ae.emit(ie.INCOMING_CALL,{customer_phone:s.customer_phone,trace_id:s.trace_id,session_id:s.session_id}),3===s.direction&&(ae.emit(ie.PREDICT_INCOMING_CALL,{customer_phone:s.customer_phone,trace_id:s.trace_id,session_id:s.session_id}),this.sipClient?.answerCall()),he.updateActionConfigs(Dt),Ve.enableBrowserAlert&&Ps.show("📞 客户来电",`客户 [${s.customer_phone}] 正在呼入...`,()=>{ae.emit(ne.NOTIFICATION_CLICK,{customer_phone:s.customer_phone})},Ve.browserAlertTime)}if(t===te.INCOMING_CALL_OFF_HOOK&&(he.updateIncomingIsAnswer(!0),ae.emit(ie.INCOMING_CALL_OFF_HOOK,{session_id:Ve.sessionId}),he.updateActionConfigs(Ut),he.updateDisplayText(oe.CALLING)),t===te.INCOMING_CALL_END)if(he.updateDisplayText(""),1===s.device_type&&s.agent_no===Ve.agentInfo.agent_no)this.putAgentState(!0),he.reset(),ae.emit(ie.INCOMING_CALL_END,{session_id:s.session_id}),he.updateActionConfigs(ft);else if(2===s.device_type&&s.agent_no===Ve.agentInfo.agent_no)he.reset(),ae.emit(ie.OUR_SIDE_CONSULT_HANGUP,{session_id:s.session_id}),this.putAgentState(!0),he.updateActionConfigs(ft);else if(2===s.device_type&&s.agent_no!==Ve.agentInfo.agent_no)he.updateDisplayText(oe.CALLING),ae.emit(ie.OTHER_SIDE_CONSULT_HANGUP,{session_id:s.session_id}),he.updateActionConfigs(St);else if(3===s.device_type&&s.agent_no===Ve.agentInfo.agent_no)this.putAgentState(!0),he.reset(),ae.emit(ie.THREE_WAY_OUR_SIDE_HANGUP,{session_id:s.session_id}),he.updateActionConfigs(ft);else if(4===s.device_type&&s.agent_no===Ve.agentInfo.agent_no)this.putAgentState(!0),he.reset(),ae.emit(ie.TRANSFER_END,{session_id:s.session_id}),he.updateActionConfigs(ft);else if(4===s.device_type&&s.agent_no!==Ve.agentInfo.agent_no){ae.emit(ie.TRANSFER_FAILED,"未知原因"),ae.emit(ne.FAIL_NOTIFICATION,"转接失败");const A=he.get("direction");A===ee.OUTGOING?he.updateActionConfigs(St):A===ee.INCOMING&&he.updateActionConfigs(Ut)}else 5===s.device_type&&s.agent_no===Ve.agentInfo.agent_no&&(this.putAgentState(!0,5),Ve.monitoredAgentNo="",Ve.earSpeakAgent="",he.reset(),ae.emit(ie.LISTEN_HANGUP,{session_id:s.session_id}),he.updateActionConfigs(ft));if(t===te.SATISFACTION_EVALUATION&&(0===i?ae.emit(ie.SATISFACTION_EVALUATION_SUCCESS):(ae.emit(ie.SATISFACTION_EVALUATION_FAILED,n),ae.emit(ne.FAIL_NOTIFICATION,n))),t===te.CONSULT_THREE_WAY&&(0!==i?(ae.emit(ie.CONSULT_THREE_WAY_FAILED,n),ae.emit(ne.FAIL_NOTIFICATION,n)):0===i&&he.updateIsMeeting(!0)),t===te.CONSULT_THREE_WAY_RESULT&&("success"===s.status?(he.updateDirection(ee.MEETING),he.updateDisplayText(oe.MEETING),he.updateActionConfigs(Rt),ae.emit(ie.CONSULT_THREE_WAY_SUCCESS)):(ae.emit(ie.CONSULT_THREE_WAY_FAILED,{conf_name:s.conf_name}),he.updateIsMeeting(!1))),t===te.TRANSFER_INCOMING_CALL&&(clearTimeout(Ve.autoStateTimer),s.agent_no===Ve.agentInfo.agent_no?(he.updateDisplayText(oe.TRANSFERRING),ae.emit(ie.TRANSFER_PROGRESS),he.updateActionConfigs([]),he.updateTransferIsAnswer(!0)):(Ve.sessionId=s.session_id,ae.emit(ie.TRANSFER_INCOMING_CALL,{agent_no:s.agent_no,agent_name:s.agent_name,session_id:s.session_id}),he.updateIsCalling(!0),he.updateDirection(ee.TRANSFER),he.updateDisplayText(oe.TRANSFER_INCOMING_CALL),he.updateActionConfigs(Dt),Ve.enableBrowserAlert&&Ps.show("📞 转接来电",`坐席 [${s.agent_no}] 正在转接...`,()=>{ae.emit(ne.NOTIFICATION_CLICK,{agent_no:s.agent_no})},Ve.browserAlertTime))),t===te.TRANSFER_OFF_HOOK&&(ae.emit(ie.TRANSFER_OFF_HOOK,{session_id:Ve.sessionId}),he.updateTransferIsAnswer(!1),he.updateDisplayText(oe.CALLING),he.updateActionConfigs(Ut)),t===te.TRANSFER_FAILED&&"failed"===s.status){ae.emit(ie.TRANSFER_FAILED,"未知原因"),ae.emit(ne.FAIL_NOTIFICATION,"转接失败");const A=he.get("direction");A===ee.OUTGOING?he.updateActionConfigs(St):A===ee.INCOMING&&he.updateActionConfigs(Ut)}t===te.KICK_OFFLINE&&ae.emit(ne.KICK_OFF),t===te.CONTINUOUS_NOT_ANSWER&&(ae.emit(ne.FAIL_NOTIFICATION,`已连续 ${s.miss_count} 次未接, 设置忙碌`),this.stateBusyChange()),t===te.SIGN_OUT&&ae.emit(ne.SIGN_OUT),t===te.SIGN_IN_SWITCH_DEVICE&&ae.emit(ne.SIGN_IN_SWITCH_DEVICE,s),t===te.LISTEN_INCOMING_CALL&&(clearTimeout(Ve.autoStateTimer),Ve.sessionId=s.session_id,he.updateIsCalling(!0),he.updateDirection(ee.INCOMING),this.inCallAutoAnswer(),he.updateDisplayText(oe.LISTENING),he.updateActionConfigs(Dt),ae.emit(ie.LISTEN_INCOMING_CALL,{session_id:s.session_id}),Ve.enableBrowserAlert&&Ps.show("📞 监听来电","正在来电...",()=>{ae.emit(ne.NOTIFICATION_CLICK,{customer_phone:s.customer_phone})},Ve.browserAlertTime)),t===te.LISTEN_INCOMING_CALL_OFF_HOOK&&(Ve.monitoredAgentNo=s.monitored_agent_no,Ve.earSpeakAgent="",he.updateIncomingIsAnswer(!0),ae.emit(ie.LISTEN_INCOMING_CALL_OFF_HOOK,{session_id:Ve.sessionId,monitored_agent_no:s.monitored_agent_no}),he.updateActionConfigs(kt),he.updateDisplayText(oe.CALLING)),t===te.LISTEN_CALL_FAILED&&"failed"===s.status&&(ae.emit(ie.LISTEN_CALL_FAILED),Ve.monitoredAgentNo=""),t===te.EAR_SPEECH&&("success"===s.status?(s.monitored_agent_no===Ve.agentInfo.agent_no&&(he.updateActionConfigs(Gt),ae.emit(ie.EAR_SPEECH_BE_SUCCESS)),s.monitor_agent_no===Ve.agentInfo.agent_no&&(he.updateActionConfigs(Ft),ae.emit(ie.EAR_SPEECH_SUCCESS,{monitored_agent_no:s.monitored_agent_no,monitor_agent_no:s.monitor_agent_no}),Ve.earSpeakAgent=s.monitored_agent_no)):ae.emit(ie.EAR_SPEECH_FAILED)),t===te.STOP_EAR_SPEECH&&("success"===s.status?(s.monitored_agent_no===Ve.agentInfo.agent_no&&he.updateActionConfigs(Ut),s.monitor_agent_no===Ve.agentInfo.agent_no&&he.updateActionConfigs(kt),Ve.earSpeakAgent="",ae.emit(ie.STOP_EAR_SPEECH_SUCCESS)):ae.emit(ie.STOP_EAR_SPEECH_FAILED)),t===te.CALL_FORCE_INSERT&&("success"===s.status?(s.monitored_agent_no===Ve.agentInfo.agent_no&&(he.updateActionConfigs(Zt),ae.emit(ie.CALL_FORCE_INSERT_SUCCESS,{monitored_agent_no:s.monitored_agent_no,monitor_agent_no:s.monitor_agent_no})),s.monitor_agent_no===Ve.agentInfo.agent_no&&(he.updateActionConfigs(Yt),ae.emit(ie.CALL_FORCE_INSERT_SUCCESS_LISTENER,{monitored_agent_no:s.monitored_agent_no,monitor_agent_no:s.monitor_agent_no}))):ae.emit(ie.CALL_FORCE_INSERT_FAILED))},this.ws.onclose=()=>{this.stopHeartbeat(),this.manualClose||setTimeout(()=>this.initWebSocket(),this.reconnectDelay)},this.ws.onerror=()=>{this.ws?.close()}}startHeartbeat(){const A=new Blob(["\n let timer;\n self.onmessage = function (e) {\n if (e.data === 'start') {\n timer = setInterval(() => {\n self.postMessage('heartbeat');\n }, 5000);\n } else if (e.data === 'stop') {\n clearInterval(timer);\n }\n };\n "],{type:"application/javascript"}),e=URL.createObjectURL(A);this.heartbeatWorker=new Worker(e),this.heartbeatWorker.onmessage=()=>{const A=(new Date).getTime();this.sendMessage({type:0,msg_id:A}),this.pendingMessages.set(A,performance.now())},this.heartbeatWorker.postMessage("start")}stopHeartbeat(){this.heartbeatWorker&&(this.heartbeatWorker.postMessage("stop"),this.heartbeatWorker.terminate(),this.heartbeatWorker=null)}sendMessage(A){this.ws?.readyState===WebSocket.OPEN?(this.ws.send(JSON.stringify(A)),0!==A.type&&Xs("%cWebSocket 发送消息:","color: green; font-weight: bold;",A)):console.warn("WebSocket 未连接,消息未发送")}close(){this.manualClose=!0,this.stopHeartbeat(),this.ws?.close()}reconnect(){this.initWebSocket()}static __internalClose(){_s&&(_s.close(),_s=null)}static __internalSend(A){if(!_s)throw new Error("WebSocket 尚未初始化");_s.sendMessage(A)}}class Ai{constructor(A){this.sipClient=null,this.sipClient=A}roleMute(){const A=he.get("isCalling"),e=Ve.sessionId;if(!A)throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}));if(!e)throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}))}holdCall(){this.roleMute();const A=he.get("isHold"),{sessionId:e}=Ve;if(!e)return;if(!he.get("actionConfigs").includes("hold"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿保持"}));if(A)throw new Error(JSON.stringify({code:1,msg:"当前已处于保持状态"}));$s.__internalSend({type:te.CALL_HOLD,data:{session_id:e}})}unholdCall(){this.roleMute();const{isHold:A}=he.getState(),{sessionId:e}=Ve;if(!he.get("actionConfigs").includes("unhold"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿取消保持"}));if(e){if(!A)throw new Error(JSON.stringify({code:1,msg:"当前未处于保持状态"}));$s.__internalSend({type:te.CALL_UNHOLD,data:{session_id:e}})}}sendDTMF(A){if(1!==he.get("answerDevice"))throw new Error(JSON.stringify({code:1,msg:"当前设备不支持此功能"}));if(!he.get("actionConfigs").includes("dtmf"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿发送按键"}));const{isCalling:e}=he.getState();if(!e)throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}));this.sipClient.sendDTMF(A)}cancelCall(){if(!he.get("isCalling"))throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}));const A=Ve.sessionId;ae.emit(ie.REJECT,{session_id:A}),$s.__internalSend({type:te.AGENT_HANGUP,data:{session_id:A}})}async consultCall(A){this.roleMute();const{isCalling:e,outCallIsAnswer:t,consultIsAnswer:s,incomingIsAnswer:i}=he.getState(),n=Ve.sessionId;if(!he.get("actionConfigs").includes("consult"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用咨询"}));if(s)throw new Error(JSON.stringify({code:1,msg:"当前正在咨询"}));if(!(e||t||i))throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}));if(!n)throw new Error(JSON.stringify({code:1,msg:"sessionId不存在"}));$s.__internalSend({type:te.CONSULT_REQUEST,data:{session_id:n,consultation_agent_no:A}})}async answerCall(){if(1!==he.get("answerDevice"))throw new Error(JSON.stringify({code:1,msg:"当前设备不支持此功能"}));await this.sipClient.answerCall()}makeCall(A){if("string"!=typeof A||""===A.trim())throw new Error(JSON.stringify({code:1,msg:"外呼号码非法"}));const e={};Ds(A)?e.trace_id=A:e.customer_phone=A.replace(/[\s\r\n-]+/g,"");const t=he.get("isCalling"),s=he.get("selectOutNumber")||"";if(t)throw new Error(JSON.stringify({code:1,msg:"正在呼叫中,请勿重复点击"}));he.updateIsCalling(!0),he.updateDirection(ee.OUTGOING),$s.__internalSend({type:te.OUT_CALL,data:{...e,caller:s.replace(/[\s\r\n-]+/g,"")}})}previewCall(A,e){const t={};Ds(A)?t.trace_id=A:t.customer_phone=A.replace(/[\s\r\n-]+/g,"");const s=he.get("isCalling"),i=he.get("selectOutNumber")||"";if(s)throw new Error(JSON.stringify({code:1,msg:"正在呼叫中,请勿重复点击"}));if("string"!=typeof A||""===A.trim())throw new Error(JSON.stringify({code:1,msg:"外呼号码非法"}));he.updateIsCalling(!0),he.updateDirection(ee.OUTGOING),$s.__internalSend({type:te.PREVIEW_OUT_CALL,data:{...t,caller:i,preview_task_id:e}})}async hangup(){if(!he.get("isCalling"))throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}));const A=Ve.sessionId;$s.__internalSend({type:te.AGENT_HANGUP,data:{session_id:A}})}mute(){this.roleMute();const A=he.get("actionConfigs"),e=he.get("isMuted"),t=Ve.sessionId;if(e)throw new Error(JSON.stringify({code:1,msg:"当前未处于静音状态"}));if(!A.includes("mute"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用静音"}));$s.__internalSend({type:te.MUTE,data:{session_id:t}})}unmute(){this.roleMute();const A=he.get("actionConfigs"),e=he.get("isMuted"),t=Ve.sessionId;if(!e)throw new Error(JSON.stringify({code:1,msg:"当前未处于未静音状态"}));if(!A.includes("unmute"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用静音"}));$s.__internalSend({type:te.UNMUTE,data:{session_id:t}})}consultTransfer(){this.roleMute();const A=he.get("consultIsAnswer"),e=Ve.sessionId;if(!he.get("actionConfigs").includes("consult_transfer"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用咨询转移"}));if(!A)throw new Error(JSON.stringify({code:1,msg:"当前未处于咨询中"}));$s.__internalSend({type:te.CONSULT_TRANSFER,data:{session_id:e}})}satisfactionEvaluation(){this.roleMute();const A=Ve.sessionId;if(!he.get("actionConfigs").includes("satisfaction"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用满意度评价"}));$s.__internalSend({type:te.SATISFACTION_EVALUATION,data:{session_id:A}})}startConference(){this.roleMute();if(!he.get("actionConfigs").includes("conference"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用三方会议"}));const A=Ve.sessionId;if(!he.get("consultIsAnswer"))throw new Error(JSON.stringify({code:1,msg:"当前未处于咨询中"}));$s.__internalSend({type:te.CONSULT_THREE_WAY,data:{session_id:A}})}consultRetrieve(){this.roleMute();if(!he.get("actionConfigs").includes("consult_retrieve"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿取回通话"}));const A=Ve.sessionId;if(!he.get("consultIsAnswer"))throw new Error(JSON.stringify({code:1,msg:"当前未处于咨询中"}));$s.__internalSend({type:te.CONSULT_RETURN_CALL,data:{session_id:A}})}async transferAgent(A){this.roleMute();if(!he.get("actionConfigs").includes("transfer"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿转接"}));const{transferIsAnswer:e}=he.getState(),t=Ve.sessionId;if(e)throw new Error(JSON.stringify({code:1,msg:"当前正在转接"}));$s.__internalSend({type:te.TRANSFER,data:{session_id:t,transfer_type:0,transfer_dest:A}})}async transferOutline(A){this.roleMute();if(!he.get("actionConfigs").includes("transfer"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿转接"}));if(!A)throw new Error(JSON.stringify({code:1,msg:"号码不能为空"}));if(!/^\d+$/.test(A))throw new Error(JSON.stringify({code:1,msg:"号码格式错误"}));const{transferIsAnswer:e}=he.getState(),t=Ve.sessionId;if(e)throw new Error(JSON.stringify({code:1,msg:"当前正在转接"}));if(e)throw new Error(JSON.stringify({code:1,msg:"当前正在转接"}));$s.__internalSend({type:te.TRANSFER,data:{session_id:t,transfer_type:2,transfer_dest:A}})}}var ei;class ti{static{this._instance=null}#A=null;#e=new Ks;#t;#s=null;#i=!1;#n;#o;#r=!1;static{this._timer=null}#a=null;constructor(){if(!ei._creatingInstance)throw new Error("请使用 YSYTAgentSdk.getInstance() 获取实例");clearInterval(this.#s),this.#s=setInterval(async()=>{const A=await this.#V();le.updateRttObject(A)},2e3),Ps.requestMediaPermissions(),this.#n=he.subscribeKey("answerDevice",async A=>{1===A?(await this.initWebRtc(),Ve.stateObject.state===Ae.UNREGISTERED&&await this.#e._changeState({data:{...Ve.stateBeforeChange,state_name:Ve.stateBeforeChange.state===Ae.IDLE?"空闲":"忙碌"}})):await this.destroyRtc()}),this.#o=he.subscribeKey("isNextState",async A=>{if(A){await this.#e._agentLogin(Zs);1===he.get("answerDevice")&&await this.#A.reconnect();let A="";const e=new URL(Gs).host;A=Gs.includes("https")?`wss://${e}`:`ws://${e}`;const{agent_no:t}=Ve.agentInfo;$s.getInstance({url:`${A}/ccs-ws/api/v1/ws/call/${t}?Authorization=`,sipClient:this.#A}),Ve.stateObject.state===Ae.OFFLINE&&setTimeout(()=>{this.#e._changeState({data:{...this.#a,state_name:this.#a.state===Ae.BUSY&&"整理"===this.#a.state_name?"忙碌":this.#a.state_name},success:()=>{he.updateDisplayText(""),ae.emit(ie.AGENT_STATE,this.#a)}})},10)}else this.#a=Ve.stateObject,$s.__internalClose(),he.updateDisplayText("离线")}),this.on(ne.KICK_OFF,()=>{this.destroy()}),window.addEventListener("beforeunload",A=>{A.returnValue="",this.destroy()}),window.addEventListener("online",this.#c),window.addEventListener("offline",this.#h)}#c=()=>{he.updateIsNextState(!0)};#h=()=>{he.updateIsNextState(!1)};async#l(){const A=he.get("isNextState");try{await Ns(),A||he.updateIsNextState(!0)}catch(e){A&&he.updateIsNextState(!1)}}#g(){ei._timer||(ei._timer=setInterval(()=>this.#l(),3e3))}static get instance(){return this._instance}static async destroyInstance(){this._instance&&(await this._instance.destroy(),this._instance=null)}static getInstance(A){if(!this._instance){if(!A)throw new Error("首次调用必须传入配置参数");const{url:e,appKey:t,appSecret:s,agentNo:i}=A;Gs=e||"https://aicc-api.yescloudy.com",Fs=t,Ys=s,Zs=i,ei._creatingInstance=!0,this._instance=new ei,delete ei._creatingInstance}return this._instance}getInit(){return this.#r}async initWebRtc(){if(this.#A)return;const A=Ve.agentInfo.sip_account_list.find(A=>1===A.device_type);if(!A)throw new Error(JSON.stringify({code:1,msg:"未找到 sip_account_list"}));{const e=A.sip_register_addr;this.#A=new ge({server:e.split(":")[0],user:A.sip_account,password:A.sip_password,webSocket:`wss://${e}`,callTimeout:3e4},this.#q),await this.#A.start()}}async init({actionNodeParams:A,enableBrowserAlert:e,logBuiltinEnabled:t,browserAlertTime:s,initialState:i,initDevice:n}){try{let o="";const r=new URL(Gs).host;o=Gs.includes("https")?`wss://${r}`:`ws://${r}`;const a=$s.getInstance({url:`${o}/ccs-ws/api/v1/ws/call/${Zs}?Authorization=`,sipClient:null,onOpen:async()=>{const{viewHtmlElement:o,rttHTML:r}=A||{};Ve.logBuiltinEnabled=t||!1,Ve.browserAlertTime=s||5e3,await this.#e._agentLogin(Zs);const{current_answer_device:V,current_state:c,out_display_number:h,out_display_select_switch:l}=Ve.agentInfo;(i||c)===Ae.IDLE?(he.updateActionConfigs(ft),Ve.stateObject={state:i||c,state_name:"空闲"},Ve.stateBeforeChange=Ve.stateObject):(i||c)===Ae.BUSY?(he.updateActionConfigs(Bt),Ve.stateObject={state:c,state_name:"忙碌"},Ve.stateBeforeChange=Ve.stateObject):(i||c)===Ae.UNREGISTERED&&(he.updateActionConfigs(Bt),Ve.stateObject={state:c,state_name:"未注册"}),1===l?he.updateSelectOutNumber(h):he.updateSelectOutNumber(""),he.updateAnswerDevice(n||V),1===(n||V)&&(await this.initWebRtc(),a.setSipClient(this.#A)),this.#t=new Ai(this.#A),A&&o&&(this.#i=!0,new Ws({container:o,rttHTML:r,statusParams:{state:Ve.stateObject.state,statusName:Ve.stateObject.state_name}},this,this.#t)),Ve.enableBrowserAlert=e,e&&Ps.requestPermission(),ae.emit(ne.AGENT_INIT_STATUS,!0),this.#r=!0,clearInterval(ei._timer),ei._timer=null,this.#g()}})}catch(A){this.#i&&Hs("初始化失败"),ae.emit(ne.AGENT_INIT_STATUS,!1),this.#r=!1}}#q=A=>{const{type:e,data:t}=A;ae.emit(e,t)};on(A,e){return ae.on(A,e),this}off(A,e){ae.off(A,e)}offAll(){ae.clearAllListeners()}async destroyRtc(){await(this.#A?.destroy()),this.#A=null}async destroy(){ei._instance&&(await this.destroyRtc(),clearInterval(this.#s),$s.__internalClose(),ae.emit(ne.DESTROY),this.offAll(),this.#n?.(),this.#o?.(),this.#i=!1,ei._instance=null,he.reset(),he.updateSelectOutNumber(""),clearInterval(ei._timer),ei._timer=null)}async#V(){return this.#A?this.#A.getNetworkStats():{}}get call_api(){const A=A=>(...e)=>{if(!this.#i)return A.apply(this,e);console.warn("当前未使用个人开发模式")};return{answerCall:A(()=>this.#t.answerCall()),makeCall:A=>this.#t.makeCall(A),previewMakeCall:(A,e)=>this.#t.previewCall(A,e),cancelCall:A(()=>this.#t.cancelCall()),hangup:A(()=>this.#t.hangup()),sendDTMF:A(A=>this.#t.sendDTMF(A)),holdCall:A(()=>this.#t.holdCall()),unholdCall:A(()=>this.#t.unholdCall()),consultCall:A(async A=>{try{await this.#t.consultCall(A)}catch(A){throw console.warn(A),A}}),consultRetrieve:A(()=>this.#t.consultRetrieve()),consultTransfer:A(()=>this.#t.consultTransfer()),mute:A(()=>this.#t.mute()),unmute:A(()=>this.#t.unmute()),satisfactionEvaluation:A(()=>this.#t.satisfactionEvaluation()),startConference:A(()=>this.#t.startConference()),transferAgent:A(A=>this.#t.transferAgent(A)),transferOutline:A(A=>this.#t.transferOutline(A)),earSpeakStart:A(A=>this.#e.earCall(A)),earSpeakStop:A(A=>this.#e.stopEarCall(A)),forceInsert:A(A=>this.#e.forceInsert(A)),forceDisconnect:A(A=>this.#e.forceDisconnect(A))}}get agent_api(){const A=A=>(...e)=>{if(!this.#i)return A.apply(this,e);console.warn("当前未使用个人开发模式")};return{changeDevice:A(A=>this.#e.changeDevice(A)),changeState:A(A=>this.#e.changeState(A)),getIdleAgentList:A(A=>this.#e.getIdleAgentList(A)),getAgentState:A(A=>this.#e.getAgentState(A)),setAgentExplicit:A(A=>this.#e.setAgentExplicit(A)),checkAgentLogin:A=>this.#e.checkAgentLogin(A),getCallRecord:A=>this.#e.getCallRecord(A),agentLogout:async(A,e)=>{try{await this.#e.logout(A),await this.destroy(),e?.({code:0,msg:""})}catch(A){}},listenAgentCall:A=>this.#e.listenCall(A)}}getAgentData(){const{agent_no:A,agent_name:e,answer_devices:t,out_display_data:s,out_display_select_switch:i,current_state:n}=Ve.agentInfo,{stateObject:o}=Ve,r={1:"软电话",2:"手机模式",3:"SIP话机"};return{agent_no:A,agent_name:e,init_state:n,default_device:he.get("answerDevice"),out_display_data:s,out_display_select_switch:i,answer_devices:(t||"").split(",").map(A=>({value:Number(A),label:r[Number(A)]})),stateObject:o}}async getRealTimeCustomerInfo(){return he.get("customerObject")}setBrowserAlert(A){if("boolean"!=typeof A)throw new Error(JSON.stringify({code:1,msg:"参数类型错误"}));Ve.logBuiltinEnabled=A||!1}setBrowserAlertTime(A){if("number"!=typeof A)throw new Error(JSON.stringify({code:1,msg:"参数类型错误"}));Ve.browserAlertTime=A||3e3}}ei=ti,import.meta.hot&&import.meta.hot.dispose(()=>{window.location.reload()});export{Ae as AgentStatus,ie as CallSipCallEventEnum,ee as DirectionEnum,oe as DisplayTextEnum,ne as EventName,se as WebRtcCallSipEventEnum,te as WebsocketTypeEnum,ti as default};
|