ug-js-sdk 3.0.65 → 3.0.66

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.
@@ -6,7 +6,6 @@ import { IAvatar } from '../types';
6
6
  export declare class AvatarManager extends EventEmitter implements IAvatar {
7
7
  private animationLayers;
8
8
  private layerPriority;
9
- private thinkToggle;
10
9
  constructor();
11
10
  playIdle(): void;
12
11
  playListen(): void;
@@ -7,7 +7,6 @@ import { AvatarManagerEvents } from '../types';
7
7
  export class AvatarManager extends EventEmitter {
8
8
  animationLayers = new Map();
9
9
  layerPriority = ['mouth', 'facial', 'body', 'idle'];
10
- thinkToggle = false;
11
10
  constructor() {
12
11
  const logger = new DefaultLogger({ category: '🧒 AvatarManager', style: StyleOrange });
13
12
  super(logger);
@@ -22,10 +21,7 @@ export class AvatarManager extends EventEmitter {
22
21
  this.playAnimation('body_talk_to_user_loop', 0, true);
23
22
  }
24
23
  playThink() {
25
- // Alternate between two think animations for variety
26
- const thinkAnim = this.thinkToggle ? 'body_idle_think2' : 'body_idle_think';
27
- this.thinkToggle = !this.thinkToggle;
28
- this.playAnimation(thinkAnim, 0, true);
24
+ this.playAnimation('body_idle_think', 0, true);
29
25
  }
30
26
  playLaugh() {
31
27
  this.playAnimation('body_laugh', 0, true);
@@ -54,8 +50,6 @@ export class AvatarManager extends EventEmitter {
54
50
  this.logger.debug('Disposing AvatarManager...');
55
51
  // Clear all animation layers
56
52
  this.animationLayers.clear();
57
- // Reset state
58
- this.thinkToggle = false;
59
53
  // Remove all event listeners
60
54
  this.removeAllListeners();
61
55
  this.logger.debug('AvatarManager disposed');
@@ -1 +1 @@
1
- {"version":3,"file":"AvatarManager.js","sourceRoot":"","sources":["../../../../src/ug-core/playback-manager/avatar-manager/AvatarManager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAA;AACtD,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAC9D,OAAO,EAAW,mBAAmB,EAAE,MAAM,UAAU,CAAA;AAEvD;;GAEG;AACH,MAAM,OAAO,aAAc,SAAQ,YAAY;IACrC,eAAe,GAAwB,IAAI,GAAG,EAAE,CAAA;IAChD,aAAa,GAAG,CAAC,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,CAAC,CAAA;IACnD,WAAW,GAAY,KAAK,CAAA;IAEpC;QACE,MAAM,MAAM,GAAG,IAAI,aAAa,CAAC,EAAE,QAAQ,EAAE,kBAAkB,EAAE,KAAK,EAAE,WAAW,EAAE,CAAC,CAAA;QACtF,KAAK,CAAC,MAAM,CAAC,CAAA;IACf,CAAC;IAED,QAAQ;QACN,IAAI,CAAC,aAAa,CAAC,WAAW,EAAE,CAAC,EAAE,IAAI,CAAC,CAAA;IAC1C,CAAC;IAED,UAAU;QACR,IAAI,CAAC,aAAa,CAAC,kBAAkB,EAAE,CAAC,EAAE,IAAI,CAAC,CAAA;IACjD,CAAC;IAED,QAAQ;QACN,IAAI,CAAC,aAAa,CAAC,wBAAwB,EAAE,CAAC,EAAE,IAAI,CAAC,CAAA;IACvD,CAAC;IAED,SAAS;QACP,qDAAqD;QACrD,MAAM,SAAS,GAAG,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC,kBAAkB,CAAC,CAAC,CAAC,iBAAiB,CAAA;QAC3E,IAAI,CAAC,WAAW,GAAG,CAAC,IAAI,CAAC,WAAW,CAAA;QACpC,IAAI,CAAC,aAAa,CAAC,SAAS,EAAE,CAAC,EAAE,IAAI,CAAC,CAAA;IACxC,CAAC;IAED,SAAS;QACP,IAAI,CAAC,aAAa,CAAC,YAAY,EAAE,CAAC,EAAE,IAAI,CAAC,CAAA;IAC3C,CAAC;IAED,UAAU;QACR,IAAI,CAAC,aAAa,CAAC,aAAa,EAAE,CAAC,EAAE,KAAK,CAAC,CAAA;IAC7C,CAAC;IAED,UAAU,CAAC,UAAkB;QAC3B,IAAI,CAAC,aAAa,CAAC,UAAU,EAAE,CAAC,EAAE,KAAK,CAAC,CAAA,CAAC,gCAAgC;IAC3E,CAAC;IAED,KAAK,CAAC,aAAa,CAAC,IAAY,EAAE,KAAK,GAAG,CAAC,EAAE,IAAI,GAAG,IAAI;QACtD,IAAI,CAAC,eAAe,CAAC,GAAG,CAAC,KAAK,EAAE,IAAI,CAAC,CAAA;QACrC,MAAM,IAAI,CAAC,IAAI,CAAC,mBAAmB,CAAC,gBAAgB,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAA;QAC5E,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,sBAAsB,IAAI,aAAa,KAAK,WAAW,IAAI,GAAG,CAAC,CAAA;IACnF,CAAC;IAED,KAAK,CAAC,aAAa,CAAC,KAAK,GAAG,CAAC;QAC3B,IAAI,CAAC,eAAe,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;QAClC,MAAM,IAAI,CAAC,IAAI,CAAC,mBAAmB,CAAC,gBAAgB,EAAE,EAAE,KAAK,EAAE,CAAC,CAAA;IAClE,CAAC;IAED;;;OAGG;IACH,OAAO;QACL,IAAI,CAAC;YACH,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,4BAA4B,CAAC,CAAA;YAE/C,6BAA6B;YAC7B,IAAI,CAAC,eAAe,CAAC,KAAK,EAAE,CAAA;YAE5B,cAAc;YACd,IAAI,CAAC,WAAW,GAAG,KAAK,CAAA;YAExB,6BAA6B;YAC7B,IAAI,CAAC,kBAAkB,EAAE,CAAA;YAEzB,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,wBAAwB,CAAC,CAAA;QAC7C,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,qCAAqC,EAAE,KAAK,CAAC,CAAA;QAChE,CAAC;IACH,CAAC;CACF"}
1
+ {"version":3,"file":"AvatarManager.js","sourceRoot":"","sources":["../../../../src/ug-core/playback-manager/avatar-manager/AvatarManager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAA;AACtD,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAC9D,OAAO,EAAW,mBAAmB,EAAE,MAAM,UAAU,CAAA;AAEvD;;GAEG;AACH,MAAM,OAAO,aAAc,SAAQ,YAAY;IACrC,eAAe,GAAwB,IAAI,GAAG,EAAE,CAAA;IAChD,aAAa,GAAG,CAAC,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,CAAC,CAAA;IAG3D;QACE,MAAM,MAAM,GAAG,IAAI,aAAa,CAAC,EAAE,QAAQ,EAAE,kBAAkB,EAAE,KAAK,EAAE,WAAW,EAAE,CAAC,CAAA;QACtF,KAAK,CAAC,MAAM,CAAC,CAAA;IACf,CAAC;IAED,QAAQ;QACN,IAAI,CAAC,aAAa,CAAC,WAAW,EAAE,CAAC,EAAE,IAAI,CAAC,CAAA;IAC1C,CAAC;IAED,UAAU;QACR,IAAI,CAAC,aAAa,CAAC,kBAAkB,EAAE,CAAC,EAAE,IAAI,CAAC,CAAA;IACjD,CAAC;IAED,QAAQ;QACN,IAAI,CAAC,aAAa,CAAC,wBAAwB,EAAE,CAAC,EAAE,IAAI,CAAC,CAAA;IACvD,CAAC;IAED,SAAS;QACP,IAAI,CAAC,aAAa,CAAC,iBAAiB,EAAE,CAAC,EAAE,IAAI,CAAC,CAAA;IAChD,CAAC;IAED,SAAS;QACP,IAAI,CAAC,aAAa,CAAC,YAAY,EAAE,CAAC,EAAE,IAAI,CAAC,CAAA;IAC3C,CAAC;IAED,UAAU;QACR,IAAI,CAAC,aAAa,CAAC,aAAa,EAAE,CAAC,EAAE,KAAK,CAAC,CAAA;IAC7C,CAAC;IAED,UAAU,CAAC,UAAkB;QAC3B,IAAI,CAAC,aAAa,CAAC,UAAU,EAAE,CAAC,EAAE,KAAK,CAAC,CAAA,CAAC,gCAAgC;IAC3E,CAAC;IAED,KAAK,CAAC,aAAa,CAAC,IAAY,EAAE,KAAK,GAAG,CAAC,EAAE,IAAI,GAAG,IAAI;QACtD,IAAI,CAAC,eAAe,CAAC,GAAG,CAAC,KAAK,EAAE,IAAI,CAAC,CAAA;QACrC,MAAM,IAAI,CAAC,IAAI,CAAC,mBAAmB,CAAC,gBAAgB,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAA;QAC5E,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,sBAAsB,IAAI,aAAa,KAAK,WAAW,IAAI,GAAG,CAAC,CAAA;IACnF,CAAC;IAED,KAAK,CAAC,aAAa,CAAC,KAAK,GAAG,CAAC;QAC3B,IAAI,CAAC,eAAe,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;QAClC,MAAM,IAAI,CAAC,IAAI,CAAC,mBAAmB,CAAC,gBAAgB,EAAE,EAAE,KAAK,EAAE,CAAC,CAAA;IAClE,CAAC;IAED;;;OAGG;IACH,OAAO;QACL,IAAI,CAAC;YACH,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,4BAA4B,CAAC,CAAA;YAE/C,6BAA6B;YAC7B,IAAI,CAAC,eAAe,CAAC,KAAK,EAAE,CAAA;YAE5B,6BAA6B;YAC7B,IAAI,CAAC,kBAAkB,EAAE,CAAA;YAEzB,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,wBAAwB,CAAC,CAAA;QAC7C,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,qCAAqC,EAAE,KAAK,CAAC,CAAA;QAChE,CAAC;IACH,CAAC;CACF"}
@@ -181,4 +181,4 @@ g¹¯´™†ì±Ñp·¯Æúj!Ùê3û|©Ña°9èóPSà‡j¨ÎZ®%Wç–Ç&Õ¹
181
181
  CU¡ÍÌ]Âuf¸i^ÍN¸<ößiIÒzWš’HÈDŒkœòPë­Kã.í»+{J¸…V0¸“J¸“J¸Ã³“J#’ŠÄ8+KÉA¥NA–ú¬ Í)šôš¼¡%°|ù(‹JPs S±E¯j=M-˜2އ[6+Ïû¿²h$ɳ0˜= ê™L,§D1ÌJÈB„„rÚœ YÏxSžòRFxž¤ŒÚK+šÐÃ:„‚&öôê˜GºŸ´;̈ú‚Ïà7Кr´Í6I)ó÷~~àrOŠy­E \ÔfËøª¬ÓFܧìpQ¯{‘¨EO:.ÆÔEÊm‘¢Ä‘¹#{±
182
182
  r÷|‹ú–“
183
183
  Œ*ÔàñT'’ 8KpLjúwù@Q(HÎj{”¥¦Sٝ01TÀ–7F<˜â„<$êÐÉSLVa·ËE–k´&[[ʉׇzôEtF@Ë9KV©ÇEÙK¬)„GŽ4WېWv3ýn§ÁçÂxârjŠzš= €phˆx˜d„t”lŒ|\=}-M¥ìÉÜ\¾Ù%Á?vqš³¾Ãð6†æø Ç]\C"…3ú¹ï5ÞÞçpXç)z²^Rû÷üoõ‚{µ(âw]WT½Ïm§Cö›4&t]Ât[bìÓ®\%óêëÌÔfìW´@¨ÁÚ_ª÷n xV½é9]Mà˝àUß Í/»äµÝ<½äÙÝåáýõaY¿DÅìZÀìØüÛ£üÞÿÍ$Z-×8Ò\ Üb½RLhuñ®äâøþsBV_»ë»Um3
184
- ½ÇaztŸÅò¬¼Åâ`});var T={a:x};this.setModule=e=>{t.setModule(EmscriptenWASM,e)},this.getModule=()=>t.getModule(EmscriptenWASM),this.instantiate=()=>(this.getModule().then(t=>WebAssembly.instantiate(t,T)).then(t=>{const i=t.exports;var o;!function(t){g=t.m,m=t.n,b=t.p,y=t.q,_=t.r,w=t.s,v=t.t}(i),o=(r=i.k).buffer,new Int8Array(o),new Int16Array(o),e=new Uint8Array(o),new Uint16Array(o),new Int32Array(o),n=new Uint32Array(o),new Float32Array(o),new Float64Array(o),new BigInt64Array(o),new BigUint64Array(o),function(t){t.l()}(i),s()}),this.ready=new Promise(t=>{s=t}).then(()=>{this.HEAP=r.buffer,this.malloc=g,this.free=m,this.mpeg_decoder_feed=y,this.mpeg_decoder_read=_,this.mpeg_frame_decoder_create=b,this.mpeg_frame_decoder_destroy=w}),this)}function MPEGDecoder(t={}){return this._init=()=>(new this._WASMAudioDecoderCommon).instantiate(this._EmscriptenWASM,this._module).then(e=>{this._common=e,this._sampleRate=0,this._inputBytes=0,this._outputSamples=0,this._frameNumber=0,this._input=this._common.allocateTypedArray(this._inputSize,Uint8Array),this._output=this._common.allocateTypedArray(this._outputSize,Float32Array);const n=this._common.allocateTypedArray(1,Uint32Array);this._samplesDecodedPtr=this._common.allocateTypedArray(1,Uint32Array),this._sampleRatePtr=this._common.allocateTypedArray(1,Uint32Array),this._errorStringPtr=this._common.allocateTypedArray(1,Uint32Array);const r=this._common.wasm.mpeg_frame_decoder_create(n.ptr,!1===t.enableGapless?0:1);if(r)throw Error(this._getErrorMessage(r));this._decoder=n.buf[0]}),Object.defineProperty(this,"ready",{enumerable:!0,get:()=>this._ready}),this._getErrorMessage=t=>t+" "+this._common.codeToString(this._errorStringPtr.buf[0]),this.reset=()=>(this.free(),this._init()),this.free=()=>{this._common.wasm.mpeg_frame_decoder_destroy(this._decoder),this._common.wasm.free(this._decoder),this._common.free()},this.decode=t=>{let e=[],n=[],r=0;if(!(t instanceof Uint8Array))throw Error("Data to decode must be Uint8Array. Instead got "+typeof t);t:for(let i=0,o=0;i<t.length;i+=o){const s=t.subarray(i,this._input.len+i);o=s.length,this._inputBytes+=o,this._input.buf.set(s);let a=this._common.wasm.mpeg_decoder_feed(this._decoder,this._input.ptr,o);if(-10!==a)for(;;){this._samplesDecodedPtr.buf[0]=0,a=this._common.wasm.mpeg_decoder_read(this._decoder,this._output.ptr,this._output.len,this._samplesDecodedPtr.ptr,this._sampleRatePtr.ptr,this._errorStringPtr.ptr);const t=this._samplesDecodedPtr.buf[0];if(this._outputSamples+=t,t&&(r+=t,e.push([this._output.buf.slice(0,t),this._output.buf.slice(t,2*t)])),-11!=a){if(-10===a)continue t;if(a){const t=this._getErrorMessage(a);console.error("mpg123-decoder: "+t),this._common.addError(n,t,0,this._frameNumber,this._inputBytes,this._outputSamples)}}}}return this._WASMAudioDecoderCommon.getDecodedAudioMultiChannel(n,e,2,r,this._sampleRatePtr.buf[0])},this.decodeFrame=t=>{const e=this.decode(t);return this._frameNumber++,e},this.decodeFrames=t=>{let e=[],n=[],r=0,i=0;for(;i<t.length;){const o=this.decodeFrame(t[i++]);e.push(o.channelData),n=n.concat(o.errors),r+=o.samplesDecoded}return this._WASMAudioDecoderCommon.getDecodedAudioMultiChannel(n,e,2,r,this._sampleRatePtr.buf[0])},this._isWebWorker=MPEGDecoder.isWebWorker,this._WASMAudioDecoderCommon=MPEGDecoder.WASMAudioDecoderCommon||WASMAudioDecoderCommon,this._EmscriptenWASM=MPEGDecoder.EmscriptenWASM||EmscriptenWASM,this._module=MPEGDecoder.module,this._inputSize=65536,this._outputSize=92448,this._ready=this._init(),this}class AudioPlayer extends EventEmitter{MIN_CHUNKS_TO_PROCESS=2;FLUSH_DELAY_MS=450;audioContext;currentSource=null;scheduledSources=[];playbackQueue=[];mp3Decoder=null;scheduledPlayTime=0;playbackStartTime=0;chunkQueue=[];isProcessingQueue=!1;frameExtractor=new MP3FrameExtractor;totalFramesDecoded=0;totalFramesFailed=0;totalFramesExtracted=0;isPlayingState=!1;isPausedState=!1;stopped=!1;frameBuffer=[];chunkAccumulator=[];flushTimeout=null;isAudioComplete=!1;timing;isScheduling=!1;allAudioPlayed=!1;constructor(){super(new DefaultLogger({category:"🎵 AudioPlayer",style:StyleGreen})),this.audioContext=new AudioContext,this.timing=new AudioPlayerTiming(()=>this.getCurrentTime(),()=>this.getQueueLength(),()=>this.getRemainingAudioSeconds(),1e3),this.setupTimingListeners()}setupTimingListeners(){this.timing.on("about-to-complete",async()=>{await this.emit(AudioPlayerEvents.AboutToComplete)})}async initialize(){this.audioContext||(this.audioContext=new AudioContext,this.logger.debug("AudioContext created by user gesture")),"suspended"===this.audioContext.state&&(this.audioContext.resume(),this.logger.debug("AudioContext resumed by user gesture")),this.mp3Decoder||(this.logger.debug("Initializing MP3 decoder..."),this.mp3Decoder=new MPEGDecoder,await this.mp3Decoder.ready,this.logger.debug("MP3 decoder ready")),this.resetState()}resetState(){for(const t of this.scheduledSources){t.onended=null;try{t.stop()}catch(t){}t.disconnect()}this.scheduledSources=[],this.currentSource=null,this.playbackQueue=[],this.scheduledPlayTime=0,this.playbackStartTime=0,this.chunkQueue=[],this.isProcessingQueue=!1,this.frameExtractor=new MP3FrameExtractor,this.isPlayingState=!1,this.isPausedState=!1,this.stopped=!1,this.frameBuffer=[],this.chunkAccumulator=[],this.isAudioComplete=!1,this.totalFramesDecoded=0,this.totalFramesFailed=0,this.totalFramesExtracted=0,this.flushTimeout&&(clearTimeout(this.flushTimeout),this.flushTimeout=null),this.timing.reset(),this.allAudioPlayed=!1}reset(){this.resetState()}enqueue(t){this.logger.debug("Enqueue: received chunk"),this.allAudioPlayed=!1,this.chunkQueue.push({base64String:t}),this.processChunkQueue(),this.isAudioComplete||(this.flushTimeout&&clearTimeout(this.flushTimeout),this.flushTimeout=setTimeout(()=>{this.logger.debug("auto-flush timer fired"),this.flush()},this.FLUSH_DELAY_MS))}markComplete(){this.logger.debug("markComplete: no more audio will be added"),this.isAudioComplete=!0,this.flushTimeout&&(clearTimeout(this.flushTimeout),this.flushTimeout=null),this.timing.onAudioComplete(),this.flush();const t=this.frameExtractor.extractRemainingFrames();t.length>0&&(this.logger.debug("markComplete: Found additional frames after flush",{count:t.length}),this.totalFramesExtracted+=t.length,this.decodeAudioFrames(t).then(e=>{if(e){this.totalFramesDecoded+=t.length;const n=0===this.playbackQueue.length;this.playbackQueue.push(e),this.emit(AudioPlayerEvents.Enqueued,{duration:e.duration}),this.timing.onAudioEnqueued(),n&&!this.isPlayingState?this.emit(AudioPlayerEvents.Ready):this.isPlayingState&&this.schedulePlayback()}}).catch(e=>{this.totalFramesFailed+=t.length,this.logger.error("Failed to decode final remaining frames after markComplete",e)}));const e=this.frameExtractor.getRemainingBufferSize();e>0&&this.logger.warn("markComplete: Extractor buffer still has data after final extraction",{remainingBytes:e}),this.allAudioPlayed&&(this.logger.debug("markComplete: All audio was already played, emitting Finished."),this.isPlayingState=!1,this.emit(AudioPlayerEvents.Finished))}async processChunkQueue(){if(!this.isProcessingQueue){this.isProcessingQueue=!0;try{for(;this.chunkQueue.length>0;){const{base64String:t}=this.chunkQueue.shift();if(this.chunkAccumulator.push(t),this.chunkAccumulator.length>=this.MIN_CHUNKS_TO_PROCESS){const t=this.chunkAccumulator;this.chunkAccumulator=[],await this.decodeAndEnqueueChunks(t)}}}finally{this.isProcessingQueue=!1}}}decodeBase64ToUint8Array(t){const e="undefined"!=typeof atob?atob(t):Buffer.from(t,"base64").toString("binary"),n=new Uint8Array(e.length);for(let t=0;t<e.length;t++)n[t]=e.charCodeAt(t);return n}combineArrays(t,e){const n=new Uint8Array(t.length+e.length);return n.set(t),n.set(e,t.length),n}async decodeAudioFrames(t){if(0===t.length)return null;if(!this.mp3Decoder)throw new Error("MP3 decoder not initialized. Call initialize() first.");const e=[];let n=0,r=0,i=0,o=0;for(let s=0;s<t.length;s++){const a=t[s];if(a.length<2||255!==a[0]||224&~a[1])this.logger.warn(`Frame ${s} doesn't start with valid sync word, skipping`,{firstBytes:Array.from(a.slice(0,4)),frameLength:a.length}),o++;else try{const t=this.mp3Decoder.decodeFrame(a);if(t.errors&&t.errors.length>0&&this.logger.warn(`Frame ${s} decoded with errors`,{errors:t.errors.map(t=>t.message||String(t)),frameLength:a.length}),0===t.samplesDecoded){this.logger.warn(`Frame ${s} decoded but produced 0 samples`,{frameLength:a.length,errors:t.errors?.map(t=>t.message||String(t))}),o++;continue}if(0===n?n=t.sampleRate:t.sampleRate!==n&&this.logger.warn(`Frame ${s} has different sample rate: ${t.sampleRate} vs ${n}`),0===e.length)for(let n=0;n<t.channelData.length;n++)e.push([]);for(let n=0;n<t.channelData.length;n++)n<e.length&&e[n].push(t.channelData[n]);r+=t.samplesDecoded,i++}catch(t){this.logger.error(`Failed to decode frame ${s}`,{error:t instanceof Error?t.message:String(t),frameLength:a.length,firstBytes:Array.from(a.slice(0,4))}),o++}}if(0===i)return this.logger.error("All frames failed to decode",{totalFrames:t.length}),null;o>0&&this.logger.warn("Some frames failed to decode",{successful:i,failed:o,total:t.length});const s=[];for(let t=0;t<e.length;t++){const n=new Float32Array(r);let i=0;for(const r of e[t])n.set(r,i),i+=r.length;s.push(n)}const a=this.audioContext.createBuffer(s.length,r,n);for(let t=0;t<s.length;t++)a.getChannelData(t).set(s[t]);return this.logger.debug("Decoded frames",{successful:i,failed:o,totalSamples:r,sampleRate:n,duration:a.duration.toFixed(3)}),a}async play(){this.isPlayingState||0===this.playbackQueue.length?this.logger.debug("Play called but already playing or queue is empty."):(this.isPlayingState=!0,this.stopped=!1,this.isPausedState=!1,this.isPausedState?this.scheduledPlayTime=this.audioContext.currentTime:(this.scheduledPlayTime=this.audioContext.currentTime,this.playbackStartTime=this.audioContext.currentTime),this.logger.debug(`Playback starting. Start time: ${this.playbackStartTime}`),this.schedulePlayback(),await this.emit(AudioPlayerEvents.Playing))}schedulePlayback(){if(this.isScheduling||0===this.playbackQueue.length||this.stopped)return;this.isScheduling=!0,this.currentSource&&(this.currentSource.onended=null);let t=null;for(;this.playbackQueue.length>0;){const e=this.playbackQueue.shift(),n=this.audioContext.createBufferSource();n.buffer=e,n.connect(this.audioContext.destination),this.scheduledPlayTime<this.audioContext.currentTime&&(this.logger.debug(`Adjusting scheduledPlayTime from ${this.scheduledPlayTime} to current time ${this.audioContext.currentTime}`),this.scheduledPlayTime=this.audioContext.currentTime),this.logger.debug(`Scheduling buffer of duration ${e.duration}s to start at ${this.scheduledPlayTime} (context time: ${this.audioContext.currentTime})`),n.start(this.scheduledPlayTime),this.scheduledPlayTime+=e.duration,this.scheduledSources.push(n),t=n}t&&(this.currentSource=t,t.onended=()=>{this.logger.debug(`Buffer finished playing. Context time: ${this.audioContext.currentTime}, isPaused: ${this.isPausedState}, stopped: ${this.stopped}`),this.scheduledSources=this.scheduledSources.filter(t=>t===this.currentSource),this.stopped||this.isPausedState||(this.playbackQueue.length>0?(this.logger.debug("More buffers in queue, scheduling next batch."),this.schedulePlayback()):this.isAudioComplete?(this.logger.debug("Last scheduled audio buffer finished and audio is complete."),this.isPlayingState=!1,this.emit(AudioPlayerEvents.Finished)):(this.logger.debug("All scheduled audio has been played, but not marked as complete yet."),this.allAudioPlayed=!0))}),this.isScheduling=!1}async pause(){this.currentSource&&this.isPlayingState&&(this.isPausedState=!0,this.isPlayingState=!1,this.audioContext.suspend(),await this.emit(AudioPlayerEvents.Paused))}async resume(){this.isPausedState&&(this.logger.debug("Resume called, resuming playback."),this.isPausedState=!1,this.isPlayingState=!0,this.audioContext.resume(),this.schedulePlayback(),await this.emit(AudioPlayerEvents.Playing))}async stop(){this.stopped=!0,this.resetState(),await this.emit(AudioPlayerEvents.Stopped)}getCurrentTime(){return this.isPlayingState?1e3*(this.audioContext.currentTime-this.playbackStartTime):0}getQueueLength(){return this.playbackQueue.length}getRemainingAudioSeconds(){const t=this.playbackQueue.reduce((t,e)=>t+e.duration,0);let e=0;return(this.isPlayingState||this.isPausedState)&&this.scheduledPlayTime>0&&(e=Math.max(0,this.scheduledPlayTime-this.audioContext.currentTime)),t+e}async decodeAndEnqueueChunks(t){if(0===t.length)return void this.logger.debug("decodeAndEnqueueChunks: no chunks to decode");let e=[];for(const n of t){const t=this.decodeBase64ToUint8Array(n),{frames:r,remainingData:i}=this.frameExtractor.feedAndExtractCompleteFrames(t);e=e.concat(r),this.totalFramesExtracted+=r.length,i.length>0&&this.logger.debug("Chunk ended with incomplete frame",{remainingBytes:i.length,framesExtracted:r.length})}if(this.logger.debug("decodeAndEnqueueChunks: total frames to decode",e.length),e.length>0){const t=e.map(t=>t.length).reduce((t,e)=>(t[e]=(t[e]||0)+1,t),{});this.logger.debug("Frame sizes distribution",t);try{const t=await this.decodeAudioFrames(e);if(t){this.totalFramesDecoded+=e.length;const n=0===this.playbackQueue.length;this.playbackQueue.push(t),await this.emit(AudioPlayerEvents.Enqueued,{duration:t.duration}),this.logger.debug("decodeAndEnqueueChunks: enqueued audioBuffer, duration",t.duration,"frames decoded",e.length),this.timing.onAudioEnqueued(),n&&!this.isPlayingState?await this.emit(AudioPlayerEvents.Ready):this.isPlayingState&&this.schedulePlayback()}}catch(t){this.totalFramesFailed+=e.length,this.logger.error("Failed to decode audio frames",t),this.logger.error("Lost frames",{count:e.length,sizes:e.map(t=>t.length)})}}else this.logger.debug("decodeAndEnqueueChunks: no frames to decode")}async flush(){this.logger.debug("Flushing",{chunks:this.chunkAccumulator.length});const t=this.chunkAccumulator;this.chunkAccumulator=[],await this.decodeAndEnqueueChunks(t);const e=this.frameExtractor.extractRemainingFrames();if(e.length>0){this.logger.debug("Flush: Found complete frames in extractor buffer",{count:e.length}),this.totalFramesExtracted+=e.length;try{const t=await this.decodeAudioFrames(e);if(t){this.totalFramesDecoded+=e.length;const n=0===this.playbackQueue.length;this.playbackQueue.push(t),await this.emit(AudioPlayerEvents.Enqueued,{duration:t.duration}),this.timing.onAudioEnqueued(),n&&!this.isPlayingState?await this.emit(AudioPlayerEvents.Ready):this.isPlayingState&&this.schedulePlayback()}}catch(t){this.totalFramesFailed+=e.length,this.logger.error("Failed to decode remaining frames after flush",t)}}}resetAboutToComplete(){this.timing.reset()}getFrameStats(){return{totalFramesExtracted:this.totalFramesExtracted,totalFramesDecoded:this.totalFramesDecoded,totalFramesFailed:this.totalFramesFailed,extractorRemainingBufferSize:this.frameExtractor.getRemainingBufferSize()}}dispose(){this.logger.debug("Disposing AudioPlayer..."),this.stopped=!0,this.isPlayingState=!1,this.isPausedState=!1;for(const t of this.scheduledSources){t.onended=null;try{t.stop()}catch(t){}t.disconnect()}this.scheduledSources=[],this.currentSource=null,this.flushTimeout&&(clearTimeout(this.flushTimeout),this.flushTimeout=null),this.timing&&this.timing.reset(),this.audioContext&&"closed"!==this.audioContext.state&&this.audioContext.close().catch(t=>{this.logger.debug("Error closing AudioContext:",t)}),this.mp3Decoder=null,this.resetState(),this.removeAllListeners(),this.logger.debug("AudioPlayer disposed")}destroy(){this.dispose()}}const SubtitleManagerEvents={WordChange:"wordChange",Playing:"playing",Paused:"paused",SubtitleFinished:"subtitleFinished",SubtitleChange:"subtitleChange"};class SubtitleManager extends EventEmitter{getCurrentTime;subtitleQueue=[];rafId=null;isPlaying=!1;cumulativeOffset=0;tickDebugLast=0;state={currentLine:null,nextLine:null,globalWordIndex:-1};constructor(t){super(new DefaultLogger({category:"🎬 SubtitleManager",style:StylePink})),this.getCurrentTime=t,this._resetState()}enqueue(t){this.logger.debug("Enqueue subtitle",{text:t.characters.join(""),queueLength:this.subtitleQueue.length});const{wordBoundaries:e,wordStartTimesMs:n}=this._computeWordBoundariesAndStartTimes(t.characters,t.start_times_ms),r={...t,wordBoundaries:e,wordStartTimesMs:n};this.subtitleQueue.push(r),this.isPlaying||1!==this.subtitleQueue.length||(this.logger.debug("Reset globalWordIndex to -1 (initial enqueue)"),this.state.globalWordIndex=-1),this.isPlaying&&!this.state.nextLine&&this.subtitleQueue.length>1&&(this._prepareState(),this._emitSubtitleChange())}async play(){this.isPlaying?this.logger.debug("Play called but already playing"):(this.logger.debug("Play called, starting playback"),this.isPlaying=!0,await this.emit(SubtitleManagerEvents.Playing),this.subtitleQueue.length>0&&(this._prepareState(),await this._emitSubtitleChange()),this.rafId=requestAnimationFrame(this._tick))}async pause(){this.logger.debug("Pause called"),this.isPlaying=!1,this.rafId&&(cancelAnimationFrame(this.rafId),this.rafId=null),await this.emit(SubtitleManagerEvents.Paused)}clearQueue(){this.logger.debug("Clear queue"),this._resetState(),this.pause()}reset(){this._resetState()}_resetState(){this.subtitleQueue=[],this.cumulativeOffset=0,this.rafId=null,this.isPlaying=!1,this.tickDebugLast=0,this.state={currentLine:null,nextLine:null,globalWordIndex:-1}}_subtitleDuration(t){const{start_times_ms:e,durations_ms:n}=t;if(!e.length||!n.length)return 0;const r=e.length-1;return e[r]+n[r]}_applyOffset(t,e){return t.map(t=>t+e)}_prepareState(){if(!this.subtitleQueue.length)return;const t=this.subtitleQueue[0];t.adjustedWordStartTimesMs=this._applyOffset(t.wordStartTimesMs,this.cumulativeOffset);const e=this.subtitleQueue[1]||null;if(e){const n=this.cumulativeOffset+this._subtitleDuration(t);e.adjustedWordStartTimesMs=this._applyOffset(e.wordStartTimesMs,n)}this.state.currentLine=t,this.state.nextLine=e,this.logger.debug("Prepared state",{cumulativeOffset:this.cumulativeOffset,hasCurrentLine:!!t,hasNextLine:!!e,firstFewAdjusted:t.adjustedWordStartTimesMs?.slice(0,3)})}_computeWordBoundariesAndStartTimes(t,e){const n=[],r=[];let i=null;for(let o=0;o<t.length;o++){const s=t[o];if(" "!==s&&null===i&&(i=o),(" "===s||o===t.length-1)&&null!==i){const t=" "===s?o-1:o;n.push({start:i,end:t}),r.push(e[i]),i=null}}return{wordBoundaries:n,wordStartTimesMs:r}}_calculateGlobalWordIndex(t){let e=-1;const{currentLine:n,nextLine:r}=this.state;if(n?.adjustedWordStartTimesMs)for(let r=0;r<n.adjustedWordStartTimesMs.length&&t>=n.adjustedWordStartTimesMs[r];r++)e=r;if(r?.adjustedWordStartTimesMs){const i=n?.wordBoundaries.length||0;for(let n=0;n<r.adjustedWordStartTimesMs.length&&t>=r.adjustedWordStartTimesMs[n];n++)e=i+n}return e}_decodeGlobalIndex(t){if(t<0)return{lineIndex:0,wordIndex:-1};const e=this.state.currentLine?.wordBoundaries.length||0;return t<e?{lineIndex:0,wordIndex:t}:{lineIndex:1,wordIndex:t-e}}_isFinished(t,e,n){return e>n+this._subtitleDuration(t)}_buildWordChangeEvent(t){const{lineIndex:e,wordIndex:n}=this._decodeGlobalIndex(t),{currentLine:r,nextLine:i}=this.state,o=t=>t?{characters:t.characters,wordBoundaries:t.wordBoundaries}:null;let s;if(0===e&&r&&n>=0){const t=r.wordBoundaries[n];s=r.characters.slice(t.start,t.end+1).join("")}else if(1===e&&i&&n>=0){const t=i.wordBoundaries[n];s=i.characters.slice(t.start,t.end+1).join("")}return{globalWordIndex:t,currentLineIndex:e,wordIndexInLine:n,word:s,currentLineData:o(r),nextLineData:o(i)}}_buildSubtitleChangeEvent(){const t=t=>t?.adjustedWordStartTimesMs?{characters:t.characters,wordBoundaries:t.wordBoundaries,adjustedWordStartTimesMs:t.adjustedWordStartTimesMs}:null;return{currentLine:t(this.state.currentLine),nextLine:t(this.state.nextLine)}}async _emitWordChange(t){const e=this._buildWordChangeEvent(t);this.logger.debug("Word change",{globalWordIndex:t,lineIndex:e.currentLineIndex,wordIndex:e.wordIndexInLine,word:e.word,currentLine:e.currentLineData?.characters.join(""),nextLine:e.nextLineData?.characters.join("")}),await this.emit(SubtitleManagerEvents.WordChange,e)}async _emitSubtitleChange(){const t=this._buildSubtitleChangeEvent();this.logger.debug("Subtitle change",{hasCurrentLine:!!t.currentLine,hasNextLine:!!t.nextLine}),await this.emit(SubtitleManagerEvents.SubtitleChange,t)}_tick=async()=>{if(!this.isPlaying||0===this.subtitleQueue.length)return;!this.state.nextLine&&this.subtitleQueue.length>1&&(this._prepareState(),await this._emitSubtitleChange());const t=this.getCurrentTime();this.state.currentLine||this._prepareState(),t-this.tickDebugLast>=3e3&&(this.logger.debug("Tick debug",{currentTimeMs:t,cumulativeOffset:this.cumulativeOffset,hasCurrentLine:!!this.state.currentLine,hasNextLine:!!this.state.nextLine}),this.tickDebugLast=t);const e=this._calculateGlobalWordIndex(t);e!==this.state.globalWordIndex&&(this.state.globalWordIndex=e,await this._emitWordChange(e));const n=this.subtitleQueue[0],r=this.subtitleQueue[1]||null;let i=0;if(r){const e=this.cumulativeOffset+this._subtitleDuration(n);this._isFinished(r,t,e)&&(i=2)}else n&&this._isFinished(n,t,this.cumulativeOffset)&&(i=1);if(i>0){this.logger.debug(`Subtitle batch finished. Advancing ${i} lines.`,{currentTimeMs:t,cumulativeOffset:this.cumulativeOffset});const e=this.subtitleQueue.splice(0,i);for(const t of e)this.cumulativeOffset+=this._subtitleDuration(t),await this.emit(SubtitleManagerEvents.SubtitleFinished,t);if(!(this.subtitleQueue.length>0))return this.logger.debug("Queue empty, pausing"),this.state.globalWordIndex=-1,await this._emitWordChange(-1),void await this.pause();this._prepareState(),await this._emitSubtitleChange()}this.rafId=requestAnimationFrame(this._tick)}}class VisemeScheduler extends EventEmitter{scheduledEvents=[];masterClock;constructor(t){super(new DefaultLogger({category:"👄 VisemeScheduler",style:StyleRed})),this.masterClock=t}enqueue(t){this.scheduledEvents.push(...t),this.scheduleEvents()}async play(){await this.emit(VisemeSchedulerEvents.Playing)}async pause(){await this.emit(VisemeSchedulerEvents.Paused)}scheduleEvents(){this.masterClock.currentTime,this.scheduledEvents.forEach(t=>{setTimeout(async()=>{await this.emit(VisemeSchedulerEvents.Viseme,t)})})}}class AvatarManager extends EventEmitter{animationLayers=new Map;layerPriority=["mouth","facial","body","idle"];thinkToggle=!1;constructor(){super(new DefaultLogger({category:"🧒 AvatarManager",style:StyleOrange}))}playIdle(){this.playAnimation("body_idle",0,!0)}playListen(){this.playAnimation("body_idle_listen",0,!0)}playTalk(){this.playAnimation("body_talk_to_user_loop",0,!0)}playThink(){const t=this.thinkToggle?"body_idle_think2":"body_idle_think";this.thinkToggle=!this.thinkToggle,this.playAnimation(t,0,!0)}playLaugh(){this.playAnimation("body_laugh",0,!0)}playWaving(){this.playAnimation("body_waving",0,!1)}playViseme(t){this.playAnimation(t,1,!1)}async playAnimation(t,e=0,n=!0){this.animationLayers.set(e,t),await this.emit(AvatarManagerEvents.AnimationChanged,{name:t,layer:e,loop:n}),this.logger.debug(`Playing animation: ${t} on layer ${e} (loop: ${n})`)}async stopAnimation(t=0){this.animationLayers.delete(t),await this.emit(AvatarManagerEvents.AnimationStopped,{layer:t})}dispose(){try{this.logger.debug("Disposing AvatarManager..."),this.animationLayers.clear(),this.thinkToggle=!1,this.removeAllListeners(),this.logger.debug("AvatarManager disposed")}catch(t){this.logger.warn("Error while disposing AvatarManager",t)}}}const PlaybackManagerEvents={Playing:"playing",Paused:"paused",PlaybackError:"playbackError",SubtitleWordChange:"subtitleWordChange",SubtitleChange:"subtitleChange",ImageChange:"imageChange",AboutToComplete:"aboutToComplete",Finished:"finished",AvatarAnimationChanged:"avatarAnimationChanged"};class PlaybackManager extends EventEmitter{capabilities;audioPlayer;visemeScheduler;subtitleManager;isPaused=!0;hasStarted=!1;avatarManager;constructor(t){super(new DefaultLogger({category:"🎪 PlaybackManager",style:StyleOrange})),this.capabilities=t,this.audioPlayer=new AudioPlayer,this.visemeScheduler=new VisemeScheduler(new AudioContext),this.subtitleManager=new SubtitleManager(()=>this.audioPlayer.getCurrentTime()),this.avatarManager=new AvatarManager,this.avatarManager.on("animation-changed",async t=>{await this.emit(PlaybackManagerEvents.AvatarAnimationChanged,t)}),this.setupChildListeners(),this.resetState()}setupChildListeners(){this.audioPlayer.on(AudioPlayerEvents.Ready,async()=>{this.isPaused&&!this.hasStarted?(this.logger.debug("Ready event received, starting playback"),await this.play()):this.logger.debug("Ready event received but already playing or paused, ignoring")}),this.audioPlayer.on(AudioPlayerEvents.AboutToComplete,async()=>{this.logger.debug("Audio about to complete"),await this.emit(PlaybackManagerEvents.AboutToComplete)}),this.audioPlayer.on(AudioPlayerEvents.Error,async t=>{await this.emit(PlaybackManagerEvents.PlaybackError,{source:"audio",error:t})}),this.audioPlayer.on(AudioPlayerEvents.Finished,async()=>{await this.emit(PlaybackManagerEvents.Finished),this.reset()}),this.subtitleManager.on(SubtitleManagerEvents.WordChange,async t=>{await this.emit(PlaybackManagerEvents.SubtitleWordChange,t)}),this.subtitleManager.on(SubtitleManagerEvents.SubtitleChange,async t=>{await this.emit(PlaybackManagerEvents.SubtitleChange,t)})}resetState(){this.isPaused=!0,this.hasStarted=!1}async initialize(){this.logger.debug("Initialized",this.capabilities),this.isPaused=!0,this.hasStarted=!1,await this.audioPlayer.initialize()}handleMessage(t){"audio"===t.event||"subtitles"===t.event||"viseme"===t.event?this.enqueue(t):"audio_complete"===t.event?this.markAudioComplete():"image"===t.event&&this.emit(PlaybackManagerEvents.ImageChange,t)}enqueue(t){if(this.capabilities.audio&&"audio"===t.event){const e=t;if(this.audioPlayer.enqueue(e.audio),this.capabilities.subtitles&&e.subtitles?.length){const t=this.convertSubtitleUnitsToMessage(e.subtitles);this.subtitleManager.enqueue(t)}}if(this.capabilities.subtitles&&"subtitles"===t.event){const e=t;this.subtitleManager.enqueue(e)}}convertSubtitleUnitsToMessage(t){return{type:"subtitles",characters:t.map(t=>t.text),start_times_ms:t.map(t=>Math.round(1e3*t.start_time_sec)),durations_ms:t.map(t=>Math.round(1e3*t.duration_sec))}}markAudioComplete(){this.logger.debug("Marking audio as complete"),this.capabilities.audio&&this.audioPlayer.markComplete()}async play(){this.isPaused=!1,this.hasStarted=!0,this.avatarManager.playTalk(),await Promise.all([this.capabilities.viseme?this.visemeScheduler.play():Promise.resolve(),this.capabilities.subtitles?this.subtitleManager.play():Promise.resolve(),this.capabilities.audio?this.audioPlayer.play():Promise.resolve()]),await this.emit(PlaybackManagerEvents.Playing)}async pause(){this.isPaused=!0,this.audioPlayer.pause(),this.visemeScheduler.pause(),this.subtitleManager.pause(),this.avatarManager.playIdle(),await this.emit(PlaybackManagerEvents.Paused)}async resume(){this.isPaused&&(this.isPaused=!1,this.avatarManager.playTalk(),await Promise.all([this.capabilities.viseme?this.visemeScheduler.play():Promise.resolve(),this.capabilities.subtitles?this.subtitleManager.play():Promise.resolve(),this.capabilities.audio?this.audioPlayer.resume():Promise.resolve()]),await this.emit(PlaybackManagerEvents.Playing))}async resumeAudioContext(){"function"==typeof this.audioPlayer.resumeAudioContext&&await this.audioPlayer.resumeAudioContext()}getRemainingAudioSeconds(){return this.audioPlayer?.getRemainingAudioSeconds()??0}reset(){this.resetState(),this.audioPlayer?.reset(),this.subtitleManager?.reset(),this.avatarManager.playIdle()}resetAboutToComplete(){this.audioPlayer?.resetAboutToComplete()}dispose(){this.logger.debug("Disposing PlaybackManager...");try{this.isPaused=!0,this.hasStarted=!1,this.audioPlayer?.pause(),this.visemeScheduler?.pause(),this.subtitleManager?.pause(),this.avatarManager?.playIdle()}catch(t){this.logger.warn("Error pausing components during dispose",t)}try{this.audioPlayer&&"function"==typeof this.audioPlayer.dispose&&this.audioPlayer.dispose()}catch(t){this.logger.warn("Error disposing audioPlayer",t)}try{this.avatarManager&&"function"==typeof this.avatarManager.dispose&&this.avatarManager.dispose()}catch(t){this.logger.warn("Error disposing avatarManager",t)}try{this.subtitleManager?.reset(),this.removeAllListeners()}catch(t){this.logger.warn("Error resetting subtitleManager or removing listeners",t)}this.logger.debug("PlaybackManager disposed")}wireVADToAvatar(t){t.on(ConversationManagerEvents.StateChange,t=>{"userSpeaking"===t.newState?this.avatarManager.playListen():"listening"===t.newState&&this.avatarManager.playIdle()})}wireConversationStateToAvatar(t){t.on(ConversationManagerEvents.StateChange,t=>{"waiting"===t.newState&&this.avatarManager.playThink()})}}var dist=__webpack_require__(937);const VadManagerEvents={VoiceActivity:"voiceActivity",Silence:"silence"};class VADManager extends EventEmitter{vadModel=null;silenceTimer=null;silenceTimeoutMs;positiveSpeechThreshold;negativeSpeechThreshold;minSpeechFrames;isDisposed=!1;currentStream=null;constructor(t=300,e=.5,n=.35,r=3){super(new DefaultLogger({category:"🎤 VADManager",style:StylePurple})),this.silenceTimeoutMs=t,this.positiveSpeechThreshold=e,this.negativeSpeechThreshold=n,this.minSpeechFrames=r}async initialize(t){if(this.isDisposed)return;this.currentStream=t,this.logger.debug("You may need to close the browser console for this to initialize");const e=await dist.Jl.new({stream:t,baseAssetPath:"/static/binaries/",onnxWASMBasePath:"/static/binaries/",positiveSpeechThreshold:this.positiveSpeechThreshold,negativeSpeechThreshold:this.negativeSpeechThreshold,minSpeechFrames:this.minSpeechFrames,onSpeechStart:async()=>{this.isDisposed||(this.logger.debug("Speech started"),this.clearSilenceTimer(),await this.emit(VadManagerEvents.VoiceActivity,{isSpeaking:!0,confidence:1}))},onSpeechEnd:async t=>{this.isDisposed||(this.logger.debug("Speech ended"),this.startSilenceTimer(),await this.emit(VadManagerEvents.VoiceActivity,{isSpeaking:!1,confidence:1}))},onVADMisfire:async()=>{this.isDisposed||(this.logger.debug("Misfire detected"),await this.emit(VadManagerEvents.VoiceActivity,{isSpeaking:!1,confidence:0,misfire:!0}))},onFrameProcessed:async t=>{}});if(this.isDisposed){this.logger.debug("VADManager disposed during initialization, cleaning up model");try{"function"==typeof e.destroy?e.destroy():e.pause()}catch(t){this.logger.debug("Error cleaning up VAD model after disposal during init",t)}}else this.vadModel=e,this.logger.debug(`VAD Manager initialized (MicVAD) with silenceTimeoutMs=${this.silenceTimeoutMs}, positiveSpeechThreshold=${this.positiveSpeechThreshold}, negativeSpeechThreshold=${this.negativeSpeechThreshold}, minSpeechFrames=${this.minSpeechFrames}`)}startAnalysis(){if(this.isDisposed)this.logger.debug("Cannot start VAD analysis - VADManager is disposed");else if(this.vadModel)if(this.currentStream)try{this.vadModel.start(),this.logger.debug("VAD analysis started (MicVAD)")}catch(t){this.logger.warn("Error starting VAD analysis, VAD may be in inconsistent state:",t)}else this.logger.debug("Cannot start VAD analysis - no active stream");else this.logger.debug("Cannot start VAD analysis - VAD model not initialized")}stopAnalysis(){if(this.clearSilenceTimer(),this.vadModel)try{this.vadModel.pause(),this.logger.debug("VAD analysis stopped (MicVAD)")}catch(t){}}startSilenceTimer(){this.silenceTimer||(this.silenceTimer=window.setTimeout(async()=>{await this.emit(VadManagerEvents.Silence),this.silenceTimer=null},this.silenceTimeoutMs))}clearSilenceTimer(){this.silenceTimer&&(clearTimeout(this.silenceTimer),this.silenceTimer=null)}dispose(){try{if(this.logger.debug("Disposing VADManager..."),this.isDisposed=!0,this.clearSilenceTimer(),this.removeAllListeners(),this.currentStream=null,this.vadModel){if(this.stopAnalysis(),"function"==typeof this.vadModel.destroy)try{this.vadModel.destroy(),this.logger.debug("VAD model destroyed")}catch(t){}this.vadModel=null}this.logger.debug("VADManager disposed")}catch(t){this.logger.warn("Error while disposing VADManager",t)}}}const UserInputManagerEvents={UserInput:"user-input",UserSpeaking:"user-speaking",UserSilence:"user-silence"},AUDIO_RECORDER_EVENTS={RecordingStarted:"recording-started",RecordingStopped:"recording-stopped",AudioData:"audio-data",Error:"error"},types_VadManagerEvents={VoiceActivity:"voiceActivity",Silence:"silence"},randomUUID="undefined"!=typeof crypto&&crypto.randomUUID&&crypto.randomUUID.bind(crypto),dist_native={randomUUID};let getRandomValues;const rnds8=new Uint8Array(16);function rng(){if(!getRandomValues){if("undefined"==typeof crypto||!crypto.getRandomValues)throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");getRandomValues=crypto.getRandomValues.bind(crypto)}return getRandomValues(rnds8)}const byteToHex=[];for(let t=0;t<256;++t)byteToHex.push((t+256).toString(16).slice(1));function unsafeStringify(t,e=0){return(byteToHex[t[e+0]]+byteToHex[t[e+1]]+byteToHex[t[e+2]]+byteToHex[t[e+3]]+"-"+byteToHex[t[e+4]]+byteToHex[t[e+5]]+"-"+byteToHex[t[e+6]]+byteToHex[t[e+7]]+"-"+byteToHex[t[e+8]]+byteToHex[t[e+9]]+"-"+byteToHex[t[e+10]]+byteToHex[t[e+11]]+byteToHex[t[e+12]]+byteToHex[t[e+13]]+byteToHex[t[e+14]]+byteToHex[t[e+15]]).toLowerCase()}function stringify(t,e=0){const n=unsafeStringify(t,e);if(!validate(n))throw TypeError("Stringified UUID is invalid");return n}const dist_stringify=null;function _v4(t,e,n){const r=(t=t||{}).random??t.rng?.()??rng();if(r.length<16)throw new Error("Random bytes length must be >= 16");if(r[6]=15&r[6]|64,r[8]=63&r[8]|128,e){if((n=n||0)<0||n+16>e.length)throw new RangeError(`UUID byte range ${n}:${n+15} is out of buffer bounds`);for(let t=0;t<16;++t)e[n+t]=r[t];return e}return unsafeStringify(r)}function v4(t,e,n){return!dist_native.randomUUID||e||t?_v4(t,e,n):dist_native.randomUUID()}const dist_v4=v4;class WebSocketConnection{websocket=null;handlers;serverUrl;constructor(t,e){this.serverUrl=t,this.handlers=e}connect(){this.websocket=new WebSocket(this.serverUrl),this.setupHandlers()}disconnect(){this.websocket&&(this.websocket.onmessage=null,this.websocket.onerror=null,this.websocket.onclose=null,this.websocket.close(1e3,"Client initiated disconnect"),this.websocket=null)}send(t){if(!this.websocket||this.websocket.readyState!==WebSocket.OPEN)throw new Error("Cant send. WebSocket is not open");this.websocket.send(JSON.stringify(t))}isReady(){return this.websocket?.readyState===WebSocket.OPEN}setupHandlers(){this.websocket&&(this.websocket.onopen=()=>{this.handlers.onOpen&&this.handlers.onOpen()},this.websocket.onmessage=t=>{const e=JSON.parse(t.data);this.handlers.onMessage&&this.handlers.onMessage(e)},this.websocket.onerror=t=>{this.handlers.onError&&this.handlers.onError(t)},this.websocket.onclose=()=>{this.handlers.onClose&&this.handlers.onClose()})}}function bind(t,e){return function(){return t.apply(e,arguments)}}const{toString:utils_toString}=Object.prototype,{getPrototypeOf}=Object,{iterator,toStringTag}=Symbol,kindOf=(cache=Object.create(null),t=>{const e=utils_toString.call(t);return cache[e]||(cache[e]=e.slice(8,-1).toLowerCase())});var cache;const kindOfTest=t=>(t=t.toLowerCase(),e=>kindOf(e)===t),typeOfTest=t=>e=>typeof e===t,{isArray}=Array,isUndefined=typeOfTest("undefined");function isBuffer(t){return null!==t&&!isUndefined(t)&&null!==t.constructor&&!isUndefined(t.constructor)&&isFunction(t.constructor.isBuffer)&&t.constructor.isBuffer(t)}const isArrayBuffer=kindOfTest("ArrayBuffer");function isArrayBufferView(t){let e;return e="undefined"!=typeof ArrayBuffer&&ArrayBuffer.isView?ArrayBuffer.isView(t):t&&t.buffer&&isArrayBuffer(t.buffer),e}const isString=typeOfTest("string"),isFunction=typeOfTest("function"),isNumber=typeOfTest("number"),isObject=t=>null!==t&&"object"==typeof t,isBoolean=t=>!0===t||!1===t,isPlainObject=t=>{if("object"!==kindOf(t))return!1;const e=getPrototypeOf(t);return!(null!==e&&e!==Object.prototype&&null!==Object.getPrototypeOf(e)||toStringTag in t||iterator in t)},isEmptyObject=t=>{if(!isObject(t)||isBuffer(t))return!1;try{return 0===Object.keys(t).length&&Object.getPrototypeOf(t)===Object.prototype}catch(t){return!1}},isDate=kindOfTest("Date"),isFile=kindOfTest("File"),isBlob=kindOfTest("Blob"),isFileList=kindOfTest("FileList"),isStream=t=>isObject(t)&&isFunction(t.pipe),isFormData=t=>{let e;return t&&("function"==typeof FormData&&t instanceof FormData||isFunction(t.append)&&("formdata"===(e=kindOf(t))||"object"===e&&isFunction(t.toString)&&"[object FormData]"===t.toString()))},isURLSearchParams=kindOfTest("URLSearchParams"),[isReadableStream,isRequest,isResponse,isHeaders]=["ReadableStream","Request","Response","Headers"].map(kindOfTest),trim=t=>t.trim?t.trim():t.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g,"");function forEach(t,e,{allOwnKeys:n=!1}={}){if(null==t)return;let r,i;if("object"!=typeof t&&(t=[t]),isArray(t))for(r=0,i=t.length;r<i;r++)e.call(null,t[r],r,t);else{if(isBuffer(t))return;const i=n?Object.getOwnPropertyNames(t):Object.keys(t),o=i.length;let s;for(r=0;r<o;r++)s=i[r],e.call(null,t[s],s,t)}}function findKey(t,e){if(isBuffer(t))return null;e=e.toLowerCase();const n=Object.keys(t);let r,i=n.length;for(;i-- >0;)if(r=n[i],e===r.toLowerCase())return r;return null}const _global="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:"undefined"!=typeof window?window:__webpack_require__.g,isContextDefined=t=>!isUndefined(t)&&t!==_global;function merge(){const{caseless:t,skipUndefined:e}=isContextDefined(this)&&this||{},n={},r=(r,i)=>{const o=t&&findKey(n,i)||i;isPlainObject(n[o])&&isPlainObject(r)?n[o]=merge(n[o],r):isPlainObject(r)?n[o]=merge({},r):isArray(r)?n[o]=r.slice():e&&isUndefined(r)||(n[o]=r)};for(let t=0,e=arguments.length;t<e;t++)arguments[t]&&forEach(arguments[t],r);return n}const extend=(t,e,n,{allOwnKeys:r}={})=>(forEach(e,(e,r)=>{n&&isFunction(e)?t[r]=bind(e,n):t[r]=e},{allOwnKeys:r}),t),stripBOM=t=>(65279===t.charCodeAt(0)&&(t=t.slice(1)),t),inherits=(t,e,n,r)=>{t.prototype=Object.create(e.prototype,r),t.prototype.constructor=t,Object.defineProperty(t,"super",{value:e.prototype}),n&&Object.assign(t.prototype,n)},toFlatObject=(t,e,n,r)=>{let i,o,s;const a={};if(e=e||{},null==t)return e;do{for(i=Object.getOwnPropertyNames(t),o=i.length;o-- >0;)s=i[o],r&&!r(s,t,e)||a[s]||(e[s]=t[s],a[s]=!0);t=!1!==n&&getPrototypeOf(t)}while(t&&(!n||n(t,e))&&t!==Object.prototype);return e},endsWith=(t,e,n)=>{t=String(t),(void 0===n||n>t.length)&&(n=t.length),n-=e.length;const r=t.indexOf(e,n);return-1!==r&&r===n},toArray=t=>{if(!t)return null;if(isArray(t))return t;let e=t.length;if(!isNumber(e))return null;const n=new Array(e);for(;e-- >0;)n[e]=t[e];return n},isTypedArray=(TypedArray="undefined"!=typeof Uint8Array&&getPrototypeOf(Uint8Array),t=>TypedArray&&t instanceof TypedArray);var TypedArray;const forEachEntry=(t,e)=>{const n=(t&&t[iterator]).call(t);let r;for(;(r=n.next())&&!r.done;){const n=r.value;e.call(t,n[0],n[1])}},matchAll=(t,e)=>{let n;const r=[];for(;null!==(n=t.exec(e));)r.push(n);return r},isHTMLForm=kindOfTest("HTMLFormElement"),toCamelCase=t=>t.toLowerCase().replace(/[-_\s]([a-z\d])(\w*)/g,function(t,e,n){return e.toUpperCase()+n}),utils_hasOwnProperty=(({hasOwnProperty:t})=>(e,n)=>t.call(e,n))(Object.prototype),isRegExp=kindOfTest("RegExp"),reduceDescriptors=(t,e)=>{const n=Object.getOwnPropertyDescriptors(t),r={};forEach(n,(n,i)=>{let o;!1!==(o=e(n,i,t))&&(r[i]=o||n)}),Object.defineProperties(t,r)},freezeMethods=t=>{reduceDescriptors(t,(e,n)=>{if(isFunction(t)&&-1!==["arguments","caller","callee"].indexOf(n))return!1;const r=t[n];isFunction(r)&&(e.enumerable=!1,"writable"in e?e.writable=!1:e.set||(e.set=()=>{throw Error("Can not rewrite read-only method '"+n+"'")}))})},toObjectSet=(t,e)=>{const n={},r=t=>{t.forEach(t=>{n[t]=!0})};return isArray(t)?r(t):r(String(t).split(e)),n},noop=()=>{},toFiniteNumber=(t,e)=>null!=t&&Number.isFinite(t=+t)?t:e;function isSpecCompliantForm(t){return!!(t&&isFunction(t.append)&&"FormData"===t[toStringTag]&&t[iterator])}const toJSONObject=t=>{const e=new Array(10),n=(t,r)=>{if(isObject(t)){if(e.indexOf(t)>=0)return;if(isBuffer(t))return t;if(!("toJSON"in t)){e[r]=t;const i=isArray(t)?[]:{};return forEach(t,(t,e)=>{const o=n(t,r+1);!isUndefined(o)&&(i[e]=o)}),e[r]=void 0,i}}return t};return n(t,0)},isAsyncFn=kindOfTest("AsyncFunction"),isThenable=t=>t&&(isObject(t)||isFunction(t))&&isFunction(t.then)&&isFunction(t.catch),_setImmediate=(setImmediateSupported="function"==typeof setImmediate,postMessageSupported=isFunction(_global.postMessage),setImmediateSupported?setImmediate:postMessageSupported?(token=`axios@${Math.random()}`,callbacks=[],_global.addEventListener("message",({source:t,data:e})=>{t===_global&&e===token&&callbacks.length&&callbacks.shift()()},!1),t=>{callbacks.push(t),_global.postMessage(token,"*")}):t=>setTimeout(t));var setImmediateSupported,postMessageSupported,token,callbacks;const asap="undefined"!=typeof queueMicrotask?queueMicrotask.bind(_global):"undefined"!=typeof process&&process.nextTick||_setImmediate,isIterable=t=>null!=t&&isFunction(t[iterator]),utils={isArray,isArrayBuffer,isBuffer,isFormData,isArrayBufferView,isString,isNumber,isBoolean,isObject,isPlainObject,isEmptyObject,isReadableStream,isRequest,isResponse,isHeaders,isUndefined,isDate,isFile,isBlob,isRegExp,isFunction,isStream,isURLSearchParams,isTypedArray,isFileList,forEach,merge,extend,trim,stripBOM,inherits,toFlatObject,kindOf,kindOfTest,endsWith,toArray,forEachEntry,matchAll,isHTMLForm,hasOwnProperty:utils_hasOwnProperty,hasOwnProp:utils_hasOwnProperty,reduceDescriptors,freezeMethods,toObjectSet,toCamelCase,noop,toFiniteNumber,findKey,global:_global,isContextDefined,isSpecCompliantForm,toJSONObject,isAsyncFn,isThenable,setImmediate:_setImmediate,asap,isIterable};function AxiosError(t,e,n,r,i){Error.call(this),Error.captureStackTrace?Error.captureStackTrace(this,this.constructor):this.stack=(new Error).stack,this.message=t,this.name="AxiosError",e&&(this.code=e),n&&(this.config=n),r&&(this.request=r),i&&(this.response=i,this.status=i.status?i.status:null)}utils.inherits(AxiosError,Error,{toJSON:function(){return{message:this.message,name:this.name,description:this.description,number:this.number,fileName:this.fileName,lineNumber:this.lineNumber,columnNumber:this.columnNumber,stack:this.stack,config:utils.toJSONObject(this.config),code:this.code,status:this.status}}});const AxiosError_prototype=AxiosError.prototype,descriptors={};["ERR_BAD_OPTION_VALUE","ERR_BAD_OPTION","ECONNABORTED","ETIMEDOUT","ERR_NETWORK","ERR_FR_TOO_MANY_REDIRECTS","ERR_DEPRECATED","ERR_BAD_RESPONSE","ERR_BAD_REQUEST","ERR_CANCELED","ERR_NOT_SUPPORT","ERR_INVALID_URL"].forEach(t=>{descriptors[t]={value:t}}),Object.defineProperties(AxiosError,descriptors),Object.defineProperty(AxiosError_prototype,"isAxiosError",{value:!0}),AxiosError.from=(t,e,n,r,i,o)=>{const s=Object.create(AxiosError_prototype);utils.toFlatObject(t,s,function(t){return t!==Error.prototype},t=>"isAxiosError"!==t);const a=t&&t.message?t.message:"Error",u=null==e&&t?t.code:e;return AxiosError.call(s,a,u,n,r,i),t&&null==s.cause&&Object.defineProperty(s,"cause",{value:t,configurable:!0}),s.name=t&&t.name||"Error",o&&Object.assign(s,o),s};const core_AxiosError=AxiosError,helpers_null=null;function isVisitable(t){return utils.isPlainObject(t)||utils.isArray(t)}function removeBrackets(t){return utils.endsWith(t,"[]")?t.slice(0,-2):t}function renderKey(t,e,n){return t?t.concat(e).map(function(t,e){return t=removeBrackets(t),!n&&e?"["+t+"]":t}).join(n?".":""):e}function isFlatArray(t){return utils.isArray(t)&&!t.some(isVisitable)}const predicates=utils.toFlatObject(utils,{},null,function(t){return/^is[A-Z]/.test(t)});function toFormData(t,e,n){if(!utils.isObject(t))throw new TypeError("target must be an object");e=e||new(helpers_null||FormData);const r=(n=utils.toFlatObject(n,{metaTokens:!0,dots:!1,indexes:!1},!1,function(t,e){return!utils.isUndefined(e[t])})).metaTokens,i=n.visitor||l,o=n.dots,s=n.indexes,a=(n.Blob||"undefined"!=typeof Blob&&Blob)&&utils.isSpecCompliantForm(e);if(!utils.isFunction(i))throw new TypeError("visitor must be a function");function u(t){if(null===t)return"";if(utils.isDate(t))return t.toISOString();if(utils.isBoolean(t))return t.toString();if(!a&&utils.isBlob(t))throw new core_AxiosError("Blob is not supported. Use a Buffer instead.");return utils.isArrayBuffer(t)||utils.isTypedArray(t)?a&&"function"==typeof Blob?new Blob([t]):Buffer.from(t):t}function l(t,n,i){let a=t;if(t&&!i&&"object"==typeof t)if(utils.endsWith(n,"{}"))n=r?n:n.slice(0,-2),t=JSON.stringify(t);else if(utils.isArray(t)&&isFlatArray(t)||(utils.isFileList(t)||utils.endsWith(n,"[]"))&&(a=utils.toArray(t)))return n=removeBrackets(n),a.forEach(function(t,r){!utils.isUndefined(t)&&null!==t&&e.append(!0===s?renderKey([n],r,o):null===s?n:n+"[]",u(t))}),!1;return!!isVisitable(t)||(e.append(renderKey(i,n,o),u(t)),!1)}const c=[],d=Object.assign(predicates,{defaultVisitor:l,convertValue:u,isVisitable});if(!utils.isObject(t))throw new TypeError("data must be an object");return function t(n,r){if(!utils.isUndefined(n)){if(-1!==c.indexOf(n))throw Error("Circular reference detected in "+r.join("."));c.push(n),utils.forEach(n,function(n,o){!0===(!(utils.isUndefined(n)||null===n)&&i.call(e,n,utils.isString(o)?o.trim():o,r,d))&&t(n,r?r.concat(o):[o])}),c.pop()}}(t),e}const helpers_toFormData=toFormData;function encode(t){const e={"!":"%21","'":"%27","(":"%28",")":"%29","~":"%7E","%20":"+","%00":"\0"};return encodeURIComponent(t).replace(/[!'()~]|%20|%00/g,function(t){return e[t]})}function AxiosURLSearchParams(t,e){this._pairs=[],t&&helpers_toFormData(t,this,e)}const AxiosURLSearchParams_prototype=AxiosURLSearchParams.prototype;AxiosURLSearchParams_prototype.append=function(t,e){this._pairs.push([t,e])},AxiosURLSearchParams_prototype.toString=function(t){const e=t?function(e){return t.call(this,e,encode)}:encode;return this._pairs.map(function(t){return e(t[0])+"="+e(t[1])},"").join("&")};const helpers_AxiosURLSearchParams=AxiosURLSearchParams;function buildURL_encode(t){return encodeURIComponent(t).replace(/%3A/gi,":").replace(/%24/g,"$").replace(/%2C/gi,",").replace(/%20/g,"+")}function buildURL(t,e,n){if(!e)return t;const r=n&&n.encode||buildURL_encode;utils.isFunction(n)&&(n={serialize:n});const i=n&&n.serialize;let o;if(o=i?i(e,n):utils.isURLSearchParams(e)?e.toString():new helpers_AxiosURLSearchParams(e,n).toString(r),o){const e=t.indexOf("#");-1!==e&&(t=t.slice(0,e)),t+=(-1===t.indexOf("?")?"?":"&")+o}return t}class InterceptorManager{constructor(){this.handlers=[]}use(t,e,n){return this.handlers.push({fulfilled:t,rejected:e,synchronous:!!n&&n.synchronous,runWhen:n?n.runWhen:null}),this.handlers.length-1}eject(t){this.handlers[t]&&(this.handlers[t]=null)}clear(){this.handlers&&(this.handlers=[])}forEach(t){utils.forEach(this.handlers,function(e){null!==e&&t(e)})}}const core_InterceptorManager=InterceptorManager,defaults_transitional={silentJSONParsing:!0,forcedJSONParsing:!0,clarifyTimeoutError:!1},classes_URLSearchParams="undefined"!=typeof URLSearchParams?URLSearchParams:helpers_AxiosURLSearchParams,classes_FormData="undefined"!=typeof FormData?FormData:null,classes_Blob="undefined"!=typeof Blob?Blob:null,browser={isBrowser:!0,classes:{URLSearchParams:classes_URLSearchParams,FormData:classes_FormData,Blob:classes_Blob},protocols:["http","https","file","blob","url","data"]},hasBrowserEnv="undefined"!=typeof window&&"undefined"!=typeof document,_navigator="object"==typeof navigator&&navigator||void 0,hasStandardBrowserEnv=hasBrowserEnv&&(!_navigator||["ReactNative","NativeScript","NS"].indexOf(_navigator.product)<0),hasStandardBrowserWebWorkerEnv="undefined"!=typeof WorkerGlobalScope&&self instanceof WorkerGlobalScope&&"function"==typeof self.importScripts,origin=hasBrowserEnv&&window.location.href||"http://localhost",platform={...common_utils_namespaceObject,...browser};function toURLEncodedForm(t,e){return helpers_toFormData(t,new platform.classes.URLSearchParams,{visitor:function(t,e,n,r){return platform.isNode&&utils.isBuffer(t)?(this.append(e,t.toString("base64")),!1):r.defaultVisitor.apply(this,arguments)},...e})}function parsePropPath(t){return utils.matchAll(/\w+|\[(\w*)]/g,t).map(t=>"[]"===t[0]?"":t[1]||t[0])}function arrayToObject(t){const e={},n=Object.keys(t);let r;const i=n.length;let o;for(r=0;r<i;r++)o=n[r],e[o]=t[o];return e}function formDataToJSON(t){function e(t,n,r,i){let o=t[i++];if("__proto__"===o)return!0;const s=Number.isFinite(+o),a=i>=t.length;return o=!o&&utils.isArray(r)?r.length:o,a?(utils.hasOwnProp(r,o)?r[o]=[r[o],n]:r[o]=n,!s):(r[o]&&utils.isObject(r[o])||(r[o]=[]),e(t,n,r[o],i)&&utils.isArray(r[o])&&(r[o]=arrayToObject(r[o])),!s)}if(utils.isFormData(t)&&utils.isFunction(t.entries)){const n={};return utils.forEachEntry(t,(t,r)=>{e(parsePropPath(t),r,n,0)}),n}return null}const helpers_formDataToJSON=formDataToJSON;function stringifySafely(t,e,n){if(utils.isString(t))try{return(e||JSON.parse)(t),utils.trim(t)}catch(t){if("SyntaxError"!==t.name)throw t}return(n||JSON.stringify)(t)}const defaults={transitional:defaults_transitional,adapter:["xhr","http","fetch"],transformRequest:[function(t,e){const n=e.getContentType()||"",r=n.indexOf("application/json")>-1,i=utils.isObject(t);if(i&&utils.isHTMLForm(t)&&(t=new FormData(t)),utils.isFormData(t))return r?JSON.stringify(helpers_formDataToJSON(t)):t;if(utils.isArrayBuffer(t)||utils.isBuffer(t)||utils.isStream(t)||utils.isFile(t)||utils.isBlob(t)||utils.isReadableStream(t))return t;if(utils.isArrayBufferView(t))return t.buffer;if(utils.isURLSearchParams(t))return e.setContentType("application/x-www-form-urlencoded;charset=utf-8",!1),t.toString();let o;if(i){if(n.indexOf("application/x-www-form-urlencoded")>-1)return toURLEncodedForm(t,this.formSerializer).toString();if((o=utils.isFileList(t))||n.indexOf("multipart/form-data")>-1){const e=this.env&&this.env.FormData;return helpers_toFormData(o?{"files[]":t}:t,e&&new e,this.formSerializer)}}return i||r?(e.setContentType("application/json",!1),stringifySafely(t)):t}],transformResponse:[function(t){const e=this.transitional||defaults.transitional,n=e&&e.forcedJSONParsing,r="json"===this.responseType;if(utils.isResponse(t)||utils.isReadableStream(t))return t;if(t&&utils.isString(t)&&(n&&!this.responseType||r)){const n=!(e&&e.silentJSONParsing)&&r;try{return JSON.parse(t,this.parseReviver)}catch(t){if(n){if("SyntaxError"===t.name)throw core_AxiosError.from(t,core_AxiosError.ERR_BAD_RESPONSE,this,null,this.response);throw t}}}return t}],timeout:0,xsrfCookieName:"XSRF-TOKEN",xsrfHeaderName:"X-XSRF-TOKEN",maxContentLength:-1,maxBodyLength:-1,env:{FormData:platform.classes.FormData,Blob:platform.classes.Blob},validateStatus:function(t){return t>=200&&t<300},headers:{common:{Accept:"application/json, text/plain, */*","Content-Type":void 0}}};utils.forEach(["delete","get","head","post","put","patch"],t=>{defaults.headers[t]={}});const lib_defaults=defaults,ignoreDuplicateOf=utils.toObjectSet(["age","authorization","content-length","content-type","etag","expires","from","host","if-modified-since","if-unmodified-since","last-modified","location","max-forwards","proxy-authorization","referer","retry-after","user-agent"]),parseHeaders=t=>{const e={};let n,r,i;return t&&t.split("\n").forEach(function(t){i=t.indexOf(":"),n=t.substring(0,i).trim().toLowerCase(),r=t.substring(i+1).trim(),!n||e[n]&&ignoreDuplicateOf[n]||("set-cookie"===n?e[n]?e[n].push(r):e[n]=[r]:e[n]=e[n]?e[n]+", "+r:r)}),e},$internals=Symbol("internals");function normalizeHeader(t){return t&&String(t).trim().toLowerCase()}function normalizeValue(t){return!1===t||null==t?t:utils.isArray(t)?t.map(normalizeValue):String(t)}function parseTokens(t){const e=Object.create(null),n=/([^\s,;=]+)\s*(?:=\s*([^,;]+))?/g;let r;for(;r=n.exec(t);)e[r[1]]=r[2];return e}const isValidHeaderName=t=>/^[-_a-zA-Z0-9^`|~,!#$%&'*+.]+$/.test(t.trim());function matchHeaderValue(t,e,n,r,i){return utils.isFunction(r)?r.call(this,e,n):(i&&(e=n),utils.isString(e)?utils.isString(r)?-1!==e.indexOf(r):utils.isRegExp(r)?r.test(e):void 0:void 0)}function formatHeader(t){return t.trim().toLowerCase().replace(/([a-z\d])(\w*)/g,(t,e,n)=>e.toUpperCase()+n)}function buildAccessors(t,e){const n=utils.toCamelCase(" "+e);["get","set","has"].forEach(r=>{Object.defineProperty(t,r+n,{value:function(t,n,i){return this[r].call(this,e,t,n,i)},configurable:!0})})}class AxiosHeaders{constructor(t){t&&this.set(t)}set(t,e,n){const r=this;function i(t,e,n){const i=normalizeHeader(e);if(!i)throw new Error("header name must be a non-empty string");const o=utils.findKey(r,i);(!o||void 0===r[o]||!0===n||void 0===n&&!1!==r[o])&&(r[o||e]=normalizeValue(t))}const o=(t,e)=>utils.forEach(t,(t,n)=>i(t,n,e));if(utils.isPlainObject(t)||t instanceof this.constructor)o(t,e);else if(utils.isString(t)&&(t=t.trim())&&!isValidHeaderName(t))o(parseHeaders(t),e);else if(utils.isObject(t)&&utils.isIterable(t)){let n,r,i={};for(const e of t){if(!utils.isArray(e))throw TypeError("Object iterator must return a key-value pair");i[r=e[0]]=(n=i[r])?utils.isArray(n)?[...n,e[1]]:[n,e[1]]:e[1]}o(i,e)}else null!=t&&i(e,t,n);return this}get(t,e){if(t=normalizeHeader(t)){const n=utils.findKey(this,t);if(n){const t=this[n];if(!e)return t;if(!0===e)return parseTokens(t);if(utils.isFunction(e))return e.call(this,t,n);if(utils.isRegExp(e))return e.exec(t);throw new TypeError("parser must be boolean|regexp|function")}}}has(t,e){if(t=normalizeHeader(t)){const n=utils.findKey(this,t);return!(!n||void 0===this[n]||e&&!matchHeaderValue(this,this[n],n,e))}return!1}delete(t,e){const n=this;let r=!1;function i(t){if(t=normalizeHeader(t)){const i=utils.findKey(n,t);!i||e&&!matchHeaderValue(n,n[i],i,e)||(delete n[i],r=!0)}}return utils.isArray(t)?t.forEach(i):i(t),r}clear(t){const e=Object.keys(this);let n=e.length,r=!1;for(;n--;){const i=e[n];t&&!matchHeaderValue(this,this[i],i,t,!0)||(delete this[i],r=!0)}return r}normalize(t){const e=this,n={};return utils.forEach(this,(r,i)=>{const o=utils.findKey(n,i);if(o)return e[o]=normalizeValue(r),void delete e[i];const s=t?formatHeader(i):String(i).trim();s!==i&&delete e[i],e[s]=normalizeValue(r),n[s]=!0}),this}concat(...t){return this.constructor.concat(this,...t)}toJSON(t){const e=Object.create(null);return utils.forEach(this,(n,r)=>{null!=n&&!1!==n&&(e[r]=t&&utils.isArray(n)?n.join(", "):n)}),e}[Symbol.iterator](){return Object.entries(this.toJSON())[Symbol.iterator]()}toString(){return Object.entries(this.toJSON()).map(([t,e])=>t+": "+e).join("\n")}getSetCookie(){return this.get("set-cookie")||[]}get[Symbol.toStringTag](){return"AxiosHeaders"}static from(t){return t instanceof this?t:new this(t)}static concat(t,...e){const n=new this(t);return e.forEach(t=>n.set(t)),n}static accessor(t){const e=(this[$internals]=this[$internals]={accessors:{}}).accessors,n=this.prototype;function r(t){const r=normalizeHeader(t);e[r]||(buildAccessors(n,t),e[r]=!0)}return utils.isArray(t)?t.forEach(r):r(t),this}}AxiosHeaders.accessor(["Content-Type","Content-Length","Accept","Accept-Encoding","User-Agent","Authorization"]),utils.reduceDescriptors(AxiosHeaders.prototype,({value:t},e)=>{let n=e[0].toUpperCase()+e.slice(1);return{get:()=>t,set(t){this[n]=t}}}),utils.freezeMethods(AxiosHeaders);const core_AxiosHeaders=AxiosHeaders;function transformData(t,e){const n=this||lib_defaults,r=e||n,i=core_AxiosHeaders.from(r.headers);let o=r.data;return utils.forEach(t,function(t){o=t.call(n,o,i.normalize(),e?e.status:void 0)}),i.normalize(),o}function isCancel(t){return!(!t||!t.__CANCEL__)}function CanceledError(t,e,n){core_AxiosError.call(this,null==t?"canceled":t,core_AxiosError.ERR_CANCELED,e,n),this.name="CanceledError"}utils.inherits(CanceledError,core_AxiosError,{__CANCEL__:!0});const cancel_CanceledError=CanceledError;function settle(t,e,n){const r=n.config.validateStatus;n.status&&r&&!r(n.status)?e(new core_AxiosError("Request failed with status code "+n.status,[core_AxiosError.ERR_BAD_REQUEST,core_AxiosError.ERR_BAD_RESPONSE][Math.floor(n.status/100)-4],n.config,n.request,n)):t(n)}function parseProtocol(t){const e=/^([-+\w]{1,25})(:?\/\/|:)/.exec(t);return e&&e[1]||""}function speedometer(t,e){t=t||10;const n=new Array(t),r=new Array(t);let i,o=0,s=0;return e=void 0!==e?e:1e3,function(a){const u=Date.now(),l=r[s];i||(i=u),n[o]=a,r[o]=u;let c=s,d=0;for(;c!==o;)d+=n[c++],c%=t;if(o=(o+1)%t,o===s&&(s=(s+1)%t),u-i<e)return;const h=l&&u-l;return h?Math.round(1e3*d/h):void 0}}const helpers_speedometer=speedometer;function throttle(t,e){let n,r,i=0,o=1e3/e;const s=(e,o=Date.now())=>{i=o,n=null,r&&(clearTimeout(r),r=null),t(...e)};return[(...t)=>{const e=Date.now(),a=e-i;a>=o?s(t,e):(n=t,r||(r=setTimeout(()=>{r=null,s(n)},o-a)))},()=>n&&s(n)]}const helpers_throttle=throttle,progressEventReducer=(t,e,n=3)=>{let r=0;const i=helpers_speedometer(50,250);return helpers_throttle(n=>{const o=n.loaded,s=n.lengthComputable?n.total:void 0,a=o-r,u=i(a);r=o,t({loaded:o,total:s,progress:s?o/s:void 0,bytes:a,rate:u||void 0,estimated:u&&s&&o<=s?(s-o)/u:void 0,event:n,lengthComputable:null!=s,[e?"download":"upload"]:!0})},n)},progressEventDecorator=(t,e)=>{const n=null!=t;return[r=>e[0]({lengthComputable:n,total:t,loaded:r}),e[1]]},asyncDecorator=t=>(...e)=>utils.asap(()=>t(...e)),isURLSameOrigin=platform.hasStandardBrowserEnv?((t,e)=>n=>(n=new URL(n,platform.origin),t.protocol===n.protocol&&t.host===n.host&&(e||t.port===n.port)))(new URL(platform.origin),platform.navigator&&/(msie|trident)/i.test(platform.navigator.userAgent)):()=>!0,cookies=platform.hasStandardBrowserEnv?{write(t,e,n,r,i,o,s){if("undefined"==typeof document)return;const a=[`${t}=${encodeURIComponent(e)}`];utils.isNumber(n)&&a.push(`expires=${new Date(n).toUTCString()}`),utils.isString(r)&&a.push(`path=${r}`),utils.isString(i)&&a.push(`domain=${i}`),!0===o&&a.push("secure"),utils.isString(s)&&a.push(`SameSite=${s}`),document.cookie=a.join("; ")},read(t){if("undefined"==typeof document)return null;const e=document.cookie.match(new RegExp("(?:^|; )"+t+"=([^;]*)"));return e?decodeURIComponent(e[1]):null},remove(t){this.write(t,"",Date.now()-864e5,"/")}}:{write(){},read:()=>null,remove(){}};function isAbsoluteURL(t){return/^([a-z][a-z\d+\-.]*:)?\/\//i.test(t)}function combineURLs(t,e){return e?t.replace(/\/?\/$/,"")+"/"+e.replace(/^\/+/,""):t}function buildFullPath(t,e,n){let r=!isAbsoluteURL(e);return t&&(r||0==n)?combineURLs(t,e):e}const headersToObject=t=>t instanceof core_AxiosHeaders?{...t}:t;function mergeConfig(t,e){e=e||{};const n={};function r(t,e,n,r){return utils.isPlainObject(t)&&utils.isPlainObject(e)?utils.merge.call({caseless:r},t,e):utils.isPlainObject(e)?utils.merge({},e):utils.isArray(e)?e.slice():e}function i(t,e,n,i){return utils.isUndefined(e)?utils.isUndefined(t)?void 0:r(void 0,t,0,i):r(t,e,0,i)}function o(t,e){if(!utils.isUndefined(e))return r(void 0,e)}function s(t,e){return utils.isUndefined(e)?utils.isUndefined(t)?void 0:r(void 0,t):r(void 0,e)}function a(n,i,o){return o in e?r(n,i):o in t?r(void 0,n):void 0}const u={url:o,method:o,data:o,baseURL:s,transformRequest:s,transformResponse:s,paramsSerializer:s,timeout:s,timeoutMessage:s,withCredentials:s,withXSRFToken:s,adapter:s,responseType:s,xsrfCookieName:s,xsrfHeaderName:s,onUploadProgress:s,onDownloadProgress:s,decompress:s,maxContentLength:s,maxBodyLength:s,beforeRedirect:s,transport:s,httpAgent:s,httpsAgent:s,cancelToken:s,socketPath:s,responseEncoding:s,validateStatus:a,headers:(t,e,n)=>i(headersToObject(t),headersToObject(e),0,!0)};return utils.forEach(Object.keys({...t,...e}),function(r){const o=u[r]||i,s=o(t[r],e[r],r);utils.isUndefined(s)&&o!==a||(n[r]=s)}),n}const resolveConfig=t=>{const e=mergeConfig({},t);let{data:n,withXSRFToken:r,xsrfHeaderName:i,xsrfCookieName:o,headers:s,auth:a}=e;if(e.headers=s=core_AxiosHeaders.from(s),e.url=buildURL(buildFullPath(e.baseURL,e.url,e.allowAbsoluteUrls),t.params,t.paramsSerializer),a&&s.set("Authorization","Basic "+btoa((a.username||"")+":"+(a.password?unescape(encodeURIComponent(a.password)):""))),utils.isFormData(n))if(platform.hasStandardBrowserEnv||platform.hasStandardBrowserWebWorkerEnv)s.setContentType(void 0);else if(utils.isFunction(n.getHeaders)){const t=n.getHeaders(),e=["content-type","content-length"];Object.entries(t).forEach(([t,n])=>{e.includes(t.toLowerCase())&&s.set(t,n)})}if(platform.hasStandardBrowserEnv&&(r&&utils.isFunction(r)&&(r=r(e)),r||!1!==r&&isURLSameOrigin(e.url))){const t=i&&o&&cookies.read(o);t&&s.set(i,t)}return e},isXHRAdapterSupported="undefined"!=typeof XMLHttpRequest,xhr=isXHRAdapterSupported&&function(t){return new Promise(function(e,n){const r=resolveConfig(t);let i=r.data;const o=core_AxiosHeaders.from(r.headers).normalize();let s,a,u,l,c,{responseType:d,onUploadProgress:h,onDownloadProgress:p}=r;function f(){l&&l(),c&&c(),r.cancelToken&&r.cancelToken.unsubscribe(s),r.signal&&r.signal.removeEventListener("abort",s)}let g=new XMLHttpRequest;function m(){if(!g)return;const r=core_AxiosHeaders.from("getAllResponseHeaders"in g&&g.getAllResponseHeaders());settle(function(t){e(t),f()},function(t){n(t),f()},{data:d&&"text"!==d&&"json"!==d?g.response:g.responseText,status:g.status,statusText:g.statusText,headers:r,config:t,request:g}),g=null}g.open(r.method.toUpperCase(),r.url,!0),g.timeout=r.timeout,"onloadend"in g?g.onloadend=m:g.onreadystatechange=function(){g&&4===g.readyState&&(0!==g.status||g.responseURL&&0===g.responseURL.indexOf("file:"))&&setTimeout(m)},g.onabort=function(){g&&(n(new core_AxiosError("Request aborted",core_AxiosError.ECONNABORTED,t,g)),g=null)},g.onerror=function(e){const r=e&&e.message?e.message:"Network Error",i=new core_AxiosError(r,core_AxiosError.ERR_NETWORK,t,g);i.event=e||null,n(i),g=null},g.ontimeout=function(){let e=r.timeout?"timeout of "+r.timeout+"ms exceeded":"timeout exceeded";const i=r.transitional||defaults_transitional;r.timeoutErrorMessage&&(e=r.timeoutErrorMessage),n(new core_AxiosError(e,i.clarifyTimeoutError?core_AxiosError.ETIMEDOUT:core_AxiosError.ECONNABORTED,t,g)),g=null},void 0===i&&o.setContentType(null),"setRequestHeader"in g&&utils.forEach(o.toJSON(),function(t,e){g.setRequestHeader(e,t)}),utils.isUndefined(r.withCredentials)||(g.withCredentials=!!r.withCredentials),d&&"json"!==d&&(g.responseType=r.responseType),p&&([u,c]=progressEventReducer(p,!0),g.addEventListener("progress",u)),h&&g.upload&&([a,l]=progressEventReducer(h),g.upload.addEventListener("progress",a),g.upload.addEventListener("loadend",l)),(r.cancelToken||r.signal)&&(s=e=>{g&&(n(!e||e.type?new cancel_CanceledError(null,t,g):e),g.abort(),g=null)},r.cancelToken&&r.cancelToken.subscribe(s),r.signal&&(r.signal.aborted?s():r.signal.addEventListener("abort",s)));const b=parseProtocol(r.url);b&&-1===platform.protocols.indexOf(b)?n(new core_AxiosError("Unsupported protocol "+b+":",core_AxiosError.ERR_BAD_REQUEST,t)):g.send(i||null)})},composeSignals=(t,e)=>{const{length:n}=t=t?t.filter(Boolean):[];if(e||n){let n,r=new AbortController;const i=function(t){if(!n){n=!0,s();const e=t instanceof Error?t:this.reason;r.abort(e instanceof core_AxiosError?e:new cancel_CanceledError(e instanceof Error?e.message:e))}};let o=e&&setTimeout(()=>{o=null,i(new core_AxiosError(`timeout ${e} of ms exceeded`,core_AxiosError.ETIMEDOUT))},e);const s=()=>{t&&(o&&clearTimeout(o),o=null,t.forEach(t=>{t.unsubscribe?t.unsubscribe(i):t.removeEventListener("abort",i)}),t=null)};t.forEach(t=>t.addEventListener("abort",i));const{signal:a}=r;return a.unsubscribe=()=>utils.asap(s),a}},helpers_composeSignals=composeSignals,streamChunk=function*(t,e){let n=t.byteLength;if(!e||n<e)return void(yield t);let r,i=0;for(;i<n;)r=i+e,yield t.slice(i,r),i=r},readBytes=async function*(t,e){for await(const n of readStream(t))yield*streamChunk(n,e)},readStream=async function*(t){if(t[Symbol.asyncIterator])return void(yield*t);const e=t.getReader();try{for(;;){const{done:t,value:n}=await e.read();if(t)break;yield n}}finally{await e.cancel()}},trackStream=(t,e,n,r)=>{const i=readBytes(t,e);let o,s=0,a=t=>{o||(o=!0,r&&r(t))};return new ReadableStream({async pull(t){try{const{done:e,value:r}=await i.next();if(e)return a(),void t.close();let o=r.byteLength;if(n){let t=s+=o;n(t)}t.enqueue(new Uint8Array(r))}catch(t){throw a(t),t}},cancel:t=>(a(t),i.return())},{highWaterMark:2})},DEFAULT_CHUNK_SIZE=65536,{isFunction:fetch_isFunction}=utils,globalFetchAPI=(({Request:t,Response:e})=>({Request:t,Response:e}))(utils.global),{ReadableStream:fetch_ReadableStream,TextEncoder}=utils.global,test=(t,...e)=>{try{return!!t(...e)}catch(t){return!1}},factory=t=>{t=utils.merge.call({skipUndefined:!0},globalFetchAPI,t);const{fetch:e,Request:n,Response:r}=t,i=e?fetch_isFunction(e):"function"==typeof fetch,o=fetch_isFunction(n),s=fetch_isFunction(r);if(!i)return!1;const a=i&&fetch_isFunction(fetch_ReadableStream),u=i&&("function"==typeof TextEncoder?(l=new TextEncoder,t=>l.encode(t)):async t=>new Uint8Array(await new n(t).arrayBuffer()));var l;const c=o&&a&&test(()=>{let t=!1;const e=new n(platform.origin,{body:new fetch_ReadableStream,method:"POST",get duplex(){return t=!0,"half"}}).headers.has("Content-Type");return t&&!e}),d=s&&a&&test(()=>utils.isReadableStream(new r("").body)),h={stream:d&&(t=>t.body)};i&&["text","arrayBuffer","blob","formData","stream"].forEach(t=>{!h[t]&&(h[t]=(e,n)=>{let r=e&&e[t];if(r)return r.call(e);throw new core_AxiosError(`Response type '${t}' is not supported`,core_AxiosError.ERR_NOT_SUPPORT,n)})});return async t=>{let{url:i,method:s,data:a,signal:l,cancelToken:p,timeout:f,onDownloadProgress:g,onUploadProgress:m,responseType:b,headers:y,withCredentials:_="same-origin",fetchOptions:w}=resolveConfig(t),v=e||fetch;b=b?(b+"").toLowerCase():"text";let x=helpers_composeSignals([l,p&&p.toAbortSignal()],f),T=null;const S=x&&x.unsubscribe&&(()=>{x.unsubscribe()});let A;try{if(m&&c&&"get"!==s&&"head"!==s&&0!==(A=await(async(t,e)=>{const r=utils.toFiniteNumber(t.getContentLength());return null==r?(async t=>{if(null==t)return 0;if(utils.isBlob(t))return t.size;if(utils.isSpecCompliantForm(t)){const e=new n(platform.origin,{method:"POST",body:t});return(await e.arrayBuffer()).byteLength}return utils.isArrayBufferView(t)||utils.isArrayBuffer(t)?t.byteLength:(utils.isURLSearchParams(t)&&(t+=""),utils.isString(t)?(await u(t)).byteLength:void 0)})(e):r})(y,a))){let t,e=new n(i,{method:"POST",body:a,duplex:"half"});if(utils.isFormData(a)&&(t=e.headers.get("content-type"))&&y.setContentType(t),e.body){const[t,n]=progressEventDecorator(A,progressEventReducer(asyncDecorator(m)));a=trackStream(e.body,DEFAULT_CHUNK_SIZE,t,n)}}utils.isString(_)||(_=_?"include":"omit");const e=o&&"credentials"in n.prototype,l={...w,signal:x,method:s.toUpperCase(),headers:y.normalize().toJSON(),body:a,duplex:"half",credentials:e?_:void 0};T=o&&new n(i,l);let p=await(o?v(T,w):v(i,l));const f=d&&("stream"===b||"response"===b);if(d&&(g||f&&S)){const t={};["status","statusText","headers"].forEach(e=>{t[e]=p[e]});const e=utils.toFiniteNumber(p.headers.get("content-length")),[n,i]=g&&progressEventDecorator(e,progressEventReducer(asyncDecorator(g),!0))||[];p=new r(trackStream(p.body,DEFAULT_CHUNK_SIZE,n,()=>{i&&i(),S&&S()}),t)}b=b||"text";let E=await h[utils.findKey(h,b)||"text"](p,t);return!f&&S&&S(),await new Promise((e,n)=>{settle(e,n,{data:E,headers:core_AxiosHeaders.from(p.headers),status:p.status,statusText:p.statusText,config:t,request:T})})}catch(e){if(S&&S(),e&&"TypeError"===e.name&&/Load failed|fetch/i.test(e.message))throw Object.assign(new core_AxiosError("Network Error",core_AxiosError.ERR_NETWORK,t,T),{cause:e.cause||e});throw core_AxiosError.from(e,e&&e.code,t,T)}}},seedCache=new Map,getFetch=t=>{let e=t&&t.env||{};const{fetch:n,Request:r,Response:i}=e,o=[r,i,n];let s,a,u=o.length,l=seedCache;for(;u--;)s=o[u],a=l.get(s),void 0===a&&l.set(s,a=u?new Map:factory(e)),l=a;return a},adapter=getFetch(),adapters_fetch=null,knownAdapters={http:helpers_null,xhr,fetch:{get:getFetch}};utils.forEach(knownAdapters,(t,e)=>{if(t){try{Object.defineProperty(t,"name",{value:e})}catch(t){}Object.defineProperty(t,"adapterName",{value:e})}});const renderReason=t=>`- ${t}`,isResolvedHandle=t=>utils.isFunction(t)||null===t||!1===t;function getAdapter(t,e){t=utils.isArray(t)?t:[t];const{length:n}=t;let r,i;const o={};for(let s=0;s<n;s++){let n;if(r=t[s],i=r,!isResolvedHandle(r)&&(i=knownAdapters[(n=String(r)).toLowerCase()],void 0===i))throw new core_AxiosError(`Unknown adapter '${n}'`);if(i&&(utils.isFunction(i)||(i=i.get(e))))break;o[n||"#"+s]=i}if(!i){const t=Object.entries(o).map(([t,e])=>`adapter ${t} `+(!1===e?"is not supported by the environment":"is not available in the build"));let e=n?t.length>1?"since :\n"+t.map(renderReason).join("\n"):" "+renderReason(t[0]):"as no adapter specified";throw new core_AxiosError("There is no suitable adapter to dispatch the request "+e,"ERR_NOT_SUPPORT")}return i}const adapters={getAdapter,adapters:knownAdapters};function throwIfCancellationRequested(t){if(t.cancelToken&&t.cancelToken.throwIfRequested(),t.signal&&t.signal.aborted)throw new cancel_CanceledError(null,t)}function dispatchRequest(t){return throwIfCancellationRequested(t),t.headers=core_AxiosHeaders.from(t.headers),t.data=transformData.call(t,t.transformRequest),-1!==["post","put","patch"].indexOf(t.method)&&t.headers.setContentType("application/x-www-form-urlencoded",!1),adapters.getAdapter(t.adapter||lib_defaults.adapter,t)(t).then(function(e){return throwIfCancellationRequested(t),e.data=transformData.call(t,t.transformResponse,e),e.headers=core_AxiosHeaders.from(e.headers),e},function(e){return isCancel(e)||(throwIfCancellationRequested(t),e&&e.response&&(e.response.data=transformData.call(t,t.transformResponse,e.response),e.response.headers=core_AxiosHeaders.from(e.response.headers))),Promise.reject(e)})}const VERSION="1.13.2",validators={};["object","boolean","number","function","string","symbol"].forEach((t,e)=>{validators[t]=function(n){return typeof n===t||"a"+(e<1?"n ":" ")+t}});const deprecatedWarnings={};function assertOptions(t,e,n){if("object"!=typeof t)throw new core_AxiosError("options must be an object",core_AxiosError.ERR_BAD_OPTION_VALUE);const r=Object.keys(t);let i=r.length;for(;i-- >0;){const o=r[i],s=e[o];if(s){const e=t[o],n=void 0===e||s(e,o,t);if(!0!==n)throw new core_AxiosError("option "+o+" must be "+n,core_AxiosError.ERR_BAD_OPTION_VALUE);continue}if(!0!==n)throw new core_AxiosError("Unknown option "+o,core_AxiosError.ERR_BAD_OPTION)}}validators.transitional=function(t,e,n){function r(t,e){return"[Axios v"+VERSION+"] Transitional option '"+t+"'"+e+(n?". "+n:"")}return(n,i,o)=>{if(!1===t)throw new core_AxiosError(r(i," has been removed"+(e?" in "+e:"")),core_AxiosError.ERR_DEPRECATED);return e&&!deprecatedWarnings[i]&&(deprecatedWarnings[i]=!0,console.warn(r(i," has been deprecated since v"+e+" and will be removed in the near future"))),!t||t(n,i,o)}},validators.spelling=function(t){return(e,n)=>(console.warn(`${n} is likely a misspelling of ${t}`),!0)};const validator={assertOptions,validators},Axios_validators=validator.validators;class Axios{constructor(t){this.defaults=t||{},this.interceptors={request:new core_InterceptorManager,response:new core_InterceptorManager}}async request(t,e){try{return await this._request(t,e)}catch(t){if(t instanceof Error){let e={};Error.captureStackTrace?Error.captureStackTrace(e):e=new Error;const n=e.stack?e.stack.replace(/^.+\n/,""):"";try{t.stack?n&&!String(t.stack).endsWith(n.replace(/^.+\n.+\n/,""))&&(t.stack+="\n"+n):t.stack=n}catch(t){}}throw t}}_request(t,e){"string"==typeof t?(e=e||{}).url=t:e=t||{},e=mergeConfig(this.defaults,e);const{transitional:n,paramsSerializer:r,headers:i}=e;void 0!==n&&validator.assertOptions(n,{silentJSONParsing:Axios_validators.transitional(Axios_validators.boolean),forcedJSONParsing:Axios_validators.transitional(Axios_validators.boolean),clarifyTimeoutError:Axios_validators.transitional(Axios_validators.boolean)},!1),null!=r&&(utils.isFunction(r)?e.paramsSerializer={serialize:r}:validator.assertOptions(r,{encode:Axios_validators.function,serialize:Axios_validators.function},!0)),void 0!==e.allowAbsoluteUrls||(void 0!==this.defaults.allowAbsoluteUrls?e.allowAbsoluteUrls=this.defaults.allowAbsoluteUrls:e.allowAbsoluteUrls=!0),validator.assertOptions(e,{baseUrl:Axios_validators.spelling("baseURL"),withXsrfToken:Axios_validators.spelling("withXSRFToken")},!0),e.method=(e.method||this.defaults.method||"get").toLowerCase();let o=i&&utils.merge(i.common,i[e.method]);i&&utils.forEach(["delete","get","head","post","put","patch","common"],t=>{delete i[t]}),e.headers=core_AxiosHeaders.concat(o,i);const s=[];let a=!0;this.interceptors.request.forEach(function(t){"function"==typeof t.runWhen&&!1===t.runWhen(e)||(a=a&&t.synchronous,s.unshift(t.fulfilled,t.rejected))});const u=[];let l;this.interceptors.response.forEach(function(t){u.push(t.fulfilled,t.rejected)});let c,d=0;if(!a){const t=[dispatchRequest.bind(this),void 0];for(t.unshift(...s),t.push(...u),c=t.length,l=Promise.resolve(e);d<c;)l=l.then(t[d++],t[d++]);return l}c=s.length;let h=e;for(;d<c;){const t=s[d++],e=s[d++];try{h=t(h)}catch(t){e.call(this,t);break}}try{l=dispatchRequest.call(this,h)}catch(t){return Promise.reject(t)}for(d=0,c=u.length;d<c;)l=l.then(u[d++],u[d++]);return l}getUri(t){return buildURL(buildFullPath((t=mergeConfig(this.defaults,t)).baseURL,t.url,t.allowAbsoluteUrls),t.params,t.paramsSerializer)}}utils.forEach(["delete","get","head","options"],function(t){Axios.prototype[t]=function(e,n){return this.request(mergeConfig(n||{},{method:t,url:e,data:(n||{}).data}))}}),utils.forEach(["post","put","patch"],function(t){function e(e){return function(n,r,i){return this.request(mergeConfig(i||{},{method:t,headers:e?{"Content-Type":"multipart/form-data"}:{},url:n,data:r}))}}Axios.prototype[t]=e(),Axios.prototype[t+"Form"]=e(!0)});const core_Axios=Axios;class CancelToken{constructor(t){if("function"!=typeof t)throw new TypeError("executor must be a function.");let e;this.promise=new Promise(function(t){e=t});const n=this;this.promise.then(t=>{if(!n._listeners)return;let e=n._listeners.length;for(;e-- >0;)n._listeners[e](t);n._listeners=null}),this.promise.then=t=>{let e;const r=new Promise(t=>{n.subscribe(t),e=t}).then(t);return r.cancel=function(){n.unsubscribe(e)},r},t(function(t,r,i){n.reason||(n.reason=new cancel_CanceledError(t,r,i),e(n.reason))})}throwIfRequested(){if(this.reason)throw this.reason}subscribe(t){this.reason?t(this.reason):this._listeners?this._listeners.push(t):this._listeners=[t]}unsubscribe(t){if(!this._listeners)return;const e=this._listeners.indexOf(t);-1!==e&&this._listeners.splice(e,1)}toAbortSignal(){const t=new AbortController,e=e=>{t.abort(e)};return this.subscribe(e),t.signal.unsubscribe=()=>this.unsubscribe(e),t.signal}static source(){let t;return{token:new CancelToken(function(e){t=e}),cancel:t}}}const cancel_CancelToken=CancelToken;function spread(t){return function(e){return t.apply(null,e)}}function isAxiosError(t){return utils.isObject(t)&&!0===t.isAxiosError}const HttpStatusCode={Continue:100,SwitchingProtocols:101,Processing:102,EarlyHints:103,Ok:200,Created:201,Accepted:202,NonAuthoritativeInformation:203,NoContent:204,ResetContent:205,PartialContent:206,MultiStatus:207,AlreadyReported:208,ImUsed:226,MultipleChoices:300,MovedPermanently:301,Found:302,SeeOther:303,NotModified:304,UseProxy:305,Unused:306,TemporaryRedirect:307,PermanentRedirect:308,BadRequest:400,Unauthorized:401,PaymentRequired:402,Forbidden:403,NotFound:404,MethodNotAllowed:405,NotAcceptable:406,ProxyAuthenticationRequired:407,RequestTimeout:408,Conflict:409,Gone:410,LengthRequired:411,PreconditionFailed:412,PayloadTooLarge:413,UriTooLong:414,UnsupportedMediaType:415,RangeNotSatisfiable:416,ExpectationFailed:417,ImATeapot:418,MisdirectedRequest:421,UnprocessableEntity:422,Locked:423,FailedDependency:424,TooEarly:425,UpgradeRequired:426,PreconditionRequired:428,TooManyRequests:429,RequestHeaderFieldsTooLarge:431,UnavailableForLegalReasons:451,InternalServerError:500,NotImplemented:501,BadGateway:502,ServiceUnavailable:503,GatewayTimeout:504,HttpVersionNotSupported:505,VariantAlsoNegotiates:506,InsufficientStorage:507,LoopDetected:508,NotExtended:510,NetworkAuthenticationRequired:511,WebServerIsDown:521,ConnectionTimedOut:522,OriginIsUnreachable:523,TimeoutOccurred:524,SslHandshakeFailed:525,InvalidSslCertificate:526};Object.entries(HttpStatusCode).forEach(([t,e])=>{HttpStatusCode[e]=t});const helpers_HttpStatusCode=HttpStatusCode;function createInstance(t){const e=new core_Axios(t),n=bind(core_Axios.prototype.request,e);return utils.extend(n,core_Axios.prototype,e,{allOwnKeys:!0}),utils.extend(n,e,null,{allOwnKeys:!0}),n.create=function(e){return createInstance(mergeConfig(t,e))},n}const axios=createInstance(lib_defaults);axios.Axios=core_Axios,axios.CanceledError=cancel_CanceledError,axios.CancelToken=cancel_CancelToken,axios.isCancel=isCancel,axios.VERSION=VERSION,axios.toFormData=helpers_toFormData,axios.AxiosError=core_AxiosError,axios.Cancel=axios.CanceledError,axios.all=function(t){return Promise.all(t)},axios.spread=spread,axios.isAxiosError=isAxiosError,axios.mergeConfig=mergeConfig,axios.AxiosHeaders=core_AxiosHeaders,axios.formToJSON=t=>helpers_formDataToJSON(utils.isHTMLForm(t)?new FormData(t):t),axios.getAdapter=adapters.getAdapter,axios.HttpStatusCode=helpers_HttpStatusCode,axios.default=axios;const lib_axios=axios;class LocalStorage{static setItem(t,e){localStorage.setItem(t,e)}static setWithExpiry(t,e,n=33e5){const r={value:e,expiry:Date.now()+n};try{localStorage.setItem(t,JSON.stringify(r))}catch{}}static getWithExpiry(t){const e=localStorage.getItem(t);if(!e)return null;let n=null;try{n=JSON.parse(e)}catch(e){throw localStorage.removeItem(t),e}const r="number"==typeof n.expiry?n.expiry:0;return Date.now()>r?(localStorage.removeItem(t),null):n.value}static removeItem(t){localStorage.removeItem(t)}}const core_localStorage=LocalStorage,REQUEST_ID_HEADER="x-request-id";class Api{_authToken;_requestId=null;apiUrl;_apiKey;_federatedId=null;apiClient;constructor({apiUrl:t,apiKey:e,federatedId:n}){this.apiUrl=t,this._apiKey=e,this._federatedId=n,this._authToken=core_localStorage.getWithExpiry("authToken")||void 0,this.apiClient=lib_axios.create({baseURL:this.apiUrl,headers:{Authorization:`Bearer ${this.authToken}`}}),this.setInterceptors()}async login(){if(!Boolean(core_localStorage.getWithExpiry("authToken")||this.authToken))try{await this.loginWithApiKey()}catch(t){throw console.error("Failed to authenticate with client credentials.",t),core_localStorage.removeItem("authToken"),this._authToken=void 0,t}}setInterceptors(){this.apiClient.interceptors.request.use(async t=>(t.url?.includes("/auth/login")||this.login(),t),t=>Promise.reject(t)),this.apiClient.interceptors.response.use(t=>(this.updatedRequestFromResponse(t),t),t=>(t.response&&this.updatedRequestFromResponse(t.response),Promise.reject(t))),this.apiClient.interceptors.response.use(t=>t,this.handleError.bind(this)),this.apiClient.interceptors.response.use(t=>t,t=>{if(t.response)try{return JSON.stringify(t.response.data),lib_axios.isAxiosError(t)&&401===t.response?.status&&(core_localStorage.removeItem("authToken"),this._authToken=void 0),Promise.reject(t)}catch(t){return console.error(t),Promise.reject(t)}return Promise.reject(t)})}async verifyAuthentication(){await this.getMe()}updatedRequestFromResponse(t){return this.updateRequestId(t.headers[REQUEST_ID_HEADER]??""),t}handleError(t){return Promise.reject(t)}get authToken(){return this._authToken}set authToken(t){t?core_localStorage.setWithExpiry("authToken",t):core_localStorage.removeItem("authToken"),this._authToken=t,this.apiClient&&(this.apiClient.defaults.headers.Authorization=`Bearer ${t}`)}get requestId(){return this._requestId}updateRequestId(t){t&&(this._requestId=t)}async loginWithApiKey(){const t=await this.apiClient.post("/auth/login",{api_key:this._apiKey,federated_id:this._federatedId});return this.authToken=t.data.access_token,this.apiClient.defaults.headers.common.Authorization=`Bearer ${this.authToken}`,this.authToken||""}async getMe(){return(await this.apiClient.get("/users/me")).data}async generateImage(t){const{provider:e,...n}=t,r=`/images/generate/${e}`;return(await this.apiClient.post(r,n)).data}async generateImageWithBria(t){return(await this.apiClient.post("/images/generate/bria",t)).data}async generateImageWithReplicate(t){return(await this.apiClient.post("/images/generate/replicate",t)).data}async getAvailableModels(){return(await this.apiClient.get("/images/models")).data}}const api=Api,audioConfig={sampling_rate:48e3,mime_type:"audio/mpeg"};class ConversationNetwork extends EventEmitter{config;wsConnection=null;_api=null;pendingRequests=new Map;_isInitialized=!1;_sendInitialPing=!0;_didSendInitialPing=!1;_sessionReadyResolve=null;_sessionReadyReject=null;_sessionReadyPromise=null;constructor(t){super(new DefaultLogger({category:"🗣️ConversationNetwork",style:StyleGrey})),this.config=t,this._api=new api({apiUrl:t.apiUrl,apiKey:t.apiKey,federatedId:t.federatedId})}async initialize(){if(!this._isInitialized)try{await this._api.login(),this._isInitialized=!0}catch(t){const e=t.response?.data?.error||t.message||"Authentication failed",n=t.response?.data?.traceback;n?this.logger.error("Authentication failed:",e,"\nTraceback:",n):this.logger.error("Authentication failed:",e);const r=new Error(e);throw n&&(r.traceback=n),r}}async connect(t=!0){if(this.isReady())return;if(this._sessionReadyPromise)return void await this._sessionReadyPromise;this._sendInitialPing=t,this._sessionReadyPromise=new Promise((t,e)=>{this._sessionReadyResolve=t,this._sessionReadyReject=e});let e=this.config.apiUrl.replace(/\/api\/?$/,"")+"/interact";this.wsConnection=new WebSocketConnection(e,this.getWebSocketHandlers()),this.wsConnection.connect(),await this.waitForSessionReady(1e4),this.logger.debug("Network connections established - Session ready")}async waitForSessionReady(t){let e;const n=new Promise((n,r)=>{e=setTimeout(()=>r(new Error(`WebSocket session timeout after ${t}ms`)),t)});try{await Promise.race([this._sessionReadyPromise,n]),clearTimeout(e)}catch(t){throw clearTimeout(e),this._sessionReadyPromise=null,this._sessionReadyResolve=null,this._sessionReadyReject=null,t}}getWebSocketHandlers(){return{onOpen:async()=>{try{await this.authenticate(),await this.setConfiguration(),this.emit(ConversationNetworkEvents.Connected),this._sessionReadyResolve?.()}catch(t){const e=t?.error||String(t);return this._sessionReadyReject?.(new Error(e)),this.emit(ConversationNetworkEvents.Error,new Error(e)),void this.wsConnection?.disconnect()}if(this._sendInitialPing){this._didSendInitialPing=!0;try{await this.interact({text:".",uid:"",kind:"interact",type:"stream"})}catch(t){const e=t?.error||String(t);this.emit(ConversationNetworkEvents.Error,new Error(e))}}},onMessage:async t=>{if(t.uid&&this.pendingRequests.has(t.uid)){const e=this.pendingRequests.get(t.uid);if(e)if(e.isStream)"close"===t.kind?(clearTimeout(e.timeout),e.resolve(t),this.pendingRequests.delete(t.uid)):"error"===t.kind?(this.logger.error(t.error),this.emit(ConversationNetworkEvents.Error,new Error(t.error))):this.emit(ConversationNetworkEvents.Message,t);else{if(clearTimeout(e.timeout),"error"===t.kind){const n=t;this.logger.error(n?.error),e.reject(n.error)}else e.resolve(t);this.pendingRequests.delete(t.uid),this.emit(ConversationNetworkEvents.Message,t)}}else"interact"===t.kind?this.emit(ConversationNetworkEvents.Message,t):"error"===t.kind?(this.logger.error(t.error),await this.emit(ConversationNetworkEvents.Error,t.error)):this.logger.warn("Received message without a matching request UID",t)},onError:async t=>{await this.emit(ConversationNetworkEvents.Error,t)},onClose:async()=>{await this.emit(ConversationNetworkEvents.Disconnected)}}}async makeRequest(t,e=5e4){return this.makeRequestInternal(t,e,!1)}async makeStreamRequest(t,e=5e4){return this.makeRequestInternal(t,e,!0)}async makeRequestInternal(t,e,n){if(!this.wsConnection?.isReady())throw new Error("WebSocket not ready");t.uid=dist_v4(),t.client_start_time=(new Date).toISOString();const r=new Promise((r,i)=>{const o=setTimeout(()=>{this.pendingRequests.delete(t.uid),i(new Error(`Request ${t.uid} timed out after ${e}ms`))},e);this.pendingRequests.set(t.uid,{resolve:r,reject:i,timeout:o,isStream:n})});return this.wsConnection.send(t),r}async authenticate(){const t={type:"request",kind:"authenticate",access_token:this._api.authToken,uid:"",metadata:this.config.metadata};await this.makeRequest(t)}updateConfig(t){this.config=t}async updateRemoteConfiguration(t){this.updateConfig(t),await this.setConfiguration()}async setConfiguration(){const t=void 0!==this.config.configRef&&""!==this.config.configRef,e=t?{reference:this.config.configRef}:{prompt:this.config.prompt,utilities:this.config.utilities,voice_profile:this.config.voiceProfile,safety_policy:this.config.safetyPolicy};this.logger.debug("setConfiguration",t?"using reference":"using inline config",e);const n={type:"request",kind:"set_configuration",config:e,uid:""};await this.makeRequest(n)}async addAudio(t){const e={type:"request",kind:"add_audio",audio:t,uid:"",config:audioConfig};await this.makeRequest(e)}async clearAudio(){await this.makeRequest({type:"request",kind:"clear_audio",uid:""})}async checkTurn(){await this.makeRequest({type:"request",kind:"check_turn",uid:""})}async generateImage(t){return{kind:"generate_image",uid:"",image:(await this._api.generateImage(t)).image}}async generateImageWithBria(t){return{kind:"generate_image",uid:"",image:(await this._api.generateImageWithBria(t)).image}}async generateImageWithReplicate(t){return{kind:"generate_image",uid:"",image:(await this._api.generateImageWithReplicate(t)).image}}async getAvailableModels(){return{kind:"get_available_models",uid:"",models:(await this._api.getAvailableModels()).models}}async interact(t){t.context={...this.config.context,...t.context},t.audio_output=this.config.capabilities?.audio??!0,t.kind=t.kind??"interact",t.type=t.type??"stream",await this.makeStreamRequest(t)}async interactAudio(t){t.context={...this.config.context,...t.context},t.audio_output=this.config.capabilities?.audio??!0,t.kind=t.kind??"interact",t.type=t.type??"stream",await this.makeStreamRequest(t)}async disconnect(){this._sessionReadyPromise=null,this._sessionReadyResolve=null,this._sessionReadyReject=null,this._didSendInitialPing=!1,this.wsConnection?.disconnect()}isReady(){return this.wsConnection?.isReady()||!1}didSendInitialPing(){return this._didSendInitialPing}async send(t){"audio"!==t.type?this.logger.warn("ConversationNetwork.send is deprecated. Use specific methods instead."):await this.addAudio(t.audio)}}class MediaRecorderFallback{mediaRecorder=null;mimeType;onAudioData;timeslice;logger=new DefaultLogger({category:"🎤 MediaRecorderFallback",style:StyleBrown});constructor(t,e,n=100){this.mimeType=t,this.onAudioData=e,this.timeslice=n}initialize(t){this.logger.debug("Initializing MediaRecorder for audio processing"),this.mediaRecorder=new MediaRecorder(t,{mimeType:this.mimeType}),this.logger.debug("MediaRecorder created, state:",this.mediaRecorder.state),this.mediaRecorder.ondataavailable=t=>{this.logger.debug("ondataavailable called, size:",t.data.size),t.data.size>0&&t.data.arrayBuffer().then(t=>{this.onAudioData(t)})}}start(){this.mediaRecorder&&"inactive"===this.mediaRecorder.state&&(this.mediaRecorder.start(this.timeslice),this.logger.debug(`MediaRecorder started with ${this.timeslice}ms timeslice`))}stop(){this.mediaRecorder&&"inactive"!==this.mediaRecorder.state&&(this.mediaRecorder.stop(),this.logger.debug("MediaRecorder stopped"))}}const AudioRecorderEvents={RecordingStarted:"recording-started",RecordingStopped:"recording-stopped",AudioData:"audio-data",Error:"error"};class AudioRecorder extends EventEmitter{mediaStream=null;audioContext=null;workletNode=null;mediaRecorder=null;recording=!1;config;useMediaRecorder=!1;mediaRecorderFallback=null;audioBuffer=[];bufferingMode=!1;MIME_TYPE="audio/webm;codecs=opus";constructor(t={}){super(new DefaultLogger({category:"🎤 AudioRecorder",style:StyleBrown})),this.config={sampleRate:41e3,channels:1,bitDepth:16,echoCancellation:!0,noiseSuppression:!0,autoGainControl:!0,timeslice:100,...t}}async initialize(t){try{this.logger.debug("Initializing..."),await this.setupAudioContext(),await this.loadAudioWorklet(),this.mediaStream=t,this.useMediaRecorder&&(this.mediaRecorderFallback=new MediaRecorderFallback(this.MIME_TYPE,t=>this.handleAudioData(t),this.config.timeslice),this.mediaRecorderFallback.initialize(this.mediaStream)),this.logger.debug("Initialized successfully")}catch(t){throw this.logger.error("Failed to initialize",t),t}}async start(){if(this.recording)this.logger.warn("Recording already in progress");else{if(this.audioContext&&"suspended"===this.audioContext.state)try{await this.audioContext.resume(),this.logger.debug("AudioContext resumed successfully at start of recording.")}catch(t){this.logger.error("Failed to resume AudioContext:",t);const e=new Error("The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page. https://developer.chrome.com/blog/autoplay/#web_audio");throw await this.emit(AudioRecorderEvents.Error,e),e}this.audioContext||(await this.setupAudioContext(),await this.loadAudioWorklet());try{if(this.audioContext&&"suspended"===this.audioContext.state&&(this.logger.debug("Resuming AudioContext before starting recording..."),await this.audioContext.resume()),!this.mediaStream)throw new Error("AudioRecorder: mediaStream must be set via initialize(stream) before starting recording.");this.logger.debug("setupMediaStream complete"),await this.setupAudioProcessing(),this.logger.debug("setupAudioProcessing complete"),this.recording=!0,await this.emit(AudioRecorderEvents.RecordingStarted),this.logger.debug("Audio recording started")}catch(t){throw this.logger.error("Failed to start recording",t),await this.emit(AudioRecorderEvents.Error,t),t}}}async stop(){if(this.logger.debug("Stopping recording..."),this.recording)try{await this.cleanup(),this.recording=!1,await this.emit(AudioRecorderEvents.RecordingStopped),this.logger.debug("Audio recording stopped")}catch(t){throw this.logger.error("Failed to stop recording",t),await this.emit(AudioRecorderEvents.Error,t),t}else this.logger.warn("No recording in progress")}isRecording(){return this.recording}enableBufferingMode(){this.logger.debug("Buffering mode enabled"),this.bufferingMode=!0,this.audioBuffer=[]}disableBufferingMode(){this.logger.debug("Buffering mode disabled"),this.bufferingMode=!1}getBufferedAudio(){return[...this.audioBuffer]}clearBuffer(){const t=this.audioBuffer.length;this.logger.debug(`Audio buffer cleared (Was ${t})`),this.audioBuffer=[]}async resumeAudioContext(){this.audioContext&&"suspended"===this.audioContext.state&&(this.logger.debug("Resuming AudioContext..."),await this.audioContext.resume(),this.logger.debug("AudioContext resumed, state:",this.audioContext.state))}async setupMediaRecorder(){this.logger.debug("Setting up MediaRecorder fallback..."),this.useMediaRecorder=!0}async setupAudioContext(){this.audioContext&&"closed"!==this.audioContext.state||(this.audioContext=new AudioContext({sampleRate:this.config.sampleRate,latencyHint:"interactive"}),this.logger.debug("AudioContext created, state:",this.audioContext.state)),"suspended"===this.audioContext.state&&(this.logger.debug("Resuming suspended AudioContext..."),await this.audioContext.resume(),this.logger.debug("AudioContext resumed, new state:",this.audioContext.state))}async loadAudioWorklet(){if(!this.audioContext)throw new Error("AudioContext not initialized");return this.setupMediaRecorder()}async setupAudioProcessing(){if(this.logger.debug("setupAudioProcessing called, useMediaRecorder:",this.useMediaRecorder),!this.audioContext||!this.mediaStream)throw new Error("AudioContext or MediaStream not initialized");if(this.useMediaRecorder)this.logger.debug("Using MediaRecorder fallback branch"),this.mediaRecorderFallback&&(this.logger.debug("Stopping existing MediaRecorderFallback before creating new one"),this.mediaRecorderFallback.stop()),this.mediaRecorderFallback=new MediaRecorderFallback(this.MIME_TYPE,t=>this.handleAudioData(t),this.config.timeslice),this.mediaRecorderFallback.initialize(this.mediaStream),this.mediaRecorderFallback.start();else{this.logger.debug("Using AudioWorklet for audio processing");const t=this.audioContext.createMediaStreamSource(this.mediaStream);this.workletNode=new AudioWorkletNode(this.audioContext,"audio-recording-processor",{processorOptions:{sampleRate:this.config.sampleRate,channels:this.config.channels,bitDepth:this.config.bitDepth}}),t.connect(this.workletNode),this.workletNode.port.onmessage=t=>{const{type:e,data:n}=t.data;e===AudioRecorderEvents.AudioData&&this.handleAudioData(n)}}}async handleAudioData(t){let e=new Uint8Array(t),n=e.findIndex(t=>0!==t);if(-1===n)return void this.logger.warn("Skipping all-zero audio packet");n>0&&(this.logger.debug(`Trimming ${n} leading zero bytes from audio packet`),e=e.slice(n),t=e.buffer);const r=Array.from(e.slice(0,8));this.logger.debug(`Audio chunk: size=${t.byteLength}, first bytes=${r.join(",")}`),this.bufferingMode?(this.logger.debug(`Buffered audio chunk, buffer size: ${this.audioBuffer.length+1}`),this.audioBuffer.push(t)):await this.emit(AudioRecorderEvents.AudioData,t)}async cleanup(){this.workletNode&&(this.workletNode.disconnect(),this.workletNode=null),this.mediaRecorder&&(this.mediaRecorder.stop(),this.mediaRecorder=null),this.mediaRecorderFallback&&(this.mediaRecorderFallback.stop(),this.mediaRecorderFallback=null),this.audioBuffer=[],this.bufferingMode=!1,this.logger.debug("Audio buffer cleared during cleanup")}async preInitialize(){try{await this.setupAudioContext(),await this.loadAudioWorklet(),this.logger.debug("Pre-initialization complete")}catch(t){this.logger.error("Pre-initialization failed",t)}}isInitialized(){return null!==this.mediaStream}dispose(){try{this.logger.debug("Disposing AudioRecorder..."),this.recording&&this.stop(),this.cleanup(),this.audioContext&&"closed"!==this.audioContext.state&&(this.audioContext.close().catch(t=>{this.logger.debug("Error closing AudioContext:",t)}),this.audioContext=null),this.mediaStream=null,this.removeAllListeners(),this.logger.debug("AudioRecorder disposed")}catch(t){this.logger.warn("Error while disposing AudioRecorder",t)}}}class UserInputManager extends EventEmitter{capabilities;vadManager;network;recordingConfig;audioRecorder=null;isStopped=!0;mediaStream=null;isInputCompleteSent=!1;debugMode=!1;capturedAudioChunks=[];constructor(t,e,n,r){super(new DefaultLogger({category:"🎩 UserInputManager",style:StyleBlue})),this.capabilities=t,this.vadManager=e,this.network=n,this.recordingConfig=r,this.setupListeners(),this.capabilities.audio&&(this.audioRecorder=new AudioRecorder(r),this.setupRecorderListeners())}async initialize(t){await this.reinitializeAudio(t)}async reinitializeAudio(t){if(this.capabilities.audio){if(t&&(this.mediaStream=t),!this.mediaStream){const t={echoCancellation:this.recordingConfig?.echoCancellation,noiseSuppression:this.recordingConfig?.noiseSuppression,autoGainControl:this.recordingConfig?.autoGainControl,sampleRate:this.recordingConfig?.sampleRate,channelCount:this.recordingConfig?.channels};Object.keys(t).forEach(e=>void 0===t[e]&&delete t[e]),this.mediaStream=await navigator.mediaDevices.getUserMedia({audio:t})}await this.vadManager.initialize(this.mediaStream),await(this.audioRecorder?.initialize(this.mediaStream))}}async start(t=!1){if(this.isStopped){this.isStopped=!1;try{await this.network.clearAudio(),this.logger.debug("Server audio buffer cleared for new session")}catch(t){this.logger.error("Failed to clear server audio buffer",t)}this.debugMode&&(this.capturedAudioChunks=[],this.logger.debug("Cleared captured audio chunks for new interaction")),this.capabilities.audio&&(t?this.logger.debug("Skipping VAD startup to reduce CPU contention during playback"):this.vadManager.startAnalysis(),await this.startRecording())}}startVAD(){this.capabilities.audio&&!this.isStopped&&(this.logger.debug("Starting VAD analysis"),this.vadManager.startAnalysis())}async stop(){return!this.isStopped&&(this.logger.debug("Stopping User Input..."),this.isStopped=!0,this.capabilities.audio&&(this.vadManager.stopAnalysis(),await this.stopRecording()),!0)}async updateCapabilities(t){this.capabilities=t,this.logger.debug("Input capabilities updated",this.capabilities),this.capabilities.audio&&!this.audioRecorder?(this.audioRecorder=new AudioRecorder(this.recordingConfig),this.setupRecorderListeners(),await this.reinitializeAudio()):!this.capabilities.audio&&this.audioRecorder&&(this.vadManager.stopAnalysis(),await this.audioRecorder.stop(),this.audioRecorder=null,this.mediaStream&&(this.mediaStream.getTracks().forEach(t=>t.stop()),this.mediaStream=null))}async stopRecording(){this.capabilities.audio&&this.audioRecorder?.isRecording()&&await this.audioRecorder.stop()}async startRecording(){this.capabilities.audio&&this.audioRecorder?(this.audioRecorder.isInitialized()||(this.logger.warn("AudioRecorder not initialized, reinitializing..."),await this.reinitializeAudio()),await this.audioRecorder.start()):this.logger.info("Will not start recording, audio capabilities are not enabled or audio recorder is not initialized")}isRecording(){return this.capabilities.audio&&(this.audioRecorder?.isRecording()||!1)}enableAudioBuffering(){this.capabilities.audio&&this.audioRecorder&&this.audioRecorder.enableBufferingMode()}disableAudioBuffering(){this.capabilities.audio&&this.audioRecorder&&this.audioRecorder.disableBufferingMode()}getBufferedAudio(){return this.audioRecorder?.getBufferedAudio()||[]}clearAudioBuffer(){this.capabilities.audio&&this.audioRecorder&&this.audioRecorder.clearBuffer()}async flushBufferedAudio(){this.disableAudioBuffering();const t=this.getBufferedAudio();if(this.clearAudioBuffer(),t.length>0){this.logger.debug(`Flushing ${t.length} buffered audio chunks`);for(const e of t)await this.sendAudio(e)}}async sendAudio(t){if(this.isStopped)return;this.logger.debug("sendAudio called, size:",t.byteLength),this.debugMode&&(this.capturedAudioChunks.push(t),this.logger.debug(`Captured audio chunk for debug (${this.capturedAudioChunks.length} total chunks)`));const e=this.arrayBufferToBase64(t);await this.network.addAudio(e)}arrayBufferToBase64(t){let e="";const n=new Uint8Array(t),r=n.byteLength;for(let t=0;t<r;t++)e+=String.fromCharCode(n[t]);return btoa(e)}async interact(t){await this.network.interact(t)}async sendInputComplete(){this.isStopped||this.isInputCompleteSent||(this.isInputCompleteSent=!0,await this.emit(UserInputManagerEvents.UserInput,{type:"input_complete"}))}reset(){this.isInputCompleteSent=!1,this.debugMode&&this.capturedAudioChunks.length>0&&(this.logger.debug(`Clearing ${this.capturedAudioChunks.length} captured audio chunks on reset`),this.capturedAudioChunks=[])}dispose(){try{this.logger.debug("Disposing UserInputManager..."),this.isStopped=!0,this.vadManager&&(this.vadManager.stopAnalysis(),"function"==typeof this.vadManager.dispose&&this.vadManager.dispose()),this.capabilities.audio&&this.audioRecorder?.isRecording()&&this.audioRecorder.stop(),this.audioRecorder&&"function"==typeof this.audioRecorder.dispose&&this.audioRecorder.dispose(),this.mediaStream&&(this.mediaStream.getTracks().forEach(t=>{t.stop(),this.logger.debug(`Stopped media track: ${t.kind}`)}),this.mediaStream=null),this.removeAllListeners(),this.logger.debug("UserInputManager disposed")}catch(t){this.logger.warn("Error while disposing UserInputManager",t)}}setupListeners(){this.capabilities.audio&&(this.vadManager.on(types_VadManagerEvents.VoiceActivity,async t=>{t&&!this.isStopped&&(t.isSpeaking?await this.emit(UserInputManagerEvents.UserSpeaking):await this.emit(UserInputManagerEvents.UserSilence))}),this.vadManager.on(types_VadManagerEvents.Silence,async()=>{await this.sendInputComplete(),await this.network.checkTurn()}))}setupRecorderListeners(){this.audioRecorder&&this.audioRecorder.on(AudioRecorderEvents.AudioData,t=>{this.sendAudio(t).catch(t=>{this.logger.error("Failed to send audio chunk",t)})})}async resumeAudioContext(){this.capabilities.audio&&this.audioRecorder&&await this.audioRecorder.resumeAudioContext()}enableDebugMode(){this.debugMode=!0,this.capturedAudioChunks=[],this.logger.info("Debug mode enabled - audio chunks will be captured")}disableDebugMode(){this.debugMode=!1,this.logger.info("Debug mode disabled")}clearCapturedAudio(){this.capturedAudioChunks=[],this.logger.info("Captured audio chunks cleared")}downloadCapturedAudio(t="captured-audio.webm"){if(0===this.capturedAudioChunks.length)return void this.logger.warn("No audio chunks captured to download");this.logger.info(`Downloading ${this.capturedAudioChunks.length} captured audio chunks as ${t}`);const e=new Blob(this.capturedAudioChunks,{type:"audio/webm;codecs=opus"}),n=URL.createObjectURL(e),r=document.createElement("a");r.href=n,r.download=t,document.body.appendChild(r),r.click(),document.body.removeChild(r),URL.revokeObjectURL(n),this.logger.info("Download completed")}getCapturedAudioInfo(){const t=this.capturedAudioChunks.reduce((t,e)=>t+e.byteLength,0);return{chunkCount:this.capturedAudioChunks.length,totalSize:t}}}class ConversationCommands{static DEFAULT_START_COMMAND="[start conversation]";static DEFAULT_PAUSE_RESUME_COMMAND="[resume after short pause]";static DEFAULT_LONG_PAUSE_RESUME_COMMAND="[resume after long break]";static DEFAULT_ERROR_RESUME_COMMAND="[resume after error]";startCommand=ConversationCommands.DEFAULT_START_COMMAND;pauseResumeCommand=ConversationCommands.DEFAULT_PAUSE_RESUME_COMMAND;longPauseResumeCommand=ConversationCommands.DEFAULT_LONG_PAUSE_RESUME_COMMAND;errorResumeCommand=ConversationCommands.DEFAULT_ERROR_RESUME_COMMAND;set(t,e,n,r){this.startCommand=t??ConversationCommands.DEFAULT_START_COMMAND,this.pauseResumeCommand=e??ConversationCommands.DEFAULT_PAUSE_RESUME_COMMAND,this.longPauseResumeCommand=n??ConversationCommands.DEFAULT_LONG_PAUSE_RESUME_COMMAND,this.errorResumeCommand=r??ConversationCommands.DEFAULT_ERROR_RESUME_COMMAND}setDefault(){this.startCommand=ConversationCommands.DEFAULT_START_COMMAND,this.pauseResumeCommand=ConversationCommands.DEFAULT_PAUSE_RESUME_COMMAND,this.longPauseResumeCommand=ConversationCommands.DEFAULT_LONG_PAUSE_RESUME_COMMAND,this.errorResumeCommand=ConversationCommands.DEFAULT_ERROR_RESUME_COMMAND}}class ErrorRetryHandler{deps;static DEFAULT_MAX_RETRY_COUNT=3;currentRetryCount=0;isRetrying=!1;maxRetryCount;constructor(t){this.deps=t,this.maxRetryCount=t.maxRetryCount??ErrorRetryHandler.DEFAULT_MAX_RETRY_COUNT}async attemptRecovery(t){if(this.deps.logger.info("Attempting silent recovery for error type: "+t),!["network_timeout","network_error","server_error"].includes(t))return!1;if(!this.deps.isConnected())return this.deps.logger.info("Network is not connected, skipping retry"),!1;if(this.currentRetryCount>=this.maxRetryCount)return!1;if(this.isRetrying)return!1;this.currentRetryCount++,this.isRetrying=!0,this.deps.logger.info(`Attempting silent retry ${this.currentRetryCount}/${this.maxRetryCount} with error resume command`);try{return await this.deps.clearAudio(),await this.deps.sendText(this.deps.conversationCommands.errorResumeCommand),this.isRetrying=!1,!0}catch(e){return this.deps.logger.error(`Retry attempt ${this.currentRetryCount} failed`,e),this.isRetrying=!1,this.currentRetryCount<this.maxRetryCount&&await this.attemptRecovery(t)}}resetRetryCount(){this.currentRetryCount=0,this.isRetrying=!1}getCurrentRetryCount(){return this.currentRetryCount}}const ConversationManagerEvents={StateChange:"stateChange"};class ConversationManager extends EventEmitter{state="idle";network;vadManager;userInputManager;playbackManager;config;interactionCompletePending=!1;mediaStream=null;isTextOnly=!1;isDisposed=!1;isTransitioning=!1;pausedFromState=null;errorRetryHandler;isSimulateErrorsDebugMode=!1;conversationCommands=new ConversationCommands;constructor(t){const e=new DefaultLogger({category:"🧞 ConversationManager",style:StylePurple});super(e),this.setConfiguration(t);const n={...t,...t.apiKey&&{apiKey:t.apiKey.slice(0,4)+"..."},...t.federatedId&&{federatedId:t.federatedId.slice(0,4)+"..."}};e.info("Initializing with config",n),this.network=new ConversationNetwork(this.config),this.vadManager=new VADManager,this.userInputManager=new UserInputManager(this.config.inputCapabilities,this.vadManager,this.network,t.recordingConfig),this.playbackManager=new PlaybackManager(this.config.capabilities),this.playbackManager.wireVADToAvatar(this),this.playbackManager.wireConversationStateToAvatar(this),this.setupEventListeners(),this.errorRetryHandler=new ErrorRetryHandler({logger:this.logger,conversationCommands:this.conversationCommands,sendText:t=>this.sendText(t),clearAudio:async()=>{this.userInputManager.clearAudioBuffer(),await this.network.clearAudio()},isConnected:()=>this.isConnected(),maxRetryCount:3}),this.setState("uninitialized")}async setConfiguration(t){const e=t.languageCode||t.voiceProfile?.deepdub_locale?.split("-")[0];if(this.config={...t,languageCode:e,capabilities:t.capabilities||{audio:!0,viseme:!0,subtitles:!0,avatar:!0},inputCapabilities:t.inputCapabilities||{audio:!0,text:!0},hooks:{...this.config?.hooks||{},...t.hooks},onInputUtilities:t.onInputUtilities??this.config?.onInputUtilities,onInputNonBlockingUtilities:t.onInputNonBlockingUtilities??this.config?.onInputNonBlockingUtilities,onOutputUtilities:t.onOutputUtilities??this.config?.onOutputUtilities},this.network&&(this.network.updateConfig(this.config),this.network.isReady())){this.logger.debug("Updating remote configuration");try{await this.network.updateRemoteConfiguration(this.config),this.logger.debug("Remote configuration updated successfully")}catch(t){this.logger.error("Failed to update remote configuration",t)}}}async initialize(){try{if(this.logger.debug("Initializing..."),this.setState("initializing"),await this.network.initialize(),this.isDisposed)return;if(await this.playbackManager.initialize(),this.isDisposed)return;if(this.config.inputCapabilities?.audio&&!this.mediaStream){const t=this.config.recordingConfig,e={echoCancellation:t?.echoCancellation,noiseSuppression:t?.noiseSuppression,autoGainControl:t?.autoGainControl,sampleRate:t?.sampleRate,channelCount:t?.channels};Object.keys(e).forEach(t=>void 0===e[t]&&delete e[t]),this.mediaStream=await navigator.mediaDevices.getUserMedia({audio:e})}if(this.isDisposed)return;if(await this.userInputManager.initialize(this.mediaStream??void 0),this.isDisposed)return;await this.network.connect(),this.logger.debug("Initialized successfully"),this.setState(this.network.didSendInitialPing()?"waiting":"idle")}catch(t){if(this.isDisposed)return;this.handleError("server_error",t)}}async prepareConversation(){if(this.isDisposed)return!1;try{return await this.network.initialize(),!this.isDisposed&&(await this.network.connect(!1),!0)}catch(t){return this.logger.warn("prepareConversation failed:",t),!1}}async startListening(){if("idle"!==this.state)throw new Error(`Cannot start listening from state: ${this.state}`);try{await this.userInputManager.start(),this.setState("listening")}catch(t){this.handleError("mic_denied",t)}}async stopListening(){this.logger.debug("stopListening called from state:",this.state),await this.userInputManager.stop(),await this.setState("waiting")}async sendText(t){return this.interact({text:t,kind:"interact",type:"stream",uid:"",language_code:this.config.languageCode})}async generateImage(t){return this.network.generateImage(t)}async generateImageWithBria(t){return this.network.generateImageWithBria(t)}async generateImageWithReplicate(t){return this.network.generateImageWithReplicate(t)}async getAvailableModels(){return this.network.getAvailableModels()}async interact(t){if(!this.network.isReady()){const t="Network is not ready, cannot send text. probably disconnected";return this.logger.error(t),void await this.handleError("network_timeout",new Error(t))}if("idle"===this.state||"listening"===this.state||"processing_complete"===this.state||"error"===this.state)try{if(this.playbackManager.reset(),this.isSimulateErrorsDebugMode&&Math.random()<.5)throw this.logger.error("DebugMode:Simulating server error"),new Error("server_error");await this.setState("waiting"),await this.userInputManager.interact(t)}catch(t){this.logger.error("Error sending text message",t),await this.handleError("network_timeout",t)}}async interrupt(){this.playbackManager.pause(),await this.setState("interrupted")}async pause(){if(this.isTransitioning)this.logger.debug("Pause ignored: transition already in progress");else if("playing"===this.state||"processing_complete"===this.state||"listening"===this.state||"userSpeaking"===this.state){this.isTransitioning=!0;try{if(this.pausedFromState=this.state,"listening"===this.state||"userSpeaking"===this.state){this.logger.debug("Pausing from listening/speaking state, stopping input and clearing audio"),await this.userInputManager.stop(),this.userInputManager.clearAudioBuffer();try{await this.network.clearAudio()}catch(t){this.logger.warn("Failed to clear server audio buffer on pause",t)}}else this.playbackManager.pause();await this.setState("paused")}finally{this.isTransitioning=!1}}else this.logger.warn(`Cannot pause from state: ${this.state}`)}async resume(){if(this.isTransitioning)this.logger.debug("Resume ignored: transition already in progress");else if("paused"===this.state||"processing_complete"===this.state){this.isTransitioning=!0;try{const t=this.playbackManager.getRemainingAudioSeconds()>0,e="listening"===this.pausedFromState||"userSpeaking"===this.pausedFromState;if(t){this.logger.debug("Resuming playback with buffered audio");try{await this.playbackManager.resume(),await this.setState("playing")}catch(t){this.logger.error("Failed to resume playback, falling back to listening",t),await this.userInputManager.start(),await this.setState("listening")}}else if(e){this.logger.debug("Resuming from listening pause, restarting recording");try{await this.userInputManager.start(),await this.setState("listening")}catch(t){this.logger.error("Failed to restart recording after pause",t),await this.handleError("mic_denied",t)}}else this.logger.debug("No buffered audio to resume, going to listening state"),this.interactionCompletePending?(this.interactionCompletePending=!1,await this.handleInteractionComplete()):await this.setState("listening")}finally{this.pausedFromState=null,this.isTransitioning=!1}}else this.logger.warn(`Cannot resume from state: ${this.state}`)}async forceInputComplete(){"userSpeaking"===this.state||"listening"===this.state?await this.userInputManager.sendInputComplete():this.logger.warn(`Cannot force input complete from state: ${this.state}`)}async stop(){await this.userInputManager.stop(),this.playbackManager.pause(),await this.network.disconnect(),await this.setState("idle")}async dispose(){if(this.isDisposed)this.logger.warn("ConversationManager already disposed");else{this.logger.debug("Disposing ConversationManager..."),this.isDisposed=!0,this.userInputManager?.removeAllListeners(),this.playbackManager?.removeAllListeners(),this.network?.removeAllListeners(),this.state="idle";try{this.userInputManager&&"function"==typeof this.userInputManager.dispose?this.userInputManager.dispose():(await this.userInputManager.stop(),this.mediaStream&&(this.mediaStream.getTracks().forEach(t=>{t.stop(),this.logger.debug(`Stopped media track: ${t.kind}`)}),this.mediaStream=null)),this.playbackManager&&"function"==typeof this.playbackManager.dispose&&this.playbackManager.dispose(),await this.network.disconnect(),this.removeAllListeners(),this.logger.debug("ConversationManager disposed successfully")}catch(t){this.logger.warn("Error while disposing ConversationManager",t)}}}async setState(t){if(this.isDisposed)return;if(this.state===t)return;const e=this.state;this.state=t,this.config.hooks.onStateChange?.(t),await this.emit(ConversationManagerEvents.StateChange,{oldState:e,newState:t}),this.logger.debug(`State transition: ${e} -> ${t}`)}setupEventListeners(){this.userInputManager.on(UserInputManagerEvents.UserInput,async t=>{await this.network.send(t),"input_complete"===t.type&&await this.setState("waiting")}),this.playbackManager.on(PlaybackManagerEvents.SubtitleWordChange,t=>{this.config.hooks.onSubtitleHighlight?.(t)}),this.playbackManager.on(PlaybackManagerEvents.SubtitleChange,t=>{this.config.hooks.onSubtitleChange?.(t)}),this.playbackManager.on(PlaybackManagerEvents.ImageChange,t=>{this.config.hooks.onImageChange?.(t)}),this.playbackManager.on(PlaybackManagerEvents.AvatarAnimationChanged,t=>{this.config.hooks.onAvatarAnimationChanged?.(t)}),this.userInputManager.on(UserInputManagerEvents.UserSpeaking,async()=>{"listening"===this.state?await this.setState("userSpeaking"):this.logger.debug(`VAD UserSpeaking ignored in state: ${this.state}`)}),this.userInputManager.on(UserInputManagerEvents.UserSilence,async()=>{"userSpeaking"===this.state?await this.setState("listening"):this.logger.debug(`VAD UserSilence ignored in state: ${this.state}`)}),this.network.on(ConversationNetworkEvents.Connected,async()=>{this.config.hooks.onNetworkStatusChange?.(!0)}),this.network.on(ConversationNetworkEvents.Disconnected,async()=>{this.config.hooks.onNetworkStatusChange?.(!1)}),this.network.on(ConversationNetworkEvents.Message,async t=>{if("interact"===t.kind&&"text"===t.event&&this.config.hooks.onTextMessage?.(t),"data"===t.event&&this.config.hooks.onDataMessage?.(t),"interact"===t.kind&&"safety_policy"===t.event&&this.config.hooks.onSafetyEvent?.(t),"check_turn"===t.kind&&!1===t.is_user_still_speaking){if(this.logger.debug(`check_turn handler: state is ${this.state}`),"playing"===this.state||"paused"===this.state)return void this.logger.debug("check_turn received while playing / paused, ignoring.");await this.stopListening();try{if(this.isSimulateErrorsDebugMode&&Math.random()<.5)throw this.logger.error("DebugMode:Simulating server error"),new Error("server_error");await this.network.interactAudio({uid:"",kind:"interact",type:"stream",on_input_non_blocking:this.config.onInputNonBlockingUtilities,on_input:this.config.onInputUtilities,on_output:this.config.onOutputUtilities,language_code:this.config.languageCode})}catch(t){this.userInputManager.clearAudioBuffer();try{await this.network.clearAudio(),this.logger.debug("Server audio buffer cleared for new session")}catch(t){this.logger.error("Failed to clear server audio buffer",t)}this.logger.error("Error in interactAudio",t),await this.handleError("network_timeout",t)}}"interact"===t.kind&&"interaction_complete"===t.event&&(this.errorRetryHandler.resetRetryCount(),"playing"===this.state||"paused"===this.state?(this.logger.debug("Interaction complete received while playing / paused, deferring."),this.interactionCompletePending=!0,await this.setState("processing_complete")):await this.handleInteractionComplete()),"interaction_error"===t.type&&this.handleError("server_error",new Error(t.error||"Unknown server interaction error")),"string"===t.type&&this.config.hooks.onStringMessage?.(t),this.playbackManager.handleMessage(t)}),this.network.on(ConversationNetworkEvents.Error,async t=>{this.handleError("network_error",t)}),this.playbackManager.on(PlaybackManagerEvents.PlaybackError,async t=>{this.handleError("decode_error",t)}),this.playbackManager.on(PlaybackManagerEvents.Playing,async()=>{this.userInputManager.isRecording()&&(this.logger.debug("Stopping recording as playback starts (no-overlap)"),await this.userInputManager.stop()),await this.setState("playing")}),this.playbackManager.on(PlaybackManagerEvents.Finished,async()=>{if(this.interactionCompletePending)this.logger.debug("Playback finished, processing deferred interaction complete."),this.interactionCompletePending=!1,await this.handleInteractionComplete();else if("playing"===this.state){this.logger.debug("Playback finished, starting recording now (no overlap)"),this.userInputManager.reset();try{await this.userInputManager.start(),await this.setState("listening"),this.logger.debug("Recording started successfully after playback")}catch(t){this.logger.error("Failed to start recording after playback",t),await this.setState("idle")}}})}async handleError(t,e){await this.setState("error"),this.logger.error("handleError called with type: "+t+" and error: "+e.message);let n=!1;try{n=await this.errorRetryHandler.attemptRecovery(t)}catch(t){this.logger.error("Error during recovery attempt",t),n=!1}if(n)return void this.logger.debug("Error recovery succeeded, not raising error");const r={type:t,message:e.message,originalError:e};this.config.hooks.onError?.(r),this.logger.error(`Conversation error: ${t}`,e)}async handleInteractionComplete(){this.logger.debug("Interaction complete received"),this.playbackManager.resetAboutToComplete();try{if(this.userInputManager.reset(),this.userInputManager.isRecording())await this.setState("listening");else{this.logger.debug("Starting recording after interaction complete");try{await this.userInputManager.start(),await this.setState("listening")}catch(t){this.logger.error("Failed to start recording",t),await this.setState("idle")}}}catch(t){this.logger.error("Failed to handle interaction complete",t),await this.handleError("network_timeout",t)}}async toggleTextOnlyInput(t){this.isTextOnly=t,await this.updateInputCapabilities()}isConnected(){return this.network.isReady()}enableAudioDebugMode(){this.userInputManager.enableDebugMode(),this.logger.info("Audio debug mode enabled")}disableAudioDebugMode(){this.userInputManager.disableDebugMode(),this.logger.info("Audio debug mode disabled")}downloadCapturedAudio(t){this.userInputManager.downloadCapturedAudio(t)}clearCapturedAudio(){this.userInputManager.clearCapturedAudio()}getCapturedAudioInfo(){return this.userInputManager.getCapturedAudioInfo()}async updateInputCapabilities(){this.logger.debug(`Updating input capabilities, textOnly: ${this.isTextOnly}`),this.config.inputCapabilities={...this.config.inputCapabilities,audio:!this.isTextOnly,text:this.isTextOnly},await this.userInputManager.updateCapabilities(this.config.inputCapabilities),this.isTextOnly?await this.userInputManager.stopRecording():"idle"===this.state&&await this.startListening()}}export{AudioPlayer,ConversationCommands,ConversationManager};
184
+ ½ÇaztŸÅò¬¼Åâ`});var T={a:x};this.setModule=e=>{t.setModule(EmscriptenWASM,e)},this.getModule=()=>t.getModule(EmscriptenWASM),this.instantiate=()=>(this.getModule().then(t=>WebAssembly.instantiate(t,T)).then(t=>{const i=t.exports;var o;!function(t){g=t.m,m=t.n,b=t.p,y=t.q,_=t.r,w=t.s,v=t.t}(i),o=(r=i.k).buffer,new Int8Array(o),new Int16Array(o),e=new Uint8Array(o),new Uint16Array(o),new Int32Array(o),n=new Uint32Array(o),new Float32Array(o),new Float64Array(o),new BigInt64Array(o),new BigUint64Array(o),function(t){t.l()}(i),s()}),this.ready=new Promise(t=>{s=t}).then(()=>{this.HEAP=r.buffer,this.malloc=g,this.free=m,this.mpeg_decoder_feed=y,this.mpeg_decoder_read=_,this.mpeg_frame_decoder_create=b,this.mpeg_frame_decoder_destroy=w}),this)}function MPEGDecoder(t={}){return this._init=()=>(new this._WASMAudioDecoderCommon).instantiate(this._EmscriptenWASM,this._module).then(e=>{this._common=e,this._sampleRate=0,this._inputBytes=0,this._outputSamples=0,this._frameNumber=0,this._input=this._common.allocateTypedArray(this._inputSize,Uint8Array),this._output=this._common.allocateTypedArray(this._outputSize,Float32Array);const n=this._common.allocateTypedArray(1,Uint32Array);this._samplesDecodedPtr=this._common.allocateTypedArray(1,Uint32Array),this._sampleRatePtr=this._common.allocateTypedArray(1,Uint32Array),this._errorStringPtr=this._common.allocateTypedArray(1,Uint32Array);const r=this._common.wasm.mpeg_frame_decoder_create(n.ptr,!1===t.enableGapless?0:1);if(r)throw Error(this._getErrorMessage(r));this._decoder=n.buf[0]}),Object.defineProperty(this,"ready",{enumerable:!0,get:()=>this._ready}),this._getErrorMessage=t=>t+" "+this._common.codeToString(this._errorStringPtr.buf[0]),this.reset=()=>(this.free(),this._init()),this.free=()=>{this._common.wasm.mpeg_frame_decoder_destroy(this._decoder),this._common.wasm.free(this._decoder),this._common.free()},this.decode=t=>{let e=[],n=[],r=0;if(!(t instanceof Uint8Array))throw Error("Data to decode must be Uint8Array. Instead got "+typeof t);t:for(let i=0,o=0;i<t.length;i+=o){const s=t.subarray(i,this._input.len+i);o=s.length,this._inputBytes+=o,this._input.buf.set(s);let a=this._common.wasm.mpeg_decoder_feed(this._decoder,this._input.ptr,o);if(-10!==a)for(;;){this._samplesDecodedPtr.buf[0]=0,a=this._common.wasm.mpeg_decoder_read(this._decoder,this._output.ptr,this._output.len,this._samplesDecodedPtr.ptr,this._sampleRatePtr.ptr,this._errorStringPtr.ptr);const t=this._samplesDecodedPtr.buf[0];if(this._outputSamples+=t,t&&(r+=t,e.push([this._output.buf.slice(0,t),this._output.buf.slice(t,2*t)])),-11!=a){if(-10===a)continue t;if(a){const t=this._getErrorMessage(a);console.error("mpg123-decoder: "+t),this._common.addError(n,t,0,this._frameNumber,this._inputBytes,this._outputSamples)}}}}return this._WASMAudioDecoderCommon.getDecodedAudioMultiChannel(n,e,2,r,this._sampleRatePtr.buf[0])},this.decodeFrame=t=>{const e=this.decode(t);return this._frameNumber++,e},this.decodeFrames=t=>{let e=[],n=[],r=0,i=0;for(;i<t.length;){const o=this.decodeFrame(t[i++]);e.push(o.channelData),n=n.concat(o.errors),r+=o.samplesDecoded}return this._WASMAudioDecoderCommon.getDecodedAudioMultiChannel(n,e,2,r,this._sampleRatePtr.buf[0])},this._isWebWorker=MPEGDecoder.isWebWorker,this._WASMAudioDecoderCommon=MPEGDecoder.WASMAudioDecoderCommon||WASMAudioDecoderCommon,this._EmscriptenWASM=MPEGDecoder.EmscriptenWASM||EmscriptenWASM,this._module=MPEGDecoder.module,this._inputSize=65536,this._outputSize=92448,this._ready=this._init(),this}class AudioPlayer extends EventEmitter{MIN_CHUNKS_TO_PROCESS=2;FLUSH_DELAY_MS=450;audioContext;currentSource=null;scheduledSources=[];playbackQueue=[];mp3Decoder=null;scheduledPlayTime=0;playbackStartTime=0;chunkQueue=[];isProcessingQueue=!1;frameExtractor=new MP3FrameExtractor;totalFramesDecoded=0;totalFramesFailed=0;totalFramesExtracted=0;isPlayingState=!1;isPausedState=!1;stopped=!1;frameBuffer=[];chunkAccumulator=[];flushTimeout=null;isAudioComplete=!1;timing;isScheduling=!1;allAudioPlayed=!1;constructor(){super(new DefaultLogger({category:"🎵 AudioPlayer",style:StyleGreen})),this.audioContext=new AudioContext,this.timing=new AudioPlayerTiming(()=>this.getCurrentTime(),()=>this.getQueueLength(),()=>this.getRemainingAudioSeconds(),1e3),this.setupTimingListeners()}setupTimingListeners(){this.timing.on("about-to-complete",async()=>{await this.emit(AudioPlayerEvents.AboutToComplete)})}async initialize(){this.audioContext||(this.audioContext=new AudioContext,this.logger.debug("AudioContext created by user gesture")),"suspended"===this.audioContext.state&&(this.audioContext.resume(),this.logger.debug("AudioContext resumed by user gesture")),this.mp3Decoder||(this.logger.debug("Initializing MP3 decoder..."),this.mp3Decoder=new MPEGDecoder,await this.mp3Decoder.ready,this.logger.debug("MP3 decoder ready")),this.resetState()}resetState(){for(const t of this.scheduledSources){t.onended=null;try{t.stop()}catch(t){}t.disconnect()}this.scheduledSources=[],this.currentSource=null,this.playbackQueue=[],this.scheduledPlayTime=0,this.playbackStartTime=0,this.chunkQueue=[],this.isProcessingQueue=!1,this.frameExtractor=new MP3FrameExtractor,this.isPlayingState=!1,this.isPausedState=!1,this.stopped=!1,this.frameBuffer=[],this.chunkAccumulator=[],this.isAudioComplete=!1,this.totalFramesDecoded=0,this.totalFramesFailed=0,this.totalFramesExtracted=0,this.flushTimeout&&(clearTimeout(this.flushTimeout),this.flushTimeout=null),this.timing.reset(),this.allAudioPlayed=!1}reset(){this.resetState()}enqueue(t){this.logger.debug("Enqueue: received chunk"),this.allAudioPlayed=!1,this.chunkQueue.push({base64String:t}),this.processChunkQueue(),this.isAudioComplete||(this.flushTimeout&&clearTimeout(this.flushTimeout),this.flushTimeout=setTimeout(()=>{this.logger.debug("auto-flush timer fired"),this.flush()},this.FLUSH_DELAY_MS))}markComplete(){this.logger.debug("markComplete: no more audio will be added"),this.isAudioComplete=!0,this.flushTimeout&&(clearTimeout(this.flushTimeout),this.flushTimeout=null),this.timing.onAudioComplete(),this.flush();const t=this.frameExtractor.extractRemainingFrames();t.length>0&&(this.logger.debug("markComplete: Found additional frames after flush",{count:t.length}),this.totalFramesExtracted+=t.length,this.decodeAudioFrames(t).then(e=>{if(e){this.totalFramesDecoded+=t.length;const n=0===this.playbackQueue.length;this.playbackQueue.push(e),this.emit(AudioPlayerEvents.Enqueued,{duration:e.duration}),this.timing.onAudioEnqueued(),n&&!this.isPlayingState?this.emit(AudioPlayerEvents.Ready):this.isPlayingState&&this.schedulePlayback()}}).catch(e=>{this.totalFramesFailed+=t.length,this.logger.error("Failed to decode final remaining frames after markComplete",e)}));const e=this.frameExtractor.getRemainingBufferSize();e>0&&this.logger.warn("markComplete: Extractor buffer still has data after final extraction",{remainingBytes:e}),this.allAudioPlayed&&(this.logger.debug("markComplete: All audio was already played, emitting Finished."),this.isPlayingState=!1,this.emit(AudioPlayerEvents.Finished))}async processChunkQueue(){if(!this.isProcessingQueue){this.isProcessingQueue=!0;try{for(;this.chunkQueue.length>0;){const{base64String:t}=this.chunkQueue.shift();if(this.chunkAccumulator.push(t),this.chunkAccumulator.length>=this.MIN_CHUNKS_TO_PROCESS){const t=this.chunkAccumulator;this.chunkAccumulator=[],await this.decodeAndEnqueueChunks(t)}}}finally{this.isProcessingQueue=!1}}}decodeBase64ToUint8Array(t){const e="undefined"!=typeof atob?atob(t):Buffer.from(t,"base64").toString("binary"),n=new Uint8Array(e.length);for(let t=0;t<e.length;t++)n[t]=e.charCodeAt(t);return n}combineArrays(t,e){const n=new Uint8Array(t.length+e.length);return n.set(t),n.set(e,t.length),n}async decodeAudioFrames(t){if(0===t.length)return null;if(!this.mp3Decoder)throw new Error("MP3 decoder not initialized. Call initialize() first.");const e=[];let n=0,r=0,i=0,o=0;for(let s=0;s<t.length;s++){const a=t[s];if(a.length<2||255!==a[0]||224&~a[1])this.logger.warn(`Frame ${s} doesn't start with valid sync word, skipping`,{firstBytes:Array.from(a.slice(0,4)),frameLength:a.length}),o++;else try{const t=this.mp3Decoder.decodeFrame(a);if(t.errors&&t.errors.length>0&&this.logger.warn(`Frame ${s} decoded with errors`,{errors:t.errors.map(t=>t.message||String(t)),frameLength:a.length}),0===t.samplesDecoded){this.logger.warn(`Frame ${s} decoded but produced 0 samples`,{frameLength:a.length,errors:t.errors?.map(t=>t.message||String(t))}),o++;continue}if(0===n?n=t.sampleRate:t.sampleRate!==n&&this.logger.warn(`Frame ${s} has different sample rate: ${t.sampleRate} vs ${n}`),0===e.length)for(let n=0;n<t.channelData.length;n++)e.push([]);for(let n=0;n<t.channelData.length;n++)n<e.length&&e[n].push(t.channelData[n]);r+=t.samplesDecoded,i++}catch(t){this.logger.error(`Failed to decode frame ${s}`,{error:t instanceof Error?t.message:String(t),frameLength:a.length,firstBytes:Array.from(a.slice(0,4))}),o++}}if(0===i)return this.logger.error("All frames failed to decode",{totalFrames:t.length}),null;o>0&&this.logger.warn("Some frames failed to decode",{successful:i,failed:o,total:t.length});const s=[];for(let t=0;t<e.length;t++){const n=new Float32Array(r);let i=0;for(const r of e[t])n.set(r,i),i+=r.length;s.push(n)}const a=this.audioContext.createBuffer(s.length,r,n);for(let t=0;t<s.length;t++)a.getChannelData(t).set(s[t]);return this.logger.debug("Decoded frames",{successful:i,failed:o,totalSamples:r,sampleRate:n,duration:a.duration.toFixed(3)}),a}async play(){this.isPlayingState||0===this.playbackQueue.length?this.logger.debug("Play called but already playing or queue is empty."):(this.isPlayingState=!0,this.stopped=!1,this.isPausedState=!1,this.isPausedState?this.scheduledPlayTime=this.audioContext.currentTime:(this.scheduledPlayTime=this.audioContext.currentTime,this.playbackStartTime=this.audioContext.currentTime),this.logger.debug(`Playback starting. Start time: ${this.playbackStartTime}`),this.schedulePlayback(),await this.emit(AudioPlayerEvents.Playing))}schedulePlayback(){if(this.isScheduling||0===this.playbackQueue.length||this.stopped)return;this.isScheduling=!0,this.currentSource&&(this.currentSource.onended=null);let t=null;for(;this.playbackQueue.length>0;){const e=this.playbackQueue.shift(),n=this.audioContext.createBufferSource();n.buffer=e,n.connect(this.audioContext.destination),this.scheduledPlayTime<this.audioContext.currentTime&&(this.logger.debug(`Adjusting scheduledPlayTime from ${this.scheduledPlayTime} to current time ${this.audioContext.currentTime}`),this.scheduledPlayTime=this.audioContext.currentTime),this.logger.debug(`Scheduling buffer of duration ${e.duration}s to start at ${this.scheduledPlayTime} (context time: ${this.audioContext.currentTime})`),n.start(this.scheduledPlayTime),this.scheduledPlayTime+=e.duration,this.scheduledSources.push(n),t=n}t&&(this.currentSource=t,t.onended=()=>{this.logger.debug(`Buffer finished playing. Context time: ${this.audioContext.currentTime}, isPaused: ${this.isPausedState}, stopped: ${this.stopped}`),this.scheduledSources=this.scheduledSources.filter(t=>t===this.currentSource),this.stopped||this.isPausedState||(this.playbackQueue.length>0?(this.logger.debug("More buffers in queue, scheduling next batch."),this.schedulePlayback()):this.isAudioComplete?(this.logger.debug("Last scheduled audio buffer finished and audio is complete."),this.isPlayingState=!1,this.emit(AudioPlayerEvents.Finished)):(this.logger.debug("All scheduled audio has been played, but not marked as complete yet."),this.allAudioPlayed=!0))}),this.isScheduling=!1}async pause(){this.currentSource&&this.isPlayingState&&(this.isPausedState=!0,this.isPlayingState=!1,this.audioContext.suspend(),await this.emit(AudioPlayerEvents.Paused))}async resume(){this.isPausedState&&(this.logger.debug("Resume called, resuming playback."),this.isPausedState=!1,this.isPlayingState=!0,this.audioContext.resume(),this.schedulePlayback(),await this.emit(AudioPlayerEvents.Playing))}async stop(){this.stopped=!0,this.resetState(),await this.emit(AudioPlayerEvents.Stopped)}getCurrentTime(){return this.isPlayingState?1e3*(this.audioContext.currentTime-this.playbackStartTime):0}getQueueLength(){return this.playbackQueue.length}getRemainingAudioSeconds(){const t=this.playbackQueue.reduce((t,e)=>t+e.duration,0);let e=0;return(this.isPlayingState||this.isPausedState)&&this.scheduledPlayTime>0&&(e=Math.max(0,this.scheduledPlayTime-this.audioContext.currentTime)),t+e}async decodeAndEnqueueChunks(t){if(0===t.length)return void this.logger.debug("decodeAndEnqueueChunks: no chunks to decode");let e=[];for(const n of t){const t=this.decodeBase64ToUint8Array(n),{frames:r,remainingData:i}=this.frameExtractor.feedAndExtractCompleteFrames(t);e=e.concat(r),this.totalFramesExtracted+=r.length,i.length>0&&this.logger.debug("Chunk ended with incomplete frame",{remainingBytes:i.length,framesExtracted:r.length})}if(this.logger.debug("decodeAndEnqueueChunks: total frames to decode",e.length),e.length>0){const t=e.map(t=>t.length).reduce((t,e)=>(t[e]=(t[e]||0)+1,t),{});this.logger.debug("Frame sizes distribution",t);try{const t=await this.decodeAudioFrames(e);if(t){this.totalFramesDecoded+=e.length;const n=0===this.playbackQueue.length;this.playbackQueue.push(t),await this.emit(AudioPlayerEvents.Enqueued,{duration:t.duration}),this.logger.debug("decodeAndEnqueueChunks: enqueued audioBuffer, duration",t.duration,"frames decoded",e.length),this.timing.onAudioEnqueued(),n&&!this.isPlayingState?await this.emit(AudioPlayerEvents.Ready):this.isPlayingState&&this.schedulePlayback()}}catch(t){this.totalFramesFailed+=e.length,this.logger.error("Failed to decode audio frames",t),this.logger.error("Lost frames",{count:e.length,sizes:e.map(t=>t.length)})}}else this.logger.debug("decodeAndEnqueueChunks: no frames to decode")}async flush(){this.logger.debug("Flushing",{chunks:this.chunkAccumulator.length});const t=this.chunkAccumulator;this.chunkAccumulator=[],await this.decodeAndEnqueueChunks(t);const e=this.frameExtractor.extractRemainingFrames();if(e.length>0){this.logger.debug("Flush: Found complete frames in extractor buffer",{count:e.length}),this.totalFramesExtracted+=e.length;try{const t=await this.decodeAudioFrames(e);if(t){this.totalFramesDecoded+=e.length;const n=0===this.playbackQueue.length;this.playbackQueue.push(t),await this.emit(AudioPlayerEvents.Enqueued,{duration:t.duration}),this.timing.onAudioEnqueued(),n&&!this.isPlayingState?await this.emit(AudioPlayerEvents.Ready):this.isPlayingState&&this.schedulePlayback()}}catch(t){this.totalFramesFailed+=e.length,this.logger.error("Failed to decode remaining frames after flush",t)}}}resetAboutToComplete(){this.timing.reset()}getFrameStats(){return{totalFramesExtracted:this.totalFramesExtracted,totalFramesDecoded:this.totalFramesDecoded,totalFramesFailed:this.totalFramesFailed,extractorRemainingBufferSize:this.frameExtractor.getRemainingBufferSize()}}dispose(){this.logger.debug("Disposing AudioPlayer..."),this.stopped=!0,this.isPlayingState=!1,this.isPausedState=!1;for(const t of this.scheduledSources){t.onended=null;try{t.stop()}catch(t){}t.disconnect()}this.scheduledSources=[],this.currentSource=null,this.flushTimeout&&(clearTimeout(this.flushTimeout),this.flushTimeout=null),this.timing&&this.timing.reset(),this.audioContext&&"closed"!==this.audioContext.state&&this.audioContext.close().catch(t=>{this.logger.debug("Error closing AudioContext:",t)}),this.mp3Decoder=null,this.resetState(),this.removeAllListeners(),this.logger.debug("AudioPlayer disposed")}destroy(){this.dispose()}}const SubtitleManagerEvents={WordChange:"wordChange",Playing:"playing",Paused:"paused",SubtitleFinished:"subtitleFinished",SubtitleChange:"subtitleChange"};class SubtitleManager extends EventEmitter{getCurrentTime;subtitleQueue=[];rafId=null;isPlaying=!1;cumulativeOffset=0;tickDebugLast=0;state={currentLine:null,nextLine:null,globalWordIndex:-1};constructor(t){super(new DefaultLogger({category:"🎬 SubtitleManager",style:StylePink})),this.getCurrentTime=t,this._resetState()}enqueue(t){this.logger.debug("Enqueue subtitle",{text:t.characters.join(""),queueLength:this.subtitleQueue.length});const{wordBoundaries:e,wordStartTimesMs:n}=this._computeWordBoundariesAndStartTimes(t.characters,t.start_times_ms),r={...t,wordBoundaries:e,wordStartTimesMs:n};this.subtitleQueue.push(r),this.isPlaying||1!==this.subtitleQueue.length||(this.logger.debug("Reset globalWordIndex to -1 (initial enqueue)"),this.state.globalWordIndex=-1),this.isPlaying&&!this.state.nextLine&&this.subtitleQueue.length>1&&(this._prepareState(),this._emitSubtitleChange())}async play(){this.isPlaying?this.logger.debug("Play called but already playing"):(this.logger.debug("Play called, starting playback"),this.isPlaying=!0,await this.emit(SubtitleManagerEvents.Playing),this.subtitleQueue.length>0&&(this._prepareState(),await this._emitSubtitleChange()),this.rafId=requestAnimationFrame(this._tick))}async pause(){this.logger.debug("Pause called"),this.isPlaying=!1,this.rafId&&(cancelAnimationFrame(this.rafId),this.rafId=null),await this.emit(SubtitleManagerEvents.Paused)}clearQueue(){this.logger.debug("Clear queue"),this._resetState(),this.pause()}reset(){this._resetState()}_resetState(){this.subtitleQueue=[],this.cumulativeOffset=0,this.rafId=null,this.isPlaying=!1,this.tickDebugLast=0,this.state={currentLine:null,nextLine:null,globalWordIndex:-1}}_subtitleDuration(t){const{start_times_ms:e,durations_ms:n}=t;if(!e.length||!n.length)return 0;const r=e.length-1;return e[r]+n[r]}_applyOffset(t,e){return t.map(t=>t+e)}_prepareState(){if(!this.subtitleQueue.length)return;const t=this.subtitleQueue[0];t.adjustedWordStartTimesMs=this._applyOffset(t.wordStartTimesMs,this.cumulativeOffset);const e=this.subtitleQueue[1]||null;if(e){const n=this.cumulativeOffset+this._subtitleDuration(t);e.adjustedWordStartTimesMs=this._applyOffset(e.wordStartTimesMs,n)}this.state.currentLine=t,this.state.nextLine=e,this.logger.debug("Prepared state",{cumulativeOffset:this.cumulativeOffset,hasCurrentLine:!!t,hasNextLine:!!e,firstFewAdjusted:t.adjustedWordStartTimesMs?.slice(0,3)})}_computeWordBoundariesAndStartTimes(t,e){const n=[],r=[];let i=null;for(let o=0;o<t.length;o++){const s=t[o];if(" "!==s&&null===i&&(i=o),(" "===s||o===t.length-1)&&null!==i){const t=" "===s?o-1:o;n.push({start:i,end:t}),r.push(e[i]),i=null}}return{wordBoundaries:n,wordStartTimesMs:r}}_calculateGlobalWordIndex(t){let e=-1;const{currentLine:n,nextLine:r}=this.state;if(n?.adjustedWordStartTimesMs)for(let r=0;r<n.adjustedWordStartTimesMs.length&&t>=n.adjustedWordStartTimesMs[r];r++)e=r;if(r?.adjustedWordStartTimesMs){const i=n?.wordBoundaries.length||0;for(let n=0;n<r.adjustedWordStartTimesMs.length&&t>=r.adjustedWordStartTimesMs[n];n++)e=i+n}return e}_decodeGlobalIndex(t){if(t<0)return{lineIndex:0,wordIndex:-1};const e=this.state.currentLine?.wordBoundaries.length||0;return t<e?{lineIndex:0,wordIndex:t}:{lineIndex:1,wordIndex:t-e}}_isFinished(t,e,n){return e>n+this._subtitleDuration(t)}_buildWordChangeEvent(t){const{lineIndex:e,wordIndex:n}=this._decodeGlobalIndex(t),{currentLine:r,nextLine:i}=this.state,o=t=>t?{characters:t.characters,wordBoundaries:t.wordBoundaries}:null;let s;if(0===e&&r&&n>=0){const t=r.wordBoundaries[n];s=r.characters.slice(t.start,t.end+1).join("")}else if(1===e&&i&&n>=0){const t=i.wordBoundaries[n];s=i.characters.slice(t.start,t.end+1).join("")}return{globalWordIndex:t,currentLineIndex:e,wordIndexInLine:n,word:s,currentLineData:o(r),nextLineData:o(i)}}_buildSubtitleChangeEvent(){const t=t=>t?.adjustedWordStartTimesMs?{characters:t.characters,wordBoundaries:t.wordBoundaries,adjustedWordStartTimesMs:t.adjustedWordStartTimesMs}:null;return{currentLine:t(this.state.currentLine),nextLine:t(this.state.nextLine)}}async _emitWordChange(t){const e=this._buildWordChangeEvent(t);this.logger.debug("Word change",{globalWordIndex:t,lineIndex:e.currentLineIndex,wordIndex:e.wordIndexInLine,word:e.word,currentLine:e.currentLineData?.characters.join(""),nextLine:e.nextLineData?.characters.join("")}),await this.emit(SubtitleManagerEvents.WordChange,e)}async _emitSubtitleChange(){const t=this._buildSubtitleChangeEvent();this.logger.debug("Subtitle change",{hasCurrentLine:!!t.currentLine,hasNextLine:!!t.nextLine}),await this.emit(SubtitleManagerEvents.SubtitleChange,t)}_tick=async()=>{if(!this.isPlaying||0===this.subtitleQueue.length)return;!this.state.nextLine&&this.subtitleQueue.length>1&&(this._prepareState(),await this._emitSubtitleChange());const t=this.getCurrentTime();this.state.currentLine||this._prepareState(),t-this.tickDebugLast>=3e3&&(this.logger.debug("Tick debug",{currentTimeMs:t,cumulativeOffset:this.cumulativeOffset,hasCurrentLine:!!this.state.currentLine,hasNextLine:!!this.state.nextLine}),this.tickDebugLast=t);const e=this._calculateGlobalWordIndex(t);e!==this.state.globalWordIndex&&(this.state.globalWordIndex=e,await this._emitWordChange(e));const n=this.subtitleQueue[0],r=this.subtitleQueue[1]||null;let i=0;if(r){const e=this.cumulativeOffset+this._subtitleDuration(n);this._isFinished(r,t,e)&&(i=2)}else n&&this._isFinished(n,t,this.cumulativeOffset)&&(i=1);if(i>0){this.logger.debug(`Subtitle batch finished. Advancing ${i} lines.`,{currentTimeMs:t,cumulativeOffset:this.cumulativeOffset});const e=this.subtitleQueue.splice(0,i);for(const t of e)this.cumulativeOffset+=this._subtitleDuration(t),await this.emit(SubtitleManagerEvents.SubtitleFinished,t);if(!(this.subtitleQueue.length>0))return this.logger.debug("Queue empty, pausing"),this.state.globalWordIndex=-1,await this._emitWordChange(-1),void await this.pause();this._prepareState(),await this._emitSubtitleChange()}this.rafId=requestAnimationFrame(this._tick)}}class VisemeScheduler extends EventEmitter{scheduledEvents=[];masterClock;constructor(t){super(new DefaultLogger({category:"👄 VisemeScheduler",style:StyleRed})),this.masterClock=t}enqueue(t){this.scheduledEvents.push(...t),this.scheduleEvents()}async play(){await this.emit(VisemeSchedulerEvents.Playing)}async pause(){await this.emit(VisemeSchedulerEvents.Paused)}scheduleEvents(){this.masterClock.currentTime,this.scheduledEvents.forEach(t=>{setTimeout(async()=>{await this.emit(VisemeSchedulerEvents.Viseme,t)})})}}class AvatarManager extends EventEmitter{animationLayers=new Map;layerPriority=["mouth","facial","body","idle"];constructor(){super(new DefaultLogger({category:"🧒 AvatarManager",style:StyleOrange}))}playIdle(){this.playAnimation("body_idle",0,!0)}playListen(){this.playAnimation("body_idle_listen",0,!0)}playTalk(){this.playAnimation("body_talk_to_user_loop",0,!0)}playThink(){this.playAnimation("body_idle_think",0,!0)}playLaugh(){this.playAnimation("body_laugh",0,!0)}playWaving(){this.playAnimation("body_waving",0,!1)}playViseme(t){this.playAnimation(t,1,!1)}async playAnimation(t,e=0,n=!0){this.animationLayers.set(e,t),await this.emit(AvatarManagerEvents.AnimationChanged,{name:t,layer:e,loop:n}),this.logger.debug(`Playing animation: ${t} on layer ${e} (loop: ${n})`)}async stopAnimation(t=0){this.animationLayers.delete(t),await this.emit(AvatarManagerEvents.AnimationStopped,{layer:t})}dispose(){try{this.logger.debug("Disposing AvatarManager..."),this.animationLayers.clear(),this.removeAllListeners(),this.logger.debug("AvatarManager disposed")}catch(t){this.logger.warn("Error while disposing AvatarManager",t)}}}const PlaybackManagerEvents={Playing:"playing",Paused:"paused",PlaybackError:"playbackError",SubtitleWordChange:"subtitleWordChange",SubtitleChange:"subtitleChange",ImageChange:"imageChange",AboutToComplete:"aboutToComplete",Finished:"finished",AvatarAnimationChanged:"avatarAnimationChanged"};class PlaybackManager extends EventEmitter{capabilities;audioPlayer;visemeScheduler;subtitleManager;isPaused=!0;hasStarted=!1;avatarManager;constructor(t){super(new DefaultLogger({category:"🎪 PlaybackManager",style:StyleOrange})),this.capabilities=t,this.audioPlayer=new AudioPlayer,this.visemeScheduler=new VisemeScheduler(new AudioContext),this.subtitleManager=new SubtitleManager(()=>this.audioPlayer.getCurrentTime()),this.avatarManager=new AvatarManager,this.avatarManager.on("animation-changed",async t=>{await this.emit(PlaybackManagerEvents.AvatarAnimationChanged,t)}),this.setupChildListeners(),this.resetState()}setupChildListeners(){this.audioPlayer.on(AudioPlayerEvents.Ready,async()=>{this.isPaused&&!this.hasStarted?(this.logger.debug("Ready event received, starting playback"),await this.play()):this.logger.debug("Ready event received but already playing or paused, ignoring")}),this.audioPlayer.on(AudioPlayerEvents.AboutToComplete,async()=>{this.logger.debug("Audio about to complete"),await this.emit(PlaybackManagerEvents.AboutToComplete)}),this.audioPlayer.on(AudioPlayerEvents.Error,async t=>{await this.emit(PlaybackManagerEvents.PlaybackError,{source:"audio",error:t})}),this.audioPlayer.on(AudioPlayerEvents.Finished,async()=>{await this.emit(PlaybackManagerEvents.Finished),this.reset()}),this.subtitleManager.on(SubtitleManagerEvents.WordChange,async t=>{await this.emit(PlaybackManagerEvents.SubtitleWordChange,t)}),this.subtitleManager.on(SubtitleManagerEvents.SubtitleChange,async t=>{await this.emit(PlaybackManagerEvents.SubtitleChange,t)})}resetState(){this.isPaused=!0,this.hasStarted=!1}async initialize(){this.logger.debug("Initialized",this.capabilities),this.isPaused=!0,this.hasStarted=!1,await this.audioPlayer.initialize()}handleMessage(t){"audio"===t.event||"subtitles"===t.event||"viseme"===t.event?this.enqueue(t):"audio_complete"===t.event?this.markAudioComplete():"image"===t.event&&this.emit(PlaybackManagerEvents.ImageChange,t)}enqueue(t){if(this.capabilities.audio&&"audio"===t.event){const e=t;if(this.audioPlayer.enqueue(e.audio),this.capabilities.subtitles&&e.subtitles?.length){const t=this.convertSubtitleUnitsToMessage(e.subtitles);this.subtitleManager.enqueue(t)}}if(this.capabilities.subtitles&&"subtitles"===t.event){const e=t;this.subtitleManager.enqueue(e)}}convertSubtitleUnitsToMessage(t){return{type:"subtitles",characters:t.map(t=>t.text),start_times_ms:t.map(t=>Math.round(1e3*t.start_time_sec)),durations_ms:t.map(t=>Math.round(1e3*t.duration_sec))}}markAudioComplete(){this.logger.debug("Marking audio as complete"),this.capabilities.audio&&this.audioPlayer.markComplete()}async play(){this.isPaused=!1,this.hasStarted=!0,this.avatarManager.playTalk(),await Promise.all([this.capabilities.viseme?this.visemeScheduler.play():Promise.resolve(),this.capabilities.subtitles?this.subtitleManager.play():Promise.resolve(),this.capabilities.audio?this.audioPlayer.play():Promise.resolve()]),await this.emit(PlaybackManagerEvents.Playing)}async pause(){this.isPaused=!0,this.audioPlayer.pause(),this.visemeScheduler.pause(),this.subtitleManager.pause(),this.avatarManager.playIdle(),await this.emit(PlaybackManagerEvents.Paused)}async resume(){this.isPaused&&(this.isPaused=!1,this.avatarManager.playTalk(),await Promise.all([this.capabilities.viseme?this.visemeScheduler.play():Promise.resolve(),this.capabilities.subtitles?this.subtitleManager.play():Promise.resolve(),this.capabilities.audio?this.audioPlayer.resume():Promise.resolve()]),await this.emit(PlaybackManagerEvents.Playing))}async resumeAudioContext(){"function"==typeof this.audioPlayer.resumeAudioContext&&await this.audioPlayer.resumeAudioContext()}getRemainingAudioSeconds(){return this.audioPlayer?.getRemainingAudioSeconds()??0}reset(){this.resetState(),this.audioPlayer?.reset(),this.subtitleManager?.reset(),this.avatarManager.playIdle()}resetAboutToComplete(){this.audioPlayer?.resetAboutToComplete()}dispose(){this.logger.debug("Disposing PlaybackManager...");try{this.isPaused=!0,this.hasStarted=!1,this.audioPlayer?.pause(),this.visemeScheduler?.pause(),this.subtitleManager?.pause(),this.avatarManager?.playIdle()}catch(t){this.logger.warn("Error pausing components during dispose",t)}try{this.audioPlayer&&"function"==typeof this.audioPlayer.dispose&&this.audioPlayer.dispose()}catch(t){this.logger.warn("Error disposing audioPlayer",t)}try{this.avatarManager&&"function"==typeof this.avatarManager.dispose&&this.avatarManager.dispose()}catch(t){this.logger.warn("Error disposing avatarManager",t)}try{this.subtitleManager?.reset(),this.removeAllListeners()}catch(t){this.logger.warn("Error resetting subtitleManager or removing listeners",t)}this.logger.debug("PlaybackManager disposed")}wireVADToAvatar(t){t.on(ConversationManagerEvents.StateChange,t=>{"userSpeaking"===t.newState?this.avatarManager.playListen():"listening"===t.newState&&this.avatarManager.playIdle()})}wireConversationStateToAvatar(t){t.on(ConversationManagerEvents.StateChange,t=>{"waiting"===t.newState&&this.avatarManager.playThink()})}}var dist=__webpack_require__(937);const VadManagerEvents={VoiceActivity:"voiceActivity",Silence:"silence"};class VADManager extends EventEmitter{vadModel=null;silenceTimer=null;silenceTimeoutMs;positiveSpeechThreshold;negativeSpeechThreshold;minSpeechFrames;isDisposed=!1;currentStream=null;constructor(t=300,e=.5,n=.35,r=3){super(new DefaultLogger({category:"🎤 VADManager",style:StylePurple})),this.silenceTimeoutMs=t,this.positiveSpeechThreshold=e,this.negativeSpeechThreshold=n,this.minSpeechFrames=r}async initialize(t){if(this.isDisposed)return;this.currentStream=t,this.logger.debug("You may need to close the browser console for this to initialize");const e=await dist.Jl.new({stream:t,baseAssetPath:"/static/binaries/",onnxWASMBasePath:"/static/binaries/",positiveSpeechThreshold:this.positiveSpeechThreshold,negativeSpeechThreshold:this.negativeSpeechThreshold,minSpeechFrames:this.minSpeechFrames,onSpeechStart:async()=>{this.isDisposed||(this.logger.debug("Speech started"),this.clearSilenceTimer(),await this.emit(VadManagerEvents.VoiceActivity,{isSpeaking:!0,confidence:1}))},onSpeechEnd:async t=>{this.isDisposed||(this.logger.debug("Speech ended"),this.startSilenceTimer(),await this.emit(VadManagerEvents.VoiceActivity,{isSpeaking:!1,confidence:1}))},onVADMisfire:async()=>{this.isDisposed||(this.logger.debug("Misfire detected"),await this.emit(VadManagerEvents.VoiceActivity,{isSpeaking:!1,confidence:0,misfire:!0}))},onFrameProcessed:async t=>{}});if(this.isDisposed){this.logger.debug("VADManager disposed during initialization, cleaning up model");try{"function"==typeof e.destroy?e.destroy():e.pause()}catch(t){this.logger.debug("Error cleaning up VAD model after disposal during init",t)}}else this.vadModel=e,this.logger.debug(`VAD Manager initialized (MicVAD) with silenceTimeoutMs=${this.silenceTimeoutMs}, positiveSpeechThreshold=${this.positiveSpeechThreshold}, negativeSpeechThreshold=${this.negativeSpeechThreshold}, minSpeechFrames=${this.minSpeechFrames}`)}startAnalysis(){if(this.isDisposed)this.logger.debug("Cannot start VAD analysis - VADManager is disposed");else if(this.vadModel)if(this.currentStream)try{this.vadModel.start(),this.logger.debug("VAD analysis started (MicVAD)")}catch(t){this.logger.warn("Error starting VAD analysis, VAD may be in inconsistent state:",t)}else this.logger.debug("Cannot start VAD analysis - no active stream");else this.logger.debug("Cannot start VAD analysis - VAD model not initialized")}stopAnalysis(){if(this.clearSilenceTimer(),this.vadModel)try{this.vadModel.pause(),this.logger.debug("VAD analysis stopped (MicVAD)")}catch(t){}}startSilenceTimer(){this.silenceTimer||(this.silenceTimer=window.setTimeout(async()=>{await this.emit(VadManagerEvents.Silence),this.silenceTimer=null},this.silenceTimeoutMs))}clearSilenceTimer(){this.silenceTimer&&(clearTimeout(this.silenceTimer),this.silenceTimer=null)}dispose(){try{if(this.logger.debug("Disposing VADManager..."),this.isDisposed=!0,this.clearSilenceTimer(),this.removeAllListeners(),this.currentStream=null,this.vadModel){if(this.stopAnalysis(),"function"==typeof this.vadModel.destroy)try{this.vadModel.destroy(),this.logger.debug("VAD model destroyed")}catch(t){}this.vadModel=null}this.logger.debug("VADManager disposed")}catch(t){this.logger.warn("Error while disposing VADManager",t)}}}const UserInputManagerEvents={UserInput:"user-input",UserSpeaking:"user-speaking",UserSilence:"user-silence"},AUDIO_RECORDER_EVENTS={RecordingStarted:"recording-started",RecordingStopped:"recording-stopped",AudioData:"audio-data",Error:"error"},types_VadManagerEvents={VoiceActivity:"voiceActivity",Silence:"silence"},randomUUID="undefined"!=typeof crypto&&crypto.randomUUID&&crypto.randomUUID.bind(crypto),dist_native={randomUUID};let getRandomValues;const rnds8=new Uint8Array(16);function rng(){if(!getRandomValues){if("undefined"==typeof crypto||!crypto.getRandomValues)throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");getRandomValues=crypto.getRandomValues.bind(crypto)}return getRandomValues(rnds8)}const byteToHex=[];for(let t=0;t<256;++t)byteToHex.push((t+256).toString(16).slice(1));function unsafeStringify(t,e=0){return(byteToHex[t[e+0]]+byteToHex[t[e+1]]+byteToHex[t[e+2]]+byteToHex[t[e+3]]+"-"+byteToHex[t[e+4]]+byteToHex[t[e+5]]+"-"+byteToHex[t[e+6]]+byteToHex[t[e+7]]+"-"+byteToHex[t[e+8]]+byteToHex[t[e+9]]+"-"+byteToHex[t[e+10]]+byteToHex[t[e+11]]+byteToHex[t[e+12]]+byteToHex[t[e+13]]+byteToHex[t[e+14]]+byteToHex[t[e+15]]).toLowerCase()}function stringify(t,e=0){const n=unsafeStringify(t,e);if(!validate(n))throw TypeError("Stringified UUID is invalid");return n}const dist_stringify=null;function _v4(t,e,n){const r=(t=t||{}).random??t.rng?.()??rng();if(r.length<16)throw new Error("Random bytes length must be >= 16");if(r[6]=15&r[6]|64,r[8]=63&r[8]|128,e){if((n=n||0)<0||n+16>e.length)throw new RangeError(`UUID byte range ${n}:${n+15} is out of buffer bounds`);for(let t=0;t<16;++t)e[n+t]=r[t];return e}return unsafeStringify(r)}function v4(t,e,n){return!dist_native.randomUUID||e||t?_v4(t,e,n):dist_native.randomUUID()}const dist_v4=v4;class WebSocketConnection{websocket=null;handlers;serverUrl;constructor(t,e){this.serverUrl=t,this.handlers=e}connect(){this.websocket=new WebSocket(this.serverUrl),this.setupHandlers()}disconnect(){this.websocket&&(this.websocket.onmessage=null,this.websocket.onerror=null,this.websocket.onclose=null,this.websocket.close(1e3,"Client initiated disconnect"),this.websocket=null)}send(t){if(!this.websocket||this.websocket.readyState!==WebSocket.OPEN)throw new Error("Cant send. WebSocket is not open");this.websocket.send(JSON.stringify(t))}isReady(){return this.websocket?.readyState===WebSocket.OPEN}setupHandlers(){this.websocket&&(this.websocket.onopen=()=>{this.handlers.onOpen&&this.handlers.onOpen()},this.websocket.onmessage=t=>{const e=JSON.parse(t.data);this.handlers.onMessage&&this.handlers.onMessage(e)},this.websocket.onerror=t=>{this.handlers.onError&&this.handlers.onError(t)},this.websocket.onclose=()=>{this.handlers.onClose&&this.handlers.onClose()})}}function bind(t,e){return function(){return t.apply(e,arguments)}}const{toString:utils_toString}=Object.prototype,{getPrototypeOf}=Object,{iterator,toStringTag}=Symbol,kindOf=(cache=Object.create(null),t=>{const e=utils_toString.call(t);return cache[e]||(cache[e]=e.slice(8,-1).toLowerCase())});var cache;const kindOfTest=t=>(t=t.toLowerCase(),e=>kindOf(e)===t),typeOfTest=t=>e=>typeof e===t,{isArray}=Array,isUndefined=typeOfTest("undefined");function isBuffer(t){return null!==t&&!isUndefined(t)&&null!==t.constructor&&!isUndefined(t.constructor)&&isFunction(t.constructor.isBuffer)&&t.constructor.isBuffer(t)}const isArrayBuffer=kindOfTest("ArrayBuffer");function isArrayBufferView(t){let e;return e="undefined"!=typeof ArrayBuffer&&ArrayBuffer.isView?ArrayBuffer.isView(t):t&&t.buffer&&isArrayBuffer(t.buffer),e}const isString=typeOfTest("string"),isFunction=typeOfTest("function"),isNumber=typeOfTest("number"),isObject=t=>null!==t&&"object"==typeof t,isBoolean=t=>!0===t||!1===t,isPlainObject=t=>{if("object"!==kindOf(t))return!1;const e=getPrototypeOf(t);return!(null!==e&&e!==Object.prototype&&null!==Object.getPrototypeOf(e)||toStringTag in t||iterator in t)},isEmptyObject=t=>{if(!isObject(t)||isBuffer(t))return!1;try{return 0===Object.keys(t).length&&Object.getPrototypeOf(t)===Object.prototype}catch(t){return!1}},isDate=kindOfTest("Date"),isFile=kindOfTest("File"),isBlob=kindOfTest("Blob"),isFileList=kindOfTest("FileList"),isStream=t=>isObject(t)&&isFunction(t.pipe),isFormData=t=>{let e;return t&&("function"==typeof FormData&&t instanceof FormData||isFunction(t.append)&&("formdata"===(e=kindOf(t))||"object"===e&&isFunction(t.toString)&&"[object FormData]"===t.toString()))},isURLSearchParams=kindOfTest("URLSearchParams"),[isReadableStream,isRequest,isResponse,isHeaders]=["ReadableStream","Request","Response","Headers"].map(kindOfTest),trim=t=>t.trim?t.trim():t.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g,"");function forEach(t,e,{allOwnKeys:n=!1}={}){if(null==t)return;let r,i;if("object"!=typeof t&&(t=[t]),isArray(t))for(r=0,i=t.length;r<i;r++)e.call(null,t[r],r,t);else{if(isBuffer(t))return;const i=n?Object.getOwnPropertyNames(t):Object.keys(t),o=i.length;let s;for(r=0;r<o;r++)s=i[r],e.call(null,t[s],s,t)}}function findKey(t,e){if(isBuffer(t))return null;e=e.toLowerCase();const n=Object.keys(t);let r,i=n.length;for(;i-- >0;)if(r=n[i],e===r.toLowerCase())return r;return null}const _global="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:"undefined"!=typeof window?window:__webpack_require__.g,isContextDefined=t=>!isUndefined(t)&&t!==_global;function merge(){const{caseless:t,skipUndefined:e}=isContextDefined(this)&&this||{},n={},r=(r,i)=>{const o=t&&findKey(n,i)||i;isPlainObject(n[o])&&isPlainObject(r)?n[o]=merge(n[o],r):isPlainObject(r)?n[o]=merge({},r):isArray(r)?n[o]=r.slice():e&&isUndefined(r)||(n[o]=r)};for(let t=0,e=arguments.length;t<e;t++)arguments[t]&&forEach(arguments[t],r);return n}const extend=(t,e,n,{allOwnKeys:r}={})=>(forEach(e,(e,r)=>{n&&isFunction(e)?t[r]=bind(e,n):t[r]=e},{allOwnKeys:r}),t),stripBOM=t=>(65279===t.charCodeAt(0)&&(t=t.slice(1)),t),inherits=(t,e,n,r)=>{t.prototype=Object.create(e.prototype,r),t.prototype.constructor=t,Object.defineProperty(t,"super",{value:e.prototype}),n&&Object.assign(t.prototype,n)},toFlatObject=(t,e,n,r)=>{let i,o,s;const a={};if(e=e||{},null==t)return e;do{for(i=Object.getOwnPropertyNames(t),o=i.length;o-- >0;)s=i[o],r&&!r(s,t,e)||a[s]||(e[s]=t[s],a[s]=!0);t=!1!==n&&getPrototypeOf(t)}while(t&&(!n||n(t,e))&&t!==Object.prototype);return e},endsWith=(t,e,n)=>{t=String(t),(void 0===n||n>t.length)&&(n=t.length),n-=e.length;const r=t.indexOf(e,n);return-1!==r&&r===n},toArray=t=>{if(!t)return null;if(isArray(t))return t;let e=t.length;if(!isNumber(e))return null;const n=new Array(e);for(;e-- >0;)n[e]=t[e];return n},isTypedArray=(TypedArray="undefined"!=typeof Uint8Array&&getPrototypeOf(Uint8Array),t=>TypedArray&&t instanceof TypedArray);var TypedArray;const forEachEntry=(t,e)=>{const n=(t&&t[iterator]).call(t);let r;for(;(r=n.next())&&!r.done;){const n=r.value;e.call(t,n[0],n[1])}},matchAll=(t,e)=>{let n;const r=[];for(;null!==(n=t.exec(e));)r.push(n);return r},isHTMLForm=kindOfTest("HTMLFormElement"),toCamelCase=t=>t.toLowerCase().replace(/[-_\s]([a-z\d])(\w*)/g,function(t,e,n){return e.toUpperCase()+n}),utils_hasOwnProperty=(({hasOwnProperty:t})=>(e,n)=>t.call(e,n))(Object.prototype),isRegExp=kindOfTest("RegExp"),reduceDescriptors=(t,e)=>{const n=Object.getOwnPropertyDescriptors(t),r={};forEach(n,(n,i)=>{let o;!1!==(o=e(n,i,t))&&(r[i]=o||n)}),Object.defineProperties(t,r)},freezeMethods=t=>{reduceDescriptors(t,(e,n)=>{if(isFunction(t)&&-1!==["arguments","caller","callee"].indexOf(n))return!1;const r=t[n];isFunction(r)&&(e.enumerable=!1,"writable"in e?e.writable=!1:e.set||(e.set=()=>{throw Error("Can not rewrite read-only method '"+n+"'")}))})},toObjectSet=(t,e)=>{const n={},r=t=>{t.forEach(t=>{n[t]=!0})};return isArray(t)?r(t):r(String(t).split(e)),n},noop=()=>{},toFiniteNumber=(t,e)=>null!=t&&Number.isFinite(t=+t)?t:e;function isSpecCompliantForm(t){return!!(t&&isFunction(t.append)&&"FormData"===t[toStringTag]&&t[iterator])}const toJSONObject=t=>{const e=new Array(10),n=(t,r)=>{if(isObject(t)){if(e.indexOf(t)>=0)return;if(isBuffer(t))return t;if(!("toJSON"in t)){e[r]=t;const i=isArray(t)?[]:{};return forEach(t,(t,e)=>{const o=n(t,r+1);!isUndefined(o)&&(i[e]=o)}),e[r]=void 0,i}}return t};return n(t,0)},isAsyncFn=kindOfTest("AsyncFunction"),isThenable=t=>t&&(isObject(t)||isFunction(t))&&isFunction(t.then)&&isFunction(t.catch),_setImmediate=(setImmediateSupported="function"==typeof setImmediate,postMessageSupported=isFunction(_global.postMessage),setImmediateSupported?setImmediate:postMessageSupported?(token=`axios@${Math.random()}`,callbacks=[],_global.addEventListener("message",({source:t,data:e})=>{t===_global&&e===token&&callbacks.length&&callbacks.shift()()},!1),t=>{callbacks.push(t),_global.postMessage(token,"*")}):t=>setTimeout(t));var setImmediateSupported,postMessageSupported,token,callbacks;const asap="undefined"!=typeof queueMicrotask?queueMicrotask.bind(_global):"undefined"!=typeof process&&process.nextTick||_setImmediate,isIterable=t=>null!=t&&isFunction(t[iterator]),utils={isArray,isArrayBuffer,isBuffer,isFormData,isArrayBufferView,isString,isNumber,isBoolean,isObject,isPlainObject,isEmptyObject,isReadableStream,isRequest,isResponse,isHeaders,isUndefined,isDate,isFile,isBlob,isRegExp,isFunction,isStream,isURLSearchParams,isTypedArray,isFileList,forEach,merge,extend,trim,stripBOM,inherits,toFlatObject,kindOf,kindOfTest,endsWith,toArray,forEachEntry,matchAll,isHTMLForm,hasOwnProperty:utils_hasOwnProperty,hasOwnProp:utils_hasOwnProperty,reduceDescriptors,freezeMethods,toObjectSet,toCamelCase,noop,toFiniteNumber,findKey,global:_global,isContextDefined,isSpecCompliantForm,toJSONObject,isAsyncFn,isThenable,setImmediate:_setImmediate,asap,isIterable};function AxiosError(t,e,n,r,i){Error.call(this),Error.captureStackTrace?Error.captureStackTrace(this,this.constructor):this.stack=(new Error).stack,this.message=t,this.name="AxiosError",e&&(this.code=e),n&&(this.config=n),r&&(this.request=r),i&&(this.response=i,this.status=i.status?i.status:null)}utils.inherits(AxiosError,Error,{toJSON:function(){return{message:this.message,name:this.name,description:this.description,number:this.number,fileName:this.fileName,lineNumber:this.lineNumber,columnNumber:this.columnNumber,stack:this.stack,config:utils.toJSONObject(this.config),code:this.code,status:this.status}}});const AxiosError_prototype=AxiosError.prototype,descriptors={};["ERR_BAD_OPTION_VALUE","ERR_BAD_OPTION","ECONNABORTED","ETIMEDOUT","ERR_NETWORK","ERR_FR_TOO_MANY_REDIRECTS","ERR_DEPRECATED","ERR_BAD_RESPONSE","ERR_BAD_REQUEST","ERR_CANCELED","ERR_NOT_SUPPORT","ERR_INVALID_URL"].forEach(t=>{descriptors[t]={value:t}}),Object.defineProperties(AxiosError,descriptors),Object.defineProperty(AxiosError_prototype,"isAxiosError",{value:!0}),AxiosError.from=(t,e,n,r,i,o)=>{const s=Object.create(AxiosError_prototype);utils.toFlatObject(t,s,function(t){return t!==Error.prototype},t=>"isAxiosError"!==t);const a=t&&t.message?t.message:"Error",u=null==e&&t?t.code:e;return AxiosError.call(s,a,u,n,r,i),t&&null==s.cause&&Object.defineProperty(s,"cause",{value:t,configurable:!0}),s.name=t&&t.name||"Error",o&&Object.assign(s,o),s};const core_AxiosError=AxiosError,helpers_null=null;function isVisitable(t){return utils.isPlainObject(t)||utils.isArray(t)}function removeBrackets(t){return utils.endsWith(t,"[]")?t.slice(0,-2):t}function renderKey(t,e,n){return t?t.concat(e).map(function(t,e){return t=removeBrackets(t),!n&&e?"["+t+"]":t}).join(n?".":""):e}function isFlatArray(t){return utils.isArray(t)&&!t.some(isVisitable)}const predicates=utils.toFlatObject(utils,{},null,function(t){return/^is[A-Z]/.test(t)});function toFormData(t,e,n){if(!utils.isObject(t))throw new TypeError("target must be an object");e=e||new(helpers_null||FormData);const r=(n=utils.toFlatObject(n,{metaTokens:!0,dots:!1,indexes:!1},!1,function(t,e){return!utils.isUndefined(e[t])})).metaTokens,i=n.visitor||l,o=n.dots,s=n.indexes,a=(n.Blob||"undefined"!=typeof Blob&&Blob)&&utils.isSpecCompliantForm(e);if(!utils.isFunction(i))throw new TypeError("visitor must be a function");function u(t){if(null===t)return"";if(utils.isDate(t))return t.toISOString();if(utils.isBoolean(t))return t.toString();if(!a&&utils.isBlob(t))throw new core_AxiosError("Blob is not supported. Use a Buffer instead.");return utils.isArrayBuffer(t)||utils.isTypedArray(t)?a&&"function"==typeof Blob?new Blob([t]):Buffer.from(t):t}function l(t,n,i){let a=t;if(t&&!i&&"object"==typeof t)if(utils.endsWith(n,"{}"))n=r?n:n.slice(0,-2),t=JSON.stringify(t);else if(utils.isArray(t)&&isFlatArray(t)||(utils.isFileList(t)||utils.endsWith(n,"[]"))&&(a=utils.toArray(t)))return n=removeBrackets(n),a.forEach(function(t,r){!utils.isUndefined(t)&&null!==t&&e.append(!0===s?renderKey([n],r,o):null===s?n:n+"[]",u(t))}),!1;return!!isVisitable(t)||(e.append(renderKey(i,n,o),u(t)),!1)}const c=[],d=Object.assign(predicates,{defaultVisitor:l,convertValue:u,isVisitable});if(!utils.isObject(t))throw new TypeError("data must be an object");return function t(n,r){if(!utils.isUndefined(n)){if(-1!==c.indexOf(n))throw Error("Circular reference detected in "+r.join("."));c.push(n),utils.forEach(n,function(n,o){!0===(!(utils.isUndefined(n)||null===n)&&i.call(e,n,utils.isString(o)?o.trim():o,r,d))&&t(n,r?r.concat(o):[o])}),c.pop()}}(t),e}const helpers_toFormData=toFormData;function encode(t){const e={"!":"%21","'":"%27","(":"%28",")":"%29","~":"%7E","%20":"+","%00":"\0"};return encodeURIComponent(t).replace(/[!'()~]|%20|%00/g,function(t){return e[t]})}function AxiosURLSearchParams(t,e){this._pairs=[],t&&helpers_toFormData(t,this,e)}const AxiosURLSearchParams_prototype=AxiosURLSearchParams.prototype;AxiosURLSearchParams_prototype.append=function(t,e){this._pairs.push([t,e])},AxiosURLSearchParams_prototype.toString=function(t){const e=t?function(e){return t.call(this,e,encode)}:encode;return this._pairs.map(function(t){return e(t[0])+"="+e(t[1])},"").join("&")};const helpers_AxiosURLSearchParams=AxiosURLSearchParams;function buildURL_encode(t){return encodeURIComponent(t).replace(/%3A/gi,":").replace(/%24/g,"$").replace(/%2C/gi,",").replace(/%20/g,"+")}function buildURL(t,e,n){if(!e)return t;const r=n&&n.encode||buildURL_encode;utils.isFunction(n)&&(n={serialize:n});const i=n&&n.serialize;let o;if(o=i?i(e,n):utils.isURLSearchParams(e)?e.toString():new helpers_AxiosURLSearchParams(e,n).toString(r),o){const e=t.indexOf("#");-1!==e&&(t=t.slice(0,e)),t+=(-1===t.indexOf("?")?"?":"&")+o}return t}class InterceptorManager{constructor(){this.handlers=[]}use(t,e,n){return this.handlers.push({fulfilled:t,rejected:e,synchronous:!!n&&n.synchronous,runWhen:n?n.runWhen:null}),this.handlers.length-1}eject(t){this.handlers[t]&&(this.handlers[t]=null)}clear(){this.handlers&&(this.handlers=[])}forEach(t){utils.forEach(this.handlers,function(e){null!==e&&t(e)})}}const core_InterceptorManager=InterceptorManager,defaults_transitional={silentJSONParsing:!0,forcedJSONParsing:!0,clarifyTimeoutError:!1},classes_URLSearchParams="undefined"!=typeof URLSearchParams?URLSearchParams:helpers_AxiosURLSearchParams,classes_FormData="undefined"!=typeof FormData?FormData:null,classes_Blob="undefined"!=typeof Blob?Blob:null,browser={isBrowser:!0,classes:{URLSearchParams:classes_URLSearchParams,FormData:classes_FormData,Blob:classes_Blob},protocols:["http","https","file","blob","url","data"]},hasBrowserEnv="undefined"!=typeof window&&"undefined"!=typeof document,_navigator="object"==typeof navigator&&navigator||void 0,hasStandardBrowserEnv=hasBrowserEnv&&(!_navigator||["ReactNative","NativeScript","NS"].indexOf(_navigator.product)<0),hasStandardBrowserWebWorkerEnv="undefined"!=typeof WorkerGlobalScope&&self instanceof WorkerGlobalScope&&"function"==typeof self.importScripts,origin=hasBrowserEnv&&window.location.href||"http://localhost",platform={...common_utils_namespaceObject,...browser};function toURLEncodedForm(t,e){return helpers_toFormData(t,new platform.classes.URLSearchParams,{visitor:function(t,e,n,r){return platform.isNode&&utils.isBuffer(t)?(this.append(e,t.toString("base64")),!1):r.defaultVisitor.apply(this,arguments)},...e})}function parsePropPath(t){return utils.matchAll(/\w+|\[(\w*)]/g,t).map(t=>"[]"===t[0]?"":t[1]||t[0])}function arrayToObject(t){const e={},n=Object.keys(t);let r;const i=n.length;let o;for(r=0;r<i;r++)o=n[r],e[o]=t[o];return e}function formDataToJSON(t){function e(t,n,r,i){let o=t[i++];if("__proto__"===o)return!0;const s=Number.isFinite(+o),a=i>=t.length;return o=!o&&utils.isArray(r)?r.length:o,a?(utils.hasOwnProp(r,o)?r[o]=[r[o],n]:r[o]=n,!s):(r[o]&&utils.isObject(r[o])||(r[o]=[]),e(t,n,r[o],i)&&utils.isArray(r[o])&&(r[o]=arrayToObject(r[o])),!s)}if(utils.isFormData(t)&&utils.isFunction(t.entries)){const n={};return utils.forEachEntry(t,(t,r)=>{e(parsePropPath(t),r,n,0)}),n}return null}const helpers_formDataToJSON=formDataToJSON;function stringifySafely(t,e,n){if(utils.isString(t))try{return(e||JSON.parse)(t),utils.trim(t)}catch(t){if("SyntaxError"!==t.name)throw t}return(n||JSON.stringify)(t)}const defaults={transitional:defaults_transitional,adapter:["xhr","http","fetch"],transformRequest:[function(t,e){const n=e.getContentType()||"",r=n.indexOf("application/json")>-1,i=utils.isObject(t);if(i&&utils.isHTMLForm(t)&&(t=new FormData(t)),utils.isFormData(t))return r?JSON.stringify(helpers_formDataToJSON(t)):t;if(utils.isArrayBuffer(t)||utils.isBuffer(t)||utils.isStream(t)||utils.isFile(t)||utils.isBlob(t)||utils.isReadableStream(t))return t;if(utils.isArrayBufferView(t))return t.buffer;if(utils.isURLSearchParams(t))return e.setContentType("application/x-www-form-urlencoded;charset=utf-8",!1),t.toString();let o;if(i){if(n.indexOf("application/x-www-form-urlencoded")>-1)return toURLEncodedForm(t,this.formSerializer).toString();if((o=utils.isFileList(t))||n.indexOf("multipart/form-data")>-1){const e=this.env&&this.env.FormData;return helpers_toFormData(o?{"files[]":t}:t,e&&new e,this.formSerializer)}}return i||r?(e.setContentType("application/json",!1),stringifySafely(t)):t}],transformResponse:[function(t){const e=this.transitional||defaults.transitional,n=e&&e.forcedJSONParsing,r="json"===this.responseType;if(utils.isResponse(t)||utils.isReadableStream(t))return t;if(t&&utils.isString(t)&&(n&&!this.responseType||r)){const n=!(e&&e.silentJSONParsing)&&r;try{return JSON.parse(t,this.parseReviver)}catch(t){if(n){if("SyntaxError"===t.name)throw core_AxiosError.from(t,core_AxiosError.ERR_BAD_RESPONSE,this,null,this.response);throw t}}}return t}],timeout:0,xsrfCookieName:"XSRF-TOKEN",xsrfHeaderName:"X-XSRF-TOKEN",maxContentLength:-1,maxBodyLength:-1,env:{FormData:platform.classes.FormData,Blob:platform.classes.Blob},validateStatus:function(t){return t>=200&&t<300},headers:{common:{Accept:"application/json, text/plain, */*","Content-Type":void 0}}};utils.forEach(["delete","get","head","post","put","patch"],t=>{defaults.headers[t]={}});const lib_defaults=defaults,ignoreDuplicateOf=utils.toObjectSet(["age","authorization","content-length","content-type","etag","expires","from","host","if-modified-since","if-unmodified-since","last-modified","location","max-forwards","proxy-authorization","referer","retry-after","user-agent"]),parseHeaders=t=>{const e={};let n,r,i;return t&&t.split("\n").forEach(function(t){i=t.indexOf(":"),n=t.substring(0,i).trim().toLowerCase(),r=t.substring(i+1).trim(),!n||e[n]&&ignoreDuplicateOf[n]||("set-cookie"===n?e[n]?e[n].push(r):e[n]=[r]:e[n]=e[n]?e[n]+", "+r:r)}),e},$internals=Symbol("internals");function normalizeHeader(t){return t&&String(t).trim().toLowerCase()}function normalizeValue(t){return!1===t||null==t?t:utils.isArray(t)?t.map(normalizeValue):String(t)}function parseTokens(t){const e=Object.create(null),n=/([^\s,;=]+)\s*(?:=\s*([^,;]+))?/g;let r;for(;r=n.exec(t);)e[r[1]]=r[2];return e}const isValidHeaderName=t=>/^[-_a-zA-Z0-9^`|~,!#$%&'*+.]+$/.test(t.trim());function matchHeaderValue(t,e,n,r,i){return utils.isFunction(r)?r.call(this,e,n):(i&&(e=n),utils.isString(e)?utils.isString(r)?-1!==e.indexOf(r):utils.isRegExp(r)?r.test(e):void 0:void 0)}function formatHeader(t){return t.trim().toLowerCase().replace(/([a-z\d])(\w*)/g,(t,e,n)=>e.toUpperCase()+n)}function buildAccessors(t,e){const n=utils.toCamelCase(" "+e);["get","set","has"].forEach(r=>{Object.defineProperty(t,r+n,{value:function(t,n,i){return this[r].call(this,e,t,n,i)},configurable:!0})})}class AxiosHeaders{constructor(t){t&&this.set(t)}set(t,e,n){const r=this;function i(t,e,n){const i=normalizeHeader(e);if(!i)throw new Error("header name must be a non-empty string");const o=utils.findKey(r,i);(!o||void 0===r[o]||!0===n||void 0===n&&!1!==r[o])&&(r[o||e]=normalizeValue(t))}const o=(t,e)=>utils.forEach(t,(t,n)=>i(t,n,e));if(utils.isPlainObject(t)||t instanceof this.constructor)o(t,e);else if(utils.isString(t)&&(t=t.trim())&&!isValidHeaderName(t))o(parseHeaders(t),e);else if(utils.isObject(t)&&utils.isIterable(t)){let n,r,i={};for(const e of t){if(!utils.isArray(e))throw TypeError("Object iterator must return a key-value pair");i[r=e[0]]=(n=i[r])?utils.isArray(n)?[...n,e[1]]:[n,e[1]]:e[1]}o(i,e)}else null!=t&&i(e,t,n);return this}get(t,e){if(t=normalizeHeader(t)){const n=utils.findKey(this,t);if(n){const t=this[n];if(!e)return t;if(!0===e)return parseTokens(t);if(utils.isFunction(e))return e.call(this,t,n);if(utils.isRegExp(e))return e.exec(t);throw new TypeError("parser must be boolean|regexp|function")}}}has(t,e){if(t=normalizeHeader(t)){const n=utils.findKey(this,t);return!(!n||void 0===this[n]||e&&!matchHeaderValue(this,this[n],n,e))}return!1}delete(t,e){const n=this;let r=!1;function i(t){if(t=normalizeHeader(t)){const i=utils.findKey(n,t);!i||e&&!matchHeaderValue(n,n[i],i,e)||(delete n[i],r=!0)}}return utils.isArray(t)?t.forEach(i):i(t),r}clear(t){const e=Object.keys(this);let n=e.length,r=!1;for(;n--;){const i=e[n];t&&!matchHeaderValue(this,this[i],i,t,!0)||(delete this[i],r=!0)}return r}normalize(t){const e=this,n={};return utils.forEach(this,(r,i)=>{const o=utils.findKey(n,i);if(o)return e[o]=normalizeValue(r),void delete e[i];const s=t?formatHeader(i):String(i).trim();s!==i&&delete e[i],e[s]=normalizeValue(r),n[s]=!0}),this}concat(...t){return this.constructor.concat(this,...t)}toJSON(t){const e=Object.create(null);return utils.forEach(this,(n,r)=>{null!=n&&!1!==n&&(e[r]=t&&utils.isArray(n)?n.join(", "):n)}),e}[Symbol.iterator](){return Object.entries(this.toJSON())[Symbol.iterator]()}toString(){return Object.entries(this.toJSON()).map(([t,e])=>t+": "+e).join("\n")}getSetCookie(){return this.get("set-cookie")||[]}get[Symbol.toStringTag](){return"AxiosHeaders"}static from(t){return t instanceof this?t:new this(t)}static concat(t,...e){const n=new this(t);return e.forEach(t=>n.set(t)),n}static accessor(t){const e=(this[$internals]=this[$internals]={accessors:{}}).accessors,n=this.prototype;function r(t){const r=normalizeHeader(t);e[r]||(buildAccessors(n,t),e[r]=!0)}return utils.isArray(t)?t.forEach(r):r(t),this}}AxiosHeaders.accessor(["Content-Type","Content-Length","Accept","Accept-Encoding","User-Agent","Authorization"]),utils.reduceDescriptors(AxiosHeaders.prototype,({value:t},e)=>{let n=e[0].toUpperCase()+e.slice(1);return{get:()=>t,set(t){this[n]=t}}}),utils.freezeMethods(AxiosHeaders);const core_AxiosHeaders=AxiosHeaders;function transformData(t,e){const n=this||lib_defaults,r=e||n,i=core_AxiosHeaders.from(r.headers);let o=r.data;return utils.forEach(t,function(t){o=t.call(n,o,i.normalize(),e?e.status:void 0)}),i.normalize(),o}function isCancel(t){return!(!t||!t.__CANCEL__)}function CanceledError(t,e,n){core_AxiosError.call(this,null==t?"canceled":t,core_AxiosError.ERR_CANCELED,e,n),this.name="CanceledError"}utils.inherits(CanceledError,core_AxiosError,{__CANCEL__:!0});const cancel_CanceledError=CanceledError;function settle(t,e,n){const r=n.config.validateStatus;n.status&&r&&!r(n.status)?e(new core_AxiosError("Request failed with status code "+n.status,[core_AxiosError.ERR_BAD_REQUEST,core_AxiosError.ERR_BAD_RESPONSE][Math.floor(n.status/100)-4],n.config,n.request,n)):t(n)}function parseProtocol(t){const e=/^([-+\w]{1,25})(:?\/\/|:)/.exec(t);return e&&e[1]||""}function speedometer(t,e){t=t||10;const n=new Array(t),r=new Array(t);let i,o=0,s=0;return e=void 0!==e?e:1e3,function(a){const u=Date.now(),l=r[s];i||(i=u),n[o]=a,r[o]=u;let c=s,d=0;for(;c!==o;)d+=n[c++],c%=t;if(o=(o+1)%t,o===s&&(s=(s+1)%t),u-i<e)return;const h=l&&u-l;return h?Math.round(1e3*d/h):void 0}}const helpers_speedometer=speedometer;function throttle(t,e){let n,r,i=0,o=1e3/e;const s=(e,o=Date.now())=>{i=o,n=null,r&&(clearTimeout(r),r=null),t(...e)};return[(...t)=>{const e=Date.now(),a=e-i;a>=o?s(t,e):(n=t,r||(r=setTimeout(()=>{r=null,s(n)},o-a)))},()=>n&&s(n)]}const helpers_throttle=throttle,progressEventReducer=(t,e,n=3)=>{let r=0;const i=helpers_speedometer(50,250);return helpers_throttle(n=>{const o=n.loaded,s=n.lengthComputable?n.total:void 0,a=o-r,u=i(a);r=o,t({loaded:o,total:s,progress:s?o/s:void 0,bytes:a,rate:u||void 0,estimated:u&&s&&o<=s?(s-o)/u:void 0,event:n,lengthComputable:null!=s,[e?"download":"upload"]:!0})},n)},progressEventDecorator=(t,e)=>{const n=null!=t;return[r=>e[0]({lengthComputable:n,total:t,loaded:r}),e[1]]},asyncDecorator=t=>(...e)=>utils.asap(()=>t(...e)),isURLSameOrigin=platform.hasStandardBrowserEnv?((t,e)=>n=>(n=new URL(n,platform.origin),t.protocol===n.protocol&&t.host===n.host&&(e||t.port===n.port)))(new URL(platform.origin),platform.navigator&&/(msie|trident)/i.test(platform.navigator.userAgent)):()=>!0,cookies=platform.hasStandardBrowserEnv?{write(t,e,n,r,i,o,s){if("undefined"==typeof document)return;const a=[`${t}=${encodeURIComponent(e)}`];utils.isNumber(n)&&a.push(`expires=${new Date(n).toUTCString()}`),utils.isString(r)&&a.push(`path=${r}`),utils.isString(i)&&a.push(`domain=${i}`),!0===o&&a.push("secure"),utils.isString(s)&&a.push(`SameSite=${s}`),document.cookie=a.join("; ")},read(t){if("undefined"==typeof document)return null;const e=document.cookie.match(new RegExp("(?:^|; )"+t+"=([^;]*)"));return e?decodeURIComponent(e[1]):null},remove(t){this.write(t,"",Date.now()-864e5,"/")}}:{write(){},read:()=>null,remove(){}};function isAbsoluteURL(t){return/^([a-z][a-z\d+\-.]*:)?\/\//i.test(t)}function combineURLs(t,e){return e?t.replace(/\/?\/$/,"")+"/"+e.replace(/^\/+/,""):t}function buildFullPath(t,e,n){let r=!isAbsoluteURL(e);return t&&(r||0==n)?combineURLs(t,e):e}const headersToObject=t=>t instanceof core_AxiosHeaders?{...t}:t;function mergeConfig(t,e){e=e||{};const n={};function r(t,e,n,r){return utils.isPlainObject(t)&&utils.isPlainObject(e)?utils.merge.call({caseless:r},t,e):utils.isPlainObject(e)?utils.merge({},e):utils.isArray(e)?e.slice():e}function i(t,e,n,i){return utils.isUndefined(e)?utils.isUndefined(t)?void 0:r(void 0,t,0,i):r(t,e,0,i)}function o(t,e){if(!utils.isUndefined(e))return r(void 0,e)}function s(t,e){return utils.isUndefined(e)?utils.isUndefined(t)?void 0:r(void 0,t):r(void 0,e)}function a(n,i,o){return o in e?r(n,i):o in t?r(void 0,n):void 0}const u={url:o,method:o,data:o,baseURL:s,transformRequest:s,transformResponse:s,paramsSerializer:s,timeout:s,timeoutMessage:s,withCredentials:s,withXSRFToken:s,adapter:s,responseType:s,xsrfCookieName:s,xsrfHeaderName:s,onUploadProgress:s,onDownloadProgress:s,decompress:s,maxContentLength:s,maxBodyLength:s,beforeRedirect:s,transport:s,httpAgent:s,httpsAgent:s,cancelToken:s,socketPath:s,responseEncoding:s,validateStatus:a,headers:(t,e,n)=>i(headersToObject(t),headersToObject(e),0,!0)};return utils.forEach(Object.keys({...t,...e}),function(r){const o=u[r]||i,s=o(t[r],e[r],r);utils.isUndefined(s)&&o!==a||(n[r]=s)}),n}const resolveConfig=t=>{const e=mergeConfig({},t);let{data:n,withXSRFToken:r,xsrfHeaderName:i,xsrfCookieName:o,headers:s,auth:a}=e;if(e.headers=s=core_AxiosHeaders.from(s),e.url=buildURL(buildFullPath(e.baseURL,e.url,e.allowAbsoluteUrls),t.params,t.paramsSerializer),a&&s.set("Authorization","Basic "+btoa((a.username||"")+":"+(a.password?unescape(encodeURIComponent(a.password)):""))),utils.isFormData(n))if(platform.hasStandardBrowserEnv||platform.hasStandardBrowserWebWorkerEnv)s.setContentType(void 0);else if(utils.isFunction(n.getHeaders)){const t=n.getHeaders(),e=["content-type","content-length"];Object.entries(t).forEach(([t,n])=>{e.includes(t.toLowerCase())&&s.set(t,n)})}if(platform.hasStandardBrowserEnv&&(r&&utils.isFunction(r)&&(r=r(e)),r||!1!==r&&isURLSameOrigin(e.url))){const t=i&&o&&cookies.read(o);t&&s.set(i,t)}return e},isXHRAdapterSupported="undefined"!=typeof XMLHttpRequest,xhr=isXHRAdapterSupported&&function(t){return new Promise(function(e,n){const r=resolveConfig(t);let i=r.data;const o=core_AxiosHeaders.from(r.headers).normalize();let s,a,u,l,c,{responseType:d,onUploadProgress:h,onDownloadProgress:p}=r;function f(){l&&l(),c&&c(),r.cancelToken&&r.cancelToken.unsubscribe(s),r.signal&&r.signal.removeEventListener("abort",s)}let g=new XMLHttpRequest;function m(){if(!g)return;const r=core_AxiosHeaders.from("getAllResponseHeaders"in g&&g.getAllResponseHeaders());settle(function(t){e(t),f()},function(t){n(t),f()},{data:d&&"text"!==d&&"json"!==d?g.response:g.responseText,status:g.status,statusText:g.statusText,headers:r,config:t,request:g}),g=null}g.open(r.method.toUpperCase(),r.url,!0),g.timeout=r.timeout,"onloadend"in g?g.onloadend=m:g.onreadystatechange=function(){g&&4===g.readyState&&(0!==g.status||g.responseURL&&0===g.responseURL.indexOf("file:"))&&setTimeout(m)},g.onabort=function(){g&&(n(new core_AxiosError("Request aborted",core_AxiosError.ECONNABORTED,t,g)),g=null)},g.onerror=function(e){const r=e&&e.message?e.message:"Network Error",i=new core_AxiosError(r,core_AxiosError.ERR_NETWORK,t,g);i.event=e||null,n(i),g=null},g.ontimeout=function(){let e=r.timeout?"timeout of "+r.timeout+"ms exceeded":"timeout exceeded";const i=r.transitional||defaults_transitional;r.timeoutErrorMessage&&(e=r.timeoutErrorMessage),n(new core_AxiosError(e,i.clarifyTimeoutError?core_AxiosError.ETIMEDOUT:core_AxiosError.ECONNABORTED,t,g)),g=null},void 0===i&&o.setContentType(null),"setRequestHeader"in g&&utils.forEach(o.toJSON(),function(t,e){g.setRequestHeader(e,t)}),utils.isUndefined(r.withCredentials)||(g.withCredentials=!!r.withCredentials),d&&"json"!==d&&(g.responseType=r.responseType),p&&([u,c]=progressEventReducer(p,!0),g.addEventListener("progress",u)),h&&g.upload&&([a,l]=progressEventReducer(h),g.upload.addEventListener("progress",a),g.upload.addEventListener("loadend",l)),(r.cancelToken||r.signal)&&(s=e=>{g&&(n(!e||e.type?new cancel_CanceledError(null,t,g):e),g.abort(),g=null)},r.cancelToken&&r.cancelToken.subscribe(s),r.signal&&(r.signal.aborted?s():r.signal.addEventListener("abort",s)));const b=parseProtocol(r.url);b&&-1===platform.protocols.indexOf(b)?n(new core_AxiosError("Unsupported protocol "+b+":",core_AxiosError.ERR_BAD_REQUEST,t)):g.send(i||null)})},composeSignals=(t,e)=>{const{length:n}=t=t?t.filter(Boolean):[];if(e||n){let n,r=new AbortController;const i=function(t){if(!n){n=!0,s();const e=t instanceof Error?t:this.reason;r.abort(e instanceof core_AxiosError?e:new cancel_CanceledError(e instanceof Error?e.message:e))}};let o=e&&setTimeout(()=>{o=null,i(new core_AxiosError(`timeout ${e} of ms exceeded`,core_AxiosError.ETIMEDOUT))},e);const s=()=>{t&&(o&&clearTimeout(o),o=null,t.forEach(t=>{t.unsubscribe?t.unsubscribe(i):t.removeEventListener("abort",i)}),t=null)};t.forEach(t=>t.addEventListener("abort",i));const{signal:a}=r;return a.unsubscribe=()=>utils.asap(s),a}},helpers_composeSignals=composeSignals,streamChunk=function*(t,e){let n=t.byteLength;if(!e||n<e)return void(yield t);let r,i=0;for(;i<n;)r=i+e,yield t.slice(i,r),i=r},readBytes=async function*(t,e){for await(const n of readStream(t))yield*streamChunk(n,e)},readStream=async function*(t){if(t[Symbol.asyncIterator])return void(yield*t);const e=t.getReader();try{for(;;){const{done:t,value:n}=await e.read();if(t)break;yield n}}finally{await e.cancel()}},trackStream=(t,e,n,r)=>{const i=readBytes(t,e);let o,s=0,a=t=>{o||(o=!0,r&&r(t))};return new ReadableStream({async pull(t){try{const{done:e,value:r}=await i.next();if(e)return a(),void t.close();let o=r.byteLength;if(n){let t=s+=o;n(t)}t.enqueue(new Uint8Array(r))}catch(t){throw a(t),t}},cancel:t=>(a(t),i.return())},{highWaterMark:2})},DEFAULT_CHUNK_SIZE=65536,{isFunction:fetch_isFunction}=utils,globalFetchAPI=(({Request:t,Response:e})=>({Request:t,Response:e}))(utils.global),{ReadableStream:fetch_ReadableStream,TextEncoder}=utils.global,test=(t,...e)=>{try{return!!t(...e)}catch(t){return!1}},factory=t=>{t=utils.merge.call({skipUndefined:!0},globalFetchAPI,t);const{fetch:e,Request:n,Response:r}=t,i=e?fetch_isFunction(e):"function"==typeof fetch,o=fetch_isFunction(n),s=fetch_isFunction(r);if(!i)return!1;const a=i&&fetch_isFunction(fetch_ReadableStream),u=i&&("function"==typeof TextEncoder?(l=new TextEncoder,t=>l.encode(t)):async t=>new Uint8Array(await new n(t).arrayBuffer()));var l;const c=o&&a&&test(()=>{let t=!1;const e=new n(platform.origin,{body:new fetch_ReadableStream,method:"POST",get duplex(){return t=!0,"half"}}).headers.has("Content-Type");return t&&!e}),d=s&&a&&test(()=>utils.isReadableStream(new r("").body)),h={stream:d&&(t=>t.body)};i&&["text","arrayBuffer","blob","formData","stream"].forEach(t=>{!h[t]&&(h[t]=(e,n)=>{let r=e&&e[t];if(r)return r.call(e);throw new core_AxiosError(`Response type '${t}' is not supported`,core_AxiosError.ERR_NOT_SUPPORT,n)})});return async t=>{let{url:i,method:s,data:a,signal:l,cancelToken:p,timeout:f,onDownloadProgress:g,onUploadProgress:m,responseType:b,headers:y,withCredentials:_="same-origin",fetchOptions:w}=resolveConfig(t),v=e||fetch;b=b?(b+"").toLowerCase():"text";let x=helpers_composeSignals([l,p&&p.toAbortSignal()],f),T=null;const S=x&&x.unsubscribe&&(()=>{x.unsubscribe()});let A;try{if(m&&c&&"get"!==s&&"head"!==s&&0!==(A=await(async(t,e)=>{const r=utils.toFiniteNumber(t.getContentLength());return null==r?(async t=>{if(null==t)return 0;if(utils.isBlob(t))return t.size;if(utils.isSpecCompliantForm(t)){const e=new n(platform.origin,{method:"POST",body:t});return(await e.arrayBuffer()).byteLength}return utils.isArrayBufferView(t)||utils.isArrayBuffer(t)?t.byteLength:(utils.isURLSearchParams(t)&&(t+=""),utils.isString(t)?(await u(t)).byteLength:void 0)})(e):r})(y,a))){let t,e=new n(i,{method:"POST",body:a,duplex:"half"});if(utils.isFormData(a)&&(t=e.headers.get("content-type"))&&y.setContentType(t),e.body){const[t,n]=progressEventDecorator(A,progressEventReducer(asyncDecorator(m)));a=trackStream(e.body,DEFAULT_CHUNK_SIZE,t,n)}}utils.isString(_)||(_=_?"include":"omit");const e=o&&"credentials"in n.prototype,l={...w,signal:x,method:s.toUpperCase(),headers:y.normalize().toJSON(),body:a,duplex:"half",credentials:e?_:void 0};T=o&&new n(i,l);let p=await(o?v(T,w):v(i,l));const f=d&&("stream"===b||"response"===b);if(d&&(g||f&&S)){const t={};["status","statusText","headers"].forEach(e=>{t[e]=p[e]});const e=utils.toFiniteNumber(p.headers.get("content-length")),[n,i]=g&&progressEventDecorator(e,progressEventReducer(asyncDecorator(g),!0))||[];p=new r(trackStream(p.body,DEFAULT_CHUNK_SIZE,n,()=>{i&&i(),S&&S()}),t)}b=b||"text";let E=await h[utils.findKey(h,b)||"text"](p,t);return!f&&S&&S(),await new Promise((e,n)=>{settle(e,n,{data:E,headers:core_AxiosHeaders.from(p.headers),status:p.status,statusText:p.statusText,config:t,request:T})})}catch(e){if(S&&S(),e&&"TypeError"===e.name&&/Load failed|fetch/i.test(e.message))throw Object.assign(new core_AxiosError("Network Error",core_AxiosError.ERR_NETWORK,t,T),{cause:e.cause||e});throw core_AxiosError.from(e,e&&e.code,t,T)}}},seedCache=new Map,getFetch=t=>{let e=t&&t.env||{};const{fetch:n,Request:r,Response:i}=e,o=[r,i,n];let s,a,u=o.length,l=seedCache;for(;u--;)s=o[u],a=l.get(s),void 0===a&&l.set(s,a=u?new Map:factory(e)),l=a;return a},adapter=getFetch(),adapters_fetch=null,knownAdapters={http:helpers_null,xhr,fetch:{get:getFetch}};utils.forEach(knownAdapters,(t,e)=>{if(t){try{Object.defineProperty(t,"name",{value:e})}catch(t){}Object.defineProperty(t,"adapterName",{value:e})}});const renderReason=t=>`- ${t}`,isResolvedHandle=t=>utils.isFunction(t)||null===t||!1===t;function getAdapter(t,e){t=utils.isArray(t)?t:[t];const{length:n}=t;let r,i;const o={};for(let s=0;s<n;s++){let n;if(r=t[s],i=r,!isResolvedHandle(r)&&(i=knownAdapters[(n=String(r)).toLowerCase()],void 0===i))throw new core_AxiosError(`Unknown adapter '${n}'`);if(i&&(utils.isFunction(i)||(i=i.get(e))))break;o[n||"#"+s]=i}if(!i){const t=Object.entries(o).map(([t,e])=>`adapter ${t} `+(!1===e?"is not supported by the environment":"is not available in the build"));let e=n?t.length>1?"since :\n"+t.map(renderReason).join("\n"):" "+renderReason(t[0]):"as no adapter specified";throw new core_AxiosError("There is no suitable adapter to dispatch the request "+e,"ERR_NOT_SUPPORT")}return i}const adapters={getAdapter,adapters:knownAdapters};function throwIfCancellationRequested(t){if(t.cancelToken&&t.cancelToken.throwIfRequested(),t.signal&&t.signal.aborted)throw new cancel_CanceledError(null,t)}function dispatchRequest(t){return throwIfCancellationRequested(t),t.headers=core_AxiosHeaders.from(t.headers),t.data=transformData.call(t,t.transformRequest),-1!==["post","put","patch"].indexOf(t.method)&&t.headers.setContentType("application/x-www-form-urlencoded",!1),adapters.getAdapter(t.adapter||lib_defaults.adapter,t)(t).then(function(e){return throwIfCancellationRequested(t),e.data=transformData.call(t,t.transformResponse,e),e.headers=core_AxiosHeaders.from(e.headers),e},function(e){return isCancel(e)||(throwIfCancellationRequested(t),e&&e.response&&(e.response.data=transformData.call(t,t.transformResponse,e.response),e.response.headers=core_AxiosHeaders.from(e.response.headers))),Promise.reject(e)})}const VERSION="1.13.2",validators={};["object","boolean","number","function","string","symbol"].forEach((t,e)=>{validators[t]=function(n){return typeof n===t||"a"+(e<1?"n ":" ")+t}});const deprecatedWarnings={};function assertOptions(t,e,n){if("object"!=typeof t)throw new core_AxiosError("options must be an object",core_AxiosError.ERR_BAD_OPTION_VALUE);const r=Object.keys(t);let i=r.length;for(;i-- >0;){const o=r[i],s=e[o];if(s){const e=t[o],n=void 0===e||s(e,o,t);if(!0!==n)throw new core_AxiosError("option "+o+" must be "+n,core_AxiosError.ERR_BAD_OPTION_VALUE);continue}if(!0!==n)throw new core_AxiosError("Unknown option "+o,core_AxiosError.ERR_BAD_OPTION)}}validators.transitional=function(t,e,n){function r(t,e){return"[Axios v"+VERSION+"] Transitional option '"+t+"'"+e+(n?". "+n:"")}return(n,i,o)=>{if(!1===t)throw new core_AxiosError(r(i," has been removed"+(e?" in "+e:"")),core_AxiosError.ERR_DEPRECATED);return e&&!deprecatedWarnings[i]&&(deprecatedWarnings[i]=!0,console.warn(r(i," has been deprecated since v"+e+" and will be removed in the near future"))),!t||t(n,i,o)}},validators.spelling=function(t){return(e,n)=>(console.warn(`${n} is likely a misspelling of ${t}`),!0)};const validator={assertOptions,validators},Axios_validators=validator.validators;class Axios{constructor(t){this.defaults=t||{},this.interceptors={request:new core_InterceptorManager,response:new core_InterceptorManager}}async request(t,e){try{return await this._request(t,e)}catch(t){if(t instanceof Error){let e={};Error.captureStackTrace?Error.captureStackTrace(e):e=new Error;const n=e.stack?e.stack.replace(/^.+\n/,""):"";try{t.stack?n&&!String(t.stack).endsWith(n.replace(/^.+\n.+\n/,""))&&(t.stack+="\n"+n):t.stack=n}catch(t){}}throw t}}_request(t,e){"string"==typeof t?(e=e||{}).url=t:e=t||{},e=mergeConfig(this.defaults,e);const{transitional:n,paramsSerializer:r,headers:i}=e;void 0!==n&&validator.assertOptions(n,{silentJSONParsing:Axios_validators.transitional(Axios_validators.boolean),forcedJSONParsing:Axios_validators.transitional(Axios_validators.boolean),clarifyTimeoutError:Axios_validators.transitional(Axios_validators.boolean)},!1),null!=r&&(utils.isFunction(r)?e.paramsSerializer={serialize:r}:validator.assertOptions(r,{encode:Axios_validators.function,serialize:Axios_validators.function},!0)),void 0!==e.allowAbsoluteUrls||(void 0!==this.defaults.allowAbsoluteUrls?e.allowAbsoluteUrls=this.defaults.allowAbsoluteUrls:e.allowAbsoluteUrls=!0),validator.assertOptions(e,{baseUrl:Axios_validators.spelling("baseURL"),withXsrfToken:Axios_validators.spelling("withXSRFToken")},!0),e.method=(e.method||this.defaults.method||"get").toLowerCase();let o=i&&utils.merge(i.common,i[e.method]);i&&utils.forEach(["delete","get","head","post","put","patch","common"],t=>{delete i[t]}),e.headers=core_AxiosHeaders.concat(o,i);const s=[];let a=!0;this.interceptors.request.forEach(function(t){"function"==typeof t.runWhen&&!1===t.runWhen(e)||(a=a&&t.synchronous,s.unshift(t.fulfilled,t.rejected))});const u=[];let l;this.interceptors.response.forEach(function(t){u.push(t.fulfilled,t.rejected)});let c,d=0;if(!a){const t=[dispatchRequest.bind(this),void 0];for(t.unshift(...s),t.push(...u),c=t.length,l=Promise.resolve(e);d<c;)l=l.then(t[d++],t[d++]);return l}c=s.length;let h=e;for(;d<c;){const t=s[d++],e=s[d++];try{h=t(h)}catch(t){e.call(this,t);break}}try{l=dispatchRequest.call(this,h)}catch(t){return Promise.reject(t)}for(d=0,c=u.length;d<c;)l=l.then(u[d++],u[d++]);return l}getUri(t){return buildURL(buildFullPath((t=mergeConfig(this.defaults,t)).baseURL,t.url,t.allowAbsoluteUrls),t.params,t.paramsSerializer)}}utils.forEach(["delete","get","head","options"],function(t){Axios.prototype[t]=function(e,n){return this.request(mergeConfig(n||{},{method:t,url:e,data:(n||{}).data}))}}),utils.forEach(["post","put","patch"],function(t){function e(e){return function(n,r,i){return this.request(mergeConfig(i||{},{method:t,headers:e?{"Content-Type":"multipart/form-data"}:{},url:n,data:r}))}}Axios.prototype[t]=e(),Axios.prototype[t+"Form"]=e(!0)});const core_Axios=Axios;class CancelToken{constructor(t){if("function"!=typeof t)throw new TypeError("executor must be a function.");let e;this.promise=new Promise(function(t){e=t});const n=this;this.promise.then(t=>{if(!n._listeners)return;let e=n._listeners.length;for(;e-- >0;)n._listeners[e](t);n._listeners=null}),this.promise.then=t=>{let e;const r=new Promise(t=>{n.subscribe(t),e=t}).then(t);return r.cancel=function(){n.unsubscribe(e)},r},t(function(t,r,i){n.reason||(n.reason=new cancel_CanceledError(t,r,i),e(n.reason))})}throwIfRequested(){if(this.reason)throw this.reason}subscribe(t){this.reason?t(this.reason):this._listeners?this._listeners.push(t):this._listeners=[t]}unsubscribe(t){if(!this._listeners)return;const e=this._listeners.indexOf(t);-1!==e&&this._listeners.splice(e,1)}toAbortSignal(){const t=new AbortController,e=e=>{t.abort(e)};return this.subscribe(e),t.signal.unsubscribe=()=>this.unsubscribe(e),t.signal}static source(){let t;return{token:new CancelToken(function(e){t=e}),cancel:t}}}const cancel_CancelToken=CancelToken;function spread(t){return function(e){return t.apply(null,e)}}function isAxiosError(t){return utils.isObject(t)&&!0===t.isAxiosError}const HttpStatusCode={Continue:100,SwitchingProtocols:101,Processing:102,EarlyHints:103,Ok:200,Created:201,Accepted:202,NonAuthoritativeInformation:203,NoContent:204,ResetContent:205,PartialContent:206,MultiStatus:207,AlreadyReported:208,ImUsed:226,MultipleChoices:300,MovedPermanently:301,Found:302,SeeOther:303,NotModified:304,UseProxy:305,Unused:306,TemporaryRedirect:307,PermanentRedirect:308,BadRequest:400,Unauthorized:401,PaymentRequired:402,Forbidden:403,NotFound:404,MethodNotAllowed:405,NotAcceptable:406,ProxyAuthenticationRequired:407,RequestTimeout:408,Conflict:409,Gone:410,LengthRequired:411,PreconditionFailed:412,PayloadTooLarge:413,UriTooLong:414,UnsupportedMediaType:415,RangeNotSatisfiable:416,ExpectationFailed:417,ImATeapot:418,MisdirectedRequest:421,UnprocessableEntity:422,Locked:423,FailedDependency:424,TooEarly:425,UpgradeRequired:426,PreconditionRequired:428,TooManyRequests:429,RequestHeaderFieldsTooLarge:431,UnavailableForLegalReasons:451,InternalServerError:500,NotImplemented:501,BadGateway:502,ServiceUnavailable:503,GatewayTimeout:504,HttpVersionNotSupported:505,VariantAlsoNegotiates:506,InsufficientStorage:507,LoopDetected:508,NotExtended:510,NetworkAuthenticationRequired:511,WebServerIsDown:521,ConnectionTimedOut:522,OriginIsUnreachable:523,TimeoutOccurred:524,SslHandshakeFailed:525,InvalidSslCertificate:526};Object.entries(HttpStatusCode).forEach(([t,e])=>{HttpStatusCode[e]=t});const helpers_HttpStatusCode=HttpStatusCode;function createInstance(t){const e=new core_Axios(t),n=bind(core_Axios.prototype.request,e);return utils.extend(n,core_Axios.prototype,e,{allOwnKeys:!0}),utils.extend(n,e,null,{allOwnKeys:!0}),n.create=function(e){return createInstance(mergeConfig(t,e))},n}const axios=createInstance(lib_defaults);axios.Axios=core_Axios,axios.CanceledError=cancel_CanceledError,axios.CancelToken=cancel_CancelToken,axios.isCancel=isCancel,axios.VERSION=VERSION,axios.toFormData=helpers_toFormData,axios.AxiosError=core_AxiosError,axios.Cancel=axios.CanceledError,axios.all=function(t){return Promise.all(t)},axios.spread=spread,axios.isAxiosError=isAxiosError,axios.mergeConfig=mergeConfig,axios.AxiosHeaders=core_AxiosHeaders,axios.formToJSON=t=>helpers_formDataToJSON(utils.isHTMLForm(t)?new FormData(t):t),axios.getAdapter=adapters.getAdapter,axios.HttpStatusCode=helpers_HttpStatusCode,axios.default=axios;const lib_axios=axios;class LocalStorage{static setItem(t,e){localStorage.setItem(t,e)}static setWithExpiry(t,e,n=33e5){const r={value:e,expiry:Date.now()+n};try{localStorage.setItem(t,JSON.stringify(r))}catch{}}static getWithExpiry(t){const e=localStorage.getItem(t);if(!e)return null;let n=null;try{n=JSON.parse(e)}catch(e){throw localStorage.removeItem(t),e}const r="number"==typeof n.expiry?n.expiry:0;return Date.now()>r?(localStorage.removeItem(t),null):n.value}static removeItem(t){localStorage.removeItem(t)}}const core_localStorage=LocalStorage,REQUEST_ID_HEADER="x-request-id";class Api{_authToken;_requestId=null;apiUrl;_apiKey;_federatedId=null;apiClient;constructor({apiUrl:t,apiKey:e,federatedId:n}){this.apiUrl=t,this._apiKey=e,this._federatedId=n,this._authToken=core_localStorage.getWithExpiry("authToken")||void 0,this.apiClient=lib_axios.create({baseURL:this.apiUrl,headers:{Authorization:`Bearer ${this.authToken}`}}),this.setInterceptors()}async login(){if(!Boolean(core_localStorage.getWithExpiry("authToken")||this.authToken))try{await this.loginWithApiKey()}catch(t){throw console.error("Failed to authenticate with client credentials.",t),core_localStorage.removeItem("authToken"),this._authToken=void 0,t}}setInterceptors(){this.apiClient.interceptors.request.use(async t=>(t.url?.includes("/auth/login")||this.login(),t),t=>Promise.reject(t)),this.apiClient.interceptors.response.use(t=>(this.updatedRequestFromResponse(t),t),t=>(t.response&&this.updatedRequestFromResponse(t.response),Promise.reject(t))),this.apiClient.interceptors.response.use(t=>t,this.handleError.bind(this)),this.apiClient.interceptors.response.use(t=>t,t=>{if(t.response)try{return JSON.stringify(t.response.data),lib_axios.isAxiosError(t)&&401===t.response?.status&&(core_localStorage.removeItem("authToken"),this._authToken=void 0),Promise.reject(t)}catch(t){return console.error(t),Promise.reject(t)}return Promise.reject(t)})}async verifyAuthentication(){await this.getMe()}updatedRequestFromResponse(t){return this.updateRequestId(t.headers[REQUEST_ID_HEADER]??""),t}handleError(t){return Promise.reject(t)}get authToken(){return this._authToken}set authToken(t){t?core_localStorage.setWithExpiry("authToken",t):core_localStorage.removeItem("authToken"),this._authToken=t,this.apiClient&&(this.apiClient.defaults.headers.Authorization=`Bearer ${t}`)}get requestId(){return this._requestId}updateRequestId(t){t&&(this._requestId=t)}async loginWithApiKey(){const t=await this.apiClient.post("/auth/login",{api_key:this._apiKey,federated_id:this._federatedId});return this.authToken=t.data.access_token,this.apiClient.defaults.headers.common.Authorization=`Bearer ${this.authToken}`,this.authToken||""}async getMe(){return(await this.apiClient.get("/users/me")).data}async generateImage(t){const{provider:e,...n}=t,r=`/images/generate/${e}`;return(await this.apiClient.post(r,n)).data}async generateImageWithBria(t){return(await this.apiClient.post("/images/generate/bria",t)).data}async generateImageWithReplicate(t){return(await this.apiClient.post("/images/generate/replicate",t)).data}async getAvailableModels(){return(await this.apiClient.get("/images/models")).data}}const api=Api,audioConfig={sampling_rate:48e3,mime_type:"audio/mpeg"};class ConversationNetwork extends EventEmitter{config;wsConnection=null;_api=null;pendingRequests=new Map;_isInitialized=!1;_sendInitialPing=!0;_didSendInitialPing=!1;_sessionReadyResolve=null;_sessionReadyReject=null;_sessionReadyPromise=null;constructor(t){super(new DefaultLogger({category:"🗣️ConversationNetwork",style:StyleGrey})),this.config=t,this._api=new api({apiUrl:t.apiUrl,apiKey:t.apiKey,federatedId:t.federatedId})}async initialize(){if(!this._isInitialized)try{await this._api.login(),this._isInitialized=!0}catch(t){const e=t.response?.data?.error||t.message||"Authentication failed",n=t.response?.data?.traceback;n?this.logger.error("Authentication failed:",e,"\nTraceback:",n):this.logger.error("Authentication failed:",e);const r=new Error(e);throw n&&(r.traceback=n),r}}async connect(t=!0){if(this.isReady())return;if(this._sessionReadyPromise)return void await this._sessionReadyPromise;this._sendInitialPing=t,this._sessionReadyPromise=new Promise((t,e)=>{this._sessionReadyResolve=t,this._sessionReadyReject=e});let e=this.config.apiUrl.replace(/\/api\/?$/,"")+"/interact";this.wsConnection=new WebSocketConnection(e,this.getWebSocketHandlers()),this.wsConnection.connect(),await this.waitForSessionReady(1e4),this.logger.debug("Network connections established - Session ready")}async waitForSessionReady(t){let e;const n=new Promise((n,r)=>{e=setTimeout(()=>r(new Error(`WebSocket session timeout after ${t}ms`)),t)});try{await Promise.race([this._sessionReadyPromise,n]),clearTimeout(e)}catch(t){throw clearTimeout(e),this._sessionReadyPromise=null,this._sessionReadyResolve=null,this._sessionReadyReject=null,t}}getWebSocketHandlers(){return{onOpen:async()=>{try{await this.authenticate(),await this.setConfiguration(),this.emit(ConversationNetworkEvents.Connected),this._sessionReadyResolve?.()}catch(t){const e=t?.error||String(t);return this._sessionReadyReject?.(new Error(e)),this.emit(ConversationNetworkEvents.Error,new Error(e)),void this.wsConnection?.disconnect()}if(this._sendInitialPing){this._didSendInitialPing=!0;try{await this.interact({text:".",uid:"",kind:"interact",type:"stream"})}catch(t){const e=t?.error||String(t);this.emit(ConversationNetworkEvents.Error,new Error(e))}}},onMessage:async t=>{if(t.uid&&this.pendingRequests.has(t.uid)){const e=this.pendingRequests.get(t.uid);if(e)if(e.isStream)"close"===t.kind?(clearTimeout(e.timeout),e.resolve(t),this.pendingRequests.delete(t.uid)):"error"===t.kind?(this.logger.error(t.error),this.emit(ConversationNetworkEvents.Error,new Error(t.error))):this.emit(ConversationNetworkEvents.Message,t);else{if(clearTimeout(e.timeout),"error"===t.kind){const n=t;this.logger.error(n?.error),e.reject(n.error)}else e.resolve(t);this.pendingRequests.delete(t.uid),this.emit(ConversationNetworkEvents.Message,t)}}else"interact"===t.kind?this.emit(ConversationNetworkEvents.Message,t):"error"===t.kind?(this.logger.error(t.error),await this.emit(ConversationNetworkEvents.Error,t.error)):this.logger.warn("Received message without a matching request UID",t)},onError:async t=>{await this.emit(ConversationNetworkEvents.Error,t)},onClose:async()=>{await this.emit(ConversationNetworkEvents.Disconnected)}}}async makeRequest(t,e=5e4){return this.makeRequestInternal(t,e,!1)}async makeStreamRequest(t,e=5e4){return this.makeRequestInternal(t,e,!0)}async makeRequestInternal(t,e,n){if(!this.wsConnection?.isReady())throw new Error("WebSocket not ready");t.uid=dist_v4(),t.client_start_time=(new Date).toISOString();const r=new Promise((r,i)=>{const o=setTimeout(()=>{this.pendingRequests.delete(t.uid),i(new Error(`Request ${t.uid} timed out after ${e}ms`))},e);this.pendingRequests.set(t.uid,{resolve:r,reject:i,timeout:o,isStream:n})});return this.wsConnection.send(t),r}async authenticate(){const t={type:"request",kind:"authenticate",access_token:this._api.authToken,uid:"",metadata:this.config.metadata};await this.makeRequest(t)}updateConfig(t){this.config=t}async updateRemoteConfiguration(t){this.updateConfig(t),await this.setConfiguration()}async setConfiguration(){const t=void 0!==this.config.configRef&&""!==this.config.configRef,e=t?{reference:this.config.configRef}:{prompt:this.config.prompt,utilities:this.config.utilities,voice_profile:this.config.voiceProfile,safety_policy:this.config.safetyPolicy};this.logger.debug("setConfiguration",t?"using reference":"using inline config",e);const n={type:"request",kind:"set_configuration",config:e,uid:""};await this.makeRequest(n)}async addAudio(t){const e={type:"request",kind:"add_audio",audio:t,uid:"",config:audioConfig};await this.makeRequest(e)}async clearAudio(){await this.makeRequest({type:"request",kind:"clear_audio",uid:""})}async checkTurn(){await this.makeRequest({type:"request",kind:"check_turn",uid:""})}async generateImage(t){return{kind:"generate_image",uid:"",image:(await this._api.generateImage(t)).image}}async generateImageWithBria(t){return{kind:"generate_image",uid:"",image:(await this._api.generateImageWithBria(t)).image}}async generateImageWithReplicate(t){return{kind:"generate_image",uid:"",image:(await this._api.generateImageWithReplicate(t)).image}}async getAvailableModels(){return{kind:"get_available_models",uid:"",models:(await this._api.getAvailableModels()).models}}async interact(t){t.context={...this.config.context,...t.context},t.audio_output=this.config.capabilities?.audio??!0,t.kind=t.kind??"interact",t.type=t.type??"stream",await this.makeStreamRequest(t)}async interactAudio(t){t.context={...this.config.context,...t.context},t.audio_output=this.config.capabilities?.audio??!0,t.kind=t.kind??"interact",t.type=t.type??"stream",await this.makeStreamRequest(t)}async disconnect(){this._sessionReadyPromise=null,this._sessionReadyResolve=null,this._sessionReadyReject=null,this._didSendInitialPing=!1,this.wsConnection?.disconnect()}isReady(){return this.wsConnection?.isReady()||!1}didSendInitialPing(){return this._didSendInitialPing}async send(t){"audio"!==t.type?this.logger.warn("ConversationNetwork.send is deprecated. Use specific methods instead."):await this.addAudio(t.audio)}}class MediaRecorderFallback{mediaRecorder=null;mimeType;onAudioData;timeslice;logger=new DefaultLogger({category:"🎤 MediaRecorderFallback",style:StyleBrown});constructor(t,e,n=100){this.mimeType=t,this.onAudioData=e,this.timeslice=n}initialize(t){this.logger.debug("Initializing MediaRecorder for audio processing"),this.mediaRecorder=new MediaRecorder(t,{mimeType:this.mimeType}),this.logger.debug("MediaRecorder created, state:",this.mediaRecorder.state),this.mediaRecorder.ondataavailable=t=>{this.logger.debug("ondataavailable called, size:",t.data.size),t.data.size>0&&t.data.arrayBuffer().then(t=>{this.onAudioData(t)})}}start(){this.mediaRecorder&&"inactive"===this.mediaRecorder.state&&(this.mediaRecorder.start(this.timeslice),this.logger.debug(`MediaRecorder started with ${this.timeslice}ms timeslice`))}stop(){this.mediaRecorder&&"inactive"!==this.mediaRecorder.state&&(this.mediaRecorder.stop(),this.logger.debug("MediaRecorder stopped"))}}const AudioRecorderEvents={RecordingStarted:"recording-started",RecordingStopped:"recording-stopped",AudioData:"audio-data",Error:"error"};class AudioRecorder extends EventEmitter{mediaStream=null;audioContext=null;workletNode=null;mediaRecorder=null;recording=!1;config;useMediaRecorder=!1;mediaRecorderFallback=null;audioBuffer=[];bufferingMode=!1;MIME_TYPE="audio/webm;codecs=opus";constructor(t={}){super(new DefaultLogger({category:"🎤 AudioRecorder",style:StyleBrown})),this.config={sampleRate:41e3,channels:1,bitDepth:16,echoCancellation:!0,noiseSuppression:!0,autoGainControl:!0,timeslice:100,...t}}async initialize(t){try{this.logger.debug("Initializing..."),await this.setupAudioContext(),await this.loadAudioWorklet(),this.mediaStream=t,this.useMediaRecorder&&(this.mediaRecorderFallback=new MediaRecorderFallback(this.MIME_TYPE,t=>this.handleAudioData(t),this.config.timeslice),this.mediaRecorderFallback.initialize(this.mediaStream)),this.logger.debug("Initialized successfully")}catch(t){throw this.logger.error("Failed to initialize",t),t}}async start(){if(this.recording)this.logger.warn("Recording already in progress");else{if(this.audioContext&&"suspended"===this.audioContext.state)try{await this.audioContext.resume(),this.logger.debug("AudioContext resumed successfully at start of recording.")}catch(t){this.logger.error("Failed to resume AudioContext:",t);const e=new Error("The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page. https://developer.chrome.com/blog/autoplay/#web_audio");throw await this.emit(AudioRecorderEvents.Error,e),e}this.audioContext||(await this.setupAudioContext(),await this.loadAudioWorklet());try{if(this.audioContext&&"suspended"===this.audioContext.state&&(this.logger.debug("Resuming AudioContext before starting recording..."),await this.audioContext.resume()),!this.mediaStream)throw new Error("AudioRecorder: mediaStream must be set via initialize(stream) before starting recording.");this.logger.debug("setupMediaStream complete"),await this.setupAudioProcessing(),this.logger.debug("setupAudioProcessing complete"),this.recording=!0,await this.emit(AudioRecorderEvents.RecordingStarted),this.logger.debug("Audio recording started")}catch(t){throw this.logger.error("Failed to start recording",t),await this.emit(AudioRecorderEvents.Error,t),t}}}async stop(){if(this.logger.debug("Stopping recording..."),this.recording)try{await this.cleanup(),this.recording=!1,await this.emit(AudioRecorderEvents.RecordingStopped),this.logger.debug("Audio recording stopped")}catch(t){throw this.logger.error("Failed to stop recording",t),await this.emit(AudioRecorderEvents.Error,t),t}else this.logger.warn("No recording in progress")}isRecording(){return this.recording}enableBufferingMode(){this.logger.debug("Buffering mode enabled"),this.bufferingMode=!0,this.audioBuffer=[]}disableBufferingMode(){this.logger.debug("Buffering mode disabled"),this.bufferingMode=!1}getBufferedAudio(){return[...this.audioBuffer]}clearBuffer(){const t=this.audioBuffer.length;this.logger.debug(`Audio buffer cleared (Was ${t})`),this.audioBuffer=[]}async resumeAudioContext(){this.audioContext&&"suspended"===this.audioContext.state&&(this.logger.debug("Resuming AudioContext..."),await this.audioContext.resume(),this.logger.debug("AudioContext resumed, state:",this.audioContext.state))}async setupMediaRecorder(){this.logger.debug("Setting up MediaRecorder fallback..."),this.useMediaRecorder=!0}async setupAudioContext(){this.audioContext&&"closed"!==this.audioContext.state||(this.audioContext=new AudioContext({sampleRate:this.config.sampleRate,latencyHint:"interactive"}),this.logger.debug("AudioContext created, state:",this.audioContext.state)),"suspended"===this.audioContext.state&&(this.logger.debug("Resuming suspended AudioContext..."),await this.audioContext.resume(),this.logger.debug("AudioContext resumed, new state:",this.audioContext.state))}async loadAudioWorklet(){if(!this.audioContext)throw new Error("AudioContext not initialized");return this.setupMediaRecorder()}async setupAudioProcessing(){if(this.logger.debug("setupAudioProcessing called, useMediaRecorder:",this.useMediaRecorder),!this.audioContext||!this.mediaStream)throw new Error("AudioContext or MediaStream not initialized");if(this.useMediaRecorder)this.logger.debug("Using MediaRecorder fallback branch"),this.mediaRecorderFallback&&(this.logger.debug("Stopping existing MediaRecorderFallback before creating new one"),this.mediaRecorderFallback.stop()),this.mediaRecorderFallback=new MediaRecorderFallback(this.MIME_TYPE,t=>this.handleAudioData(t),this.config.timeslice),this.mediaRecorderFallback.initialize(this.mediaStream),this.mediaRecorderFallback.start();else{this.logger.debug("Using AudioWorklet for audio processing");const t=this.audioContext.createMediaStreamSource(this.mediaStream);this.workletNode=new AudioWorkletNode(this.audioContext,"audio-recording-processor",{processorOptions:{sampleRate:this.config.sampleRate,channels:this.config.channels,bitDepth:this.config.bitDepth}}),t.connect(this.workletNode),this.workletNode.port.onmessage=t=>{const{type:e,data:n}=t.data;e===AudioRecorderEvents.AudioData&&this.handleAudioData(n)}}}async handleAudioData(t){let e=new Uint8Array(t),n=e.findIndex(t=>0!==t);if(-1===n)return void this.logger.warn("Skipping all-zero audio packet");n>0&&(this.logger.debug(`Trimming ${n} leading zero bytes from audio packet`),e=e.slice(n),t=e.buffer);const r=Array.from(e.slice(0,8));this.logger.debug(`Audio chunk: size=${t.byteLength}, first bytes=${r.join(",")}`),this.bufferingMode?(this.logger.debug(`Buffered audio chunk, buffer size: ${this.audioBuffer.length+1}`),this.audioBuffer.push(t)):await this.emit(AudioRecorderEvents.AudioData,t)}async cleanup(){this.workletNode&&(this.workletNode.disconnect(),this.workletNode=null),this.mediaRecorder&&(this.mediaRecorder.stop(),this.mediaRecorder=null),this.mediaRecorderFallback&&(this.mediaRecorderFallback.stop(),this.mediaRecorderFallback=null),this.audioBuffer=[],this.bufferingMode=!1,this.logger.debug("Audio buffer cleared during cleanup")}async preInitialize(){try{await this.setupAudioContext(),await this.loadAudioWorklet(),this.logger.debug("Pre-initialization complete")}catch(t){this.logger.error("Pre-initialization failed",t)}}isInitialized(){return null!==this.mediaStream}dispose(){try{this.logger.debug("Disposing AudioRecorder..."),this.recording&&this.stop(),this.cleanup(),this.audioContext&&"closed"!==this.audioContext.state&&(this.audioContext.close().catch(t=>{this.logger.debug("Error closing AudioContext:",t)}),this.audioContext=null),this.mediaStream=null,this.removeAllListeners(),this.logger.debug("AudioRecorder disposed")}catch(t){this.logger.warn("Error while disposing AudioRecorder",t)}}}class UserInputManager extends EventEmitter{capabilities;vadManager;network;recordingConfig;audioRecorder=null;isStopped=!0;mediaStream=null;isInputCompleteSent=!1;debugMode=!1;capturedAudioChunks=[];constructor(t,e,n,r){super(new DefaultLogger({category:"🎩 UserInputManager",style:StyleBlue})),this.capabilities=t,this.vadManager=e,this.network=n,this.recordingConfig=r,this.setupListeners(),this.capabilities.audio&&(this.audioRecorder=new AudioRecorder(r),this.setupRecorderListeners())}async initialize(t){await this.reinitializeAudio(t)}async reinitializeAudio(t){if(this.capabilities.audio){if(t&&(this.mediaStream=t),!this.mediaStream){const t={echoCancellation:this.recordingConfig?.echoCancellation,noiseSuppression:this.recordingConfig?.noiseSuppression,autoGainControl:this.recordingConfig?.autoGainControl,sampleRate:this.recordingConfig?.sampleRate,channelCount:this.recordingConfig?.channels};Object.keys(t).forEach(e=>void 0===t[e]&&delete t[e]),this.mediaStream=await navigator.mediaDevices.getUserMedia({audio:t})}await this.vadManager.initialize(this.mediaStream),await(this.audioRecorder?.initialize(this.mediaStream))}}async start(t=!1){if(this.isStopped){this.isStopped=!1;try{await this.network.clearAudio(),this.logger.debug("Server audio buffer cleared for new session")}catch(t){this.logger.error("Failed to clear server audio buffer",t)}this.debugMode&&(this.capturedAudioChunks=[],this.logger.debug("Cleared captured audio chunks for new interaction")),this.capabilities.audio&&(t?this.logger.debug("Skipping VAD startup to reduce CPU contention during playback"):this.vadManager.startAnalysis(),await this.startRecording())}}startVAD(){this.capabilities.audio&&!this.isStopped&&(this.logger.debug("Starting VAD analysis"),this.vadManager.startAnalysis())}async stop(){return!this.isStopped&&(this.logger.debug("Stopping User Input..."),this.isStopped=!0,this.capabilities.audio&&(this.vadManager.stopAnalysis(),await this.stopRecording()),!0)}async updateCapabilities(t){this.capabilities=t,this.logger.debug("Input capabilities updated",this.capabilities),this.capabilities.audio&&!this.audioRecorder?(this.audioRecorder=new AudioRecorder(this.recordingConfig),this.setupRecorderListeners(),await this.reinitializeAudio()):!this.capabilities.audio&&this.audioRecorder&&(this.vadManager.stopAnalysis(),await this.audioRecorder.stop(),this.audioRecorder=null,this.mediaStream&&(this.mediaStream.getTracks().forEach(t=>t.stop()),this.mediaStream=null))}async stopRecording(){this.capabilities.audio&&this.audioRecorder?.isRecording()&&await this.audioRecorder.stop()}async startRecording(){this.capabilities.audio&&this.audioRecorder?(this.audioRecorder.isInitialized()||(this.logger.warn("AudioRecorder not initialized, reinitializing..."),await this.reinitializeAudio()),await this.audioRecorder.start()):this.logger.info("Will not start recording, audio capabilities are not enabled or audio recorder is not initialized")}isRecording(){return this.capabilities.audio&&(this.audioRecorder?.isRecording()||!1)}enableAudioBuffering(){this.capabilities.audio&&this.audioRecorder&&this.audioRecorder.enableBufferingMode()}disableAudioBuffering(){this.capabilities.audio&&this.audioRecorder&&this.audioRecorder.disableBufferingMode()}getBufferedAudio(){return this.audioRecorder?.getBufferedAudio()||[]}clearAudioBuffer(){this.capabilities.audio&&this.audioRecorder&&this.audioRecorder.clearBuffer()}async flushBufferedAudio(){this.disableAudioBuffering();const t=this.getBufferedAudio();if(this.clearAudioBuffer(),t.length>0){this.logger.debug(`Flushing ${t.length} buffered audio chunks`);for(const e of t)await this.sendAudio(e)}}async sendAudio(t){if(this.isStopped)return;this.logger.debug("sendAudio called, size:",t.byteLength),this.debugMode&&(this.capturedAudioChunks.push(t),this.logger.debug(`Captured audio chunk for debug (${this.capturedAudioChunks.length} total chunks)`));const e=this.arrayBufferToBase64(t);await this.network.addAudio(e)}arrayBufferToBase64(t){let e="";const n=new Uint8Array(t),r=n.byteLength;for(let t=0;t<r;t++)e+=String.fromCharCode(n[t]);return btoa(e)}async interact(t){await this.network.interact(t)}async sendInputComplete(){this.isStopped||this.isInputCompleteSent||(this.isInputCompleteSent=!0,await this.emit(UserInputManagerEvents.UserInput,{type:"input_complete"}))}reset(){this.isInputCompleteSent=!1,this.debugMode&&this.capturedAudioChunks.length>0&&(this.logger.debug(`Clearing ${this.capturedAudioChunks.length} captured audio chunks on reset`),this.capturedAudioChunks=[])}dispose(){try{this.logger.debug("Disposing UserInputManager..."),this.isStopped=!0,this.vadManager&&(this.vadManager.stopAnalysis(),"function"==typeof this.vadManager.dispose&&this.vadManager.dispose()),this.capabilities.audio&&this.audioRecorder?.isRecording()&&this.audioRecorder.stop(),this.audioRecorder&&"function"==typeof this.audioRecorder.dispose&&this.audioRecorder.dispose(),this.mediaStream&&(this.mediaStream.getTracks().forEach(t=>{t.stop(),this.logger.debug(`Stopped media track: ${t.kind}`)}),this.mediaStream=null),this.removeAllListeners(),this.logger.debug("UserInputManager disposed")}catch(t){this.logger.warn("Error while disposing UserInputManager",t)}}setupListeners(){this.capabilities.audio&&(this.vadManager.on(types_VadManagerEvents.VoiceActivity,async t=>{t&&!this.isStopped&&(t.isSpeaking?await this.emit(UserInputManagerEvents.UserSpeaking):await this.emit(UserInputManagerEvents.UserSilence))}),this.vadManager.on(types_VadManagerEvents.Silence,async()=>{await this.sendInputComplete(),await this.network.checkTurn()}))}setupRecorderListeners(){this.audioRecorder&&this.audioRecorder.on(AudioRecorderEvents.AudioData,t=>{this.sendAudio(t).catch(t=>{this.logger.error("Failed to send audio chunk",t)})})}async resumeAudioContext(){this.capabilities.audio&&this.audioRecorder&&await this.audioRecorder.resumeAudioContext()}enableDebugMode(){this.debugMode=!0,this.capturedAudioChunks=[],this.logger.info("Debug mode enabled - audio chunks will be captured")}disableDebugMode(){this.debugMode=!1,this.logger.info("Debug mode disabled")}clearCapturedAudio(){this.capturedAudioChunks=[],this.logger.info("Captured audio chunks cleared")}downloadCapturedAudio(t="captured-audio.webm"){if(0===this.capturedAudioChunks.length)return void this.logger.warn("No audio chunks captured to download");this.logger.info(`Downloading ${this.capturedAudioChunks.length} captured audio chunks as ${t}`);const e=new Blob(this.capturedAudioChunks,{type:"audio/webm;codecs=opus"}),n=URL.createObjectURL(e),r=document.createElement("a");r.href=n,r.download=t,document.body.appendChild(r),r.click(),document.body.removeChild(r),URL.revokeObjectURL(n),this.logger.info("Download completed")}getCapturedAudioInfo(){const t=this.capturedAudioChunks.reduce((t,e)=>t+e.byteLength,0);return{chunkCount:this.capturedAudioChunks.length,totalSize:t}}}class ConversationCommands{static DEFAULT_START_COMMAND="[start conversation]";static DEFAULT_PAUSE_RESUME_COMMAND="[resume after short pause]";static DEFAULT_LONG_PAUSE_RESUME_COMMAND="[resume after long break]";static DEFAULT_ERROR_RESUME_COMMAND="[resume after error]";startCommand=ConversationCommands.DEFAULT_START_COMMAND;pauseResumeCommand=ConversationCommands.DEFAULT_PAUSE_RESUME_COMMAND;longPauseResumeCommand=ConversationCommands.DEFAULT_LONG_PAUSE_RESUME_COMMAND;errorResumeCommand=ConversationCommands.DEFAULT_ERROR_RESUME_COMMAND;set(t,e,n,r){this.startCommand=t??ConversationCommands.DEFAULT_START_COMMAND,this.pauseResumeCommand=e??ConversationCommands.DEFAULT_PAUSE_RESUME_COMMAND,this.longPauseResumeCommand=n??ConversationCommands.DEFAULT_LONG_PAUSE_RESUME_COMMAND,this.errorResumeCommand=r??ConversationCommands.DEFAULT_ERROR_RESUME_COMMAND}setDefault(){this.startCommand=ConversationCommands.DEFAULT_START_COMMAND,this.pauseResumeCommand=ConversationCommands.DEFAULT_PAUSE_RESUME_COMMAND,this.longPauseResumeCommand=ConversationCommands.DEFAULT_LONG_PAUSE_RESUME_COMMAND,this.errorResumeCommand=ConversationCommands.DEFAULT_ERROR_RESUME_COMMAND}}class ErrorRetryHandler{deps;static DEFAULT_MAX_RETRY_COUNT=3;currentRetryCount=0;isRetrying=!1;maxRetryCount;constructor(t){this.deps=t,this.maxRetryCount=t.maxRetryCount??ErrorRetryHandler.DEFAULT_MAX_RETRY_COUNT}async attemptRecovery(t){if(this.deps.logger.info("Attempting silent recovery for error type: "+t),!["network_timeout","network_error","server_error"].includes(t))return!1;if(!this.deps.isConnected())return this.deps.logger.info("Network is not connected, skipping retry"),!1;if(this.currentRetryCount>=this.maxRetryCount)return!1;if(this.isRetrying)return!1;this.currentRetryCount++,this.isRetrying=!0,this.deps.logger.info(`Attempting silent retry ${this.currentRetryCount}/${this.maxRetryCount} with error resume command`);try{return await this.deps.clearAudio(),await this.deps.sendText(this.deps.conversationCommands.errorResumeCommand),this.isRetrying=!1,!0}catch(e){return this.deps.logger.error(`Retry attempt ${this.currentRetryCount} failed`,e),this.isRetrying=!1,this.currentRetryCount<this.maxRetryCount&&await this.attemptRecovery(t)}}resetRetryCount(){this.currentRetryCount=0,this.isRetrying=!1}getCurrentRetryCount(){return this.currentRetryCount}}const ConversationManagerEvents={StateChange:"stateChange"};class ConversationManager extends EventEmitter{state="idle";network;vadManager;userInputManager;playbackManager;config;interactionCompletePending=!1;mediaStream=null;isTextOnly=!1;isDisposed=!1;isTransitioning=!1;pausedFromState=null;errorRetryHandler;isSimulateErrorsDebugMode=!1;conversationCommands=new ConversationCommands;constructor(t){const e=new DefaultLogger({category:"🧞 ConversationManager",style:StylePurple});super(e),this.setConfiguration(t);const n={...t,...t.apiKey&&{apiKey:t.apiKey.slice(0,4)+"..."},...t.federatedId&&{federatedId:t.federatedId.slice(0,4)+"..."}};e.info("Initializing with config",n),this.network=new ConversationNetwork(this.config),this.vadManager=new VADManager,this.userInputManager=new UserInputManager(this.config.inputCapabilities,this.vadManager,this.network,t.recordingConfig),this.playbackManager=new PlaybackManager(this.config.capabilities),this.playbackManager.wireVADToAvatar(this),this.playbackManager.wireConversationStateToAvatar(this),this.setupEventListeners(),this.errorRetryHandler=new ErrorRetryHandler({logger:this.logger,conversationCommands:this.conversationCommands,sendText:t=>this.sendText(t),clearAudio:async()=>{this.userInputManager.clearAudioBuffer(),await this.network.clearAudio()},isConnected:()=>this.isConnected(),maxRetryCount:3}),this.setState("uninitialized")}async setConfiguration(t){const e=t.languageCode||t.voiceProfile?.deepdub_locale?.split("-")[0];if(this.config={...t,languageCode:e,capabilities:t.capabilities||{audio:!0,viseme:!0,subtitles:!0,avatar:!0},inputCapabilities:t.inputCapabilities||{audio:!0,text:!0},hooks:{...this.config?.hooks||{},...t.hooks},onInputUtilities:t.onInputUtilities??this.config?.onInputUtilities,onInputNonBlockingUtilities:t.onInputNonBlockingUtilities??this.config?.onInputNonBlockingUtilities,onOutputUtilities:t.onOutputUtilities??this.config?.onOutputUtilities},this.network&&(this.network.updateConfig(this.config),this.network.isReady())){this.logger.debug("Updating remote configuration");try{await this.network.updateRemoteConfiguration(this.config),this.logger.debug("Remote configuration updated successfully")}catch(t){this.logger.error("Failed to update remote configuration",t)}}}async initialize(){try{if(this.logger.debug("Initializing..."),this.setState("initializing"),await this.network.initialize(),this.isDisposed)return;if(await this.playbackManager.initialize(),this.isDisposed)return;if(this.config.inputCapabilities?.audio&&!this.mediaStream){const t=this.config.recordingConfig,e={echoCancellation:t?.echoCancellation,noiseSuppression:t?.noiseSuppression,autoGainControl:t?.autoGainControl,sampleRate:t?.sampleRate,channelCount:t?.channels};Object.keys(e).forEach(t=>void 0===e[t]&&delete e[t]),this.mediaStream=await navigator.mediaDevices.getUserMedia({audio:e})}if(this.isDisposed)return;if(await this.userInputManager.initialize(this.mediaStream??void 0),this.isDisposed)return;await this.network.connect(),this.logger.debug("Initialized successfully"),this.setState(this.network.didSendInitialPing()?"waiting":"idle")}catch(t){if(this.isDisposed)return;this.handleError("server_error",t)}}async prepareConversation(){if(this.isDisposed)return!1;try{return await this.network.initialize(),!this.isDisposed&&(await this.network.connect(!1),!0)}catch(t){return this.logger.warn("prepareConversation failed:",t),!1}}async startListening(){if("idle"!==this.state)throw new Error(`Cannot start listening from state: ${this.state}`);try{await this.userInputManager.start(),this.setState("listening")}catch(t){this.handleError("mic_denied",t)}}async stopListening(){this.logger.debug("stopListening called from state:",this.state),await this.userInputManager.stop(),await this.setState("waiting")}async sendText(t){return this.interact({text:t,kind:"interact",type:"stream",uid:"",language_code:this.config.languageCode})}async generateImage(t){return this.network.generateImage(t)}async generateImageWithBria(t){return this.network.generateImageWithBria(t)}async generateImageWithReplicate(t){return this.network.generateImageWithReplicate(t)}async getAvailableModels(){return this.network.getAvailableModels()}async interact(t){if(!this.network.isReady()){const t="Network is not ready, cannot send text. probably disconnected";return this.logger.error(t),void await this.handleError("network_timeout",new Error(t))}if("idle"===this.state||"listening"===this.state||"processing_complete"===this.state||"error"===this.state)try{if(this.playbackManager.reset(),this.isSimulateErrorsDebugMode&&Math.random()<.5)throw this.logger.error("DebugMode:Simulating server error"),new Error("server_error");await this.setState("waiting"),await this.userInputManager.interact(t)}catch(t){this.logger.error("Error sending text message",t),await this.handleError("network_timeout",t)}}async interrupt(){this.playbackManager.pause(),await this.setState("interrupted")}async pause(){if(this.isTransitioning)this.logger.debug("Pause ignored: transition already in progress");else if("playing"===this.state||"processing_complete"===this.state||"listening"===this.state||"userSpeaking"===this.state){this.isTransitioning=!0;try{if(this.pausedFromState=this.state,"listening"===this.state||"userSpeaking"===this.state){this.logger.debug("Pausing from listening/speaking state, stopping input and clearing audio"),await this.userInputManager.stop(),this.userInputManager.clearAudioBuffer();try{await this.network.clearAudio()}catch(t){this.logger.warn("Failed to clear server audio buffer on pause",t)}}else this.playbackManager.pause();await this.setState("paused")}finally{this.isTransitioning=!1}}else this.logger.warn(`Cannot pause from state: ${this.state}`)}async resume(){if(this.isTransitioning)this.logger.debug("Resume ignored: transition already in progress");else if("paused"===this.state||"processing_complete"===this.state){this.isTransitioning=!0;try{const t=this.playbackManager.getRemainingAudioSeconds()>0,e="listening"===this.pausedFromState||"userSpeaking"===this.pausedFromState;if(t){this.logger.debug("Resuming playback with buffered audio");try{await this.playbackManager.resume(),await this.setState("playing")}catch(t){this.logger.error("Failed to resume playback, falling back to listening",t),await this.userInputManager.start(),await this.setState("listening")}}else if(e){this.logger.debug("Resuming from listening pause, restarting recording");try{await this.userInputManager.start(),await this.setState("listening")}catch(t){this.logger.error("Failed to restart recording after pause",t),await this.handleError("mic_denied",t)}}else this.logger.debug("No buffered audio to resume, going to listening state"),this.interactionCompletePending?(this.interactionCompletePending=!1,await this.handleInteractionComplete()):await this.setState("listening")}finally{this.pausedFromState=null,this.isTransitioning=!1}}else this.logger.warn(`Cannot resume from state: ${this.state}`)}async forceInputComplete(){"userSpeaking"===this.state||"listening"===this.state?await this.userInputManager.sendInputComplete():this.logger.warn(`Cannot force input complete from state: ${this.state}`)}async stop(){await this.userInputManager.stop(),this.playbackManager.pause(),await this.network.disconnect(),await this.setState("idle")}async dispose(){if(this.isDisposed)this.logger.warn("ConversationManager already disposed");else{this.logger.debug("Disposing ConversationManager..."),this.isDisposed=!0,this.userInputManager?.removeAllListeners(),this.playbackManager?.removeAllListeners(),this.network?.removeAllListeners(),this.state="idle";try{this.userInputManager&&"function"==typeof this.userInputManager.dispose?this.userInputManager.dispose():(await this.userInputManager.stop(),this.mediaStream&&(this.mediaStream.getTracks().forEach(t=>{t.stop(),this.logger.debug(`Stopped media track: ${t.kind}`)}),this.mediaStream=null)),this.playbackManager&&"function"==typeof this.playbackManager.dispose&&this.playbackManager.dispose(),await this.network.disconnect(),this.removeAllListeners(),this.logger.debug("ConversationManager disposed successfully")}catch(t){this.logger.warn("Error while disposing ConversationManager",t)}}}async setState(t){if(this.isDisposed)return;if(this.state===t)return;const e=this.state;this.state=t,this.config.hooks.onStateChange?.(t),await this.emit(ConversationManagerEvents.StateChange,{oldState:e,newState:t}),this.logger.debug(`State transition: ${e} -> ${t}`)}setupEventListeners(){this.userInputManager.on(UserInputManagerEvents.UserInput,async t=>{await this.network.send(t),"input_complete"===t.type&&await this.setState("waiting")}),this.playbackManager.on(PlaybackManagerEvents.SubtitleWordChange,t=>{this.config.hooks.onSubtitleHighlight?.(t)}),this.playbackManager.on(PlaybackManagerEvents.SubtitleChange,t=>{this.config.hooks.onSubtitleChange?.(t)}),this.playbackManager.on(PlaybackManagerEvents.ImageChange,t=>{this.config.hooks.onImageChange?.(t)}),this.playbackManager.on(PlaybackManagerEvents.AvatarAnimationChanged,t=>{this.config.hooks.onAvatarAnimationChanged?.(t)}),this.userInputManager.on(UserInputManagerEvents.UserSpeaking,async()=>{"listening"===this.state?await this.setState("userSpeaking"):this.logger.debug(`VAD UserSpeaking ignored in state: ${this.state}`)}),this.userInputManager.on(UserInputManagerEvents.UserSilence,async()=>{"userSpeaking"===this.state?await this.setState("listening"):this.logger.debug(`VAD UserSilence ignored in state: ${this.state}`)}),this.network.on(ConversationNetworkEvents.Connected,async()=>{this.config.hooks.onNetworkStatusChange?.(!0)}),this.network.on(ConversationNetworkEvents.Disconnected,async()=>{this.config.hooks.onNetworkStatusChange?.(!1)}),this.network.on(ConversationNetworkEvents.Message,async t=>{if("interact"===t.kind&&"text"===t.event&&this.config.hooks.onTextMessage?.(t),"data"===t.event&&this.config.hooks.onDataMessage?.(t),"interact"===t.kind&&"safety_policy"===t.event&&this.config.hooks.onSafetyEvent?.(t),"check_turn"===t.kind&&!1===t.is_user_still_speaking){if(this.logger.debug(`check_turn handler: state is ${this.state}`),"playing"===this.state||"paused"===this.state)return void this.logger.debug("check_turn received while playing / paused, ignoring.");await this.stopListening();try{if(this.isSimulateErrorsDebugMode&&Math.random()<.5)throw this.logger.error("DebugMode:Simulating server error"),new Error("server_error");await this.network.interactAudio({uid:"",kind:"interact",type:"stream",on_input_non_blocking:this.config.onInputNonBlockingUtilities,on_input:this.config.onInputUtilities,on_output:this.config.onOutputUtilities,language_code:this.config.languageCode})}catch(t){this.userInputManager.clearAudioBuffer();try{await this.network.clearAudio(),this.logger.debug("Server audio buffer cleared for new session")}catch(t){this.logger.error("Failed to clear server audio buffer",t)}this.logger.error("Error in interactAudio",t),await this.handleError("network_timeout",t)}}"interact"===t.kind&&"interaction_complete"===t.event&&(this.errorRetryHandler.resetRetryCount(),"playing"===this.state||"paused"===this.state?(this.logger.debug("Interaction complete received while playing / paused, deferring."),this.interactionCompletePending=!0,await this.setState("processing_complete")):await this.handleInteractionComplete()),"interaction_error"===t.type&&this.handleError("server_error",new Error(t.error||"Unknown server interaction error")),"string"===t.type&&this.config.hooks.onStringMessage?.(t),this.playbackManager.handleMessage(t)}),this.network.on(ConversationNetworkEvents.Error,async t=>{this.handleError("network_error",t)}),this.playbackManager.on(PlaybackManagerEvents.PlaybackError,async t=>{this.handleError("decode_error",t)}),this.playbackManager.on(PlaybackManagerEvents.Playing,async()=>{this.userInputManager.isRecording()&&(this.logger.debug("Stopping recording as playback starts (no-overlap)"),await this.userInputManager.stop()),await this.setState("playing")}),this.playbackManager.on(PlaybackManagerEvents.Finished,async()=>{if(this.interactionCompletePending)this.logger.debug("Playback finished, processing deferred interaction complete."),this.interactionCompletePending=!1,await this.handleInteractionComplete();else if("playing"===this.state){this.logger.debug("Playback finished, starting recording now (no overlap)"),this.userInputManager.reset();try{await this.userInputManager.start(),await this.setState("listening"),this.logger.debug("Recording started successfully after playback")}catch(t){this.logger.error("Failed to start recording after playback",t),await this.setState("idle")}}})}async handleError(t,e){await this.setState("error"),this.logger.error("handleError called with type: "+t+" and error: "+e.message);let n=!1;try{n=await this.errorRetryHandler.attemptRecovery(t)}catch(t){this.logger.error("Error during recovery attempt",t),n=!1}if(n)return void this.logger.debug("Error recovery succeeded, not raising error");const r={type:t,message:e.message,originalError:e};this.config.hooks.onError?.(r),this.logger.error(`Conversation error: ${t}`,e)}async handleInteractionComplete(){this.logger.debug("Interaction complete received"),this.playbackManager.resetAboutToComplete();try{if(this.userInputManager.reset(),this.userInputManager.isRecording())await this.setState("listening");else{this.logger.debug("Starting recording after interaction complete");try{await this.userInputManager.start(),await this.setState("listening")}catch(t){this.logger.error("Failed to start recording",t),await this.setState("idle")}}}catch(t){this.logger.error("Failed to handle interaction complete",t),await this.handleError("network_timeout",t)}}async toggleTextOnlyInput(t){this.isTextOnly=t,await this.updateInputCapabilities()}isConnected(){return this.network.isReady()}enableAudioDebugMode(){this.userInputManager.enableDebugMode(),this.logger.info("Audio debug mode enabled")}disableAudioDebugMode(){this.userInputManager.disableDebugMode(),this.logger.info("Audio debug mode disabled")}downloadCapturedAudio(t){this.userInputManager.downloadCapturedAudio(t)}clearCapturedAudio(){this.userInputManager.clearCapturedAudio()}getCapturedAudioInfo(){return this.userInputManager.getCapturedAudioInfo()}async updateInputCapabilities(){this.logger.debug(`Updating input capabilities, textOnly: ${this.isTextOnly}`),this.config.inputCapabilities={...this.config.inputCapabilities,audio:!this.isTextOnly,text:this.isTextOnly},await this.userInputManager.updateCapabilities(this.config.inputCapabilities),this.isTextOnly?await this.userInputManager.stopRecording():"idle"===this.state&&await this.startListening()}}export{AudioPlayer,ConversationCommands,ConversationManager};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ug-js-sdk",
3
- "version": "3.0.65",
3
+ "version": "3.0.66",
4
4
  "description": "JavaScript SDK for building conversational AI experiences with voice, text, avatars, and real-time interactions",
5
5
  "main": "dist/ug-js-sdk.mjs",
6
6
  "module": "dist/ug-js-sdk.mjs",