ysyt-agent-sdk 1.0.57 → 1.0.58

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.
@@ -873,4 +873,4 @@ const ms=A=>(e,t)=>{void 0!==t?t.addInitializer(()=>{customElements.define(A,e)}
873
873
  `:""}
874
874
  </div>
875
875
  </my-popover>
876
- `;gt(s,this.rttHTML)}}const vs=new class{constructor(){this.events={}}on(A,e){this.events[A]=this.events[A]||[],this.events[A].push(e)}off(A,e){this.events[A]=(this.events[A]||[]).filter(A=>A!==e)}emit(A,...e){(this.events[A]||[]).forEach(A=>A(...e))}};class Ks{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 console.log("已授权麦克风权限"),null;const e=await navigator.mediaDevices.getUserMedia({audio:!0});return console.log("获取音频权限成功"),e}catch(A){return console.warn("获取音频权限失败:",A),null}}static show(A,e,t){if("Notification"in window&&"granted"===Notification.permission){const s=new Notification(A,{body:e,tag:"xy-global-notification",renotify:!0});s.onclick=()=>{window.focus(),s.close(),t?.()},setTimeout(()=>s.close(),5e3)}}static async checkMediaPermissions(){try{return(await navigator.mediaDevices.enumerateDevices()).some(A=>"audioinput"===A.kind&&""!==A.label)}catch(A){return console.warn("无法检查设备权限:",A),!1}}}let Os=null;class Ns{constructor(A){this.url=A,this.ws=null,this.heartbeatWorker=null,this.reconnectDelay=3e3,this.manualClose=!1,this.apiClient=new Us,this.initWebSocket()}static getInstance(A){if(!Os){if(!A)throw new Error("WebSocket 尚未初始化");Os=new Ns(A)}return Os}stateIdleChange(){const{state:A}=St.stateObject;A!==exports.AgentStatus.IDLE&&this.apiClient._changeState({data:{state:exports.AgentStatus.IDLE,state_name:"空闲"}})}putAgentState(){St.sessionId="";const{post_call_process_time:A}=St.agentInfo,e=A||0;St.autoStateTimer&&(clearTimeout(St.autoStateTimer),St.autoStateTimer=null),e>0?St.autoStateTimer=setTimeout(()=>{St.autoStateTimer=null,this.stateIdleChange()},e):this.stateIdleChange()}initWebSocket(){this.ws=new WebSocket(this.url),this.ws.onopen=()=>{this.sendMessage({type:0}),this.startHeartbeat()},this.ws.onmessage=A=>{const e=JSON.parse(A.data),{type:t,data:s,code:i,msg:r}=e;if(0!==t&&console.warn(t),t===exports.WebsocketTypeEnum.OUT_CALL&&(0!==i?(Tt.reset(),ae.emit(exports.CallSipCallEvent.OUT_FAILED,r),this.stateIdleChange()):0===i&&ae.emit(exports.CallSipCallEvent.OUT_SUCCESS)),t===exports.WebsocketTypeEnum.AGENT_STATE){const{state:A,state_name:e}=s;St.stateObject={state:A,state_name:e},"振铃中"===e&&Tt.updateDisplayText(exports.DisplayTextEnum.AGENT_RINGING),ae.emit(exports.CallSipCallEvent.AGENT_STATE,{state:A,state_name:e})}if(t===exports.WebsocketTypeEnum.OUT_CALL_INCOMING_CALL&&(ae.emit(exports.CallSipCallEvent.OUT_INCOMING_CALL),St.sessionId=s.session_id,Tt.updateActionConfigs(Et)),t===exports.WebsocketTypeEnum.OUT_CALL_END&&(Tt.updateDisplayText(""),1===s.device_type&&s.agent_no===St.agentInfo.agent_no?(this.putAgentState(),Tt.reset(),ae.emit(exports.CallSipCallEvent.OUT_HANGUP,s),Tt.updateActionConfigs(mt)):2===s.device_type&&s.agent_no===St.agentInfo.agent_no?(Tt.reset(),ae.emit(exports.CallSipCallEvent.OUR_SIDE_CONSULT_HANGUP,s),this.putAgentState(),Tt.updateActionConfigs(mt)):2===s.device_type&&s.agent_no!==St.agentInfo.agent_no?(Tt.updateDisplayText(exports.DisplayTextEnum.CALLING),ae.emit(exports.CallSipCallEvent.OTHER_SIDE_CONSULT_HANGUP,s),Tt.updateActionConfigs(It)):3===s.device_type&&s.agent_no===St.agentInfo.agent_no?(this.putAgentState(),Tt.reset(),ae.emit(exports.CallSipCallEvent.THREE_WAY_OUR_SIDE_HANGUP),Tt.updateActionConfigs(mt)):4===s.device_type&&s.agent_no===St.agentInfo.agent_no&&(this.putAgentState(),Tt.reset(),ae.emit(exports.CallSipCallEvent.TRANSFER_END),Tt.updateActionConfigs(mt))),t===exports.WebsocketTypeEnum.OUT_CALL_RINGING&&1===s.type&&(ae.emit(exports.CallSipCallEvent.OUT_RINGING,s),Tt.updateDisplayText(exports.DisplayTextEnum.RINGING)),t===exports.WebsocketTypeEnum.OUT_CALL_ANSWER&&(ae.emit(exports.CallSipCallEvent.OUT_OFF_ANSWER,s),Tt.updateOutCallIsAnswer(!0),Tt.updateDisplayText(exports.DisplayTextEnum.CALLING),Tt.updateActionConfigs(It)),t===exports.WebsocketTypeEnum.CONSULT_RINGING&&(ae.emit(exports.CallSipCallEvent.CONSULT_RINGING),Tt.updateDisplayText(exports.DisplayTextEnum.AGENT_RINGING),Tt.updateActionConfigs([])),t===exports.WebsocketTypeEnum.CONSULT_ANSWER&&(ae.emit(exports.CallSipCallEvent.CONSULT_OFF_HOOK,s),Tt.updateConsultIsAnswer(!0),Tt.updateDisplayText(exports.DisplayTextEnum.CONSULTING),Tt.updateActionConfigs(Mt)),t===exports.WebsocketTypeEnum.CONSULT_FAIL&&(ae.emit(exports.CallSipCallEvent.CONSULT_FAILED,{custom_reason:s.custom_reason}),Tt.updateDisplayText(exports.DisplayTextEnum.CALLING),Tt.updateActionConfigs(It)),t===exports.WebsocketTypeEnum.CONSULT_CALL_IN&&(ae.emit(exports.CallSipCallEvent.CONSULT_INCOMING,s),Tt.updateIsCalling(!0),Tt.updateDirection(exports.DirectionEnum.CONSULT),Tt.updateDisplayText(exports.DisplayTextEnum.CONSULT_CALL_IN),Tt.updateActionConfigs(Bt),St.enableBrowserAlert&&Ks.show("📞 咨询来电",`坐席 [${s.agent_name}] 正在咨询...`,()=>{ae.emit(exports.EventName.NOTIFICATION_CLICK,{agent_mane:s.agent_name})})),t===exports.WebsocketTypeEnum.CONSULT_CALL_IN_SUCCESS&&(St.sessionId=s.session_id,ae.emit(exports.CallSipCallEvent.CONSULT_CALL_IN_SUCCESS),Tt.updateConsultIsAnswer(!0),Tt.updateDisplayText(exports.DisplayTextEnum.CALLING),Tt.updateActionConfigs(wt)),t===exports.WebsocketTypeEnum.CONSULT_RETURN_CALL_SUCCESS&&(ae.emit(exports.CallSipCallEvent.CONSULT_RETURN_CALL_SUCCESS),Tt.updateDisplayText(exports.DisplayTextEnum.CALLING),ae.emit(exports.CallSipCallEvent.OTHER_SIDE_CONSULT_HANGUP,s),Tt.updateActionConfigs(It)),t===exports.WebsocketTypeEnum.MUTE&&(0===i?(Tt.updateIsMuted(!0),ae.emit(exports.CallSipCallEvent.MUTE_SUCCESS),Tt.updateDisplayText(exports.DisplayTextEnum.MUTING)):ae.emit(exports.CallSipCallEvent.MUTE_FAILED,r)),t===exports.WebsocketTypeEnum.UNMUTE&&(0===i?(Tt.updateIsMuted(!1),ae.emit(exports.CallSipCallEvent.UNMUTE_SUCCESS),Tt.updateDisplayText(exports.DisplayTextEnum.CALLING)):ae.emit(exports.CallSipCallEvent.UNMUTE_FAILED,r)),t===exports.WebsocketTypeEnum.CALL_HOLD&&(0===i?(Tt.updateIsHold(!0),Tt.updateDisplayText(exports.DisplayTextEnum.HOLDING),ae.emit(exports.CallSipCallEvent.HOLD_SUCCESS)):ae.emit(exports.CallSipCallEvent.HOLD_FAILED,r)),t===exports.WebsocketTypeEnum.CALL_UNHOLD&&(0===i?(Tt.updateDisplayText(exports.DisplayTextEnum.CALLING),Tt.updateIsHold(!1),ae.emit(exports.CallSipCallEvent.UNHOLD_SUCCESS)):ae.emit(exports.CallSipCallEvent.UNHOLD_FAILED,r)),t===exports.WebsocketTypeEnum.CONSULT_TRANSFER&&(0===i?ae.emit(exports.CallSipCallEvent.CONSULT_TRANSFER_SUCCESS):ae.emit(exports.CallSipCallEvent.CONSULT_TRANSFER_FAILED,r)),t===exports.WebsocketTypeEnum.CONSULT_TRANSFER_FAILED&&ae.emit(exports.CallSipCallEvent.CONSULT_TRANSFER_FAILED,s.status),t===exports.WebsocketTypeEnum.CONSULT_TRANSFER_OFF_HOOK&&(Tt.updateDirection(exports.DirectionEnum.INCOMING),Tt.updateDisplayText(exports.DisplayTextEnum.CALLING),ae.emit(exports.CallSipCallEvent.CONSULT_TRANSFER_OFF_HOOK),Tt.updateActionConfigs(Qt),Tt.updateIncomingIsAnswer(!0),Tt.updateConsultIsAnswer(!1)),t===exports.WebsocketTypeEnum.INCOMING_CALL&&(St.sessionId=s.session_id,Tt.updateIsCalling(!0),Tt.updateDirection(exports.DirectionEnum.INCOMING),Tt.updateDisplayText(exports.DisplayTextEnum.INCOMING_CALL),ae.emit(exports.CallSipCallEvent.INCOMING_CALL,{customer_phone:s.customer_phone}),Tt.updateActionConfigs(Bt),St.enableBrowserAlert&&Ks.show("📞 客户来电",`客户 [${s.customer_phone}] 正在呼入...`,()=>{ae.emit(exports.EventName.NOTIFICATION_CLICK,{customer_phone:s.customer_phone})})),t===exports.WebsocketTypeEnum.INCOMING_CALL_OFF_HOOK&&(Tt.updateIncomingIsAnswer(!0),ae.emit(exports.CallSipCallEvent.INCOMING_CALL_OFF_HOOK),Tt.updateActionConfigs(Qt),Tt.updateDisplayText(exports.DisplayTextEnum.CALLING)),t===exports.WebsocketTypeEnum.INCOMING_CALL_END&&(Tt.updateDisplayText(""),1===s.device_type&&s.agent_no===St.agentInfo.agent_no?(this.putAgentState(),Tt.reset(),ae.emit(exports.CallSipCallEvent.INCOMING_CALL_END),Tt.updateActionConfigs(mt)):2===s.device_type&&s.agent_no===St.agentInfo.agent_no?(Tt.reset(),ae.emit(exports.CallSipCallEvent.OUR_SIDE_CONSULT_HANGUP,s),this.putAgentState(),Tt.updateActionConfigs(mt)):2===s.device_type&&s.agent_no!==St.agentInfo.agent_no?(Tt.updateDisplayText(exports.DisplayTextEnum.CALLING),ae.emit(exports.CallSipCallEvent.OTHER_SIDE_CONSULT_HANGUP,s),Tt.updateActionConfigs(It)):3===s.device_type&&s.agent_no===St.agentInfo.agent_no?(this.putAgentState(),Tt.reset(),ae.emit(exports.CallSipCallEvent.THREE_WAY_OUR_SIDE_HANGUP),Tt.updateActionConfigs(mt)):4===s.device_type&&s.agent_no===St.agentInfo.agent_no&&(this.putAgentState(),Tt.reset(),ae.emit(exports.CallSipCallEvent.TRANSFER_END),Tt.updateActionConfigs(mt))),t===exports.WebsocketTypeEnum.SATISFACTION_EVALUATION&&(0===i?ae.emit(exports.CallSipCallEvent.SATISFACTION_EVALUATION_SUCCESS):ae.emit(exports.CallSipCallEvent.SATISFACTION_EVALUATION_FAILED,r)),t===exports.WebsocketTypeEnum.CONSULT_THREE_WAY&&(0!==i?ae.emit(exports.CallSipCallEvent.CONSULT_THREE_WAY_FAILED,r):0===i&&Tt.updateIsMeeting(!0)),t===exports.WebsocketTypeEnum.CONSULT_THREE_WAY_RESULT&&("success"===s.status?(Tt.updateDirection(exports.DirectionEnum.MEETING),Tt.updateDisplayText(exports.DisplayTextEnum.MEETING),Tt.updateActionConfigs(ft),ae.emit(exports.CallSipCallEvent.CONSULT_THREE_WAY_SUCCESS)):(ae.emit(exports.CallSipCallEvent.CONSULT_THREE_WAY_FAILED,{conf_name:s.conf_name}),Tt.updateIsMeeting(!1))),t===exports.WebsocketTypeEnum.TRANSFER_INCOMING_CALL&&(s.agent_no===St.agentInfo.agent_no?(Tt.updateDisplayText(exports.DisplayTextEnum.TRANSFERRING),ae.emit(exports.CallSipCallEvent.TRANSFER_PROGRESS),Tt.updateActionConfigs([]),Tt.updateTransferIsAnswer(!0)):(ae.emit(exports.CallSipCallEvent.TRANSFER_INCOMING_CALL,s),Tt.updateIsCalling(!0),Tt.updateDirection(exports.DirectionEnum.TRANSFER),Tt.updateDisplayText(exports.DisplayTextEnum.TRANSFER_INCOMING_CALL),Tt.updateActionConfigs(Bt),St.enableBrowserAlert&&Ks.show("📞 转接来电",`坐席 [${s.agent_no}] 正在转接...`,()=>{ae.emit(exports.EventName.NOTIFICATION_CLICK,{agent_mane:s.agent_no})}))),t===exports.WebsocketTypeEnum.TRANSFER_OFF_HOOK&&(St.sessionId=s.session_id,ae.emit(exports.CallSipCallEvent.TRANSFER_OFF_HOOK),Tt.updateTransferIsAnswer(!0),Tt.updateDisplayText(exports.DisplayTextEnum.CALLING),Tt.updateActionConfigs(Qt)),t===exports.WebsocketTypeEnum.TRANSFER_FAILED&&"failed"===s.status){ae.emit(exports.CallSipCallEvent.TRANSFER_FAILED,"未知原因");const A=Tt.get("direction");A===exports.DirectionEnum.OUTGOING?Tt.updateActionConfigs(It):A===exports.DirectionEnum.INCOMING&&Tt.updateActionConfigs(Qt)}0!==t&&vs.emit("ws-message",e)},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 }, 14000);\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=()=>{this.sendMessage({type:0})},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)):console.warn("WebSocket 未连接,消息未发送")}close(){this.manualClose=!0,this.stopHeartbeat(),this.ws?.close()}static __internalClose(){Os&&(Os.close(),Os=null)}static __internalSend(A){if(!Os)throw new Error("WebSocket 尚未初始化");Os.sendMessage(A)}}class bs{constructor(A){this.sipClient=null,this.sipClient=A}roleMute(){const A=Tt.get("isCalling"),e=St.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=Tt.get("isHold"),{sessionId:e}=St;if(!e)return;if(!Tt.get("actionConfigs").includes("hold"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿保持"}));if(A)throw new Error(JSON.stringify({code:1,msg:"当前已处于保持状态"}));Ns.__internalSend({type:exports.WebsocketTypeEnum.CALL_HOLD,data:{session_id:e}})}unholdCall(){this.roleMute();const{isHold:A}=Tt.getState(),{sessionId:e}=St;if(!Tt.get("actionConfigs").includes("unhold"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿取消保持"}));if(e){if(!A)throw new Error(JSON.stringify({code:1,msg:"当前未处于保持状态"}));Ns.__internalSend({type:exports.WebsocketTypeEnum.CALL_UNHOLD,data:{session_id:e}})}}sendDTMF(A){if(1!==Tt.get("answerDevice"))throw new Error(JSON.stringify({code:1,msg:"当前设备不支持此功能"}));if(!Tt.get("actionConfigs").includes("dtmf"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿发送按键"}));const{isCalling:e}=Tt.getState();if(!e)throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}));this.sipClient.sendDTMF(A)}cancelCall(){if(1!==Tt.get("answerDevice"))throw new Error(JSON.stringify({code:1,msg:"当前设备不支持此功能"}));if(!Tt.get("actionConfigs").includes("reject"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用拒接"}));const{isCalling:A,direction:e,outCallIsAnswer:t}=Tt.getState();if(!A)throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}));t||(e===exports.DirectionEnum.INCOMING?this.rejectInCall():e===exports.DirectionEnum.OUTGOING&&this.rejectOutCall())}async consultCall(A){this.roleMute();const{isCalling:e,outCallIsAnswer:t,consultIsAnswer:s,incomingIsAnswer:i}=Tt.getState(),r=St.sessionId;if(!Tt.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(!r)throw new Error(JSON.stringify({code:1,msg:"sessionId不存在"}));Ns.__internalSend({type:exports.WebsocketTypeEnum.CONSULT_REQUEST,data:{session_id:r,consultation_agent_no:A}})}async answerCall(){if(1!==Tt.get("answerDevice"))throw new Error(JSON.stringify({code:1,msg:"当前设备不支持此功能"}));await this.sipClient.answerCall()}async rejectInCall(){if(1!==Tt.get("answerDevice"))throw new Error(JSON.stringify({code:1,msg:"当前设备不支持此功能"}));const{isCalling:A}=Tt.getState();if(!A)throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}));await this.sipClient.rejectInCall()}async makeCall(A){if(Tt.getState().isCalling)throw new Error(JSON.stringify({code:1,msg:"正在呼叫中,请勿重复点击"}));Tt.updateIsCalling(!0),Tt.updateDirection(exports.DirectionEnum.OUTGOING),Ns.__internalSend({type:exports.WebsocketTypeEnum.OUT_CALL,data:{customer_phone:A}})}async rejectOutCall(){const{isCalling:A}=Tt.getState();if(1!==Tt.get("answerDevice"))throw new Error(JSON.stringify({code:1,msg:"当前设备不支持此功能"}));if(!A)throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}));await this.sipClient.rejectOutCall()}async hangup(){if(1!==Tt.get("answerDevice"))throw new Error(JSON.stringify({code:1,msg:"当前设备不支持此功能"}));if(!Tt.get("isCalling"))throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}));await this.sipClient.hangup()}mute(){this.roleMute();const A=Tt.get("actionConfigs"),e=Tt.get("isMuted"),t=St.sessionId;if(e)throw new Error(JSON.stringify({code:1,msg:"当前未处于静音状态"}));if(!A.includes("mute"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用静音"}));Ns.__internalSend({type:exports.WebsocketTypeEnum.MUTE,data:{session_id:t}})}unmute(){this.roleMute();const A=Tt.get("actionConfigs"),e=Tt.get("isMuted"),t=St.sessionId;if(!e)throw new Error(JSON.stringify({code:1,msg:"当前未处于未静音状态"}));if(!A.includes("unmute"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用静音"}));Ns.__internalSend({type:exports.WebsocketTypeEnum.UNMUTE,data:{session_id:t}})}consultTransfer(){this.roleMute();const A=Tt.get("consultIsAnswer"),e=St.sessionId;if(!Tt.get("actionConfigs").includes("consult_transfer"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用咨询转移"}));if(!A)throw new Error(JSON.stringify({code:1,msg:"当前未处于咨询中"}));Ns.__internalSend({type:exports.WebsocketTypeEnum.CONSULT_TRANSFER,data:{session_id:e}})}satisfactionEvaluation(){this.roleMute();const A=St.sessionId;if(!Tt.get("actionConfigs").includes("satisfaction"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用满意度评价"}));Ns.__internalSend({type:exports.WebsocketTypeEnum.SATISFACTION_EVALUATION,data:{session_id:A}})}startConference(){this.roleMute();if(!Tt.get("actionConfigs").includes("conference"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用三方会议"}));const A=St.sessionId;if(!Tt.get("consultIsAnswer"))throw new Error(JSON.stringify({code:1,msg:"当前未处于咨询中"}));Ns.__internalSend({type:exports.WebsocketTypeEnum.CONSULT_THREE_WAY,data:{session_id:A}})}consultRetrieve(){this.roleMute();if(!Tt.get("actionConfigs").includes("consult_retrieve"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿取回通话"}));const A=St.sessionId;if(!Tt.get("consultIsAnswer"))throw new Error(JSON.stringify({code:1,msg:"当前未处于咨询中"}));Ns.__internalSend({type:exports.WebsocketTypeEnum.CONSULT_RETURN_CALL,data:{session_id:A}})}async transferAgent(A){this.roleMute();if(!Tt.get("actionConfigs").includes("transfer"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿转接"}));const{transferIsAnswer:e}=Tt.getState(),t=St.sessionId;if(e)throw new Error(JSON.stringify({code:1,msg:"当前正在转接"}));Ns.__internalSend({type:exports.WebsocketTypeEnum.TRANSFER,data:{session_id:t,transfer_type:0,transfer_dest:A}})}async transferOutline(A){this.roleMute();if(!Tt.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}=Tt.getState(),t=St.sessionId;if(e)throw new Error(JSON.stringify({code:1,msg:"当前正在转接"}));if(e)throw new Error(JSON.stringify({code:1,msg:"当前正在转接"}));Ns.__internalSend({type:exports.WebsocketTypeEnum.TRANSFER,data:{session_id:t,transfer_type:2,transfer_dest:A}})}}exports.default=class{#A=null;#e=new Us;#t;#s=null;#i=!1;#r;constructor(){clearInterval(this.#s),this.#s=setInterval(async()=>{const A=await this.#o();ds.updateRttObject(A)},2e3),Ks.requestMediaPermissions(),window.addEventListener("beforeunload",()=>{this.destroy()}),this.#r=Tt.subscribeKey("answerDevice",A=>{1===A&&this.initWebRtc()})}async initWebRtc(){const A=St.agentInfo.sip_account_list.find(A=>1===A.device_type);if(A){const e=A.sip_register_addr;this.#A=new ps({server:e.split(":")[0],user:A.sip_account,password:A.sip_password,webSocket:`wss://${e}`,callTimeout:3e4},this.#n),await this.#A.start()}}async init({actionNodeParams:A,url:e,appKey:t,appSecret:s,agentNo:i,enableBrowserAlert:r}){try{const{viewHtmlElement:o,rttHTML:n}=A||{},a=e||"https://aicc.yescloudy.com";cs=a,hs=t,ls=s;const V=ns(t,s);await this.#e._agentLogin(i);const{agent_no:c,current_answer_device:h,init_state:l}=St.agentInfo;l===exports.AgentStatus.IDLE?(Tt.updateActionConfigs(mt),St.stateObject={state:l,state_name:"空闲"}):l===exports.AgentStatus.BUSY&&(Tt.updateActionConfigs(Ct),St.stateObject={state:l,state_name:"忙碌"});let g="";const q=new URL(a).host;g=a.includes("https")?`wss://${q}`:`ws://${q}`,Ns.getInstance(`${g}/ccs-ws/api/v1/ws/call/${c}?Authorization=${V}`),Tt.updateAnswerDevice(h),1===h&&await this.initWebRtc(),this.#t=new bs(this.#A),A&&o&&(this.#i=!0,new Zs({container:o,rttHTML:n,statusParams:{state:exports.AgentStatus.IDLE,statusName:"空闲"}},this,this.#t)),St.enableBrowserAlert=r,r&&Ks.requestPermission()}catch(A){this.#i&&Fs("初始化失败"),console.log(A)}}#n=A=>{const{type:e,data:t}=A;ae.emit(e,t)};#a=A=>{};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())}destroy(){this.destroyRtc(),clearInterval(this.#s),vs.off("ws-message",this.#a),Ns.__internalClose(),this.offAll(),this.#r?.(),this.#A=null,this.#e=null,this.#i=!1}async#o(){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(A=>this.#t.makeCall(A)),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))}}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))}}getAgentData(){const{agent_no:A,agent_name:e,answer_devices:t,init_state:s,default_device:i}=St.agentInfo,{stateObject:r}=St,o={1:"软电话",2:"手机模式",3:"SIP话机"};return{agent_no:A,agent_name:e,init_state:s,default_device:i,answer_devices:(t||"").split(",").map(A=>({value:Number(A),label:o[Number(A)]})),stateObject:r}}};
876
+ `;gt(s,this.rttHTML)}}const vs=new class{constructor(){this.events={}}on(A,e){this.events[A]=this.events[A]||[],this.events[A].push(e)}off(A,e){this.events[A]=(this.events[A]||[]).filter(A=>A!==e)}emit(A,...e){(this.events[A]||[]).forEach(A=>A(...e))}};class Ks{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 console.log("已授权麦克风权限"),null;const e=await navigator.mediaDevices.getUserMedia({audio:!0});return console.log("获取音频权限成功"),e}catch(A){return console.warn("获取音频权限失败:",A),null}}static show(A,e,t){if("Notification"in window&&"granted"===Notification.permission){const s=new Notification(A,{body:e,tag:"xy-global-notification",renotify:!0});s.onclick=()=>{window.focus(),s.close(),t?.()},setTimeout(()=>s.close(),5e3)}}static async checkMediaPermissions(){try{return(await navigator.mediaDevices.enumerateDevices()).some(A=>"audioinput"===A.kind&&""!==A.label)}catch(A){return console.warn("无法检查设备权限:",A),!1}}}let Os=null;class Ns{constructor(A){this.url=A,this.ws=null,this.heartbeatWorker=null,this.reconnectDelay=3e3,this.manualClose=!1,this.apiClient=new Us,this.initWebSocket()}static getInstance(A){if(!Os){if(!A)throw new Error("WebSocket 尚未初始化");Os=new Ns(A)}return Os}stateIdleChange(){const{state:A}=St.stateObject;A!==exports.AgentStatus.IDLE&&this.apiClient._changeState({data:{state:exports.AgentStatus.IDLE,state_name:"空闲"}})}putAgentState(){St.sessionId="";const{post_call_process_time:A}=St.agentInfo,e=A||0;St.autoStateTimer&&(clearTimeout(St.autoStateTimer),St.autoStateTimer=null),e>0?St.autoStateTimer=setTimeout(()=>{St.autoStateTimer=null,this.stateIdleChange()},e):this.stateIdleChange()}initWebSocket(){this.ws=new WebSocket(this.url),this.ws.onopen=()=>{this.sendMessage({type:0}),this.startHeartbeat()},this.ws.onmessage=A=>{const e=JSON.parse(A.data),{type:t,data:s,code:i,msg:r}=e;if(0!==t&&console.warn(t),t===exports.WebsocketTypeEnum.OUT_CALL&&(0!==i?(Tt.reset(),ae.emit(exports.CallSipCallEvent.OUT_FAILED,r),this.stateIdleChange()):0===i&&ae.emit(exports.CallSipCallEvent.OUT_SUCCESS)),t===exports.WebsocketTypeEnum.AGENT_STATE){const{state:A,state_name:e}=s;St.stateObject={state:A,state_name:e},"振铃中"===e&&Tt.updateDisplayText(exports.DisplayTextEnum.AGENT_RINGING),ae.emit(exports.CallSipCallEvent.AGENT_STATE,{state:A,state_name:e})}if(t===exports.WebsocketTypeEnum.OUT_CALL_INCOMING_CALL&&(ae.emit(exports.CallSipCallEvent.OUT_INCOMING_CALL),St.sessionId=s.session_id,Tt.updateActionConfigs(Et)),t===exports.WebsocketTypeEnum.OUT_CALL_END&&(1===s.device_type&&s.agent_no===St.agentInfo.agent_no?(this.putAgentState(),Tt.reset(),ae.emit(exports.CallSipCallEvent.OUT_HANGUP,s),Tt.updateActionConfigs(mt)):2===s.device_type&&s.agent_no===St.agentInfo.agent_no?(Tt.reset(),ae.emit(exports.CallSipCallEvent.OUR_SIDE_CONSULT_HANGUP,s),this.putAgentState(),Tt.updateActionConfigs(mt)):2===s.device_type&&s.agent_no!==St.agentInfo.agent_no?(Tt.updateDisplayText(exports.DisplayTextEnum.CALLING),ae.emit(exports.CallSipCallEvent.OTHER_SIDE_CONSULT_HANGUP,s),Tt.updateActionConfigs(It)):3===s.device_type&&s.agent_no===St.agentInfo.agent_no?(this.putAgentState(),Tt.reset(),ae.emit(exports.CallSipCallEvent.THREE_WAY_OUR_SIDE_HANGUP),Tt.updateActionConfigs(mt)):4===s.device_type&&s.agent_no===St.agentInfo.agent_no&&(this.putAgentState(),Tt.reset(),ae.emit(exports.CallSipCallEvent.TRANSFER_END),Tt.updateActionConfigs(mt))),t===exports.WebsocketTypeEnum.OUT_CALL_RINGING&&1===s.type&&(ae.emit(exports.CallSipCallEvent.OUT_RINGING,s),Tt.updateDisplayText(exports.DisplayTextEnum.RINGING)),t===exports.WebsocketTypeEnum.OUT_CALL_ANSWER&&(ae.emit(exports.CallSipCallEvent.OUT_OFF_ANSWER,s),Tt.updateOutCallIsAnswer(!0),Tt.updateDisplayText(exports.DisplayTextEnum.CALLING),Tt.updateActionConfigs(It)),t===exports.WebsocketTypeEnum.CONSULT_RINGING&&(ae.emit(exports.CallSipCallEvent.CONSULT_RINGING),Tt.updateDisplayText(exports.DisplayTextEnum.AGENT_RINGING),Tt.updateActionConfigs([])),t===exports.WebsocketTypeEnum.CONSULT_ANSWER&&(ae.emit(exports.CallSipCallEvent.CONSULT_OFF_HOOK,s),Tt.updateConsultIsAnswer(!0),Tt.updateDisplayText(exports.DisplayTextEnum.CONSULTING),Tt.updateActionConfigs(Mt)),t===exports.WebsocketTypeEnum.CONSULT_FAIL&&(ae.emit(exports.CallSipCallEvent.CONSULT_FAILED,{custom_reason:s.custom_reason}),Tt.updateDisplayText(exports.DisplayTextEnum.CALLING),Tt.updateActionConfigs(It)),t===exports.WebsocketTypeEnum.CONSULT_CALL_IN&&(ae.emit(exports.CallSipCallEvent.CONSULT_INCOMING,s),Tt.updateIsCalling(!0),Tt.updateDirection(exports.DirectionEnum.CONSULT),Tt.updateDisplayText(exports.DisplayTextEnum.CONSULT_CALL_IN),Tt.updateActionConfigs(Bt),St.enableBrowserAlert&&Ks.show("📞 咨询来电",`坐席 [${s.agent_name}] 正在咨询...`,()=>{ae.emit(exports.EventName.NOTIFICATION_CLICK,{agent_mane:s.agent_name})})),t===exports.WebsocketTypeEnum.CONSULT_CALL_IN_SUCCESS&&(St.sessionId=s.session_id,ae.emit(exports.CallSipCallEvent.CONSULT_CALL_IN_SUCCESS),Tt.updateConsultIsAnswer(!0),Tt.updateDisplayText(exports.DisplayTextEnum.CALLING),Tt.updateActionConfigs(wt)),t===exports.WebsocketTypeEnum.CONSULT_RETURN_CALL_SUCCESS&&(ae.emit(exports.CallSipCallEvent.CONSULT_RETURN_CALL_SUCCESS),Tt.updateDisplayText(exports.DisplayTextEnum.CALLING),ae.emit(exports.CallSipCallEvent.OTHER_SIDE_CONSULT_HANGUP,s),Tt.updateActionConfigs(It)),t===exports.WebsocketTypeEnum.MUTE&&(0===i?(Tt.updateIsMuted(!0),ae.emit(exports.CallSipCallEvent.MUTE_SUCCESS),Tt.updateDisplayText(exports.DisplayTextEnum.MUTING)):ae.emit(exports.CallSipCallEvent.MUTE_FAILED,r)),t===exports.WebsocketTypeEnum.UNMUTE&&(0===i?(Tt.updateIsMuted(!1),ae.emit(exports.CallSipCallEvent.UNMUTE_SUCCESS),Tt.updateDisplayText(exports.DisplayTextEnum.CALLING)):ae.emit(exports.CallSipCallEvent.UNMUTE_FAILED,r)),t===exports.WebsocketTypeEnum.CALL_HOLD&&(0===i?(Tt.updateIsHold(!0),Tt.updateDisplayText(exports.DisplayTextEnum.HOLDING),ae.emit(exports.CallSipCallEvent.HOLD_SUCCESS)):ae.emit(exports.CallSipCallEvent.HOLD_FAILED,r)),t===exports.WebsocketTypeEnum.CALL_UNHOLD&&(0===i?(Tt.updateDisplayText(exports.DisplayTextEnum.CALLING),Tt.updateIsHold(!1),ae.emit(exports.CallSipCallEvent.UNHOLD_SUCCESS)):ae.emit(exports.CallSipCallEvent.UNHOLD_FAILED,r)),t===exports.WebsocketTypeEnum.CONSULT_TRANSFER&&(0===i?ae.emit(exports.CallSipCallEvent.CONSULT_TRANSFER_SUCCESS):ae.emit(exports.CallSipCallEvent.CONSULT_TRANSFER_FAILED,r)),t===exports.WebsocketTypeEnum.CONSULT_TRANSFER_FAILED&&ae.emit(exports.CallSipCallEvent.CONSULT_TRANSFER_FAILED,s.status),t===exports.WebsocketTypeEnum.CONSULT_TRANSFER_OFF_HOOK&&(Tt.updateDirection(exports.DirectionEnum.INCOMING),Tt.updateDisplayText(exports.DisplayTextEnum.CALLING),ae.emit(exports.CallSipCallEvent.CONSULT_TRANSFER_OFF_HOOK),Tt.updateActionConfigs(Qt),Tt.updateIncomingIsAnswer(!0),Tt.updateConsultIsAnswer(!1)),t===exports.WebsocketTypeEnum.INCOMING_CALL&&(St.sessionId=s.session_id,Tt.updateIsCalling(!0),Tt.updateDirection(exports.DirectionEnum.INCOMING),Tt.updateDisplayText(exports.DisplayTextEnum.INCOMING_CALL),ae.emit(exports.CallSipCallEvent.INCOMING_CALL,{customer_phone:s.customer_phone}),Tt.updateActionConfigs(Bt),St.enableBrowserAlert&&Ks.show("📞 客户来电",`客户 [${s.customer_phone}] 正在呼入...`,()=>{ae.emit(exports.EventName.NOTIFICATION_CLICK,{customer_phone:s.customer_phone})})),t===exports.WebsocketTypeEnum.INCOMING_CALL_OFF_HOOK&&(Tt.updateIncomingIsAnswer(!0),ae.emit(exports.CallSipCallEvent.INCOMING_CALL_OFF_HOOK),Tt.updateActionConfigs(Qt),Tt.updateDisplayText(exports.DisplayTextEnum.CALLING)),t===exports.WebsocketTypeEnum.INCOMING_CALL_END&&(1===s.device_type&&s.agent_no===St.agentInfo.agent_no?(this.putAgentState(),Tt.reset(),ae.emit(exports.CallSipCallEvent.INCOMING_CALL_END),Tt.updateActionConfigs(mt)):2===s.device_type&&s.agent_no===St.agentInfo.agent_no?(Tt.reset(),ae.emit(exports.CallSipCallEvent.OUR_SIDE_CONSULT_HANGUP,s),this.putAgentState(),Tt.updateActionConfigs(mt)):2===s.device_type&&s.agent_no!==St.agentInfo.agent_no?(Tt.updateDisplayText(exports.DisplayTextEnum.CALLING),ae.emit(exports.CallSipCallEvent.OTHER_SIDE_CONSULT_HANGUP,s),Tt.updateActionConfigs(It)):3===s.device_type&&s.agent_no===St.agentInfo.agent_no?(this.putAgentState(),Tt.reset(),ae.emit(exports.CallSipCallEvent.THREE_WAY_OUR_SIDE_HANGUP),Tt.updateActionConfigs(mt)):4===s.device_type&&s.agent_no===St.agentInfo.agent_no&&(this.putAgentState(),Tt.reset(),ae.emit(exports.CallSipCallEvent.TRANSFER_END),Tt.updateActionConfigs(mt))),t===exports.WebsocketTypeEnum.SATISFACTION_EVALUATION&&(0===i?ae.emit(exports.CallSipCallEvent.SATISFACTION_EVALUATION_SUCCESS):ae.emit(exports.CallSipCallEvent.SATISFACTION_EVALUATION_FAILED,r)),t===exports.WebsocketTypeEnum.CONSULT_THREE_WAY&&(0!==i?ae.emit(exports.CallSipCallEvent.CONSULT_THREE_WAY_FAILED,r):0===i&&Tt.updateIsMeeting(!0)),t===exports.WebsocketTypeEnum.CONSULT_THREE_WAY_RESULT&&("success"===s.status?(Tt.updateDirection(exports.DirectionEnum.MEETING),Tt.updateDisplayText(exports.DisplayTextEnum.MEETING),Tt.updateActionConfigs(ft),ae.emit(exports.CallSipCallEvent.CONSULT_THREE_WAY_SUCCESS)):(ae.emit(exports.CallSipCallEvent.CONSULT_THREE_WAY_FAILED,{conf_name:s.conf_name}),Tt.updateIsMeeting(!1))),t===exports.WebsocketTypeEnum.TRANSFER_INCOMING_CALL&&(s.agent_no===St.agentInfo.agent_no?(Tt.updateDisplayText(exports.DisplayTextEnum.TRANSFERRING),ae.emit(exports.CallSipCallEvent.TRANSFER_PROGRESS),Tt.updateActionConfigs([]),Tt.updateTransferIsAnswer(!0)):(ae.emit(exports.CallSipCallEvent.TRANSFER_INCOMING_CALL,s),Tt.updateIsCalling(!0),Tt.updateDirection(exports.DirectionEnum.TRANSFER),Tt.updateDisplayText(exports.DisplayTextEnum.TRANSFER_INCOMING_CALL),Tt.updateActionConfigs(Bt),St.enableBrowserAlert&&Ks.show("📞 转接来电",`坐席 [${s.agent_no}] 正在转接...`,()=>{ae.emit(exports.EventName.NOTIFICATION_CLICK,{agent_mane:s.agent_no})}))),t===exports.WebsocketTypeEnum.TRANSFER_OFF_HOOK&&(St.sessionId=s.session_id,ae.emit(exports.CallSipCallEvent.TRANSFER_OFF_HOOK),Tt.updateTransferIsAnswer(!0),Tt.updateDisplayText(exports.DisplayTextEnum.CALLING),Tt.updateActionConfigs(Qt)),t===exports.WebsocketTypeEnum.TRANSFER_FAILED&&"failed"===s.status){ae.emit(exports.CallSipCallEvent.TRANSFER_FAILED,"未知原因");const A=Tt.get("direction");A===exports.DirectionEnum.OUTGOING?Tt.updateActionConfigs(It):A===exports.DirectionEnum.INCOMING&&Tt.updateActionConfigs(Qt)}0!==t&&vs.emit("ws-message",e)},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 }, 14000);\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=()=>{this.sendMessage({type:0})},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)):console.warn("WebSocket 未连接,消息未发送")}close(){this.manualClose=!0,this.stopHeartbeat(),this.ws?.close()}static __internalClose(){Os&&(Os.close(),Os=null)}static __internalSend(A){if(!Os)throw new Error("WebSocket 尚未初始化");Os.sendMessage(A)}}class bs{constructor(A){this.sipClient=null,this.sipClient=A}roleMute(){const A=Tt.get("isCalling"),e=St.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=Tt.get("isHold"),{sessionId:e}=St;if(!e)return;if(!Tt.get("actionConfigs").includes("hold"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿保持"}));if(A)throw new Error(JSON.stringify({code:1,msg:"当前已处于保持状态"}));Ns.__internalSend({type:exports.WebsocketTypeEnum.CALL_HOLD,data:{session_id:e}})}unholdCall(){this.roleMute();const{isHold:A}=Tt.getState(),{sessionId:e}=St;if(!Tt.get("actionConfigs").includes("unhold"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿取消保持"}));if(e){if(!A)throw new Error(JSON.stringify({code:1,msg:"当前未处于保持状态"}));Ns.__internalSend({type:exports.WebsocketTypeEnum.CALL_UNHOLD,data:{session_id:e}})}}sendDTMF(A){if(1!==Tt.get("answerDevice"))throw new Error(JSON.stringify({code:1,msg:"当前设备不支持此功能"}));if(!Tt.get("actionConfigs").includes("dtmf"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿发送按键"}));const{isCalling:e}=Tt.getState();if(!e)throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}));this.sipClient.sendDTMF(A)}cancelCall(){if(1!==Tt.get("answerDevice"))throw new Error(JSON.stringify({code:1,msg:"当前设备不支持此功能"}));if(!Tt.get("actionConfigs").includes("reject"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用拒接"}));const{isCalling:A,direction:e,outCallIsAnswer:t}=Tt.getState();if(!A)throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}));t||(e===exports.DirectionEnum.INCOMING?this.rejectInCall():e===exports.DirectionEnum.OUTGOING&&this.rejectOutCall())}async consultCall(A){this.roleMute();const{isCalling:e,outCallIsAnswer:t,consultIsAnswer:s,incomingIsAnswer:i}=Tt.getState(),r=St.sessionId;if(!Tt.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(!r)throw new Error(JSON.stringify({code:1,msg:"sessionId不存在"}));Ns.__internalSend({type:exports.WebsocketTypeEnum.CONSULT_REQUEST,data:{session_id:r,consultation_agent_no:A}})}async answerCall(){if(1!==Tt.get("answerDevice"))throw new Error(JSON.stringify({code:1,msg:"当前设备不支持此功能"}));await this.sipClient.answerCall()}async rejectInCall(){if(1!==Tt.get("answerDevice"))throw new Error(JSON.stringify({code:1,msg:"当前设备不支持此功能"}));const{isCalling:A}=Tt.getState();if(!A)throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}));await this.sipClient.rejectInCall()}async makeCall(A){if(Tt.getState().isCalling)throw new Error(JSON.stringify({code:1,msg:"正在呼叫中,请勿重复点击"}));Tt.updateIsCalling(!0),Tt.updateDirection(exports.DirectionEnum.OUTGOING),Ns.__internalSend({type:exports.WebsocketTypeEnum.OUT_CALL,data:{customer_phone:A}})}async rejectOutCall(){const{isCalling:A}=Tt.getState();if(1!==Tt.get("answerDevice"))throw new Error(JSON.stringify({code:1,msg:"当前设备不支持此功能"}));if(!A)throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}));await this.sipClient.rejectOutCall()}async hangup(){if(1!==Tt.get("answerDevice"))throw new Error(JSON.stringify({code:1,msg:"当前设备不支持此功能"}));if(!Tt.get("isCalling"))throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}));await this.sipClient.hangup()}mute(){this.roleMute();const A=Tt.get("actionConfigs"),e=Tt.get("isMuted"),t=St.sessionId;if(e)throw new Error(JSON.stringify({code:1,msg:"当前未处于静音状态"}));if(!A.includes("mute"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用静音"}));Ns.__internalSend({type:exports.WebsocketTypeEnum.MUTE,data:{session_id:t}})}unmute(){this.roleMute();const A=Tt.get("actionConfigs"),e=Tt.get("isMuted"),t=St.sessionId;if(!e)throw new Error(JSON.stringify({code:1,msg:"当前未处于未静音状态"}));if(!A.includes("unmute"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用静音"}));Ns.__internalSend({type:exports.WebsocketTypeEnum.UNMUTE,data:{session_id:t}})}consultTransfer(){this.roleMute();const A=Tt.get("consultIsAnswer"),e=St.sessionId;if(!Tt.get("actionConfigs").includes("consult_transfer"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用咨询转移"}));if(!A)throw new Error(JSON.stringify({code:1,msg:"当前未处于咨询中"}));Ns.__internalSend({type:exports.WebsocketTypeEnum.CONSULT_TRANSFER,data:{session_id:e}})}satisfactionEvaluation(){this.roleMute();const A=St.sessionId;if(!Tt.get("actionConfigs").includes("satisfaction"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用满意度评价"}));Ns.__internalSend({type:exports.WebsocketTypeEnum.SATISFACTION_EVALUATION,data:{session_id:A}})}startConference(){this.roleMute();if(!Tt.get("actionConfigs").includes("conference"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用三方会议"}));const A=St.sessionId;if(!Tt.get("consultIsAnswer"))throw new Error(JSON.stringify({code:1,msg:"当前未处于咨询中"}));Ns.__internalSend({type:exports.WebsocketTypeEnum.CONSULT_THREE_WAY,data:{session_id:A}})}consultRetrieve(){this.roleMute();if(!Tt.get("actionConfigs").includes("consult_retrieve"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿取回通话"}));const A=St.sessionId;if(!Tt.get("consultIsAnswer"))throw new Error(JSON.stringify({code:1,msg:"当前未处于咨询中"}));Ns.__internalSend({type:exports.WebsocketTypeEnum.CONSULT_RETURN_CALL,data:{session_id:A}})}async transferAgent(A){this.roleMute();if(!Tt.get("actionConfigs").includes("transfer"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿转接"}));const{transferIsAnswer:e}=Tt.getState(),t=St.sessionId;if(e)throw new Error(JSON.stringify({code:1,msg:"当前正在转接"}));Ns.__internalSend({type:exports.WebsocketTypeEnum.TRANSFER,data:{session_id:t,transfer_type:0,transfer_dest:A}})}async transferOutline(A){this.roleMute();if(!Tt.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}=Tt.getState(),t=St.sessionId;if(e)throw new Error(JSON.stringify({code:1,msg:"当前正在转接"}));if(e)throw new Error(JSON.stringify({code:1,msg:"当前正在转接"}));Ns.__internalSend({type:exports.WebsocketTypeEnum.TRANSFER,data:{session_id:t,transfer_type:2,transfer_dest:A}})}}exports.default=class{#A=null;#e=new Us;#t;#s=null;#i=!1;#r;constructor(){clearInterval(this.#s),this.#s=setInterval(async()=>{const A=await this.#o();ds.updateRttObject(A)},2e3),Ks.requestMediaPermissions(),window.addEventListener("beforeunload",()=>{this.destroy()}),this.#r=Tt.subscribeKey("answerDevice",A=>{1===A&&this.initWebRtc()})}async initWebRtc(){const A=St.agentInfo.sip_account_list.find(A=>1===A.device_type);if(A){const e=A.sip_register_addr;this.#A=new ps({server:e.split(":")[0],user:A.sip_account,password:A.sip_password,webSocket:`wss://${e}`,callTimeout:3e4},this.#n),await this.#A.start()}}async init({actionNodeParams:A,url:e,appKey:t,appSecret:s,agentNo:i,enableBrowserAlert:r}){try{const{viewHtmlElement:o,rttHTML:n}=A||{},a=e||"https://aicc.yescloudy.com";cs=a,hs=t,ls=s;const V=ns(t,s);await this.#e._agentLogin(i);const{agent_no:c,current_answer_device:h,init_state:l}=St.agentInfo;l===exports.AgentStatus.IDLE?(Tt.updateActionConfigs(mt),St.stateObject={state:l,state_name:"空闲"}):l===exports.AgentStatus.BUSY&&(Tt.updateActionConfigs(Ct),St.stateObject={state:l,state_name:"忙碌"});let g="";const q=new URL(a).host;g=a.includes("https")?`wss://${q}`:`ws://${q}`,Ns.getInstance(`${g}/ccs-ws/api/v1/ws/call/${c}?Authorization=${V}`),Tt.updateAnswerDevice(h),1===h&&await this.initWebRtc(),this.#t=new bs(this.#A),A&&o&&(this.#i=!0,new Zs({container:o,rttHTML:n,statusParams:{state:exports.AgentStatus.IDLE,statusName:"空闲"}},this,this.#t)),St.enableBrowserAlert=r,r&&Ks.requestPermission()}catch(A){this.#i&&Fs("初始化失败"),console.log(A)}}#n=A=>{const{type:e,data:t}=A;ae.emit(e,t)};#a=A=>{};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())}destroy(){this.destroyRtc(),clearInterval(this.#s),vs.off("ws-message",this.#a),Ns.__internalClose(),this.offAll(),this.#r?.(),this.#A=null,this.#e=null,this.#i=!1}async#o(){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(A=>this.#t.makeCall(A)),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))}}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))}}getAgentData(){const{agent_no:A,agent_name:e,answer_devices:t,init_state:s,default_device:i}=St.agentInfo,{stateObject:r}=St,o={1:"软电话",2:"手机模式",3:"SIP话机"};return{agent_no:A,agent_name:e,init_state:s,default_device:i,answer_devices:(t||"").split(",").map(A=>({value:Number(A),label:o[Number(A)]})),stateObject:r}}};
@@ -873,4 +873,4 @@ const ms=A=>(e,t)=>{void 0!==t?t.addInitializer(()=>{customElements.define(A,e)}
873
873
  `:""}
874
874
  </div>
875
875
  </my-popover>
876
- `;gt(s,this.rttHTML)}}const xs=new class{constructor(){this.events={}}on(A,e){this.events[A]=this.events[A]||[],this.events[A].push(e)}off(A,e){this.events[A]=(this.events[A]||[]).filter(A=>A!==e)}emit(A,...e){(this.events[A]||[]).forEach(A=>A(...e))}};class Ks{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 console.log("已授权麦克风权限"),null;const e=await navigator.mediaDevices.getUserMedia({audio:!0});return console.log("获取音频权限成功"),e}catch(A){return console.warn("获取音频权限失败:",A),null}}static show(A,e,t){if("Notification"in window&&"granted"===Notification.permission){const s=new Notification(A,{body:e,tag:"xy-global-notification",renotify:!0});s.onclick=()=>{window.focus(),s.close(),t?.()},setTimeout(()=>s.close(),5e3)}}static async checkMediaPermissions(){try{return(await navigator.mediaDevices.enumerateDevices()).some(A=>"audioinput"===A.kind&&""!==A.label)}catch(A){return console.warn("无法检查设备权限:",A),!1}}}let Os=null;class Ns{constructor(A){this.url=A,this.ws=null,this.heartbeatWorker=null,this.reconnectDelay=3e3,this.manualClose=!1,this.apiClient=new Us,this.initWebSocket()}static getInstance(A){if(!Os){if(!A)throw new Error("WebSocket 尚未初始化");Os=new Ns(A)}return Os}stateIdleChange(){const{state:A}=St.stateObject;A!==Ae.IDLE&&this.apiClient._changeState({data:{state:Ae.IDLE,state_name:"空闲"}})}putAgentState(){St.sessionId="";const{post_call_process_time:A}=St.agentInfo,e=A||0;St.autoStateTimer&&(clearTimeout(St.autoStateTimer),St.autoStateTimer=null),e>0?St.autoStateTimer=setTimeout(()=>{St.autoStateTimer=null,this.stateIdleChange()},e):this.stateIdleChange()}initWebSocket(){this.ws=new WebSocket(this.url),this.ws.onopen=()=>{this.sendMessage({type:0}),this.startHeartbeat()},this.ws.onmessage=A=>{const e=JSON.parse(A.data),{type:t,data:s,code:i,msg:r}=e;if(0!==t&&console.warn(t),t===ie.OUT_CALL&&(0!==i?(Tt.reset(),ae.emit(se.OUT_FAILED,r),this.stateIdleChange()):0===i&&ae.emit(se.OUT_SUCCESS)),t===ie.AGENT_STATE){const{state:A,state_name:e}=s;St.stateObject={state:A,state_name:e},"振铃中"===e&&Tt.updateDisplayText(oe.AGENT_RINGING),ae.emit(se.AGENT_STATE,{state:A,state_name:e})}if(t===ie.OUT_CALL_INCOMING_CALL&&(ae.emit(se.OUT_INCOMING_CALL),St.sessionId=s.session_id,Tt.updateActionConfigs(Et)),t===ie.OUT_CALL_END&&(Tt.updateDisplayText(""),1===s.device_type&&s.agent_no===St.agentInfo.agent_no?(this.putAgentState(),Tt.reset(),ae.emit(se.OUT_HANGUP,s),Tt.updateActionConfigs(mt)):2===s.device_type&&s.agent_no===St.agentInfo.agent_no?(Tt.reset(),ae.emit(se.OUR_SIDE_CONSULT_HANGUP,s),this.putAgentState(),Tt.updateActionConfigs(mt)):2===s.device_type&&s.agent_no!==St.agentInfo.agent_no?(Tt.updateDisplayText(oe.CALLING),ae.emit(se.OTHER_SIDE_CONSULT_HANGUP,s),Tt.updateActionConfigs(It)):3===s.device_type&&s.agent_no===St.agentInfo.agent_no?(this.putAgentState(),Tt.reset(),ae.emit(se.THREE_WAY_OUR_SIDE_HANGUP),Tt.updateActionConfigs(mt)):4===s.device_type&&s.agent_no===St.agentInfo.agent_no&&(this.putAgentState(),Tt.reset(),ae.emit(se.TRANSFER_END),Tt.updateActionConfigs(mt))),t===ie.OUT_CALL_RINGING&&1===s.type&&(ae.emit(se.OUT_RINGING,s),Tt.updateDisplayText(oe.RINGING)),t===ie.OUT_CALL_ANSWER&&(ae.emit(se.OUT_OFF_ANSWER,s),Tt.updateOutCallIsAnswer(!0),Tt.updateDisplayText(oe.CALLING),Tt.updateActionConfigs(It)),t===ie.CONSULT_RINGING&&(ae.emit(se.CONSULT_RINGING),Tt.updateDisplayText(oe.AGENT_RINGING),Tt.updateActionConfigs([])),t===ie.CONSULT_ANSWER&&(ae.emit(se.CONSULT_OFF_HOOK,s),Tt.updateConsultIsAnswer(!0),Tt.updateDisplayText(oe.CONSULTING),Tt.updateActionConfigs(Mt)),t===ie.CONSULT_FAIL&&(ae.emit(se.CONSULT_FAILED,{custom_reason:s.custom_reason}),Tt.updateDisplayText(oe.CALLING),Tt.updateActionConfigs(It)),t===ie.CONSULT_CALL_IN&&(ae.emit(se.CONSULT_INCOMING,s),Tt.updateIsCalling(!0),Tt.updateDirection(ee.CONSULT),Tt.updateDisplayText(oe.CONSULT_CALL_IN),Tt.updateActionConfigs(Bt),St.enableBrowserAlert&&Ks.show("📞 咨询来电",`坐席 [${s.agent_name}] 正在咨询...`,()=>{ae.emit(re.NOTIFICATION_CLICK,{agent_mane:s.agent_name})})),t===ie.CONSULT_CALL_IN_SUCCESS&&(St.sessionId=s.session_id,ae.emit(se.CONSULT_CALL_IN_SUCCESS),Tt.updateConsultIsAnswer(!0),Tt.updateDisplayText(oe.CALLING),Tt.updateActionConfigs(wt)),t===ie.CONSULT_RETURN_CALL_SUCCESS&&(ae.emit(se.CONSULT_RETURN_CALL_SUCCESS),Tt.updateDisplayText(oe.CALLING),ae.emit(se.OTHER_SIDE_CONSULT_HANGUP,s),Tt.updateActionConfigs(It)),t===ie.MUTE&&(0===i?(Tt.updateIsMuted(!0),ae.emit(se.MUTE_SUCCESS),Tt.updateDisplayText(oe.MUTING)):ae.emit(se.MUTE_FAILED,r)),t===ie.UNMUTE&&(0===i?(Tt.updateIsMuted(!1),ae.emit(se.UNMUTE_SUCCESS),Tt.updateDisplayText(oe.CALLING)):ae.emit(se.UNMUTE_FAILED,r)),t===ie.CALL_HOLD&&(0===i?(Tt.updateIsHold(!0),Tt.updateDisplayText(oe.HOLDING),ae.emit(se.HOLD_SUCCESS)):ae.emit(se.HOLD_FAILED,r)),t===ie.CALL_UNHOLD&&(0===i?(Tt.updateDisplayText(oe.CALLING),Tt.updateIsHold(!1),ae.emit(se.UNHOLD_SUCCESS)):ae.emit(se.UNHOLD_FAILED,r)),t===ie.CONSULT_TRANSFER&&(0===i?ae.emit(se.CONSULT_TRANSFER_SUCCESS):ae.emit(se.CONSULT_TRANSFER_FAILED,r)),t===ie.CONSULT_TRANSFER_FAILED&&ae.emit(se.CONSULT_TRANSFER_FAILED,s.status),t===ie.CONSULT_TRANSFER_OFF_HOOK&&(Tt.updateDirection(ee.INCOMING),Tt.updateDisplayText(oe.CALLING),ae.emit(se.CONSULT_TRANSFER_OFF_HOOK),Tt.updateActionConfigs(Qt),Tt.updateIncomingIsAnswer(!0),Tt.updateConsultIsAnswer(!1)),t===ie.INCOMING_CALL&&(St.sessionId=s.session_id,Tt.updateIsCalling(!0),Tt.updateDirection(ee.INCOMING),Tt.updateDisplayText(oe.INCOMING_CALL),ae.emit(se.INCOMING_CALL,{customer_phone:s.customer_phone}),Tt.updateActionConfigs(Bt),St.enableBrowserAlert&&Ks.show("📞 客户来电",`客户 [${s.customer_phone}] 正在呼入...`,()=>{ae.emit(re.NOTIFICATION_CLICK,{customer_phone:s.customer_phone})})),t===ie.INCOMING_CALL_OFF_HOOK&&(Tt.updateIncomingIsAnswer(!0),ae.emit(se.INCOMING_CALL_OFF_HOOK),Tt.updateActionConfigs(Qt),Tt.updateDisplayText(oe.CALLING)),t===ie.INCOMING_CALL_END&&(Tt.updateDisplayText(""),1===s.device_type&&s.agent_no===St.agentInfo.agent_no?(this.putAgentState(),Tt.reset(),ae.emit(se.INCOMING_CALL_END),Tt.updateActionConfigs(mt)):2===s.device_type&&s.agent_no===St.agentInfo.agent_no?(Tt.reset(),ae.emit(se.OUR_SIDE_CONSULT_HANGUP,s),this.putAgentState(),Tt.updateActionConfigs(mt)):2===s.device_type&&s.agent_no!==St.agentInfo.agent_no?(Tt.updateDisplayText(oe.CALLING),ae.emit(se.OTHER_SIDE_CONSULT_HANGUP,s),Tt.updateActionConfigs(It)):3===s.device_type&&s.agent_no===St.agentInfo.agent_no?(this.putAgentState(),Tt.reset(),ae.emit(se.THREE_WAY_OUR_SIDE_HANGUP),Tt.updateActionConfigs(mt)):4===s.device_type&&s.agent_no===St.agentInfo.agent_no&&(this.putAgentState(),Tt.reset(),ae.emit(se.TRANSFER_END),Tt.updateActionConfigs(mt))),t===ie.SATISFACTION_EVALUATION&&(0===i?ae.emit(se.SATISFACTION_EVALUATION_SUCCESS):ae.emit(se.SATISFACTION_EVALUATION_FAILED,r)),t===ie.CONSULT_THREE_WAY&&(0!==i?ae.emit(se.CONSULT_THREE_WAY_FAILED,r):0===i&&Tt.updateIsMeeting(!0)),t===ie.CONSULT_THREE_WAY_RESULT&&("success"===s.status?(Tt.updateDirection(ee.MEETING),Tt.updateDisplayText(oe.MEETING),Tt.updateActionConfigs(ft),ae.emit(se.CONSULT_THREE_WAY_SUCCESS)):(ae.emit(se.CONSULT_THREE_WAY_FAILED,{conf_name:s.conf_name}),Tt.updateIsMeeting(!1))),t===ie.TRANSFER_INCOMING_CALL&&(s.agent_no===St.agentInfo.agent_no?(Tt.updateDisplayText(oe.TRANSFERRING),ae.emit(se.TRANSFER_PROGRESS),Tt.updateActionConfigs([]),Tt.updateTransferIsAnswer(!0)):(ae.emit(se.TRANSFER_INCOMING_CALL,s),Tt.updateIsCalling(!0),Tt.updateDirection(ee.TRANSFER),Tt.updateDisplayText(oe.TRANSFER_INCOMING_CALL),Tt.updateActionConfigs(Bt),St.enableBrowserAlert&&Ks.show("📞 转接来电",`坐席 [${s.agent_no}] 正在转接...`,()=>{ae.emit(re.NOTIFICATION_CLICK,{agent_mane:s.agent_no})}))),t===ie.TRANSFER_OFF_HOOK&&(St.sessionId=s.session_id,ae.emit(se.TRANSFER_OFF_HOOK),Tt.updateTransferIsAnswer(!0),Tt.updateDisplayText(oe.CALLING),Tt.updateActionConfigs(Qt)),t===ie.TRANSFER_FAILED&&"failed"===s.status){ae.emit(se.TRANSFER_FAILED,"未知原因");const A=Tt.get("direction");A===ee.OUTGOING?Tt.updateActionConfigs(It):A===ee.INCOMING&&Tt.updateActionConfigs(Qt)}0!==t&&xs.emit("ws-message",e)},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 }, 14000);\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=()=>{this.sendMessage({type:0})},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)):console.warn("WebSocket 未连接,消息未发送")}close(){this.manualClose=!0,this.stopHeartbeat(),this.ws?.close()}static __internalClose(){Os&&(Os.close(),Os=null)}static __internalSend(A){if(!Os)throw new Error("WebSocket 尚未初始化");Os.sendMessage(A)}}class Js{constructor(A){this.sipClient=null,this.sipClient=A}roleMute(){const A=Tt.get("isCalling"),e=St.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=Tt.get("isHold"),{sessionId:e}=St;if(!e)return;if(!Tt.get("actionConfigs").includes("hold"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿保持"}));if(A)throw new Error(JSON.stringify({code:1,msg:"当前已处于保持状态"}));Ns.__internalSend({type:ie.CALL_HOLD,data:{session_id:e}})}unholdCall(){this.roleMute();const{isHold:A}=Tt.getState(),{sessionId:e}=St;if(!Tt.get("actionConfigs").includes("unhold"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿取消保持"}));if(e){if(!A)throw new Error(JSON.stringify({code:1,msg:"当前未处于保持状态"}));Ns.__internalSend({type:ie.CALL_UNHOLD,data:{session_id:e}})}}sendDTMF(A){if(1!==Tt.get("answerDevice"))throw new Error(JSON.stringify({code:1,msg:"当前设备不支持此功能"}));if(!Tt.get("actionConfigs").includes("dtmf"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿发送按键"}));const{isCalling:e}=Tt.getState();if(!e)throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}));this.sipClient.sendDTMF(A)}cancelCall(){if(1!==Tt.get("answerDevice"))throw new Error(JSON.stringify({code:1,msg:"当前设备不支持此功能"}));if(!Tt.get("actionConfigs").includes("reject"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用拒接"}));const{isCalling:A,direction:e,outCallIsAnswer:t}=Tt.getState();if(!A)throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}));t||(e===ee.INCOMING?this.rejectInCall():e===ee.OUTGOING&&this.rejectOutCall())}async consultCall(A){this.roleMute();const{isCalling:e,outCallIsAnswer:t,consultIsAnswer:s,incomingIsAnswer:i}=Tt.getState(),r=St.sessionId;if(!Tt.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(!r)throw new Error(JSON.stringify({code:1,msg:"sessionId不存在"}));Ns.__internalSend({type:ie.CONSULT_REQUEST,data:{session_id:r,consultation_agent_no:A}})}async answerCall(){if(1!==Tt.get("answerDevice"))throw new Error(JSON.stringify({code:1,msg:"当前设备不支持此功能"}));await this.sipClient.answerCall()}async rejectInCall(){if(1!==Tt.get("answerDevice"))throw new Error(JSON.stringify({code:1,msg:"当前设备不支持此功能"}));const{isCalling:A}=Tt.getState();if(!A)throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}));await this.sipClient.rejectInCall()}async makeCall(A){if(Tt.getState().isCalling)throw new Error(JSON.stringify({code:1,msg:"正在呼叫中,请勿重复点击"}));Tt.updateIsCalling(!0),Tt.updateDirection(ee.OUTGOING),Ns.__internalSend({type:ie.OUT_CALL,data:{customer_phone:A}})}async rejectOutCall(){const{isCalling:A}=Tt.getState();if(1!==Tt.get("answerDevice"))throw new Error(JSON.stringify({code:1,msg:"当前设备不支持此功能"}));if(!A)throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}));await this.sipClient.rejectOutCall()}async hangup(){if(1!==Tt.get("answerDevice"))throw new Error(JSON.stringify({code:1,msg:"当前设备不支持此功能"}));if(!Tt.get("isCalling"))throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}));await this.sipClient.hangup()}mute(){this.roleMute();const A=Tt.get("actionConfigs"),e=Tt.get("isMuted"),t=St.sessionId;if(e)throw new Error(JSON.stringify({code:1,msg:"当前未处于静音状态"}));if(!A.includes("mute"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用静音"}));Ns.__internalSend({type:ie.MUTE,data:{session_id:t}})}unmute(){this.roleMute();const A=Tt.get("actionConfigs"),e=Tt.get("isMuted"),t=St.sessionId;if(!e)throw new Error(JSON.stringify({code:1,msg:"当前未处于未静音状态"}));if(!A.includes("unmute"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用静音"}));Ns.__internalSend({type:ie.UNMUTE,data:{session_id:t}})}consultTransfer(){this.roleMute();const A=Tt.get("consultIsAnswer"),e=St.sessionId;if(!Tt.get("actionConfigs").includes("consult_transfer"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用咨询转移"}));if(!A)throw new Error(JSON.stringify({code:1,msg:"当前未处于咨询中"}));Ns.__internalSend({type:ie.CONSULT_TRANSFER,data:{session_id:e}})}satisfactionEvaluation(){this.roleMute();const A=St.sessionId;if(!Tt.get("actionConfigs").includes("satisfaction"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用满意度评价"}));Ns.__internalSend({type:ie.SATISFACTION_EVALUATION,data:{session_id:A}})}startConference(){this.roleMute();if(!Tt.get("actionConfigs").includes("conference"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用三方会议"}));const A=St.sessionId;if(!Tt.get("consultIsAnswer"))throw new Error(JSON.stringify({code:1,msg:"当前未处于咨询中"}));Ns.__internalSend({type:ie.CONSULT_THREE_WAY,data:{session_id:A}})}consultRetrieve(){this.roleMute();if(!Tt.get("actionConfigs").includes("consult_retrieve"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿取回通话"}));const A=St.sessionId;if(!Tt.get("consultIsAnswer"))throw new Error(JSON.stringify({code:1,msg:"当前未处于咨询中"}));Ns.__internalSend({type:ie.CONSULT_RETURN_CALL,data:{session_id:A}})}async transferAgent(A){this.roleMute();if(!Tt.get("actionConfigs").includes("transfer"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿转接"}));const{transferIsAnswer:e}=Tt.getState(),t=St.sessionId;if(e)throw new Error(JSON.stringify({code:1,msg:"当前正在转接"}));Ns.__internalSend({type:ie.TRANSFER,data:{session_id:t,transfer_type:0,transfer_dest:A}})}async transferOutline(A){this.roleMute();if(!Tt.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}=Tt.getState(),t=St.sessionId;if(e)throw new Error(JSON.stringify({code:1,msg:"当前正在转接"}));if(e)throw new Error(JSON.stringify({code:1,msg:"当前正在转接"}));Ns.__internalSend({type:ie.TRANSFER,data:{session_id:t,transfer_type:2,transfer_dest:A}})}}class bs{#A=null;#e=new Us;#t;#s=null;#i=!1;#r;constructor(){clearInterval(this.#s),this.#s=setInterval(async()=>{const A=await this.#o();ds.updateRttObject(A)},2e3),Ks.requestMediaPermissions(),window.addEventListener("beforeunload",()=>{this.destroy()}),this.#r=Tt.subscribeKey("answerDevice",A=>{1===A&&this.initWebRtc()})}async initWebRtc(){const A=St.agentInfo.sip_account_list.find(A=>1===A.device_type);if(A){const e=A.sip_register_addr;this.#A=new us({server:e.split(":")[0],user:A.sip_account,password:A.sip_password,webSocket:`wss://${e}`,callTimeout:3e4},this.#n),await this.#A.start()}}async init({actionNodeParams:A,url:e,appKey:t,appSecret:s,agentNo:i,enableBrowserAlert:r}){try{const{viewHtmlElement:o,rttHTML:n}=A||{},a=e||"https://aicc.yescloudy.com";cs=a,hs=t,ls=s;const V=ns(t,s);await this.#e._agentLogin(i);const{agent_no:c,current_answer_device:h,init_state:l}=St.agentInfo;l===Ae.IDLE?(Tt.updateActionConfigs(mt),St.stateObject={state:l,state_name:"空闲"}):l===Ae.BUSY&&(Tt.updateActionConfigs(Ct),St.stateObject={state:l,state_name:"忙碌"});let g="";const q=new URL(a).host;g=a.includes("https")?`wss://${q}`:`ws://${q}`,Ns.getInstance(`${g}/ccs-ws/api/v1/ws/call/${c}?Authorization=${V}`),Tt.updateAnswerDevice(h),1===h&&await this.initWebRtc(),this.#t=new Js(this.#A),A&&o&&(this.#i=!0,new vs({container:o,rttHTML:n,statusParams:{state:Ae.IDLE,statusName:"空闲"}},this,this.#t)),St.enableBrowserAlert=r,r&&Ks.requestPermission()}catch(A){this.#i&&Fs("初始化失败"),console.log(A)}}#n=A=>{const{type:e,data:t}=A;ae.emit(e,t)};#a=A=>{};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())}destroy(){this.destroyRtc(),clearInterval(this.#s),xs.off("ws-message",this.#a),Ns.__internalClose(),this.offAll(),this.#r?.(),this.#A=null,this.#e=null,this.#i=!1}async#o(){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(A=>this.#t.makeCall(A)),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))}}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))}}getAgentData(){const{agent_no:A,agent_name:e,answer_devices:t,init_state:s,default_device:i}=St.agentInfo,{stateObject:r}=St,o={1:"软电话",2:"手机模式",3:"SIP话机"};return{agent_no:A,agent_name:e,init_state:s,default_device:i,answer_devices:(t||"").split(",").map(A=>({value:Number(A),label:o[Number(A)]})),stateObject:r}}}export{Ae as AgentStatus,se as CallSipCallEvent,ee as DirectionEnum,oe as DisplayTextEnum,re as EventName,te as WebRtcCallSipEventEnum,ie as WebsocketTypeEnum,bs as default};
876
+ `;gt(s,this.rttHTML)}}const xs=new class{constructor(){this.events={}}on(A,e){this.events[A]=this.events[A]||[],this.events[A].push(e)}off(A,e){this.events[A]=(this.events[A]||[]).filter(A=>A!==e)}emit(A,...e){(this.events[A]||[]).forEach(A=>A(...e))}};class Ks{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 console.log("已授权麦克风权限"),null;const e=await navigator.mediaDevices.getUserMedia({audio:!0});return console.log("获取音频权限成功"),e}catch(A){return console.warn("获取音频权限失败:",A),null}}static show(A,e,t){if("Notification"in window&&"granted"===Notification.permission){const s=new Notification(A,{body:e,tag:"xy-global-notification",renotify:!0});s.onclick=()=>{window.focus(),s.close(),t?.()},setTimeout(()=>s.close(),5e3)}}static async checkMediaPermissions(){try{return(await navigator.mediaDevices.enumerateDevices()).some(A=>"audioinput"===A.kind&&""!==A.label)}catch(A){return console.warn("无法检查设备权限:",A),!1}}}let Os=null;class Ns{constructor(A){this.url=A,this.ws=null,this.heartbeatWorker=null,this.reconnectDelay=3e3,this.manualClose=!1,this.apiClient=new Us,this.initWebSocket()}static getInstance(A){if(!Os){if(!A)throw new Error("WebSocket 尚未初始化");Os=new Ns(A)}return Os}stateIdleChange(){const{state:A}=St.stateObject;A!==Ae.IDLE&&this.apiClient._changeState({data:{state:Ae.IDLE,state_name:"空闲"}})}putAgentState(){St.sessionId="";const{post_call_process_time:A}=St.agentInfo,e=A||0;St.autoStateTimer&&(clearTimeout(St.autoStateTimer),St.autoStateTimer=null),e>0?St.autoStateTimer=setTimeout(()=>{St.autoStateTimer=null,this.stateIdleChange()},e):this.stateIdleChange()}initWebSocket(){this.ws=new WebSocket(this.url),this.ws.onopen=()=>{this.sendMessage({type:0}),this.startHeartbeat()},this.ws.onmessage=A=>{const e=JSON.parse(A.data),{type:t,data:s,code:i,msg:r}=e;if(0!==t&&console.warn(t),t===ie.OUT_CALL&&(0!==i?(Tt.reset(),ae.emit(se.OUT_FAILED,r),this.stateIdleChange()):0===i&&ae.emit(se.OUT_SUCCESS)),t===ie.AGENT_STATE){const{state:A,state_name:e}=s;St.stateObject={state:A,state_name:e},"振铃中"===e&&Tt.updateDisplayText(oe.AGENT_RINGING),ae.emit(se.AGENT_STATE,{state:A,state_name:e})}if(t===ie.OUT_CALL_INCOMING_CALL&&(ae.emit(se.OUT_INCOMING_CALL),St.sessionId=s.session_id,Tt.updateActionConfigs(Et)),t===ie.OUT_CALL_END&&(1===s.device_type&&s.agent_no===St.agentInfo.agent_no?(this.putAgentState(),Tt.reset(),ae.emit(se.OUT_HANGUP,s),Tt.updateActionConfigs(mt)):2===s.device_type&&s.agent_no===St.agentInfo.agent_no?(Tt.reset(),ae.emit(se.OUR_SIDE_CONSULT_HANGUP,s),this.putAgentState(),Tt.updateActionConfigs(mt)):2===s.device_type&&s.agent_no!==St.agentInfo.agent_no?(Tt.updateDisplayText(oe.CALLING),ae.emit(se.OTHER_SIDE_CONSULT_HANGUP,s),Tt.updateActionConfigs(It)):3===s.device_type&&s.agent_no===St.agentInfo.agent_no?(this.putAgentState(),Tt.reset(),ae.emit(se.THREE_WAY_OUR_SIDE_HANGUP),Tt.updateActionConfigs(mt)):4===s.device_type&&s.agent_no===St.agentInfo.agent_no&&(this.putAgentState(),Tt.reset(),ae.emit(se.TRANSFER_END),Tt.updateActionConfigs(mt))),t===ie.OUT_CALL_RINGING&&1===s.type&&(ae.emit(se.OUT_RINGING,s),Tt.updateDisplayText(oe.RINGING)),t===ie.OUT_CALL_ANSWER&&(ae.emit(se.OUT_OFF_ANSWER,s),Tt.updateOutCallIsAnswer(!0),Tt.updateDisplayText(oe.CALLING),Tt.updateActionConfigs(It)),t===ie.CONSULT_RINGING&&(ae.emit(se.CONSULT_RINGING),Tt.updateDisplayText(oe.AGENT_RINGING),Tt.updateActionConfigs([])),t===ie.CONSULT_ANSWER&&(ae.emit(se.CONSULT_OFF_HOOK,s),Tt.updateConsultIsAnswer(!0),Tt.updateDisplayText(oe.CONSULTING),Tt.updateActionConfigs(Mt)),t===ie.CONSULT_FAIL&&(ae.emit(se.CONSULT_FAILED,{custom_reason:s.custom_reason}),Tt.updateDisplayText(oe.CALLING),Tt.updateActionConfigs(It)),t===ie.CONSULT_CALL_IN&&(ae.emit(se.CONSULT_INCOMING,s),Tt.updateIsCalling(!0),Tt.updateDirection(ee.CONSULT),Tt.updateDisplayText(oe.CONSULT_CALL_IN),Tt.updateActionConfigs(Bt),St.enableBrowserAlert&&Ks.show("📞 咨询来电",`坐席 [${s.agent_name}] 正在咨询...`,()=>{ae.emit(re.NOTIFICATION_CLICK,{agent_mane:s.agent_name})})),t===ie.CONSULT_CALL_IN_SUCCESS&&(St.sessionId=s.session_id,ae.emit(se.CONSULT_CALL_IN_SUCCESS),Tt.updateConsultIsAnswer(!0),Tt.updateDisplayText(oe.CALLING),Tt.updateActionConfigs(wt)),t===ie.CONSULT_RETURN_CALL_SUCCESS&&(ae.emit(se.CONSULT_RETURN_CALL_SUCCESS),Tt.updateDisplayText(oe.CALLING),ae.emit(se.OTHER_SIDE_CONSULT_HANGUP,s),Tt.updateActionConfigs(It)),t===ie.MUTE&&(0===i?(Tt.updateIsMuted(!0),ae.emit(se.MUTE_SUCCESS),Tt.updateDisplayText(oe.MUTING)):ae.emit(se.MUTE_FAILED,r)),t===ie.UNMUTE&&(0===i?(Tt.updateIsMuted(!1),ae.emit(se.UNMUTE_SUCCESS),Tt.updateDisplayText(oe.CALLING)):ae.emit(se.UNMUTE_FAILED,r)),t===ie.CALL_HOLD&&(0===i?(Tt.updateIsHold(!0),Tt.updateDisplayText(oe.HOLDING),ae.emit(se.HOLD_SUCCESS)):ae.emit(se.HOLD_FAILED,r)),t===ie.CALL_UNHOLD&&(0===i?(Tt.updateDisplayText(oe.CALLING),Tt.updateIsHold(!1),ae.emit(se.UNHOLD_SUCCESS)):ae.emit(se.UNHOLD_FAILED,r)),t===ie.CONSULT_TRANSFER&&(0===i?ae.emit(se.CONSULT_TRANSFER_SUCCESS):ae.emit(se.CONSULT_TRANSFER_FAILED,r)),t===ie.CONSULT_TRANSFER_FAILED&&ae.emit(se.CONSULT_TRANSFER_FAILED,s.status),t===ie.CONSULT_TRANSFER_OFF_HOOK&&(Tt.updateDirection(ee.INCOMING),Tt.updateDisplayText(oe.CALLING),ae.emit(se.CONSULT_TRANSFER_OFF_HOOK),Tt.updateActionConfigs(Qt),Tt.updateIncomingIsAnswer(!0),Tt.updateConsultIsAnswer(!1)),t===ie.INCOMING_CALL&&(St.sessionId=s.session_id,Tt.updateIsCalling(!0),Tt.updateDirection(ee.INCOMING),Tt.updateDisplayText(oe.INCOMING_CALL),ae.emit(se.INCOMING_CALL,{customer_phone:s.customer_phone}),Tt.updateActionConfigs(Bt),St.enableBrowserAlert&&Ks.show("📞 客户来电",`客户 [${s.customer_phone}] 正在呼入...`,()=>{ae.emit(re.NOTIFICATION_CLICK,{customer_phone:s.customer_phone})})),t===ie.INCOMING_CALL_OFF_HOOK&&(Tt.updateIncomingIsAnswer(!0),ae.emit(se.INCOMING_CALL_OFF_HOOK),Tt.updateActionConfigs(Qt),Tt.updateDisplayText(oe.CALLING)),t===ie.INCOMING_CALL_END&&(1===s.device_type&&s.agent_no===St.agentInfo.agent_no?(this.putAgentState(),Tt.reset(),ae.emit(se.INCOMING_CALL_END),Tt.updateActionConfigs(mt)):2===s.device_type&&s.agent_no===St.agentInfo.agent_no?(Tt.reset(),ae.emit(se.OUR_SIDE_CONSULT_HANGUP,s),this.putAgentState(),Tt.updateActionConfigs(mt)):2===s.device_type&&s.agent_no!==St.agentInfo.agent_no?(Tt.updateDisplayText(oe.CALLING),ae.emit(se.OTHER_SIDE_CONSULT_HANGUP,s),Tt.updateActionConfigs(It)):3===s.device_type&&s.agent_no===St.agentInfo.agent_no?(this.putAgentState(),Tt.reset(),ae.emit(se.THREE_WAY_OUR_SIDE_HANGUP),Tt.updateActionConfigs(mt)):4===s.device_type&&s.agent_no===St.agentInfo.agent_no&&(this.putAgentState(),Tt.reset(),ae.emit(se.TRANSFER_END),Tt.updateActionConfigs(mt))),t===ie.SATISFACTION_EVALUATION&&(0===i?ae.emit(se.SATISFACTION_EVALUATION_SUCCESS):ae.emit(se.SATISFACTION_EVALUATION_FAILED,r)),t===ie.CONSULT_THREE_WAY&&(0!==i?ae.emit(se.CONSULT_THREE_WAY_FAILED,r):0===i&&Tt.updateIsMeeting(!0)),t===ie.CONSULT_THREE_WAY_RESULT&&("success"===s.status?(Tt.updateDirection(ee.MEETING),Tt.updateDisplayText(oe.MEETING),Tt.updateActionConfigs(ft),ae.emit(se.CONSULT_THREE_WAY_SUCCESS)):(ae.emit(se.CONSULT_THREE_WAY_FAILED,{conf_name:s.conf_name}),Tt.updateIsMeeting(!1))),t===ie.TRANSFER_INCOMING_CALL&&(s.agent_no===St.agentInfo.agent_no?(Tt.updateDisplayText(oe.TRANSFERRING),ae.emit(se.TRANSFER_PROGRESS),Tt.updateActionConfigs([]),Tt.updateTransferIsAnswer(!0)):(ae.emit(se.TRANSFER_INCOMING_CALL,s),Tt.updateIsCalling(!0),Tt.updateDirection(ee.TRANSFER),Tt.updateDisplayText(oe.TRANSFER_INCOMING_CALL),Tt.updateActionConfigs(Bt),St.enableBrowserAlert&&Ks.show("📞 转接来电",`坐席 [${s.agent_no}] 正在转接...`,()=>{ae.emit(re.NOTIFICATION_CLICK,{agent_mane:s.agent_no})}))),t===ie.TRANSFER_OFF_HOOK&&(St.sessionId=s.session_id,ae.emit(se.TRANSFER_OFF_HOOK),Tt.updateTransferIsAnswer(!0),Tt.updateDisplayText(oe.CALLING),Tt.updateActionConfigs(Qt)),t===ie.TRANSFER_FAILED&&"failed"===s.status){ae.emit(se.TRANSFER_FAILED,"未知原因");const A=Tt.get("direction");A===ee.OUTGOING?Tt.updateActionConfigs(It):A===ee.INCOMING&&Tt.updateActionConfigs(Qt)}0!==t&&xs.emit("ws-message",e)},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 }, 14000);\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=()=>{this.sendMessage({type:0})},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)):console.warn("WebSocket 未连接,消息未发送")}close(){this.manualClose=!0,this.stopHeartbeat(),this.ws?.close()}static __internalClose(){Os&&(Os.close(),Os=null)}static __internalSend(A){if(!Os)throw new Error("WebSocket 尚未初始化");Os.sendMessage(A)}}class Js{constructor(A){this.sipClient=null,this.sipClient=A}roleMute(){const A=Tt.get("isCalling"),e=St.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=Tt.get("isHold"),{sessionId:e}=St;if(!e)return;if(!Tt.get("actionConfigs").includes("hold"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿保持"}));if(A)throw new Error(JSON.stringify({code:1,msg:"当前已处于保持状态"}));Ns.__internalSend({type:ie.CALL_HOLD,data:{session_id:e}})}unholdCall(){this.roleMute();const{isHold:A}=Tt.getState(),{sessionId:e}=St;if(!Tt.get("actionConfigs").includes("unhold"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿取消保持"}));if(e){if(!A)throw new Error(JSON.stringify({code:1,msg:"当前未处于保持状态"}));Ns.__internalSend({type:ie.CALL_UNHOLD,data:{session_id:e}})}}sendDTMF(A){if(1!==Tt.get("answerDevice"))throw new Error(JSON.stringify({code:1,msg:"当前设备不支持此功能"}));if(!Tt.get("actionConfigs").includes("dtmf"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿发送按键"}));const{isCalling:e}=Tt.getState();if(!e)throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}));this.sipClient.sendDTMF(A)}cancelCall(){if(1!==Tt.get("answerDevice"))throw new Error(JSON.stringify({code:1,msg:"当前设备不支持此功能"}));if(!Tt.get("actionConfigs").includes("reject"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用拒接"}));const{isCalling:A,direction:e,outCallIsAnswer:t}=Tt.getState();if(!A)throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}));t||(e===ee.INCOMING?this.rejectInCall():e===ee.OUTGOING&&this.rejectOutCall())}async consultCall(A){this.roleMute();const{isCalling:e,outCallIsAnswer:t,consultIsAnswer:s,incomingIsAnswer:i}=Tt.getState(),r=St.sessionId;if(!Tt.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(!r)throw new Error(JSON.stringify({code:1,msg:"sessionId不存在"}));Ns.__internalSend({type:ie.CONSULT_REQUEST,data:{session_id:r,consultation_agent_no:A}})}async answerCall(){if(1!==Tt.get("answerDevice"))throw new Error(JSON.stringify({code:1,msg:"当前设备不支持此功能"}));await this.sipClient.answerCall()}async rejectInCall(){if(1!==Tt.get("answerDevice"))throw new Error(JSON.stringify({code:1,msg:"当前设备不支持此功能"}));const{isCalling:A}=Tt.getState();if(!A)throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}));await this.sipClient.rejectInCall()}async makeCall(A){if(Tt.getState().isCalling)throw new Error(JSON.stringify({code:1,msg:"正在呼叫中,请勿重复点击"}));Tt.updateIsCalling(!0),Tt.updateDirection(ee.OUTGOING),Ns.__internalSend({type:ie.OUT_CALL,data:{customer_phone:A}})}async rejectOutCall(){const{isCalling:A}=Tt.getState();if(1!==Tt.get("answerDevice"))throw new Error(JSON.stringify({code:1,msg:"当前设备不支持此功能"}));if(!A)throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}));await this.sipClient.rejectOutCall()}async hangup(){if(1!==Tt.get("answerDevice"))throw new Error(JSON.stringify({code:1,msg:"当前设备不支持此功能"}));if(!Tt.get("isCalling"))throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}));await this.sipClient.hangup()}mute(){this.roleMute();const A=Tt.get("actionConfigs"),e=Tt.get("isMuted"),t=St.sessionId;if(e)throw new Error(JSON.stringify({code:1,msg:"当前未处于静音状态"}));if(!A.includes("mute"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用静音"}));Ns.__internalSend({type:ie.MUTE,data:{session_id:t}})}unmute(){this.roleMute();const A=Tt.get("actionConfigs"),e=Tt.get("isMuted"),t=St.sessionId;if(!e)throw new Error(JSON.stringify({code:1,msg:"当前未处于未静音状态"}));if(!A.includes("unmute"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用静音"}));Ns.__internalSend({type:ie.UNMUTE,data:{session_id:t}})}consultTransfer(){this.roleMute();const A=Tt.get("consultIsAnswer"),e=St.sessionId;if(!Tt.get("actionConfigs").includes("consult_transfer"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用咨询转移"}));if(!A)throw new Error(JSON.stringify({code:1,msg:"当前未处于咨询中"}));Ns.__internalSend({type:ie.CONSULT_TRANSFER,data:{session_id:e}})}satisfactionEvaluation(){this.roleMute();const A=St.sessionId;if(!Tt.get("actionConfigs").includes("satisfaction"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用满意度评价"}));Ns.__internalSend({type:ie.SATISFACTION_EVALUATION,data:{session_id:A}})}startConference(){this.roleMute();if(!Tt.get("actionConfigs").includes("conference"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用三方会议"}));const A=St.sessionId;if(!Tt.get("consultIsAnswer"))throw new Error(JSON.stringify({code:1,msg:"当前未处于咨询中"}));Ns.__internalSend({type:ie.CONSULT_THREE_WAY,data:{session_id:A}})}consultRetrieve(){this.roleMute();if(!Tt.get("actionConfigs").includes("consult_retrieve"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿取回通话"}));const A=St.sessionId;if(!Tt.get("consultIsAnswer"))throw new Error(JSON.stringify({code:1,msg:"当前未处于咨询中"}));Ns.__internalSend({type:ie.CONSULT_RETURN_CALL,data:{session_id:A}})}async transferAgent(A){this.roleMute();if(!Tt.get("actionConfigs").includes("transfer"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿转接"}));const{transferIsAnswer:e}=Tt.getState(),t=St.sessionId;if(e)throw new Error(JSON.stringify({code:1,msg:"当前正在转接"}));Ns.__internalSend({type:ie.TRANSFER,data:{session_id:t,transfer_type:0,transfer_dest:A}})}async transferOutline(A){this.roleMute();if(!Tt.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}=Tt.getState(),t=St.sessionId;if(e)throw new Error(JSON.stringify({code:1,msg:"当前正在转接"}));if(e)throw new Error(JSON.stringify({code:1,msg:"当前正在转接"}));Ns.__internalSend({type:ie.TRANSFER,data:{session_id:t,transfer_type:2,transfer_dest:A}})}}class bs{#A=null;#e=new Us;#t;#s=null;#i=!1;#r;constructor(){clearInterval(this.#s),this.#s=setInterval(async()=>{const A=await this.#o();ds.updateRttObject(A)},2e3),Ks.requestMediaPermissions(),window.addEventListener("beforeunload",()=>{this.destroy()}),this.#r=Tt.subscribeKey("answerDevice",A=>{1===A&&this.initWebRtc()})}async initWebRtc(){const A=St.agentInfo.sip_account_list.find(A=>1===A.device_type);if(A){const e=A.sip_register_addr;this.#A=new us({server:e.split(":")[0],user:A.sip_account,password:A.sip_password,webSocket:`wss://${e}`,callTimeout:3e4},this.#n),await this.#A.start()}}async init({actionNodeParams:A,url:e,appKey:t,appSecret:s,agentNo:i,enableBrowserAlert:r}){try{const{viewHtmlElement:o,rttHTML:n}=A||{},a=e||"https://aicc.yescloudy.com";cs=a,hs=t,ls=s;const V=ns(t,s);await this.#e._agentLogin(i);const{agent_no:c,current_answer_device:h,init_state:l}=St.agentInfo;l===Ae.IDLE?(Tt.updateActionConfigs(mt),St.stateObject={state:l,state_name:"空闲"}):l===Ae.BUSY&&(Tt.updateActionConfigs(Ct),St.stateObject={state:l,state_name:"忙碌"});let g="";const q=new URL(a).host;g=a.includes("https")?`wss://${q}`:`ws://${q}`,Ns.getInstance(`${g}/ccs-ws/api/v1/ws/call/${c}?Authorization=${V}`),Tt.updateAnswerDevice(h),1===h&&await this.initWebRtc(),this.#t=new Js(this.#A),A&&o&&(this.#i=!0,new vs({container:o,rttHTML:n,statusParams:{state:Ae.IDLE,statusName:"空闲"}},this,this.#t)),St.enableBrowserAlert=r,r&&Ks.requestPermission()}catch(A){this.#i&&Fs("初始化失败"),console.log(A)}}#n=A=>{const{type:e,data:t}=A;ae.emit(e,t)};#a=A=>{};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())}destroy(){this.destroyRtc(),clearInterval(this.#s),xs.off("ws-message",this.#a),Ns.__internalClose(),this.offAll(),this.#r?.(),this.#A=null,this.#e=null,this.#i=!1}async#o(){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(A=>this.#t.makeCall(A)),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))}}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))}}getAgentData(){const{agent_no:A,agent_name:e,answer_devices:t,init_state:s,default_device:i}=St.agentInfo,{stateObject:r}=St,o={1:"软电话",2:"手机模式",3:"SIP话机"};return{agent_no:A,agent_name:e,init_state:s,default_device:i,answer_devices:(t||"").split(",").map(A=>({value:Number(A),label:o[Number(A)]})),stateObject:r}}}export{Ae as AgentStatus,se as CallSipCallEvent,ee as DirectionEnum,oe as DisplayTextEnum,re as EventName,te as WebRtcCallSipEventEnum,ie as WebsocketTypeEnum,bs as default};
@@ -873,4 +873,4 @@ const ms=A=>(e,t)=>{void 0!==t?t.addInitializer(()=>{customElements.define(A,e)}
873
873
  `:""}
874
874
  </div>
875
875
  </my-popover>
876
- `;qt(s,this.rttHTML)}}const xs=new class{constructor(){this.events={}}on(A,e){this.events[A]=this.events[A]||[],this.events[A].push(e)}off(A,e){this.events[A]=(this.events[A]||[]).filter(A=>A!==e)}emit(A,...e){(this.events[A]||[]).forEach(A=>A(...e))}};class Ks{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 console.log("已授权麦克风权限"),null;const e=await navigator.mediaDevices.getUserMedia({audio:!0});return console.log("获取音频权限成功"),e}catch(A){return console.warn("获取音频权限失败:",A),null}}static show(A,e,t){if("Notification"in window&&"granted"===Notification.permission){const s=new Notification(A,{body:e,tag:"xy-global-notification",renotify:!0});s.onclick=()=>{window.focus(),s.close(),t?.()},setTimeout(()=>s.close(),5e3)}}static async checkMediaPermissions(){try{return(await navigator.mediaDevices.enumerateDevices()).some(A=>"audioinput"===A.kind&&""!==A.label)}catch(A){return console.warn("无法检查设备权限:",A),!1}}}let Os=null;class Ns{constructor(A){this.url=A,this.ws=null,this.heartbeatWorker=null,this.reconnectDelay=3e3,this.manualClose=!1,this.apiClient=new Us,this.initWebSocket()}static getInstance(A){if(!Os){if(!A)throw new Error("WebSocket 尚未初始化");Os=new Ns(A)}return Os}stateIdleChange(){const{state:e}=yt.stateObject;e!==A.AgentStatus.IDLE&&this.apiClient._changeState({data:{state:A.AgentStatus.IDLE,state_name:"空闲"}})}putAgentState(){yt.sessionId="";const{post_call_process_time:A}=yt.agentInfo,e=A||0;yt.autoStateTimer&&(clearTimeout(yt.autoStateTimer),yt.autoStateTimer=null),e>0?yt.autoStateTimer=setTimeout(()=>{yt.autoStateTimer=null,this.stateIdleChange()},e):this.stateIdleChange()}initWebSocket(){this.ws=new WebSocket(this.url),this.ws.onopen=()=>{this.sendMessage({type:0}),this.startHeartbeat()},this.ws.onmessage=e=>{const t=JSON.parse(e.data),{type:s,data:i,code:r,msg:o}=t;if(0!==s&&console.warn(s),s===A.WebsocketTypeEnum.OUT_CALL&&(0!==r?(Dt.reset(),Ve.emit(A.CallSipCallEvent.OUT_FAILED,o),this.stateIdleChange()):0===r&&Ve.emit(A.CallSipCallEvent.OUT_SUCCESS)),s===A.WebsocketTypeEnum.AGENT_STATE){const{state:e,state_name:t}=i;yt.stateObject={state:e,state_name:t},"振铃中"===t&&Dt.updateDisplayText(A.DisplayTextEnum.AGENT_RINGING),Ve.emit(A.CallSipCallEvent.AGENT_STATE,{state:e,state_name:t})}if(s===A.WebsocketTypeEnum.OUT_CALL_INCOMING_CALL&&(Ve.emit(A.CallSipCallEvent.OUT_INCOMING_CALL),yt.sessionId=i.session_id,Dt.updateActionConfigs(It)),s===A.WebsocketTypeEnum.OUT_CALL_END&&(Dt.updateDisplayText(""),1===i.device_type&&i.agent_no===yt.agentInfo.agent_no?(this.putAgentState(),Dt.reset(),Ve.emit(A.CallSipCallEvent.OUT_HANGUP,i),Dt.updateActionConfigs(Ct)):2===i.device_type&&i.agent_no===yt.agentInfo.agent_no?(Dt.reset(),Ve.emit(A.CallSipCallEvent.OUR_SIDE_CONSULT_HANGUP,i),this.putAgentState(),Dt.updateActionConfigs(Ct)):2===i.device_type&&i.agent_no!==yt.agentInfo.agent_no?(Dt.updateDisplayText(A.DisplayTextEnum.CALLING),Ve.emit(A.CallSipCallEvent.OTHER_SIDE_CONSULT_HANGUP,i),Dt.updateActionConfigs(Mt)):3===i.device_type&&i.agent_no===yt.agentInfo.agent_no?(this.putAgentState(),Dt.reset(),Ve.emit(A.CallSipCallEvent.THREE_WAY_OUR_SIDE_HANGUP),Dt.updateActionConfigs(Ct)):4===i.device_type&&i.agent_no===yt.agentInfo.agent_no&&(this.putAgentState(),Dt.reset(),Ve.emit(A.CallSipCallEvent.TRANSFER_END),Dt.updateActionConfigs(Ct))),s===A.WebsocketTypeEnum.OUT_CALL_RINGING&&1===i.type&&(Ve.emit(A.CallSipCallEvent.OUT_RINGING,i),Dt.updateDisplayText(A.DisplayTextEnum.RINGING)),s===A.WebsocketTypeEnum.OUT_CALL_ANSWER&&(Ve.emit(A.CallSipCallEvent.OUT_OFF_ANSWER,i),Dt.updateOutCallIsAnswer(!0),Dt.updateDisplayText(A.DisplayTextEnum.CALLING),Dt.updateActionConfigs(Mt)),s===A.WebsocketTypeEnum.CONSULT_RINGING&&(Ve.emit(A.CallSipCallEvent.CONSULT_RINGING),Dt.updateDisplayText(A.DisplayTextEnum.AGENT_RINGING),Dt.updateActionConfigs([])),s===A.WebsocketTypeEnum.CONSULT_ANSWER&&(Ve.emit(A.CallSipCallEvent.CONSULT_OFF_HOOK,i),Dt.updateConsultIsAnswer(!0),Dt.updateDisplayText(A.DisplayTextEnum.CONSULTING),Dt.updateActionConfigs(wt)),s===A.WebsocketTypeEnum.CONSULT_FAIL&&(Ve.emit(A.CallSipCallEvent.CONSULT_FAILED,{custom_reason:i.custom_reason}),Dt.updateDisplayText(A.DisplayTextEnum.CALLING),Dt.updateActionConfigs(Mt)),s===A.WebsocketTypeEnum.CONSULT_CALL_IN&&(Ve.emit(A.CallSipCallEvent.CONSULT_INCOMING,i),Dt.updateIsCalling(!0),Dt.updateDirection(A.DirectionEnum.CONSULT),Dt.updateDisplayText(A.DisplayTextEnum.CONSULT_CALL_IN),Dt.updateActionConfigs(Qt),yt.enableBrowserAlert&&Ks.show("📞 咨询来电",`坐席 [${i.agent_name}] 正在咨询...`,()=>{Ve.emit(A.EventName.NOTIFICATION_CLICK,{agent_mane:i.agent_name})})),s===A.WebsocketTypeEnum.CONSULT_CALL_IN_SUCCESS&&(yt.sessionId=i.session_id,Ve.emit(A.CallSipCallEvent.CONSULT_CALL_IN_SUCCESS),Dt.updateConsultIsAnswer(!0),Dt.updateDisplayText(A.DisplayTextEnum.CALLING),Dt.updateActionConfigs(ft)),s===A.WebsocketTypeEnum.CONSULT_RETURN_CALL_SUCCESS&&(Ve.emit(A.CallSipCallEvent.CONSULT_RETURN_CALL_SUCCESS),Dt.updateDisplayText(A.DisplayTextEnum.CALLING),Ve.emit(A.CallSipCallEvent.OTHER_SIDE_CONSULT_HANGUP,i),Dt.updateActionConfigs(Mt)),s===A.WebsocketTypeEnum.MUTE&&(0===r?(Dt.updateIsMuted(!0),Ve.emit(A.CallSipCallEvent.MUTE_SUCCESS),Dt.updateDisplayText(A.DisplayTextEnum.MUTING)):Ve.emit(A.CallSipCallEvent.MUTE_FAILED,o)),s===A.WebsocketTypeEnum.UNMUTE&&(0===r?(Dt.updateIsMuted(!1),Ve.emit(A.CallSipCallEvent.UNMUTE_SUCCESS),Dt.updateDisplayText(A.DisplayTextEnum.CALLING)):Ve.emit(A.CallSipCallEvent.UNMUTE_FAILED,o)),s===A.WebsocketTypeEnum.CALL_HOLD&&(0===r?(Dt.updateIsHold(!0),Dt.updateDisplayText(A.DisplayTextEnum.HOLDING),Ve.emit(A.CallSipCallEvent.HOLD_SUCCESS)):Ve.emit(A.CallSipCallEvent.HOLD_FAILED,o)),s===A.WebsocketTypeEnum.CALL_UNHOLD&&(0===r?(Dt.updateDisplayText(A.DisplayTextEnum.CALLING),Dt.updateIsHold(!1),Ve.emit(A.CallSipCallEvent.UNHOLD_SUCCESS)):Ve.emit(A.CallSipCallEvent.UNHOLD_FAILED,o)),s===A.WebsocketTypeEnum.CONSULT_TRANSFER&&(0===r?Ve.emit(A.CallSipCallEvent.CONSULT_TRANSFER_SUCCESS):Ve.emit(A.CallSipCallEvent.CONSULT_TRANSFER_FAILED,o)),s===A.WebsocketTypeEnum.CONSULT_TRANSFER_FAILED&&Ve.emit(A.CallSipCallEvent.CONSULT_TRANSFER_FAILED,i.status),s===A.WebsocketTypeEnum.CONSULT_TRANSFER_OFF_HOOK&&(Dt.updateDirection(A.DirectionEnum.INCOMING),Dt.updateDisplayText(A.DisplayTextEnum.CALLING),Ve.emit(A.CallSipCallEvent.CONSULT_TRANSFER_OFF_HOOK),Dt.updateActionConfigs(St),Dt.updateIncomingIsAnswer(!0),Dt.updateConsultIsAnswer(!1)),s===A.WebsocketTypeEnum.INCOMING_CALL&&(yt.sessionId=i.session_id,Dt.updateIsCalling(!0),Dt.updateDirection(A.DirectionEnum.INCOMING),Dt.updateDisplayText(A.DisplayTextEnum.INCOMING_CALL),Ve.emit(A.CallSipCallEvent.INCOMING_CALL,{customer_phone:i.customer_phone}),Dt.updateActionConfigs(Qt),yt.enableBrowserAlert&&Ks.show("📞 客户来电",`客户 [${i.customer_phone}] 正在呼入...`,()=>{Ve.emit(A.EventName.NOTIFICATION_CLICK,{customer_phone:i.customer_phone})})),s===A.WebsocketTypeEnum.INCOMING_CALL_OFF_HOOK&&(Dt.updateIncomingIsAnswer(!0),Ve.emit(A.CallSipCallEvent.INCOMING_CALL_OFF_HOOK),Dt.updateActionConfigs(St),Dt.updateDisplayText(A.DisplayTextEnum.CALLING)),s===A.WebsocketTypeEnum.INCOMING_CALL_END&&(Dt.updateDisplayText(""),1===i.device_type&&i.agent_no===yt.agentInfo.agent_no?(this.putAgentState(),Dt.reset(),Ve.emit(A.CallSipCallEvent.INCOMING_CALL_END),Dt.updateActionConfigs(Ct)):2===i.device_type&&i.agent_no===yt.agentInfo.agent_no?(Dt.reset(),Ve.emit(A.CallSipCallEvent.OUR_SIDE_CONSULT_HANGUP,i),this.putAgentState(),Dt.updateActionConfigs(Ct)):2===i.device_type&&i.agent_no!==yt.agentInfo.agent_no?(Dt.updateDisplayText(A.DisplayTextEnum.CALLING),Ve.emit(A.CallSipCallEvent.OTHER_SIDE_CONSULT_HANGUP,i),Dt.updateActionConfigs(Mt)):3===i.device_type&&i.agent_no===yt.agentInfo.agent_no?(this.putAgentState(),Dt.reset(),Ve.emit(A.CallSipCallEvent.THREE_WAY_OUR_SIDE_HANGUP),Dt.updateActionConfigs(Ct)):4===i.device_type&&i.agent_no===yt.agentInfo.agent_no&&(this.putAgentState(),Dt.reset(),Ve.emit(A.CallSipCallEvent.TRANSFER_END),Dt.updateActionConfigs(Ct))),s===A.WebsocketTypeEnum.SATISFACTION_EVALUATION&&(0===r?Ve.emit(A.CallSipCallEvent.SATISFACTION_EVALUATION_SUCCESS):Ve.emit(A.CallSipCallEvent.SATISFACTION_EVALUATION_FAILED,o)),s===A.WebsocketTypeEnum.CONSULT_THREE_WAY&&(0!==r?Ve.emit(A.CallSipCallEvent.CONSULT_THREE_WAY_FAILED,o):0===r&&Dt.updateIsMeeting(!0)),s===A.WebsocketTypeEnum.CONSULT_THREE_WAY_RESULT&&("success"===i.status?(Dt.updateDirection(A.DirectionEnum.MEETING),Dt.updateDisplayText(A.DisplayTextEnum.MEETING),Dt.updateActionConfigs(Bt),Ve.emit(A.CallSipCallEvent.CONSULT_THREE_WAY_SUCCESS)):(Ve.emit(A.CallSipCallEvent.CONSULT_THREE_WAY_FAILED,{conf_name:i.conf_name}),Dt.updateIsMeeting(!1))),s===A.WebsocketTypeEnum.TRANSFER_INCOMING_CALL&&(i.agent_no===yt.agentInfo.agent_no?(Dt.updateDisplayText(A.DisplayTextEnum.TRANSFERRING),Ve.emit(A.CallSipCallEvent.TRANSFER_PROGRESS),Dt.updateActionConfigs([]),Dt.updateTransferIsAnswer(!0)):(Ve.emit(A.CallSipCallEvent.TRANSFER_INCOMING_CALL,i),Dt.updateIsCalling(!0),Dt.updateDirection(A.DirectionEnum.TRANSFER),Dt.updateDisplayText(A.DisplayTextEnum.TRANSFER_INCOMING_CALL),Dt.updateActionConfigs(Qt),yt.enableBrowserAlert&&Ks.show("📞 转接来电",`坐席 [${i.agent_no}] 正在转接...`,()=>{Ve.emit(A.EventName.NOTIFICATION_CLICK,{agent_mane:i.agent_no})}))),s===A.WebsocketTypeEnum.TRANSFER_OFF_HOOK&&(yt.sessionId=i.session_id,Ve.emit(A.CallSipCallEvent.TRANSFER_OFF_HOOK),Dt.updateTransferIsAnswer(!0),Dt.updateDisplayText(A.DisplayTextEnum.CALLING),Dt.updateActionConfigs(St)),s===A.WebsocketTypeEnum.TRANSFER_FAILED&&"failed"===i.status){Ve.emit(A.CallSipCallEvent.TRANSFER_FAILED,"未知原因");const e=Dt.get("direction");e===A.DirectionEnum.OUTGOING?Dt.updateActionConfigs(Mt):e===A.DirectionEnum.INCOMING&&Dt.updateActionConfigs(St)}0!==s&&xs.emit("ws-message",t)},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 }, 14000);\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=()=>{this.sendMessage({type:0})},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)):console.warn("WebSocket 未连接,消息未发送")}close(){this.manualClose=!0,this.stopHeartbeat(),this.ws?.close()}static __internalClose(){Os&&(Os.close(),Os=null)}static __internalSend(A){if(!Os)throw new Error("WebSocket 尚未初始化");Os.sendMessage(A)}}class bs{constructor(A){this.sipClient=null,this.sipClient=A}roleMute(){const A=Dt.get("isCalling"),e=yt.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 e=Dt.get("isHold"),{sessionId:t}=yt;if(!t)return;if(!Dt.get("actionConfigs").includes("hold"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿保持"}));if(e)throw new Error(JSON.stringify({code:1,msg:"当前已处于保持状态"}));Ns.__internalSend({type:A.WebsocketTypeEnum.CALL_HOLD,data:{session_id:t}})}unholdCall(){this.roleMute();const{isHold:e}=Dt.getState(),{sessionId:t}=yt;if(!Dt.get("actionConfigs").includes("unhold"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿取消保持"}));if(t){if(!e)throw new Error(JSON.stringify({code:1,msg:"当前未处于保持状态"}));Ns.__internalSend({type:A.WebsocketTypeEnum.CALL_UNHOLD,data:{session_id:t}})}}sendDTMF(A){if(1!==Dt.get("answerDevice"))throw new Error(JSON.stringify({code:1,msg:"当前设备不支持此功能"}));if(!Dt.get("actionConfigs").includes("dtmf"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿发送按键"}));const{isCalling:e}=Dt.getState();if(!e)throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}));this.sipClient.sendDTMF(A)}cancelCall(){if(1!==Dt.get("answerDevice"))throw new Error(JSON.stringify({code:1,msg:"当前设备不支持此功能"}));if(!Dt.get("actionConfigs").includes("reject"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用拒接"}));const{isCalling:e,direction:t,outCallIsAnswer:s}=Dt.getState();if(!e)throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}));s||(t===A.DirectionEnum.INCOMING?this.rejectInCall():t===A.DirectionEnum.OUTGOING&&this.rejectOutCall())}async consultCall(e){this.roleMute();const{isCalling:t,outCallIsAnswer:s,consultIsAnswer:i,incomingIsAnswer:r}=Dt.getState(),o=yt.sessionId;if(!Dt.get("actionConfigs").includes("consult"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用咨询"}));if(i)throw new Error(JSON.stringify({code:1,msg:"当前正在咨询"}));if(!(t||s||r))throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}));if(!o)throw new Error(JSON.stringify({code:1,msg:"sessionId不存在"}));Ns.__internalSend({type:A.WebsocketTypeEnum.CONSULT_REQUEST,data:{session_id:o,consultation_agent_no:e}})}async answerCall(){if(1!==Dt.get("answerDevice"))throw new Error(JSON.stringify({code:1,msg:"当前设备不支持此功能"}));await this.sipClient.answerCall()}async rejectInCall(){if(1!==Dt.get("answerDevice"))throw new Error(JSON.stringify({code:1,msg:"当前设备不支持此功能"}));const{isCalling:A}=Dt.getState();if(!A)throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}));await this.sipClient.rejectInCall()}async makeCall(e){if(Dt.getState().isCalling)throw new Error(JSON.stringify({code:1,msg:"正在呼叫中,请勿重复点击"}));Dt.updateIsCalling(!0),Dt.updateDirection(A.DirectionEnum.OUTGOING),Ns.__internalSend({type:A.WebsocketTypeEnum.OUT_CALL,data:{customer_phone:e}})}async rejectOutCall(){const{isCalling:A}=Dt.getState();if(1!==Dt.get("answerDevice"))throw new Error(JSON.stringify({code:1,msg:"当前设备不支持此功能"}));if(!A)throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}));await this.sipClient.rejectOutCall()}async hangup(){if(1!==Dt.get("answerDevice"))throw new Error(JSON.stringify({code:1,msg:"当前设备不支持此功能"}));if(!Dt.get("isCalling"))throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}));await this.sipClient.hangup()}mute(){this.roleMute();const e=Dt.get("actionConfigs"),t=Dt.get("isMuted"),s=yt.sessionId;if(t)throw new Error(JSON.stringify({code:1,msg:"当前未处于静音状态"}));if(!e.includes("mute"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用静音"}));Ns.__internalSend({type:A.WebsocketTypeEnum.MUTE,data:{session_id:s}})}unmute(){this.roleMute();const e=Dt.get("actionConfigs"),t=Dt.get("isMuted"),s=yt.sessionId;if(!t)throw new Error(JSON.stringify({code:1,msg:"当前未处于未静音状态"}));if(!e.includes("unmute"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用静音"}));Ns.__internalSend({type:A.WebsocketTypeEnum.UNMUTE,data:{session_id:s}})}consultTransfer(){this.roleMute();const e=Dt.get("consultIsAnswer"),t=yt.sessionId;if(!Dt.get("actionConfigs").includes("consult_transfer"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用咨询转移"}));if(!e)throw new Error(JSON.stringify({code:1,msg:"当前未处于咨询中"}));Ns.__internalSend({type:A.WebsocketTypeEnum.CONSULT_TRANSFER,data:{session_id:t}})}satisfactionEvaluation(){this.roleMute();const e=yt.sessionId;if(!Dt.get("actionConfigs").includes("satisfaction"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用满意度评价"}));Ns.__internalSend({type:A.WebsocketTypeEnum.SATISFACTION_EVALUATION,data:{session_id:e}})}startConference(){this.roleMute();if(!Dt.get("actionConfigs").includes("conference"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用三方会议"}));const e=yt.sessionId;if(!Dt.get("consultIsAnswer"))throw new Error(JSON.stringify({code:1,msg:"当前未处于咨询中"}));Ns.__internalSend({type:A.WebsocketTypeEnum.CONSULT_THREE_WAY,data:{session_id:e}})}consultRetrieve(){this.roleMute();if(!Dt.get("actionConfigs").includes("consult_retrieve"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿取回通话"}));const e=yt.sessionId;if(!Dt.get("consultIsAnswer"))throw new Error(JSON.stringify({code:1,msg:"当前未处于咨询中"}));Ns.__internalSend({type:A.WebsocketTypeEnum.CONSULT_RETURN_CALL,data:{session_id:e}})}async transferAgent(e){this.roleMute();if(!Dt.get("actionConfigs").includes("transfer"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿转接"}));const{transferIsAnswer:t}=Dt.getState(),s=yt.sessionId;if(t)throw new Error(JSON.stringify({code:1,msg:"当前正在转接"}));Ns.__internalSend({type:A.WebsocketTypeEnum.TRANSFER,data:{session_id:s,transfer_type:0,transfer_dest:e}})}async transferOutline(e){this.roleMute();if(!Dt.get("actionConfigs").includes("transfer"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿转接"}));if(!e)throw new Error(JSON.stringify({code:1,msg:"号码不能为空"}));if(!/^\d+$/.test(e))throw new Error(JSON.stringify({code:1,msg:"号码格式错误"}));const{transferIsAnswer:t}=Dt.getState(),s=yt.sessionId;if(t)throw new Error(JSON.stringify({code:1,msg:"当前正在转接"}));if(t)throw new Error(JSON.stringify({code:1,msg:"当前正在转接"}));Ns.__internalSend({type:A.WebsocketTypeEnum.TRANSFER,data:{session_id:s,transfer_type:2,transfer_dest:e}})}}A.default=class{#A=null;#e=new Us;#t;#s=null;#i=!1;#r;constructor(){clearInterval(this.#s),this.#s=setInterval(async()=>{const A=await this.#o();ds.updateRttObject(A)},2e3),Ks.requestMediaPermissions(),window.addEventListener("beforeunload",()=>{this.destroy()}),this.#r=Dt.subscribeKey("answerDevice",A=>{1===A&&this.initWebRtc()})}async initWebRtc(){const A=yt.agentInfo.sip_account_list.find(A=>1===A.device_type);if(A){const e=A.sip_register_addr;this.#A=new us({server:e.split(":")[0],user:A.sip_account,password:A.sip_password,webSocket:`wss://${e}`,callTimeout:3e4},this.#n),await this.#A.start()}}async init({actionNodeParams:e,url:t,appKey:s,appSecret:i,agentNo:r,enableBrowserAlert:o}){try{const{viewHtmlElement:n,rttHTML:a}=e||{},V=t||"https://aicc.yescloudy.com";cs=V,hs=s,ls=i;const c=ns(s,i);await this.#e._agentLogin(r);const{agent_no:h,current_answer_device:l,init_state:g}=yt.agentInfo;g===A.AgentStatus.IDLE?(Dt.updateActionConfigs(Ct),yt.stateObject={state:g,state_name:"空闲"}):g===A.AgentStatus.BUSY&&(Dt.updateActionConfigs(Et),yt.stateObject={state:g,state_name:"忙碌"});let q="";const d=new URL(V).host;q=V.includes("https")?`wss://${d}`:`ws://${d}`,Ns.getInstance(`${q}/ccs-ws/api/v1/ws/call/${h}?Authorization=${c}`),Dt.updateAnswerDevice(l),1===l&&await this.initWebRtc(),this.#t=new bs(this.#A),e&&n&&(this.#i=!0,new vs({container:n,rttHTML:a,statusParams:{state:A.AgentStatus.IDLE,statusName:"空闲"}},this,this.#t)),yt.enableBrowserAlert=o,o&&Ks.requestPermission()}catch(A){this.#i&&Fs("初始化失败"),console.log(A)}}#n=A=>{const{type:e,data:t}=A;Ve.emit(e,t)};#a=A=>{};on(A,e){return Ve.on(A,e),this}off(A,e){Ve.off(A,e)}offAll(){Ve.clearAllListeners()}async destroyRtc(){await(this.#A?.destroy())}destroy(){this.destroyRtc(),clearInterval(this.#s),xs.off("ws-message",this.#a),Ns.__internalClose(),this.offAll(),this.#r?.(),this.#A=null,this.#e=null,this.#i=!1}async#o(){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(A=>this.#t.makeCall(A)),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))}}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))}}getAgentData(){const{agent_no:A,agent_name:e,answer_devices:t,init_state:s,default_device:i}=yt.agentInfo,{stateObject:r}=yt,o={1:"软电话",2:"手机模式",3:"SIP话机"};return{agent_no:A,agent_name:e,init_state:s,default_device:i,answer_devices:(t||"").split(",").map(A=>({value:Number(A),label:o[Number(A)]})),stateObject:r}}},Object.defineProperty(A,"__esModule",{value:!0})});
876
+ `;qt(s,this.rttHTML)}}const xs=new class{constructor(){this.events={}}on(A,e){this.events[A]=this.events[A]||[],this.events[A].push(e)}off(A,e){this.events[A]=(this.events[A]||[]).filter(A=>A!==e)}emit(A,...e){(this.events[A]||[]).forEach(A=>A(...e))}};class Ks{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 console.log("已授权麦克风权限"),null;const e=await navigator.mediaDevices.getUserMedia({audio:!0});return console.log("获取音频权限成功"),e}catch(A){return console.warn("获取音频权限失败:",A),null}}static show(A,e,t){if("Notification"in window&&"granted"===Notification.permission){const s=new Notification(A,{body:e,tag:"xy-global-notification",renotify:!0});s.onclick=()=>{window.focus(),s.close(),t?.()},setTimeout(()=>s.close(),5e3)}}static async checkMediaPermissions(){try{return(await navigator.mediaDevices.enumerateDevices()).some(A=>"audioinput"===A.kind&&""!==A.label)}catch(A){return console.warn("无法检查设备权限:",A),!1}}}let Os=null;class Ns{constructor(A){this.url=A,this.ws=null,this.heartbeatWorker=null,this.reconnectDelay=3e3,this.manualClose=!1,this.apiClient=new Us,this.initWebSocket()}static getInstance(A){if(!Os){if(!A)throw new Error("WebSocket 尚未初始化");Os=new Ns(A)}return Os}stateIdleChange(){const{state:e}=yt.stateObject;e!==A.AgentStatus.IDLE&&this.apiClient._changeState({data:{state:A.AgentStatus.IDLE,state_name:"空闲"}})}putAgentState(){yt.sessionId="";const{post_call_process_time:A}=yt.agentInfo,e=A||0;yt.autoStateTimer&&(clearTimeout(yt.autoStateTimer),yt.autoStateTimer=null),e>0?yt.autoStateTimer=setTimeout(()=>{yt.autoStateTimer=null,this.stateIdleChange()},e):this.stateIdleChange()}initWebSocket(){this.ws=new WebSocket(this.url),this.ws.onopen=()=>{this.sendMessage({type:0}),this.startHeartbeat()},this.ws.onmessage=e=>{const t=JSON.parse(e.data),{type:s,data:i,code:r,msg:o}=t;if(0!==s&&console.warn(s),s===A.WebsocketTypeEnum.OUT_CALL&&(0!==r?(Dt.reset(),Ve.emit(A.CallSipCallEvent.OUT_FAILED,o),this.stateIdleChange()):0===r&&Ve.emit(A.CallSipCallEvent.OUT_SUCCESS)),s===A.WebsocketTypeEnum.AGENT_STATE){const{state:e,state_name:t}=i;yt.stateObject={state:e,state_name:t},"振铃中"===t&&Dt.updateDisplayText(A.DisplayTextEnum.AGENT_RINGING),Ve.emit(A.CallSipCallEvent.AGENT_STATE,{state:e,state_name:t})}if(s===A.WebsocketTypeEnum.OUT_CALL_INCOMING_CALL&&(Ve.emit(A.CallSipCallEvent.OUT_INCOMING_CALL),yt.sessionId=i.session_id,Dt.updateActionConfigs(It)),s===A.WebsocketTypeEnum.OUT_CALL_END&&(1===i.device_type&&i.agent_no===yt.agentInfo.agent_no?(this.putAgentState(),Dt.reset(),Ve.emit(A.CallSipCallEvent.OUT_HANGUP,i),Dt.updateActionConfigs(Ct)):2===i.device_type&&i.agent_no===yt.agentInfo.agent_no?(Dt.reset(),Ve.emit(A.CallSipCallEvent.OUR_SIDE_CONSULT_HANGUP,i),this.putAgentState(),Dt.updateActionConfigs(Ct)):2===i.device_type&&i.agent_no!==yt.agentInfo.agent_no?(Dt.updateDisplayText(A.DisplayTextEnum.CALLING),Ve.emit(A.CallSipCallEvent.OTHER_SIDE_CONSULT_HANGUP,i),Dt.updateActionConfigs(Mt)):3===i.device_type&&i.agent_no===yt.agentInfo.agent_no?(this.putAgentState(),Dt.reset(),Ve.emit(A.CallSipCallEvent.THREE_WAY_OUR_SIDE_HANGUP),Dt.updateActionConfigs(Ct)):4===i.device_type&&i.agent_no===yt.agentInfo.agent_no&&(this.putAgentState(),Dt.reset(),Ve.emit(A.CallSipCallEvent.TRANSFER_END),Dt.updateActionConfigs(Ct))),s===A.WebsocketTypeEnum.OUT_CALL_RINGING&&1===i.type&&(Ve.emit(A.CallSipCallEvent.OUT_RINGING,i),Dt.updateDisplayText(A.DisplayTextEnum.RINGING)),s===A.WebsocketTypeEnum.OUT_CALL_ANSWER&&(Ve.emit(A.CallSipCallEvent.OUT_OFF_ANSWER,i),Dt.updateOutCallIsAnswer(!0),Dt.updateDisplayText(A.DisplayTextEnum.CALLING),Dt.updateActionConfigs(Mt)),s===A.WebsocketTypeEnum.CONSULT_RINGING&&(Ve.emit(A.CallSipCallEvent.CONSULT_RINGING),Dt.updateDisplayText(A.DisplayTextEnum.AGENT_RINGING),Dt.updateActionConfigs([])),s===A.WebsocketTypeEnum.CONSULT_ANSWER&&(Ve.emit(A.CallSipCallEvent.CONSULT_OFF_HOOK,i),Dt.updateConsultIsAnswer(!0),Dt.updateDisplayText(A.DisplayTextEnum.CONSULTING),Dt.updateActionConfigs(wt)),s===A.WebsocketTypeEnum.CONSULT_FAIL&&(Ve.emit(A.CallSipCallEvent.CONSULT_FAILED,{custom_reason:i.custom_reason}),Dt.updateDisplayText(A.DisplayTextEnum.CALLING),Dt.updateActionConfigs(Mt)),s===A.WebsocketTypeEnum.CONSULT_CALL_IN&&(Ve.emit(A.CallSipCallEvent.CONSULT_INCOMING,i),Dt.updateIsCalling(!0),Dt.updateDirection(A.DirectionEnum.CONSULT),Dt.updateDisplayText(A.DisplayTextEnum.CONSULT_CALL_IN),Dt.updateActionConfigs(Qt),yt.enableBrowserAlert&&Ks.show("📞 咨询来电",`坐席 [${i.agent_name}] 正在咨询...`,()=>{Ve.emit(A.EventName.NOTIFICATION_CLICK,{agent_mane:i.agent_name})})),s===A.WebsocketTypeEnum.CONSULT_CALL_IN_SUCCESS&&(yt.sessionId=i.session_id,Ve.emit(A.CallSipCallEvent.CONSULT_CALL_IN_SUCCESS),Dt.updateConsultIsAnswer(!0),Dt.updateDisplayText(A.DisplayTextEnum.CALLING),Dt.updateActionConfigs(ft)),s===A.WebsocketTypeEnum.CONSULT_RETURN_CALL_SUCCESS&&(Ve.emit(A.CallSipCallEvent.CONSULT_RETURN_CALL_SUCCESS),Dt.updateDisplayText(A.DisplayTextEnum.CALLING),Ve.emit(A.CallSipCallEvent.OTHER_SIDE_CONSULT_HANGUP,i),Dt.updateActionConfigs(Mt)),s===A.WebsocketTypeEnum.MUTE&&(0===r?(Dt.updateIsMuted(!0),Ve.emit(A.CallSipCallEvent.MUTE_SUCCESS),Dt.updateDisplayText(A.DisplayTextEnum.MUTING)):Ve.emit(A.CallSipCallEvent.MUTE_FAILED,o)),s===A.WebsocketTypeEnum.UNMUTE&&(0===r?(Dt.updateIsMuted(!1),Ve.emit(A.CallSipCallEvent.UNMUTE_SUCCESS),Dt.updateDisplayText(A.DisplayTextEnum.CALLING)):Ve.emit(A.CallSipCallEvent.UNMUTE_FAILED,o)),s===A.WebsocketTypeEnum.CALL_HOLD&&(0===r?(Dt.updateIsHold(!0),Dt.updateDisplayText(A.DisplayTextEnum.HOLDING),Ve.emit(A.CallSipCallEvent.HOLD_SUCCESS)):Ve.emit(A.CallSipCallEvent.HOLD_FAILED,o)),s===A.WebsocketTypeEnum.CALL_UNHOLD&&(0===r?(Dt.updateDisplayText(A.DisplayTextEnum.CALLING),Dt.updateIsHold(!1),Ve.emit(A.CallSipCallEvent.UNHOLD_SUCCESS)):Ve.emit(A.CallSipCallEvent.UNHOLD_FAILED,o)),s===A.WebsocketTypeEnum.CONSULT_TRANSFER&&(0===r?Ve.emit(A.CallSipCallEvent.CONSULT_TRANSFER_SUCCESS):Ve.emit(A.CallSipCallEvent.CONSULT_TRANSFER_FAILED,o)),s===A.WebsocketTypeEnum.CONSULT_TRANSFER_FAILED&&Ve.emit(A.CallSipCallEvent.CONSULT_TRANSFER_FAILED,i.status),s===A.WebsocketTypeEnum.CONSULT_TRANSFER_OFF_HOOK&&(Dt.updateDirection(A.DirectionEnum.INCOMING),Dt.updateDisplayText(A.DisplayTextEnum.CALLING),Ve.emit(A.CallSipCallEvent.CONSULT_TRANSFER_OFF_HOOK),Dt.updateActionConfigs(St),Dt.updateIncomingIsAnswer(!0),Dt.updateConsultIsAnswer(!1)),s===A.WebsocketTypeEnum.INCOMING_CALL&&(yt.sessionId=i.session_id,Dt.updateIsCalling(!0),Dt.updateDirection(A.DirectionEnum.INCOMING),Dt.updateDisplayText(A.DisplayTextEnum.INCOMING_CALL),Ve.emit(A.CallSipCallEvent.INCOMING_CALL,{customer_phone:i.customer_phone}),Dt.updateActionConfigs(Qt),yt.enableBrowserAlert&&Ks.show("📞 客户来电",`客户 [${i.customer_phone}] 正在呼入...`,()=>{Ve.emit(A.EventName.NOTIFICATION_CLICK,{customer_phone:i.customer_phone})})),s===A.WebsocketTypeEnum.INCOMING_CALL_OFF_HOOK&&(Dt.updateIncomingIsAnswer(!0),Ve.emit(A.CallSipCallEvent.INCOMING_CALL_OFF_HOOK),Dt.updateActionConfigs(St),Dt.updateDisplayText(A.DisplayTextEnum.CALLING)),s===A.WebsocketTypeEnum.INCOMING_CALL_END&&(1===i.device_type&&i.agent_no===yt.agentInfo.agent_no?(this.putAgentState(),Dt.reset(),Ve.emit(A.CallSipCallEvent.INCOMING_CALL_END),Dt.updateActionConfigs(Ct)):2===i.device_type&&i.agent_no===yt.agentInfo.agent_no?(Dt.reset(),Ve.emit(A.CallSipCallEvent.OUR_SIDE_CONSULT_HANGUP,i),this.putAgentState(),Dt.updateActionConfigs(Ct)):2===i.device_type&&i.agent_no!==yt.agentInfo.agent_no?(Dt.updateDisplayText(A.DisplayTextEnum.CALLING),Ve.emit(A.CallSipCallEvent.OTHER_SIDE_CONSULT_HANGUP,i),Dt.updateActionConfigs(Mt)):3===i.device_type&&i.agent_no===yt.agentInfo.agent_no?(this.putAgentState(),Dt.reset(),Ve.emit(A.CallSipCallEvent.THREE_WAY_OUR_SIDE_HANGUP),Dt.updateActionConfigs(Ct)):4===i.device_type&&i.agent_no===yt.agentInfo.agent_no&&(this.putAgentState(),Dt.reset(),Ve.emit(A.CallSipCallEvent.TRANSFER_END),Dt.updateActionConfigs(Ct))),s===A.WebsocketTypeEnum.SATISFACTION_EVALUATION&&(0===r?Ve.emit(A.CallSipCallEvent.SATISFACTION_EVALUATION_SUCCESS):Ve.emit(A.CallSipCallEvent.SATISFACTION_EVALUATION_FAILED,o)),s===A.WebsocketTypeEnum.CONSULT_THREE_WAY&&(0!==r?Ve.emit(A.CallSipCallEvent.CONSULT_THREE_WAY_FAILED,o):0===r&&Dt.updateIsMeeting(!0)),s===A.WebsocketTypeEnum.CONSULT_THREE_WAY_RESULT&&("success"===i.status?(Dt.updateDirection(A.DirectionEnum.MEETING),Dt.updateDisplayText(A.DisplayTextEnum.MEETING),Dt.updateActionConfigs(Bt),Ve.emit(A.CallSipCallEvent.CONSULT_THREE_WAY_SUCCESS)):(Ve.emit(A.CallSipCallEvent.CONSULT_THREE_WAY_FAILED,{conf_name:i.conf_name}),Dt.updateIsMeeting(!1))),s===A.WebsocketTypeEnum.TRANSFER_INCOMING_CALL&&(i.agent_no===yt.agentInfo.agent_no?(Dt.updateDisplayText(A.DisplayTextEnum.TRANSFERRING),Ve.emit(A.CallSipCallEvent.TRANSFER_PROGRESS),Dt.updateActionConfigs([]),Dt.updateTransferIsAnswer(!0)):(Ve.emit(A.CallSipCallEvent.TRANSFER_INCOMING_CALL,i),Dt.updateIsCalling(!0),Dt.updateDirection(A.DirectionEnum.TRANSFER),Dt.updateDisplayText(A.DisplayTextEnum.TRANSFER_INCOMING_CALL),Dt.updateActionConfigs(Qt),yt.enableBrowserAlert&&Ks.show("📞 转接来电",`坐席 [${i.agent_no}] 正在转接...`,()=>{Ve.emit(A.EventName.NOTIFICATION_CLICK,{agent_mane:i.agent_no})}))),s===A.WebsocketTypeEnum.TRANSFER_OFF_HOOK&&(yt.sessionId=i.session_id,Ve.emit(A.CallSipCallEvent.TRANSFER_OFF_HOOK),Dt.updateTransferIsAnswer(!0),Dt.updateDisplayText(A.DisplayTextEnum.CALLING),Dt.updateActionConfigs(St)),s===A.WebsocketTypeEnum.TRANSFER_FAILED&&"failed"===i.status){Ve.emit(A.CallSipCallEvent.TRANSFER_FAILED,"未知原因");const e=Dt.get("direction");e===A.DirectionEnum.OUTGOING?Dt.updateActionConfigs(Mt):e===A.DirectionEnum.INCOMING&&Dt.updateActionConfigs(St)}0!==s&&xs.emit("ws-message",t)},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 }, 14000);\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=()=>{this.sendMessage({type:0})},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)):console.warn("WebSocket 未连接,消息未发送")}close(){this.manualClose=!0,this.stopHeartbeat(),this.ws?.close()}static __internalClose(){Os&&(Os.close(),Os=null)}static __internalSend(A){if(!Os)throw new Error("WebSocket 尚未初始化");Os.sendMessage(A)}}class bs{constructor(A){this.sipClient=null,this.sipClient=A}roleMute(){const A=Dt.get("isCalling"),e=yt.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 e=Dt.get("isHold"),{sessionId:t}=yt;if(!t)return;if(!Dt.get("actionConfigs").includes("hold"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿保持"}));if(e)throw new Error(JSON.stringify({code:1,msg:"当前已处于保持状态"}));Ns.__internalSend({type:A.WebsocketTypeEnum.CALL_HOLD,data:{session_id:t}})}unholdCall(){this.roleMute();const{isHold:e}=Dt.getState(),{sessionId:t}=yt;if(!Dt.get("actionConfigs").includes("unhold"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿取消保持"}));if(t){if(!e)throw new Error(JSON.stringify({code:1,msg:"当前未处于保持状态"}));Ns.__internalSend({type:A.WebsocketTypeEnum.CALL_UNHOLD,data:{session_id:t}})}}sendDTMF(A){if(1!==Dt.get("answerDevice"))throw new Error(JSON.stringify({code:1,msg:"当前设备不支持此功能"}));if(!Dt.get("actionConfigs").includes("dtmf"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿发送按键"}));const{isCalling:e}=Dt.getState();if(!e)throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}));this.sipClient.sendDTMF(A)}cancelCall(){if(1!==Dt.get("answerDevice"))throw new Error(JSON.stringify({code:1,msg:"当前设备不支持此功能"}));if(!Dt.get("actionConfigs").includes("reject"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用拒接"}));const{isCalling:e,direction:t,outCallIsAnswer:s}=Dt.getState();if(!e)throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}));s||(t===A.DirectionEnum.INCOMING?this.rejectInCall():t===A.DirectionEnum.OUTGOING&&this.rejectOutCall())}async consultCall(e){this.roleMute();const{isCalling:t,outCallIsAnswer:s,consultIsAnswer:i,incomingIsAnswer:r}=Dt.getState(),o=yt.sessionId;if(!Dt.get("actionConfigs").includes("consult"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用咨询"}));if(i)throw new Error(JSON.stringify({code:1,msg:"当前正在咨询"}));if(!(t||s||r))throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}));if(!o)throw new Error(JSON.stringify({code:1,msg:"sessionId不存在"}));Ns.__internalSend({type:A.WebsocketTypeEnum.CONSULT_REQUEST,data:{session_id:o,consultation_agent_no:e}})}async answerCall(){if(1!==Dt.get("answerDevice"))throw new Error(JSON.stringify({code:1,msg:"当前设备不支持此功能"}));await this.sipClient.answerCall()}async rejectInCall(){if(1!==Dt.get("answerDevice"))throw new Error(JSON.stringify({code:1,msg:"当前设备不支持此功能"}));const{isCalling:A}=Dt.getState();if(!A)throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}));await this.sipClient.rejectInCall()}async makeCall(e){if(Dt.getState().isCalling)throw new Error(JSON.stringify({code:1,msg:"正在呼叫中,请勿重复点击"}));Dt.updateIsCalling(!0),Dt.updateDirection(A.DirectionEnum.OUTGOING),Ns.__internalSend({type:A.WebsocketTypeEnum.OUT_CALL,data:{customer_phone:e}})}async rejectOutCall(){const{isCalling:A}=Dt.getState();if(1!==Dt.get("answerDevice"))throw new Error(JSON.stringify({code:1,msg:"当前设备不支持此功能"}));if(!A)throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}));await this.sipClient.rejectOutCall()}async hangup(){if(1!==Dt.get("answerDevice"))throw new Error(JSON.stringify({code:1,msg:"当前设备不支持此功能"}));if(!Dt.get("isCalling"))throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}));await this.sipClient.hangup()}mute(){this.roleMute();const e=Dt.get("actionConfigs"),t=Dt.get("isMuted"),s=yt.sessionId;if(t)throw new Error(JSON.stringify({code:1,msg:"当前未处于静音状态"}));if(!e.includes("mute"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用静音"}));Ns.__internalSend({type:A.WebsocketTypeEnum.MUTE,data:{session_id:s}})}unmute(){this.roleMute();const e=Dt.get("actionConfigs"),t=Dt.get("isMuted"),s=yt.sessionId;if(!t)throw new Error(JSON.stringify({code:1,msg:"当前未处于未静音状态"}));if(!e.includes("unmute"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用静音"}));Ns.__internalSend({type:A.WebsocketTypeEnum.UNMUTE,data:{session_id:s}})}consultTransfer(){this.roleMute();const e=Dt.get("consultIsAnswer"),t=yt.sessionId;if(!Dt.get("actionConfigs").includes("consult_transfer"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用咨询转移"}));if(!e)throw new Error(JSON.stringify({code:1,msg:"当前未处于咨询中"}));Ns.__internalSend({type:A.WebsocketTypeEnum.CONSULT_TRANSFER,data:{session_id:t}})}satisfactionEvaluation(){this.roleMute();const e=yt.sessionId;if(!Dt.get("actionConfigs").includes("satisfaction"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用满意度评价"}));Ns.__internalSend({type:A.WebsocketTypeEnum.SATISFACTION_EVALUATION,data:{session_id:e}})}startConference(){this.roleMute();if(!Dt.get("actionConfigs").includes("conference"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用三方会议"}));const e=yt.sessionId;if(!Dt.get("consultIsAnswer"))throw new Error(JSON.stringify({code:1,msg:"当前未处于咨询中"}));Ns.__internalSend({type:A.WebsocketTypeEnum.CONSULT_THREE_WAY,data:{session_id:e}})}consultRetrieve(){this.roleMute();if(!Dt.get("actionConfigs").includes("consult_retrieve"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿取回通话"}));const e=yt.sessionId;if(!Dt.get("consultIsAnswer"))throw new Error(JSON.stringify({code:1,msg:"当前未处于咨询中"}));Ns.__internalSend({type:A.WebsocketTypeEnum.CONSULT_RETURN_CALL,data:{session_id:e}})}async transferAgent(e){this.roleMute();if(!Dt.get("actionConfigs").includes("transfer"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿转接"}));const{transferIsAnswer:t}=Dt.getState(),s=yt.sessionId;if(t)throw new Error(JSON.stringify({code:1,msg:"当前正在转接"}));Ns.__internalSend({type:A.WebsocketTypeEnum.TRANSFER,data:{session_id:s,transfer_type:0,transfer_dest:e}})}async transferOutline(e){this.roleMute();if(!Dt.get("actionConfigs").includes("transfer"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿转接"}));if(!e)throw new Error(JSON.stringify({code:1,msg:"号码不能为空"}));if(!/^\d+$/.test(e))throw new Error(JSON.stringify({code:1,msg:"号码格式错误"}));const{transferIsAnswer:t}=Dt.getState(),s=yt.sessionId;if(t)throw new Error(JSON.stringify({code:1,msg:"当前正在转接"}));if(t)throw new Error(JSON.stringify({code:1,msg:"当前正在转接"}));Ns.__internalSend({type:A.WebsocketTypeEnum.TRANSFER,data:{session_id:s,transfer_type:2,transfer_dest:e}})}}A.default=class{#A=null;#e=new Us;#t;#s=null;#i=!1;#r;constructor(){clearInterval(this.#s),this.#s=setInterval(async()=>{const A=await this.#o();ds.updateRttObject(A)},2e3),Ks.requestMediaPermissions(),window.addEventListener("beforeunload",()=>{this.destroy()}),this.#r=Dt.subscribeKey("answerDevice",A=>{1===A&&this.initWebRtc()})}async initWebRtc(){const A=yt.agentInfo.sip_account_list.find(A=>1===A.device_type);if(A){const e=A.sip_register_addr;this.#A=new us({server:e.split(":")[0],user:A.sip_account,password:A.sip_password,webSocket:`wss://${e}`,callTimeout:3e4},this.#n),await this.#A.start()}}async init({actionNodeParams:e,url:t,appKey:s,appSecret:i,agentNo:r,enableBrowserAlert:o}){try{const{viewHtmlElement:n,rttHTML:a}=e||{},V=t||"https://aicc.yescloudy.com";cs=V,hs=s,ls=i;const c=ns(s,i);await this.#e._agentLogin(r);const{agent_no:h,current_answer_device:l,init_state:g}=yt.agentInfo;g===A.AgentStatus.IDLE?(Dt.updateActionConfigs(Ct),yt.stateObject={state:g,state_name:"空闲"}):g===A.AgentStatus.BUSY&&(Dt.updateActionConfigs(Et),yt.stateObject={state:g,state_name:"忙碌"});let q="";const d=new URL(V).host;q=V.includes("https")?`wss://${d}`:`ws://${d}`,Ns.getInstance(`${q}/ccs-ws/api/v1/ws/call/${h}?Authorization=${c}`),Dt.updateAnswerDevice(l),1===l&&await this.initWebRtc(),this.#t=new bs(this.#A),e&&n&&(this.#i=!0,new vs({container:n,rttHTML:a,statusParams:{state:A.AgentStatus.IDLE,statusName:"空闲"}},this,this.#t)),yt.enableBrowserAlert=o,o&&Ks.requestPermission()}catch(A){this.#i&&Fs("初始化失败"),console.log(A)}}#n=A=>{const{type:e,data:t}=A;Ve.emit(e,t)};#a=A=>{};on(A,e){return Ve.on(A,e),this}off(A,e){Ve.off(A,e)}offAll(){Ve.clearAllListeners()}async destroyRtc(){await(this.#A?.destroy())}destroy(){this.destroyRtc(),clearInterval(this.#s),xs.off("ws-message",this.#a),Ns.__internalClose(),this.offAll(),this.#r?.(),this.#A=null,this.#e=null,this.#i=!1}async#o(){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(A=>this.#t.makeCall(A)),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))}}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))}}getAgentData(){const{agent_no:A,agent_name:e,answer_devices:t,init_state:s,default_device:i}=yt.agentInfo,{stateObject:r}=yt,o={1:"软电话",2:"手机模式",3:"SIP话机"};return{agent_no:A,agent_name:e,init_state:s,default_device:i,answer_devices:(t||"").split(",").map(A=>({value:Number(A),label:o[Number(A)]})),stateObject:r}}},Object.defineProperty(A,"__esModule",{value:!0})});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ysyt-agent-sdk",
3
- "version": "1.0.57",
3
+ "version": "1.0.58",
4
4
  "main": "dist/ysyt-agent-sdk.cjs.js",
5
5
  "module": "dist/ysyt-agent-sdk.esm.js",
6
6
  "types": "dist/src/index.d.ts",