@elevenlabs/react 0.2.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +80 -21
  2. package/package.json +2 -2
package/README.md CHANGED
@@ -24,7 +24,7 @@ pnpm install @elevenlabs/react
24
24
 
25
25
  ### useConversation
26
26
 
27
- React hook for managing websocket connection and audio usage for ElevenLabs Conversational AI.
27
+ React hook for managing WebSocket and WebRTC connections and audio usage for ElevenLabs Conversational AI.
28
28
 
29
29
  #### Initialize conversation
30
30
 
@@ -60,8 +60,8 @@ const conversation = useConversation({
60
60
  - **clientTools** - object definition for client tools that can be invoked by agent. [See below](#client-tools) for details.
61
61
  - **overrides** - object definition conversations settings overrides. [See below](#conversation-overrides) for details.
62
62
  - **textOnly** - whether the conversation should run in text-only mode. [See below](#text-only) for details.
63
- - **onConnect** - handler called when the conversation websocket connection is established.
64
- - **onDisconnect** - handler called when the conversation websocket connection is ended.
63
+ - **onConnect** - handler called when the conversation connection is established.
64
+ - **onDisconnect** - handler called when the conversation connection has ended.
65
65
  - **onMessage** - handler called when a new message is received. These can be tentative or final transcriptions of user voice, replies produced by LLM, or debug message when a debug option is enabled.
66
66
  - **onError** - handler called when a error is encountered.
67
67
 
@@ -83,7 +83,7 @@ const conversation = useConversation({
83
83
  });
84
84
  ```
85
85
 
86
- In case function returns a value, it will be passed back to the agent as a response.
86
+ In case function returns a value, it will be passed back to the agent as a response.
87
87
  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.
88
88
 
89
89
  #### Conversation overrides
@@ -166,41 +166,100 @@ const conversation = useConversation({
166
166
 
167
167
  ##### startConversation
168
168
 
169
- `startConversation` method kick off the websocket connection and starts using microphone to communicate with the ElevenLabs Conversational AI agent.
170
- The method accepts options object, with the `url` or `agentId` option being required.
169
+ `startConversation` method kicks off the WebSocket or WebRTC connection and starts using the microphone to communicate with the ElevenLabs Conversational AI agent. The method accepts an options object, with the `signedUrl`, `conversationToken` or `agentId` option being required.
171
170
 
172
171
  Agent ID can be acquired through [ElevenLabs UI](https://elevenlabs.io/app/conversational-ai) and is always necessary.
173
172
 
174
173
  ```js
175
174
  const conversation = useConversation();
176
- const conversationId = await conversation.startSession({ url });
175
+
176
+ // For public agents, pass in the agent ID and the connection type
177
+ const conversationId = await conversation.startSession({
178
+ agentId: '<your-agent-id>',
179
+ connectionType: 'webrtc', // either 'webrtc' or 'websocket'
180
+ });
177
181
  ```
178
182
 
179
- For the public agents, define `agentId` - no signed link generation necessary.
183
+ For public agents (i.e. agents that don't have authentication enabled), define `agentId` - no signed link generation necessary.
180
184
 
181
- In case the conversation requires authorization, use the REST API to generate signed links. Use the signed link as a `url` parameter.
185
+ In case the conversation requires authorization, use the REST API to generate signed links for a WebSocket connection or a conversation token for a WebRTC connection.
182
186
 
183
187
  `startSession` returns promise resolving to `conversationId`. The value is a globally unique conversation ID you can use to identify separate conversations.
184
188
 
189
+ For WebSocket connections:
190
+
185
191
  ```js
186
- // your server
187
- const requestHeaders: HeadersInit = new Headers();
188
- requestHeaders.set("xi-api-key", process.env.XI_API_KEY); // use your ElevenLabs API key
192
+ // Node.js server
189
193
 
190
- const response = await fetch(
191
- "https://api.elevenlabs.io/v1/convai/conversation/get_signed_url?agent_id={{agent id created through ElevenLabs UI}}",
194
+ app.get("/signed-url", yourAuthMiddleware, async (req, res) => {
195
+ const response = await fetch(
196
+ `https://api.elevenlabs.io/v1/convai/conversation/get-signed-url?agent_id=${process.env.AGENT_ID}`,
192
197
  {
193
- method: "GET",
194
- headers: requestHeaders,
198
+ headers: {
199
+ // Requesting a signed url requires your ElevenLabs API key
200
+ // Do NOT expose your API key to the client!
201
+ "xi-api-key": process.env.ELEVENLABS_API_KEY,
202
+ },
195
203
  }
204
+ );
205
+
206
+ if (!response.ok) {
207
+ return res.status(500).send("Failed to get signed URL");
208
+ }
209
+
210
+ const body = await response.json();
211
+ res.send(body.signed_url);
212
+ });
213
+ ```
214
+
215
+ ```js
216
+ // Client
217
+
218
+ const response = await fetch("/signed-url", yourAuthHeaders);
219
+ const signedUrl = await response.text();
220
+
221
+ const conversation = await Conversation.startSession({
222
+ signedUrl,
223
+ connectionType: 'websocket',
224
+ });
225
+ ```
226
+
227
+ For WebRTC connections:
228
+
229
+ ```js
230
+ // Node.js server
231
+
232
+ app.get("/conversation-token", yourAuthMiddleware, async (req, res) => {
233
+ const response = await fetch(
234
+ `https://api.elevenlabs.io/v1/convai/conversation/token?agent_id=${process.env.AGENT_ID}`,
235
+ {
236
+ headers: {
237
+ // Requesting a conversation token requires your ElevenLabs API key
238
+ // Do NOT expose your API key to the client!
239
+ 'xi-api-key': process.env.ELEVENLABS_API_KEY,
240
+ }
241
+ }
242
+ );
243
+
244
+ if (!response.ok) {
245
+ return res.status(500).send("Failed to get conversation token");
246
+ }
247
+
248
+ const body = await response.json();
249
+ res.send(body.token);
196
250
  );
251
+ ```
197
252
 
198
- if (!response.ok) {
199
- return Response.error();
200
- }
253
+ ```js
254
+ // Client
255
+
256
+ const response = await fetch("/conversation-token", yourAuthHeaders);
257
+ const conversationToken = await response.text();
201
258
 
202
- const body = await response.json();
203
- const url = body.signed_url; // use this URL for startConversation method.
259
+ const conversation = await Conversation.startSession({
260
+ conversationToken,
261
+ connectionType: 'webrtc',
262
+ });
204
263
  ```
205
264
 
206
265
  ##### endSession
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@elevenlabs/react",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "description": "ElevenLabs React Library",
5
5
  "main": "./dist/lib.umd.js",
6
6
  "module": "./dist/lib.module.js",
@@ -19,7 +19,7 @@
19
19
  "author": "ElevenLabs",
20
20
  "license": "MIT",
21
21
  "dependencies": {
22
- "@elevenlabs/client": "0.2.0"
22
+ "@elevenlabs/client": "0.2.2"
23
23
  },
24
24
  "peerDependencies": {
25
25
  "react": ">=16.8.0"