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

114 lines (104 loc) 3.48 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AgentEventEmitter = void 0; var _storeKey = require("../store/store.key.js"); var _api = require("../api/api.js"); var _constant = require("../utils/constant.js"); var _embedTypes = require("./embed.types.js"); /** * Agent Event Emitter * Handles listening to agent state changes (connection, disconnection, popup visibility) * Also sends agent events to the backend API */ class AgentEventEmitter { agentListeners = {}; /** * Add an event listener for agent events * @param eventKey - The agent event to listen for * @param callback - Callback function to execute when event fires */ on(eventKey, callback) { if (!this.agentListeners[eventKey]) { this.agentListeners[eventKey] = []; } this.agentListeners[eventKey]?.push(callback); } /** * Remove an event listener for agent events * @param eventKey - The agent event to stop listening to * @param callback - The callback function to remove */ off(eventKey, callback) { if (!this.agentListeners[eventKey]) return; const index = this.agentListeners[eventKey]?.indexOf(callback); if (index !== undefined && index > -1) { this.agentListeners[eventKey]?.splice(index, 1); } } /** * Emit an agent event to all registered listeners AND send to backend * @param eventKey - The agent event to emit * @param data - Data to pass to listeners * @internal - Used internally by the library */ async emit(eventKey, data) { // Trigger local event listeners first this.agentListeners[eventKey]?.forEach(callback => { try { callback(data); } catch (error) { console.error(`Error in agent event listener for ${eventKey}:`, error); } }); // Send to backend API await this.sendToBackend(eventKey, data); } /** * Send agent event data to backend API * @param eventKey - The agent event type * @param data - Event data * @private */ async sendToBackend(eventKey, data) { try { // Get user identity to include app_user_id const userIdentity = await (0, _storeKey.getAgentData)(_embedTypes.EventKeys.USER_DATA); if (!userIdentity?.app_user_id) { console.warn(`[AgentEvent] Cannot send ${eventKey} to backend: User identity not found`); return; } // Prepare payload with app_user_id const payload = { ...data, app_user_id: userIdentity.app_user_id, eventKey: eventKey, eventType: 'agent_event' }; // Send to API const apiService = _api.APIService.getInstance(); const paramsData = (0, _constant.buildEventPayload)(payload); const response = await apiService.updateUserData(paramsData); if (!response.success) { console.error(`[AgentEvent] Failed to send ${eventKey} to backend:`, response.error); } } catch (error) { console.error(`[AgentEvent] Error sending ${eventKey} to backend:`, error); // Fail silently - agent events should not break the app } } /** * Remove all listeners for a specific event * @param eventKey - The agent event to clear listeners for */ removeAllListeners(eventKey) { if (eventKey) { delete this.agentListeners[eventKey]; } else { this.agentListeners = {}; } } } exports.AgentEventEmitter = AgentEventEmitter; //# sourceMappingURL=agent-event-emitter.js.map