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

120 lines (109 loc) 3.4 kB
"use strict"; /** * 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 */ import { useEffect, useState, useRef, useCallback } from 'react'; import { APIService } from "../api/api.js"; import { setAgentData } from "../store/store.key.js"; import registerAgent from "./initialize.livekit.js"; import { DEFAULT_EMBED_URL } from "../utils/constant.js"; // Global flag to ensure LiveKit is only initialized once let livekitInitialized = false; let livekitInitializing = false; export function useInitialize({ apiKey, embedUrl }) { const [isInitialized, setIsInitialized] = useState(false); const [error, setError] = useState(null); const initializingRef = useRef(false); /** * Initialize LiveKit once globally */ const initializeLiveKit = async () => { if (livekitInitialized || livekitInitializing) { return; } try { livekitInitializing = true; await registerAgent(); 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 = 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 = 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 : DEFAULT_EMBED_URL; // Step 4: Store API key in secure storage await setAgentData({ apiKey, embedUrl: resolvedEmbedUrl }); // Step 5: Initialize API service const apiService = 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 useEffect(() => { initialize(); }, [initialize]); return { isInitialized, error }; } //# sourceMappingURL=initialize.js.map