@elevenlabs/react-native 0.2.0 → 0.3.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.
@@ -24,10 +24,12 @@ export interface Conversation {
24
24
  // TODO: Implement setVolume when LiveKit React Native supports it
25
25
  // setVolume: (volume: number) => void;
26
26
  canSendFeedback: boolean;
27
+ getId: () => string;
27
28
  sendFeedback: (like: boolean) => void;
28
29
  sendContextualUpdate: (text: string) => void;
29
30
  sendUserMessage: (text: string) => void;
30
31
  sendUserActivity: () => void;
32
+ setMicMuted: (muted: boolean) => void;
31
33
  }
32
34
 
33
35
  interface ElevenLabsContextType {
@@ -87,7 +89,7 @@ export const ElevenLabsProvider: React.FC<ElevenLabsProviderProps> = ({ children
87
89
  const [status, setStatus] = useState<ConversationStatus>('disconnected');
88
90
  const [serverUrl, setServerUrl] = useState(DEFAULT_SERVER_URL);
89
91
  const [tokenFetchUrl, setTokenFetchUrl] = useState<string | undefined>(undefined);
90
- const [roomId, setRoomId] = useState<string>('');
92
+ const [conversationId, setConversationId] = useState<string>('');
91
93
  const [isSpeaking, setIsSpeaking] = useState(false);
92
94
  const [canSendFeedback, setCanSendFeedback] = useState(false);
93
95
 
@@ -119,7 +121,8 @@ export const ElevenLabsProvider: React.FC<ElevenLabsProviderProps> = ({ children
119
121
  overrides,
120
122
  customLlmExtraBody,
121
123
  dynamicVariables,
122
- } = useConversationSession(callbacksRef, setStatus, setConnect, setToken, setRoomId, tokenFetchUrl);
124
+ userId,
125
+ } = useConversationSession(callbacksRef, setStatus, setConnect, setToken, setConversationId, tokenFetchUrl);
123
126
 
124
127
  const {
125
128
  roomConnected,
@@ -128,7 +131,7 @@ export const ElevenLabsProvider: React.FC<ElevenLabsProviderProps> = ({ children
128
131
  handleConnected,
129
132
  handleDisconnected,
130
133
  handleError,
131
- } = useLiveKitRoom(callbacksRef, setStatus, roomId);
134
+ } = useLiveKitRoom(callbacksRef, setStatus, conversationId);
132
135
 
133
136
  // Enhanced connection handler to initialize feedback state
134
137
  const handleConnectedWithFeedback = React.useCallback(() => {
@@ -184,6 +187,14 @@ export const ElevenLabsProvider: React.FC<ElevenLabsProviderProps> = ({ children
184
187
  console.warn('setVolume is not yet implemented in React Native SDK');
185
188
  }, []);
186
189
 
190
+ const getId = () => conversationId;
191
+
192
+ const setMicMuted = React.useCallback((muted: boolean) => {
193
+ if (localParticipant) {
194
+ localParticipant.setMicrophoneEnabled(!muted);
195
+ }
196
+ }, [localParticipant]);
197
+
187
198
  // Update current event ID for feedback tracking
188
199
  const updateCurrentEventId = React.useCallback((eventId: number) => {
189
200
  currentEventIdRef.current = eventId;
@@ -199,10 +210,11 @@ export const ElevenLabsProvider: React.FC<ElevenLabsProviderProps> = ({ children
199
210
  overrides,
200
211
  customLlmExtraBody,
201
212
  dynamicVariables,
213
+ userId,
202
214
  });
203
215
  sendMessage(overridesEvent);
204
216
  }
205
- }, [handleParticipantReady, localParticipant, overrides, customLlmExtraBody, dynamicVariables, sendMessage]);
217
+ }, [handleParticipantReady, localParticipant, overrides, customLlmExtraBody, dynamicVariables, userId, sendMessage]);
206
218
 
207
219
  const conversation: Conversation = {
208
220
  startSession,
@@ -211,6 +223,8 @@ export const ElevenLabsProvider: React.FC<ElevenLabsProviderProps> = ({ children
211
223
  isSpeaking,
212
224
  // setVolume,
213
225
  canSendFeedback,
226
+ getId,
227
+ setMicMuted,
214
228
  sendFeedback,
215
229
  sendContextualUpdate: (text: string) => {
216
230
  sendMessage({
@@ -6,7 +6,7 @@ import type {
6
6
  } from "../types";
7
7
  import {
8
8
  getConversationToken,
9
- extractRoomIdFromToken,
9
+ extractConversationIdFromToken,
10
10
  } from "../utils/tokenUtils";
11
11
 
12
12
  export const useConversationSession = (
@@ -14,7 +14,7 @@ export const useConversationSession = (
14
14
  setStatus: (status: ConversationStatus) => void,
15
15
  setConnect: (connect: boolean) => void,
16
16
  setToken: (token: string) => void,
17
- setRoomId: (roomId: string) => void,
17
+ setConversationId: (conversationId: string) => void,
18
18
  tokenFetchUrl?: string
19
19
  ) => {
20
20
  const [overrides, setOverrides] = useState<ConversationConfig["overrides"]>(
@@ -25,6 +25,7 @@ export const useConversationSession = (
25
25
  const [dynamicVariables, setDynamicVariables] = useState<
26
26
  ConversationConfig["dynamicVariables"]
27
27
  >({});
28
+ const [userId, setUserId] = useState<ConversationConfig["userId"]>(undefined);
28
29
 
29
30
  const startSession = useCallback(
30
31
  async (config: ConversationConfig) => {
@@ -35,6 +36,7 @@ export const useConversationSession = (
35
36
  setOverrides(config.overrides || {});
36
37
  setCustomLlmExtraBody(config.customLlmExtraBody || null);
37
38
  setDynamicVariables(config.dynamicVariables || {});
39
+ setUserId(config.userId);
38
40
 
39
41
  let conversationToken: string;
40
42
 
@@ -55,9 +57,9 @@ export const useConversationSession = (
55
57
  throw new Error("Either conversationToken or agentId is required");
56
58
  }
57
59
 
58
- // Extract room ID from token
59
- const extractedRoomId = extractRoomIdFromToken(conversationToken);
60
- setRoomId(extractedRoomId);
60
+ const extractedConversationId =
61
+ extractConversationIdFromToken(conversationToken);
62
+ setConversationId(extractedConversationId);
61
63
 
62
64
  setToken(conversationToken);
63
65
  setConnect(true);
@@ -68,7 +70,14 @@ export const useConversationSession = (
68
70
  throw error;
69
71
  }
70
72
  },
71
- [callbacksRef, setStatus, setConnect, setToken, setRoomId, tokenFetchUrl]
73
+ [
74
+ callbacksRef,
75
+ setStatus,
76
+ setConnect,
77
+ setToken,
78
+ setConversationId,
79
+ tokenFetchUrl,
80
+ ]
72
81
  );
73
82
 
74
83
  const endSession = useCallback(async () => {
@@ -90,5 +99,6 @@ export const useConversationSession = (
90
99
  overrides,
91
100
  customLlmExtraBody,
92
101
  dynamicVariables,
102
+ userId,
93
103
  };
94
104
  };
@@ -6,7 +6,7 @@ import type { ConversationStatus, Callbacks } from "../types";
6
6
  export const useLiveKitRoom = (
7
7
  callbacksRef: { current: Callbacks },
8
8
  setStatus: (status: ConversationStatus) => void,
9
- roomId: string
9
+ conversationId: string
10
10
  ) => {
11
11
  const [roomConnected, setRoomConnected] = useState(false);
12
12
  const [localParticipant, setLocalParticipant] =
@@ -33,8 +33,8 @@ export const useLiveKitRoom = (
33
33
  const handleConnected = useCallback(() => {
34
34
  setRoomConnected(true);
35
35
  setStatus("connected");
36
- callbacksRef.current.onConnect?.({ conversationId: roomId });
37
- }, [roomId, callbacksRef, setStatus]);
36
+ callbacksRef.current.onConnect?.({ conversationId });
37
+ }, [conversationId, callbacksRef, setStatus]);
38
38
 
39
39
  const handleDisconnected = useCallback(() => {
40
40
  setRoomConnected(false);
@@ -37,7 +37,7 @@ export function constructOverrides(
37
37
  }
38
38
 
39
39
  if (config.userId) {
40
- overridesEvent.user_id = config.userId;
40
+ overridesEvent.user_id = String(config.userId);
41
41
  }
42
42
 
43
43
  return overridesEvent;
@@ -1,11 +1,13 @@
1
1
  import { PACKAGE_VERSION } from "../version";
2
2
 
3
- export const extractRoomIdFromToken = (token: string): string => {
3
+ export const extractConversationIdFromToken = (token: string): string => {
4
4
  try {
5
5
  const tokenPayload = JSON.parse(atob(token.split(".")[1]));
6
- return tokenPayload.video?.room || "";
6
+ const roomId = tokenPayload.video?.room || "";
7
+
8
+ return roomId.match(/(conv_[a-zA-Z0-9]+)/)?.[0] || "";
7
9
  } catch (error) {
8
- console.warn("Could not extract room ID from token");
10
+ console.warn("Could not extract conversation ID from token");
9
11
  return "";
10
12
  }
11
13
  };
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  // This file is auto-generated during build
2
- export const PACKAGE_VERSION = "0.2.0";
2
+ export const PACKAGE_VERSION = "0.3.0";