@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
109 lines (99 loc) • 3.31 kB
JavaScript
;
/**
* Agent Event Emitter
* Handles listening to agent state changes (connection, disconnection, popup visibility)
* Also sends agent events to the backend API
*/
import { getAgentData } from "../store/store.key.js";
import { APIService } from "../api/api.js";
import { buildEventPayload } from "../utils/constant.js";
import { EventKeys } from "./embed.types.js";
export 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 getAgentData(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 = APIService.getInstance();
const paramsData = 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 = {};
}
}
}
//# sourceMappingURL=agent-event-emitter.js.map