react-native-voice-bot-sdk 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +49 -0
- package/LICENSE +21 -0
- package/README.md +464 -2
- package/android/build.gradle +15 -0
- package/android/src/main/AndroidManifest.xml +16 -0
- package/android/src/main/java/expo/modules/voicebot/VoiceBotAudioModule.kt +416 -0
- package/android/src/main/java/expo/modules/voicebot/VoiceBotMicService.kt +107 -0
- package/app.plugin.js +1 -0
- package/build/NativeAudio.d.ts +24 -0
- package/build/NativeAudio.js +1 -0
- package/build/VoiceBotClient.d.ts +231 -0
- package/build/VoiceBotClient.js +1 -0
- package/build/audio/noiseGate.d.ts +83 -0
- package/build/audio/noiseGate.js +1 -0
- package/build/base64.d.ts +6 -0
- package/build/base64.js +1 -0
- package/build/expo-router.d.ts +16 -0
- package/build/expo-router.js +1 -0
- package/build/index.d.ts +20 -0
- package/build/index.js +1 -0
- package/build/logger.d.ts +35 -0
- package/build/logger.js +1 -0
- package/build/navigation/callbackNavigation.d.ts +28 -0
- package/build/navigation/callbackNavigation.js +1 -0
- package/build/navigation/reactNavigation.d.ts +33 -0
- package/build/navigation/reactNavigation.js +1 -0
- package/build/navigation/state.d.ts +20 -0
- package/build/navigation/state.js +1 -0
- package/build/navigation/types.d.ts +27 -0
- package/build/navigation/types.js +1 -0
- package/build/protocol.d.ts +108 -0
- package/build/protocol.js +1 -0
- package/build/react/VoiceBotProvider.d.ts +52 -0
- package/build/react/VoiceBotProvider.js +1 -0
- package/build/react/VoiceBotWidget.d.ts +29 -0
- package/build/react/VoiceBotWidget.js +1 -0
- package/build/react/useVoiceBot.d.ts +17 -0
- package/build/react/useVoiceBot.js +1 -0
- package/docs/INTEGRATION.md +189 -0
- package/docs/PROTOCOL.md +271 -0
- package/expo-module.config.json +9 -0
- package/ios/VoiceBotAudio.podspec +25 -0
- package/ios/VoiceBotAudioModule.swift +219 -0
- package/package.json +76 -4
- package/plugin/withVoiceBot.js +137 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.1.0 (first release)
|
|
4
|
+
|
|
5
|
+
The first public release of `react-native-voice-bot-sdk`: a drop-in voice assistant widget for
|
|
6
|
+
React Native and Expo apps.
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
- **Drop-in widget.** Wrap your app with `<VoiceBotProvider url="wss://…" domainId="…">` and a
|
|
11
|
+
floating mic button appears. Or use your own button with the `useVoiceBot()` hook.
|
|
12
|
+
- **Voice streaming.** The mic is sent to your server as raw PCM (16-bit, 16 kHz, mono) in 640-byte
|
|
13
|
+
frames, 50 a second. The server's spoken reply is played at the `sample_rate` it sends
|
|
14
|
+
(default 16 kHz, e.g. 24 kHz voices).
|
|
15
|
+
- **Voice navigation.** The app sends the names of its screens (never code or data) when it
|
|
16
|
+
connects. The server opens screens with `output.type: "navigation"` + `output.screen` in a reply,
|
|
17
|
+
or a `navigate` message, with normal back-stack behaviour. Works with Expo Router, React
|
|
18
|
+
Navigation, or your own router (callbacks).
|
|
19
|
+
- **Cleaner audio for the server.** A noise gate sends silence instead of room noise between
|
|
20
|
+
sentences, so the server can tell where a sentence ends.
|
|
21
|
+
- **Barge-in.** Talking over the bot stops its reply. The bot's own voice coming back into the mic
|
|
22
|
+
is ignored (`whileSpeaking: 'guard'`).
|
|
23
|
+
- **New replies replace old ones.** A reply to a new question stops the previous reply's audio.
|
|
24
|
+
- **Background listening (Android).** The mic keeps working when the app is in the background,
|
|
25
|
+
with a notification while it's on.
|
|
26
|
+
- **Auto-reconnect.** After a drop the SDK retries (1 s, 2 s, 4 s … up to 30 s), keeping the mic
|
|
27
|
+
running. Optionally keeps the same `sessionId` (`keepSessionOnReconnect`).
|
|
28
|
+
- **Other apps and calls (Android).** Other apps' audio is lowered while the bot speaks; the bot
|
|
29
|
+
stops when another app takes the audio or a phone call starts, and the mic sends silence during
|
|
30
|
+
the call.
|
|
31
|
+
- **Saved visitor id.** `cookie_id` is saved on the device, so it stays the same across launches.
|
|
32
|
+
- **Logs.** Every request and response in the console (`logLevel`), with readable summaries.
|
|
33
|
+
- **Expo config plugin.** Microphone permission, optional `ws://` support, and trusting a
|
|
34
|
+
self-signed test-server certificate in Android dev builds.
|
|
35
|
+
|
|
36
|
+
### Platform support
|
|
37
|
+
|
|
38
|
+
| Platform | Status |
|
|
39
|
+
|----------|--------|
|
|
40
|
+
| Android | Tested on a Pixel 7 against a real voice server. |
|
|
41
|
+
| iOS | Implemented, **not tested on a device yet**. Background listening isn't supported on iOS yet. |
|
|
42
|
+
| Expo Go | Not supported (native module); use a development build. |
|
|
43
|
+
|
|
44
|
+
### Known limitations
|
|
45
|
+
|
|
46
|
+
- The noise gate works on loudness. It removes steady noise, but people talking near the phone as
|
|
47
|
+
loudly as the user still get through.
|
|
48
|
+
- The React Navigation adapter is covered by unit tests only; the example app uses Expo Router.
|
|
49
|
+
- `trustedCertificates` (self-signed servers) works in Android dev builds only.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,465 @@
|
|
|
1
|
-
#
|
|
1
|
+
# react-native-voice-bot-sdk
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Drop-in **voice assistant widget** for React Native and Expo apps. Your users tap a floating mic and
|
|
4
|
+
talk; the SDK streams their voice to your voice server, plays the spoken answer, and lets the server
|
|
5
|
+
**open screens in your app** ("open my cart", "go back").
|
|
6
|
+
|
|
7
|
+
- ✅ One wrapper, one floating mic button: no other UI work needed
|
|
8
|
+
- ✅ Or your own button with the `useVoiceBot()` hook
|
|
9
|
+
- ✅ The server can navigate your app (Expo Router, React Navigation, or your own router)
|
|
10
|
+
- ✅ Clean audio: noise gate, barge-in (talk over the bot), echo guard
|
|
11
|
+
- ✅ Keeps listening in the background (Android), reconnects by itself, handles phone calls
|
|
12
|
+
- ✅ Every request and response in the console, TypeScript-first
|
|
13
|
+
|
|
14
|
+
> Integration guide: [docs/INTEGRATION.md](docs/INTEGRATION.md) · Wire protocol for the server team:
|
|
15
|
+
> [docs/PROTOCOL.md](docs/PROTOCOL.md) · Release notes: [CHANGELOG.md](CHANGELOG.md)
|
|
16
|
+
|
|
17
|
+
## Requirements
|
|
18
|
+
|
|
19
|
+
- Expo SDK 52 or newer with a **development build** (`npx expo run:android` / EAS). It doesn't run in
|
|
20
|
+
**Expo Go**, because it has its own native audio module.
|
|
21
|
+
- Or a bare React Native app with Expo modules installed (`npx install-expo-modules@latest`).
|
|
22
|
+
- Android 7+ (tested). iOS 16.4+ (first version, not tested on a device yet).
|
|
23
|
+
|
|
24
|
+
## Installation
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
npx expo install react-native-voice-bot-sdk
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Add the config plugin to `app.json` (adds the microphone permission on Android and iOS):
|
|
31
|
+
|
|
32
|
+
```json
|
|
33
|
+
{
|
|
34
|
+
"expo": {
|
|
35
|
+
"plugins": [
|
|
36
|
+
["react-native-voice-bot-sdk", {
|
|
37
|
+
"microphonePermission": "Allow $(PRODUCT_NAME) to use the microphone for the voice assistant",
|
|
38
|
+
"allowCleartextTraffic": true
|
|
39
|
+
}]
|
|
40
|
+
]
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Set `allowCleartextTraffic` only if your server URL is `ws://` (not `wss://`) and you need it in
|
|
46
|
+
release builds.
|
|
47
|
+
|
|
48
|
+
### Self-signed `wss://` server (Android dev builds)
|
|
49
|
+
|
|
50
|
+
Android only trusts certificates from public authorities. If your test server uses a self-signed
|
|
51
|
+
certificate, connecting fails with `Trust anchor for certification path not found`. Save the
|
|
52
|
+
server's certificate in your project and list it in the plugin:
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
mkdir -p certs
|
|
56
|
+
openssl s_client -connect your-test-server:8091 </dev/null 2>/dev/null | openssl x509 -outform PEM > certs/server.pem
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
```json
|
|
60
|
+
["react-native-voice-bot-sdk", { "trustedCertificates": ["./certs/server.pem"] }]
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Then run `npx expo prebuild --clean` and rebuild. The plugin adds an Android network security
|
|
64
|
+
config that trusts these certificates **in debuggable (dev) builds only**; release builds keep the
|
|
65
|
+
normal system trust. The certificate's Subject Alternative Name must contain the host you connect
|
|
66
|
+
to (IP or DNS name). For production, use a certificate from a public CA (e.g. Let's Encrypt) and
|
|
67
|
+
leave this option out. If your app already has its own `networkSecurityConfig`, add the certificate
|
|
68
|
+
there instead. iOS is not covered by this option.
|
|
69
|
+
|
|
70
|
+
Then rebuild the native app:
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
npx expo prebuild --clean
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
npx expo run:android
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
**Bare React Native:** after `npx install-expo-modules@latest` and installing this package, rebuild
|
|
81
|
+
the app. The library's own Android manifest adds `RECORD_AUDIO`; on iOS add
|
|
82
|
+
`NSMicrophoneUsageDescription` to `Info.plist` and run `pod install`.
|
|
83
|
+
|
|
84
|
+
## Quick start (2 steps)
|
|
85
|
+
|
|
86
|
+
### Step 1: wrap your app once with `VoiceBotProvider`
|
|
87
|
+
|
|
88
|
+
**Expo Router** (`app/_layout.tsx`):
|
|
89
|
+
|
|
90
|
+
```tsx
|
|
91
|
+
import { Stack } from 'expo-router';
|
|
92
|
+
import { VoiceBotProvider } from 'react-native-voice-bot-sdk';
|
|
93
|
+
import { useExpoRouterNavigation } from 'react-native-voice-bot-sdk/expo-router';
|
|
94
|
+
|
|
95
|
+
export default function RootLayout() {
|
|
96
|
+
const navigation = useExpoRouterNavigation(); // lets the server open your screens
|
|
97
|
+
|
|
98
|
+
return (
|
|
99
|
+
<VoiceBotProvider url="wss://voice.example.com/demo" domainId="YOUR_DOMAIN_ID" navigation={navigation}>
|
|
100
|
+
<Stack />
|
|
101
|
+
</VoiceBotProvider>
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
**React Native without Expo Router** (`App.tsx`):
|
|
107
|
+
|
|
108
|
+
```tsx
|
|
109
|
+
import { VoiceBotProvider } from 'react-native-voice-bot-sdk';
|
|
110
|
+
|
|
111
|
+
export default function App() {
|
|
112
|
+
return (
|
|
113
|
+
<VoiceBotProvider url="wss://voice.example.com/demo" domainId="YOUR_DOMAIN_ID">
|
|
114
|
+
<AppNavigator />
|
|
115
|
+
</VoiceBotProvider>
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
That's the whole setup for the **widget-only** integration: the provider connects to your server
|
|
121
|
+
and shows the floating mic (bottom-right). The user taps it to talk and taps again to stop.
|
|
122
|
+
|
|
123
|
+
### Step 2 (optional): use it from any screen with `useVoiceBot`
|
|
124
|
+
|
|
125
|
+
```tsx
|
|
126
|
+
import { Button } from 'react-native';
|
|
127
|
+
import { useVoiceBot } from 'react-native-voice-bot-sdk';
|
|
128
|
+
|
|
129
|
+
function TalkButton() {
|
|
130
|
+
const { isListening, start, stop, lastQuestion, lastPrompt } = useVoiceBot();
|
|
131
|
+
return <Button title={isListening ? 'Stop' : 'Talk to us'} onPress={isListening ? stop : start} />;
|
|
132
|
+
}
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
Right after connecting, the SDK sends one connect message with your screen names, then streams
|
|
136
|
+
the mic:
|
|
137
|
+
|
|
138
|
+
```json
|
|
139
|
+
{
|
|
140
|
+
"sessionId": "c0a8f6e2-…",
|
|
141
|
+
"domain_id": "YOUR_DOMAIN_ID",
|
|
142
|
+
"cookie_id": "9b2e4c1a-…",
|
|
143
|
+
"type": "Buffer",
|
|
144
|
+
"deviceId": 1,
|
|
145
|
+
"sampleRate": 16000,
|
|
146
|
+
"screens": ["cart", "index", "products", "profile", "product/[id]"]
|
|
147
|
+
}
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
## Widget on or off
|
|
151
|
+
|
|
152
|
+
```tsx
|
|
153
|
+
<VoiceBotProvider url={URL}> {/* default floating mic */}
|
|
154
|
+
<VoiceBotProvider url={URL} widget={{ size: 56, right: 16, bottom: 90, colors: { idle: '#7048E8' } }}>
|
|
155
|
+
<VoiceBotProvider url={URL} widget={false}> {/* no widget: use your own button */}
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
You can also place `<VoiceBotWidget />` yourself anywhere. The widget shows a spinner while
|
|
159
|
+
connecting, a pulsing red ring while listening, and green while the bot is speaking.
|
|
160
|
+
|
|
161
|
+
## Your own button
|
|
162
|
+
|
|
163
|
+
```tsx
|
|
164
|
+
import { useVoiceBot } from 'react-native-voice-bot-sdk';
|
|
165
|
+
|
|
166
|
+
function TalkButton() {
|
|
167
|
+
const bot = useVoiceBot();
|
|
168
|
+
return (
|
|
169
|
+
<Button
|
|
170
|
+
title={bot.isListening ? 'Stop' : 'Talk'}
|
|
171
|
+
onPress={() => (bot.isListening ? bot.stop() : bot.start())}
|
|
172
|
+
/>
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
Without React, use the `VoiceBot` object: `VoiceBot.start()`, `VoiceBot.stop()`, `VoiceBot.toggle()`,
|
|
178
|
+
`VoiceBot.disconnect()`, `VoiceBot.getState()`, `VoiceBot.subscribe(fn)`.
|
|
179
|
+
|
|
180
|
+
`start()` asks for the microphone permission the first time. If the socket isn't open it reconnects
|
|
181
|
+
with the options from the last `connect()` call and then starts the mic.
|
|
182
|
+
|
|
183
|
+
## API
|
|
184
|
+
|
|
185
|
+
### `VoiceBot.connect(options)`
|
|
186
|
+
|
|
187
|
+
| Option | Type | Description |
|
|
188
|
+
|--------|------|-------------|
|
|
189
|
+
| `url` | `string` | **Required.** WebSocket URL of the voice server. |
|
|
190
|
+
| `domainId` | `string` | Sent as `domain_id` in the connect message. Default `""`. |
|
|
191
|
+
| `cookieId` | `string` | Sent as `cookie_id`. Default: a random id the SDK saves on the device, so it stays the same across app launches. Pass your own id (e.g. your user's visitor id) to use that instead. |
|
|
192
|
+
| `onSuccess` | `({ sessionId }) => void` | Connected; the connect message (with the screen names) was sent. |
|
|
193
|
+
| `onFailure` | `(error) => void` | Couldn't connect. `error.code`: `connect_failed`, `connect_timeout`, `native_missing`, ... |
|
|
194
|
+
| `onClose` | `({ code, reason }) => void` | The socket closed after a successful connect. |
|
|
195
|
+
| `onReply` | `(reply) => void` | Every reply from the server (`question`, `output`, `buffer`). |
|
|
196
|
+
| `autoStart` | `boolean` | Start the mic right after connecting. Default `false`. |
|
|
197
|
+
| `uplinkEncoding` | `'binary' \| 'base64'` | Default `binary` (what the server expects today). `base64` needs server support, see the protocol doc. |
|
|
198
|
+
| `deviceId` | `number` | `deviceId` in the connect message. Default `1`. |
|
|
199
|
+
| `connectTimeoutMs` | `number` | Default `10000`. |
|
|
200
|
+
| `autoReconnect` | `boolean \| { maxAttempts?, maxDelayMs? }` | Reconnect by itself when the socket drops. Default `true` (backoff 1 s, 2 s, 4 s … 30 s, 20 attempts). See [Reconnecting](#reconnecting). |
|
|
201
|
+
| `onReconnecting` | `({ attempt, delayMs }) => void` | Called before each reconnect attempt. |
|
|
202
|
+
| `backgroundListening` | `boolean \| { title?, text? }` | Android: keep listening in the background, with a notification while the mic is on. Default `true`. See [Background listening](#background-listening). |
|
|
203
|
+
| `interruptOnNewReply` | `boolean` | Default `true`: a reply to a new question stops the old reply's audio and plays right away. Parts of the same reply still play in order. `false` queues every reply. |
|
|
204
|
+
| `noiseGate` | `boolean \| { thresholdDb?, minLevelDb?, hangoverMs?, preRollMs? }` | Default `true`: send silence instead of room noise between sentences. See [Noise gate and barge-in](#noise-gate-and-barge-in). |
|
|
205
|
+
| `whileSpeaking` | `'guard' \| 'mute' \| 'listen'` | What the mic does while the bot speaks. Default `'guard'`. |
|
|
206
|
+
| `bargeIn` | `boolean` | Default `true`: talking over the bot stops its reply. |
|
|
207
|
+
| `pauseOnCall` | `boolean` | Default `true`: during a phone call, stop the bot's audio and send silence. |
|
|
208
|
+
| `keepSessionOnReconnect` | `boolean` | Default `false`. `true` keeps the same `sessionId` after an automatic reconnect. |
|
|
209
|
+
| `logLevel` | `'trace' \| 'debug' \| 'info' \| 'warn' \| 'error' \| 'none'` | Console logging, see [Logs](#logs). |
|
|
210
|
+
|
|
211
|
+
### `useVoiceBot()`
|
|
212
|
+
|
|
213
|
+
```tsx
|
|
214
|
+
const { status, isReady, isListening, speaking, lastQuestion, lastPrompt, lastMessage, error,
|
|
215
|
+
start, stop, toggle, connect, disconnect, addMessageListener } = useVoiceBot();
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
| Returns | Description |
|
|
219
|
+
|---------|-------------|
|
|
220
|
+
| `status` | `'idle' \| 'connecting' \| 'connected' \| 'listening'` |
|
|
221
|
+
| `isReady` / `isConnected` | Connected to the server, ready to listen. |
|
|
222
|
+
| `isListening` | The mic is on. |
|
|
223
|
+
| `speaking` | The bot's reply is playing. |
|
|
224
|
+
| `sessionId` | Current session id, or `null`. |
|
|
225
|
+
| `lastQuestion`, `lastPrompt` | What the user said (`question`) and the bot's answer text (`output.prompt`). |
|
|
226
|
+
| `lastMessage` | The most recent JSON message from the server. |
|
|
227
|
+
| `error` / `lastError` | Last error (`{ code, message }`), or `null`. |
|
|
228
|
+
| `start`, `stop`, `toggle` | Mic on / off. `start()` asks for the mic permission the first time and reconnects if needed. |
|
|
229
|
+
| `connect`, `disconnect` | Same as `VoiceBot.connect()` / `disconnect()`. |
|
|
230
|
+
| `addMessageListener(fn)` | Every JSON message from the server. Returns an unsubscribe function. |
|
|
231
|
+
|
|
232
|
+
### `<VoiceBotProvider>`
|
|
233
|
+
|
|
234
|
+
| Prop | Description |
|
|
235
|
+
|------|-------------|
|
|
236
|
+
| `url` | Voice server WebSocket URL. When set, the provider connects by itself. |
|
|
237
|
+
| `domainId` | Sent as `domain_id`. |
|
|
238
|
+
| `autoConnect` | Default `true` (when `url` is set). |
|
|
239
|
+
| `widget` | Default `true`: the floating mic. Widget props to style it, `false` to use your own button. |
|
|
240
|
+
| `navigation` | A navigation adapter, so the server can open screens (see below). |
|
|
241
|
+
| `onConnect`, `onError`, `onClose` | Connection callbacks. |
|
|
242
|
+
| `onReply`, `onMessage` | Every reply / every JSON message from the server. |
|
|
243
|
+
| `options` | Any other `VoiceBot.connect()` option (`noiseGate`, `bargeIn`, `autoReconnect`, `logLevel` …). |
|
|
244
|
+
| `disconnectOnUnmount` | Default `true`. |
|
|
245
|
+
|
|
246
|
+
Without `url`, call `VoiceBot.connect({ url, … })` yourself (for example after login).
|
|
247
|
+
|
|
248
|
+
## Reconnecting
|
|
249
|
+
|
|
250
|
+
If the connection drops (Wi-Fi change, server restart, close code other than 1000), the SDK
|
|
251
|
+
reconnects by itself: after 1 s, 2 s, 4 s … up to 30 s between tries, 20 tries by default. While it
|
|
252
|
+
reconnects:
|
|
253
|
+
|
|
254
|
+
- `status` is `'connecting'` and the widget shows the spinner;
|
|
255
|
+
- **the mic keeps running**, frames recorded while offline are dropped (not queued), and streaming
|
|
256
|
+
continues as soon as the socket is open again;
|
|
257
|
+
- each reconnect is a **new session**: new `sessionId`, the connect message is sent again, and
|
|
258
|
+
`onSuccess` is called again; `onClose` is called for the dropped socket.
|
|
259
|
+
|
|
260
|
+
Tapping the mic, or coming back to the app, retries right away. `disconnect()` stops reconnecting.
|
|
261
|
+
The server closing with code `1000` is treated as a normal end and is not retried.
|
|
262
|
+
|
|
263
|
+
```ts
|
|
264
|
+
VoiceBot.connect({
|
|
265
|
+
url,
|
|
266
|
+
autoReconnect: { maxAttempts: 10, maxDelayMs: 15000 }, // or false
|
|
267
|
+
onReconnecting: ({ attempt, delayMs }) => console.log('reconnect', attempt, delayMs),
|
|
268
|
+
});
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
## Noise gate and barge-in
|
|
272
|
+
|
|
273
|
+
**Noise gate.** In a real room the pauses between sentences are full of noise, so the server can't
|
|
274
|
+
tell where a sentence ends. The SDK checks every 20 ms frame against the room's noise level and,
|
|
275
|
+
while nobody is speaking near the phone, sends silence instead. The stream keeps its shape (one
|
|
276
|
+
640-byte frame every 20 ms). The 300 ms before speech starts is kept, so the first syllable isn't
|
|
277
|
+
cut, which delays the stream by 300 ms; 700 ms after the last word is kept too. It's an energy gate:
|
|
278
|
+
it removes steady noise and makes pauses clean, but people talking near the phone at the same
|
|
279
|
+
volume as the user still get through.
|
|
280
|
+
|
|
281
|
+
```ts
|
|
282
|
+
VoiceBot.connect({ url, noiseGate: { thresholdDb: 8 } }); // more sensitive (default 10)
|
|
283
|
+
VoiceBot.connect({ url, noiseGate: false }); // send the mic unchanged
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
**While the bot speaks** (`whileSpeaking`): `'guard'` (default) needs 8 dB louder speech, so the
|
|
287
|
+
bot's own voice coming back into the mic isn't taken as the user. `'mute'` sends silence while the
|
|
288
|
+
bot speaks. `'listen'` treats it like any other time.
|
|
289
|
+
|
|
290
|
+
**Barge-in** (`bargeIn`, default on): when the user talks over the bot for about 200 ms, the bot's
|
|
291
|
+
reply stops right away, and the rest of that reply is not played.
|
|
292
|
+
|
|
293
|
+
**Other apps and calls.** While the bot speaks it takes the audio focus, so music from other apps
|
|
294
|
+
gets quieter. If another app takes the audio (or a call comes in), the bot's reply stops. During a
|
|
295
|
+
phone call the mic sends silence, and normal audio resumes when the call ends (`pauseOnCall`).
|
|
296
|
+
|
|
297
|
+
## Background listening
|
|
298
|
+
|
|
299
|
+
On Android, apps lose the microphone in the background unless they run a *foreground service* of
|
|
300
|
+
type microphone, which must show a notification. While the mic is on, the SDK runs one, so the voice
|
|
301
|
+
bot keeps listening when the user switches apps or locks the screen. The notification goes away
|
|
302
|
+
when the mic stops. On Android 13+ the SDK asks for the notification permission the first time; if
|
|
303
|
+
it's denied, background listening still works but the notification is hidden.
|
|
304
|
+
|
|
305
|
+
```ts
|
|
306
|
+
VoiceBot.connect({ url, backgroundListening: { title: 'My Shop assistant', text: 'Listening…' } });
|
|
307
|
+
VoiceBot.connect({ url, backgroundListening: false }); // mic only while the app is on screen
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
The library's manifest adds `FOREGROUND_SERVICE`, `FOREGROUND_SERVICE_MICROPHONE` and
|
|
311
|
+
`POST_NOTIFICATIONS`, and the service. Apps on Google Play must declare the microphone foreground
|
|
312
|
+
service use in the Play Console. iOS: not supported yet.
|
|
313
|
+
|
|
314
|
+
## Navigation ("agentic")
|
|
315
|
+
|
|
316
|
+
The server can send `{"type":"navigate","screen":"cart"}` or `{"type":"navigate","action":"back"}`.
|
|
317
|
+
The SDK only opens screens it sent to the server, and follows the usual back stack rules:
|
|
318
|
+
|
|
319
|
+
- a screen that isn't in the stack is **pushed**;
|
|
320
|
+
- a screen that's already in the stack is **popped back to** (the screens above it are removed);
|
|
321
|
+
- the visible screen: nothing happens (`already_on_screen`);
|
|
322
|
+
- `back` works like the Back button.
|
|
323
|
+
|
|
324
|
+
The result goes back to the server as `navigate_result`, and every screen change (voice, tap or
|
|
325
|
+
Back) is sent as `screen_changed` with the back stack. Pick the adapter for your app:
|
|
326
|
+
|
|
327
|
+
### Expo Router
|
|
328
|
+
|
|
329
|
+
```tsx
|
|
330
|
+
import { useExpoRouterNavigation } from 'react-native-voice-bot-sdk/expo-router';
|
|
331
|
+
|
|
332
|
+
const navigation = useExpoRouterNavigation({ exclude: ['modal'] });
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
Call it in the root `_layout.tsx`. Screen names are the file routes: `index`, `products`,
|
|
336
|
+
`(tabs)/index`, `product/[id]`. The server can pass `params` for dynamic routes.
|
|
337
|
+
|
|
338
|
+
### React Navigation
|
|
339
|
+
|
|
340
|
+
```tsx
|
|
341
|
+
import { NavigationContainer, createNavigationContainerRef } from '@react-navigation/native';
|
|
342
|
+
import { VoiceBotProvider, createReactNavigationAdapter } from 'react-native-voice-bot-sdk';
|
|
343
|
+
|
|
344
|
+
const navigationRef = createNavigationContainerRef();
|
|
345
|
+
const navigation = createReactNavigationAdapter(navigationRef, {
|
|
346
|
+
// Optional. Defaults to the screens of the navigators that are mounted.
|
|
347
|
+
screens: ['Home', 'Products', { name: 'Cart', parents: ['Shop'] }],
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
export default function App() {
|
|
351
|
+
return (
|
|
352
|
+
<VoiceBotProvider url={URL} navigation={navigation}>
|
|
353
|
+
<NavigationContainer ref={navigationRef}>{/* navigators */}</NavigationContainer>
|
|
354
|
+
</VoiceBotProvider>
|
|
355
|
+
);
|
|
356
|
+
}
|
|
357
|
+
```
|
|
358
|
+
|
|
359
|
+
Written for React Navigation 7 (`popTo`).
|
|
360
|
+
|
|
361
|
+
### Your own navigation (callbacks)
|
|
362
|
+
|
|
363
|
+
```tsx
|
|
364
|
+
import { createCallbackNavigation } from 'react-native-voice-bot-sdk';
|
|
365
|
+
|
|
366
|
+
const navigation = createCallbackNavigation({
|
|
367
|
+
screens: ['Home', 'Products', 'Cart'],
|
|
368
|
+
initialScreen: 'Home',
|
|
369
|
+
onNavigate: (screen, { action, params }) => myRouter.open(screen.name, params), // action: 'push' | 'popTo'
|
|
370
|
+
onBack: () => myRouter.back(),
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
// When the user moves by hand, tell the SDK:
|
|
374
|
+
navigation.setCurrentScreen('Cart');
|
|
375
|
+
```
|
|
376
|
+
|
|
377
|
+
## Logs
|
|
378
|
+
|
|
379
|
+
The SDK logs to the console with a `[VoiceBot]` prefix, the time and a level. You see these in
|
|
380
|
+
Metro or in `adb logcat` (tag `ReactNativeJS`):
|
|
381
|
+
|
|
382
|
+
```
|
|
383
|
+
[VoiceBot] 14:41:41.205 INFO connecting to ws://localhost:8091/demo
|
|
384
|
+
[VoiceBot] 14:41:41.512 INFO connected in 307 ms, session=81185a05-ccbf-43be-8ffc-20eae1c65a64
|
|
385
|
+
[VoiceBot] 14:41:41.514 INFO → connect message: domain_id=your-domain cookie_id=a527edd9… deviceId=1 sampleRate=16000 screens(5)=["cart","index","product/[id]","products","profile"]
|
|
386
|
+
[VoiceBot] 14:41:41.520 INFO → screen_changed: "index" backStack=["index"]
|
|
387
|
+
[VoiceBot] 14:41:43.101 INFO mic started: PCM s16le 16000 Hz mono, 640-byte frames (20 ms), uplink=BINARY
|
|
388
|
+
[VoiceBot] 14:41:43.140 INFO → first audio frame sent
|
|
389
|
+
[VoiceBot] 14:41:48.120 INFO → audio streaming: 250 frames (5.00 s)
|
|
390
|
+
[VoiceBot] 14:41:49.002 INFO ← you said: "open my cart"
|
|
391
|
+
[VoiceBot] 14:41:49.003 INFO ← bot: "Opening your cart"
|
|
392
|
+
[VoiceBot] 14:41:49.004 INFO 🔊 bot speaking (reply audio playing)
|
|
393
|
+
[VoiceBot] 14:41:49.010 INFO ← navigate: screen="cart" requestId=1
|
|
394
|
+
[VoiceBot] 14:41:49.030 INFO → navigate_result: opened "cart"
|
|
395
|
+
[VoiceBot] 14:41:49.031 INFO → screen_changed: "cart" backStack=["index","cart"]
|
|
396
|
+
[VoiceBot] 14:41:50.300 INFO 🔇 bot finished speaking (1.20 s of audio)
|
|
397
|
+
[VoiceBot] 14:41:58.400 INFO mic stopped after 15.3 s: sent 765 frames (15.30 s of audio, 489600 bytes)
|
|
398
|
+
[VoiceBot] 14:42:00.000 INFO → closing socket: code=1000 reason="User exited"
|
|
399
|
+
```
|
|
400
|
+
|
|
401
|
+
| Level | What is logged |
|
|
402
|
+
|-------|----------------|
|
|
403
|
+
| `trace` | Everything below, plus **every audio frame** sent (`→ SEND audio frame #123`), 50 lines a second. |
|
|
404
|
+
| `debug` | **Every request and response in full:** each JSON message sent (`→ SEND {...}`) and received (`← RECV {...}`). Base64 audio is replaced by its size (`<base64 42668 chars = 32000 bytes>`); everything else is printed as is. Plus each reply audio chunk. **Default in development.** |
|
|
405
|
+
| `info` | Readable summary: connect, connect message, screens, mic start/stop with frame counts, audio progress every 5 s, what you said, bot reply, speaking start/end, navigate and its result, screen changes, socket close. |
|
|
406
|
+
| `warn` | Problems that don't stop the session: permission denied, dropped frames, unknown screen, abnormal close codes, slow network. **Default in release.** |
|
|
407
|
+
| `error` | Connect failed, mic failed, playback failed, navigation threw. |
|
|
408
|
+
| `none` | Nothing. |
|
|
409
|
+
|
|
410
|
+
```ts
|
|
411
|
+
import { setLogLevel, setLogHandler } from 'react-native-voice-bot-sdk';
|
|
412
|
+
|
|
413
|
+
setLogLevel('debug'); // or VoiceBot.connect({ url, logLevel: 'debug' })
|
|
414
|
+
setLogHandler((level, message) => myCrashReporter.log(`[VoiceBot] ${level} ${message}`));
|
|
415
|
+
```
|
|
416
|
+
|
|
417
|
+
`→` is app to server, `←` is server to app. On Android the native audio module also logs to logcat
|
|
418
|
+
with the tag `VoiceBotAudio` (audio source, echo cancel / gain / noise effects, start and stop):
|
|
419
|
+
|
|
420
|
+
```bash
|
|
421
|
+
adb logcat -s ReactNativeJS VoiceBotAudio
|
|
422
|
+
```
|
|
423
|
+
|
|
424
|
+
## Removing the SDK
|
|
425
|
+
|
|
426
|
+
Remove `<VoiceBotProvider>` (or `<VoiceBotWidget>`) and any `VoiceBot.*` calls, delete the plugin
|
|
427
|
+
entry from `app.json`, `npm uninstall react-native-voice-bot-sdk`, and rebuild the native app.
|
|
428
|
+
|
|
429
|
+
## Audio format
|
|
430
|
+
|
|
431
|
+
Both directions use raw PCM, signed 16-bit little-endian, mono, no header. The mic is always 16 kHz.
|
|
432
|
+
Reply audio is played at the reply's `sample_rate` (e.g. `24000`), or 16 kHz if the server doesn't
|
|
433
|
+
send one. The mic is sent as
|
|
434
|
+
640-byte (20 ms) binary frames, in order, with no gaps. Android records from `VOICE_COMMUNICATION`
|
|
435
|
+
with echo cancellation, gain control and noise suppression. Details: [docs/PROTOCOL.md](docs/PROTOCOL.md).
|
|
436
|
+
|
|
437
|
+
## Development
|
|
438
|
+
|
|
439
|
+
```bash
|
|
440
|
+
cd example && npm install && cd ..
|
|
441
|
+
./scripts/link-example-deps.sh # type-check against the example's react-native / expo
|
|
442
|
+
npm run typecheck && npm run build && npm test
|
|
443
|
+
npm pack # → react-native-voice-bot-sdk-<version>.tgz
|
|
444
|
+
```
|
|
445
|
+
|
|
446
|
+
Example app (installs the packed tarball, like a real third-party app):
|
|
447
|
+
|
|
448
|
+
```bash
|
|
449
|
+
cd example && npm install ../react-native-voice-bot-sdk-0.1.0.tgz
|
|
450
|
+
npx expo run:android
|
|
451
|
+
```
|
|
452
|
+
|
|
453
|
+
Local test server (Python 3, no packages needed), with the phone on USB:
|
|
454
|
+
|
|
455
|
+
```bash
|
|
456
|
+
python3 test-server/server.py
|
|
457
|
+
adb reverse tcp:8091 tcp:8091
|
|
458
|
+
```
|
|
459
|
+
|
|
460
|
+
Then use `ws://localhost:8091/demo` in the example app. The server checks the connect message and the
|
|
461
|
+
640-byte frames, sends a reply with audio, and runs a navigation script.
|
|
462
|
+
|
|
463
|
+
## License
|
|
464
|
+
|
|
465
|
+
MIT
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
|
2
|
+
<uses-permission android:name="android.permission.INTERNET" />
|
|
3
|
+
<uses-permission android:name="android.permission.RECORD_AUDIO" />
|
|
4
|
+
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
|
|
5
|
+
<!-- Keep listening while the app is in the background (shows a notification while the mic is on). -->
|
|
6
|
+
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
|
7
|
+
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MICROPHONE" />
|
|
8
|
+
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
|
|
9
|
+
|
|
10
|
+
<application>
|
|
11
|
+
<service
|
|
12
|
+
android:name="expo.modules.voicebot.VoiceBotMicService"
|
|
13
|
+
android:exported="false"
|
|
14
|
+
android:foregroundServiceType="microphone" />
|
|
15
|
+
</application>
|
|
16
|
+
</manifest>
|