@sanseng/livekit-ws-sdk 0.1.9 → 0.1.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs.js +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.esm.js +1 -1
- package/dist/index.full.umd.js +1 -1
- package/dist/index.umd.js +1 -1
- package/package.json +1 -1
package/dist/index.cjs.js
CHANGED
|
@@ -107,4 +107,4 @@ class AudioResamplerProcessor extends AudioWorkletProcessor {
|
|
|
107
107
|
}
|
|
108
108
|
|
|
109
109
|
registerProcessor('audio-resampler', AudioResamplerProcessor);
|
|
110
|
-
`;function Ie(){const o=new Blob([Ne],{type:"text/javascript"});return URL.createObjectURL(o)}class ke extends be{constructor(e){super(),this.options=e,this._stream=null,this._audioContext=null,this._sourceNode=null,this._workletNode=null,this._targetSampleRate=R,this._bitDepth=I,this._channels=1;const t=e?.input;this._targetSampleRate=t?.sampleRate||R,this._bitDepth=t?.bitDepth||I,this._channels=t?.channels||1}async start(){this.ensureNotDisposed();try{const t={audio:this.options?.input?.constraints||{channelCount:this._channels}};this._stream=await navigator.mediaDevices.getUserMedia(t);const r=this._stream.getAudioTracks()[0].getSettings().sampleRate||44100;this._audioContext=new AudioContext({sampleRate:r});const a=Ie();await this._audioContext.audioWorklet.addModule(a),this._sourceNode=this._audioContext.createMediaStreamSource(this._stream),this._workletNode=new AudioWorkletNode(this._audioContext,"audio-resampler",{processorOptions:{targetSampleRate:this._targetSampleRate,bitDepth:this._bitDepth,channels:this._channels},numberOfInputs:1,numberOfOutputs:0}),this._workletNode.port.onmessage=c=>{const{type:l,data:p,sampleRate:m}=c.data;l==="audioData"&&p&&this.emitAudioData(p,m)},this._sourceNode.connect(this._workletNode),i.info(`Audio capture started: input=${r}Hz -> output=${this._targetSampleRate}Hz, ${this._bitDepth}bit`)}catch(e){throw u.fromError(e,d.AUDIO_CAPTURE_FAILED)}}stop(){this._stream&&(this._stream.getTracks().forEach(e=>e.stop()),this._stream=null),this._workletNode&&(this._workletNode.port.onmessage=null,this._workletNode.disconnect(),this._workletNode=null),this._sourceNode&&(this._sourceNode.disconnect(),this._sourceNode=null),this._audioContext&&(this._audioContext.close(),this._audioContext=null),i.info("Audio capture stopped")}onDispose(){this.stop()}}class Oe{create(e){return new ke(e)}}class xe extends h{constructor(e,t,s){super(),this._source=null,this._isCapturing=!1,this._currentOutputTrackId=null,this._context=e,this._inputPipeline=new me(t),this._outputPipeline=new fe,this._frameSender=new ye(e,t),this._sourceFactory=s||new Oe,this._inputPipeline.onProcessedAudio=(n,r)=>{this._context.eventBus.emit("inner:audio:frame:received",{data:n,sampleRate:r})},i.info("Audio Controller created")}setWebSocketSender(e){this._frameSender.setWebSocketSender(e)}async startCapture(){if(this.ensureNotDisposed(),this._isCapturing){i.warn("Audio capture already started");return}try{this._source||(this._source=this._sourceFactory.create(this._context.options.audio),this._source.onAudioData=(e,t)=>{this._inputPipeline.process(e,t)}),await this._source.start(),this._isCapturing=!0,this._frameSender.start(),this._context.eventBus.emit("inner:audio:input:started",void 0),i.info("Audio capture started")}catch(e){throw this._context.emitError(e,d.AUDIO_CAPTURE_START_FAILED)}}stopCapture(){this.ensureNotDisposed(),this._isCapturing&&(this._source&&this._source.stop(),this._frameSender.stop(),this._isCapturing=!1,this._context.eventBus.emit("inner:audio:input:stopped",void 0),i.info("Audio capture stopped"))}get isCapturing(){return this._isCapturing}setOutputTrack(e,t,s){this.ensureNotDisposed(),this._outputPipeline.setTrack(e),this._currentOutputTrackId=t,this._context.eventBus.emit("inner:audio:track:added",{trackId:t,participantId:s}),i.info("Audio output track ready")}removeOutputTrack(e){this.ensureNotDisposed(),this._currentOutputTrackId===e&&(this._outputPipeline.removeTrack(),this._currentOutputTrackId=null,this._context.eventBus.emit("inner:audio:track:removed",{trackId:e}),i.info("Audio output track removed"))}setVolume(e){this.ensureNotDisposed(),this._outputPipeline.setVolume(e),this._context.eventBus.emit("inner:audio:volume:changed",{volume:e}),i.debug(`Audio volume set to: ${e}`)}getVolume(){return this._outputPipeline.getVolume()}mute(){this.ensureNotDisposed(),this._outputPipeline.mute(),this._context.eventBus.emit("inner:audio:muted",void 0),i.debug("Audio muted")}unmute(){this.ensureNotDisposed(),this._outputPipeline.unmute(),this._context.eventBus.emit("inner:audio:unmuted",void 0),i.debug("Audio unmuted")}getOutputElement(){return this._outputPipeline.getAudioElement()}isMuted(){return this._outputPipeline.isMuted()}onDispose(){this.stopCapture(),this._source&&(this._source.dispose(),this._source=null),this._inputPipeline.dispose(),this._outputPipeline.dispose(),this._frameSender.dispose(),i.info("Audio Controller disposed")}}exports.ConversationStateType=void 0;(function(o){o.IDLE="idle",o.WAITING="waiting",o.STREAMING="streaming",o.COMPLETED="completed",o.ERROR="error"})(exports.ConversationStateType||(exports.ConversationStateType={}));class Le extends W{constructor(){super(...arguments),this._currentState=exports.ConversationStateType.IDLE,this.transitionTable=[{from:exports.ConversationStateType.IDLE,to:[exports.ConversationStateType.WAITING,exports.ConversationStateType.ERROR]},{from:exports.ConversationStateType.WAITING,to:[exports.ConversationStateType.STREAMING,exports.ConversationStateType.ERROR]},{from:exports.ConversationStateType.STREAMING,to:[exports.ConversationStateType.COMPLETED,exports.ConversationStateType.ERROR]},{from:exports.ConversationStateType.COMPLETED,to:[exports.ConversationStateType.IDLE,exports.ConversationStateType.WAITING,exports.ConversationStateType.ERROR]},{from:exports.ConversationStateType.ERROR,to:[exports.ConversationStateType.IDLE]}]}reset(){this._currentState=exports.ConversationStateType.IDLE}}class O extends h{constructor(e,t){super(),this._currentQuestion=null,this._currentAnswer=null,this._sessionId=e,this._stateMachine=new Le,i.info(`Conversation session created: ${e}`)}get sessionId(){return this._sessionId}get state(){return this._stateMachine.getCurrentState()}_setQuestion(e){this._currentQuestion=e,this._stateMachine.transitionTo(exports.ConversationStateType.WAITING),this._syncContextState()}get currentQuestion(){return this._currentQuestion}_startAnswer(e){this._currentAnswer={questionId:e,text:"",isComplete:!1,timestamp:Date.now()},this._stateMachine.transitionTo(exports.ConversationStateType.STREAMING),this._syncContextState()}_appendAnswerChunk(e){this._currentAnswer&&(this._currentAnswer.text+=e)}_completeAnswer(){this._currentAnswer&&(this._currentAnswer.isComplete=!0,this._currentAnswer.completedAt=Date.now()),this._stateMachine.transitionTo(exports.ConversationStateType.COMPLETED),this._syncContextState()}get currentAnswer(){return this._currentAnswer}_setError(e){this._stateMachine.transitionTo(exports.ConversationStateType.ERROR),this._syncContextState()}_syncContextState(){}handleEvent(e){const t=this._stateMachine.getCurrentState();switch(e.type){case"QUESTION":if(t!==exports.ConversationStateType.IDLE&&t!==exports.ConversationStateType.COMPLETED){i.warn(`Ignoring QUESTION event in invalid state: ${t}`);return}this._setQuestion(e.payload);break;case"ANSWER_START":if(t!==exports.ConversationStateType.WAITING){i.warn(`Ignoring ANSWER_START event in invalid state: ${t}. Expected: WAITING`);return}this._startAnswer(e.payload);break;case"ANSWER_CHUNK":if(t!==exports.ConversationStateType.STREAMING){i.warn(`Ignoring ANSWER_CHUNK event in invalid state: ${t}. Expected: STREAMING`);return}this._appendAnswerChunk(e.payload&&e.payload.chunk||""),e.payload&&e.payload.isComplete&&this._completeAnswer();break;case"ANSWER_COMPLETE":if(t!==exports.ConversationStateType.STREAMING){i.warn(`Ignoring ANSWER_COMPLETE event in invalid state: ${t}. Expected: STREAMING`);return}this._completeAnswer();break;case"ERROR":this._setError(e.payload);break;default:i.warn(`Unknown event type: ${e.type}`);break}}reset(){this._stateMachine.reset(),this._currentQuestion=null,this._currentAnswer=null}onDispose(){this.reset(),i.info(`Conversation session disposed: ${this._sessionId}`)}}class Pe extends h{constructor(){super(...arguments),this._buffers=new Map}appendChunk(e,t){this.ensureNotDisposed();const n=(this._buffers.get(e)||"")+t;return this._buffers.set(e,n),i.debug(`Message chunk appended for question ${e}, length: ${n.length}`),n}getMessage(e){return this._buffers.get(e)||null}complete(e){const t=this.getMessage(e)||"";return this._buffers.delete(e),i.debug(`Message assembly completed for question ${e}, length: ${t.length}`),t}clear(e){this._buffers.delete(e)}onDispose(){this._buffers.clear(),i.info("Message Assembler disposed")}}class Fe{generateSessionId(){return`session_${Date.now()}_${Math.random().toString(36).substring(2,9)}`}generateQuestionId(){return Math.random().toString(36).substring(2,6)}}class Me extends h{constructor(e,t){super(),this._transport=null,this._sessions=new Map,this._context=e,this._assembler=new Pe,this._idService=new Fe,i.info("Conversation Manager created")}setTransport(e){this._transport=e}async sendQuestion(e,t={speed:100,mood:0,vol:50}){this.ensureNotDisposed();const s=this._idService.generateQuestionId(),n={id:s,text:e,timestamp:Date.now(),inputMode:"text"},r=this._idService.generateSessionId(),a=new O(r,this._context);if(a.handleEvent({type:"QUESTION",payload:n}),this._sessions.set(s,a),this._transport)try{this._transport.sendText(s,e,200,t)}catch(c){throw i.error("Error sending question via transport:",c),a.handleEvent({type:"ERROR",payload:"Failed to send question"}),this._context.eventBus.emit("inner:sdk:error",{error:c instanceof Error?c:new Error(String(c))}),c}else i.warn("Transport not set, question not sent");return this._context.eventBus.emit("inner:conversation:question:sent",{questionId:s,text:e}),this._context.eventBus.emit("inner:conversation:answer:waiting",{questionId:s}),i.info(`Question sent: ${s}`),s}handleServerPong(){this.ensureNotDisposed(),this._transport&&this._transport.handlePongReceived()}willDisconnect(){this.ensureNotDisposed(),this._transport?.willDisconnect()}handleTextInput(e){this.sendQuestion(e).catch(t=>{i.error("Error sending question:",t)})}handleAnswerChunk(e,t,s){this.ensureNotDisposed();const n=this._sessions.get(e);if(!n){i.warn(`No active session for answer chunk: ${e}`);return}if(n.state===exports.ConversationStateType.WAITING&&n.handleEvent({type:"ANSWER_START",payload:e}),s){n.handleEvent({type:"ANSWER_COMPLETE"});const a=this._assembler.complete(e);this._context.eventBus.emit("inner:conversation:answer:completed",{questionId:e,fullAnswer:a});try{n.dispose()}catch(c){i.error("Error disposing session:",c)}this._sessions.delete(e)}else n.handleEvent({type:"ANSWER_CHUNK",payload:{uid:e,chunk:t,isComplete:s}}),this._assembler.appendChunk(e,t),this._context.eventBus.emit("inner:conversation:answer:chunk",{questionId:e,chunk:t,isComplete:s})}handleServerInitiatedMessage(e,t){this.ensureNotDisposed(),this._context.eventBus.emit("inner:conversation:server:message",{questionId:e,message:t,type:"text"})}handleAsrTextReceived(e,t){this.ensureNotDisposed();const s=this._idService.generateSessionId(),n=new O(s,this._context);n.handleEvent({type:"QUESTION",payload:{id:e,text:t,timestamp:Date.now(),inputMode:"voice"}}),this._sessions.set(e,n),this._context.eventBus.emit("inner:conversation:asr:received",{questionId:e,text:t})}onDispose(){for(const e of this._sessions.values())try{e.dispose()}catch(t){i.error("Error disposing session:",t)}this._sessions.clear(),this._assembler.dispose(),i.info("Conversation Manager disposed")}}class Ve extends h{constructor(e,t){super(),this._manager=new Me(e,t),i.info("Conversation Controller created")}get manager(){return this._manager}setTransport(e){this._manager.setTransport(e)}async sendQuestion(e,t){return this.ensureNotDisposed(),this._manager.sendQuestion(e,t)}onDispose(){this._manager.dispose(),i.info("Conversation Controller disposed")}}class Ue extends h{constructor(){super(...arguments),this._tracks=new Map,this._participants=new Map}register(e,t,s){this.ensureNotDisposed(),this._tracks.set(e,t),this._participants.has(s)||this._participants.set(s,new Set),this._participants.get(s).add(e),t.on("ended",()=>{i.debug(`Track ended: ${e}`),this.unregister(e)}),i.debug(`Track registered: ${e} from participant ${s}`)}unregister(e){const t=this._tracks.get(e);if(t){t.stop(),this._tracks.delete(e);for(const[s,n]of this._participants.entries())if(n.has(e)){n.delete(e),n.size===0&&this._participants.delete(s);break}i.debug(`Track unregistered: ${e}`)}}getTrack(e){return this._tracks.get(e)||null}getParticipantTracks(e){const t=this._participants.get(e);return t?Array.from(t):[]}getAllTrackIds(){return Array.from(this._tracks.keys())}getVideoAvailability(){let e=!1,t=!1,s=!1;for(const n of this._tracks.values()){const r=n.mediaStreamTrack;if(r.kind==="video"&&(e=!0,r.readyState==="live"&&(t=!0),r.enabled&&(s=!0),r.readyState==="live"&&r.enabled))return{available:!0}}return e?t?s?{available:!1,reason:v.NO_VIDEO_TRACK}:{available:!1,reason:v.TRACK_MUTED}:{available:!1,reason:v.TRACK_ENDED}:{available:!1,reason:v.NO_VIDEO_TRACK}}clear(){for(const e of this._tracks.values())e.stop();this._tracks.clear(),this._participants.clear(),i.info("All tracks cleared")}onDispose(){this.clear()}}class x extends h{constructor(e,t){super(),this._videoController=null,this._room=null,this._context=e,this._trackRegistry=t,this._loosTrackTimer=null,i.info("Video Track Handler created")}setRoom(e){this._room=e}setVideoController(e){this._videoController=e}handleTrackSubscribed(e,t,s){this.ensureNotDisposed(),this._loosTrackTimer&&clearTimeout(this._loosTrackTimer),this._trackRegistry.register(t,e,s),this._videoController&&e.attach(this._videoController.getSource().getInternalElement()),e.on("videoPlaybackStarted",()=>{i.info("inner:rtc:video:available"),this._context.eventBus.emit("inner:rtc:video:available",void 0)}),this._context.eventBus.emit("inner:video:track:added",{trackId:t,participantId:s}),i.debug(`Video track subscribed: ${t}`)}handleTrackUnsubscribed(e,t,s){this.ensureNotDisposed(),this._videoController&&e.detach(this._videoController.getSource().getInternalElement()),e.removeAllListeners(),this._context.eventBus.emit("inner:video:track:removed",{trackId:t,participantId:s}),this._loosTrackTimer&&clearTimeout(this._loosTrackTimer),this._loosTrackTimer=setTimeout(()=>{this._context.eventBus.emit("inner:rtc:video:unavailable",void 0)},Y),this._trackRegistry.unregister(t),i.debug(`Video track unsubscribed: ${t}`)}supports(e){return e.mediaStreamTrack.kind==="video"}onDispose(){this._loosTrackTimer&&(clearTimeout(this._loosTrackTimer),this._loosTrackTimer=null),i.info("Video Track Handler disposed")}}class L extends h{constructor(e,t){super(),this._audioController=null,this._room=null,this._context=e,this._trackRegistry=t,i.info("Audio Track Handler created")}setRoom(e){this._room=e}setAudioController(e){this._audioController=e}handleTrackSubscribed(e,t,s){this.ensureNotDisposed(),this._trackRegistry.register(t,e,s),this._audioController?e.attach(this._audioController.getOutputElement()):this._context.eventBus.emit("inner:audio:track:added",{trackId:t,participantId:s}),i.debug(`Audio track subscribed: ${t}`)}handleTrackUnsubscribed(e,t,s){this.ensureNotDisposed(),this._audioController?(this._audioController.removeOutputTrack(t),e.detach(this._audioController.getOutputElement())):this._context.eventBus.emit("inner:audio:track:removed",{trackId:t}),this._trackRegistry.unregister(t),i.debug(`Audio track unsubscribed: ${t}`)}supports(e){return e.mediaStreamTrack.kind==="audio"}onDispose(){i.info("Audio Track Handler disposed")}}class Ke extends h{constructor(e,t){super(),this._room=null,this._context=e,this._trackRegistry=t,this._handlers=[new x(e,t),new L(e,t)],i.info("LiveKit Event Adapter created")}setRoom(e){this._room=e,this._handlers.forEach(t=>{t.setRoom(e)}),console.log("LiveKit room set:",this._room)}setVideoController(e){const t=this._handlers.find(s=>s instanceof x);t&&t.setVideoController(e)}setAudioController(e){const t=this._handlers.find(s=>s instanceof L);t&&t.setAudioController(e)}handleTrackSubscribed(e,t,s){this.ensureNotDisposed();const n=this._findHandler(e);n?n.handleTrackSubscribed(e,t,s):i.warn(`No handler found for track, trackId: ${t}`)}handleTrackUnsubscribed(e,t){this.ensureNotDisposed();const s=this._trackRegistry.getTrack(e);if(s){const n=this._findHandler(s);n?n.handleTrackUnsubscribed(s,e,t):(this._trackRegistry.unregister(e),i.warn(`No handler found for track, trackId: ${e}`))}}_findHandler(e){return this._handlers.find(t=>t.supports(e))||null}onDispose(){this._handlers.forEach(e=>{try{e.dispose()}catch(t){i.error("Error disposing track handler:",t)}}),this._handlers.length=0,i.info("LiveKit Event Adapter disposed")}}class We extends h{constructor(e){super(),this._room=null,this._context=e,this._trackRegistry=new Ue,this._eventAdapter=new Ke(e,this._trackRegistry),this._maxAttempts=e.options.reconnect?.maxAttempts||S,this._maxDelay=(e.options.reconnect?.delay??y)*T,i.info("LiveKit Service created")}async connect(e,t,s,n){if(this.ensureNotDisposed(),this._room&&this._room.isConnected()){i.warn("Already connected to LiveKit room");return}try{i.info(`Connecting to LiveKit room: ${s} at ${e}`);const r=new f.Room({reconnectPolicy:{nextRetryDelayInMs:a=>a.elapsedMs<this._maxDelay?Math.min(T*Math.pow(2,a.retryCount),this._maxDelay):null}});r.prepareConnection(e,t),r.on(f.RoomEvent.TrackSubscribed,(a,c,l)=>{this._handleTrackSubscribed(a,c,l)}),r.on(f.RoomEvent.TrackUnsubscribed,(a,c,l)=>{this._handleTrackUnsubscribed(a,c,l)}),r.on(f.RoomEvent.TrackMuted,(a,c)=>{this._handleTrackMuted(a,c)}),r.on(f.RoomEvent.TrackUnmuted,(a,c)=>{this._handleTrackUnmuted(a,c)}),r.on(f.RoomEvent.Connected,()=>{i.info("Connected to LiveKit room"),this._context.eventBus.emit("inner:rtc:connected",void 0)}),r.on(f.RoomEvent.Disconnected,a=>{a?i.info(`LiveKit room disconnected: ${a}`):i.info("LiveKit room disconnected"),a!==f.DisconnectReason.CLIENT_INITIATED&&this._context.eventBus.emit("inner:rtc:disconnected",{reason:a?String(a):void 0}),this._room=null}),i.info("Connecting to LiveKit room..."),await r.connect(e,t,{autoSubscribe:!0,maxRetries:this._context.options.reconnect?.maxAttempts||S}),this._eventAdapter.setRoom(r),this._room=r}catch(r){throw u.fromError(r,d.LIVEKIT_CONNECT_FAILED)}}_handleTrackSubscribed(e,t,s){try{if(!e.mediaStreamTrack){i.warn("Track does not have MediaStreamTrack");return}e.kind==="audio"&&s.on(f.ParticipantEvent.IsSpeakingChanged,c=>{console.log("\u8F68\u9053 \u8BF4\u8BDD\u72B6\u6001:",c)});const r=t.trackSid,a=s.identity;this._eventAdapter.handleTrackSubscribed(e,r,a)}catch(n){i.error("Error handling track subscribed:",n)}}_handleTrackUnsubscribed(e,t,s){try{const n=t,r=s,a=n.trackSid,c=r.identity;this._trackRegistry.getTrack(a)?.kind==="video"&&this._context.eventBus.emit("inner:rtc:video:unavailable",void 0),this._eventAdapter.handleTrackUnsubscribed(a,c)}catch(n){i.error("Error handling track unsubscribed:",n)}}_handleTrackMuted(e,t){try{e.kind==="video"&&this._context.eventBus.emit("inner:rtc:video:unavailable",void 0)}catch(s){i.error("Error handling track muted:",s)}}_handleTrackUnmuted(e,t){try{e.kind==="video"&&this._context.eventBus.emit("inner:rtc:video:available",void 0)}catch(s){i.error("Error handling track unmuted:",s)}}async disconnect(){if(this._room)try{await this._room.disconnect(),this._room=null,this._context.eventBus.emit("inner:rtc:disconnected",{reason:"Disconnected from LiveKit room"}),i.info("Disconnected from LiveKit room")}catch(e){i.error("Error disconnecting from LiveKit room:",e),this._room=null}}isConnected(){return this._room?this._room.state===f.ConnectionState.Connected:!1}getTrackRegistry(){return this._trackRegistry}get trackRegistry(){return this._trackRegistry}getEventAdapter(){return this._eventAdapter}onDispose(){this.disconnect(),this._trackRegistry.dispose(),this._eventAdapter.dispose(),i.info("LiveKit Service disposed")}}class Be extends h{constructor(e){super(),this._conversationManager=null,this._context=e,i.info("WebSocket Dispatcher created")}setConversationManager(e){this._conversationManager=e,i.debug("Conversation manager set in dispatcher")}dispatch(e){this.ensureNotDisposed();try{if(e.type==="ws"){this._handleWSServerMessage(e);return}if(e.type==="text"){this._handleServerTextMessage(e);return}}catch(t){i.error("Error dispatching message:",t)}}_handleWSServerMessage(e){e.code===101?(this._conversationManager?.handleServerPong(),i.debug("Received pingpong response")):e.code===102&&(i.warn("Server will disconnect"),this._conversationManager?.willDisconnect())}_handleServerTextMessage(e){if(!this._conversationManager){i.warn("Conversation manager not set, text message ignored");return}const{uid:t,payload:s,code:n}=e;n===200?(this._conversationManager.handleAnswerChunk(t,s,!1),i.debug(`Answer received (code 401) for uid: ${t}`)):n===201?(this._conversationManager.handleAnswerChunk(t,"",!0),i.debug(`Answer completed (code 411) for uid: ${t}`)):n===300?(this._conversationManager.handleAsrTextReceived(t,s),i.debug(`ASR text chunk received (code 410) for uid: ${t}`)):n===400&&(this._conversationManager.handleServerInitiatedMessage(t,s),i.debug(`Server initiated message received (code 412) for uid: ${t}`))}onDispose(){i.info("WebSocket Dispatcher disposed")}}class $e extends h{constructor(e){super(),this._ws=null,this._dispatcher=null,this._isConnecting=!1,this._activeTimers=new Set,this._retryCount=0,this._maxRetries=S,this._pingTimer=void 0,this._pongTimer=void 0,this._isExplicitDisconnect=!1,this._context=e,this._maxRetries=e.options.reconnect?.maxAttempts??S,this._maxDelay=(e.options.reconnect?.delay??y)*T,this._dispatcher=new Be(e)}setConversationManager(e){this._dispatcher&&this._dispatcher.setConversationManager(e)}async connect(){if(this.ensureNotDisposed(),this._stopHeartbeat(),!(this._isConnecting||this.isConnected||this._ws?.readyState===WebSocket.OPEN)){this._isConnecting=!0,this._isExplicitDisconnect=!1;try{return this._ws=new WebSocket(this._context.wsUrl),new Promise((e,t)=>{this._ws.onopen=()=>{this._isConnecting=!1,this._retryCount=0,this.sendPing(),this._context.eventBus.emit("inner:ws:connected",void 0),i.info("Conversation transport connected"),e()},this._ws.onerror=s=>{this._isConnecting=!1,i.error("Conversation transport error:",s),this._context.eventBus.emit("inner:sdk:error",{error:new u("WebSocket connection error",d.WS_CONNECT_ERROR,s)}),t(new u("WebSocket connection error",d.WS_CONNECT_ERROR,s))},this._ws.onclose=s=>{const n=s.code,r=s.reason?s.reason:s.wasClean?"Clean close":"Closed by remote";this._handleDisconnect(n,r)},this._ws.onmessage=s=>{this._handleMessage(s.data)}})}catch(e){throw this._isConnecting=!1,this._context.emitError(e,d.WS_CONNECT_FAILED)}}}disconnect(){this._isExplicitDisconnect=!0,this._stopHeartbeat(),this._ws&&(this._ws.close(),this._ws=null),this._context.eventBus.emit("inner:ws:disconnected",{reason:"Client disconnect"}),i.info("Conversation transport disconnected")}_handleDisconnect(e,t){if(this._stopHeartbeat(),this._isConnecting=!1,e!==void 0&&new Set([1e3,1001,1002,1003,1007,1008]).has(e)){this._isExplicitDisconnect=!0;const n=`WebSocket closed (code ${e}): ${t??""}`;this._context.eventBus.emit("inner:ws:disconnected",{reason:n}),i.info(`Conversation transport closed with non-retry code ${e}: ${t??""}`);return}if(!this._isExplicitDisconnect&&this._retryCount<this._maxRetries){this._retryCount++;const n=Math.min(T*Math.pow(2,this._retryCount-1),this._maxDelay);i.warn(`Connection lost. Retry ${this._retryCount} in ${n}ms`);const r=setTimeout(()=>{!this._isExplicitDisconnect&&!this.isDisposed&&this.connect().catch(()=>{})},n);this._registerTimer(r)}else this._context.eventBus.emit("inner:ws:disconnected",{reason:"Closed"})}_registerTimer(e){return this._activeTimers.add(e),e}_clearTimer(e){e&&(clearTimeout(e),this._activeTimers.delete(e))}sendPing(){if(!this.isConnected)return;const e={type:"ws",code:101,payload:"ping"};this._ws?.send(JSON.stringify(e)),clearTimeout(this._pongTimer),this._pongTimer=setTimeout(()=>{i.error(`Pong timeout (${N/T}s). Reconnecting...`),this._ws?.close()},N)}handlePongReceived(){clearTimeout(this._pongTimer),this._clearTimer(this._pingTimer),this._pingTimer=setTimeout(()=>{this.sendPing()},J)}willDisconnect(){if(this.ensureNotDisposed(),this._ws&&this._ws.readyState===WebSocket.OPEN){const t=JSON.stringify({type:"ws",code:102,payload:"will disconnect"});this._ws.send(t),this._isExplicitDisconnect=!0,this._ws.close(),this._ws=null,this._context.eventBus.emit("inner:ws:disconnected",{reason:"Client responsive to server's disconnect message."})}}sendText(e,t,s=200,n){if(this.ensureNotDisposed(),!this._ws||this._ws.readyState!==WebSocket.OPEN)throw new u("WebSocket is not connected",d.WS_NOT_CONNECTED);try{const a=JSON.stringify({type:"text",code:s,uid:e,metadata:n,payload:t});this._ws.send(a),i.debug("Text message sent via transport:",e)}catch(r){throw this._context.emitError(r,d.WS_TEXT_SEND_FAILED)}}_handleMessage(e){try{if(typeof e!="string")return;const t=JSON.parse(e);i.debug("Message received:",t.type),this._dispatcher?this._dispatcher.dispatch(t):i.warn("Dispatcher not set, message ignored")}catch(t){i.error("Error parsing message:",t),this._context.emitError(t,d.WS_PARSE_ERROR)}}sendBinary(e){if(this.ensureNotDisposed(),!this._ws||this._ws.readyState!==WebSocket.OPEN){i.debug("WebSocket not connected, binary data dropped");return}try{this._ws.send(e),i.debug("Binary data sent via transport")}catch(t){i.error("Error sending binary data:",t)}}get isConnected(){return this._ws?.readyState===WebSocket.OPEN}_stopHeartbeat(){this._clearTimer(this._pingTimer),this._clearTimer(this._pongTimer)}onDispose(){this._stopHeartbeat(),this.disconnect(),this._dispatcher&&(this._dispatcher.dispose(),this._dispatcher=null),i.info("Conversation transport disposed")}}const A=["sdk:connected","sdk:disconnected","sdk:error","media:video:available","media:video:unavailable","media:video:trackAdded","media:video:trackRemoved","media:audio:trackAdded","media:audio:trackRemoved","media:audio:captureStarted","media:audio:captureStopped","media:audio:volumeChanged","media:audio:muted","media:audio:unmuted","conversation:question:sent","conversation:answer:waiting","conversation:server:message","conversation:asr:received","conversation:answer:chunk","conversation:answer:completed"],P=/^[a-z]+:[a-zA-z]+(:[a-zA-Z]+)?$/;class He{constructor(e){this.inner=e,this.listeners=new Map,this.forwardedVideoTrackIds=new Set,this.wsConnected=!1,this.livekitConnected=!1,this.httpConnected=!1,this.internalMap={"inner:sdk:error":{public:"sdk:error",sanitizer:t=>({message:t?.error?.message,code:t?.error?.code})},"inner:rtc:video:available":{public:"media:video:available"},"inner:rtc:video:unavailable":{public:"media:video:unavailable"},"inner:video:track:added":{public:"media:video:trackAdded"},"inner:video:track:removed":{public:"media:video:trackRemoved"},"inner:audio:track:added":{public:"media:audio:trackAdded"},"inner:audio:track:removed":{public:"media:audio:trackRemoved"},"inner:audio:input:started":{public:"media:audio:captureStarted"},"inner:audio:input:stopped":{public:"media:audio:captureStopped"},"inner:audio:volume:changed":{public:"media:audio:volumeChanged",sanitizer:t=>({volume:t?.volume})},"inner:audio:muted":{public:"media:audio:muted"},"inner:audio:unmuted":{public:"media:audio:unmuted"},"inner:conversation:question:sent":{public:"conversation:question:sent",sanitizer:t=>({questionId:t?.questionId,text:String(t?.text||"")})},"inner:conversation:answer:waiting":{public:"conversation:answer:waiting",sanitizer:t=>({questionId:t?.questionId})},"inner:conversation:server:message":{public:"conversation:server:message",sanitizer:t=>({questionId:t?.questionId,message:String(t?.message||""),type:String(t?.type||"")})},"inner:conversation:asr:received":{public:"conversation:asr:received",sanitizer:t=>({questionId:t?.questionId,text:String(t?.text||"")})},"inner:conversation:answer:chunk":{public:"conversation:answer:chunk",sanitizer:t=>({questionId:t?.questionId,chunk:String(t?.chunk||"")})},"inner:conversation:answer:completed":{public:"conversation:answer:completed",sanitizer:t=>({questionId:t?.questionId,fullAnswer:String(t?.fullAnswer||"")})}},Object.keys(this.internalMap).forEach(t=>{this.inner.on(t,s=>this.handleInternalEvent(t,s))}),this.inner.on("inner:sdk:connected",t=>this.handleConnected(t,!0)),this.inner.on("inner:sdk:disconnected",t=>this.handleConnected(t,!1)),A.forEach(t=>{this.inner.on(t,s=>this.handlePublicEvent(t,s))})}publicAPI(){return{on:this.on.bind(this),off:this.off.bind(this),once:this.once.bind(this)}}on(e,t){const s=String(e);return this.assertAllowed(s),this.listeners.has(s)||this.listeners.set(s,new Set),this.listeners.get(s).add(t),()=>this.off(e,t)}once(e,t){const s=String(e);this.assertAllowed(s);const n=r=>{try{t(r)}finally{this.off(e,n)}};return this.on(e,n)}off(e,t){const s=String(e);this.assertAllowed(s);const n=this.listeners.get(s);n&&(n.delete(t),n.size===0&&this.listeners.delete(s))}handleInternalEvent(e,t){const s=this.internalMap[e];if(!s)return;const n=s.public;if(A.indexOf(n)===-1)return;if(n==="media:video:trackAdded"){const c=t?.trackId||t?.id;if(typeof c=="string"){if(this.forwardedVideoTrackIds.has(c))return;this.forwardedVideoTrackIds.add(c)}}const r=s.sanitizer?s.sanitizer(t):t;if(!P.test(n))return;const a=this.listeners.get(n);!a||a.size===0||a.forEach(c=>{try{c(r)}catch(l){console.error("PublicEventEmitter listener error",l)}})}handlePublicEvent(e,t){if(A.indexOf(e)===-1)return;const s=Object.values(this.internalMap).find(a=>a.public===e),n=s&&s.sanitizer?s.sanitizer(t):t;if(e==="media:video:trackAdded"){const a=t?.trackId||t?.id;if(typeof a=="string"){if(this.forwardedVideoTrackIds.has(a))return;this.forwardedVideoTrackIds.add(a)}}const r=this.listeners.get(e);!r||r.size===0||r.forEach(a=>{try{a(n)}catch(c){console.error("PublicEventEmitter listener error",c)}})}handleConnected(e,t){e.source==="livekit"?this.livekitConnected=t:e.source==="ws"?this.wsConnected=t:e.source==="http"&&(this.httpConnected=t),this.maybeEmitSdkConnected(t?"connected":"disconnected")}maybeEmitSdkConnected(e){const t=e==="connected",s=t?!!this.livekitConnected:!this.livekitConnected,n=t?!!this.wsConnected:!this.wsConnected,r=t?!!this.httpConnected:!this.httpConnected,a={livekit:s,ws:n,http:r,all:s&&n&&r},c=JSON.stringify(a);this.lastConnectedPayloadJson!==c&&(this.lastConnectedPayloadJson=c,this.inner.emit(`sdk:${e}`,a))}assertAllowed(e){if(A.indexOf(e)===-1)throw new Error(`Event "${e}" is not allowed for public consumers`);if(!P.test(e))throw new Error(`Event name "${e}" does not follow namespace:domain[:action] naming`)}}class ze extends h{constructor(e,t,s){super(),this._requestCounter=0,this._context=e,this._baseURL=t||"",this._defaultHeaders=s||{},i.info(`HTTP Service created with baseURL: ${this._baseURL||"none"}`)}setAuthToken(e){this._defaultHeaders.Authorization=`Bearer ${e}`}request(e){this.ensureNotDisposed();const t=e.requestId||`req_${Date.now()}_${++this._requestCounter}`,s=this._buildURL(e.url,e.params),n=this._mergeHeaders(e.headers),r={method:e.method||"GET",headers:n};e.data!==void 0&&(e.method||"GET")!=="GET"&&(typeof e.data=="string"?r.body=e.data:(r.body=JSON.stringify(e.data),!n["Content-Type"]&&!n["content-type"]&&(n["Content-Type"]="application/json")));const a=e.timeout||3e4,c=new AbortController;return r.signal=c.signal,new Promise((l,p)=>{const m=setTimeout(()=>c.abort(),a);fetch(s,r).then(async _=>{clearTimeout(m);const g={};_.headers.forEach((G,j)=>{g[j]=G});let E;const b=_.headers.get("content-type");b&&b.includes("application/json")?E=await _.json():E=await _.text();const q={requestId:t,status:_.status,headers:g,data:E,config:e};i.debug(`HTTP request succeeded: ${e.method} ${s} [${t}]`),l(q)}).catch(_=>{clearTimeout(m);const g=_&&_.name==="AbortError",E={requestId:t,status:_&&_.status||void 0,message:g?"Request aborted (timeout)":_&&_.message||"HTTP request failed",error:_,config:e};i.error(`HTTP request failed: ${e.method} ${s} [${t}]`,_),p(E)})})}get(e,t,s){return this.request({...s,url:e,method:"GET",params:t})}post(e,t,s){return this.request({...s,url:e,method:"POST",data:t})}put(e,t,s){return this.request({...s,url:e,method:"PUT",data:t})}delete(e,t){return this.request({...t,url:e,method:"DELETE"})}patch(e,t,s){return this.request({...s,url:e,method:"PATCH",data:t})}_buildURL(e,t){if(e.startsWith("http://")||e.startsWith("https://"))return this._appendParams(e,t);let s=this._baseURL;return s&&!s.endsWith("/")&&!e.startsWith("/")?s+="/":s&&s.endsWith("/")&&e.startsWith("/")&&(s=s.slice(0,-1)),s+=e,this._appendParams(s,t)}_appendParams(e,t){if(!t||Object.keys(t).length===0)return e;const s=new URLSearchParams;for(const[r,a]of Object.entries(t))s.append(r,String(a));const n=e.includes("?")?"&":"?";return`${e}${n}${s.toString()}`}_mergeHeaders(e){const t={...this._defaultHeaders};return e&&Object.assign(t,e),t}onDispose(){i.info("HTTP Service disposed")}}function qe(o,e="https"){return o.startsWith(e)?o:`${e}://${o}`}class Ge extends h{constructor(e,t){super(),this._context=e,this._httpService=new ze(e,t||te)}getAuthToken(){return this.ensureNotDisposed(),this._httpService?.request({url:"/auth/login",method:"GET",data:{}}).then(e=>{e.data.code===C&&this._httpService?.setAuthToken(e.data.data.token)}).catch(e=>{throw this._context.emitError(e,d.SDK_AUTH_TOKEN_FAILED)})}getLivekitConfig(){return this.ensureNotDisposed(),this._httpService?.request({url:"/stream/v1/start",method:"POST",data:{avatarName:this._context.options.avatarName,voice:this._context.options.avatarVoice}}).then(e=>{if(e.data.code===C){const t=e.data.data;return this._context.setLivekitConfig(qe(t.livekitUrl,"wss"),t.roomToken,t.roomId),this._context.setWsUrl(`${this._context.options.wsUrlPrefix||ie}/${t.roomId}/${this._context.options.avatarName}`),this._context.setVideoOptions({renderMode:t.greenScreen.enabled?"processed":"raw",greenScreen:t.greenScreen||{}}),this._context.eventBus.emit("inner:sdk:connected",{source:"http"}),!0}else throw new u(e.data.msg,d.SDK_GET_LIVEKIT_CONFIG_FAILED)}).catch(e=>{throw this._context.eventBus.emit("inner:sdk:error",{error:new u(e.message,d.SDK_GET_LIVEKIT_CONFIG_FAILED)}),u.fromError(e,d.SDK_GET_LIVEKIT_CONFIG_FAILED)})}switchVideo({onceActions:e,loopActions:t}){return this.ensureNotDisposed(),this._httpService?.request({url:"/avatar/v1/switch-video",method:"POST",data:{roomId:this._context.roomId,onceActions:e,loopActions:t}}).then(s=>{if(s.data.code===C)return!0;throw this._context.emitError(s.data.message,d.SDK_SWITCH_VIDEO_FAILED)})}interruptConversation(){return this.ensureNotDisposed(),this._httpService?.request({url:"/stream/v1/interrupt",method:"POST",data:{roomId:this._context.roomId}}).then(e=>{if(e.data.code===C)return!0;throw this._context.emitError(e.data.message,d.SDK_INTERRUPT_CONVERSATION_FAILED)}).finally(()=>{this._context.eventBus.emit("inner:sdk:disconnected",{source:"http"})})}disconnectConversation(){return this.ensureNotDisposed(),this._httpService?.request({url:"/stream/v1/stop",method:"POST",data:{roomId:this._context.roomId}}).then(e=>e.data.code===C).finally(()=>{this._context.eventBus.emit("inner:sdk:disconnected",{source:"http"})})}onDispose(){this._httpService.dispose()}}class z extends h{constructor(e){super(),this._videoController=null,this._audioController=null,this._conversationController=null,this._liveKitService=null,this._conversationTransport=null,this._connectionCoordinator=null,this._httpController=null,this._preConnectPromise=null,this._lastPreConnectSuccessAt=null,this._context=new re(e),i.info("SDK Client created")}get events(){return this.ensureNotDisposed(),this._publicEventAPI||(this._publicEmitter=new He(this._context.eventBus),this._publicEventAPI=this._publicEmitter.publicAPI()),this._publicEventAPI}pushActions({onceActions:e,loopActions:t}){return this.ensureNotDisposed(),this._ensureConnected(),this._httpController?.switchVideo({onceActions:e,loopActions:t})}interruptConnection(){return this.ensureNotDisposed(),this._ensureConnected(),this._httpController?.interruptConversation()}sendTextQuestion(e){if(this.ensureNotDisposed(),this._ensureConnected(),this._conversationController)return this._conversationController.sendQuestion(e);throw new u("Conversation controller is not available",d.CONVERSATION_CONTROLLER_NOT_AVAILABLE)}setRenderFitMode(e){this.ensureNotDisposed(),this._ensureConnected(),this._videoController&&this._videoController.setRenderFitMode(e)}async startAudioCapture(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",d.AUDIO_CONTROLLER_NOT_AVAILABLE);await this._audioController.startCapture()}async stopAudioCapture(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",d.AUDIO_CONTROLLER_NOT_AVAILABLE);await this._audioController.stopCapture()}setVolume(e){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",d.AUDIO_CONTROLLER_NOT_AVAILABLE);this._audioController.setVolume(e)}getVolume(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",d.AUDIO_CONTROLLER_NOT_AVAILABLE);return this._audioController?.getVolume()||1}mute(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",d.AUDIO_CONTROLLER_NOT_AVAILABLE);this._audioController.mute()}unmute(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",d.AUDIO_CONTROLLER_NOT_AVAILABLE);this._audioController.unmute()}get isMuted(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",d.AUDIO_CONTROLLER_NOT_AVAILABLE);return this._audioController.isMuted()}get isAudioCapturing(){return this.ensureNotDisposed(),this._ensureConnected(),this._audioController?this._audioController.isCapturing:!1}async preConnect(){if(this.ensureNotDisposed(),this._preConnectPromise)return this._preConnectPromise;const e=Date.now();return this._lastPreConnectSuccessAt!==null&&e-this._lastPreConnectSuccessAt<ne?Promise.resolve(!0):(this._preConnectPromise=(async()=>{try{if(i.info("Creating HTTP controller..."),this._httpController=this._httpController??new Ge(this._context,this._context.options.http?.baseURL),i.info("Getting auth token..."),await this._httpController.getAuthToken(),i.info("Getting livekit config..."),!!!await this._httpController?.getLivekitConfig())throw new Error("Config missing");return this._lastPreConnectSuccessAt=Date.now(),!0}catch(t){throw this._context.emitError(t,d.SDK_PRECONNECT_FAILED)}finally{this._preConnectPromise=null}})(),this._preConnectPromise)}async connect(){if(this.ensureNotDisposed(),this._connectionCoordinator&&this._context.sessionState.isConnected){i.warn("SDK already initialized");return}try{if(!await this.preConnect())throw new u("PreConnect failed without explicit error",d.SDK_CONNECT_FAILED)}catch(e){throw this._context.emitError(e,d.SDK_CONNECT_FAILED)}try{if(i.info("Initializing SDK..."),this._connectionCoordinator=new ae(this._context),this._connectionCoordinator.start(),this._connectionCoordinator.startConnecting(),this._videoController=new _e(this._context,this._context.options.video),this._audioController=new xe(this._context,this._context.options.audio),this._conversationController=new Ve(this._context),this._liveKitService=new We(this._context),this._conversationTransport=new $e(this._context),this._liveKitService&&this._videoController&&this._audioController){const e=this._liveKitService.getEventAdapter();e&&(typeof e.setVideoController=="function"&&e.setVideoController(this._videoController),typeof e.setAudioController=="function"&&e.setAudioController(this._audioController))}this._audioController&&this._conversationTransport&&this._audioController.setWebSocketSender({sendBinary:e=>{this._conversationTransport.sendBinary(e)},isConnected:()=>this._conversationTransport.isConnected}),this._conversationTransport&&this._conversationController&&(this._conversationTransport.setConversationManager(this._conversationController.manager),this._conversationController.setTransport(this._conversationTransport)),await this._liveKitService.connect(this._context.livekitUrl,this._context.token,this._context.options.avatarName),this._conversationTransport&&await this._conversationTransport.connect(),i.info("SDK initialized and connected")}catch(e){throw this._connectionCoordinator&&this._connectionCoordinator.transitionToIdle(),await this._cleanup(),this._context.emitError(e,d.SDK_INITIALIZATION_FAILED)}}async disconnect(){if(this.ensureNotDisposed(),!(!this._connectionCoordinator||this._connectionCoordinator.getState()==="idle"||this._connectionCoordinator.getState()==="disposed")){i.info("Disconnecting SDK..."),this._connectionCoordinator.startDisconnecting();try{this._conversationTransport&&this._conversationTransport.disconnect(),this._liveKitService&&await this._liveKitService.disconnect(),this._audioController&&this._audioController.stopCapture(),this._httpController&&await this._httpController.disconnectConversation(),this._preConnectPromise=null,this._connectionCoordinator.completeDisconnecting(),i.info("SDK disconnected")}catch(e){throw i.error("Error during disconnect:",e),this._context.emitError(e,d.SDK_DISCONNECT_FAILED)}}}async reconnect(){this.ensureNotDisposed();const e=this._connectionCoordinator;if(!e?.canStartReconnectingManual()){const s=e?e.getSnapshot():this.connectionSnapshot;return i.warn("SDK reconnect not allowed in current state, returning current snapshot"),s}const t=e.captureSnapshotAndStartReconnectingManual();try{if(this._audioController&&this._audioController.stopCapture(),t.rtc.connected&&!t.rtc.hasVideoTrack&&(await this._liveKitService?.disconnect(),await this._conversationTransport?.disconnect()),this._conversationTransport&&!this._conversationTransport.isConnected){if(!await this.preConnect())throw new u("PreConnect failed without explicit error",d.SDK_CONNECT_FAILED);await this._conversationTransport.connect()}return this._liveKitService&&!this._liveKitService.isConnected()&&await this._liveKitService.connect(this._context.livekitUrl,this._context.token,this._context.options.avatarName),t}catch(s){throw i.error("Error during reconnect:",s),this._context.emitError(s,d.SDK_RECONNECT_FAILED)}}get connectionSnapshot(){return this.ensureNotDisposed(),this._connectionCoordinator?this._connectionCoordinator.getSnapshot():K(U("idle"))}async _cleanup(){if(this._conversationTransport&&this._conversationTransport.disconnect(),this._liveKitService)try{await this._liveKitService.disconnect()}catch(e){i.error("Error disconnecting LiveKit:",e)}this._audioController&&this._audioController.stopCapture()}get isConnected(){return this.ensureNotDisposed(),this._context.sessionState.isConnected}_ensureConnected(){if(!this._context.sessionState.isConnected)throw new u("SDK is not connected. Call connect() first.",d.SDK_NOT_CONNECTED)}onDispose(){this.disconnect().catch(e=>{i.error("Error during disconnect in dispose:",e)}),this._videoController&&(this._videoController.dispose(),this._videoController=null),this._audioController&&(this._audioController.dispose(),this._audioController=null),this._conversationController&&(this._conversationController.dispose(),this._conversationController=null),this._liveKitService&&(this._liveKitService.dispose(),this._liveKitService=null),this._conversationTransport&&(this._conversationTransport.dispose(),this._conversationTransport=null),this._httpController&&(this._httpController.dispose(),this._httpController=null),this._connectionCoordinator&&(this._connectionCoordinator.transitionToDisposed(),this._connectionCoordinator.dispose(),this._connectionCoordinator=null),this._context.dispose(),i.info("SDK Client disposed")}}function je(o){if(!o.avatarName)throw new u("avatarName is required",d.NO_AVATARNAME);return i.info("Creating SDK client with options:",{avatarName:o.avatarName}),new z(o)}class F extends h{constructor(e,t){super(),this._videoElement=null,this._videoUrl=null,this._isPlaying=!1,this._frameCallbackId=null,this._videoUrl=e,t?this._videoElement=t:(this._videoElement=document.createElement("video"),this._videoElement.style.opacity="0",this._videoElement.muted=!0,this._videoElement.playsInline=!0,this._videoElement.loop=!0,document.querySelector("#video-container")?.appendChild(this._videoElement)),i.info("Local Video Debug Source created")}get videoElement(){return this._videoElement}async load(){if(!this._videoElement)throw new Error("Video element or URL not set");return new Promise((e,t)=>{const s=this._videoElement,n=()=>{s.removeEventListener("loadedmetadata",n),s.removeEventListener("error",r),i.info(`Video loaded: ${s.videoWidth}x${s.videoHeight}`),e()},r=a=>{s.removeEventListener("loadedmetadata",n),s.removeEventListener("error",r),t(new Error(`Failed to load video: ${a}`))};s.addEventListener("loadedmetadata",n),s.addEventListener("error",r),s.src=this._videoUrl||"",s.load()})}async play(){if(!this._videoElement)throw new Error("Video element not set");if(!this._isPlaying)try{await this._videoElement.play(),this._videoElement.style.opacity="0",this._videoElement.setAttribute("loop","true"),this._isPlaying=!0,i.info("Video playback started")}catch(e){throw i.error("Failed to play video:",e),e}}pause(){this._videoElement&&this._isPlaying&&(this._videoElement.pause(),this._isPlaying=!1,i.info("Video playback paused"))}stop(){this._videoElement&&(this._videoElement.pause(),this._videoElement.currentTime=0,this._isPlaying=!1,i.info("Video playback stopped"))}get isPlaying(){return this._isPlaying&&this._videoElement!==null&&!this._videoElement.paused}get videoWidth(){return this._videoElement?.videoWidth||0}get videoHeight(){return this._videoElement?.videoHeight||0}async captureFrame(){if(!this._videoElement||this._videoElement.readyState<2)return null;try{if("VideoFrame"in window&&typeof window.VideoFrame=="function")try{return new VideoFrame(this._videoElement,{timestamp:this._videoElement.currentTime*1e6})}catch(s){i.error("",s)}const e=await createImageBitmap(this._videoElement),t=new VideoFrame(e,{timestamp:this._videoElement.currentTime*1e6});return e.close(),t}catch(e){return i.error("Failed to capture video frame:",e),null}}onFrame(e){if(!this._videoElement)return;this.offFrame();const t=this._videoElement;if("requestVideoFrameCallback"in t){const s=(n,r)=>{!this._isPlaying||!this._videoElement||this.captureFrame().then(a=>{a&&(e(a),a.close()),this._isPlaying&&this._videoElement&&(this._frameCallbackId=t.requestVideoFrameCallback(s))}).catch(a=>{i.error("Error in frame callback:",a)})};this._frameCallbackId=t.requestVideoFrameCallback(s)}else{const s=()=>{!this._isPlaying||!this._videoElement||this.captureFrame().then(n=>{n&&(e(n),n.close()),this._isPlaying&&this._videoElement&&(this._frameCallbackId=requestAnimationFrame(s))}).catch(n=>{i.error("Error in frame callback:",n)})};this._frameCallbackId=requestAnimationFrame(s)}}offFrame(){this._frameCallbackId!==null&&(this._videoElement&&"cancelVideoFrameCallback"in this._videoElement?this._videoElement.cancelVideoFrameCallback(this._frameCallbackId):cancelAnimationFrame(this._frameCallbackId),this._frameCallbackId=null)}onDispose(){this.stop(),this.offFrame(),this._videoElement&&this._videoElement.parentElement===document.body&&document.body.removeChild(this._videoElement),this._videoElement=null,this._videoUrl=null,i.info("Local Video Debug Source disposed")}}class Qe extends h{constructor(e,t,s){super(),this._outputCanvas=null,this._outputRenderer=null,this._source=new B;const n={...e,renderMode:"processed"};this._pipeline=new H(this._source,n),this._outputRenderer=new D(s,t),this._pipeline.setRenderer(this._outputRenderer),this._outputCanvas=this._outputRenderer.getCanvas(),i.info("Video Pipeline Runner created")}get outputCanvas(){return this._outputCanvas}get processor(){return this._pipeline.strategy.getProcessors()[0]||null}setProcessor(e){this._pipeline.setProcessor(e),i.info("Processor set on pipeline")}async processFrame(e){if(this.ensureNotDisposed(),!this._outputRenderer){i.error("Output renderer not available"),e.close();return}try{const t=this._pipeline.strategy.getProcessors()[0]||null;let s=null;if(t){try{s=t.process(e)}catch(n){i.error("Error processing frame:",n),s=e}if(s===null&&"getImageData"in t){const n=t.getImageData();if(n&&this._outputCanvas&&this._outputRenderer.getContext()){const r=this._outputRenderer.getContext();r&&((this._outputCanvas.width!==n.width||this._outputCanvas.height!==n.height)&&(this._outputCanvas.width=n.width,this._outputCanvas.height=n.height),r.putImageData(n,0,0))}e.close();return}s&&s!==e?(this._outputRenderer.render(s),e.close()):s===e?this._outputRenderer.render(e):e.close()}else this._outputRenderer.render(e)}catch(t){i.error("Error in processFrame:",t),e.close()}}setOutputSize(e,t){this._outputCanvas&&(this._outputCanvas.width=e,this._outputCanvas.height=t,i.info(`Output canvas size set to ${e}x${t}`))}onDispose(){this._pipeline.dispose(),this._outputRenderer&&(this._outputRenderer.dispose(),this._outputRenderer=null),this._outputCanvas&&this._outputCanvas.parentElement===document.body&&document.body.removeChild(this._outputCanvas),this._outputCanvas=null,i.info("Video Pipeline Runner disposed")}}class Ye extends h{constructor(e,t,s,n){if(super(),this._isRunning=!1,this._videoSource=new F(e,t),this._options={renderMode:"processed",containerElement:t,...n},this._pipelineRunner=new Qe(this._options,t,s),this._options.greenScreen?.enabled){const r=new w(this._options.greenScreen);this._pipelineRunner.setProcessor(r),i.info("Green screen processor enabled in debugger")}i.info("Standalone Video Debugger created")}async switchSource(e,t){this.ensureNotDisposed();const s=this._videoSource,n=this._isRunning,r=new F(e,t);try{await r.load(),n&&await r.play(),r.onFrame(l=>{this._pipelineRunner.processFrame(l).catch(p=>{i.error("Error processing frame:",p)})}),this._videoSource=r;try{s.offFrame(),s.stop(),s.dispose()}catch(l){console.warn("Error disposing old video source during switch:",l)}const a=this._videoSource.videoWidth,c=this._videoSource.videoHeight;a>0&&c>0&&this._pipelineRunner.setOutputSize(a,c)}catch(a){i.error("Failed to switch video source:",a);try{r.dispose()}catch(c){console.warn("Error disposing new video source after failed switch:",c)}throw a}}get outputCanvas(){return this._pipelineRunner.outputCanvas}get videoSource(){return this._videoSource}get processor(){return this._pipelineRunner.processor}setProcessor(e){this._pipelineRunner.setProcessor(e)}updateGreenScreenOptions(e){const t=this._pipelineRunner.processor;if(t&&"updateOptions"in t&&typeof t.updateOptions=="function")t.updateOptions(e),i.info("Green screen options updated");else{const s=this._options.greenScreen||{enabled:!1},n={...s,...e,enabled:e.enabled!==void 0?e.enabled:s.enabled},r=new w(n);this._pipelineRunner.setProcessor(r),this._options.greenScreen=n,i.info("Green screen processor recreated with new options")}}async initialize(){this.ensureNotDisposed();try{await this._videoSource.load();const e=this._videoSource.videoWidth,t=this._videoSource.videoHeight;e>0&&t>0&&this._pipelineRunner.setOutputSize(e,t),i.info("Video debugger initialized")}catch(e){throw i.error("Failed to initialize video debugger:",e),e}}async start(){if(this.ensureNotDisposed(),this._isRunning){i.warn("Debugger is already running");return}try{this._videoSource.videoWidth===0&&await this.initialize(),await this._videoSource.play(),this._videoSource.onFrame(e=>{this._pipelineRunner.processFrame(e).catch(t=>{i.error("Error processing frame:",t)})}),this._isRunning=!0,i.info("Video debugger started")}catch(e){throw i.error("Failed to start video debugger:",e),e}}stop(){this._isRunning&&(this._videoSource.stop(),this._videoSource.offFrame(),this._isRunning=!1,i.info("Video debugger stopped"))}pause(){this._isRunning&&(this._videoSource.pause(),i.info("Video debugger paused"))}async resume(){this._isRunning&&(await this._videoSource.play(),i.info("Video debugger resumed"))}get isRunning(){return this._isRunning}onDispose(){this.stop(),this._videoSource.dispose(),this._pipelineRunner.dispose(),i.info("Standalone Video Debugger disposed")}}exports.DEFAULT_AUDIO_BUFFER_SIZE=ee;exports.DEFAULT_AUDIO_CHANNELS=Z;exports.DEFAULT_AUDIO_SAMPLE_RATE=R;exports.DEFAULT_RECONNECT_ATTEMPTS=S;exports.DEFAULT_RECONNECT_DELAY=y;exports.DEFAULT_WS_TIMEOUT=X;exports.Disposable=h;exports.EventBus=M;exports.Logger=i;exports.SDKClient=z;exports.SDKError=u;exports.SessionState=V;exports.StandaloneVideoDebugger=Ye;exports.createClient=je;
|
|
110
|
+
`;function Ie(){const o=new Blob([Ne],{type:"text/javascript"});return URL.createObjectURL(o)}class ke extends be{constructor(e){super(),this.options=e,this._stream=null,this._audioContext=null,this._sourceNode=null,this._workletNode=null,this._targetSampleRate=R,this._bitDepth=I,this._channels=1;const t=e?.input;this._targetSampleRate=t?.sampleRate||R,this._bitDepth=t?.bitDepth||I,this._channels=t?.channels||1}async start(){this.ensureNotDisposed();try{const t={audio:this.options?.input?.constraints||{channelCount:this._channels}};this._stream=await navigator.mediaDevices.getUserMedia(t);const r=this._stream.getAudioTracks()[0].getSettings().sampleRate||44100;this._audioContext=new AudioContext({sampleRate:r});const a=Ie();await this._audioContext.audioWorklet.addModule(a),this._sourceNode=this._audioContext.createMediaStreamSource(this._stream),this._workletNode=new AudioWorkletNode(this._audioContext,"audio-resampler",{processorOptions:{targetSampleRate:this._targetSampleRate,bitDepth:this._bitDepth,channels:this._channels},numberOfInputs:1,numberOfOutputs:0}),this._workletNode.port.onmessage=c=>{const{type:l,data:p,sampleRate:m}=c.data;l==="audioData"&&p&&this.emitAudioData(p,m)},this._sourceNode.connect(this._workletNode),i.info(`Audio capture started: input=${r}Hz -> output=${this._targetSampleRate}Hz, ${this._bitDepth}bit`)}catch(e){throw u.fromError(e,d.AUDIO_CAPTURE_FAILED)}}stop(){this._stream&&(this._stream.getTracks().forEach(e=>e.stop()),this._stream=null),this._workletNode&&(this._workletNode.port.onmessage=null,this._workletNode.disconnect(),this._workletNode=null),this._sourceNode&&(this._sourceNode.disconnect(),this._sourceNode=null),this._audioContext&&(this._audioContext.close(),this._audioContext=null),i.info("Audio capture stopped")}onDispose(){this.stop()}}class Oe{create(e){return new ke(e)}}class xe extends h{constructor(e,t,s){super(),this._source=null,this._isCapturing=!1,this._currentOutputTrackId=null,this._context=e,this._inputPipeline=new me(t),this._outputPipeline=new fe,this._frameSender=new ye(e,t),this._sourceFactory=s||new Oe,this._inputPipeline.onProcessedAudio=(n,r)=>{this._context.eventBus.emit("inner:audio:frame:received",{data:n,sampleRate:r})},i.info("Audio Controller created")}setWebSocketSender(e){this._frameSender.setWebSocketSender(e)}async startCapture(){if(this.ensureNotDisposed(),this._isCapturing){i.warn("Audio capture already started");return}try{this._source||(this._source=this._sourceFactory.create(this._context.options.audio),this._source.onAudioData=(e,t)=>{this._inputPipeline.process(e,t)}),await this._source.start(),this._isCapturing=!0,this._frameSender.start(),this._context.eventBus.emit("inner:audio:input:started",void 0),i.info("Audio capture started")}catch(e){throw this._context.emitError(e,d.AUDIO_CAPTURE_START_FAILED)}}stopCapture(){this.ensureNotDisposed(),this._isCapturing&&(this._source&&this._source.stop(),this._frameSender.stop(),this._isCapturing=!1,this._context.eventBus.emit("inner:audio:input:stopped",void 0),i.info("Audio capture stopped"))}get isCapturing(){return this._isCapturing}setOutputTrack(e,t,s){this.ensureNotDisposed(),this._outputPipeline.setTrack(e),this._currentOutputTrackId=t,this._context.eventBus.emit("inner:audio:track:added",{trackId:t,participantId:s}),i.info("Audio output track ready")}removeOutputTrack(e){this.ensureNotDisposed(),this._currentOutputTrackId===e&&(this._outputPipeline.removeTrack(),this._currentOutputTrackId=null,this._context.eventBus.emit("inner:audio:track:removed",{trackId:e}),i.info("Audio output track removed"))}setVolume(e){this.ensureNotDisposed(),this._outputPipeline.setVolume(e),this._context.eventBus.emit("inner:audio:volume:changed",{volume:e}),i.debug(`Audio volume set to: ${e}`)}getVolume(){return this._outputPipeline.getVolume()}mute(){this.ensureNotDisposed(),this._outputPipeline.mute(),this._context.eventBus.emit("inner:audio:muted",void 0),i.debug("Audio muted")}unmute(){this.ensureNotDisposed(),this._outputPipeline.unmute(),this._context.eventBus.emit("inner:audio:unmuted",void 0),i.debug("Audio unmuted")}getOutputElement(){return this._outputPipeline.getAudioElement()}isMuted(){return this._outputPipeline.isMuted()}onDispose(){this.stopCapture(),this._source&&(this._source.dispose(),this._source=null),this._inputPipeline.dispose(),this._outputPipeline.dispose(),this._frameSender.dispose(),i.info("Audio Controller disposed")}}exports.ConversationStateType=void 0;(function(o){o.IDLE="idle",o.WAITING="waiting",o.STREAMING="streaming",o.COMPLETED="completed",o.ERROR="error"})(exports.ConversationStateType||(exports.ConversationStateType={}));class Le extends W{constructor(){super(...arguments),this._currentState=exports.ConversationStateType.IDLE,this.transitionTable=[{from:exports.ConversationStateType.IDLE,to:[exports.ConversationStateType.WAITING,exports.ConversationStateType.ERROR]},{from:exports.ConversationStateType.WAITING,to:[exports.ConversationStateType.STREAMING,exports.ConversationStateType.ERROR]},{from:exports.ConversationStateType.STREAMING,to:[exports.ConversationStateType.COMPLETED,exports.ConversationStateType.ERROR]},{from:exports.ConversationStateType.COMPLETED,to:[exports.ConversationStateType.IDLE,exports.ConversationStateType.WAITING,exports.ConversationStateType.ERROR]},{from:exports.ConversationStateType.ERROR,to:[exports.ConversationStateType.IDLE]}]}reset(){this._currentState=exports.ConversationStateType.IDLE}}class O extends h{constructor(e,t){super(),this._currentQuestion=null,this._currentAnswer=null,this._sessionId=e,this._stateMachine=new Le,i.info(`Conversation session created: ${e}`)}get sessionId(){return this._sessionId}get state(){return this._stateMachine.getCurrentState()}_setQuestion(e){this._currentQuestion=e,this._stateMachine.transitionTo(exports.ConversationStateType.WAITING),this._syncContextState()}get currentQuestion(){return this._currentQuestion}_startAnswer(e){this._currentAnswer={questionId:e,text:"",isComplete:!1,timestamp:Date.now()},this._stateMachine.transitionTo(exports.ConversationStateType.STREAMING),this._syncContextState()}_appendAnswerChunk(e){this._currentAnswer&&(this._currentAnswer.text+=e)}_completeAnswer(){this._currentAnswer&&(this._currentAnswer.isComplete=!0,this._currentAnswer.completedAt=Date.now()),this._stateMachine.transitionTo(exports.ConversationStateType.COMPLETED),this._syncContextState()}get currentAnswer(){return this._currentAnswer}_setError(e){this._stateMachine.transitionTo(exports.ConversationStateType.ERROR),this._syncContextState()}_syncContextState(){}handleEvent(e){const t=this._stateMachine.getCurrentState();switch(e.type){case"QUESTION":if(t!==exports.ConversationStateType.IDLE&&t!==exports.ConversationStateType.COMPLETED){i.warn(`Ignoring QUESTION event in invalid state: ${t}`);return}this._setQuestion(e.payload);break;case"ANSWER_START":if(t!==exports.ConversationStateType.WAITING){i.warn(`Ignoring ANSWER_START event in invalid state: ${t}. Expected: WAITING`);return}this._startAnswer(e.payload);break;case"ANSWER_CHUNK":if(t!==exports.ConversationStateType.STREAMING){i.warn(`Ignoring ANSWER_CHUNK event in invalid state: ${t}. Expected: STREAMING`);return}this._appendAnswerChunk(e.payload&&e.payload.chunk||""),e.payload&&e.payload.isComplete&&this._completeAnswer();break;case"ANSWER_COMPLETE":if(t!==exports.ConversationStateType.STREAMING){i.warn(`Ignoring ANSWER_COMPLETE event in invalid state: ${t}. Expected: STREAMING`);return}this._completeAnswer();break;case"ERROR":this._setError(e.payload);break;default:i.warn(`Unknown event type: ${e.type}`);break}}reset(){this._stateMachine.reset(),this._currentQuestion=null,this._currentAnswer=null}onDispose(){this.reset(),i.info(`Conversation session disposed: ${this._sessionId}`)}}class Pe extends h{constructor(){super(...arguments),this._buffers=new Map}appendChunk(e,t){this.ensureNotDisposed();const n=(this._buffers.get(e)||"")+t;return this._buffers.set(e,n),i.debug(`Message chunk appended for question ${e}, length: ${n.length}`),n}getMessage(e){return this._buffers.get(e)||null}complete(e){const t=this.getMessage(e)||"";return this._buffers.delete(e),i.debug(`Message assembly completed for question ${e}, length: ${t.length}`),t}clear(e){this._buffers.delete(e)}onDispose(){this._buffers.clear(),i.info("Message Assembler disposed")}}class Fe{generateSessionId(){return`session_${Date.now()}_${Math.random().toString(36).substring(2,9)}`}generateQuestionId(){return Math.random().toString(36).substring(2,6)}}class Me extends h{constructor(e,t){super(),this._transport=null,this._sessions=new Map,this._context=e,this._assembler=new Pe,this._idService=new Fe,i.info("Conversation Manager created")}setTransport(e){this._transport=e}async sendQuestion(e,t={speed:100,mood:0,vol:50}){this.ensureNotDisposed();const s=this._idService.generateQuestionId(),n={id:s,text:e,timestamp:Date.now(),inputMode:"text"},r=this._idService.generateSessionId(),a=new O(r,this._context);if(a.handleEvent({type:"QUESTION",payload:n}),this._sessions.set(s,a),this._transport)try{this._transport.sendText(s,e,200,t)}catch(c){throw i.error("Error sending question via transport:",c),a.handleEvent({type:"ERROR",payload:"Failed to send question"}),this._context.eventBus.emit("inner:sdk:error",{error:c instanceof Error?c:new Error(String(c))}),c}else i.warn("Transport not set, question not sent");return this._context.eventBus.emit("inner:conversation:question:sent",{questionId:s,text:e}),this._context.eventBus.emit("inner:conversation:answer:waiting",{questionId:s}),i.info(`Question sent: ${s}`),s}handleServerPong(){this.ensureNotDisposed(),this._transport&&this._transport.handlePongReceived()}willDisconnect(){this.ensureNotDisposed(),this._transport?.willDisconnect()}handleTextInput(e){this.sendQuestion(e).catch(t=>{i.error("Error sending question:",t)})}handleAnswerChunk(e,t,s){this.ensureNotDisposed();const n=this._sessions.get(e);if(!n){i.warn(`No active session for answer chunk: ${e}`);return}if(n.state===exports.ConversationStateType.WAITING&&n.handleEvent({type:"ANSWER_START",payload:e}),s){n.handleEvent({type:"ANSWER_COMPLETE"});const a=this._assembler.complete(e);this._context.eventBus.emit("inner:conversation:answer:completed",{questionId:e,fullAnswer:a});try{n.dispose()}catch(c){i.error("Error disposing session:",c)}this._sessions.delete(e)}else n.handleEvent({type:"ANSWER_CHUNK",payload:{uid:e,chunk:t,isComplete:s}}),this._assembler.appendChunk(e,t),this._context.eventBus.emit("inner:conversation:answer:chunk",{questionId:e,chunk:t,isComplete:s})}handleServerInitiatedMessage(e,t){this.ensureNotDisposed(),this._context.eventBus.emit("inner:conversation:server:message",{questionId:e,message:t,type:"text"})}handleAsrTextReceived(e,t){this.ensureNotDisposed();const s=this._idService.generateSessionId(),n=new O(s,this._context);n.handleEvent({type:"QUESTION",payload:{id:e,text:t,timestamp:Date.now(),inputMode:"voice"}}),this._sessions.set(e,n),this._context.eventBus.emit("inner:conversation:asr:received",{questionId:e,text:t})}onDispose(){for(const e of this._sessions.values())try{e.dispose()}catch(t){i.error("Error disposing session:",t)}this._sessions.clear(),this._assembler.dispose(),i.info("Conversation Manager disposed")}}class Ve extends h{constructor(e,t){super(),this._manager=new Me(e,t),i.info("Conversation Controller created")}get manager(){return this._manager}setTransport(e){this._manager.setTransport(e)}async sendQuestion(e,t){return this.ensureNotDisposed(),this._manager.sendQuestion(e,t)}onDispose(){this._manager.dispose(),i.info("Conversation Controller disposed")}}class Ue extends h{constructor(){super(...arguments),this._tracks=new Map,this._participants=new Map}register(e,t,s){this.ensureNotDisposed(),this._tracks.set(e,t),this._participants.has(s)||this._participants.set(s,new Set),this._participants.get(s).add(e),t.on("ended",()=>{i.debug(`Track ended: ${e}`),this.unregister(e)}),i.debug(`Track registered: ${e} from participant ${s}`)}unregister(e){const t=this._tracks.get(e);if(t){t.stop(),this._tracks.delete(e);for(const[s,n]of this._participants.entries())if(n.has(e)){n.delete(e),n.size===0&&this._participants.delete(s);break}i.debug(`Track unregistered: ${e}`)}}getTrack(e){return this._tracks.get(e)||null}getParticipantTracks(e){const t=this._participants.get(e);return t?Array.from(t):[]}getAllTrackIds(){return Array.from(this._tracks.keys())}getVideoAvailability(){let e=!1,t=!1,s=!1;for(const n of this._tracks.values()){const r=n.mediaStreamTrack;if(r.kind==="video"&&(e=!0,r.readyState==="live"&&(t=!0),r.enabled&&(s=!0),r.readyState==="live"&&r.enabled))return{available:!0}}return e?t?s?{available:!1,reason:v.NO_VIDEO_TRACK}:{available:!1,reason:v.TRACK_MUTED}:{available:!1,reason:v.TRACK_ENDED}:{available:!1,reason:v.NO_VIDEO_TRACK}}clear(){for(const e of this._tracks.values())e.stop();this._tracks.clear(),this._participants.clear(),i.info("All tracks cleared")}onDispose(){this.clear()}}class x extends h{constructor(e,t){super(),this._videoController=null,this._room=null,this._context=e,this._trackRegistry=t,this._loosTrackTimer=null,i.info("Video Track Handler created")}setRoom(e){this._room=e}setVideoController(e){this._videoController=e}handleTrackSubscribed(e,t,s){this.ensureNotDisposed(),this._loosTrackTimer&&clearTimeout(this._loosTrackTimer),this._trackRegistry.register(t,e,s),this._videoController&&e.attach(this._videoController.getSource().getInternalElement()),e.on("videoPlaybackStarted",()=>{i.info("inner:rtc:video:available"),this._context.eventBus.emit("inner:rtc:video:available",void 0)}),this._context.eventBus.emit("inner:video:track:added",{trackId:t,participantId:s}),i.debug(`Video track subscribed: ${t}`)}handleTrackUnsubscribed(e,t,s){this.ensureNotDisposed(),this._videoController&&e.detach(this._videoController.getSource().getInternalElement()),e.removeAllListeners(),this._context.eventBus.emit("inner:video:track:removed",{trackId:t,participantId:s}),this._loosTrackTimer&&clearTimeout(this._loosTrackTimer),this._loosTrackTimer=setTimeout(()=>{this._context.eventBus.emit("inner:rtc:video:unavailable",void 0)},Y),this._trackRegistry.unregister(t),i.debug(`Video track unsubscribed: ${t}`)}supports(e){return e.mediaStreamTrack.kind==="video"}onDispose(){this._loosTrackTimer&&(clearTimeout(this._loosTrackTimer),this._loosTrackTimer=null),i.info("Video Track Handler disposed")}}class L extends h{constructor(e,t){super(),this._audioController=null,this._room=null,this._context=e,this._trackRegistry=t,i.info("Audio Track Handler created")}setRoom(e){this._room=e}setAudioController(e){this._audioController=e}handleTrackSubscribed(e,t,s){this.ensureNotDisposed(),this._trackRegistry.register(t,e,s),this._audioController?e.attach(this._audioController.getOutputElement()):this._context.eventBus.emit("inner:audio:track:added",{trackId:t,participantId:s}),i.debug(`Audio track subscribed: ${t}`)}handleTrackUnsubscribed(e,t,s){this.ensureNotDisposed(),this._audioController?(this._audioController.removeOutputTrack(t),e.detach(this._audioController.getOutputElement())):this._context.eventBus.emit("inner:audio:track:removed",{trackId:t}),this._trackRegistry.unregister(t),i.debug(`Audio track unsubscribed: ${t}`)}supports(e){return e.mediaStreamTrack.kind==="audio"}onDispose(){i.info("Audio Track Handler disposed")}}class Ke extends h{constructor(e,t){super(),this._room=null,this._context=e,this._trackRegistry=t,this._handlers=[new x(e,t),new L(e,t)],i.info("LiveKit Event Adapter created")}setRoom(e){this._room=e,this._handlers.forEach(t=>{t.setRoom(e)}),console.log("LiveKit room set:",this._room)}setVideoController(e){const t=this._handlers.find(s=>s instanceof x);t&&t.setVideoController(e)}setAudioController(e){const t=this._handlers.find(s=>s instanceof L);t&&t.setAudioController(e)}handleTrackSubscribed(e,t,s){this.ensureNotDisposed();const n=this._findHandler(e);n?n.handleTrackSubscribed(e,t,s):i.warn(`No handler found for track, trackId: ${t}`)}handleTrackUnsubscribed(e,t){this.ensureNotDisposed();const s=this._trackRegistry.getTrack(e);if(s){const n=this._findHandler(s);n?n.handleTrackUnsubscribed(s,e,t):(this._trackRegistry.unregister(e),i.warn(`No handler found for track, trackId: ${e}`))}}_findHandler(e){return this._handlers.find(t=>t.supports(e))||null}onDispose(){this._handlers.forEach(e=>{try{e.dispose()}catch(t){i.error("Error disposing track handler:",t)}}),this._handlers.length=0,i.info("LiveKit Event Adapter disposed")}}class We extends h{constructor(e){super(),this._room=null,this._context=e,this._trackRegistry=new Ue,this._eventAdapter=new Ke(e,this._trackRegistry),this._maxAttempts=e.options.reconnect?.maxAttempts||S,this._maxDelay=(e.options.reconnect?.delay??y)*T,i.info("LiveKit Service created")}async connect(e,t,s,n){if(this.ensureNotDisposed(),this._room&&this._room.isConnected()){i.warn("Already connected to LiveKit room");return}try{i.info(`Connecting to LiveKit room: ${s} at ${e}`);const r=new f.Room({reconnectPolicy:{nextRetryDelayInMs:a=>a.elapsedMs<this._maxDelay?Math.min(T*Math.pow(2,a.retryCount),this._maxDelay):null}});r.prepareConnection(e,t),r.on(f.RoomEvent.TrackSubscribed,(a,c,l)=>{this._handleTrackSubscribed(a,c,l)}),r.on(f.RoomEvent.TrackUnsubscribed,(a,c,l)=>{this._handleTrackUnsubscribed(a,c,l)}),r.on(f.RoomEvent.TrackMuted,(a,c)=>{this._handleTrackMuted(a,c)}),r.on(f.RoomEvent.TrackUnmuted,(a,c)=>{this._handleTrackUnmuted(a,c)}),r.on(f.RoomEvent.Connected,()=>{i.info("Connected to LiveKit room"),this._context.eventBus.emit("inner:rtc:connected",void 0)}),r.on(f.RoomEvent.Disconnected,a=>{a?i.info(`LiveKit room disconnected: ${a}`):i.info("LiveKit room disconnected"),a!==f.DisconnectReason.CLIENT_INITIATED&&this._context.eventBus.emit("inner:rtc:disconnected",{reason:a?String(a):void 0}),this._room=null}),i.info("Connecting to LiveKit room..."),await r.connect(e,t,{autoSubscribe:!0,maxRetries:this._context.options.reconnect?.maxAttempts||S}),this._eventAdapter.setRoom(r),this._room=r}catch(r){throw u.fromError(r,d.LIVEKIT_CONNECT_FAILED)}}_handleTrackSubscribed(e,t,s){try{if(!e.mediaStreamTrack){i.warn("Track does not have MediaStreamTrack");return}e.kind==="audio"&&s.on(f.ParticipantEvent.IsSpeakingChanged,c=>{console.log("\u8F68\u9053 \u8BF4\u8BDD\u72B6\u6001:",c)});const r=t.trackSid,a=s.identity;this._eventAdapter.handleTrackSubscribed(e,r,a)}catch(n){i.error("Error handling track subscribed:",n)}}_handleTrackUnsubscribed(e,t,s){try{const n=t,r=s,a=n.trackSid,c=r.identity;this._trackRegistry.getTrack(a)?.kind==="video"&&this._context.eventBus.emit("inner:rtc:video:unavailable",void 0),this._eventAdapter.handleTrackUnsubscribed(a,c)}catch(n){i.error("Error handling track unsubscribed:",n)}}_handleTrackMuted(e,t){try{e.kind==="video"&&this._context.eventBus.emit("inner:rtc:video:unavailable",void 0)}catch(s){i.error("Error handling track muted:",s)}}_handleTrackUnmuted(e,t){try{e.kind==="video"&&this._context.eventBus.emit("inner:rtc:video:available",void 0)}catch(s){i.error("Error handling track unmuted:",s)}}async disconnect(){if(this._room)try{await this._room.disconnect(),this._room=null,this._context.eventBus.emit("inner:rtc:disconnected",{reason:"Disconnected from LiveKit room"}),i.info("Disconnected from LiveKit room")}catch(e){i.error("Error disconnecting from LiveKit room:",e),this._room=null}}isConnected(){return this._room?this._room.state===f.ConnectionState.Connected:!1}getTrackRegistry(){return this._trackRegistry}get trackRegistry(){return this._trackRegistry}getEventAdapter(){return this._eventAdapter}onDispose(){this.disconnect(),this._trackRegistry.dispose(),this._eventAdapter.dispose(),i.info("LiveKit Service disposed")}}class Be extends h{constructor(e){super(),this._conversationManager=null,this._context=e,i.info("WebSocket Dispatcher created")}setConversationManager(e){this._conversationManager=e,i.debug("Conversation manager set in dispatcher")}dispatch(e){this.ensureNotDisposed();try{if(e.type==="ws"){this._handleWSServerMessage(e);return}if(e.type==="text"){this._handleServerTextMessage(e);return}}catch(t){i.error("Error dispatching message:",t)}}_handleWSServerMessage(e){e.code===101?(this._conversationManager?.handleServerPong(),i.debug("Received pingpong response")):e.code===102&&(i.warn("Server will disconnect"),this._conversationManager?.willDisconnect())}_handleServerTextMessage(e){if(!this._conversationManager){i.warn("Conversation manager not set, text message ignored");return}const{uid:t,payload:s,code:n}=e;n===200?(this._conversationManager.handleAnswerChunk(t,s,!1),i.debug(`Answer received (code 401) for uid: ${t}`)):n===201?(this._conversationManager.handleAnswerChunk(t,"",!0),i.debug(`Answer completed (code 411) for uid: ${t}`)):n===300?(this._conversationManager.handleAsrTextReceived(t,s),i.debug(`ASR text chunk received (code 410) for uid: ${t}`)):n===400&&(this._conversationManager.handleServerInitiatedMessage(t,s),i.debug(`Server initiated message received (code 412) for uid: ${t}`))}onDispose(){i.info("WebSocket Dispatcher disposed")}}class $e extends h{constructor(e){super(),this._ws=null,this._dispatcher=null,this._isConnecting=!1,this._activeTimers=new Set,this._retryCount=0,this._maxRetries=S,this._pingTimer=void 0,this._pongTimer=void 0,this._isExplicitDisconnect=!1,this._context=e,this._maxRetries=e.options.reconnect?.maxAttempts??S,this._maxDelay=(e.options.reconnect?.delay??y)*T,this._dispatcher=new Be(e)}setConversationManager(e){this._dispatcher&&this._dispatcher.setConversationManager(e)}async connect(){if(this.ensureNotDisposed(),this._stopHeartbeat(),!(this._isConnecting||this.isConnected||this._ws?.readyState===WebSocket.OPEN)){this._isConnecting=!0,this._isExplicitDisconnect=!1;try{return this._ws=new WebSocket(this._context.wsUrl),new Promise((e,t)=>{this._ws.onopen=()=>{this._isConnecting=!1,this._retryCount=0,this.sendPing(),this._context.eventBus.emit("inner:ws:connected",void 0),i.info("Conversation transport connected"),e()},this._ws.onerror=s=>{this._isConnecting=!1,i.error("Conversation transport error:",s),this._context.eventBus.emit("inner:sdk:error",{error:new u("WebSocket connection error",d.WS_CONNECT_ERROR,s)}),t(new u("WebSocket connection error",d.WS_CONNECT_ERROR,s))},this._ws.onclose=s=>{const n=s.code,r=s.reason?s.reason:s.wasClean?"Clean close":"Closed by remote";this._handleDisconnect(n,r)},this._ws.onmessage=s=>{this._handleMessage(s.data)}})}catch(e){throw this._isConnecting=!1,this._context.emitError(e,d.WS_CONNECT_FAILED)}}}disconnect(){this._isExplicitDisconnect=!0,this._stopHeartbeat(),this._ws&&(this._ws.close(),this._ws=null),this._context.eventBus.emit("inner:ws:disconnected",{reason:"Client disconnect"}),i.info("Conversation transport disconnected")}_handleDisconnect(e,t){if(this._stopHeartbeat(),this._isConnecting=!1,e!==void 0&&new Set([1e3,1001,1002,1003,1007,1008]).has(e)){this._isExplicitDisconnect=!0;const n=`WebSocket closed (code ${e}): ${t??""}`;this._context.eventBus.emit("inner:ws:disconnected",{reason:n}),i.info(`Conversation transport closed with non-retry code ${e}: ${t??""}`);return}if(!this._isExplicitDisconnect&&this._retryCount<this._maxRetries){this._retryCount++;const n=Math.min(T*Math.pow(2,this._retryCount-1),this._maxDelay);i.warn(`Connection lost. Retry ${this._retryCount} in ${n}ms`);const r=setTimeout(()=>{!this._isExplicitDisconnect&&!this.isDisposed&&this.connect().catch(()=>{})},n);this._registerTimer(r)}else this._context.eventBus.emit("inner:ws:disconnected",{reason:"Closed"})}_registerTimer(e){return this._activeTimers.add(e),e}_clearTimer(e){e&&(clearTimeout(e),this._activeTimers.delete(e))}sendPing(){if(!this.isConnected)return;const e={type:"ws",code:101,payload:"ping"};this._ws?.send(JSON.stringify(e)),clearTimeout(this._pongTimer),this._pongTimer=setTimeout(()=>{i.error(`Pong timeout (${N/T}s). Reconnecting...`),this._ws?.close()},N)}handlePongReceived(){clearTimeout(this._pongTimer),this._clearTimer(this._pingTimer),this._pingTimer=setTimeout(()=>{this.sendPing()},J)}willDisconnect(){if(this.ensureNotDisposed(),this._ws&&this._ws.readyState===WebSocket.OPEN){const t=JSON.stringify({type:"ws",code:102,payload:"will disconnect"});this._ws.send(t),this._isExplicitDisconnect=!0,this._ws.close(),this._ws=null,this._context.eventBus.emit("inner:ws:disconnected",{reason:"Client responsive to server's disconnect message."})}}sendText(e,t,s=200,n){if(this.ensureNotDisposed(),!this._ws||this._ws.readyState!==WebSocket.OPEN)throw new u("WebSocket is not connected",d.WS_NOT_CONNECTED);try{const a=JSON.stringify({type:"text",code:s,uid:e,metadata:n,payload:t});this._ws.send(a),i.debug("Text message sent via transport:",e)}catch(r){throw this._context.emitError(r,d.WS_TEXT_SEND_FAILED)}}_handleMessage(e){try{if(typeof e!="string")return;const t=JSON.parse(e);i.debug("Message received:",t.type),this._dispatcher?this._dispatcher.dispatch(t):i.warn("Dispatcher not set, message ignored")}catch(t){i.error("Error parsing message:",t),this._context.emitError(t,d.WS_PARSE_ERROR)}}sendBinary(e){if(this.ensureNotDisposed(),!this._ws||this._ws.readyState!==WebSocket.OPEN){i.debug("WebSocket not connected, binary data dropped");return}try{this._ws.send(e),i.debug("Binary data sent via transport")}catch(t){i.error("Error sending binary data:",t)}}get isConnected(){return this._ws?.readyState===WebSocket.OPEN}_stopHeartbeat(){this._clearTimer(this._pingTimer),this._clearTimer(this._pongTimer)}onDispose(){this._stopHeartbeat(),this.disconnect(),this._dispatcher&&(this._dispatcher.dispose(),this._dispatcher=null),i.info("Conversation transport disposed")}}const A=["sdk:connected","sdk:disconnected","sdk:error","media:video:available","media:video:unavailable","media:video:trackAdded","media:video:trackRemoved","media:audio:trackAdded","media:audio:trackRemoved","media:audio:captureStarted","media:audio:captureStopped","media:audio:volumeChanged","media:audio:muted","media:audio:unmuted","conversation:question:sent","conversation:answer:waiting","conversation:server:message","conversation:asr:received","conversation:answer:chunk","conversation:answer:completed"],P=/^[a-z]+:[a-zA-z]+(:[a-zA-Z]+)?$/;class He{constructor(e){this.inner=e,this.listeners=new Map,this.forwardedVideoTrackIds=new Set,this.wsConnected=!1,this.livekitConnected=!1,this.httpConnected=!1,this.internalMap={"inner:sdk:error":{public:"sdk:error",sanitizer:t=>({message:t?.error?.message,code:t?.error?.code})},"inner:rtc:video:available":{public:"media:video:available"},"inner:rtc:video:unavailable":{public:"media:video:unavailable"},"inner:video:track:added":{public:"media:video:trackAdded"},"inner:video:track:removed":{public:"media:video:trackRemoved"},"inner:audio:track:added":{public:"media:audio:trackAdded"},"inner:audio:track:removed":{public:"media:audio:trackRemoved"},"inner:audio:input:started":{public:"media:audio:captureStarted"},"inner:audio:input:stopped":{public:"media:audio:captureStopped"},"inner:audio:volume:changed":{public:"media:audio:volumeChanged",sanitizer:t=>({volume:t?.volume})},"inner:audio:muted":{public:"media:audio:muted"},"inner:audio:unmuted":{public:"media:audio:unmuted"},"inner:conversation:question:sent":{public:"conversation:question:sent",sanitizer:t=>({questionId:t?.questionId,text:String(t?.text||"")})},"inner:conversation:answer:waiting":{public:"conversation:answer:waiting",sanitizer:t=>({questionId:t?.questionId})},"inner:conversation:server:message":{public:"conversation:server:message",sanitizer:t=>({questionId:t?.questionId,message:String(t?.message||""),type:String(t?.type||"")})},"inner:conversation:asr:received":{public:"conversation:asr:received",sanitizer:t=>({questionId:t?.questionId,text:String(t?.text||"")})},"inner:conversation:answer:chunk":{public:"conversation:answer:chunk",sanitizer:t=>({questionId:t?.questionId,chunk:String(t?.chunk||"")})},"inner:conversation:answer:completed":{public:"conversation:answer:completed",sanitizer:t=>({questionId:t?.questionId,fullAnswer:String(t?.fullAnswer||"")})}},Object.keys(this.internalMap).forEach(t=>{this.inner.on(t,s=>this.handleInternalEvent(t,s))}),this.inner.on("inner:sdk:connected",t=>this.handleConnected(t,!0)),this.inner.on("inner:sdk:disconnected",t=>this.handleConnected(t,!1)),A.forEach(t=>{this.inner.on(t,s=>this.handlePublicEvent(t,s))})}publicAPI(){return{on:this.on.bind(this),off:this.off.bind(this),once:this.once.bind(this)}}on(e,t){const s=String(e);return this.assertAllowed(s),this.listeners.has(s)||this.listeners.set(s,new Set),this.listeners.get(s).add(t),()=>this.off(e,t)}once(e,t){const s=String(e);this.assertAllowed(s);const n=r=>{try{t(r)}finally{this.off(e,n)}};return this.on(e,n)}off(e,t){const s=String(e);this.assertAllowed(s);const n=this.listeners.get(s);n&&(n.delete(t),n.size===0&&this.listeners.delete(s))}handleInternalEvent(e,t){const s=this.internalMap[e];if(!s)return;const n=s.public;if(A.indexOf(n)===-1)return;if(n==="media:video:trackAdded"){const c=t?.trackId||t?.id;if(typeof c=="string"){if(this.forwardedVideoTrackIds.has(c))return;this.forwardedVideoTrackIds.add(c)}}const r=s.sanitizer?s.sanitizer(t):t;if(!P.test(n))return;const a=this.listeners.get(n);!a||a.size===0||a.forEach(c=>{try{c(r)}catch(l){console.error("PublicEventEmitter listener error",l)}})}handlePublicEvent(e,t){if(A.indexOf(e)===-1)return;const s=Object.values(this.internalMap).find(a=>a.public===e),n=s&&s.sanitizer?s.sanitizer(t):t;if(e==="media:video:trackAdded"){const a=t?.trackId||t?.id;if(typeof a=="string"){if(this.forwardedVideoTrackIds.has(a))return;this.forwardedVideoTrackIds.add(a)}}const r=this.listeners.get(e);!r||r.size===0||r.forEach(a=>{try{a(n)}catch(c){console.error("PublicEventEmitter listener error",c)}})}handleConnected(e,t){e.source==="livekit"?this.livekitConnected=t:e.source==="ws"?this.wsConnected=t:e.source==="http"&&(this.httpConnected=t),this.maybeEmitSdkConnected(t?"connected":"disconnected")}maybeEmitSdkConnected(e){const t=e==="connected",s=t?!!this.livekitConnected:!this.livekitConnected,n=t?!!this.wsConnected:!this.wsConnected,r=t?!!this.httpConnected:!this.httpConnected,a={livekit:s,ws:n,http:r,all:s&&n&&r},c=JSON.stringify(a);this.lastConnectedPayloadJson!==c&&(this.lastConnectedPayloadJson=c,this.inner.emit(`sdk:${e}`,a))}assertAllowed(e){if(A.indexOf(e)===-1)throw new Error(`Event "${e}" is not allowed for public consumers`);if(!P.test(e))throw new Error(`Event name "${e}" does not follow namespace:domain[:action] naming`)}}class ze extends h{constructor(e,t,s){super(),this._requestCounter=0,this._context=e,this._baseURL=t||"",this._defaultHeaders=s||{},i.info(`HTTP Service created with baseURL: ${this._baseURL||"none"}`)}setAuthToken(e){this._defaultHeaders.Authorization=`Bearer ${e}`}request(e){this.ensureNotDisposed();const t=e.requestId||`req_${Date.now()}_${++this._requestCounter}`,s=this._buildURL(e.url,e.params),n=this._mergeHeaders(e.headers),r={method:e.method||"GET",headers:n};e.data!==void 0&&(e.method||"GET")!=="GET"&&(typeof e.data=="string"?r.body=e.data:(r.body=JSON.stringify(e.data),!n["Content-Type"]&&!n["content-type"]&&(n["Content-Type"]="application/json")));const a=e.timeout||3e4,c=new AbortController;return r.signal=c.signal,new Promise((l,p)=>{const m=setTimeout(()=>c.abort(),a);fetch(s,r).then(async _=>{clearTimeout(m);const g={};_.headers.forEach((G,j)=>{g[j]=G});let E;const b=_.headers.get("content-type");b&&b.includes("application/json")?E=await _.json():E=await _.text();const q={requestId:t,status:_.status,headers:g,data:E,config:e};i.debug(`HTTP request succeeded: ${e.method} ${s} [${t}]`),l(q)}).catch(_=>{clearTimeout(m);const g=_&&_.name==="AbortError",E={requestId:t,status:_&&_.status||void 0,message:g?"Request aborted (timeout)":_&&_.message||"HTTP request failed",error:_,config:e};i.error(`HTTP request failed: ${e.method} ${s} [${t}]`,_),p(E)})})}get(e,t,s){return this.request({...s,url:e,method:"GET",params:t})}post(e,t,s){return this.request({...s,url:e,method:"POST",data:t})}put(e,t,s){return this.request({...s,url:e,method:"PUT",data:t})}delete(e,t){return this.request({...t,url:e,method:"DELETE"})}patch(e,t,s){return this.request({...s,url:e,method:"PATCH",data:t})}_buildURL(e,t){if(e.startsWith("http://")||e.startsWith("https://"))return this._appendParams(e,t);let s=this._baseURL;return s&&!s.endsWith("/")&&!e.startsWith("/")?s+="/":s&&s.endsWith("/")&&e.startsWith("/")&&(s=s.slice(0,-1)),s+=e,this._appendParams(s,t)}_appendParams(e,t){if(!t||Object.keys(t).length===0)return e;const s=new URLSearchParams;for(const[r,a]of Object.entries(t))s.append(r,String(a));const n=e.includes("?")?"&":"?";return`${e}${n}${s.toString()}`}_mergeHeaders(e){const t={...this._defaultHeaders};return e&&Object.assign(t,e),t}onDispose(){i.info("HTTP Service disposed")}}function qe(o,e="https"){return o.startsWith(e)?o:`${e}://${o}`}class Ge extends h{constructor(e,t){super(),this._context=e,this._httpService=new ze(e,t||te)}getAuthToken(){return this.ensureNotDisposed(),this._httpService?.request({url:"/auth/login",method:"GET",data:{}}).then(e=>{e.data.code===C&&this._httpService?.setAuthToken(e.data.data.token)}).catch(e=>{throw this._context.emitError(e,d.SDK_AUTH_TOKEN_FAILED)})}getLivekitConfig(){return this.ensureNotDisposed(),this._httpService?.request({url:"/stream/v1/start",method:"POST",data:{avatarName:this._context.options.avatarName,voice:this._context.options.avatarVoice||""}}).then(e=>{if(e.data.code===C){const t=e.data.data;return this._context.setLivekitConfig(qe(t.livekitUrl,"wss"),t.roomToken,t.roomId),this._context.setWsUrl(`${this._context.options.wsUrlPrefix||ie}/${t.roomId}/${this._context.options.avatarName}`),this._context.setVideoOptions({renderMode:t.greenScreen.enabled?"processed":"raw",greenScreen:t.greenScreen||{}}),this._context.eventBus.emit("inner:sdk:connected",{source:"http"}),!0}else throw new u(e.data.msg,d.SDK_GET_LIVEKIT_CONFIG_FAILED)}).catch(e=>{throw this._context.eventBus.emit("inner:sdk:error",{error:new u(e.message,d.SDK_GET_LIVEKIT_CONFIG_FAILED)}),u.fromError(e,d.SDK_GET_LIVEKIT_CONFIG_FAILED)})}switchVideo({onceActions:e,loopActions:t}){return this.ensureNotDisposed(),this._httpService?.request({url:"/avatar/v1/switch-video",method:"POST",data:{roomId:this._context.roomId,onceActions:e,loopActions:t}}).then(s=>{if(s.data.code===C)return!0;throw this._context.emitError(s.data.message,d.SDK_SWITCH_VIDEO_FAILED)})}interruptConversation(){return this.ensureNotDisposed(),this._httpService?.request({url:"/stream/v1/interrupt",method:"POST",data:{roomId:this._context.roomId}}).then(e=>{if(e.data.code===C)return!0;throw this._context.emitError(e.data.message,d.SDK_INTERRUPT_CONVERSATION_FAILED)}).finally(()=>{this._context.eventBus.emit("inner:sdk:disconnected",{source:"http"})})}disconnectConversation(){return this.ensureNotDisposed(),this._httpService?.request({url:"/stream/v1/stop",method:"POST",data:{roomId:this._context.roomId}}).then(e=>e.data.code===C).finally(()=>{this._context.eventBus.emit("inner:sdk:disconnected",{source:"http"})})}onDispose(){this._httpService.dispose()}}class z extends h{constructor(e){super(),this._videoController=null,this._audioController=null,this._conversationController=null,this._liveKitService=null,this._conversationTransport=null,this._connectionCoordinator=null,this._httpController=null,this._preConnectPromise=null,this._lastPreConnectSuccessAt=null,this._context=new re(e),i.info("SDK Client created")}get events(){return this.ensureNotDisposed(),this._publicEventAPI||(this._publicEmitter=new He(this._context.eventBus),this._publicEventAPI=this._publicEmitter.publicAPI()),this._publicEventAPI}pushActions({onceActions:e,loopActions:t}){return this.ensureNotDisposed(),this._ensureConnected(),this._httpController?.switchVideo({onceActions:e,loopActions:t})}interruptConnection(){return this.ensureNotDisposed(),this._ensureConnected(),this._httpController?.interruptConversation()}sendTextQuestion(e){if(this.ensureNotDisposed(),this._ensureConnected(),this._conversationController)return this._conversationController.sendQuestion(e);throw new u("Conversation controller is not available",d.CONVERSATION_CONTROLLER_NOT_AVAILABLE)}setRenderFitMode(e){this.ensureNotDisposed(),this._ensureConnected(),this._videoController&&this._videoController.setRenderFitMode(e)}async startAudioCapture(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",d.AUDIO_CONTROLLER_NOT_AVAILABLE);await this._audioController.startCapture()}async stopAudioCapture(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",d.AUDIO_CONTROLLER_NOT_AVAILABLE);await this._audioController.stopCapture()}setVolume(e){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",d.AUDIO_CONTROLLER_NOT_AVAILABLE);this._audioController.setVolume(e)}getVolume(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",d.AUDIO_CONTROLLER_NOT_AVAILABLE);return this._audioController?.getVolume()||1}mute(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",d.AUDIO_CONTROLLER_NOT_AVAILABLE);this._audioController.mute()}unmute(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",d.AUDIO_CONTROLLER_NOT_AVAILABLE);this._audioController.unmute()}get isMuted(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",d.AUDIO_CONTROLLER_NOT_AVAILABLE);return this._audioController.isMuted()}get isAudioCapturing(){return this.ensureNotDisposed(),this._ensureConnected(),this._audioController?this._audioController.isCapturing:!1}async preConnect(){if(this.ensureNotDisposed(),this._preConnectPromise)return this._preConnectPromise;const e=Date.now();return this._lastPreConnectSuccessAt!==null&&e-this._lastPreConnectSuccessAt<ne?Promise.resolve(!0):(this._preConnectPromise=(async()=>{try{if(i.info("Creating HTTP controller..."),this._httpController=this._httpController??new Ge(this._context,this._context.options.http?.baseURL),i.info("Getting auth token..."),await this._httpController.getAuthToken(),i.info("Getting livekit config..."),!!!await this._httpController?.getLivekitConfig())throw new Error("Config missing");return this._lastPreConnectSuccessAt=Date.now(),!0}catch(t){throw this._context.emitError(t,d.SDK_PRECONNECT_FAILED)}finally{this._preConnectPromise=null}})(),this._preConnectPromise)}async connect(){if(this.ensureNotDisposed(),this._connectionCoordinator&&this._context.sessionState.isConnected){i.warn("SDK already initialized");return}try{if(!await this.preConnect())throw new u("PreConnect failed without explicit error",d.SDK_CONNECT_FAILED)}catch(e){throw this._context.emitError(e,d.SDK_CONNECT_FAILED)}try{if(i.info("Initializing SDK..."),this._connectionCoordinator=new ae(this._context),this._connectionCoordinator.start(),this._connectionCoordinator.startConnecting(),this._videoController=new _e(this._context,this._context.options.video),this._audioController=new xe(this._context,this._context.options.audio),this._conversationController=new Ve(this._context),this._liveKitService=new We(this._context),this._conversationTransport=new $e(this._context),this._liveKitService&&this._videoController&&this._audioController){const e=this._liveKitService.getEventAdapter();e&&(typeof e.setVideoController=="function"&&e.setVideoController(this._videoController),typeof e.setAudioController=="function"&&e.setAudioController(this._audioController))}this._audioController&&this._conversationTransport&&this._audioController.setWebSocketSender({sendBinary:e=>{this._conversationTransport.sendBinary(e)},isConnected:()=>this._conversationTransport.isConnected}),this._conversationTransport&&this._conversationController&&(this._conversationTransport.setConversationManager(this._conversationController.manager),this._conversationController.setTransport(this._conversationTransport)),await this._liveKitService.connect(this._context.livekitUrl,this._context.token,this._context.options.avatarName),this._conversationTransport&&await this._conversationTransport.connect(),i.info("SDK initialized and connected")}catch(e){throw this._connectionCoordinator&&this._connectionCoordinator.transitionToIdle(),await this._cleanup(),this._context.emitError(e,d.SDK_INITIALIZATION_FAILED)}}async disconnect(){if(this.ensureNotDisposed(),!(!this._connectionCoordinator||this._connectionCoordinator.getState()==="idle"||this._connectionCoordinator.getState()==="disposed")){i.info("Disconnecting SDK..."),this._connectionCoordinator.startDisconnecting();try{this._conversationTransport&&this._conversationTransport.disconnect(),this._liveKitService&&await this._liveKitService.disconnect(),this._audioController&&this._audioController.stopCapture(),this._httpController&&await this._httpController.disconnectConversation(),this._preConnectPromise=null,this._connectionCoordinator.completeDisconnecting(),i.info("SDK disconnected")}catch(e){throw i.error("Error during disconnect:",e),this._context.emitError(e,d.SDK_DISCONNECT_FAILED)}}}async reconnect(){this.ensureNotDisposed();const e=this._connectionCoordinator;if(!e?.canStartReconnectingManual()){const s=e?e.getSnapshot():this.connectionSnapshot;return i.warn("SDK reconnect not allowed in current state, returning current snapshot"),s}const t=e.captureSnapshotAndStartReconnectingManual();try{if(this._audioController&&this._audioController.stopCapture(),t.rtc.connected&&!t.rtc.hasVideoTrack&&(await this._liveKitService?.disconnect(),await this._conversationTransport?.disconnect()),this._conversationTransport&&!this._conversationTransport.isConnected){if(!await this.preConnect())throw new u("PreConnect failed without explicit error",d.SDK_CONNECT_FAILED);await this._conversationTransport.connect()}return this._liveKitService&&!this._liveKitService.isConnected()&&await this._liveKitService.connect(this._context.livekitUrl,this._context.token,this._context.options.avatarName),t}catch(s){throw i.error("Error during reconnect:",s),this._context.emitError(s,d.SDK_RECONNECT_FAILED)}}get connectionSnapshot(){return this.ensureNotDisposed(),this._connectionCoordinator?this._connectionCoordinator.getSnapshot():K(U("idle"))}async _cleanup(){if(this._conversationTransport&&this._conversationTransport.disconnect(),this._liveKitService)try{await this._liveKitService.disconnect()}catch(e){i.error("Error disconnecting LiveKit:",e)}this._audioController&&this._audioController.stopCapture()}get isConnected(){return this.ensureNotDisposed(),this._context.sessionState.isConnected}_ensureConnected(){if(!this._context.sessionState.isConnected)throw new u("SDK is not connected. Call connect() first.",d.SDK_NOT_CONNECTED)}onDispose(){this.disconnect().catch(e=>{i.error("Error during disconnect in dispose:",e)}),this._videoController&&(this._videoController.dispose(),this._videoController=null),this._audioController&&(this._audioController.dispose(),this._audioController=null),this._conversationController&&(this._conversationController.dispose(),this._conversationController=null),this._liveKitService&&(this._liveKitService.dispose(),this._liveKitService=null),this._conversationTransport&&(this._conversationTransport.dispose(),this._conversationTransport=null),this._httpController&&(this._httpController.dispose(),this._httpController=null),this._connectionCoordinator&&(this._connectionCoordinator.transitionToDisposed(),this._connectionCoordinator.dispose(),this._connectionCoordinator=null),this._context.dispose(),i.info("SDK Client disposed")}}function je(o){if(!o.avatarName)throw new u("avatarName is required",d.NO_AVATARNAME);return i.info("Creating SDK client with options:",{avatarName:o.avatarName}),new z(o)}class F extends h{constructor(e,t){super(),this._videoElement=null,this._videoUrl=null,this._isPlaying=!1,this._frameCallbackId=null,this._videoUrl=e,t?this._videoElement=t:(this._videoElement=document.createElement("video"),this._videoElement.style.opacity="0",this._videoElement.muted=!0,this._videoElement.playsInline=!0,this._videoElement.loop=!0,document.querySelector("#video-container")?.appendChild(this._videoElement)),i.info("Local Video Debug Source created")}get videoElement(){return this._videoElement}async load(){if(!this._videoElement)throw new Error("Video element or URL not set");return new Promise((e,t)=>{const s=this._videoElement,n=()=>{s.removeEventListener("loadedmetadata",n),s.removeEventListener("error",r),i.info(`Video loaded: ${s.videoWidth}x${s.videoHeight}`),e()},r=a=>{s.removeEventListener("loadedmetadata",n),s.removeEventListener("error",r),t(new Error(`Failed to load video: ${a}`))};s.addEventListener("loadedmetadata",n),s.addEventListener("error",r),s.src=this._videoUrl||"",s.load()})}async play(){if(!this._videoElement)throw new Error("Video element not set");if(!this._isPlaying)try{await this._videoElement.play(),this._videoElement.style.opacity="0",this._videoElement.setAttribute("loop","true"),this._isPlaying=!0,i.info("Video playback started")}catch(e){throw i.error("Failed to play video:",e),e}}pause(){this._videoElement&&this._isPlaying&&(this._videoElement.pause(),this._isPlaying=!1,i.info("Video playback paused"))}stop(){this._videoElement&&(this._videoElement.pause(),this._videoElement.currentTime=0,this._isPlaying=!1,i.info("Video playback stopped"))}get isPlaying(){return this._isPlaying&&this._videoElement!==null&&!this._videoElement.paused}get videoWidth(){return this._videoElement?.videoWidth||0}get videoHeight(){return this._videoElement?.videoHeight||0}async captureFrame(){if(!this._videoElement||this._videoElement.readyState<2)return null;try{if("VideoFrame"in window&&typeof window.VideoFrame=="function")try{return new VideoFrame(this._videoElement,{timestamp:this._videoElement.currentTime*1e6})}catch(s){i.error("",s)}const e=await createImageBitmap(this._videoElement),t=new VideoFrame(e,{timestamp:this._videoElement.currentTime*1e6});return e.close(),t}catch(e){return i.error("Failed to capture video frame:",e),null}}onFrame(e){if(!this._videoElement)return;this.offFrame();const t=this._videoElement;if("requestVideoFrameCallback"in t){const s=(n,r)=>{!this._isPlaying||!this._videoElement||this.captureFrame().then(a=>{a&&(e(a),a.close()),this._isPlaying&&this._videoElement&&(this._frameCallbackId=t.requestVideoFrameCallback(s))}).catch(a=>{i.error("Error in frame callback:",a)})};this._frameCallbackId=t.requestVideoFrameCallback(s)}else{const s=()=>{!this._isPlaying||!this._videoElement||this.captureFrame().then(n=>{n&&(e(n),n.close()),this._isPlaying&&this._videoElement&&(this._frameCallbackId=requestAnimationFrame(s))}).catch(n=>{i.error("Error in frame callback:",n)})};this._frameCallbackId=requestAnimationFrame(s)}}offFrame(){this._frameCallbackId!==null&&(this._videoElement&&"cancelVideoFrameCallback"in this._videoElement?this._videoElement.cancelVideoFrameCallback(this._frameCallbackId):cancelAnimationFrame(this._frameCallbackId),this._frameCallbackId=null)}onDispose(){this.stop(),this.offFrame(),this._videoElement&&this._videoElement.parentElement===document.body&&document.body.removeChild(this._videoElement),this._videoElement=null,this._videoUrl=null,i.info("Local Video Debug Source disposed")}}class Qe extends h{constructor(e,t,s){super(),this._outputCanvas=null,this._outputRenderer=null,this._source=new B;const n={...e,renderMode:"processed"};this._pipeline=new H(this._source,n),this._outputRenderer=new D(s,t),this._pipeline.setRenderer(this._outputRenderer),this._outputCanvas=this._outputRenderer.getCanvas(),i.info("Video Pipeline Runner created")}get outputCanvas(){return this._outputCanvas}get processor(){return this._pipeline.strategy.getProcessors()[0]||null}setProcessor(e){this._pipeline.setProcessor(e),i.info("Processor set on pipeline")}async processFrame(e){if(this.ensureNotDisposed(),!this._outputRenderer){i.error("Output renderer not available"),e.close();return}try{const t=this._pipeline.strategy.getProcessors()[0]||null;let s=null;if(t){try{s=t.process(e)}catch(n){i.error("Error processing frame:",n),s=e}if(s===null&&"getImageData"in t){const n=t.getImageData();if(n&&this._outputCanvas&&this._outputRenderer.getContext()){const r=this._outputRenderer.getContext();r&&((this._outputCanvas.width!==n.width||this._outputCanvas.height!==n.height)&&(this._outputCanvas.width=n.width,this._outputCanvas.height=n.height),r.putImageData(n,0,0))}e.close();return}s&&s!==e?(this._outputRenderer.render(s),e.close()):s===e?this._outputRenderer.render(e):e.close()}else this._outputRenderer.render(e)}catch(t){i.error("Error in processFrame:",t),e.close()}}setOutputSize(e,t){this._outputCanvas&&(this._outputCanvas.width=e,this._outputCanvas.height=t,i.info(`Output canvas size set to ${e}x${t}`))}onDispose(){this._pipeline.dispose(),this._outputRenderer&&(this._outputRenderer.dispose(),this._outputRenderer=null),this._outputCanvas&&this._outputCanvas.parentElement===document.body&&document.body.removeChild(this._outputCanvas),this._outputCanvas=null,i.info("Video Pipeline Runner disposed")}}class Ye extends h{constructor(e,t,s,n){if(super(),this._isRunning=!1,this._videoSource=new F(e,t),this._options={renderMode:"processed",containerElement:t,...n},this._pipelineRunner=new Qe(this._options,t,s),this._options.greenScreen?.enabled){const r=new w(this._options.greenScreen);this._pipelineRunner.setProcessor(r),i.info("Green screen processor enabled in debugger")}i.info("Standalone Video Debugger created")}async switchSource(e,t){this.ensureNotDisposed();const s=this._videoSource,n=this._isRunning,r=new F(e,t);try{await r.load(),n&&await r.play(),r.onFrame(l=>{this._pipelineRunner.processFrame(l).catch(p=>{i.error("Error processing frame:",p)})}),this._videoSource=r;try{s.offFrame(),s.stop(),s.dispose()}catch(l){console.warn("Error disposing old video source during switch:",l)}const a=this._videoSource.videoWidth,c=this._videoSource.videoHeight;a>0&&c>0&&this._pipelineRunner.setOutputSize(a,c)}catch(a){i.error("Failed to switch video source:",a);try{r.dispose()}catch(c){console.warn("Error disposing new video source after failed switch:",c)}throw a}}get outputCanvas(){return this._pipelineRunner.outputCanvas}get videoSource(){return this._videoSource}get processor(){return this._pipelineRunner.processor}setProcessor(e){this._pipelineRunner.setProcessor(e)}updateGreenScreenOptions(e){const t=this._pipelineRunner.processor;if(t&&"updateOptions"in t&&typeof t.updateOptions=="function")t.updateOptions(e),i.info("Green screen options updated");else{const s=this._options.greenScreen||{enabled:!1},n={...s,...e,enabled:e.enabled!==void 0?e.enabled:s.enabled},r=new w(n);this._pipelineRunner.setProcessor(r),this._options.greenScreen=n,i.info("Green screen processor recreated with new options")}}async initialize(){this.ensureNotDisposed();try{await this._videoSource.load();const e=this._videoSource.videoWidth,t=this._videoSource.videoHeight;e>0&&t>0&&this._pipelineRunner.setOutputSize(e,t),i.info("Video debugger initialized")}catch(e){throw i.error("Failed to initialize video debugger:",e),e}}async start(){if(this.ensureNotDisposed(),this._isRunning){i.warn("Debugger is already running");return}try{this._videoSource.videoWidth===0&&await this.initialize(),await this._videoSource.play(),this._videoSource.onFrame(e=>{this._pipelineRunner.processFrame(e).catch(t=>{i.error("Error processing frame:",t)})}),this._isRunning=!0,i.info("Video debugger started")}catch(e){throw i.error("Failed to start video debugger:",e),e}}stop(){this._isRunning&&(this._videoSource.stop(),this._videoSource.offFrame(),this._isRunning=!1,i.info("Video debugger stopped"))}pause(){this._isRunning&&(this._videoSource.pause(),i.info("Video debugger paused"))}async resume(){this._isRunning&&(await this._videoSource.play(),i.info("Video debugger resumed"))}get isRunning(){return this._isRunning}onDispose(){this.stop(),this._videoSource.dispose(),this._pipelineRunner.dispose(),i.info("Standalone Video Debugger disposed")}}exports.DEFAULT_AUDIO_BUFFER_SIZE=ee;exports.DEFAULT_AUDIO_CHANNELS=Z;exports.DEFAULT_AUDIO_SAMPLE_RATE=R;exports.DEFAULT_RECONNECT_ATTEMPTS=S;exports.DEFAULT_RECONNECT_DELAY=y;exports.DEFAULT_WS_TIMEOUT=X;exports.Disposable=h;exports.EventBus=M;exports.Logger=i;exports.SDKClient=z;exports.SDKError=u;exports.SessionState=V;exports.StandaloneVideoDebugger=Ye;exports.createClient=je;
|
package/dist/index.d.ts
CHANGED
package/dist/index.esm.js
CHANGED
|
@@ -107,4 +107,4 @@ class AudioResamplerProcessor extends AudioWorkletProcessor {
|
|
|
107
107
|
}
|
|
108
108
|
|
|
109
109
|
registerProcessor('audio-resampler', AudioResamplerProcessor);
|
|
110
|
-
`;function xe(){const o=new Blob([Oe],{type:"text/javascript"});return URL.createObjectURL(o)}class Le extends ke{constructor(e){super(),this.options=e,this._stream=null,this._audioContext=null,this._sourceNode=null,this._workletNode=null,this._targetSampleRate=k,this._bitDepth=O,this._channels=1;const t=e?.input;this._targetSampleRate=t?.sampleRate||k,this._bitDepth=t?.bitDepth||O,this._channels=t?.channels||1}async start(){this.ensureNotDisposed();try{const t={audio:this.options?.input?.constraints||{channelCount:this._channels}};this._stream=await navigator.mediaDevices.getUserMedia(t);const r=this._stream.getAudioTracks()[0].getSettings().sampleRate||44100;this._audioContext=new AudioContext({sampleRate:r});const a=xe();await this._audioContext.audioWorklet.addModule(a),this._sourceNode=this._audioContext.createMediaStreamSource(this._stream),this._workletNode=new AudioWorkletNode(this._audioContext,"audio-resampler",{processorOptions:{targetSampleRate:this._targetSampleRate,bitDepth:this._bitDepth,channels:this._channels},numberOfInputs:1,numberOfOutputs:0}),this._workletNode.port.onmessage=c=>{const{type:h,data:m,sampleRate:g}=c.data;h==="audioData"&&m&&this.emitAudioData(m,g)},this._sourceNode.connect(this._workletNode),i.info(`Audio capture started: input=${r}Hz -> output=${this._targetSampleRate}Hz, ${this._bitDepth}bit`)}catch(e){throw u.fromError(e,d.AUDIO_CAPTURE_FAILED)}}stop(){this._stream&&(this._stream.getTracks().forEach(e=>e.stop()),this._stream=null),this._workletNode&&(this._workletNode.port.onmessage=null,this._workletNode.disconnect(),this._workletNode=null),this._sourceNode&&(this._sourceNode.disconnect(),this._sourceNode=null),this._audioContext&&(this._audioContext.close(),this._audioContext=null),i.info("Audio capture stopped")}onDispose(){this.stop()}}class Pe{create(e){return new Le(e)}}class Fe extends l{constructor(e,t,s){super(),this._source=null,this._isCapturing=!1,this._currentOutputTrackId=null,this._context=e,this._inputPipeline=new ve(t),this._outputPipeline=new Ee,this._frameSender=new Ie(e,t),this._sourceFactory=s||new Pe,this._inputPipeline.onProcessedAudio=(n,r)=>{this._context.eventBus.emit("inner:audio:frame:received",{data:n,sampleRate:r})},i.info("Audio Controller created")}setWebSocketSender(e){this._frameSender.setWebSocketSender(e)}async startCapture(){if(this.ensureNotDisposed(),this._isCapturing){i.warn("Audio capture already started");return}try{this._source||(this._source=this._sourceFactory.create(this._context.options.audio),this._source.onAudioData=(e,t)=>{this._inputPipeline.process(e,t)}),await this._source.start(),this._isCapturing=!0,this._frameSender.start(),this._context.eventBus.emit("inner:audio:input:started",void 0),i.info("Audio capture started")}catch(e){throw this._context.emitError(e,d.AUDIO_CAPTURE_START_FAILED)}}stopCapture(){this.ensureNotDisposed(),this._isCapturing&&(this._source&&this._source.stop(),this._frameSender.stop(),this._isCapturing=!1,this._context.eventBus.emit("inner:audio:input:stopped",void 0),i.info("Audio capture stopped"))}get isCapturing(){return this._isCapturing}setOutputTrack(e,t,s){this.ensureNotDisposed(),this._outputPipeline.setTrack(e),this._currentOutputTrackId=t,this._context.eventBus.emit("inner:audio:track:added",{trackId:t,participantId:s}),i.info("Audio output track ready")}removeOutputTrack(e){this.ensureNotDisposed(),this._currentOutputTrackId===e&&(this._outputPipeline.removeTrack(),this._currentOutputTrackId=null,this._context.eventBus.emit("inner:audio:track:removed",{trackId:e}),i.info("Audio output track removed"))}setVolume(e){this.ensureNotDisposed(),this._outputPipeline.setVolume(e),this._context.eventBus.emit("inner:audio:volume:changed",{volume:e}),i.debug(`Audio volume set to: ${e}`)}getVolume(){return this._outputPipeline.getVolume()}mute(){this.ensureNotDisposed(),this._outputPipeline.mute(),this._context.eventBus.emit("inner:audio:muted",void 0),i.debug("Audio muted")}unmute(){this.ensureNotDisposed(),this._outputPipeline.unmute(),this._context.eventBus.emit("inner:audio:unmuted",void 0),i.debug("Audio unmuted")}getOutputElement(){return this._outputPipeline.getAudioElement()}isMuted(){return this._outputPipeline.isMuted()}onDispose(){this.stopCapture(),this._source&&(this._source.dispose(),this._source=null),this._inputPipeline.dispose(),this._outputPipeline.dispose(),this._frameSender.dispose(),i.info("Audio Controller disposed")}}var _;(function(o){o.IDLE="idle",o.WAITING="waiting",o.STREAMING="streaming",o.COMPLETED="completed",o.ERROR="error"})(_||(_={}));class Me extends B{constructor(){super(...arguments),this._currentState=_.IDLE,this.transitionTable=[{from:_.IDLE,to:[_.WAITING,_.ERROR]},{from:_.WAITING,to:[_.STREAMING,_.ERROR]},{from:_.STREAMING,to:[_.COMPLETED,_.ERROR]},{from:_.COMPLETED,to:[_.IDLE,_.WAITING,_.ERROR]},{from:_.ERROR,to:[_.IDLE]}]}reset(){this._currentState=_.IDLE}}class L extends l{constructor(e,t){super(),this._currentQuestion=null,this._currentAnswer=null,this._sessionId=e,this._stateMachine=new Me,i.info(`Conversation session created: ${e}`)}get sessionId(){return this._sessionId}get state(){return this._stateMachine.getCurrentState()}_setQuestion(e){this._currentQuestion=e,this._stateMachine.transitionTo(_.WAITING),this._syncContextState()}get currentQuestion(){return this._currentQuestion}_startAnswer(e){this._currentAnswer={questionId:e,text:"",isComplete:!1,timestamp:Date.now()},this._stateMachine.transitionTo(_.STREAMING),this._syncContextState()}_appendAnswerChunk(e){this._currentAnswer&&(this._currentAnswer.text+=e)}_completeAnswer(){this._currentAnswer&&(this._currentAnswer.isComplete=!0,this._currentAnswer.completedAt=Date.now()),this._stateMachine.transitionTo(_.COMPLETED),this._syncContextState()}get currentAnswer(){return this._currentAnswer}_setError(e){this._stateMachine.transitionTo(_.ERROR),this._syncContextState()}_syncContextState(){}handleEvent(e){const t=this._stateMachine.getCurrentState();switch(e.type){case"QUESTION":if(t!==_.IDLE&&t!==_.COMPLETED){i.warn(`Ignoring QUESTION event in invalid state: ${t}`);return}this._setQuestion(e.payload);break;case"ANSWER_START":if(t!==_.WAITING){i.warn(`Ignoring ANSWER_START event in invalid state: ${t}. Expected: WAITING`);return}this._startAnswer(e.payload);break;case"ANSWER_CHUNK":if(t!==_.STREAMING){i.warn(`Ignoring ANSWER_CHUNK event in invalid state: ${t}. Expected: STREAMING`);return}this._appendAnswerChunk(e.payload&&e.payload.chunk||""),e.payload&&e.payload.isComplete&&this._completeAnswer();break;case"ANSWER_COMPLETE":if(t!==_.STREAMING){i.warn(`Ignoring ANSWER_COMPLETE event in invalid state: ${t}. Expected: STREAMING`);return}this._completeAnswer();break;case"ERROR":this._setError(e.payload);break;default:i.warn(`Unknown event type: ${e.type}`);break}}reset(){this._stateMachine.reset(),this._currentQuestion=null,this._currentAnswer=null}onDispose(){this.reset(),i.info(`Conversation session disposed: ${this._sessionId}`)}}class Ve extends l{constructor(){super(...arguments),this._buffers=new Map}appendChunk(e,t){this.ensureNotDisposed();const n=(this._buffers.get(e)||"")+t;return this._buffers.set(e,n),i.debug(`Message chunk appended for question ${e}, length: ${n.length}`),n}getMessage(e){return this._buffers.get(e)||null}complete(e){const t=this.getMessage(e)||"";return this._buffers.delete(e),i.debug(`Message assembly completed for question ${e}, length: ${t.length}`),t}clear(e){this._buffers.delete(e)}onDispose(){this._buffers.clear(),i.info("Message Assembler disposed")}}class Ue{generateSessionId(){return`session_${Date.now()}_${Math.random().toString(36).substring(2,9)}`}generateQuestionId(){return Math.random().toString(36).substring(2,6)}}class Ke extends l{constructor(e,t){super(),this._transport=null,this._sessions=new Map,this._context=e,this._assembler=new Ve,this._idService=new Ue,i.info("Conversation Manager created")}setTransport(e){this._transport=e}async sendQuestion(e,t={speed:100,mood:0,vol:50}){this.ensureNotDisposed();const s=this._idService.generateQuestionId(),n={id:s,text:e,timestamp:Date.now(),inputMode:"text"},r=this._idService.generateSessionId(),a=new L(r,this._context);if(a.handleEvent({type:"QUESTION",payload:n}),this._sessions.set(s,a),this._transport)try{this._transport.sendText(s,e,200,t)}catch(c){throw i.error("Error sending question via transport:",c),a.handleEvent({type:"ERROR",payload:"Failed to send question"}),this._context.eventBus.emit("inner:sdk:error",{error:c instanceof Error?c:new Error(String(c))}),c}else i.warn("Transport not set, question not sent");return this._context.eventBus.emit("inner:conversation:question:sent",{questionId:s,text:e}),this._context.eventBus.emit("inner:conversation:answer:waiting",{questionId:s}),i.info(`Question sent: ${s}`),s}handleServerPong(){this.ensureNotDisposed(),this._transport&&this._transport.handlePongReceived()}willDisconnect(){this.ensureNotDisposed(),this._transport?.willDisconnect()}handleTextInput(e){this.sendQuestion(e).catch(t=>{i.error("Error sending question:",t)})}handleAnswerChunk(e,t,s){this.ensureNotDisposed();const n=this._sessions.get(e);if(!n){i.warn(`No active session for answer chunk: ${e}`);return}if(n.state===_.WAITING&&n.handleEvent({type:"ANSWER_START",payload:e}),s){n.handleEvent({type:"ANSWER_COMPLETE"});const a=this._assembler.complete(e);this._context.eventBus.emit("inner:conversation:answer:completed",{questionId:e,fullAnswer:a});try{n.dispose()}catch(c){i.error("Error disposing session:",c)}this._sessions.delete(e)}else n.handleEvent({type:"ANSWER_CHUNK",payload:{uid:e,chunk:t,isComplete:s}}),this._assembler.appendChunk(e,t),this._context.eventBus.emit("inner:conversation:answer:chunk",{questionId:e,chunk:t,isComplete:s})}handleServerInitiatedMessage(e,t){this.ensureNotDisposed(),this._context.eventBus.emit("inner:conversation:server:message",{questionId:e,message:t,type:"text"})}handleAsrTextReceived(e,t){this.ensureNotDisposed();const s=this._idService.generateSessionId(),n=new L(s,this._context);n.handleEvent({type:"QUESTION",payload:{id:e,text:t,timestamp:Date.now(),inputMode:"voice"}}),this._sessions.set(e,n),this._context.eventBus.emit("inner:conversation:asr:received",{questionId:e,text:t})}onDispose(){for(const e of this._sessions.values())try{e.dispose()}catch(t){i.error("Error disposing session:",t)}this._sessions.clear(),this._assembler.dispose(),i.info("Conversation Manager disposed")}}class We extends l{constructor(e,t){super(),this._manager=new Ke(e,t),i.info("Conversation Controller created")}get manager(){return this._manager}setTransport(e){this._manager.setTransport(e)}async sendQuestion(e,t){return this.ensureNotDisposed(),this._manager.sendQuestion(e,t)}onDispose(){this._manager.dispose(),i.info("Conversation Controller disposed")}}class Be extends l{constructor(){super(...arguments),this._tracks=new Map,this._participants=new Map}register(e,t,s){this.ensureNotDisposed(),this._tracks.set(e,t),this._participants.has(s)||this._participants.set(s,new Set),this._participants.get(s).add(e),t.on("ended",()=>{i.debug(`Track ended: ${e}`),this.unregister(e)}),i.debug(`Track registered: ${e} from participant ${s}`)}unregister(e){const t=this._tracks.get(e);if(t){t.stop(),this._tracks.delete(e);for(const[s,n]of this._participants.entries())if(n.has(e)){n.delete(e),n.size===0&&this._participants.delete(s);break}i.debug(`Track unregistered: ${e}`)}}getTrack(e){return this._tracks.get(e)||null}getParticipantTracks(e){const t=this._participants.get(e);return t?Array.from(t):[]}getAllTrackIds(){return Array.from(this._tracks.keys())}getVideoAvailability(){let e=!1,t=!1,s=!1;for(const n of this._tracks.values()){const r=n.mediaStreamTrack;if(r.kind==="video"&&(e=!0,r.readyState==="live"&&(t=!0),r.enabled&&(s=!0),r.readyState==="live"&&r.enabled))return{available:!0}}return e?t?s?{available:!1,reason:T.NO_VIDEO_TRACK}:{available:!1,reason:T.TRACK_MUTED}:{available:!1,reason:T.TRACK_ENDED}:{available:!1,reason:T.NO_VIDEO_TRACK}}clear(){for(const e of this._tracks.values())e.stop();this._tracks.clear(),this._participants.clear(),i.info("All tracks cleared")}onDispose(){this.clear()}}class P extends l{constructor(e,t){super(),this._videoController=null,this._room=null,this._context=e,this._trackRegistry=t,this._loosTrackTimer=null,i.info("Video Track Handler created")}setRoom(e){this._room=e}setVideoController(e){this._videoController=e}handleTrackSubscribed(e,t,s){this.ensureNotDisposed(),this._loosTrackTimer&&clearTimeout(this._loosTrackTimer),this._trackRegistry.register(t,e,s),this._videoController&&e.attach(this._videoController.getSource().getInternalElement()),e.on("videoPlaybackStarted",()=>{i.info("inner:rtc:video:available"),this._context.eventBus.emit("inner:rtc:video:available",void 0)}),this._context.eventBus.emit("inner:video:track:added",{trackId:t,participantId:s}),i.debug(`Video track subscribed: ${t}`)}handleTrackUnsubscribed(e,t,s){this.ensureNotDisposed(),this._videoController&&e.detach(this._videoController.getSource().getInternalElement()),e.removeAllListeners(),this._context.eventBus.emit("inner:video:track:removed",{trackId:t,participantId:s}),this._loosTrackTimer&&clearTimeout(this._loosTrackTimer),this._loosTrackTimer=setTimeout(()=>{this._context.eventBus.emit("inner:rtc:video:unavailable",void 0)},se),this._trackRegistry.unregister(t),i.debug(`Video track unsubscribed: ${t}`)}supports(e){return e.mediaStreamTrack.kind==="video"}onDispose(){this._loosTrackTimer&&(clearTimeout(this._loosTrackTimer),this._loosTrackTimer=null),i.info("Video Track Handler disposed")}}class F extends l{constructor(e,t){super(),this._audioController=null,this._room=null,this._context=e,this._trackRegistry=t,i.info("Audio Track Handler created")}setRoom(e){this._room=e}setAudioController(e){this._audioController=e}handleTrackSubscribed(e,t,s){this.ensureNotDisposed(),this._trackRegistry.register(t,e,s),this._audioController?e.attach(this._audioController.getOutputElement()):this._context.eventBus.emit("inner:audio:track:added",{trackId:t,participantId:s}),i.debug(`Audio track subscribed: ${t}`)}handleTrackUnsubscribed(e,t,s){this.ensureNotDisposed(),this._audioController?(this._audioController.removeOutputTrack(t),e.detach(this._audioController.getOutputElement())):this._context.eventBus.emit("inner:audio:track:removed",{trackId:t}),this._trackRegistry.unregister(t),i.debug(`Audio track unsubscribed: ${t}`)}supports(e){return e.mediaStreamTrack.kind==="audio"}onDispose(){i.info("Audio Track Handler disposed")}}class $e extends l{constructor(e,t){super(),this._room=null,this._context=e,this._trackRegistry=t,this._handlers=[new P(e,t),new F(e,t)],i.info("LiveKit Event Adapter created")}setRoom(e){this._room=e,this._handlers.forEach(t=>{t.setRoom(e)}),console.log("LiveKit room set:",this._room)}setVideoController(e){const t=this._handlers.find(s=>s instanceof P);t&&t.setVideoController(e)}setAudioController(e){const t=this._handlers.find(s=>s instanceof F);t&&t.setAudioController(e)}handleTrackSubscribed(e,t,s){this.ensureNotDisposed();const n=this._findHandler(e);n?n.handleTrackSubscribed(e,t,s):i.warn(`No handler found for track, trackId: ${t}`)}handleTrackUnsubscribed(e,t){this.ensureNotDisposed();const s=this._trackRegistry.getTrack(e);if(s){const n=this._findHandler(s);n?n.handleTrackUnsubscribed(s,e,t):(this._trackRegistry.unregister(e),i.warn(`No handler found for track, trackId: ${e}`))}}_findHandler(e){return this._handlers.find(t=>t.supports(e))||null}onDispose(){this._handlers.forEach(e=>{try{e.dispose()}catch(t){i.error("Error disposing track handler:",t)}}),this._handlers.length=0,i.info("LiveKit Event Adapter disposed")}}class He extends l{constructor(e){super(),this._room=null,this._context=e,this._trackRegistry=new Be,this._eventAdapter=new $e(e,this._trackRegistry),this._maxAttempts=e.options.reconnect?.maxAttempts||D,this._maxDelay=(e.options.reconnect?.delay??U)*A,i.info("LiveKit Service created")}async connect(e,t,s,n){if(this.ensureNotDisposed(),this._room&&this._room.isConnected()){i.warn("Already connected to LiveKit room");return}try{i.info(`Connecting to LiveKit room: ${s} at ${e}`);const r=new Q({reconnectPolicy:{nextRetryDelayInMs:a=>a.elapsedMs<this._maxDelay?Math.min(A*Math.pow(2,a.retryCount),this._maxDelay):null}});r.prepareConnection(e,t),r.on(C.TrackSubscribed,(a,c,h)=>{this._handleTrackSubscribed(a,c,h)}),r.on(C.TrackUnsubscribed,(a,c,h)=>{this._handleTrackUnsubscribed(a,c,h)}),r.on(C.TrackMuted,(a,c)=>{this._handleTrackMuted(a,c)}),r.on(C.TrackUnmuted,(a,c)=>{this._handleTrackUnmuted(a,c)}),r.on(C.Connected,()=>{i.info("Connected to LiveKit room"),this._context.eventBus.emit("inner:rtc:connected",void 0)}),r.on(C.Disconnected,a=>{a?i.info(`LiveKit room disconnected: ${a}`):i.info("LiveKit room disconnected"),a!==Y.CLIENT_INITIATED&&this._context.eventBus.emit("inner:rtc:disconnected",{reason:a?String(a):void 0}),this._room=null}),i.info("Connecting to LiveKit room..."),await r.connect(e,t,{autoSubscribe:!0,maxRetries:this._context.options.reconnect?.maxAttempts||D}),this._eventAdapter.setRoom(r),this._room=r}catch(r){throw u.fromError(r,d.LIVEKIT_CONNECT_FAILED)}}_handleTrackSubscribed(e,t,s){try{if(!e.mediaStreamTrack){i.warn("Track does not have MediaStreamTrack");return}e.kind==="audio"&&s.on(X.IsSpeakingChanged,c=>{console.log("\u8F68\u9053 \u8BF4\u8BDD\u72B6\u6001:",c)});const r=t.trackSid,a=s.identity;this._eventAdapter.handleTrackSubscribed(e,r,a)}catch(n){i.error("Error handling track subscribed:",n)}}_handleTrackUnsubscribed(e,t,s){try{const n=t,r=s,a=n.trackSid,c=r.identity;this._trackRegistry.getTrack(a)?.kind==="video"&&this._context.eventBus.emit("inner:rtc:video:unavailable",void 0),this._eventAdapter.handleTrackUnsubscribed(a,c)}catch(n){i.error("Error handling track unsubscribed:",n)}}_handleTrackMuted(e,t){try{e.kind==="video"&&this._context.eventBus.emit("inner:rtc:video:unavailable",void 0)}catch(s){i.error("Error handling track muted:",s)}}_handleTrackUnmuted(e,t){try{e.kind==="video"&&this._context.eventBus.emit("inner:rtc:video:available",void 0)}catch(s){i.error("Error handling track unmuted:",s)}}async disconnect(){if(this._room)try{await this._room.disconnect(),this._room=null,this._context.eventBus.emit("inner:rtc:disconnected",{reason:"Disconnected from LiveKit room"}),i.info("Disconnected from LiveKit room")}catch(e){i.error("Error disconnecting from LiveKit room:",e),this._room=null}}isConnected(){return this._room?this._room.state===J.Connected:!1}getTrackRegistry(){return this._trackRegistry}get trackRegistry(){return this._trackRegistry}getEventAdapter(){return this._eventAdapter}onDispose(){this.disconnect(),this._trackRegistry.dispose(),this._eventAdapter.dispose(),i.info("LiveKit Service disposed")}}class ze extends l{constructor(e){super(),this._conversationManager=null,this._context=e,i.info("WebSocket Dispatcher created")}setConversationManager(e){this._conversationManager=e,i.debug("Conversation manager set in dispatcher")}dispatch(e){this.ensureNotDisposed();try{if(e.type==="ws"){this._handleWSServerMessage(e);return}if(e.type==="text"){this._handleServerTextMessage(e);return}}catch(t){i.error("Error dispatching message:",t)}}_handleWSServerMessage(e){e.code===101?(this._conversationManager?.handleServerPong(),i.debug("Received pingpong response")):e.code===102&&(i.warn("Server will disconnect"),this._conversationManager?.willDisconnect())}_handleServerTextMessage(e){if(!this._conversationManager){i.warn("Conversation manager not set, text message ignored");return}const{uid:t,payload:s,code:n}=e;n===200?(this._conversationManager.handleAnswerChunk(t,s,!1),i.debug(`Answer received (code 401) for uid: ${t}`)):n===201?(this._conversationManager.handleAnswerChunk(t,"",!0),i.debug(`Answer completed (code 411) for uid: ${t}`)):n===300?(this._conversationManager.handleAsrTextReceived(t,s),i.debug(`ASR text chunk received (code 410) for uid: ${t}`)):n===400&&(this._conversationManager.handleServerInitiatedMessage(t,s),i.debug(`Server initiated message received (code 412) for uid: ${t}`))}onDispose(){i.info("WebSocket Dispatcher disposed")}}class qe extends l{constructor(e){super(),this._ws=null,this._dispatcher=null,this._isConnecting=!1,this._activeTimers=new Set,this._retryCount=0,this._maxRetries=D,this._pingTimer=void 0,this._pongTimer=void 0,this._isExplicitDisconnect=!1,this._context=e,this._maxRetries=e.options.reconnect?.maxAttempts??D,this._maxDelay=(e.options.reconnect?.delay??U)*A,this._dispatcher=new ze(e)}setConversationManager(e){this._dispatcher&&this._dispatcher.setConversationManager(e)}async connect(){if(this.ensureNotDisposed(),this._stopHeartbeat(),!(this._isConnecting||this.isConnected||this._ws?.readyState===WebSocket.OPEN)){this._isConnecting=!0,this._isExplicitDisconnect=!1;try{return this._ws=new WebSocket(this._context.wsUrl),new Promise((e,t)=>{this._ws.onopen=()=>{this._isConnecting=!1,this._retryCount=0,this.sendPing(),this._context.eventBus.emit("inner:ws:connected",void 0),i.info("Conversation transport connected"),e()},this._ws.onerror=s=>{this._isConnecting=!1,i.error("Conversation transport error:",s),this._context.eventBus.emit("inner:sdk:error",{error:new u("WebSocket connection error",d.WS_CONNECT_ERROR,s)}),t(new u("WebSocket connection error",d.WS_CONNECT_ERROR,s))},this._ws.onclose=s=>{const n=s.code,r=s.reason?s.reason:s.wasClean?"Clean close":"Closed by remote";this._handleDisconnect(n,r)},this._ws.onmessage=s=>{this._handleMessage(s.data)}})}catch(e){throw this._isConnecting=!1,this._context.emitError(e,d.WS_CONNECT_FAILED)}}}disconnect(){this._isExplicitDisconnect=!0,this._stopHeartbeat(),this._ws&&(this._ws.close(),this._ws=null),this._context.eventBus.emit("inner:ws:disconnected",{reason:"Client disconnect"}),i.info("Conversation transport disconnected")}_handleDisconnect(e,t){if(this._stopHeartbeat(),this._isConnecting=!1,e!==void 0&&new Set([1e3,1001,1002,1003,1007,1008]).has(e)){this._isExplicitDisconnect=!0;const n=`WebSocket closed (code ${e}): ${t??""}`;this._context.eventBus.emit("inner:ws:disconnected",{reason:n}),i.info(`Conversation transport closed with non-retry code ${e}: ${t??""}`);return}if(!this._isExplicitDisconnect&&this._retryCount<this._maxRetries){this._retryCount++;const n=Math.min(A*Math.pow(2,this._retryCount-1),this._maxDelay);i.warn(`Connection lost. Retry ${this._retryCount} in ${n}ms`);const r=setTimeout(()=>{!this._isExplicitDisconnect&&!this.isDisposed&&this.connect().catch(()=>{})},n);this._registerTimer(r)}else this._context.eventBus.emit("inner:ws:disconnected",{reason:"Closed"})}_registerTimer(e){return this._activeTimers.add(e),e}_clearTimer(e){e&&(clearTimeout(e),this._activeTimers.delete(e))}sendPing(){if(!this.isConnected)return;const e={type:"ws",code:101,payload:"ping"};this._ws?.send(JSON.stringify(e)),clearTimeout(this._pongTimer),this._pongTimer=setTimeout(()=>{i.error(`Pong timeout (${I/A}s). Reconnecting...`),this._ws?.close()},I)}handlePongReceived(){clearTimeout(this._pongTimer),this._clearTimer(this._pingTimer),this._pingTimer=setTimeout(()=>{this.sendPing()},ie)}willDisconnect(){if(this.ensureNotDisposed(),this._ws&&this._ws.readyState===WebSocket.OPEN){const t=JSON.stringify({type:"ws",code:102,payload:"will disconnect"});this._ws.send(t),this._isExplicitDisconnect=!0,this._ws.close(),this._ws=null,this._context.eventBus.emit("inner:ws:disconnected",{reason:"Client responsive to server's disconnect message."})}}sendText(e,t,s=200,n){if(this.ensureNotDisposed(),!this._ws||this._ws.readyState!==WebSocket.OPEN)throw new u("WebSocket is not connected",d.WS_NOT_CONNECTED);try{const a=JSON.stringify({type:"text",code:s,uid:e,metadata:n,payload:t});this._ws.send(a),i.debug("Text message sent via transport:",e)}catch(r){throw this._context.emitError(r,d.WS_TEXT_SEND_FAILED)}}_handleMessage(e){try{if(typeof e!="string")return;const t=JSON.parse(e);i.debug("Message received:",t.type),this._dispatcher?this._dispatcher.dispatch(t):i.warn("Dispatcher not set, message ignored")}catch(t){i.error("Error parsing message:",t),this._context.emitError(t,d.WS_PARSE_ERROR)}}sendBinary(e){if(this.ensureNotDisposed(),!this._ws||this._ws.readyState!==WebSocket.OPEN){i.debug("WebSocket not connected, binary data dropped");return}try{this._ws.send(e),i.debug("Binary data sent via transport")}catch(t){i.error("Error sending binary data:",t)}}get isConnected(){return this._ws?.readyState===WebSocket.OPEN}_stopHeartbeat(){this._clearTimer(this._pingTimer),this._clearTimer(this._pongTimer)}onDispose(){this._stopHeartbeat(),this.disconnect(),this._dispatcher&&(this._dispatcher.dispose(),this._dispatcher=null),i.info("Conversation transport disposed")}}const w=["sdk:connected","sdk:disconnected","sdk:error","media:video:available","media:video:unavailable","media:video:trackAdded","media:video:trackRemoved","media:audio:trackAdded","media:audio:trackRemoved","media:audio:captureStarted","media:audio:captureStopped","media:audio:volumeChanged","media:audio:muted","media:audio:unmuted","conversation:question:sent","conversation:answer:waiting","conversation:server:message","conversation:asr:received","conversation:answer:chunk","conversation:answer:completed"],M=/^[a-z]+:[a-zA-z]+(:[a-zA-Z]+)?$/;class Ge{constructor(e){this.inner=e,this.listeners=new Map,this.forwardedVideoTrackIds=new Set,this.wsConnected=!1,this.livekitConnected=!1,this.httpConnected=!1,this.internalMap={"inner:sdk:error":{public:"sdk:error",sanitizer:t=>({message:t?.error?.message,code:t?.error?.code})},"inner:rtc:video:available":{public:"media:video:available"},"inner:rtc:video:unavailable":{public:"media:video:unavailable"},"inner:video:track:added":{public:"media:video:trackAdded"},"inner:video:track:removed":{public:"media:video:trackRemoved"},"inner:audio:track:added":{public:"media:audio:trackAdded"},"inner:audio:track:removed":{public:"media:audio:trackRemoved"},"inner:audio:input:started":{public:"media:audio:captureStarted"},"inner:audio:input:stopped":{public:"media:audio:captureStopped"},"inner:audio:volume:changed":{public:"media:audio:volumeChanged",sanitizer:t=>({volume:t?.volume})},"inner:audio:muted":{public:"media:audio:muted"},"inner:audio:unmuted":{public:"media:audio:unmuted"},"inner:conversation:question:sent":{public:"conversation:question:sent",sanitizer:t=>({questionId:t?.questionId,text:String(t?.text||"")})},"inner:conversation:answer:waiting":{public:"conversation:answer:waiting",sanitizer:t=>({questionId:t?.questionId})},"inner:conversation:server:message":{public:"conversation:server:message",sanitizer:t=>({questionId:t?.questionId,message:String(t?.message||""),type:String(t?.type||"")})},"inner:conversation:asr:received":{public:"conversation:asr:received",sanitizer:t=>({questionId:t?.questionId,text:String(t?.text||"")})},"inner:conversation:answer:chunk":{public:"conversation:answer:chunk",sanitizer:t=>({questionId:t?.questionId,chunk:String(t?.chunk||"")})},"inner:conversation:answer:completed":{public:"conversation:answer:completed",sanitizer:t=>({questionId:t?.questionId,fullAnswer:String(t?.fullAnswer||"")})}},Object.keys(this.internalMap).forEach(t=>{this.inner.on(t,s=>this.handleInternalEvent(t,s))}),this.inner.on("inner:sdk:connected",t=>this.handleConnected(t,!0)),this.inner.on("inner:sdk:disconnected",t=>this.handleConnected(t,!1)),w.forEach(t=>{this.inner.on(t,s=>this.handlePublicEvent(t,s))})}publicAPI(){return{on:this.on.bind(this),off:this.off.bind(this),once:this.once.bind(this)}}on(e,t){const s=String(e);return this.assertAllowed(s),this.listeners.has(s)||this.listeners.set(s,new Set),this.listeners.get(s).add(t),()=>this.off(e,t)}once(e,t){const s=String(e);this.assertAllowed(s);const n=r=>{try{t(r)}finally{this.off(e,n)}};return this.on(e,n)}off(e,t){const s=String(e);this.assertAllowed(s);const n=this.listeners.get(s);n&&(n.delete(t),n.size===0&&this.listeners.delete(s))}handleInternalEvent(e,t){const s=this.internalMap[e];if(!s)return;const n=s.public;if(w.indexOf(n)===-1)return;if(n==="media:video:trackAdded"){const c=t?.trackId||t?.id;if(typeof c=="string"){if(this.forwardedVideoTrackIds.has(c))return;this.forwardedVideoTrackIds.add(c)}}const r=s.sanitizer?s.sanitizer(t):t;if(!M.test(n))return;const a=this.listeners.get(n);!a||a.size===0||a.forEach(c=>{try{c(r)}catch(h){console.error("PublicEventEmitter listener error",h)}})}handlePublicEvent(e,t){if(w.indexOf(e)===-1)return;const s=Object.values(this.internalMap).find(a=>a.public===e),n=s&&s.sanitizer?s.sanitizer(t):t;if(e==="media:video:trackAdded"){const a=t?.trackId||t?.id;if(typeof a=="string"){if(this.forwardedVideoTrackIds.has(a))return;this.forwardedVideoTrackIds.add(a)}}const r=this.listeners.get(e);!r||r.size===0||r.forEach(a=>{try{a(n)}catch(c){console.error("PublicEventEmitter listener error",c)}})}handleConnected(e,t){e.source==="livekit"?this.livekitConnected=t:e.source==="ws"?this.wsConnected=t:e.source==="http"&&(this.httpConnected=t),this.maybeEmitSdkConnected(t?"connected":"disconnected")}maybeEmitSdkConnected(e){const t=e==="connected",s=t?!!this.livekitConnected:!this.livekitConnected,n=t?!!this.wsConnected:!this.wsConnected,r=t?!!this.httpConnected:!this.httpConnected,a={livekit:s,ws:n,http:r,all:s&&n&&r},c=JSON.stringify(a);this.lastConnectedPayloadJson!==c&&(this.lastConnectedPayloadJson=c,this.inner.emit(`sdk:${e}`,a))}assertAllowed(e){if(w.indexOf(e)===-1)throw new Error(`Event "${e}" is not allowed for public consumers`);if(!M.test(e))throw new Error(`Event name "${e}" does not follow namespace:domain[:action] naming`)}}class je extends l{constructor(e,t,s){super(),this._requestCounter=0,this._context=e,this._baseURL=t||"",this._defaultHeaders=s||{},i.info(`HTTP Service created with baseURL: ${this._baseURL||"none"}`)}setAuthToken(e){this._defaultHeaders.Authorization=`Bearer ${e}`}request(e){this.ensureNotDisposed();const t=e.requestId||`req_${Date.now()}_${++this._requestCounter}`,s=this._buildURL(e.url,e.params),n=this._mergeHeaders(e.headers),r={method:e.method||"GET",headers:n};e.data!==void 0&&(e.method||"GET")!=="GET"&&(typeof e.data=="string"?r.body=e.data:(r.body=JSON.stringify(e.data),!n["Content-Type"]&&!n["content-type"]&&(n["Content-Type"]="application/json")));const a=e.timeout||3e4,c=new AbortController;return r.signal=c.signal,new Promise((h,m)=>{const g=setTimeout(()=>c.abort(),a);fetch(s,r).then(async p=>{clearTimeout(g);const v={};p.headers.forEach((G,j)=>{v[j]=G});let S;const N=p.headers.get("content-type");N&&N.includes("application/json")?S=await p.json():S=await p.text();const q={requestId:t,status:p.status,headers:v,data:S,config:e};i.debug(`HTTP request succeeded: ${e.method} ${s} [${t}]`),h(q)}).catch(p=>{clearTimeout(g);const v=p&&p.name==="AbortError",S={requestId:t,status:p&&p.status||void 0,message:v?"Request aborted (timeout)":p&&p.message||"HTTP request failed",error:p,config:e};i.error(`HTTP request failed: ${e.method} ${s} [${t}]`,p),m(S)})})}get(e,t,s){return this.request({...s,url:e,method:"GET",params:t})}post(e,t,s){return this.request({...s,url:e,method:"POST",data:t})}put(e,t,s){return this.request({...s,url:e,method:"PUT",data:t})}delete(e,t){return this.request({...t,url:e,method:"DELETE"})}patch(e,t,s){return this.request({...s,url:e,method:"PATCH",data:t})}_buildURL(e,t){if(e.startsWith("http://")||e.startsWith("https://"))return this._appendParams(e,t);let s=this._baseURL;return s&&!s.endsWith("/")&&!e.startsWith("/")?s+="/":s&&s.endsWith("/")&&e.startsWith("/")&&(s=s.slice(0,-1)),s+=e,this._appendParams(s,t)}_appendParams(e,t){if(!t||Object.keys(t).length===0)return e;const s=new URLSearchParams;for(const[r,a]of Object.entries(t))s.append(r,String(a));const n=e.includes("?")?"&":"?";return`${e}${n}${s.toString()}`}_mergeHeaders(e){const t={...this._defaultHeaders};return e&&Object.assign(t,e),t}onDispose(){i.info("HTTP Service disposed")}}function Qe(o,e="https"){return o.startsWith(e)?o:`${e}://${o}`}class Ye extends l{constructor(e,t){super(),this._context=e,this._httpService=new je(e,t||ne)}getAuthToken(){return this.ensureNotDisposed(),this._httpService?.request({url:"/auth/login",method:"GET",data:{}}).then(e=>{e.data.code===R&&this._httpService?.setAuthToken(e.data.data.token)}).catch(e=>{throw this._context.emitError(e,d.SDK_AUTH_TOKEN_FAILED)})}getLivekitConfig(){return this.ensureNotDisposed(),this._httpService?.request({url:"/stream/v1/start",method:"POST",data:{avatarName:this._context.options.avatarName,voice:this._context.options.avatarVoice}}).then(e=>{if(e.data.code===R){const t=e.data.data;return this._context.setLivekitConfig(Qe(t.livekitUrl,"wss"),t.roomToken,t.roomId),this._context.setWsUrl(`${this._context.options.wsUrlPrefix||oe}/${t.roomId}/${this._context.options.avatarName}`),this._context.setVideoOptions({renderMode:t.greenScreen.enabled?"processed":"raw",greenScreen:t.greenScreen||{}}),this._context.eventBus.emit("inner:sdk:connected",{source:"http"}),!0}else throw new u(e.data.msg,d.SDK_GET_LIVEKIT_CONFIG_FAILED)}).catch(e=>{throw this._context.eventBus.emit("inner:sdk:error",{error:new u(e.message,d.SDK_GET_LIVEKIT_CONFIG_FAILED)}),u.fromError(e,d.SDK_GET_LIVEKIT_CONFIG_FAILED)})}switchVideo({onceActions:e,loopActions:t}){return this.ensureNotDisposed(),this._httpService?.request({url:"/avatar/v1/switch-video",method:"POST",data:{roomId:this._context.roomId,onceActions:e,loopActions:t}}).then(s=>{if(s.data.code===R)return!0;throw this._context.emitError(s.data.message,d.SDK_SWITCH_VIDEO_FAILED)})}interruptConversation(){return this.ensureNotDisposed(),this._httpService?.request({url:"/stream/v1/interrupt",method:"POST",data:{roomId:this._context.roomId}}).then(e=>{if(e.data.code===R)return!0;throw this._context.emitError(e.data.message,d.SDK_INTERRUPT_CONVERSATION_FAILED)}).finally(()=>{this._context.eventBus.emit("inner:sdk:disconnected",{source:"http"})})}disconnectConversation(){return this.ensureNotDisposed(),this._httpService?.request({url:"/stream/v1/stop",method:"POST",data:{roomId:this._context.roomId}}).then(e=>e.data.code===R).finally(()=>{this._context.eventBus.emit("inner:sdk:disconnected",{source:"http"})})}onDispose(){this._httpService.dispose()}}class Xe extends l{constructor(e){super(),this._videoController=null,this._audioController=null,this._conversationController=null,this._liveKitService=null,this._conversationTransport=null,this._connectionCoordinator=null,this._httpController=null,this._preConnectPromise=null,this._lastPreConnectSuccessAt=null,this._context=new ce(e),i.info("SDK Client created")}get events(){return this.ensureNotDisposed(),this._publicEventAPI||(this._publicEmitter=new Ge(this._context.eventBus),this._publicEventAPI=this._publicEmitter.publicAPI()),this._publicEventAPI}pushActions({onceActions:e,loopActions:t}){return this.ensureNotDisposed(),this._ensureConnected(),this._httpController?.switchVideo({onceActions:e,loopActions:t})}interruptConnection(){return this.ensureNotDisposed(),this._ensureConnected(),this._httpController?.interruptConversation()}sendTextQuestion(e){if(this.ensureNotDisposed(),this._ensureConnected(),this._conversationController)return this._conversationController.sendQuestion(e);throw new u("Conversation controller is not available",d.CONVERSATION_CONTROLLER_NOT_AVAILABLE)}setRenderFitMode(e){this.ensureNotDisposed(),this._ensureConnected(),this._videoController&&this._videoController.setRenderFitMode(e)}async startAudioCapture(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",d.AUDIO_CONTROLLER_NOT_AVAILABLE);await this._audioController.startCapture()}async stopAudioCapture(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",d.AUDIO_CONTROLLER_NOT_AVAILABLE);await this._audioController.stopCapture()}setVolume(e){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",d.AUDIO_CONTROLLER_NOT_AVAILABLE);this._audioController.setVolume(e)}getVolume(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",d.AUDIO_CONTROLLER_NOT_AVAILABLE);return this._audioController?.getVolume()||1}mute(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",d.AUDIO_CONTROLLER_NOT_AVAILABLE);this._audioController.mute()}unmute(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",d.AUDIO_CONTROLLER_NOT_AVAILABLE);this._audioController.unmute()}get isMuted(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",d.AUDIO_CONTROLLER_NOT_AVAILABLE);return this._audioController.isMuted()}get isAudioCapturing(){return this.ensureNotDisposed(),this._ensureConnected(),this._audioController?this._audioController.isCapturing:!1}async preConnect(){if(this.ensureNotDisposed(),this._preConnectPromise)return this._preConnectPromise;const e=Date.now();return this._lastPreConnectSuccessAt!==null&&e-this._lastPreConnectSuccessAt<ae?Promise.resolve(!0):(this._preConnectPromise=(async()=>{try{if(i.info("Creating HTTP controller..."),this._httpController=this._httpController??new Ye(this._context,this._context.options.http?.baseURL),i.info("Getting auth token..."),await this._httpController.getAuthToken(),i.info("Getting livekit config..."),!!!await this._httpController?.getLivekitConfig())throw new Error("Config missing");return this._lastPreConnectSuccessAt=Date.now(),!0}catch(t){throw this._context.emitError(t,d.SDK_PRECONNECT_FAILED)}finally{this._preConnectPromise=null}})(),this._preConnectPromise)}async connect(){if(this.ensureNotDisposed(),this._connectionCoordinator&&this._context.sessionState.isConnected){i.warn("SDK already initialized");return}try{if(!await this.preConnect())throw new u("PreConnect failed without explicit error",d.SDK_CONNECT_FAILED)}catch(e){throw this._context.emitError(e,d.SDK_CONNECT_FAILED)}try{if(i.info("Initializing SDK..."),this._connectionCoordinator=new he(this._context),this._connectionCoordinator.start(),this._connectionCoordinator.startConnecting(),this._videoController=new fe(this._context,this._context.options.video),this._audioController=new Fe(this._context,this._context.options.audio),this._conversationController=new We(this._context),this._liveKitService=new He(this._context),this._conversationTransport=new qe(this._context),this._liveKitService&&this._videoController&&this._audioController){const e=this._liveKitService.getEventAdapter();e&&(typeof e.setVideoController=="function"&&e.setVideoController(this._videoController),typeof e.setAudioController=="function"&&e.setAudioController(this._audioController))}this._audioController&&this._conversationTransport&&this._audioController.setWebSocketSender({sendBinary:e=>{this._conversationTransport.sendBinary(e)},isConnected:()=>this._conversationTransport.isConnected}),this._conversationTransport&&this._conversationController&&(this._conversationTransport.setConversationManager(this._conversationController.manager),this._conversationController.setTransport(this._conversationTransport)),await this._liveKitService.connect(this._context.livekitUrl,this._context.token,this._context.options.avatarName),this._conversationTransport&&await this._conversationTransport.connect(),i.info("SDK initialized and connected")}catch(e){throw this._connectionCoordinator&&this._connectionCoordinator.transitionToIdle(),await this._cleanup(),this._context.emitError(e,d.SDK_INITIALIZATION_FAILED)}}async disconnect(){if(this.ensureNotDisposed(),!(!this._connectionCoordinator||this._connectionCoordinator.getState()==="idle"||this._connectionCoordinator.getState()==="disposed")){i.info("Disconnecting SDK..."),this._connectionCoordinator.startDisconnecting();try{this._conversationTransport&&this._conversationTransport.disconnect(),this._liveKitService&&await this._liveKitService.disconnect(),this._audioController&&this._audioController.stopCapture(),this._httpController&&await this._httpController.disconnectConversation(),this._preConnectPromise=null,this._connectionCoordinator.completeDisconnecting(),i.info("SDK disconnected")}catch(e){throw i.error("Error during disconnect:",e),this._context.emitError(e,d.SDK_DISCONNECT_FAILED)}}}async reconnect(){this.ensureNotDisposed();const e=this._connectionCoordinator;if(!e?.canStartReconnectingManual()){const s=e?e.getSnapshot():this.connectionSnapshot;return i.warn("SDK reconnect not allowed in current state, returning current snapshot"),s}const t=e.captureSnapshotAndStartReconnectingManual();try{if(this._audioController&&this._audioController.stopCapture(),t.rtc.connected&&!t.rtc.hasVideoTrack&&(await this._liveKitService?.disconnect(),await this._conversationTransport?.disconnect()),this._conversationTransport&&!this._conversationTransport.isConnected){if(!await this.preConnect())throw new u("PreConnect failed without explicit error",d.SDK_CONNECT_FAILED);await this._conversationTransport.connect()}return this._liveKitService&&!this._liveKitService.isConnected()&&await this._liveKitService.connect(this._context.livekitUrl,this._context.token,this._context.options.avatarName),t}catch(s){throw i.error("Error during reconnect:",s),this._context.emitError(s,d.SDK_RECONNECT_FAILED)}}get connectionSnapshot(){return this.ensureNotDisposed(),this._connectionCoordinator?this._connectionCoordinator.getSnapshot():W(K("idle"))}async _cleanup(){if(this._conversationTransport&&this._conversationTransport.disconnect(),this._liveKitService)try{await this._liveKitService.disconnect()}catch(e){i.error("Error disconnecting LiveKit:",e)}this._audioController&&this._audioController.stopCapture()}get isConnected(){return this.ensureNotDisposed(),this._context.sessionState.isConnected}_ensureConnected(){if(!this._context.sessionState.isConnected)throw new u("SDK is not connected. Call connect() first.",d.SDK_NOT_CONNECTED)}onDispose(){this.disconnect().catch(e=>{i.error("Error during disconnect in dispose:",e)}),this._videoController&&(this._videoController.dispose(),this._videoController=null),this._audioController&&(this._audioController.dispose(),this._audioController=null),this._conversationController&&(this._conversationController.dispose(),this._conversationController=null),this._liveKitService&&(this._liveKitService.dispose(),this._liveKitService=null),this._conversationTransport&&(this._conversationTransport.dispose(),this._conversationTransport=null),this._httpController&&(this._httpController.dispose(),this._httpController=null),this._connectionCoordinator&&(this._connectionCoordinator.transitionToDisposed(),this._connectionCoordinator.dispose(),this._connectionCoordinator=null),this._context.dispose(),i.info("SDK Client disposed")}}function it(o){if(!o.avatarName)throw new u("avatarName is required",d.NO_AVATARNAME);return i.info("Creating SDK client with options:",{avatarName:o.avatarName}),new Xe(o)}class V extends l{constructor(e,t){super(),this._videoElement=null,this._videoUrl=null,this._isPlaying=!1,this._frameCallbackId=null,this._videoUrl=e,t?this._videoElement=t:(this._videoElement=document.createElement("video"),this._videoElement.style.opacity="0",this._videoElement.muted=!0,this._videoElement.playsInline=!0,this._videoElement.loop=!0,document.querySelector("#video-container")?.appendChild(this._videoElement)),i.info("Local Video Debug Source created")}get videoElement(){return this._videoElement}async load(){if(!this._videoElement)throw new Error("Video element or URL not set");return new Promise((e,t)=>{const s=this._videoElement,n=()=>{s.removeEventListener("loadedmetadata",n),s.removeEventListener("error",r),i.info(`Video loaded: ${s.videoWidth}x${s.videoHeight}`),e()},r=a=>{s.removeEventListener("loadedmetadata",n),s.removeEventListener("error",r),t(new Error(`Failed to load video: ${a}`))};s.addEventListener("loadedmetadata",n),s.addEventListener("error",r),s.src=this._videoUrl||"",s.load()})}async play(){if(!this._videoElement)throw new Error("Video element not set");if(!this._isPlaying)try{await this._videoElement.play(),this._videoElement.style.opacity="0",this._videoElement.setAttribute("loop","true"),this._isPlaying=!0,i.info("Video playback started")}catch(e){throw i.error("Failed to play video:",e),e}}pause(){this._videoElement&&this._isPlaying&&(this._videoElement.pause(),this._isPlaying=!1,i.info("Video playback paused"))}stop(){this._videoElement&&(this._videoElement.pause(),this._videoElement.currentTime=0,this._isPlaying=!1,i.info("Video playback stopped"))}get isPlaying(){return this._isPlaying&&this._videoElement!==null&&!this._videoElement.paused}get videoWidth(){return this._videoElement?.videoWidth||0}get videoHeight(){return this._videoElement?.videoHeight||0}async captureFrame(){if(!this._videoElement||this._videoElement.readyState<2)return null;try{if("VideoFrame"in window&&typeof window.VideoFrame=="function")try{return new VideoFrame(this._videoElement,{timestamp:this._videoElement.currentTime*1e6})}catch(s){i.error("",s)}const e=await createImageBitmap(this._videoElement),t=new VideoFrame(e,{timestamp:this._videoElement.currentTime*1e6});return e.close(),t}catch(e){return i.error("Failed to capture video frame:",e),null}}onFrame(e){if(!this._videoElement)return;this.offFrame();const t=this._videoElement;if("requestVideoFrameCallback"in t){const s=(n,r)=>{!this._isPlaying||!this._videoElement||this.captureFrame().then(a=>{a&&(e(a),a.close()),this._isPlaying&&this._videoElement&&(this._frameCallbackId=t.requestVideoFrameCallback(s))}).catch(a=>{i.error("Error in frame callback:",a)})};this._frameCallbackId=t.requestVideoFrameCallback(s)}else{const s=()=>{!this._isPlaying||!this._videoElement||this.captureFrame().then(n=>{n&&(e(n),n.close()),this._isPlaying&&this._videoElement&&(this._frameCallbackId=requestAnimationFrame(s))}).catch(n=>{i.error("Error in frame callback:",n)})};this._frameCallbackId=requestAnimationFrame(s)}}offFrame(){this._frameCallbackId!==null&&(this._videoElement&&"cancelVideoFrameCallback"in this._videoElement?this._videoElement.cancelVideoFrameCallback(this._frameCallbackId):cancelAnimationFrame(this._frameCallbackId),this._frameCallbackId=null)}onDispose(){this.stop(),this.offFrame(),this._videoElement&&this._videoElement.parentElement===document.body&&document.body.removeChild(this._videoElement),this._videoElement=null,this._videoUrl=null,i.info("Local Video Debug Source disposed")}}class Je extends l{constructor(e,t,s){super(),this._outputCanvas=null,this._outputRenderer=null,this._source=new $;const n={...e,renderMode:"processed"};this._pipeline=new z(this._source,n),this._outputRenderer=new b(s,t),this._pipeline.setRenderer(this._outputRenderer),this._outputCanvas=this._outputRenderer.getCanvas(),i.info("Video Pipeline Runner created")}get outputCanvas(){return this._outputCanvas}get processor(){return this._pipeline.strategy.getProcessors()[0]||null}setProcessor(e){this._pipeline.setProcessor(e),i.info("Processor set on pipeline")}async processFrame(e){if(this.ensureNotDisposed(),!this._outputRenderer){i.error("Output renderer not available"),e.close();return}try{const t=this._pipeline.strategy.getProcessors()[0]||null;let s=null;if(t){try{s=t.process(e)}catch(n){i.error("Error processing frame:",n),s=e}if(s===null&&"getImageData"in t){const n=t.getImageData();if(n&&this._outputCanvas&&this._outputRenderer.getContext()){const r=this._outputRenderer.getContext();r&&((this._outputCanvas.width!==n.width||this._outputCanvas.height!==n.height)&&(this._outputCanvas.width=n.width,this._outputCanvas.height=n.height),r.putImageData(n,0,0))}e.close();return}s&&s!==e?(this._outputRenderer.render(s),e.close()):s===e?this._outputRenderer.render(e):e.close()}else this._outputRenderer.render(e)}catch(t){i.error("Error in processFrame:",t),e.close()}}setOutputSize(e,t){this._outputCanvas&&(this._outputCanvas.width=e,this._outputCanvas.height=t,i.info(`Output canvas size set to ${e}x${t}`))}onDispose(){this._pipeline.dispose(),this._outputRenderer&&(this._outputRenderer.dispose(),this._outputRenderer=null),this._outputCanvas&&this._outputCanvas.parentElement===document.body&&document.body.removeChild(this._outputCanvas),this._outputCanvas=null,i.info("Video Pipeline Runner disposed")}}class nt extends l{constructor(e,t,s,n){if(super(),this._isRunning=!1,this._videoSource=new V(e,t),this._options={renderMode:"processed",containerElement:t,...n},this._pipelineRunner=new Je(this._options,t,s),this._options.greenScreen?.enabled){const r=new y(this._options.greenScreen);this._pipelineRunner.setProcessor(r),i.info("Green screen processor enabled in debugger")}i.info("Standalone Video Debugger created")}async switchSource(e,t){this.ensureNotDisposed();const s=this._videoSource,n=this._isRunning,r=new V(e,t);try{await r.load(),n&&await r.play(),r.onFrame(h=>{this._pipelineRunner.processFrame(h).catch(m=>{i.error("Error processing frame:",m)})}),this._videoSource=r;try{s.offFrame(),s.stop(),s.dispose()}catch(h){console.warn("Error disposing old video source during switch:",h)}const a=this._videoSource.videoWidth,c=this._videoSource.videoHeight;a>0&&c>0&&this._pipelineRunner.setOutputSize(a,c)}catch(a){i.error("Failed to switch video source:",a);try{r.dispose()}catch(c){console.warn("Error disposing new video source after failed switch:",c)}throw a}}get outputCanvas(){return this._pipelineRunner.outputCanvas}get videoSource(){return this._videoSource}get processor(){return this._pipelineRunner.processor}setProcessor(e){this._pipelineRunner.setProcessor(e)}updateGreenScreenOptions(e){const t=this._pipelineRunner.processor;if(t&&"updateOptions"in t&&typeof t.updateOptions=="function")t.updateOptions(e),i.info("Green screen options updated");else{const s=this._options.greenScreen||{enabled:!1},n={...s,...e,enabled:e.enabled!==void 0?e.enabled:s.enabled},r=new y(n);this._pipelineRunner.setProcessor(r),this._options.greenScreen=n,i.info("Green screen processor recreated with new options")}}async initialize(){this.ensureNotDisposed();try{await this._videoSource.load();const e=this._videoSource.videoWidth,t=this._videoSource.videoHeight;e>0&&t>0&&this._pipelineRunner.setOutputSize(e,t),i.info("Video debugger initialized")}catch(e){throw i.error("Failed to initialize video debugger:",e),e}}async start(){if(this.ensureNotDisposed(),this._isRunning){i.warn("Debugger is already running");return}try{this._videoSource.videoWidth===0&&await this.initialize(),await this._videoSource.play(),this._videoSource.onFrame(e=>{this._pipelineRunner.processFrame(e).catch(t=>{i.error("Error processing frame:",t)})}),this._isRunning=!0,i.info("Video debugger started")}catch(e){throw i.error("Failed to start video debugger:",e),e}}stop(){this._isRunning&&(this._videoSource.stop(),this._videoSource.offFrame(),this._isRunning=!1,i.info("Video debugger stopped"))}pause(){this._isRunning&&(this._videoSource.pause(),i.info("Video debugger paused"))}async resume(){this._isRunning&&(await this._videoSource.play(),i.info("Video debugger resumed"))}get isRunning(){return this._isRunning}onDispose(){this.stop(),this._videoSource.dispose(),this._pipelineRunner.dispose(),i.info("Standalone Video Debugger disposed")}}export{_ as ConversationStateType,st as DEFAULT_AUDIO_BUFFER_SIZE,tt as DEFAULT_AUDIO_CHANNELS,k as DEFAULT_AUDIO_SAMPLE_RATE,D as DEFAULT_RECONNECT_ATTEMPTS,U as DEFAULT_RECONNECT_DELAY,et as DEFAULT_WS_TIMEOUT,l as Disposable,Z as EventBus,E as LogLevel,i as Logger,Xe as SDKClient,u as SDKError,te as SessionState,f as SessionStatus,nt as StandaloneVideoDebugger,it as createClient};
|
|
110
|
+
`;function xe(){const o=new Blob([Oe],{type:"text/javascript"});return URL.createObjectURL(o)}class Le extends ke{constructor(e){super(),this.options=e,this._stream=null,this._audioContext=null,this._sourceNode=null,this._workletNode=null,this._targetSampleRate=k,this._bitDepth=O,this._channels=1;const t=e?.input;this._targetSampleRate=t?.sampleRate||k,this._bitDepth=t?.bitDepth||O,this._channels=t?.channels||1}async start(){this.ensureNotDisposed();try{const t={audio:this.options?.input?.constraints||{channelCount:this._channels}};this._stream=await navigator.mediaDevices.getUserMedia(t);const r=this._stream.getAudioTracks()[0].getSettings().sampleRate||44100;this._audioContext=new AudioContext({sampleRate:r});const a=xe();await this._audioContext.audioWorklet.addModule(a),this._sourceNode=this._audioContext.createMediaStreamSource(this._stream),this._workletNode=new AudioWorkletNode(this._audioContext,"audio-resampler",{processorOptions:{targetSampleRate:this._targetSampleRate,bitDepth:this._bitDepth,channels:this._channels},numberOfInputs:1,numberOfOutputs:0}),this._workletNode.port.onmessage=c=>{const{type:h,data:m,sampleRate:g}=c.data;h==="audioData"&&m&&this.emitAudioData(m,g)},this._sourceNode.connect(this._workletNode),i.info(`Audio capture started: input=${r}Hz -> output=${this._targetSampleRate}Hz, ${this._bitDepth}bit`)}catch(e){throw u.fromError(e,d.AUDIO_CAPTURE_FAILED)}}stop(){this._stream&&(this._stream.getTracks().forEach(e=>e.stop()),this._stream=null),this._workletNode&&(this._workletNode.port.onmessage=null,this._workletNode.disconnect(),this._workletNode=null),this._sourceNode&&(this._sourceNode.disconnect(),this._sourceNode=null),this._audioContext&&(this._audioContext.close(),this._audioContext=null),i.info("Audio capture stopped")}onDispose(){this.stop()}}class Pe{create(e){return new Le(e)}}class Fe extends l{constructor(e,t,s){super(),this._source=null,this._isCapturing=!1,this._currentOutputTrackId=null,this._context=e,this._inputPipeline=new ve(t),this._outputPipeline=new Ee,this._frameSender=new Ie(e,t),this._sourceFactory=s||new Pe,this._inputPipeline.onProcessedAudio=(n,r)=>{this._context.eventBus.emit("inner:audio:frame:received",{data:n,sampleRate:r})},i.info("Audio Controller created")}setWebSocketSender(e){this._frameSender.setWebSocketSender(e)}async startCapture(){if(this.ensureNotDisposed(),this._isCapturing){i.warn("Audio capture already started");return}try{this._source||(this._source=this._sourceFactory.create(this._context.options.audio),this._source.onAudioData=(e,t)=>{this._inputPipeline.process(e,t)}),await this._source.start(),this._isCapturing=!0,this._frameSender.start(),this._context.eventBus.emit("inner:audio:input:started",void 0),i.info("Audio capture started")}catch(e){throw this._context.emitError(e,d.AUDIO_CAPTURE_START_FAILED)}}stopCapture(){this.ensureNotDisposed(),this._isCapturing&&(this._source&&this._source.stop(),this._frameSender.stop(),this._isCapturing=!1,this._context.eventBus.emit("inner:audio:input:stopped",void 0),i.info("Audio capture stopped"))}get isCapturing(){return this._isCapturing}setOutputTrack(e,t,s){this.ensureNotDisposed(),this._outputPipeline.setTrack(e),this._currentOutputTrackId=t,this._context.eventBus.emit("inner:audio:track:added",{trackId:t,participantId:s}),i.info("Audio output track ready")}removeOutputTrack(e){this.ensureNotDisposed(),this._currentOutputTrackId===e&&(this._outputPipeline.removeTrack(),this._currentOutputTrackId=null,this._context.eventBus.emit("inner:audio:track:removed",{trackId:e}),i.info("Audio output track removed"))}setVolume(e){this.ensureNotDisposed(),this._outputPipeline.setVolume(e),this._context.eventBus.emit("inner:audio:volume:changed",{volume:e}),i.debug(`Audio volume set to: ${e}`)}getVolume(){return this._outputPipeline.getVolume()}mute(){this.ensureNotDisposed(),this._outputPipeline.mute(),this._context.eventBus.emit("inner:audio:muted",void 0),i.debug("Audio muted")}unmute(){this.ensureNotDisposed(),this._outputPipeline.unmute(),this._context.eventBus.emit("inner:audio:unmuted",void 0),i.debug("Audio unmuted")}getOutputElement(){return this._outputPipeline.getAudioElement()}isMuted(){return this._outputPipeline.isMuted()}onDispose(){this.stopCapture(),this._source&&(this._source.dispose(),this._source=null),this._inputPipeline.dispose(),this._outputPipeline.dispose(),this._frameSender.dispose(),i.info("Audio Controller disposed")}}var _;(function(o){o.IDLE="idle",o.WAITING="waiting",o.STREAMING="streaming",o.COMPLETED="completed",o.ERROR="error"})(_||(_={}));class Me extends B{constructor(){super(...arguments),this._currentState=_.IDLE,this.transitionTable=[{from:_.IDLE,to:[_.WAITING,_.ERROR]},{from:_.WAITING,to:[_.STREAMING,_.ERROR]},{from:_.STREAMING,to:[_.COMPLETED,_.ERROR]},{from:_.COMPLETED,to:[_.IDLE,_.WAITING,_.ERROR]},{from:_.ERROR,to:[_.IDLE]}]}reset(){this._currentState=_.IDLE}}class L extends l{constructor(e,t){super(),this._currentQuestion=null,this._currentAnswer=null,this._sessionId=e,this._stateMachine=new Me,i.info(`Conversation session created: ${e}`)}get sessionId(){return this._sessionId}get state(){return this._stateMachine.getCurrentState()}_setQuestion(e){this._currentQuestion=e,this._stateMachine.transitionTo(_.WAITING),this._syncContextState()}get currentQuestion(){return this._currentQuestion}_startAnswer(e){this._currentAnswer={questionId:e,text:"",isComplete:!1,timestamp:Date.now()},this._stateMachine.transitionTo(_.STREAMING),this._syncContextState()}_appendAnswerChunk(e){this._currentAnswer&&(this._currentAnswer.text+=e)}_completeAnswer(){this._currentAnswer&&(this._currentAnswer.isComplete=!0,this._currentAnswer.completedAt=Date.now()),this._stateMachine.transitionTo(_.COMPLETED),this._syncContextState()}get currentAnswer(){return this._currentAnswer}_setError(e){this._stateMachine.transitionTo(_.ERROR),this._syncContextState()}_syncContextState(){}handleEvent(e){const t=this._stateMachine.getCurrentState();switch(e.type){case"QUESTION":if(t!==_.IDLE&&t!==_.COMPLETED){i.warn(`Ignoring QUESTION event in invalid state: ${t}`);return}this._setQuestion(e.payload);break;case"ANSWER_START":if(t!==_.WAITING){i.warn(`Ignoring ANSWER_START event in invalid state: ${t}. Expected: WAITING`);return}this._startAnswer(e.payload);break;case"ANSWER_CHUNK":if(t!==_.STREAMING){i.warn(`Ignoring ANSWER_CHUNK event in invalid state: ${t}. Expected: STREAMING`);return}this._appendAnswerChunk(e.payload&&e.payload.chunk||""),e.payload&&e.payload.isComplete&&this._completeAnswer();break;case"ANSWER_COMPLETE":if(t!==_.STREAMING){i.warn(`Ignoring ANSWER_COMPLETE event in invalid state: ${t}. Expected: STREAMING`);return}this._completeAnswer();break;case"ERROR":this._setError(e.payload);break;default:i.warn(`Unknown event type: ${e.type}`);break}}reset(){this._stateMachine.reset(),this._currentQuestion=null,this._currentAnswer=null}onDispose(){this.reset(),i.info(`Conversation session disposed: ${this._sessionId}`)}}class Ve extends l{constructor(){super(...arguments),this._buffers=new Map}appendChunk(e,t){this.ensureNotDisposed();const n=(this._buffers.get(e)||"")+t;return this._buffers.set(e,n),i.debug(`Message chunk appended for question ${e}, length: ${n.length}`),n}getMessage(e){return this._buffers.get(e)||null}complete(e){const t=this.getMessage(e)||"";return this._buffers.delete(e),i.debug(`Message assembly completed for question ${e}, length: ${t.length}`),t}clear(e){this._buffers.delete(e)}onDispose(){this._buffers.clear(),i.info("Message Assembler disposed")}}class Ue{generateSessionId(){return`session_${Date.now()}_${Math.random().toString(36).substring(2,9)}`}generateQuestionId(){return Math.random().toString(36).substring(2,6)}}class Ke extends l{constructor(e,t){super(),this._transport=null,this._sessions=new Map,this._context=e,this._assembler=new Ve,this._idService=new Ue,i.info("Conversation Manager created")}setTransport(e){this._transport=e}async sendQuestion(e,t={speed:100,mood:0,vol:50}){this.ensureNotDisposed();const s=this._idService.generateQuestionId(),n={id:s,text:e,timestamp:Date.now(),inputMode:"text"},r=this._idService.generateSessionId(),a=new L(r,this._context);if(a.handleEvent({type:"QUESTION",payload:n}),this._sessions.set(s,a),this._transport)try{this._transport.sendText(s,e,200,t)}catch(c){throw i.error("Error sending question via transport:",c),a.handleEvent({type:"ERROR",payload:"Failed to send question"}),this._context.eventBus.emit("inner:sdk:error",{error:c instanceof Error?c:new Error(String(c))}),c}else i.warn("Transport not set, question not sent");return this._context.eventBus.emit("inner:conversation:question:sent",{questionId:s,text:e}),this._context.eventBus.emit("inner:conversation:answer:waiting",{questionId:s}),i.info(`Question sent: ${s}`),s}handleServerPong(){this.ensureNotDisposed(),this._transport&&this._transport.handlePongReceived()}willDisconnect(){this.ensureNotDisposed(),this._transport?.willDisconnect()}handleTextInput(e){this.sendQuestion(e).catch(t=>{i.error("Error sending question:",t)})}handleAnswerChunk(e,t,s){this.ensureNotDisposed();const n=this._sessions.get(e);if(!n){i.warn(`No active session for answer chunk: ${e}`);return}if(n.state===_.WAITING&&n.handleEvent({type:"ANSWER_START",payload:e}),s){n.handleEvent({type:"ANSWER_COMPLETE"});const a=this._assembler.complete(e);this._context.eventBus.emit("inner:conversation:answer:completed",{questionId:e,fullAnswer:a});try{n.dispose()}catch(c){i.error("Error disposing session:",c)}this._sessions.delete(e)}else n.handleEvent({type:"ANSWER_CHUNK",payload:{uid:e,chunk:t,isComplete:s}}),this._assembler.appendChunk(e,t),this._context.eventBus.emit("inner:conversation:answer:chunk",{questionId:e,chunk:t,isComplete:s})}handleServerInitiatedMessage(e,t){this.ensureNotDisposed(),this._context.eventBus.emit("inner:conversation:server:message",{questionId:e,message:t,type:"text"})}handleAsrTextReceived(e,t){this.ensureNotDisposed();const s=this._idService.generateSessionId(),n=new L(s,this._context);n.handleEvent({type:"QUESTION",payload:{id:e,text:t,timestamp:Date.now(),inputMode:"voice"}}),this._sessions.set(e,n),this._context.eventBus.emit("inner:conversation:asr:received",{questionId:e,text:t})}onDispose(){for(const e of this._sessions.values())try{e.dispose()}catch(t){i.error("Error disposing session:",t)}this._sessions.clear(),this._assembler.dispose(),i.info("Conversation Manager disposed")}}class We extends l{constructor(e,t){super(),this._manager=new Ke(e,t),i.info("Conversation Controller created")}get manager(){return this._manager}setTransport(e){this._manager.setTransport(e)}async sendQuestion(e,t){return this.ensureNotDisposed(),this._manager.sendQuestion(e,t)}onDispose(){this._manager.dispose(),i.info("Conversation Controller disposed")}}class Be extends l{constructor(){super(...arguments),this._tracks=new Map,this._participants=new Map}register(e,t,s){this.ensureNotDisposed(),this._tracks.set(e,t),this._participants.has(s)||this._participants.set(s,new Set),this._participants.get(s).add(e),t.on("ended",()=>{i.debug(`Track ended: ${e}`),this.unregister(e)}),i.debug(`Track registered: ${e} from participant ${s}`)}unregister(e){const t=this._tracks.get(e);if(t){t.stop(),this._tracks.delete(e);for(const[s,n]of this._participants.entries())if(n.has(e)){n.delete(e),n.size===0&&this._participants.delete(s);break}i.debug(`Track unregistered: ${e}`)}}getTrack(e){return this._tracks.get(e)||null}getParticipantTracks(e){const t=this._participants.get(e);return t?Array.from(t):[]}getAllTrackIds(){return Array.from(this._tracks.keys())}getVideoAvailability(){let e=!1,t=!1,s=!1;for(const n of this._tracks.values()){const r=n.mediaStreamTrack;if(r.kind==="video"&&(e=!0,r.readyState==="live"&&(t=!0),r.enabled&&(s=!0),r.readyState==="live"&&r.enabled))return{available:!0}}return e?t?s?{available:!1,reason:T.NO_VIDEO_TRACK}:{available:!1,reason:T.TRACK_MUTED}:{available:!1,reason:T.TRACK_ENDED}:{available:!1,reason:T.NO_VIDEO_TRACK}}clear(){for(const e of this._tracks.values())e.stop();this._tracks.clear(),this._participants.clear(),i.info("All tracks cleared")}onDispose(){this.clear()}}class P extends l{constructor(e,t){super(),this._videoController=null,this._room=null,this._context=e,this._trackRegistry=t,this._loosTrackTimer=null,i.info("Video Track Handler created")}setRoom(e){this._room=e}setVideoController(e){this._videoController=e}handleTrackSubscribed(e,t,s){this.ensureNotDisposed(),this._loosTrackTimer&&clearTimeout(this._loosTrackTimer),this._trackRegistry.register(t,e,s),this._videoController&&e.attach(this._videoController.getSource().getInternalElement()),e.on("videoPlaybackStarted",()=>{i.info("inner:rtc:video:available"),this._context.eventBus.emit("inner:rtc:video:available",void 0)}),this._context.eventBus.emit("inner:video:track:added",{trackId:t,participantId:s}),i.debug(`Video track subscribed: ${t}`)}handleTrackUnsubscribed(e,t,s){this.ensureNotDisposed(),this._videoController&&e.detach(this._videoController.getSource().getInternalElement()),e.removeAllListeners(),this._context.eventBus.emit("inner:video:track:removed",{trackId:t,participantId:s}),this._loosTrackTimer&&clearTimeout(this._loosTrackTimer),this._loosTrackTimer=setTimeout(()=>{this._context.eventBus.emit("inner:rtc:video:unavailable",void 0)},se),this._trackRegistry.unregister(t),i.debug(`Video track unsubscribed: ${t}`)}supports(e){return e.mediaStreamTrack.kind==="video"}onDispose(){this._loosTrackTimer&&(clearTimeout(this._loosTrackTimer),this._loosTrackTimer=null),i.info("Video Track Handler disposed")}}class F extends l{constructor(e,t){super(),this._audioController=null,this._room=null,this._context=e,this._trackRegistry=t,i.info("Audio Track Handler created")}setRoom(e){this._room=e}setAudioController(e){this._audioController=e}handleTrackSubscribed(e,t,s){this.ensureNotDisposed(),this._trackRegistry.register(t,e,s),this._audioController?e.attach(this._audioController.getOutputElement()):this._context.eventBus.emit("inner:audio:track:added",{trackId:t,participantId:s}),i.debug(`Audio track subscribed: ${t}`)}handleTrackUnsubscribed(e,t,s){this.ensureNotDisposed(),this._audioController?(this._audioController.removeOutputTrack(t),e.detach(this._audioController.getOutputElement())):this._context.eventBus.emit("inner:audio:track:removed",{trackId:t}),this._trackRegistry.unregister(t),i.debug(`Audio track unsubscribed: ${t}`)}supports(e){return e.mediaStreamTrack.kind==="audio"}onDispose(){i.info("Audio Track Handler disposed")}}class $e extends l{constructor(e,t){super(),this._room=null,this._context=e,this._trackRegistry=t,this._handlers=[new P(e,t),new F(e,t)],i.info("LiveKit Event Adapter created")}setRoom(e){this._room=e,this._handlers.forEach(t=>{t.setRoom(e)}),console.log("LiveKit room set:",this._room)}setVideoController(e){const t=this._handlers.find(s=>s instanceof P);t&&t.setVideoController(e)}setAudioController(e){const t=this._handlers.find(s=>s instanceof F);t&&t.setAudioController(e)}handleTrackSubscribed(e,t,s){this.ensureNotDisposed();const n=this._findHandler(e);n?n.handleTrackSubscribed(e,t,s):i.warn(`No handler found for track, trackId: ${t}`)}handleTrackUnsubscribed(e,t){this.ensureNotDisposed();const s=this._trackRegistry.getTrack(e);if(s){const n=this._findHandler(s);n?n.handleTrackUnsubscribed(s,e,t):(this._trackRegistry.unregister(e),i.warn(`No handler found for track, trackId: ${e}`))}}_findHandler(e){return this._handlers.find(t=>t.supports(e))||null}onDispose(){this._handlers.forEach(e=>{try{e.dispose()}catch(t){i.error("Error disposing track handler:",t)}}),this._handlers.length=0,i.info("LiveKit Event Adapter disposed")}}class He extends l{constructor(e){super(),this._room=null,this._context=e,this._trackRegistry=new Be,this._eventAdapter=new $e(e,this._trackRegistry),this._maxAttempts=e.options.reconnect?.maxAttempts||D,this._maxDelay=(e.options.reconnect?.delay??U)*A,i.info("LiveKit Service created")}async connect(e,t,s,n){if(this.ensureNotDisposed(),this._room&&this._room.isConnected()){i.warn("Already connected to LiveKit room");return}try{i.info(`Connecting to LiveKit room: ${s} at ${e}`);const r=new Q({reconnectPolicy:{nextRetryDelayInMs:a=>a.elapsedMs<this._maxDelay?Math.min(A*Math.pow(2,a.retryCount),this._maxDelay):null}});r.prepareConnection(e,t),r.on(C.TrackSubscribed,(a,c,h)=>{this._handleTrackSubscribed(a,c,h)}),r.on(C.TrackUnsubscribed,(a,c,h)=>{this._handleTrackUnsubscribed(a,c,h)}),r.on(C.TrackMuted,(a,c)=>{this._handleTrackMuted(a,c)}),r.on(C.TrackUnmuted,(a,c)=>{this._handleTrackUnmuted(a,c)}),r.on(C.Connected,()=>{i.info("Connected to LiveKit room"),this._context.eventBus.emit("inner:rtc:connected",void 0)}),r.on(C.Disconnected,a=>{a?i.info(`LiveKit room disconnected: ${a}`):i.info("LiveKit room disconnected"),a!==Y.CLIENT_INITIATED&&this._context.eventBus.emit("inner:rtc:disconnected",{reason:a?String(a):void 0}),this._room=null}),i.info("Connecting to LiveKit room..."),await r.connect(e,t,{autoSubscribe:!0,maxRetries:this._context.options.reconnect?.maxAttempts||D}),this._eventAdapter.setRoom(r),this._room=r}catch(r){throw u.fromError(r,d.LIVEKIT_CONNECT_FAILED)}}_handleTrackSubscribed(e,t,s){try{if(!e.mediaStreamTrack){i.warn("Track does not have MediaStreamTrack");return}e.kind==="audio"&&s.on(X.IsSpeakingChanged,c=>{console.log("\u8F68\u9053 \u8BF4\u8BDD\u72B6\u6001:",c)});const r=t.trackSid,a=s.identity;this._eventAdapter.handleTrackSubscribed(e,r,a)}catch(n){i.error("Error handling track subscribed:",n)}}_handleTrackUnsubscribed(e,t,s){try{const n=t,r=s,a=n.trackSid,c=r.identity;this._trackRegistry.getTrack(a)?.kind==="video"&&this._context.eventBus.emit("inner:rtc:video:unavailable",void 0),this._eventAdapter.handleTrackUnsubscribed(a,c)}catch(n){i.error("Error handling track unsubscribed:",n)}}_handleTrackMuted(e,t){try{e.kind==="video"&&this._context.eventBus.emit("inner:rtc:video:unavailable",void 0)}catch(s){i.error("Error handling track muted:",s)}}_handleTrackUnmuted(e,t){try{e.kind==="video"&&this._context.eventBus.emit("inner:rtc:video:available",void 0)}catch(s){i.error("Error handling track unmuted:",s)}}async disconnect(){if(this._room)try{await this._room.disconnect(),this._room=null,this._context.eventBus.emit("inner:rtc:disconnected",{reason:"Disconnected from LiveKit room"}),i.info("Disconnected from LiveKit room")}catch(e){i.error("Error disconnecting from LiveKit room:",e),this._room=null}}isConnected(){return this._room?this._room.state===J.Connected:!1}getTrackRegistry(){return this._trackRegistry}get trackRegistry(){return this._trackRegistry}getEventAdapter(){return this._eventAdapter}onDispose(){this.disconnect(),this._trackRegistry.dispose(),this._eventAdapter.dispose(),i.info("LiveKit Service disposed")}}class ze extends l{constructor(e){super(),this._conversationManager=null,this._context=e,i.info("WebSocket Dispatcher created")}setConversationManager(e){this._conversationManager=e,i.debug("Conversation manager set in dispatcher")}dispatch(e){this.ensureNotDisposed();try{if(e.type==="ws"){this._handleWSServerMessage(e);return}if(e.type==="text"){this._handleServerTextMessage(e);return}}catch(t){i.error("Error dispatching message:",t)}}_handleWSServerMessage(e){e.code===101?(this._conversationManager?.handleServerPong(),i.debug("Received pingpong response")):e.code===102&&(i.warn("Server will disconnect"),this._conversationManager?.willDisconnect())}_handleServerTextMessage(e){if(!this._conversationManager){i.warn("Conversation manager not set, text message ignored");return}const{uid:t,payload:s,code:n}=e;n===200?(this._conversationManager.handleAnswerChunk(t,s,!1),i.debug(`Answer received (code 401) for uid: ${t}`)):n===201?(this._conversationManager.handleAnswerChunk(t,"",!0),i.debug(`Answer completed (code 411) for uid: ${t}`)):n===300?(this._conversationManager.handleAsrTextReceived(t,s),i.debug(`ASR text chunk received (code 410) for uid: ${t}`)):n===400&&(this._conversationManager.handleServerInitiatedMessage(t,s),i.debug(`Server initiated message received (code 412) for uid: ${t}`))}onDispose(){i.info("WebSocket Dispatcher disposed")}}class qe extends l{constructor(e){super(),this._ws=null,this._dispatcher=null,this._isConnecting=!1,this._activeTimers=new Set,this._retryCount=0,this._maxRetries=D,this._pingTimer=void 0,this._pongTimer=void 0,this._isExplicitDisconnect=!1,this._context=e,this._maxRetries=e.options.reconnect?.maxAttempts??D,this._maxDelay=(e.options.reconnect?.delay??U)*A,this._dispatcher=new ze(e)}setConversationManager(e){this._dispatcher&&this._dispatcher.setConversationManager(e)}async connect(){if(this.ensureNotDisposed(),this._stopHeartbeat(),!(this._isConnecting||this.isConnected||this._ws?.readyState===WebSocket.OPEN)){this._isConnecting=!0,this._isExplicitDisconnect=!1;try{return this._ws=new WebSocket(this._context.wsUrl),new Promise((e,t)=>{this._ws.onopen=()=>{this._isConnecting=!1,this._retryCount=0,this.sendPing(),this._context.eventBus.emit("inner:ws:connected",void 0),i.info("Conversation transport connected"),e()},this._ws.onerror=s=>{this._isConnecting=!1,i.error("Conversation transport error:",s),this._context.eventBus.emit("inner:sdk:error",{error:new u("WebSocket connection error",d.WS_CONNECT_ERROR,s)}),t(new u("WebSocket connection error",d.WS_CONNECT_ERROR,s))},this._ws.onclose=s=>{const n=s.code,r=s.reason?s.reason:s.wasClean?"Clean close":"Closed by remote";this._handleDisconnect(n,r)},this._ws.onmessage=s=>{this._handleMessage(s.data)}})}catch(e){throw this._isConnecting=!1,this._context.emitError(e,d.WS_CONNECT_FAILED)}}}disconnect(){this._isExplicitDisconnect=!0,this._stopHeartbeat(),this._ws&&(this._ws.close(),this._ws=null),this._context.eventBus.emit("inner:ws:disconnected",{reason:"Client disconnect"}),i.info("Conversation transport disconnected")}_handleDisconnect(e,t){if(this._stopHeartbeat(),this._isConnecting=!1,e!==void 0&&new Set([1e3,1001,1002,1003,1007,1008]).has(e)){this._isExplicitDisconnect=!0;const n=`WebSocket closed (code ${e}): ${t??""}`;this._context.eventBus.emit("inner:ws:disconnected",{reason:n}),i.info(`Conversation transport closed with non-retry code ${e}: ${t??""}`);return}if(!this._isExplicitDisconnect&&this._retryCount<this._maxRetries){this._retryCount++;const n=Math.min(A*Math.pow(2,this._retryCount-1),this._maxDelay);i.warn(`Connection lost. Retry ${this._retryCount} in ${n}ms`);const r=setTimeout(()=>{!this._isExplicitDisconnect&&!this.isDisposed&&this.connect().catch(()=>{})},n);this._registerTimer(r)}else this._context.eventBus.emit("inner:ws:disconnected",{reason:"Closed"})}_registerTimer(e){return this._activeTimers.add(e),e}_clearTimer(e){e&&(clearTimeout(e),this._activeTimers.delete(e))}sendPing(){if(!this.isConnected)return;const e={type:"ws",code:101,payload:"ping"};this._ws?.send(JSON.stringify(e)),clearTimeout(this._pongTimer),this._pongTimer=setTimeout(()=>{i.error(`Pong timeout (${I/A}s). Reconnecting...`),this._ws?.close()},I)}handlePongReceived(){clearTimeout(this._pongTimer),this._clearTimer(this._pingTimer),this._pingTimer=setTimeout(()=>{this.sendPing()},ie)}willDisconnect(){if(this.ensureNotDisposed(),this._ws&&this._ws.readyState===WebSocket.OPEN){const t=JSON.stringify({type:"ws",code:102,payload:"will disconnect"});this._ws.send(t),this._isExplicitDisconnect=!0,this._ws.close(),this._ws=null,this._context.eventBus.emit("inner:ws:disconnected",{reason:"Client responsive to server's disconnect message."})}}sendText(e,t,s=200,n){if(this.ensureNotDisposed(),!this._ws||this._ws.readyState!==WebSocket.OPEN)throw new u("WebSocket is not connected",d.WS_NOT_CONNECTED);try{const a=JSON.stringify({type:"text",code:s,uid:e,metadata:n,payload:t});this._ws.send(a),i.debug("Text message sent via transport:",e)}catch(r){throw this._context.emitError(r,d.WS_TEXT_SEND_FAILED)}}_handleMessage(e){try{if(typeof e!="string")return;const t=JSON.parse(e);i.debug("Message received:",t.type),this._dispatcher?this._dispatcher.dispatch(t):i.warn("Dispatcher not set, message ignored")}catch(t){i.error("Error parsing message:",t),this._context.emitError(t,d.WS_PARSE_ERROR)}}sendBinary(e){if(this.ensureNotDisposed(),!this._ws||this._ws.readyState!==WebSocket.OPEN){i.debug("WebSocket not connected, binary data dropped");return}try{this._ws.send(e),i.debug("Binary data sent via transport")}catch(t){i.error("Error sending binary data:",t)}}get isConnected(){return this._ws?.readyState===WebSocket.OPEN}_stopHeartbeat(){this._clearTimer(this._pingTimer),this._clearTimer(this._pongTimer)}onDispose(){this._stopHeartbeat(),this.disconnect(),this._dispatcher&&(this._dispatcher.dispose(),this._dispatcher=null),i.info("Conversation transport disposed")}}const w=["sdk:connected","sdk:disconnected","sdk:error","media:video:available","media:video:unavailable","media:video:trackAdded","media:video:trackRemoved","media:audio:trackAdded","media:audio:trackRemoved","media:audio:captureStarted","media:audio:captureStopped","media:audio:volumeChanged","media:audio:muted","media:audio:unmuted","conversation:question:sent","conversation:answer:waiting","conversation:server:message","conversation:asr:received","conversation:answer:chunk","conversation:answer:completed"],M=/^[a-z]+:[a-zA-z]+(:[a-zA-Z]+)?$/;class Ge{constructor(e){this.inner=e,this.listeners=new Map,this.forwardedVideoTrackIds=new Set,this.wsConnected=!1,this.livekitConnected=!1,this.httpConnected=!1,this.internalMap={"inner:sdk:error":{public:"sdk:error",sanitizer:t=>({message:t?.error?.message,code:t?.error?.code})},"inner:rtc:video:available":{public:"media:video:available"},"inner:rtc:video:unavailable":{public:"media:video:unavailable"},"inner:video:track:added":{public:"media:video:trackAdded"},"inner:video:track:removed":{public:"media:video:trackRemoved"},"inner:audio:track:added":{public:"media:audio:trackAdded"},"inner:audio:track:removed":{public:"media:audio:trackRemoved"},"inner:audio:input:started":{public:"media:audio:captureStarted"},"inner:audio:input:stopped":{public:"media:audio:captureStopped"},"inner:audio:volume:changed":{public:"media:audio:volumeChanged",sanitizer:t=>({volume:t?.volume})},"inner:audio:muted":{public:"media:audio:muted"},"inner:audio:unmuted":{public:"media:audio:unmuted"},"inner:conversation:question:sent":{public:"conversation:question:sent",sanitizer:t=>({questionId:t?.questionId,text:String(t?.text||"")})},"inner:conversation:answer:waiting":{public:"conversation:answer:waiting",sanitizer:t=>({questionId:t?.questionId})},"inner:conversation:server:message":{public:"conversation:server:message",sanitizer:t=>({questionId:t?.questionId,message:String(t?.message||""),type:String(t?.type||"")})},"inner:conversation:asr:received":{public:"conversation:asr:received",sanitizer:t=>({questionId:t?.questionId,text:String(t?.text||"")})},"inner:conversation:answer:chunk":{public:"conversation:answer:chunk",sanitizer:t=>({questionId:t?.questionId,chunk:String(t?.chunk||"")})},"inner:conversation:answer:completed":{public:"conversation:answer:completed",sanitizer:t=>({questionId:t?.questionId,fullAnswer:String(t?.fullAnswer||"")})}},Object.keys(this.internalMap).forEach(t=>{this.inner.on(t,s=>this.handleInternalEvent(t,s))}),this.inner.on("inner:sdk:connected",t=>this.handleConnected(t,!0)),this.inner.on("inner:sdk:disconnected",t=>this.handleConnected(t,!1)),w.forEach(t=>{this.inner.on(t,s=>this.handlePublicEvent(t,s))})}publicAPI(){return{on:this.on.bind(this),off:this.off.bind(this),once:this.once.bind(this)}}on(e,t){const s=String(e);return this.assertAllowed(s),this.listeners.has(s)||this.listeners.set(s,new Set),this.listeners.get(s).add(t),()=>this.off(e,t)}once(e,t){const s=String(e);this.assertAllowed(s);const n=r=>{try{t(r)}finally{this.off(e,n)}};return this.on(e,n)}off(e,t){const s=String(e);this.assertAllowed(s);const n=this.listeners.get(s);n&&(n.delete(t),n.size===0&&this.listeners.delete(s))}handleInternalEvent(e,t){const s=this.internalMap[e];if(!s)return;const n=s.public;if(w.indexOf(n)===-1)return;if(n==="media:video:trackAdded"){const c=t?.trackId||t?.id;if(typeof c=="string"){if(this.forwardedVideoTrackIds.has(c))return;this.forwardedVideoTrackIds.add(c)}}const r=s.sanitizer?s.sanitizer(t):t;if(!M.test(n))return;const a=this.listeners.get(n);!a||a.size===0||a.forEach(c=>{try{c(r)}catch(h){console.error("PublicEventEmitter listener error",h)}})}handlePublicEvent(e,t){if(w.indexOf(e)===-1)return;const s=Object.values(this.internalMap).find(a=>a.public===e),n=s&&s.sanitizer?s.sanitizer(t):t;if(e==="media:video:trackAdded"){const a=t?.trackId||t?.id;if(typeof a=="string"){if(this.forwardedVideoTrackIds.has(a))return;this.forwardedVideoTrackIds.add(a)}}const r=this.listeners.get(e);!r||r.size===0||r.forEach(a=>{try{a(n)}catch(c){console.error("PublicEventEmitter listener error",c)}})}handleConnected(e,t){e.source==="livekit"?this.livekitConnected=t:e.source==="ws"?this.wsConnected=t:e.source==="http"&&(this.httpConnected=t),this.maybeEmitSdkConnected(t?"connected":"disconnected")}maybeEmitSdkConnected(e){const t=e==="connected",s=t?!!this.livekitConnected:!this.livekitConnected,n=t?!!this.wsConnected:!this.wsConnected,r=t?!!this.httpConnected:!this.httpConnected,a={livekit:s,ws:n,http:r,all:s&&n&&r},c=JSON.stringify(a);this.lastConnectedPayloadJson!==c&&(this.lastConnectedPayloadJson=c,this.inner.emit(`sdk:${e}`,a))}assertAllowed(e){if(w.indexOf(e)===-1)throw new Error(`Event "${e}" is not allowed for public consumers`);if(!M.test(e))throw new Error(`Event name "${e}" does not follow namespace:domain[:action] naming`)}}class je extends l{constructor(e,t,s){super(),this._requestCounter=0,this._context=e,this._baseURL=t||"",this._defaultHeaders=s||{},i.info(`HTTP Service created with baseURL: ${this._baseURL||"none"}`)}setAuthToken(e){this._defaultHeaders.Authorization=`Bearer ${e}`}request(e){this.ensureNotDisposed();const t=e.requestId||`req_${Date.now()}_${++this._requestCounter}`,s=this._buildURL(e.url,e.params),n=this._mergeHeaders(e.headers),r={method:e.method||"GET",headers:n};e.data!==void 0&&(e.method||"GET")!=="GET"&&(typeof e.data=="string"?r.body=e.data:(r.body=JSON.stringify(e.data),!n["Content-Type"]&&!n["content-type"]&&(n["Content-Type"]="application/json")));const a=e.timeout||3e4,c=new AbortController;return r.signal=c.signal,new Promise((h,m)=>{const g=setTimeout(()=>c.abort(),a);fetch(s,r).then(async p=>{clearTimeout(g);const v={};p.headers.forEach((G,j)=>{v[j]=G});let S;const N=p.headers.get("content-type");N&&N.includes("application/json")?S=await p.json():S=await p.text();const q={requestId:t,status:p.status,headers:v,data:S,config:e};i.debug(`HTTP request succeeded: ${e.method} ${s} [${t}]`),h(q)}).catch(p=>{clearTimeout(g);const v=p&&p.name==="AbortError",S={requestId:t,status:p&&p.status||void 0,message:v?"Request aborted (timeout)":p&&p.message||"HTTP request failed",error:p,config:e};i.error(`HTTP request failed: ${e.method} ${s} [${t}]`,p),m(S)})})}get(e,t,s){return this.request({...s,url:e,method:"GET",params:t})}post(e,t,s){return this.request({...s,url:e,method:"POST",data:t})}put(e,t,s){return this.request({...s,url:e,method:"PUT",data:t})}delete(e,t){return this.request({...t,url:e,method:"DELETE"})}patch(e,t,s){return this.request({...s,url:e,method:"PATCH",data:t})}_buildURL(e,t){if(e.startsWith("http://")||e.startsWith("https://"))return this._appendParams(e,t);let s=this._baseURL;return s&&!s.endsWith("/")&&!e.startsWith("/")?s+="/":s&&s.endsWith("/")&&e.startsWith("/")&&(s=s.slice(0,-1)),s+=e,this._appendParams(s,t)}_appendParams(e,t){if(!t||Object.keys(t).length===0)return e;const s=new URLSearchParams;for(const[r,a]of Object.entries(t))s.append(r,String(a));const n=e.includes("?")?"&":"?";return`${e}${n}${s.toString()}`}_mergeHeaders(e){const t={...this._defaultHeaders};return e&&Object.assign(t,e),t}onDispose(){i.info("HTTP Service disposed")}}function Qe(o,e="https"){return o.startsWith(e)?o:`${e}://${o}`}class Ye extends l{constructor(e,t){super(),this._context=e,this._httpService=new je(e,t||ne)}getAuthToken(){return this.ensureNotDisposed(),this._httpService?.request({url:"/auth/login",method:"GET",data:{}}).then(e=>{e.data.code===R&&this._httpService?.setAuthToken(e.data.data.token)}).catch(e=>{throw this._context.emitError(e,d.SDK_AUTH_TOKEN_FAILED)})}getLivekitConfig(){return this.ensureNotDisposed(),this._httpService?.request({url:"/stream/v1/start",method:"POST",data:{avatarName:this._context.options.avatarName,voice:this._context.options.avatarVoice||""}}).then(e=>{if(e.data.code===R){const t=e.data.data;return this._context.setLivekitConfig(Qe(t.livekitUrl,"wss"),t.roomToken,t.roomId),this._context.setWsUrl(`${this._context.options.wsUrlPrefix||oe}/${t.roomId}/${this._context.options.avatarName}`),this._context.setVideoOptions({renderMode:t.greenScreen.enabled?"processed":"raw",greenScreen:t.greenScreen||{}}),this._context.eventBus.emit("inner:sdk:connected",{source:"http"}),!0}else throw new u(e.data.msg,d.SDK_GET_LIVEKIT_CONFIG_FAILED)}).catch(e=>{throw this._context.eventBus.emit("inner:sdk:error",{error:new u(e.message,d.SDK_GET_LIVEKIT_CONFIG_FAILED)}),u.fromError(e,d.SDK_GET_LIVEKIT_CONFIG_FAILED)})}switchVideo({onceActions:e,loopActions:t}){return this.ensureNotDisposed(),this._httpService?.request({url:"/avatar/v1/switch-video",method:"POST",data:{roomId:this._context.roomId,onceActions:e,loopActions:t}}).then(s=>{if(s.data.code===R)return!0;throw this._context.emitError(s.data.message,d.SDK_SWITCH_VIDEO_FAILED)})}interruptConversation(){return this.ensureNotDisposed(),this._httpService?.request({url:"/stream/v1/interrupt",method:"POST",data:{roomId:this._context.roomId}}).then(e=>{if(e.data.code===R)return!0;throw this._context.emitError(e.data.message,d.SDK_INTERRUPT_CONVERSATION_FAILED)}).finally(()=>{this._context.eventBus.emit("inner:sdk:disconnected",{source:"http"})})}disconnectConversation(){return this.ensureNotDisposed(),this._httpService?.request({url:"/stream/v1/stop",method:"POST",data:{roomId:this._context.roomId}}).then(e=>e.data.code===R).finally(()=>{this._context.eventBus.emit("inner:sdk:disconnected",{source:"http"})})}onDispose(){this._httpService.dispose()}}class Xe extends l{constructor(e){super(),this._videoController=null,this._audioController=null,this._conversationController=null,this._liveKitService=null,this._conversationTransport=null,this._connectionCoordinator=null,this._httpController=null,this._preConnectPromise=null,this._lastPreConnectSuccessAt=null,this._context=new ce(e),i.info("SDK Client created")}get events(){return this.ensureNotDisposed(),this._publicEventAPI||(this._publicEmitter=new Ge(this._context.eventBus),this._publicEventAPI=this._publicEmitter.publicAPI()),this._publicEventAPI}pushActions({onceActions:e,loopActions:t}){return this.ensureNotDisposed(),this._ensureConnected(),this._httpController?.switchVideo({onceActions:e,loopActions:t})}interruptConnection(){return this.ensureNotDisposed(),this._ensureConnected(),this._httpController?.interruptConversation()}sendTextQuestion(e){if(this.ensureNotDisposed(),this._ensureConnected(),this._conversationController)return this._conversationController.sendQuestion(e);throw new u("Conversation controller is not available",d.CONVERSATION_CONTROLLER_NOT_AVAILABLE)}setRenderFitMode(e){this.ensureNotDisposed(),this._ensureConnected(),this._videoController&&this._videoController.setRenderFitMode(e)}async startAudioCapture(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",d.AUDIO_CONTROLLER_NOT_AVAILABLE);await this._audioController.startCapture()}async stopAudioCapture(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",d.AUDIO_CONTROLLER_NOT_AVAILABLE);await this._audioController.stopCapture()}setVolume(e){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",d.AUDIO_CONTROLLER_NOT_AVAILABLE);this._audioController.setVolume(e)}getVolume(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",d.AUDIO_CONTROLLER_NOT_AVAILABLE);return this._audioController?.getVolume()||1}mute(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",d.AUDIO_CONTROLLER_NOT_AVAILABLE);this._audioController.mute()}unmute(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",d.AUDIO_CONTROLLER_NOT_AVAILABLE);this._audioController.unmute()}get isMuted(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",d.AUDIO_CONTROLLER_NOT_AVAILABLE);return this._audioController.isMuted()}get isAudioCapturing(){return this.ensureNotDisposed(),this._ensureConnected(),this._audioController?this._audioController.isCapturing:!1}async preConnect(){if(this.ensureNotDisposed(),this._preConnectPromise)return this._preConnectPromise;const e=Date.now();return this._lastPreConnectSuccessAt!==null&&e-this._lastPreConnectSuccessAt<ae?Promise.resolve(!0):(this._preConnectPromise=(async()=>{try{if(i.info("Creating HTTP controller..."),this._httpController=this._httpController??new Ye(this._context,this._context.options.http?.baseURL),i.info("Getting auth token..."),await this._httpController.getAuthToken(),i.info("Getting livekit config..."),!!!await this._httpController?.getLivekitConfig())throw new Error("Config missing");return this._lastPreConnectSuccessAt=Date.now(),!0}catch(t){throw this._context.emitError(t,d.SDK_PRECONNECT_FAILED)}finally{this._preConnectPromise=null}})(),this._preConnectPromise)}async connect(){if(this.ensureNotDisposed(),this._connectionCoordinator&&this._context.sessionState.isConnected){i.warn("SDK already initialized");return}try{if(!await this.preConnect())throw new u("PreConnect failed without explicit error",d.SDK_CONNECT_FAILED)}catch(e){throw this._context.emitError(e,d.SDK_CONNECT_FAILED)}try{if(i.info("Initializing SDK..."),this._connectionCoordinator=new he(this._context),this._connectionCoordinator.start(),this._connectionCoordinator.startConnecting(),this._videoController=new fe(this._context,this._context.options.video),this._audioController=new Fe(this._context,this._context.options.audio),this._conversationController=new We(this._context),this._liveKitService=new He(this._context),this._conversationTransport=new qe(this._context),this._liveKitService&&this._videoController&&this._audioController){const e=this._liveKitService.getEventAdapter();e&&(typeof e.setVideoController=="function"&&e.setVideoController(this._videoController),typeof e.setAudioController=="function"&&e.setAudioController(this._audioController))}this._audioController&&this._conversationTransport&&this._audioController.setWebSocketSender({sendBinary:e=>{this._conversationTransport.sendBinary(e)},isConnected:()=>this._conversationTransport.isConnected}),this._conversationTransport&&this._conversationController&&(this._conversationTransport.setConversationManager(this._conversationController.manager),this._conversationController.setTransport(this._conversationTransport)),await this._liveKitService.connect(this._context.livekitUrl,this._context.token,this._context.options.avatarName),this._conversationTransport&&await this._conversationTransport.connect(),i.info("SDK initialized and connected")}catch(e){throw this._connectionCoordinator&&this._connectionCoordinator.transitionToIdle(),await this._cleanup(),this._context.emitError(e,d.SDK_INITIALIZATION_FAILED)}}async disconnect(){if(this.ensureNotDisposed(),!(!this._connectionCoordinator||this._connectionCoordinator.getState()==="idle"||this._connectionCoordinator.getState()==="disposed")){i.info("Disconnecting SDK..."),this._connectionCoordinator.startDisconnecting();try{this._conversationTransport&&this._conversationTransport.disconnect(),this._liveKitService&&await this._liveKitService.disconnect(),this._audioController&&this._audioController.stopCapture(),this._httpController&&await this._httpController.disconnectConversation(),this._preConnectPromise=null,this._connectionCoordinator.completeDisconnecting(),i.info("SDK disconnected")}catch(e){throw i.error("Error during disconnect:",e),this._context.emitError(e,d.SDK_DISCONNECT_FAILED)}}}async reconnect(){this.ensureNotDisposed();const e=this._connectionCoordinator;if(!e?.canStartReconnectingManual()){const s=e?e.getSnapshot():this.connectionSnapshot;return i.warn("SDK reconnect not allowed in current state, returning current snapshot"),s}const t=e.captureSnapshotAndStartReconnectingManual();try{if(this._audioController&&this._audioController.stopCapture(),t.rtc.connected&&!t.rtc.hasVideoTrack&&(await this._liveKitService?.disconnect(),await this._conversationTransport?.disconnect()),this._conversationTransport&&!this._conversationTransport.isConnected){if(!await this.preConnect())throw new u("PreConnect failed without explicit error",d.SDK_CONNECT_FAILED);await this._conversationTransport.connect()}return this._liveKitService&&!this._liveKitService.isConnected()&&await this._liveKitService.connect(this._context.livekitUrl,this._context.token,this._context.options.avatarName),t}catch(s){throw i.error("Error during reconnect:",s),this._context.emitError(s,d.SDK_RECONNECT_FAILED)}}get connectionSnapshot(){return this.ensureNotDisposed(),this._connectionCoordinator?this._connectionCoordinator.getSnapshot():W(K("idle"))}async _cleanup(){if(this._conversationTransport&&this._conversationTransport.disconnect(),this._liveKitService)try{await this._liveKitService.disconnect()}catch(e){i.error("Error disconnecting LiveKit:",e)}this._audioController&&this._audioController.stopCapture()}get isConnected(){return this.ensureNotDisposed(),this._context.sessionState.isConnected}_ensureConnected(){if(!this._context.sessionState.isConnected)throw new u("SDK is not connected. Call connect() first.",d.SDK_NOT_CONNECTED)}onDispose(){this.disconnect().catch(e=>{i.error("Error during disconnect in dispose:",e)}),this._videoController&&(this._videoController.dispose(),this._videoController=null),this._audioController&&(this._audioController.dispose(),this._audioController=null),this._conversationController&&(this._conversationController.dispose(),this._conversationController=null),this._liveKitService&&(this._liveKitService.dispose(),this._liveKitService=null),this._conversationTransport&&(this._conversationTransport.dispose(),this._conversationTransport=null),this._httpController&&(this._httpController.dispose(),this._httpController=null),this._connectionCoordinator&&(this._connectionCoordinator.transitionToDisposed(),this._connectionCoordinator.dispose(),this._connectionCoordinator=null),this._context.dispose(),i.info("SDK Client disposed")}}function it(o){if(!o.avatarName)throw new u("avatarName is required",d.NO_AVATARNAME);return i.info("Creating SDK client with options:",{avatarName:o.avatarName}),new Xe(o)}class V extends l{constructor(e,t){super(),this._videoElement=null,this._videoUrl=null,this._isPlaying=!1,this._frameCallbackId=null,this._videoUrl=e,t?this._videoElement=t:(this._videoElement=document.createElement("video"),this._videoElement.style.opacity="0",this._videoElement.muted=!0,this._videoElement.playsInline=!0,this._videoElement.loop=!0,document.querySelector("#video-container")?.appendChild(this._videoElement)),i.info("Local Video Debug Source created")}get videoElement(){return this._videoElement}async load(){if(!this._videoElement)throw new Error("Video element or URL not set");return new Promise((e,t)=>{const s=this._videoElement,n=()=>{s.removeEventListener("loadedmetadata",n),s.removeEventListener("error",r),i.info(`Video loaded: ${s.videoWidth}x${s.videoHeight}`),e()},r=a=>{s.removeEventListener("loadedmetadata",n),s.removeEventListener("error",r),t(new Error(`Failed to load video: ${a}`))};s.addEventListener("loadedmetadata",n),s.addEventListener("error",r),s.src=this._videoUrl||"",s.load()})}async play(){if(!this._videoElement)throw new Error("Video element not set");if(!this._isPlaying)try{await this._videoElement.play(),this._videoElement.style.opacity="0",this._videoElement.setAttribute("loop","true"),this._isPlaying=!0,i.info("Video playback started")}catch(e){throw i.error("Failed to play video:",e),e}}pause(){this._videoElement&&this._isPlaying&&(this._videoElement.pause(),this._isPlaying=!1,i.info("Video playback paused"))}stop(){this._videoElement&&(this._videoElement.pause(),this._videoElement.currentTime=0,this._isPlaying=!1,i.info("Video playback stopped"))}get isPlaying(){return this._isPlaying&&this._videoElement!==null&&!this._videoElement.paused}get videoWidth(){return this._videoElement?.videoWidth||0}get videoHeight(){return this._videoElement?.videoHeight||0}async captureFrame(){if(!this._videoElement||this._videoElement.readyState<2)return null;try{if("VideoFrame"in window&&typeof window.VideoFrame=="function")try{return new VideoFrame(this._videoElement,{timestamp:this._videoElement.currentTime*1e6})}catch(s){i.error("",s)}const e=await createImageBitmap(this._videoElement),t=new VideoFrame(e,{timestamp:this._videoElement.currentTime*1e6});return e.close(),t}catch(e){return i.error("Failed to capture video frame:",e),null}}onFrame(e){if(!this._videoElement)return;this.offFrame();const t=this._videoElement;if("requestVideoFrameCallback"in t){const s=(n,r)=>{!this._isPlaying||!this._videoElement||this.captureFrame().then(a=>{a&&(e(a),a.close()),this._isPlaying&&this._videoElement&&(this._frameCallbackId=t.requestVideoFrameCallback(s))}).catch(a=>{i.error("Error in frame callback:",a)})};this._frameCallbackId=t.requestVideoFrameCallback(s)}else{const s=()=>{!this._isPlaying||!this._videoElement||this.captureFrame().then(n=>{n&&(e(n),n.close()),this._isPlaying&&this._videoElement&&(this._frameCallbackId=requestAnimationFrame(s))}).catch(n=>{i.error("Error in frame callback:",n)})};this._frameCallbackId=requestAnimationFrame(s)}}offFrame(){this._frameCallbackId!==null&&(this._videoElement&&"cancelVideoFrameCallback"in this._videoElement?this._videoElement.cancelVideoFrameCallback(this._frameCallbackId):cancelAnimationFrame(this._frameCallbackId),this._frameCallbackId=null)}onDispose(){this.stop(),this.offFrame(),this._videoElement&&this._videoElement.parentElement===document.body&&document.body.removeChild(this._videoElement),this._videoElement=null,this._videoUrl=null,i.info("Local Video Debug Source disposed")}}class Je extends l{constructor(e,t,s){super(),this._outputCanvas=null,this._outputRenderer=null,this._source=new $;const n={...e,renderMode:"processed"};this._pipeline=new z(this._source,n),this._outputRenderer=new b(s,t),this._pipeline.setRenderer(this._outputRenderer),this._outputCanvas=this._outputRenderer.getCanvas(),i.info("Video Pipeline Runner created")}get outputCanvas(){return this._outputCanvas}get processor(){return this._pipeline.strategy.getProcessors()[0]||null}setProcessor(e){this._pipeline.setProcessor(e),i.info("Processor set on pipeline")}async processFrame(e){if(this.ensureNotDisposed(),!this._outputRenderer){i.error("Output renderer not available"),e.close();return}try{const t=this._pipeline.strategy.getProcessors()[0]||null;let s=null;if(t){try{s=t.process(e)}catch(n){i.error("Error processing frame:",n),s=e}if(s===null&&"getImageData"in t){const n=t.getImageData();if(n&&this._outputCanvas&&this._outputRenderer.getContext()){const r=this._outputRenderer.getContext();r&&((this._outputCanvas.width!==n.width||this._outputCanvas.height!==n.height)&&(this._outputCanvas.width=n.width,this._outputCanvas.height=n.height),r.putImageData(n,0,0))}e.close();return}s&&s!==e?(this._outputRenderer.render(s),e.close()):s===e?this._outputRenderer.render(e):e.close()}else this._outputRenderer.render(e)}catch(t){i.error("Error in processFrame:",t),e.close()}}setOutputSize(e,t){this._outputCanvas&&(this._outputCanvas.width=e,this._outputCanvas.height=t,i.info(`Output canvas size set to ${e}x${t}`))}onDispose(){this._pipeline.dispose(),this._outputRenderer&&(this._outputRenderer.dispose(),this._outputRenderer=null),this._outputCanvas&&this._outputCanvas.parentElement===document.body&&document.body.removeChild(this._outputCanvas),this._outputCanvas=null,i.info("Video Pipeline Runner disposed")}}class nt extends l{constructor(e,t,s,n){if(super(),this._isRunning=!1,this._videoSource=new V(e,t),this._options={renderMode:"processed",containerElement:t,...n},this._pipelineRunner=new Je(this._options,t,s),this._options.greenScreen?.enabled){const r=new y(this._options.greenScreen);this._pipelineRunner.setProcessor(r),i.info("Green screen processor enabled in debugger")}i.info("Standalone Video Debugger created")}async switchSource(e,t){this.ensureNotDisposed();const s=this._videoSource,n=this._isRunning,r=new V(e,t);try{await r.load(),n&&await r.play(),r.onFrame(h=>{this._pipelineRunner.processFrame(h).catch(m=>{i.error("Error processing frame:",m)})}),this._videoSource=r;try{s.offFrame(),s.stop(),s.dispose()}catch(h){console.warn("Error disposing old video source during switch:",h)}const a=this._videoSource.videoWidth,c=this._videoSource.videoHeight;a>0&&c>0&&this._pipelineRunner.setOutputSize(a,c)}catch(a){i.error("Failed to switch video source:",a);try{r.dispose()}catch(c){console.warn("Error disposing new video source after failed switch:",c)}throw a}}get outputCanvas(){return this._pipelineRunner.outputCanvas}get videoSource(){return this._videoSource}get processor(){return this._pipelineRunner.processor}setProcessor(e){this._pipelineRunner.setProcessor(e)}updateGreenScreenOptions(e){const t=this._pipelineRunner.processor;if(t&&"updateOptions"in t&&typeof t.updateOptions=="function")t.updateOptions(e),i.info("Green screen options updated");else{const s=this._options.greenScreen||{enabled:!1},n={...s,...e,enabled:e.enabled!==void 0?e.enabled:s.enabled},r=new y(n);this._pipelineRunner.setProcessor(r),this._options.greenScreen=n,i.info("Green screen processor recreated with new options")}}async initialize(){this.ensureNotDisposed();try{await this._videoSource.load();const e=this._videoSource.videoWidth,t=this._videoSource.videoHeight;e>0&&t>0&&this._pipelineRunner.setOutputSize(e,t),i.info("Video debugger initialized")}catch(e){throw i.error("Failed to initialize video debugger:",e),e}}async start(){if(this.ensureNotDisposed(),this._isRunning){i.warn("Debugger is already running");return}try{this._videoSource.videoWidth===0&&await this.initialize(),await this._videoSource.play(),this._videoSource.onFrame(e=>{this._pipelineRunner.processFrame(e).catch(t=>{i.error("Error processing frame:",t)})}),this._isRunning=!0,i.info("Video debugger started")}catch(e){throw i.error("Failed to start video debugger:",e),e}}stop(){this._isRunning&&(this._videoSource.stop(),this._videoSource.offFrame(),this._isRunning=!1,i.info("Video debugger stopped"))}pause(){this._isRunning&&(this._videoSource.pause(),i.info("Video debugger paused"))}async resume(){this._isRunning&&(await this._videoSource.play(),i.info("Video debugger resumed"))}get isRunning(){return this._isRunning}onDispose(){this.stop(),this._videoSource.dispose(),this._pipelineRunner.dispose(),i.info("Standalone Video Debugger disposed")}}export{_ as ConversationStateType,st as DEFAULT_AUDIO_BUFFER_SIZE,tt as DEFAULT_AUDIO_CHANNELS,k as DEFAULT_AUDIO_SAMPLE_RATE,D as DEFAULT_RECONNECT_ATTEMPTS,U as DEFAULT_RECONNECT_DELAY,et as DEFAULT_WS_TIMEOUT,l as Disposable,Z as EventBus,E as LogLevel,i as Logger,Xe as SDKClient,u as SDKError,te as SessionState,f as SessionStatus,nt as StandaloneVideoDebugger,it as createClient};
|