@elevenlabs/client 0.6.0 → 0.6.2

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 CHANGED
@@ -161,6 +161,20 @@ The options passed to `startSession` can also be used to register optional callb
161
161
  - **onModeChange** - handler called when a status changes, eg. agent switches from `speaking` to `listening`, or the other way around.
162
162
  - **onCanSendFeedbackChange** - handler called when sending feedback becomes available or unavailable.
163
163
 
164
+ #### Setting input/output devices
165
+
166
+ You can provide a device ID to start the conversation using the input/output device of your choice. If the device ID is invalid, the default input and output devices will be used.
167
+
168
+ ```js
169
+ const conversation = await Conversation.startSession({
170
+ agentId: "<your-agent-id>",
171
+ inputDeviceId: "<new-input-id>",
172
+ outputDeviceId: "<new-output-id>",
173
+ });
174
+ ```
175
+
176
+ **Note:** Device switching only works for voice conversations. You can enumerate available devices using the [MediaDevices.enumerateDevices()](https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/enumerateDevices) API.
177
+
164
178
  #### Client Tools
165
179
 
166
180
  Client tools are a way to enabled agent to invoke client-side functionality. This can be used to trigger actions in the client, such as opening a modal or doing an API call on behalf of the user.
@@ -374,27 +388,27 @@ const outputVolume = await conversation.getOutputVolume();
374
388
 
375
389
  ##### getInputByteFrequencyData / getOutputByteFrequencyData
376
390
 
377
- Methods that return `Uint8Array`s containg the current input/output frequency data. See [AnalyserNode.getByteFrequencyData](https://developer.mozilla.org/en-US/docs/Web/API/AnalyserNode/getByteFrequencyData) for more information.
391
+ Methods that return `Uint8Array`s containing the current input/output frequency data. See [AnalyserNode.getByteFrequencyData](https://developer.mozilla.org/en-US/docs/Web/API/AnalyserNode/getByteFrequencyData) for more information.
392
+
393
+ **Note:** These methods are only available for voice conversations. In WebRTC mode the audio is hardcoded to use `pcm_48000`, meaning any visualization using the returned data might show different patterns to WebSocket connections.
378
394
 
379
395
  ##### changeInputDevice
380
396
 
381
397
  Allows you to change the audio input device during an active voice conversation. This method is only available for voice conversations.
382
398
 
383
- ```js
384
- import { VoiceConversation } from "@elevenlabs/client";
399
+ **Note:** In WebRTC mode the input format and sample rate are hardcoded to `pcm` and `48000` respectively. Changing those values when changing the input device is a no-op.
385
400
 
386
- // Start a voice conversation
401
+ ```js
387
402
  const conversation = await Conversation.startSession({
388
403
  agentId: "<your-agent-id>",
389
- textOnly: false, // Ensure this is a voice conversation
390
404
  });
391
405
 
392
406
  // Change to a specific input device
393
- await (conversation as VoiceConversation).changeInputDevice({
407
+ await conversation.changeInputDevice({
394
408
  sampleRate: 16000,
395
409
  format: "pcm",
396
410
  preferHeadphonesForIosDevices: true,
397
- inputDeviceId: "your-device-id", // Optional: specific device ID
411
+ inputDeviceId: "your-device-id",
398
412
  });
399
413
  ```
400
414
 
@@ -402,14 +416,14 @@ await (conversation as VoiceConversation).changeInputDevice({
402
416
 
403
417
  Allows you to change the audio output device during an active voice conversation. This method is only available for voice conversations.
404
418
 
405
- ```js
406
- import { VoiceConversation } from "@elevenlabs/client";
419
+ **Note:** In WebRTC mode the output format and sample rate are hardcoded to `pcm` and `48000` respectively. Changing those values when changing the output device is a no-op.
407
420
 
421
+ ```js
408
422
  // Change to a specific output device
409
423
  await conversation.changeOutputDevice({
410
424
  sampleRate: 16000,
411
425
  format: "pcm",
412
- outputDeviceId: "your-device-id", // Optional: specific device ID
426
+ outputDeviceId: "your-device-id",
413
427
  });
414
428
  ```
415
429
 
@@ -1,4 +1,4 @@
1
- import { Input, InputConfig } from "./utils/input";
1
+ import { Input, type InputConfig } from "./utils/input";
2
2
  import { Output } from "./utils/output";
3
3
  import type { BaseConnection, FormatConfig } from "./utils/BaseConnection";
4
4
  import type { AgentAudioEvent, InterruptionEvent } from "./utils/events";
package/dist/index.d.ts CHANGED
@@ -10,6 +10,7 @@ export { WebSocketConnection } from "./utils/WebSocketConnection";
10
10
  export { WebRTCConnection } from "./utils/WebRTCConnection";
11
11
  export { postOverallFeedback } from "./utils/postOverallFeedback";
12
12
  export { VoiceConversation } from "./VoiceConversation";
13
+ export { TextConversation } from "./TextConversation";
13
14
  export declare class Conversation extends BaseConversation {
14
15
  static startSession(options: PartialOptions): Promise<Conversation>;
15
16
  }
package/dist/lib.cjs CHANGED
@@ -1,2 +1,2 @@
1
- var e=require("livekit-client");function n(){return n=Object.assign?Object.assign.bind():function(e){for(var n=1;n<arguments.length;n++){var t=arguments[n];for(var o in t)({}).hasOwnProperty.call(t,o)&&(e[o]=t[o])}return e},n.apply(null,arguments)}function t(e,n){e.prototype=Object.create(n.prototype),e.prototype.constructor=e,o(e,n)}function o(e,n){return o=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,n){return e.__proto__=n,e},o(e,n)}function r(e,n){try{var t=e()}catch(e){return n(e)}return t&&t.then?t.then(void 0,n):t}var i=new Uint8Array(0),s=/*#__PURE__*/function(){function e(e,n){var t=this,o=this,i=this;this.options=void 0,this.connection=void 0,this.lastInterruptTimestamp=0,this.mode="listening",this.status="connecting",this.volume=1,this.currentEventId=1,this.lastFeedbackEventId=0,this.canSendFeedback=!1,this.endSessionWithDetails=function(e){try{return"connected"!==o.status&&"connecting"!==o.status?Promise.resolve():(o.updateStatus("disconnecting"),Promise.resolve(o.handleEndSession()).then(function(){o.updateStatus("disconnected"),o.options.onDisconnect&&o.options.onDisconnect(e)}))}catch(e){return Promise.reject(e)}},this.onMessage=function(e){try{switch(e.type){case"interruption":return i.handleInterruption(e),Promise.resolve();case"agent_response":return i.handleAgentResponse(e),Promise.resolve();case"user_transcript":return i.handleUserTranscript(e),Promise.resolve();case"internal_tentative_agent_response":return i.handleTentativeAgentResponse(e),Promise.resolve();case"client_tool_call":var n=r(function(){return Promise.resolve(i.handleClientToolCall(e)).then(function(){})},function(n){i.onError("Unexpected error in client tool call handling: "+(n instanceof Error?n.message:String(n)),{clientToolName:e.client_tool_call.tool_name,toolCallId:e.client_tool_call.tool_call_id})});return Promise.resolve(n&&n.then?n.then(function(){}):void 0);case"audio":return i.handleAudio(e),Promise.resolve();case"vad_score":return i.handleVadScore(e),Promise.resolve();case"ping":return i.connection.sendMessage({type:"pong",event_id:e.ping_event.event_id}),Promise.resolve();default:return i.options.onDebug&&i.options.onDebug(e),Promise.resolve()}}catch(e){return Promise.reject(e)}},this.setVolume=function(e){t.volume=e.volume},this.options=e,this.connection=n,this.options.onConnect&&this.options.onConnect({conversationId:n.conversationId}),this.connection.onMessage(this.onMessage),this.connection.onDisconnect(this.endSessionWithDetails),this.connection.onModeChange(function(e){return t.updateMode(e)}),this.updateStatus("connected")}e.getFullOptions=function(e){return n({clientTools:{},onConnect:function(){},onDebug:function(){},onDisconnect:function(){},onError:function(){},onMessage:function(){},onAudio:function(){},onModeChange:function(){},onStatusChange:function(){},onCanSendFeedbackChange:function(){}},e)};var t=e.prototype;return t.endSession=function(){return this.endSessionWithDetails({reason:"user"})},t.handleEndSession=function(){try{return this.connection.close(),Promise.resolve()}catch(e){return Promise.reject(e)}},t.updateMode=function(e){e!==this.mode&&(this.mode=e,this.options.onModeChange&&this.options.onModeChange({mode:e}))},t.updateStatus=function(e){e!==this.status&&(this.status=e,this.options.onStatusChange&&this.options.onStatusChange({status:e}))},t.updateCanSendFeedback=function(){var e=this.currentEventId!==this.lastFeedbackEventId;this.canSendFeedback!==e&&(this.canSendFeedback=e,this.options.onCanSendFeedbackChange&&this.options.onCanSendFeedbackChange({canSendFeedback:e}))},t.handleInterruption=function(e){e.interruption_event&&(this.lastInterruptTimestamp=e.interruption_event.event_id)},t.handleAgentResponse=function(e){this.options.onMessage&&this.options.onMessage({source:"ai",message:e.agent_response_event.agent_response})},t.handleUserTranscript=function(e){this.options.onMessage&&this.options.onMessage({source:"user",message:e.user_transcription_event.user_transcript})},t.handleTentativeAgentResponse=function(e){this.options.onDebug&&this.options.onDebug({type:"tentative_agent_response",response:e.tentative_agent_response_internal_event.tentative_agent_response})},t.handleVadScore=function(e){this.options.onVadScore&&this.options.onVadScore({vadScore:e.vad_score_event.vad_score})},t.handleClientToolCall=function(e){try{var n=this;return Promise.resolve(function(){if(Object.prototype.hasOwnProperty.call(n.options.clientTools,e.client_tool_call.tool_name)){var t=r(function(){return Promise.resolve(n.options.clientTools[e.client_tool_call.tool_name](e.client_tool_call.parameters)).then(function(t){var o="object"==typeof t?JSON.stringify(t):String(t);n.connection.sendMessage({type:"client_tool_result",tool_call_id:e.client_tool_call.tool_call_id,result:o,is_error:!1})})},function(t){n.onError("Client tool execution failed with following error: "+(null==t?void 0:t.message),{clientToolName:e.client_tool_call.tool_name}),n.connection.sendMessage({type:"client_tool_result",tool_call_id:e.client_tool_call.tool_call_id,result:"Client tool execution failed: "+(null==t?void 0:t.message),is_error:!0})});if(t&&t.then)return t.then(function(){})}else{if(n.options.onUnhandledClientToolCall)return void n.options.onUnhandledClientToolCall(e.client_tool_call);n.onError("Client tool with name "+e.client_tool_call.tool_name+" is not defined on client",{clientToolName:e.client_tool_call.tool_name}),n.connection.sendMessage({type:"client_tool_result",tool_call_id:e.client_tool_call.tool_call_id,result:"Client tool with name "+e.client_tool_call.tool_name+" is not defined on client",is_error:!0})}}())}catch(e){return Promise.reject(e)}},t.handleAudio=function(e){},t.onError=function(e,n){console.error(e,n),this.options.onError&&this.options.onError(e,n)},t.getId=function(){return this.connection.conversationId},t.isOpen=function(){return"connected"===this.status},t.setMicMuted=function(e){this.connection.setMicMuted(e)},t.getInputByteFrequencyData=function(){return i},t.getOutputByteFrequencyData=function(){return i},t.getInputVolume=function(){return 0},t.getOutputVolume=function(){return 0},t.sendFeedback=function(e){this.canSendFeedback?(this.connection.sendMessage({type:"feedback",score:e?"like":"dislike",event_id:this.currentEventId}),this.lastFeedbackEventId=this.currentEventId,this.updateCanSendFeedback()):console.warn(0===this.lastFeedbackEventId?"Cannot send feedback: the conversation has not started yet.":"Cannot send feedback: feedback has already been sent for the current response.")},t.sendContextualUpdate=function(e){this.connection.sendMessage({type:"contextual_update",text:e})},t.sendUserMessage=function(e){this.connection.sendMessage({type:"user_message",text:e})},t.sendUserActivity=function(){this.connection.sendMessage({type:"user_activity"})},t.sendMCPToolApprovalResult=function(e,n){this.connection.sendMessage({type:"mcp_tool_approval_result",tool_call_id:e,is_approved:n})},e}(),a=/*#__PURE__*/function(){function e(e){void 0===e&&(e={}),this.queue=[],this.disconnectionDetails=null,this.onDisconnectCallback=null,this.onMessageCallback=null,this.onModeChangeCallback=null,this.onDebug=void 0,this.onDebug=e.onDebug}var n=e.prototype;return n.debug=function(e){this.onDebug&&this.onDebug(e)},n.onMessage=function(e){this.onMessageCallback=e;var n=this.queue;this.queue=[],n.length>0&&queueMicrotask(function(){n.forEach(e)})},n.onDisconnect=function(e){this.onDisconnectCallback=e;var n=this.disconnectionDetails;n&&queueMicrotask(function(){e(n)})},n.onModeChange=function(e){this.onModeChangeCallback=e},n.updateMode=function(e){var n;null==(n=this.onModeChangeCallback)||n.call(this,e)},n.disconnect=function(e){var n;this.disconnectionDetails||(this.disconnectionDetails=e,null==(n=this.onDisconnectCallback)||n.call(this,e))},n.handleMessage=function(e){this.onMessageCallback?this.onMessageCallback(e):this.queue.push(e)},e}();function u(e){var n=e.split("_"),t=n[0],o=n[1];if(!["pcm","ulaw"].includes(t))throw new Error("Invalid format: "+e);var r=Number.parseInt(o);if(Number.isNaN(r))throw new Error("Invalid sample rate: "+o);return{format:t,sampleRate:r}}var c="0.6.0";function l(e){return!!e.type}var d="conversation_initiation_client_data";function h(e){var n,t,o,r,i,s,a={type:d};return e.overrides&&(a.conversation_config_override={agent:{prompt:null==(t=e.overrides.agent)?void 0:t.prompt,first_message:null==(o=e.overrides.agent)?void 0:o.firstMessage,language:null==(r=e.overrides.agent)?void 0:r.language},tts:{voice_id:null==(i=e.overrides.tts)?void 0:i.voiceId},conversation:{text_only:null==(s=e.overrides.conversation)?void 0:s.textOnly}}),e.customLlmExtraBody&&(a.custom_llm_extra_body=e.customLlmExtraBody),e.dynamicVariables&&(a.dynamic_variables=e.dynamicVariables),e.userId&&(a.user_id=e.userId),null!=(n=e.overrides)&&n.client&&(a.source_info={source:e.overrides.client.source,version:e.overrides.client.version}),a}var p=/*#__PURE__*/function(e){function n(n,t,o,r){var i;return(i=e.call(this)||this).socket=void 0,i.conversationId=void 0,i.inputFormat=void 0,i.outputFormat=void 0,i.socket=n,i.conversationId=t,i.inputFormat=o,i.outputFormat=r,i.socket.addEventListener("error",function(e){setTimeout(function(){return i.disconnect({reason:"error",message:"The connection was closed due to a socket error.",context:e})},0)}),i.socket.addEventListener("close",function(e){i.disconnect(1e3===e.code?{reason:"agent",context:e}:{reason:"error",message:e.reason||"The connection was closed by the server.",context:e})}),i.socket.addEventListener("message",function(e){try{var n=JSON.parse(e.data);if(!l(n))return void i.debug({type:"invalid_event",message:"Received invalid socket event",data:e.data});i.handleMessage(n)}catch(n){i.debug({type:"parsing_error",message:"Failed to parse socket message",error:n instanceof Error?n.message:String(n),data:e.data})}}),i}t(n,e),n.create=function(e){try{var t=null;return Promise.resolve(function(o,r){try{var i=function(){var o,r,i,s,a=null!=(o=e.origin)?o:"wss://api.elevenlabs.io",d=(null==(r=e.overrides)||null==(r=r.client)?void 0:r.version)||c,p=(null==(i=e.overrides)||null==(i=i.client)?void 0:i.source)||"js_sdk";if(e.signedUrl){var f=e.signedUrl.includes("?")?"&":"?";s=""+e.signedUrl+f+"source="+p+"&version="+d}else s=a+"/v1/convai/conversation?agent_id="+e.agentId+"&source="+p+"&version="+d;var v=["convai"];return e.authorization&&v.push("bearer."+e.authorization),t=new WebSocket(s,v),Promise.resolve(new Promise(function(n,o){t.addEventListener("open",function(){var n,o=h(e);null==(n=t)||n.send(JSON.stringify(o))},{once:!0}),t.addEventListener("error",function(e){setTimeout(function(){return o(e)},0)}),t.addEventListener("close",o),t.addEventListener("message",function(e){var t=JSON.parse(e.data);l(t)&&("conversation_initiation_metadata"===t.type?n(t.conversation_initiation_metadata_event):console.warn("First received message is not conversation metadata."))},{once:!0})})).then(function(e){var o=e.conversation_id,r=e.agent_output_audio_format,i=e.user_input_audio_format,s=u(null!=i?i:"pcm_16000"),a=u(r);return new n(t,o,s,a)})}()}catch(e){return r(e)}return i&&i.then?i.then(void 0,r):i}(0,function(e){var n;throw null==(n=t)||n.close(),e}))}catch(e){return Promise.reject(e)}};var o=n.prototype;return o.close=function(){this.socket.close()},o.sendMessage=function(e){this.socket.send(JSON.stringify(e))},o.setMicMuted=function(e){try{return console.warn("WebSocket connection setMicMuted called with "+e+", but this is handled by VoiceConversation"),Promise.resolve()}catch(e){return Promise.reject(e)}},n}(a);function f(e){var n=new Uint8Array(e);return window.btoa(String.fromCharCode.apply(String,n))}function v(e){for(var n=window.atob(e),t=n.length,o=new Uint8Array(t),r=0;r<t;r++)o[r]=n.charCodeAt(r);return o.buffer}function m(e,n){try{var t=e()}catch(e){return n(e)}return t&&t.then?t.then(void 0,n):t}var g=new Map;function y(e,n){return function(t){try{var o,r=function(r){return o?r:m(function(){var o="data:application/javascript;base64,"+btoa(n);return Promise.resolve(t.addModule(o)).then(function(){g.set(e,o)})},function(){throw new Error("Failed to load the "+e+" worklet module. Make sure the browser supports AudioWorklets.")})},i=g.get(e);if(i)return Promise.resolve(t.addModule(i));var s=new Blob([n],{type:"application/javascript"}),a=URL.createObjectURL(s),u=m(function(){return Promise.resolve(t.addModule(a)).then(function(){g.set(e,a),o=1})},function(){URL.revokeObjectURL(a)});return Promise.resolve(u&&u.then?u.then(r):r(u))}catch(e){return Promise.reject(e)}}}var b=y("raw-audio-processor",'\nconst BIAS = 0x84;\nconst CLIP = 32635;\nconst encodeTable = [\n 0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,\n 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,\n 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,\n 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,\n 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,\n 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,\n 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,\n 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,\n 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,\n 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,\n 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,\n 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,\n 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,\n 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,\n 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,\n 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7\n];\n\nfunction encodeSample(sample) {\n let sign;\n let exponent;\n let mantissa;\n let muLawSample;\n sign = (sample >> 8) & 0x80;\n if (sign !== 0) sample = -sample;\n sample = sample + BIAS;\n if (sample > CLIP) sample = CLIP;\n exponent = encodeTable[(sample>>7) & 0xFF];\n mantissa = (sample >> (exponent+3)) & 0x0F;\n muLawSample = ~(sign | (exponent << 4) | mantissa);\n \n return muLawSample;\n}\n\nclass RawAudioProcessor extends AudioWorkletProcessor {\n constructor() {\n super();\n \n this.port.onmessage = ({ data }) => {\n switch (data.type) {\n case "setFormat":\n this.isMuted = false;\n this.buffer = []; // Initialize an empty buffer\n this.bufferSize = data.sampleRate / 4;\n this.format = data.format;\n\n if (globalThis.LibSampleRate && sampleRate !== data.sampleRate) {\n globalThis.LibSampleRate.create(1, sampleRate, data.sampleRate).then(resampler => {\n this.resampler = resampler;\n });\n }\n break;\n case "setMuted":\n this.isMuted = data.isMuted;\n break;\n }\n };\n }\n process(inputs) {\n if (!this.buffer) {\n return true;\n }\n \n const input = inputs[0]; // Get the first input node\n if (input.length > 0) {\n let channelData = input[0]; // Get the first channel\'s data\n\n // Resample the audio if necessary\n if (this.resampler) {\n channelData = this.resampler.full(channelData);\n }\n\n // Add channel data to the buffer\n this.buffer.push(...channelData);\n // Get max volume \n let sum = 0.0;\n for (let i = 0; i < channelData.length; i++) {\n sum += channelData[i] * channelData[i];\n }\n const maxVolume = Math.sqrt(sum / channelData.length);\n // Check if buffer size has reached or exceeded the threshold\n if (this.buffer.length >= this.bufferSize) {\n const float32Array = this.isMuted \n ? new Float32Array(this.buffer.length)\n : new Float32Array(this.buffer);\n\n let encodedArray = this.format === "ulaw"\n ? new Uint8Array(float32Array.length)\n : new Int16Array(float32Array.length);\n\n // Iterate through the Float32Array and convert each sample to PCM16\n for (let i = 0; i < float32Array.length; i++) {\n // Clamp the value to the range [-1, 1]\n let sample = Math.max(-1, Math.min(1, float32Array[i]));\n\n // Scale the sample to the range [-32768, 32767]\n let value = sample < 0 ? sample * 32768 : sample * 32767;\n if (this.format === "ulaw") {\n value = encodeSample(Math.round(value));\n }\n\n encodedArray[i] = value;\n }\n\n // Send the buffered data to the main script\n this.port.postMessage([encodedArray, maxVolume]);\n\n // Clear the buffer after sending\n this.buffer = [];\n }\n }\n return true; // Continue processing\n }\n}\nregisterProcessor("raw-audio-processor", RawAudioProcessor);\n');function _(e,n){try{var t=e()}catch(e){return n(e)}return t&&t.then?t.then(void 0,n):t}var k=/*#__PURE__*/function(n){function o(e,t,o,r,i){var s;return void 0===i&&(i={}),(s=n.call(this,i)||this).conversationId=void 0,s.inputFormat=void 0,s.outputFormat=void 0,s.room=void 0,s.isConnected=!1,s.audioEventId=1,s.audioCaptureContext=null,s.audioElements=[],s.room=e,s.conversationId=t,s.inputFormat=o,s.outputFormat=r,s.setupRoomEventListeners(),s}t(o,n),o.create=function(n){try{var t,r=function(r){var i=new e.Room;return _(function(){var r="room_"+Date.now(),s=u("pcm_48000"),a=u("pcm_48000"),c=new o(i,r,s,a,n);return Promise.resolve(i.connect(n.livekitUrl||"wss://livekit.rtc.elevenlabs.io",t)).then(function(){return Promise.resolve(new Promise(function(n){if(c.isConnected)n();else{var t=function(){i.off(e.RoomEvent.Connected,t),n()};i.on(e.RoomEvent.Connected,t)}})).then(function(){var e;return i.name&&(c.conversationId=(null==(e=i.name.match(/(conv_[a-zA-Z0-9]+)/))?void 0:e[0])||i.name),Promise.resolve(i.localParticipant.setMicrophoneEnabled(!0)).then(function(){var e=h(n);return c.debug({type:d,message:e}),Promise.resolve(c.sendMessage(e)).then(function(){return c})})})})},function(e){return Promise.resolve(i.disconnect()).then(function(){throw e})})},i=function(){if(!("conversationToken"in n)||!n.conversationToken)return function(){if("agentId"in n&&n.agentId)return _(function(){var e,o,r,i=(null==(e=n.overrides)||null==(e=e.client)?void 0:e.version)||c,s=(null==(o=n.overrides)||null==(o=o.client)?void 0:o.source)||"js_sdk",a=function(e){return e.replace(/^wss:\/\//,"https://")}(null!=(r=n.origin)?r:"https://api.elevenlabs.io");return Promise.resolve(fetch(a+"/v1/convai/conversation/token?agent_id="+n.agentId+"&source="+s+"&version="+i)).then(function(e){if(!e.ok)throw new Error("ElevenLabs API returned "+e.status+" "+e.statusText);return Promise.resolve(e.json()).then(function(e){if(!(t=e.token))throw new Error("No conversation token received from API")})})},function(e){var t=e instanceof Error?e.message:String(e);throw e instanceof Error&&e.message.includes("401")&&(t="Your agent has authentication enabled, but no signed URL or conversation token was provided."),new Error("Failed to fetch conversation token for agent "+n.agentId+": "+t)});throw new Error("Either conversationToken or agentId is required for WebRTC connection")}();t=n.conversationToken}();return Promise.resolve(i&&i.then?i.then(r):r())}catch(e){return Promise.reject(e)}};var r=o.prototype;return r.setupRoomEventListeners=function(){var n=this,t=this,o=this,r=this;this.room.on(e.RoomEvent.Connected,function(){try{return t.isConnected=!0,console.info("WebRTC room connected"),Promise.resolve()}catch(e){return Promise.reject(e)}}),this.room.on(e.RoomEvent.Disconnected,function(e){n.isConnected=!1,n.disconnect({reason:"agent",context:new CloseEvent("close",{reason:null==e?void 0:e.toString()})})}),this.room.on(e.RoomEvent.ConnectionStateChanged,function(t){t===e.ConnectionState.Disconnected&&(n.isConnected=!1,n.disconnect({reason:"error",message:"LiveKit connection state changed to "+t,context:new Event("connection_state_changed")}))}),this.room.on(e.RoomEvent.DataReceived,function(e,t){try{var o=JSON.parse((new TextDecoder).decode(e));if("audio"===o.type)return;l(o)?n.handleMessage(o):console.warn("Invalid socket event received:",o)}catch(n){console.warn("Failed to parse incoming data message:",n),console.warn("Raw payload:",(new TextDecoder).decode(e))}}),this.room.on(e.RoomEvent.TrackSubscribed,function(n,t,r){try{var i=function(){if(n.kind===e.Track.Kind.Audio&&r.identity.includes("agent")){var t=n,i=t.attach();return i.autoplay=!0,i.controls=!1,i.style.display="none",document.body.appendChild(i),o.audioElements.push(i),1===o.audioElements.length&&(null==o.onDebug||o.onDebug({type:"audio_element_ready"})),Promise.resolve(o.setupAudioCapture(t)).then(function(){})}}();return Promise.resolve(i&&i.then?i.then(function(){}):void 0)}catch(e){return Promise.reject(e)}}),this.room.on(e.RoomEvent.ActiveSpeakersChanged,function(e){try{return r.updateMode(e.length>0&&e[0].identity.startsWith("agent")?"speaking":"listening"),Promise.resolve()}catch(e){return Promise.reject(e)}})},r.close=function(){if(this.isConnected){try{this.room.localParticipant.audioTrackPublications.forEach(function(e){e.track&&e.track.stop()})}catch(e){console.warn("Error stopping local tracks:",e)}this.audioCaptureContext&&(this.audioCaptureContext.close().catch(function(e){console.warn("Error closing audio capture context:",e)}),this.audioCaptureContext=null),this.audioElements.forEach(function(e){e.parentNode&&e.parentNode.removeChild(e)}),this.audioElements=[],this.room.disconnect()}},r.sendMessage=function(e){try{var n=this;if(!n.isConnected||!n.room.localParticipant)return console.warn("Cannot send message: room not connected or no local participant"),Promise.resolve();if("user_audio_chunk"in e)return Promise.resolve();var t=_(function(){var t=(new TextEncoder).encode(JSON.stringify(e));return Promise.resolve(n.room.localParticipant.publishData(t,{reliable:!0})).then(function(){})},function(t){n.debug({type:"send_message_error",message:{message:e,error:t}}),console.error("Failed to send message via WebRTC:",t)});return Promise.resolve(t&&t.then?t.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},r.getRoom=function(){return this.room},r.setMicMuted=function(n){try{var t=this;if(!t.isConnected||!t.room.localParticipant)return console.warn("Cannot set microphone muted: room not connected or no local participant"),Promise.resolve();var o=t.room.localParticipant.getTrackPublication(e.Track.Source.Microphone);return Promise.resolve(null!=o&&o.track?_(function(){var e=n?Promise.resolve(o.track.mute()).then(function(){}):Promise.resolve(o.track.unmute()).then(function(){});if(e&&e.then)return e.then(function(){})},function(){return Promise.resolve(t.room.localParticipant.setMicrophoneEnabled(!n)).then(function(){})}):Promise.resolve(t.room.localParticipant.setMicrophoneEnabled(!n)).then(function(){}))}catch(e){return Promise.reject(e)}},r.setupAudioCapture=function(e){try{var n=this,t=_(function(){var t=new AudioContext;n.audioCaptureContext=t;var o=new MediaStream([e.mediaStreamTrack]),r=t.createMediaStreamSource(o);return Promise.resolve(b(t.audioWorklet)).then(function(){var e=new AudioWorkletNode(t,"raw-audio-processor");e.port.postMessage({type:"setFormat",format:n.outputFormat.format,sampleRate:n.outputFormat.sampleRate}),e.port.onmessage=function(e){var t=e.data;if(t[1]>.01){var o=f(t[0].buffer),r=n.audioEventId++;n.handleMessage({type:"audio",audio_event:{audio_base_64:o,event_id:r}})}},r.connect(e)})},function(e){console.warn("Failed to set up audio capture:",e)});return Promise.resolve(t&&t.then?t.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},r.setAudioVolume=function(e){this.audioElements.forEach(function(n){n.volume=e})},o}(a),P=function(e){try{var n=function(e){return e.connectionType?e.connectionType:"conversationToken"in e&&e.conversationToken?"webrtc":"websocket"}(e);switch(n){case"websocket":return Promise.resolve(p.create(e));case"webrtc":return Promise.resolve(k.create(e));default:throw new Error("Unknown connection type: "+n)}}catch(e){return Promise.reject(e)}};function w(){return["iPad Simulator","iPhone Simulator","iPod Simulator","iPad","iPhone","iPod"].includes(navigator.platform)||navigator.userAgent.includes("Mac")&&"ontouchend"in document}var C=function(e){void 0===e&&(e={default:0,android:3e3});try{var n,t=e.default;if(/android/i.test(navigator.userAgent))t=null!=(n=e.android)?n:t;else if(w()){var o;t=null!=(o=e.ios)?o:t}var r=function(){if(t>0)return Promise.resolve(new Promise(function(e){return setTimeout(e,t)})).then(function(){})}();return Promise.resolve(r&&r.then?r.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},S=/*#__PURE__*/function(e){function n(){return e.apply(this,arguments)||this}return t(n,e),n.startSession=function(e){try{var t=s.getFullOptions(e);t.onStatusChange&&t.onStatusChange({status:"connecting"}),t.onCanSendFeedbackChange&&t.onCanSendFeedbackChange({canSendFeedback:!1}),t.onModeChange&&t.onModeChange({mode:"listening"}),t.onCanSendFeedbackChange&&t.onCanSendFeedbackChange({canSendFeedback:!1});var o=null;return Promise.resolve(function(r,i){try{var s=Promise.resolve(C(t.connectionDelay)).then(function(){return Promise.resolve(P(e)).then(function(e){return new n(t,o=e)})})}catch(e){return i(e)}return s&&s.then?s.then(void 0,i):s}(0,function(e){var n;throw t.onStatusChange&&t.onStatusChange({status:"disconnected"}),null==(n=o)||n.close(),e}))}catch(e){return Promise.reject(e)}},n}(s),M=/*#__PURE__*/function(){function e(e,n,t,o){this.context=void 0,this.analyser=void 0,this.worklet=void 0,this.inputStream=void 0,this.context=e,this.analyser=n,this.worklet=t,this.inputStream=o}e.create=function(t){var o=t.sampleRate,r=t.format,i=t.preferHeadphonesForIosDevices,s=t.inputDeviceId;try{var a=null,u=null;return Promise.resolve(function(t,c){try{var l=function(){function t(){function t(){return Promise.resolve(b(a.audioWorklet)).then(function(){var t=n({voiceIsolation:!0},c);return Promise.resolve(navigator.mediaDevices.getUserMedia({audio:t})).then(function(n){var t=a.createMediaStreamSource(u=n),i=new AudioWorkletNode(a,"raw-audio-processor");return i.port.postMessage({type:"setFormat",format:r,sampleRate:o}),t.connect(l),l.connect(i),Promise.resolve(a.resume()).then(function(){return new e(a,l,i,u)})})})}s&&(c.deviceId={exact:s});var i=navigator.mediaDevices.getSupportedConstraints().sampleRate,l=(a=new window.AudioContext(i?{sampleRate:o}:{})).createAnalyser(),d=function(){if(!i)return Promise.resolve(a.audioWorklet.addModule("https://cdn.jsdelivr.net/npm/@alexanderolsen/libsamplerate-js@2.1.2/dist/libsamplerate.worklet.js")).then(function(){})}();return d&&d.then?d.then(t):t()}var c={sampleRate:{ideal:o},echoCancellation:!0,noiseSuppression:!0},l=function(){if(w()&&i)return Promise.resolve(window.navigator.mediaDevices.enumerateDevices()).then(function(e){var n=e.find(function(e){return"audioinput"===e.kind&&["airpod","headphone","earphone"].find(function(n){return e.label.toLowerCase().includes(n)})});n&&(c.deviceId={ideal:n.deviceId})})}();return l&&l.then?l.then(t):t()}()}catch(e){return c(e)}return l&&l.then?l.then(void 0,c):l}(0,function(e){var n,t;throw null==(n=u)||n.getTracks().forEach(function(e){return e.stop()}),null==(t=a)||t.close(),e}))}catch(e){return Promise.reject(e)}};var t=e.prototype;return t.close=function(){try{return this.inputStream.getTracks().forEach(function(e){return e.stop()}),Promise.resolve(this.context.close()).then(function(){})}catch(e){return Promise.reject(e)}},t.setMuted=function(e){this.worklet.port.postMessage({type:"setMuted",isMuted:e})},e}(),I=y("audio-concat-processor",'\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 = []; // Initialize an empty buffer\n this.cursor = 0;\n this.currentBuffer = null;\n this.wasInterrupted = false;\n this.finished = false;\n \n this.port.onmessage = ({ data }) => {\n switch (data.type) {\n case "setFormat":\n this.format = data.format;\n break;\n case "buffer":\n this.wasInterrupted = false;\n this.buffers.push(\n this.format === "ulaw"\n ? new Uint8Array(data.buffer)\n : new Int16Array(data.buffer)\n );\n break;\n case "interrupt":\n this.wasInterrupted = true;\n break;\n case "clearInterrupted":\n if (this.wasInterrupted) {\n this.wasInterrupted = false;\n this.buffers = [];\n this.currentBuffer = null;\n }\n }\n };\n }\n process(_, outputs) {\n let finished = false;\n const output = outputs[0][0];\n for (let i = 0; i < output.length; i++) {\n if (!this.currentBuffer) {\n if (this.buffers.length === 0) {\n finished = true;\n break;\n }\n this.currentBuffer = this.buffers.shift();\n this.cursor = 0;\n }\n\n let value = this.currentBuffer[this.cursor];\n if (this.format === "ulaw") {\n value = decodeSample(value);\n }\n output[i] = value / 32768;\n this.cursor++;\n\n if (this.cursor >= this.currentBuffer.length) {\n this.currentBuffer = null;\n }\n }\n\n if (this.finished !== finished) {\n this.finished = finished;\n this.port.postMessage({ type: "process", finished });\n }\n\n return true; // Continue processing\n }\n}\n\nregisterProcessor("audio-concat-processor", AudioConcatProcessor);\n'),D=/*#__PURE__*/function(){function e(e,n,t,o,r,i){this.context=void 0,this.analyser=void 0,this.gain=void 0,this.worklet=void 0,this.audioElement=void 0,this.audioSource=void 0,this.context=e,this.analyser=n,this.gain=t,this.worklet=o,this.audioElement=r,this.audioSource=i}return e.create=function(n){var t=n.sampleRate,o=n.format,r=n.outputDeviceId;try{var i=null,s=null,a=null;return Promise.resolve(function(n,u){try{var c=(l=(i=new AudioContext({sampleRate:t})).createAnalyser(),(d=i.createGain()).connect(l),l.connect(i.destination),r&&((s=new Audio).src="",s.load(),(a=i.createMediaElementSource(s)).connect(d)),Promise.resolve(I(i.audioWorklet)).then(function(){var n=new AudioWorkletNode(i,"audio-concat-processor");return n.port.postMessage({type:"setFormat",format:o}),n.connect(d),Promise.resolve(i.resume()).then(function(){function t(){return new e(i,l,d,n,s,a)}var o=function(e){if(r&&null!=(e=s)&&e.setSinkId)return Promise.resolve(s.setSinkId(r)).then(function(){})}();return o&&o.then?o.then(t):t()})}))}catch(e){return u(e)}var l,d;return c&&c.then?c.then(void 0,u):c}(0,function(e){var n,t;function o(){throw e}null==(n=a)||n.disconnect(),null==(t=s)||t.pause();var r=function(){if(i&&"closed"!==i.state)return Promise.resolve(i.close()).then(function(){})}();return r&&r.then?r.then(o):o()}))}catch(e){return Promise.reject(e)}},e.prototype.close=function(){try{var e,n,t=this;return null==(e=t.audioSource)||e.disconnect(),null==(n=t.audioElement)||n.pause(),Promise.resolve(t.context.close()).then(function(){})}catch(e){return Promise.reject(e)}},e}();function F(e,n){try{var t=e()}catch(e){return n(e)}return t&&t.then?t.then(void 0,n):t}var E=/*#__PURE__*/function(e){function o(n,t,o,r,i){var s;return(s=e.call(this,n,t)||this).input=void 0,s.output=void 0,s.wakeLock=void 0,s.inputFrequencyData=void 0,s.outputFrequencyData=void 0,s.onInputWorkletMessage=function(e){"connected"===s.status&&s.connection.sendMessage({user_audio_chunk:f(e.data[0].buffer)})},s.onOutputWorkletMessage=function(e){var n=e.data;"process"===n.type&&s.updateMode(n.finished?"listening":"speaking")},s.addAudioBase64Chunk=function(e){s.output.gain.gain.value=s.volume,s.output.worklet.port.postMessage({type:"clearInterrupted"}),s.output.worklet.port.postMessage({type:"buffer",buffer:v(e)})},s.fadeOutAudio=function(){s.updateMode("listening"),s.output.worklet.port.postMessage({type:"interrupt"}),s.output.gain.gain.exponentialRampToValueAtTime(1e-4,s.output.context.currentTime+2),setTimeout(function(){s.output.gain.gain.value=s.volume,s.output.worklet.port.postMessage({type:"clearInterrupted"})},2e3)},s.calculateVolume=function(e){if(0===e.length)return 0;for(var n=0,t=0;t<e.length;t++)n+=e[t]/255;return(n/=e.length)<0?0:n>1?1:n},s.setVolume=function(e){var n=e.volume,t=Number.isFinite(n)?Math.min(1,Math.max(0,n)):1;s.volume=t,s.connection instanceof k?s.connection.setAudioVolume(t):s.output.gain.gain.value=t},s.input=o,s.output=r,s.wakeLock=i,s.input.worklet.port.onmessage=s.onInputWorkletMessage,s.output.worklet.port.onmessage=s.onOutputWorkletMessage,s}t(o,e),o.startSession=function(e){try{var t=function(){return F(function(){return Promise.resolve(navigator.mediaDevices.getUserMedia({audio:!0})).then(function(t){return c=t,Promise.resolve(C(r.connectionDelay)).then(function(){return Promise.resolve(P(e)).then(function(t){return a=t,Promise.resolve(Promise.all([M.create(n({},a.inputFormat,{preferHeadphonesForIosDevices:e.preferHeadphonesForIosDevices,inputDeviceId:e.inputDeviceId})),D.create(n({},a.outputFormat,{outputDeviceId:e.outputDeviceId}))])).then(function(e){var n;return i=e[0],u=e[1],null==(n=c)||n.getTracks().forEach(function(e){return e.stop()}),c=null,new o(r,a,i,u,l)})})})})},function(e){var n,t,o;return r.onStatusChange&&r.onStatusChange({status:"disconnected"}),null==(n=c)||n.getTracks().forEach(function(e){return e.stop()}),null==(t=a)||t.close(),Promise.resolve(null==(o=i)?void 0:o.close()).then(function(){var n;return Promise.resolve(null==(n=u)?void 0:n.close()).then(function(){function n(){throw e}var t=F(function(){var e;return Promise.resolve(null==(e=l)?void 0:e.release()).then(function(){l=null})},function(){});return t&&t.then?t.then(n):n()})})})},r=s.getFullOptions(e);r.onStatusChange&&r.onStatusChange({status:"connecting"}),r.onCanSendFeedbackChange&&r.onCanSendFeedbackChange({canSendFeedback:!1});var i=null,a=null,u=null,c=null,l=null,d=function(n){if(null==(n=e.useWakeLock)||n){var t=F(function(){return Promise.resolve(navigator.wakeLock.request("screen")).then(function(e){l=e})},function(){});if(t&&t.then)return t.then(function(){})}}();return Promise.resolve(d&&d.then?d.then(t):t())}catch(e){return Promise.reject(e)}};var r=o.prototype;return r.handleEndSession=function(){try{var n=this;return Promise.resolve(e.prototype.handleEndSession.call(n)).then(function(){function e(){return Promise.resolve(n.input.close()).then(function(){return Promise.resolve(n.output.close()).then(function(){})})}var t=F(function(){var e;return Promise.resolve(null==(e=n.wakeLock)?void 0:e.release()).then(function(){n.wakeLock=null})},function(){});return t&&t.then?t.then(e):e()})}catch(e){return Promise.reject(e)}},r.handleInterruption=function(n){e.prototype.handleInterruption.call(this,n),this.fadeOutAudio()},r.handleAudio=function(e){var n,t;this.lastInterruptTimestamp<=e.audio_event.event_id&&(null==(n=(t=this.options).onAudio)||n.call(t,e.audio_event.audio_base_64),this.connection instanceof k||this.addAudioBase64Chunk(e.audio_event.audio_base_64),this.currentEventId=e.audio_event.event_id,this.updateCanSendFeedback(),this.updateMode("speaking"))},r.setMicMuted=function(e){this.connection instanceof k?this.connection.setMicMuted(e):this.input.setMuted(e)},r.getInputByteFrequencyData=function(){return null!=this.inputFrequencyData||(this.inputFrequencyData=new Uint8Array(this.input.analyser.frequencyBinCount)),this.input.analyser.getByteFrequencyData(this.inputFrequencyData),this.inputFrequencyData},r.getOutputByteFrequencyData=function(){return null!=this.outputFrequencyData||(this.outputFrequencyData=new Uint8Array(this.output.analyser.frequencyBinCount)),this.output.analyser.getByteFrequencyData(this.outputFrequencyData),this.outputFrequencyData},r.getInputVolume=function(){return this.calculateVolume(this.getInputByteFrequencyData())},r.getOutputVolume=function(){return this.calculateVolume(this.getOutputByteFrequencyData())},r.changeInputDevice=function(e){var n=e.sampleRate,t=e.format,o=e.preferHeadphonesForIosDevices,r=e.inputDeviceId;try{var i=this;return Promise.resolve(F(function(){return Promise.resolve(i.input.close()).then(function(){return Promise.resolve(M.create({sampleRate:n,format:t,preferHeadphonesForIosDevices:o,inputDeviceId:r})).then(function(e){return i.input=e,i.input})})},function(e){throw console.error("Error changing input device",e),e}))}catch(e){return Promise.reject(e)}},r.changeOutputDevice=function(e){var n=e.sampleRate,t=e.format,o=e.outputDeviceId;try{var r=this;return Promise.resolve(F(function(){return Promise.resolve(r.output.close()).then(function(){return Promise.resolve(D.create({sampleRate:n,format:t,outputDeviceId:o})).then(function(e){return r.output=e,r.output})})},function(e){throw console.error("Error changing output device",e),e}))}catch(e){return Promise.reject(e)}},o}(s);exports.Conversation=/*#__PURE__*/function(e){function n(){return e.apply(this,arguments)||this}return t(n,e),n.startSession=function(e){return e.textOnly?S.startSession(e):E.startSession(e)},n}(s),exports.Input=M,exports.Output=D,exports.VoiceConversation=E,exports.WebRTCConnection=k,exports.WebSocketConnection=p,exports.createConnection=P,exports.postOverallFeedback=function(e,n,t){return void 0===t&&(t="https://api.elevenlabs.io"),fetch(t+"/v1/convai/conversations/"+e+"/feedback",{method:"POST",body:JSON.stringify({feedback:n?"like":"dislike"}),headers:{"Content-Type":"application/json"}})};
1
+ var e=require("livekit-client");function n(){return n=Object.assign?Object.assign.bind():function(e){for(var n=1;n<arguments.length;n++){var t=arguments[n];for(var o in t)({}).hasOwnProperty.call(t,o)&&(e[o]=t[o])}return e},n.apply(null,arguments)}function t(e,n){e.prototype=Object.create(n.prototype),e.prototype.constructor=e,o(e,n)}function o(e,n){return o=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,n){return e.__proto__=n,e},o(e,n)}function r(e,n){try{var t=e()}catch(e){return n(e)}return t&&t.then?t.then(void 0,n):t}var i=new Uint8Array(0),a=/*#__PURE__*/function(){function e(e,n){var t=this,o=this,i=this;this.options=void 0,this.connection=void 0,this.lastInterruptTimestamp=0,this.mode="listening",this.status="connecting",this.volume=1,this.currentEventId=1,this.lastFeedbackEventId=0,this.canSendFeedback=!1,this.endSessionWithDetails=function(e){try{return"connected"!==o.status&&"connecting"!==o.status?Promise.resolve():(o.updateStatus("disconnecting"),Promise.resolve(o.handleEndSession()).then(function(){o.updateStatus("disconnected"),o.options.onDisconnect&&o.options.onDisconnect(e)}))}catch(e){return Promise.reject(e)}},this.onMessage=function(e){try{switch(e.type){case"interruption":return i.handleInterruption(e),Promise.resolve();case"agent_response":return i.handleAgentResponse(e),Promise.resolve();case"user_transcript":return i.handleUserTranscript(e),Promise.resolve();case"internal_tentative_agent_response":return i.handleTentativeAgentResponse(e),Promise.resolve();case"client_tool_call":var n=r(function(){return Promise.resolve(i.handleClientToolCall(e)).then(function(){})},function(n){i.onError("Unexpected error in client tool call handling: "+(n instanceof Error?n.message:String(n)),{clientToolName:e.client_tool_call.tool_name,toolCallId:e.client_tool_call.tool_call_id})});return Promise.resolve(n&&n.then?n.then(function(){}):void 0);case"audio":return i.handleAudio(e),Promise.resolve();case"vad_score":return i.handleVadScore(e),Promise.resolve();case"ping":return i.connection.sendMessage({type:"pong",event_id:e.ping_event.event_id}),Promise.resolve();default:return i.options.onDebug&&i.options.onDebug(e),Promise.resolve()}}catch(e){return Promise.reject(e)}},this.setVolume=function(e){t.volume=e.volume},this.options=e,this.connection=n,this.options.onConnect&&this.options.onConnect({conversationId:n.conversationId}),this.connection.onMessage(this.onMessage),this.connection.onDisconnect(this.endSessionWithDetails),this.connection.onModeChange(function(e){return t.updateMode(e)}),this.updateStatus("connected")}e.getFullOptions=function(e){return n({clientTools:{},onConnect:function(){},onDebug:function(){},onDisconnect:function(){},onError:function(){},onMessage:function(){},onAudio:function(){},onModeChange:function(){},onStatusChange:function(){},onCanSendFeedbackChange:function(){}},e)};var t=e.prototype;return t.endSession=function(){return this.endSessionWithDetails({reason:"user"})},t.handleEndSession=function(){try{return this.connection.close(),Promise.resolve()}catch(e){return Promise.reject(e)}},t.updateMode=function(e){e!==this.mode&&(this.mode=e,this.options.onModeChange&&this.options.onModeChange({mode:e}))},t.updateStatus=function(e){e!==this.status&&(this.status=e,this.options.onStatusChange&&this.options.onStatusChange({status:e}))},t.updateCanSendFeedback=function(){var e=this.currentEventId!==this.lastFeedbackEventId;this.canSendFeedback!==e&&(this.canSendFeedback=e,this.options.onCanSendFeedbackChange&&this.options.onCanSendFeedbackChange({canSendFeedback:e}))},t.handleInterruption=function(e){e.interruption_event&&(this.lastInterruptTimestamp=e.interruption_event.event_id)},t.handleAgentResponse=function(e){this.options.onMessage&&this.options.onMessage({source:"ai",message:e.agent_response_event.agent_response})},t.handleUserTranscript=function(e){this.options.onMessage&&this.options.onMessage({source:"user",message:e.user_transcription_event.user_transcript})},t.handleTentativeAgentResponse=function(e){this.options.onDebug&&this.options.onDebug({type:"tentative_agent_response",response:e.tentative_agent_response_internal_event.tentative_agent_response})},t.handleVadScore=function(e){this.options.onVadScore&&this.options.onVadScore({vadScore:e.vad_score_event.vad_score})},t.handleClientToolCall=function(e){try{var n=this;return Promise.resolve(function(){if(Object.prototype.hasOwnProperty.call(n.options.clientTools,e.client_tool_call.tool_name)){var t=r(function(){return Promise.resolve(n.options.clientTools[e.client_tool_call.tool_name](e.client_tool_call.parameters)).then(function(t){var o="object"==typeof t?JSON.stringify(t):String(t);n.connection.sendMessage({type:"client_tool_result",tool_call_id:e.client_tool_call.tool_call_id,result:o,is_error:!1})})},function(t){n.onError("Client tool execution failed with following error: "+(null==t?void 0:t.message),{clientToolName:e.client_tool_call.tool_name}),n.connection.sendMessage({type:"client_tool_result",tool_call_id:e.client_tool_call.tool_call_id,result:"Client tool execution failed: "+(null==t?void 0:t.message),is_error:!0})});if(t&&t.then)return t.then(function(){})}else{if(n.options.onUnhandledClientToolCall)return void n.options.onUnhandledClientToolCall(e.client_tool_call);n.onError("Client tool with name "+e.client_tool_call.tool_name+" is not defined on client",{clientToolName:e.client_tool_call.tool_name}),n.connection.sendMessage({type:"client_tool_result",tool_call_id:e.client_tool_call.tool_call_id,result:"Client tool with name "+e.client_tool_call.tool_name+" is not defined on client",is_error:!0})}}())}catch(e){return Promise.reject(e)}},t.handleAudio=function(e){},t.onError=function(e,n){console.error(e,n),this.options.onError&&this.options.onError(e,n)},t.getId=function(){return this.connection.conversationId},t.isOpen=function(){return"connected"===this.status},t.setMicMuted=function(e){this.connection.setMicMuted(e)},t.getInputByteFrequencyData=function(){return i},t.getOutputByteFrequencyData=function(){return i},t.getInputVolume=function(){return 0},t.getOutputVolume=function(){return 0},t.sendFeedback=function(e){this.canSendFeedback?(this.connection.sendMessage({type:"feedback",score:e?"like":"dislike",event_id:this.currentEventId}),this.lastFeedbackEventId=this.currentEventId,this.updateCanSendFeedback()):console.warn(0===this.lastFeedbackEventId?"Cannot send feedback: the conversation has not started yet.":"Cannot send feedback: feedback has already been sent for the current response.")},t.sendContextualUpdate=function(e){this.connection.sendMessage({type:"contextual_update",text:e})},t.sendUserMessage=function(e){this.connection.sendMessage({type:"user_message",text:e})},t.sendUserActivity=function(){this.connection.sendMessage({type:"user_activity"})},t.sendMCPToolApprovalResult=function(e,n){this.connection.sendMessage({type:"mcp_tool_approval_result",tool_call_id:e,is_approved:n})},e}(),s=/*#__PURE__*/function(){function e(e){void 0===e&&(e={}),this.queue=[],this.disconnectionDetails=null,this.onDisconnectCallback=null,this.onMessageCallback=null,this.onModeChangeCallback=null,this.onDebug=void 0,this.onDebug=e.onDebug}var n=e.prototype;return n.debug=function(e){this.onDebug&&this.onDebug(e)},n.onMessage=function(e){this.onMessageCallback=e;var n=this.queue;this.queue=[],n.length>0&&queueMicrotask(function(){n.forEach(e)})},n.onDisconnect=function(e){this.onDisconnectCallback=e;var n=this.disconnectionDetails;n&&queueMicrotask(function(){e(n)})},n.onModeChange=function(e){this.onModeChangeCallback=e},n.updateMode=function(e){var n;null==(n=this.onModeChangeCallback)||n.call(this,e)},n.disconnect=function(e){var n;this.disconnectionDetails||(this.disconnectionDetails=e,null==(n=this.onDisconnectCallback)||n.call(this,e))},n.handleMessage=function(e){this.onMessageCallback?this.onMessageCallback(e):this.queue.push(e)},e}();function u(e){var n=e.split("_"),t=n[0],o=n[1];if(!["pcm","ulaw"].includes(t))throw new Error("Invalid format: "+e);var r=Number.parseInt(o);if(Number.isNaN(r))throw new Error("Invalid sample rate: "+o);return{format:t,sampleRate:r}}var c="0.6.2";function l(e){return!!e.type}var d="conversation_initiation_client_data";function h(e){var n,t,o,r,i,a,s={type:d};return e.overrides&&(s.conversation_config_override={agent:{prompt:null==(t=e.overrides.agent)?void 0:t.prompt,first_message:null==(o=e.overrides.agent)?void 0:o.firstMessage,language:null==(r=e.overrides.agent)?void 0:r.language},tts:{voice_id:null==(i=e.overrides.tts)?void 0:i.voiceId},conversation:{text_only:null==(a=e.overrides.conversation)?void 0:a.textOnly}}),e.customLlmExtraBody&&(s.custom_llm_extra_body=e.customLlmExtraBody),e.dynamicVariables&&(s.dynamic_variables=e.dynamicVariables),e.userId&&(s.user_id=e.userId),null!=(n=e.overrides)&&n.client&&(s.source_info={source:e.overrides.client.source,version:e.overrides.client.version}),s}var f=/*#__PURE__*/function(e){function n(n,t,o,r){var i;return(i=e.call(this)||this).socket=void 0,i.conversationId=void 0,i.inputFormat=void 0,i.outputFormat=void 0,i.socket=n,i.conversationId=t,i.inputFormat=o,i.outputFormat=r,i.socket.addEventListener("error",function(e){setTimeout(function(){return i.disconnect({reason:"error",message:"The connection was closed due to a socket error.",context:e})},0)}),i.socket.addEventListener("close",function(e){i.disconnect(1e3===e.code?{reason:"agent",context:e}:{reason:"error",message:e.reason||"The connection was closed by the server.",context:e})}),i.socket.addEventListener("message",function(e){try{var n=JSON.parse(e.data);if(!l(n))return void i.debug({type:"invalid_event",message:"Received invalid socket event",data:e.data});i.handleMessage(n)}catch(n){i.debug({type:"parsing_error",message:"Failed to parse socket message",error:n instanceof Error?n.message:String(n),data:e.data})}}),i}t(n,e),n.create=function(e){try{var t=null;return Promise.resolve(function(o,r){try{var i=function(){var o,r,i,a,s=null!=(o=e.origin)?o:"wss://api.elevenlabs.io",d=(null==(r=e.overrides)||null==(r=r.client)?void 0:r.version)||c,f=(null==(i=e.overrides)||null==(i=i.client)?void 0:i.source)||"js_sdk";if(e.signedUrl){var p=e.signedUrl.includes("?")?"&":"?";a=""+e.signedUrl+p+"source="+f+"&version="+d}else a=s+"/v1/convai/conversation?agent_id="+e.agentId+"&source="+f+"&version="+d;var v=["convai"];return e.authorization&&v.push("bearer."+e.authorization),t=new WebSocket(a,v),Promise.resolve(new Promise(function(n,o){t.addEventListener("open",function(){var n,o=h(e);null==(n=t)||n.send(JSON.stringify(o))},{once:!0}),t.addEventListener("error",function(e){setTimeout(function(){return o(e)},0)}),t.addEventListener("close",o),t.addEventListener("message",function(e){var t=JSON.parse(e.data);l(t)&&("conversation_initiation_metadata"===t.type?n(t.conversation_initiation_metadata_event):console.warn("First received message is not conversation metadata."))},{once:!0})})).then(function(e){var o=e.conversation_id,r=e.agent_output_audio_format,i=e.user_input_audio_format,a=u(null!=i?i:"pcm_16000"),s=u(r);return new n(t,o,a,s)})}()}catch(e){return r(e)}return i&&i.then?i.then(void 0,r):i}(0,function(e){var n;throw null==(n=t)||n.close(),e}))}catch(e){return Promise.reject(e)}};var o=n.prototype;return o.close=function(){this.socket.close()},o.sendMessage=function(e){this.socket.send(JSON.stringify(e))},o.setMicMuted=function(e){try{return console.warn("WebSocket connection setMicMuted called with "+e+", but this is handled by VoiceConversation"),Promise.resolve()}catch(e){return Promise.reject(e)}},n}(s);function p(e){var n=new Uint8Array(e);return window.btoa(String.fromCharCode.apply(String,n))}function v(e){for(var n=window.atob(e),t=n.length,o=new Uint8Array(t),r=0;r<t;r++)o[r]=n.charCodeAt(r);return o.buffer}function m(e,n){try{var t=e()}catch(e){return n(e)}return t&&t.then?t.then(void 0,n):t}var g=new Map;function y(e,n){return function(t){try{var o,r=function(r){return o?r:m(function(){var o="data:application/javascript;base64,"+btoa(n);return Promise.resolve(t.addModule(o)).then(function(){g.set(e,o)})},function(){throw new Error("Failed to load the "+e+" worklet module. Make sure the browser supports AudioWorklets.")})},i=g.get(e);if(i)return Promise.resolve(t.addModule(i));var a=new Blob([n],{type:"application/javascript"}),s=URL.createObjectURL(a),u=m(function(){return Promise.resolve(t.addModule(s)).then(function(){g.set(e,s),o=1})},function(){URL.revokeObjectURL(s)});return Promise.resolve(u&&u.then?u.then(r):r(u))}catch(e){return Promise.reject(e)}}}var P=y("raw-audio-processor",'\nconst BIAS = 0x84;\nconst CLIP = 32635;\nconst encodeTable = [\n 0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,\n 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,\n 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,\n 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,\n 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,\n 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,\n 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,\n 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,\n 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,\n 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,\n 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,\n 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,\n 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,\n 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,\n 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,\n 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7\n];\n\nfunction encodeSample(sample) {\n let sign;\n let exponent;\n let mantissa;\n let muLawSample;\n sign = (sample >> 8) & 0x80;\n if (sign !== 0) sample = -sample;\n sample = sample + BIAS;\n if (sample > CLIP) sample = CLIP;\n exponent = encodeTable[(sample>>7) & 0xFF];\n mantissa = (sample >> (exponent+3)) & 0x0F;\n muLawSample = ~(sign | (exponent << 4) | mantissa);\n \n return muLawSample;\n}\n\nclass RawAudioProcessor extends AudioWorkletProcessor {\n constructor() {\n super();\n \n this.port.onmessage = ({ data }) => {\n switch (data.type) {\n case "setFormat":\n this.isMuted = false;\n this.buffer = []; // Initialize an empty buffer\n this.bufferSize = data.sampleRate / 4;\n this.format = data.format;\n\n if (globalThis.LibSampleRate && sampleRate !== data.sampleRate) {\n globalThis.LibSampleRate.create(1, sampleRate, data.sampleRate).then(resampler => {\n this.resampler = resampler;\n });\n }\n break;\n case "setMuted":\n this.isMuted = data.isMuted;\n break;\n }\n };\n }\n process(inputs) {\n if (!this.buffer) {\n return true;\n }\n \n const input = inputs[0]; // Get the first input node\n if (input.length > 0) {\n let channelData = input[0]; // Get the first channel\'s data\n\n // Resample the audio if necessary\n if (this.resampler) {\n channelData = this.resampler.full(channelData);\n }\n\n // Add channel data to the buffer\n this.buffer.push(...channelData);\n // Get max volume \n let sum = 0.0;\n for (let i = 0; i < channelData.length; i++) {\n sum += channelData[i] * channelData[i];\n }\n const maxVolume = Math.sqrt(sum / channelData.length);\n // Check if buffer size has reached or exceeded the threshold\n if (this.buffer.length >= this.bufferSize) {\n const float32Array = this.isMuted \n ? new Float32Array(this.buffer.length)\n : new Float32Array(this.buffer);\n\n let encodedArray = this.format === "ulaw"\n ? new Uint8Array(float32Array.length)\n : new Int16Array(float32Array.length);\n\n // Iterate through the Float32Array and convert each sample to PCM16\n for (let i = 0; i < float32Array.length; i++) {\n // Clamp the value to the range [-1, 1]\n let sample = Math.max(-1, Math.min(1, float32Array[i]));\n\n // Scale the sample to the range [-32768, 32767]\n let value = sample < 0 ? sample * 32768 : sample * 32767;\n if (this.format === "ulaw") {\n value = encodeSample(Math.round(value));\n }\n\n encodedArray[i] = value;\n }\n\n // Send the buffered data to the main script\n this.port.postMessage([encodedArray, maxVolume]);\n\n // Clear the buffer after sending\n this.buffer = [];\n }\n }\n return true; // Continue processing\n }\n}\nregisterProcessor("raw-audio-processor", RawAudioProcessor);\n');function k(e,n){try{var t=e()}catch(e){return n(e)}return t&&t.then?t.then(void 0,n):t}var b=/*#__PURE__*/function(n){function o(e,t,o,r,i){var a;return void 0===i&&(i={}),(a=n.call(this,i)||this).conversationId=void 0,a.inputFormat=void 0,a.outputFormat=void 0,a.room=void 0,a.isConnected=!1,a.audioEventId=1,a.audioCaptureContext=null,a.audioElements=[],a.outputDeviceId=null,a.outputAnalyser=null,a.outputFrequencyData=null,a.room=e,a.conversationId=t,a.inputFormat=o,a.outputFormat=r,a.setupRoomEventListeners(),a}t(o,n),o.create=function(n){try{var t,r=function(r){var i=new e.Room;return k(function(){var r="room_"+Date.now(),a=u("pcm_48000"),s=u("pcm_48000"),c=new o(i,r,a,s,n);return Promise.resolve(i.connect(n.livekitUrl||"wss://livekit.rtc.elevenlabs.io",t)).then(function(){return Promise.resolve(new Promise(function(n){if(c.isConnected)n();else{var t=function(){i.off(e.RoomEvent.Connected,t),n()};i.on(e.RoomEvent.Connected,t)}})).then(function(){var e;return i.name&&(c.conversationId=(null==(e=i.name.match(/(conv_[a-zA-Z0-9]+)/))?void 0:e[0])||i.name),Promise.resolve(i.localParticipant.setMicrophoneEnabled(!0)).then(function(){var e=h(n);return c.debug({type:d,message:e}),Promise.resolve(c.sendMessage(e)).then(function(){return c})})})})},function(e){return Promise.resolve(i.disconnect()).then(function(){throw e})})},i=function(){if(!("conversationToken"in n)||!n.conversationToken)return function(){if("agentId"in n&&n.agentId)return k(function(){var e,o,r,i=(null==(e=n.overrides)||null==(e=e.client)?void 0:e.version)||c,a=(null==(o=n.overrides)||null==(o=o.client)?void 0:o.source)||"js_sdk",s=function(e){return e.replace(/^wss:\/\//,"https://")}(null!=(r=n.origin)?r:"https://api.elevenlabs.io");return Promise.resolve(fetch(s+"/v1/convai/conversation/token?agent_id="+n.agentId+"&source="+a+"&version="+i)).then(function(e){if(!e.ok)throw new Error("ElevenLabs API returned "+e.status+" "+e.statusText);return Promise.resolve(e.json()).then(function(e){if(!(t=e.token))throw new Error("No conversation token received from API")})})},function(e){var t=e instanceof Error?e.message:String(e);throw e instanceof Error&&e.message.includes("401")&&(t="Your agent has authentication enabled, but no signed URL or conversation token was provided."),new Error("Failed to fetch conversation token for agent "+n.agentId+": "+t)});throw new Error("Either conversationToken or agentId is required for WebRTC connection")}();t=n.conversationToken}();return Promise.resolve(i&&i.then?i.then(r):r())}catch(e){return Promise.reject(e)}};var r=o.prototype;return r.setupRoomEventListeners=function(){var n=this,t=this,o=this,r=this;this.room.on(e.RoomEvent.Connected,function(){try{return t.isConnected=!0,console.info("WebRTC room connected"),Promise.resolve()}catch(e){return Promise.reject(e)}}),this.room.on(e.RoomEvent.Disconnected,function(e){n.isConnected=!1,n.disconnect({reason:"agent",context:new CloseEvent("close",{reason:null==e?void 0:e.toString()})})}),this.room.on(e.RoomEvent.ConnectionStateChanged,function(t){t===e.ConnectionState.Disconnected&&(n.isConnected=!1,n.disconnect({reason:"error",message:"LiveKit connection state changed to "+t,context:new Event("connection_state_changed")}))}),this.room.on(e.RoomEvent.DataReceived,function(e,t){try{var o=JSON.parse((new TextDecoder).decode(e));if("audio"===o.type)return;l(o)?n.handleMessage(o):console.warn("Invalid socket event received:",o)}catch(n){console.warn("Failed to parse incoming data message:",n),console.warn("Raw payload:",(new TextDecoder).decode(e))}}),this.room.on(e.RoomEvent.TrackSubscribed,function(n,t,r){try{var i=function(){if(n.kind===e.Track.Kind.Audio&&r.identity.includes("agent")){var t=function(){return a.style.display="none",document.body.appendChild(a),o.audioElements.push(a),1===o.audioElements.length&&(null==o.onDebug||o.onDebug({type:"audio_element_ready"})),Promise.resolve(o.setupAudioCapture(i)).then(function(){})},i=n,a=i.attach();a.autoplay=!0,a.controls=!1;var s=function(){if(o.outputDeviceId&&a.setSinkId){var e=k(function(){return Promise.resolve(a.setSinkId(o.outputDeviceId)).then(function(){})},function(e){console.warn("Failed to set output device for new audio element:",e)});if(e&&e.then)return e.then(function(){})}}();return s&&s.then?s.then(t):t()}}();return Promise.resolve(i&&i.then?i.then(function(){}):void 0)}catch(e){return Promise.reject(e)}}),this.room.on(e.RoomEvent.ActiveSpeakersChanged,function(e){try{return r.updateMode(e.length>0&&e[0].identity.startsWith("agent")?"speaking":"listening"),Promise.resolve()}catch(e){return Promise.reject(e)}})},r.close=function(){if(this.isConnected){try{this.room.localParticipant.audioTrackPublications.forEach(function(e){e.track&&e.track.stop()})}catch(e){console.warn("Error stopping local tracks:",e)}this.audioCaptureContext&&(this.audioCaptureContext.close().catch(function(e){console.warn("Error closing audio capture context:",e)}),this.audioCaptureContext=null),this.audioElements.forEach(function(e){e.parentNode&&e.parentNode.removeChild(e)}),this.audioElements=[],this.room.disconnect()}},r.sendMessage=function(e){try{var n=this;if(!n.isConnected||!n.room.localParticipant)return console.warn("Cannot send message: room not connected or no local participant"),Promise.resolve();if("user_audio_chunk"in e)return Promise.resolve();var t=k(function(){var t=(new TextEncoder).encode(JSON.stringify(e));return Promise.resolve(n.room.localParticipant.publishData(t,{reliable:!0})).then(function(){})},function(t){n.debug({type:"send_message_error",message:{message:e,error:t}}),console.error("Failed to send message via WebRTC:",t)});return Promise.resolve(t&&t.then?t.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},r.getRoom=function(){return this.room},r.setMicMuted=function(n){try{var t=this;if(!t.isConnected||!t.room.localParticipant)return console.warn("Cannot set microphone muted: room not connected or no local participant"),Promise.resolve();var o=t.room.localParticipant.getTrackPublication(e.Track.Source.Microphone);return Promise.resolve(null!=o&&o.track?k(function(){var e=n?Promise.resolve(o.track.mute()).then(function(){}):Promise.resolve(o.track.unmute()).then(function(){});if(e&&e.then)return e.then(function(){})},function(){return Promise.resolve(t.room.localParticipant.setMicrophoneEnabled(!n)).then(function(){})}):Promise.resolve(t.room.localParticipant.setMicrophoneEnabled(!n)).then(function(){}))}catch(e){return Promise.reject(e)}},r.setupAudioCapture=function(e){try{var n=this,t=k(function(){var t=new AudioContext;n.audioCaptureContext=t,n.outputAnalyser=t.createAnalyser(),n.outputAnalyser.fftSize=2048,n.outputAnalyser.smoothingTimeConstant=.8;var o=new MediaStream([e.mediaStreamTrack]),r=t.createMediaStreamSource(o);return r.connect(n.outputAnalyser),Promise.resolve(P(t.audioWorklet)).then(function(){var e=new AudioWorkletNode(t,"raw-audio-processor");n.outputAnalyser.connect(e),e.port.postMessage({type:"setFormat",format:n.outputFormat.format,sampleRate:n.outputFormat.sampleRate}),e.port.onmessage=function(e){var t=e.data;if(t[1]>.01){var o=p(t[0].buffer),r=n.audioEventId++;n.handleMessage({type:"audio",audio_event:{audio_base_64:o,event_id:r}})}},r.connect(e)})},function(e){console.warn("Failed to set up audio capture:",e)});return Promise.resolve(t&&t.then?t.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},r.setAudioVolume=function(e){this.audioElements.forEach(function(n){n.volume=e})},r.setAudioOutputDevice=function(e){try{var n=this;if(!("setSinkId"in HTMLAudioElement.prototype))throw new Error("setSinkId is not supported in this browser");var t=n.audioElements.map(function(n){try{return Promise.resolve(k(function(){return Promise.resolve(n.setSinkId(e)).then(function(){})},function(e){throw console.error("Failed to set sink ID for audio element:",e),e}))}catch(e){return Promise.reject(e)}});return Promise.resolve(Promise.all(t)).then(function(){n.outputDeviceId=e})}catch(e){return Promise.reject(e)}},r.setAudioInputDevice=function(n){try{var t=this;if(!t.isConnected||!t.room.localParticipant)throw new Error("Cannot change input device: room not connected or no local participant");return Promise.resolve(k(function(){function o(){return Promise.resolve(e.createLocalAudioTrack({deviceId:{exact:n},echoCancellation:!0,noiseSuppression:!0,autoGainControl:!0,channelCount:{ideal:1}})).then(function(n){return Promise.resolve(t.room.localParticipant.publishTrack(n,{name:"microphone",source:e.Track.Source.Microphone})).then(function(){})})}var r=t.room.localParticipant.getTrackPublication(e.Track.Source.Microphone),i=function(){if(null!=r&&r.track)return Promise.resolve(r.track.stop()).then(function(){return Promise.resolve(t.room.localParticipant.unpublishTrack(r.track)).then(function(){})})}();return i&&i.then?i.then(o):o()},function(e){function n(){throw e}console.error("Failed to change input device:",e);var o=k(function(){return Promise.resolve(t.room.localParticipant.setMicrophoneEnabled(!0)).then(function(){})},function(e){console.error("Failed to recover microphone after device switch error:",e)});return o&&o.then?o.then(n):n()}))}catch(e){return Promise.reject(e)}},r.getOutputByteFrequencyData=function(){return this.outputAnalyser?(null!=this.outputFrequencyData||(this.outputFrequencyData=new Uint8Array(this.outputAnalyser.frequencyBinCount)),this.outputAnalyser.getByteFrequencyData(this.outputFrequencyData),this.outputFrequencyData):null},o}(s),w=function(e){try{var n=function(e){return e.connectionType?e.connectionType:"conversationToken"in e&&e.conversationToken?"webrtc":"websocket"}(e);switch(n){case"websocket":return Promise.resolve(f.create(e));case"webrtc":return Promise.resolve(b.create(e));default:throw new Error("Unknown connection type: "+n)}}catch(e){return Promise.reject(e)}};function _(){return["iPad Simulator","iPhone Simulator","iPod Simulator","iPad","iPhone","iPod"].includes(navigator.platform)||navigator.userAgent.includes("Mac")&&"ontouchend"in document}var C=function(e){void 0===e&&(e={default:0,android:3e3});try{var n,t=e.default;if(/android/i.test(navigator.userAgent))t=null!=(n=e.android)?n:t;else if(_()){var o;t=null!=(o=e.ios)?o:t}var r=function(){if(t>0)return Promise.resolve(new Promise(function(e){return setTimeout(e,t)})).then(function(){})}();return Promise.resolve(r&&r.then?r.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},S=/*#__PURE__*/function(e){function n(){return e.apply(this,arguments)||this}return t(n,e),n.startSession=function(e){try{var t=a.getFullOptions(e);t.onStatusChange&&t.onStatusChange({status:"connecting"}),t.onCanSendFeedbackChange&&t.onCanSendFeedbackChange({canSendFeedback:!1}),t.onModeChange&&t.onModeChange({mode:"listening"}),t.onCanSendFeedbackChange&&t.onCanSendFeedbackChange({canSendFeedback:!1});var o=null;return Promise.resolve(function(r,i){try{var a=Promise.resolve(C(t.connectionDelay)).then(function(){return Promise.resolve(w(e)).then(function(e){return new n(t,o=e)})})}catch(e){return i(e)}return a&&a.then?a.then(void 0,i):a}(0,function(e){var n;throw t.onStatusChange&&t.onStatusChange({status:"disconnected"}),null==(n=o)||n.close(),e}))}catch(e){return Promise.reject(e)}},n}(a);function M(e,n){try{var t=e()}catch(e){return n(e)}return t&&t.then?t.then(void 0,n):t}var I={echoCancellation:!0,noiseSuppression:!0,autoGainControl:!0,channelCount:{ideal:1}},D=/*#__PURE__*/function(){function e(e,n,t,o,r){this.context=void 0,this.analyser=void 0,this.worklet=void 0,this.inputStream=void 0,this.mediaStreamSource=void 0,this.context=e,this.analyser=n,this.worklet=t,this.inputStream=o,this.mediaStreamSource=r}e.create=function(t){var o=t.sampleRate,r=t.format,i=t.preferHeadphonesForIosDevices,a=t.inputDeviceId;try{var s=null,u=null;return Promise.resolve(M(function(){function t(){function t(){return Promise.resolve(P(s.audioWorklet)).then(function(){var t=n({voiceIsolation:!0},c);return Promise.resolve(navigator.mediaDevices.getUserMedia({audio:t})).then(function(n){var t=s.createMediaStreamSource(u=n),i=new AudioWorkletNode(s,"raw-audio-processor");return i.port.postMessage({type:"setFormat",format:r,sampleRate:o}),t.connect(l),l.connect(i),Promise.resolve(s.resume()).then(function(){return new e(s,l,i,u,t)})})})}a&&(c.deviceId={exact:a});var i=navigator.mediaDevices.getSupportedConstraints().sampleRate,l=(s=new window.AudioContext(i?{sampleRate:o}:{})).createAnalyser(),d=function(){if(!i)return Promise.resolve(s.audioWorklet.addModule("https://cdn.jsdelivr.net/npm/@alexanderolsen/libsamplerate-js@2.1.2/dist/libsamplerate.worklet.js")).then(function(){})}();return d&&d.then?d.then(t):t()}var c=n({sampleRate:{ideal:o}},I),l=function(){if(_()&&i)return Promise.resolve(window.navigator.mediaDevices.enumerateDevices()).then(function(e){var n=e.find(function(e){return"audioinput"===e.kind&&["airpod","headphone","earphone"].find(function(n){return e.label.toLowerCase().includes(n)})});n&&(c.deviceId={ideal:n.deviceId})})}();return l&&l.then?l.then(t):t()},function(e){var n,t;throw null==(n=u)||n.getTracks().forEach(function(e){e.stop()}),null==(t=s)||t.close(),e}))}catch(e){return Promise.reject(e)}};var t=e.prototype;return t.close=function(){try{var e=this;return e.inputStream.getTracks().forEach(function(e){e.stop()}),e.mediaStreamSource.disconnect(),Promise.resolve(e.context.close()).then(function(){})}catch(e){return Promise.reject(e)}},t.setMuted=function(e){this.worklet.port.postMessage({type:"setMuted",isMuted:e})},t.setInputDevice=function(e){try{var t=this;if(!e)throw new Error("Input device ID is required");return Promise.resolve(M(function(){var o=n({deviceId:{exact:e}},I),r=n({voiceIsolation:!0},o);return Promise.resolve(navigator.mediaDevices.getUserMedia({audio:r})).then(function(e){t.inputStream.getTracks().forEach(function(e){e.stop()}),t.mediaStreamSource.disconnect(),t.inputStream=e,t.mediaStreamSource=t.context.createMediaStreamSource(e),t.mediaStreamSource.connect(t.analyser)})},function(e){throw console.error("Failed to switch input device:",e),e}))}catch(e){return Promise.reject(e)}},e}(),F=y("audio-concat-processor",'\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 = []; // Initialize an empty buffer\n this.cursor = 0;\n this.currentBuffer = null;\n this.wasInterrupted = false;\n this.finished = false;\n \n this.port.onmessage = ({ data }) => {\n switch (data.type) {\n case "setFormat":\n this.format = data.format;\n break;\n case "buffer":\n this.wasInterrupted = false;\n this.buffers.push(\n this.format === "ulaw"\n ? new Uint8Array(data.buffer)\n : new Int16Array(data.buffer)\n );\n break;\n case "interrupt":\n this.wasInterrupted = true;\n break;\n case "clearInterrupted":\n if (this.wasInterrupted) {\n this.wasInterrupted = false;\n this.buffers = [];\n this.currentBuffer = null;\n }\n }\n };\n }\n process(_, outputs) {\n let finished = false;\n const output = outputs[0][0];\n for (let i = 0; i < output.length; i++) {\n if (!this.currentBuffer) {\n if (this.buffers.length === 0) {\n finished = true;\n break;\n }\n this.currentBuffer = this.buffers.shift();\n this.cursor = 0;\n }\n\n let value = this.currentBuffer[this.cursor];\n if (this.format === "ulaw") {\n value = decodeSample(value);\n }\n output[i] = value / 32768;\n this.cursor++;\n\n if (this.cursor >= this.currentBuffer.length) {\n this.currentBuffer = null;\n }\n }\n\n if (this.finished !== finished) {\n this.finished = finished;\n this.port.postMessage({ type: "process", finished });\n }\n\n return true; // Continue processing\n }\n}\n\nregisterProcessor("audio-concat-processor", AudioConcatProcessor);\n'),E=/*#__PURE__*/function(){function e(e,n,t,o,r){this.context=void 0,this.analyser=void 0,this.gain=void 0,this.worklet=void 0,this.audioElement=void 0,this.context=e,this.analyser=n,this.gain=t,this.worklet=o,this.audioElement=r}e.create=function(n){var t=n.sampleRate,o=n.format,r=n.outputDeviceId;try{var i=null,a=null;return Promise.resolve(function(n,s){try{var u=function(){var n=(i=new AudioContext({sampleRate:t})).createAnalyser(),s=i.createGain();(a=new Audio).src="",a.load(),a.autoplay=!0,a.style.display="none",document.body.appendChild(a);var u=i.createMediaStreamDestination();return a.srcObject=u.stream,s.connect(n),n.connect(u),Promise.resolve(F(i.audioWorklet)).then(function(){var t=new AudioWorkletNode(i,"audio-concat-processor");return t.port.postMessage({type:"setFormat",format:o}),t.connect(s),Promise.resolve(i.resume()).then(function(){function o(){return new e(i,n,s,t,a)}var u=function(){if(r&&a.setSinkId)return Promise.resolve(a.setSinkId(r)).then(function(){})}();return u&&u.then?u.then(o):o()})})}()}catch(e){return s(e)}return u&&u.then?u.then(void 0,s):u}(0,function(e){var n,t;function o(){throw e}null!=(n=a)&&n.parentNode&&a.parentNode.removeChild(a),null==(t=a)||t.pause();var r=function(){if(i&&"closed"!==i.state)return Promise.resolve(i.close()).then(function(){})}();return r&&r.then?r.then(o):o()}))}catch(e){return Promise.reject(e)}};var n=e.prototype;return n.setOutputDevice=function(e){try{if(!("setSinkId"in HTMLAudioElement.prototype))throw new Error("setSinkId is not supported in this browser");return Promise.resolve(this.audioElement.setSinkId(e)).then(function(){})}catch(e){return Promise.reject(e)}},n.close=function(){try{var e=this;return e.audioElement.parentNode&&e.audioElement.parentNode.removeChild(e.audioElement),e.audioElement.pause(),Promise.resolve(e.context.close()).then(function(){})}catch(e){return Promise.reject(e)}},e}();function A(e,n){try{var t=e()}catch(e){return n(e)}return t&&t.then?t.then(void 0,n):t}var x=/*#__PURE__*/function(e){function o(n,t,o,r,i){var a;return(a=e.call(this,n,t)||this).input=void 0,a.output=void 0,a.wakeLock=void 0,a.inputFrequencyData=void 0,a.outputFrequencyData=void 0,a.onInputWorkletMessage=function(e){"connected"===a.status&&a.connection.sendMessage({user_audio_chunk:p(e.data[0].buffer)})},a.onOutputWorkletMessage=function(e){var n=e.data;"process"===n.type&&a.updateMode(n.finished?"listening":"speaking")},a.addAudioBase64Chunk=function(e){a.output.gain.gain.value=a.volume,a.output.worklet.port.postMessage({type:"clearInterrupted"}),a.output.worklet.port.postMessage({type:"buffer",buffer:v(e)})},a.fadeOutAudio=function(){a.updateMode("listening"),a.output.worklet.port.postMessage({type:"interrupt"}),a.output.gain.gain.exponentialRampToValueAtTime(1e-4,a.output.context.currentTime+2),setTimeout(function(){a.output.gain.gain.value=a.volume,a.output.worklet.port.postMessage({type:"clearInterrupted"})},2e3)},a.calculateVolume=function(e){if(0===e.length)return 0;for(var n=0,t=0;t<e.length;t++)n+=e[t]/255;return(n/=e.length)<0?0:n>1?1:n},a.setVolume=function(e){var n=e.volume,t=Number.isFinite(n)?Math.min(1,Math.max(0,n)):1;a.volume=t,a.connection instanceof b?a.connection.setAudioVolume(t):a.output.gain.gain.value=t},a.input=o,a.output=r,a.wakeLock=i,a.input.worklet.port.onmessage=a.onInputWorkletMessage,a.output.worklet.port.onmessage=a.onOutputWorkletMessage,a}t(o,e),o.startSession=function(e){try{var t=function(){return A(function(){return Promise.resolve(navigator.mediaDevices.getUserMedia({audio:!0})).then(function(t){return c=t,Promise.resolve(C(r.connectionDelay)).then(function(){return Promise.resolve(w(e)).then(function(t){return s=t,Promise.resolve(Promise.all([D.create(n({},s.inputFormat,{preferHeadphonesForIosDevices:e.preferHeadphonesForIosDevices,inputDeviceId:e.inputDeviceId})),E.create(n({},s.outputFormat,{outputDeviceId:e.outputDeviceId}))])).then(function(e){var n;return i=e[0],u=e[1],null==(n=c)||n.getTracks().forEach(function(e){e.stop()}),c=null,new o(r,s,i,u,l)})})})})},function(e){var n,t,o;return r.onStatusChange&&r.onStatusChange({status:"disconnected"}),null==(n=c)||n.getTracks().forEach(function(e){e.stop()}),null==(t=s)||t.close(),Promise.resolve(null==(o=i)?void 0:o.close()).then(function(){var n;return Promise.resolve(null==(n=u)?void 0:n.close()).then(function(){function n(){throw e}var t=A(function(){var e;return Promise.resolve(null==(e=l)?void 0:e.release()).then(function(){l=null})},function(){});return t&&t.then?t.then(n):n()})})})},r=a.getFullOptions(e);r.onStatusChange&&r.onStatusChange({status:"connecting"}),r.onCanSendFeedbackChange&&r.onCanSendFeedbackChange({canSendFeedback:!1});var i=null,s=null,u=null,c=null,l=null,d=function(n){if(null==(n=e.useWakeLock)||n){var t=A(function(){return Promise.resolve(navigator.wakeLock.request("screen")).then(function(e){l=e})},function(){});if(t&&t.then)return t.then(function(){})}}();return Promise.resolve(d&&d.then?d.then(t):t())}catch(e){return Promise.reject(e)}};var r=o.prototype;return r.handleEndSession=function(){try{var n=this;return Promise.resolve(e.prototype.handleEndSession.call(n)).then(function(){function e(){return Promise.resolve(n.input.close()).then(function(){return Promise.resolve(n.output.close()).then(function(){})})}var t=A(function(){var e;return Promise.resolve(null==(e=n.wakeLock)?void 0:e.release()).then(function(){n.wakeLock=null})},function(){});return t&&t.then?t.then(e):e()})}catch(e){return Promise.reject(e)}},r.handleInterruption=function(n){e.prototype.handleInterruption.call(this,n),this.fadeOutAudio()},r.handleAudio=function(e){var n,t;this.lastInterruptTimestamp<=e.audio_event.event_id&&(null==(n=(t=this.options).onAudio)||n.call(t,e.audio_event.audio_base_64),this.connection instanceof b||this.addAudioBase64Chunk(e.audio_event.audio_base_64),this.currentEventId=e.audio_event.event_id,this.updateCanSendFeedback(),this.updateMode("speaking"))},r.setMicMuted=function(e){this.connection instanceof b?this.connection.setMicMuted(e):this.input.setMuted(e)},r.getInputByteFrequencyData=function(){return null!=this.inputFrequencyData||(this.inputFrequencyData=new Uint8Array(this.input.analyser.frequencyBinCount)),this.input.analyser.getByteFrequencyData(this.inputFrequencyData),this.inputFrequencyData},r.getOutputByteFrequencyData=function(){return this.connection instanceof b?this.connection.getOutputByteFrequencyData()||new Uint8Array(1024):(null!=this.outputFrequencyData||(this.outputFrequencyData=new Uint8Array(this.output.analyser.frequencyBinCount)),this.output.analyser.getByteFrequencyData(this.outputFrequencyData),this.outputFrequencyData)},r.getInputVolume=function(){return this.calculateVolume(this.getInputByteFrequencyData())},r.getOutputVolume=function(){return this.calculateVolume(this.getOutputByteFrequencyData())},r.changeInputDevice=function(e){var n=e.sampleRate,t=e.format,o=e.preferHeadphonesForIosDevices,r=e.inputDeviceId;try{var i,a=this;return Promise.resolve(A(function(){function e(e){if(i)return e;function s(){return Promise.resolve(a.input.close()).then(function(){return Promise.resolve(D.create({sampleRate:n,format:t,preferHeadphonesForIosDevices:o,inputDeviceId:r})).then(function(e){return a.input=e,a.input})})}var u=function(){if(a.connection instanceof b){var e=function(){if(r)return Promise.resolve(a.connection.setAudioInputDevice(r)).then(function(){})}();if(e&&e.then)return e.then(function(){})}}();return u&&u.then?u.then(s):s()}var s=function(){if(a.connection instanceof f)return function(){if(r)return A(function(){return Promise.resolve(a.input.setInputDevice(r)).then(function(){return i=1,a.input})},function(e){console.warn("Failed to change device on existing input, recreating:",e)})}()}();return s&&s.then?s.then(e):e(s)},function(e){throw console.error("Error changing input device",e),e}))}catch(e){return Promise.reject(e)}},r.changeOutputDevice=function(e){var n=e.sampleRate,t=e.format,o=e.outputDeviceId;try{var r,i=this;return Promise.resolve(A(function(){function e(e){if(r)return e;function a(){return Promise.resolve(i.output.close()).then(function(){return Promise.resolve(E.create({sampleRate:n,format:t,outputDeviceId:o})).then(function(e){return i.output=e,i.output})})}var s=function(){if(i.connection instanceof b){var e=function(){if(o)return Promise.resolve(i.connection.setAudioOutputDevice(o)).then(function(){})}();if(e&&e.then)return e.then(function(){})}}();return s&&s.then?s.then(a):a()}var a=function(){if(i.connection instanceof f)return function(){if(o)return A(function(){return Promise.resolve(i.output.setOutputDevice(o)).then(function(){return r=1,i.output})},function(e){console.warn("Failed to change device on existing output, recreating:",e)})}()}();return a&&a.then?a.then(e):e(a)},function(e){throw console.error("Error changing output device",e),e}))}catch(e){return Promise.reject(e)}},o}(a);exports.Conversation=/*#__PURE__*/function(e){function n(){return e.apply(this,arguments)||this}return t(n,e),n.startSession=function(e){return e.textOnly?S.startSession(e):x.startSession(e)},n}(a),exports.Input=D,exports.Output=E,exports.TextConversation=S,exports.VoiceConversation=x,exports.WebRTCConnection=b,exports.WebSocketConnection=f,exports.createConnection=w,exports.postOverallFeedback=function(e,n,t){return void 0===t&&(t="https://api.elevenlabs.io"),fetch(t+"/v1/convai/conversations/"+e+"/feedback",{method:"POST",body:JSON.stringify({feedback:n?"like":"dislike"}),headers:{"Content-Type":"application/json"}})};
2
2
  //# sourceMappingURL=lib.cjs.map