@elevenlabs/react-native 0.1.2 → 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.
@@ -1,7 +1,7 @@
1
1
  import { useEffect } from 'react';
2
2
  import { useLocalParticipant, useDataChannel } from '@livekit/react-native';
3
3
  import type { LocalParticipant } from 'livekit-client';
4
- import type { Callbacks, ClientToolsConfig, ClientToolCallEvent } from '../types';
4
+ import type { Callbacks, ClientToolsConfig, ClientToolCallEvent, ConversationEvent } from '../types';
5
5
  import React from 'react';
6
6
 
7
7
  interface MessageHandlerProps {
@@ -13,6 +13,10 @@ interface MessageHandlerProps {
13
13
  updateCurrentEventId?: (eventId: number) => void;
14
14
  }
15
15
 
16
+ export function isValidEvent(event: any): event is ConversationEvent {
17
+ return !!event.type;
18
+ }
19
+
16
20
  export const MessageHandler = ({
17
21
  onReady,
18
22
  isConnected,
@@ -36,13 +40,13 @@ export const MessageHandler = ({
36
40
  if (
37
41
  Object.prototype.hasOwnProperty.call(
38
42
  clientTools,
39
- clientToolCall.tool_name
43
+ clientToolCall.client_tool_call.tool_name
40
44
  )
41
45
  ) {
42
46
  try {
43
47
  const result =
44
- (await clientTools[clientToolCall.tool_name](
45
- clientToolCall.parameters
48
+ (await clientTools[clientToolCall.client_tool_call.tool_name](
49
+ clientToolCall.client_tool_call.parameters
46
50
  )) ?? "Client tool execution successful."; // default client-tool call response
47
51
 
48
52
  // The API expects result to be a string, so we need to convert it if it's not already a string
@@ -51,18 +55,18 @@ export const MessageHandler = ({
51
55
 
52
56
  sendMessage({
53
57
  type: "client_tool_result",
54
- tool_call_id: clientToolCall.tool_call_id,
58
+ tool_call_id: clientToolCall.client_tool_call.tool_call_id,
55
59
  result: formattedResult,
56
60
  is_error: false,
57
61
  });
58
62
  } catch (e) {
59
63
  const errorMessage = `Client tool execution failed with following error: ${(e as Error)?.message}`;
60
64
  callbacks.onError?.(errorMessage, {
61
- clientToolName: clientToolCall.tool_name,
65
+ clientToolName: clientToolCall.client_tool_call.tool_name,
62
66
  });
63
67
  sendMessage({
64
68
  type: "client_tool_result",
65
- tool_call_id: clientToolCall.tool_call_id,
69
+ tool_call_id: clientToolCall.client_tool_call.tool_call_id,
66
70
  result: `Client tool execution failed: ${(e as Error)?.message}`,
67
71
  is_error: true,
68
72
  });
@@ -73,13 +77,13 @@ export const MessageHandler = ({
73
77
  return;
74
78
  }
75
79
 
76
- const errorMessage = `Client tool with name ${clientToolCall.tool_name} is not defined on client`;
80
+ const errorMessage = `Client tool with name ${clientToolCall.client_tool_call.tool_name} is not defined on client`;
77
81
  callbacks.onError?.(errorMessage, {
78
- clientToolName: clientToolCall.tool_name,
82
+ clientToolName: clientToolCall.client_tool_call.tool_name,
79
83
  });
80
84
  sendMessage({
81
85
  type: "client_tool_result",
82
- tool_call_id: clientToolCall.tool_call_id,
86
+ tool_call_id: clientToolCall.client_tool_call.tool_call_id,
83
87
  result: errorMessage,
84
88
  is_error: true,
85
89
  });
@@ -90,6 +94,14 @@ export const MessageHandler = ({
90
94
  const decoder = new TextDecoder();
91
95
  const message = JSON.parse(decoder.decode(msg.payload));
92
96
 
97
+ if (!isValidEvent(message)) {
98
+ callbacks.onDebug?.({
99
+ type: "invalid_event",
100
+ message,
101
+ });
102
+ return;
103
+ }
104
+
93
105
  callbacks.onMessage?.({
94
106
  message,
95
107
  source: msg.from?.isAgent ? 'ai' : 'user',
@@ -115,7 +127,7 @@ export const MessageHandler = ({
115
127
  });
116
128
  break;
117
129
  case "client_tool_call":
118
- handleClientToolCall(message.client_tool_call);
130
+ handleClientToolCall(message);
119
131
  break;
120
132
  default:
121
133
  callbacks.onDebug?.(message);
package/src/index.ts CHANGED
@@ -1,4 +1,8 @@
1
- export { ElevenLabsProvider, useConversation } from "./ElevenLabsProvider";
1
+ export {
2
+ ElevenLabsProvider,
3
+ useConversation,
4
+ type Conversation,
5
+ } from "./ElevenLabsProvider";
2
6
 
3
7
  export type {
4
8
  ConversationStatus,
@@ -7,4 +11,5 @@ export type {
7
11
  Callbacks,
8
12
  ConversationOptions,
9
13
  ConversationConfig,
14
+ ConversationEvent,
10
15
  } from "./types";
package/src/types.ts CHANGED
@@ -47,16 +47,6 @@ export type Language =
47
47
  | "no"
48
48
  | "vi";
49
49
 
50
- /**
51
- * Client tool call event structure
52
- */
53
- export type ClientToolCallEvent = {
54
- tool_name: string;
55
- tool_call_id: string;
56
- parameters: unknown;
57
- expects_response: boolean;
58
- };
59
-
60
50
  /**
61
51
  * Client tools configuration
62
52
  */
@@ -92,7 +82,7 @@ export type Callbacks = {
92
82
  onDebug?: (props: unknown) => void;
93
83
  onDisconnect?: (details: string) => void;
94
84
  onError?: (message: string, context?: Record<string, unknown>) => void;
95
- onMessage?: (props: { message: string; source: Role }) => void;
85
+ onMessage?: (props: { message: ConversationEvent; source: Role }) => void;
96
86
  onModeChange?: (prop: { mode: Mode }) => void;
97
87
  onStatusChange?: (prop: { status: ConversationStatus }) => void;
98
88
  onCanSendFeedbackChange?: (prop: { canSendFeedback: boolean }) => void;
@@ -127,6 +117,114 @@ export type ConversationConfig = {
127
117
  userId?: string;
128
118
  };
129
119
 
120
+ export type UserTranscriptionEvent = {
121
+ type: "user_transcript";
122
+ user_transcription_event: { user_transcript: string };
123
+ };
124
+
125
+ export type AgentResponseEvent = {
126
+ type: "agent_response";
127
+ agent_response_event: { agent_response: string };
128
+ };
129
+
130
+ export type AgentResponseCorrectionEvent = {
131
+ type: "agent_response_correction";
132
+ agent_response_correction_event: {
133
+ original_agent_response: string;
134
+ corrected_agent_response: string;
135
+ };
136
+ };
137
+ export type AgentAudioEvent = {
138
+ type: "audio";
139
+ audio_event: {
140
+ audio_base_64: string;
141
+ event_id: number;
142
+ };
143
+ };
144
+
145
+ export type InterruptionEvent = {
146
+ type: "interruption";
147
+ interruption_event: {
148
+ event_id: number;
149
+ };
150
+ };
151
+
152
+ export type InternalTentativeAgentResponseEvent = {
153
+ type: "internal_tentative_agent_response";
154
+ tentative_agent_response_internal_event: {
155
+ tentative_agent_response: string;
156
+ };
157
+ };
158
+
159
+ export type ConfigEvent = {
160
+ type: "conversation_initiation_metadata";
161
+ conversation_initiation_metadata_event: {
162
+ conversation_id: string;
163
+ agent_output_audio_format: string;
164
+ user_input_audio_format?: string;
165
+ };
166
+ };
167
+
168
+ export type PingEvent = {
169
+ type: "ping";
170
+ ping_event: {
171
+ event_id: number;
172
+ ping_ms?: number;
173
+ };
174
+ };
175
+
176
+ export type ClientToolCallEvent = {
177
+ type: "client_tool_call";
178
+ client_tool_call: {
179
+ tool_name: string;
180
+ tool_call_id: string;
181
+ parameters: any;
182
+ expects_response: boolean;
183
+ };
184
+ };
185
+
186
+ export type PongEvent = {
187
+ type: "pong";
188
+ event_id: number;
189
+ };
190
+
191
+ export type UserAudioEvent = {
192
+ user_audio_chunk: string;
193
+ };
194
+
195
+ export type UserFeedbackEvent = {
196
+ type: "feedback";
197
+ score: "like" | "dislike";
198
+ event_id: number;
199
+ };
200
+
201
+ export type ClientToolResultEvent = {
202
+ type: "client_tool_result";
203
+ tool_call_id: string;
204
+ result: any;
205
+ is_error: boolean;
206
+ };
207
+
208
+ export type ContextualUpdateEvent = {
209
+ type: "contextual_update";
210
+ text: string;
211
+ };
212
+
213
+ export type UserMessageEvent = {
214
+ type: "user_message";
215
+ text: string;
216
+ };
217
+
218
+ export type UserActivityEvent = {
219
+ type: "user_activity";
220
+ };
221
+
222
+ export type MCPToolApprovalResultEvent = {
223
+ type: "mcp_tool_approval_result";
224
+ tool_call_id: string;
225
+ is_approved: boolean;
226
+ };
227
+
130
228
  export type InitiationClientDataEvent = {
131
229
  type: "conversation_initiation_client_data";
132
230
  conversation_config_override?: {
@@ -152,3 +250,14 @@ export type InitiationClientDataEvent = {
152
250
  dynamic_variables?: Record<string, string | number | boolean>;
153
251
  user_id?: string;
154
252
  };
253
+
254
+ export type ConversationEvent =
255
+ | UserTranscriptionEvent
256
+ | AgentResponseEvent
257
+ | AgentResponseCorrectionEvent
258
+ | AgentAudioEvent
259
+ | InterruptionEvent
260
+ | InternalTentativeAgentResponseEvent
261
+ | ConfigEvent
262
+ | PingEvent
263
+ | ClientToolCallEvent;
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  // This file is auto-generated during build
2
- export const PACKAGE_VERSION = "0.1.2";
2
+ export const PACKAGE_VERSION = "0.2.0";