Overview
TheuseMobileVoiceAgent hook provides a complete voice agent integration for React Native apps. It handles WebRTC transport, backend communication, function execution, and real-time event processing.
Usage
Options
Runtime configuration object containing app environment, routes, and function module loaders. Obtain from
resolveNavaiMobileApplicationRuntimeConfig.Indicates whether the runtime configuration is still loading. Set to
true during async runtime resolution.Error message if runtime configuration failed to load. Set to error message string or
null if no error.Navigation callback function invoked when the agent calls the
navigate_to tool. Receives the target path as parameter.Return Value
ReturnsUseMobileVoiceAgentResult object with the following properties:
Current session status:
idle: Not connectedconnecting: Connection in progressconnected: Active voice sessionerror: Connection failed or error occurred
Error message if status is
error, otherwise null.Convenience flag:
true when status is connecting.Convenience flag:
true when status is connected.Async function to start the voice session. Handles:
- Microphone permission request (Android)
- WebRTC connection initialization
- Backend function loading
- Agent runtime creation
- Real-time event loop setup
Async function to stop the voice session and clean up resources. Safe to call multiple times.
Examples
Basic Integration
With Status Indicators
Custom Navigation Handler
Behavior
Microphone Permissions
On Android, the hook automatically requestsRECORD_AUDIO permission when start() is called. If permission is denied or permanently blocked, an error is returned.
Function Loading
The hook loads frontend functions asynchronously when the runtime changes. Thestart() function will fail if functions are still loading.
Tool Call Handling
The hook automatically:- Extracts tool calls from real-time events
- Executes
navigate_toandexecute_app_functiontools - Handles both frontend and backend function execution
- Returns results to the agent via real-time events
Cleanup
The hook automatically cleans up resources (WebRTC connections, event listeners) when the component unmounts.Error Handling
"WebRTC native module is not available."- react-native-webrtc not installed"Runtime configuration is not available."- runtime prop is null"Functions are still loading. Try again in a moment."- functions not loaded yet"Microphone permission denied."- User denied permission (Android)
Related
- createNavaiMobileAgentRuntime - Agent runtime creation
- createNavaiMobileVoiceSession - Low-level voice session
- createReactNativeWebRtcTransport - WebRTC transport layer
