@sanseng/livekit-ws-sdk 0.1.9 → 0.1.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs.js +3 -3
- package/dist/index.d.ts +57 -2
- package/dist/index.esm.js +3 -3
- package/dist/index.full.umd.js +7 -7
- package/dist/index.umd.js +1 -1
- package/package.json +1 -1
package/dist/index.cjs.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";var f=require("livekit-client"),d;(function(o){o.SDK_AUTH_TOKEN_FAILED="SDK_AUTH_TOKEN_FAILED",o.SDK_GET_LIVEKIT_CONFIG_FAILED="SDK_GET_LIVEKIT_CONFIG_FAILED",o.SDK_RECONNECT_FAILED="SDK_RECONNECT_FAILED",o.SDK_DISCONNECT_FAILED="SDK_DISCONNECT_FAILED",o.SDK_NOT_CONNECTED="SDK_NOT_CONNECTED",o.SDK_CONNECT_FAILED="SDK_CONNECT_FAILED",o.SDK_SWITCH_VIDEO_FAILED="SDK_SWITCH_VIDEO_FAILED",o.SDK_ERROR="SDK_ERROR",o.SDK_PRECONNECT_FAILED="SDK_PRECONNECT_FAILED",o.SDK_INTERRUPT_CONVERSATION_FAILED="SDK_INTERRUPT_CONVERSATION_FAILED",o.SDK_INITIALIZATION_FAILED="SDK_INITIALIZATION_FAILED",o.SDK_INVALID_STATE_TRANSITION="SDK_INVALID_STATE_TRANSITION",o.NO_AVATARNAME="NO_AVATARNAME",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.WS_CONNECT_FAILED="WS_CONNECT_FAILED",o.WS_TEXT_SEND_FAILED="WS_TEXT_SEND_FAILED",o.WS_PARSE_ERROR="WS_PARSE_ERROR",o.WS_NOT_CONNECTED="WS_NOT_CONNECTED",o.WS_CONNECT_ERROR="WS_CONNECT_ERROR",o.LIVEKIT_CONNECT_FAILED="LIVEKIT_CONNECT_FAILED",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"})(d||(d={}));class u extends Error{constructor(e,t="SDK_ERROR",s){super(e),this.name="SDKError",this.code=t,this.cause=s,Object.setPrototypeOf(this,u.prototype)}static fromError(e,t){return e instanceof u?e:e instanceof Error?new u(e.message,t||d.SDK_ERROR,e):typeof e=="string"?new u(e,t||d.SDK_ERROR):new u("Unknown error",t||d.SDK_ERROR,e)}}class h{constructor(){this._disposed=!1}get isDisposed(){return this._disposed}dispose(){this._disposed||(this.onDispose(),this._disposed=!0)}ensureNotDisposed(){if(this._disposed)throw new u("Object has been disposed",d.OBJECT_DISPOSED)}}exports.LogLevel=void 0;(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"})(exports.LogLevel||(exports.LogLevel={}));class i{static setLevel(e){this._level=e}static setPrefix(e){this._prefix=e}static debug(e,...t){this._level<=exports.LogLevel.DEBUG&&console.debug(`${this._prefix} [DEBUG]`,e,...t)}static info(e,...t){this._level<=exports.LogLevel.INFO&&console.info(`${this._prefix} [INFO]`,e,...t)}static warn(e,...t){this._level<=exports.LogLevel.WARN&&console.warn(`${this._prefix} [WARN]`,e,...t)}static error(e,...t){this._level<=exports.LogLevel.ERROR&&console.error(`${this._prefix} [ERROR]`,e,...t)}}i._level=exports.LogLevel.ERROR;i._prefix="[SDK]";class M extends h{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(r){i.error(`Error in event listener for ${String(e)}:`,r)}}))}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()}}exports.SessionStatus=void 0;(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"})(exports.SessionStatus||(exports.SessionStatus={}));function Q(o){switch(o){case"idle":return exports.SessionStatus.IDLE;case"connecting":return exports.SessionStatus.CONNECTING;case"connected":return exports.SessionStatus.CONNECTED;case"partial":return exports.SessionStatus.PARTIAL;case"disconnecting":return exports.SessionStatus.DISCONNECTING;case"disconnected":case"error":return exports.SessionStatus.DISCONNECTED;case"reconnecting:auto":case"reconnecting:manual":return exports.SessionStatus.RECONNECTING;case"disposed":return exports.SessionStatus.DISPOSED;default:return exports.SessionStatus.DISCONNECTED}}class V{constructor(){this.status=exports.SessionStatus.DISCONNECTED,this.state=exports.SessionStatus.DISCONNECTED,this.isConnected=!1}reset(){this.status=exports.SessionStatus.DISCONNECTED,this.state=exports.SessionStatus.DISCONNECTED,this.isConnected=!1}}const S=3,Y=5e3,T=1e3,y=10,X=1e4,J=3e3,N=3e4,R=24e3,I=16,Z=1,ee=4096,C=0,te="https://api.newportai.com/s2/aigc/api/vih_dispatcher",se="wss://api.newportai.com/s2/livekit-lisbon",ie="wss://api.newportai.com/s2/aigc/api/ws_dispatch/render/v1/stream",ne=6e4;var v;(function(o){o.NO_VIDEO_TRACK="NO_VIDEO_TRACK",o.TRACK_MUTED="TRACK_MUTED",o.TRACK_ENDED="TRACK_ENDED"})(v||(v={}));class re{constructor(e){this.options=e,this._livekitUrl="",this._token="",this._wsUrl="",this.eventBus=new M,this.sessionState=new V,e.debug&&i.setLevel(0),i.info("SDK Context initialized")}setLivekitConfig(e,t,s){this._livekitUrl=e||se,this._token=t,this._roomId=s}emitError(e,t){const s=u.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}}dispose(){this.eventBus.dispose(),this.sessionState.reset(),i.info("SDK Context disposed")}}function U(o){return{ws:{connected:!1},rtc:{connected:!1,hasVideoTrack:!1},overall:{state:o}}}function K(o){return Object.freeze({ws:Object.freeze({...o.ws}),rtc:Object.freeze({...o.rtc}),overall:Object.freeze({...o.overall})})}class W{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),r=n?Array.from(n).join(", "):"none";throw t?.error??new u(`Invalid state transition: cannot transition from "${s}" to "${e}". Current state: "${s}". Allowed transitions: [${r}]`,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 oe extends W{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 ae extends h{constructor(e){super(),this._unsubscribes=[],this._context=e,this._fsm=new oe,this._snapshot=U("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",l=>{this._snapshot.rtc.connected=!1,this._snapshot.rtc.hasVideoTrack=!1,this._context.eventBus.emit("inner:sdk:disconnected",{source:"livekit",reason:l?.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:ws:connected",()=>{this._snapshot.ws.connected=!0,this._context.eventBus.emit("inner:sdk:connected",{source:"ws"}),this._onFactsChanged()}),c=e.on("inner:ws:disconnected",l=>{this._snapshot.ws.connected=!1,this._context.eventBus.emit("inner:sdk:disconnected",{source:"ws",reason:l?.reason}),this._onFactsChanged()});this._unsubscribes=[t,s,n,r,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 K(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=Q(e),this._context.sessionState.state=this._context.sessionState.status,this._context.sessionState.isConnected=e==="connected"}onDispose(){this._unsubscribes.forEach(e=>e()),this._unsubscribes=[]}}class B extends h{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 r of n)if(r!==e)try{this._stream.removeTrack(r)}catch(a){i.warn("Failed to remove track from stream",a)}if(!this._stream.getTracks().includes(e))try{this._stream.addTrack(e)}catch(r){i.error("Failed to add track to media stream:",r)}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 h{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 de{create(){return new ce}}class D extends h{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 r=e/t,a=s/n;let c,l;r>a?(c=s,l=s/r):(c=n*r,l=n),this._canvas.style.width=`${c}px`,this._canvas.style.height=`${l}px`;const p=(s-c)/2,m=(n-l)/2;this._canvas.style.left=`${p}px`,this._canvas.style.top=`${m}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,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){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 $ extends h{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){console.error(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){console.error(s)}}this._track=null}}class he extends h{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 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 $){if(this._videoElementRenderer=e,!this._canvasRenderer){const t=this._videoElementRenderer.getElement();if(t)try{t.parentElement&&(this._canvasRenderer=new D(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 D(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 le{create(){return new he}}class k{static getFactory(e){return e==="processed"?new le:new de}}class H extends h{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=k.getFactory(s),this._strategy=this._strategyFactory.create();const n=this._source.getInternalElement(),r=new $(n);this._videoElementRenderer=r,this._strategy.setRenderer(r),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=k.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 ue extends h{onDispose(){}}class w extends ue{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
|
+
"use strict";var f=require("livekit-client"),h;(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"})(h||(h={}));class u extends Error{constructor(e,t="SDK_ERROR",s){super(e),this.name="SDKError",this.code=t,this.cause=s,Object.setPrototypeOf(this,u.prototype)}static fromError(e,t){return e instanceof u?e:e instanceof Error?new u(e.message,t||h.SDK_ERROR,e):typeof e=="string"?new u(e,t||h.SDK_ERROR):new u("Unknown error",t||h.SDK_ERROR,e)}}class l{constructor(){this._disposed=!1}get isDisposed(){return this._disposed}dispose(){this._disposed||(this.onDispose(),this._disposed=!0)}ensureNotDisposed(){if(this._disposed)throw new u("Object has been disposed",h.OBJECT_DISPOSED)}}exports.LogLevel=void 0;(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"})(exports.LogLevel||(exports.LogLevel={}));class i{static setLevel(e){this._level=e}static setPrefix(e){this._prefix=e}static debug(e,...t){this._level<=exports.LogLevel.DEBUG&&console.debug(`${this._prefix} [DEBUG]`,e,...t)}static info(e,...t){this._level<=exports.LogLevel.INFO&&console.info(`${this._prefix} [INFO]`,e,...t)}static warn(e,...t){this._level<=exports.LogLevel.WARN&&console.warn(`${this._prefix} [WARN]`,e,...t)}static error(e,...t){this._level<=exports.LogLevel.ERROR&&console.error(`${this._prefix} [ERROR]`,e,...t)}}i._level=exports.LogLevel.ERROR;i._prefix="[SDK]";class q extends l{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()}}exports.SessionStatus=void 0;(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"})(exports.SessionStatus||(exports.SessionStatus={}));function ie(r){switch(r){case"idle":return exports.SessionStatus.IDLE;case"connecting":return exports.SessionStatus.CONNECTING;case"connected":return exports.SessionStatus.CONNECTED;case"partial":return exports.SessionStatus.PARTIAL;case"disconnecting":return exports.SessionStatus.DISCONNECTING;case"disconnected":case"error":return exports.SessionStatus.DISCONNECTED;case"reconnecting:auto":case"reconnecting:manual":return exports.SessionStatus.RECONNECTING;case"disposed":return exports.SessionStatus.DISPOSED;default:return exports.SessionStatus.DISCONNECTED}}class z{constructor(){this.status=exports.SessionStatus.DISCONNECTED,this.state=exports.SessionStatus.DISCONNECTED,this.isConnected=!1}reset(){this.status=exports.SessionStatus.DISCONNECTED,this.state=exports.SessionStatus.DISCONNECTED,this.isConnected=!1}}const A=3,ne=5e3,T=1e3,I=10,re=1e4,oe=3e3,O=3e4,y=24e3,x=16,ae=1,ce=4096,C=0,de="https://api.newportai.com/s2/aigc/api/vih_dispatcher",he="wss://api.newportai.com/s2/livekit-lisbon",le="wss://api.newportai.com/s2/aigc/api/ws_dispatch/render/v1/stream",ue=6e4;var g;(function(r){r.NO_VIDEO_TRACK="NO_VIDEO_TRACK",r.TRACK_MUTED="TRACK_MUTED",r.TRACK_ENDED="TRACK_ENDED"})(g||(g={}));const S=.7;class _e{constructor(e){this.options=e,this._livekitUrl="",this._token="",this._wsUrl="",this.eventBus=new q,this.sessionState=new z,e.debug&&i.setLevel(0),i.info("SDK Context initialized")}setLivekitConfig(e,t,s){this._livekitUrl=e||he,this._token=t,this._roomId=s}emitError(e,t){const s=u.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}}dispose(){this.eventBus.dispose(),this.sessionState.reset(),i.info("SDK Context disposed")}}function G(r){return{ws:{connected:!1},rtc:{connected:!1,hasVideoTrack:!1},overall:{state:r}}}function j(r){return Object.freeze({ws:Object.freeze({...r.ws}),rtc:Object.freeze({...r.rtc}),overall:Object.freeze({...r.overall})})}class Q{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 u(`Invalid state transition: cannot transition from "${s}" to "${e}". Current state: "${s}". Allowed transitions: [${o}]`,h.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 pe extends Q{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 me extends l{constructor(e){super(),this._unsubscribes=[],this._context=e,this._fsm=new pe,this._snapshot=G("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",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}),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",d=>{this._snapshot.ws.connected=!1,this._context.eventBus.emit("inner:sdk:disconnected",{source:"ws",reason:d?.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 j(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=ie(e),this._context.sessionState.state=this._context.sessionState.status,this._context.sessionState.isConnected=e==="connected"}onDispose(){this._unsubscribes.forEach(e=>e()),this._unsubscribes=[]}}class X extends l{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 fe extends l{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 ve{create(){return new fe}}class b extends l{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,d;o>a?(c=s,d=s/o):(c=n*o,d=n),this._canvas.style.width=`${c}px`,this._canvas.style.height=`${d}px`;const p=(s-c)/2,m=(n-d)/2;this._canvas.style.left=`${p}px`,this._canvas.style.top=`${m}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 Y extends l{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){console.error(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){console.error(s)}}this._track=null}}class ge extends l{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 Y){if(this._videoElementRenderer=e,!this._canvasRenderer){const t=this._videoElementRenderer.getElement();if(t)try{t.parentElement&&(this._canvasRenderer=new b(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 b(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 Ee{create(){return new ge}}class L{static getFactory(e){return e==="processed"?new Ee:new ve}}class J extends l{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=L.getFactory(s),this._strategy=this._strategyFactory.create();const n=this._source.getInternalElement(),o=new Y(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=L.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 Ce extends l{onDispose(){}}class N extends Ce{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=`
|
|
2
2
|
attribute vec2 aPosition;
|
|
3
3
|
attribute vec2 aTexCoord;
|
|
4
4
|
varying vec2 vTexCoord;
|
|
@@ -52,7 +52,7 @@
|
|
|
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]),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: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 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 s=this._gl,n=(a,c)=>{const l=s.createShader(a);return s.shaderSource(l,c),s.compileShader(l),l},r=s.createProgram();return s.attachShader(r,n(s.VERTEX_SHADER,e)),s.attachShader(r,n(s.FRAGMENT_SHADER,t)),s.linkProgram(r),r}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 _e extends h{constructor(e,t){if(super(),this._context=e,this._source=new B,this._pipeline=new H(this._source,t),t?.renderMode==="processed"&&t?.greenScreen?.enabled){const s=new w(t.greenScreen);this._pipeline.setProcessor(s),i.info("Green screen processor enabled")}this._pipeline.setRenderFitMode(t?.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 pe extends h{process(e,t){return this.ensureNotDisposed(),e}onDispose(){}}class me extends h{set onProcessedAudio(e){this._onProcessedAudio=e}constructor(e){super(),this._processors=[],this._processingStrategy=new pe,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(r){i.error("Error in audio processor:",r);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 fe extends h{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 u("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 ge=1,ve=0,Ee={0:0,1:1,2:2};function Ce(o){return o<0?0:o>4095?4095:Math.floor(o)}function Te(o){if(o<0)return 0;const e=1048575;return o>e?o&e:Math.floor(o)}function Se(o){return o<0?0:o>4095?4095:Math.floor(o)}function Ae(o){return o<0?0:o>65535?65535:Math.floor(o)}function Re(o){if(o!==0&&o!==1&&o!==2)throw new u(`Invalid sample rate encoding: ${o}. Must be 0 (16kHz), 1 (24kHz), or 2 (48kHz)`,d.AUDIO_INVALID_SAMPLE_RATE)}function De(o){Re(o.sampleRate);const e=Ce(o.sequenceNumber),t=Te(o.timestamp),s=Se(o.frameSize),n=Ae(o.payloadLength);if(o.channel!==0&&o.channel!==1)throw new u(`Invalid channel value: ${o.channel}. Must be 0 (mono) or 1 (stereo)`,d.AUDIO_INVALID_CHANNEL);const r=new Uint8Array(9);let a=ge<<6;a|=o.channel<<5,a|=(o.keyFrame?1:0)<<4,a|=e>>8&15,r[0]=a,r[1]=e&255,r[2]=t>>12&255,r[3]=t>>4&255;const c=Ee[o.sampleRate];let l=(t&15)<<4;l|=c<<2,l|=s>>10&3,r[4]=l,r[5]=s>>2&255;let p=(s&3)<<6;return p|=ve&63,r[6]=p,r[7]=n>>8&255,r[8]=n&255,r}class we{constructor(e){this.transport=e}sendBinary(e){this.transport.sendBinary(e)}isConnected(){return this.transport.isConnected}}class ye extends h{constructor(e,t){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._context=e,this._options=t;const s=t?.input;this._targetSampleRate=s?.sampleRate||24e3,this._bitDepth=s?.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 we(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),r=Math.ceil(this._bitDepth/8),a=n.length*r,c=this._getSampleRateEncoding(this._targetSampleRate),l=Math.floor(Date.now()-this._audioStartTime),p={channel:(this._options?.input?.channels||1)===1?0:1,keyFrame:!!this._isKeyFrame,sequenceNumber:this._sequenceNumber,timestamp:l,sampleRate:c,frameSize:t.length,payloadLength:a},m=this._pcmToUint8Array(n,this._bitDepth);if(setTimeout(()=>{try{this._context.eventBus.emit("inner:audio:payload:send",{payload:m,sampleRate:this._targetSampleRate,channels:this._options?.input?.channels||1,sequenceNumber:p.sequenceNumber,timestamp:p.timestamp,keyFrame:p.keyFrame,frameSize:p.frameSize})}catch(_){i.error("Error emitting inner:audio:payload:send event:",_)}},0),this._wsSender&&this._wsSender.isConnected())try{const _=De(p),g=new Uint8Array(_.length+m.length);g.set(_,0),g.set(m,_.length),this._wsSender.sendBinary(g),i.debug(`Audio frame sent via WS: seq=${p.sequenceNumber}, samples=${t.length}, size=${g.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 r=Math.max(-1,Math.min(1,e[n]));s[n]=r<0?r*32768:r*32767}return s}else if(t===32){const s=new Int32Array(e.length);for(let n=0;n<e.length;n++){const r=Math.max(-1,Math.min(1,e[n]));s[n]=r*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),r=new DataView(n.buffer);for(let a=0;a<e.length;a++)t===16?r.setInt16(a*2,e[a],!0):t===32&&r.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 h{emitAudioData(e,t){if(this.onAudioData)try{this.onAudioData(e,t)}catch(s){i.error("Error in audio data callback:",s)}}}const Ne=`
|
|
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 d=s.createShader(a);return s.shaderSource(d,c),s.compileShader(d),d},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 Se extends l{constructor(e,t){if(super(),this._context=e,this._source=new X,this._pipeline=new J(this._source,t),t?.renderMode==="processed"&&t?.greenScreen?.enabled){const s=new N(t.greenScreen);this._pipeline.setProcessor(s),i.info("Green screen processor enabled")}this._pipeline.setRenderFitMode(t?.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 Te extends l{process(e,t){return this.ensureNotDisposed(),e}onDispose(){}}class Ae extends l{set onProcessedAudio(e){this._onProcessedAudio=e}constructor(e){super(),this._processors=[],this._processingStrategy=new Te,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 Re extends l{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 u("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 we=1,De=0,ye={0:0,1:1,2:2};function be(r){return r<0?0:r>4095?4095:Math.floor(r)}function Ne(r){if(r<0)return 0;const e=1048575;return r>e?r&e:Math.floor(r)}function Ie(r){return r<0?0:r>4095?4095:Math.floor(r)}function ke(r){return r<0?0:r>65535?65535:Math.floor(r)}function Oe(r){if(r!==0&&r!==1&&r!==2)throw new u(`Invalid sample rate encoding: ${r}. Must be 0 (16kHz), 1 (24kHz), or 2 (48kHz)`,h.AUDIO_INVALID_SAMPLE_RATE)}function xe(r){Oe(r.sampleRate);const e=be(r.sequenceNumber),t=Ne(r.timestamp),s=Ie(r.frameSize),n=ke(r.payloadLength);if(r.channel!==0&&r.channel!==1)throw new u(`Invalid channel value: ${r.channel}. Must be 0 (mono) or 1 (stereo)`,h.AUDIO_INVALID_CHANNEL);const o=new Uint8Array(9);let a=we<<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=ye[r.sampleRate];let d=(t&15)<<4;d|=c<<2,d|=s>>10&3,o[4]=d,o[5]=s>>2&255;let p=(s&3)<<6;return p|=De&63,o[6]=p,o[7]=n>>8&255,o[8]=n&255,o}class Le{constructor(e){this.transport=e}sendBinary(e){this.transport.sendBinary(e)}isConnected(){return this.transport.isConnected}}class Pe extends l{constructor(e,t){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._context=e,this._options=t;const s=t?.input;this._targetSampleRate=s?.sampleRate||24e3,this._bitDepth=s?.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 Le(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),d=Math.floor(Date.now()-this._audioStartTime),p={channel:(this._options?.input?.channels||1)===1?0:1,keyFrame:!!this._isKeyFrame,sequenceNumber:this._sequenceNumber,timestamp:d,sampleRate:c,frameSize:t.length,payloadLength:a},m=this._pcmToUint8Array(n,this._bitDepth);if(setTimeout(()=>{try{this._context.eventBus.emit("inner:audio:payload:send",{payload:m,sampleRate:this._targetSampleRate,channels:this._options?.input?.channels||1,sequenceNumber:p.sequenceNumber,timestamp:p.timestamp,keyFrame:p.keyFrame,frameSize:p.frameSize})}catch(_){i.error("Error emitting inner:audio:payload:send event:",_)}},0),this._wsSender&&this._wsSender.isConnected())try{const _=xe(p),v=new Uint8Array(_.length+m.length);v.set(_,0),v.set(m,_.length),this._wsSender.sendBinary(v),i.debug(`Audio frame sent via WS: seq=${p.sequenceNumber}, samples=${t.length}, size=${v.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 Fe extends l{emitAudioData(e,t){if(this.onAudioData)try{this.onAudioData(e,t)}catch(s){i.error("Error in audio data callback:",s)}}}const Me=`
|
|
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 Ie(){const o=new Blob([Ne],{type:"text/javascript"});return URL.createObjectURL(o)}class ke extends be{constructor(e){super(),this.options=e,this._stream=null,this._audioContext=null,this._sourceNode=null,this._workletNode=null,this._targetSampleRate=R,this._bitDepth=I,this._channels=1;const t=e?.input;this._targetSampleRate=t?.sampleRate||R,this._bitDepth=t?.bitDepth||I,this._channels=t?.channels||1}async start(){this.ensureNotDisposed();try{const t={audio:this.options?.input?.constraints||{channelCount:this._channels}};this._stream=await navigator.mediaDevices.getUserMedia(t);const r=this._stream.getAudioTracks()[0].getSettings().sampleRate||44100;this._audioContext=new AudioContext({sampleRate:r});const a=Ie();await this._audioContext.audioWorklet.addModule(a),this._sourceNode=this._audioContext.createMediaStreamSource(this._stream),this._workletNode=new AudioWorkletNode(this._audioContext,"audio-resampler",{processorOptions:{targetSampleRate:this._targetSampleRate,bitDepth:this._bitDepth,channels:this._channels},numberOfInputs:1,numberOfOutputs:0}),this._workletNode.port.onmessage=c=>{const{type:l,data:p,sampleRate:m}=c.data;l==="audioData"&&p&&this.emitAudioData(p,m)},this._sourceNode.connect(this._workletNode),i.info(`Audio capture started: input=${r}Hz -> output=${this._targetSampleRate}Hz, ${this._bitDepth}bit`)}catch(e){throw u.fromError(e,d.AUDIO_CAPTURE_FAILED)}}stop(){this._stream&&(this._stream.getTracks().forEach(e=>e.stop()),this._stream=null),this._workletNode&&(this._workletNode.port.onmessage=null,this._workletNode.disconnect(),this._workletNode=null),this._sourceNode&&(this._sourceNode.disconnect(),this._sourceNode=null),this._audioContext&&(this._audioContext.close(),this._audioContext=null),i.info("Audio capture stopped")}onDispose(){this.stop()}}class Oe{create(e){return new ke(e)}}class xe extends h{constructor(e,t,s){super(),this._source=null,this._isCapturing=!1,this._currentOutputTrackId=null,this._context=e,this._inputPipeline=new me(t),this._outputPipeline=new fe,this._frameSender=new ye(e,t),this._sourceFactory=s||new Oe,this._inputPipeline.onProcessedAudio=(n,r)=>{this._context.eventBus.emit("inner:audio:frame:received",{data:n,sampleRate:r})},i.info("Audio Controller created")}setWebSocketSender(e){this._frameSender.setWebSocketSender(e)}async startCapture(){if(this.ensureNotDisposed(),this._isCapturing){i.warn("Audio capture already started");return}try{this._source||(this._source=this._sourceFactory.create(this._context.options.audio),this._source.onAudioData=(e,t)=>{this._inputPipeline.process(e,t)}),await this._source.start(),this._isCapturing=!0,this._frameSender.start(),this._context.eventBus.emit("inner:audio:input:started",void 0),i.info("Audio capture started")}catch(e){throw this._context.emitError(e,d.AUDIO_CAPTURE_START_FAILED)}}stopCapture(){this.ensureNotDisposed(),this._isCapturing&&(this._source&&this._source.stop(),this._frameSender.stop(),this._isCapturing=!1,this._context.eventBus.emit("inner:audio:input:stopped",void 0),i.info("Audio capture stopped"))}get isCapturing(){return this._isCapturing}setOutputTrack(e,t,s){this.ensureNotDisposed(),this._outputPipeline.setTrack(e),this._currentOutputTrackId=t,this._context.eventBus.emit("inner:audio:track:added",{trackId:t,participantId:s}),i.info("Audio output track ready")}removeOutputTrack(e){this.ensureNotDisposed(),this._currentOutputTrackId===e&&(this._outputPipeline.removeTrack(),this._currentOutputTrackId=null,this._context.eventBus.emit("inner:audio:track:removed",{trackId:e}),i.info("Audio output track removed"))}setVolume(e){this.ensureNotDisposed(),this._outputPipeline.setVolume(e),this._context.eventBus.emit("inner:audio:volume:changed",{volume:e}),i.debug(`Audio volume set to: ${e}`)}getVolume(){return this._outputPipeline.getVolume()}mute(){this.ensureNotDisposed(),this._outputPipeline.mute(),this._context.eventBus.emit("inner:audio:muted",void 0),i.debug("Audio muted")}unmute(){this.ensureNotDisposed(),this._outputPipeline.unmute(),this._context.eventBus.emit("inner:audio:unmuted",void 0),i.debug("Audio unmuted")}getOutputElement(){return this._outputPipeline.getAudioElement()}isMuted(){return this._outputPipeline.isMuted()}onDispose(){this.stopCapture(),this._source&&(this._source.dispose(),this._source=null),this._inputPipeline.dispose(),this._outputPipeline.dispose(),this._frameSender.dispose(),i.info("Audio Controller disposed")}}exports.ConversationStateType=void 0;(function(o){o.IDLE="idle",o.WAITING="waiting",o.STREAMING="streaming",o.COMPLETED="completed",o.ERROR="error"})(exports.ConversationStateType||(exports.ConversationStateType={}));class Le extends W{constructor(){super(...arguments),this._currentState=exports.ConversationStateType.IDLE,this.transitionTable=[{from:exports.ConversationStateType.IDLE,to:[exports.ConversationStateType.WAITING,exports.ConversationStateType.ERROR]},{from:exports.ConversationStateType.WAITING,to:[exports.ConversationStateType.STREAMING,exports.ConversationStateType.ERROR]},{from:exports.ConversationStateType.STREAMING,to:[exports.ConversationStateType.COMPLETED,exports.ConversationStateType.ERROR]},{from:exports.ConversationStateType.COMPLETED,to:[exports.ConversationStateType.IDLE,exports.ConversationStateType.WAITING,exports.ConversationStateType.ERROR]},{from:exports.ConversationStateType.ERROR,to:[exports.ConversationStateType.IDLE]}]}reset(){this._currentState=exports.ConversationStateType.IDLE}}class O extends h{constructor(e,t){super(),this._currentQuestion=null,this._currentAnswer=null,this._sessionId=e,this._stateMachine=new Le,i.info(`Conversation session created: ${e}`)}get sessionId(){return this._sessionId}get state(){return this._stateMachine.getCurrentState()}_setQuestion(e){this._currentQuestion=e,this._stateMachine.transitionTo(exports.ConversationStateType.WAITING),this._syncContextState()}get currentQuestion(){return this._currentQuestion}_startAnswer(e){this._currentAnswer={questionId:e,text:"",isComplete:!1,timestamp:Date.now()},this._stateMachine.transitionTo(exports.ConversationStateType.STREAMING),this._syncContextState()}_appendAnswerChunk(e){this._currentAnswer&&(this._currentAnswer.text+=e)}_completeAnswer(){this._currentAnswer&&(this._currentAnswer.isComplete=!0,this._currentAnswer.completedAt=Date.now()),this._stateMachine.transitionTo(exports.ConversationStateType.COMPLETED),this._syncContextState()}get currentAnswer(){return this._currentAnswer}_setError(e){this._stateMachine.transitionTo(exports.ConversationStateType.ERROR),this._syncContextState()}_syncContextState(){}handleEvent(e){const t=this._stateMachine.getCurrentState();switch(e.type){case"QUESTION":if(t!==exports.ConversationStateType.IDLE&&t!==exports.ConversationStateType.COMPLETED){i.warn(`Ignoring QUESTION event in invalid state: ${t}`);return}this._setQuestion(e.payload);break;case"ANSWER_START":if(t!==exports.ConversationStateType.WAITING){i.warn(`Ignoring ANSWER_START event in invalid state: ${t}. Expected: WAITING`);return}this._startAnswer(e.payload);break;case"ANSWER_CHUNK":if(t!==exports.ConversationStateType.STREAMING){i.warn(`Ignoring ANSWER_CHUNK event in invalid state: ${t}. Expected: STREAMING`);return}this._appendAnswerChunk(e.payload&&e.payload.chunk||""),e.payload&&e.payload.isComplete&&this._completeAnswer();break;case"ANSWER_COMPLETE":if(t!==exports.ConversationStateType.STREAMING){i.warn(`Ignoring ANSWER_COMPLETE event in invalid state: ${t}. Expected: STREAMING`);return}this._completeAnswer();break;case"ERROR":this._setError(e.payload);break;default:i.warn(`Unknown event type: ${e.type}`);break}}reset(){this._stateMachine.reset(),this._currentQuestion=null,this._currentAnswer=null}onDispose(){this.reset(),i.info(`Conversation session disposed: ${this._sessionId}`)}}class Pe extends h{constructor(){super(...arguments),this._buffers=new Map}appendChunk(e,t){this.ensureNotDisposed();const n=(this._buffers.get(e)||"")+t;return this._buffers.set(e,n),i.debug(`Message chunk appended for question ${e}, length: ${n.length}`),n}getMessage(e){return this._buffers.get(e)||null}complete(e){const t=this.getMessage(e)||"";return this._buffers.delete(e),i.debug(`Message assembly completed for question ${e}, length: ${t.length}`),t}clear(e){this._buffers.delete(e)}onDispose(){this._buffers.clear(),i.info("Message Assembler disposed")}}class Fe{generateSessionId(){return`session_${Date.now()}_${Math.random().toString(36).substring(2,9)}`}generateQuestionId(){return Math.random().toString(36).substring(2,6)}}class Me extends h{constructor(e,t){super(),this._transport=null,this._sessions=new Map,this._context=e,this._assembler=new Pe,this._idService=new Fe,i.info("Conversation Manager created")}setTransport(e){this._transport=e}async sendQuestion(e,t={speed:100,mood:0,vol:50}){this.ensureNotDisposed();const s=this._idService.generateQuestionId(),n={id:s,text:e,timestamp:Date.now(),inputMode:"text"},r=this._idService.generateSessionId(),a=new O(r,this._context);if(a.handleEvent({type:"QUESTION",payload:n}),this._sessions.set(s,a),this._transport)try{this._transport.sendText(s,e,200,t)}catch(c){throw i.error("Error sending question via transport:",c),a.handleEvent({type:"ERROR",payload:"Failed to send question"}),this._context.eventBus.emit("inner:sdk:error",{error:c instanceof Error?c:new Error(String(c))}),c}else i.warn("Transport not set, question not sent");return this._context.eventBus.emit("inner:conversation:question:sent",{questionId:s,text:e}),this._context.eventBus.emit("inner:conversation:answer:waiting",{questionId:s}),i.info(`Question sent: ${s}`),s}handleServerPong(){this.ensureNotDisposed(),this._transport&&this._transport.handlePongReceived()}willDisconnect(){this.ensureNotDisposed(),this._transport?.willDisconnect()}handleTextInput(e){this.sendQuestion(e).catch(t=>{i.error("Error sending question:",t)})}handleAnswerChunk(e,t,s){this.ensureNotDisposed();const n=this._sessions.get(e);if(!n){i.warn(`No active session for answer chunk: ${e}`);return}if(n.state===exports.ConversationStateType.WAITING&&n.handleEvent({type:"ANSWER_START",payload:e}),s){n.handleEvent({type:"ANSWER_COMPLETE"});const a=this._assembler.complete(e);this._context.eventBus.emit("inner:conversation:answer:completed",{questionId:e,fullAnswer:a});try{n.dispose()}catch(c){i.error("Error disposing session:",c)}this._sessions.delete(e)}else n.handleEvent({type:"ANSWER_CHUNK",payload:{uid:e,chunk:t,isComplete:s}}),this._assembler.appendChunk(e,t),this._context.eventBus.emit("inner:conversation:answer:chunk",{questionId:e,chunk:t,isComplete:s})}handleServerInitiatedMessage(e,t){this.ensureNotDisposed(),this._context.eventBus.emit("inner:conversation:server:message",{questionId:e,message:t,type:"text"})}handleAsrTextReceived(e,t){this.ensureNotDisposed();const s=this._idService.generateSessionId(),n=new O(s,this._context);n.handleEvent({type:"QUESTION",payload:{id:e,text:t,timestamp:Date.now(),inputMode:"voice"}}),this._sessions.set(e,n),this._context.eventBus.emit("inner:conversation:asr:received",{questionId:e,text:t})}onDispose(){for(const e of this._sessions.values())try{e.dispose()}catch(t){i.error("Error disposing session:",t)}this._sessions.clear(),this._assembler.dispose(),i.info("Conversation Manager disposed")}}class Ve extends h{constructor(e,t){super(),this._manager=new Me(e,t),i.info("Conversation Controller created")}get manager(){return this._manager}setTransport(e){this._manager.setTransport(e)}async sendQuestion(e,t){return this.ensureNotDisposed(),this._manager.sendQuestion(e,t)}onDispose(){this._manager.dispose(),i.info("Conversation Controller disposed")}}class Ue extends h{constructor(){super(...arguments),this._tracks=new Map,this._participants=new Map}register(e,t,s){this.ensureNotDisposed(),this._tracks.set(e,t),this._participants.has(s)||this._participants.set(s,new Set),this._participants.get(s).add(e),t.on("ended",()=>{i.debug(`Track ended: ${e}`),this.unregister(e)}),i.debug(`Track registered: ${e} from participant ${s}`)}unregister(e){const t=this._tracks.get(e);if(t){t.stop(),this._tracks.delete(e);for(const[s,n]of this._participants.entries())if(n.has(e)){n.delete(e),n.size===0&&this._participants.delete(s);break}i.debug(`Track unregistered: ${e}`)}}getTrack(e){return this._tracks.get(e)||null}getParticipantTracks(e){const t=this._participants.get(e);return t?Array.from(t):[]}getAllTrackIds(){return Array.from(this._tracks.keys())}getVideoAvailability(){let e=!1,t=!1,s=!1;for(const n of this._tracks.values()){const r=n.mediaStreamTrack;if(r.kind==="video"&&(e=!0,r.readyState==="live"&&(t=!0),r.enabled&&(s=!0),r.readyState==="live"&&r.enabled))return{available:!0}}return e?t?s?{available:!1,reason:v.NO_VIDEO_TRACK}:{available:!1,reason:v.TRACK_MUTED}:{available:!1,reason:v.TRACK_ENDED}:{available:!1,reason:v.NO_VIDEO_TRACK}}clear(){for(const e of this._tracks.values())e.stop();this._tracks.clear(),this._participants.clear(),i.info("All tracks cleared")}onDispose(){this.clear()}}class x extends h{constructor(e,t){super(),this._videoController=null,this._room=null,this._context=e,this._trackRegistry=t,this._loosTrackTimer=null,i.info("Video Track Handler created")}setRoom(e){this._room=e}setVideoController(e){this._videoController=e}handleTrackSubscribed(e,t,s){this.ensureNotDisposed(),this._loosTrackTimer&&clearTimeout(this._loosTrackTimer),this._trackRegistry.register(t,e,s),this._videoController&&e.attach(this._videoController.getSource().getInternalElement()),e.on("videoPlaybackStarted",()=>{i.info("inner:rtc:video:available"),this._context.eventBus.emit("inner:rtc:video:available",void 0)}),this._context.eventBus.emit("inner:video:track:added",{trackId:t,participantId:s}),i.debug(`Video track subscribed: ${t}`)}handleTrackUnsubscribed(e,t,s){this.ensureNotDisposed(),this._videoController&&e.detach(this._videoController.getSource().getInternalElement()),e.removeAllListeners(),this._context.eventBus.emit("inner:video:track:removed",{trackId:t,participantId:s}),this._loosTrackTimer&&clearTimeout(this._loosTrackTimer),this._loosTrackTimer=setTimeout(()=>{this._context.eventBus.emit("inner:rtc:video:unavailable",void 0)},Y),this._trackRegistry.unregister(t),i.debug(`Video track unsubscribed: ${t}`)}supports(e){return e.mediaStreamTrack.kind==="video"}onDispose(){this._loosTrackTimer&&(clearTimeout(this._loosTrackTimer),this._loosTrackTimer=null),i.info("Video Track Handler disposed")}}class L extends h{constructor(e,t){super(),this._audioController=null,this._room=null,this._context=e,this._trackRegistry=t,i.info("Audio Track Handler created")}setRoom(e){this._room=e}setAudioController(e){this._audioController=e}handleTrackSubscribed(e,t,s){this.ensureNotDisposed(),this._trackRegistry.register(t,e,s),this._audioController?e.attach(this._audioController.getOutputElement()):this._context.eventBus.emit("inner:audio:track:added",{trackId:t,participantId:s}),i.debug(`Audio track subscribed: ${t}`)}handleTrackUnsubscribed(e,t,s){this.ensureNotDisposed(),this._audioController?(this._audioController.removeOutputTrack(t),e.detach(this._audioController.getOutputElement())):this._context.eventBus.emit("inner:audio:track:removed",{trackId:t}),this._trackRegistry.unregister(t),i.debug(`Audio track unsubscribed: ${t}`)}supports(e){return e.mediaStreamTrack.kind==="audio"}onDispose(){i.info("Audio Track Handler disposed")}}class Ke extends h{constructor(e,t){super(),this._room=null,this._context=e,this._trackRegistry=t,this._handlers=[new x(e,t),new L(e,t)],i.info("LiveKit Event Adapter created")}setRoom(e){this._room=e,this._handlers.forEach(t=>{t.setRoom(e)}),console.log("LiveKit room set:",this._room)}setVideoController(e){const t=this._handlers.find(s=>s instanceof x);t&&t.setVideoController(e)}setAudioController(e){const t=this._handlers.find(s=>s instanceof L);t&&t.setAudioController(e)}handleTrackSubscribed(e,t,s){this.ensureNotDisposed();const n=this._findHandler(e);n?n.handleTrackSubscribed(e,t,s):i.warn(`No handler found for track, trackId: ${t}`)}handleTrackUnsubscribed(e,t){this.ensureNotDisposed();const s=this._trackRegistry.getTrack(e);if(s){const n=this._findHandler(s);n?n.handleTrackUnsubscribed(s,e,t):(this._trackRegistry.unregister(e),i.warn(`No handler found for track, trackId: ${e}`))}}_findHandler(e){return this._handlers.find(t=>t.supports(e))||null}onDispose(){this._handlers.forEach(e=>{try{e.dispose()}catch(t){i.error("Error disposing track handler:",t)}}),this._handlers.length=0,i.info("LiveKit Event Adapter disposed")}}class We extends h{constructor(e){super(),this._room=null,this._context=e,this._trackRegistry=new Ue,this._eventAdapter=new Ke(e,this._trackRegistry),this._maxAttempts=e.options.reconnect?.maxAttempts||S,this._maxDelay=(e.options.reconnect?.delay??y)*T,i.info("LiveKit Service created")}async connect(e,t,s,n){if(this.ensureNotDisposed(),this._room&&this._room.isConnected()){i.warn("Already connected to LiveKit room");return}try{i.info(`Connecting to LiveKit room: ${s} at ${e}`);const r=new f.Room({reconnectPolicy:{nextRetryDelayInMs:a=>a.elapsedMs<this._maxDelay?Math.min(T*Math.pow(2,a.retryCount),this._maxDelay):null}});r.prepareConnection(e,t),r.on(f.RoomEvent.TrackSubscribed,(a,c,l)=>{this._handleTrackSubscribed(a,c,l)}),r.on(f.RoomEvent.TrackUnsubscribed,(a,c,l)=>{this._handleTrackUnsubscribed(a,c,l)}),r.on(f.RoomEvent.TrackMuted,(a,c)=>{this._handleTrackMuted(a,c)}),r.on(f.RoomEvent.TrackUnmuted,(a,c)=>{this._handleTrackUnmuted(a,c)}),r.on(f.RoomEvent.Connected,()=>{i.info("Connected to LiveKit room"),this._context.eventBus.emit("inner:rtc:connected",void 0)}),r.on(f.RoomEvent.Disconnected,a=>{a?i.info(`LiveKit room disconnected: ${a}`):i.info("LiveKit room disconnected"),a!==f.DisconnectReason.CLIENT_INITIATED&&this._context.eventBus.emit("inner:rtc:disconnected",{reason:a?String(a):void 0}),this._room=null}),i.info("Connecting to LiveKit room..."),await r.connect(e,t,{autoSubscribe:!0,maxRetries:this._context.options.reconnect?.maxAttempts||S}),this._eventAdapter.setRoom(r),this._room=r}catch(r){throw u.fromError(r,d.LIVEKIT_CONNECT_FAILED)}}_handleTrackSubscribed(e,t,s){try{if(!e.mediaStreamTrack){i.warn("Track does not have MediaStreamTrack");return}e.kind==="audio"&&s.on(f.ParticipantEvent.IsSpeakingChanged,c=>{console.log("\u8F68\u9053 \u8BF4\u8BDD\u72B6\u6001:",c)});const r=t.trackSid,a=s.identity;this._eventAdapter.handleTrackSubscribed(e,r,a)}catch(n){i.error("Error handling track subscribed:",n)}}_handleTrackUnsubscribed(e,t,s){try{const n=t,r=s,a=n.trackSid,c=r.identity;this._trackRegistry.getTrack(a)?.kind==="video"&&this._context.eventBus.emit("inner:rtc:video:unavailable",void 0),this._eventAdapter.handleTrackUnsubscribed(a,c)}catch(n){i.error("Error handling track unsubscribed:",n)}}_handleTrackMuted(e,t){try{e.kind==="video"&&this._context.eventBus.emit("inner:rtc:video:unavailable",void 0)}catch(s){i.error("Error handling track muted:",s)}}_handleTrackUnmuted(e,t){try{e.kind==="video"&&this._context.eventBus.emit("inner:rtc:video:available",void 0)}catch(s){i.error("Error handling track unmuted:",s)}}async disconnect(){if(this._room)try{await this._room.disconnect(),this._room=null,this._context.eventBus.emit("inner:rtc:disconnected",{reason:"Disconnected from LiveKit room"}),i.info("Disconnected from LiveKit room")}catch(e){i.error("Error disconnecting from LiveKit room:",e),this._room=null}}isConnected(){return this._room?this._room.state===f.ConnectionState.Connected:!1}getTrackRegistry(){return this._trackRegistry}get trackRegistry(){return this._trackRegistry}getEventAdapter(){return this._eventAdapter}onDispose(){this.disconnect(),this._trackRegistry.dispose(),this._eventAdapter.dispose(),i.info("LiveKit Service disposed")}}class Be extends h{constructor(e){super(),this._conversationManager=null,this._context=e,i.info("WebSocket Dispatcher created")}setConversationManager(e){this._conversationManager=e,i.debug("Conversation manager set in dispatcher")}dispatch(e){this.ensureNotDisposed();try{if(e.type==="ws"){this._handleWSServerMessage(e);return}if(e.type==="text"){this._handleServerTextMessage(e);return}}catch(t){i.error("Error dispatching message:",t)}}_handleWSServerMessage(e){e.code===101?(this._conversationManager?.handleServerPong(),i.debug("Received pingpong response")):e.code===102&&(i.warn("Server will disconnect"),this._conversationManager?.willDisconnect())}_handleServerTextMessage(e){if(!this._conversationManager){i.warn("Conversation manager not set, text message ignored");return}const{uid:t,payload:s,code:n}=e;n===200?(this._conversationManager.handleAnswerChunk(t,s,!1),i.debug(`Answer received (code 401) for uid: ${t}`)):n===201?(this._conversationManager.handleAnswerChunk(t,"",!0),i.debug(`Answer completed (code 411) for uid: ${t}`)):n===300?(this._conversationManager.handleAsrTextReceived(t,s),i.debug(`ASR text chunk received (code 410) for uid: ${t}`)):n===400&&(this._conversationManager.handleServerInitiatedMessage(t,s),i.debug(`Server initiated message received (code 412) for uid: ${t}`))}onDispose(){i.info("WebSocket Dispatcher disposed")}}class $e extends h{constructor(e){super(),this._ws=null,this._dispatcher=null,this._isConnecting=!1,this._activeTimers=new Set,this._retryCount=0,this._maxRetries=S,this._pingTimer=void 0,this._pongTimer=void 0,this._isExplicitDisconnect=!1,this._context=e,this._maxRetries=e.options.reconnect?.maxAttempts??S,this._maxDelay=(e.options.reconnect?.delay??y)*T,this._dispatcher=new Be(e)}setConversationManager(e){this._dispatcher&&this._dispatcher.setConversationManager(e)}async connect(){if(this.ensureNotDisposed(),this._stopHeartbeat(),!(this._isConnecting||this.isConnected||this._ws?.readyState===WebSocket.OPEN)){this._isConnecting=!0,this._isExplicitDisconnect=!1;try{return this._ws=new WebSocket(this._context.wsUrl),new Promise((e,t)=>{this._ws.onopen=()=>{this._isConnecting=!1,this._retryCount=0,this.sendPing(),this._context.eventBus.emit("inner:ws:connected",void 0),i.info("Conversation transport connected"),e()},this._ws.onerror=s=>{this._isConnecting=!1,i.error("Conversation transport error:",s),this._context.eventBus.emit("inner:sdk:error",{error:new u("WebSocket connection error",d.WS_CONNECT_ERROR,s)}),t(new u("WebSocket connection error",d.WS_CONNECT_ERROR,s))},this._ws.onclose=s=>{const n=s.code,r=s.reason?s.reason:s.wasClean?"Clean close":"Closed by remote";this._handleDisconnect(n,r)},this._ws.onmessage=s=>{this._handleMessage(s.data)}})}catch(e){throw this._isConnecting=!1,this._context.emitError(e,d.WS_CONNECT_FAILED)}}}disconnect(){this._isExplicitDisconnect=!0,this._stopHeartbeat(),this._ws&&(this._ws.close(),this._ws=null),this._context.eventBus.emit("inner:ws:disconnected",{reason:"Client disconnect"}),i.info("Conversation transport disconnected")}_handleDisconnect(e,t){if(this._stopHeartbeat(),this._isConnecting=!1,e!==void 0&&new Set([1e3,1001,1002,1003,1007,1008]).has(e)){this._isExplicitDisconnect=!0;const n=`WebSocket closed (code ${e}): ${t??""}`;this._context.eventBus.emit("inner:ws:disconnected",{reason:n}),i.info(`Conversation transport closed with non-retry code ${e}: ${t??""}`);return}if(!this._isExplicitDisconnect&&this._retryCount<this._maxRetries){this._retryCount++;const n=Math.min(T*Math.pow(2,this._retryCount-1),this._maxDelay);i.warn(`Connection lost. Retry ${this._retryCount} in ${n}ms`);const r=setTimeout(()=>{!this._isExplicitDisconnect&&!this.isDisposed&&this.connect().catch(()=>{})},n);this._registerTimer(r)}else this._context.eventBus.emit("inner:ws:disconnected",{reason:"Closed"})}_registerTimer(e){return this._activeTimers.add(e),e}_clearTimer(e){e&&(clearTimeout(e),this._activeTimers.delete(e))}sendPing(){if(!this.isConnected)return;const e={type:"ws",code:101,payload:"ping"};this._ws?.send(JSON.stringify(e)),clearTimeout(this._pongTimer),this._pongTimer=setTimeout(()=>{i.error(`Pong timeout (${N/T}s). Reconnecting...`),this._ws?.close()},N)}handlePongReceived(){clearTimeout(this._pongTimer),this._clearTimer(this._pingTimer),this._pingTimer=setTimeout(()=>{this.sendPing()},J)}willDisconnect(){if(this.ensureNotDisposed(),this._ws&&this._ws.readyState===WebSocket.OPEN){const t=JSON.stringify({type:"ws",code:102,payload:"will disconnect"});this._ws.send(t),this._isExplicitDisconnect=!0,this._ws.close(),this._ws=null,this._context.eventBus.emit("inner:ws:disconnected",{reason:"Client responsive to server's disconnect message."})}}sendText(e,t,s=200,n){if(this.ensureNotDisposed(),!this._ws||this._ws.readyState!==WebSocket.OPEN)throw new u("WebSocket is not connected",d.WS_NOT_CONNECTED);try{const a=JSON.stringify({type:"text",code:s,uid:e,metadata:n,payload:t});this._ws.send(a),i.debug("Text message sent via transport:",e)}catch(r){throw this._context.emitError(r,d.WS_TEXT_SEND_FAILED)}}_handleMessage(e){try{if(typeof e!="string")return;const t=JSON.parse(e);i.debug("Message received:",t.type),this._dispatcher?this._dispatcher.dispatch(t):i.warn("Dispatcher not set, message ignored")}catch(t){i.error("Error parsing message:",t),this._context.emitError(t,d.WS_PARSE_ERROR)}}sendBinary(e){if(this.ensureNotDisposed(),!this._ws||this._ws.readyState!==WebSocket.OPEN){i.debug("WebSocket not connected, binary data dropped");return}try{this._ws.send(e),i.debug("Binary data sent via transport")}catch(t){i.error("Error sending binary data:",t)}}get isConnected(){return this._ws?.readyState===WebSocket.OPEN}_stopHeartbeat(){this._clearTimer(this._pingTimer),this._clearTimer(this._pongTimer)}onDispose(){this._stopHeartbeat(),this.disconnect(),this._dispatcher&&(this._dispatcher.dispose(),this._dispatcher=null),i.info("Conversation transport disposed")}}const A=["sdk:connected","sdk:disconnected","sdk:error","media:video:available","media:video:unavailable","media:video:trackAdded","media:video:trackRemoved","media:audio:trackAdded","media:audio:trackRemoved","media:audio:captureStarted","media:audio:captureStopped","media:audio:volumeChanged","media:audio:muted","media:audio:unmuted","conversation:question:sent","conversation:answer:waiting","conversation:server:message","conversation:asr:received","conversation:answer:chunk","conversation:answer:completed"],P=/^[a-z]+:[a-zA-z]+(:[a-zA-Z]+)?$/;class He{constructor(e){this.inner=e,this.listeners=new Map,this.forwardedVideoTrackIds=new Set,this.wsConnected=!1,this.livekitConnected=!1,this.httpConnected=!1,this.internalMap={"inner:sdk:error":{public:"sdk:error",sanitizer:t=>({message:t?.error?.message,code:t?.error?.code})},"inner:rtc:video:available":{public:"media:video:available"},"inner:rtc:video:unavailable":{public:"media:video:unavailable"},"inner:video:track:added":{public:"media:video:trackAdded"},"inner:video:track:removed":{public:"media:video:trackRemoved"},"inner:audio:track:added":{public:"media:audio:trackAdded"},"inner:audio:track:removed":{public:"media:audio:trackRemoved"},"inner:audio:input:started":{public:"media:audio:captureStarted"},"inner:audio:input:stopped":{public:"media:audio:captureStopped"},"inner:audio:volume:changed":{public:"media:audio:volumeChanged",sanitizer:t=>({volume:t?.volume})},"inner:audio:muted":{public:"media:audio:muted"},"inner:audio:unmuted":{public:"media:audio:unmuted"},"inner:conversation:question:sent":{public:"conversation:question:sent",sanitizer:t=>({questionId:t?.questionId,text:String(t?.text||"")})},"inner:conversation:answer:waiting":{public:"conversation:answer:waiting",sanitizer:t=>({questionId:t?.questionId})},"inner:conversation:server:message":{public:"conversation:server:message",sanitizer:t=>({questionId:t?.questionId,message:String(t?.message||""),type:String(t?.type||"")})},"inner:conversation:asr:received":{public:"conversation:asr:received",sanitizer:t=>({questionId:t?.questionId,text:String(t?.text||"")})},"inner:conversation:answer:chunk":{public:"conversation:answer:chunk",sanitizer:t=>({questionId:t?.questionId,chunk:String(t?.chunk||"")})},"inner:conversation:answer:completed":{public:"conversation:answer:completed",sanitizer:t=>({questionId:t?.questionId,fullAnswer:String(t?.fullAnswer||"")})}},Object.keys(this.internalMap).forEach(t=>{this.inner.on(t,s=>this.handleInternalEvent(t,s))}),this.inner.on("inner:sdk:connected",t=>this.handleConnected(t,!0)),this.inner.on("inner:sdk:disconnected",t=>this.handleConnected(t,!1)),A.forEach(t=>{this.inner.on(t,s=>this.handlePublicEvent(t,s))})}publicAPI(){return{on:this.on.bind(this),off:this.off.bind(this),once:this.once.bind(this)}}on(e,t){const s=String(e);return this.assertAllowed(s),this.listeners.has(s)||this.listeners.set(s,new Set),this.listeners.get(s).add(t),()=>this.off(e,t)}once(e,t){const s=String(e);this.assertAllowed(s);const n=r=>{try{t(r)}finally{this.off(e,n)}};return this.on(e,n)}off(e,t){const s=String(e);this.assertAllowed(s);const n=this.listeners.get(s);n&&(n.delete(t),n.size===0&&this.listeners.delete(s))}handleInternalEvent(e,t){const s=this.internalMap[e];if(!s)return;const n=s.public;if(A.indexOf(n)===-1)return;if(n==="media:video:trackAdded"){const c=t?.trackId||t?.id;if(typeof c=="string"){if(this.forwardedVideoTrackIds.has(c))return;this.forwardedVideoTrackIds.add(c)}}const r=s.sanitizer?s.sanitizer(t):t;if(!P.test(n))return;const a=this.listeners.get(n);!a||a.size===0||a.forEach(c=>{try{c(r)}catch(l){console.error("PublicEventEmitter listener error",l)}})}handlePublicEvent(e,t){if(A.indexOf(e)===-1)return;const s=Object.values(this.internalMap).find(a=>a.public===e),n=s&&s.sanitizer?s.sanitizer(t):t;if(e==="media:video:trackAdded"){const a=t?.trackId||t?.id;if(typeof a=="string"){if(this.forwardedVideoTrackIds.has(a))return;this.forwardedVideoTrackIds.add(a)}}const r=this.listeners.get(e);!r||r.size===0||r.forEach(a=>{try{a(n)}catch(c){console.error("PublicEventEmitter listener error",c)}})}handleConnected(e,t){e.source==="livekit"?this.livekitConnected=t:e.source==="ws"?this.wsConnected=t:e.source==="http"&&(this.httpConnected=t),this.maybeEmitSdkConnected(t?"connected":"disconnected")}maybeEmitSdkConnected(e){const t=e==="connected",s=t?!!this.livekitConnected:!this.livekitConnected,n=t?!!this.wsConnected:!this.wsConnected,r=t?!!this.httpConnected:!this.httpConnected,a={livekit:s,ws:n,http:r,all:s&&n&&r},c=JSON.stringify(a);this.lastConnectedPayloadJson!==c&&(this.lastConnectedPayloadJson=c,this.inner.emit(`sdk:${e}`,a))}assertAllowed(e){if(A.indexOf(e)===-1)throw new Error(`Event "${e}" is not allowed for public consumers`);if(!P.test(e))throw new Error(`Event name "${e}" does not follow namespace:domain[:action] naming`)}}class ze extends h{constructor(e,t,s){super(),this._requestCounter=0,this._context=e,this._baseURL=t||"",this._defaultHeaders=s||{},i.info(`HTTP Service created with baseURL: ${this._baseURL||"none"}`)}setAuthToken(e){this._defaultHeaders.Authorization=`Bearer ${e}`}request(e){this.ensureNotDisposed();const t=e.requestId||`req_${Date.now()}_${++this._requestCounter}`,s=this._buildURL(e.url,e.params),n=this._mergeHeaders(e.headers),r={method:e.method||"GET",headers:n};e.data!==void 0&&(e.method||"GET")!=="GET"&&(typeof e.data=="string"?r.body=e.data:(r.body=JSON.stringify(e.data),!n["Content-Type"]&&!n["content-type"]&&(n["Content-Type"]="application/json")));const a=e.timeout||3e4,c=new AbortController;return r.signal=c.signal,new Promise((l,p)=>{const m=setTimeout(()=>c.abort(),a);fetch(s,r).then(async _=>{clearTimeout(m);const g={};_.headers.forEach((G,j)=>{g[j]=G});let E;const b=_.headers.get("content-type");b&&b.includes("application/json")?E=await _.json():E=await _.text();const q={requestId:t,status:_.status,headers:g,data:E,config:e};i.debug(`HTTP request succeeded: ${e.method} ${s} [${t}]`),l(q)}).catch(_=>{clearTimeout(m);const g=_&&_.name==="AbortError",E={requestId:t,status:_&&_.status||void 0,message:g?"Request aborted (timeout)":_&&_.message||"HTTP request failed",error:_,config:e};i.error(`HTTP request failed: ${e.method} ${s} [${t}]`,_),p(E)})})}get(e,t,s){return this.request({...s,url:e,method:"GET",params:t})}post(e,t,s){return this.request({...s,url:e,method:"POST",data:t})}put(e,t,s){return this.request({...s,url:e,method:"PUT",data:t})}delete(e,t){return this.request({...t,url:e,method:"DELETE"})}patch(e,t,s){return this.request({...s,url:e,method:"PATCH",data:t})}_buildURL(e,t){if(e.startsWith("http://")||e.startsWith("https://"))return this._appendParams(e,t);let s=this._baseURL;return s&&!s.endsWith("/")&&!e.startsWith("/")?s+="/":s&&s.endsWith("/")&&e.startsWith("/")&&(s=s.slice(0,-1)),s+=e,this._appendParams(s,t)}_appendParams(e,t){if(!t||Object.keys(t).length===0)return e;const s=new URLSearchParams;for(const[r,a]of Object.entries(t))s.append(r,String(a));const n=e.includes("?")?"&":"?";return`${e}${n}${s.toString()}`}_mergeHeaders(e){const t={...this._defaultHeaders};return e&&Object.assign(t,e),t}onDispose(){i.info("HTTP Service disposed")}}function qe(o,e="https"){return o.startsWith(e)?o:`${e}://${o}`}class Ge extends h{constructor(e,t){super(),this._context=e,this._httpService=new ze(e,t||te)}getAuthToken(){return this.ensureNotDisposed(),this._httpService?.request({url:"/auth/login",method:"GET",data:{}}).then(e=>{e.data.code===C&&this._httpService?.setAuthToken(e.data.data.token)}).catch(e=>{throw this._context.emitError(e,d.SDK_AUTH_TOKEN_FAILED)})}getLivekitConfig(){return this.ensureNotDisposed(),this._httpService?.request({url:"/stream/v1/start",method:"POST",data:{avatarName:this._context.options.avatarName,voice:this._context.options.avatarVoice}}).then(e=>{if(e.data.code===C){const t=e.data.data;return this._context.setLivekitConfig(qe(t.livekitUrl,"wss"),t.roomToken,t.roomId),this._context.setWsUrl(`${this._context.options.wsUrlPrefix||ie}/${t.roomId}/${this._context.options.avatarName}`),this._context.setVideoOptions({renderMode:t.greenScreen.enabled?"processed":"raw",greenScreen:t.greenScreen||{}}),this._context.eventBus.emit("inner:sdk:connected",{source:"http"}),!0}else throw new u(e.data.msg,d.SDK_GET_LIVEKIT_CONFIG_FAILED)}).catch(e=>{throw this._context.eventBus.emit("inner:sdk:error",{error:new u(e.message,d.SDK_GET_LIVEKIT_CONFIG_FAILED)}),u.fromError(e,d.SDK_GET_LIVEKIT_CONFIG_FAILED)})}switchVideo({onceActions:e,loopActions:t}){return this.ensureNotDisposed(),this._httpService?.request({url:"/avatar/v1/switch-video",method:"POST",data:{roomId:this._context.roomId,onceActions:e,loopActions:t}}).then(s=>{if(s.data.code===C)return!0;throw this._context.emitError(s.data.message,d.SDK_SWITCH_VIDEO_FAILED)})}interruptConversation(){return this.ensureNotDisposed(),this._httpService?.request({url:"/stream/v1/interrupt",method:"POST",data:{roomId:this._context.roomId}}).then(e=>{if(e.data.code===C)return!0;throw this._context.emitError(e.data.message,d.SDK_INTERRUPT_CONVERSATION_FAILED)}).finally(()=>{this._context.eventBus.emit("inner:sdk:disconnected",{source:"http"})})}disconnectConversation(){return this.ensureNotDisposed(),this._httpService?.request({url:"/stream/v1/stop",method:"POST",data:{roomId:this._context.roomId}}).then(e=>e.data.code===C).finally(()=>{this._context.eventBus.emit("inner:sdk:disconnected",{source:"http"})})}onDispose(){this._httpService.dispose()}}class z extends h{constructor(e){super(),this._videoController=null,this._audioController=null,this._conversationController=null,this._liveKitService=null,this._conversationTransport=null,this._connectionCoordinator=null,this._httpController=null,this._preConnectPromise=null,this._lastPreConnectSuccessAt=null,this._context=new re(e),i.info("SDK Client created")}get events(){return this.ensureNotDisposed(),this._publicEventAPI||(this._publicEmitter=new He(this._context.eventBus),this._publicEventAPI=this._publicEmitter.publicAPI()),this._publicEventAPI}pushActions({onceActions:e,loopActions:t}){return this.ensureNotDisposed(),this._ensureConnected(),this._httpController?.switchVideo({onceActions:e,loopActions:t})}interruptConnection(){return this.ensureNotDisposed(),this._ensureConnected(),this._httpController?.interruptConversation()}sendTextQuestion(e){if(this.ensureNotDisposed(),this._ensureConnected(),this._conversationController)return this._conversationController.sendQuestion(e);throw new u("Conversation controller is not available",d.CONVERSATION_CONTROLLER_NOT_AVAILABLE)}setRenderFitMode(e){this.ensureNotDisposed(),this._ensureConnected(),this._videoController&&this._videoController.setRenderFitMode(e)}async startAudioCapture(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",d.AUDIO_CONTROLLER_NOT_AVAILABLE);await this._audioController.startCapture()}async stopAudioCapture(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",d.AUDIO_CONTROLLER_NOT_AVAILABLE);await this._audioController.stopCapture()}setVolume(e){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",d.AUDIO_CONTROLLER_NOT_AVAILABLE);this._audioController.setVolume(e)}getVolume(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",d.AUDIO_CONTROLLER_NOT_AVAILABLE);return this._audioController?.getVolume()||1}mute(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",d.AUDIO_CONTROLLER_NOT_AVAILABLE);this._audioController.mute()}unmute(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",d.AUDIO_CONTROLLER_NOT_AVAILABLE);this._audioController.unmute()}get isMuted(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",d.AUDIO_CONTROLLER_NOT_AVAILABLE);return this._audioController.isMuted()}get isAudioCapturing(){return this.ensureNotDisposed(),this._ensureConnected(),this._audioController?this._audioController.isCapturing:!1}async preConnect(){if(this.ensureNotDisposed(),this._preConnectPromise)return this._preConnectPromise;const e=Date.now();return this._lastPreConnectSuccessAt!==null&&e-this._lastPreConnectSuccessAt<ne?Promise.resolve(!0):(this._preConnectPromise=(async()=>{try{if(i.info("Creating HTTP controller..."),this._httpController=this._httpController??new Ge(this._context,this._context.options.http?.baseURL),i.info("Getting auth token..."),await this._httpController.getAuthToken(),i.info("Getting livekit config..."),!!!await this._httpController?.getLivekitConfig())throw new Error("Config missing");return this._lastPreConnectSuccessAt=Date.now(),!0}catch(t){throw this._context.emitError(t,d.SDK_PRECONNECT_FAILED)}finally{this._preConnectPromise=null}})(),this._preConnectPromise)}async connect(){if(this.ensureNotDisposed(),this._connectionCoordinator&&this._context.sessionState.isConnected){i.warn("SDK already initialized");return}try{if(!await this.preConnect())throw new u("PreConnect failed without explicit error",d.SDK_CONNECT_FAILED)}catch(e){throw this._context.emitError(e,d.SDK_CONNECT_FAILED)}try{if(i.info("Initializing SDK..."),this._connectionCoordinator=new ae(this._context),this._connectionCoordinator.start(),this._connectionCoordinator.startConnecting(),this._videoController=new _e(this._context,this._context.options.video),this._audioController=new xe(this._context,this._context.options.audio),this._conversationController=new Ve(this._context),this._liveKitService=new We(this._context),this._conversationTransport=new $e(this._context),this._liveKitService&&this._videoController&&this._audioController){const e=this._liveKitService.getEventAdapter();e&&(typeof e.setVideoController=="function"&&e.setVideoController(this._videoController),typeof e.setAudioController=="function"&&e.setAudioController(this._audioController))}this._audioController&&this._conversationTransport&&this._audioController.setWebSocketSender({sendBinary:e=>{this._conversationTransport.sendBinary(e)},isConnected:()=>this._conversationTransport.isConnected}),this._conversationTransport&&this._conversationController&&(this._conversationTransport.setConversationManager(this._conversationController.manager),this._conversationController.setTransport(this._conversationTransport)),await this._liveKitService.connect(this._context.livekitUrl,this._context.token,this._context.options.avatarName),this._conversationTransport&&await this._conversationTransport.connect(),i.info("SDK initialized and connected")}catch(e){throw this._connectionCoordinator&&this._connectionCoordinator.transitionToIdle(),await this._cleanup(),this._context.emitError(e,d.SDK_INITIALIZATION_FAILED)}}async disconnect(){if(this.ensureNotDisposed(),!(!this._connectionCoordinator||this._connectionCoordinator.getState()==="idle"||this._connectionCoordinator.getState()==="disposed")){i.info("Disconnecting SDK..."),this._connectionCoordinator.startDisconnecting();try{this._conversationTransport&&this._conversationTransport.disconnect(),this._liveKitService&&await this._liveKitService.disconnect(),this._audioController&&this._audioController.stopCapture(),this._httpController&&await this._httpController.disconnectConversation(),this._preConnectPromise=null,this._connectionCoordinator.completeDisconnecting(),i.info("SDK disconnected")}catch(e){throw i.error("Error during disconnect:",e),this._context.emitError(e,d.SDK_DISCONNECT_FAILED)}}}async reconnect(){this.ensureNotDisposed();const e=this._connectionCoordinator;if(!e?.canStartReconnectingManual()){const s=e?e.getSnapshot():this.connectionSnapshot;return i.warn("SDK reconnect not allowed in current state, returning current snapshot"),s}const t=e.captureSnapshotAndStartReconnectingManual();try{if(this._audioController&&this._audioController.stopCapture(),t.rtc.connected&&!t.rtc.hasVideoTrack&&(await this._liveKitService?.disconnect(),await this._conversationTransport?.disconnect()),this._conversationTransport&&!this._conversationTransport.isConnected){if(!await this.preConnect())throw new u("PreConnect failed without explicit error",d.SDK_CONNECT_FAILED);await this._conversationTransport.connect()}return this._liveKitService&&!this._liveKitService.isConnected()&&await this._liveKitService.connect(this._context.livekitUrl,this._context.token,this._context.options.avatarName),t}catch(s){throw i.error("Error during reconnect:",s),this._context.emitError(s,d.SDK_RECONNECT_FAILED)}}get connectionSnapshot(){return this.ensureNotDisposed(),this._connectionCoordinator?this._connectionCoordinator.getSnapshot():K(U("idle"))}async _cleanup(){if(this._conversationTransport&&this._conversationTransport.disconnect(),this._liveKitService)try{await this._liveKitService.disconnect()}catch(e){i.error("Error disconnecting LiveKit:",e)}this._audioController&&this._audioController.stopCapture()}get isConnected(){return this.ensureNotDisposed(),this._context.sessionState.isConnected}_ensureConnected(){if(!this._context.sessionState.isConnected)throw new u("SDK is not connected. Call connect() first.",d.SDK_NOT_CONNECTED)}onDispose(){this.disconnect().catch(e=>{i.error("Error during disconnect in dispose:",e)}),this._videoController&&(this._videoController.dispose(),this._videoController=null),this._audioController&&(this._audioController.dispose(),this._audioController=null),this._conversationController&&(this._conversationController.dispose(),this._conversationController=null),this._liveKitService&&(this._liveKitService.dispose(),this._liveKitService=null),this._conversationTransport&&(this._conversationTransport.dispose(),this._conversationTransport=null),this._httpController&&(this._httpController.dispose(),this._httpController=null),this._connectionCoordinator&&(this._connectionCoordinator.transitionToDisposed(),this._connectionCoordinator.dispose(),this._connectionCoordinator=null),this._context.dispose(),i.info("SDK Client disposed")}}function je(o){if(!o.avatarName)throw new u("avatarName is required",d.NO_AVATARNAME);return i.info("Creating SDK client with options:",{avatarName:o.avatarName}),new z(o)}class F extends h{constructor(e,t){super(),this._videoElement=null,this._videoUrl=null,this._isPlaying=!1,this._frameCallbackId=null,this._videoUrl=e,t?this._videoElement=t:(this._videoElement=document.createElement("video"),this._videoElement.style.opacity="0",this._videoElement.muted=!0,this._videoElement.playsInline=!0,this._videoElement.loop=!0,document.querySelector("#video-container")?.appendChild(this._videoElement)),i.info("Local Video Debug Source created")}get videoElement(){return this._videoElement}async load(){if(!this._videoElement)throw new Error("Video element or URL not set");return new Promise((e,t)=>{const s=this._videoElement,n=()=>{s.removeEventListener("loadedmetadata",n),s.removeEventListener("error",r),i.info(`Video loaded: ${s.videoWidth}x${s.videoHeight}`),e()},r=a=>{s.removeEventListener("loadedmetadata",n),s.removeEventListener("error",r),t(new Error(`Failed to load video: ${a}`))};s.addEventListener("loadedmetadata",n),s.addEventListener("error",r),s.src=this._videoUrl||"",s.load()})}async play(){if(!this._videoElement)throw new Error("Video element not set");if(!this._isPlaying)try{await this._videoElement.play(),this._videoElement.style.opacity="0",this._videoElement.setAttribute("loop","true"),this._isPlaying=!0,i.info("Video playback started")}catch(e){throw i.error("Failed to play video:",e),e}}pause(){this._videoElement&&this._isPlaying&&(this._videoElement.pause(),this._isPlaying=!1,i.info("Video playback paused"))}stop(){this._videoElement&&(this._videoElement.pause(),this._videoElement.currentTime=0,this._isPlaying=!1,i.info("Video playback stopped"))}get isPlaying(){return this._isPlaying&&this._videoElement!==null&&!this._videoElement.paused}get videoWidth(){return this._videoElement?.videoWidth||0}get videoHeight(){return this._videoElement?.videoHeight||0}async captureFrame(){if(!this._videoElement||this._videoElement.readyState<2)return null;try{if("VideoFrame"in window&&typeof window.VideoFrame=="function")try{return new VideoFrame(this._videoElement,{timestamp:this._videoElement.currentTime*1e6})}catch(s){i.error("",s)}const e=await createImageBitmap(this._videoElement),t=new VideoFrame(e,{timestamp:this._videoElement.currentTime*1e6});return e.close(),t}catch(e){return i.error("Failed to capture video frame:",e),null}}onFrame(e){if(!this._videoElement)return;this.offFrame();const t=this._videoElement;if("requestVideoFrameCallback"in t){const s=(n,r)=>{!this._isPlaying||!this._videoElement||this.captureFrame().then(a=>{a&&(e(a),a.close()),this._isPlaying&&this._videoElement&&(this._frameCallbackId=t.requestVideoFrameCallback(s))}).catch(a=>{i.error("Error in frame callback:",a)})};this._frameCallbackId=t.requestVideoFrameCallback(s)}else{const s=()=>{!this._isPlaying||!this._videoElement||this.captureFrame().then(n=>{n&&(e(n),n.close()),this._isPlaying&&this._videoElement&&(this._frameCallbackId=requestAnimationFrame(s))}).catch(n=>{i.error("Error in frame callback:",n)})};this._frameCallbackId=requestAnimationFrame(s)}}offFrame(){this._frameCallbackId!==null&&(this._videoElement&&"cancelVideoFrameCallback"in this._videoElement?this._videoElement.cancelVideoFrameCallback(this._frameCallbackId):cancelAnimationFrame(this._frameCallbackId),this._frameCallbackId=null)}onDispose(){this.stop(),this.offFrame(),this._videoElement&&this._videoElement.parentElement===document.body&&document.body.removeChild(this._videoElement),this._videoElement=null,this._videoUrl=null,i.info("Local Video Debug Source disposed")}}class Qe extends h{constructor(e,t,s){super(),this._outputCanvas=null,this._outputRenderer=null,this._source=new B;const n={...e,renderMode:"processed"};this._pipeline=new H(this._source,n),this._outputRenderer=new D(s,t),this._pipeline.setRenderer(this._outputRenderer),this._outputCanvas=this._outputRenderer.getCanvas(),i.info("Video Pipeline Runner created")}get outputCanvas(){return this._outputCanvas}get processor(){return this._pipeline.strategy.getProcessors()[0]||null}setProcessor(e){this._pipeline.setProcessor(e),i.info("Processor set on pipeline")}async processFrame(e){if(this.ensureNotDisposed(),!this._outputRenderer){i.error("Output renderer not available"),e.close();return}try{const t=this._pipeline.strategy.getProcessors()[0]||null;let s=null;if(t){try{s=t.process(e)}catch(n){i.error("Error processing frame:",n),s=e}if(s===null&&"getImageData"in t){const n=t.getImageData();if(n&&this._outputCanvas&&this._outputRenderer.getContext()){const r=this._outputRenderer.getContext();r&&((this._outputCanvas.width!==n.width||this._outputCanvas.height!==n.height)&&(this._outputCanvas.width=n.width,this._outputCanvas.height=n.height),r.putImageData(n,0,0))}e.close();return}s&&s!==e?(this._outputRenderer.render(s),e.close()):s===e?this._outputRenderer.render(e):e.close()}else this._outputRenderer.render(e)}catch(t){i.error("Error in processFrame:",t),e.close()}}setOutputSize(e,t){this._outputCanvas&&(this._outputCanvas.width=e,this._outputCanvas.height=t,i.info(`Output canvas size set to ${e}x${t}`))}onDispose(){this._pipeline.dispose(),this._outputRenderer&&(this._outputRenderer.dispose(),this._outputRenderer=null),this._outputCanvas&&this._outputCanvas.parentElement===document.body&&document.body.removeChild(this._outputCanvas),this._outputCanvas=null,i.info("Video Pipeline Runner disposed")}}class Ye extends h{constructor(e,t,s,n){if(super(),this._isRunning=!1,this._videoSource=new F(e,t),this._options={renderMode:"processed",containerElement:t,...n},this._pipelineRunner=new Qe(this._options,t,s),this._options.greenScreen?.enabled){const r=new w(this._options.greenScreen);this._pipelineRunner.setProcessor(r),i.info("Green screen processor enabled in debugger")}i.info("Standalone Video Debugger created")}async switchSource(e,t){this.ensureNotDisposed();const s=this._videoSource,n=this._isRunning,r=new F(e,t);try{await r.load(),n&&await r.play(),r.onFrame(l=>{this._pipelineRunner.processFrame(l).catch(p=>{i.error("Error processing frame:",p)})}),this._videoSource=r;try{s.offFrame(),s.stop(),s.dispose()}catch(l){console.warn("Error disposing old video source during switch:",l)}const a=this._videoSource.videoWidth,c=this._videoSource.videoHeight;a>0&&c>0&&this._pipelineRunner.setOutputSize(a,c)}catch(a){i.error("Failed to switch video source:",a);try{r.dispose()}catch(c){console.warn("Error disposing new video source after failed switch:",c)}throw a}}get outputCanvas(){return this._pipelineRunner.outputCanvas}get videoSource(){return this._videoSource}get processor(){return this._pipelineRunner.processor}setProcessor(e){this._pipelineRunner.setProcessor(e)}updateGreenScreenOptions(e){const t=this._pipelineRunner.processor;if(t&&"updateOptions"in t&&typeof t.updateOptions=="function")t.updateOptions(e),i.info("Green screen options updated");else{const s=this._options.greenScreen||{enabled:!1},n={...s,...e,enabled:e.enabled!==void 0?e.enabled:s.enabled},r=new w(n);this._pipelineRunner.setProcessor(r),this._options.greenScreen=n,i.info("Green screen processor recreated with new options")}}async initialize(){this.ensureNotDisposed();try{await this._videoSource.load();const e=this._videoSource.videoWidth,t=this._videoSource.videoHeight;e>0&&t>0&&this._pipelineRunner.setOutputSize(e,t),i.info("Video debugger initialized")}catch(e){throw i.error("Failed to initialize video debugger:",e),e}}async start(){if(this.ensureNotDisposed(),this._isRunning){i.warn("Debugger is already running");return}try{this._videoSource.videoWidth===0&&await this.initialize(),await this._videoSource.play(),this._videoSource.onFrame(e=>{this._pipelineRunner.processFrame(e).catch(t=>{i.error("Error processing frame:",t)})}),this._isRunning=!0,i.info("Video debugger started")}catch(e){throw i.error("Failed to start video debugger:",e),e}}stop(){this._isRunning&&(this._videoSource.stop(),this._videoSource.offFrame(),this._isRunning=!1,i.info("Video debugger stopped"))}pause(){this._isRunning&&(this._videoSource.pause(),i.info("Video debugger paused"))}async resume(){this._isRunning&&(await this._videoSource.play(),i.info("Video debugger resumed"))}get isRunning(){return this._isRunning}onDispose(){this.stop(),this._videoSource.dispose(),this._pipelineRunner.dispose(),i.info("Standalone Video Debugger disposed")}}exports.DEFAULT_AUDIO_BUFFER_SIZE=ee;exports.DEFAULT_AUDIO_CHANNELS=Z;exports.DEFAULT_AUDIO_SAMPLE_RATE=R;exports.DEFAULT_RECONNECT_ATTEMPTS=S;exports.DEFAULT_RECONNECT_DELAY=y;exports.DEFAULT_WS_TIMEOUT=X;exports.Disposable=h;exports.EventBus=M;exports.Logger=i;exports.SDKClient=z;exports.SDKError=u;exports.SessionState=V;exports.StandaloneVideoDebugger=Ye;exports.createClient=je;
|
|
110
|
+
`;function Ve(){const r=new Blob([Me],{type:"text/javascript"});return URL.createObjectURL(r)}class Ue extends Fe{constructor(e){super(),this.options=e,this._stream=null,this._audioContext=null,this._sourceNode=null,this._workletNode=null,this._targetSampleRate=y,this._bitDepth=x,this._channels=1;const t=e?.input;this._targetSampleRate=t?.sampleRate||y,this._bitDepth=t?.bitDepth||x,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=Ve();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:m}=c.data;d==="audioData"&&p&&this.emitAudioData(p,m)},this._sourceNode.connect(this._workletNode),i.info(`Audio capture started: input=${o}Hz -> output=${this._targetSampleRate}Hz, ${this._bitDepth}bit`)}catch(e){throw u.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),i.info("Audio capture stopped")}onDispose(){this.stop()}}class Ke{create(e){return new Ue(e)}}class Be extends l{constructor(e,t,s){super(),this._source=null,this._isCapturing=!1,this._currentOutputTrackId=null,this._context=e,this._inputPipeline=new Ae(t),this._outputPipeline=new Re,this._frameSender=new Pe(e,t),this._sourceFactory=s||new Ke,this._inputPipeline.onProcessedAudio=(n,o)=>{this._context.eventBus.emit("inner:audio:frame:received",{data:n,sampleRate:o})},i.info("Audio Controller created")}setWebSocketSender(e){this._frameSender.setWebSocketSender(e)}async startCapture(){if(this.ensureNotDisposed(),this._isCapturing){i.warn("Audio capture already started");return}try{this._source||(this._source=this._sourceFactory.create(this._context.options.audio),this._source.onAudioData=(e,t)=>{this._inputPipeline.process(e,t)}),await this._source.start(),this._isCapturing=!0,this._frameSender.start(),this._context.eventBus.emit("inner:audio:input:started",void 0),i.info("Audio capture started")}catch(e){throw this._context.emitError(e,h.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 We extends l{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 u.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}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 R=640,P=640;class $e extends l{constructor(e,t,s){super(),this._videoElement=null,this._canvas=null,this._videoElement=e,this._onFrame=t,this._context=s,this._handleServerCommand=this._onServerCommand.bind(this)}setVideoElement(e){this._videoElement=e}start(){this.ensureNotDisposed(),this._canvas||(typeof OffscreenCanvas<"u"?this._canvas=new OffscreenCanvas(R,P):(this._canvas=document.createElement("canvas"),this._canvas.width=R,this._canvas.height=P)),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,a=R,c=R*o/n;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:S}).then(d=>{if(d)try{this._onFrame(d,a,c,0,S)}catch(p){i.error("ScreenshotScheduler onFrame callback error",p)}}).catch(d=>{i.error("ScreenshotScheduler convertToBlob error",d)}):t instanceof HTMLCanvasElement&&t.toBlob(d=>{if(d)try{this._onFrame(d,a,c,0,S)}catch(p){i.error("ScreenshotScheduler onFrame callback error",p)}},"image/jpeg",S)}onDispose(){this.stop(),this._canvas=null,this._videoElement=null}}async function He(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,ze=2,Ge=0,F=0,M=4,V=65535,U=4294967295;function je(r){const e=Math.floor(r);return e<0?0:e>255?255:e}function D(r){const e=Math.floor(r);return e<0?0:e>V?V:e}function Qe(r){const e=Math.floor(r);return e<0?0:e>U?U:e}function Xe(r){const e=Math.floor(r);return e<F?F:e>M?M:e}function Ye(r){const e=new ArrayBuffer(qe),t=new DataView(e),s=Xe(r.format)&15,n=ze<<6|Ge<<4|s;return t.setUint8(0,n),t.setUint8(1,je(r.quality)),t.setUint16(2,D(r.id),!1),t.setUint16(4,D(r.width),!1),t.setUint16(6,D(r.height),!1),t.setUint32(8,Qe(r.payloadLength),!1),new Uint8Array(e)}const Je=65535;class Ze extends l{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=Ye({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>=Je?0:this._imageId+1,e}onDispose(){this._wsSender=null}}class et extends l{constructor(e){super(),this._isRunning=!1,this._context=e,this._source=new We,this._sender=new Ze;const t=(s,n,o,a,c=S)=>{He(s,{width:n,height:o,format:a,quality:c}).then(d=>{this._sender.send(d.data,{width:d.width,height:d.height,format:d.format,quality:d.quality})})};this._scheduler=new $e(null,t,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()}}exports.ConversationStateType=void 0;(function(r){r.IDLE="idle",r.WAITING="waiting",r.STREAMING="streaming",r.COMPLETED="completed",r.ERROR="error"})(exports.ConversationStateType||(exports.ConversationStateType={}));class tt extends Q{constructor(){super(...arguments),this._currentState=exports.ConversationStateType.IDLE,this.transitionTable=[{from:exports.ConversationStateType.IDLE,to:[exports.ConversationStateType.WAITING,exports.ConversationStateType.ERROR]},{from:exports.ConversationStateType.WAITING,to:[exports.ConversationStateType.STREAMING,exports.ConversationStateType.ERROR]},{from:exports.ConversationStateType.STREAMING,to:[exports.ConversationStateType.COMPLETED,exports.ConversationStateType.ERROR]},{from:exports.ConversationStateType.COMPLETED,to:[exports.ConversationStateType.IDLE,exports.ConversationStateType.WAITING,exports.ConversationStateType.ERROR]},{from:exports.ConversationStateType.ERROR,to:[exports.ConversationStateType.IDLE]}]}reset(){this._currentState=exports.ConversationStateType.IDLE}}class K extends l{constructor(e,t){super(),this._currentQuestion=null,this._currentAnswer=null,this._sessionId=e,this._stateMachine=new tt,i.info(`Conversation session created: ${e}`)}get sessionId(){return this._sessionId}get state(){return this._stateMachine.getCurrentState()}_setQuestion(e){this._currentQuestion=e,this._stateMachine.transitionTo(exports.ConversationStateType.WAITING),this._syncContextState()}get currentQuestion(){return this._currentQuestion}_startAnswer(e){this._currentAnswer={questionId:e,text:"",isComplete:!1,timestamp:Date.now()},this._stateMachine.transitionTo(exports.ConversationStateType.STREAMING),this._syncContextState()}_appendAnswerChunk(e){this._currentAnswer&&(this._currentAnswer.text+=e)}_completeAnswer(){this._currentAnswer&&(this._currentAnswer.isComplete=!0,this._currentAnswer.completedAt=Date.now()),this._stateMachine.transitionTo(exports.ConversationStateType.COMPLETED),this._syncContextState()}get currentAnswer(){return this._currentAnswer}_setError(e){this._stateMachine.transitionTo(exports.ConversationStateType.ERROR),this._syncContextState()}_syncContextState(){}handleEvent(e){const t=this._stateMachine.getCurrentState();switch(e.type){case"QUESTION":if(t!==exports.ConversationStateType.IDLE&&t!==exports.ConversationStateType.COMPLETED){i.warn(`Ignoring QUESTION event in invalid state: ${t}`);return}this._setQuestion(e.payload);break;case"ANSWER_START":if(t!==exports.ConversationStateType.WAITING){i.warn(`Ignoring ANSWER_START event in invalid state: ${t}. Expected: WAITING`);return}this._startAnswer(e.payload);break;case"ANSWER_CHUNK":if(t!==exports.ConversationStateType.STREAMING){i.warn(`Ignoring ANSWER_CHUNK event in invalid state: ${t}. Expected: STREAMING`);return}this._appendAnswerChunk(e.payload&&e.payload.chunk||""),e.payload&&e.payload.isComplete&&this._completeAnswer();break;case"ANSWER_COMPLETE":if(t!==exports.ConversationStateType.STREAMING){i.warn(`Ignoring ANSWER_COMPLETE event in invalid state: ${t}. Expected: STREAMING`);return}this._completeAnswer();break;case"ERROR":this._setError(e.payload);break;default:i.warn(`Unknown event type: ${e.type}`);break}}reset(){this._stateMachine.reset(),this._currentQuestion=null,this._currentAnswer=null}onDispose(){this.reset(),i.info(`Conversation session disposed: ${this._sessionId}`)}}class st extends l{constructor(){super(...arguments),this._buffers=new Map}appendChunk(e,t){this.ensureNotDisposed();const n=(this._buffers.get(e)||"")+t;return this._buffers.set(e,n),i.debug(`Message chunk appended for question ${e}, length: ${n.length}`),n}getMessage(e){return this._buffers.get(e)||null}complete(e){const t=this.getMessage(e)||"";return this._buffers.delete(e),i.debug(`Message assembly completed for question ${e}, length: ${t.length}`),t}clear(e){this._buffers.delete(e)}onDispose(){this._buffers.clear(),i.info("Message Assembler disposed")}}class it{generateSessionId(){return`session_${Date.now()}_${Math.random().toString(36).substring(2,9)}`}generateQuestionId(){return Math.random().toString(36).substring(2,6)}}class nt extends l{constructor(e,t){super(),this._transport=null,this._sessions=new Map,this._context=e,this._assembler=new st,this._idService=new it,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 K(o,this._context);if(a.handleEvent({type:"QUESTION",payload:n}),this._sessions.set(s,a),this._transport)try{this._transport.sendText(s,e,200,t)}catch(c){throw i.error("Error sending question via transport:",c),a.handleEvent({type:"ERROR",payload:"Failed to send question"}),this._context.eventBus.emit("inner:sdk:error",{error:c instanceof Error?c:new Error(String(c))}),c}else i.warn("Transport not set, question not sent");return this._context.eventBus.emit("inner:conversation:question:sent",{questionId:s,text:e}),this._context.eventBus.emit("inner:conversation:answer:waiting",{questionId:s}),i.info(`Question sent: ${s}`),s}handleServerPong(){this.ensureNotDisposed(),this._transport&&this._transport.handlePongReceived()}willDisconnect(){this.ensureNotDisposed(),this._transport?.willDisconnect()}handleTextInput(e){this.sendQuestion(e).catch(t=>{i.error("Error sending question:",t)})}handleAnswerChunk(e,t,s){this.ensureNotDisposed();const n=this._sessions.get(e);if(!n){i.warn(`No active session for answer chunk: ${e}`);return}if(n.state===exports.ConversationStateType.WAITING&&n.handleEvent({type:"ANSWER_START",payload:e}),s){n.handleEvent({type:"ANSWER_COMPLETE"});const a=this._assembler.complete(e);this._context.eventBus.emit("inner:conversation:answer:completed",{questionId:e,fullAnswer:a});try{n.dispose()}catch(c){i.error("Error disposing session:",c)}this._sessions.delete(e)}else n.handleEvent({type:"ANSWER_CHUNK",payload:{uid:e,chunk:t,isComplete:s}}),this._assembler.appendChunk(e,t),this._context.eventBus.emit("inner:conversation:answer:chunk",{questionId:e,chunk:t,isComplete:s})}handleServerInitiatedMessage(e,t){this.ensureNotDisposed(),this._context.eventBus.emit("inner:conversation:server:message",{questionId:e,message:t,type:"text"})}handleAsrTextReceived(e,t){this.ensureNotDisposed();const s=this._idService.generateSessionId(),n=new K(s,this._context);n.handleEvent({type:"QUESTION",payload:{id:e,text:t,timestamp:Date.now(),inputMode:"voice"}}),this._sessions.set(e,n),this._context.eventBus.emit("inner:conversation:asr:received",{questionId:e,text:t})}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 rt extends l{constructor(e,t){super(),this._manager=new nt(e,t),i.info("Conversation Controller created")}get manager(){return this._manager}setTransport(e){this._manager.setTransport(e)}async sendQuestion(e,t){return this.ensureNotDisposed(),this._manager.sendQuestion(e,t)}onDispose(){this._manager.dispose(),i.info("Conversation Controller disposed")}}class ot extends l{constructor(){super(...arguments),this._tracks=new Map,this._participants=new Map}register(e,t,s){this.ensureNotDisposed(),this._tracks.set(e,t),this._participants.has(s)||this._participants.set(s,new Set),this._participants.get(s).add(e),t.on("ended",()=>{i.debug(`Track ended: ${e}`),this.unregister(e)}),i.debug(`Track registered: ${e} from participant ${s}`)}unregister(e){const t=this._tracks.get(e);if(t){t.stop(),this._tracks.delete(e);for(const[s,n]of this._participants.entries())if(n.has(e)){n.delete(e),n.size===0&&this._participants.delete(s);break}i.debug(`Track unregistered: ${e}`)}}getTrack(e){return this._tracks.get(e)||null}getParticipantTracks(e){const t=this._participants.get(e);return t?Array.from(t):[]}getAllTrackIds(){return Array.from(this._tracks.keys())}getVideoAvailability(){let e=!1,t=!1,s=!1;for(const n of this._tracks.values()){const 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:g.NO_VIDEO_TRACK}:{available:!1,reason:g.TRACK_MUTED}:{available:!1,reason:g.TRACK_ENDED}:{available:!1,reason:g.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 B extends l{constructor(e,t){super(),this._videoController=null,this._room=null,this._context=e,this._trackRegistry=t,this._loosTrackTimer=null,i.info("Video Track Handler created")}setRoom(e){this._room=e}setVideoController(e){this._videoController=e}handleTrackSubscribed(e,t,s){this.ensureNotDisposed(),this._loosTrackTimer&&clearTimeout(this._loosTrackTimer),this._trackRegistry.register(t,e,s),this._videoController&&e.attach(this._videoController.getSource().getInternalElement()),e.on("videoPlaybackStarted",()=>{i.info("inner:rtc:video:available"),this._context.eventBus.emit("inner:rtc:video:available",void 0)}),this._context.eventBus.emit("inner:video:track:added",{trackId:t,participantId:s}),i.debug(`Video track subscribed: ${t}`)}handleTrackUnsubscribed(e,t,s){this.ensureNotDisposed(),this._videoController&&e.detach(this._videoController.getSource().getInternalElement()),e.removeAllListeners(),this._context.eventBus.emit("inner:video:track:removed",{trackId:t,participantId:s}),this._loosTrackTimer&&clearTimeout(this._loosTrackTimer),this._loosTrackTimer=setTimeout(()=>{this._context.eventBus.emit("inner:rtc:video:unavailable",void 0)},ne),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 W extends l{constructor(e,t){super(),this._audioController=null,this._room=null,this._context=e,this._trackRegistry=t,i.info("Audio Track Handler created")}setRoom(e){this._room=e}setAudioController(e){this._audioController=e}handleTrackSubscribed(e,t,s){this.ensureNotDisposed(),this._trackRegistry.register(t,e,s),this._audioController?e.attach(this._audioController.getOutputElement()):this._context.eventBus.emit("inner:audio:track:added",{trackId:t,participantId:s}),i.debug(`Audio track subscribed: ${t}`)}handleTrackUnsubscribed(e,t,s){this.ensureNotDisposed(),this._audioController?(this._audioController.removeOutputTrack(t),e.detach(this._audioController.getOutputElement())):this._context.eventBus.emit("inner:audio:track:removed",{trackId:t}),this._trackRegistry.unregister(t),i.debug(`Audio track unsubscribed: ${t}`)}supports(e){return e.mediaStreamTrack.kind==="audio"}onDispose(){i.info("Audio Track Handler disposed")}}class at extends l{constructor(e,t){super(),this._room=null,this._context=e,this._trackRegistry=t,this._handlers=[new B(e,t),new W(e,t)],i.info("LiveKit Event Adapter created")}setRoom(e){this._room=e,this._handlers.forEach(t=>{t.setRoom(e)}),console.log("LiveKit room set:",this._room)}setVideoController(e){const t=this._handlers.find(s=>s instanceof B);t&&t.setVideoController(e)}setAudioController(e){const t=this._handlers.find(s=>s instanceof W);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 ct extends l{constructor(e){super(),this._room=null,this._context=e,this._trackRegistry=new ot,this._eventAdapter=new at(e,this._trackRegistry),this._maxAttempts=e.options.reconnect?.maxAttempts||A,this._maxDelay=(e.options.reconnect?.delay??I)*T,i.info("LiveKit Service created")}async connect(e,t,s,n){if(this.ensureNotDisposed(),this._room&&this._room.isConnected()){i.warn("Already connected to LiveKit room");return}try{i.info(`Connecting to LiveKit room: ${s} at ${e}`);const o=new f.Room({reconnectPolicy:{nextRetryDelayInMs:a=>a.elapsedMs<this._maxDelay?Math.min(T*Math.pow(2,a.retryCount),this._maxDelay):null}});o.prepareConnection(e,t),o.on(f.RoomEvent.TrackSubscribed,(a,c,d)=>{this._handleTrackSubscribed(a,c,d)}),o.on(f.RoomEvent.TrackUnsubscribed,(a,c,d)=>{this._handleTrackUnsubscribed(a,c,d)}),o.on(f.RoomEvent.TrackMuted,(a,c)=>{this._handleTrackMuted(a,c)}),o.on(f.RoomEvent.TrackUnmuted,(a,c)=>{this._handleTrackUnmuted(a,c)}),o.on(f.RoomEvent.Connected,()=>{i.info("Connected to LiveKit room"),this._context.eventBus.emit("inner:rtc:connected",void 0)}),o.on(f.RoomEvent.Disconnected,a=>{a?i.info(`LiveKit room disconnected: ${a}`):i.info("LiveKit room disconnected"),a!==f.DisconnectReason.CLIENT_INITIATED&&this._context.eventBus.emit("inner:rtc:disconnected",{reason:a?String(a):void 0}),this._room=null}),i.info("Connecting to LiveKit room..."),await o.connect(e,t,{autoSubscribe:!0,maxRetries:this._context.options.reconnect?.maxAttempts||A}),this._eventAdapter.setRoom(o),this._room=o}catch(o){throw u.fromError(o,h.LIVEKIT_CONNECT_FAILED)}}_handleTrackSubscribed(e,t,s){try{if(!e.mediaStreamTrack){i.warn("Track does not have MediaStreamTrack");return}e.kind==="audio"&&s.on(f.ParticipantEvent.IsSpeakingChanged,c=>{console.log("\u8F68\u9053 \u8BF4\u8BDD\u72B6\u6001:",c)});const 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===f.ConnectionState.Connected:!1}getTrackRegistry(){return this._trackRegistry}get trackRegistry(){return this._trackRegistry}getEventAdapter(){return this._eventAdapter}onDispose(){this.disconnect(),this._trackRegistry.dispose(),this._eventAdapter.dispose(),i.info("LiveKit Service disposed")}}class dt extends l{constructor(e){super(),this._conversationManager=null,this._context=e,i.info("WebSocket Dispatcher created")}setConversationManager(e){this._conversationManager=e,i.debug("Conversation manager set in dispatcher")}dispatch(e){this.ensureNotDisposed();try{if(e.type==="ws"){this._handleWSServerMessage(e);return}if(e.type==="text"){this._handleServerTextMessage(e);return}}catch(t){i.error("Error dispatching message:",t)}}_handleWSServerMessage(e){e.code===101?(this._conversationManager?.handleServerPong(),i.debug("Received pingpong response")):e.code===102&&(i.warn("Server will disconnect"),this._conversationManager?.willDisconnect())}_handleServerTextMessage(e){if(!this._conversationManager){i.warn("Conversation manager not set, text message ignored");return}const{uid:t,payload:s,code:n}=e;n===200?(this._conversationManager.handleAnswerChunk(t,s,!1),i.debug(`Answer received (code 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 ht extends l{constructor(e){super(),this._ws=null,this._dispatcher=null,this._isConnecting=!1,this._activeTimers=new Set,this._retryCount=0,this._maxRetries=A,this._pingTimer=void 0,this._pongTimer=void 0,this._isExplicitDisconnect=!1,this._context=e,this._maxRetries=e.options.reconnect?.maxAttempts??A,this._maxDelay=(e.options.reconnect?.delay??I)*T,this._dispatcher=new dt(e)}setConversationManager(e){this._dispatcher&&this._dispatcher.setConversationManager(e)}async connect(){if(this.ensureNotDisposed(),this._stopHeartbeat(),!(this._isConnecting||this.isConnected||this._ws?.readyState===WebSocket.OPEN)){this._isConnecting=!0,this._isExplicitDisconnect=!1;try{return this._ws=new WebSocket(this._context.wsUrl),new Promise((e,t)=>{this._ws.onopen=()=>{this._isConnecting=!1,this._retryCount=0,this.sendPing(),this._context.eventBus.emit("inner:ws:connected",void 0),i.info("Conversation transport connected"),e()},this._ws.onerror=s=>{this._isConnecting=!1,i.error("Conversation transport error:",s),this._context.eventBus.emit("inner:sdk:error",{error:new u("WebSocket connection error",h.WS_CONNECT_ERROR,s)}),t(new u("WebSocket connection error",h.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,h.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 (${O/T}s). Reconnecting...`),this._ws?.close()},O)}handlePongReceived(){clearTimeout(this._pongTimer),this._clearTimer(this._pingTimer),this._pingTimer=setTimeout(()=>{this.sendPing()},oe)}willDisconnect(){if(this.ensureNotDisposed(),this._ws&&this._ws.readyState===WebSocket.OPEN){const t=JSON.stringify({type:"ws",code:102,payload:"will disconnect"});this._ws.send(t),this._isExplicitDisconnect=!0,this._ws.close(),this._ws=null,this._context.eventBus.emit("inner:ws:disconnected",{reason:"Client responsive to server's disconnect message."})}}sendText(e,t,s=200,n){if(this.ensureNotDisposed(),!this._ws||this._ws.readyState!==WebSocket.OPEN)throw new u("WebSocket is not connected",h.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,h.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,h.WS_PARSE_ERROR)}}sendBinary(e){if(this.ensureNotDisposed(),!this._ws||this._ws.readyState!==WebSocket.OPEN){i.debug("WebSocket not connected, binary data dropped");return}try{this._ws.send(e),i.debug("Binary data sent via transport")}catch(t){i.error("Error sending binary data:",t)}}get isConnected(){return this._ws?.readyState===WebSocket.OPEN}_stopHeartbeat(){this._clearTimer(this._pingTimer),this._clearTimer(this._pongTimer)}onDispose(){this._stopHeartbeat(),this.disconnect(),this._dispatcher&&(this._dispatcher.dispose(),this._dispatcher=null),i.info("Conversation transport disposed")}}const w=["sdk:connected","sdk:disconnected","sdk:error","media:video:available","media:video:unavailable","media:video:trackAdded","media:video:trackRemoved","media:audio:trackAdded","media:audio:trackRemoved","media:audio:captureStarted","media:audio:captureStopped","media: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"],$=/^[a-z]+:[a-zA-z]+(:[a-zA-Z]+)?$/;class lt{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)),w.forEach(t=>{this.inner.on(t,s=>this.handlePublicEvent(t,s))})}publicAPI(){return{on:this.on.bind(this),off:this.off.bind(this),once:this.once.bind(this)}}on(e,t){const s=String(e);return this.assertAllowed(s),this.listeners.has(s)||this.listeners.set(s,new Set),this.listeners.get(s).add(t),()=>this.off(e,t)}once(e,t){const s=String(e);this.assertAllowed(s);const n=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(w.indexOf(n)===-1)return;if(n==="media:video:trackAdded"){const c=t?.trackId||t?.id;if(typeof c=="string"){if(this.forwardedVideoTrackIds.has(c))return;this.forwardedVideoTrackIds.add(c)}}const o=s.sanitizer?s.sanitizer(t):t;if(!$.test(n))return;const a=this.listeners.get(n);!a||a.size===0||a.forEach(c=>{try{c(o)}catch(d){console.error("PublicEventEmitter listener error",d)}})}handlePublicEvent(e,t){if(w.indexOf(e)===-1)return;const s=Object.values(this.internalMap).find(a=>a.public===e),n=s&&s.sanitizer?s.sanitizer(t):t;if(e==="media:video:trackAdded"){const a=t?.trackId||t?.id;if(typeof a=="string"){if(this.forwardedVideoTrackIds.has(a))return;this.forwardedVideoTrackIds.add(a)}}const o=this.listeners.get(e);!o||o.size===0||o.forEach(a=>{try{a(n)}catch(c){console.error("PublicEventEmitter listener error",c)}})}handleConnected(e,t){e.source==="livekit"?this.livekitConnected=t:e.source==="ws"?this.wsConnected=t:e.source==="http"&&(this.httpConnected=t),this.maybeEmitSdkConnected(t?"connected":"disconnected")}maybeEmitSdkConnected(e){const t=e==="connected",s=t?!!this.livekitConnected:!this.livekitConnected,n=t?!!this.wsConnected:!this.wsConnected,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(w.indexOf(e)===-1)throw new Error(`Event "${e}" is not allowed for public consumers`);if(!$.test(e))throw new Error(`Event name "${e}" does not follow namespace:domain[:action] naming`)}}class ut extends l{constructor(e,t,s){super(),this._requestCounter=0,this._context=e,this._baseURL=t||"",this._defaultHeaders=s||{},i.info(`HTTP Service created with baseURL: ${this._baseURL||"none"}`)}setAuthToken(e){this._defaultHeaders.Authorization=`Bearer ${e}`}request(e){this.ensureNotDisposed();const t=e.requestId||`req_${Date.now()}_${++this._requestCounter}`,s=this._buildURL(e.url,e.params),n=this._mergeHeaders(e.headers),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((d,p)=>{const m=setTimeout(()=>c.abort(),a);fetch(s,o).then(async _=>{clearTimeout(m);const v={};_.headers.forEach((te,se)=>{v[se]=te});let E;const k=_.headers.get("content-type");k&&k.includes("application/json")?E=await _.json():E=await _.text();const ee={requestId:t,status:_.status,headers:v,data:E,config:e};i.debug(`HTTP request succeeded: ${e.method} ${s} [${t}]`),d(ee)}).catch(_=>{clearTimeout(m);const v=_&&_.name==="AbortError",E={requestId:t,status:_&&_.status||void 0,message:v?"Request aborted (timeout)":_&&_.message||"HTTP request failed",error:_,config:e};i.error(`HTTP request failed: ${e.method} ${s} [${t}]`,_),p(E)})})}get(e,t,s){return this.request({...s,url:e,method:"GET",params:t})}post(e,t,s){return this.request({...s,url:e,method:"POST",data:t})}put(e,t,s){return this.request({...s,url:e,method:"PUT",data:t})}delete(e,t){return this.request({...t,url:e,method:"DELETE"})}patch(e,t,s){return this.request({...s,url:e,method:"PATCH",data:t})}_buildURL(e,t){if(e.startsWith("http://")||e.startsWith("https://"))return this._appendParams(e,t);let s=this._baseURL;return s&&!s.endsWith("/")&&!e.startsWith("/")?s+="/":s&&s.endsWith("/")&&e.startsWith("/")&&(s=s.slice(0,-1)),s+=e,this._appendParams(s,t)}_appendParams(e,t){if(!t||Object.keys(t).length===0)return e;const s=new URLSearchParams;for(const[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 _t(r,e="https"){return r.startsWith(e)?r:`${e}://${r}`}class pt extends l{constructor(e,t){super(),this._context=e,this._httpService=new ut(e,t||de)}getAuthToken(){return this.ensureNotDisposed(),this._httpService?.request({url:"/auth/login",method:"GET",data:{}}).then(e=>{e.data.code===C&&this._httpService?.setAuthToken(e.data.data.token)}).catch(e=>{throw this._context.emitError(e,h.SDK_AUTH_TOKEN_FAILED)})}getLivekitConfig(){return this.ensureNotDisposed(),this._httpService?.request({url:"/stream/v1/start",method:"POST",data:{avatarName:this._context.options.avatarName,voice:this._context.options.avatarVoice||""}}).then(e=>{if(e.data.code===C){const t=e.data.data;return this._context.setLivekitConfig(_t(t.livekitUrl,"wss"),t.roomToken,t.roomId),this._context.setWsUrl(`${this._context.options.wsUrlPrefix||le}/${t.roomId}/${this._context.options.avatarName}`),this._context.setVideoOptions({renderMode:t.greenScreen.enabled?"processed":"raw",greenScreen:t.greenScreen||{}}),this._context.eventBus.emit("inner:sdk:connected",{source:"http"}),!0}else throw new u(e.data.msg,h.SDK_GET_LIVEKIT_CONFIG_FAILED)}).catch(e=>{throw this._context.eventBus.emit("inner:sdk:error",{error:new u(e.message,h.SDK_GET_LIVEKIT_CONFIG_FAILED)}),u.fromError(e,h.SDK_GET_LIVEKIT_CONFIG_FAILED)})}switchVideo({onceActions:e,loopActions:t}){return this.ensureNotDisposed(),this._httpService?.request({url:"/avatar/v1/switch-video",method:"POST",data:{roomId:this._context.roomId,onceActions:e,loopActions:t}}).then(s=>{if(s.data.code===C)return!0;throw this._context.emitError(s.data.message,h.SDK_SWITCH_VIDEO_FAILED)})}interruptConversation(){return this.ensureNotDisposed(),this._httpService?.request({url:"/stream/v1/interrupt",method:"POST",data:{roomId:this._context.roomId}}).then(e=>{if(e.data.code===C)return!0;throw this._context.emitError(e.data.message,h.SDK_INTERRUPT_CONVERSATION_FAILED)}).finally(()=>{this._context.eventBus.emit("inner:sdk:disconnected",{source:"http"})})}disconnectConversation(){return this.ensureNotDisposed(),this._httpService?.request({url:"/stream/v1/stop",method:"POST",data:{roomId:this._context.roomId}}).then(e=>e.data.code===C).finally(()=>{this._context.eventBus.emit("inner:sdk:disconnected",{source:"http"})})}onDispose(){this._httpService.dispose()}}class Z extends l{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 _e(e),i.info("SDK Client created")}get events(){return this.ensureNotDisposed(),this._publicEventAPI||(this._publicEmitter=new lt(this._context.eventBus),this._publicEventAPI=this._publicEmitter.publicAPI()),this._publicEventAPI}pushActions({onceActions:e,loopActions:t}){return this.ensureNotDisposed(),this._ensureConnected(),this._httpController?.switchVideo({onceActions:e,loopActions:t})}interruptConnection(){return this.ensureNotDisposed(),this._ensureConnected(),this._httpController?.interruptConversation()}sendTextQuestion(e){if(this.ensureNotDisposed(),this._ensureConnected(),this._conversationController)return this._conversationController.sendQuestion(e);throw new u("Conversation controller is not available",h.CONVERSATION_CONTROLLER_NOT_AVAILABLE)}setRenderFitMode(e){this.ensureNotDisposed(),this._ensureConnected(),this._videoController&&this._videoController.setRenderFitMode(e)}async startAudioCapture(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",h.AUDIO_CONTROLLER_NOT_AVAILABLE);await this._audioController.startCapture()}async stopAudioCapture(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",h.AUDIO_CONTROLLER_NOT_AVAILABLE);await this._audioController.stopCapture()}setVolume(e){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",h.AUDIO_CONTROLLER_NOT_AVAILABLE);this._audioController.setVolume(e)}getVolume(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",h.AUDIO_CONTROLLER_NOT_AVAILABLE);return this._audioController?.getVolume()||1}mute(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",h.AUDIO_CONTROLLER_NOT_AVAILABLE);this._audioController.mute()}unmute(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",h.AUDIO_CONTROLLER_NOT_AVAILABLE);this._audioController.unmute()}get isMuted(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",h.AUDIO_CONTROLLER_NOT_AVAILABLE);return this._audioController.isMuted()}get isAudioCapturing(){return this.ensureNotDisposed(),this._ensureConnected(),this._audioController?this._audioController.isCapturing:!1}async startCamera(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._cameraController)throw new u("Camera controller is not available",h.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 u("Camera controller is not available",h.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<ue?Promise.resolve(!0):(this._preConnectPromise=(async()=>{try{if(i.info("Creating HTTP controller..."),this._httpController=this._httpController??new pt(this._context,this._context.options.http?.baseURL),i.info("Getting auth token..."),await this._httpController.getAuthToken(),i.info("Getting livekit config..."),!!!await this._httpController?.getLivekitConfig())throw new Error("Config missing");return this._lastPreConnectSuccessAt=Date.now(),!0}catch(t){throw this._context.emitError(t,h.SDK_PRECONNECT_FAILED)}finally{this._preConnectPromise=null}})(),this._preConnectPromise)}async connect(){if(this.ensureNotDisposed(),this._connectionCoordinator&&this._context.sessionState.isConnected){i.warn("SDK already initialized");return}try{if(!await this.preConnect())throw new u("PreConnect failed without explicit error",h.SDK_CONNECT_FAILED)}catch(e){throw this._context.emitError(e,h.SDK_CONNECT_FAILED)}try{if(i.info("Initializing SDK..."),this._connectionCoordinator=new me(this._context),this._connectionCoordinator.start(),this._connectionCoordinator.startConnecting(),this._videoController=new Se(this._context,this._context.options.video),this._audioController=new Be(this._context,this._context.options.audio),this._cameraController=new et(this._context),this._conversationController=new rt(this._context),this._liveKitService=new ct(this._context),this._conversationTransport=new ht(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,h.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,h.SDK_DISCONNECT_FAILED)}}}async reconnect(){this.ensureNotDisposed();const e=this._connectionCoordinator;if(!e?.canStartReconnectingManual()){const s=e?e.getSnapshot():this.connectionSnapshot;return i.warn("SDK reconnect not allowed in current state, returning current snapshot"),s}const t=e.captureSnapshotAndStartReconnectingManual();try{if(this._audioController&&this._audioController.stopCapture(),t.rtc.connected&&!t.rtc.hasVideoTrack&&(await this._liveKitService?.disconnect(),await this._conversationTransport?.disconnect()),this._conversationTransport&&!this._conversationTransport.isConnected){if(!await this.preConnect())throw new u("PreConnect failed without explicit error",h.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,h.SDK_RECONNECT_FAILED)}}get connectionSnapshot(){return this.ensureNotDisposed(),this._connectionCoordinator?this._connectionCoordinator.getSnapshot():j(G("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 u("SDK is not connected. Call connect() first.",h.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 mt(r){if(!r.avatarName)throw new u("avatarName is required",h.NO_AVATARNAME);return i.info("Creating SDK client with options:",{avatarName:r.avatarName}),new Z(r)}class H extends l{constructor(e,t){super(),this._videoElement=null,this._videoUrl=null,this._isPlaying=!1,this._frameCallbackId=null,this._videoUrl=e,t?this._videoElement=t:(this._videoElement=document.createElement("video"),this._videoElement.style.opacity="0",this._videoElement.muted=!0,this._videoElement.playsInline=!0,this._videoElement.loop=!0,document.querySelector("#video-container")?.appendChild(this._videoElement)),i.info("Local Video Debug Source created")}get videoElement(){return this._videoElement}async load(){if(!this._videoElement)throw new Error("Video element or URL not set");return new Promise((e,t)=>{const s=this._videoElement,n=()=>{s.removeEventListener("loadedmetadata",n),s.removeEventListener("error",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 ft extends l{constructor(e,t,s){super(),this._outputCanvas=null,this._outputRenderer=null,this._source=new X;const n={...e,renderMode:"processed"};this._pipeline=new J(this._source,n),this._outputRenderer=new b(s,t),this._pipeline.setRenderer(this._outputRenderer),this._outputCanvas=this._outputRenderer.getCanvas(),i.info("Video Pipeline Runner created")}get outputCanvas(){return this._outputCanvas}get processor(){return this._pipeline.strategy.getProcessors()[0]||null}setProcessor(e){this._pipeline.setProcessor(e),i.info("Processor set on pipeline")}async processFrame(e){if(this.ensureNotDisposed(),!this._outputRenderer){i.error("Output renderer not available"),e.close();return}try{const t=this._pipeline.strategy.getProcessors()[0]||null;let s=null;if(t){try{s=t.process(e)}catch(n){i.error("Error processing frame:",n),s=e}if(s===null&&"getImageData"in t){const n=t.getImageData();if(n&&this._outputCanvas&&this._outputRenderer.getContext()){const 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 vt extends l{constructor(e,t,s,n){if(super(),this._isRunning=!1,this._videoSource=new H(e,t),this._options={renderMode:"processed",containerElement:t,...n},this._pipelineRunner=new ft(this._options,t,s),this._options.greenScreen?.enabled){const o=new N(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 H(e,t);try{await o.load(),n&&await o.play(),o.onFrame(d=>{this._pipelineRunner.processFrame(d).catch(p=>{i.error("Error processing frame:",p)})}),this._videoSource=o;try{s.offFrame(),s.stop(),s.dispose()}catch(d){console.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){i.error("Failed to switch video source:",a);try{o.dispose()}catch(c){console.warn("Error disposing new video source after failed switch:",c)}throw a}}get outputCanvas(){return this._pipelineRunner.outputCanvas}get videoSource(){return this._videoSource}get processor(){return this._pipelineRunner.processor}setProcessor(e){this._pipelineRunner.setProcessor(e)}updateGreenScreenOptions(e){const t=this._pipelineRunner.processor;if(t&&"updateOptions"in t&&typeof t.updateOptions=="function")t.updateOptions(e),i.info("Green screen options updated");else{const s=this._options.greenScreen||{enabled:!1},n={...s,...e,enabled:e.enabled!==void 0?e.enabled:s.enabled},o=new N(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")}}exports.DEFAULT_AUDIO_BUFFER_SIZE=ce;exports.DEFAULT_AUDIO_CHANNELS=ae;exports.DEFAULT_AUDIO_SAMPLE_RATE=y;exports.DEFAULT_RECONNECT_ATTEMPTS=A;exports.DEFAULT_RECONNECT_DELAY=I;exports.DEFAULT_WS_TIMEOUT=re;exports.Disposable=l;exports.EventBus=q;exports.Logger=i;exports.SDKClient=Z;exports.SDKError=u;exports.SessionState=z;exports.StandaloneVideoDebugger=vt;exports.createClient=mt;
|
package/dist/index.d.ts
CHANGED
|
@@ -137,6 +137,16 @@ interface AudioEvents {
|
|
|
137
137
|
'inner:audio:unmuted': void;
|
|
138
138
|
}
|
|
139
139
|
|
|
140
|
+
/**
|
|
141
|
+
* Camera-related events (local camera capture)
|
|
142
|
+
*
|
|
143
|
+
* @author sansen
|
|
144
|
+
*/
|
|
145
|
+
interface CameraEvents {
|
|
146
|
+
'inner:camera:started': void;
|
|
147
|
+
'inner:camera:stopped': void;
|
|
148
|
+
}
|
|
149
|
+
|
|
140
150
|
/**
|
|
141
151
|
* Conversation-related events
|
|
142
152
|
*
|
|
@@ -150,6 +160,7 @@ interface ConversationEvents {
|
|
|
150
160
|
'inner:conversation:answer:completed': { questionId: string; fullAnswer: string };
|
|
151
161
|
'inner:conversation:server:message': { questionId: string; message: string; type: string };
|
|
152
162
|
'inner:conversation:asr:received': { questionId: string; text: string };
|
|
163
|
+
'inner:conversation:server:command': { code: number; command: string };
|
|
153
164
|
}
|
|
154
165
|
|
|
155
166
|
/**
|
|
@@ -205,6 +216,9 @@ interface PublicSDKEvents {
|
|
|
205
216
|
'media:audio:captureStarted': undefined;
|
|
206
217
|
'media:audio:captureStopped': undefined;
|
|
207
218
|
|
|
219
|
+
'media:camera:started': undefined;
|
|
220
|
+
'media:camera:stopped': undefined;
|
|
221
|
+
|
|
208
222
|
'media:audio:volumeChanged': { volume: number };
|
|
209
223
|
'media:audio:muted': undefined;
|
|
210
224
|
'media:audio:unmuted': undefined;
|
|
@@ -223,7 +237,13 @@ interface PublicSDKEvents {
|
|
|
223
237
|
* @author sansen
|
|
224
238
|
* @typedef {SDKEvents}
|
|
225
239
|
*/
|
|
226
|
-
type SDKEvents = PublicSDKEvents &
|
|
240
|
+
type SDKEvents = PublicSDKEvents &
|
|
241
|
+
VideoEvents &
|
|
242
|
+
AudioEvents &
|
|
243
|
+
CameraEvents &
|
|
244
|
+
ConversationEvents &
|
|
245
|
+
ConnectionEvents &
|
|
246
|
+
InternalConnectionFactEvents;
|
|
227
247
|
|
|
228
248
|
/**
|
|
229
249
|
* Event listener function type
|
|
@@ -684,7 +704,7 @@ interface ClientOptions extends BaseOptions {
|
|
|
684
704
|
* @default ''
|
|
685
705
|
* @author sansen
|
|
686
706
|
*/
|
|
687
|
-
avatarVoice
|
|
707
|
+
avatarVoice?: string;
|
|
688
708
|
/**
|
|
689
709
|
* Audio configuration options
|
|
690
710
|
*
|
|
@@ -976,6 +996,11 @@ declare class SDKClient extends Disposable {
|
|
|
976
996
|
* @author sansen
|
|
977
997
|
*/
|
|
978
998
|
private _conversationController;
|
|
999
|
+
/**
|
|
1000
|
+
* Camera controller (local camera capture + screenshot send)
|
|
1001
|
+
* @author sansen
|
|
1002
|
+
*/
|
|
1003
|
+
private _cameraController;
|
|
979
1004
|
/**
|
|
980
1005
|
* LiveKit service
|
|
981
1006
|
* @author sansen
|
|
@@ -1094,6 +1119,36 @@ declare class SDKClient extends Disposable {
|
|
|
1094
1119
|
* @author sansen
|
|
1095
1120
|
*/
|
|
1096
1121
|
get isAudioCapturing(): boolean;
|
|
1122
|
+
/**
|
|
1123
|
+
* Start camera capture and periodic screenshot sending over WebSocket
|
|
1124
|
+
* @returns Promise<void>
|
|
1125
|
+
* @throws SDKError if not connected or camera controller unavailable
|
|
1126
|
+
* @author sansen
|
|
1127
|
+
*/
|
|
1128
|
+
startCamera(): Promise<void>;
|
|
1129
|
+
/**
|
|
1130
|
+
* Stop camera capture and screenshot sending
|
|
1131
|
+
* @author sansen
|
|
1132
|
+
*/
|
|
1133
|
+
stopCamera(): void;
|
|
1134
|
+
/**
|
|
1135
|
+
* Get the local camera MediaStream (null if camera not running)
|
|
1136
|
+
* @returns MediaStream | null
|
|
1137
|
+
* @author sansen
|
|
1138
|
+
*/
|
|
1139
|
+
getCameraStream(): MediaStream | null;
|
|
1140
|
+
/**
|
|
1141
|
+
* Get the local camera video track (null if camera not running)
|
|
1142
|
+
* @returns MediaStreamTrack | null
|
|
1143
|
+
* @author sansen
|
|
1144
|
+
*/
|
|
1145
|
+
getCameraTrack(): MediaStreamTrack | null;
|
|
1146
|
+
/**
|
|
1147
|
+
* Attach the local camera stream to a video element for preview
|
|
1148
|
+
* @param videoElement HTML video element to show the stream
|
|
1149
|
+
* @author sansen
|
|
1150
|
+
*/
|
|
1151
|
+
attachCameraTo(videoElement: HTMLVideoElement): void;
|
|
1097
1152
|
/**
|
|
1098
1153
|
* Pre-connect to remote services
|
|
1099
1154
|
* Performs pre-connection steps: fetches auth token and LiveKit configuration
|