UNPKG

@revrag-ai/embed-react-native

Version:

A powerful React Native library for integrating AI-powered voice agents into mobile applications. Features real-time voice communication, intelligent speech processing, customizable UI components, and comprehensive event handling for building conversation

126 lines (114 loc) 3.68 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useInitialize = useInitialize; var _react = require("react"); var _api = require("../api/api.js"); var _storeKey = require("../store/store.key.js"); var _initializeLivekit = _interopRequireDefault(require("./initialize.livekit.js")); var _constant = require("../utils/constant.js"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } /** * Custom hook for initializing the OnWid SDK * * Required Parameters: * - apiKey: string - Unique identifier for the user * * The initialization process: * 1. Validates required input parameters * 2. Initializes LiveKit * 3. Stores API key securely in keychain * 4. Registers the device with provided details */ // Global flag to ensure LiveKit is only initialized once let livekitInitialized = false; let livekitInitializing = false; function useInitialize({ apiKey, embedUrl }) { const [isInitialized, setIsInitialized] = (0, _react.useState)(false); const [error, setError] = (0, _react.useState)(null); const initializingRef = (0, _react.useRef)(false); /** * Initialize LiveKit once globally */ const initializeLiveKit = async () => { if (livekitInitialized || livekitInitializing) { return; } try { livekitInitializing = true; await (0, _initializeLivekit.default)(); livekitInitialized = true; } catch (err) { console.error('Failed to initialize LiveKit:', err); throw new Error('LiveKit initialization failed'); } finally { livekitInitializing = false; } }; /** * Validates required initialization parameters */ const validateInputs = (0, _react.useCallback)(() => { if (!apiKey || typeof apiKey !== 'string') { setError('apiKey is required and must be a string'); return false; } return true; }, [apiKey]); /** * Main initialization function */ const initialize = (0, _react.useCallback)(async () => { // Prevent multiple simultaneous initializations if (initializingRef.current) { return; } try { initializingRef.current = true; // Step 1: Initialize LiveKit first (critical!) await initializeLiveKit(); // Step 2: Validate required parameters if (!validateInputs()) { return; } // Step 3: Determine embed URL const resolvedEmbedUrl = embedUrl && typeof embedUrl === 'string' ? embedUrl : _constant.DEFAULT_EMBED_URL; // Step 4: Store API key in secure storage await (0, _storeKey.setAgentData)({ apiKey, embedUrl: resolvedEmbedUrl }); // Step 5: Initialize API service const apiService = _api.APIService.getInstance(); await apiService.initialize(); // Step 6: Register device with backend const registerResponse = await apiService.registerOnInitialize(); if (!registerResponse.success) { throw new Error(registerResponse.error || 'Device registration failed'); } // Success! if (registerResponse.data) { setIsInitialized(true); } } catch (err) { const errorMessage = err instanceof Error ? err.message : 'Initialization failed'; console.error('Initialization error:', errorMessage); setError(errorMessage); } finally { initializingRef.current = false; } }, [apiKey, embedUrl, validateInputs]); // Dependencies for useCallback // Initialize once when component mounts (0, _react.useEffect)(() => { initialize(); }, [initialize]); return { isInitialized, error }; } //# sourceMappingURL=initialize.js.map