@unith-ai/core-client 1.10.4 → 1.10.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -26
- package/dist/index.d.ts +12 -15
- package/dist/index.d.ts.map +1 -1
- package/dist/lib.js +1 -1
- package/dist/lib.js.map +1 -1
- package/dist/lib.module.js +1 -1
- package/dist/lib.module.js.map +1 -1
- package/dist/lib.umd.js +1 -1
- package/dist/lib.umd.js.map +1 -1
- package/dist/modules/connection.d.ts.map +1 -1
- package/dist/modules/microphone.d.ts +1 -0
- package/dist/modules/microphone.d.ts.map +1 -1
- package/dist/modules/monitor.d.ts.map +1 -1
- package/dist/modules/video.d.ts.map +1 -1
- package/dist/utils/microphone.d.ts +6 -0
- package/dist/utils/microphone.d.ts.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -59,9 +59,9 @@ const conversation = await Conversation.startDigitalHuman({
|
|
|
59
59
|
- **mode** - Conversation mode (default: "default")
|
|
60
60
|
- **language** - Language code for the conversation (default: browser language)
|
|
61
61
|
- **allowWakeLock** - Prevent screen from sleeping during conversation (default: true)
|
|
62
|
-
- **microphoneProvider** - Provider for the microphone - `"azure" | "
|
|
62
|
+
- **microphoneProvider** - Provider for the microphone - `"azure" | "eleven_labs"`
|
|
63
63
|
- **microphoneOptions** - Callbacks for microphone events
|
|
64
|
-
- **onMicrophoneSpeechRecognitionResult ({ transcript: string })** - Called when microphone
|
|
64
|
+
- **onMicrophoneSpeechRecognitionResult ({ transcript: string })** - Called when microphone recognizes your user's speech. This returns a transcript. Ideal behavior is to call the `.sendMessage` method with your transcript as microphone doesn't automatically commit / send users text to our AI.
|
|
65
65
|
- **onMicrophoneStatusChange ({status})** Called when microphone status changes
|
|
66
66
|
- **status** `"ON" | "OFF" | "PROCESSING"` Shows current status of microphone.
|
|
67
67
|
- **onMicrophoneError ({ message: string })** - Called when microphone has an error with the error message.
|
|
@@ -95,7 +95,6 @@ Register callbacks to handle various events:
|
|
|
95
95
|
- **onMuteStatusChange** - Called when mute status changes
|
|
96
96
|
- **onSpeakingStart** - Called when the digital human starts speaking
|
|
97
97
|
- **onSpeakingEnd** - Called when the digital human finishes speaking
|
|
98
|
-
- **onStoppingEnd** - Called when a response is manually stopped
|
|
99
98
|
- **onTimeout** - Called when the session times out due to inactivity
|
|
100
99
|
- **onTimeoutWarning** - Called before the session times out. This event warns you that the customers session is going to end in a bit. You can call the `keepSession` method to extend the customers session.
|
|
101
100
|
- **onKeepSession** - Called when a keep-alive request is processed
|
|
@@ -132,39 +131,38 @@ Send a text message to the digital human:
|
|
|
132
131
|
conversation.sendMessage("Hello, how are you?");
|
|
133
132
|
```
|
|
134
133
|
|
|
135
|
-
|
|
136
134
|
#### toggleMicrophone()
|
|
137
135
|
|
|
138
|
-
Toogles microphone status.
|
|
136
|
+
Toogles microphone status between ON/OFF.
|
|
139
137
|
|
|
140
138
|
```js
|
|
141
139
|
conversation.toggleMicrophone();
|
|
142
140
|
```
|
|
143
141
|
|
|
144
|
-
####
|
|
142
|
+
#### getMicrophoneStatus()
|
|
145
143
|
|
|
146
|
-
|
|
144
|
+
Get status of microphone. Can be ON/OFF/PROCESSING
|
|
147
145
|
|
|
148
146
|
```js
|
|
149
|
-
conversation.
|
|
147
|
+
conversation.getMicrophoneStatus();
|
|
150
148
|
```
|
|
151
149
|
|
|
152
|
-
####
|
|
150
|
+
#### keepSession()
|
|
153
151
|
|
|
154
|
-
|
|
152
|
+
Sends keep-alive event to prevent session timeout. Trigger this when you receive the **onTimeoutWarning** event to prevent session from timing out.
|
|
155
153
|
|
|
156
154
|
```js
|
|
157
|
-
conversation.
|
|
155
|
+
conversation.keepSession();
|
|
158
156
|
```
|
|
159
157
|
|
|
160
158
|
This clears both audio and video queues and returns the digital human to idle state.
|
|
161
159
|
|
|
162
|
-
####
|
|
160
|
+
#### toggleMute()
|
|
163
161
|
|
|
164
162
|
Toggle the mute status of the audio output:
|
|
165
163
|
|
|
166
164
|
```js
|
|
167
|
-
const volume = await conversation.
|
|
165
|
+
const volume = await conversation.toggleMute();
|
|
168
166
|
console.log("New volume:", volume); // 0 for muted, 1 for unmuted
|
|
169
167
|
```
|
|
170
168
|
|
|
@@ -186,19 +184,6 @@ await conversation.endSession();
|
|
|
186
184
|
|
|
187
185
|
This closes the WebSocket connection, releases the wake lock, and destroys audio/video outputs.
|
|
188
186
|
|
|
189
|
-
### Message Structure
|
|
190
|
-
|
|
191
|
-
Messages sent to and from the digital human follow this structure:
|
|
192
|
-
|
|
193
|
-
```typescript
|
|
194
|
-
interface Message {
|
|
195
|
-
timestamp: Date;
|
|
196
|
-
sender: SpeakerType;
|
|
197
|
-
text: string;
|
|
198
|
-
visible: boolean;
|
|
199
|
-
}
|
|
200
|
-
```
|
|
201
|
-
|
|
202
187
|
### Error Handling
|
|
203
188
|
|
|
204
189
|
Always handle errors appropriately:
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import { AudioOutput } from "./modules/audio";
|
|
2
|
-
import { AVController } from "./modules/av";
|
|
3
2
|
import { Connection } from "./modules/connection";
|
|
4
3
|
import { IdleVideo } from "./modules/idle-video";
|
|
5
|
-
import { SyncController } from "./modules/sync";
|
|
6
4
|
import { User } from "./modules/user";
|
|
7
5
|
import { VideoOutput } from "./modules/video";
|
|
8
6
|
import type { ConversationOptions, ConversationEvents, DigitalHumanOptions, HeadOptions, VideoHtmlElement, MicrophoneType } from "./types/Conversation";
|
|
@@ -19,21 +17,21 @@ export type Options = ConversationOptions & ConversationEvents & MicrophoneType;
|
|
|
19
17
|
export type HeadConfigOptions = HeadOptions & Partial<DigitalHumanOptions>;
|
|
20
18
|
export type PartialOptions = HeadConfigOptions & Partial<MicrophoneType> & VideoHtmlElement & Partial<ConversationEvents>;
|
|
21
19
|
export declare class Conversation {
|
|
22
|
-
options
|
|
23
|
-
microphoneAccess
|
|
24
|
-
connection
|
|
25
|
-
idleVideo
|
|
26
|
-
wakeLock
|
|
27
|
-
user
|
|
28
|
-
audioOutput
|
|
29
|
-
videoOutput
|
|
30
|
-
headInfo
|
|
20
|
+
private options;
|
|
21
|
+
private microphoneAccess;
|
|
22
|
+
private connection;
|
|
23
|
+
private idleVideo;
|
|
24
|
+
private wakeLock;
|
|
25
|
+
private user;
|
|
26
|
+
private audioOutput;
|
|
27
|
+
private videoOutput;
|
|
28
|
+
private headInfo;
|
|
31
29
|
private status;
|
|
32
30
|
protected volume: number;
|
|
33
31
|
private sessionStarted;
|
|
34
32
|
private messageCounter;
|
|
35
|
-
syncController
|
|
36
|
-
avController
|
|
33
|
+
private syncController;
|
|
34
|
+
private avController;
|
|
37
35
|
private monitor;
|
|
38
36
|
private microphone;
|
|
39
37
|
private videoFrameQueue;
|
|
@@ -77,8 +75,7 @@ export declare class Conversation {
|
|
|
77
75
|
* 3. clear audio queue
|
|
78
76
|
* 4. send an event if all expected response from BE has been received. If not, FE will keep status in 'stopping' mode
|
|
79
77
|
*/
|
|
80
|
-
|
|
81
|
-
toggleMuteStatus(): Promise<number>;
|
|
78
|
+
toggleMute(): Promise<number>;
|
|
82
79
|
startSession(): Promise<Connection>;
|
|
83
80
|
toggleMicrophone(): Promise<void>;
|
|
84
81
|
getMicrophoneStatus(): import("./types/microphone").MicrophoneStatus;
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAE9C,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAElD,OAAO,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AAIjD,OAAO,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AACtC,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAE9C,OAAO,KAAK,EACV,mBAAmB,EACnB,kBAAkB,EAElB,mBAAmB,EACnB,WAAW,EACX,gBAAgB,EAEhB,cAAc,EACf,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAiBlD,OAAO,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAKxC,cAAc,eAAe,CAAC;AAC9B,cAAc,sBAAsB,CAAC;AACrC,cAAc,oBAAoB,CAAC;AACnC,cAAc,cAAc,CAAC;AAC7B,cAAc,qBAAqB,CAAC;AACpC,cAAc,oBAAoB,CAAC;AACnC,OAAO,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAClD,MAAM,MAAM,OAAO,GAAG,mBAAmB,GAAG,kBAAkB,GAAG,cAAc,CAAC;AAChF,MAAM,MAAM,iBAAiB,GAAG,WAAW,GAAG,OAAO,CAAC,mBAAmB,CAAC,CAAC;AAC3E,MAAM,MAAM,cAAc,GAAG,iBAAiB,GAC5C,OAAO,CAAC,cAAc,CAAC,GACvB,gBAAgB,GAChB,OAAO,CAAC,kBAAkB,CAAC,CAAC;AAC9B,qBAAa,YAAY;IA8LrB,OAAO,CAAC,OAAO;IACf,OAAO,CAAC,gBAAgB;IACxB,OAAO,CAAC,UAAU;IAClB,OAAO,CAAC,SAAS;IACjB,OAAO,CAAC,QAAQ;IAChB,OAAO,CAAC,IAAI;IACZ,OAAO,CAAC,WAAW;IACnB,OAAO,CAAC,WAAW;IACnB,OAAO,CAAC,QAAQ;IArMlB,OAAO,CAAC,MAAM,CAAwB;IACtC,SAAS,CAAC,MAAM,EAAE,MAAM,CAAK;IAC7B,OAAO,CAAC,cAAc,CAAkB;IACxC,OAAO,CAAC,cAAc,CAAK;IAE3B,OAAO,CAAC,cAAc,CAAiB;IACvC,OAAO,CAAC,YAAY,CAAe;IACnC,OAAO,CAAC,OAAO,CAA+B;IAC9C,OAAO,CAAC,UAAU,CAA2B;IAE7C,OAAO,CAAC,eAAe,CAAwB;IAE/C,OAAO,CAAC,mBAAmB,CAGlB;IAET,OAAO,CAAC,MAAM,CAAC,cAAc;IA6B7B;;;;OAIG;WACiB,iBAAiB,CACnC,OAAO,EAAE,cAAc,GACtB,OAAO,CAAC,YAAY,CAAC;IAkHxB;;;;OAIG;WACiB,kBAAkB,CAAC,OAAO,EAAE;QAC9C,KAAK,EAAE,MAAM,CAAC;QACd,MAAM,EAAE,MAAM,CAAC;QACf,WAAW,CAAC,EAAE,WAAW,CAAC;KAC3B;gBAaS,OAAO,EAAE,OAAO,EAChB,gBAAgB,EAAE,OAAO,EACzB,UAAU,EAAE,UAAU,EACtB,SAAS,EAAE,SAAS,EACpB,QAAQ,EAAE,gBAAgB,GAAG,IAAI,EACjC,IAAI,EAAE,IAAI,EACV,WAAW,EAAE,WAAW,EACxB,WAAW,EAAE,WAAW,EACxB,QAAQ,EAAE,QAAQ;IA4B5B,OAAO,CAAC,sBAAsB;IAQ9B,OAAO,CAAC,mBAAmB;IAc3B,OAAO,CAAC,sBAAsB,CAE5B;IAEF,OAAO,CAAC,aAAa,CASnB;IAEF,OAAO,CAAC,iBAAiB,CA2DvB;IAEF,OAAO,CAAC,oBAAoB,CA6D1B;IAEF,OAAO,CAAC,gBAAgB,CAYtB;IAEF,OAAO,CAAC,gBAAgB,CAStB;IAEF,OAAO,CAAC,gBAAgB,CA0BtB;IAEF,OAAO,CAAC,SAAS,CAuEf;IAEF,OAAO,CAAC,qBAAqB,CAQ3B;IAEK,SAAS;YAIF,gBAAgB;IAG9B,OAAO,CAAC,YAAY;IAQpB;;;;;;OAMG;IAaU,UAAU;IASV,YAAY;IA8BZ,gBAAgB;IAqBtB,mBAAmB;IAInB,UAAU;IAIV,WAAW,CAAC,IAAI,EAAE,MAAM;IAuBxB,WAAW;CAsBnB"}
|
package/dist/lib.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var e=require("microsoft-cognitiveservices-speech-sdk"),t=require("@elevenlabs/client");function n(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,i=Array(t);n<t;n++)i[n]=e[n];return i}function i(){return i=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var i in n)({}).hasOwnProperty.call(n,i)&&(e[i]=n[i])}return e},i.apply(null,arguments)}function r(e,t){try{var n=e()}catch(e){return t(e)}return n&&n.then?n.then(void 0,t):n}var o,s,a=new Map,c=(o="audio-concat-processor",s='\nconst decodeTable = [0,132,396,924,1980,4092,8316,16764];\n\nexport function decodeSample(muLawSample) {\n let sign;\n let exponent;\n let mantissa;\n let sample;\n muLawSample = ~muLawSample;\n sign = (muLawSample & 0x80);\n exponent = (muLawSample >> 4) & 0x07;\n mantissa = muLawSample & 0x0F;\n sample = decodeTable[exponent] + (mantissa << (exponent+3));\n if (sign !== 0) sample = -sample;\n\n return sample;\n}\n\nclass AudioConcatProcessor extends AudioWorkletProcessor {\n constructor() {\n super();\n this.buffers = [];\n this.cursor = 0;\n this.currentBuffer = null;\n this.currentBufferTimestamp = null;\n this.currentBufferStartSample = 0;\n this.wasInterrupted = false;\n this.playing = false;\n this.finished = false;\n\n this.playbackRate = 1.0;\n this.adjustUntilSample = null;\n this.sampleCounter = 0;\n this.format = { sampleRate: 48000 };\n\n // Time synchronization properties\n this.playbackStartTime = null;\n this.totalSamplesPlayed = 0;\n \n // Drift correction parameters\n this.maxCorrectionRate = 0.1; // Maximum 10% speed adjustment\n this.correctionSmoothness = 0.95; // How smoothly to apply corrections (0-1)\n this.targetPlaybackRate = 1.0;\n\n this.port.onmessage = ({ data }) => {\n switch (data.type) {\n case "setFormat":\n this.format = data.format;\n break;\n\n case "buffer":\n this.wasInterrupted = false;\n \n const bufferData = this.format.encoding === "ulaw"\n ? new Uint8Array(data.buffer)\n : new Int16Array(data.buffer);\n \n // Store buffer with its timestamp\n this.buffers.push({\n buffer: bufferData,\n timestamp: data.timestamp_ms\n });\n break;\n \n case "startPlayback":\n this.playing = true;\n this.playbackStartTime = currentTime;\n this.totalSamplesPlayed = 0;\n this.sampleCounter = 0;\n break;\n \n case "stopPlayback":\n this.playing = false;\n this.finished = true;\n \n // Discard all queued buffers\n this.buffers = [];\n \n // Clear current buffer and reset cursor\n this.currentBuffer = null;\n this.currentBufferTimestamp = null;\n this.cursor = 0;\n \n // Reset playback rate adjustments\n this.playbackRate = 1.0;\n this.adjustUntilSample = null;\n this.targetPlaybackRate = 1.0;\n \n // Notify that playback has stopped\n this.port.postMessage({ \n type: "process", \n finished: true,\n stopped: true\n });\n break;\n\n case "adjustPlaybackRate":\n this.playbackRate = data.rate;\n this.adjustUntilSample = this.sampleCounter + Math.floor(this.format.sampleRate * data.duration);\n break;\n \n case "reset":\n this.playing = false;\n this.finished = false;\n this.currentBuffer = null;\n this.currentBufferTimestamp = null;\n this.currentBufferStartSample = 0;\n this.cursor = 0;\n this.buffers = [];\n this.playbackStartTime = null;\n this.totalSamplesPlayed = 0;\n this.targetPlaybackRate = 1.0;\n break;\n\n case "interrupt":\n this.wasInterrupted = true;\n break;\n\n case "clearInterrupted":\n if (this.wasInterrupted) {\n this.wasInterrupted = false;\n this.buffers = [];\n this.currentBuffer = null;\n this.currentBufferTimestamp = null;\n }\n break;\n }\n };\n }\n\n // Calculate the expected timestamp based on samples played\n getExpectedTimestamp() {\n if (!this.playbackStartTime) return 0;\n \n // Convert samples to milliseconds\n const samplesPlayedMs = (this.totalSamplesPlayed / this.format.sampleRate) * 1000;\n return samplesPlayedMs;\n }\n\n // Calculate the actual timestamp of current playback position\n getCurrentActualTimestamp() {\n if (!this.currentBufferTimestamp) return 0;\n \n // Current position within the buffer in samples\n const samplesIntoBuffer = Math.floor(this.cursor);\n \n // Convert buffer position to milliseconds (assuming buffer covers specific time duration)\n // Each buffer typically represents a fixed time duration\n const bufferDurationMs = (this.currentBuffer.length / this.format.sampleRate) * 1000;\n const progressThroughBuffer = samplesIntoBuffer / this.currentBuffer.length;\n \n return this.currentBufferTimestamp + (progressThroughBuffer * bufferDurationMs);\n }\n\n // Calculate timing drift and adjust playback rate accordingly\n calculateDriftCorrection() {\n if (!this.currentBufferTimestamp || !this.playbackStartTime) {\n return 1.0;\n }\n\n const expectedTimestamp = this.getExpectedTimestamp();\n const actualTimestamp = this.getCurrentActualTimestamp();\n \n // Calculate drift in milliseconds\n const drift = actualTimestamp - expectedTimestamp;\n \n // Convert drift to a playback rate adjustment\n // Positive drift means we\'re ahead - slow down\n // Negative drift means we\'re behind - speed up\n let correctionFactor = 1.0;\n \n if (Math.abs(drift) > 10) { // Only correct if drift > 10ms\n // Calculate correction rate based on drift\n // More drift = more correction, but capped at maxCorrectionRate\n const driftRatio = Math.min(Math.abs(drift) / 1000, this.maxCorrectionRate);\n \n if (drift > 0) {\n // We\'re ahead, slow down\n correctionFactor = 1.0 - driftRatio;\n } else {\n // We\'re behind, speed up\n correctionFactor = 1.0 + driftRatio;\n }\n \n // Apply smoothing to avoid jarring rate changes\n this.targetPlaybackRate = this.targetPlaybackRate * this.correctionSmoothness + \n correctionFactor * (1 - this.correctionSmoothness);\n } else {\n // Gradually return to normal speed when drift is small\n this.targetPlaybackRate = this.targetPlaybackRate * this.correctionSmoothness + \n 1.0 * (1 - this.correctionSmoothness);\n }\n\n return this.targetPlaybackRate;\n }\n\n process(_, outputs, parameters) {\n const output = outputs[0][0];\n\n if (!this.playing) {\n output.fill(0);\n return true;\n }\n\n let finished = false;\n\n for (let i = 0; i < output.length; i++) {\n // If no buffer is ready, get the next one\n if (!this.currentBuffer) {\n if (this.buffers.length === 0) {\n finished = true;\n break;\n }\n \n const bufferData = this.buffers.shift();\n this.currentBuffer = bufferData.buffer;\n this.currentBufferTimestamp = bufferData.timestamp;\n this.currentBufferStartSample = this.totalSamplesPlayed;\n this.cursor = 0;\n }\n\n // Calculate drift correction for timing synchronization\n const driftCorrectedRate = this.calculateDriftCorrection();\n \n // Apply manual playback rate adjustments if active\n let finalPlaybackRate = driftCorrectedRate;\n if (this.adjustUntilSample !== null && this.sampleCounter < this.adjustUntilSample) {\n finalPlaybackRate *= this.playbackRate;\n } else if (this.adjustUntilSample !== null) {\n this.playbackRate = 1.0;\n this.adjustUntilSample = null;\n }\n\n const idx = Math.floor(this.cursor);\n const nextIdx = Math.min(idx + 1, this.currentBuffer.length - 1);\n\n let s1 = this.currentBuffer[idx];\n let s2 = this.currentBuffer[nextIdx];\n if (this.format.encoding === "ulaw") {\n s1 = decodeSample(s1);\n s2 = decodeSample(s2);\n }\n\n const frac = this.cursor - idx;\n const interpolated = s1 * (1 - frac) + s2 * frac;\n output[i] = interpolated / 32768;\n\n this.cursor += finalPlaybackRate;\n this.sampleCounter++;\n this.totalSamplesPlayed++;\n\n if (this.cursor >= this.currentBuffer.length) {\n this.currentBuffer = null;\n this.currentBufferTimestamp = null;\n }\n }\n\n if (this.finished !== finished) {\n this.finished = finished;\n this.port.postMessage({ \n type: "process", \n finished,\n // Optional: send timing info for debugging\n timing: {\n expectedTimestamp: this.getExpectedTimestamp(),\n actualTimestamp: this.getCurrentActualTimestamp(),\n playbackRate: this.targetPlaybackRate\n }\n });\n }\n\n return true;\n }\n}\nregisterProcessor("audio-concat-processor", AudioConcatProcessor);\n',function(e){try{var t,n=function(n){return t?n:r(function(){var t="data:application/javascript;base64,"+btoa(s);return Promise.resolve(e.addModule(t)).then(function(){a.set(o,t)})},function(){throw new Error("Failed to load the "+o+" worklet module. Make sure the browser supports AudioWorklets.")})},i=a.get(o);if(i)return Promise.resolve(e.addModule(i));var c=new Blob([s],{type:"application/javascript"}),u=URL.createObjectURL(c),d=r(function(){return Promise.resolve(e.addModule(u)).then(function(){a.set(o,u),t=1})},function(){URL.revokeObjectURL(u)});return Promise.resolve(d&&d.then?d.then(n):n(d))}catch(e){return Promise.reject(e)}});function u(e,t){try{var n=e()}catch(e){return t(e)}return n&&n.then?n.then(void 0,t):n}var d,h,l=/*#__PURE__*/function(){function e(e,t,n,i){this.context=void 0,this.analyser=void 0,this.gain=void 0,this.worklet=void 0,this._isMuted=!1,this.context=e,this.analyser=t,this.gain=n,this.worklet=i}e.createAudioOutput=function(t){var n=t.sampleRate,i=t.format;try{var r=null;return Promise.resolve(u(function(){var t=(r=new AudioContext({sampleRate:n})).createAnalyser(),o=r.createGain();return o.connect(t),t.connect(r.destination),Promise.resolve(c(r.audioWorklet)).then(function(){var n=new AudioWorkletNode(r,"audio-concat-processor");n.port.postMessage({type:"setFormat",format:i}),n.connect(o);var s=new e(r,t,o,n);return Promise.resolve(s.ensureIOSCompatibility()).then(function(){return s})})},function(e){var t;throw null==(t=r)||t.close(),e}))}catch(e){return Promise.reject(e)}};var t=e.prototype;return t.ensureIOSCompatibility=function(){try{var e=this;if(/iPad|iPhone|iPod/.test(navigator.userAgent)){var t=e.context.createBuffer(1,1,e.context.sampleRate),n=e.context.createBufferSource();n.buffer=t,n.connect(e.context.destination),n.start()}return Promise.resolve()}catch(e){return Promise.reject(e)}},t.getOutputDevice=function(){try{var e=this;return Promise.resolve(u(function(){var t=e.context.sinkId||"";return Promise.resolve(navigator.mediaDevices.enumerateDevices()).then(function(e){var n=e.filter(function(e){return"audiooutput"===e.kind});return""===t?n.find(function(e){return"default"===e.deviceId})||n[0]||null:n.find(function(e){return e.deviceId===t})||null})},function(e){return console.error("Error getting output device:",e),null}))}catch(e){return Promise.reject(e)}},t.getAvailableOutputDevices=function(){try{return Promise.resolve(u(function(){return Promise.resolve(navigator.mediaDevices.enumerateDevices()).then(function(e){return e.filter(function(e){return"audiooutput"===e.kind})})},function(e){return console.error("Error enumerating devices:",e),[]}))}catch(e){return Promise.reject(e)}},t.mute=function(){this.gain.gain.value=0,this._isMuted=!0},t.unmute=function(){this.gain.gain.value=1,this._isMuted=!1},t.toggleMute=function(){return this._isMuted?this.unmute():this.mute(),this._isMuted},t.close=function(){try{return Promise.resolve(this.context.close()).then(function(){})}catch(e){return Promise.reject(e)}},e}(),f=/*#__PURE__*/function(){function e(e,t,n){var i=this;this.syncController=void 0,this.audioOutput=void 0,this.videoOutput=void 0,this.initialized=!1,this.isPlaying=!1,this.isPlayingAudio=!1,this.isPlayingVideo=!1,this.isStoppingAV=!1,this.handleAudioWorkletMessage=function(e){"process"===e.type&&(i.isPlayingAudio=!1,i.isPlayingVideo||(i.isPlaying=!1),i.audioOutput.worklet.port.postMessage({type:"reset"}))},this.syncController=e,this.audioOutput=t,this.videoOutput=n,this.videoOutput.setEventCallbacks({onIdleVideoShown:function(){},onIdleVideoHidden:function(){i.isPlayingVideo=!1,i.isPlayingAudio||(i.isPlaying=!1),i.videoOutput.stopBufferMonitoring()}})}var t=e.prototype;return t.updatePlayingState=function(e){this.isPlaying=e,this.isPlayingAudio=e,this.isPlayingVideo=e},t.toggleStoppingVideo=function(e){this.isStoppingAV=e},t.startPlayback=function(e){void 0===e&&(e=!1);try{var t=this;return t.isPlaying?Promise.resolve():(e&&(t.initialized=e),Promise.resolve(t.audioOutput.context.resume()).then(function(){t.updatePlayingState(!0),t.videoOutput.startStreaming(e),t.audioOutput.worklet.port.postMessage({type:"startPlayback"})}))}catch(e){return Promise.reject(e)}},t.playAudioVideo=function(){try{var e=this;if(!e.initialized)return Promise.resolve();if(e.isPlaying||e.isStoppingAV)return Promise.resolve();var t=e.videoOutput.getBufferLength();return e.startPlayback(!0),t>=6&&e.startPlayback(),Promise.resolve()}catch(e){return Promise.reject(e)}},e}();function p(e){return e.event===exports.EventType.JOIN}function m(e){return e.event===exports.EventType.TEXT}function v(e){return e.event===exports.EventType.RESPONSE}function g(e){return e.event===exports.EventType.STREAMING}function y(e){return e.event===exports.EventType.STREAMING&&e.type===exports.StreamingEventType.ERROR}function P(e){return e.event===exports.EventType.BINARY}function S(e){return e.type===exports.EventType.PONG}function w(e){return e.event===exports.EventType.TIMEOUT_WARNING}function I(e){return e.event===exports.EventType.TIME_OUT}function E(e){return e.event===exports.EventType.KEEP_SESSION}function T(e){switch(e){case"production":default:return"https://chat-api.unith.ai";case"staging":return"https://chat-api.stg.unith.live";case"development":return"https://chat-api.dev.unith.live"}}function b(e){return!!e.event}exports.EventType=void 0,(d=exports.EventType||(exports.EventType={})).TEXT="text",d.CONVERSATION_END="conversation_end",d.JOIN="join",d.ERROR="error",d.TIME_OUT="timeout",d.UNITH_NLP_EXCEPTION="unith_nlp_exception",d.ANALYTICS="analytics-userFeedback",d.CHOICE="choice",d.TIMEOUT_WARNING="timeout_warning",d.KEEP_SESSION="keep_session",d.RESPONSE="response",d.STREAMING="streaming",d.PING="ping",d.PONG="pong",d.BINARY="binary",exports.StreamingEventType=void 0,(h=exports.StreamingEventType||(exports.StreamingEventType={})).VIDEO_FRAME="video_frame",h.AUDIO_FRAME="audio_frame",h.METADATA="metadata",h.ERROR="error",h.CACHE="cache";var k=/*#__PURE__*/function(){function e(e,t){var n=this;this.socket=void 0,this.userId=void 0,this.queue=[],this.disconnectionDetails=null,this.onDisconnectCallback=null,this.onMessageCallback=null,this.onPingPongCallback=null,this.socket=e,this.userId=t,this.socket.addEventListener("error",function(e){console.error("WebSocket error:",e),n.disconnect({reason:"error",message:"The connection was closed due to a websocket error.",context:e})}),this.socket.addEventListener("close",function(e){console.log("WebSocket closed:",e),n.disconnect({reason:"error",message:e.reason||"The connection was closed by the server.",context:e})}),this.socket.addEventListener("message",function(e){try{var t;if(S(t=e.data instanceof Blob||e.data instanceof ArrayBuffer?{event:exports.EventType.BINARY,user_id:"",username:"",data:e.data}:JSON.parse(e.data))){if(!n.onPingPongCallback)return;return void n.onPingPongCallback(t)}if(!b(t))return;n.onMessageCallback?n.onMessageCallback(t):n.queue.push(t)}catch(e){}})}e.create=function(t){try{var n=null,i=function(e){switch(e){case"production":default:return"wss://stream-api.unith.ai/stream-hub";case"staging":return"wss://stream-api.stg.unith.live/stream-hub";case"development":return"wss://stream-api.dev.unith.live/stream-hub"}}(t.environment);return Promise.resolve(function(r,o){try{var s=(n=new WebSocket(i+"/"+t.orgId+"/"+t.headId),Promise.resolve(new Promise(function(e,i){n.addEventListener("open",function(){n.send(JSON.stringify({token:t.token,api_key:t.apiKey,text_only:!0,format:"vp8",quality:"standard",crop:!1}))},{once:!0}),n.addEventListener("close",function(e){console.log("WebSocket closed during connection:",e),i()}),n.addEventListener("error",i),n.addEventListener("message",function(n){var r=JSON.parse(n.data);b(r)&&(p(r)?r.granted&&(null==t.onJoin||t.onJoin(),e(r.user_id)):y(r)&&i({type:"connection",message:"We are currently at full capacity. Please try again later."}))},{once:!0})})).then(function(t){return new e(n,t)}))}catch(e){return o(e)}return s&&s.then?s.then(void 0,o):s}(0,function(e){var t;throw null==(t=n)||t.close(),e}))}catch(e){return Promise.reject(e)}};var t=e.prototype;return t.disconnect=function(e){var t;this.disconnectionDetails||(this.disconnectionDetails=e,null==(t=this.onDisconnectCallback)||t.call(this,e))},t.close=function(){this.socket.close()},t.sendMessage=function(e){this.socket.send(JSON.stringify(e))},t.sendPingEvent=function(e){this.onPingPongCallback?this.socket.send(JSON.stringify(e)):console.warn("Ping event sent without a callback set.")},t.onPingPong=function(e){this.onPingPongCallback=e},t.onMessage=function(e){this.onMessageCallback=e;var t=this.queue;this.queue=[],t.length>0&&queueMicrotask(function(){t.forEach(e)})},t.onDisconnect=function(e){this.onDisconnectCallback=e;var t=this.disconnectionDetails;t&&queueMicrotask(function(){e(t)})},e}(),V=/*#__PURE__*/function(){function e(e,t){this.idleVideoSource=void 0,this.videoId=void 0,this.idleVideoSource=e,this.videoId=t}return e.getIdleVideo=function(t,n,i){try{return Promise.resolve(e.getIdleVideoId(t,n,i)).then(function(r){return Promise.resolve(fetch(t+"/api/v1/idle/"+n+"/"+i+"/"+r)).then(function(t){return t.status>=200&&t.status<=299?Promise.resolve(t.json()).then(function(t){if(t)return new e(t,r);throw new Error("No idle video found for the specified orgId and headId.")}):Promise.resolve(t.text()).then(function(e){var n=new Error("An error occurred retrieving idle video: "+t.status+" "+t.statusText+". Response: "+e);throw n.name="IdleVideoError",n})})})}catch(e){return Promise.reject(e)}},e.prototype.getAvatarSrc=function(e,t,n){try{return Promise.resolve(fetch(e+"/api/v1/avatar/"+t+"/"+n+"/"+this.videoId)).then(function(e){return e.status>=200&&e.status<=299?Promise.resolve(e.json()).then(function(e){if(e)return e;throw new Error("No avatar image found for the specified orgId and headId.")}):Promise.resolve(e.text()).then(function(t){var n=new Error("An error occurred retrieving avatar: "+e.status+" "+e.statusText+". Response: "+t);throw n.name="AvatarError",n})})}catch(e){return Promise.reject(e)}},e.getIdleVideoId=function(e,t,n){try{return Promise.resolve(fetch(e+"/api/v1/videos/"+t+"/"+n)).then(function(e){return e.status>=200&&e.status<=299?Promise.resolve(e.json()).then(function(e){if(e.length>0)return e[0].id;throw new Error("No idle video found for the specified orgId and headId.")}):Promise.resolve(e.text()).then(function(t){var n=new Error("An error occurred retrieving idle video: "+e.status+" "+e.statusText+". Response: "+t);throw n.name="IdleVideoError",n})})}catch(e){return Promise.reject(e)}},e}();function C(e,t){try{var n=e()}catch(e){return t(e)}return n&&n.then?n.then(void 0,t):n}var O=/*#__PURE__*/function(){function n(e,t,n,i,r,o,s,a){this.provider=void 0,this.options=void 0,this.elevenLabsOptions=void 0,this.user=void 0,this.headInfo=void 0,this.sendMessage=void 0,this.microphoneStatus="OFF",this.microphoneAccess=!1,this.recognizer=null,this.connection=null,this.tokenObj={token:"",generatedAt:null,region:""},this.provider=e,this.options=t,this.elevenLabsOptions=n,this.user=i,this.headInfo=r,this.sendMessage=a,this.microphoneAccess=o,this.tokenObj=s,this.microphoneAccess||this.options.onMicrophoneError({message:"Microphone access not granted."})}n.initializeMicrophone=function(e,t,i,r,o,s,a){try{var c={token:"",generatedAt:null,region:""};return Promise.resolve(r.getAsrToken("eleven_labs"===t?"elevenlabs":"azure")).then(function(u){return c={token:u.token,generatedAt:Date.now(),region:u.region},new n(t,e,i,r,o,s,c,a)})}catch(e){return Promise.reject(e)}};var i=n.prototype;return i.updateMicrophoneStatus=function(e){this.microphoneStatus=e,this.options.onMicrophoneStatusChange({status:e})},i.handleRecognitionResult=function(e){e.length<2||this.options.onMicrophoneSpeechRecognitionResult({transcript:e})},i.startAzureMicrophone=function(){try{var t=function(t){var i=e.SpeechConfig.fromAuthorizationToken(n.tokenObj.token,n.tokenObj.region);i.speechRecognitionLanguage=n.headInfo.language||"en-US";var r=e.AudioConfig.fromDefaultMicrophoneInput();n.recognizer=new e.SpeechRecognizer(i,r),n.headInfo.phrases.length>0&&e.PhraseListGrammar.fromRecognizer(n.recognizer).addPhrases(n.headInfo.phrases),n.recognizer.recognized=function(t,i){i.result.reason===e.ResultReason.RecognizedSpeech?n.handleRecognitionResult(i.result.text):i.result.reason===e.ResultReason.NoMatch&&n.options.onMicrophoneError({message:"Speech could not be recognized - No clear speech detected"})},n.recognizer.startContinuousRecognitionAsync(function(){n.updateMicrophoneStatus("ON")},function(){console.error("Error starting recognizer")})},n=this,i=function(){if(!n.tokenObj.token||Date.now()-(n.tokenObj.generatedAt||0)>9e5)return Promise.resolve(n.user.getAsrToken("azure")).then(function(e){if(!e.region||!e.token)throw new Error("Failed to initialize Azure microphone.");n.tokenObj={token:e.token,region:e.region,generatedAt:Date.now()}})}();return Promise.resolve(i&&i.then?i.then(t):t())}catch(e){return Promise.reject(e)}},i.stopAzureMicrophone=function(){try{var e=this;return e.recognizer&&e.recognizer.stopContinuousRecognitionAsync(function(){e.updateMicrophoneStatus("OFF")},function(t){e.options.onMicrophoneError({message:"Error stopping microphone : "+t})}),Promise.resolve()}catch(e){return Promise.reject(e)}},i.startElevenLabsMicrophone=function(){try{var e=function(e){n.connection=t.Scribe.connect({token:n.tokenObj.token,modelId:"scribe_v2_realtime",includeTimestamps:!0,microphone:{echoCancellation:!0,noiseSuppression:n.elevenLabsOptions.noiseSuppression},commitStrategy:t.CommitStrategy.VAD,vadSilenceThresholdSecs:n.elevenLabsOptions.vadSilenceThresholdSecs,vadThreshold:n.elevenLabsOptions.vadThreshold,minSpeechDurationMs:n.elevenLabsOptions.minSpeechDurationMs,minSilenceDurationMs:n.elevenLabsOptions.minSilenceDurationMs}),n.connection.on(t.RealtimeEvents.SESSION_STARTED,function(){n.updateMicrophoneStatus("ON")}),n.connection.on(t.RealtimeEvents.COMMITTED_TRANSCRIPT,function(e){n.handleRecognitionResult(e.text)}),n.connection.on(t.RealtimeEvents.ERROR,function(e){console.error("Error:",e),n.options.onMicrophoneError({message:"Error recognizing speech"})})},n=this,i=function(){if(!n.tokenObj.token||Date.now()-(n.tokenObj.generatedAt||0)>9e5)return Promise.resolve(n.user.getAsrToken("elevenlabs")).then(function(e){if(!e.region||!e.token)throw new Error("Failed to initialize Eleven Labs microphone.");n.tokenObj={token:e.token,region:e.region,generatedAt:Date.now()}})}();return Promise.resolve(i&&i.then?i.then(e):e())}catch(e){return Promise.reject(e)}},i.stopElevenLabsMicrophone=function(){try{var e=this,t=C(function(){function t(){e.connection=null}var n=function(){if(e.connection)return Promise.resolve(e.connection.close()).then(function(){e.updateMicrophoneStatus("OFF")})}();return n&&n.then?n.then(t):t()},function(t){e.options.onMicrophoneError({message:"Error stopping microphone : "+t})});return Promise.resolve(t&&t.then?t.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},i.toggleMicrophone=function(){try{var e=this;return Promise.resolve(C(function(){var t=function(){if("ON"===e.microphoneStatus){e.updateMicrophoneStatus("PROCESSING");var t=function(){if("azure"===e.provider)return Promise.resolve(e.stopAzureMicrophone()).then(function(){});var t=function(){if("eleven_labs"===e.provider)return Promise.resolve(e.stopElevenLabsMicrophone()).then(function(){})}();return t&&t.then?t.then(function(){}):void 0}();if(t&&t.then)return t.then(function(){})}else{var n=function(){if("OFF"===e.microphoneStatus){e.updateMicrophoneStatus("PROCESSING");var t=function(){if("azure"===e.provider)return Promise.resolve(e.startAzureMicrophone()).then(function(){});var t=function(){if("eleven_labs"===e.provider)return Promise.resolve(e.startElevenLabsMicrophone()).then(function(){})}();return t&&t.then?t.then(function(){}):void 0}();if(t&&t.then)return t.then(function(){})}else console.error("Microphone is currently processing. Please wait.")}();if(n&&n.then)return n.then(function(){})}}();if(t&&t.then)return t.then(function(){})},function(t){e.updateMicrophoneStatus("OFF"),e.options.onMicrophoneError({message:t.message||"An unknown error occurred."})}))}catch(e){return Promise.reject(e)}},i.status=function(){return this.microphoneStatus},n}(),A=/*#__PURE__*/function(){function e(e,t){this.connection=void 0,this.intervalId=null,this.pingInterval=void 0,this.timeout=void 0,this.lastPingTimestamp="0",this.history=[],this.maxHistory=void 0,this.onUpdate=void 0,this.connection=e,this.pingInterval=(null==t?void 0:t.pingInterval)||4e3,this.timeout=(null==t?void 0:t.timeout)||2e3,this.maxHistory=(null==t?void 0:t.maxHistory)||3,this.onUpdate=null==t?void 0:t.onUpdate,this.handleMessage=this.handleMessage.bind(this),this.connection.onPingPong(this.handleMessage)}var t=e.prototype;return t.start=function(){var e=this;this.intervalId||(this.intervalId=setInterval(function(){e.sendPing()},this.pingInterval))},t.stop=function(){clearInterval(this.intervalId),this.intervalId=null},t.destroy=function(){this.stop()},t.sendPing=function(){var e=performance.now().toString();this.lastPingTimestamp=e;var t={event:exports.EventType.PING,timestamp:this.lastPingTimestamp,id:"0"};this.connection.sendPingEvent(t)},t.handleMessage=function(e){try{if(e.timestamp===this.lastPingTimestamp){var t=performance.now()-parseFloat(e.timestamp);this.recordLatency(t)}}catch(e){console.log("logging irrelevant message")}},t.recordLatency=function(e){this.history.push(e),this.history.length>this.maxHistory&&this.history.shift();var t=this.history.reduce(function(e,t){return e+t},0)/this.history.length,n=this.classifyLatency(t);this.onUpdate&&this.onUpdate({rtt:e,average:t,status:n})},t.classifyLatency=function(e){return e<100?"good":e<250?"moderate":"poor"},e}(),x=/*#__PURE__*/function(){function e(e){this.config=void 0,this.driftHistory=[],this.correctionInProgress=!1,this.lastAudioTiming=null,this.lastVideoTiming=null,this.config=e}var t=e.prototype;return t.updateAudioTime=function(e){this.lastAudioTiming={timestamp:performance.now(),relativeTime:e}},t.updateVideoTime=function(e){this.lastVideoTiming={timestamp:performance.now(),relativeTime:e}},t.resetTiming=function(){this.lastAudioTiming=null,this.lastVideoTiming=null},t.checkSync=function(){try{var e=this;if(e.correctionInProgress)return console.warn("Sync correction already in progress"),Promise.resolve();if(!e.lastAudioTiming||!e.lastVideoTiming)return console.warn("Insufficient timing data for sync check"),Promise.resolve();var t=Math.abs(e.lastAudioTiming.relativeTime-e.lastVideoTiming.relativeTime);return e.recordDrift(t),t>e.config.tolerance?console.log("Drift detected: "+t+"ms"):console.log("No significant drift detected"),Promise.resolve()}catch(e){return Promise.reject(e)}},t.recordDrift=function(e){this.driftHistory.push({timestamp:Date.now(),drift:e,audioTime:this.lastAudioTiming.relativeTime,videoTime:this.lastVideoTiming.relativeTime}),this.driftHistory.length>this.config.historyLength&&this.driftHistory.shift()},e}(),R=window.localStorage,M=window.location.origin+window.location.pathname,D=function(e,t,n){if(void 0!==R)return R.getItem("chat:"+M+":"+t+":"+n+":"+e)},_=function(e,t,n,i){void 0===R||R.setItem("chat:"+M+":"+n+":"+i+":"+e,t)};function F(e,t){try{var n=e()}catch(e){return t(e)}return n&&n.then?n.then(void 0,t):n}var j,N,B=/*#__PURE__*/function(){function e(e,t,n,i,r,o,s,a){this.id=void 0,this.username=void 0,this.password=void 0,this.orgId=void 0,this.headId=void 0,this.apiBase=void 0,this.EXPIRATION_OFFSET=9e5,this.accessToken="",this.tokenType="",this.sessionId=0,this.id=n,this.username=i,this.password=r,this.orgId=o,this.headId=s,this.apiBase=a,this.accessToken=e,this.tokenType=t;var c=D("session_id",o,s);c&&(this.sessionId=parseInt(c),this.sessionId+=1),_("session_id",this.sessionId.toString(),o,s)}e.loginUser=function(t,n,i,r,o){try{var s=new FormData;return s.append("username",t),s.append("password",n),Promise.resolve(F(function(){return Promise.resolve(fetch(i+"/token",{method:"POST",body:s})).then(function(s){return s.status>=200&&s.status<=299?Promise.resolve(s.json()).then(function(s){return new e(s.access_token,s.token_type,s.user_id,t,n,r,o,i)}):Promise.resolve(s.text()).then(function(e){var t=new Error("An error occurred: "+s.status+" "+s.statusText+". Response: "+e);throw t.response={status:s.status,status_code:s.status,data:JSON.parse(e)},t})})},function(e){var t,n;if(null!=e&&null!=(t=e.response)&&null!=(t=t.data)&&t.detail)throw new Error(null==e||null==(n=e.response)||null==(n=n.data)?void 0:n.detail);throw new Error(JSON.stringify(e))}))}catch(e){return Promise.reject(e)}};var t=e.prototype;return t.getHeadDetails=function(e){try{var t=this,n=t.apiBase+"/api/v1/head/"+t.orgId+"/"+t.headId+"/"+e;return Promise.resolve(F(function(){return Promise.resolve(fetch(n)).then(function(e){return e.status>=200&&e.status<=299?Promise.resolve(e.json()):Promise.resolve(e.text()).then(function(t){var n=new Error("An error occurred: "+e.status+" "+e.statusText+". Response: "+t);throw n.response={status:e.status,status_code:e.status,data:JSON.parse(t)},n})})},function(e){var t,n;if(null!=e&&null!=(t=e.response)&&null!=(t=t.data)&&t.detail)throw new Error(null==e||null==(n=e.response)||null==(n=n.data)?void 0:n.detail);throw new Error(JSON.stringify(e))}))}catch(e){return Promise.reject(e)}},t.getAuthToken=function(e,t){try{var n,i=this.apiBase+"/token",r=new FormData;r.append("username",e),r.append("password",t);var o=F(function(){return Promise.resolve(fetch(i,{method:"POST",body:r})).then(function(e){return Promise.resolve(e.json()).then(function(e){n=e})})},function(e){var t,n;if(null!=e&&null!=(t=e.response)&&null!=(t=t.data)&&t.detail)throw new Error(null==e||null==(n=e.response)||null==(n=n.data)?void 0:n.detail);throw new Error(JSON.stringify(e))});return Promise.resolve(o&&o.then?o.then(function(e){return n}):n)}catch(e){return Promise.reject(e)}},t.getProviderToken=function(e,t){try{var n,i,r=function(e){return{token:n,region:i}},o=this.apiBase+"/api/v1/asr_token?provider="+t,s=F(function(){return Promise.resolve(fetch(o,{method:"GET",headers:{Authorization:"Bearer "+e}})).then(function(e){return Promise.resolve(e.json()).then(function(e){n=e.token,i=e.region})})},function(e){var t,n;if(null!=e&&null!=(t=e.response)&&null!=(t=t.data)&&t.detail)throw new Error(null==e||null==(n=e.response)||null==(n=n.data)?void 0:n.detail);throw new Error(JSON.stringify(e))});return Promise.resolve(s&&s.then?s.then(r):r())}catch(e){return Promise.reject(e)}},t.getAccessToken=function(){try{var e=function(e){var o=JSON.parse(window.atob(n.split(".")[1]));return t.username=o.username,{access_token:n,user_id:i,session_id:r}},t=this,n=D("access_token",t.orgId,t.headId),i=D("user_id",t.orgId,t.headId),r=parseInt(D("session_id",t.orgId,t.headId)||"")||0,o=!0;if("undefined"!==n&&n&&i&&"number"==typeof r){var s=1e3*JSON.parse(window.atob(n.split(".")[1])).exp,a=(new Date).getTime()+t.EXPIRATION_OFFSET;o=a>s}var c=function(){if(o)return Promise.resolve(t.getAuthToken(t.username,t.password)).then(function(e){if(!e)throw new Error("Could not renew authentication token");i=e.user_id,r++,_("access_token",n=e.access_token,t.orgId,t.headId),_("user_id",i,t.orgId,t.headId),_("session_id",r.toString(),t.orgId,t.headId)})}();return Promise.resolve(c&&c.then?c.then(e):e())}catch(e){return Promise.reject(e)}},t.getAsrToken=function(e){try{var t=this,n=D("asr_token",t.orgId,t.headId),i=D("region",t.orgId,t.headId);return Promise.resolve(t.getAccessToken()).then(function(r){var o,s=r.access_token;function a(e){return o?e:{token:n,region:i}}var c=!0;if(n&&i){var u=1e3*JSON.parse(window.atob(n.split(".")[1])).exp,d=(new Date).getTime()+t.EXPIRATION_OFFSET;c=d>u}var h=function(){if(c)return Promise.resolve(t.getProviderToken(s,e)).then(function(e){if(!e)return o=1,{token:"",region:""};i=e.region,_("asr_token",n=e.token,t.orgId,t.headId),_("asr_region",i,t.orgId,t.headId)})}();return h&&h.then?h.then(a):a(h)})}catch(e){return Promise.reject(e)}},e}();function L(e,t){try{var n=e()}catch(e){return t(e)}return n&&n.then?n.then(void 0,t):n}!function(e){e.INITIALIZING="initializing",e.READY="ready",e.PLAYING="playing",e.PAUSED="paused",e.INTERRUPTED="interrupted",e.DESTROYED="destroyed"}(j||(j={})),exports.VideoTransitionType=void 0,(N=exports.VideoTransitionType||(exports.VideoTransitionType={})).NONE="none",N.CROSSFADE="crossfade",N.FADEIN="fadein",N.FADEOUT="fadeout";var z=/*#__PURE__*/function(){function e(e,t,n,r){var o=this;this.canvas=void 0,this.ctx=void 0,this.container=void 0,this.config=void 0,this.resizeObserver=null,this.decoder=null,this.state=j.INITIALIZING,this.isProcessingFrame=!1,this.isStreaming=!1,this.startTime=0,this.frameBuffer=[],this.currentSequenceId=0,this.animationFrameId=null,this.renderLoop=!1,this.fpsCounter={count:0,lastTime:0},this.handleContextLoss=function(){},this.handleContextRestore=function(){},this.handleResize=function(){var e=o.container.getBoundingClientRect(),t=e.width,n=e.height;o.canvas.width=t,o.canvas.height=n,o.canvas.style.width=t+"px",o.canvas.style.height=n+"px",o.ctx.imageSmoothingEnabled=!0},this.canvas=e,this.ctx=t,this.container=n,this.config=i({maxBufferSize:1e3,enableAdaptiveQuality:!1},r),this.setupContextLossHandling(),this.setupResizeHandling(),this.initializeDecoder()}e.create=function(t,n){try{var i;if(!("VideoDecoder"in window))throw new Error("WebCodecs VideoDecoder API is not supported in this browser");var r=document.createElement("canvas");r.width=n.width,r.height=n.height,r.style.position="absolute",r.style.top="0",r.style.left="0",r.style.backgroundColor=(null==(i=n.backgroundColor)?void 0:i.toString())||"#000000",r.style.maxWidth="100%",r.style.height="100%",r.style.zIndex="0",r.style.pointerEvents="none",r.style.objectFit="cover",r.style.imageRendering="pixelated";var o=r.getContext("2d",{alpha:!1,desynchronized:!0});if(!o)throw new Error("Failed to get 2D canvas context");return o.imageSmoothingEnabled=!0,t.appendChild(r),Promise.resolve(new e(r,o,t,n))}catch(e){return Promise.reject(e)}};var t=e.prototype;return t.initializeDecoder=function(){try{var e=this;return Promise.resolve(L(function(){return new VideoDecoder({output:function(){},error:function(){}}).close(),e.decoder=new VideoDecoder({output:function(t){e.renderVideoFrame(t)},error:function(e){console.error("VP8 Decoder error:",e.message)}}),Promise.resolve(e.decoder.configure({codec:"vp8",codedWidth:e.config.width,codedHeight:e.config.height})).then(function(){})},function(e){throw new Error("Failed to initialize VP8 decoder: "+e.message)}))}catch(e){return Promise.reject(e)}},t.setupResizeHandling=function(){var e=this;this.resizeObserver=new ResizeObserver(function(t){for(var i,r=function(e){var t="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(t)return(t=t.call(e)).next.bind(t);if(Array.isArray(e)||(t=function(e,t){if(e){if("string"==typeof e)return n(e,t);var i={}.toString.call(e).slice(8,-1);return"Object"===i&&e.constructor&&(i=e.constructor.name),"Map"===i||"Set"===i?Array.from(e):"Arguments"===i||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(i)?n(e,t):void 0}}(e))){t&&(e=t);var i=0;return function(){return i>=e.length?{done:!0}:{done:!1,value:e[i++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}(t);!(i=r()).done;)i.value.target===e.container&&e.handleResize()}),this.resizeObserver.observe(this.container),window.addEventListener("resize",this.handleResize)},t.getBufferLength=function(){return this.frameBuffer.length},t.getStreamingStatus=function(){return this.isStreaming},t.addFrame=function(e,t,n){void 0===n&&(n=!1);try{var i=this;if(i.state===j.DESTROYED)throw new Error("Cannot add frame to destroyed video output");var r={data:e,timestamp:t,isKeyframe:n,sequenceId:i.currentSequenceId++,size:e.byteLength};if(i.frameBuffer.length>=(i.config.maxBufferSize||1e3))if(i.config.enableAdaptiveQuality){if(!n)return Promise.resolve();var o=i.frameBuffer.findIndex(function(e){return!e.isKeyframe});-1!==o?i.frameBuffer.splice(o,1):i.frameBuffer.shift()}else i.frameBuffer.shift();return i.frameBuffer.push(r),Promise.resolve()}catch(e){return Promise.reject(e)}},t.toggleStream=function(e){try{return this.isStreaming=e,Promise.resolve()}catch(e){return Promise.reject(e)}},t.clearFrame=function(){if(this.state===j.DESTROYED)throw new Error("Cannot clear frame from destroyed video output");if(this.renderLoop=!1,this.animationFrameId&&(cancelAnimationFrame(this.animationFrameId),this.animationFrameId=null),this.frameBuffer=[],this.isProcessingFrame=!1,this.currentSequenceId=0,this.startTime=0,this.fpsCounter={count:0,lastTime:0},this.decoder&&"configured"===this.decoder.state)try{this.decoder.flush()}catch(e){console.warn("Error flushing decoder:",e)}this.clearCanvas(),this.state=j.READY},t.interrupt=function(e){var t=this;if(void 0===e&&(e=!1),this.state!==j.DESTROYED){if(this.state=j.INTERRUPTED,this.frameBuffer=[],this.isProcessingFrame=!1,this.decoder&&"configured"===this.decoder.state)try{this.decoder.flush()}catch(e){console.warn("Error flushing decoder:",e)}e?this.fadeOutCanvas().then(function(){t.clearCanvas()}):this.clearCanvas()}},t.destroy=function(){this.state!==j.DESTROYED&&(this.state=j.DESTROYED,this.renderLoop=!1,this.animationFrameId&&(cancelAnimationFrame(this.animationFrameId),this.animationFrameId=null),this.decoder&&(this.decoder.close(),this.decoder=null),this.frameBuffer=[],this.canvas.removeEventListener("webglcontextlost",this.handleContextLoss),this.canvas.removeEventListener("webglcontextrestored",this.handleContextRestore),this.canvas.parentNode&&this.canvas.parentNode.removeChild(this.canvas),this.isProcessingFrame=!1)},t.getState=function(){return this.state},t.startRenderingStreamingVideo=function(e){e&&(this.state=j.READY,this.renderLoop=!0,this.startTime=performance.now(),this.render())},t.render=function(){var e,t=this;if(this.renderLoop&&this.state!==j.DESTROYED){var n=(null==(e=this.frameBuffer[0])?void 0:e.timestamp)||0;this.frameBuffer.length>0&&0===this.frameBuffer[0].sequenceId&&(this.startTime=performance.now());var i=performance.now()-this.startTime;i>=n&&this.frameBuffer.length>0&&!this.isProcessingFrame&&this.processNextFrame(i,i-n),this.animationFrameId=requestAnimationFrame(function(){return t.render()})}},t.processNextFrame=function(e,t){try{var n=this;if(n.isProcessingFrame||0===n.frameBuffer.length||!n.decoder)return Promise.resolve();if("configured"!==n.decoder.state)return Promise.resolve();n.isProcessingFrame=!0;var i=n.frameBuffer.shift(),r=function(e,t){try{var r=L(function(){return Promise.resolve(n.decodeVP8Frame(i)).then(function(){})},function(e){console.error("Frame processing failed:",e)})}catch(e){return t(!0,e)}return r&&r.then?r.then(t.bind(null,!1),t.bind(null,!0)):t(!1,r)}(0,function(e,t){if(n.isProcessingFrame=!1,e)throw t;return t});return Promise.resolve(r&&r.then?r.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},t.decodeVP8Frame=function(e){try{try{var t=new EncodedVideoChunk({type:e.isKeyframe?"key":"delta",timestamp:1e3*e.timestamp,data:e.data});this.decoder.decode(t)}catch(e){throw new Error("VP8 decode failed: "+e.message)}return Promise.resolve()}catch(e){return Promise.reject(e)}},t.renderVideoFrame=function(e){try{this.ctx.clearRect(0,0,this.canvas.width,this.canvas.height);var t,n,i,r,o=e.displayWidth/e.displayHeight;o>this.canvas.width/this.canvas.height?(i=(this.canvas.width-(t=(n=this.canvas.height)*o))/2,r=0):(i=0,r=(this.canvas.height-(n=(t=this.canvas.width)/o))/2),this.ctx.drawImage(e,i,r,t,n),e.close()}catch(e){console.error("Error rendering video frame:",e)}},t.clearCanvas=function(){this.ctx.clearRect(0,0,this.canvas.width,this.canvas.height)},t.fadeOutCanvas=function(){try{var e=this;return Promise.resolve(new Promise(function(t){var n=1,i=function(){e.canvas.style.opacity=(n-=.05).toString(),n<=0?(e.canvas.style.opacity="1",t()):requestAnimationFrame(i)};i()}))}catch(e){return Promise.reject(e)}},t.setupContextLossHandling=function(){var e=this;this.handleContextLoss=function(t){t.preventDefault(),console.warn("Canvas context lost"),e.state=j.PAUSED},this.handleContextRestore=function(){e.state===j.PAUSED&&(e.state=j.PLAYING)},this.canvas.addEventListener("webglcontextlost",this.handleContextLoss),this.canvas.addEventListener("webglcontextrestored",this.handleContextRestore)},e}();function U(e,t){try{var n=e()}catch(e){return t(e)}return n&&n.then?n.then(void 0,t):n}var W=/*#__PURE__*/function(){function e(e,t,n,i){this.videoOutput=void 0,this.container=void 0,this.idleVideo=null,this.cachedVideo=null,this.idleVideoConfig=null,this.videoTransitionConfig=null,this.CROSSFADE_DURATION=500,this.isTransitioning=!1,this.isShowingIdleVideo=!0,this.bufferCheckAnimationId=null,this.lastBufferCheckTime=0,this.sessionStarted=!1,this.isShowingCachedVideo=!1,this.onIdleVideoShown=void 0,this.onIdleVideoHidden=void 0,this.onSpeakingStartCallback=null,this.onSpeakingEndCallback=null,this.videoOutput=e,this.container=t,this.videoTransitionConfig=n}e.createVideoOutput=function(t,n){try{return Promise.resolve(z.create(t,n)).then(function(i){var r=new e(i,t,n.transition,n.effects);return Promise.resolve(r.setupIdleVideo(n.idleVideo,n)).then(function(){return Promise.resolve(r.setupCachedVideo(n)).then(function(){return r})})})}catch(e){return Promise.reject(e)}};var t=e.prototype;return t.setupIdleVideo=function(e,t){try{var n=this;n.idleVideoConfig=e,n.idleVideo=document.createElement("video"),n.idleVideo.src=e.src,n.idleVideo.width=t.width,n.idleVideo.height=t.height,n.idleVideo.style.position="absolute",n.idleVideo.style.top="0",n.idleVideo.style.left="0",n.idleVideo.style.width="100%",n.idleVideo.style.height="100%",n.idleVideo.style.objectFit="cover",n.idleVideo.style.maxWidth="100%",n.idleVideo.style.backgroundColor=t.backgroundColor||"#000000",n.idleVideo.style.zIndex="1",n.idleVideo.style.pointerEvents="none",n.idleVideo.style.opacity="1",n.idleVideo.muted=!0,n.idleVideo.loop=!0,n.idleVideo.controls=!1,n.idleVideo.playsInline=!0,n.idleVideo.preload="auto","static"===getComputedStyle(n.container).position&&(n.container.style.position="relative"),n.container.appendChild(n.idleVideo);var i=U(function(){return Promise.resolve(n.idleVideo.load()).then(function(){return Promise.resolve(n.idleVideo.play()).then(function(){})})},function(e){console.error("Failed to load idle video:",e)});return Promise.resolve(i&&i.then?i.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},t.setupCachedVideo=function(e){try{var t=this;return t.cachedVideo=document.createElement("video"),t.cachedVideo.src="",t.cachedVideo.width=e.width,t.cachedVideo.height=e.height,t.cachedVideo.style.position="absolute",t.cachedVideo.style.top="0",t.cachedVideo.style.left="0",t.cachedVideo.style.width="100%",t.cachedVideo.style.height="100%",t.cachedVideo.style.objectFit="cover",t.cachedVideo.style.maxWidth="100%",t.cachedVideo.style.zIndex="2",t.cachedVideo.style.pointerEvents="none",t.cachedVideo.style.opacity="0",t.cachedVideo.style.transition="opacity "+t.CROSSFADE_DURATION+"ms ease-in-out",t.cachedVideo.controls=!1,t.cachedVideo.playsInline=!0,t.cachedVideo.preload="auto",t.container.appendChild(t.cachedVideo),Promise.resolve()}catch(e){return Promise.reject(e)}},t.onSpeakingStart=function(e){this.onSpeakingStartCallback=e},t.onSpeakingEnd=function(e){this.onSpeakingEndCallback=e},t.startStreaming=function(e){void 0===e&&(e=!1),this.sessionStarted=!0,this.startBufferMonitoring(),this.videoOutput.startRenderingStreamingVideo(e)},t.stopBufferMonitoring=function(){this.bufferCheckAnimationId&&(cancelAnimationFrame(this.bufferCheckAnimationId),this.bufferCheckAnimationId=null),this.lastBufferCheckTime=0,this.sessionStarted=!1},t.getBufferLength=function(){return this.videoOutput.getBufferLength()},t.startBufferMonitoring=function(){var e=this;if(!this.bufferCheckAnimationId){this.lastBufferCheckTime=0;var t=function(n){n-e.lastBufferCheckTime>=100&&(e.sessionStarted&&e.videoOutput.getBufferLength()>0?e.hideIdleVideoBeforeStream():e.videoOutput.getStreamingStatus()||0!==e.videoOutput.getBufferLength()||e.showIdleVideoAfterStream(),e.lastBufferCheckTime=n),e.bufferCheckAnimationId=requestAnimationFrame(t)};this.bufferCheckAnimationId=requestAnimationFrame(t)}},t.toggleCacheVideoMute=function(){this.cachedVideo&&(this.cachedVideo.muted=!this.cachedVideo.muted)},t.playCachedVideo=function(e){try{var t=this;if(t.isShowingCachedVideo||!t.cachedVideo||!e||t.isTransitioning)return Promise.resolve();t.isShowingCachedVideo=!0;var n=U(function(){return t.cachedVideo.src=e,t.cachedVideo.style.opacity="0",Promise.resolve(new Promise(function(e,n){t.cachedVideo.addEventListener("loadeddata",function(){return e()},{once:!0}),t.cachedVideo.addEventListener("error",function(){return n(new Error("Video failed to load"))},{once:!0}),t.cachedVideo.load()})).then(function(){return t.crossfadeFromIdleToCached(),Promise.resolve(t.cachedVideo.play()).then(function(){t.cachedVideo.addEventListener("ended",function(){try{return Promise.resolve(t.crossfadeFromCachedToIdle()).then(function(){return Promise.resolve(t.cleanupCachedVideo()).then(function(){return Promise.resolve(t.showIdleVideo()).then(function(){})})})}catch(e){return Promise.reject(e)}},{once:!0})})})},function(e){return console.error("Failed to play cached video:",e),t.cachedVideo.style.opacity="0",t.isShowingCachedVideo=!1,Promise.resolve(t.showIdleVideo()).then(function(){})});return Promise.resolve(n&&n.then?n.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},t.crossfadeFromIdleToCached=function(){try{var e=this;return e.idleVideo&&e.cachedVideo&&!e.isTransitioning?(e.isTransitioning=!0,e.idleVideo.style.opacity="1",e.cachedVideo.style.opacity="1",Promise.resolve(new Promise(function(t){return setTimeout(t,e.CROSSFADE_DURATION)})).then(function(){e.idleVideo&&(e.idleVideo.style.opacity="0"),e.isShowingIdleVideo=!1,null==e.onIdleVideoHidden||e.onIdleVideoHidden(),e.isTransitioning=!1})):Promise.resolve()}catch(e){return Promise.reject(e)}},t.crossfadeFromCachedToIdle=function(){try{var e=function(){return t.cachedVideo.style.opacity="0",Promise.resolve(new Promise(function(e){return setTimeout(e,t.CROSSFADE_DURATION)})).then(function(){t.isShowingIdleVideo=!0,null==t.onSpeakingEndCallback||t.onSpeakingEndCallback(),null==t.onIdleVideoShown||t.onIdleVideoShown(),t.isTransitioning=!1})},t=this;if(!t.idleVideo||!t.cachedVideo||t.isTransitioning)return Promise.resolve();t.isTransitioning=!0,t.idleVideo.style.opacity="1";var n=function(){if(t.idleVideo.paused){var e=U(function(){return Promise.resolve(t.idleVideo.play()).then(function(){})},function(e){console.error("Failed to play idle video during crossfade:",e)});if(e&&e.then)return e.then(function(){})}}();return Promise.resolve(n&&n.then?n.then(e):e())}catch(e){return Promise.reject(e)}},t.cleanupCachedVideo=function(){try{var e=this;return e.cachedVideo&&(e.cachedVideo.pause(),e.cachedVideo.currentTime=0,e.cachedVideo.src="",e.cachedVideo.style.opacity="0"),e.isShowingCachedVideo=!1,Promise.resolve()}catch(e){return Promise.reject(e)}},t.hideIdleVideo=function(){var e,t;this.idleVideo&&this.isShowingIdleVideo&&(null==(e=this.onSpeakingStartCallback)||e.call(this),this.isShowingIdleVideo=!1,this.idleVideo.style.opacity="0",null==(t=this.onIdleVideoHidden)||t.call(this))},t.hideIdleVideoBeforeStream=function(){var e,t;this.idleVideo&&this.isShowingIdleVideo&&(null==(e=this.onSpeakingStartCallback)||e.call(this),this.idleVideo.style.transition="opacity "+this.CROSSFADE_DURATION+"ms ease-in-out",this.isShowingIdleVideo=!1,this.idleVideo.style.opacity="0",null==(t=this.onIdleVideoHidden)||t.call(this))},t.setEventCallbacks=function(e){this.onIdleVideoShown=e.onIdleVideoShown,this.onIdleVideoHidden=e.onIdleVideoHidden},t.getStreamingStatus=function(){return this.videoOutput.getStreamingStatus()},t.showIdleVideo=function(){try{var e=this;return!e.idleVideo||e.isShowingIdleVideo?Promise.resolve():(null==e.onSpeakingEndCallback||e.onSpeakingEndCallback(),null==e.onIdleVideoShown||e.onIdleVideoShown(),e.isShowingIdleVideo=!0,e.idleVideo.style.opacity="1",Promise.resolve(U(function(){var t=function(){if(e.idleVideo.paused)return Promise.resolve(e.idleVideo.play()).then(function(){})}();if(t&&t.then)return t.then(function(){})},function(e){console.error("failed to play idle video:",e)})))}catch(e){return Promise.reject(e)}},t.showIdleVideoAfterStream=function(){try{var e=this;return!e.idleVideo||e.isShowingIdleVideo?Promise.resolve():(null==e.onSpeakingEndCallback||e.onSpeakingEndCallback(),null==e.onIdleVideoShown||e.onIdleVideoShown(),e.isShowingIdleVideo=!0,e.idleVideo.style.opacity="1",Promise.resolve(U(function(){var t=function(){if(e.idleVideo.paused)return Promise.resolve(e.idleVideo.play()).then(function(){})}();if(t&&t.then)return t.then(function(){})},function(e){console.error("failed to play idle video:",e)})))}catch(e){return Promise.reject(e)}},t.addFrame=function(e,t,n){try{var i=function(e){var i=o?new Uint8Array(e):e;return r.videoOutput.addFrame(i,t,n)},r=this,o=e instanceof Blob;return Promise.resolve(o?Promise.resolve(e.arrayBuffer()).then(i):i(new Uint8Array(e)))}catch(e){return Promise.reject(e)}},t.clearFrame=function(){return this.showIdleVideo(),this.videoOutput.clearFrame()},t.toggleStream=function(e){try{return Promise.resolve(this.videoOutput.toggleStream(e))}catch(e){return Promise.reject(e)}},t.interrupt=function(e){this.videoOutput.interrupt(e)},t.destroy=function(){this.bufferCheckAnimationId&&(cancelAnimationFrame(this.bufferCheckAnimationId),this.bufferCheckAnimationId=null),this.idleVideo&&(this.idleVideo.pause(),this.idleVideo.parentNode&&this.idleVideo.parentNode.removeChild(this.idleVideo),this.idleVideo=null),this.cachedVideo&&(this.cachedVideo.pause(),this.cachedVideo.parentNode&&this.cachedVideo.parentNode.removeChild(this.cachedVideo),this.cachedVideo=null),this.videoOutput.destroy()},e}(),H={vadSilenceThresholdSecs:1.5,noiseSuppression:!0,vadThreshold:.4,minSpeechDurationMs:100,minSilenceDurationMs:100},G={tolerance:40,softCorrectionThreshold:100,hardCorrectionThreshold:200,historyLength:50,maxCorrectionRate:.02,correctionCoolDown:500,correctionFadeTime:1e3,minAudioBuffer:100,maxAudioBuffer:500,minVideoBuffer:3,maxVideoBuffer:10};function q(e,t){try{var n=e()}catch(e){return t(e)}return n&&n.then?n.then(void 0,t):n}exports.Conversation=/*#__PURE__*/function(){function e(e,t,n,i,r,o,s,a,c){var u=this,d=this,h=this,l=this,p=this,y=this,S=this,T=this;this.options=void 0,this.microphoneAccess=void 0,this.connection=void 0,this.idleVideo=void 0,this.wakeLock=void 0,this.user=void 0,this.audioOutput=void 0,this.videoOutput=void 0,this.headInfo=void 0,this.status="connecting",this.volume=1,this.sessionStarted=!1,this.messageCounter=0,this.syncController=void 0,this.avController=void 0,this.monitor=null,this.microphone=null,this.videoFrameQueue=[],this.cachedResponseQueue=[],this.onOutputWorkletMessage=function(e){T.avController.handleAudioWorkletMessage(e.data)},this.handleMessage=function(e){try{return u.options.onMessage({timestamp:e.timestamp,sender:e.speaker,text:e.text,visible:e.visible}),Promise.resolve()}catch(e){return Promise.reject(e)}},this.handleStreamError=function(e){var t=e.error_type;if(t&&["resource_exhausted","deadline_exceeded","inactivity_timeout"].includes(t)){if("resource_exhausted"===t){if(!T.avController.isPlaying)return void T.options.onError({message:"The system is experiencing heavy traffic. Please try again later.",endConversation:!0,type:"toast"});T.options.onError({message:"We are experiencing heavy traffic and we can't deliver the response, try again",endConversation:!1,type:"toast"})}else if("deadline_exceeded"===t)T.options.onError({message:"We are experiencing heavy traffic and we can't deliver the response, try again",endConversation:!1,type:"toast"});else if("inactivity_timeout"===t)return void T.options.onTimeout()}else T.options.onError({message:"A connection error occurred. Please try again.",endConversation:!0,type:"toast"})},this.handleStreamingEvent=function(e){try{var t,n=function(n){if(t)return n;if("metadata"===e.type&&("start"!==e.metadata_type&&"end"!==e.metadata_type||(d.avController.isStoppingAV=!1,d.videoOutput.toggleStream("start"===e.metadata_type),"start"===e.metadata_type&&(d.videoFrameQueue=[],d.syncController.resetTiming()))),"audio_frame"===e.type){if(d.avController.isStoppingAV)return;d.handleAudioFrame(e)}if("video_frame"===e.type){if(d.avController.isStoppingAV)return;d.handleVideoFrame(e)}d.avController.playAudioVideo()};if("error"===e.type)return d.handleStreamError(e),Promise.resolve();var i=function(){if("cache"===e.type){var n,i=function(){t=1},r={id:e.session_id||Math.random().toString(36).substring(2),timestamp:new Date,isSent:!0,visible:!0,event:exports.EventType.TEXT,user_id:e.user_id,username:e.username,text:null!=(n=e.text)?n:"",speaker:"ai",session_id:e.session_id||"",suggestions:[]},o=function(){if(d.sessionStarted)return Promise.resolve(d.videoOutput.playCachedVideo(e.video_url||"")).then(function(){d.handleMessage(r)});d.cachedResponseQueue.push({video_url:e.video_url||"",textEventData:r})}();return o&&o.then?o.then(i):i()}}();return Promise.resolve(i&&i.then?i.then(n):n(i))}catch(e){return Promise.reject(e)}},this.handleVideoFrame=function(e){try{var t;return e.frame_data||h.videoFrameQueue.push({timeStamp:e.event_timestamp_ms,isKeyframe:null!=(t=e.is_keyframe)&&t}),Promise.resolve()}catch(e){return Promise.reject(e)}},this.handleBinaryData=function(e){try{if(l.videoFrameQueue.length>0){var t=l.videoFrameQueue.shift();l.updateStatus("connected"),l.videoOutput.addFrame(e.data,t.timeStamp,t.isKeyframe),l.syncController.updateVideoTime(t.timeStamp)}return Promise.resolve()}catch(e){return Promise.reject(e)}},this.handleAudioFrame=function(e){try{if(!e.frame_data)return console.warn("Audio frame data is missing in the event:",e),Promise.resolve();var t,n=function(e){for(var t=window.atob(e),n=t.length,i=new Uint8Array(n),r=0;r<n;r++)i[r]=t.charCodeAt(r);return i.buffer}(e.frame_data);return t=new Int16Array(n),p.audioOutput.gain.gain.value=p.volume,p.audioOutput.worklet.port.postMessage({type:"clearInterrupted"}),p.audioOutput.worklet.port.postMessage({type:"buffer",buffer:t,time_stamp:e.event_timestamp_ms}),p.syncController.updateAudioTime(e.event_timestamp_ms),Promise.resolve()}catch(e){return Promise.reject(e)}},this.onMessage=function(e){try{switch(e.event){case exports.EventType.JOIN:return Promise.resolve();case exports.EventType.TEXT:if(m(e)){var t=e;t.timestamp=new Date,t.isSent=!0,t.visible=!0,y.handleMessage(t)}return Promise.resolve();case exports.EventType.RESPONSE:if(v(e)){var n=e;n.timestamp=new Date,n.speaker="ai",n.isSent=!0,y.handleMessage(n)}return Promise.resolve();case exports.EventType.STREAMING:return g(e)&&y.handleStreamingEvent(e),Promise.resolve();case exports.EventType.BINARY:P(e)&&y.handleBinaryData(e);case exports.EventType.TIMEOUT_WARNING:return w(e)&&y.options.onTimeoutWarning(),Promise.resolve();case exports.EventType.TIME_OUT:return I(e)&&y.options.onTimeout(),Promise.resolve();case exports.EventType.KEEP_SESSION:return E(e)&&y.options.onKeepSession({granted:e.granted}),Promise.resolve();default:return console.warn("Unhandled event type:",e.event),Promise.resolve()}}catch(e){return Promise.reject(e)}},this.endSessionWithDetails=function(e){try{return"connected"!==S.status&&"connecting"!==S.status?Promise.resolve():(S.sessionStarted=!1,S.updateStatus("disconnecting"),Promise.resolve(S.handleEndSession()).then(function(){var t;S.updateStatus("disconnected"),null==(t=S.monitor)||t.stop(),S.options.onDisconnect(e)}))}catch(e){return Promise.reject(e)}},this.options=e,this.microphoneAccess=t,this.connection=n,this.idleVideo=i,this.wakeLock=r,this.user=o,this.audioOutput=s,this.videoOutput=a,this.headInfo=c,this.syncController=new x(G),this.avController=new f(this.syncController,this.audioOutput,this.videoOutput),this.options.onConnect({userId:n.userId,headInfo:{name:this.headInfo.name,phrases:this.headInfo.phrases,language:this.headInfo.language,avatar:this.headInfo.avatarSrc},microphoneAccess:t}),this.connection.onDisconnect(this.endSessionWithDetails),this.connection.onMessage(this.onMessage),this.videoOutput.onSpeakingStart(this.options.onSpeakingStart),this.videoOutput.onSpeakingEnd(this.options.onSpeakingEnd),this.updateStatus("connected"),this.audioOutput.worklet.port.onmessage=this.onOutputWorkletMessage,this.handleIOSSilentMode(),this.startLatencyMonitoring()}e.getFullOptions=function(e){return i({username:"anonymous",environment:"production",mode:"default",apiKey:"",microphoneProvider:"eleven_labs",microphoneOptions:{onMicrophoneError:function(){},onMicrophoneStatusChange:function(){},onMicrophoneSpeechRecognitionResult:function(){}},elevenLabsOptions:H,onStatusChange:function(){},onConnect:function(){},onDisconnect:function(){},onMessage:function(){},onMuteStatusChange:function(){},onSpeakingStart:function(){},onSpeakingEnd:function(){},onStoppingEnd:function(){},onTimeout:function(){},onTimeoutWarning:function(){},onKeepSession:function(){},onError:function(){}},e)},e.startDigitalHuman=function(t){try{var n=function(){var n=T(o);return q(function(){return Promise.resolve(B.loginUser(r,"Password1",n,s,a)).then(function(r){return m=r,Promise.resolve(V.getIdleVideo(n,s,a)).then(function(r){return Promise.resolve(m.getHeadDetails(u)).then(function(d){return Promise.resolve(r.getAvatarSrc(n,s,a)).then(function(n){d.avatarSrc=n;var p=h||d.language||navigator.language;return d.language=p,Promise.resolve(k.create({environment:o,orgId:s,headId:a,token:m.accessToken,mode:c,apiKey:u,language:p})).then(function(n){function o(){return Promise.resolve(Promise.all([l.createAudioOutput(P),W.createVideoOutput(s,{width:s.getBoundingClientRect().width,height:s.getBoundingClientRect().height,frameRate:30,backgroundColor:"transparent",format:"jpeg",idleVideo:{src:r.idleVideoSource,enabled:!0},transition:null!=f?f:exports.VideoTransitionType.NONE})])).then(function(t){return new e(i,a,v,r,S,m,y=t[0],g=t[1],d)})}v=n;var s=t.element,a=!1,c=q(function(){return Promise.resolve(navigator.mediaDevices.getUserMedia({audio:!0})).then(function(e){e.getTracks().forEach(function(e){e.stop()}),a=!0})},function(){a=!1});return c&&c.then?c.then(o):o()})})})})})},function(e){var t,n;return p({status:"disconnected"}),null==(t=v)||t.close(),Promise.resolve(null==(n=y)?void 0:n.close()).then(function(){var t;return Promise.resolve(null==(t=g)?void 0:t.destroy()).then(function(){function t(){throw e}var n=q(function(){var e;return Promise.resolve(null==(e=S)?void 0:e.release()).then(function(){S=null})},function(){});return n&&n.then?n.then(t):t()})})})},i=e.getFullOptions(t),r=i.username,o=i.environment,s=i.orgId,a=i.headId,c=i.mode,u=i.apiKey,d=i.allowWakeLock,h=i.language,f=i.fadeTransitionsType,p=i.onStatusChange;p({status:"connecting"});var m=null,v=null,g=null,y=null,P={sampleRate:16e3,format:"pcm"},S=null,w=function(){if(null==d||d){var e=q(function(){return Promise.resolve(navigator.wakeLock.request("screen")).then(function(e){S=e})},function(){});if(e&&e.then)return e.then(function(){})}}();return Promise.resolve(w&&w.then?w.then(n):n())}catch(e){return Promise.reject(e)}},e.getBackgroundVideo=function(e){try{var t=e.environment,n=e.orgId,i=e.headId,r=T(null!=t?t:"production");return Promise.resolve(V.getIdleVideo(null!=r?r:"https://chat-origin.api.unith.live",n,i)).then(function(e){return e.idleVideoSource})}catch(e){return Promise.reject(e)}};var t=e.prototype;return t.startLatencyMonitoring=function(){this.monitor=new A(this.connection,{onUpdate:function(e){}})},t.handleIOSSilentMode=function(){var e=this;/iPad|iPhone|iPod/.test(navigator.userAgent)&&this.audioOutput.context.addEventListener("statechange",function(){"suspended"===e.audioOutput.context.state&&(console.warn("Audio context suspended - likely due to iOS silent mode"),e.audioOutput.context.resume())})},t.getUserId=function(){return this.connection.userId},t.handleEndSession=function(){try{return this.connection.close(),Promise.resolve()}catch(e){return Promise.reject(e)}},t.updateStatus=function(e){e!==this.status&&(this.status=e,this.options.onStatusChange({status:e}))},t.stopCurrentResponse=function(){this.avController.toggleStoppingVideo(!0),this.videoOutput.clearFrame(),this.audioOutput.worklet.port.postMessage({type:"stopPlayback"}),this.videoOutput.getStreamingStatus()||(this.options.onStoppingEnd(),this.avController.toggleStoppingVideo(!1))},t.toggleMuteStatus=function(){try{var e=this;return e.volume=0===e.volume?1:0,e.audioOutput.toggleMute(),e.videoOutput.toggleCacheVideoMute(),e.options.onMuteStatusChange({isMuted:0===e.volume}),Promise.resolve(e.volume)}catch(e){return Promise.reject(e)}},t.startSession=function(){try{var e=function(){return Promise.resolve(t.avController.startPlayback(!0)).then(function(){function e(){function e(){return t.connection}var n=function(){var e;if("custom"!==t.options.microphoneProvider)return Promise.resolve(O.initializeMicrophone(t.options.microphoneOptions,t.options.microphoneProvider,null!=(e=t.options.elevenLabsOptions)?e:H,t.user,t.headInfo,t.microphoneAccess,t.sendMessage)).then(function(e){t.microphone=e})}();return n&&n.then?n.then(e):e()}var n=function(){if(t.cachedResponseQueue.length){var e=t.cachedResponseQueue[t.cachedResponseQueue.length-1];return Promise.resolve(new Promise(function(e){return setTimeout(e,50)})).then(function(){return Promise.resolve(t.videoOutput.playCachedVideo(e.video_url)).then(function(){t.handleMessage(e.textEventData)})})}}();return n&&n.then?n.then(e):e()})},t=this;t.sessionStarted=!0;var n=function(){if("suspended"===t.audioOutput.context.state)return Promise.resolve(t.audioOutput.context.resume()).then(function(){})}();return Promise.resolve(n&&n.then?n.then(e):e())}catch(e){return Promise.reject(e)}},t.toggleMicrophone=function(){try{var e,t=function(){if(!n.microphoneAccess&&"OFF"===n.microphone.status())throw new Error("Microphone access not granted.");return Promise.resolve(n.microphone.toggleMicrophone()).then(function(){})},n=this;if("custom"===n.options.microphoneProvider)throw new Error("Cannot toggle microphone for custom provider.");console.log("mic provider ",null==(e=n.microphone)?void 0:e.provider);var i=function(){var e;if(!n.microphone)return Promise.resolve(O.initializeMicrophone(n.options.microphoneOptions,n.options.microphoneProvider,null!=(e=n.options.elevenLabsOptions)?e:H,n.user,n.headInfo,n.microphoneAccess,n.sendMessage)).then(function(e){n.microphone=e})}();return Promise.resolve(i&&i.then?i.then(t):t())}catch(e){return Promise.reject(e)}},t.getMicrophoneStatus=function(){return this.microphone?this.microphone.status():"OFF"},t.endSession=function(){return this.endSessionWithDetails({reason:"user"})},t.sendMessage=function(e){if(!this.connection)throw new Error("Connection not established");var t=this.user.id+"::"+this.user.orgId+"::"+this.user.headId+"::"+this.user.sessionId.toString().padStart(5,"0"),n={id:this.messageCounter++,timestamp:(new Date).toISOString(),speaker:"user",text:e,isSent:!1,user_id:this.user.id,username:this.user.username,event:exports.EventType.TEXT,visible:!0,session_id:t};return this.connection.sendMessage(n)},t.keepSession=function(){if(!this.connection)throw new Error("Connection not established");var e=this.user.id+"::"+this.user.orgId+"::"+this.user.headId+"::"+this.user.sessionId.toString().padStart(5,"0"),t={id:this.messageCounter,timestamp:(new Date).toISOString(),speaker:"user",text:"",isSent:!1,user_id:this.user.id,username:this.user.username,event:exports.EventType.KEEP_SESSION,visible:!0,session_id:e};return this.connection.sendMessage(t)},e}(),exports.isBinaryEvent=P,exports.isConversationEndEvent=function(e){return e.event===exports.EventType.CONVERSATION_END},exports.isJoinEvent=p,exports.isKeepSessionEvent=E,exports.isPongEvent=S,exports.isResponseEvent=v,exports.isStreamingErrorEvent=y,exports.isStreamingEvent=g,exports.isTextEvent=m,exports.isTimeoutEvent=I,exports.isTimeoutWarningEvent=w;
|
|
1
|
+
var e=require("microsoft-cognitiveservices-speech-sdk"),t=require("@elevenlabs/client");function n(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,i=Array(t);n<t;n++)i[n]=e[n];return i}function i(){return i=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var i in n)({}).hasOwnProperty.call(n,i)&&(e[i]=n[i])}return e},i.apply(null,arguments)}function r(e,t){try{var n=e()}catch(e){return t(e)}return n&&n.then?n.then(void 0,t):n}var o,s,a=new Map,c=(o="audio-concat-processor",s='\nconst decodeTable = [0,132,396,924,1980,4092,8316,16764];\n\nexport function decodeSample(muLawSample) {\n let sign;\n let exponent;\n let mantissa;\n let sample;\n muLawSample = ~muLawSample;\n sign = (muLawSample & 0x80);\n exponent = (muLawSample >> 4) & 0x07;\n mantissa = muLawSample & 0x0F;\n sample = decodeTable[exponent] + (mantissa << (exponent+3));\n if (sign !== 0) sample = -sample;\n\n return sample;\n}\n\nclass AudioConcatProcessor extends AudioWorkletProcessor {\n constructor() {\n super();\n this.buffers = [];\n this.cursor = 0;\n this.currentBuffer = null;\n this.currentBufferTimestamp = null;\n this.currentBufferStartSample = 0;\n this.wasInterrupted = false;\n this.playing = false;\n this.finished = false;\n\n this.playbackRate = 1.0;\n this.adjustUntilSample = null;\n this.sampleCounter = 0;\n this.format = { sampleRate: 48000 };\n\n // Time synchronization properties\n this.playbackStartTime = null;\n this.totalSamplesPlayed = 0;\n \n // Drift correction parameters\n this.maxCorrectionRate = 0.1; // Maximum 10% speed adjustment\n this.correctionSmoothness = 0.95; // How smoothly to apply corrections (0-1)\n this.targetPlaybackRate = 1.0;\n\n this.port.onmessage = ({ data }) => {\n switch (data.type) {\n case "setFormat":\n this.format = data.format;\n break;\n\n case "buffer":\n this.wasInterrupted = false;\n \n const bufferData = this.format.encoding === "ulaw"\n ? new Uint8Array(data.buffer)\n : new Int16Array(data.buffer);\n \n // Store buffer with its timestamp\n this.buffers.push({\n buffer: bufferData,\n timestamp: data.timestamp_ms\n });\n break;\n \n case "startPlayback":\n this.playing = true;\n this.playbackStartTime = currentTime;\n this.totalSamplesPlayed = 0;\n this.sampleCounter = 0;\n break;\n \n case "stopPlayback":\n this.playing = false;\n this.finished = true;\n \n // Discard all queued buffers\n this.buffers = [];\n \n // Clear current buffer and reset cursor\n this.currentBuffer = null;\n this.currentBufferTimestamp = null;\n this.cursor = 0;\n \n // Reset playback rate adjustments\n this.playbackRate = 1.0;\n this.adjustUntilSample = null;\n this.targetPlaybackRate = 1.0;\n \n // Notify that playback has stopped\n this.port.postMessage({ \n type: "process", \n finished: true,\n stopped: true\n });\n break;\n\n case "adjustPlaybackRate":\n this.playbackRate = data.rate;\n this.adjustUntilSample = this.sampleCounter + Math.floor(this.format.sampleRate * data.duration);\n break;\n \n case "reset":\n this.playing = false;\n this.finished = false;\n this.currentBuffer = null;\n this.currentBufferTimestamp = null;\n this.currentBufferStartSample = 0;\n this.cursor = 0;\n this.buffers = [];\n this.playbackStartTime = null;\n this.totalSamplesPlayed = 0;\n this.targetPlaybackRate = 1.0;\n break;\n\n case "interrupt":\n this.wasInterrupted = true;\n break;\n\n case "clearInterrupted":\n if (this.wasInterrupted) {\n this.wasInterrupted = false;\n this.buffers = [];\n this.currentBuffer = null;\n this.currentBufferTimestamp = null;\n }\n break;\n }\n };\n }\n\n // Calculate the expected timestamp based on samples played\n getExpectedTimestamp() {\n if (!this.playbackStartTime) return 0;\n \n // Convert samples to milliseconds\n const samplesPlayedMs = (this.totalSamplesPlayed / this.format.sampleRate) * 1000;\n return samplesPlayedMs;\n }\n\n // Calculate the actual timestamp of current playback position\n getCurrentActualTimestamp() {\n if (!this.currentBufferTimestamp) return 0;\n \n // Current position within the buffer in samples\n const samplesIntoBuffer = Math.floor(this.cursor);\n \n // Convert buffer position to milliseconds (assuming buffer covers specific time duration)\n // Each buffer typically represents a fixed time duration\n const bufferDurationMs = (this.currentBuffer.length / this.format.sampleRate) * 1000;\n const progressThroughBuffer = samplesIntoBuffer / this.currentBuffer.length;\n \n return this.currentBufferTimestamp + (progressThroughBuffer * bufferDurationMs);\n }\n\n // Calculate timing drift and adjust playback rate accordingly\n calculateDriftCorrection() {\n if (!this.currentBufferTimestamp || !this.playbackStartTime) {\n return 1.0;\n }\n\n const expectedTimestamp = this.getExpectedTimestamp();\n const actualTimestamp = this.getCurrentActualTimestamp();\n \n // Calculate drift in milliseconds\n const drift = actualTimestamp - expectedTimestamp;\n \n // Convert drift to a playback rate adjustment\n // Positive drift means we\'re ahead - slow down\n // Negative drift means we\'re behind - speed up\n let correctionFactor = 1.0;\n \n if (Math.abs(drift) > 10) { // Only correct if drift > 10ms\n // Calculate correction rate based on drift\n // More drift = more correction, but capped at maxCorrectionRate\n const driftRatio = Math.min(Math.abs(drift) / 1000, this.maxCorrectionRate);\n \n if (drift > 0) {\n // We\'re ahead, slow down\n correctionFactor = 1.0 - driftRatio;\n } else {\n // We\'re behind, speed up\n correctionFactor = 1.0 + driftRatio;\n }\n \n // Apply smoothing to avoid jarring rate changes\n this.targetPlaybackRate = this.targetPlaybackRate * this.correctionSmoothness + \n correctionFactor * (1 - this.correctionSmoothness);\n } else {\n // Gradually return to normal speed when drift is small\n this.targetPlaybackRate = this.targetPlaybackRate * this.correctionSmoothness + \n 1.0 * (1 - this.correctionSmoothness);\n }\n\n return this.targetPlaybackRate;\n }\n\n process(_, outputs, parameters) {\n const output = outputs[0][0];\n\n if (!this.playing) {\n output.fill(0);\n return true;\n }\n\n let finished = false;\n\n for (let i = 0; i < output.length; i++) {\n // If no buffer is ready, get the next one\n if (!this.currentBuffer) {\n if (this.buffers.length === 0) {\n finished = true;\n break;\n }\n \n const bufferData = this.buffers.shift();\n this.currentBuffer = bufferData.buffer;\n this.currentBufferTimestamp = bufferData.timestamp;\n this.currentBufferStartSample = this.totalSamplesPlayed;\n this.cursor = 0;\n }\n\n // Calculate drift correction for timing synchronization\n const driftCorrectedRate = this.calculateDriftCorrection();\n \n // Apply manual playback rate adjustments if active\n let finalPlaybackRate = driftCorrectedRate;\n if (this.adjustUntilSample !== null && this.sampleCounter < this.adjustUntilSample) {\n finalPlaybackRate *= this.playbackRate;\n } else if (this.adjustUntilSample !== null) {\n this.playbackRate = 1.0;\n this.adjustUntilSample = null;\n }\n\n const idx = Math.floor(this.cursor);\n const nextIdx = Math.min(idx + 1, this.currentBuffer.length - 1);\n\n let s1 = this.currentBuffer[idx];\n let s2 = this.currentBuffer[nextIdx];\n if (this.format.encoding === "ulaw") {\n s1 = decodeSample(s1);\n s2 = decodeSample(s2);\n }\n\n const frac = this.cursor - idx;\n const interpolated = s1 * (1 - frac) + s2 * frac;\n output[i] = interpolated / 32768;\n\n this.cursor += finalPlaybackRate;\n this.sampleCounter++;\n this.totalSamplesPlayed++;\n\n if (this.cursor >= this.currentBuffer.length) {\n this.currentBuffer = null;\n this.currentBufferTimestamp = null;\n }\n }\n\n if (this.finished !== finished) {\n this.finished = finished;\n this.port.postMessage({ \n type: "process", \n finished,\n // Optional: send timing info for debugging\n timing: {\n expectedTimestamp: this.getExpectedTimestamp(),\n actualTimestamp: this.getCurrentActualTimestamp(),\n playbackRate: this.targetPlaybackRate\n }\n });\n }\n\n return true;\n }\n}\nregisterProcessor("audio-concat-processor", AudioConcatProcessor);\n',function(e){try{var t,n=function(n){return t?n:r(function(){var t="data:application/javascript;base64,"+btoa(s);return Promise.resolve(e.addModule(t)).then(function(){a.set(o,t)})},function(){throw new Error("Failed to load the "+o+" worklet module. Make sure the browser supports AudioWorklets.")})},i=a.get(o);if(i)return Promise.resolve(e.addModule(i));var c=new Blob([s],{type:"application/javascript"}),u=URL.createObjectURL(c),d=r(function(){return Promise.resolve(e.addModule(u)).then(function(){a.set(o,u),t=1})},function(){URL.revokeObjectURL(u)});return Promise.resolve(d&&d.then?d.then(n):n(d))}catch(e){return Promise.reject(e)}});function u(e,t){try{var n=e()}catch(e){return t(e)}return n&&n.then?n.then(void 0,t):n}var d,h,l=/*#__PURE__*/function(){function e(e,t,n,i){this.context=void 0,this.analyser=void 0,this.gain=void 0,this.worklet=void 0,this._isMuted=!1,this.context=e,this.analyser=t,this.gain=n,this.worklet=i}e.createAudioOutput=function(t){var n=t.sampleRate,i=t.format;try{var r=null;return Promise.resolve(u(function(){var t=(r=new AudioContext({sampleRate:n})).createAnalyser(),o=r.createGain();return o.connect(t),t.connect(r.destination),Promise.resolve(c(r.audioWorklet)).then(function(){var n=new AudioWorkletNode(r,"audio-concat-processor");n.port.postMessage({type:"setFormat",format:i}),n.connect(o);var s=new e(r,t,o,n);return Promise.resolve(s.ensureIOSCompatibility()).then(function(){return s})})},function(e){var t;throw null==(t=r)||t.close(),e}))}catch(e){return Promise.reject(e)}};var t=e.prototype;return t.ensureIOSCompatibility=function(){try{var e=this;if(/iPad|iPhone|iPod/.test(navigator.userAgent)){var t=e.context.createBuffer(1,1,e.context.sampleRate),n=e.context.createBufferSource();n.buffer=t,n.connect(e.context.destination),n.start()}return Promise.resolve()}catch(e){return Promise.reject(e)}},t.getOutputDevice=function(){try{var e=this;return Promise.resolve(u(function(){var t=e.context.sinkId||"";return Promise.resolve(navigator.mediaDevices.enumerateDevices()).then(function(e){var n=e.filter(function(e){return"audiooutput"===e.kind});return""===t?n.find(function(e){return"default"===e.deviceId})||n[0]||null:n.find(function(e){return e.deviceId===t})||null})},function(e){return console.error("Error getting output device:",e),null}))}catch(e){return Promise.reject(e)}},t.getAvailableOutputDevices=function(){try{return Promise.resolve(u(function(){return Promise.resolve(navigator.mediaDevices.enumerateDevices()).then(function(e){return e.filter(function(e){return"audiooutput"===e.kind})})},function(e){return console.error("Error enumerating devices:",e),[]}))}catch(e){return Promise.reject(e)}},t.mute=function(){this.gain.gain.value=0,this._isMuted=!0},t.unmute=function(){this.gain.gain.value=1,this._isMuted=!1},t.toggleMute=function(){return this._isMuted?this.unmute():this.mute(),this._isMuted},t.close=function(){try{return Promise.resolve(this.context.close()).then(function(){})}catch(e){return Promise.reject(e)}},e}(),f=/*#__PURE__*/function(){function e(e,t,n){var i=this;this.syncController=void 0,this.audioOutput=void 0,this.videoOutput=void 0,this.initialized=!1,this.isPlaying=!1,this.isPlayingAudio=!1,this.isPlayingVideo=!1,this.isStoppingAV=!1,this.handleAudioWorkletMessage=function(e){"process"===e.type&&(i.isPlayingAudio=!1,i.isPlayingVideo||(i.isPlaying=!1),i.audioOutput.worklet.port.postMessage({type:"reset"}))},this.syncController=e,this.audioOutput=t,this.videoOutput=n,this.videoOutput.setEventCallbacks({onIdleVideoShown:function(){},onIdleVideoHidden:function(){i.isPlayingVideo=!1,i.isPlayingAudio||(i.isPlaying=!1),i.videoOutput.stopBufferMonitoring()}})}var t=e.prototype;return t.updatePlayingState=function(e){this.isPlaying=e,this.isPlayingAudio=e,this.isPlayingVideo=e},t.toggleStoppingVideo=function(e){this.isStoppingAV=e},t.startPlayback=function(e){void 0===e&&(e=!1);try{var t=this;return t.isPlaying?Promise.resolve():(e&&(t.initialized=e),Promise.resolve(t.audioOutput.context.resume()).then(function(){t.updatePlayingState(!0),t.videoOutput.startStreaming(e),t.audioOutput.worklet.port.postMessage({type:"startPlayback"})}))}catch(e){return Promise.reject(e)}},t.playAudioVideo=function(){try{var e=this;if(!e.initialized)return Promise.resolve();if(e.isPlaying||e.isStoppingAV)return Promise.resolve();var t=e.videoOutput.getBufferLength();return e.startPlayback(!0),t>=6&&e.startPlayback(),Promise.resolve()}catch(e){return Promise.reject(e)}},e}();function p(e){return e.event===exports.EventType.JOIN}function m(e){return e.event===exports.EventType.TEXT}function v(e){return e.event===exports.EventType.RESPONSE}function g(e){return e.event===exports.EventType.STREAMING}function y(e){return e.event===exports.EventType.STREAMING&&e.type===exports.StreamingEventType.ERROR}function P(e){return e.event===exports.EventType.BINARY}function S(e){return e.type===exports.EventType.PONG}function I(e){return e.event===exports.EventType.TIMEOUT_WARNING}function w(e){return e.event===exports.EventType.TIME_OUT}function E(e){return e.event===exports.EventType.KEEP_SESSION}function T(e){switch(e){case"production":default:return"https://chat-api.unith.ai";case"staging":return"https://chat-api.stg.unith.live";case"development":return"https://chat-api.dev.unith.live"}}function b(e){return!!e.event}exports.EventType=void 0,(d=exports.EventType||(exports.EventType={})).TEXT="text",d.CONVERSATION_END="conversation_end",d.JOIN="join",d.ERROR="error",d.TIME_OUT="timeout",d.UNITH_NLP_EXCEPTION="unith_nlp_exception",d.ANALYTICS="analytics-userFeedback",d.CHOICE="choice",d.TIMEOUT_WARNING="timeout_warning",d.KEEP_SESSION="keep_session",d.RESPONSE="response",d.STREAMING="streaming",d.PING="ping",d.PONG="pong",d.BINARY="binary",exports.StreamingEventType=void 0,(h=exports.StreamingEventType||(exports.StreamingEventType={})).VIDEO_FRAME="video_frame",h.AUDIO_FRAME="audio_frame",h.METADATA="metadata",h.ERROR="error",h.CACHE="cache";var k=/*#__PURE__*/function(){function e(e,t){var n=this;this.socket=void 0,this.userId=void 0,this.queue=[],this.disconnectionDetails=null,this.onDisconnectCallback=null,this.onMessageCallback=null,this.onPingPongCallback=null,this.socket=e,this.userId=t,this.socket.addEventListener("error",function(e){n.disconnect({reason:"error",message:"The connection was closed due to a websocket error.",context:e})}),this.socket.addEventListener("close",function(e){n.disconnect({reason:"error",message:e.reason||"The connection was closed by the server.",context:e})}),this.socket.addEventListener("message",function(e){try{var t;if(S(t=e.data instanceof Blob||e.data instanceof ArrayBuffer?{event:exports.EventType.BINARY,user_id:"",username:"",data:e.data}:JSON.parse(e.data))){if(!n.onPingPongCallback)return;return void n.onPingPongCallback(t)}if(!b(t))return;n.onMessageCallback?n.onMessageCallback(t):n.queue.push(t)}catch(e){}})}e.create=function(t){try{var n=null,i=function(e){switch(e){case"production":default:return"wss://stream-api.unith.ai/stream-hub";case"staging":return"wss://stream-api.stg.unith.live/stream-hub";case"development":return"wss://stream-api.dev.unith.live/stream-hub"}}(t.environment);return Promise.resolve(function(r,o){try{var s=(n=new WebSocket(i+"/"+t.orgId+"/"+t.headId),Promise.resolve(new Promise(function(e,i){n.addEventListener("open",function(){n.send(JSON.stringify({token:t.token,api_key:t.apiKey,text_only:!0,format:"vp8",quality:"standard",crop:!1}))},{once:!0}),n.addEventListener("close",function(e){i()}),n.addEventListener("error",i),n.addEventListener("message",function(n){var r=JSON.parse(n.data);b(r)&&(p(r)?r.granted&&(null==t.onJoin||t.onJoin(),e(r.user_id)):y(r)&&i({type:"connection",message:"We are currently at full capacity. Please try again later."}))},{once:!0})})).then(function(t){return new e(n,t)}))}catch(e){return o(e)}return s&&s.then?s.then(void 0,o):s}(0,function(e){var t;throw null==(t=n)||t.close(),e}))}catch(e){return Promise.reject(e)}};var t=e.prototype;return t.disconnect=function(e){var t;this.disconnectionDetails||(this.disconnectionDetails=e,null==(t=this.onDisconnectCallback)||t.call(this,e))},t.close=function(){this.socket.close()},t.sendMessage=function(e){this.socket.send(JSON.stringify(e))},t.sendPingEvent=function(e){this.onPingPongCallback?this.socket.send(JSON.stringify(e)):console.warn("Ping event sent without a callback set.")},t.onPingPong=function(e){this.onPingPongCallback=e},t.onMessage=function(e){this.onMessageCallback=e;var t=this.queue;this.queue=[],t.length>0&&queueMicrotask(function(){t.forEach(e)})},t.onDisconnect=function(e){this.onDisconnectCallback=e;var t=this.disconnectionDetails;t&&queueMicrotask(function(){e(t)})},e}(),V=/*#__PURE__*/function(){function e(e,t){this.idleVideoSource=void 0,this.videoId=void 0,this.idleVideoSource=e,this.videoId=t}return e.getIdleVideo=function(t,n,i){try{return Promise.resolve(e.getIdleVideoId(t,n,i)).then(function(r){return Promise.resolve(fetch(t+"/api/v1/idle/"+n+"/"+i+"/"+r)).then(function(t){return t.status>=200&&t.status<=299?Promise.resolve(t.json()).then(function(t){if(t)return new e(t,r);throw new Error("No idle video found for the specified orgId and headId.")}):Promise.resolve(t.text()).then(function(e){var n=new Error("An error occurred retrieving idle video: "+t.status+" "+t.statusText+". Response: "+e);throw n.name="IdleVideoError",n})})})}catch(e){return Promise.reject(e)}},e.prototype.getAvatarSrc=function(e,t,n){try{return Promise.resolve(fetch(e+"/api/v1/avatar/"+t+"/"+n+"/"+this.videoId)).then(function(e){return e.status>=200&&e.status<=299?Promise.resolve(e.json()).then(function(e){if(e)return e;throw new Error("No avatar image found for the specified orgId and headId.")}):Promise.resolve(e.text()).then(function(t){var n=new Error("An error occurred retrieving avatar: "+e.status+" "+e.statusText+". Response: "+t);throw n.name="AvatarError",n})})}catch(e){return Promise.reject(e)}},e.getIdleVideoId=function(e,t,n){try{return Promise.resolve(fetch(e+"/api/v1/videos/"+t+"/"+n)).then(function(e){return e.status>=200&&e.status<=299?Promise.resolve(e.json()).then(function(e){if(e.length>0)return e[0].id;throw new Error("No idle video found for the specified orgId and headId.")}):Promise.resolve(e.text()).then(function(t){var n=new Error("An error occurred retrieving idle video: "+e.status+" "+e.statusText+". Response: "+t);throw n.name="IdleVideoError",n})})}catch(e){return Promise.reject(e)}},e}();function C(e,t){try{var n=e()}catch(e){return t(e)}return n&&n.then?n.then(void 0,t):n}var O=/*#__PURE__*/function(){function n(e,t,n,i,r,o,s,a){this.provider=void 0,this.options=void 0,this.elevenLabsOptions=void 0,this.user=void 0,this.headInfo=void 0,this.sendMessage=void 0,this.microphoneStatus="OFF",this.microphoneAccess=!1,this.recognizer=null,this.connection=null,this.tokenObj={token:"",generatedAt:null,region:""},this.micBeep=new Audio("https://embedded.unith.ai/assets/beep_up.wav"),this.provider=e,this.options=t,this.elevenLabsOptions=n,this.user=i,this.headInfo=r,this.sendMessage=a,this.microphoneAccess=o,this.tokenObj=s,this.microphoneAccess||this.options.onMicrophoneError({message:"Microphone access not granted."})}n.initializeMicrophone=function(e,t,i,r,o,s,a){try{var c={token:"",generatedAt:null,region:""};return Promise.resolve(r.getAsrToken("eleven_labs"===t?"elevenlabs":"azure")).then(function(u){return c={token:u.token,generatedAt:Date.now(),region:u.region},new n(t,e,i,r,o,s,c,a)})}catch(e){return Promise.reject(e)}};var i=n.prototype;return i.updateMicrophoneStatus=function(e){this.microphoneStatus=e,this.options.onMicrophoneStatusChange({status:e})},i.handleRecognitionResult=function(e){e.length<2||this.options.onMicrophoneSpeechRecognitionResult({transcript:e})},i.startAzureMicrophone=function(){try{var t=function(t){var i=e.SpeechConfig.fromAuthorizationToken(n.tokenObj.token,n.tokenObj.region);i.speechRecognitionLanguage=n.headInfo.lang_speech_recognition||"en-US";var r=e.AudioConfig.fromDefaultMicrophoneInput();n.recognizer=new e.SpeechRecognizer(i,r),n.headInfo.phrases.length>0&&e.PhraseListGrammar.fromRecognizer(n.recognizer).addPhrases(n.headInfo.phrases),n.recognizer.recognized=function(t,i){i.result.reason===e.ResultReason.RecognizedSpeech?n.handleRecognitionResult(i.result.text):i.result.reason===e.ResultReason.NoMatch&&n.options.onMicrophoneError({message:"Speech could not be recognized - No clear speech detected"})},n.recognizer.startContinuousRecognitionAsync(function(){n.micBeep.play(),n.updateMicrophoneStatus("ON")},function(){console.error("Error starting recognizer")}),n.tokenObj.token=""},n=this,i=function(){if(0===n.tokenObj.token.length)return Promise.resolve(n.user.getAsrToken("azure")).then(function(e){if(!e.region||!e.token)throw new Error("Failed to initialize Azure microphone.");n.tokenObj={token:e.token,region:e.region,generatedAt:Date.now()}})}();return Promise.resolve(i&&i.then?i.then(t):t())}catch(e){return Promise.reject(e)}},i.stopAzureMicrophone=function(){try{var e=this;return e.recognizer&&e.recognizer.stopContinuousRecognitionAsync(function(){e.updateMicrophoneStatus("OFF")},function(t){e.options.onMicrophoneError({message:"Error stopping microphone : "+t})}),Promise.resolve()}catch(e){return Promise.reject(e)}},i.startElevenLabsMicrophone=function(){try{var e=function(e){n.connection=t.Scribe.connect({token:n.tokenObj.token,modelId:"scribe_v2_realtime",includeTimestamps:!1,microphone:{echoCancellation:!0,noiseSuppression:n.elevenLabsOptions.noiseSuppression},commitStrategy:t.CommitStrategy.VAD,vadSilenceThresholdSecs:n.elevenLabsOptions.vadSilenceThresholdSecs,vadThreshold:n.elevenLabsOptions.vadThreshold,minSpeechDurationMs:n.elevenLabsOptions.minSpeechDurationMs,minSilenceDurationMs:n.elevenLabsOptions.minSilenceDurationMs}),n.connection.on(t.RealtimeEvents.SESSION_STARTED,function(){n.micBeep.play(),n.updateMicrophoneStatus("ON")}),n.connection.on(t.RealtimeEvents.COMMITTED_TRANSCRIPT,function(e){e.text.length<2||"("===e.text[0]||n.handleRecognitionResult(e.text)}),n.connection.on(t.RealtimeEvents.ERROR,function(e){console.error("Error:",e),n.options.onMicrophoneError({message:"Error recognizing speech"})}),n.tokenObj.token=""},n=this,i=function(){if(0===n.tokenObj.token.length)return Promise.resolve(n.user.getAsrToken("elevenlabs")).then(function(e){if(!e.token)throw new Error("Failed to initialize Eleven Labs microphone.");n.tokenObj={token:e.token,region:"",generatedAt:Date.now()}})}();return Promise.resolve(i&&i.then?i.then(e):e())}catch(e){return Promise.reject(e)}},i.stopElevenLabsMicrophone=function(){try{var e,t=this;return Promise.resolve(C(function(){function n(n){if(e)return n;t.connection=null}var i=function(){if(t.connection)return Promise.resolve(t.connection.close()).then(function(){t.updateMicrophoneStatus("OFF"),e=1})}();return i&&i.then?i.then(n):n(i)},function(){t.updateMicrophoneStatus("OFF")}))}catch(e){return Promise.reject(e)}},i.toggleMicrophone=function(){try{var e=this;return Promise.resolve(C(function(){var t=function(){if("ON"===e.microphoneStatus){e.updateMicrophoneStatus("PROCESSING");var t=function(){if("azure"===e.provider)return Promise.resolve(e.stopAzureMicrophone()).then(function(){});var t=function(){if("eleven_labs"===e.provider)return Promise.resolve(e.stopElevenLabsMicrophone()).then(function(){})}();return t&&t.then?t.then(function(){}):void 0}();if(t&&t.then)return t.then(function(){})}else{var n=function(){if("OFF"===e.microphoneStatus){e.updateMicrophoneStatus("PROCESSING");var t=function(){if("azure"===e.provider)return Promise.resolve(e.startAzureMicrophone()).then(function(){});var t=function(){if("eleven_labs"===e.provider)return Promise.resolve(e.startElevenLabsMicrophone()).then(function(){})}();return t&&t.then?t.then(function(){}):void 0}();if(t&&t.then)return t.then(function(){})}else console.error("Microphone is currently processing. Please wait.")}();if(n&&n.then)return n.then(function(){})}}();if(t&&t.then)return t.then(function(){})},function(t){e.updateMicrophoneStatus("OFF"),e.options.onMicrophoneError({message:t.message||"An unknown error occurred."})}))}catch(e){return Promise.reject(e)}},i.status=function(){return this.microphoneStatus},n}(),A=/*#__PURE__*/function(){function e(e,t){this.connection=void 0,this.intervalId=null,this.pingInterval=void 0,this.timeout=void 0,this.lastPingTimestamp="0",this.history=[],this.maxHistory=void 0,this.onUpdate=void 0,this.connection=e,this.pingInterval=(null==t?void 0:t.pingInterval)||4e3,this.timeout=(null==t?void 0:t.timeout)||2e3,this.maxHistory=(null==t?void 0:t.maxHistory)||3,this.onUpdate=null==t?void 0:t.onUpdate,this.handleMessage=this.handleMessage.bind(this),this.connection.onPingPong(this.handleMessage)}var t=e.prototype;return t.start=function(){var e=this;this.intervalId||(this.intervalId=setInterval(function(){e.sendPing()},this.pingInterval))},t.stop=function(){clearInterval(this.intervalId),this.intervalId=null},t.destroy=function(){this.stop()},t.sendPing=function(){var e=performance.now().toString();this.lastPingTimestamp=e;var t={event:exports.EventType.PING,timestamp:this.lastPingTimestamp,id:"0"};this.connection.sendPingEvent(t)},t.handleMessage=function(e){try{if(e.timestamp===this.lastPingTimestamp){var t=performance.now()-parseFloat(e.timestamp);this.recordLatency(t)}}catch(e){}},t.recordLatency=function(e){this.history.push(e),this.history.length>this.maxHistory&&this.history.shift();var t=this.history.reduce(function(e,t){return e+t},0)/this.history.length,n=this.classifyLatency(t);this.onUpdate&&this.onUpdate({rtt:e,average:t,status:n})},t.classifyLatency=function(e){return e<100?"good":e<250?"moderate":"poor"},e}(),x=/*#__PURE__*/function(){function e(e){this.config=void 0,this.driftHistory=[],this.correctionInProgress=!1,this.lastAudioTiming=null,this.lastVideoTiming=null,this.config=e}var t=e.prototype;return t.updateAudioTime=function(e){this.lastAudioTiming={timestamp:performance.now(),relativeTime:e}},t.updateVideoTime=function(e){this.lastVideoTiming={timestamp:performance.now(),relativeTime:e}},t.resetTiming=function(){this.lastAudioTiming=null,this.lastVideoTiming=null},t.checkSync=function(){try{var e=this;if(e.correctionInProgress)return console.warn("Sync correction already in progress"),Promise.resolve();if(!e.lastAudioTiming||!e.lastVideoTiming)return console.warn("Insufficient timing data for sync check"),Promise.resolve();var t=Math.abs(e.lastAudioTiming.relativeTime-e.lastVideoTiming.relativeTime);return e.recordDrift(t),t>e.config.tolerance?console.log("Drift detected: "+t+"ms"):console.log("No significant drift detected"),Promise.resolve()}catch(e){return Promise.reject(e)}},t.recordDrift=function(e){this.driftHistory.push({timestamp:Date.now(),drift:e,audioTime:this.lastAudioTiming.relativeTime,videoTime:this.lastVideoTiming.relativeTime}),this.driftHistory.length>this.config.historyLength&&this.driftHistory.shift()},e}(),R=window.localStorage,M=window.location.origin+window.location.pathname,D=function(e,t,n){if(void 0!==R)return R.getItem("chat:"+M+":"+t+":"+n+":"+e)},_=function(e,t,n,i){void 0===R||R.setItem("chat:"+M+":"+n+":"+i+":"+e,t)};function F(e,t){try{var n=e()}catch(e){return t(e)}return n&&n.then?n.then(void 0,t):n}var j,N,B=/*#__PURE__*/function(){function e(e,t,n,i,r,o,s,a){this.id=void 0,this.username=void 0,this.password=void 0,this.orgId=void 0,this.headId=void 0,this.apiBase=void 0,this.EXPIRATION_OFFSET=9e5,this.accessToken="",this.tokenType="",this.sessionId=0,this.id=n,this.username=i,this.password=r,this.orgId=o,this.headId=s,this.apiBase=a,this.accessToken=e,this.tokenType=t;var c=D("session_id",o,s);c&&(this.sessionId=parseInt(c),this.sessionId+=1),_("session_id",this.sessionId.toString(),o,s)}e.loginUser=function(t,n,i,r,o){try{var s=new FormData;return s.append("username",t),s.append("password",n),Promise.resolve(F(function(){return Promise.resolve(fetch(i+"/token",{method:"POST",body:s})).then(function(s){return s.status>=200&&s.status<=299?Promise.resolve(s.json()).then(function(s){return new e(s.access_token,s.token_type,s.user_id,t,n,r,o,i)}):Promise.resolve(s.text()).then(function(e){var t=new Error("An error occurred: "+s.status+" "+s.statusText+". Response: "+e);throw t.response={status:s.status,status_code:s.status,data:JSON.parse(e)},t})})},function(e){var t,n;if(null!=e&&null!=(t=e.response)&&null!=(t=t.data)&&t.detail)throw new Error(null==e||null==(n=e.response)||null==(n=n.data)?void 0:n.detail);throw new Error(JSON.stringify(e))}))}catch(e){return Promise.reject(e)}};var t=e.prototype;return t.getHeadDetails=function(e){try{var t=this,n=t.apiBase+"/api/v1/head/"+t.orgId+"/"+t.headId+"/"+e;return Promise.resolve(F(function(){return Promise.resolve(fetch(n)).then(function(e){return e.status>=200&&e.status<=299?Promise.resolve(e.json()):Promise.resolve(e.text()).then(function(t){var n=new Error("An error occurred: "+e.status+" "+e.statusText+". Response: "+t);throw n.response={status:e.status,status_code:e.status,data:JSON.parse(t)},n})})},function(e){var t,n;if(null!=e&&null!=(t=e.response)&&null!=(t=t.data)&&t.detail)throw new Error(null==e||null==(n=e.response)||null==(n=n.data)?void 0:n.detail);throw new Error(JSON.stringify(e))}))}catch(e){return Promise.reject(e)}},t.getAuthToken=function(e,t){try{var n,i=this.apiBase+"/token",r=new FormData;r.append("username",e),r.append("password",t);var o=F(function(){return Promise.resolve(fetch(i,{method:"POST",body:r})).then(function(e){return Promise.resolve(e.json()).then(function(e){n=e})})},function(e){var t,n;if(null!=e&&null!=(t=e.response)&&null!=(t=t.data)&&t.detail)throw new Error(null==e||null==(n=e.response)||null==(n=n.data)?void 0:n.detail);throw new Error(JSON.stringify(e))});return Promise.resolve(o&&o.then?o.then(function(e){return n}):n)}catch(e){return Promise.reject(e)}},t.getProviderToken=function(e,t){try{var n,i,r=function(e){return{token:n,region:i}},o=this.apiBase+"/api/v1/asr_token?provider="+t,s=F(function(){return Promise.resolve(fetch(o,{method:"GET",headers:{Authorization:"Bearer "+e}})).then(function(e){return Promise.resolve(e.json()).then(function(e){n=e.token,i=e.region})})},function(e){var t,n;if(null!=e&&null!=(t=e.response)&&null!=(t=t.data)&&t.detail)throw new Error(null==e||null==(n=e.response)||null==(n=n.data)?void 0:n.detail);throw new Error(JSON.stringify(e))});return Promise.resolve(s&&s.then?s.then(r):r())}catch(e){return Promise.reject(e)}},t.getAccessToken=function(){try{var e=function(e){var o=JSON.parse(window.atob(n.split(".")[1]));return t.username=o.username,{access_token:n,user_id:i,session_id:r}},t=this,n=D("access_token",t.orgId,t.headId),i=D("user_id",t.orgId,t.headId),r=parseInt(D("session_id",t.orgId,t.headId)||"")||0,o=!0;if("undefined"!==n&&n&&i&&"number"==typeof r){var s=1e3*JSON.parse(window.atob(n.split(".")[1])).exp,a=(new Date).getTime()+t.EXPIRATION_OFFSET;o=a>s}var c=function(){if(o)return Promise.resolve(t.getAuthToken(t.username,t.password)).then(function(e){if(!e)throw new Error("Could not renew authentication token");i=e.user_id,r++,_("access_token",n=e.access_token,t.orgId,t.headId),_("user_id",i,t.orgId,t.headId),_("session_id",r.toString(),t.orgId,t.headId)})}();return Promise.resolve(c&&c.then?c.then(e):e())}catch(e){return Promise.reject(e)}},t.getAsrToken=function(e){try{var t=this,n=D("asr_token",t.orgId,t.headId),i=D("region",t.orgId,t.headId);return Promise.resolve(t.getAccessToken()).then(function(r){var o,s=r.access_token;function a(e){return o?e:{token:n,region:i}}var c=!0;if(n&&i){var u=1e3*JSON.parse(window.atob(n.split(".")[1])).exp,d=(new Date).getTime()+t.EXPIRATION_OFFSET;c=d>u}var h=function(){if(c)return Promise.resolve(t.getProviderToken(s,e)).then(function(e){if(!e)return o=1,{token:"",region:""};i=e.region,_("asr_token",n=e.token,t.orgId,t.headId),_("asr_region",i,t.orgId,t.headId)})}();return h&&h.then?h.then(a):a(h)})}catch(e){return Promise.reject(e)}},e}();function L(e,t){try{var n=e()}catch(e){return t(e)}return n&&n.then?n.then(void 0,t):n}!function(e){e.INITIALIZING="initializing",e.READY="ready",e.PLAYING="playing",e.PAUSED="paused",e.INTERRUPTED="interrupted",e.DESTROYED="destroyed"}(j||(j={})),exports.VideoTransitionType=void 0,(N=exports.VideoTransitionType||(exports.VideoTransitionType={})).NONE="none",N.CROSSFADE="crossfade",N.FADEIN="fadein",N.FADEOUT="fadeout";var z=/*#__PURE__*/function(){function e(e,t,n,r){var o=this;this.canvas=void 0,this.ctx=void 0,this.container=void 0,this.config=void 0,this.resizeObserver=null,this.decoder=null,this.state=j.INITIALIZING,this.isProcessingFrame=!1,this.isStreaming=!1,this.startTime=0,this.frameBuffer=[],this.currentSequenceId=0,this.animationFrameId=null,this.renderLoop=!1,this.fpsCounter={count:0,lastTime:0},this.handleContextLoss=function(){},this.handleContextRestore=function(){},this.handleResize=function(){var e=o.container.getBoundingClientRect(),t=e.width,n=e.height;o.canvas.width=t,o.canvas.height=n,o.canvas.style.width=t+"px",o.canvas.style.height=n+"px",o.ctx.imageSmoothingEnabled=!0},this.canvas=e,this.ctx=t,this.container=n,this.config=i({maxBufferSize:1e3,enableAdaptiveQuality:!1},r),this.setupContextLossHandling(),this.setupResizeHandling(),this.initializeDecoder()}e.create=function(t,n){try{var i;if(!("VideoDecoder"in window))throw new Error("WebCodecs VideoDecoder API is not supported in this browser");var r=document.createElement("canvas");r.width=n.width,r.height=n.height,r.style.position="absolute",r.style.top="0",r.style.left="0",r.style.backgroundColor=(null==(i=n.backgroundColor)?void 0:i.toString())||"#000000",r.style.maxWidth="100%",r.style.height="100%",r.style.zIndex="0",r.style.pointerEvents="none",r.style.objectFit="cover",r.style.imageRendering="pixelated";var o=r.getContext("2d",{alpha:!1,desynchronized:!0});if(!o)throw new Error("Failed to get 2D canvas context");return o.imageSmoothingEnabled=!0,t.appendChild(r),Promise.resolve(new e(r,o,t,n))}catch(e){return Promise.reject(e)}};var t=e.prototype;return t.initializeDecoder=function(){try{var e=this;return Promise.resolve(L(function(){return new VideoDecoder({output:function(){},error:function(){}}).close(),e.decoder=new VideoDecoder({output:function(t){e.renderVideoFrame(t)},error:function(e){console.error("VP8 Decoder error:",e.message)}}),Promise.resolve(e.decoder.configure({codec:"vp8",codedWidth:e.config.width,codedHeight:e.config.height})).then(function(){})},function(e){throw new Error("Failed to initialize VP8 decoder: "+e.message)}))}catch(e){return Promise.reject(e)}},t.setupResizeHandling=function(){var e=this;this.resizeObserver=new ResizeObserver(function(t){for(var i,r=function(e){var t="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(t)return(t=t.call(e)).next.bind(t);if(Array.isArray(e)||(t=function(e,t){if(e){if("string"==typeof e)return n(e,t);var i={}.toString.call(e).slice(8,-1);return"Object"===i&&e.constructor&&(i=e.constructor.name),"Map"===i||"Set"===i?Array.from(e):"Arguments"===i||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(i)?n(e,t):void 0}}(e))){t&&(e=t);var i=0;return function(){return i>=e.length?{done:!0}:{done:!1,value:e[i++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}(t);!(i=r()).done;)i.value.target===e.container&&e.handleResize()}),this.resizeObserver.observe(this.container),window.addEventListener("resize",this.handleResize)},t.getBufferLength=function(){return this.frameBuffer.length},t.getStreamingStatus=function(){return this.isStreaming},t.addFrame=function(e,t,n){void 0===n&&(n=!1);try{var i=this;if(i.state===j.DESTROYED)throw new Error("Cannot add frame to destroyed video output");var r={data:e,timestamp:t,isKeyframe:n,sequenceId:i.currentSequenceId++,size:e.byteLength};if(i.frameBuffer.length>=(i.config.maxBufferSize||1e3))if(i.config.enableAdaptiveQuality){if(!n)return Promise.resolve();var o=i.frameBuffer.findIndex(function(e){return!e.isKeyframe});-1!==o?i.frameBuffer.splice(o,1):i.frameBuffer.shift()}else i.frameBuffer.shift();return i.frameBuffer.push(r),Promise.resolve()}catch(e){return Promise.reject(e)}},t.toggleStream=function(e){try{return this.isStreaming=e,Promise.resolve()}catch(e){return Promise.reject(e)}},t.clearFrame=function(){if(this.state===j.DESTROYED)throw new Error("Cannot clear frame from destroyed video output");if(this.renderLoop=!1,this.animationFrameId&&(cancelAnimationFrame(this.animationFrameId),this.animationFrameId=null),this.frameBuffer=[],this.isProcessingFrame=!1,this.currentSequenceId=0,this.startTime=0,this.fpsCounter={count:0,lastTime:0},this.decoder&&"configured"===this.decoder.state)try{this.decoder.flush()}catch(e){console.warn("Error flushing decoder:",e)}this.clearCanvas(),this.state=j.READY},t.interrupt=function(e){var t=this;if(void 0===e&&(e=!1),this.state!==j.DESTROYED){if(this.state=j.INTERRUPTED,this.frameBuffer=[],this.isProcessingFrame=!1,this.decoder&&"configured"===this.decoder.state)try{this.decoder.flush()}catch(e){console.warn("Error flushing decoder:",e)}e?this.fadeOutCanvas().then(function(){t.clearCanvas()}):this.clearCanvas()}},t.destroy=function(){this.state!==j.DESTROYED&&(this.state=j.DESTROYED,this.renderLoop=!1,this.animationFrameId&&(cancelAnimationFrame(this.animationFrameId),this.animationFrameId=null),this.decoder&&(this.decoder.close(),this.decoder=null),this.frameBuffer=[],this.canvas.removeEventListener("webglcontextlost",this.handleContextLoss),this.canvas.removeEventListener("webglcontextrestored",this.handleContextRestore),this.canvas.parentNode&&this.canvas.parentNode.removeChild(this.canvas),this.isProcessingFrame=!1)},t.getState=function(){return this.state},t.startRenderingStreamingVideo=function(e){e&&(this.state=j.READY,this.renderLoop=!0,this.startTime=performance.now(),this.render())},t.render=function(){var e,t=this;if(this.renderLoop&&this.state!==j.DESTROYED){var n=(null==(e=this.frameBuffer[0])?void 0:e.timestamp)||0;this.frameBuffer.length>0&&0===this.frameBuffer[0].sequenceId&&(this.startTime=performance.now());var i=performance.now()-this.startTime;i>=n&&this.frameBuffer.length>0&&!this.isProcessingFrame&&this.processNextFrame(i,i-n),this.animationFrameId=requestAnimationFrame(function(){return t.render()})}},t.processNextFrame=function(e,t){try{var n=this;if(n.isProcessingFrame||0===n.frameBuffer.length||!n.decoder)return Promise.resolve();if("configured"!==n.decoder.state)return Promise.resolve();n.isProcessingFrame=!0;var i=n.frameBuffer.shift(),r=function(e,t){try{var r=L(function(){return Promise.resolve(n.decodeVP8Frame(i)).then(function(){})},function(e){console.error("Frame processing failed:",e)})}catch(e){return t(!0,e)}return r&&r.then?r.then(t.bind(null,!1),t.bind(null,!0)):t(!1,r)}(0,function(e,t){if(n.isProcessingFrame=!1,e)throw t;return t});return Promise.resolve(r&&r.then?r.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},t.decodeVP8Frame=function(e){try{try{var t=new EncodedVideoChunk({type:e.isKeyframe?"key":"delta",timestamp:1e3*e.timestamp,data:e.data});this.decoder.decode(t)}catch(e){throw new Error("VP8 decode failed: "+e.message)}return Promise.resolve()}catch(e){return Promise.reject(e)}},t.renderVideoFrame=function(e){try{this.ctx.clearRect(0,0,this.canvas.width,this.canvas.height);var t,n,i,r,o=e.displayWidth/e.displayHeight;o>this.canvas.width/this.canvas.height?(i=(this.canvas.width-(t=(n=this.canvas.height)*o))/2,r=0):(i=0,r=(this.canvas.height-(n=(t=this.canvas.width)/o))/2),this.ctx.drawImage(e,i,r,t,n),e.close()}catch(e){console.error("Error rendering video frame:",e)}},t.clearCanvas=function(){this.ctx.clearRect(0,0,this.canvas.width,this.canvas.height)},t.fadeOutCanvas=function(){try{var e=this;return Promise.resolve(new Promise(function(t){var n=1,i=function(){e.canvas.style.opacity=(n-=.05).toString(),n<=0?(e.canvas.style.opacity="1",t()):requestAnimationFrame(i)};i()}))}catch(e){return Promise.reject(e)}},t.setupContextLossHandling=function(){var e=this;this.handleContextLoss=function(t){t.preventDefault(),console.warn("Canvas context lost"),e.state=j.PAUSED},this.handleContextRestore=function(){e.state===j.PAUSED&&(e.state=j.PLAYING)},this.canvas.addEventListener("webglcontextlost",this.handleContextLoss),this.canvas.addEventListener("webglcontextrestored",this.handleContextRestore)},e}();function U(e,t){try{var n=e()}catch(e){return t(e)}return n&&n.then?n.then(void 0,t):n}var W=/*#__PURE__*/function(){function e(e,t,n,i){this.videoOutput=void 0,this.container=void 0,this.idleVideo=null,this.cachedVideo=null,this.idleVideoConfig=null,this.videoTransitionConfig=null,this.CROSSFADE_DURATION=500,this.isTransitioning=!1,this.isShowingIdleVideo=!0,this.bufferCheckAnimationId=null,this.lastBufferCheckTime=0,this.sessionStarted=!1,this.isShowingCachedVideo=!1,this.onIdleVideoShown=void 0,this.onIdleVideoHidden=void 0,this.onSpeakingStartCallback=null,this.onSpeakingEndCallback=null,this.videoOutput=e,this.container=t,this.videoTransitionConfig=n}e.createVideoOutput=function(t,n){try{return Promise.resolve(z.create(t,n)).then(function(i){var r=new e(i,t,n.transition,n.effects);return Promise.resolve(r.setupIdleVideo(n.idleVideo,n)).then(function(){return Promise.resolve(r.setupCachedVideo(n)).then(function(){return r})})})}catch(e){return Promise.reject(e)}};var t=e.prototype;return t.setupIdleVideo=function(e,t){try{var n=this;n.idleVideoConfig=e,n.idleVideo=document.createElement("video"),n.idleVideo.src=e.src,n.idleVideo.width=t.width,n.idleVideo.height=t.height,n.idleVideo.style.position="absolute",n.idleVideo.style.top="0",n.idleVideo.style.left="0",n.idleVideo.style.width="100%",n.idleVideo.style.height="100%",n.idleVideo.style.objectFit="cover",n.idleVideo.style.maxWidth="100%",n.idleVideo.style.backgroundColor=t.backgroundColor||"#000000",n.idleVideo.style.zIndex="1",n.idleVideo.style.pointerEvents="none",n.idleVideo.style.opacity="1",n.idleVideo.muted=!0,n.idleVideo.loop=!0,n.idleVideo.controls=!1,n.idleVideo.playsInline=!0,n.idleVideo.preload="auto","static"===getComputedStyle(n.container).position&&(n.container.style.position="relative"),n.container.appendChild(n.idleVideo);var i=U(function(){return Promise.resolve(n.idleVideo.load()).then(function(){return Promise.resolve(n.idleVideo.play()).then(function(){})})},function(e){console.error("Failed to load idle video:",e)});return Promise.resolve(i&&i.then?i.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},t.setupCachedVideo=function(e){try{var t=this;return t.cachedVideo=document.createElement("video"),t.cachedVideo.src="",t.cachedVideo.width=e.width,t.cachedVideo.height=e.height,t.cachedVideo.style.position="absolute",t.cachedVideo.style.top="0",t.cachedVideo.style.left="0",t.cachedVideo.style.width="100%",t.cachedVideo.style.height="100%",t.cachedVideo.style.objectFit="cover",t.cachedVideo.style.maxWidth="100%",t.cachedVideo.style.zIndex="2",t.cachedVideo.style.pointerEvents="none",t.cachedVideo.style.opacity="0",t.cachedVideo.style.transition="opacity "+t.CROSSFADE_DURATION+"ms ease-in-out",t.cachedVideo.controls=!1,t.cachedVideo.playsInline=!0,t.cachedVideo.preload="auto",t.container.appendChild(t.cachedVideo),Promise.resolve()}catch(e){return Promise.reject(e)}},t.onSpeakingStart=function(e){this.onSpeakingStartCallback=e},t.onSpeakingEnd=function(e){this.onSpeakingEndCallback=e},t.startStreaming=function(e){void 0===e&&(e=!1),this.sessionStarted=!0,this.startBufferMonitoring(),this.videoOutput.startRenderingStreamingVideo(e)},t.stopBufferMonitoring=function(){this.bufferCheckAnimationId&&(cancelAnimationFrame(this.bufferCheckAnimationId),this.bufferCheckAnimationId=null),this.lastBufferCheckTime=0,this.sessionStarted=!1},t.getBufferLength=function(){return this.videoOutput.getBufferLength()},t.startBufferMonitoring=function(){var e=this;if(!this.bufferCheckAnimationId){this.lastBufferCheckTime=0;var t=function(n){n-e.lastBufferCheckTime>=100&&(e.sessionStarted&&e.videoOutput.getBufferLength()>0?e.hideIdleVideoBeforeStream():e.videoOutput.getStreamingStatus()||0!==e.videoOutput.getBufferLength()||e.showIdleVideoAfterStream(),e.lastBufferCheckTime=n),e.bufferCheckAnimationId=requestAnimationFrame(t)};this.bufferCheckAnimationId=requestAnimationFrame(t)}},t.toggleCacheVideoMute=function(){this.cachedVideo&&(this.cachedVideo.muted=!this.cachedVideo.muted)},t.playCachedVideo=function(e){try{var t=this;if(t.isShowingCachedVideo||!t.cachedVideo||!e||t.isTransitioning)return Promise.resolve();t.isShowingCachedVideo=!0;var n=U(function(){return t.cachedVideo.src=e,t.cachedVideo.style.opacity="0",Promise.resolve(new Promise(function(e,n){t.cachedVideo.addEventListener("loadeddata",function(){return e()},{once:!0}),t.cachedVideo.addEventListener("error",function(){return n(new Error("Video failed to load"))},{once:!0}),t.cachedVideo.load()})).then(function(){return t.crossfadeFromIdleToCached(),Promise.resolve(t.cachedVideo.play()).then(function(){t.cachedVideo.addEventListener("ended",function(){try{return Promise.resolve(t.crossfadeFromCachedToIdle()).then(function(){return Promise.resolve(t.cleanupCachedVideo()).then(function(){return Promise.resolve(t.showIdleVideo()).then(function(){})})})}catch(e){return Promise.reject(e)}},{once:!0})})})},function(e){return console.error("Failed to play cached video:",e),t.cachedVideo.style.opacity="0",t.isShowingCachedVideo=!1,Promise.resolve(t.showIdleVideo()).then(function(){})});return Promise.resolve(n&&n.then?n.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},t.crossfadeFromIdleToCached=function(){try{var e=this;return e.idleVideo&&e.cachedVideo&&!e.isTransitioning?(e.isTransitioning=!0,e.idleVideo.style.opacity="1",e.cachedVideo.style.opacity="1",Promise.resolve(new Promise(function(t){return setTimeout(t,e.CROSSFADE_DURATION)})).then(function(){e.idleVideo&&(e.idleVideo.style.opacity="0"),e.isShowingIdleVideo=!1,null==e.onIdleVideoHidden||e.onIdleVideoHidden(),null==e.onSpeakingStartCallback||e.onSpeakingStartCallback(),e.isTransitioning=!1})):Promise.resolve()}catch(e){return Promise.reject(e)}},t.crossfadeFromCachedToIdle=function(){try{var e=function(){return t.cachedVideo.style.opacity="0",Promise.resolve(new Promise(function(e){return setTimeout(e,t.CROSSFADE_DURATION)})).then(function(){t.isShowingIdleVideo=!0,null==t.onSpeakingEndCallback||t.onSpeakingEndCallback(),null==t.onIdleVideoShown||t.onIdleVideoShown(),t.isTransitioning=!1})},t=this;if(!t.idleVideo||!t.cachedVideo||t.isTransitioning)return Promise.resolve();t.isTransitioning=!0,t.idleVideo.style.opacity="1";var n=function(){if(t.idleVideo.paused){var e=U(function(){return Promise.resolve(t.idleVideo.play()).then(function(){})},function(e){console.error("Failed to play idle video during crossfade:",e)});if(e&&e.then)return e.then(function(){})}}();return Promise.resolve(n&&n.then?n.then(e):e())}catch(e){return Promise.reject(e)}},t.cleanupCachedVideo=function(){try{var e=this;return e.cachedVideo&&(e.cachedVideo.pause(),e.cachedVideo.currentTime=0,e.cachedVideo.src="",e.cachedVideo.style.opacity="0"),e.isShowingCachedVideo=!1,Promise.resolve()}catch(e){return Promise.reject(e)}},t.hideIdleVideo=function(){var e,t;this.idleVideo&&this.isShowingIdleVideo&&(null==(e=this.onSpeakingStartCallback)||e.call(this),this.isShowingIdleVideo=!1,this.idleVideo.style.opacity="0",null==(t=this.onIdleVideoHidden)||t.call(this))},t.hideIdleVideoBeforeStream=function(){var e,t;this.idleVideo&&this.isShowingIdleVideo&&(null==(e=this.onSpeakingStartCallback)||e.call(this),this.idleVideo.style.transition="opacity "+this.CROSSFADE_DURATION+"ms ease-in-out",this.isShowingIdleVideo=!1,this.idleVideo.style.opacity="0",null==(t=this.onIdleVideoHidden)||t.call(this))},t.setEventCallbacks=function(e){this.onIdleVideoShown=e.onIdleVideoShown,this.onIdleVideoHidden=e.onIdleVideoHidden},t.getStreamingStatus=function(){return this.videoOutput.getStreamingStatus()},t.showIdleVideo=function(){try{var e=this;return!e.idleVideo||e.isShowingIdleVideo?Promise.resolve():(null==e.onSpeakingEndCallback||e.onSpeakingEndCallback(),null==e.onIdleVideoShown||e.onIdleVideoShown(),e.isShowingIdleVideo=!0,e.idleVideo.style.opacity="1",Promise.resolve(U(function(){var t=function(){if(e.idleVideo.paused)return Promise.resolve(e.idleVideo.play()).then(function(){})}();if(t&&t.then)return t.then(function(){})},function(e){console.error("failed to play idle video:",e)})))}catch(e){return Promise.reject(e)}},t.showIdleVideoAfterStream=function(){try{var e=this;return!e.idleVideo||e.isShowingIdleVideo?Promise.resolve():(null==e.onSpeakingEndCallback||e.onSpeakingEndCallback(),null==e.onIdleVideoShown||e.onIdleVideoShown(),e.isShowingIdleVideo=!0,e.idleVideo.style.opacity="1",Promise.resolve(U(function(){var t=function(){if(e.idleVideo.paused)return Promise.resolve(e.idleVideo.play()).then(function(){})}();if(t&&t.then)return t.then(function(){})},function(e){console.error("failed to play idle video:",e)})))}catch(e){return Promise.reject(e)}},t.addFrame=function(e,t,n){try{var i=function(e){var i=o?new Uint8Array(e):e;return r.videoOutput.addFrame(i,t,n)},r=this,o=e instanceof Blob;return Promise.resolve(o?Promise.resolve(e.arrayBuffer()).then(i):i(new Uint8Array(e)))}catch(e){return Promise.reject(e)}},t.clearFrame=function(){return this.showIdleVideo(),this.videoOutput.clearFrame()},t.toggleStream=function(e){try{return Promise.resolve(this.videoOutput.toggleStream(e))}catch(e){return Promise.reject(e)}},t.interrupt=function(e){this.videoOutput.interrupt(e)},t.destroy=function(){this.bufferCheckAnimationId&&(cancelAnimationFrame(this.bufferCheckAnimationId),this.bufferCheckAnimationId=null),this.idleVideo&&(this.idleVideo.pause(),this.idleVideo.parentNode&&this.idleVideo.parentNode.removeChild(this.idleVideo),this.idleVideo=null),this.cachedVideo&&(this.cachedVideo.pause(),this.cachedVideo.parentNode&&this.cachedVideo.parentNode.removeChild(this.cachedVideo),this.cachedVideo=null),this.videoOutput.destroy()},e}(),H={vadSilenceThresholdSecs:1.5,noiseSuppression:!0,vadThreshold:.4,minSpeechDurationMs:100,minSilenceDurationMs:100},G={tolerance:40,softCorrectionThreshold:100,hardCorrectionThreshold:200,historyLength:50,maxCorrectionRate:.02,correctionCoolDown:500,correctionFadeTime:1e3,minAudioBuffer:100,maxAudioBuffer:500,minVideoBuffer:3,maxVideoBuffer:10};function q(e,t){try{var n=e()}catch(e){return t(e)}return n&&n.then?n.then(void 0,t):n}exports.Conversation=/*#__PURE__*/function(){function e(e,t,n,i,r,o,s,a,c){var u=this,d=this,h=this,l=this,p=this,y=this,S=this,T=this;this.options=void 0,this.microphoneAccess=void 0,this.connection=void 0,this.idleVideo=void 0,this.wakeLock=void 0,this.user=void 0,this.audioOutput=void 0,this.videoOutput=void 0,this.headInfo=void 0,this.status="connecting",this.volume=1,this.sessionStarted=!1,this.messageCounter=0,this.syncController=void 0,this.avController=void 0,this.monitor=null,this.microphone=null,this.videoFrameQueue=[],this.cachedResponseQueue=[],this.onOutputWorkletMessage=function(e){T.avController.handleAudioWorkletMessage(e.data)},this.handleMessage=function(e){try{return u.options.onMessage({timestamp:e.timestamp,sender:e.speaker,text:e.text,visible:e.visible}),Promise.resolve()}catch(e){return Promise.reject(e)}},this.handleStreamError=function(e){var t=e.error_type;if(t&&["resource_exhausted","deadline_exceeded","inactivity_timeout"].includes(t)){if("resource_exhausted"===t){if(!T.avController.isPlaying)return void T.options.onError({message:"The system is experiencing heavy traffic. Please try again later.",endConversation:!0,type:"toast"});T.options.onError({message:"We are experiencing heavy traffic and we can't deliver the response, try again",endConversation:!1,type:"toast"})}else if("deadline_exceeded"===t)T.options.onError({message:"We are experiencing heavy traffic and we can't deliver the response, try again",endConversation:!1,type:"toast"});else if("inactivity_timeout"===t)return void T.options.onTimeout()}else T.options.onError({message:"A connection error occurred. Please try again.",endConversation:!0,type:"toast"})},this.handleStreamingEvent=function(e){try{var t,n=function(n){if(t)return n;if("metadata"===e.type&&("start"!==e.metadata_type&&"end"!==e.metadata_type||(d.avController.isStoppingAV=!1,d.videoOutput.toggleStream("start"===e.metadata_type),"start"===e.metadata_type&&(d.videoFrameQueue=[],d.syncController.resetTiming()))),"audio_frame"===e.type){if(d.avController.isStoppingAV)return;d.handleAudioFrame(e)}if("video_frame"===e.type){if(d.avController.isStoppingAV)return;d.handleVideoFrame(e)}d.avController.playAudioVideo()};if("error"===e.type)return d.handleStreamError(e),Promise.resolve();var i=function(){if("cache"===e.type){var n,i=function(){t=1},r={id:e.session_id||Math.random().toString(36).substring(2),timestamp:new Date,isSent:!0,visible:!0,event:exports.EventType.TEXT,user_id:e.user_id,username:e.username,text:null!=(n=e.text)?n:"",speaker:"ai",session_id:e.session_id||"",suggestions:[]},o=function(){if(d.sessionStarted)return Promise.resolve(d.videoOutput.playCachedVideo(e.video_url||"")).then(function(){d.handleMessage(r)});d.cachedResponseQueue.push({video_url:e.video_url||"",textEventData:r})}();return o&&o.then?o.then(i):i()}}();return Promise.resolve(i&&i.then?i.then(n):n(i))}catch(e){return Promise.reject(e)}},this.handleVideoFrame=function(e){try{var t;return e.frame_data||h.videoFrameQueue.push({timeStamp:e.event_timestamp_ms,isKeyframe:null!=(t=e.is_keyframe)&&t}),Promise.resolve()}catch(e){return Promise.reject(e)}},this.handleBinaryData=function(e){try{if(l.videoFrameQueue.length>0){var t=l.videoFrameQueue.shift();l.updateStatus("connected"),l.videoOutput.addFrame(e.data,t.timeStamp,t.isKeyframe),l.syncController.updateVideoTime(t.timeStamp)}return Promise.resolve()}catch(e){return Promise.reject(e)}},this.handleAudioFrame=function(e){try{if(!e.frame_data)return console.warn("Audio frame data is missing in the event:",e),Promise.resolve();var t,n=function(e){for(var t=window.atob(e),n=t.length,i=new Uint8Array(n),r=0;r<n;r++)i[r]=t.charCodeAt(r);return i.buffer}(e.frame_data);return t=new Int16Array(n),p.audioOutput.gain.gain.value=p.volume,p.audioOutput.worklet.port.postMessage({type:"clearInterrupted"}),p.audioOutput.worklet.port.postMessage({type:"buffer",buffer:t,time_stamp:e.event_timestamp_ms}),p.syncController.updateAudioTime(e.event_timestamp_ms),Promise.resolve()}catch(e){return Promise.reject(e)}},this.onMessage=function(e){try{switch(e.event){case exports.EventType.JOIN:return Promise.resolve();case exports.EventType.TEXT:if(m(e)){var t=e;t.timestamp=new Date,t.isSent=!0,t.visible=!0,y.handleMessage(t)}return Promise.resolve();case exports.EventType.RESPONSE:if(v(e)){var n=e;n.timestamp=new Date,n.speaker="ai",n.isSent=!0,y.handleMessage(n)}return Promise.resolve();case exports.EventType.STREAMING:return g(e)&&y.handleStreamingEvent(e),Promise.resolve();case exports.EventType.BINARY:P(e)&&y.handleBinaryData(e);case exports.EventType.TIMEOUT_WARNING:return I(e)&&y.options.onTimeoutWarning(),Promise.resolve();case exports.EventType.TIME_OUT:return w(e)&&y.options.onTimeout(),Promise.resolve();case exports.EventType.KEEP_SESSION:return E(e)&&y.options.onKeepSession({granted:e.granted}),Promise.resolve();default:return console.warn("Unhandled event type:",e.event),Promise.resolve()}}catch(e){return Promise.reject(e)}},this.endSessionWithDetails=function(e){try{return"connected"!==S.status&&"connecting"!==S.status?Promise.resolve():(S.sessionStarted=!1,S.updateStatus("disconnecting"),Promise.resolve(S.handleEndSession()).then(function(){var t;S.updateStatus("disconnected"),null==(t=S.monitor)||t.stop(),S.options.onDisconnect(e)}))}catch(e){return Promise.reject(e)}},this.options=e,this.microphoneAccess=t,this.connection=n,this.idleVideo=i,this.wakeLock=r,this.user=o,this.audioOutput=s,this.videoOutput=a,this.headInfo=c,this.syncController=new x(G),this.avController=new f(this.syncController,this.audioOutput,this.videoOutput),this.options.onConnect({userId:n.userId,headInfo:{name:this.headInfo.name,phrases:this.headInfo.phrases,language:this.headInfo.language,avatar:this.headInfo.avatarSrc},microphoneAccess:t}),this.connection.onDisconnect(this.endSessionWithDetails),this.connection.onMessage(this.onMessage),this.videoOutput.onSpeakingStart(this.options.onSpeakingStart),this.videoOutput.onSpeakingEnd(this.options.onSpeakingEnd),this.updateStatus("connected"),this.audioOutput.worklet.port.onmessage=this.onOutputWorkletMessage,this.handleIOSSilentMode(),this.startLatencyMonitoring()}e.getFullOptions=function(e){return i({username:"anonymous",environment:"production",mode:"default",apiKey:"",microphoneProvider:"azure",microphoneOptions:{onMicrophoneError:function(){},onMicrophoneStatusChange:function(){},onMicrophoneSpeechRecognitionResult:function(){}},elevenLabsOptions:H,onStatusChange:function(){},onConnect:function(){},onDisconnect:function(){},onMessage:function(){},onMuteStatusChange:function(){},onSpeakingStart:function(){},onSpeakingEnd:function(){},onStoppingEnd:function(){},onTimeout:function(){},onTimeoutWarning:function(){},onKeepSession:function(){},onError:function(){}},e)},e.startDigitalHuman=function(t){try{var n=function(){var n=T(o);return q(function(){return Promise.resolve(B.loginUser(r,"Password1",n,s,a)).then(function(r){return m=r,Promise.resolve(V.getIdleVideo(n,s,a)).then(function(r){return Promise.resolve(m.getHeadDetails(u)).then(function(d){return Promise.resolve(r.getAvatarSrc(n,s,a)).then(function(n){d.avatarSrc=n;var p=h||d.language||navigator.language;return d.language=p,Promise.resolve(k.create({environment:o,orgId:s,headId:a,token:m.accessToken,mode:c,apiKey:u,language:p})).then(function(n){function o(){return Promise.resolve(Promise.all([l.createAudioOutput(P),W.createVideoOutput(s,{width:s.getBoundingClientRect().width,height:s.getBoundingClientRect().height,frameRate:30,backgroundColor:"transparent",format:"jpeg",idleVideo:{src:r.idleVideoSource,enabled:!0},transition:null!=f?f:exports.VideoTransitionType.NONE})])).then(function(t){return new e(i,a,v,r,S,m,y=t[0],g=t[1],d)})}v=n;var s=t.element,a=!1,c=q(function(){return Promise.resolve(navigator.mediaDevices.getUserMedia({audio:!0})).then(function(e){e.getTracks().forEach(function(e){e.stop()}),a=!0})},function(){a=!1});return c&&c.then?c.then(o):o()})})})})})},function(e){var t,n;return p({status:"disconnected"}),null==(t=v)||t.close(),Promise.resolve(null==(n=y)?void 0:n.close()).then(function(){var t;return Promise.resolve(null==(t=g)?void 0:t.destroy()).then(function(){function t(){throw e}var n=q(function(){var e;return Promise.resolve(null==(e=S)?void 0:e.release()).then(function(){S=null})},function(){});return n&&n.then?n.then(t):t()})})})},i=e.getFullOptions(t),r=i.username,o=i.environment,s=i.orgId,a=i.headId,c=i.mode,u=i.apiKey,d=i.allowWakeLock,h=i.language,f=i.fadeTransitionsType,p=i.onStatusChange;p({status:"connecting"});var m=null,v=null,g=null,y=null,P={sampleRate:16e3,format:"pcm"},S=null,I=function(){if(null==d||d){var e=q(function(){return Promise.resolve(navigator.wakeLock.request("screen")).then(function(e){S=e})},function(){});if(e&&e.then)return e.then(function(){})}}();return Promise.resolve(I&&I.then?I.then(n):n())}catch(e){return Promise.reject(e)}},e.getBackgroundVideo=function(e){try{var t=e.environment,n=e.orgId,i=e.headId,r=T(null!=t?t:"production");return Promise.resolve(V.getIdleVideo(null!=r?r:"https://chat-origin.api.unith.live",n,i)).then(function(e){return e.idleVideoSource})}catch(e){return Promise.reject(e)}};var t=e.prototype;return t.startLatencyMonitoring=function(){this.monitor=new A(this.connection,{onUpdate:function(e){}})},t.handleIOSSilentMode=function(){var e=this;/iPad|iPhone|iPod/.test(navigator.userAgent)&&this.audioOutput.context.addEventListener("statechange",function(){"suspended"===e.audioOutput.context.state&&(console.warn("Audio context suspended - likely due to iOS silent mode"),e.audioOutput.context.resume())})},t.getUserId=function(){return this.connection.userId},t.handleEndSession=function(){try{return this.connection.close(),Promise.resolve()}catch(e){return Promise.reject(e)}},t.updateStatus=function(e){e!==this.status&&(this.status=e,this.options.onStatusChange({status:e}))},t.toggleMute=function(){try{var e=this;return e.volume=0===e.volume?1:0,e.audioOutput.toggleMute(),e.videoOutput.toggleCacheVideoMute(),e.options.onMuteStatusChange({isMuted:0===e.volume}),Promise.resolve(e.volume)}catch(e){return Promise.reject(e)}},t.startSession=function(){try{var e=function(){return Promise.resolve(t.avController.startPlayback(!0)).then(function(){function e(){function e(){return t.connection}var n=function(){var e;if("custom"!==t.options.microphoneProvider)return Promise.resolve(O.initializeMicrophone(t.options.microphoneOptions,t.options.microphoneProvider,null!=(e=t.options.elevenLabsOptions)?e:H,t.user,t.headInfo,t.microphoneAccess,t.sendMessage)).then(function(e){t.microphone=e})}();return n&&n.then?n.then(e):e()}var n=function(){if(t.cachedResponseQueue.length){var e=t.cachedResponseQueue[t.cachedResponseQueue.length-1];return Promise.resolve(new Promise(function(e){return setTimeout(e,50)})).then(function(){return Promise.resolve(t.videoOutput.playCachedVideo(e.video_url)).then(function(){t.handleMessage(e.textEventData)})})}}();return n&&n.then?n.then(e):e()})},t=this;t.sessionStarted=!0;var n=function(){if("suspended"===t.audioOutput.context.state)return Promise.resolve(t.audioOutput.context.resume()).then(function(){})}();return Promise.resolve(n&&n.then?n.then(e):e())}catch(e){return Promise.reject(e)}},t.toggleMicrophone=function(){try{var e=function(){if(!t.microphoneAccess&&"OFF"===t.microphone.status())throw new Error("Microphone access not granted.");return Promise.resolve(t.microphone.toggleMicrophone()).then(function(){})},t=this;if("custom"===t.options.microphoneProvider)throw new Error("Cannot toggle microphone for custom provider.");var n=function(){var e;if(!t.microphone)return Promise.resolve(O.initializeMicrophone(t.options.microphoneOptions,t.options.microphoneProvider,null!=(e=t.options.elevenLabsOptions)?e:H,t.user,t.headInfo,t.microphoneAccess,t.sendMessage)).then(function(e){t.microphone=e})}();return Promise.resolve(n&&n.then?n.then(e):e())}catch(e){return Promise.reject(e)}},t.getMicrophoneStatus=function(){return this.microphone?this.microphone.status():"OFF"},t.endSession=function(){return this.endSessionWithDetails({reason:"user"})},t.sendMessage=function(e){if(!this.connection)throw new Error("Connection not established");var t=this.user.id+"::"+this.user.orgId+"::"+this.user.headId+"::"+this.user.sessionId.toString().padStart(5,"0"),n={id:this.messageCounter++,timestamp:(new Date).toISOString(),speaker:"user",text:e,isSent:!1,user_id:this.user.id,username:this.user.username,event:exports.EventType.TEXT,visible:!0,session_id:t};return this.connection.sendMessage(n)},t.keepSession=function(){if(!this.connection)throw new Error("Connection not established");var e=this.user.id+"::"+this.user.orgId+"::"+this.user.headId+"::"+this.user.sessionId.toString().padStart(5,"0"),t={id:this.messageCounter,timestamp:(new Date).toISOString(),speaker:"user",text:"",isSent:!1,user_id:this.user.id,username:this.user.username,event:exports.EventType.KEEP_SESSION,visible:!0,session_id:e};return this.connection.sendMessage(t)},e}(),exports.isBinaryEvent=P,exports.isConversationEndEvent=function(e){return e.event===exports.EventType.CONVERSATION_END},exports.isJoinEvent=p,exports.isKeepSessionEvent=E,exports.isPongEvent=S,exports.isResponseEvent=v,exports.isStreamingErrorEvent=y,exports.isStreamingEvent=g,exports.isTextEvent=m,exports.isTimeoutEvent=w,exports.isTimeoutWarningEvent=I;
|
|
2
2
|
//# sourceMappingURL=lib.js.map
|