@elevenlabs/client 0.1.7 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -26,6 +26,10 @@ This library is primarily meant for development in vanilla JavaScript projects,
26
26
  It is recommended to check whether your specific framework has it's own library.
27
27
  However, you can use this library in any JavaScript-based project.
28
28
 
29
+ ### Connection types
30
+
31
+ A conversation can be started via one of two connection types: WebSockets (the default) or WebRTC.
32
+
29
33
  ### Initialize conversation
30
34
 
31
35
  First, initialize the Conversation instance:
@@ -48,38 +52,38 @@ try {
48
52
 
49
53
  #### Session configuration
50
54
 
51
- The options passed to `startSession` specifiy how the session is established. There are two ways to start a session:
55
+ The options passed to `startSession` specifiy how the session is established. There are three ways to start a session:
52
56
 
53
- ##### Using Agent ID
57
+ ##### Public agents
58
+
59
+ Agents that don't require any authentication can be used to start a conversation by using the agent ID and the connection type. The agent ID can be acquired through the [ElevenLabs UI](https://elevenlabs.io/app/conversational-ai).
54
60
 
55
- Agent ID can be acquired through [ElevenLabs UI](https://elevenlabs.io/app/conversational-ai).
56
61
  For public agents, you can use the ID directly:
57
62
 
58
63
  ```js
59
64
  const conversation = await Conversation.startSession({
60
65
  agentId: "<your-agent-id>",
66
+ connectionType: 'webrtc' // 'websocket' is also accepted
61
67
  });
62
68
  ```
63
69
 
64
- ##### Using a signed URL
70
+ ##### Private agents
65
71
 
66
- If the conversation requires authorization, you will need to add a dedicated endpoint to your server that
67
- will request a signed url using the [ElevenLabs API](https://elevenlabs.io/docs/introduction) and pass it back to the client.
72
+ If the conversation requires authorization, you will need to add a dedicated endpoint to your server that will either request a signed url (if using the WebSockets connection type) or a conversation token (if using WebRTC) using the [ElevenLabs API](https://elevenlabs.io/docs/introduction) and pass it back to the client.
68
73
 
69
- Here's an example of how it could be set up:
74
+ Here's an example for a WebSocket connection:
70
75
 
71
76
  ```js
72
77
  // Node.js server
73
78
 
74
79
  app.get("/signed-url", yourAuthMiddleware, async (req, res) => {
75
80
  const response = await fetch(
76
- `https://api.elevenlabs.io/v1/convai/conversation/get_signed_url?agent_id=${process.env.AGENT_ID}`,
81
+ `https://api.elevenlabs.io/v1/convai/conversation/get-signed-url?agent_id=${process.env.AGENT_ID}`,
77
82
  {
78
- method: "GET",
79
83
  headers: {
80
84
  // Requesting a signed url requires your ElevenLabs API key
81
85
  // Do NOT expose your API key to the client!
82
- "xi-api-key": process.env.XI_API_KEY,
86
+ "xi-api-key": process.env.ELEVENLABS_API_KEY,
83
87
  },
84
88
  }
85
89
  );
@@ -99,7 +103,50 @@ app.get("/signed-url", yourAuthMiddleware, async (req, res) => {
99
103
  const response = await fetch("/signed-url", yourAuthHeaders);
100
104
  const signedUrl = await response.text();
101
105
 
102
- const conversation = await Conversation.startSession({ signedUrl });
106
+ const conversation = await Conversation.startSession({
107
+ signedUrl,
108
+ connectionType: 'websocket',
109
+ });
110
+ ```
111
+
112
+ Here's an example for WebRTC:
113
+
114
+ ```js
115
+ // Node.js server
116
+
117
+ app.get("/conversation-token", yourAuthMiddleware, async (req, res) => {
118
+ const response = await fetch(
119
+ `https://api.elevenlabs.io/v1/convai/conversation/token?agent_id=${process.env.AGENT_ID}`,
120
+ {
121
+ headers: {
122
+ // Requesting a conversation token requires your ElevenLabs API key
123
+ // Do NOT expose your API key to the client!
124
+ 'xi-api-key': process.env.ELEVENLABS_API_KEY,
125
+ }
126
+ }
127
+ );
128
+
129
+ if (!response.ok) {
130
+ return res.status(500).send("Failed to get conversation token");
131
+ }
132
+
133
+ const body = await response.json();
134
+ res.send(body.token);
135
+ );
136
+ ```
137
+
138
+ Once you have the token, providing it to `startSession` will initiate the conversation using WebRTC.
139
+
140
+ ```js
141
+ // Client
142
+
143
+ const response = await fetch("/conversation-token", yourAuthHeaders);
144
+ const conversationToken = await response.text();
145
+
146
+ const conversation = await Conversation.startSession({
147
+ conversationToken,
148
+ connectionType: 'webrtc',
149
+ });
103
150
  ```
104
151
 
105
152
  #### Optional callbacks
@@ -1,5 +1,5 @@
1
- import { Connection, type OnDisconnectCallback, type SessionConfig } from "./utils/connection";
2
- import { AgentAudioEvent, AgentResponseEvent, ClientToolCallEvent, InternalTentativeAgentResponseEvent, InterruptionEvent, UserTranscriptionEvent } from "./utils/events";
1
+ import type { BaseConnection, OnDisconnectCallback, SessionConfig } from "./utils/BaseConnection";
2
+ import type { AgentAudioEvent, AgentResponseEvent, ClientToolCallEvent, InternalTentativeAgentResponseEvent, InterruptionEvent, UserTranscriptionEvent } from "./utils/events";
3
3
  import type { InputConfig } from "./utils/input";
4
4
  export type Role = "user" | "ai";
5
5
  export type Mode = "speaking" | "listening";
@@ -34,7 +34,7 @@ export type Callbacks = {
34
34
  };
35
35
  export declare class BaseConversation {
36
36
  protected readonly options: Options;
37
- protected readonly connection: Connection;
37
+ protected readonly connection: BaseConnection;
38
38
  protected lastInterruptTimestamp: number;
39
39
  protected mode: Mode;
40
40
  protected status: Status;
@@ -43,7 +43,7 @@ export declare class BaseConversation {
43
43
  protected lastFeedbackEventId: number;
44
44
  protected canSendFeedback: boolean;
45
45
  protected static getFullOptions(partialOptions: PartialOptions): Options;
46
- protected constructor(options: Options, connection: Connection);
46
+ protected constructor(options: Options, connection: BaseConnection);
47
47
  endSession(): Promise<void>;
48
48
  private endSessionWithDetails;
49
49
  protected handleEndSession(): Promise<void>;
@@ -1,4 +1,4 @@
1
- import { BaseConversation, PartialOptions } from "./BaseConversation";
1
+ import { BaseConversation, type PartialOptions } from "./BaseConversation";
2
2
  export declare class TextConversation extends BaseConversation {
3
3
  static startSession(options: PartialOptions): Promise<TextConversation>;
4
4
  }
@@ -1,8 +1,8 @@
1
1
  import { Input } from "./utils/input";
2
2
  import { Output } from "./utils/output";
3
- import { Connection } from "./utils/connection";
4
- import { AgentAudioEvent, InterruptionEvent } from "./utils/events";
5
- import { BaseConversation, Options, PartialOptions } from "./BaseConversation";
3
+ import type { BaseConnection } from "./utils/BaseConnection";
4
+ import type { AgentAudioEvent, InterruptionEvent } from "./utils/events";
5
+ import { BaseConversation, type Options, type PartialOptions } from "./BaseConversation";
6
6
  export declare class VoiceConversation extends BaseConversation {
7
7
  readonly input: Input;
8
8
  readonly output: Output;
@@ -10,7 +10,7 @@ export declare class VoiceConversation extends BaseConversation {
10
10
  static startSession(options: PartialOptions): Promise<VoiceConversation>;
11
11
  private inputFrequencyData?;
12
12
  private outputFrequencyData?;
13
- protected constructor(options: Options, connection: Connection, input: Input, output: Output, wakeLock: WakeLockSentinel | null);
13
+ protected constructor(options: Options, connection: BaseConnection, input: Input, output: Output, wakeLock: WakeLockSentinel | null);
14
14
  protected handleEndSession(): Promise<void>;
15
15
  protected handleInterruption(event: InterruptionEvent): void;
16
16
  protected handleAudio(event: AgentAudioEvent): void;
package/dist/index.d.ts CHANGED
@@ -1,8 +1,11 @@
1
- import { BaseConversation, PartialOptions } from "./BaseConversation";
1
+ import { BaseConversation, type PartialOptions } from "./BaseConversation";
2
2
  export type { Mode, Role, Options, PartialOptions, ClientToolsConfig, Callbacks, Status, } from "./BaseConversation";
3
3
  export type { InputConfig } from "./utils/input";
4
4
  export type { IncomingSocketEvent } from "./utils/events";
5
- export type { SessionConfig, DisconnectionDetails, Language, } from "./utils/connection";
5
+ export type { SessionConfig, DisconnectionDetails, Language, ConnectionType, } from "./utils/BaseConnection";
6
+ export { createConnection } from "./utils/ConnectionFactory";
7
+ export { WebSocketConnection } from "./utils/WebSocketConnection";
8
+ export { WebRTCConnection } from "./utils/WebRTCConnection";
6
9
  export { postOverallFeedback } from "./utils/postOverallFeedback";
7
10
  export declare class Conversation extends BaseConversation {
8
11
  static startSession(options: PartialOptions): Promise<Conversation>;
package/dist/lib.cjs CHANGED
@@ -1,2 +1,2 @@
1
- function e(){return e=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var o in n)({}).hasOwnProperty.call(n,o)&&(e[o]=n[o])}return e},e.apply(null,arguments)}function t(e,t){e.prototype=Object.create(t.prototype),e.prototype.constructor=e,n(e,t)}function n(e,t){return n=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,t){return e.__proto__=t,e},n(e,t)}var o=new Uint8Array(0),r=/*#__PURE__*/function(){function t(e,t){var n=this,o=this,r=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=1,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(e)}))}catch(e){return Promise.reject(e)}},this.onMessage=function(e){try{switch(e.type){case"interruption":return r.handleInterruption(e),Promise.resolve();case"agent_response":return r.handleAgentResponse(e),Promise.resolve();case"user_transcript":return r.handleUserTranscript(e),Promise.resolve();case"internal_tentative_agent_response":return r.handleTentativeAgentResponse(e),Promise.resolve();case"client_tool_call":return Promise.resolve(r.handleClientToolCall(e)).then(function(){});case"audio":return r.handleAudio(e),Promise.resolve();case"ping":return r.connection.sendMessage({type:"pong",event_id:e.ping_event.event_id}),Promise.resolve();default:return r.options.onDebug(e),Promise.resolve()}}catch(e){return Promise.reject(e)}},this.setVolume=function(e){n.volume=e.volume},this.options=e,this.connection=t,this.options.onConnect({conversationId:t.conversationId}),this.connection.onMessage(this.onMessage),this.connection.onDisconnect(this.endSessionWithDetails),this.updateStatus("connected")}t.getFullOptions=function(t){return e({clientTools:{},onConnect:function(){},onDebug:function(){},onDisconnect:function(){},onError:function(){},onMessage:function(){},onAudio:function(){},onModeChange:function(){},onStatusChange:function(){},onCanSendFeedbackChange:function(){}},t)};var n=t.prototype;return n.endSession=function(){return this.endSessionWithDetails({reason:"user"})},n.handleEndSession=function(){try{return this.connection.close(),Promise.resolve()}catch(e){return Promise.reject(e)}},n.updateMode=function(e){e!==this.mode&&(this.mode=e,this.options.onModeChange({mode:e}))},n.updateStatus=function(e){e!==this.status&&(this.status=e,this.options.onStatusChange({status:e}))},n.updateCanSendFeedback=function(){var e=this.currentEventId!==this.lastFeedbackEventId;this.canSendFeedback!==e&&(this.canSendFeedback=e,this.options.onCanSendFeedbackChange({canSendFeedback:e}))},n.handleInterruption=function(e){e.interruption_event&&(this.lastInterruptTimestamp=e.interruption_event.event_id)},n.handleAgentResponse=function(e){this.options.onMessage({source:"ai",message:e.agent_response_event.agent_response})},n.handleUserTranscript=function(e){this.options.onMessage({source:"user",message:e.user_transcription_event.user_transcript})},n.handleTentativeAgentResponse=function(e){this.options.onDebug({type:"tentative_agent_response",response:e.tentative_agent_response_internal_event.tentative_agent_response})},n.handleClientToolCall=function(e){try{var t=this;return Promise.resolve(function(){if(t.options.clientTools.hasOwnProperty(e.client_tool_call.tool_name)){var n=function(n,o){try{var r=Promise.resolve(t.options.clientTools[e.client_tool_call.tool_name](e.client_tool_call.parameters)).then(function(n){var o="object"==typeof n?JSON.stringify(n):String(n);t.connection.sendMessage({type:"client_tool_result",tool_call_id:e.client_tool_call.tool_call_id,result:o,is_error:!1})})}catch(e){return o(e)}return r&&r.then?r.then(void 0,o):r}(0,function(n){t.onError("Client tool execution failed with following error: "+(null==n?void 0:n.message),{clientToolName:e.client_tool_call.tool_name}),t.connection.sendMessage({type:"client_tool_result",tool_call_id:e.client_tool_call.tool_call_id,result:"Client tool execution failed: "+(null==n?void 0:n.message),is_error:!0})});if(n&&n.then)return n.then(function(){})}else{if(t.options.onUnhandledClientToolCall)return void t.options.onUnhandledClientToolCall(e.client_tool_call);t.onError("Client tool with name "+e.client_tool_call.tool_name+" is not defined on client",{clientToolName:e.client_tool_call.tool_name}),t.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)}},n.handleAudio=function(e){},n.onError=function(e,t){console.error(e,t),this.options.onError(e,t)},n.getId=function(){return this.connection.conversationId},n.isOpen=function(){return"connected"===this.status},n.setMicMuted=function(e){},n.getInputByteFrequencyData=function(){return o},n.getOutputByteFrequencyData=function(){return o},n.getInputVolume=function(){return 0},n.getOutputVolume=function(){return 0},n.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.")},n.sendContextualUpdate=function(e){this.connection.sendMessage({type:"contextual_update",text:e})},n.sendUserMessage=function(e){this.connection.sendMessage({type:"user_message",text:e})},n.sendUserActivity=function(){this.connection.sendMessage({type:"user_activity"})},n.sendMCPToolApprovalResult=function(e,t){this.connection.sendMessage({type:"mcp_tool_approval_result",tool_call_id:e,is_approved:t})},t}();function i(e){return!!e.type}var s=/*#__PURE__*/function(){function e(e,t,n,o){var r=this;this.socket=void 0,this.conversationId=void 0,this.inputFormat=void 0,this.outputFormat=void 0,this.queue=[],this.disconnectionDetails=null,this.onDisconnectCallback=null,this.onMessageCallback=null,this.socket=e,this.conversationId=t,this.inputFormat=n,this.outputFormat=o,this.socket.addEventListener("error",function(e){setTimeout(function(){return r.disconnect({reason:"error",message:"The connection was closed due to a socket error.",context:e})},0)}),this.socket.addEventListener("close",function(e){r.disconnect(1e3===e.code?{reason:"agent",context:e}:{reason:"error",message:e.reason||"The connection was closed by the server.",context:e})}),this.socket.addEventListener("message",function(e){try{var t=JSON.parse(e.data);if(!i(t))return;r.onMessageCallback?r.onMessageCallback(t):r.queue.push(t)}catch(e){}})}e.create=function(t){try{var n=null;return Promise.resolve(function(o,r){try{var s=(c=null!=(u=t.origin)?u:"wss://api.elevenlabs.io",l=t.signedUrl?t.signedUrl:c+"/v1/convai/conversation?agent_id="+t.agentId,d=["convai"],t.authorization&&d.push("bearer."+t.authorization),n=new WebSocket(l,d),Promise.resolve(new Promise(function(e,o){n.addEventListener("open",function(){var e,o,r,i,s,a,u={type:"conversation_initiation_client_data"};t.overrides&&(u.conversation_config_override={agent:{prompt:null==(o=t.overrides.agent)?void 0:o.prompt,first_message:null==(r=t.overrides.agent)?void 0:r.firstMessage,language:null==(i=t.overrides.agent)?void 0:i.language},tts:{voice_id:null==(s=t.overrides.tts)?void 0:s.voiceId},conversation:{text_only:null==(a=t.overrides.conversation)?void 0:a.textOnly}}),t.customLlmExtraBody&&(u.custom_llm_extra_body=t.customLlmExtraBody),t.dynamicVariables&&(u.dynamic_variables=t.dynamicVariables),null==(e=n)||e.send(JSON.stringify(u))},{once:!0}),n.addEventListener("error",function(e){setTimeout(function(){return o(e)},0)}),n.addEventListener("close",o),n.addEventListener("message",function(t){var n=JSON.parse(t.data);i(n)&&("conversation_initiation_metadata"===n.type?e(n.conversation_initiation_metadata_event):console.warn("First received message is not conversation metadata."))},{once:!0})})).then(function(t){var o=t.conversation_id,r=t.agent_output_audio_format,i=t.user_input_audio_format,s=a(null!=i?i:"pcm_16000"),u=a(r);return new e(n,o,s,u)}))}catch(e){return r(e)}var u,c,l,d;return s&&s.then?s.then(void 0,r):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.close=function(){this.socket.close()},t.sendMessage=function(e){this.socket.send(JSON.stringify(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)})},t.disconnect=function(e){var t;this.disconnectionDetails||(this.disconnectionDetails=e,null==(t=this.onDisconnectCallback)||t.call(this,e))},e}();function a(e){var t=e.split("_"),n=t[0],o=t[1];if(!["pcm","ulaw"].includes(n))throw new Error("Invalid format: "+e);var r=parseInt(o);if(isNaN(r))throw new Error("Invalid sample rate: "+o);return{format:n,sampleRate:r}}function u(){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 t,n=e.default;if(/android/i.test(navigator.userAgent))n=null!=(t=e.android)?t:n;else if(u()){var o;n=null!=(o=e.ios)?o:n}var r=function(){if(n>0)return Promise.resolve(new Promise(function(e){return setTimeout(e,n)})).then(function(){})}();return Promise.resolve(r&&r.then?r.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},l=/*#__PURE__*/function(e){function n(){return e.apply(this,arguments)||this}return t(n,e),n.startSession=function(e){try{var t=r.getFullOptions(e);t.onStatusChange({status:"connecting"}),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(s.create(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({status:"disconnected"}),null==(n=o)||n.close(),e}))}catch(e){return Promise.reject(e)}},n}(r);function d(e){for(var t=window.atob(e),n=t.length,o=new Uint8Array(n),r=0;r<n;r++)o[r]=t.charCodeAt(r);return o.buffer}function h(e,t){try{var n=e()}catch(e){return t(e)}return n&&n.then?n.then(void 0,t):n}var p=new Map;function f(e,t){return function(n){try{var o,r=function(r){return o?r:h(function(){var o="data:application/javascript;base64,"+btoa(t);return Promise.resolve(n.addModule(o)).then(function(){p.set(e,o)})},function(){throw new Error("Failed to load the "+e+" worklet module. Make sure the browser supports AudioWorklets.")})},i=p.get(e);if(i)return Promise.resolve(n.addModule(i));var s=new Blob([t],{type:"application/javascript"}),a=URL.createObjectURL(s),u=h(function(){return Promise.resolve(n.addModule(a)).then(function(){p.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 v=f("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'),m=/*#__PURE__*/function(){function e(e,t,n,o){this.context=void 0,this.analyser=void 0,this.worklet=void 0,this.inputStream=void 0,this.context=e,this.analyser=t,this.worklet=n,this.inputStream=o}e.create=function(t){var n=t.sampleRate,o=t.format,r=t.preferHeadphonesForIosDevices;try{var i=null,s=null;return Promise.resolve(function(t,a){try{var c=function(){function t(){function t(){return Promise.resolve(v(i.audioWorklet)).then(function(){return Promise.resolve(navigator.mediaDevices.getUserMedia({audio:a})).then(function(t){var r=i.createMediaStreamSource(s=t),a=new AudioWorkletNode(i,"raw-audio-processor");return a.port.postMessage({type:"setFormat",format:o,sampleRate:n}),r.connect(u),u.connect(a),Promise.resolve(i.resume()).then(function(){return new e(i,u,a,s)})})})}var r=navigator.mediaDevices.getSupportedConstraints().sampleRate,u=(i=new window.AudioContext(r?{sampleRate:n}:{})).createAnalyser(),c=function(){if(!r)return Promise.resolve(i.audioWorklet.addModule("https://cdn.jsdelivr.net/npm/@alexanderolsen/libsamplerate-js@2.1.2/dist/libsamplerate.worklet.js")).then(function(){})}();return c&&c.then?c.then(t):t()}var a={sampleRate:{ideal:n},echoCancellation:{ideal:!0},noiseSuppression:{ideal:!0}},c=function(){if(u()&&r)return Promise.resolve(window.navigator.mediaDevices.enumerateDevices()).then(function(e){var t=e.find(function(e){return"audioinput"===e.kind&&["airpod","headphone","earphone"].find(function(t){return e.label.toLowerCase().includes(t)})});t&&(a.deviceId={ideal:t.deviceId})})}();return c&&c.then?c.then(t):t()}()}catch(e){return a(e)}return c&&c.then?c.then(void 0,a):c}(0,function(e){var t,n;throw null==(t=s)||t.getTracks().forEach(function(e){return e.stop()}),null==(n=i)||n.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}(),g=f("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'),y=/*#__PURE__*/function(){function e(e,t,n,o){this.context=void 0,this.analyser=void 0,this.gain=void 0,this.worklet=void 0,this.context=e,this.analyser=t,this.gain=n,this.worklet=o}return e.create=function(t){var n=t.sampleRate,o=t.format;try{var r=null;return Promise.resolve(function(t,i){try{var s=(a=(r=new AudioContext({sampleRate:n})).createAnalyser(),(u=r.createGain()).connect(a),a.connect(r.destination),Promise.resolve(g(r.audioWorklet)).then(function(){var t=new AudioWorkletNode(r,"audio-concat-processor");return t.port.postMessage({type:"setFormat",format:o}),t.connect(u),Promise.resolve(r.resume()).then(function(){return new e(r,a,u,t)})}))}catch(e){return i(e)}var a,u;return s&&s.then?s.then(void 0,i):s}(0,function(e){var t;throw null==(t=r)||t.close(),e}))}catch(e){return Promise.reject(e)}},e.prototype.close=function(){try{return Promise.resolve(this.context.close()).then(function(){})}catch(e){return Promise.reject(e)}},e}();function _(e,t){try{var n=e()}catch(e){return t(e)}return n&&n.then?n.then(void 0,t):n}var b=/*#__PURE__*/function(n){function o(e,t,o,r,i){var s;return(s=n.call(this,e,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){var t,n;"connected"===s.status&&s.connection.sendMessage({user_audio_chunk:(t=e.data[0].buffer,n=new Uint8Array(t),window.btoa(String.fromCharCode.apply(String,n)))})},s.onOutputWorkletMessage=function(e){var t=e.data;"process"===t.type&&s.updateMode(t.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:d(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 t=0,n=0;n<e.length;n++)t+=e[n]/255;return(t/=e.length)<0?0:t>1?1: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,n),o.startSession=function(t){try{var n=function(){return _(function(){return Promise.resolve(navigator.mediaDevices.getUserMedia({audio:!0})).then(function(n){return d=n,Promise.resolve(c(i.connectionDelay)).then(function(){return Promise.resolve(s.create(t)).then(function(n){return u=n,Promise.resolve(Promise.all([m.create(e({},u.inputFormat,{preferHeadphonesForIosDevices:t.preferHeadphonesForIosDevices})),y.create(u.outputFormat)])).then(function(e){var t;return a=e[0],l=e[1],null==(t=d)||t.getTracks().forEach(function(e){return e.stop()}),d=null,new o(i,u,a,l,h)})})})})},function(e){var t,n,o;return i.onStatusChange({status:"disconnected"}),null==(t=d)||t.getTracks().forEach(function(e){return e.stop()}),null==(n=u)||n.close(),Promise.resolve(null==(o=a)?void 0:o.close()).then(function(){var t;return Promise.resolve(null==(t=l)?void 0:t.close()).then(function(){function t(){throw e}var n=_(function(){var e;return Promise.resolve(null==(e=h)?void 0:e.release()).then(function(){h=null})},function(){});return n&&n.then?n.then(t):t()})})})},i=r.getFullOptions(t);i.onStatusChange({status:"connecting"}),i.onCanSendFeedbackChange({canSendFeedback:!1});var a=null,u=null,l=null,d=null,h=null,p=function(e){if(null==(e=t.useWakeLock)||e){var n=_(function(){return Promise.resolve(navigator.wakeLock.request("screen")).then(function(e){h=e})},function(){});if(n&&n.then)return n.then(function(){})}}();return Promise.resolve(p&&p.then?p.then(n):n())}catch(e){return Promise.reject(e)}};var i=o.prototype;return i.handleEndSession=function(){try{var e=this;return Promise.resolve(n.prototype.handleEndSession.call(e)).then(function(){function t(){return Promise.resolve(e.input.close()).then(function(){return Promise.resolve(e.output.close()).then(function(){})})}var n=_(function(){var t;return Promise.resolve(null==(t=e.wakeLock)?void 0:t.release()).then(function(){e.wakeLock=null})},function(){});return n&&n.then?n.then(t):t()})}catch(e){return Promise.reject(e)}},i.handleInterruption=function(e){n.prototype.handleInterruption.call(this,e),this.fadeOutAudio()},i.handleAudio=function(e){this.lastInterruptTimestamp<=e.audio_event.event_id&&(this.options.onAudio(e.audio_event.audio_base_64),this.addAudioBase64Chunk(e.audio_event.audio_base_64),this.currentEventId=e.audio_event.event_id,this.updateCanSendFeedback(),this.updateMode("speaking"))},i.setMicMuted=function(e){this.input.setMuted(e)},i.getInputByteFrequencyData=function(){return null!=this.inputFrequencyData||(this.inputFrequencyData=new Uint8Array(this.input.analyser.frequencyBinCount)),this.input.analyser.getByteFrequencyData(this.inputFrequencyData),this.inputFrequencyData},i.getOutputByteFrequencyData=function(){return null!=this.outputFrequencyData||(this.outputFrequencyData=new Uint8Array(this.output.analyser.frequencyBinCount)),this.output.analyser.getByteFrequencyData(this.outputFrequencyData),this.outputFrequencyData},i.getInputVolume=function(){return this.calculateVolume(this.getInputByteFrequencyData())},i.getOutputVolume=function(){return this.calculateVolume(this.getOutputByteFrequencyData())},o}(r);exports.Conversation=/*#__PURE__*/function(e){function n(){return e.apply(this,arguments)||this}return t(n,e),n.startSession=function(e){return e.textOnly?l.startSession(e):b.startSession(e)},n}(r),exports.postOverallFeedback=function(e,t,n){return void 0===n&&(n="https://api.elevenlabs.io"),fetch(n+"/v1/convai/conversations/"+e+"/feedback",{method:"POST",body:JSON.stringify({feedback:t?"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)}var r=new Uint8Array(0),i=/*#__PURE__*/function(){function e(e,n){var t=this,o=this,r=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=1,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(e)}))}catch(e){return Promise.reject(e)}},this.onMessage=function(e){try{switch(e.type){case"interruption":return r.handleInterruption(e),Promise.resolve();case"agent_response":return r.handleAgentResponse(e),Promise.resolve();case"user_transcript":return r.handleUserTranscript(e),Promise.resolve();case"internal_tentative_agent_response":return r.handleTentativeAgentResponse(e),Promise.resolve();case"client_tool_call":return Promise.resolve(r.handleClientToolCall(e)).then(function(){});case"audio":return r.handleAudio(e),Promise.resolve();case"ping":return r.connection.sendMessage({type:"pong",event_id:e.ping_event.event_id}),Promise.resolve();default:return r.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({conversationId:n.conversationId}),this.connection.onMessage(this.onMessage),this.connection.onDisconnect(this.endSessionWithDetails),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({mode:e}))},t.updateStatus=function(e){e!==this.status&&(this.status=e,this.options.onStatusChange({status:e}))},t.updateCanSendFeedback=function(){var e=this.currentEventId!==this.lastFeedbackEventId;this.canSendFeedback!==e&&(this.canSendFeedback=e,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({source:"ai",message:e.agent_response_event.agent_response})},t.handleUserTranscript=function(e){this.options.onMessage({source:"user",message:e.user_transcription_event.user_transcript})},t.handleTentativeAgentResponse=function(e){this.options.onDebug({type:"tentative_agent_response",response:e.tentative_agent_response_internal_event.tentative_agent_response})},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=function(t,o){try{var r=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})})}catch(e){return o(e)}return r&&r.then?r.then(void 0,o):r}(0,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(e,n)},t.getId=function(){return this.connection.conversationId},t.isOpen=function(){return"connected"===this.status},t.setMicMuted=function(e){},t.getInputByteFrequencyData=function(){return r},t.getOutputByteFrequencyData=function(){return r},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(){this.queue=[],this.disconnectionDetails=null,this.onDisconnectCallback=null,this.onMessageCallback=null}var n=e.prototype;return 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.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 a(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}}function c(e){return!!e.type}function u(e){var n,t,o,r,i,s={type:"conversation_initiation_client_data"};return e.overrides&&(s.conversation_config_override={agent:{prompt:null==(n=e.overrides.agent)?void 0:n.prompt,first_message:null==(t=e.overrides.agent)?void 0:t.firstMessage,language:null==(o=e.overrides.agent)?void 0:o.language},tts:{voice_id:null==(r=e.overrides.tts)?void 0:r.voiceId},conversation:{text_only:null==(i=e.overrides.conversation)?void 0:i.textOnly}}),e.customLlmExtraBody&&(s.custom_llm_extra_body=e.customLlmExtraBody),e.dynamicVariables&&(s.dynamic_variables=e.dynamicVariables),s}var l=/*#__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(!c(n))return;i.handleMessage(n)}catch(e){}}),i}t(n,e),n.create=function(e){try{var t=null;return Promise.resolve(function(o,r){try{var i=(l=null!=(s=e.origin)?s:"wss://api.elevenlabs.io",d=e.signedUrl?e.signedUrl:l+"/v1/convai/conversation?agent_id="+e.agentId,h=["convai"],e.authorization&&h.push("bearer."+e.authorization),t=new WebSocket(d,h),Promise.resolve(new Promise(function(n,o){t.addEventListener("open",function(){var n,o=u(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);c(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=a(null!=i?i:"pcm_16000"),c=a(r);return new n(t,o,s,c)}))}catch(e){return r(e)}var s,l,d,h;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))},n}(s);function d(e,n){try{var t=e()}catch(e){return n(e)}return t&&t.then?t.then(void 0,n):t}var h=/*#__PURE__*/function(n){function o(e,t,o,r){var i;return(i=n.call(this)||this).conversationId=void 0,i.inputFormat=void 0,i.outputFormat=void 0,i.room=void 0,i.isConnected=!1,i.room=e,i.conversationId=t,i.inputFormat=o,i.outputFormat=r,i.setupRoomEventListeners(),i}t(o,n),o.create=function(n){try{var t,r=function(r){var i=new e.Room;return d(function(){var r="webrtc-"+Date.now(),s=a("pcm_48000"),c=a("pcm_48000"),l=new o(i,r,s,c);return Promise.resolve(i.connect("wss://livekit.rtc.elevenlabs.io",t)).then(function(){return Promise.resolve(new Promise(function(n){if(l.isConnected)n();else{var t=function(){i.off(e.RoomEvent.Connected,t),n()};i.on(e.RoomEvent.Connected,t)}})).then(function(){return i.name&&(l.conversationId=i.name),Promise.resolve(i.localParticipant.setMicrophoneEnabled(!0)).then(function(){var e=u(n);return Promise.resolve(l.sendMessage(e)).then(function(){return l})})})})},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 d(function(){return Promise.resolve(fetch("https://api.elevenlabs.io/v1/convai/conversation/token?agent_id="+n.agentId)).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;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;c(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,o){try{if(n.kind===e.Track.Kind.Audio&&o.identity.includes("agent")){var r=n.attach();r.autoplay=!0,r.controls=!1,r.style.display="none",document.body.appendChild(r)}return Promise.resolve()}catch(e){return Promise.reject(e)}})},r.close=function(){this.isConnected&&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=d(function(){var t=(new TextEncoder).encode(JSON.stringify(e));return Promise.resolve(n.room.localParticipant.publishData(t,{reliable:!0})).then(function(){})},function(e){console.error("Failed to send message via WebRTC:",e),console.error("Error details:",e)});return Promise.resolve(t&&t.then?t.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},r.getRoom=function(){return this.room},o}(s),f=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(l.create(e));case"webrtc":return Promise.resolve(h.create(e));default:throw new Error("Unknown connection type: "+n)}}catch(e){return Promise.reject(e)}};function p(){return["iPad Simulator","iPhone Simulator","iPod Simulator","iPad","iPhone","iPod"].includes(navigator.platform)||navigator.userAgent.includes("Mac")&&"ontouchend"in document}var v=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(p()){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)}},m=/*#__PURE__*/function(e){function n(){return e.apply(this,arguments)||this}return t(n,e),n.startSession=function(e){try{var t=i.getFullOptions(e);t.onStatusChange({status:"connecting"}),t.onCanSendFeedbackChange({canSendFeedback:!1});var o=null;return Promise.resolve(function(r,i){try{var s=Promise.resolve(v(t.connectionDelay)).then(function(){return Promise.resolve(f(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({status:"disconnected"}),null==(n=o)||n.close(),e}))}catch(e){return Promise.reject(e)}},n}(i);function g(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 y(e,n){try{var t=e()}catch(e){return n(e)}return t&&t.then?t.then(void 0,n):t}var _=new Map;function w(e,n){return function(t){try{var o,r=function(r){return o?r:y(function(){var o="data:application/javascript;base64,"+btoa(n);return Promise.resolve(t.addModule(o)).then(function(){_.set(e,o)})},function(){throw new Error("Failed to load the "+e+" worklet module. Make sure the browser supports AudioWorklets.")})},i=_.get(e);if(i)return Promise.resolve(t.addModule(i));var s=new Blob([n],{type:"application/javascript"}),a=URL.createObjectURL(s),c=y(function(){return Promise.resolve(t.addModule(a)).then(function(){_.set(e,a),o=1})},function(){URL.revokeObjectURL(a)});return Promise.resolve(c&&c.then?c.then(r):r(c))}catch(e){return Promise.reject(e)}}}var b=w("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'),k=/*#__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(n){var t=n.sampleRate,o=n.format,r=n.preferHeadphonesForIosDevices;try{var i=null,s=null;return Promise.resolve(function(n,a){try{var c=function(){function n(){function n(){return Promise.resolve(b(i.audioWorklet)).then(function(){return Promise.resolve(navigator.mediaDevices.getUserMedia({audio:a})).then(function(n){var r=i.createMediaStreamSource(s=n),a=new AudioWorkletNode(i,"raw-audio-processor");return a.port.postMessage({type:"setFormat",format:o,sampleRate:t}),r.connect(c),c.connect(a),Promise.resolve(i.resume()).then(function(){return new e(i,c,a,s)})})})}var r=navigator.mediaDevices.getSupportedConstraints().sampleRate,c=(i=new window.AudioContext(r?{sampleRate:t}:{})).createAnalyser(),u=function(){if(!r)return Promise.resolve(i.audioWorklet.addModule("https://cdn.jsdelivr.net/npm/@alexanderolsen/libsamplerate-js@2.1.2/dist/libsamplerate.worklet.js")).then(function(){})}();return u&&u.then?u.then(n):n()}var a={sampleRate:{ideal:t},echoCancellation:{ideal:!0},noiseSuppression:{ideal:!0}},c=function(){if(p()&&r)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&&(a.deviceId={ideal:n.deviceId})})}();return c&&c.then?c.then(n):n()}()}catch(e){return a(e)}return c&&c.then?c.then(void 0,a):c}(0,function(e){var n,t;throw null==(n=s)||n.getTracks().forEach(function(e){return e.stop()}),null==(t=i)||t.close(),e}))}catch(e){return Promise.reject(e)}};var n=e.prototype;return n.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)}},n.setMuted=function(e){this.worklet.port.postMessage({type:"setMuted",isMuted:e})},e}(),P=w("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'),C=/*#__PURE__*/function(){function e(e,n,t,o){this.context=void 0,this.analyser=void 0,this.gain=void 0,this.worklet=void 0,this.context=e,this.analyser=n,this.gain=t,this.worklet=o}return e.create=function(n){var t=n.sampleRate,o=n.format;try{var r=null;return Promise.resolve(function(n,i){try{var s=(a=(r=new AudioContext({sampleRate:t})).createAnalyser(),(c=r.createGain()).connect(a),a.connect(r.destination),Promise.resolve(P(r.audioWorklet)).then(function(){var n=new AudioWorkletNode(r,"audio-concat-processor");return n.port.postMessage({type:"setFormat",format:o}),n.connect(c),Promise.resolve(r.resume()).then(function(){return new e(r,a,c,n)})}))}catch(e){return i(e)}var a,c;return s&&s.then?s.then(void 0,i):s}(0,function(e){var n;throw null==(n=r)||n.close(),e}))}catch(e){return Promise.reject(e)}},e.prototype.close=function(){try{return Promise.resolve(this.context.close()).then(function(){})}catch(e){return Promise.reject(e)}},e}();function S(e,n){try{var t=e()}catch(e){return n(e)}return t&&t.then?t.then(void 0,n):t}var M=/*#__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){var n,t;"connected"===s.status&&s.connection.sendMessage({user_audio_chunk:(n=e.data[0].buffer,t=new Uint8Array(n),window.btoa(String.fromCharCode.apply(String,t)))})},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:g(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.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 S(function(){return Promise.resolve(navigator.mediaDevices.getUserMedia({audio:!0})).then(function(t){return u=t,Promise.resolve(v(r.connectionDelay)).then(function(){return Promise.resolve(f(e)).then(function(t){return a=t,Promise.resolve(Promise.all([k.create(n({},a.inputFormat,{preferHeadphonesForIosDevices:e.preferHeadphonesForIosDevices})),C.create(a.outputFormat)])).then(function(e){var n;return s=e[0],c=e[1],null==(n=u)||n.getTracks().forEach(function(e){return e.stop()}),u=null,new o(r,a,s,c,l)})})})})},function(e){var n,t,o;return r.onStatusChange({status:"disconnected"}),null==(n=u)||n.getTracks().forEach(function(e){return e.stop()}),null==(t=a)||t.close(),Promise.resolve(null==(o=s)?void 0:o.close()).then(function(){var n;return Promise.resolve(null==(n=c)?void 0:n.close()).then(function(){function n(){throw e}var t=S(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=i.getFullOptions(e);r.onStatusChange({status:"connecting"}),r.onCanSendFeedbackChange({canSendFeedback:!1});var s=null,a=null,c=null,u=null,l=null,d=function(n){if(null==(n=e.useWakeLock)||n){var t=S(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=S(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){this.lastInterruptTimestamp<=e.audio_event.event_id&&(this.options.onAudio(e.audio_event.audio_base_64),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.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())},o}(i);exports.Conversation=/*#__PURE__*/function(e){function n(){return e.apply(this,arguments)||this}return t(n,e),n.startSession=function(e){return e.textOnly?m.startSession(e):M.startSession(e)},n}(i),exports.WebRTCConnection=h,exports.WebSocketConnection=l,exports.createConnection=f,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