@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.
- package/README.md +24 -2
- package/dist/ElevenLabsProvider.d.ts +2 -0
- package/dist/hooks/useConversationSession.d.ts +2 -1
- package/dist/hooks/useLiveKitRoom.d.ts +1 -1
- package/dist/lib.js +1 -1
- package/dist/lib.js.map +1 -1
- package/dist/lib.modern.js +1 -1
- package/dist/lib.modern.js.map +1 -1
- package/dist/lib.module.js +1 -1
- package/dist/lib.module.js.map +1 -1
- package/dist/lib.umd.js +1 -1
- package/dist/lib.umd.js.map +1 -1
- package/dist/utils/tokenUtils.d.ts +1 -1
- package/dist/version.d.ts +1 -1
- package/package.json +1 -1
- package/src/ElevenLabsProvider.tsx +18 -4
- package/src/hooks/useConversationSession.ts +16 -6
- package/src/hooks/useLiveKitRoom.ts +3 -3
- package/src/utils/overrides.ts +1 -1
- package/src/utils/tokenUtils.ts +5 -3
- package/src/version.ts +1 -1
|
@@ -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 [
|
|
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
|
-
|
|
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,
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
59
|
-
|
|
60
|
-
|
|
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
|
-
[
|
|
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
|
-
|
|
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
|
|
37
|
-
}, [
|
|
36
|
+
callbacksRef.current.onConnect?.({ conversationId });
|
|
37
|
+
}, [conversationId, callbacksRef, setStatus]);
|
|
38
38
|
|
|
39
39
|
const handleDisconnected = useCallback(() => {
|
|
40
40
|
setRoomConnected(false);
|
package/src/utils/overrides.ts
CHANGED
package/src/utils/tokenUtils.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { PACKAGE_VERSION } from "../version";
|
|
2
2
|
|
|
3
|
-
export const
|
|
3
|
+
export const extractConversationIdFromToken = (token: string): string => {
|
|
4
4
|
try {
|
|
5
5
|
const tokenPayload = JSON.parse(atob(token.split(".")[1]));
|
|
6
|
-
|
|
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
|
|
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
|
+
export const PACKAGE_VERSION = "0.3.0";
|