@sanseng/livekit-ws-sdk 0.1.20 → 1.0.0
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/LICENSE +201 -0
- package/README.md +21 -1
- package/dist/index.cjs.js +4 -4
- package/dist/index.d.ts +221 -115
- package/dist/index.esm.js +4 -4
- package/dist/index.full.umd.js +8 -8
- package/dist/index.umd.js +1 -1
- package/package.json +2 -2
package/dist/index.esm.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import{Room as ne,RoomEvent as S,DisconnectReason as re,ParticipantEvent as oe,ConnectionState as ae}from"livekit-client";var d;(function(r){r.SDK_AUTH_TOKEN_FAILED="SDK_AUTH_TOKEN_FAILED",r.SDK_GET_LIVEKIT_CONFIG_FAILED="SDK_GET_LIVEKIT_CONFIG_FAILED",r.SDK_RECONNECT_FAILED="SDK_RECONNECT_FAILED",r.SDK_DISCONNECT_FAILED="SDK_DISCONNECT_FAILED",r.SDK_NOT_CONNECTED="SDK_NOT_CONNECTED",r.SDK_CONNECT_FAILED="SDK_CONNECT_FAILED",r.SDK_SWITCH_VIDEO_FAILED="SDK_SWITCH_VIDEO_FAILED",r.SDK_ERROR="SDK_ERROR",r.SDK_PRECONNECT_FAILED="SDK_PRECONNECT_FAILED",r.SDK_INTERRUPT_CONVERSATION_FAILED="SDK_INTERRUPT_CONVERSATION_FAILED",r.SDK_INITIALIZATION_FAILED="SDK_INITIALIZATION_FAILED",r.SDK_INVALID_STATE_TRANSITION="SDK_INVALID_STATE_TRANSITION",r.NO_AVATARNAME="NO_AVATARNAME",r.AUDIO_CAPTURE_START_FAILED="AUDIO_CAPTURE_START_FAILED",r.AUDIO_CAPTURE_FAILED="AUDIO_CAPTURE_FAILED",r.AUDIO_INVALID_SAMPLE_RATE="AUDIO_INVALID_SAMPLE_RATE",r.AUDIO_INVALID_CHANNEL="AUDIO_INVALID_CHANNEL",r.AUDIO_INVALID_HEADER_LENGTH="AUDIO_INVALID_HEADER_LENGTH",r.AUDIO_INVALID_TYPE="AUDIO_INVALID_TYPE",r.AUDIO_INVALID_RESERVED="AUDIO_INVALID_RESERVED",r.AUDIO_CONTROLLER_NOT_AVAILABLE="AUDIO_CONTROLLER_NOT_AVAILABLE",r.CAMERA_CONTROLLER_NOT_AVAILABLE="CAMERA_CONTROLLER_NOT_AVAILABLE",r.WS_CONNECT_FAILED="WS_CONNECT_FAILED",r.WS_TEXT_SEND_FAILED="WS_TEXT_SEND_FAILED",r.WS_PARSE_ERROR="WS_PARSE_ERROR",r.WS_NOT_CONNECTED="WS_NOT_CONNECTED",r.WS_CONNECT_ERROR="WS_CONNECT_ERROR",r.LIVEKIT_CONNECT_FAILED="LIVEKIT_CONNECT_FAILED",r.CONVERSATION_CONTROLLER_NOT_AVAILABLE="CONVERSATION_CONTROLLER_NOT_AVAILABLE",r.STATE_MACHINE_INVALID_STATE_TRANSITION="STATE_MACHINE_INVALID_STATE_TRANSITION",r.OBJECT_DISPOSED="OBJECT_DISPOSED",r.HTTP_CONTROLLER_NOT_AVAILABLE="HTTP_CONTROLLER_NOT_AVAILABLE"})(d||(d={}));class l extends Error{constructor(e,t="SDK_ERROR",s){super(e),this.name="SDKError",this.code=t,this.cause=s,Object.setPrototypeOf(this,l.prototype)}static fromError(e,t){return e instanceof l?e:e instanceof Error?new l(e.message,t||d.SDK_ERROR,e):typeof e=="string"?new l(e,t||d.SDK_ERROR):new l("Unknown error",t||d.SDK_ERROR,e)}}class p{constructor(){this._disposed=!1}get isDisposed(){return this._disposed}dispose(){this._disposed||(this.onDispose(),this._disposed=!0)}ensureNotDisposed(){if(this._disposed)throw new l("Object has been disposed",d.OBJECT_DISPOSED)}}var C;(function(r){r[r.DEBUG=0]="DEBUG",r[r.INFO=1]="INFO",r[r.WARN=2]="WARN",r[r.ERROR=3]="ERROR",r[r.NONE=4]="NONE"})(C||(C={}));class i{static setLevel(e){this._level=e}static setPrefix(e){this._prefix=e}static debug(e,...t){this._level<=C.DEBUG&&console.debug(`${this._prefix} [DEBUG]`,e,...t)}static info(e,...t){this._level<=C.INFO&&console.info(`${this._prefix} [INFO]`,e,...t)}static warn(e,...t){this._level<=C.WARN&&console.warn(`${this._prefix} [WARN]`,e,...t)}static error(e,...t){this._level<=C.ERROR&&console.error(`${this._prefix} [ERROR]`,e,...t)}}i._level=C.ERROR;i._prefix="[SDK]";class ce extends p{constructor(){super(...arguments),this._listeners=new Map}on(e,t){this.ensureNotDisposed(),this._listeners.has(e)||this._listeners.set(e,new Set);const s=this._listeners.get(e);return s.add(t),i.debug(`Event listener added for: ${String(e)}`),()=>{s.delete(t),i.debug(`Event listener removed for: ${String(e)}`)}}once(e,t){this.ensureNotDisposed();const s=n=>{t(n),this.off(e,s)};return this.on(e,s)}off(e,t){this.ensureNotDisposed();const s=this._listeners.get(e);s&&s.delete(t)}emit(e,t){this.ensureNotDisposed();const s=this._listeners.get(e);s&&s.size>0&&(i.debug(`Emitting event: ${String(e)}`,t),s.forEach(n=>{try{n(t)}catch(o){i.error(`Error in event listener for ${String(e)}:`,o)}}))}removeAllListeners(){this._listeners.clear(),i.debug("All event listeners removed")}listenerCount(e){const t=this._listeners.get(e);return t?t.size:0}onDispose(){this.removeAllListeners()}}var f;(function(r){r.IDLE="idle",r.CONNECTING="connecting",r.CONNECTED="connected",r.PARTIAL="partial",r.DISCONNECTED="disconnected",r.RECONNECTING="reconnecting",r.DISCONNECTING="disconnecting",r.ERROR="error",r.DISPOSED="disposed"})(f||(f={}));function de(r){switch(r){case"idle":return f.IDLE;case"connecting":return f.CONNECTING;case"connected":return f.CONNECTED;case"partial":return f.PARTIAL;case"disconnecting":return f.DISCONNECTING;case"disconnected":case"error":return f.DISCONNECTED;case"reconnecting:auto":case"reconnecting:manual":return f.RECONNECTING;case"disposed":return f.DISPOSED;default:return f.DISCONNECTED}}class he{constructor(){this.status=f.DISCONNECTED,this.state=f.DISCONNECTED,this.isConnected=!1}reset(){this.status=f.DISCONNECTED,this.state=f.DISCONNECTED,this.isConnected=!1}}const y=3,le=5e3,T=1e3,k=10,St=1e4,ue=3e3,F=3e4,M=24e3,V=16,At=1,Rt=4096,b=0,_e="https://api.newportai.com/s2/aigc/api/vih_dispatcher",pe="wss://api.newportai.com/s2/livekit-lisbon",me="wss://api.newportai.com/s2/aigc/api/ws_dispatch/render/v1/stream",fe=6e4;var A;(function(r){r.NO_VIDEO_TRACK="NO_VIDEO_TRACK",r.TRACK_MUTED="TRACK_MUTED",r.TRACK_ENDED="TRACK_ENDED"})(A||(A={}));const D=.7;class ge{constructor(e){this._connectionStateGetter=null,this.options=e,this._livekitUrl="",this._token="",this._wsUrl="",this.eventBus=new ce,this.sessionState=new he,e.debug&&i.setLevel(0),i.info("SDK Context initialized")}setLivekitConfig(e,t,s){this._livekitUrl=e||pe,this._token=t,this._roomId=s}setAuthToken(e){this._httpToken=e}get authToken(){return this._httpToken}emitError(e,t){const s=l.fromError(e,t);return this.eventBus.emit("inner:sdk:error",{error:s}),s}setVideoOptions(e){this.options.video=Object.assign({},e,this.options.video||{})}setWsUrl(e){this._wsUrl=e}get wsUrl(){return this._wsUrl}get livekitUrl(){return this._livekitUrl}get token(){return this._token}get roomId(){return this._roomId}getLivekitConfig(){return{livekitUrl:this._livekitUrl,token:this._token}}bindConnectionState(e){this._connectionStateGetter=e}get connectionState(){return this._connectionStateGetter?.()}clearConnectionStateBinding(){this._connectionStateGetter=null}dispose(){this.clearConnectionStateBinding(),this.eventBus.dispose(),this.sessionState.reset(),i.info("SDK Context disposed")}}class g extends p{setContext(e){this._context=e}}function Q(r){return{ws:{connected:!1},rtc:{connected:!1,hasVideoTrack:!1},overall:{state:r}}}function X(r){return Object.freeze({ws:Object.freeze({...r.ws}),rtc:Object.freeze({...r.rtc}),overall:Object.freeze({...r.overall})})}class Y{constructor(){this._transitionMap=null}get transitionMap(){if(!this._transitionMap){const e=new Map;for(const t of this.transitionTable)e.set(t.from,new Set(t.to));this._transitionMap=e}return this._transitionMap}getCurrentState(){return this._currentState}transitionTo(e,t){const s=this._currentState;if(!this.canTransition(s,e)){const n=this.transitionMap.get(s),o=n?Array.from(n).join(", "):"none";throw t?.error??new l(`Invalid state transition: cannot transition from "${s}" to "${e}". Current state: "${s}". Allowed transitions: [${o}]`,d.STATE_MACHINE_INVALID_STATE_TRANSITION)}this._currentState=e}canTransition(e,t){return e===t?!1:this.transitionMap.get(e)?.has(t)??!1}reset(){throw new Error("Method not implemented.")}}class ve extends Y{constructor(){super(...arguments),this._currentState="idle",this.transitionTable=[{from:"idle",to:["connecting","disposed"]},{from:"connecting",to:["connected","partial","error","idle","disposed"]},{from:"connected",to:["disconnecting","partial","reconnecting:auto","reconnecting:manual","error","disposed"]},{from:"partial",to:["connected","disconnecting","reconnecting:auto","reconnecting:manual","error","disposed"]},{from:"reconnecting:auto",to:["connected","partial","error","idle","disposed"]},{from:"reconnecting:manual",to:["connected","partial","error","idle","disposed"]},{from:"disconnecting",to:["disconnected","error","disposed"]},{from:"disconnected",to:["reconnecting:manual","reconnecting:auto","idle","disposed"]},{from:"error",to:["reconnecting:manual","reconnecting:auto","idle","disposed"]},{from:"disposed",to:[]}]}reset(){this._currentState="idle"}}class Ee extends g{constructor(){super(),this._unsubscribes=[],this._fsm=new ve,this._snapshot=Q("idle")}start(){this.ensureNotDisposed();const e=this._context.eventBus,t=e.on("inner:rtc:connected",()=>{this._snapshot.rtc.connected=!0,this._context.eventBus.emit("inner:sdk:connected",{source:"livekit"}),this._onFactsChanged()}),s=e.on("inner:rtc:disconnected",h=>{this._snapshot.rtc.connected=!1,this._snapshot.rtc.hasVideoTrack=!1,this._context.eventBus.emit("inner:sdk:disconnected",{source:"livekit",reason:h?.reason}),this._onFactsChanged()}),n=e.on("inner:rtc:video:available",()=>{this._snapshot.rtc.hasVideoTrack=!0}),o=e.on("inner:rtc:video:unavailable",()=>{this._snapshot.rtc.hasVideoTrack=!1}),a=e.on("inner:ws:connected",()=>{this._snapshot.ws.connected=!0,this._context.eventBus.emit("inner:sdk:connected",{source:"ws"}),this._onFactsChanged()}),c=e.on("inner:ws:disconnected",h=>{this._snapshot.ws.connected=!1,this._context.eventBus.emit("inner:sdk:disconnected",{source:"ws",reason:h?.reason}),this._onFactsChanged()});this._unsubscribes=[t,s,n,o,a,c]}startConnecting(){this._transitionTo("connecting")}startDisconnecting(){this._transitionTo("disconnecting")}completeDisconnecting(){this._snapshot.ws.connected=!1,this._snapshot.rtc.connected=!1,this._snapshot.rtc.hasVideoTrack=!1,this._transitionTo("disconnected")}captureSnapshotAndStartReconnectingManual(){const e=this.getSnapshot();return this._transitionTo("reconnecting:manual"),e}transitionToIdle(){this._snapshot.ws.connected=!1,this._snapshot.rtc.connected=!1,this._snapshot.rtc.hasVideoTrack=!1,this._transitionTo("idle")}transitionToDisposed(){this._transitionTo("disposed")}getSnapshot(){return X(this._snapshot)}getState(){return this._fsm.getCurrentState()}canStartReconnectingManual(){const e=this._fsm.getCurrentState();return e==="connected"||e==="partial"||e==="disconnected"||e==="error"}_onFactsChanged(){const e=this._fsm.getCurrentState();if(e==="connecting"||e==="reconnecting:auto"||e==="reconnecting:manual"){const t=this._snapshot.rtc.connected&&this._snapshot.ws.connected,s=this._snapshot.rtc.connected||this._snapshot.ws.connected;if(t){this._transitionTo("connected");return}if(s){this._transitionTo("partial");return}}if(e==="connected"||e==="partial"){const t=this._snapshot.rtc.connected&&this._snapshot.ws.connected;if(!(this._snapshot.rtc.connected||this._snapshot.ws.connected)){this._transitionTo("disconnected");return}if(!t){this._transitionTo("partial");return}if(t&&e==="partial"){this._transitionTo("connected");return}}}_transitionTo(e){this._fsm.canTransition(this._fsm.getCurrentState(),e)&&(this._fsm.transitionTo(e),this._snapshot.overall.state=this._fsm.getCurrentState(),this._syncSessionState())}_syncSessionState(){const e=this._fsm.getCurrentState();this._context.sessionState.status=de(e),this._context.sessionState.state=this._context.sessionState.status,this._context.sessionState.isConnected=e==="connected"}onDispose(){this._unsubscribes.forEach(e=>e()),this._unsubscribes=[]}}class J extends p{get trackId(){return this._trackId}get participantId(){return this._participantId}get track(){return this._track}constructor(){super(),this._trackId=null,this._participantId=null,this._track=null,this._stream=null,this._videoElement=document.createElement("video"),this._videoElement.autoplay=!0,this._videoElement.playsInline=!0,this._videoElement.muted=!0,this._videoElement.crossOrigin="anonymous",this._videoElement.style.display="none",i.info("Video Source initialized with internal video element")}getInternalElement(){return this._videoElement}setTrack(e,t,s){this.ensureNotDisposed(),this._track&&this._track.stop(),this._track=e,this._trackId=t,this._participantId=s,this._stream||(this._stream=new MediaStream);const n=this._stream.getTracks();for(const o of n)if(o!==e)try{this._stream.removeTrack(o)}catch(a){i.warn("Failed to remove track from stream",a)}if(!this._stream.getTracks().includes(e))try{this._stream.addTrack(e)}catch(o){i.error("Failed to add track to media stream:",o)}i.info("Video track ready")}clearTrack(){if(this._stream){const e=this._stream.getTracks();for(const t of e)try{this._stream.removeTrack(t)}catch(s){i.warn("Failed to remove track from stream",s)}}this._track&&(this._track.stop(),this._track=null),this._trackId=null,this._participantId=null,i.info("Video track cleared")}get hasTrack(){return this._track!==null&&this._track.readyState==="live"}onDispose(){this.clearTrack(),this._videoElement&&this._videoElement.parentElement&&this._videoElement.parentElement.removeChild(this._videoElement),this._stream&&(this._stream.getTracks().forEach(e=>e.stop()),this._stream=null),i.info("Video Source disposed")}}class Ce extends p{constructor(){super(...arguments),this._renderer=null}getRenderer(){return this._renderer}setRenderer(e){this._renderer&&this._renderer.dispose(),this._renderer=e,i.debug("Raw render strategy renderer set")}getProcessors(){return[]}setProcessors(e){i.warn("Raw render strategy does not support processors")}attachToContainer(e){if(!this._renderer){i.warn("No renderer available to attach to container");return}if("getElement"in this._renderer&&typeof this._renderer.getElement=="function"){const t=this._renderer.getElement();t instanceof HTMLVideoElement&&(t.parentElement&&t.parentElement!==e&&t.parentElement.removeChild(t),t.parentElement!==e&&(e.appendChild(t),t.style.display="",i.debug("Video element attached to container in raw mode")))}}onDispose(){this._renderer&&(this._renderer.dispose(),this._renderer=null),i.info("Raw Render Strategy disposed")}}class Te{create(){return new Ce}}class O extends p{constructor(e,t){if(super(),this._canvas=null,this._ctx=null,this._videoElement=null,this._parentElement=null,this._resizeObserver=null,this._animationFrameId=null,this._isRendering=!1,this._renderLoop=()=>{if(!(!this._isRendering||!this._videoElement||!this._canvas||!this._ctx)){try{this._syncCanvasSize(),this._ctx.drawImage(this._videoElement,0,0,this._canvas.width,this._canvas.height)}catch(s){i.error("Error rendering from video to canvas:",s)}this._animationFrameId=requestAnimationFrame(this._renderLoop)}},e){if(this._canvas=e,this._ctx=e.getContext("2d"),!this._ctx)throw new Error("Failed to get 2D context from canvas")}else if(t)this._videoElement=t,this._createCanvasFromVideo();else throw new Error("CanvasRenderer requires either canvas or videoElement");i.info("Canvas Renderer created")}_createCanvasFromVideo(){if(!this._videoElement)return;if(this._parentElement=this._videoElement.parentElement,!this._parentElement)throw new Error("Video element must have a parent element");if(window.getComputedStyle(this._parentElement).position==="static"&&(this._parentElement.style.position="relative"),this._canvas=document.createElement("canvas"),this._ctx=this._canvas.getContext("2d"),!this._ctx)throw new Error("Failed to get 2D context from canvas");this._canvas.style.position="absolute",this._canvas.style.top="0",this._canvas.style.left="0",this._canvas.style.width="100%",this._canvas.style.height="100%",this._canvas.style.pointerEvents="none",this._parentElement.appendChild(this._canvas),this._syncCanvasSize(),this._setupResizeObserver()}_syncCanvasSize(){if(!this._canvas||!this._videoElement||!this._ctx)return;const e=this._videoElement.videoWidth,t=this._videoElement.videoHeight,s=this._videoElement.clientWidth,n=this._videoElement.clientHeight;if(e>0&&t>0&&s>0&&n>0){const o=e/t,a=s/n;let c,h;o>a?(c=s,h=s/o):(c=n*o,h=n),this._canvas.style.width=`${c}px`,this._canvas.style.height=`${h}px`;const m=(s-c)/2,v=(n-h)/2;this._canvas.style.left=`${m}px`,this._canvas.style.top=`${v}px`,this._canvas.width=e,this._canvas.height=t}}_setupResizeObserver(){!this._videoElement||typeof ResizeObserver>"u"||(this._resizeObserver=new ResizeObserver(()=>{this._syncCanvasSize()}),this._resizeObserver.observe(this._videoElement))}startRenderingFromVideo(){this._isRendering||!this._videoElement||!this._canvas||!this._ctx||(this._isRendering=!0,this._renderLoop())}stopRenderingFromVideo(){this._isRendering=!1,this._animationFrameId!==null&&(cancelAnimationFrame(this._animationFrameId),this._animationFrameId=null)}setFitMode(e){this._canvas&&(this._canvas.style.width="100%",this._canvas.style.height="100%",this._canvas.style.objectFit=e)}render(e){if(this.ensureNotDisposed(),!(!this._ctx||!this._canvas))try{const t=e.displayWidth,s=e.displayHeight;this._videoElement?this._syncCanvasSize():(this._canvas.width!==t||this._canvas.height!==s)&&(this._canvas.width=t,this._canvas.height=s);const n=e instanceof VideoFrame?e.displayWidth:e.videoWidth,o=e instanceof VideoFrame?e.displayHeight:e.videoHeight;n>0&&o>0&&(this._canvas.width!==n||this._canvas.height!==o)&&(this._canvas.width=n,this._canvas.height=o),this._ctx.clearRect(0,0,this._canvas.width,this._canvas.height),this._ctx.drawImage(e,0,0,this._canvas.width,this._canvas.height)}catch(t){i.error("Error rendering video frame to canvas:",t)}}getCanvas(){return this._canvas}getContext(){return this._ctx}onDispose(){this.stopRenderingFromVideo(),this._resizeObserver&&(this._resizeObserver.disconnect(),this._resizeObserver=null),this._canvas&&this._parentElement&&this._canvas.parentElement===this._parentElement&&this._parentElement.removeChild(this._canvas),this._canvas=null,this._ctx=null,this._videoElement=null,this._parentElement=null,i.info("Canvas Renderer disposed")}}class Z extends p{constructor(e){super(),this._element=null,this._stream=null,this._track=null,this._element=e,i.info("Video Element Renderer created")}setTrack(e){if(this.ensureNotDisposed(),this._track=e,!this._element){i.warn("No video element available to set track");return}this._stream||(this._stream=new MediaStream,this._element.srcObject=this._stream);const t=this._stream.getTracks();for(const s of t)if(s!==e)try{this._stream.removeTrack(s)}catch(n){i.error("Failed to remove track from media stream:",n)}if(!this._stream.getTracks().includes(e))try{this._stream.addTrack(e)}catch(s){i.error("Failed to add track to media stream:",s)}this._element.play().catch(s=>{i.error("Error playing video:",s)}),i.debug("Video track set to element (stream reused)")}setOpacity(e){this._element&&(this._element.style.opacity=String(e))}getElement(){return this._element}setFitMode(e){this._element&&(this._element.style.width="100%",this._element.style.height="100%",this._element.style.objectFit=e)}render(e){i.debug("Render called in direct mode (no-op)")}onDispose(){this._element&&(this._element.srcObject=null),this._stream&&(this._stream.getTracks().forEach(e=>e.stop()),this._stream=null),this._track&&(this._track.stop(),this._track=null),i.info("Video Element Renderer disposed")}clearTrack(){if(this._stream){const e=this._stream.getTracks();for(const t of e)try{this._stream.removeTrack(t)}catch(s){i.error("Failed to remove track from media stream:",s)}}this._track=null}}class Se extends p{constructor(){super(...arguments),this._renderer=null,this._processors=[],this._videoElementRenderer=null,this._canvasRenderer=null,this._videoFrameCallbackId=null,this._animationFrameId=null,this._isProcessing=!1,this._processVideoFrame=()=>{if(!this._isProcessing||!this._videoElementRenderer||!this._canvasRenderer)return;const e=this._videoElementRenderer.getElement();if(!e||e.readyState<2){this._isProcessing&&e&&(this._videoFrameCallbackId=e.requestVideoFrameCallback(this._processVideoFrame));return}try{let t=new VideoFrame(e,{timestamp:performance.now()*1e3});for(const s of this._processors){const n=s.process(t);n&&n!==t&&(t instanceof VideoFrame&&t.close(),t=n)}t instanceof ImageData?this._renderImageData(t):this._canvasRenderer.render(t),t instanceof VideoFrame&&t.close()}catch(t){i.error("Error processing video frame:",t)}this._isProcessing&&e&&(this._videoFrameCallbackId=e.requestVideoFrameCallback(this._processVideoFrame))},this._processVideoFrameFallback=()=>{if(!this._isProcessing||!this._canvasRenderer||!this._videoElementRenderer)return;const e=this._videoElementRenderer.getElement();if(!e||e.readyState<2||e.paused){const a=this._canvasRenderer.getContext(),c=this._canvasRenderer.getCanvas();a&&c&&a.clearRect(0,0,c.width,c.height),this._animationFrameId=requestAnimationFrame(this._processVideoFrameFallback);return}const t=e.videoWidth,s=e.videoHeight,n=this._canvasRenderer.getCanvas();t>0&&n&&(n.width!==t||n.height!==s)&&(n.width=t,n.height=s);let o=new VideoFrame(e);try{for(const a of this._processors){const c=a.process(o);c&&c!==o&&(o instanceof VideoFrame&&o.close(),o=c)}o instanceof ImageData?this._renderImageData(o):this._canvasRenderer.render(o)}finally{o instanceof VideoFrame&&o.close()}this._isProcessing&&(this._animationFrameId=requestAnimationFrame(this._processVideoFrameFallback))}}getRenderer(){return this._renderer}setRenderer(e){if(this._renderer&&this._renderer.dispose(),this._renderer=e,e instanceof Z){if(this._videoElementRenderer=e,!this._canvasRenderer){const t=this._videoElementRenderer.getElement();if(t)try{t.parentElement&&(this._canvasRenderer=new O(void 0,t),this._renderer=this._canvasRenderer,i.debug("Canvas renderer created in setRenderer (processed mode)"))}catch(s){i.warn("Failed to create CanvasRenderer in setRenderer, will retry in attachToContainer:",s)}}this._setupProcessingPipeline()}else this._videoElementRenderer=null,this._stopProcessingPipeline();i.debug("Processed render strategy renderer set")}addProcessor(e,t){t!==void 0&&t>=0&&t<this._processors.length?this._processors.splice(t,0,e):this._processors.push(e),this._setupProcessingPipeline()}getProcessors(){return this._processors}setProcessors(e){this._processors.forEach(t=>t.dispose()),this._processors=e,this._setupProcessingPipeline(),i.debug("Processed render strategy processor set")}attachToContainer(e){if(this._canvasRenderer){const t=this._canvasRenderer.getCanvas();if(t&&(t.parentElement&&t.parentElement!==e&&t.parentElement.removeChild(t),t.parentElement!==e&&(e.appendChild(t),i.debug("Canvas element attached to container in processed mode"))),this._videoElementRenderer){const s=this._videoElementRenderer.getElement();s&&(s.parentElement&&s.parentElement.removeChild(s),s.style.display="none")}}else if(this._videoElementRenderer){const t=this._videoElementRenderer.getElement();if(t){const s=t.parentElement!==null;s||e.appendChild(t);try{this._canvasRenderer=new O(void 0,t),this._renderer=this._canvasRenderer,!s&&t.parentElement===e&&e.removeChild(t),t.style.display="none";const n=this._canvasRenderer.getCanvas();n&&n.parentElement!==e&&e.appendChild(n),this._startProcessingLoop(),i.debug("Canvas created and attached to container in processed mode")}catch(n){i.error("Failed to create CanvasRenderer in attachToContainer:",n),!s&&t.parentElement===e&&e.removeChild(t),t.parentElement!==e&&(e.appendChild(t),t.style.display="")}}}}_setupProcessingPipeline(){if(!this._videoElementRenderer)return;const e=this._videoElementRenderer.getElement();if(!e){i.warn("Video element not available for processing");return}if(e.parentElement&&e.parentElement.removeChild(e),e.style.display="none",e.readyState<2){const t=()=>{this._setupProcessingPipeline(),e.removeEventListener("loadedmetadata",t)};e.addEventListener("loadedmetadata",t);return}this._canvasRenderer&&(this._startProcessingLoop(),i.info("Processing pipeline setup completed"))}_stopProcessingPipeline(){this._stopProcessingLoop(),this._canvasRenderer&&(this._canvasRenderer.dispose(),this._canvasRenderer=null),this._videoElementRenderer&&(this._renderer=this._videoElementRenderer)}_startProcessingLoop(){this._isProcessing||!this._videoElementRenderer||!this._canvasRenderer||!this._videoElementRenderer.getElement()||(this._isProcessing=!0,"requestVideoFrameCallback"in HTMLVideoElement.prototype?this._processVideoFrame():this._processVideoFrameFallback())}_stopProcessingLoop(){this._isProcessing=!1;const e=this._videoElementRenderer?.getElement();this._videoFrameCallbackId!==null&&e&&"cancelVideoFrameCallback"in e&&(e.cancelVideoFrameCallback(this._videoFrameCallbackId),this._videoFrameCallbackId=null),this._animationFrameId!==null&&(cancelAnimationFrame(this._animationFrameId),this._animationFrameId=null)}_renderImageData(e){if(!this._canvasRenderer)return;const t=this._canvasRenderer.getContext(),s=this._canvasRenderer.getCanvas();t&&s&&((s.width!==e.width||s.height!==e.height)&&(s.width=e.width,s.height=e.height),t.clearRect(0,0,s.width,s.height),t.putImageData(e,0,0))}onDispose(){this._stopProcessingPipeline(),this._renderer&&(this._renderer.dispose(),this._renderer=null),this._processors.forEach(e=>e.dispose()),this._processors=[],this._videoElementRenderer=null,this._canvasRenderer=null,i.info("Processed Render Strategy disposed")}}class Ae{create(){return new Se}}class U{static getFactory(e){return e==="processed"?new Ae:new Te}}class ee extends p{constructor(e,t){super(),this._videoElementRenderer=null,this._containerElement=null,this._source=e;const s=t?.renderMode||"raw";this._containerElement=t?.containerElement||null,this._strategyFactory=U.getFactory(s),this._strategy=this._strategyFactory.create();const n=this._source.getInternalElement(),o=new Z(n);this._videoElementRenderer=o,this._strategy.setRenderer(o),this._containerElement&&(this._strategy.attachToContainer(this._containerElement),i.info("Renderer elements attached to container via strategy")),i.info("Video Pipeline created")}setRenderMode(e){this.ensureNotDisposed();const t=U.getFactory(e);if(this._strategyFactory.constructor===t.constructor)return;const s=this._strategy.getRenderer(),n=this._strategy.getProcessors();this._strategy.dispose(),this._strategyFactory=t,this._strategy=this._strategyFactory.create(),s&&this._strategy.setRenderer(s),n.length>0&&this._strategy.setProcessors(n),this._containerElement&&this._strategy.attachToContainer(this._containerElement),i.info(`Video render mode changed to: ${e}`)}setRenderFitMode(e){this.ensureNotDisposed(),this._strategy.getRenderer()?.setFitMode(e),i.info(`Video render fit mode changed to: ${e}`)}setProcessor(e){this.ensureNotDisposed(),this._strategy.setProcessors(e?[e]:[])}setRenderer(e){this.ensureNotDisposed(),this._strategy.setRenderer(e)}get strategy(){return this._strategy}get videoElementRenderer(){return this._videoElementRenderer}onDispose(){if(this._strategy.dispose(),this._videoElementRenderer){const e=this._videoElementRenderer.getElement();e&&e.parentElement&&e.parentElement.removeChild(e),this._videoElementRenderer.dispose(),this._videoElementRenderer=null}i.info("Video Pipeline disposed")}}class Re extends p{onDispose(){}}class L extends Re{constructor(e){super(),this._program=null,this._texture=null,this._lastProcessedTimestamp=null,this._options=e,this._canvas=document.createElement("canvas");const t=this._canvas.getContext("webgl",{premultipliedAlpha:!1,preserveDrawingBuffer:!0});if(!t)throw new Error("WebGL not supported");this._gl=t,this._initGL(),i.info("WebGL Green Screen Processor initialized")}_initGL(){const e=this._gl,t=`
|
|
1
|
+
import{ConnectionState as F,Room as oe,RoomEvent as g,DisconnectReason as ae,ParticipantEvent as w,createLocalAudioTrack as ce,createLocalVideoTrack as he}from"livekit-client";var h;(function(o){o.SDK_AUTH_TOKEN_FAILED="SDK_AUTH_TOKEN_FAILED",o.SDK_GET_LIVEKIT_CONFIG_FAILED="SDK_GET_LIVEKIT_CONFIG_FAILED",o.SDK_SWITCH_VIDEO_FAILED="SDK_SWITCH_VIDEO_FAILED",o.SDK_INTERRUPT_CONVERSATION_FAILED="SDK_INTERRUPT_CONVERSATION_FAILED",o.HTTP_CONTROLLER_NOT_AVAILABLE="HTTP_CONTROLLER_NOT_AVAILABLE",o.SDK_PRECONNECT_FAILED="SDK_PRECONNECT_FAILED",o.SDK_CONNECT_FAILED="SDK_CONNECT_FAILED",o.SDK_INITIALIZATION_FAILED="SDK_INITIALIZATION_FAILED",o.SDK_DISCONNECT_FAILED="SDK_DISCONNECT_FAILED",o.SDK_RECONNECT_FAILED="SDK_RECONNECT_FAILED",o.SDK_NOT_CONNECTED="SDK_NOT_CONNECTED",o.SDK_INVALID_STATE_TRANSITION="SDK_INVALID_STATE_TRANSITION",o.SDK_ERROR="SDK_ERROR",o.LIVEKIT_CONNECT_FAILED="LIVEKIT_CONNECT_FAILED",o.AUDIO_CAPTURE_START_FAILED="AUDIO_CAPTURE_START_FAILED",o.AUDIO_CAPTURE_FAILED="AUDIO_CAPTURE_FAILED",o.AUDIO_INVALID_SAMPLE_RATE="AUDIO_INVALID_SAMPLE_RATE",o.AUDIO_INVALID_CHANNEL="AUDIO_INVALID_CHANNEL",o.AUDIO_INVALID_HEADER_LENGTH="AUDIO_INVALID_HEADER_LENGTH",o.AUDIO_INVALID_TYPE="AUDIO_INVALID_TYPE",o.AUDIO_INVALID_RESERVED="AUDIO_INVALID_RESERVED",o.AUDIO_CONTROLLER_NOT_AVAILABLE="AUDIO_CONTROLLER_NOT_AVAILABLE",o.CAMERA_CONTROLLER_NOT_AVAILABLE="CAMERA_CONTROLLER_NOT_AVAILABLE",o.CONVERSATION_CONTROLLER_NOT_AVAILABLE="CONVERSATION_CONTROLLER_NOT_AVAILABLE",o.STATE_MACHINE_INVALID_STATE_TRANSITION="STATE_MACHINE_INVALID_STATE_TRANSITION",o.OBJECT_DISPOSED="OBJECT_DISPOSED",o.AUDIO_INVALID_CODEC="AUDIO_INVALID_CODEC",o.LIVEKIT_SEND_VIDEO_AVAILABLE_STATE_FAILED="LIVEKIT_SEND_VIDEO_AVAILABLE_STATE_FAILED",o.LIVEKIT_SEND_TEXT_DATA_FAILED="LIVEKIT_SEND_TEXT_DATA_FAILED",o.LIVEKIT_DATA_MESSAGE_PARSE_ERROR="LIVEKIT_DATA_MESSAGE_PARSE_ERROR",o.LIVEKIT_UNPUBLISH_MICROPHONE_FAILED="LIVEKIT_UNPUBLISH_MICROPHONE_FAILED",o.NO_AVATARID="NO_AVATARID",o.NO_AUTH_TOKEN="NO_AUTH_TOKEN",o.INVALID_CONNECT_CONFIG="INVALID_CONNECT_CONFIG"})(h||(h={}));class l extends Error{constructor(e,t,i){super(t),this.name="SDKError",this.code=e,this.cause=i,Object.setPrototypeOf(this,l.prototype)}static fromError(e,t){return e instanceof l?e:e instanceof Error?new l(t||h.SDK_ERROR,e.message,e):typeof e=="string"?new l(t||h.SDK_ERROR,e):new l(t||h.SDK_ERROR,"Unknown error",e)}}class _{constructor(){this._disposed=!1}get isDisposed(){return this._disposed}dispose(){this._disposed||(this.onDispose(),this._disposed=!0)}ensureNotDisposed(){if(this._disposed)throw new l(h.OBJECT_DISPOSED,"Object has been disposed")}}var E;(function(o){o[o.DEBUG=0]="DEBUG",o[o.INFO=1]="INFO",o[o.WARN=2]="WARN",o[o.ERROR=3]="ERROR",o[o.NONE=4]="NONE"})(E||(E={}));class s{static setLevel(e){this._level=e}static setPrefix(e){this._prefix=e}static debug(e,...t){this._level<=E.DEBUG&&console.debug(`${this._prefix} [DEBUG]`,e,...t)}static info(e,...t){this._level<=E.INFO&&console.info(`${this._prefix} [INFO]`,e,...t)}static warn(e,...t){this._level<=E.WARN&&console.warn(`${this._prefix} [WARN]`,e,...t)}static error(e,...t){this._level<=E.ERROR&&console.error(`${this._prefix} [ERROR]`,e,...t)}}s._level=E.ERROR;s._prefix="[SDK]";class de extends _{constructor(){super(...arguments),this._listeners=new Map}on(e,t){this.ensureNotDisposed(),this._listeners.has(e)||this._listeners.set(e,new Set);const i=this._listeners.get(e);return i.add(t),s.debug(`Event listener added for: ${String(e)}`),()=>{i.delete(t),s.debug(`Event listener removed for: ${String(e)}`)}}once(e,t){this.ensureNotDisposed();const i=n=>{t(n),this.off(e,i)};return this.on(e,i)}off(e,t){this.ensureNotDisposed();const i=this._listeners.get(e);i&&i.delete(t)}emit(e,t){this.ensureNotDisposed();const i=this._listeners.get(e);i&&i.size>0&&(s.debug(`Emitting event: ${String(e)}`,t),i.forEach(n=>{try{n(t)}catch(r){s.error(`Error in event listener for ${String(e)}:`,r)}}))}removeAllListeners(){this._listeners.clear(),s.debug("All event listeners removed")}listenerCount(e){const t=this._listeners.get(e);return t?t.size:0}onDispose(){this.removeAllListeners()}}var m;(function(o){o.IDLE="idle",o.CONNECTING="connecting",o.CONNECTED="connected",o.PARTIAL="partial",o.DISCONNECTED="disconnected",o.RECONNECTING="reconnecting",o.DISCONNECTING="disconnecting",o.ERROR="error",o.DISPOSED="disposed"})(m||(m={}));function le(o){switch(o){case"idle":return m.IDLE;case"connecting":return m.CONNECTING;case"connected":return m.CONNECTED;case"partial":return m.PARTIAL;case"disconnecting":return m.DISCONNECTING;case"disconnected":case"error":return m.DISCONNECTED;case"reconnecting:auto":case"reconnecting:manual":return m.RECONNECTING;case"disposed":return m.DISPOSED;default:return m.DISCONNECTED}}class ue{constructor(){this.status=m.DISCONNECTED,this.state=m.DISCONNECTED,this.isConnected=!1}reset(){this.status=m.DISCONNECTED,this.state=m.DISCONNECTED,this.isConnected=!1}}const k=3,_e=5e3,N=1e3,M=10,ut=1e4,V=24e3,K=16,_t=1,pt=4096,pe=0,me="https://api.newportai.com/s2/aigc/api/vih_dispatcher",fe="wss://api.newportai.com/s2/livekit-lisbon",ve=6e4;var A;(function(o){o.NO_VIDEO_TRACK="NO_VIDEO_TRACK",o.TRACK_MUTED="TRACK_MUTED",o.TRACK_ENDED="TRACK_ENDED"})(A||(A={}));const D=.7;class ge{constructor(){this._active=null}get active(){return this._active}tryBegin(e){return this._active!==null&&this._active!==e?!1:(this._active=e,!0)}end(e){this._active===e&&(this._active=null)}reset(){this._active=null}}class Ee{constructor(e){this._connectionStateGetter=null,this.options=e,this._livekitUrl="",this._wsUrl="",this.eventBus=new de,this.sessionState=new ue,this.mediaAudioCaptureState=new ge,this._token=e.connectConfig.type==="auth"&&e.connectConfig.config.authToken||"",e.debug&&s.setLevel(0),s.info("SDK Context initialized")}setLivekitConfig(e,t,i){this._livekitUrl=e||fe,this._token=t,this._roomId=i}setAuthToken(e){this._httpToken=e}get authToken(){return this._httpToken}emitError(e,t){const i=l.fromError(e,t);return this.eventBus.emit("inner:sdk:error",{error:i}),i}setVideoOptions(e){this.options.video=Object.assign({},e,this.options.video||{})}get wsUrl(){return this._wsUrl}get livekitUrl(){return this._livekitUrl}get token(){return this._token}get roomId(){return this._roomId}getLivekitConfig(){return{livekitUrl:this._livekitUrl,token:this._token}}bindConnectionState(e){this._connectionStateGetter=e}get connectionState(){return this._connectionStateGetter?.()}clearConnectionStateBinding(){this._connectionStateGetter=null}dispose(){this.clearConnectionStateBinding(),this.mediaAudioCaptureState.reset(),this.eventBus.dispose(),this.sessionState.reset(),s.info("SDK Context disposed")}}class v extends _{setContext(e){this._context=e}}function Y(o){return{http:{connected:!1},rtc:{connected:!1,hasVideoTrack:!1},overall:{state:o}}}function J(o){return Object.freeze({http:Object.freeze({...o.http}),rtc:Object.freeze({...o.rtc}),overall:Object.freeze({...o.overall})})}class Z{constructor(){this._transitionMap=null}get transitionMap(){if(!this._transitionMap){const e=new Map;for(const t of this.transitionTable)e.set(t.from,new Set(t.to));this._transitionMap=e}return this._transitionMap}getCurrentState(){return this._currentState}transitionTo(e,t){const i=this._currentState;if(!this.canTransition(i,e)){const n=this.transitionMap.get(i),r=n?Array.from(n).join(", "):"none";throw t?.error??new l(h.STATE_MACHINE_INVALID_STATE_TRANSITION,`Invalid state transition: cannot transition from "${i}" to "${e}". Current state: "${i}". Allowed transitions: [${r}]`)}this._currentState=e}canTransition(e,t){return e===t?!1:this.transitionMap.get(e)?.has(t)??!1}reset(){throw new Error("Method not implemented.")}}class Ce extends Z{constructor(){super(...arguments),this._currentState="idle",this.transitionTable=[{from:"idle",to:["connecting","disposed"]},{from:"connecting",to:["connected","partial","error","idle","disposed"]},{from:"connected",to:["disconnecting","partial","reconnecting:auto","reconnecting:manual","error","disposed"]},{from:"partial",to:["connected","disconnecting","reconnecting:auto","reconnecting:manual","error","disposed"]},{from:"reconnecting:auto",to:["connected","partial","error","idle","disposed"]},{from:"reconnecting:manual",to:["connected","partial","error","idle","disposed"]},{from:"disconnecting",to:["disconnected","error","disposed"]},{from:"disconnected",to:["reconnecting:manual","reconnecting:auto","idle","disposed"]},{from:"error",to:["reconnecting:manual","reconnecting:auto","idle","disposed"]},{from:"disposed",to:[]}]}reset(){this._currentState="idle"}}class Te extends v{constructor(){super(),this._unsubscribes=[],this._fsm=new Ce,this._snapshot=Y("idle")}start(){this.ensureNotDisposed();const e=this._context.eventBus,t=e.on("inner:rtc:connected",()=>{this._snapshot.rtc.connected=!0,this._context.eventBus.emit("inner:sdk:connected",{source:"livekit"}),this._onFactsChanged()}),i=e.on("inner:rtc:disconnected",d=>{this._snapshot.rtc.connected=!1,this._snapshot.rtc.hasVideoTrack=!1,this._context.eventBus.emit("inner:sdk:disconnected",{source:"livekit",reason:d?.reason}),this._onFactsChanged()}),n=e.on("inner:rtc:video:available",()=>{this._snapshot.rtc.hasVideoTrack=!0}),r=e.on("inner:rtc:video:unavailable",()=>{this._snapshot.rtc.hasVideoTrack=!1}),a=e.on("inner:sdk:connected",d=>{d.source==="http"&&(this._snapshot.http.connected=!0,this._onFactsChanged())}),c=e.on("inner:sdk:disconnected",d=>{d.source==="http"&&(this._snapshot.http.connected=!1,this._onFactsChanged())});this._unsubscribes=[t,i,n,r,a,c],this._syncHttpFromContext(),this._onFactsChanged()}startConnecting(){this._transitionTo("connecting")}startDisconnecting(){this._transitionTo("disconnecting")}completeDisconnecting(){this._snapshot.http.connected=!1,this._snapshot.rtc.connected=!1,this._snapshot.rtc.hasVideoTrack=!1,this._transitionTo("disconnected")}captureSnapshotAndStartReconnectingManual(){const e=this.getSnapshot();return this._transitionTo("reconnecting:manual"),e}transitionToIdle(){this._snapshot.http.connected=!1,this._snapshot.rtc.connected=!1,this._snapshot.rtc.hasVideoTrack=!1,this._transitionTo("idle")}transitionToDisposed(){this._transitionTo("disposed")}getSnapshot(){return J(this._snapshot)}getState(){return this._fsm.getCurrentState()}canStartReconnectingManual(){const e=this._fsm.getCurrentState();return e==="connected"||e==="partial"||e==="disconnected"||e==="error"}_factsFullyConnected(){return this._context.options.connectConfig.type==="direct"?this._snapshot.rtc.connected:this._snapshot.http.connected&&this._snapshot.rtc.connected}_factsAnyCritical(){return this._context.options.connectConfig.type==="direct"?this._snapshot.rtc.connected:this._snapshot.http.connected||this._snapshot.rtc.connected}_syncHttpFromContext(){this._snapshot.http.connected=!!this._context.token}_onFactsChanged(){const e=this._fsm.getCurrentState(),t=this._factsFullyConnected(),i=this._factsAnyCritical();if(e==="connecting"||e==="reconnecting:auto"||e==="reconnecting:manual"){if(t){this._transitionTo("connected");return}if(i){this._transitionTo("partial");return}}if(e==="connected"||e==="partial"){if(!i){this._transitionTo("disconnected");return}if(!t){this._transitionTo("partial");return}if(t&&e==="partial"){this._transitionTo("connected");return}}}_transitionTo(e){this._fsm.canTransition(this._fsm.getCurrentState(),e)&&(this._fsm.transitionTo(e),this._snapshot.overall.state=this._fsm.getCurrentState(),this._syncSessionState())}_syncSessionState(){const e=this._fsm.getCurrentState();this._context.sessionState.status=le(e),this._context.sessionState.state=this._context.sessionState.status,this._context.sessionState.isConnected=e==="connected"}onDispose(){this._unsubscribes.forEach(e=>e()),this._unsubscribes=[]}}class ee extends _{get trackId(){return this._trackId}get participantId(){return this._participantId}get track(){return this._track}constructor(){super(),this._trackId=null,this._participantId=null,this._track=null,this._stream=null,this._videoElement=document.createElement("video"),this._videoElement.autoplay=!0,this._videoElement.playsInline=!0,this._videoElement.muted=!0,this._videoElement.crossOrigin="anonymous",this._videoElement.style.display="none",s.info("Video Source initialized with internal video element")}getInternalElement(){return this._videoElement}setTrack(e,t,i){this.ensureNotDisposed(),this._track&&this._track.stop(),this._track=e,this._trackId=t,this._participantId=i,this._stream||(this._stream=new MediaStream);const n=this._stream.getTracks();for(const r of n)if(r!==e)try{this._stream.removeTrack(r)}catch(a){s.warn("Failed to remove track from stream",a)}if(!this._stream.getTracks().includes(e))try{this._stream.addTrack(e)}catch(r){s.error("Failed to add track to media stream:",r)}s.info("Video track ready")}clearTrack(){if(this._videoElement&&(this._videoElement.srcObject=null),this._stream){const e=this._stream.getTracks();for(const t of e)try{this._stream.removeTrack(t)}catch(i){s.warn("Failed to remove track from stream",i)}}this._track&&(this._track.stop(),this._track=null),this._trackId=null,this._participantId=null,s.info("Video track cleared")}get hasTrack(){return this._track!==null&&this._track.readyState==="live"}onDispose(){this.clearTrack(),this._videoElement&&(this._videoElement.srcObject=null,this._videoElement.parentElement&&this._videoElement.parentElement.removeChild(this._videoElement)),this._stream&&(this._stream.getTracks().forEach(e=>e.stop()),this._stream=null),s.info("Video Source disposed")}}class Ae extends _{constructor(){super(...arguments),this._renderer=null}getRenderer(){return this._renderer}setRenderer(e){this._renderer&&this._renderer.dispose(),this._renderer=e,s.debug("Raw render strategy renderer set")}getProcessors(){return[]}setProcessors(e){s.warn("Raw render strategy does not support processors")}attachToContainer(e){if(!this._renderer){s.warn("No renderer available to attach to container");return}if("getElement"in this._renderer&&typeof this._renderer.getElement=="function"){const t=this._renderer.getElement();t instanceof HTMLVideoElement&&(t.parentElement&&t.parentElement!==e&&t.parentElement.removeChild(t),t.parentElement!==e&&(e.appendChild(t),t.style.display="",s.debug("Video element attached to container in raw mode")))}}onDispose(){this._renderer&&(this._renderer.dispose(),this._renderer=null),s.info("Raw Render Strategy disposed")}}class Re{create(){return new Ae}}class L extends _{constructor(e,t){if(super(),this._canvas=null,this._ctx=null,this._videoElement=null,this._parentElement=null,this._resizeObserver=null,this._animationFrameId=null,this._isRendering=!1,this._renderLoop=()=>{if(!(!this._isRendering||!this._videoElement||!this._canvas||!this._ctx)){try{this._syncCanvasSize(),this._ctx.drawImage(this._videoElement,0,0,this._canvas.width,this._canvas.height)}catch(i){s.error("Error rendering from video to canvas:",i)}this._animationFrameId=requestAnimationFrame(this._renderLoop)}},e){if(this._canvas=e,this._ctx=e.getContext("2d"),!this._ctx)throw new Error("Failed to get 2D context from canvas")}else if(t)this._videoElement=t,this._createCanvasFromVideo();else throw new Error("CanvasRenderer requires either canvas or videoElement");s.info("Canvas Renderer created")}_createCanvasFromVideo(){if(!this._videoElement)return;if(this._parentElement=this._videoElement.parentElement,!this._parentElement)throw new Error("Video element must have a parent element");if(window.getComputedStyle(this._parentElement).position==="static"&&(this._parentElement.style.position="relative"),this._canvas=document.createElement("canvas"),this._ctx=this._canvas.getContext("2d"),!this._ctx)throw new Error("Failed to get 2D context from canvas");this._canvas.style.position="absolute",this._canvas.style.top="0",this._canvas.style.left="0",this._canvas.style.width="100%",this._canvas.style.height="100%",this._canvas.style.pointerEvents="none",this._parentElement.appendChild(this._canvas),this._syncCanvasSize(),this._setupResizeObserver()}_syncCanvasSize(){if(!this._canvas||!this._videoElement||!this._ctx)return;const e=this._videoElement.videoWidth,t=this._videoElement.videoHeight,i=this._videoElement.clientWidth,n=this._videoElement.clientHeight;if(e>0&&t>0&&i>0&&n>0){const r=e/t,a=i/n;let c,d;r>a?(c=i,d=i/r):(c=n*r,d=n),this._canvas.style.width=`${c}px`,this._canvas.style.height=`${d}px`;const p=(i-c)/2,T=(n-d)/2;this._canvas.style.left=`${p}px`,this._canvas.style.top=`${T}px`,this._canvas.width=e,this._canvas.height=t}}_setupResizeObserver(){!this._videoElement||typeof ResizeObserver>"u"||(this._resizeObserver=new ResizeObserver(()=>{this._syncCanvasSize()}),this._resizeObserver.observe(this._videoElement))}startRenderingFromVideo(){this._isRendering||!this._videoElement||!this._canvas||!this._ctx||(this._isRendering=!0,this._renderLoop())}stopRenderingFromVideo(){this._isRendering=!1,this._animationFrameId!==null&&(cancelAnimationFrame(this._animationFrameId),this._animationFrameId=null)}setFitMode(e){this._canvas&&(this._canvas.style.width="100%",this._canvas.style.height="100%",this._canvas.style.objectFit=e)}render(e){if(this.ensureNotDisposed(),!(!this._ctx||!this._canvas))try{const t=e.displayWidth,i=e.displayHeight;this._videoElement?this._syncCanvasSize():(this._canvas.width!==t||this._canvas.height!==i)&&(this._canvas.width=t,this._canvas.height=i);const n=e instanceof VideoFrame?e.displayWidth:e.videoWidth,r=e instanceof VideoFrame?e.displayHeight:e.videoHeight;n>0&&r>0&&(this._canvas.width!==n||this._canvas.height!==r)&&(this._canvas.width=n,this._canvas.height=r),this._ctx.clearRect(0,0,this._canvas.width,this._canvas.height),this._ctx.drawImage(e,0,0,this._canvas.width,this._canvas.height)}catch(t){s.error("Error rendering video frame to canvas:",t)}}getCanvas(){return this._canvas}getContext(){return this._ctx}onDispose(){this.stopRenderingFromVideo(),this._resizeObserver&&(this._resizeObserver.disconnect(),this._resizeObserver=null),this._canvas&&this._parentElement&&this._canvas.parentElement===this._parentElement&&this._parentElement.removeChild(this._canvas),this._canvas=null,this._ctx=null,this._videoElement=null,this._parentElement=null,s.info("Canvas Renderer disposed")}}class te extends _{constructor(e){super(),this._element=null,this._stream=null,this._track=null,this._element=e,s.info("Video Element Renderer created")}setTrack(e){if(this.ensureNotDisposed(),this._track=e,!this._element){s.warn("No video element available to set track");return}this._stream||(this._stream=new MediaStream,this._element.srcObject=this._stream);const t=this._stream.getTracks();for(const i of t)if(i!==e)try{this._stream.removeTrack(i)}catch(n){s.error("Failed to remove track from media stream:",n)}if(!this._stream.getTracks().includes(e))try{this._stream.addTrack(e)}catch(i){s.error("Failed to add track to media stream:",i)}this._element.play().catch(i=>{s.error("Error playing video:",i)}),s.debug("Video track set to element (stream reused)")}setOpacity(e){this._element&&(this._element.style.opacity=String(e))}getElement(){return this._element}setFitMode(e){this._element&&(this._element.style.width="100%",this._element.style.height="100%",this._element.style.objectFit=e)}render(e){s.debug("Render called in direct mode (no-op)")}onDispose(){this._element&&(this._element.srcObject=null),this._stream&&(this._stream.getTracks().forEach(e=>e.stop()),this._stream=null),this._track&&(this._track.stop(),this._track=null),s.info("Video Element Renderer disposed")}clearTrack(){if(this._stream){const e=this._stream.getTracks();for(const t of e)try{this._stream.removeTrack(t)}catch(i){s.error("Failed to remove track from media stream:",i)}}this._track=null}}class Se extends _{constructor(){super(...arguments),this._renderer=null,this._processors=[],this._videoElementRenderer=null,this._canvasRenderer=null,this._videoFrameCallbackId=null,this._animationFrameId=null,this._isProcessing=!1,this._processVideoFrame=()=>{if(!this._isProcessing||!this._videoElementRenderer||!this._canvasRenderer)return;const e=this._videoElementRenderer.getElement();if(!e||e.readyState<2){this._isProcessing&&e&&(this._videoFrameCallbackId=e.requestVideoFrameCallback(this._processVideoFrame));return}try{let t=new VideoFrame(e,{timestamp:performance.now()*1e3});for(const i of this._processors){const n=i.process(t);n&&n!==t&&(t instanceof VideoFrame&&t.close(),t=n)}t instanceof ImageData?this._renderImageData(t):this._canvasRenderer.render(t),t instanceof VideoFrame&&t.close()}catch(t){s.error("Error processing video frame:",t)}this._isProcessing&&e&&(this._videoFrameCallbackId=e.requestVideoFrameCallback(this._processVideoFrame))},this._processVideoFrameFallback=()=>{if(!this._isProcessing||!this._canvasRenderer||!this._videoElementRenderer)return;const e=this._videoElementRenderer.getElement();if(!e||e.readyState<2||e.paused){const a=this._canvasRenderer.getContext(),c=this._canvasRenderer.getCanvas();a&&c&&a.clearRect(0,0,c.width,c.height),this._animationFrameId=requestAnimationFrame(this._processVideoFrameFallback);return}const t=e.videoWidth,i=e.videoHeight,n=this._canvasRenderer.getCanvas();t>0&&n&&(n.width!==t||n.height!==i)&&(n.width=t,n.height=i);let r=new VideoFrame(e);try{for(const a of this._processors){const c=a.process(r);c&&c!==r&&(r instanceof VideoFrame&&r.close(),r=c)}r instanceof ImageData?this._renderImageData(r):this._canvasRenderer.render(r)}finally{r instanceof VideoFrame&&r.close()}this._isProcessing&&(this._animationFrameId=requestAnimationFrame(this._processVideoFrameFallback))}}getRenderer(){return this._renderer}setRenderer(e){if(this._renderer&&this._renderer.dispose(),this._renderer=e,e instanceof te){if(this._videoElementRenderer=e,!this._canvasRenderer){const t=this._videoElementRenderer.getElement();if(t)try{t.parentElement&&(this._canvasRenderer=new L(void 0,t),this._renderer=this._canvasRenderer,s.debug("Canvas renderer created in setRenderer (processed mode)"))}catch(i){s.warn("Failed to create CanvasRenderer in setRenderer, will retry in attachToContainer:",i)}}this._setupProcessingPipeline()}else this._videoElementRenderer=null,this._stopProcessingPipeline();s.debug("Processed render strategy renderer set")}addProcessor(e,t){t!==void 0&&t>=0&&t<this._processors.length?this._processors.splice(t,0,e):this._processors.push(e),this._setupProcessingPipeline()}getProcessors(){return this._processors}setProcessors(e){this._processors.forEach(t=>t.dispose()),this._processors=e,this._setupProcessingPipeline(),s.debug("Processed render strategy processor set")}attachToContainer(e){if(this._canvasRenderer){const t=this._canvasRenderer.getCanvas();if(t&&(t.parentElement&&t.parentElement!==e&&t.parentElement.removeChild(t),t.parentElement!==e&&(e.appendChild(t),s.debug("Canvas element attached to container in processed mode"))),this._videoElementRenderer){const i=this._videoElementRenderer.getElement();i&&(i.parentElement&&i.parentElement.removeChild(i),i.style.display="none")}}else if(this._videoElementRenderer){const t=this._videoElementRenderer.getElement();if(t){const i=t.parentElement!==null;i||e.appendChild(t);try{this._canvasRenderer=new L(void 0,t),this._renderer=this._canvasRenderer,!i&&t.parentElement===e&&e.removeChild(t),t.style.display="none";const n=this._canvasRenderer.getCanvas();n&&n.parentElement!==e&&e.appendChild(n),this._startProcessingLoop(),s.debug("Canvas created and attached to container in processed mode")}catch(n){s.error("Failed to create CanvasRenderer in attachToContainer:",n),!i&&t.parentElement===e&&e.removeChild(t),t.parentElement!==e&&(e.appendChild(t),t.style.display="")}}}}_setupProcessingPipeline(){if(!this._videoElementRenderer)return;const e=this._videoElementRenderer.getElement();if(!e){s.warn("Video element not available for processing");return}if(e.parentElement&&e.parentElement.removeChild(e),e.style.display="none",e.readyState<2){const t=()=>{this._setupProcessingPipeline(),e.removeEventListener("loadedmetadata",t)};e.addEventListener("loadedmetadata",t);return}this._canvasRenderer&&(this._startProcessingLoop(),s.info("Processing pipeline setup completed"))}_stopProcessingPipeline(){this._stopProcessingLoop(),this._canvasRenderer&&(this._canvasRenderer.dispose(),this._canvasRenderer=null),this._videoElementRenderer&&(this._renderer=this._videoElementRenderer)}_startProcessingLoop(){this._isProcessing||!this._videoElementRenderer||!this._canvasRenderer||!this._videoElementRenderer.getElement()||(this._isProcessing=!0,"requestVideoFrameCallback"in HTMLVideoElement.prototype?this._processVideoFrame():this._processVideoFrameFallback())}_stopProcessingLoop(){this._isProcessing=!1;const e=this._videoElementRenderer?.getElement();this._videoFrameCallbackId!==null&&e&&"cancelVideoFrameCallback"in e&&(e.cancelVideoFrameCallback(this._videoFrameCallbackId),this._videoFrameCallbackId=null),this._animationFrameId!==null&&(cancelAnimationFrame(this._animationFrameId),this._animationFrameId=null)}_renderImageData(e){if(!this._canvasRenderer)return;const t=this._canvasRenderer.getContext(),i=this._canvasRenderer.getCanvas();t&&i&&((i.width!==e.width||i.height!==e.height)&&(i.width=e.width,i.height=e.height),t.clearRect(0,0,i.width,i.height),t.putImageData(e,0,0))}onDispose(){this._stopProcessingPipeline(),this._renderer&&(this._renderer.dispose(),this._renderer=null),this._processors.forEach(e=>e.dispose()),this._processors=[],this._videoElementRenderer=null,this._canvasRenderer=null,s.info("Processed Render Strategy disposed")}}class De{create(){return new Se}}class U{static getFactory(e){return e==="processed"?new De:new Re}}class ie extends _{constructor(e,t){super(),this._videoElementRenderer=null,this._containerElement=null,this._source=e;const i=t?.renderMode||"raw";this._containerElement=t?.containerElement||null,this._strategyFactory=U.getFactory(i),this._strategy=this._strategyFactory.create();const n=this._source.getInternalElement(),r=new te(n);this._videoElementRenderer=r,this._strategy.setRenderer(r),this._containerElement&&(this._strategy.attachToContainer(this._containerElement),s.info("Renderer elements attached to container via strategy")),s.info("Video Pipeline created")}setRenderMode(e){this.ensureNotDisposed();const t=U.getFactory(e);if(this._strategyFactory.constructor===t.constructor)return;const i=this._strategy.getRenderer(),n=this._strategy.getProcessors();this._strategy.dispose(),this._strategyFactory=t,this._strategy=this._strategyFactory.create(),i&&this._strategy.setRenderer(i),n.length>0&&this._strategy.setProcessors(n),this._containerElement&&this._strategy.attachToContainer(this._containerElement),s.info(`Video render mode changed to: ${e}`)}setRenderFitMode(e){this.ensureNotDisposed(),this._strategy.getRenderer()?.setFitMode(e),s.info(`Video render fit mode changed to: ${e}`)}setProcessor(e){this.ensureNotDisposed(),this._strategy.setProcessors(e?[e]:[])}setRenderer(e){this.ensureNotDisposed(),this._strategy.setRenderer(e)}get strategy(){return this._strategy}get videoElementRenderer(){return this._videoElementRenderer}onDispose(){if(this._strategy.dispose(),this._videoElementRenderer){const e=this._videoElementRenderer.getElement();e&&e.parentElement&&e.parentElement.removeChild(e),this._videoElementRenderer.dispose(),this._videoElementRenderer=null}s.info("Video Pipeline disposed")}}class Ie extends _{onDispose(){}}class O extends Ie{constructor(e){super(),this._program=null,this._texture=null,this._lastProcessedTimestamp=null,this._options=e,this._canvas=document.createElement("canvas");const t=this._canvas.getContext("webgl",{premultipliedAlpha:!1,preserveDrawingBuffer:!0});if(!t)throw new Error("WebGL not supported");this._gl=t,this._initGL(),s.info("WebGL Green Screen Processor initialized")}_initGL(){const e=this._gl,t=`
|
|
2
2
|
attribute vec2 aPosition;
|
|
3
3
|
attribute vec2 aTexCoord;
|
|
4
4
|
varying vec2 vTexCoord;
|
|
5
5
|
void main() {
|
|
6
6
|
gl_Position = vec4(aPosition, 0, 1);
|
|
7
7
|
vTexCoord = aTexCoord;
|
|
8
|
-
}`,
|
|
8
|
+
}`,i=`
|
|
9
9
|
precision mediump float;
|
|
10
10
|
varying vec2 vTexCoord;
|
|
11
11
|
uniform sampler2D uSampler;
|
|
@@ -52,7 +52,7 @@ import{Room as ne,RoomEvent as S,DisconnectReason as re,ParticipantEvent as oe,C
|
|
|
52
52
|
|
|
53
53
|
gl_FragColor = vec4(rgb, alpha * color.a);
|
|
54
54
|
}
|
|
55
|
-
`;this._program=this._createProgram(t,s),this._texture=e.createTexture();const n=new Float32Array([-1,-1,0,1,1,-1,1,1,-1,1,0,0,1,1,1,0]),o=e.createBuffer();e.bindBuffer(e.ARRAY_BUFFER,o),e.bufferData(e.ARRAY_BUFFER,n,e.STATIC_DRAW);const a=e.getAttribLocation(this._program,"aPosition"),c=e.getAttribLocation(this._program,"aTexCoord");e.enableVertexAttribArray(a),e.vertexAttribPointer(a,2,e.FLOAT,!1,16,0),e.enableVertexAttribArray(c),e.vertexAttribPointer(c,2,e.FLOAT,!1,16,8)}process(e){if(!this._options.enabled)return e;const{displayWidth:t,displayHeight:s,timestamp:n}=e;(this._canvas.width!==t||this._canvas.height!==s)&&(this._canvas.width=t,this._canvas.height=s,this._gl.viewport(0,0,t,s));const o=this._gl;o.useProgram(this._program),o.activeTexture(o.TEXTURE0),o.bindTexture(o.TEXTURE_2D,this._texture),o.texImage2D(o.TEXTURE_2D,0,o.RGBA,o.RGBA,o.UNSIGNED_BYTE,e),o.texParameteri(o.TEXTURE_2D,o.TEXTURE_WRAP_S,o.CLAMP_TO_EDGE),o.texParameteri(o.TEXTURE_2D,o.TEXTURE_WRAP_T,o.CLAMP_TO_EDGE),o.texParameteri(o.TEXTURE_2D,o.TEXTURE_MIN_FILTER,o.LINEAR);const a=this._options.chromaKey||[0,255,0];o.uniform3f(o.getUniformLocation(this._program,"uChromaKey"),a[0]/255,a[1]/255,a[2]/255),o.uniform1f(o.getUniformLocation(this._program,"uSimilarity"),this._options.similarity??.3),o.uniform1f(o.getUniformLocation(this._program,"uSmoothness"),this._options.smoothness??.25),o.uniform1f(o.getUniformLocation(this._program,"uSpill"),this._options.despillStrength??1.15),o.drawArrays(o.TRIANGLE_STRIP,0,4);const c=new VideoFrame(this._canvas,{timestamp:n,duration:e.duration??void 0});return e.close(),c}_createProgram(e,t){const s=this._gl,n=(a,c)=>{const h=s.createShader(a);return s.shaderSource(h,c),s.compileShader(h),h},o=s.createProgram();return s.attachShader(o,n(s.VERTEX_SHADER,e)),s.attachShader(o,n(s.FRAGMENT_SHADER,t)),s.linkProgram(o),o}onDispose(){const e=this._gl;this._texture&&e.deleteTexture(this._texture),this._program&&e.deleteProgram(this._program),i.info("WebGL Green Screen Processor disposed")}updateOptions(e){this._options={...this._options,...e}}}class we extends g{constructor(e){if(super(),this._source=new J,this._pipeline=new ee(this._source,e),e?.renderMode==="processed"&&e?.greenScreen?.enabled){const t=new L(e.greenScreen);this._pipeline.setProcessor(t),i.info("Green screen processor enabled")}this._pipeline.setRenderFitMode(e?.fitMode||"contain"),i.info("Video Controller created")}setTrack(e,t,s){this.ensureNotDisposed(),this._source.setTrack(e,t,s)}removeTrack(e){this.ensureNotDisposed(),this._source.trackId===e&&this._source.clearTrack()}setRenderMode(e){this.ensureNotDisposed(),this._pipeline.setRenderMode(e),this._context.eventBus.emit("inner:video:render:mode:changed",{mode:e})}setRenderFitMode(e){this.ensureNotDisposed(),this._pipeline.setRenderFitMode(e)}getSource(){return this.ensureNotDisposed(),this._source}onDispose(){this._source.dispose(),this._pipeline.dispose(),i.info("Video Controller disposed")}}class De extends p{process(e,t){return this.ensureNotDisposed(),e}onDispose(){}}class ye extends p{set onProcessedAudio(e){this._onProcessedAudio=e}constructor(e){super(),this._processors=[],this._processingStrategy=new De,i.info("Input Audio Pipeline created")}setProcessingStrategy(e){this.ensureNotDisposed(),this._processingStrategy&&this._processingStrategy.dispose(),this._processingStrategy=e,i.debug("Audio processing strategy set")}get processingStrategy(){return this._processingStrategy}addProcessor(e){this.ensureNotDisposed(),this._processors.push(e),i.debug(`Audio processor added. Total: ${this._processors.length}`)}removeProcessor(e){this.ensureNotDisposed();const t=this._processors.indexOf(e);t>=0&&(this._processors.splice(t,1),i.debug(`Audio processor removed. Total: ${this._processors.length}`))}process(e,t){this.ensureNotDisposed();let s=e;try{s=this._processingStrategy.process(s,t)}catch(n){i.error("Error in audio processing strategy:",n)}for(const n of this._processors)try{s=n.process(s,t)}catch(o){i.error("Error in audio processor:",o);continue}this._onProcessedAudio&&this._onProcessedAudio(s,t)}onDispose(){if(this._processingStrategy)try{this._processingStrategy.dispose()}catch(e){i.error("Error disposing audio processing strategy:",e)}this._processors.forEach(e=>{try{e.dispose()}catch(t){i.error("Error disposing audio processor:",t)}}),this._processors.length=0,this._onProcessedAudio=void 0,i.info("Input Audio Pipeline disposed")}}class be extends p{constructor(){super(),this._audioElement=null,this._stream=null,this._track=null,this._volume=1,this._isMuted=!1,this._audioElement=document.createElement("audio"),this._audioElement.autoplay=!0,this._audioElement.volume=this._volume,this._audioElement.muted=this._isMuted,i.info("Output Audio Pipeline created")}getAudioElement(){if(!this._audioElement)throw new l("Audio element not found");return this._audioElement}setTrack(e){this.ensureNotDisposed(),this._track&&this._track!==e&&this._track.stop(),this._track=e,this._audioElement&&(this._stream&&(this._stream.getTracks().forEach(t=>t.stop()),this._stream=null),this._stream=new MediaStream([e]),this._audioElement.volume=this._volume,this._audioElement.muted=this._isMuted,i.debug("Audio track set to output pipeline"))}removeTrack(){this.ensureNotDisposed(),this._stream&&(this._stream.getTracks().forEach(e=>e.stop()),this._stream=null),this._track&&(this._track.stop(),this._track=null),i.debug("Audio track removed from output pipeline")}setVolume(e){this.ensureNotDisposed(),this._volume=Math.max(0,Math.min(1,e)),this._audioElement&&(this._audioElement.volume=this._volume),i.debug(`Audio output volume set to: ${this._volume}`)}getVolume(){return this._volume}mute(){this.ensureNotDisposed(),this._isMuted=!0,this._audioElement&&(this._audioElement.muted=!0),i.debug("Audio output muted")}unmute(){this.ensureNotDisposed(),this._isMuted=!1,this._audioElement&&(this._audioElement.muted=!1),i.debug("Audio output unmuted")}isMuted(){return this._isMuted}onDispose(){this.removeTrack(),this._audioElement&&(this._audioElement=null),i.info("Output Audio Pipeline disposed")}}const Ne=1,Ie=0,ke={0:0,1:1,2:2};function xe(r){return r<0?0:r>4095?4095:Math.floor(r)}function Oe(r){if(r<0)return 0;const e=1048575;return r>e?r&e:Math.floor(r)}function Le(r){return r<0?0:r>4095?4095:Math.floor(r)}function Pe(r){return r<0?0:r>65535?65535:Math.floor(r)}function Fe(r){if(r!==0&&r!==1&&r!==2)throw new l(`Invalid sample rate encoding: ${r}. Must be 0 (16kHz), 1 (24kHz), or 2 (48kHz)`,d.AUDIO_INVALID_SAMPLE_RATE)}function Me(r){Fe(r.sampleRate);const e=xe(r.sequenceNumber),t=Oe(r.timestamp),s=Le(r.frameSize),n=Pe(r.payloadLength);if(r.channel!==0&&r.channel!==1)throw new l(`Invalid channel value: ${r.channel}. Must be 0 (mono) or 1 (stereo)`,d.AUDIO_INVALID_CHANNEL);const o=new Uint8Array(9);let a=Ne<<6;a|=r.channel<<5,a|=(r.keyFrame?1:0)<<4,a|=e>>8&15,o[0]=a,o[1]=e&255,o[2]=t>>12&255,o[3]=t>>4&255;const c=ke[r.sampleRate];let h=(t&15)<<4;h|=c<<2,h|=s>>10&3,o[4]=h,o[5]=s>>2&255;let m=(s&3)<<6;return m|=Ie&63,o[6]=m,o[7]=n>>8&255,o[8]=n&255,o}class Ve{constructor(e){this.transport=e}sendBinary(e){this.transport.sendBinary(e)}isConnected(){return this.transport.isConnected}}class Ue extends g{constructor(e){super(),this._wsSender=null,this._sequenceNumber=0,this._isKeyFrame=!0,this._audioStartTime=0,this._isSending=!1,this._audioFrameListener=null,this._audioBuffer=[],this._bufferedSampleCount=0,this._targetSampleRate=24e3,this._bitDepth=16,this._minSamplesToSend=0,this._options=e;const t=e?.input;this._targetSampleRate=t?.sampleRate||24e3,this._bitDepth=t?.bitDepth||16,this._minSamplesToSend=Math.floor(this._targetSampleRate/10),i.info(`Audio Frame Sender created: sampleRate=${this._targetSampleRate}Hz, bitDepth=${this._bitDepth}bit, minSamples=${this._minSamplesToSend}`)}setWebSocketSender(e){this._wsSender=e}setConversationTransport(e){this._wsSender=new Ve(e)}start(){if(this.ensureNotDisposed(),this._isSending){i.warn("Audio frame sending already started");return}this._sequenceNumber=0,this._isKeyFrame=!0,this._audioStartTime=Date.now(),this._audioFrameListener=e=>{this._handleAudioFrame(e.data,e.sampleRate)},this._context.eventBus.on("inner:audio:frame:received",this._audioFrameListener),this._isSending=!0,i.info("Audio frame sending started")}stop(){this.ensureNotDisposed(),this._isSending&&(this._flushBuffer(),this._audioFrameListener&&(this._context.eventBus.off("inner:audio:frame:received",this._audioFrameListener),this._audioFrameListener=null),this._isSending=!1,i.info("Audio frame sending stopped"))}_handleAudioFrame(e,t){if(this._isSending)try{Math.abs(t-this._targetSampleRate)>1&&i.warn(`Sample rate mismatch: expected ${this._targetSampleRate}Hz, got ${t}Hz`),this._audioBuffer.push(new Float32Array(e)),this._bufferedSampleCount+=e.length,this._bufferedSampleCount>=this._minSamplesToSend&&this._flushBuffer()}catch(s){i.error("Error handling audio frame:",s)}}_flushBuffer(){if(!(this._audioBuffer.length===0||this._bufferedSampleCount===0))try{const e=this._bufferedSampleCount,t=new Float32Array(e);let s=0;for(const _ of this._audioBuffer)t.set(_,s),s+=_.length;this._audioBuffer=[],this._bufferedSampleCount=0;const n=this._convertToPCM(t,this._bitDepth),o=Math.ceil(this._bitDepth/8),a=n.length*o,c=this._getSampleRateEncoding(this._targetSampleRate),h=Math.floor(Date.now()-this._audioStartTime),m={channel:(this._options?.input?.channels||1)===1?0:1,keyFrame:!!this._isKeyFrame,sequenceNumber:this._sequenceNumber,timestamp:h,sampleRate:c,frameSize:t.length,payloadLength:a},v=this._pcmToUint8Array(n,this._bitDepth);if(setTimeout(()=>{try{this._context.eventBus.emit("inner:audio:payload:send",{payload:v,sampleRate:this._targetSampleRate,channels:this._options?.input?.channels||1,sequenceNumber:m.sequenceNumber,timestamp:m.timestamp,keyFrame:m.keyFrame,frameSize:m.frameSize})}catch(_){i.error("Error emitting inner:audio:payload:send event:",_)}},0),this._wsSender&&this._wsSender.isConnected())try{const _=Me(m),E=new Uint8Array(_.length+v.length);E.set(_,0),E.set(v,_.length),this._wsSender.sendBinary(E),i.debug(`Audio frame sent via WS: seq=${m.sequenceNumber}, samples=${t.length}, size=${E.length} bytes`)}catch(_){i.error("Error sending audio frame via WebSocket:",_)}this._isKeyFrame=!1,this._sequenceNumber=(this._sequenceNumber+1)%4096}catch(e){i.error("Error flushing audio buffer:",e)}}_convertToPCM(e,t){if(t===16){const s=new Int16Array(e.length);for(let n=0;n<e.length;n++){const o=Math.max(-1,Math.min(1,e[n]));s[n]=o<0?o*32768:o*32767}return s}else if(t===32){const s=new Int32Array(e.length);for(let n=0;n<e.length;n++){const o=Math.max(-1,Math.min(1,e[n]));s[n]=o*2147483647}return s}else return i.warn(`Unsupported bit depth: ${t}, using 16-bit`),this._convertToPCM(e,16)}_pcmToUint8Array(e,t){const s=Math.ceil(t/8),n=new Uint8Array(e.length*s),o=new DataView(n.buffer);for(let a=0;a<e.length;a++)t===16?o.setInt16(a*2,e[a],!0):t===32&&o.setInt32(a*4,e[a],!0);return n}_getSampleRateEncoding(e){return e===16e3?0:e===24e3?1:e===48e3?2:(i.warn(`Unsupported sample rate: ${e}, defaulting to 48kHz`),1)}onDispose(){this.stop(),i.info("Audio Frame Sender disposed")}}class Be extends p{emitAudioData(e,t){if(this.onAudioData)try{this.onAudioData(e,t)}catch(s){i.error("Error in audio data callback:",s)}}}const Ke=`
|
|
55
|
+
`;this._program=this._createProgram(t,i),this._texture=e.createTexture();const n=new Float32Array([-1,-1,0,1,1,-1,1,1,-1,1,0,0,1,1,1,0]),r=e.createBuffer();e.bindBuffer(e.ARRAY_BUFFER,r),e.bufferData(e.ARRAY_BUFFER,n,e.STATIC_DRAW);const a=e.getAttribLocation(this._program,"aPosition"),c=e.getAttribLocation(this._program,"aTexCoord");e.enableVertexAttribArray(a),e.vertexAttribPointer(a,2,e.FLOAT,!1,16,0),e.enableVertexAttribArray(c),e.vertexAttribPointer(c,2,e.FLOAT,!1,16,8)}process(e){if(!this._options.enabled)return e;const{displayWidth:t,displayHeight:i,timestamp:n}=e;(this._canvas.width!==t||this._canvas.height!==i)&&(this._canvas.width=t,this._canvas.height=i,this._gl.viewport(0,0,t,i));const r=this._gl;r.useProgram(this._program),r.activeTexture(r.TEXTURE0),r.bindTexture(r.TEXTURE_2D,this._texture),r.texImage2D(r.TEXTURE_2D,0,r.RGBA,r.RGBA,r.UNSIGNED_BYTE,e),r.texParameteri(r.TEXTURE_2D,r.TEXTURE_WRAP_S,r.CLAMP_TO_EDGE),r.texParameteri(r.TEXTURE_2D,r.TEXTURE_WRAP_T,r.CLAMP_TO_EDGE),r.texParameteri(r.TEXTURE_2D,r.TEXTURE_MIN_FILTER,r.LINEAR);const a=this._options.chromaKey||[0,255,0];r.uniform3f(r.getUniformLocation(this._program,"uChromaKey"),a[0]/255,a[1]/255,a[2]/255),r.uniform1f(r.getUniformLocation(this._program,"uSimilarity"),this._options.similarity??.3),r.uniform1f(r.getUniformLocation(this._program,"uSmoothness"),this._options.smoothness??.25),r.uniform1f(r.getUniformLocation(this._program,"uSpill"),this._options.despillStrength??1.15),r.drawArrays(r.TRIANGLE_STRIP,0,4);const c=new VideoFrame(this._canvas,{timestamp:n,duration:e.duration??void 0});return e.close(),c}_createProgram(e,t){const i=this._gl,n=(a,c)=>{const d=i.createShader(a);return i.shaderSource(d,c),i.compileShader(d),d},r=i.createProgram();return i.attachShader(r,n(i.VERTEX_SHADER,e)),i.attachShader(r,n(i.FRAGMENT_SHADER,t)),i.linkProgram(r),r}onDispose(){const e=this._gl;this._texture&&e.deleteTexture(this._texture),this._program&&e.deleteProgram(this._program),s.info("WebGL Green Screen Processor disposed")}updateOptions(e){this._options={...this._options,...e}}}class ye extends v{constructor(e){if(super(),this._source=new ee,this._pipeline=new ie(this._source,e),e?.renderMode==="processed"&&e?.greenScreen?.enabled){const t=new O(e.greenScreen);this._pipeline.setProcessor(t),s.info("Green screen processor enabled")}this._pipeline.setRenderFitMode(e?.fitMode||"contain"),s.info("Video Controller created")}setTrack(e,t,i){this.ensureNotDisposed(),this._source.setTrack(e,t,i)}removeTrack(e){this.ensureNotDisposed(),this._source.trackId===e&&this._source.clearTrack()}setRenderMode(e){this.ensureNotDisposed(),this._pipeline.setRenderMode(e),this._context.eventBus.emit("inner:video:render:mode:changed",{mode:e})}setRenderFitMode(e){this.ensureNotDisposed(),this._pipeline.setRenderFitMode(e)}getSource(){return this.ensureNotDisposed(),this._source}onDispose(){this._source.dispose(),this._pipeline.dispose(),s.info("Video Controller disposed")}}class be extends _{process(e,t){return this.ensureNotDisposed(),e}onDispose(){}}class we extends _{set onProcessedAudio(e){this._onProcessedAudio=e}constructor(e){super(),this._processors=[],this._processingStrategy=new be,s.info("Input Audio Pipeline created")}setProcessingStrategy(e){this.ensureNotDisposed(),this._processingStrategy&&this._processingStrategy.dispose(),this._processingStrategy=e,s.debug("Audio processing strategy set")}get processingStrategy(){return this._processingStrategy}addProcessor(e){this.ensureNotDisposed(),this._processors.push(e),s.debug(`Audio processor added. Total: ${this._processors.length}`)}removeProcessor(e){this.ensureNotDisposed();const t=this._processors.indexOf(e);t>=0&&(this._processors.splice(t,1),s.debug(`Audio processor removed. Total: ${this._processors.length}`))}process(e,t){this.ensureNotDisposed();let i=e;try{i=this._processingStrategy.process(i,t)}catch(n){s.error("Error in audio processing strategy:",n)}for(const n of this._processors)try{i=n.process(i,t)}catch(r){s.error("Error in audio processor:",r);continue}this._onProcessedAudio&&this._onProcessedAudio(i,t)}onDispose(){if(this._processingStrategy)try{this._processingStrategy.dispose()}catch(e){s.error("Error disposing audio processing strategy:",e)}this._processors.forEach(e=>{try{e.dispose()}catch(t){s.error("Error disposing audio processor:",t)}}),this._processors.length=0,this._onProcessedAudio=void 0,s.info("Input Audio Pipeline disposed")}}class ke extends _{constructor(){super(),this._audioElement=null,this._stream=null,this._track=null,this._volume=1,this._isMuted=!1,this._audioElement=document.createElement("audio"),this._audioElement.autoplay=!0,this._audioElement.volume=this._volume,this._audioElement.muted=this._isMuted,s.info("Output Audio Pipeline created")}getAudioElement(){if(!this._audioElement)throw new l(h.SDK_ERROR,"Audio element not found");return this._audioElement}setTrack(e){this.ensureNotDisposed(),this._track&&this._track!==e&&this._track.stop(),this._track=e,this._audioElement&&(this._stream&&(this._stream.getTracks().forEach(t=>t.stop()),this._stream=null),this._stream=new MediaStream([e]),this._audioElement.volume=this._volume,this._audioElement.muted=this._isMuted,s.debug("Audio track set to output pipeline"))}removeTrack(){this.ensureNotDisposed(),this._audioElement&&(this._audioElement.srcObject=null),this._stream&&(this._stream.getTracks().forEach(e=>e.stop()),this._stream=null),this._track&&(this._track.stop(),this._track=null),s.debug("Audio track removed from output pipeline")}setVolume(e){this.ensureNotDisposed(),this._volume=Math.max(0,Math.min(1,e)),this._audioElement&&(this._audioElement.volume=this._volume),s.debug(`Audio output volume set to: ${this._volume}`)}getVolume(){return this._volume}mute(){this.ensureNotDisposed(),this._isMuted=!0,this._audioElement&&(this._audioElement.muted=!0),s.debug("Audio output muted")}unmute(){this.ensureNotDisposed(),this._isMuted=!1,this._audioElement&&(this._audioElement.muted=!1),s.debug("Audio output unmuted")}isMuted(){return this._isMuted}onDispose(){if(this.removeTrack(),this._audioElement){try{this._audioElement.pause(),this._audioElement.srcObject=null}catch{}this._audioElement=null}s.info("Output Audio Pipeline disposed")}}class Ne extends _{emitAudioData(e,t){if(this.onAudioData)try{this.onAudioData(e,t)}catch(i){s.error("Error in audio data callback:",i)}}}const xe=`
|
|
56
56
|
class AudioResamplerProcessor extends AudioWorkletProcessor {
|
|
57
57
|
constructor(options) {
|
|
58
58
|
super();
|
|
@@ -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:{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};
|
|
110
|
+
`;function Le(){const o=new Blob([xe],{type:"text/javascript"});return URL.createObjectURL(o)}class Oe extends Ne{constructor(e){super(),this.options=e,this._stream=null,this._audioContext=null,this._sourceNode=null,this._workletNode=null,this._targetSampleRate=V,this._bitDepth=K,this._channels=1;const t=e?.input;this._targetSampleRate=t?.sampleRate||V,this._bitDepth=t?.bitDepth||K,this._channels=t?.channels||1}async start(){this.ensureNotDisposed(),(this._stream||this._audioContext||this._sourceNode||this._workletNode)&&this.stop();try{const e=this.options?.input,t={audio:e?.constraints?e.constraints:{channelCount:this._channels}};this._stream=await navigator.mediaDevices.getUserMedia(t);const r=this._stream.getAudioTracks()[0].getSettings().sampleRate||44100;this._audioContext=new AudioContext({sampleRate:r});const a=Le();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:d,data:p,sampleRate:T}=c.data;d==="audioData"&&p&&this.emitAudioData(p,T)},this._sourceNode.connect(this._workletNode),s.info(`Audio capture started: input=${r}Hz -> output=${this._targetSampleRate}Hz, ${this._bitDepth}bit`)}catch(e){throw l.fromError(e,h.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),s.info("Audio capture stopped")}onDispose(){this.stop()}}class Pe{create(e){return new Oe(e)}}class Fe extends v{constructor(e,t){super(),this._source=null,this._isWsCapturing=!1,this._currentOutputTrackId=null,this._inputPipeline=new we(e),this._outputPipeline=new ke,this._sourceFactory=t||new Pe,s.info("Audio Controller created")}setContext(e){super.setContext(e),this._wireLocalInputToEventBus()}_wireLocalInputToEventBus(){this._inputPipeline.onProcessedAudio=(e,t)=>{this._context.eventBus.emit("inner:audio:frame:received",{data:e,sampleRate:t})}}async startWsCapture(){if(this.ensureNotDisposed(),this._isWsCapturing){s.warn("Audio capture already started");return}if(!this._context.mediaAudioCaptureState.tryBegin("ws"))throw new l(h.SDK_INVALID_STATE_TRANSITION,"RTC audio capture is active");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._isWsCapturing=!0,this._context.eventBus.emit("inner:audio:input:started",void 0),s.info("Audio capture started (ws)")}catch(e){throw this._context.mediaAudioCaptureState.end("ws"),this._context.emitError(e,h.AUDIO_CAPTURE_START_FAILED)}}stopWsCapture(){this.ensureNotDisposed(),this._isWsCapturing&&(this._source&&this._source.stop(),this._isWsCapturing=!1,this._context.mediaAudioCaptureState.end("ws"),this._context.eventBus.emit("inner:audio:input:stopped",void 0),s.info("Audio capture stopped (ws)"))}get isWsCapturing(){return this._isWsCapturing}setOutputTrack(e,t,i){this.ensureNotDisposed(),this._outputPipeline.setTrack(e),this._currentOutputTrackId=t,this._context.eventBus.emit("inner:audio:track:added",{trackId:t,participantId:i}),s.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}),s.info("Audio output track removed"))}setVolume(e){this.ensureNotDisposed(),this._outputPipeline.setVolume(e),this._context.eventBus.emit("inner:audio:volume:changed",{volume:e}),s.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),s.debug("Audio muted")}unmute(){this.ensureNotDisposed(),this._outputPipeline.unmute(),this._context.eventBus.emit("inner:audio:unmuted",void 0),s.debug("Audio unmuted")}getOutputElement(){return this._outputPipeline.getAudioElement()}isMuted(){return this._outputPipeline.isMuted()}onDispose(){this.stopWsCapture(),this._source&&(this._source.dispose(),this._source=null),this._inputPipeline.dispose(),this._outputPipeline.dispose(),s.info("Audio Controller disposed")}}class Me extends _{constructor(){super(...arguments),this._stream=null,this._videoElement=null}async start(){if(this.ensureNotDisposed(),this._stream){s.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=>{s.error("Failed to start camera video playback",t)})}catch(e){throw l.fromError(e,h.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}s.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 y=640,S=360;class Ve extends _{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(y,S):(this._canvas=document.createElement("canvas"),this._canvas.width=y,this._canvas.height=S)),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 i=t.getContext("2d");if(!i)return;const n=e.videoWidth,r=e.videoHeight;let a=y,c=y*r/n;c>S&&(c=S,a=S*n/r),t.width!==a&&(t.width=a),t.height!==c&&(t.height=c),i.clearRect(0,0,t.width,t.height),i.drawImage(e,0,0,n,r,0,0,a,c),typeof OffscreenCanvas<"u"&&t instanceof OffscreenCanvas&&typeof t.convertToBlob=="function"?t.convertToBlob({type:"image/jpeg",quality:D}).then(d=>{if(d)try{this._onFrame(d,a,c,0,D)}catch(p){s.error("ScreenshotScheduler onFrame callback error",p)}}).catch(d=>{s.error("ScreenshotScheduler convertToBlob error",d)}):t instanceof HTMLCanvasElement&&t.toBlob(d=>{if(d)try{this._onFrame(d,a,c,0,D)}catch(p){s.error("ScreenshotScheduler onFrame callback error",p)}},"image/jpeg",D)}onDispose(){this.stop(),this._canvas=null,this._videoElement=null}}async function Ke(o,e){const t=await o.arrayBuffer();return{data:new Uint8Array(t),width:e.width,height:e.height,format:e.format,quality:e.quality}}const Ue=12,He=2,Be=0,H=0,B=4,q=65535,W=4294967295;function qe(o){const e=Math.floor(o);return e<0?0:e>255?255:e}function x(o){const e=Math.floor(o);return e<0?0:e>q?q:e}function We(o){const e=Math.floor(o);return e<0?0:e>W?W:e}function Ge(o){const e=Math.floor(o);return e<H?H:e>B?B:e}function $e(o){const e=new ArrayBuffer(Ue),t=new DataView(e),i=Ge(o.format)&15,n=He<<6|Be<<4|i;return t.setUint8(0,n),t.setUint8(1,qe(o.quality)),t.setUint16(2,x(o.id),!1),t.setUint16(4,x(o.width),!1),t.setUint16(6,x(o.height),!1),t.setUint32(8,We(o.payloadLength),!1),new Uint8Array(e)}const ze=65535;class je extends _{constructor(){super(...arguments),this._wsSender=null,this._imageId=0}send(e,t){if(this.ensureNotDisposed(),!this._wsSender||!this._wsSender.isConnected())return;const i=e.byteLength,n=$e({format:t.format,quality:t.quality,id:this._nextId(),width:t.width,height:t.height,payloadLength:i}),r=new Uint8Array(n.byteLength+i);r.set(n,0),r.set(e,n.byteLength),this._wsSender.sendBinary(r)}_nextId(){const e=this._imageId;return this._imageId=this._imageId>=ze?0:this._imageId+1,e}onDispose(){this._wsSender=null}}class Qe extends v{constructor(){super(),this._isRunning=!1,this._source=new Me,this._sender=new je;const e=(t,i,n,r,a=D)=>{Ke(t,{width:i,height:n,format:r,quality:a}).then(c=>{this._sender.send(c.data,{width:c.width,height:c.height,format:c.format,quality:c.quality})})};this._scheduler=new Ve(null,e)}setContext(e){super.setContext(e),this._scheduler.setContext(e)}async startCamera(){if(this.ensureNotDisposed(),this._isRunning){s.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),s.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),s.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(i=>s.error("attachTo play failed",i)))}onDispose(){this.stopCamera(),this._source.dispose(),this._scheduler.dispose(),this._sender.dispose()}}var u;(function(o){o.IDLE="idle",o.WAITING="waiting",o.STREAMING="streaming",o.COMPLETED="completed",o.ERROR="error"})(u||(u={}));class Xe extends Z{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 G extends _{constructor(e){super(),this._currentQuestion=null,this._currentAnswer=null,this._sessionId=e,this._stateMachine=new Xe,s.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){s.warn(`Ignoring QUESTION event in invalid state: ${t}`);return}this._setQuestion(e.payload);break;case"ANSWER_START":if(t!==u.WAITING){s.warn(`Ignoring ANSWER_START event in invalid state: ${t}. Expected: WAITING`);return}this._startAnswer(e.payload);break;case"ANSWER_CHUNK":if(t!==u.STREAMING){s.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){s.warn(`Ignoring ANSWER_COMPLETE event in invalid state: ${t}. Expected: STREAMING`);return}this._completeAnswer();break;case"ERROR":this._setError(e.payload);break;default:s.warn(`Unknown event type: ${e.type}`);break}}reset(){this._stateMachine.reset(),this._currentQuestion=null,this._currentAnswer=null}onDispose(){this.reset(),s.info(`Conversation session disposed: ${this._sessionId}`)}}class Ye extends _{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),s.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),s.debug(`Message assembly completed for question ${e}, length: ${t.length}`),t}clear(e){this._buffers.delete(e)}onDispose(){this._buffers.clear(),s.info("Message Assembler disposed")}}class Je{generateSessionId(){return`session_${Date.now()}_${Math.random().toString(36).substring(2,9)}`}generateQuestionId(){return Math.random().toString(36).substring(2,6)}}class Ze extends v{constructor(e){super(),this._rtcTextSender=null,this._sessions=new Map,this._assembler=new Ye,this._idService=new Je,s.info("Conversation Manager created")}setRtcTextSender(e){this._rtcTextSender=e}async sendQuestion(e,t={speed:100,mood:0,vol:50}){this.ensureNotDisposed();const i=this._idService.generateQuestionId(),n={id:i,text:e,timestamp:Date.now(),inputMode:"text"},r=this._idService.generateSessionId(),a=new G(r);return a.handleEvent({type:"QUESTION",payload:n}),this._sessions.set(i,a),await this._dispatchServerTextUplink(i,e,t,a),this._context.eventBus.emit("inner:conversation:question:sent",{questionId:i,text:e}),this._context.eventBus.emit("inner:conversation:answer:waiting",{questionId:i}),s.info(`Question sent: ${i}`),i}async sendSystemEvent(e,t){if(this.ensureNotDisposed(),this._rtcTextSender)try{await this._rtcTextSender.sendSystemEvent(e,t)}catch(i){s.error("Error sending video avaliable state:",i)}}async _dispatchServerTextUplink(e,t,i,n){if(this._rtcTextSender)try{await this._rtcTextSender.sendTextData(e,t,"input.text",i)}catch(r){throw s.error("Error sending question via RTC:",r),n.handleEvent({type:"ERROR",payload:"Failed to send question"}),this._context.eventBus.emit("inner:sdk:error",{error:l.fromError(r,h.LIVEKIT_SEND_TEXT_DATA_FAILED)}),r}else s.warn("RTC text sender not set, question not sent")}handleServerPong(){this.ensureNotDisposed()}willDisconnect(){this.ensureNotDisposed()}handleAnswerChunk(e,t,i){this.ensureNotDisposed();const n=this._sessions.get(e);if(!n){s.warn(`No active session for answer chunk: ${e}`);return}if(n.state===u.WAITING&&n.handleEvent({type:"ANSWER_START",payload:e}),i){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){s.error("Error disposing session:",c)}this._sessions.delete(e)}else n.handleEvent({type:"ANSWER_CHUNK",payload:{uid:e,chunk:t,isComplete:i}}),this._assembler.appendChunk(e,t),this._context.eventBus.emit("inner:conversation:answer:chunk",{questionId:e,chunk:t,isComplete:i})}handleServerInitiatedMessage(e,t){this.ensureNotDisposed();const i=e??this._idService.generateQuestionId();this._context.eventBus.emit("inner:conversation:server:message",{questionId:i,message:t,type:"text"})}handleAsrTextReceived(e,t,i,n){this.ensureNotDisposed();const r=this._idService.generateSessionId(),a=new G(r);a.handleEvent({type:"QUESTION",payload:{id:e,text:t,timestamp:Date.now(),inputMode:"voice"}}),this._sessions.set(e,a),i?this._context.eventBus.emit("inner:conversation:asr:chunk",{questionId:e,text:t,isComplete:n??!1}):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){s.error("Error disposing session:",t)}this._sessions.clear(),this._assembler.dispose(),s.info("Conversation Manager disposed")}}class et extends v{constructor(e){super(),this._eventUnsubscribers=[],this._manager=new Ze(e),s.info("Conversation Controller created")}setContext(e){super.setContext(e),this._manager.setContext(e),this._bindEventBus()}get manager(){return this._manager}setRtcTextSender(e){this._manager.setRtcTextSender(e)}async sendQuestion(e,t){return this.ensureNotDisposed(),this._manager.sendQuestion(e,t)}async interrupt(){return this.ensureNotDisposed(),this._manager.sendSystemEvent("control.interrupt")}_clearEventListeners(){this._eventUnsubscribers.forEach(e=>{try{e()}catch(t){s.error("Performance monitor unsubscribe failed",t)}}),this._eventUnsubscribers=[]}_bindEventBus(){this._eventUnsubscribers.push(this._context.eventBus.on("inner:rtc:video:available",()=>{this.sendVideoAvaliableState()}))}async sendVideoAvaliableState(){return this.ensureNotDisposed(),this._manager.sendSystemEvent("scene.ready")}onDispose(){this._clearEventListeners(),this._manager.dispose(),s.info("Conversation Controller disposed")}}class tt extends _{constructor(){super(...arguments),this._tracks=new Map,this._participants=new Map,this._trackEndedHandlers=new Map}register(e,t,i){this.ensureNotDisposed(),this._tracks.set(e,t),this._participants.has(i)||this._participants.set(i,new Set),this._participants.get(i).add(e);const n=()=>{s.debug(`Track ended: ${e}`),this.unregister(e)};this._trackEndedHandlers.set(e,n),t.on("ended",n),s.debug(`Track registered: ${e} from participant ${i}`)}unregister(e){const t=this._tracks.get(e);if(t){const i=this._trackEndedHandlers.get(e);i&&(t.off("ended",i),this._trackEndedHandlers.delete(e)),t.stop(),this._tracks.delete(e);for(const[n,r]of this._participants.entries())if(r.has(e)){r.delete(e),r.size===0&&this._participants.delete(n);break}s.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,i=!1;for(const n of this._tracks.values()){const r=n.mediaStreamTrack;if(r.kind==="video"&&(e=!0,r.readyState==="live"&&(t=!0),r.enabled&&(i=!0),r.readyState==="live"&&r.enabled))return{available:!0}}return e?t?i?{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(){const e=Array.from(this._tracks.keys());for(const t of e)this.unregister(t);s.info("All tracks cleared")}onDispose(){this.clear()}}class $ extends v{constructor(e){super(),this._videoController=null,this._room=null,this._trackRegistry=e,this._loosTrackTimer=null,s.info("Video Track Handler created")}setRoom(e){this._room=e}setVideoController(e){this._videoController=e}handleTrackSubscribed(e,t,i){this.ensureNotDisposed(),this._loosTrackTimer&&clearTimeout(this._loosTrackTimer),this._trackRegistry.register(t,e,i),this._videoController&&e.attach(this._videoController.getSource().getInternalElement()),e.on("videoPlaybackStarted",()=>{s.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:i}),s.debug(`Video track subscribed: ${t}`)}handleTrackUnsubscribed(e,t,i){this.ensureNotDisposed(),this._videoController&&e.detach(this._videoController.getSource().getInternalElement()),e.removeAllListeners(),this._context.eventBus.emit("inner:video:track:removed",{trackId:t,participantId:i}),this._loosTrackTimer&&clearTimeout(this._loosTrackTimer),this._loosTrackTimer=setTimeout(()=>{this._context.eventBus.emit("inner:rtc:video:unavailable",void 0)},_e),this._trackRegistry.unregister(t),s.debug(`Video track unsubscribed: ${t}`)}supports(e){return e.mediaStreamTrack.kind==="video"}onDispose(){this._loosTrackTimer&&(clearTimeout(this._loosTrackTimer),this._loosTrackTimer=null),s.info("Video Track Handler disposed")}}class z extends v{constructor(e){super(),this._audioController=null,this._room=null,this._trackRegistry=e,s.info("Audio Track Handler created")}setRoom(e){this._room=e}setAudioController(e){this._audioController=e}handleTrackSubscribed(e,t,i){this.ensureNotDisposed(),this._trackRegistry.register(t,e,i),this._audioController?e.attach(this._audioController.getOutputElement()):this._context.eventBus.emit("inner:audio:track:added",{trackId:t,participantId:i}),s.debug(`Audio track subscribed: ${t}`)}handleTrackUnsubscribed(e,t,i){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),s.debug(`Audio track unsubscribed: ${t}`)}supports(e){return e.mediaStreamTrack.kind==="audio"}onDispose(){s.info("Audio Track Handler disposed")}}class it extends v{constructor(e){super(),this._handlers=[],this._room=null,this._trackRegistry=e,s.info("LiveKit Event Adapter created")}setContext(e){if(super.setContext(e),this._handlers.length===0){const t=new $(this._trackRegistry),i=new z(this._trackRegistry);t.setContext(e),i.setContext(e),this._handlers.push(t,i),this._room&&(t.setRoom(this._room),i.setRoom(this._room))}}setRoom(e){this._room=e,this._handlers.forEach(t=>{t.setRoom(e)}),s.info("LiveKit room set:",this._room)}setVideoController(e){const t=this._handlers.find(i=>i instanceof $);t&&t.setVideoController(e)}setAudioController(e){const t=this._handlers.find(i=>i instanceof z);t&&t.setAudioController(e)}handleTrackSubscribed(e,t,i){this.ensureNotDisposed();const n=this._findHandler(e);n?n.handleTrackSubscribed(e,t,i):s.warn(`No handler found for track, trackId: ${t}`)}handleTrackUnsubscribed(e,t){this.ensureNotDisposed();const i=this._trackRegistry.getTrack(e);if(i){const n=this._findHandler(i);n?n.handleTrackUnsubscribed(i,e,t):(this._trackRegistry.unregister(e),s.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){s.error("Error disposing track handler:",t)}}),this._handlers.length=0,s.info("LiveKit Event Adapter disposed")}}class st extends v{constructor(){super(),this._room=null,this._rtcRoom=null,this._micTrack=null,this._cameraVideoTrack=null,this._protocolDispatch=null,this._maxAttempts=k,this._maxDelay=M*N,this._speakingHandlers=new Map,this._trackRegistry=new tt,this._eventAdapter=new it(this._trackRegistry),s.info("LiveKit Service created")}setContext(e){super.setContext(e),this._maxAttempts=e.options.reconnect?.maxAttempts||k,this._maxDelay=(e.options.reconnect?.delay??M)*N,this._eventAdapter.setContext(e)}async connect(e,t,i,n){if(this.ensureNotDisposed(),this._rtcRoom&&this._rtcRoom.state===F.Connected){s.warn("Already connected to LiveKit room");return}let r;try{await this._tearDownStaleRoom(),s.info(`Connecting to LiveKit room: ${i} at ${e}`),r=new oe({reconnectPolicy:{nextRetryDelayInMs:a=>a.elapsedMs<this._maxDelay?Math.min(N*Math.pow(2,a.retryCount),this._maxDelay):null}}),r.prepareConnection(e,t),this._registerRoomListeners(r),s.info("Connecting to LiveKit room..."),await r.connect(e,t,{autoSubscribe:!0,maxRetries:this._context.options.reconnect?.maxAttempts||k}),this._eventAdapter.setRoom(r),this._rtcRoom=r,this._room=r}catch(a){if(r){try{r.removeAllListeners()}catch{}try{await r.disconnect()}catch{}}throw this._rtcRoom=null,this._room=null,l.fromError(a,h.LIVEKIT_CONNECT_FAILED)}}_registerRoomListeners(e){e.on(g.TrackSubscribed,(t,i,n)=>{this._handleTrackSubscribed(t,i,n)}),e.on(g.TrackUnsubscribed,(t,i,n)=>{this._handleTrackUnsubscribed(t,i,n)}),e.on(g.TrackMuted,(t,i)=>{this._handleTrackMuted(t,i)}),e.on(g.TrackUnmuted,(t,i)=>{this._handleTrackUnmuted(t,i)}),e.on(g.Connected,()=>{s.info("Connected to LiveKit room"),this._context.eventBus.emit("inner:rtc:connected",void 0)}),e.on(g.Disconnected,t=>{t?s.info(`LiveKit room disconnected: ${t}`):s.info("LiveKit room disconnected"),t!==ae.CLIENT_INITIATED&&(this._context.eventBus.emit("inner:rtc:disconnected",{reason:t?String(t):void 0}),this._context.emitError(new l(h.LIVEKIT_CONNECT_FAILED,t?`LiveKit disconnected: ${String(t)}`:"LiveKit disconnected"),h.LIVEKIT_CONNECT_FAILED)),this._micTrack?.stop(),this._micTrack=null,this._cameraVideoTrack?.stop(),this._cameraVideoTrack=null,this._clearSpeakingHandlers(),this._context.mediaAudioCaptureState.end("rtc"),this._room=null,this._rtcRoom=null}),e.on(g.DataReceived,t=>{this._onRoomDataReceived(t)})}async _tearDownStaleRoom(){if(!this._rtcRoom)return;await this.stopMicrophone(),await this.stopCamera(),this._clearSpeakingHandlers();const e=this._rtcRoom;this._rtcRoom=null,this._room=null;try{e.removeAllListeners()}catch{}try{await e.disconnect()}catch{}}_clearSpeakingHandlers(){if(!this._rtcRoom){this._speakingHandlers.clear();return}for(const[e,t]of this._speakingHandlers.entries()){const i=this._findRemoteParticipantByIdentity(e);try{i?.off(w.IsSpeakingChanged,t)}catch{}}this._speakingHandlers.clear()}_findRemoteParticipantByIdentity(e){const t=this._rtcRoom;if(t){for(const i of t.remoteParticipants.values())if(i.identity===e)return i}}_removeSpeakingListenerIfNoAudioTracks(e){const t=this._speakingHandlers.get(e);if(!t)return;const i=this._trackRegistry.getParticipantTracks(e);for(const r of i)if(this._trackRegistry.getTrack(r)?.kind==="audio")return;const n=this._findRemoteParticipantByIdentity(e);try{n?.off(w.IsSpeakingChanged,t)}catch{}this._speakingHandlers.delete(e)}async startMicrophone(){this.ensureNotDisposed();const e=this._rtcRoom;if(!e||!this.isConnected())throw new l(h.LIVEKIT_CONNECT_FAILED,"LiveKit is not connected");if(!this._context.mediaAudioCaptureState.tryBegin("rtc"))throw new l(h.SDK_INVALID_STATE_TRANSITION,"WebSocket audio capture is active");try{if(this._micTrack){s.warn("Microphone already published");return}const t=await ce(this._context.options.audio?.input??{});await e.localParticipant.publishTrack(t),this._micTrack=t,s.info("Microphone published (rtc)")}catch(t){throw this._context.mediaAudioCaptureState.end("rtc"),this._context.emitError(t,h.AUDIO_CAPTURE_START_FAILED)}}async stopMicrophone(){const e=this._rtcRoom,t=this._micTrack;if(t&&e)try{await e.localParticipant.unpublishTrack(t,!0)}catch(i){throw this._context.emitError(i,h.LIVEKIT_UNPUBLISH_MICROPHONE_FAILED)}this._micTrack?.stop(),this._micTrack=null,this._context.mediaAudioCaptureState.end("rtc"),s.info("Microphone stopped (rtc)")}isMicrophoneActive(){return!!this._micTrack}async sendTextData(e,t,i,n){this.ensureNotDisposed();const r=this._rtcRoom;if(!r||!this.isConnected())throw new l(h.LIVEKIT_CONNECT_FAILED,"LiveKit is not connected");try{const a={event:i,requestId:e,data:{...n,text:t}},c=new TextEncoder().encode(JSON.stringify(a));await r.localParticipant.publishData(c,{reliable:!0}),s.debug("Text message sent via RTC data:",e)}catch(a){throw this._context.emitError(a,h.LIVEKIT_SEND_TEXT_DATA_FAILED)}}async sendSystemEvent(e,t){this.ensureNotDisposed();const i=this._rtcRoom;if(!i||!this.isConnected())throw new l(h.LIVEKIT_CONNECT_FAILED,"LiveKit is not connected");try{const n={event:e,data:t??{}},r=new TextEncoder().encode(JSON.stringify(n));await i.localParticipant.publishData(r,{reliable:!0}),s.debug("System event :",e,"sent via RTC data")}catch(n){throw this._context.emitError(n,h.LIVEKIT_SEND_TEXT_DATA_FAILED)}}setProtocolMessageDispatcher(e){this._protocolDispatch=e}async startCamera(){this.ensureNotDisposed();const e=this._rtcRoom;if(!e||!this.isConnected())throw new l(h.LIVEKIT_CONNECT_FAILED,"LiveKit is not connected");if(this._cameraVideoTrack){s.warn("RTC camera already published");return}try{const t=await he({});await e.localParticipant.publishTrack(t),this._cameraVideoTrack=t,s.info("Camera published (rtc)")}catch(t){throw this._context.emitError(t,h.LIVEKIT_CONNECT_FAILED)}}async stopCamera(){const e=this._rtcRoom,t=this._cameraVideoTrack;if(t&&e)try{await e.localParticipant.unpublishTrack(t,!0)}catch(i){s.error("Error unpublishing RTC camera:",i)}this._cameraVideoTrack?.stop(),this._cameraVideoTrack=null,s.info("Camera stopped (rtc)")}attachCameraTo(e){if(this.ensureNotDisposed(),!this._cameraVideoTrack)throw new l(h.LIVEKIT_CONNECT_FAILED,"Camera track is not published");this._cameraVideoTrack.attach(e)}detachCameraFrom(e){if(this.ensureNotDisposed(),!this._cameraVideoTrack)throw new l(h.LIVEKIT_CONNECT_FAILED,"Camera track is not published");this._cameraVideoTrack.detach(e)}isRtcCameraActive(){return this._cameraVideoTrack!==null}_onRoomDataReceived(e){if(!(!this._protocolDispatch||e.length===0))try{const t=new TextDecoder().decode(e),i=JSON.parse(t);if(!i||typeof i!="object")return;s.debug("RTC data message received:",t),this._protocolDispatch(i)}catch(t){s.error("RTC data message parse error:",t),this._context.emitError(t,h.LIVEKIT_DATA_MESSAGE_PARSE_ERROR)}}_handleTrackSubscribed(e,t,i){try{if(!e.mediaStreamTrack){s.warn("Track does not have MediaStreamTrack");return}if(e.kind==="audio"){const c=i.identity;if(!this._speakingHandlers.has(c)){const d=p=>{this._context.eventBus.emit("inner:audio:speaking:changed",{participantId:c,isSpeaking:p}),s.info(`Participant ${c} is speaking: ${p}`)};i.on(w.IsSpeakingChanged,d),this._speakingHandlers.set(c,d)}}const r=t.trackSid,a=i.identity;this._eventAdapter.handleTrackSubscribed(e,r,a)}catch(n){s.error("Error handling track subscribed:",n)}}_handleTrackUnsubscribed(e,t,i){try{const n=t,r=i,a=n.trackSid,c=r.identity,d=this._trackRegistry.getTrack(a),p=d?.kind==="audio";d?.kind==="video"&&this._context.eventBus.emit("inner:rtc:video:unavailable",void 0),this._eventAdapter.handleTrackUnsubscribed(a,c),p&&this._removeSpeakingListenerIfNoAudioTracks(c)}catch(n){s.error("Error handling track unsubscribed:",n)}}_handleTrackMuted(e,t){try{e.kind==="video"&&this._context.eventBus.emit("inner:rtc:video:unavailable",void 0)}catch(i){s.error("Error handling track muted:",i)}}_handleTrackUnmuted(e,t){try{e.kind==="video"&&this._context.eventBus.emit("inner:rtc:video:available",void 0)}catch(i){s.error("Error handling track unmuted:",i)}}async disconnect(){await this.stopMicrophone(),await this.stopCamera(),this._clearSpeakingHandlers();const e=this._rtcRoom;if(e)try{try{e.removeAllListeners()}catch{}await e.disconnect(),this._room=null,this._rtcRoom=null,this._context.eventBus.emit("inner:rtc:disconnected",{reason:"Disconnected from LiveKit room"}),s.info("Disconnected from LiveKit room")}catch(t){s.error("Error disconnecting from LiveKit room:",t),this._room=null,this._rtcRoom=null}else this._room=null}isConnected(){return this._room?this._room.state===F.Connected:!1}getTrackRegistry(){return this._trackRegistry}get trackRegistry(){return this._trackRegistry}getEventAdapter(){return this._eventAdapter}onDispose(){this._protocolDispatch=null,this.disconnect(),this._trackRegistry.dispose(),this._eventAdapter.dispose(),s.info("LiveKit Service disposed")}}class nt extends v{constructor(){super(),this._conversationManager=null,s.info("ProtocolMessageDispatcher created")}setConversationManager(e){this._conversationManager=e,s.debug("Conversation manager set in ProtocolMessageDispatcher")}dispatch(e){this.ensureNotDisposed();try{if(e.event){this._handleLivekitServerResponseMessage(e);return}}catch(t){s.error("Error dispatching protocol message:",t)}}_handleLivekitServerResponseMessage(e){switch(e.event){case"response.chunk":this._conversationManager?.handleAnswerChunk(e.requestId,e.data.text,!1);break;case"response.done":this._conversationManager?.handleAnswerChunk(e.requestId,"",!0);break;case"input.asr.partial":this._conversationManager?.handleAsrTextReceived(e.requestId,e.data.text,!0,e.data.final??!1);break;case"input.asr.final":this._conversationManager?.handleAsrTextReceived(e.requestId,e.data.text,!1,!0);break;case"system.prompt":this._conversationManager?.handleServerInitiatedMessage(void 0,e.data.text);break;case"session.closing":break;case"input.voice.start":this._context.eventBus.emit("inner:conversation:voice:start",{requestId:e.requestId});break;case"input.voice.finish":this._context.eventBus.emit("inner:conversation:voice:finish",{requestId:e.requestId});break;default:s.warn("Unknown event:",e.event)}}onDispose(){this._conversationManager=null,s.info("ProtocolMessageDispatcher disposed")}}const b=["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:asr:chunk","conversation:answer:chunk","conversation:answer:completed"],j=/^[a-z]+:[a-zA-z]+(:[a-zA-Z]+)?$/;class rt{constructor(e){this.inner=e,this.listeners=new Map,this.forwardedVideoTrackIds=new Set,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:asr:chunk":{public:"conversation:asr:chunk",sanitizer:t=>({questionId:t?.questionId,text:String(t?.text||""),isComplete:t?.isComplete??!1})},"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,i=>this.handleInternalEvent(t,i))}),this.inner.on("inner:sdk:connected",t=>this.handleConnected(t,!0)),this.inner.on("inner:sdk:disconnected",t=>this.handleConnected(t,!1)),b.forEach(t=>{this.inner.on(t,i=>this.handlePublicEvent(t,i))})}publicAPI(){return{on:this.on.bind(this),off:this.off.bind(this),once:this.once.bind(this)}}on(e,t){const i=String(e);return this.assertAllowed(i),this.listeners.has(i)||this.listeners.set(i,new Set),this.listeners.get(i).add(t),()=>this.off(e,t)}once(e,t){const i=String(e);this.assertAllowed(i);const n=r=>{try{t(r)}finally{this.off(e,n)}};return this.on(e,n)}off(e,t){const i=String(e);this.assertAllowed(i);const n=this.listeners.get(i);n&&(n.delete(t),n.size===0&&this.listeners.delete(i))}handleInternalEvent(e,t){const i=this.internalMap[e];if(!i)return;const n=i.public;if(b.indexOf(n)===-1)return;if(n==="media:video:trackAdded"){const c=t?.trackId||t?.id;if(typeof c=="string"){if(this.forwardedVideoTrackIds.has(c))return;this.forwardedVideoTrackIds.add(c)}}const r=i.sanitizer?i.sanitizer(t):t;if(!j.test(n))return;const a=this.listeners.get(n);!a||a.size===0||a.forEach(c=>{try{c(r)}catch(d){s.error("PublicEventEmitter listener error",d)}})}handlePublicEvent(e,t){if(b.indexOf(e)===-1)return;const i=Object.values(this.internalMap).find(a=>a.public===e),n=i&&i.sanitizer?i.sanitizer(t):t;if(e==="media:video:trackAdded"){const a=t?.trackId||t?.id;if(typeof a=="string"){if(this.forwardedVideoTrackIds.has(a))return;this.forwardedVideoTrackIds.add(a)}}const r=this.listeners.get(e);!r||r.size===0||r.forEach(a=>{try{a(n)}catch(c){s.error("PublicEventEmitter listener error",c)}})}handleConnected(e,t){e.source==="livekit"?this.livekitConnected=t:e.source==="http"&&(this.httpConnected=t),this.maybeEmitSdkConnected(t?"connected":"disconnected")}maybeEmitSdkConnected(e){const t=e==="connected",i=t?!!this.livekitConnected:!this.livekitConnected,n=t?!!this.httpConnected:!this.httpConnected,a={livekit:i,http:n,all:i&&n},c=JSON.stringify(a);this.lastConnectedPayloadJson!==c&&(this.lastConnectedPayloadJson=c,this.inner.emit(`sdk:${e}`,a))}assertAllowed(e){if(b.indexOf(e)===-1)throw new Error(`Event "${e}" is not allowed for public consumers`);if(!j.test(e))throw new Error(`Event name "${e}" does not follow namespace:domain[:action] naming`)}}class ot extends _{constructor(e,t){super(),this._requestCounter=0,this._baseURL=e||"",this._defaultHeaders=t||{},s.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}`,i=this._buildURL(e.url,e.params),n=this._mergeHeaders(e.headers),r={method:e.method||"GET",headers:n};e.data!==void 0&&(e.method||"GET")!=="GET"&&(typeof e.data=="string"?r.body=e.data:(r.body=JSON.stringify(e.data),!n["Content-Type"]&&!n["content-type"]&&(n["Content-Type"]="application/json")));const a=e.timeout||3e4,c=new AbortController;return r.signal=c.signal,new Promise((d,p)=>{const T=setTimeout(()=>c.abort(),a);fetch(i,r).then(async f=>{clearTimeout(T);const I={};f.headers.forEach((ne,re)=>{I[re]=ne});let R;const P=f.headers.get("content-type");P&&P.includes("application/json")?R=await f.json():R=await f.text();const se={requestId:t,status:f.status,headers:I,data:R,config:e};s.debug(`HTTP request succeeded: ${e.method} ${i} [${t}]`),d(se)}).catch(f=>{clearTimeout(T);const I=f&&f.name==="AbortError",R={requestId:t,status:f&&f.status||void 0,message:I?"Request aborted (timeout)":f&&f.message||"HTTP request failed",error:f,config:e};s.error(`HTTP request failed: ${e.method} ${i} [${t}]`,f),p(R)})})}get(e,t,i){return this.request({...i,url:e,method:"GET",params:t})}post(e,t,i){return this.request({...i,url:e,method:"POST",data:t})}put(e,t,i){return this.request({...i,url:e,method:"PUT",data:t})}delete(e,t){return this.request({...t,url:e,method:"DELETE"})}patch(e,t,i){return this.request({...i,url:e,method:"PATCH",data:t})}_buildURL(e,t){if(e.startsWith("http://")||e.startsWith("https://"))return this._appendParams(e,t);let i=this._baseURL;return i&&!i.endsWith("/")&&!e.startsWith("/")?i+="/":i&&i.endsWith("/")&&e.startsWith("/")&&(i=i.slice(0,-1)),i+=e,this._appendParams(i,t)}_appendParams(e,t){if(!t||Object.keys(t).length===0)return e;const i=new URLSearchParams;for(const[r,a]of Object.entries(t))i.append(r,String(a));const n=e.includes("?")?"&":"?";return`${e}${n}${i.toString()}`}_mergeHeaders(e){const t={...this._defaultHeaders};return e&&Object.assign(t,e),t}onDispose(){s.info("HTTP Service disposed")}}function at(o,e="https"){return o.includes("://")?o:`${e.replace(/:$/,"")}://${o}`}class ct extends v{constructor(e,t){super(),this._httpService=new ot(e||me,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",h.SDK_AUTH_TOKEN_FAILED)}async fetchConnectionConfig(){this.ensureNotDisposed();const e=await this._httpService.request({url:"/v1/session/start",method:"POST",data:{avatarId:this._context.options.connectConfig.type==="auth"?this._context.options.connectConfig.config.avatarId:"",voice:this._context.options.connectConfig.type==="auth"&&this._context.options.connectConfig.config.avatarVoice||""},headers:this._context.options.sandbox?{"X-Env-Sandbox":"true"}:void 0}).catch(t=>{throw this._context.emitError(t,h.SDK_GET_LIVEKIT_CONFIG_FAILED)});if(e.data.code!==pe||!e.data.data)throw this._context.emitError(new l(h.SDK_GET_LIVEKIT_CONFIG_FAILED,e.data.msg||"LiveKit config response invalid"),h.SDK_GET_LIVEKIT_CONFIG_FAILED);return{roomId:"",roomToken:e.data.data.userToken,livekitUrl:at(e.data.data.sfuUrl,"wss")}}onDispose(){this._httpService.dispose()}}class Q extends v{constructor(e,t){super(),this._connectStartAt=null,this._hasReportedFirstFrame=!1,this._videoElement=null,this._videoFirstFrameHandler=null,this._pendingTextResponse=new Map,this._pendingTextAudioQueue=[],this._pendingTextAudioStartAt=new Map,this._pendingNoSpeechStartAt=null,this._eventUnsubscribers=[],this._reporter=e??null,this._now=t??(()=>Date.now())}setReporter(e){this._reporter=e??null}startConnectMeasurement(){this._connectStartAt=this._now(),this._hasReportedFirstFrame=!1}bindVideoElement(e){this._unbindVideoElement(),this._videoElement=e,this._videoFirstFrameHandler=this._handleVideoFirstFrame.bind(this),this._videoElement.addEventListener("loadeddata",this._videoFirstFrameHandler,{once:!1})}setContext(e){super.setContext(e),this._bindEventBus()}_bindEventBus(){this._clearEventListeners(),this._subscribeConversationMetrics(),this._subscribeAudioMetrics()}_subscribeConversationMetrics(){this._eventUnsubscribers.push(this._context.eventBus.on("inner:conversation:question:sent",({questionId:e})=>{const t=this._now();this._pendingTextResponse.set(e,t),this._pendingTextAudioQueue.push(e),this._pendingTextAudioStartAt.set(e,t)})),this._eventUnsubscribers.push(this._context.eventBus.on("inner:conversation:answer:chunk",({questionId:e})=>{this._tryCompleteTextResponse(e)})),this._eventUnsubscribers.push(this._context.eventBus.on("inner:conversation:answer:completed",({questionId:e})=>{this._tryCompleteTextResponse(e)})),this._eventUnsubscribers.push(this._context.eventBus.on("inner:conversation:server:message",({questionId:e})=>{this._tryCompleteTextResponse(e)}))}_subscribeAudioMetrics(){this._eventUnsubscribers.push(this._context.eventBus.on("inner:audio:speaking:changed",({isSpeaking:e})=>{e&&(this._tryCompleteTextToAudio(),this._tryCompleteNoSpeechToAudio())})),this._eventUnsubscribers.push(this._context.eventBus.on("inner:audio:no_speech:reported",({timestamp:e})=>{this._pendingNoSpeechStartAt===null&&(this._pendingNoSpeechStartAt=e)}))}_handleVideoFirstFrame(){this._hasReportedFirstFrame||this._connectStartAt===null||(this._hasReportedFirstFrame=!0,this._report({metric:"connect_to_first_frame_ms",durationMs:this._now()-this._connectStartAt,startedAt:this._connectStartAt,endedAt:this._now()}))}_tryCompleteTextResponse(e){const t=this._pendingTextResponse.get(e);if(t===void 0)return;this._pendingTextResponse.delete(e);const i=this._now();this._report({metric:"text_send_to_text_response_ms",durationMs:i-t,startedAt:t,endedAt:i,questionId:e})}_tryCompleteTextToAudio(){let e;for(;this._pendingTextAudioQueue.length>0;){const n=this._pendingTextAudioQueue.shift();if(n&&this._pendingTextAudioStartAt.has(n)){e=n;break}}if(!e)return;const t=this._pendingTextAudioStartAt.get(e);if(t===void 0)return;this._pendingTextAudioStartAt.delete(e);const i=this._now();this._report({metric:"text_send_to_audio_response_ms",durationMs:i-t,startedAt:t,endedAt:i,questionId:e})}_tryCompleteNoSpeechToAudio(){if(this._pendingNoSpeechStartAt===null)return;const e=this._pendingNoSpeechStartAt;this._pendingNoSpeechStartAt=null;const t=this._now();this._report({metric:"no_speech_report_to_audio_response_ms",durationMs:t-e,startedAt:e,endedAt:t})}_report(e){const t=this._reporter??this._defaultReporter;try{t(e)}catch(i){s.error("Performance metric reporter failed",i)}}_defaultReporter(e){s.info("[performance-metric]",e)}_unbindVideoElement(){this._videoElement&&this._videoFirstFrameHandler&&this._videoElement.removeEventListener("loadeddata",this._videoFirstFrameHandler),this._videoElement=null,this._videoFirstFrameHandler=null}_clearEventListeners(){this._eventUnsubscribers.forEach(e=>{try{e()}catch(t){s.error("Performance monitor unsubscribe failed",t)}}),this._eventUnsubscribers=[]}onDispose(){this._unbindVideoElement(),this._clearEventListeners(),this._pendingTextResponse.clear(),this._pendingTextAudioQueue.length=0,this._pendingTextAudioStartAt.clear(),this._pendingNoSpeechStartAt=null,this._reporter=null}}class C extends v{constructor(e,t=ve){super(),this._cachedConfig=null,this._cachedAt=null,this._inflightPromise=null,this._nextDirectConfig=null,this._httpController=e,this._ttlMs=t}async preConnect(){this.ensureNotDisposed();const e=this.getValidConfig();return e||(this._inflightPromise?this._inflightPromise:(this._inflightPromise=this._fetchConfig().finally(()=>{this._inflightPromise=null}),this._inflightPromise))}getValidConfig(){return this.ensureNotDisposed(),!this._cachedConfig||this._cachedAt===null||Date.now()-this._cachedAt>=this._ttlMs?null:this._cachedConfig}async refreshConfig(){return this.ensureNotDisposed(),this._cachedConfig=null,this._cachedAt=null,this._inflightPromise?this._inflightPromise:(this._inflightPromise=this._fetchConfig().finally(()=>{this._inflightPromise=null}),this._inflightPromise)}replaceDirectConfig(e){this.ensureNotDisposed();const t=this._validateDirectConfig(e);this._nextDirectConfig=t,this._cachedConfig=null,this._cachedAt=null}async _fetchConfig(){const e=this._context.options.connectConfig;if(e.type==="direct"){const t=this._nextDirectConfig??this._validateDirectConfig(e.config),i={livekitUrl:t.sfuUrl,token:t.clientToken};return this._cache(i),this._nextDirectConfig=null,i}return this._fetchAuthConfigWithRetry(e.config.authToken||"")}_fromAuthPayload(e){if(!e.livekitUrl||!e.roomToken)throw this._context.emitError("Invalid connection config payload",h.SDK_GET_LIVEKIT_CONFIG_FAILED);return{livekitUrl:e.livekitUrl,token:e.roomToken,roomId:e.roomId,videoOptions:{renderMode:e.greenScreen?.enabled?"processed":"raw",greenScreen:{enabled:!!e.greenScreen?.enabled,...e.greenScreen}}}}_cache(e){this._cachedConfig=e,this._cachedAt=Date.now()}async _fetchAuthConfigWithRetry(e){let t=null;for(let i=1;i<=C.AUTH_RETRY_MAX_ATTEMPTS;i++)try{const n=this._context.authToken||e;this._context.setAuthToken(n),await this._httpController.getAuthToken();const r=await this._httpController.fetchConnectionConfig(),a=this._fromAuthPayload(r);return this._cache(a),a}catch(n){if(t=n,i>=C.AUTH_RETRY_MAX_ATTEMPTS)break;await this._delay(C.AUTH_RETRY_BASE_DELAY_MS*i)}throw this._context.emitError(t||"Failed to fetch auth connection config",h.SDK_GET_LIVEKIT_CONFIG_FAILED)}_delay(e){return new Promise(t=>setTimeout(t,e))}_validateDirectConfig(e){const t=(e.sfuUrl||"").trim(),i=(e.clientToken||"").trim();if(!t||!i)throw this._context.emitError("Invalid direct connection config",h.SDK_PRECONNECT_FAILED);return{sfuUrl:t,clientToken:i}}onDispose(){this._cachedConfig=null,this._cachedAt=null,this._inflightPromise=null,this._nextDirectConfig=null}}C.AUTH_RETRY_MAX_ATTEMPTS=2;C.AUTH_RETRY_BASE_DELAY_MS=300;class ht extends _{constructor(e){super(),this._videoController=null,this._audioController=null,this._conversationController=null,this._cameraController=null,this._liveKitService=null,this._connectionCoordinator=null,this._httpController=null,this._configManager=null,this._performanceMonitor=null,this._pendingDirectConfig=null,this._isReconnectInProgress=!1,this._reconnectPromise=null,this._liveKitProtocolDispatcher=null,this._context=new Ee(e),s.info("SDK Client created")}get events(){return this.ensureNotDisposed(),this._publicEventAPI||(this._publicEmitter=new rt(this._context.eventBus),this._publicEventAPI=this._publicEmitter.publicAPI()),this._publicEventAPI}sendTextQuestion(e){if(this.ensureNotDisposed(),this._ensureConnected(),this._conversationController)return this._conversationController.sendQuestion(e);throw new l(h.CONVERSATION_CONTROLLER_NOT_AVAILABLE,"Conversation controller is not available")}async interrupt(){if(this.ensureNotDisposed(),this._ensureConnected(),this._conversationController)return this._conversationController.interrupt();throw new l(h.CONVERSATION_CONTROLLER_NOT_AVAILABLE,"Conversation controller is not available")}setPerformanceMetricReporter(e){if(this.ensureNotDisposed(),!this._performanceMonitor){this._performanceMonitor=new Q(e),this._performanceMonitor.setContext(this._context);return}this._performanceMonitor.setReporter(e)}setRenderFitMode(e){this.ensureNotDisposed(),this._ensureConnected(),this._videoController&&this._videoController.setRenderFitMode(e)}async startAudioCapture(){this.ensureNotDisposed(),this._ensureConnected(),await this._startAudioCaptureRtc()}async stopAudioCapture(){this.ensureNotDisposed(),this._ensureConnected(),await this._stopAudioCaptureRtc()}setVolume(e){this.ensureNotDisposed(),this._ensureConnected(),this._requireAudioController().setVolume(e)}getVolume(){return this.ensureNotDisposed(),this._ensureConnected(),this._requireAudioController().getVolume()||1}mute(){this.ensureNotDisposed(),this._ensureConnected(),this._requireAudioController().mute()}unmute(){this.ensureNotDisposed(),this._ensureConnected(),this._requireAudioController().unmute()}get isMuted(){return this.ensureNotDisposed(),this._ensureConnected(),this._requireAudioController().isMuted()}get isAudioCapturing(){return this.ensureNotDisposed(),this._ensureConnected(),this._liveKitService?.isMicrophoneActive()??!1}setAuthToken(e){this.ensureNotDisposed(),this._context.setAuthToken(e),this._httpController&&this._httpController.getAuthToken()}updateConnectionConfig(e){if(this.ensureNotDisposed(),this._context.options.connectConfig.type!=="direct")throw this._context.emitError(new l(h.SDK_INVALID_STATE_TRANSITION,"updateConnectionConfig is only available in direct mode"),h.SDK_INVALID_STATE_TRANSITION);this._pendingDirectConfig=this._validateDirectConnectionConfig(e)}async startCamera(){this.ensureNotDisposed(),this._ensureConnected(),await this._requireLiveKitService().startCamera()}stopCamera(){this.ensureNotDisposed(),this._ensureConnected(),this._liveKitService?.stopCamera()}getCameraStream(){return this.ensureNotDisposed(),this._ensureConnected(),this._cameraController?.getStream()??null}getCameraTrack(){return this.ensureNotDisposed(),this._ensureConnected(),this._cameraController?.getTrack()??null}attachCameraTo(e){this.ensureNotDisposed(),this._ensureConnected(),this._requireLiveKitService().attachCameraTo(e)}async preConnect(){this.ensureNotDisposed();try{this._ensureConfigManagerReady();const e=await this._configManager.preConnect();return this._applyConnectionConfig(e),!0}catch(e){throw this._context.emitError(e,h.SDK_PRECONNECT_FAILED)}}async connect(){if(this.ensureNotDisposed(),this._ensurePerformanceMonitorReady(),this._performanceMonitor?.startConnectMeasurement(),this._connectionCoordinator&&this._context.sessionState.isConnected){s.warn("SDK already initialized");return}try{this._ensureConfigManagerReady();const e=this._configManager.getValidConfig();if(e)this._applyConnectionConfig(e);else if(!await this.preConnect())throw new l(h.SDK_CONNECT_FAILED,"PreConnect failed without explicit error")}catch(e){throw this._context.emitError(e,h.SDK_CONNECT_FAILED)}try{await this._disposeConnectionRuntimeBeforeConnect(),s.info("Initializing SDK..."),this._connectionCoordinator=new Te,this._connectionCoordinator.setContext(this._context),this._context.bindConnectionState(()=>this._connectionCoordinator.getState()),this._connectionCoordinator.start(),this._connectionCoordinator.startConnecting(),this._bootstrapDomainControllers(),this._bootstrapLiveKitService(),this._wireLiveKitEventAdapterToControllers(),this._setupRtcProtocolDispatcher(),await this._establishMediaAndSessionTransports(),s.info("SDK initialized and connected")}catch(e){throw this._connectionCoordinator&&this._connectionCoordinator.transitionToIdle(),await this._cleanup(),this._context.emitError(e,h.SDK_INITIALIZATION_FAILED)}}async disconnect(){if(this.ensureNotDisposed(),!(!this._connectionCoordinator||this._connectionCoordinator.getState()==="idle"||this._connectionCoordinator.getState()==="disposed")){s.info("Disconnecting SDK..."),this._connectionCoordinator.startDisconnecting();try{this._audioController&&this._audioController.stopWsCapture(),this._liveKitService&&(await this._liveKitService.sendSystemEvent("session.stop"),await this._liveKitService.disconnect()),this._cameraController&&this._cameraController.stopCamera(),this._connectionCoordinator.completeDisconnecting(),s.info("SDK disconnected")}catch(e){throw s.error("Error during disconnect:",e),this._context.emitError(e,h.SDK_DISCONNECT_FAILED)}}}async reconnect(){if(this.ensureNotDisposed(),this._reconnectPromise)return this._reconnectPromise;const e=this._connectionCoordinator;if(!e?.canStartReconnectingManual()){const i=e?e.getSnapshot():this.connectionSnapshot;return s.warn("SDK reconnect not allowed in current state, returning current snapshot"),i}const t=e.captureSnapshotAndStartReconnectingManual();return this._reconnectPromise=(async()=>{this._isReconnectInProgress=!0;try{return await this.disconnect(),this._stageDirectConfigForNextReconnect(),await this._refreshConnectionConfigForReconnect(),await this.connect(),t}catch(i){throw s.error("Error during reconnect:",i),this._context.emitError(i,h.SDK_RECONNECT_FAILED)}finally{this._isReconnectInProgress=!1,this._reconnectPromise=null}})(),this._reconnectPromise}get connectionSnapshot(){return this.ensureNotDisposed(),this._connectionCoordinator?this._connectionCoordinator.getSnapshot():J(Y("idle"))}async _cleanup(){if(this._conversationController?.setRtcTextSender(null),this._liveKitService?.setProtocolMessageDispatcher(null),this._liveKitProtocolDispatcher&&(this._liveKitProtocolDispatcher.dispose(),this._liveKitProtocolDispatcher=null),this._audioController&&this._audioController.stopWsCapture(),this._liveKitService)try{await this._liveKitService.disconnect()}catch(e){s.error("Error disconnecting LiveKit:",e)}this._cameraController&&this._cameraController.stopCamera()}async _disposeConnectionRuntimeBeforeConnect(){await this._cleanup(),this._conversationController?.setRtcTextSender(null),this._liveKitService?.setProtocolMessageDispatcher(null),this._liveKitProtocolDispatcher&&(this._liveKitProtocolDispatcher.dispose(),this._liveKitProtocolDispatcher=null),this._videoController?.dispose(),this._videoController=null,this._audioController?.dispose(),this._audioController=null,this._cameraController?.dispose(),this._cameraController=null,this._conversationController?.dispose(),this._conversationController=null,this._liveKitService?.dispose(),this._liveKitService=null,this._connectionCoordinator?.dispose(),this._connectionCoordinator=null}_bootstrapDomainControllers(){this._videoController=new ye(this._context.options.video),this._videoController.setContext(this._context),this._performanceMonitor?.bindVideoElement(this._videoController.getSource().getInternalElement()),this._audioController=new Fe(this._context.options.audio),this._audioController.setContext(this._context),this._cameraController=new Qe,this._cameraController.setContext(this._context),this._conversationController=new et,this._conversationController.setContext(this._context)}_bootstrapLiveKitService(){this._liveKitService=new st,this._liveKitService.setContext(this._context)}_wireLiveKitEventAdapterToControllers(){if(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))}}_setupRtcProtocolDispatcher(){this._liveKitProtocolDispatcher&&(this._liveKitProtocolDispatcher.dispose(),this._liveKitProtocolDispatcher=null),this._liveKitProtocolDispatcher=new nt,this._liveKitProtocolDispatcher.setContext(this._context),this._liveKitProtocolDispatcher.setConversationManager(this._conversationController.manager),this._liveKitService.setProtocolMessageDispatcher(e=>{this._liveKitProtocolDispatcher.dispatch(e)}),this._conversationController.setRtcTextSender(this._liveKitService)}async _establishMediaAndSessionTransports(){await this._liveKitService.connect(this._context.livekitUrl,this._context.token,this._context.options.connectConfig.type==="auth"?this._context.options.connectConfig.config.avatarId:"")}async _refreshConnectionConfigForReconnect(){this._ensureConfigManagerReady();const e=await this._configManager.refreshConfig();this._applyConnectionConfig(e)}_ensureConfigManagerReady(){s.info("Creating HTTP controller..."),this._httpController=this._httpController??new ct(this._context.options.http?.baseURL,this._context.options.http?.headers),this._httpController.setContext(this._context),this._configManager=this._configManager??new C(this._httpController),this._configManager.setContext(this._context)}_applyConnectionConfig(e){this._context.setLivekitConfig(e.livekitUrl,e.token,e.roomId||""),e.videoOptions&&this._context.setVideoOptions(e.videoOptions),this._context.options.connectConfig.type==="auth"&&this._context.eventBus.emit("inner:sdk:connected",{source:"http"})}_stageDirectConfigForNextReconnect(){this._context.options.connectConfig.type==="direct"&&this._pendingDirectConfig&&(this._ensureConfigManagerReady(),this._configManager.replaceDirectConfig(this._pendingDirectConfig),this._pendingDirectConfig=null)}_validateDirectConnectionConfig(e){const t=(e?.sfuUrl||"").trim(),i=(e?.clientToken||"").trim();if(!t||!i)throw this._context.emitError(new l(h.SDK_PRECONNECT_FAILED,"Invalid direct connection config"),h.SDK_PRECONNECT_FAILED);return{sfuUrl:t,clientToken:i}}_ensurePerformanceMonitorReady(){const e=this._context.options.performanceMonitor;if(!(e?.enabled!==!1)){this._performanceMonitor?.dispose(),this._performanceMonitor=null;return}if(this._performanceMonitor){this._performanceMonitor.setReporter(e?.reporter);return}this._performanceMonitor=new Q(e?.reporter),this._performanceMonitor.setContext(this._context)}get isConnected(){return this.ensureNotDisposed(),this._context.sessionState.isConnected}_ensureConnected(){if(!this._context.sessionState.isConnected)throw new l(h.SDK_NOT_CONNECTED,"SDK is not connected. Call connect() first.")}async _startAudioCaptureRtc(){await this._requireLiveKitService().startMicrophone()}async _stopAudioCaptureRtc(){await this._requireLiveKitService().stopMicrophone()}_requireAudioController(){if(!this._audioController)throw new l(h.AUDIO_CONTROLLER_NOT_AVAILABLE,"Audio controller is not available");return this._audioController}_requireLiveKitService(){if(!this._liveKitService)throw new l(h.LIVEKIT_CONNECT_FAILED,"LiveKit service is not available");return this._liveKitService}onDispose(){this.disconnect().catch(e=>{s.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?.setRtcTextSender(null),this._liveKitService?.setProtocolMessageDispatcher(null),this._liveKitProtocolDispatcher&&(this._liveKitProtocolDispatcher.dispose(),this._liveKitProtocolDispatcher=null),this._conversationController&&(this._conversationController.dispose(),this._conversationController=null),this._liveKitService&&(this._liveKitService.dispose(),this._liveKitService=null),this._httpController&&(this._httpController.dispose(),this._httpController=null),this._configManager&&(this._configManager.dispose(),this._configManager=null),this._performanceMonitor&&(this._performanceMonitor.dispose(),this._performanceMonitor=null),this._connectionCoordinator&&(this._connectionCoordinator.transitionToDisposed(),this._connectionCoordinator.dispose(),this._connectionCoordinator=null),this._context.dispose(),s.info("SDK Client disposed")}}function mt(o){const e=o.connectConfig;if(e.type==="direct"&&(!e.config.sfuUrl||!e.config.clientToken))throw new l(h.INVALID_CONNECT_CONFIG,"Invalid direct connect config");if(e.type==="auth"&&!e.config.avatarId)throw new l(h.NO_AVATARID,"avatarId is required");return new ht(o)}class X extends _{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)),s.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 i=this._videoElement,n=()=>{i.removeEventListener("loadedmetadata",n),i.removeEventListener("error",r),s.info(`Video loaded: ${i.videoWidth}x${i.videoHeight}`),e()},r=a=>{i.removeEventListener("loadedmetadata",n),i.removeEventListener("error",r),t(new Error(`Failed to load video: ${a}`))};i.addEventListener("loadedmetadata",n),i.addEventListener("error",r),i.src=this._videoUrl||"",i.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,s.info("Video playback started")}catch(e){throw s.error("Failed to play video:",e),e}}pause(){this._videoElement&&this._isPlaying&&(this._videoElement.pause(),this._isPlaying=!1,s.info("Video playback paused"))}stop(){this._videoElement&&(this._videoElement.pause(),this._videoElement.currentTime=0,this._isPlaying=!1,s.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(i){s.error("",i)}const e=await createImageBitmap(this._videoElement),t=new VideoFrame(e,{timestamp:this._videoElement.currentTime*1e6});return e.close(),t}catch(e){return s.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 i=(n,r)=>{!this._isPlaying||!this._videoElement||this.captureFrame().then(a=>{a&&(e(a),a.close()),this._isPlaying&&this._videoElement&&(this._frameCallbackId=t.requestVideoFrameCallback(i))}).catch(a=>{s.error("Error in frame callback:",a)})};this._frameCallbackId=t.requestVideoFrameCallback(i)}else{const i=()=>{!this._isPlaying||!this._videoElement||this.captureFrame().then(n=>{n&&(e(n),n.close()),this._isPlaying&&this._videoElement&&(this._frameCallbackId=requestAnimationFrame(i))}).catch(n=>{s.error("Error in frame callback:",n)})};this._frameCallbackId=requestAnimationFrame(i)}}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,s.info("Local Video Debug Source disposed")}}class dt extends _{constructor(e,t,i){super(),this._outputCanvas=null,this._outputRenderer=null,this._source=new ee;const n={...e,renderMode:"processed"};this._pipeline=new ie(this._source,n),this._outputRenderer=new L(i,t),this._pipeline.setRenderer(this._outputRenderer),this._outputCanvas=this._outputRenderer.getCanvas(),s.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),s.info("Processor set on pipeline")}async processFrame(e){if(this.ensureNotDisposed(),!this._outputRenderer){s.error("Output renderer not available"),e.close();return}try{const t=this._pipeline.strategy.getProcessors()[0]||null;let i=null;if(t){try{i=t.process(e)}catch(n){s.error("Error processing frame:",n),i=e}if(i===null&&"getImageData"in t){const n=t.getImageData();if(n&&this._outputCanvas&&this._outputRenderer.getContext()){const r=this._outputRenderer.getContext();r&&((this._outputCanvas.width!==n.width||this._outputCanvas.height!==n.height)&&(this._outputCanvas.width=n.width,this._outputCanvas.height=n.height),r.putImageData(n,0,0))}e.close();return}i&&i!==e?(this._outputRenderer.render(i),e.close()):i===e?this._outputRenderer.render(e):e.close()}else this._outputRenderer.render(e)}catch(t){s.error("Error in processFrame:",t),e.close()}}setOutputSize(e,t){this._outputCanvas&&(this._outputCanvas.width=e,this._outputCanvas.height=t,s.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,s.info("Video Pipeline Runner disposed")}}class ft extends _{constructor(e,t,i,n){if(super(),this._isRunning=!1,this._videoSource=new X(e,t),this._options={renderMode:"processed",containerElement:t,...n},this._pipelineRunner=new dt(this._options,t,i),this._options.greenScreen?.enabled){const r=new O(this._options.greenScreen);this._pipelineRunner.setProcessor(r),s.info("Green screen processor enabled in debugger")}s.info("Standalone Video Debugger created")}async switchSource(e,t){this.ensureNotDisposed();const i=this._videoSource,n=this._isRunning,r=new X(e,t);try{await r.load(),n&&await r.play(),r.onFrame(d=>{this._pipelineRunner.processFrame(d).catch(p=>{s.error("Error processing frame:",p)})}),this._videoSource=r;try{i.offFrame(),i.stop(),i.dispose()}catch(d){s.warn("Error disposing old video source during switch:",d)}const a=this._videoSource.videoWidth,c=this._videoSource.videoHeight;a>0&&c>0&&this._pipelineRunner.setOutputSize(a,c)}catch(a){s.error("Failed to switch video source:",a);try{r.dispose()}catch(c){s.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),s.info("Green screen options updated");else{const i=this._options.greenScreen||{enabled:!1},n={...i,...e,enabled:e.enabled!==void 0?e.enabled:i.enabled},r=new O(n);this._pipelineRunner.setProcessor(r),this._options.greenScreen=n,s.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),s.info("Video debugger initialized")}catch(e){throw s.error("Failed to initialize video debugger:",e),e}}async start(){if(this.ensureNotDisposed(),this._isRunning){s.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=>{s.error("Error processing frame:",t)})}),this._isRunning=!0,s.info("Video debugger started")}catch(e){throw s.error("Failed to start video debugger:",e),e}}stop(){this._isRunning&&(this._videoSource.stop(),this._videoSource.offFrame(),this._isRunning=!1,s.info("Video debugger stopped"))}pause(){this._isRunning&&(this._videoSource.pause(),s.info("Video debugger paused"))}async resume(){this._isRunning&&(await this._videoSource.play(),s.info("Video debugger resumed"))}get isRunning(){return this._isRunning}onDispose(){this.stop(),this._videoSource.dispose(),this._pipelineRunner.dispose(),s.info("Standalone Video Debugger disposed")}}export{u as ConversationStateType,pt as DEFAULT_AUDIO_BUFFER_SIZE,_t as DEFAULT_AUDIO_CHANNELS,V as DEFAULT_AUDIO_SAMPLE_RATE,k as DEFAULT_RECONNECT_ATTEMPTS,M as DEFAULT_RECONNECT_DELAY,ut as DEFAULT_WS_TIMEOUT,_ as Disposable,de as EventBus,E as LogLevel,s as Logger,ht as SDKClient,l as SDKError,ue as SessionState,m as SessionStatus,ft as StandaloneVideoDebugger,mt as createClient};
|