@revrag-ai/embed-react-native 1.0.5 → 1.0.7
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 +1 -1
- package/README.md +13 -382
- package/android/CMakeLists.txt +15 -0
- package/android/build.gradle +77 -25
- package/android/cpp-adapter.cpp +8 -0
- package/android/generated/java/com/revragai/embedreactnative/NativeEmbedReactNativeSpec.java +37 -0
- package/android/generated/jni/CMakeLists.txt +49 -0
- package/android/generated/jni/RNEmbedReactNativeSpec-generated.cpp +32 -0
- package/android/generated/jni/RNEmbedReactNativeSpec.h +31 -0
- package/android/generated/jni/react/renderer/components/RNEmbedReactNativeSpec/RNEmbedReactNativeSpecJSI-generated.cpp +28 -0
- package/android/generated/jni/react/renderer/components/RNEmbedReactNativeSpec/RNEmbedReactNativeSpecJSI.h +67 -0
- package/android/gradle.properties +5 -5
- package/cpp/revrag-ai-embed-react-native.cpp +7 -0
- package/cpp/revrag-ai-embed-react-native.h +8 -0
- package/ios/EmbedReactNative.h +9 -0
- package/ios/{Onwid.mm → EmbedReactNative.mm} +4 -4
- package/ios/generated/RNEmbedReactNativeSpec/RNEmbedReactNativeSpec-generated.mm +29 -0
- package/ios/generated/RNEmbedReactNativeSpec/RNEmbedReactNativeSpec.h +50 -0
- package/ios/generated/RNEmbedReactNativeSpecJSI-generated.cpp +28 -0
- package/ios/generated/RNEmbedReactNativeSpecJSI.h +67 -0
- package/lib/commonjs/NativeEmbedReactNative.js +9 -0
- package/lib/commonjs/api/api.js +256 -0
- package/lib/commonjs/api/types/embed.api.types.js +2 -0
- package/lib/commonjs/components/Embed/EmbedAudioWave.js +157 -0
- package/lib/commonjs/components/Embed/EmbedButton.js +511 -0
- package/lib/commonjs/components/Embed/EmbedVoice.js +131 -0
- package/lib/commonjs/components/styles/EmbedButton.style.js +248 -0
- package/lib/commonjs/events/embed.event.js +74 -0
- package/lib/commonjs/hooks/initialize.js +102 -0
- package/lib/commonjs/hooks/initialize.livekit.js +20 -0
- package/lib/commonjs/hooks/types/initialize.types.js +2 -0
- package/lib/commonjs/hooks/types/voiceAgent.types.js +6 -0
- package/lib/commonjs/hooks/voiceagent.js +358 -0
- package/lib/commonjs/index.js +34 -0
- package/lib/commonjs/index.types.js +22 -0
- package/lib/commonjs/store/store.key.js +46 -0
- package/lib/commonjs/utils/reanimated.helper.js +100 -0
- package/lib/module/NativeEmbedReactNative.js +5 -0
- package/lib/module/api/api.js +248 -0
- package/lib/module/api/types/embed.api.types.js +2 -0
- package/lib/module/components/Embed/EmbedAudioWave.js +152 -0
- package/lib/module/components/Embed/EmbedButton.js +506 -0
- package/lib/module/components/Embed/EmbedVoice.js +127 -0
- package/lib/module/components/styles/EmbedButton.style.js +243 -0
- package/lib/module/events/embed.event.js +70 -0
- package/lib/module/hooks/initialize.js +79 -75
- package/lib/module/hooks/{initializelivekit.js → initialize.livekit.js} +7 -4
- package/lib/module/hooks/types/initialize.types.js +2 -0
- package/lib/module/hooks/types/voiceAgent.types.js +4 -0
- package/lib/module/hooks/voiceagent.js +353 -0
- package/lib/module/index.js +6 -60
- package/lib/module/index.types.js +23 -0
- package/lib/module/store/store.key.js +38 -0
- package/lib/module/utils/reanimated.helper.js +94 -0
- package/lib/typescript/commonjs/package.json +1 -0
- package/lib/typescript/module/package.json +1 -0
- package/package.json +69 -27
- package/react-native.config.js +8 -14
- package/revrag-ai-embed-react-native.podspec +41 -0
- package/Onwid.podspec +0 -20
- package/ios/Onwid.h +0 -5
- package/lib/index.d.ts +0 -77
- package/lib/module/Event/onwid.js +0 -74
- package/lib/module/NativeOnwid.js +0 -4
- package/lib/module/component/OnwidButton.js +0 -366
- package/lib/module/component/audiowave.js +0 -137
- package/lib/module/component/voice.js +0 -103
- package/lib/module/hooks/initialize.types.js +0 -2
- package/lib/module/hooks/voiceAgent.js +0 -334
- package/lib/module/hooks/voiceAgent.types.js +0 -2
- package/lib/module/onwidApi/api.js +0 -184
- package/lib/module/onwidApi/api.types.js +0 -2
- package/lib/module/store.key.js +0 -47
- package/lib/module/style/onwidButton.style.js +0 -230
- package/lib/module/utils/reanimatedHelpers.js +0 -87
- package/lib/module/utils/utils.js +0 -1
- package/lib/typescript/Event/onwid.d.ts +0 -13
- package/lib/typescript/NativeOnwid.d.ts +0 -6
- package/lib/typescript/component/OnwidButton.d.ts +0 -28
- package/lib/typescript/component/audiowave.d.ts +0 -6
- package/lib/typescript/component/voice.d.ts +0 -15
- package/lib/typescript/hooks/initialize.d.ts +0 -2
- package/lib/typescript/hooks/initialize.types.d.ts +0 -5
- package/lib/typescript/hooks/initializelivekit.d.ts +0 -3
- package/lib/typescript/hooks/voiceAgent.d.ts +0 -2
- package/lib/typescript/hooks/voiceAgent.types.d.ts +0 -16
- package/lib/typescript/index.d.ts +0 -27
- package/lib/typescript/onwidApi/api.d.ts +0 -53
- package/lib/typescript/onwidApi/api.types.d.ts +0 -21
- package/lib/typescript/store.key.d.ts +0 -3
- package/lib/typescript/style/onwidButton.style.d.ts +0 -98
- package/lib/typescript/utils/reanimatedHelpers.d.ts +0 -29
- package/lib/typescript/utils/utils.d.ts +0 -0
- package/scripts/verify-setup.js +0 -90
|
@@ -1,334 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.useVoiceAgent = void 0;
|
|
4
|
-
const livekit_client_1 = require("livekit-client");
|
|
5
|
-
const react_1 = require("react");
|
|
6
|
-
const onwid_1 = require("../Event/onwid");
|
|
7
|
-
const api_1 = require("../onwidApi/api");
|
|
8
|
-
const store_key_1 = require("../store.key");
|
|
9
|
-
// Create a singleton instance of Room that persists across hook instances
|
|
10
|
-
// This ensures we don't create multiple Room instances that could conflict
|
|
11
|
-
let roomInstance = null;
|
|
12
|
-
let isConnecting = false;
|
|
13
|
-
let isDisconnecting = false;
|
|
14
|
-
let hasListeners = false;
|
|
15
|
-
let connectionAttempts = 0;
|
|
16
|
-
let stableConnectionTimerId = null;
|
|
17
|
-
const MAX_CONNECTION_ATTEMPTS = 3;
|
|
18
|
-
const STABLE_CONNECTION_TIMEOUT = 5000; // 5 seconds
|
|
19
|
-
// Create getters for the room instance
|
|
20
|
-
const getRoomInstance = () => {
|
|
21
|
-
if (!roomInstance) {
|
|
22
|
-
console.log('Creating new Room instance');
|
|
23
|
-
// Configure the room with the right options at creation time
|
|
24
|
-
roomInstance = new livekit_client_1.Room({
|
|
25
|
-
adaptiveStream: true,
|
|
26
|
-
dynacast: true,
|
|
27
|
-
// Using the most stable configuration for React Native
|
|
28
|
-
publishDefaults: {
|
|
29
|
-
simulcast: false, // Disable simulcast to reduce SDP complexity
|
|
30
|
-
},
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
return roomInstance;
|
|
34
|
-
};
|
|
35
|
-
// Reset connection attempt counter
|
|
36
|
-
const resetConnectionAttempts = () => {
|
|
37
|
-
connectionAttempts = 0;
|
|
38
|
-
// Clear any pending stable connection timer
|
|
39
|
-
if (stableConnectionTimerId) {
|
|
40
|
-
clearTimeout(stableConnectionTimerId);
|
|
41
|
-
stableConnectionTimerId = null;
|
|
42
|
-
}
|
|
43
|
-
};
|
|
44
|
-
// Cleanup function to reset the room state
|
|
45
|
-
const resetRoomState = () => {
|
|
46
|
-
if (roomInstance) {
|
|
47
|
-
try {
|
|
48
|
-
console.log('Resetting room state');
|
|
49
|
-
console.log('isDisconnecting', isDisconnecting);
|
|
50
|
-
// Only disconnect if currently connected
|
|
51
|
-
if (roomInstance.state !== livekit_client_1.ConnectionState.Disconnected &&
|
|
52
|
-
!isDisconnecting) {
|
|
53
|
-
isDisconnecting = true;
|
|
54
|
-
roomInstance.disconnect().finally(() => {
|
|
55
|
-
isDisconnecting = false;
|
|
56
|
-
});
|
|
57
|
-
}
|
|
58
|
-
// Don't destroy the room instance, just reset its state
|
|
59
|
-
hasListeners = false;
|
|
60
|
-
resetConnectionAttempts();
|
|
61
|
-
}
|
|
62
|
-
catch (e) {
|
|
63
|
-
console.error('Error resetting room state:', e);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
};
|
|
67
|
-
const useVoiceAgent = () => {
|
|
68
|
-
const [isLoading, setIsLoading] = (0, react_1.useState)(false);
|
|
69
|
-
const [error, setError] = (0, react_1.useState)(null);
|
|
70
|
-
const [tokenDetails, setTokenDetails] = (0, react_1.useState)(null);
|
|
71
|
-
const [isMicMuted, setIsMicMuted] = (0, react_1.useState)(false);
|
|
72
|
-
const [connectionState, setConnectionState] = (0, react_1.useState)(() => {
|
|
73
|
-
// Initialize with the current room state if it exists
|
|
74
|
-
const room = getRoomInstance();
|
|
75
|
-
return room ? room.state : livekit_client_1.ConnectionState.Disconnected;
|
|
76
|
-
});
|
|
77
|
-
const [stableConnection, setStableConnection] = (0, react_1.useState)(false);
|
|
78
|
-
console.log('ConnectionState_connected', connectionState
|
|
79
|
-
// tokenDetails?.token
|
|
80
|
-
);
|
|
81
|
-
// Setup event listeners for the room
|
|
82
|
-
const setupRoomListeners = () => {
|
|
83
|
-
if (hasListeners || !roomInstance)
|
|
84
|
-
return;
|
|
85
|
-
console.log('Setting up room listeners');
|
|
86
|
-
const handleConnectionChange = (state) => {
|
|
87
|
-
console.log('Connection state changed:', state);
|
|
88
|
-
// Use a function to ensure we're setting with the latest state
|
|
89
|
-
setConnectionState(() => state);
|
|
90
|
-
// Handle connection state changes
|
|
91
|
-
if (state === livekit_client_1.ConnectionState.Connected) {
|
|
92
|
-
// Reset connection attempts when connected successfully
|
|
93
|
-
resetConnectionAttempts();
|
|
94
|
-
// Set up a timer to mark the connection as stable after a few seconds
|
|
95
|
-
// This prevents immediate disconnections from being treated as stable
|
|
96
|
-
if (stableConnectionTimerId) {
|
|
97
|
-
clearTimeout(stableConnectionTimerId);
|
|
98
|
-
}
|
|
99
|
-
stableConnectionTimerId = setTimeout(() => {
|
|
100
|
-
console.log('Connection marked as stable');
|
|
101
|
-
setStableConnection(true);
|
|
102
|
-
}, STABLE_CONNECTION_TIMEOUT);
|
|
103
|
-
}
|
|
104
|
-
else if (state === livekit_client_1.ConnectionState.Disconnected) {
|
|
105
|
-
// Mark connection as unstable
|
|
106
|
-
setStableConnection(false);
|
|
107
|
-
// Clear any pending stable connection timer
|
|
108
|
-
if (stableConnectionTimerId) {
|
|
109
|
-
clearTimeout(stableConnectionTimerId);
|
|
110
|
-
stableConnectionTimerId = null;
|
|
111
|
-
}
|
|
112
|
-
// If disconnected unexpectedly and we have token details, don't automatically reconnect
|
|
113
|
-
// This prevents connection loops
|
|
114
|
-
if (tokenDetails && !isDisconnecting) {
|
|
115
|
-
console.log('Room disconnected unexpectedly - not auto-reconnecting');
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
};
|
|
119
|
-
const handleTrackSubscribed = (track) => {
|
|
120
|
-
if (track.kind === 'audio') {
|
|
121
|
-
track.setVolume(1.0);
|
|
122
|
-
}
|
|
123
|
-
};
|
|
124
|
-
// Handle participant disconnected - this could be helpful to detect server-side kicks
|
|
125
|
-
roomInstance.on('participantDisconnected', () => {
|
|
126
|
-
console.log('Participant disconnected from room');
|
|
127
|
-
});
|
|
128
|
-
roomInstance.on('connectionStateChanged', handleConnectionChange);
|
|
129
|
-
roomInstance.on('trackSubscribed', handleTrackSubscribed);
|
|
130
|
-
// Listen for SDP negotiation errors to handle them better
|
|
131
|
-
roomInstance.on('mediaDevicesError', (e) => {
|
|
132
|
-
console.log('Media devices error:', e.message);
|
|
133
|
-
});
|
|
134
|
-
hasListeners = true;
|
|
135
|
-
};
|
|
136
|
-
// Initialize room and listeners
|
|
137
|
-
(0, react_1.useEffect)(() => {
|
|
138
|
-
const room = getRoomInstance();
|
|
139
|
-
setupRoomListeners();
|
|
140
|
-
// Sync local state with room state
|
|
141
|
-
setConnectionState(room.state);
|
|
142
|
-
return () => {
|
|
143
|
-
// Do NOT disconnect or destroy the room on component unmount
|
|
144
|
-
// This ensures the singleton persists across component lifecycle
|
|
145
|
-
// But we do want to update local state if the component is unmounted
|
|
146
|
-
if (!tokenDetails) {
|
|
147
|
-
setTokenDetails(null);
|
|
148
|
-
}
|
|
149
|
-
};
|
|
150
|
-
}, []);
|
|
151
|
-
// Connect to LiveKit when token is set
|
|
152
|
-
(0, react_1.useEffect)(() => {
|
|
153
|
-
const room = getRoomInstance();
|
|
154
|
-
if (!tokenDetails || isConnecting)
|
|
155
|
-
return;
|
|
156
|
-
// Always sync our state with the room's current state
|
|
157
|
-
setConnectionState(room.state);
|
|
158
|
-
const connectToRoom = async () => {
|
|
159
|
-
// Prevent multiple connection attempts
|
|
160
|
-
if (isConnecting)
|
|
161
|
-
return;
|
|
162
|
-
// Limit connection attempts to prevent infinite loops
|
|
163
|
-
if (connectionAttempts >= MAX_CONNECTION_ATTEMPTS) {
|
|
164
|
-
console.log(`Maximum connection attempts (${MAX_CONNECTION_ATTEMPTS}) reached. Not trying again.`);
|
|
165
|
-
setError(`Failed to connect after ${MAX_CONNECTION_ATTEMPTS} attempts`);
|
|
166
|
-
return;
|
|
167
|
-
}
|
|
168
|
-
connectionAttempts++;
|
|
169
|
-
try {
|
|
170
|
-
isConnecting = true;
|
|
171
|
-
console.log(`Connecting to LiveKit room... (attempt ${connectionAttempts})`);
|
|
172
|
-
// Only attempt to connect if we're disconnected
|
|
173
|
-
if (room.state === livekit_client_1.ConnectionState.Disconnected) {
|
|
174
|
-
// Update state before connection attempt
|
|
175
|
-
setConnectionState(livekit_client_1.ConnectionState.Connecting);
|
|
176
|
-
await room.connect(tokenDetails.server_url, tokenDetails.token, {
|
|
177
|
-
autoSubscribe: true, // Ensure we subscribe to tracks automatically
|
|
178
|
-
});
|
|
179
|
-
// Explicitly set to connected if connection was successful
|
|
180
|
-
setConnectionState(room.state);
|
|
181
|
-
console.log('Connected to LiveKit room');
|
|
182
|
-
}
|
|
183
|
-
else if (room.state === livekit_client_1.ConnectionState.Connected) {
|
|
184
|
-
console.log('Room is already connected');
|
|
185
|
-
// Ensure our state matches
|
|
186
|
-
setConnectionState(livekit_client_1.ConnectionState.Connected);
|
|
187
|
-
}
|
|
188
|
-
else {
|
|
189
|
-
console.log('Room is in transition state:', room.state);
|
|
190
|
-
// Sync our state with the room's current state
|
|
191
|
-
setConnectionState(room.state);
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
catch (err) {
|
|
195
|
-
const message = err instanceof Error ? err.message : 'Failed to connect to room';
|
|
196
|
-
setError(message);
|
|
197
|
-
console.error('Connection error:', message);
|
|
198
|
-
// Sync state to disconnected if we failed to connect
|
|
199
|
-
setConnectionState(room.state);
|
|
200
|
-
// Don't keep retrying if we hit an SDP error
|
|
201
|
-
if (message.includes('SDP') || message.includes('sdp')) {
|
|
202
|
-
console.log('SDP error detected, will not retry automatically');
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
finally {
|
|
206
|
-
isConnecting = false;
|
|
207
|
-
}
|
|
208
|
-
};
|
|
209
|
-
connectToRoom();
|
|
210
|
-
// No cleanup here - we don't want to disconnect when token changes or component unmounts
|
|
211
|
-
}, [tokenDetails]);
|
|
212
|
-
// Log connection status periodically for debugging
|
|
213
|
-
(0, react_1.useEffect)(() => {
|
|
214
|
-
const debugInterval = setInterval(() => {
|
|
215
|
-
if (roomInstance) {
|
|
216
|
-
const state = roomInstance.state;
|
|
217
|
-
const participantCount = roomInstance.numParticipants;
|
|
218
|
-
console.log(`[DEBUG] Room state: ${state}, Participants: ${participantCount}, Stable: ${stableConnection}`);
|
|
219
|
-
}
|
|
220
|
-
}, 10000); // Every 10 seconds
|
|
221
|
-
return () => clearInterval(debugInterval);
|
|
222
|
-
}, [stableConnection]);
|
|
223
|
-
// Generate token and connect
|
|
224
|
-
const generateVoiceToken = async () => {
|
|
225
|
-
console.log('generateVoiceToken');
|
|
226
|
-
// Don't generate a new token if we're already connecting/connected
|
|
227
|
-
if (isConnecting || isLoading) {
|
|
228
|
-
console.log('Already connecting or loading, skipping token generation');
|
|
229
|
-
return;
|
|
230
|
-
}
|
|
231
|
-
// Reset connection attempts when starting fresh
|
|
232
|
-
resetConnectionAttempts();
|
|
233
|
-
setStableConnection(false);
|
|
234
|
-
const userData = await (0, store_key_1.getAgentData)(onwid_1.EventKeys.USER_DATA);
|
|
235
|
-
setIsLoading(true);
|
|
236
|
-
setError(null);
|
|
237
|
-
console.log('userData', userData);
|
|
238
|
-
try {
|
|
239
|
-
const apiService = api_1.APIService.getInstance();
|
|
240
|
-
const response = await apiService.getTokenDetails({
|
|
241
|
-
app_user_id: userData === null || userData === void 0 ? void 0 : userData.app_user_id,
|
|
242
|
-
call_type: 'EMBEDDED',
|
|
243
|
-
});
|
|
244
|
-
if (!response.data)
|
|
245
|
-
throw new Error('No voice token found');
|
|
246
|
-
// Only set token details if we're not already connected
|
|
247
|
-
const room = getRoomInstance();
|
|
248
|
-
if (room.state !== livekit_client_1.ConnectionState.Connected) {
|
|
249
|
-
setTokenDetails(response.data);
|
|
250
|
-
}
|
|
251
|
-
else {
|
|
252
|
-
console.log('Room already connected, skipping token update');
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
catch (err) {
|
|
256
|
-
const message = err instanceof Error ? err.message : 'Failed to generate voice token';
|
|
257
|
-
setError(message);
|
|
258
|
-
throw new Error(message);
|
|
259
|
-
}
|
|
260
|
-
finally {
|
|
261
|
-
setIsLoading(false);
|
|
262
|
-
}
|
|
263
|
-
};
|
|
264
|
-
// End call
|
|
265
|
-
const endCall = async () => {
|
|
266
|
-
if (isDisconnecting) {
|
|
267
|
-
console.log('Already disconnecting, skipping');
|
|
268
|
-
return;
|
|
269
|
-
}
|
|
270
|
-
try {
|
|
271
|
-
console.log('Ending call');
|
|
272
|
-
isDisconnecting = true;
|
|
273
|
-
setTokenDetails(null);
|
|
274
|
-
setIsMicMuted(false);
|
|
275
|
-
resetConnectionAttempts();
|
|
276
|
-
setStableConnection(false);
|
|
277
|
-
const room = getRoomInstance();
|
|
278
|
-
if (room.state !== livekit_client_1.ConnectionState.Disconnected) {
|
|
279
|
-
// Update state before disconnection
|
|
280
|
-
setConnectionState(livekit_client_1.ConnectionState.Connecting);
|
|
281
|
-
await room.disconnect();
|
|
282
|
-
// Update state after disconnection
|
|
283
|
-
setConnectionState(livekit_client_1.ConnectionState.Disconnected);
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
catch (err) {
|
|
287
|
-
setError(err instanceof Error ? err.message : 'Failed to end call');
|
|
288
|
-
// Make sure state is correctly reflected even if there's an error
|
|
289
|
-
const room = getRoomInstance();
|
|
290
|
-
setConnectionState(room.state);
|
|
291
|
-
}
|
|
292
|
-
finally {
|
|
293
|
-
isDisconnecting = false;
|
|
294
|
-
}
|
|
295
|
-
};
|
|
296
|
-
// Mute microphone
|
|
297
|
-
const muteMic = () => {
|
|
298
|
-
const room = getRoomInstance();
|
|
299
|
-
if (room.localParticipant) {
|
|
300
|
-
room.localParticipant.setMicrophoneEnabled(false);
|
|
301
|
-
setIsMicMuted(true);
|
|
302
|
-
console.log('Microphone muted');
|
|
303
|
-
}
|
|
304
|
-
};
|
|
305
|
-
// Unmute microphone
|
|
306
|
-
const unmuteMic = () => {
|
|
307
|
-
const room = getRoomInstance();
|
|
308
|
-
if (room.localParticipant) {
|
|
309
|
-
room.localParticipant.setMicrophoneEnabled(true);
|
|
310
|
-
setIsMicMuted(false);
|
|
311
|
-
console.log('Microphone unmuted');
|
|
312
|
-
}
|
|
313
|
-
};
|
|
314
|
-
// Clean up everything (use only when the app is shutting down)
|
|
315
|
-
const cleanup = () => {
|
|
316
|
-
endCall();
|
|
317
|
-
resetRoomState();
|
|
318
|
-
};
|
|
319
|
-
return {
|
|
320
|
-
initializeVoiceAgent: generateVoiceToken,
|
|
321
|
-
endCall,
|
|
322
|
-
muteMic,
|
|
323
|
-
unmuteMic,
|
|
324
|
-
isMicMuted,
|
|
325
|
-
connectionState,
|
|
326
|
-
room: getRoomInstance(),
|
|
327
|
-
tokenDetails,
|
|
328
|
-
isLoading,
|
|
329
|
-
error,
|
|
330
|
-
roomRef: { current: getRoomInstance() },
|
|
331
|
-
cleanup,
|
|
332
|
-
};
|
|
333
|
-
};
|
|
334
|
-
exports.useVoiceAgent = useVoiceAgent;
|
|
@@ -1,184 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getTokenDetails = exports.updateUserData = exports.registerOnInitialize = exports.initializeApi = exports.APIService = void 0;
|
|
4
|
-
const store_key_1 = require("../store.key");
|
|
5
|
-
/**
|
|
6
|
-
* APIService class that ensures proper initialization before API calls
|
|
7
|
-
*/
|
|
8
|
-
class APIService {
|
|
9
|
-
constructor() {
|
|
10
|
-
this.apiBaseUrl = null;
|
|
11
|
-
this.isInitialized = false;
|
|
12
|
-
}
|
|
13
|
-
/**
|
|
14
|
-
* Get singleton instance of APIService
|
|
15
|
-
*/
|
|
16
|
-
static getInstance() {
|
|
17
|
-
if (!APIService.instance) {
|
|
18
|
-
APIService.instance = new APIService();
|
|
19
|
-
}
|
|
20
|
-
return APIService.instance;
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* Initialize the API service with the base URL
|
|
24
|
-
*/
|
|
25
|
-
async initialize() {
|
|
26
|
-
if (this.isInitialized && this.apiBaseUrl) {
|
|
27
|
-
return; // Already initialized
|
|
28
|
-
}
|
|
29
|
-
const AgentData = await (0, store_key_1.getAgentData)();
|
|
30
|
-
console.log('AgentData', AgentData);
|
|
31
|
-
if (AgentData === null || AgentData === void 0 ? void 0 : AgentData.onwidUrl) {
|
|
32
|
-
this.apiBaseUrl = AgentData.onwidUrl;
|
|
33
|
-
this.isInitialized = true;
|
|
34
|
-
console.log('API_BASE_URL initialized:', this.apiBaseUrl);
|
|
35
|
-
}
|
|
36
|
-
else {
|
|
37
|
-
throw new Error('API base URL not found in keychain');
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* Ensure the service is initialized before making API calls
|
|
42
|
-
*/
|
|
43
|
-
async ensureInitialized() {
|
|
44
|
-
if (!this.isInitialized || !this.apiBaseUrl) {
|
|
45
|
-
await this.initialize();
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* Get headers with stored API key
|
|
50
|
-
*/
|
|
51
|
-
async getHeaders() {
|
|
52
|
-
const AgentData = await (0, store_key_1.getAgentData)();
|
|
53
|
-
if (!(AgentData === null || AgentData === void 0 ? void 0 : AgentData.apiKey)) {
|
|
54
|
-
throw new Error('API key not found in keychain');
|
|
55
|
-
}
|
|
56
|
-
return {
|
|
57
|
-
'Content-Type': 'application/json',
|
|
58
|
-
'Authorization': `Bearer ${AgentData.apiKey}`,
|
|
59
|
-
'X-Revrag-Embedded-Key': AgentData.apiKey,
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
/**
|
|
63
|
-
* Register a new user/device on initialization
|
|
64
|
-
* @returns Promise with registration response
|
|
65
|
-
*/
|
|
66
|
-
async registerOnInitialize() {
|
|
67
|
-
try {
|
|
68
|
-
await this.ensureInitialized();
|
|
69
|
-
console.log('registerOnInitialize ApiData', this.apiBaseUrl);
|
|
70
|
-
const headers = await this.getHeaders();
|
|
71
|
-
const response = await fetch(`${this.apiBaseUrl}/embedded-agent/initialize`, {
|
|
72
|
-
method: 'GET',
|
|
73
|
-
headers: headers,
|
|
74
|
-
});
|
|
75
|
-
const data = await response.json();
|
|
76
|
-
console.log('dat config data after register', data);
|
|
77
|
-
await (0, store_key_1.setAgentData)(data, '@config_data');
|
|
78
|
-
if (!response.ok) {
|
|
79
|
-
console.log('registerOnInitialize error', data.error);
|
|
80
|
-
throw new Error(data.error || 'Registration failed');
|
|
81
|
-
}
|
|
82
|
-
return {
|
|
83
|
-
success: true,
|
|
84
|
-
data: data,
|
|
85
|
-
};
|
|
86
|
-
}
|
|
87
|
-
catch (error) {
|
|
88
|
-
console.log('registerOnInitialize error', error);
|
|
89
|
-
return {
|
|
90
|
-
success: false,
|
|
91
|
-
error: error instanceof Error ? error.message : 'Unknown error occurred',
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* Update user data
|
|
97
|
-
* @param params Update parameters including userId and data to update
|
|
98
|
-
* @returns Promise with update response
|
|
99
|
-
*/
|
|
100
|
-
async updateUserData(params) {
|
|
101
|
-
try {
|
|
102
|
-
await this.ensureInitialized();
|
|
103
|
-
console.log('params', params, `${this.apiBaseUrl}/embedded-agent/user-context/update?app_user_id=${params.data.app_user_id}`);
|
|
104
|
-
console.log('updateUserData');
|
|
105
|
-
const headers = await this.getHeaders();
|
|
106
|
-
const response = await fetch(`${this.apiBaseUrl}/embedded-agent/user-context/update?app_user_id=${params.data.app_user_id}`, {
|
|
107
|
-
method: 'PUT',
|
|
108
|
-
headers,
|
|
109
|
-
body: JSON.stringify({ [params.eventKey]: params.data }),
|
|
110
|
-
});
|
|
111
|
-
const data = await response.json();
|
|
112
|
-
console.log('data after update', data);
|
|
113
|
-
if (!response.ok) {
|
|
114
|
-
throw new Error(data.error || 'Update failed');
|
|
115
|
-
}
|
|
116
|
-
return {
|
|
117
|
-
success: true,
|
|
118
|
-
};
|
|
119
|
-
}
|
|
120
|
-
catch (error) {
|
|
121
|
-
console.log('updateUserData error', error);
|
|
122
|
-
return {
|
|
123
|
-
success: false,
|
|
124
|
-
error: error instanceof Error ? error.message : 'Unknown error occurred',
|
|
125
|
-
};
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
/**
|
|
129
|
-
* Get token details for a user
|
|
130
|
-
* @param params Parameters including app_user_id and call_type
|
|
131
|
-
* @returns Promise with token details
|
|
132
|
-
*/
|
|
133
|
-
async getTokenDetails(params) {
|
|
134
|
-
try {
|
|
135
|
-
await this.ensureInitialized();
|
|
136
|
-
const headers = await this.getHeaders();
|
|
137
|
-
console.log('params', this.apiBaseUrl, params, headers, `${this.apiBaseUrl}/embedded-agent/token`);
|
|
138
|
-
const response = await fetch(`${this.apiBaseUrl}/embedded-agent/token`, {
|
|
139
|
-
method: 'POST',
|
|
140
|
-
headers,
|
|
141
|
-
body: JSON.stringify(params),
|
|
142
|
-
});
|
|
143
|
-
const data = await response.json();
|
|
144
|
-
console.log('data', data);
|
|
145
|
-
if (!response.ok) {
|
|
146
|
-
throw new Error(data.error || 'Failed to get token details');
|
|
147
|
-
}
|
|
148
|
-
return {
|
|
149
|
-
success: true,
|
|
150
|
-
data: data,
|
|
151
|
-
};
|
|
152
|
-
}
|
|
153
|
-
catch (error) {
|
|
154
|
-
console.log('getTokenDetails error', error);
|
|
155
|
-
return {
|
|
156
|
-
success: false,
|
|
157
|
-
error: error instanceof Error ? error.message : 'Unknown error occurred',
|
|
158
|
-
};
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
exports.APIService = APIService;
|
|
163
|
-
APIService.instance = null;
|
|
164
|
-
// Export convenience functions for backward compatibility
|
|
165
|
-
const initializeApi = async () => {
|
|
166
|
-
const apiService = APIService.getInstance();
|
|
167
|
-
await apiService.initialize();
|
|
168
|
-
};
|
|
169
|
-
exports.initializeApi = initializeApi;
|
|
170
|
-
const registerOnInitialize = async () => {
|
|
171
|
-
const apiService = APIService.getInstance();
|
|
172
|
-
return await apiService.registerOnInitialize();
|
|
173
|
-
};
|
|
174
|
-
exports.registerOnInitialize = registerOnInitialize;
|
|
175
|
-
const updateUserData = async (params) => {
|
|
176
|
-
const apiService = APIService.getInstance();
|
|
177
|
-
return await apiService.updateUserData(params);
|
|
178
|
-
};
|
|
179
|
-
exports.updateUserData = updateUserData;
|
|
180
|
-
const getTokenDetails = async (params) => {
|
|
181
|
-
const apiService = APIService.getInstance();
|
|
182
|
-
return await apiService.getTokenDetails(params);
|
|
183
|
-
};
|
|
184
|
-
exports.getTokenDetails = getTokenDetails;
|
package/lib/module/store.key.js
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.deleteFromDetail = exports.getAgentData = exports.setAgentData = void 0;
|
|
7
|
-
const async_storage_1 = __importDefault(require("@react-native-async-storage/async-storage"));
|
|
8
|
-
const STORAGE_KEY = '@user_data';
|
|
9
|
-
const setAgentData = async (data, key = STORAGE_KEY) => {
|
|
10
|
-
try {
|
|
11
|
-
const jsonString = JSON.stringify(data);
|
|
12
|
-
await async_storage_1.default.setItem(key, jsonString);
|
|
13
|
-
console.log('Data saved to storage successfully');
|
|
14
|
-
}
|
|
15
|
-
catch (error) {
|
|
16
|
-
console.error('Storage save error:', error);
|
|
17
|
-
throw error; // Re-throw to allow caller to handle
|
|
18
|
-
}
|
|
19
|
-
};
|
|
20
|
-
exports.setAgentData = setAgentData;
|
|
21
|
-
const getAgentData = async (key = STORAGE_KEY) => {
|
|
22
|
-
try {
|
|
23
|
-
const jsonString = await async_storage_1.default.getItem(key);
|
|
24
|
-
if (!jsonString) {
|
|
25
|
-
console.log('No data stored');
|
|
26
|
-
return null;
|
|
27
|
-
}
|
|
28
|
-
const data = JSON.parse(jsonString);
|
|
29
|
-
return data;
|
|
30
|
-
}
|
|
31
|
-
catch (error) {
|
|
32
|
-
console.error('Storage fetch error:', error);
|
|
33
|
-
throw error; // Re-throw to allow caller to handle
|
|
34
|
-
}
|
|
35
|
-
};
|
|
36
|
-
exports.getAgentData = getAgentData;
|
|
37
|
-
const deleteFromDetail = async (key) => {
|
|
38
|
-
try {
|
|
39
|
-
await async_storage_1.default.removeItem(key);
|
|
40
|
-
console.log('Storage data reset successfully');
|
|
41
|
-
}
|
|
42
|
-
catch (error) {
|
|
43
|
-
console.error('Storage reset error:', error);
|
|
44
|
-
throw error; // Re-throw to allow caller to handle
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
exports.deleteFromDetail = deleteFromDetail;
|