@sanseng/livekit-ws-sdk 0.1.19 → 0.1.20

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.esm.js CHANGED
@@ -107,4 +107,4 @@ class AudioResamplerProcessor extends AudioWorkletProcessor {
107
107
  }
108
108
 
109
109
  registerProcessor('audio-resampler', AudioResamplerProcessor);
110
- `;function We(){const r=new Blob([Ke],{type:"text/javascript"});return URL.createObjectURL(r)}class $e extends Be{constructor(e){super(),this.options=e,this._stream=null,this._audioContext=null,this._sourceNode=null,this._workletNode=null,this._targetSampleRate=M,this._bitDepth=V,this._channels=1;const t=e?.input;this._targetSampleRate=t?.sampleRate||M,this._bitDepth=t?.bitDepth||V,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 o=this._stream.getAudioTracks()[0].getSettings().sampleRate||44100;this._audioContext=new AudioContext({sampleRate:o});const a=We();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:v}=c.data;h==="audioData"&&m&&this.emitAudioData(m,v)},this._sourceNode.connect(this._workletNode),i.info(`Audio capture started: input=${o}Hz -> output=${this._targetSampleRate}Hz, ${this._bitDepth}bit`)}catch(e){throw l.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 He{create(e){return new $e(e)}}class qe extends g{constructor(e,t){super(),this._source=null,this._isCapturing=!1,this._currentOutputTrackId=null,this._inputPipeline=new ye(e),this._outputPipeline=new be,this._frameSender=new Ue(e),this._sourceFactory=t||new He,i.info("Audio Controller created")}setContext(e){super.setContext(e),this._frameSender.setContext(e),this._inputPipeline.onProcessedAudio=(t,s)=>{this._context.eventBus.emit("inner:audio:frame:received",{data:t,sampleRate:s})}}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")}}class ze extends p{constructor(){super(...arguments),this._stream=null,this._videoElement=null}async start(){if(this.ensureNotDisposed(),this._stream){i.warn("Camera capture already started");return}try{const e=await navigator.mediaDevices.getUserMedia({video:!0});if(this._stream=e,!this._videoElement){const t=document.createElement("video");t.autoplay=!0,t.muted=!0,t.playsInline=!0,t.style.position="fixed",t.style.opacity="0",t.style.pointerEvents="none",t.style.width="0",t.style.height="0",t.style.zIndex="-1",typeof document<"u"&&document.body&&document.body.appendChild(t),this._videoElement=t}this._videoElement.srcObject=this._stream,this._videoElement.play().catch(t=>{i.error("Failed to start camera video playback",t)})}catch(e){throw l.fromError(e,d.SDK_ERROR)}}stop(){if(this._stream&&(this._stream.getTracks().forEach(e=>e.stop()),this._stream=null),this._videoElement){this._videoElement.srcObject=null;const e=this._videoElement.parentNode;e&&e.removeChild(this._videoElement),this._videoElement=null}i.info("Camera capture stopped")}getStream(){return this.ensureNotDisposed(),this._stream}getVideoElement(){return this.ensureNotDisposed(),this._videoElement}getTrack(){if(this.ensureNotDisposed(),!this._stream)return null;const e=this._stream.getVideoTracks();return e.length>0?e[0]:null}onDispose(){this.stop()}}const N=640,w=360;class Ge extends p{constructor(e,t){super(),this._videoElement=null,this._canvas=null,this._videoElement=e,this._onFrame=t,this._handleServerCommand=this._onServerCommand.bind(this)}setContext(e){this._context=e}setVideoElement(e){this._videoElement=e}start(){this.ensureNotDisposed(),this._canvas||(typeof OffscreenCanvas<"u"?this._canvas=new OffscreenCanvas(N,w):(this._canvas=document.createElement("canvas"),this._canvas.width=N,this._canvas.height=w)),this._context.eventBus.on("inner:conversation:server:command",this._handleServerCommand)}_onServerCommand({code:e}){e===500&&this._captureFrame()}stop(){this._context.eventBus.off("inner:conversation:server:command",this._handleServerCommand)}_captureFrame(){const e=this._videoElement,t=this._canvas;if(!e||!t||e.readyState<2||e.videoWidth===0||e.videoHeight===0)return;const s=t.getContext("2d");if(!s)return;const n=e.videoWidth,o=e.videoHeight;let a=N,c=N*o/n;c>w&&(c=w,a=w*n/o),t.width!==a&&(t.width=a),t.height!==c&&(t.height=c),s.clearRect(0,0,t.width,t.height),s.drawImage(e,0,0,n,o,0,0,a,c),typeof OffscreenCanvas<"u"&&t instanceof OffscreenCanvas&&typeof t.convertToBlob=="function"?t.convertToBlob({type:"image/jpeg",quality:D}).then(h=>{if(h)try{this._onFrame(h,a,c,0,D)}catch(m){i.error("ScreenshotScheduler onFrame callback error",m)}}).catch(h=>{i.error("ScreenshotScheduler convertToBlob error",h)}):t instanceof HTMLCanvasElement&&t.toBlob(h=>{if(h)try{this._onFrame(h,a,c,0,D)}catch(m){i.error("ScreenshotScheduler onFrame callback error",m)}},"image/jpeg",D)}onDispose(){this.stop(),this._canvas=null,this._videoElement=null}}async function je(r,e){const t=await r.arrayBuffer();return{data:new Uint8Array(t),width:e.width,height:e.height,format:e.format,quality:e.quality}}const Qe=12,Xe=2,Ye=0,B=0,K=4,W=65535,$=4294967295;function Je(r){const e=Math.floor(r);return e<0?0:e>255?255:e}function x(r){const e=Math.floor(r);return e<0?0:e>W?W:e}function Ze(r){const e=Math.floor(r);return e<0?0:e>$?$:e}function et(r){const e=Math.floor(r);return e<B?B:e>K?K:e}function tt(r){const e=new ArrayBuffer(Qe),t=new DataView(e),s=et(r.format)&15,n=Xe<<6|Ye<<4|s;return t.setUint8(0,n),t.setUint8(1,Je(r.quality)),t.setUint16(2,x(r.id),!1),t.setUint16(4,x(r.width),!1),t.setUint16(6,x(r.height),!1),t.setUint32(8,Ze(r.payloadLength),!1),new Uint8Array(e)}const st=65535;class it extends p{constructor(){super(...arguments),this._wsSender=null,this._imageId=0}setWebSocketSender(e){this._wsSender=e}send(e,t){if(this.ensureNotDisposed(),!this._wsSender||!this._wsSender.isConnected())return;const s=e.byteLength,n=tt({format:t.format,quality:t.quality,id:this._nextId(),width:t.width,height:t.height,payloadLength:s}),o=new Uint8Array(n.byteLength+s);o.set(n,0),o.set(e,n.byteLength),this._wsSender.sendBinary(o)}_nextId(){const e=this._imageId;return this._imageId=this._imageId>=st?0:this._imageId+1,e}onDispose(){this._wsSender=null}}class nt extends g{constructor(){super(),this._isRunning=!1,this._source=new ze,this._sender=new it;const e=(t,s,n,o,a=D)=>{je(t,{width:s,height:n,format:o,quality:a}).then(c=>{this._sender.send(c.data,{width:c.width,height:c.height,format:c.format,quality:c.quality})})};this._scheduler=new Ge(null,e)}setContext(e){super.setContext(e),this._scheduler.setContext(e)}async startCamera(){if(this.ensureNotDisposed(),this._isRunning){i.warn("Camera already running");return}await this._source.start();const e=this._source.getVideoElement();this._scheduler.setVideoElement(e),this._scheduler.start(),this._isRunning=!0,this._context.eventBus.emit("inner:camera:started",void 0),i.info("Camera started")}stopCamera(){this.ensureNotDisposed(),this._isRunning&&(this._scheduler.stop(),this._source.stop(),this._isRunning=!1,this._context.eventBus.emit("inner:camera:stopped",void 0),i.info("Camera stopped"))}isCameraRunning(){return this._isRunning}getStream(){return this.ensureNotDisposed(),this._source.getStream()}getTrack(){return this.ensureNotDisposed(),this._source.getTrack()}attachTo(e){this.ensureNotDisposed();const t=this._source.getStream();t&&(e.srcObject=t,e.play().catch(s=>i.error("attachTo play failed",s)))}setWebSocketSender(e){this._sender.setWebSocketSender(e)}onDispose(){this.stopCamera(),this._source.dispose(),this._scheduler.dispose(),this._sender.dispose()}}var u;(function(r){r.IDLE="idle",r.WAITING="waiting",r.STREAMING="streaming",r.COMPLETED="completed",r.ERROR="error"})(u||(u={}));class rt extends Y{constructor(){super(...arguments),this._currentState=u.IDLE,this.transitionTable=[{from:u.IDLE,to:[u.WAITING,u.ERROR]},{from:u.WAITING,to:[u.STREAMING,u.ERROR]},{from:u.STREAMING,to:[u.COMPLETED,u.ERROR]},{from:u.COMPLETED,to:[u.IDLE,u.WAITING,u.ERROR]},{from:u.ERROR,to:[u.IDLE]}]}reset(){this._currentState=u.IDLE}}class H extends p{constructor(e){super(),this._currentQuestion=null,this._currentAnswer=null,this._sessionId=e,this._stateMachine=new rt,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(u.WAITING),this._syncContextState()}get currentQuestion(){return this._currentQuestion}_startAnswer(e){this._currentAnswer={questionId:e,text:"",isComplete:!1,timestamp:Date.now()},this._stateMachine.transitionTo(u.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(u.COMPLETED),this._syncContextState()}get currentAnswer(){return this._currentAnswer}_setError(e){this._stateMachine.transitionTo(u.ERROR),this._syncContextState()}_syncContextState(){}handleEvent(e){const t=this._stateMachine.getCurrentState();switch(e.type){case"QUESTION":if(t!==u.IDLE&&t!==u.COMPLETED){i.warn(`Ignoring QUESTION event in invalid state: ${t}`);return}this._setQuestion(e.payload);break;case"ANSWER_START":if(t!==u.WAITING){i.warn(`Ignoring ANSWER_START event in invalid state: ${t}. Expected: WAITING`);return}this._startAnswer(e.payload);break;case"ANSWER_CHUNK":if(t!==u.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!==u.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 ot extends p{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 at{generateSessionId(){return`session_${Date.now()}_${Math.random().toString(36).substring(2,9)}`}generateQuestionId(){return Math.random().toString(36).substring(2,6)}}class ct extends g{constructor(e){super(),this._transport=null,this._sessions=new Map,this._assembler=new ot,this._idService=new at,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"},o=this._idService.generateSessionId(),a=new H(o);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===u.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 H(s);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})}handleServerCommand(e,t){this.ensureNotDisposed(),this._context.eventBus.emit("inner:conversation:server:command",{code:e,command: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 dt extends g{constructor(e){super(),this._manager=new ct(e),i.info("Conversation Controller created")}setContext(e){super.setContext(e),this._manager.setContext(e)}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 ht extends p{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 o=n.mediaStreamTrack;if(o.kind==="video"&&(e=!0,o.readyState==="live"&&(t=!0),o.enabled&&(s=!0),o.readyState==="live"&&o.enabled))return{available:!0}}return e?t?s?{available:!1,reason:A.NO_VIDEO_TRACK}:{available:!1,reason:A.TRACK_MUTED}:{available:!1,reason:A.TRACK_ENDED}:{available:!1,reason:A.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 q extends g{constructor(e){super(),this._videoController=null,this._room=null,this._trackRegistry=e,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)},le),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 z extends g{constructor(e){super(),this._audioController=null,this._room=null,this._trackRegistry=e,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 lt extends g{constructor(e){super(),this._handlers=[],this._room=null,this._trackRegistry=e,i.info("LiveKit Event Adapter created")}setContext(e){if(super.setContext(e),this._handlers.length===0){const t=new q(this._trackRegistry),s=new z(this._trackRegistry);t.setContext(e),s.setContext(e),this._handlers.push(t,s),this._room&&(t.setRoom(this._room),s.setRoom(this._room))}}setRoom(e){this._room=e,this._handlers.forEach(t=>{t.setRoom(e)}),i.info("LiveKit room set:",this._room)}setVideoController(e){const t=this._handlers.find(s=>s instanceof q);t&&t.setVideoController(e)}setAudioController(e){const t=this._handlers.find(s=>s instanceof z);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 ut extends g{constructor(){super(),this._room=null,this._maxAttempts=y,this._maxDelay=k*T,this._trackRegistry=new ht,this._eventAdapter=new lt(this._trackRegistry),i.info("LiveKit Service created")}setContext(e){super.setContext(e),this._maxAttempts=e.options.reconnect?.maxAttempts||y,this._maxDelay=(e.options.reconnect?.delay??k)*T,this._eventAdapter.setContext(e)}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 o=new ne({reconnectPolicy:{nextRetryDelayInMs:a=>a.elapsedMs<this._maxDelay?Math.min(T*Math.pow(2,a.retryCount),this._maxDelay):null}});o.prepareConnection(e,t),o.on(S.TrackSubscribed,(a,c,h)=>{this._handleTrackSubscribed(a,c,h)}),o.on(S.TrackUnsubscribed,(a,c,h)=>{this._handleTrackUnsubscribed(a,c,h)}),o.on(S.TrackMuted,(a,c)=>{this._handleTrackMuted(a,c)}),o.on(S.TrackUnmuted,(a,c)=>{this._handleTrackUnmuted(a,c)}),o.on(S.Connected,()=>{i.info("Connected to LiveKit room"),this._context.eventBus.emit("inner:rtc:connected",void 0)}),o.on(S.Disconnected,a=>{a?i.info(`LiveKit room disconnected: ${a}`):i.info("LiveKit room disconnected"),a!==re.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 o.connect(e,t,{autoSubscribe:!0,maxRetries:this._context.options.reconnect?.maxAttempts||y}),this._eventAdapter.setRoom(o),this._room=o}catch(o){throw l.fromError(o,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(oe.IsSpeakingChanged,c=>{this._context.eventBus.emit("inner:audio:speaking:changed",{participantId:s.identity,isSpeaking:c}),i.info(`Participant ${s.identity} is speaking: ${c}`)});const o=t.trackSid,a=s.identity;this._eventAdapter.handleTrackSubscribed(e,o,a)}catch(n){i.error("Error handling track subscribed:",n)}}_handleTrackUnsubscribed(e,t,s){try{const n=t,o=s,a=n.trackSid,c=o.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===ae.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 _t extends g{constructor(){super(),this._conversationManager=null,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 200) for uid: ${t}`)):n===201?(this._conversationManager.handleAnswerChunk(t,"",!0),i.debug(`Answer completed (code 201) for uid: ${t}`)):n===300?(this._conversationManager.handleAsrTextReceived(t,s),i.debug(`ASR text chunk received (code 300) for uid: ${t}`)):n===400?(this._conversationManager.handleServerInitiatedMessage(t,s),i.debug(`Server initiated message received (code 300) for uid: ${t}`)):n===500&&(this._conversationManager.handleServerCommand(n,s),i.debug(`Server command received (code 500) for uid: ${t}`))}onDispose(){i.info("WebSocket Dispatcher disposed")}}class pt extends g{constructor(){super(),this._ws=null,this._dispatcher=null,this._isConnecting=!1,this._activeTimers=new Set,this._retryCount=0,this._maxRetries=y,this._pingTimer=void 0,this._pongTimer=void 0,this._isExplicitDisconnect=!1,this._maxDelay=k*T}setContext(e){super.setContext(e),this._maxRetries=e.options.reconnect?.maxAttempts??y,this._maxDelay=(e.options.reconnect?.delay??k)*T,this._dispatcher&&(this._dispatcher.dispose(),this._dispatcher=null),this._dispatcher=new _t,this._dispatcher.setContext(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 l("WebSocket connection error",d.WS_CONNECT_ERROR,s)}),t(new l("WebSocket connection error",d.WS_CONNECT_ERROR,s))},this._ws.onclose=s=>{const n=s.code,o=s.reason?s.reason:s.wasClean?"Clean close":"Closed by remote";this._handleDisconnect(n,o)},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 o=setTimeout(()=>{!this._isExplicitDisconnect&&!this.isDisposed&&this.connect().catch(()=>{})},n);this._registerTimer(o)}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 (${F/T}s). Reconnecting...`),this._ws?.close()},F)}handlePongReceived(){clearTimeout(this._pongTimer),this._clearTimer(this._pingTimer),this._pingTimer=setTimeout(()=>{this.sendPing()},ue)}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 l("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(o){throw this._context.emitError(o,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 I=["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:camera:started","media:camera:stopped","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"],G=/^[a-z]+:[a-zA-z]+(:[a-zA-Z]+)?$/;class mt{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:camera:started":{public:"media:camera:started"},"inner:camera:stopped":{public:"media:camera:stopped"},"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)),I.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=o=>{try{t(o)}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(I.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 o=s.sanitizer?s.sanitizer(t):t;if(!G.test(n))return;const a=this.listeners.get(n);!a||a.size===0||a.forEach(c=>{try{c(o)}catch(h){i.error("PublicEventEmitter listener error",h)}})}handlePublicEvent(e,t){if(I.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 o=this.listeners.get(e);!o||o.size===0||o.forEach(a=>{try{a(n)}catch(c){i.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,o=t?!!this.httpConnected:!this.httpConnected,a={livekit:s,ws:n,http:o,all:s&&n&&o},c=JSON.stringify(a);this.lastConnectedPayloadJson!==c&&(this.lastConnectedPayloadJson=c,this.inner.emit(`sdk:${e}`,a))}assertAllowed(e){if(I.indexOf(e)===-1)throw new Error(`Event "${e}" is not allowed for public consumers`);if(!G.test(e))throw new Error(`Event name "${e}" does not follow namespace:domain[:action] naming`)}}class ft extends p{constructor(e,t){super(),this._requestCounter=0,this._baseURL=e||"",this._defaultHeaders=t||{},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),o={method:e.method||"GET",headers:n};e.data!==void 0&&(e.method||"GET")!=="GET"&&(typeof e.data=="string"?o.body=e.data:(o.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 o.signal=c.signal,new Promise((h,m)=>{const v=setTimeout(()=>c.abort(),a);fetch(s,o).then(async _=>{clearTimeout(v);const E={};_.headers.forEach((se,ie)=>{E[ie]=se});let R;const P=_.headers.get("content-type");P&&P.includes("application/json")?R=await _.json():R=await _.text();const te={requestId:t,status:_.status,headers:E,data:R,config:e};i.debug(`HTTP request succeeded: ${e.method} ${s} [${t}]`),h(te)}).catch(_=>{clearTimeout(v);const E=_&&_.name==="AbortError",R={requestId:t,status:_&&_.status||void 0,message:E?"Request aborted (timeout)":_&&_.message||"HTTP request failed",error:_,config:e};i.error(`HTTP request failed: ${e.method} ${s} [${t}]`,_),m(R)})})}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[o,a]of Object.entries(t))s.append(o,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 gt(r,e="https"){return r.includes("://")?r:`${e.replace(/:$/,"")}://${r}`}class vt extends g{constructor(e,t){super(),this._httpService=new ft(e||_e,t)}getAuthToken(){if(this.ensureNotDisposed(),this._context.authToken)return this._httpService?.setAuthToken(this._context.authToken),Promise.resolve();throw this._context.emitError("Auth token not found",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||""},headers:this._context.options.sandbox?{"X-Env-Sandbox":"true"}:void 0}).then(e=>{if(e.data.code===b){const t=e.data.data;return this._context.setLivekitConfig(gt(t.livekitUrl,"wss"),t.roomToken,t.roomId),this._context.setWsUrl(`${this._context.options.wsUrlPrefix||me}/${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 l(e.data.msg,d.SDK_GET_LIVEKIT_CONFIG_FAILED)}).catch(e=>{throw this._context.eventBus.emit("inner:sdk:error",{error:new l(e.message,d.SDK_GET_LIVEKIT_CONFIG_FAILED)}),l.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===b)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===b)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===b).finally(()=>{this._context.eventBus.emit("inner:sdk:disconnected",{source:"http"})})}onDispose(){this._httpService.dispose()}}class Et extends p{constructor(e){super(),this._videoController=null,this._audioController=null,this._conversationController=null,this._cameraController=null,this._liveKitService=null,this._conversationTransport=null,this._connectionCoordinator=null,this._httpController=null,this._preConnectPromise=null,this._lastPreConnectSuccessAt=null,this._context=new ge(e),i.info("SDK Client created")}get events(){return this.ensureNotDisposed(),this._publicEventAPI||(this._publicEmitter=new mt(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 l("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 l("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 l("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 l("Audio controller is not available",d.AUDIO_CONTROLLER_NOT_AVAILABLE);this._audioController.setVolume(e)}getVolume(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new l("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 l("Audio controller is not available",d.AUDIO_CONTROLLER_NOT_AVAILABLE);this._audioController.mute()}unmute(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new l("Audio controller is not available",d.AUDIO_CONTROLLER_NOT_AVAILABLE);this._audioController.unmute()}get isMuted(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new l("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}setAuthToken(e){this.ensureNotDisposed(),this._context.setAuthToken(e),this._httpController&&this._httpController.getAuthToken()}async startCamera(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._cameraController)throw new l("Camera controller is not available",d.CAMERA_CONTROLLER_NOT_AVAILABLE);await this._cameraController.startCamera()}stopCamera(){this.ensureNotDisposed(),this._ensureConnected(),this._cameraController&&this._cameraController.stopCamera()}getCameraStream(){return this.ensureNotDisposed(),this._ensureConnected(),this._cameraController?.getStream()??null}getCameraTrack(){return this.ensureNotDisposed(),this._ensureConnected(),this._cameraController?.getTrack()??null}attachCameraTo(e){if(this.ensureNotDisposed(),this._ensureConnected(),!this._cameraController)throw new l("Camera controller is not available",d.CAMERA_CONTROLLER_NOT_AVAILABLE);this._cameraController.attachTo(e)}async preConnect(){if(this.ensureNotDisposed(),this._preConnectPromise)return this._preConnectPromise;const e=Date.now();return this._lastPreConnectSuccessAt!==null&&e-this._lastPreConnectSuccessAt<fe?Promise.resolve(!0):(this._preConnectPromise=(async()=>{try{if(i.info("Creating HTTP controller..."),this._httpController=this._httpController??new vt(this._context.options.http?.baseURL,this._context.options.http?.headers),this._httpController.setContext(this._context),i.info("Setting 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 l("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 Ee,this._connectionCoordinator.setContext(this._context),this._context.bindConnectionState(()=>this._connectionCoordinator.getState()),this._connectionCoordinator.start(),this._connectionCoordinator.startConnecting(),this._videoController=new we(this._context.options.video),this._videoController.setContext(this._context),this._audioController=new qe(this._context.options.audio),this._audioController.setContext(this._context),this._cameraController=new nt,this._cameraController.setContext(this._context),this._conversationController=new dt,this._conversationController.setContext(this._context),this._liveKitService=new ut,this._liveKitService.setContext(this._context),this._conversationTransport=new pt,this._conversationTransport.setContext(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))}if(this._conversationTransport){const e={sendBinary:t=>{this._conversationTransport.sendBinary(t)},isConnected:()=>this._conversationTransport.isConnected};this._audioController&&this._audioController.setWebSocketSender(e),this._cameraController&&this._cameraController.setWebSocketSender(e)}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._cameraController&&this._cameraController.stopCamera(),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 l("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():X(Q("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(),this._cameraController&&this._cameraController.stopCamera()}get isConnected(){return this.ensureNotDisposed(),this._context.sessionState.isConnected}_ensureConnected(){if(!this._context.sessionState.isConnected)throw new l("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._cameraController&&(this._cameraController.dispose(),this._cameraController=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 wt(r){if(!r.avatarName)throw new l("avatarName is required",d.NO_AVATARNAME);return i.info("Creating SDK client with options:",{avatarName:r.avatarName}),new Et(r)}class j extends p{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",o),i.info(`Video loaded: ${s.videoWidth}x${s.videoHeight}`),e()},o=a=>{s.removeEventListener("loadedmetadata",n),s.removeEventListener("error",o),t(new Error(`Failed to load video: ${a}`))};s.addEventListener("loadedmetadata",n),s.addEventListener("error",o),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,o)=>{!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 Ct extends p{constructor(e,t,s){super(),this._outputCanvas=null,this._outputRenderer=null,this._source=new J;const n={...e,renderMode:"processed"};this._pipeline=new ee(this._source,n),this._outputRenderer=new O(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 o=this._outputRenderer.getContext();o&&((this._outputCanvas.width!==n.width||this._outputCanvas.height!==n.height)&&(this._outputCanvas.width=n.width,this._outputCanvas.height=n.height),o.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 Dt extends p{constructor(e,t,s,n){if(super(),this._isRunning=!1,this._videoSource=new j(e,t),this._options={renderMode:"processed",containerElement:t,...n},this._pipelineRunner=new Ct(this._options,t,s),this._options.greenScreen?.enabled){const o=new L(this._options.greenScreen);this._pipelineRunner.setProcessor(o),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,o=new j(e,t);try{await o.load(),n&&await o.play(),o.onFrame(h=>{this._pipelineRunner.processFrame(h).catch(m=>{i.error("Error processing frame:",m)})}),this._videoSource=o;try{s.offFrame(),s.stop(),s.dispose()}catch(h){i.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{o.dispose()}catch(c){i.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},o=new L(n);this._pipelineRunner.setProcessor(o),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{u as ConversationStateType,Rt as DEFAULT_AUDIO_BUFFER_SIZE,At as DEFAULT_AUDIO_CHANNELS,M as DEFAULT_AUDIO_SAMPLE_RATE,y as DEFAULT_RECONNECT_ATTEMPTS,k as DEFAULT_RECONNECT_DELAY,St as DEFAULT_WS_TIMEOUT,p as Disposable,ce as EventBus,C as LogLevel,i as Logger,Et as SDKClient,l as SDKError,he as SessionState,f as SessionStatus,Dt as StandaloneVideoDebugger,wt as createClient};
110
+ `;function We(){const r=new Blob([Ke],{type:"text/javascript"});return URL.createObjectURL(r)}class $e extends Be{constructor(e){super(),this.options=e,this._stream=null,this._audioContext=null,this._sourceNode=null,this._workletNode=null,this._targetSampleRate=M,this._bitDepth=V,this._channels=1;const t=e?.input;this._targetSampleRate=t?.sampleRate||M,this._bitDepth=t?.bitDepth||V,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 o=this._stream.getAudioTracks()[0].getSettings().sampleRate||44100;this._audioContext=new AudioContext({sampleRate:o});const a=We();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:v}=c.data;h==="audioData"&&m&&this.emitAudioData(m,v)},this._sourceNode.connect(this._workletNode),i.info(`Audio capture started: input=${o}Hz -> output=${this._targetSampleRate}Hz, ${this._bitDepth}bit`)}catch(e){throw l.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 He{create(e){return new $e(e)}}class qe extends g{constructor(e,t){super(),this._source=null,this._isCapturing=!1,this._currentOutputTrackId=null,this._inputPipeline=new ye(e),this._outputPipeline=new be,this._frameSender=new Ue(e),this._sourceFactory=t||new He,i.info("Audio Controller created")}setContext(e){super.setContext(e),this._frameSender.setContext(e),this._inputPipeline.onProcessedAudio=(t,s)=>{this._context.eventBus.emit("inner:audio:frame:received",{data:t,sampleRate:s})}}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")}}class ze extends p{constructor(){super(...arguments),this._stream=null,this._videoElement=null}async start(){if(this.ensureNotDisposed(),this._stream){i.warn("Camera capture already started");return}try{const e=await navigator.mediaDevices.getUserMedia({video:!0});if(this._stream=e,!this._videoElement){const t=document.createElement("video");t.autoplay=!0,t.muted=!0,t.playsInline=!0,t.style.position="fixed",t.style.opacity="0",t.style.pointerEvents="none",t.style.width="0",t.style.height="0",t.style.zIndex="-1",typeof document<"u"&&document.body&&document.body.appendChild(t),this._videoElement=t}this._videoElement.srcObject=this._stream,this._videoElement.play().catch(t=>{i.error("Failed to start camera video playback",t)})}catch(e){throw l.fromError(e,d.SDK_ERROR)}}stop(){if(this._stream&&(this._stream.getTracks().forEach(e=>e.stop()),this._stream=null),this._videoElement){this._videoElement.srcObject=null;const e=this._videoElement.parentNode;e&&e.removeChild(this._videoElement),this._videoElement=null}i.info("Camera capture stopped")}getStream(){return this.ensureNotDisposed(),this._stream}getVideoElement(){return this.ensureNotDisposed(),this._videoElement}getTrack(){if(this.ensureNotDisposed(),!this._stream)return null;const e=this._stream.getVideoTracks();return e.length>0?e[0]:null}onDispose(){this.stop()}}const N=640,w=360;class Ge extends p{constructor(e,t){super(),this._videoElement=null,this._canvas=null,this._videoElement=e,this._onFrame=t,this._handleServerCommand=this._onServerCommand.bind(this)}setContext(e){this._context=e}setVideoElement(e){this._videoElement=e}start(){this.ensureNotDisposed(),this._canvas||(typeof OffscreenCanvas<"u"?this._canvas=new OffscreenCanvas(N,w):(this._canvas=document.createElement("canvas"),this._canvas.width=N,this._canvas.height=w)),this._context.eventBus.on("inner:conversation:server:command",this._handleServerCommand)}_onServerCommand({code:e}){e===500&&this._captureFrame()}stop(){this._context.eventBus.off("inner:conversation:server:command",this._handleServerCommand)}_captureFrame(){const e=this._videoElement,t=this._canvas;if(!e||!t||e.readyState<2||e.videoWidth===0||e.videoHeight===0)return;const s=t.getContext("2d");if(!s)return;const n=e.videoWidth,o=e.videoHeight;let a=N,c=N*o/n;c>w&&(c=w,a=w*n/o),t.width!==a&&(t.width=a),t.height!==c&&(t.height=c),s.clearRect(0,0,t.width,t.height),s.drawImage(e,0,0,n,o,0,0,a,c),typeof OffscreenCanvas<"u"&&t instanceof OffscreenCanvas&&typeof t.convertToBlob=="function"?t.convertToBlob({type:"image/jpeg",quality:D}).then(h=>{if(h)try{this._onFrame(h,a,c,0,D)}catch(m){i.error("ScreenshotScheduler onFrame callback error",m)}}).catch(h=>{i.error("ScreenshotScheduler convertToBlob error",h)}):t instanceof HTMLCanvasElement&&t.toBlob(h=>{if(h)try{this._onFrame(h,a,c,0,D)}catch(m){i.error("ScreenshotScheduler onFrame callback error",m)}},"image/jpeg",D)}onDispose(){this.stop(),this._canvas=null,this._videoElement=null}}async function je(r,e){const t=await r.arrayBuffer();return{data:new Uint8Array(t),width:e.width,height:e.height,format:e.format,quality:e.quality}}const Qe=12,Xe=2,Ye=0,B=0,K=4,W=65535,$=4294967295;function Je(r){const e=Math.floor(r);return e<0?0:e>255?255:e}function x(r){const e=Math.floor(r);return e<0?0:e>W?W:e}function Ze(r){const e=Math.floor(r);return e<0?0:e>$?$:e}function et(r){const e=Math.floor(r);return e<B?B:e>K?K:e}function tt(r){const e=new ArrayBuffer(Qe),t=new DataView(e),s=et(r.format)&15,n=Xe<<6|Ye<<4|s;return t.setUint8(0,n),t.setUint8(1,Je(r.quality)),t.setUint16(2,x(r.id),!1),t.setUint16(4,x(r.width),!1),t.setUint16(6,x(r.height),!1),t.setUint32(8,Ze(r.payloadLength),!1),new Uint8Array(e)}const st=65535;class it extends p{constructor(){super(...arguments),this._wsSender=null,this._imageId=0}setWebSocketSender(e){this._wsSender=e}send(e,t){if(this.ensureNotDisposed(),!this._wsSender||!this._wsSender.isConnected())return;const s=e.byteLength,n=tt({format:t.format,quality:t.quality,id:this._nextId(),width:t.width,height:t.height,payloadLength:s}),o=new Uint8Array(n.byteLength+s);o.set(n,0),o.set(e,n.byteLength),this._wsSender.sendBinary(o)}_nextId(){const e=this._imageId;return this._imageId=this._imageId>=st?0:this._imageId+1,e}onDispose(){this._wsSender=null}}class nt extends g{constructor(){super(),this._isRunning=!1,this._source=new ze,this._sender=new it;const e=(t,s,n,o,a=D)=>{je(t,{width:s,height:n,format:o,quality:a}).then(c=>{this._sender.send(c.data,{width:c.width,height:c.height,format:c.format,quality:c.quality})})};this._scheduler=new Ge(null,e)}setContext(e){super.setContext(e),this._scheduler.setContext(e)}async startCamera(){if(this.ensureNotDisposed(),this._isRunning){i.warn("Camera already running");return}await this._source.start();const e=this._source.getVideoElement();this._scheduler.setVideoElement(e),this._scheduler.start(),this._isRunning=!0,this._context.eventBus.emit("inner:camera:started",void 0),i.info("Camera started")}stopCamera(){this.ensureNotDisposed(),this._isRunning&&(this._scheduler.stop(),this._source.stop(),this._isRunning=!1,this._context.eventBus.emit("inner:camera:stopped",void 0),i.info("Camera stopped"))}isCameraRunning(){return this._isRunning}getStream(){return this.ensureNotDisposed(),this._source.getStream()}getTrack(){return this.ensureNotDisposed(),this._source.getTrack()}attachTo(e){this.ensureNotDisposed();const t=this._source.getStream();t&&(e.srcObject=t,e.play().catch(s=>i.error("attachTo play failed",s)))}setWebSocketSender(e){this._sender.setWebSocketSender(e)}onDispose(){this.stopCamera(),this._source.dispose(),this._scheduler.dispose(),this._sender.dispose()}}var u;(function(r){r.IDLE="idle",r.WAITING="waiting",r.STREAMING="streaming",r.COMPLETED="completed",r.ERROR="error"})(u||(u={}));class rt extends Y{constructor(){super(...arguments),this._currentState=u.IDLE,this.transitionTable=[{from:u.IDLE,to:[u.WAITING,u.ERROR]},{from:u.WAITING,to:[u.STREAMING,u.ERROR]},{from:u.STREAMING,to:[u.COMPLETED,u.ERROR]},{from:u.COMPLETED,to:[u.IDLE,u.WAITING,u.ERROR]},{from:u.ERROR,to:[u.IDLE]}]}reset(){this._currentState=u.IDLE}}class H extends p{constructor(e){super(),this._currentQuestion=null,this._currentAnswer=null,this._sessionId=e,this._stateMachine=new rt,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(u.WAITING),this._syncContextState()}get currentQuestion(){return this._currentQuestion}_startAnswer(e){this._currentAnswer={questionId:e,text:"",isComplete:!1,timestamp:Date.now()},this._stateMachine.transitionTo(u.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(u.COMPLETED),this._syncContextState()}get currentAnswer(){return this._currentAnswer}_setError(e){this._stateMachine.transitionTo(u.ERROR),this._syncContextState()}_syncContextState(){}handleEvent(e){const t=this._stateMachine.getCurrentState();switch(e.type){case"QUESTION":if(t!==u.IDLE&&t!==u.COMPLETED){i.warn(`Ignoring QUESTION event in invalid state: ${t}`);return}this._setQuestion(e.payload);break;case"ANSWER_START":if(t!==u.WAITING){i.warn(`Ignoring ANSWER_START event in invalid state: ${t}. Expected: WAITING`);return}this._startAnswer(e.payload);break;case"ANSWER_CHUNK":if(t!==u.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!==u.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 ot extends p{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 at{generateSessionId(){return`session_${Date.now()}_${Math.random().toString(36).substring(2,9)}`}generateQuestionId(){return Math.random().toString(36).substring(2,6)}}class ct extends g{constructor(e){super(),this._transport=null,this._sessions=new Map,this._assembler=new ot,this._idService=new at,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"},o=this._idService.generateSessionId(),a=new H(o);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===u.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 H(s);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})}handleServerCommand(e,t){this.ensureNotDisposed(),this._context.eventBus.emit("inner:conversation:server:command",{code:e,command: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 dt extends g{constructor(e){super(),this._manager=new ct(e),i.info("Conversation Controller created")}setContext(e){super.setContext(e),this._manager.setContext(e)}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 ht extends p{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 o=n.mediaStreamTrack;if(o.kind==="video"&&(e=!0,o.readyState==="live"&&(t=!0),o.enabled&&(s=!0),o.readyState==="live"&&o.enabled))return{available:!0}}return e?t?s?{available:!1,reason:A.NO_VIDEO_TRACK}:{available:!1,reason:A.TRACK_MUTED}:{available:!1,reason:A.TRACK_ENDED}:{available:!1,reason:A.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 q extends g{constructor(e){super(),this._videoController=null,this._room=null,this._trackRegistry=e,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)},le),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 z extends g{constructor(e){super(),this._audioController=null,this._room=null,this._trackRegistry=e,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 lt extends g{constructor(e){super(),this._handlers=[],this._room=null,this._trackRegistry=e,i.info("LiveKit Event Adapter created")}setContext(e){if(super.setContext(e),this._handlers.length===0){const t=new q(this._trackRegistry),s=new z(this._trackRegistry);t.setContext(e),s.setContext(e),this._handlers.push(t,s),this._room&&(t.setRoom(this._room),s.setRoom(this._room))}}setRoom(e){this._room=e,this._handlers.forEach(t=>{t.setRoom(e)}),i.info("LiveKit room set:",this._room)}setVideoController(e){const t=this._handlers.find(s=>s instanceof q);t&&t.setVideoController(e)}setAudioController(e){const t=this._handlers.find(s=>s instanceof z);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 ut extends g{constructor(){super(),this._room=null,this._maxAttempts=y,this._maxDelay=k*T,this._trackRegistry=new ht,this._eventAdapter=new lt(this._trackRegistry),i.info("LiveKit Service created")}setContext(e){super.setContext(e),this._maxAttempts=e.options.reconnect?.maxAttempts||y,this._maxDelay=(e.options.reconnect?.delay??k)*T,this._eventAdapter.setContext(e)}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 o=new ne({reconnectPolicy:{nextRetryDelayInMs:a=>a.elapsedMs<this._maxDelay?Math.min(T*Math.pow(2,a.retryCount),this._maxDelay):null}});o.prepareConnection(e,t),o.on(S.TrackSubscribed,(a,c,h)=>{this._handleTrackSubscribed(a,c,h)}),o.on(S.TrackUnsubscribed,(a,c,h)=>{this._handleTrackUnsubscribed(a,c,h)}),o.on(S.TrackMuted,(a,c)=>{this._handleTrackMuted(a,c)}),o.on(S.TrackUnmuted,(a,c)=>{this._handleTrackUnmuted(a,c)}),o.on(S.Connected,()=>{i.info("Connected to LiveKit room"),this._context.eventBus.emit("inner:rtc:connected",void 0)}),o.on(S.Disconnected,a=>{a?i.info(`LiveKit room disconnected: ${a}`):i.info("LiveKit room disconnected"),a!==re.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 o.connect(e,t,{autoSubscribe:!0,maxRetries:this._context.options.reconnect?.maxAttempts||y}),this._eventAdapter.setRoom(o),this._room=o}catch(o){throw l.fromError(o,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(oe.IsSpeakingChanged,c=>{this._context.eventBus.emit("inner:audio:speaking:changed",{participantId:s.identity,isSpeaking:c}),i.info(`Participant ${s.identity} is speaking: ${c}`)});const o=t.trackSid,a=s.identity;this._eventAdapter.handleTrackSubscribed(e,o,a)}catch(n){i.error("Error handling track subscribed:",n)}}_handleTrackUnsubscribed(e,t,s){try{const n=t,o=s,a=n.trackSid,c=o.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===ae.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 _t extends g{constructor(){super(),this._conversationManager=null,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 200) for uid: ${t}`)):n===201?(this._conversationManager.handleAnswerChunk(t,"",!0),i.debug(`Answer completed (code 201) for uid: ${t}`)):n===300?(this._conversationManager.handleAsrTextReceived(t,s),i.debug(`ASR text chunk received (code 300) for uid: ${t}`)):n===400?(this._conversationManager.handleServerInitiatedMessage(t,s),i.debug(`Server initiated message received (code 300) for uid: ${t}`)):n===500&&(this._conversationManager.handleServerCommand(n,s),i.debug(`Server command received (code 500) for uid: ${t}`))}onDispose(){i.info("WebSocket Dispatcher disposed")}}class pt extends g{constructor(){super(),this._ws=null,this._dispatcher=null,this._isConnecting=!1,this._activeTimers=new Set,this._retryCount=0,this._maxRetries=y,this._pingTimer=void 0,this._pongTimer=void 0,this._isExplicitDisconnect=!1,this._maxDelay=k*T}setContext(e){super.setContext(e),this._maxRetries=e.options.reconnect?.maxAttempts??y,this._maxDelay=(e.options.reconnect?.delay??k)*T,this._dispatcher&&(this._dispatcher.dispose(),this._dispatcher=null),this._dispatcher=new _t,this._dispatcher.setContext(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 l("WebSocket connection error",d.WS_CONNECT_ERROR,s)}),t(new l("WebSocket connection error",d.WS_CONNECT_ERROR,s))},this._ws.onclose=s=>{const n=s.code,o=s.reason?s.reason:s.wasClean?"Clean close":"Closed by remote";this._handleDisconnect(n,o)},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 o=setTimeout(()=>{!this._isExplicitDisconnect&&!this.isDisposed&&this.connect().catch(()=>{})},n);this._registerTimer(o)}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 (${F/T}s). Reconnecting...`),this._ws?.close()},F)}handlePongReceived(){clearTimeout(this._pongTimer),this._clearTimer(this._pingTimer),this._pingTimer=setTimeout(()=>{this.sendPing()},ue)}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 l("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(o){throw this._context.emitError(o,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 I=["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:camera:started","media:camera:stopped","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"],G=/^[a-z]+:[a-zA-z]+(:[a-zA-Z]+)?$/;class mt{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:camera:started":{public:"media:camera:started"},"inner:camera:stopped":{public:"media:camera:stopped"},"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)),I.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=o=>{try{t(o)}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(I.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 o=s.sanitizer?s.sanitizer(t):t;if(!G.test(n))return;const a=this.listeners.get(n);!a||a.size===0||a.forEach(c=>{try{c(o)}catch(h){i.error("PublicEventEmitter listener error",h)}})}handlePublicEvent(e,t){if(I.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 o=this.listeners.get(e);!o||o.size===0||o.forEach(a=>{try{a(n)}catch(c){i.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,o=t?!!this.httpConnected:!this.httpConnected,a={livekit:s,ws:n,http:o,all:s&&n&&o},c=JSON.stringify(a);this.lastConnectedPayloadJson!==c&&(this.lastConnectedPayloadJson=c,this.inner.emit(`sdk:${e}`,a))}assertAllowed(e){if(I.indexOf(e)===-1)throw new Error(`Event "${e}" is not allowed for public consumers`);if(!G.test(e))throw new Error(`Event name "${e}" does not follow namespace:domain[:action] naming`)}}class ft extends p{constructor(e,t){super(),this._requestCounter=0,this._baseURL=e||"",this._defaultHeaders=t||{},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),o={method:e.method||"GET",headers:n};e.data!==void 0&&(e.method||"GET")!=="GET"&&(typeof e.data=="string"?o.body=e.data:(o.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 o.signal=c.signal,new Promise((h,m)=>{const v=setTimeout(()=>c.abort(),a);fetch(s,o).then(async _=>{clearTimeout(v);const E={};_.headers.forEach((se,ie)=>{E[ie]=se});let R;const P=_.headers.get("content-type");P&&P.includes("application/json")?R=await _.json():R=await _.text();const te={requestId:t,status:_.status,headers:E,data:R,config:e};i.debug(`HTTP request succeeded: ${e.method} ${s} [${t}]`),h(te)}).catch(_=>{clearTimeout(v);const E=_&&_.name==="AbortError",R={requestId:t,status:_&&_.status||void 0,message:E?"Request aborted (timeout)":_&&_.message||"HTTP request failed",error:_,config:e};i.error(`HTTP request failed: ${e.method} ${s} [${t}]`,_),m(R)})})}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[o,a]of Object.entries(t))s.append(o,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 gt(r,e="https"){return r.includes("://")?r:`${e.replace(/:$/,"")}://${r}`}class vt extends g{constructor(e,t){super(),this._httpService=new ft(e||_e,t)}getAuthToken(){if(this.ensureNotDisposed(),this._context.authToken)return this._httpService?.setAuthToken(this._context.authToken),Promise.resolve();throw this._context.emitError("Auth token not found",d.SDK_AUTH_TOKEN_FAILED)}getLivekitConfig(){return this.ensureNotDisposed(),this._httpService?.request({url:"/stream/v1/start",method:"POST",data:{avatarId:this._context.options.avatarName,voice:this._context.options.avatarVoice||""},headers:this._context.options.sandbox?{"X-Env-Sandbox":"true"}:void 0}).then(e=>{if(e.data.code===b){const t=e.data.data;return this._context.setLivekitConfig(gt(t.livekitUrl,"wss"),t.roomToken,t.roomId),this._context.setWsUrl(`${this._context.options.wsUrlPrefix||me}/${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 l(e.data.msg,d.SDK_GET_LIVEKIT_CONFIG_FAILED)}).catch(e=>{throw this._context.eventBus.emit("inner:sdk:error",{error:new l(e.message,d.SDK_GET_LIVEKIT_CONFIG_FAILED)}),l.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===b)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===b)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===b).finally(()=>{this._context.eventBus.emit("inner:sdk:disconnected",{source:"http"})})}onDispose(){this._httpService.dispose()}}class Et extends p{constructor(e){super(),this._videoController=null,this._audioController=null,this._conversationController=null,this._cameraController=null,this._liveKitService=null,this._conversationTransport=null,this._connectionCoordinator=null,this._httpController=null,this._preConnectPromise=null,this._lastPreConnectSuccessAt=null,this._context=new ge(e),i.info("SDK Client created")}get events(){return this.ensureNotDisposed(),this._publicEventAPI||(this._publicEmitter=new mt(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 l("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 l("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 l("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 l("Audio controller is not available",d.AUDIO_CONTROLLER_NOT_AVAILABLE);this._audioController.setVolume(e)}getVolume(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new l("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 l("Audio controller is not available",d.AUDIO_CONTROLLER_NOT_AVAILABLE);this._audioController.mute()}unmute(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new l("Audio controller is not available",d.AUDIO_CONTROLLER_NOT_AVAILABLE);this._audioController.unmute()}get isMuted(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new l("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}setAuthToken(e){this.ensureNotDisposed(),this._context.setAuthToken(e),this._httpController&&this._httpController.getAuthToken()}async startCamera(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._cameraController)throw new l("Camera controller is not available",d.CAMERA_CONTROLLER_NOT_AVAILABLE);await this._cameraController.startCamera()}stopCamera(){this.ensureNotDisposed(),this._ensureConnected(),this._cameraController&&this._cameraController.stopCamera()}getCameraStream(){return this.ensureNotDisposed(),this._ensureConnected(),this._cameraController?.getStream()??null}getCameraTrack(){return this.ensureNotDisposed(),this._ensureConnected(),this._cameraController?.getTrack()??null}attachCameraTo(e){if(this.ensureNotDisposed(),this._ensureConnected(),!this._cameraController)throw new l("Camera controller is not available",d.CAMERA_CONTROLLER_NOT_AVAILABLE);this._cameraController.attachTo(e)}async preConnect(){if(this.ensureNotDisposed(),this._preConnectPromise)return this._preConnectPromise;const e=Date.now();return this._lastPreConnectSuccessAt!==null&&e-this._lastPreConnectSuccessAt<fe?Promise.resolve(!0):(this._preConnectPromise=(async()=>{try{if(i.info("Creating HTTP controller..."),this._httpController=this._httpController??new vt(this._context.options.http?.baseURL,this._context.options.http?.headers),this._httpController.setContext(this._context),i.info("Setting 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 l("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 Ee,this._connectionCoordinator.setContext(this._context),this._context.bindConnectionState(()=>this._connectionCoordinator.getState()),this._connectionCoordinator.start(),this._connectionCoordinator.startConnecting(),this._videoController=new we(this._context.options.video),this._videoController.setContext(this._context),this._audioController=new qe(this._context.options.audio),this._audioController.setContext(this._context),this._cameraController=new nt,this._cameraController.setContext(this._context),this._conversationController=new dt,this._conversationController.setContext(this._context),this._liveKitService=new ut,this._liveKitService.setContext(this._context),this._conversationTransport=new pt,this._conversationTransport.setContext(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))}if(this._conversationTransport){const e={sendBinary:t=>{this._conversationTransport.sendBinary(t)},isConnected:()=>this._conversationTransport.isConnected};this._audioController&&this._audioController.setWebSocketSender(e),this._cameraController&&this._cameraController.setWebSocketSender(e)}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._cameraController&&this._cameraController.stopCamera(),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 l("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():X(Q("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(),this._cameraController&&this._cameraController.stopCamera()}get isConnected(){return this.ensureNotDisposed(),this._context.sessionState.isConnected}_ensureConnected(){if(!this._context.sessionState.isConnected)throw new l("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._cameraController&&(this._cameraController.dispose(),this._cameraController=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 wt(r){if(!r.avatarName)throw new l("avatarName is required",d.NO_AVATARNAME);return i.info("Creating SDK client with options:",{avatarName:r.avatarName}),new Et(r)}class j extends p{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",o),i.info(`Video loaded: ${s.videoWidth}x${s.videoHeight}`),e()},o=a=>{s.removeEventListener("loadedmetadata",n),s.removeEventListener("error",o),t(new Error(`Failed to load video: ${a}`))};s.addEventListener("loadedmetadata",n),s.addEventListener("error",o),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,o)=>{!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 Ct extends p{constructor(e,t,s){super(),this._outputCanvas=null,this._outputRenderer=null,this._source=new J;const n={...e,renderMode:"processed"};this._pipeline=new ee(this._source,n),this._outputRenderer=new O(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 o=this._outputRenderer.getContext();o&&((this._outputCanvas.width!==n.width||this._outputCanvas.height!==n.height)&&(this._outputCanvas.width=n.width,this._outputCanvas.height=n.height),o.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 Dt extends p{constructor(e,t,s,n){if(super(),this._isRunning=!1,this._videoSource=new j(e,t),this._options={renderMode:"processed",containerElement:t,...n},this._pipelineRunner=new Ct(this._options,t,s),this._options.greenScreen?.enabled){const o=new L(this._options.greenScreen);this._pipelineRunner.setProcessor(o),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,o=new j(e,t);try{await o.load(),n&&await o.play(),o.onFrame(h=>{this._pipelineRunner.processFrame(h).catch(m=>{i.error("Error processing frame:",m)})}),this._videoSource=o;try{s.offFrame(),s.stop(),s.dispose()}catch(h){i.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{o.dispose()}catch(c){i.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},o=new L(n);this._pipelineRunner.setProcessor(o),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{u as ConversationStateType,Rt as DEFAULT_AUDIO_BUFFER_SIZE,At as DEFAULT_AUDIO_CHANNELS,M as DEFAULT_AUDIO_SAMPLE_RATE,y as DEFAULT_RECONNECT_ATTEMPTS,k as DEFAULT_RECONNECT_DELAY,St as DEFAULT_WS_TIMEOUT,p as Disposable,ce as EventBus,C as LogLevel,i as Logger,Et as SDKClient,l as SDKError,he as SessionState,f as SessionStatus,Dt as StandaloneVideoDebugger,wt as createClient};