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

86 lines (80 loc) 2.56 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")); 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. Stores API key securely in keychain * 3. Registers the device with provided details */ function useInitialize({ apiKey, embedUrl }) { const [isInitialized, setIsInitialized] = (0, _react.useState)(false); const [error, setError] = (0, _react.useState)(null); /** * Validates required initialization parameters * @throws Error if any required parameter is missing or invalid */ const validateInputs = () => { if (!apiKey || typeof apiKey !== 'string') { setError('apiKey is required and must be a string'); return; } if (!embedUrl || typeof embedUrl !== 'string') { setError('embedUrl is required and must be a string'); return; } }; const initialize = async () => { try { (0, _initializeLivekit.default)(); // Validate required parameters before proceeding validateInputs(); // Store API key in keychain await (0, _storeKey.setAgentData)({ apiKey, embedUrl }); // Get the APIService instance and initialize it const apiService = _api.APIService.getInstance(); await apiService.initialize(); // Register new device with provided details const registerResponse = await apiService.registerOnInitialize(); if (!registerResponse.success) { setError(registerResponse.error || 'Device registration failed'); return; } if (registerResponse.data) { // todo: store config setIsInitialized(true); } } catch (err) { console.log('err---', err); console.log('err instanceof Error', err instanceof Error); const errorMessage = err instanceof Error ? err.message : 'Initialization failed'; setError(errorMessage); return; } }; // Initialize immediately when hook is called initialize().catch(console.error); return { isInitialized, error }; } //# sourceMappingURL=initialize.js.map