@elevenlabs/client 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +326 -0
- package/dist/BaseConversation.d.ts +75 -0
- package/dist/TextConversation.d.ts +4 -0
- package/dist/VoiceConversation.d.ts +27 -0
- package/dist/index.d.ts +9 -0
- package/dist/lib.cjs +2 -0
- package/dist/lib.cjs.map +1 -0
- package/dist/lib.modern.js +2 -0
- package/dist/lib.modern.js.map +1 -0
- package/dist/lib.module.js +2 -0
- package/dist/lib.module.js.map +1 -0
- package/dist/lib.umd.js +2 -0
- package/dist/lib.umd.js.map +1 -0
- package/dist/utils/applyDelay.d.ts +2 -0
- package/dist/utils/audio.d.ts +2 -0
- package/dist/utils/audioConcatProcessor.d.ts +1 -0
- package/dist/utils/compatibility.d.ts +2 -0
- package/dist/utils/connection.d.ts +70 -0
- package/dist/utils/createWorkletModuleLoader.d.ts +1 -0
- package/dist/utils/events.d.ts +108 -0
- package/dist/utils/input.d.ts +14 -0
- package/dist/utils/output.d.ts +10 -0
- package/dist/utils/postOverallFeedback.d.ts +1 -0
- package/dist/utils/rawAudioProcessor.d.ts +1 -0
- package/package.json +47 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 ElevenLabs
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
# ElevenLabs JavaScript Client Library
|
|
2
|
+
|
|
3
|
+
An SDK library for using ElevenLabs in browser based applications. If you're looking for a Node.js library, please refer to the [ElevenLabs Node.js Library](https://www.npmjs.com/package/elevenlabs).
|
|
4
|
+
|
|
5
|
+
> Note that this library is launching to primarily support Conversational AI. The support for speech synthesis and other more generic use cases is planned for the future.
|
|
6
|
+
|
|
7
|
+

|
|
8
|
+
[](https://discord.gg/elevenlabs)
|
|
9
|
+
[](https://twitter.com/elevenlabsio)
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
Install the package in your project through package manager.
|
|
14
|
+
|
|
15
|
+
```shell
|
|
16
|
+
npm install @elevenlabs/client
|
|
17
|
+
# or
|
|
18
|
+
yarn add @elevenlabs/client
|
|
19
|
+
# or
|
|
20
|
+
pnpm install @elevenlabs/client
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
This library is primarily meant for development in vanilla JavaScript projects, or as a base for libraries tailored to specific frameworks.
|
|
26
|
+
It is recommended to check whether your specific framework has it's own library.
|
|
27
|
+
However, you can use this library in any JavaScript-based project.
|
|
28
|
+
|
|
29
|
+
### Initialize conversation
|
|
30
|
+
|
|
31
|
+
First, initialize the Conversation instance:
|
|
32
|
+
|
|
33
|
+
```js
|
|
34
|
+
const conversation = await Conversation.startSession(options);
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
This will kick off the websocket connection and start using microphone to communicate with the ElevenLabs Conversational AI agent. Consider explaining and allowing microphone access in your apps UI before the Conversation kicks off. The microphone may also be blocked for the current page by default, resulting in the allow prompt not showing up at all. You should handle such use case in your application and display appropriate message to the user:
|
|
38
|
+
|
|
39
|
+
```js
|
|
40
|
+
// call after explaning to the user why the microphone access is needed
|
|
41
|
+
// handle errors and show appropriate message to the user
|
|
42
|
+
try {
|
|
43
|
+
await navigator.mediaDevices.getUserMedia();
|
|
44
|
+
} catch {
|
|
45
|
+
// handle error
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
#### Session configuration
|
|
50
|
+
|
|
51
|
+
The options passed to `startSession` specifiy how the session is established. There are two ways to start a session:
|
|
52
|
+
|
|
53
|
+
##### Using Agent ID
|
|
54
|
+
|
|
55
|
+
Agent ID can be acquired through [ElevenLabs UI](https://elevenlabs.io/app/conversational-ai).
|
|
56
|
+
For public agents, you can use the ID directly:
|
|
57
|
+
|
|
58
|
+
```js
|
|
59
|
+
const conversation = await Conversation.startSession({
|
|
60
|
+
agentId: "<your-agent-id>",
|
|
61
|
+
});
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
##### Using a signed URL
|
|
65
|
+
|
|
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.
|
|
68
|
+
|
|
69
|
+
Here's an example of how it could be set up:
|
|
70
|
+
|
|
71
|
+
```js
|
|
72
|
+
// Node.js server
|
|
73
|
+
|
|
74
|
+
app.get("/signed-url", yourAuthMiddleware, async (req, res) => {
|
|
75
|
+
const response = await fetch(
|
|
76
|
+
`https://api.elevenlabs.io/v1/convai/conversation/get_signed_url?agent_id=${process.env.AGENT_ID}`,
|
|
77
|
+
{
|
|
78
|
+
method: "GET",
|
|
79
|
+
headers: {
|
|
80
|
+
// Requesting a signed url requires your ElevenLabs API key
|
|
81
|
+
// Do NOT expose your API key to the client!
|
|
82
|
+
"xi-api-key": process.env.XI_API_KEY,
|
|
83
|
+
},
|
|
84
|
+
}
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
if (!response.ok) {
|
|
88
|
+
return res.status(500).send("Failed to get signed URL");
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const body = await response.json();
|
|
92
|
+
res.send(body.signed_url);
|
|
93
|
+
});
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
```js
|
|
97
|
+
// Client
|
|
98
|
+
|
|
99
|
+
const response = await fetch("/signed-url", yourAuthHeaders);
|
|
100
|
+
const signedUrl = await response.text();
|
|
101
|
+
|
|
102
|
+
const conversation = await Conversation.startSession({ signedUrl });
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
#### Optional callbacks
|
|
106
|
+
|
|
107
|
+
The options passed to `startSession` can also be used to register optional callbacks:
|
|
108
|
+
|
|
109
|
+
- **onConnect** - handler called when the conversation websocket connection is established.
|
|
110
|
+
- **onDisconnect** - handler called when the conversation websocket connection is ended.
|
|
111
|
+
- **onMessage** - handler called when a new text message is received. These can be tentative or final transcriptions of user voice, replies produced by LLM. Primarily used for handling conversation transcription.
|
|
112
|
+
- **onError** - handler called when an error is encountered.
|
|
113
|
+
- **onStatusChange** - handler called whenever connection status changes. Can be `connected`, `connecting` and `disconnected` (initial).
|
|
114
|
+
- **onModeChange** - handler called when a status changes, eg. agent switches from `speaking` to `listening`, or the other way around.
|
|
115
|
+
- **onCanSendFeedbackChange** - handler called when sending feedback becomes available or unavailable.
|
|
116
|
+
|
|
117
|
+
#### Client Tools
|
|
118
|
+
|
|
119
|
+
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.
|
|
120
|
+
|
|
121
|
+
Client tools definition is an object of functions, and needs to be identical with your configuration within the [ElevenLabs UI](https://elevenlabs.io/app/conversational-ai), where you can name and describe different tools, as well as set up the parameters passed by the agent.
|
|
122
|
+
|
|
123
|
+
```ts
|
|
124
|
+
const conversation = await Conversation.startSession({
|
|
125
|
+
clientTools: {
|
|
126
|
+
displayMessage: async (parameters: { text: string }) => {
|
|
127
|
+
alert(text);
|
|
128
|
+
|
|
129
|
+
return "Message displayed";
|
|
130
|
+
},
|
|
131
|
+
},
|
|
132
|
+
});
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
In case function returns a value, it will be passed back to the agent as a response.
|
|
136
|
+
Note that the tool needs to be explicitly set to be blocking conversation in ElevenLabs UI for the agent to await and react to the response, otherwise agent assumes success and continues the conversation.
|
|
137
|
+
|
|
138
|
+
#### Conversation overrides
|
|
139
|
+
|
|
140
|
+
You may choose to override various settings of the conversation and set them dynamically based other user interactions.
|
|
141
|
+
We support overriding various settings.
|
|
142
|
+
These settings are optional and can be used to customize the conversation experience.
|
|
143
|
+
The following settings are available:
|
|
144
|
+
|
|
145
|
+
```ts
|
|
146
|
+
const conversation = await Conversation.startSession({
|
|
147
|
+
overrides: {
|
|
148
|
+
agent: {
|
|
149
|
+
prompt: {
|
|
150
|
+
prompt: "My custom prompt",
|
|
151
|
+
},
|
|
152
|
+
firstMessage: "My custom first message",
|
|
153
|
+
language: "en",
|
|
154
|
+
},
|
|
155
|
+
tts: {
|
|
156
|
+
voiceId: "custom voice id",
|
|
157
|
+
},
|
|
158
|
+
conversation: {
|
|
159
|
+
textOnly: true,
|
|
160
|
+
},
|
|
161
|
+
},
|
|
162
|
+
});
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
#### Text only
|
|
166
|
+
|
|
167
|
+
If your agent is configured to run in text-only mode, i.e. it does not send or receive audio messages,
|
|
168
|
+
you can use this flag to use a lighter version of the conversation. In that case, the
|
|
169
|
+
user will not be asked for microphone permissions and no audio context will be created.
|
|
170
|
+
|
|
171
|
+
```ts
|
|
172
|
+
const conversation = await Conversation.startSession({
|
|
173
|
+
textOnly: true,
|
|
174
|
+
});
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
#### Prefer Headphones for iOS Devices
|
|
178
|
+
|
|
179
|
+
While this SDK leaves the choice of audio input/output device to the browser/system, iOS Safari seem to prefer the built-in speaker over headphones even when bluetooth device is in use. If you want to "force" the use of headphones on iOS devices when available, you can use the following option. Please, keep in mind that this is not guaranteed, since this functionality is not provided by the browser. System audio should be the default choice.
|
|
180
|
+
|
|
181
|
+
```ts
|
|
182
|
+
const conversation = await Conversation.startSession({
|
|
183
|
+
preferHeadphonesForIosDevices: true,
|
|
184
|
+
});
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
#### Connection delay
|
|
188
|
+
|
|
189
|
+
You can configure additional delay between when the microphone is activated and when the connection is established.
|
|
190
|
+
On Android, the delay is set to 3 seconds by default to make sure the device has time to switch to the correct audio mode.
|
|
191
|
+
Without it, you may experience issues with the beginning of the first message being cut off.
|
|
192
|
+
|
|
193
|
+
```ts
|
|
194
|
+
const conversation = await Conversation.startSession({
|
|
195
|
+
connectionDelay: {
|
|
196
|
+
android: 3_000,
|
|
197
|
+
ios: 0,
|
|
198
|
+
default: 0,
|
|
199
|
+
},
|
|
200
|
+
});
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
#### Acquiring a Wake Lock
|
|
204
|
+
|
|
205
|
+
By default, the conversation will attempt to acquire a [wake lock](https://developer.mozilla.org/en-US/docs/Web/API/Screen_Wake_Lock_API) to prevent the device from going to sleep during the conversation.
|
|
206
|
+
This can be disabled by setting the `useWakeLock` option to `false`:
|
|
207
|
+
|
|
208
|
+
```ts
|
|
209
|
+
const conversation = await Conversation.startSession({
|
|
210
|
+
useWakeLock: false,
|
|
211
|
+
});
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
#### Return value
|
|
215
|
+
|
|
216
|
+
`startSession` returns a `Conversation` instance that can be used to control the session. The method will throw an error if the session cannot be established. This can happen if the user denies microphone access, or if the websocket connection
|
|
217
|
+
fails.
|
|
218
|
+
|
|
219
|
+
##### endSession
|
|
220
|
+
|
|
221
|
+
A method to manually end the conversation. The method will end the conversation and disconnect from websocket.
|
|
222
|
+
Afterwards the conversation instance will be unusable and can be safely discarded.
|
|
223
|
+
|
|
224
|
+
```js
|
|
225
|
+
await conversation.endSession();
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
##### sendFeedback
|
|
229
|
+
|
|
230
|
+
A method for sending binary feedback to the agent.
|
|
231
|
+
The method accepts a boolean value, where `true` represents positive feedback and `false` negative feedback.
|
|
232
|
+
Feedback is always correlated to the most recent agent response and can be sent only once per response.
|
|
233
|
+
You can listen to `onCanSendFeedbackChange` to know if feedback can be sent at the given moment.
|
|
234
|
+
|
|
235
|
+
```js
|
|
236
|
+
conversation.sendFeedback(true);
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
##### sendContextualUpdate
|
|
240
|
+
|
|
241
|
+
A method to send contextual updates to the agent.
|
|
242
|
+
This can be used to inform the agent about user actions that are not directly related to the conversation, but may influence the agent's responses.
|
|
243
|
+
|
|
244
|
+
```js
|
|
245
|
+
conversation.sendContextualUpdate(
|
|
246
|
+
"User navigated to another page. Consider it for next response, but don't react to this contextual update."
|
|
247
|
+
);
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
##### sendUserMessage
|
|
251
|
+
|
|
252
|
+
Sends a text messages to the agent.
|
|
253
|
+
|
|
254
|
+
Can be used to let the user type in the message instead of using the microphone.
|
|
255
|
+
Unlike `sendContextualUpdate`, this will be treated as a user message and will prompt the agent to take its turn in the conversation.
|
|
256
|
+
|
|
257
|
+
```js
|
|
258
|
+
sendButton.addEventListener("click", e => {
|
|
259
|
+
conversation.sendUserMessage(textInput.value);
|
|
260
|
+
textInput.value = "";
|
|
261
|
+
});
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
##### sendUserActivity
|
|
265
|
+
|
|
266
|
+
Notifies the agent about user activity.
|
|
267
|
+
|
|
268
|
+
The agent will not attempt to speak for at least 2 seconds after the user activity is detected.
|
|
269
|
+
This can be used to prevent the agent from interrupting the user when they are typing.
|
|
270
|
+
|
|
271
|
+
```js
|
|
272
|
+
textInput.addEventListener("input", () => {
|
|
273
|
+
conversation.sendUserActivity();
|
|
274
|
+
});
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
##### getId
|
|
278
|
+
|
|
279
|
+
A method returning the conversation ID.
|
|
280
|
+
|
|
281
|
+
```js
|
|
282
|
+
const id = conversation.getId();
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
##### setVolume
|
|
286
|
+
|
|
287
|
+
A method to set the output volume of the conversation. Accepts object with volume field between 0 and 1.
|
|
288
|
+
|
|
289
|
+
```js
|
|
290
|
+
await conversation.setVolume({ volume: 0.5 });
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
##### muteMic
|
|
294
|
+
|
|
295
|
+
A method to mute/unmute the microphone.
|
|
296
|
+
|
|
297
|
+
```js
|
|
298
|
+
// Mute the microphone
|
|
299
|
+
conversation.setMicMuted(true);
|
|
300
|
+
|
|
301
|
+
// Unmute the microphone
|
|
302
|
+
conversation.setMicMuted(false);
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
##### getInputVolume / getOutputVolume
|
|
306
|
+
|
|
307
|
+
Methods that return the current input/output volume on a scale from `0` to `1` where `0` is -100 dB and `1` is -30 dB.
|
|
308
|
+
|
|
309
|
+
```js
|
|
310
|
+
const inputVolume = await conversation.getInputVolume();
|
|
311
|
+
const outputVolume = await conversation.getOutputVolume();
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
##### getInputByteFrequencyData / getOutputByteFrequencyData
|
|
315
|
+
|
|
316
|
+
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.
|
|
317
|
+
|
|
318
|
+
## Development
|
|
319
|
+
|
|
320
|
+
Please, refer to the README.md file in the root of this repository.
|
|
321
|
+
|
|
322
|
+
## Contributing
|
|
323
|
+
|
|
324
|
+
Please, create an issue first to discuss the proposed changes. Any contributions are welcome!
|
|
325
|
+
|
|
326
|
+
Remember, if merged, your code will be used as part of a MIT licensed project. By submitting a Pull Request, you are giving your consent for your code to be integrated into this library.
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { Connection, type OnDisconnectCallback, type SessionConfig } from "./utils/connection";
|
|
2
|
+
import { AgentAudioEvent, AgentResponseEvent, ClientToolCallEvent, InternalTentativeAgentResponseEvent, InterruptionEvent, UserTranscriptionEvent } from "./utils/events";
|
|
3
|
+
import type { InputConfig } from "./utils/input";
|
|
4
|
+
export type Role = "user" | "ai";
|
|
5
|
+
export type Mode = "speaking" | "listening";
|
|
6
|
+
export type Status = "connecting" | "connected" | "disconnecting" | "disconnected";
|
|
7
|
+
export type Options = SessionConfig & Callbacks & ClientToolsConfig & InputConfig;
|
|
8
|
+
export type PartialOptions = SessionConfig & Partial<Callbacks> & Partial<ClientToolsConfig> & Partial<InputConfig>;
|
|
9
|
+
export type ClientToolsConfig = {
|
|
10
|
+
clientTools: Record<string, (parameters: any) => Promise<string | number | void> | string | number | void>;
|
|
11
|
+
};
|
|
12
|
+
export type Callbacks = {
|
|
13
|
+
onConnect: (props: {
|
|
14
|
+
conversationId: string;
|
|
15
|
+
}) => void;
|
|
16
|
+
onDebug: (props: any) => void;
|
|
17
|
+
onDisconnect: OnDisconnectCallback;
|
|
18
|
+
onError: (message: string, context?: any) => void;
|
|
19
|
+
onMessage: (props: {
|
|
20
|
+
message: string;
|
|
21
|
+
source: Role;
|
|
22
|
+
}) => void;
|
|
23
|
+
onAudio: (base64Audio: string) => void;
|
|
24
|
+
onModeChange: (prop: {
|
|
25
|
+
mode: Mode;
|
|
26
|
+
}) => void;
|
|
27
|
+
onStatusChange: (prop: {
|
|
28
|
+
status: Status;
|
|
29
|
+
}) => void;
|
|
30
|
+
onCanSendFeedbackChange: (prop: {
|
|
31
|
+
canSendFeedback: boolean;
|
|
32
|
+
}) => void;
|
|
33
|
+
onUnhandledClientToolCall?: (params: ClientToolCallEvent["client_tool_call"]) => void;
|
|
34
|
+
};
|
|
35
|
+
export declare class BaseConversation {
|
|
36
|
+
protected readonly options: Options;
|
|
37
|
+
protected readonly connection: Connection;
|
|
38
|
+
protected lastInterruptTimestamp: number;
|
|
39
|
+
protected mode: Mode;
|
|
40
|
+
protected status: Status;
|
|
41
|
+
protected volume: number;
|
|
42
|
+
protected currentEventId: number;
|
|
43
|
+
protected lastFeedbackEventId: number;
|
|
44
|
+
protected canSendFeedback: boolean;
|
|
45
|
+
protected static getFullOptions(partialOptions: PartialOptions): Options;
|
|
46
|
+
protected constructor(options: Options, connection: Connection);
|
|
47
|
+
endSession(): Promise<void>;
|
|
48
|
+
private endSessionWithDetails;
|
|
49
|
+
protected handleEndSession(): Promise<void>;
|
|
50
|
+
protected updateMode(mode: Mode): void;
|
|
51
|
+
protected updateStatus(status: Status): void;
|
|
52
|
+
protected updateCanSendFeedback(): void;
|
|
53
|
+
protected handleInterruption(event: InterruptionEvent): void;
|
|
54
|
+
protected handleAgentResponse(event: AgentResponseEvent): void;
|
|
55
|
+
protected handleUserTranscript(event: UserTranscriptionEvent): void;
|
|
56
|
+
protected handleTentativeAgentResponse(event: InternalTentativeAgentResponseEvent): void;
|
|
57
|
+
protected handleClientToolCall(event: ClientToolCallEvent): Promise<void>;
|
|
58
|
+
protected handleAudio(event: AgentAudioEvent): void;
|
|
59
|
+
private onMessage;
|
|
60
|
+
private onError;
|
|
61
|
+
getId(): string;
|
|
62
|
+
isOpen(): boolean;
|
|
63
|
+
setVolume: ({ volume }: {
|
|
64
|
+
volume: number;
|
|
65
|
+
}) => void;
|
|
66
|
+
setMicMuted(isMuted: boolean): void;
|
|
67
|
+
getInputByteFrequencyData(): Uint8Array;
|
|
68
|
+
getOutputByteFrequencyData(): Uint8Array;
|
|
69
|
+
getInputVolume(): number;
|
|
70
|
+
getOutputVolume(): number;
|
|
71
|
+
sendFeedback(like: boolean): void;
|
|
72
|
+
sendContextualUpdate(text: string): void;
|
|
73
|
+
sendUserMessage(text: string): void;
|
|
74
|
+
sendUserActivity(): void;
|
|
75
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Input } from "./utils/input";
|
|
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";
|
|
6
|
+
export declare class VoiceConversation extends BaseConversation {
|
|
7
|
+
readonly input: Input;
|
|
8
|
+
readonly output: Output;
|
|
9
|
+
wakeLock: WakeLockSentinel | null;
|
|
10
|
+
static startSession(options: PartialOptions): Promise<VoiceConversation>;
|
|
11
|
+
private inputFrequencyData?;
|
|
12
|
+
private outputFrequencyData?;
|
|
13
|
+
protected constructor(options: Options, connection: Connection, input: Input, output: Output, wakeLock: WakeLockSentinel | null);
|
|
14
|
+
protected handleEndSession(): Promise<void>;
|
|
15
|
+
protected handleInterruption(event: InterruptionEvent): void;
|
|
16
|
+
protected handleAudio(event: AgentAudioEvent): void;
|
|
17
|
+
private onInputWorkletMessage;
|
|
18
|
+
private onOutputWorkletMessage;
|
|
19
|
+
private addAudioBase64Chunk;
|
|
20
|
+
private fadeOutAudio;
|
|
21
|
+
private calculateVolume;
|
|
22
|
+
setMicMuted(isMuted: boolean): void;
|
|
23
|
+
getInputByteFrequencyData(): Uint8Array;
|
|
24
|
+
getOutputByteFrequencyData(): Uint8Array;
|
|
25
|
+
getInputVolume(): number;
|
|
26
|
+
getOutputVolume(): number;
|
|
27
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { BaseConversation, PartialOptions } from "./BaseConversation";
|
|
2
|
+
export type { Mode, Role, Options, PartialOptions, ClientToolsConfig, Callbacks, Status, } from "./BaseConversation";
|
|
3
|
+
export type { InputConfig } from "./utils/input";
|
|
4
|
+
export type { IncomingSocketEvent } from "./utils/events";
|
|
5
|
+
export type { SessionConfig, DisconnectionDetails, Language, } from "./utils/connection";
|
|
6
|
+
export { postOverallFeedback } from "./utils/postOverallFeedback";
|
|
7
|
+
export declare class Conversation extends BaseConversation {
|
|
8
|
+
static startSession(options: PartialOptions): Promise<Conversation>;
|
|
9
|
+
}
|
package/dist/lib.cjs
ADDED
|
@@ -0,0 +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.onDisconnect(this.endSessionWithDetails),this.connection.onMessage(this.onMessage),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"})},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,this.queue.forEach(e),this.queue=[]},t.onDisconnect=function(e){this.onDisconnectCallback=e,this.disconnectionDetails&&e(this.disconnectionDetails)},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"}})};
|
|
2
|
+
//# sourceMappingURL=lib.cjs.map
|