@sagepilot-ai/react-native-sdk 0.3.1 → 0.3.2
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/README.md +83 -494
- package/dist/index.d.mts +3 -2
- package/dist/index.d.ts +3 -2
- package/dist/index.js +30 -2
- package/dist/index.mjs +30 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,31 +2,25 @@
|
|
|
2
2
|
|
|
3
3
|
Official React Native SDK for adding Sagepilot chat to iOS and Android apps.
|
|
4
4
|
|
|
5
|
-
The SDK opens the Sagepilot-hosted chat experience inside a React Native WebView and provides APIs for setup, identity, unread count, and app-owned launchers.
|
|
5
|
+
The SDK opens the Sagepilot-hosted chat experience inside a React Native WebView and provides APIs for setup, identity, unread count, attachments, and app-owned launchers.
|
|
6
|
+
|
|
7
|
+
For full integration docs, see [React Native SDK docs](https://docs.sagepilot.ai/sdks/react-native/overview).
|
|
6
8
|
|
|
7
9
|
## Requirements
|
|
8
10
|
|
|
9
11
|
- React 18+
|
|
10
12
|
- React Native 0.72+
|
|
11
13
|
- `react-native-webview` 13+
|
|
12
|
-
- A Sagepilot workspace
|
|
14
|
+
- A Sagepilot workspace ID and chat channel ID
|
|
13
15
|
- Secure token storage for production apps
|
|
14
16
|
|
|
15
|
-
## Install
|
|
16
|
-
|
|
17
|
-
### 1. Install The Base Chat SDK
|
|
18
|
-
|
|
19
|
-
Use this install when your app wants Sagepilot chat without Sagepilot's Android CameraX implementation:
|
|
17
|
+
## Install
|
|
20
18
|
|
|
21
19
|
```bash
|
|
22
20
|
npm install @sagepilot-ai/react-native-sdk@latest react-native-webview
|
|
23
21
|
```
|
|
24
22
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
### 2. Add Secure Session Storage
|
|
28
|
-
|
|
29
|
-
For production apps, install one secure storage library so Sagepilot customer sessions survive app restarts.
|
|
23
|
+
For secure persisted sessions, install one storage library:
|
|
30
24
|
|
|
31
25
|
```bash
|
|
32
26
|
npm install react-native-keychain
|
|
@@ -38,387 +32,11 @@ Expo apps can use `expo-secure-store` instead:
|
|
|
38
32
|
npx expo install expo-secure-store
|
|
39
33
|
```
|
|
40
34
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
Configure Sagepilot once when your app starts. The minimum working setup is:
|
|
44
|
-
|
|
45
|
-
```ts
|
|
46
|
-
import { SagepilotChat } from "@sagepilot-ai/react-native-sdk";
|
|
47
|
-
|
|
48
|
-
await SagepilotChat.configure({
|
|
49
|
-
key: "workspace_id:channel_id"
|
|
50
|
-
});
|
|
51
|
-
```
|
|
52
|
-
|
|
53
|
-
For production, pass secure token storage:
|
|
54
|
-
|
|
55
|
-
```ts
|
|
56
|
-
import * as Keychain from "react-native-keychain";
|
|
57
|
-
import {
|
|
58
|
-
SagepilotChat,
|
|
59
|
-
createKeychainTokenStorage
|
|
60
|
-
} from "@sagepilot-ai/react-native-sdk";
|
|
61
|
-
|
|
62
|
-
await SagepilotChat.configure({
|
|
63
|
-
key: "workspace_id:channel_id",
|
|
64
|
-
tokenStorage: createKeychainTokenStorage(Keychain)
|
|
65
|
-
});
|
|
66
|
-
```
|
|
67
|
-
|
|
68
|
-
Then mount `SagepilotChatProvider` once near the top of your app so the SDK can render the hosted chat WebView when you open it:
|
|
69
|
-
|
|
70
|
-
```tsx
|
|
71
|
-
import { useEffect, type ReactNode } from "react";
|
|
72
|
-
import * as Keychain from "react-native-keychain";
|
|
73
|
-
import {
|
|
74
|
-
SagepilotChat,
|
|
75
|
-
SagepilotChatProvider,
|
|
76
|
-
createKeychainTokenStorage
|
|
77
|
-
} from "@sagepilot-ai/react-native-sdk";
|
|
78
|
-
|
|
79
|
-
function SagepilotProvider({ children }: { children: ReactNode }) {
|
|
80
|
-
useEffect(() => {
|
|
81
|
-
void SagepilotChat.configure({
|
|
82
|
-
key: "workspace_id:channel_id",
|
|
83
|
-
tokenStorage: createKeychainTokenStorage(Keychain)
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
return () => {
|
|
87
|
-
SagepilotChat.destroy();
|
|
88
|
-
};
|
|
89
|
-
}, []);
|
|
90
|
-
|
|
91
|
-
return <SagepilotChatProvider>{children}</SagepilotChatProvider>;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
export function App() {
|
|
95
|
-
return (
|
|
96
|
-
<SagepilotProvider>
|
|
97
|
-
<RootNavigator />
|
|
98
|
-
</SagepilotProvider>
|
|
99
|
-
);
|
|
100
|
-
}
|
|
101
|
-
```
|
|
102
|
-
|
|
103
|
-
### 4. Optional: Add Sagepilot Android CameraX
|
|
104
|
-
|
|
105
|
-
Install this addon only when you want Sagepilot's Android in-app CameraX picker. Apps that skip this step keep the smaller base SDK install.
|
|
106
|
-
|
|
107
|
-
```bash
|
|
108
|
-
npm install @sagepilot-ai/react-native-camera-addon@latest
|
|
109
|
-
```
|
|
110
|
-
|
|
111
|
-
The addon declares `@sagepilot-ai/react-native-sdk` as a peer dependency. Keep the base SDK installed directly in your app; the addon should not be treated as a replacement SDK.
|
|
112
|
-
|
|
113
|
-
After installing the addon, run a fresh native Android build. A Metro reload is not enough for React Native autolinking to register `NativeModules.SagepilotInAppCamera`.
|
|
114
|
-
|
|
115
|
-
Then pass the addon picker into `SagepilotChat.configure`:
|
|
116
|
-
|
|
117
|
-
```ts
|
|
118
|
-
import { SagepilotChat } from "@sagepilot-ai/react-native-sdk";
|
|
119
|
-
import { createSagepilotCameraXFilePicker } from "@sagepilot-ai/react-native-camera-addon";
|
|
120
|
-
|
|
121
|
-
await SagepilotChat.configure({
|
|
122
|
-
key: "workspace_id:channel_id",
|
|
123
|
-
filePicker: createSagepilotCameraXFilePicker()
|
|
124
|
-
});
|
|
125
|
-
```
|
|
126
|
-
|
|
127
|
-
With this setup, the base SDK still owns chat configuration, hosted WebView rendering, auth/session, and the bridge. The camera addon owns CameraX dependencies, native module registration, camera activity, overlay, and image processing.
|
|
128
|
-
|
|
129
|
-
## Configuration Values
|
|
130
|
-
|
|
131
|
-
You configure the SDK with:
|
|
132
|
-
|
|
133
|
-
- `key`: public routing config in the format `workspace_id:channel_id`
|
|
134
|
-
- `tokenStorage`: secure storage adapter for SDK-created customer session tokens
|
|
135
|
-
- optional request, presentation, and behavior settings
|
|
136
|
-
|
|
137
|
-
For standard Sagepilot cloud usage, omit `host`. The SDK connects to Sagepilot's hosted endpoints automatically. Set `host` only when Sagepilot provides a dedicated endpoint for your workspace.
|
|
138
|
-
|
|
139
|
-
```ts
|
|
140
|
-
await SagepilotChat.configure({
|
|
141
|
-
key: "workspace_id:channel_id"
|
|
142
|
-
});
|
|
143
|
-
```
|
|
144
|
-
|
|
145
|
-
If Sagepilot provides a dedicated endpoint for your workspace, pass it as an optional override:
|
|
146
|
-
|
|
147
|
-
```ts
|
|
148
|
-
await SagepilotChat.configure({
|
|
149
|
-
key: "workspace_id:channel_id",
|
|
150
|
-
host: "https://your-sagepilot-host.com"
|
|
151
|
-
});
|
|
152
|
-
```
|
|
153
|
-
|
|
154
|
-
The `key` is not a secret. It is safe to ship in a mobile app. Do not put server API keys, signing secrets, or private workspace secrets in a React Native app.
|
|
155
|
-
|
|
156
|
-
### Supported Configuration Options
|
|
157
|
-
|
|
158
|
-
These options are part of the supported React Native SDK configuration contract.
|
|
159
|
-
|
|
160
|
-
| Option | Purpose |
|
|
161
|
-
|---|---|
|
|
162
|
-
| `key` | Public routing key in the format `workspace_id:channel_id`. |
|
|
163
|
-
| `host` | Optional Sagepilot host override. Most apps should omit this unless Sagepilot provides a dedicated endpoint. |
|
|
164
|
-
| `widgetHost` | Optional hosted widget origin override. Use only when Sagepilot provides a separate widget endpoint. |
|
|
165
|
-
| `headers` | Additional headers for SDK service requests. Do not pass server API keys or long-lived secrets from a mobile app. |
|
|
166
|
-
| `fetch` | Custom fetch implementation for runtimes that need one. |
|
|
167
|
-
| `tokenStorage` | Secure storage adapter for SDK-created customer session tokens. |
|
|
168
|
-
| `anonymousId` | Optional stable anonymous identifier sent when the SDK creates a customer session. |
|
|
169
|
-
| `metadata` | Optional app metadata merged into the session creation payload. |
|
|
170
|
-
| `deviceInfo` | Optional adapter that returns device metadata to include during session creation. |
|
|
171
|
-
| `presentation.style` | Modal presentation style: `"sheet"`, `"fullScreen"`, or `"push"`. |
|
|
172
|
-
| `presentation.mobile` | Adds `mobile=1` to the hosted widget URL. Defaults to `true` for React Native. |
|
|
173
|
-
| `presentation.showCloseButton` | Controls whether the provider renders a native close button around the WebView. Defaults to `false` so the hosted Sagepilot widget owns close behavior. |
|
|
174
|
-
| `theme.accentColor` | Primary brand color used as the fallback launcher button color. |
|
|
175
|
-
| `theme.logoUrl` | Optional brand logo URL exposed in SDK theme state. |
|
|
176
|
-
| `theme.preferredColorScheme` | Optional hosted chat color scheme hint: `"light"`, `"dark"`, or `"system"`. |
|
|
177
|
-
| `theme.launcher` | Optional chat launcher icon, label, and badge color configuration. |
|
|
178
|
-
| `behavior.preloadWebView` | Preloads a hidden hosted chat WebView after configuration. |
|
|
179
|
-
| `behavior.enableUnreadPolling` | Starts or disables automatic unread-count polling after configuration. |
|
|
180
|
-
| `behavior.unreadPollIntervalMs` | Sets the unread polling interval in milliseconds. |
|
|
181
|
-
|
|
182
|
-
`SagepilotChatProvider` renders the Sagepilot-hosted chat UI in a React Native WebView. Most apps do not need any host override.
|
|
183
|
-
|
|
184
|
-
### Chat Launcher Theme
|
|
185
|
-
|
|
186
|
-
Use `theme.launcher` to configure the icon button and unread badge colors for your app-owned launcher. The SDK resolves omitted values to Sagepilot defaults, so customers only need to set the colors they want to control.
|
|
187
|
-
|
|
188
|
-
```ts
|
|
189
|
-
const CHAT_LAUNCHER_CONFIG = {
|
|
190
|
-
label: "Support",
|
|
191
|
-
buttonColor: "#173c2d",
|
|
192
|
-
pressedButtonColor: "#225340",
|
|
193
|
-
disabledButtonColor: "#d8cdbb",
|
|
194
|
-
borderColor: "#2a5e49",
|
|
195
|
-
disabledBorderColor: "#cbbda9",
|
|
196
|
-
iconColor: "#fff8ef",
|
|
197
|
-
iconInsideColor: "#173c2d",
|
|
198
|
-
labelColor: "#d4e9dc",
|
|
199
|
-
disabledContentColor: "#8d8172",
|
|
200
|
-
unreadBadgeColor: "#e76f51",
|
|
201
|
-
unreadBadgeTextColor: "#ffffff",
|
|
202
|
-
unreadBadgeBorderColor: "#efe4d4"
|
|
203
|
-
};
|
|
204
|
-
|
|
205
|
-
await SagepilotChat.configure({
|
|
206
|
-
key: "workspace_id:channel_id",
|
|
207
|
-
theme: {
|
|
208
|
-
accentColor: CHAT_LAUNCHER_CONFIG.buttonColor,
|
|
209
|
-
preferredColorScheme: "system",
|
|
210
|
-
launcher: CHAT_LAUNCHER_CONFIG
|
|
211
|
-
}
|
|
212
|
-
});
|
|
213
|
-
```
|
|
214
|
-
|
|
215
|
-
Use the resolved launcher theme from `useSagepilotChat()` when rendering your own button:
|
|
216
|
-
|
|
217
|
-
```tsx
|
|
218
|
-
import { Pressable, Text, View } from "react-native";
|
|
219
|
-
import { MessageCircle } from "lucide-react-native";
|
|
220
|
-
import { useSagepilotChat } from "@sagepilot-ai/react-native-sdk";
|
|
221
|
-
|
|
222
|
-
export function ChatLauncher() {
|
|
223
|
-
const { configured, unreadCount, presentMessages, theme } = useSagepilotChat();
|
|
224
|
-
const launcher = theme.launcher;
|
|
225
|
-
|
|
226
|
-
return (
|
|
227
|
-
<Pressable
|
|
228
|
-
accessibilityRole="button"
|
|
229
|
-
accessibilityLabel={launcher.label}
|
|
230
|
-
disabled={!configured}
|
|
231
|
-
onPress={presentMessages}
|
|
232
|
-
style={({ pressed }) => ({
|
|
233
|
-
alignItems: "center",
|
|
234
|
-
backgroundColor: !configured
|
|
235
|
-
? launcher.disabledButtonColor
|
|
236
|
-
: pressed
|
|
237
|
-
? launcher.pressedButtonColor
|
|
238
|
-
: launcher.buttonColor,
|
|
239
|
-
borderColor: !configured ? launcher.disabledBorderColor : launcher.borderColor,
|
|
240
|
-
borderRadius: 28,
|
|
241
|
-
borderWidth: 1,
|
|
242
|
-
height: 56,
|
|
243
|
-
justifyContent: "center",
|
|
244
|
-
width: 56
|
|
245
|
-
})}
|
|
246
|
-
>
|
|
247
|
-
<MessageCircle
|
|
248
|
-
color={!configured ? launcher.disabledContentColor : launcher.iconColor}
|
|
249
|
-
fill={!configured ? launcher.disabledContentColor : launcher.iconInsideColor}
|
|
250
|
-
size={24}
|
|
251
|
-
/>
|
|
252
|
-
{unreadCount > 0 ? (
|
|
253
|
-
<View
|
|
254
|
-
style={{
|
|
255
|
-
alignItems: "center",
|
|
256
|
-
backgroundColor: launcher.unreadBadgeColor,
|
|
257
|
-
borderColor: launcher.unreadBadgeBorderColor,
|
|
258
|
-
borderRadius: 9,
|
|
259
|
-
borderWidth: 1,
|
|
260
|
-
minWidth: 18,
|
|
261
|
-
paddingHorizontal: 4,
|
|
262
|
-
position: "absolute",
|
|
263
|
-
right: -2,
|
|
264
|
-
top: -2
|
|
265
|
-
}}
|
|
266
|
-
>
|
|
267
|
-
<Text style={{ color: launcher.unreadBadgeTextColor, fontSize: 11 }}>
|
|
268
|
-
{unreadCount}
|
|
269
|
-
</Text>
|
|
270
|
-
</View>
|
|
271
|
-
) : null}
|
|
272
|
-
</Pressable>
|
|
273
|
-
);
|
|
274
|
-
}
|
|
275
|
-
```
|
|
276
|
-
|
|
277
|
-
## Native File Picker
|
|
278
|
-
|
|
279
|
-
By default, the hosted chat widget's attach button uses the WebView's `<input type="file">`. On low-RAM Android devices this is unreliable: while the system camera is in the foreground, Android can kill the WebView render process, leaving a dead white screen and losing the captured photo.
|
|
280
|
-
|
|
281
|
-
Configure `filePicker` so the attach button uses native pickers instead. Captured files are delivered to the widget over a hardened bridge (protocol v2): they are held in app memory **and** mirrored to `cacheStorage`, then **re-delivered until the widget acknowledges them** — surviving the WebView hydration race, renderer crashes, and (for small batches) full app-process death. Pending batches are pinned to the hosted chat route that requested them, so a recovered photo is re-delivered to the originating thread. On Android the SDK also runs a liveness watchdog on app-resume that repaints and, if the widget is confirmed dead, remounts the WebView.
|
|
282
|
-
|
|
283
|
-
The base package does not include CameraX. Apps that want the smallest install can use only:
|
|
284
|
-
|
|
285
|
-
```bash
|
|
286
|
-
npm install @sagepilot-ai/react-native-sdk@latest react-native-webview
|
|
287
|
-
```
|
|
288
|
-
|
|
289
|
-
### Optional Android CameraX Addon
|
|
290
|
-
|
|
291
|
-
Install the CameraX addon only when you want Sagepilot's Android in-app camera implementation:
|
|
292
|
-
|
|
293
|
-
```bash
|
|
294
|
-
npm install @sagepilot-ai/react-native-camera-addon@latest
|
|
295
|
-
```
|
|
296
|
-
|
|
297
|
-
The addon declares `@sagepilot-ai/react-native-sdk` as a peer dependency. Keep the base SDK installed directly in your app; the addon should not be treated as a replacement SDK.
|
|
298
|
-
|
|
299
|
-
Run a clean native Android rebuild after installing the addon. If you only Metro-reload, the native module is not linked yet and `createSagepilotCameraXFilePicker()` returns `undefined`, so the hosted widget falls back to the WebView file input.
|
|
300
|
-
|
|
301
|
-
Then configure the addon adapter:
|
|
302
|
-
|
|
303
|
-
```ts
|
|
304
|
-
import { SagepilotChat } from "@sagepilot-ai/react-native-sdk";
|
|
305
|
-
import { createSagepilotCameraXFilePicker } from "@sagepilot-ai/react-native-camera-addon";
|
|
306
|
-
|
|
307
|
-
await SagepilotChat.configure({
|
|
308
|
-
key: "workspace_id:channel_id",
|
|
309
|
-
filePicker: createSagepilotCameraXFilePicker()
|
|
310
|
-
});
|
|
311
|
-
```
|
|
312
|
-
|
|
313
|
-
The addon owns the Android CameraX Gradle dependencies, `CAMERA` permission merge, native module, camera activity, overlay, and image processing. The base SDK continues to own the hosted chat WebView, auth/session, bridge, attachment protocol, and `SagepilotFilePickerAdapter` type.
|
|
314
|
-
|
|
315
|
-
For the strongest resilience (a photo captured right before the OS kills the app survives the restart, at any size), pass BOTH `cacheStorage` (stores the tiny manifest) and `fileStore` (stores the bytes on disk). `cacheStorage` alone keeps only small (≤~2MB) batches durable, because Android's AsyncStorage caps a single row near 2MB; `fileStore` lifts that limit by writing bytes to an app-private file and persisting only file keys in the manifest:
|
|
316
|
-
|
|
317
|
-
```ts
|
|
318
|
-
import AsyncStorage from "@react-native-async-storage/async-storage";
|
|
319
|
-
import ReactNativeBlobUtil from "react-native-blob-util";
|
|
320
|
-
import { createAsyncStorageCacheStorage, createSagepilotFileStore } from "@sagepilot-ai/react-native-sdk";
|
|
321
|
-
import { createSagepilotCameraXFilePicker } from "@sagepilot-ai/react-native-camera-addon";
|
|
322
|
-
|
|
323
|
-
await SagepilotChat.configure({
|
|
324
|
-
key: "workspace_id:channel_id",
|
|
325
|
-
cacheStorage: createAsyncStorageCacheStorage(AsyncStorage),
|
|
326
|
-
fileStore: createSagepilotFileStore(ReactNativeBlobUtil),
|
|
327
|
-
filePicker: createSagepilotCameraXFilePicker()
|
|
328
|
-
});
|
|
329
|
-
```
|
|
330
|
-
|
|
331
|
-
### Host-Provided Pickers
|
|
332
|
-
|
|
333
|
-
You can also provide your own picker modules to the base SDK. Modules are passed in rather than imported by the SDK, so Metro never resolves packages your app did not install.
|
|
334
|
-
|
|
335
|
-
Install the optional dependencies your app needs:
|
|
336
|
-
|
|
337
|
-
```sh
|
|
338
|
-
# Camera/gallery through react-native-image-picker
|
|
339
|
-
npm install react-native-image-picker
|
|
340
|
-
|
|
341
|
-
# Document/file picking (optional)
|
|
342
|
-
npm install @react-native-documents/picker
|
|
343
|
-
|
|
344
|
-
# Reliable document reads (optional, recommended with the documents picker)
|
|
345
|
-
npm install react-native-blob-util
|
|
346
|
-
```
|
|
347
|
-
|
|
348
|
-
Then pass the modules to `createSagepilotFilePicker`:
|
|
349
|
-
|
|
350
|
-
```ts
|
|
351
|
-
import * as imagePicker from "react-native-image-picker";
|
|
352
|
-
import * as documentsPicker from "@react-native-documents/picker";
|
|
353
|
-
import ReactNativeBlobUtil from "react-native-blob-util";
|
|
354
|
-
import {
|
|
355
|
-
SagepilotChat,
|
|
356
|
-
createSagepilotFilePicker
|
|
357
|
-
} from "@sagepilot-ai/react-native-sdk";
|
|
358
|
-
|
|
359
|
-
await SagepilotChat.configure({
|
|
360
|
-
key: "workspace_id:channel_id",
|
|
361
|
-
filePicker: createSagepilotFilePicker({
|
|
362
|
-
imagePicker,
|
|
363
|
-
documentsPicker,
|
|
364
|
-
fileReader: ReactNativeBlobUtil
|
|
365
|
-
})
|
|
366
|
-
});
|
|
367
|
-
```
|
|
368
|
-
|
|
369
|
-
Notes:
|
|
370
|
-
|
|
371
|
-
- Android CameraX images are captured by `@sagepilot-ai/react-native-camera-addon` and downscaled natively (1920px longest edge, JPEG quality 0.8 by default; override with `imageMaxDimension` / `imageQuality`). This keeps memory and upload sizes low on constrained devices.
|
|
372
|
-
- The CameraX addon can also expose Android Photo Picker and document sources from its native module. The base `createSagepilotFilePicker(...)` helper uses only the host-provided modules you pass into it.
|
|
373
|
-
- Gallery multi-select is capped at 5 by default (`imageSelectionLimit`); documents are rejected above 20MB (`documentMaxFileSizeBytes`) and images above 15MB (`imageMaxFileSizeBytes`) **before** they are read into memory, preventing out-of-memory crashes on large files.
|
|
374
|
-
- Failures are never silent: the picker distinguishes user-cancel from real errors and surfaces a typed `code` (`permission_denied`, `camera_unavailable`, `encode_failed`, `file_too_large`, `read_failed`) to the widget.
|
|
375
|
-
- Android apps that install the CameraX addon must grant `CAMERA` at runtime; the addon requests it automatically before opening the camera and surfaces a clear message (with a Settings hint) when it is denied. iOS apps using `react-native-image-picker` need `NSCameraUsageDescription` and `NSPhotoLibraryUsageDescription` in `Info.plist`.
|
|
376
|
-
- Apps that skip `filePicker` keep the WebView file input. The SDK still recovers from WebView renderer crashes by reloading the widget, but a photo captured at crash time cannot be restored in that mode.
|
|
377
|
-
|
|
378
|
-
## Secure Session Storage
|
|
379
|
-
|
|
380
|
-
The SDK creates opaque customer session tokens at runtime. These tokens are not exposed through hooks or public session APIs.
|
|
381
|
-
|
|
382
|
-
If `tokenStorage` is not provided, tokens are kept in memory only and the session will not persist after the app process restarts. Production apps should pass secure native storage.
|
|
383
|
-
|
|
384
|
-
### React Native Keychain
|
|
385
|
-
|
|
386
|
-
`react-native-keychain` stores values in iOS Keychain and Android secure storage backed by Android Keystore.
|
|
387
|
-
|
|
388
|
-
```ts
|
|
389
|
-
import * as Keychain from "react-native-keychain";
|
|
390
|
-
import {
|
|
391
|
-
SagepilotChat,
|
|
392
|
-
createKeychainTokenStorage
|
|
393
|
-
} from "@sagepilot-ai/react-native-sdk";
|
|
394
|
-
|
|
395
|
-
await SagepilotChat.configure({
|
|
396
|
-
key: "workspace_id:channel_id",
|
|
397
|
-
tokenStorage: createKeychainTokenStorage(Keychain)
|
|
398
|
-
});
|
|
399
|
-
```
|
|
400
|
-
|
|
401
|
-
### Expo SecureStore
|
|
402
|
-
|
|
403
|
-
Expo SecureStore uses iOS Keychain and Android encrypted storage backed by Android Keystore.
|
|
404
|
-
|
|
405
|
-
```ts
|
|
406
|
-
import * as SecureStore from "expo-secure-store";
|
|
407
|
-
import { SagepilotChat } from "@sagepilot-ai/react-native-sdk";
|
|
408
|
-
|
|
409
|
-
await SagepilotChat.configure({
|
|
410
|
-
key: "workspace_id:channel_id",
|
|
411
|
-
tokenStorage: {
|
|
412
|
-
getItem: SecureStore.getItemAsync,
|
|
413
|
-
setItem: SecureStore.setItemAsync,
|
|
414
|
-
removeItem: SecureStore.deleteItemAsync
|
|
415
|
-
}
|
|
416
|
-
});
|
|
417
|
-
```
|
|
35
|
+
The SDK key is public routing config in the format `workspace_id:channel_id`. Do not ship server API keys, signing secrets, or private workspace secrets in a React Native app.
|
|
418
36
|
|
|
419
37
|
## Basic Setup
|
|
420
38
|
|
|
421
|
-
Configure
|
|
39
|
+
Configure Sagepilot once when your app starts, then mount `SagepilotChatProvider` near the top of the app.
|
|
422
40
|
|
|
423
41
|
```tsx
|
|
424
42
|
import * as Keychain from "react-native-keychain";
|
|
@@ -445,7 +63,8 @@ export function SagepilotProvider({ children }: { children: ReactNode }) {
|
|
|
445
63
|
},
|
|
446
64
|
behavior: {
|
|
447
65
|
preloadWebView: true,
|
|
448
|
-
enableUnreadPolling: true
|
|
66
|
+
enableUnreadPolling: true,
|
|
67
|
+
waitForIdentifyBeforeComposer: true
|
|
449
68
|
}
|
|
450
69
|
});
|
|
451
70
|
|
|
@@ -470,8 +89,6 @@ export function SagepilotProvider({ children }: { children: ReactNode }) {
|
|
|
470
89
|
}
|
|
471
90
|
```
|
|
472
91
|
|
|
473
|
-
Mount the provider once near the top of your app:
|
|
474
|
-
|
|
475
92
|
```tsx
|
|
476
93
|
export function App() {
|
|
477
94
|
return (
|
|
@@ -482,9 +99,11 @@ export function App() {
|
|
|
482
99
|
}
|
|
483
100
|
```
|
|
484
101
|
|
|
102
|
+
More setup details: [Configuration](https://docs.sagepilot.ai/sdks/react-native/configuration).
|
|
103
|
+
|
|
485
104
|
## Opening Chat
|
|
486
105
|
|
|
487
|
-
|
|
106
|
+
Use the hook for app-owned launchers:
|
|
488
107
|
|
|
489
108
|
```tsx
|
|
490
109
|
import { Pressable, Text } from "react-native";
|
|
@@ -503,145 +122,115 @@ export function SupportButton() {
|
|
|
503
122
|
}
|
|
504
123
|
```
|
|
505
124
|
|
|
506
|
-
|
|
125
|
+
You can also call the public methods directly:
|
|
507
126
|
|
|
508
127
|
```ts
|
|
128
|
+
import { SagepilotChat } from "@sagepilot-ai/react-native-sdk";
|
|
129
|
+
|
|
509
130
|
SagepilotChat.present();
|
|
510
131
|
SagepilotChat.presentMessages();
|
|
511
|
-
SagepilotChat.presentMessageComposer("I need help with my order");
|
|
512
|
-
SagepilotChat.presentMessageComposer("
|
|
513
|
-
SagepilotChat.presentMessageComposer("
|
|
132
|
+
await SagepilotChat.presentMessageComposer("I need help with my order");
|
|
133
|
+
await SagepilotChat.presentMessageComposer("I need help with my order", { mode: "auto" });
|
|
134
|
+
await SagepilotChat.presentMessageComposer("Start a new request", { mode: "new" });
|
|
135
|
+
await SagepilotChat.presentMessageComposer("I need help with this order", { chatId: savedChatId });
|
|
514
136
|
```
|
|
515
137
|
|
|
516
|
-
`presentMessageComposer(message)` pre-fills the composer and lets Sagepilot reuse an existing conversation when one is available.
|
|
517
|
-
|
|
518
|
-
Use `{ mode: "new" }` only when the button or workflow should always start a fresh conversation.
|
|
519
|
-
|
|
520
|
-
Pass `{ chatId }` when an app-owned surface should reopen a specific conversation, such as an order help button. The SDK sends this as the hosted widget `chat_id` and keeps the existing no-`chatId` behavior unchanged.
|
|
521
|
-
|
|
522
|
-
To store the chat id when a customer creates a conversation from a screen-specific composer:
|
|
138
|
+
`presentMessageComposer(message)` uses `{ mode: "auto" }` by default. It pre-fills the composer and lets Sagepilot reuse an existing conversation when one is available. Use `{ mode: "new" }` only when a workflow should always start a fresh conversation.
|
|
523
139
|
|
|
524
|
-
|
|
525
|
-
SagepilotChat.presentMessageComposer("I need help with this order", {
|
|
526
|
-
metadata: { orderId: "10212984" },
|
|
527
|
-
onConversationCreated: ({ chat_id, metadata }) => {
|
|
528
|
-
// Store metadata.orderId -> chat_id in your app.
|
|
529
|
-
}
|
|
530
|
-
});
|
|
531
|
-
```
|
|
140
|
+
`presentMessageComposer()` returns `boolean` by default. If `behavior.waitForIdentifyBeforeComposer` is `true` and `identify()` is already in flight, it returns `Promise<boolean>` and waits up to 60 seconds for identity to finish before opening. You can `await` it in both cases.
|
|
532
141
|
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
```ts
|
|
536
|
-
const unsubscribe = SagepilotChat.onConversationCreated(({ chat_id }) => {
|
|
537
|
-
// Persist chat_id for later use.
|
|
538
|
-
});
|
|
539
|
-
```
|
|
142
|
+
More examples: [Opening chat](https://docs.sagepilot.ai/sdks/react-native/opening-chat).
|
|
540
143
|
|
|
541
144
|
## Identity
|
|
542
145
|
|
|
543
|
-
Call `identify()` when you know the signed-in
|
|
146
|
+
Call `identify()` after `configure()` when you know the signed-in customer.
|
|
544
147
|
|
|
545
148
|
```ts
|
|
546
149
|
await SagepilotChat.identify({
|
|
547
|
-
userId:
|
|
548
|
-
email:
|
|
549
|
-
name:
|
|
550
|
-
phone:
|
|
551
|
-
customProperties: {
|
|
552
|
-
plan: "pro",
|
|
553
|
-
storeId: "store_123"
|
|
554
|
-
}
|
|
150
|
+
userId: "user_123",
|
|
151
|
+
email: "customer@example.com",
|
|
152
|
+
name: "Customer Name",
|
|
153
|
+
phone: "+15551234567"
|
|
555
154
|
});
|
|
556
155
|
```
|
|
557
156
|
|
|
558
|
-
|
|
157
|
+
Use `waitForIdentifyBeforeComposer: true` for authenticated support flows where the composer should wait for an in-flight identity request before opening.
|
|
559
158
|
|
|
560
159
|
```ts
|
|
561
|
-
await SagepilotChat.
|
|
562
|
-
userId: user.id,
|
|
563
|
-
email: user.email,
|
|
564
|
-
userHash: serverGeneratedUserHash
|
|
565
|
-
});
|
|
160
|
+
const opened = await SagepilotChat.presentMessageComposer("I need help with my order");
|
|
566
161
|
```
|
|
567
162
|
|
|
568
|
-
Never generate `userHash` in the mobile app with a signing secret.
|
|
569
|
-
|
|
570
|
-
## Logout
|
|
571
|
-
|
|
572
163
|
When the app user signs out, clear Sagepilot identity/session state:
|
|
573
164
|
|
|
574
165
|
```ts
|
|
575
166
|
await SagepilotChat.logout();
|
|
576
|
-
|
|
167
|
+
SagepilotChat.destroy();
|
|
577
168
|
```
|
|
578
169
|
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
## Public API
|
|
582
|
-
|
|
583
|
-
### Setup
|
|
584
|
-
|
|
585
|
-
- `SagepilotChat.configure(config)`: initializes the SDK, loads channel config, creates or resumes a customer session, and starts unread polling unless disabled.
|
|
586
|
-
- `SagepilotChat.destroy()`: stops polling, clears in-memory SDK state, and removes listeners.
|
|
587
|
-
- `SagepilotChat.getChannel()`: returns the loaded channel bootstrap data.
|
|
588
|
-
|
|
589
|
-
### Identity And Session
|
|
590
|
-
|
|
591
|
-
- `SagepilotChat.identify(identity)`: links the active session to a known customer.
|
|
592
|
-
- `SagepilotChat.logout()`: removes known-customer identity from the active session.
|
|
593
|
-
- `SagepilotChat.getSession()`: fetches the current session and returns safe session metadata. It does not expose the session token.
|
|
594
|
-
- `SagepilotChat.getSessionState()`: returns safe local session metadata. It does not expose the session token.
|
|
595
|
-
- `SagepilotChat.getIdentityState()`: returns local identity state.
|
|
596
|
-
- `SagepilotChat.onIdentify(callback)`: subscribes to successful identify events.
|
|
597
|
-
|
|
598
|
-
### Hosted Chat UI
|
|
599
|
-
|
|
600
|
-
- `SagepilotChat.present()`: opens the hosted chat home screen.
|
|
601
|
-
- `SagepilotChat.presentMessages()`: opens the hosted conversations/messages screen.
|
|
602
|
-
- `SagepilotChat.presentMessageComposer(message?, options?)`: opens the message composer and optionally pre-fills the composer text. By default, Sagepilot reuses an existing conversation when one is available. Pass `{ mode: "new" }` to force a fresh conversation, or `{ chatId }` to open a specific conversation. It does not auto-send.
|
|
603
|
-
- `SagepilotChat.dismiss()`: closes the hosted chat modal.
|
|
604
|
-
- `SagepilotChat.hide()`: alias for `dismiss()`.
|
|
605
|
-
- `SagepilotChat.toggle()`: opens the chat when closed and closes it when open.
|
|
606
|
-
- `SagepilotChat.isPresented()`: returns whether the hosted chat modal is open.
|
|
607
|
-
- `SagepilotChat.onConversationCreated(callback)`: subscribes to hosted conversations created from the React Native widget and returns `chat_id` plus any composer metadata.
|
|
608
|
-
- `SagepilotChatProvider`: renders the hosted Sagepilot chat inside a React Native modal WebView.
|
|
170
|
+
More details: [Identity](https://docs.sagepilot.ai/sdks/react-native/identity).
|
|
609
171
|
|
|
610
|
-
|
|
172
|
+
## Attachments
|
|
611
173
|
|
|
612
|
-
|
|
174
|
+
The base package supports custom native file-picker adapters. Apps that want Sagepilot's Android in-app CameraX picker can install the optional addon:
|
|
613
175
|
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
- `SagepilotChat.stopUnreadPolling()`: stops unread polling.
|
|
618
|
-
|
|
619
|
-
### Lifecycle Events
|
|
176
|
+
```bash
|
|
177
|
+
npm install @sagepilot-ai/react-native-camera-addon@latest
|
|
178
|
+
```
|
|
620
179
|
|
|
621
|
-
|
|
622
|
-
- `SagepilotChat.onPresent(callback)`: fires when chat opens.
|
|
623
|
-
- `SagepilotChat.onDismiss(callback)`: fires when chat closes.
|
|
624
|
-
- `SagepilotChat.onError(callback)`: subscribes to SDK errors.
|
|
625
|
-
- `SagepilotChat.onStateChange(callback)`: subscribes to local SDK state changes.
|
|
180
|
+
The base chat SDK does not bundle CameraX. The optional addon adds these Android dependencies through its own Gradle file:
|
|
626
181
|
|
|
627
|
-
|
|
182
|
+
- `androidx.camera:camera-core`
|
|
183
|
+
- `androidx.camera:camera-camera2`
|
|
184
|
+
- `androidx.camera:camera-lifecycle`
|
|
185
|
+
- `androidx.camera:camera-view`
|
|
186
|
+
- `androidx.exifinterface:exifinterface`
|
|
187
|
+
- `androidx.activity:activity`
|
|
628
188
|
|
|
629
|
-
|
|
189
|
+
By default, the addon uses CameraX `1.5.3`, ExifInterface `1.4.2`, and AndroidX Activity `1.10.1`. Apps can override them from the root Gradle project with `sagepilotCameraXVersion`, `sagepilotExifInterfaceVersion`, and `sagepilotActivityVersion`.
|
|
630
190
|
|
|
631
|
-
|
|
191
|
+
```ts
|
|
192
|
+
import { SagepilotChat } from "@sagepilot-ai/react-native-sdk";
|
|
193
|
+
import { createSagepilotCameraXFilePicker } from "@sagepilot-ai/react-native-camera-addon";
|
|
632
194
|
|
|
633
|
-
|
|
195
|
+
await SagepilotChat.configure({
|
|
196
|
+
key: "workspace_id:channel_id",
|
|
197
|
+
filePicker: createSagepilotCameraXFilePicker()
|
|
198
|
+
});
|
|
199
|
+
```
|
|
634
200
|
|
|
635
|
-
|
|
201
|
+
Run a fresh native Android build after installing the addon so React Native autolinking can register the native module.
|
|
636
202
|
|
|
637
|
-
|
|
203
|
+
More details: [Native file picker](https://docs.sagepilot.ai/sdks/react-native/native-file-picker).
|
|
638
204
|
|
|
639
|
-
|
|
205
|
+
## Method Return Behavior
|
|
640
206
|
|
|
641
|
-
|
|
207
|
+
| Method | Return behavior |
|
|
208
|
+
|---|---|
|
|
209
|
+
| `SagepilotChat.configure(config)` | `Promise<SagepilotMobileConfigureResult>` |
|
|
210
|
+
| `SagepilotChat.identify(identity)` | `Promise<SagepilotIdentifyResult>` |
|
|
211
|
+
| `SagepilotChat.logout()` | `Promise<SagepilotLogoutResponse>` |
|
|
212
|
+
| `SagepilotChat.getSession()` | `Promise<SagepilotSessionState>` |
|
|
213
|
+
| `SagepilotChat.getUnreadCount()` | `Promise<number>` |
|
|
214
|
+
| `SagepilotChat.presentMessageComposer(message?, options?)` | `boolean` or `Promise<boolean>` when `waitForIdentifyBeforeComposer` is enabled and identity is pending |
|
|
215
|
+
| `SagepilotChat.present()` | `boolean` |
|
|
216
|
+
| `SagepilotChat.presentMessages()` | `boolean` |
|
|
217
|
+
| `SagepilotChat.dismiss()` / `hide()` / `toggle()` | `boolean` |
|
|
218
|
+
| `SagepilotChat.isPresented()` | `boolean` |
|
|
219
|
+
| `SagepilotChat.getIdentityState()` / `getSessionState()` / `getChannel()` | synchronous state |
|
|
220
|
+
| `SagepilotChat.onIdentify(...)` / `onConversationCreated(...)` / lifecycle subscriptions | unsubscribe function |
|
|
221
|
+
| `SagepilotChat.destroy()` | `void` |
|
|
222
|
+
|
|
223
|
+
Full API reference: [SDK API reference](https://docs.sagepilot.ai/sdks/react-native/api-reference).
|
|
224
|
+
|
|
225
|
+
## Useful Links
|
|
226
|
+
|
|
227
|
+
- [React Native SDK overview](https://docs.sagepilot.ai/sdks/react-native/overview)
|
|
228
|
+
- [Installation](https://docs.sagepilot.ai/sdks/react-native/installation)
|
|
229
|
+
- [Configuration](https://docs.sagepilot.ai/sdks/react-native/configuration)
|
|
230
|
+
- [Opening chat](https://docs.sagepilot.ai/sdks/react-native/opening-chat)
|
|
231
|
+
- [Native file picker](https://docs.sagepilot.ai/sdks/react-native/native-file-picker)
|
|
232
|
+
- [SDK API reference](https://docs.sagepilot.ai/sdks/react-native/api-reference)
|
|
642
233
|
|
|
643
234
|
## License
|
|
644
235
|
|
|
645
|
-
MIT
|
|
646
|
-
|
|
647
|
-
The MIT License applies only to this SDK code. It does not grant access to Sagepilot AI services, workspaces, API credentials, hosted infrastructure, models, data, or paid features. Use of Sagepilot AI hosted services, APIs, Workspace IDs, and runtime-generated license keys is governed separately by Sagepilot AI's Terms of Service or the applicable customer agreement.
|
|
236
|
+
MIT
|
package/dist/index.d.mts
CHANGED
|
@@ -246,6 +246,7 @@ type SagepilotMobileBehaviorConfig = {
|
|
|
246
246
|
enableUnreadPolling?: boolean;
|
|
247
247
|
preloadWebView?: boolean;
|
|
248
248
|
unreadPollIntervalMs?: number;
|
|
249
|
+
waitForIdentifyBeforeComposer?: boolean;
|
|
249
250
|
};
|
|
250
251
|
type SagepilotTokenStorage = {
|
|
251
252
|
getItem: (key: string) => Promise<string | null>;
|
|
@@ -471,7 +472,7 @@ type SagepilotChatHookState = {
|
|
|
471
472
|
theme: SagepilotMobileThemeState;
|
|
472
473
|
present: () => boolean;
|
|
473
474
|
presentMessages: () => boolean;
|
|
474
|
-
presentMessageComposer: (message?: string, options?: SagepilotMessageComposerOptions) => boolean
|
|
475
|
+
presentMessageComposer: (message?: string, options?: SagepilotMessageComposerOptions) => boolean | Promise<boolean>;
|
|
475
476
|
dismiss: () => boolean;
|
|
476
477
|
hide: () => boolean;
|
|
477
478
|
toggle: () => boolean;
|
|
@@ -505,7 +506,7 @@ declare const SagepilotChat: {
|
|
|
505
506
|
stopUnreadPolling: () => void;
|
|
506
507
|
present: () => boolean;
|
|
507
508
|
presentMessages: () => boolean;
|
|
508
|
-
presentMessageComposer: (message?: string, options?: SagepilotMessageComposerOptions) => boolean
|
|
509
|
+
presentMessageComposer: (message?: string, options?: SagepilotMessageComposerOptions) => boolean | Promise<boolean>;
|
|
509
510
|
dismiss: () => boolean;
|
|
510
511
|
hide: () => boolean;
|
|
511
512
|
toggle: () => boolean;
|
package/dist/index.d.ts
CHANGED
|
@@ -246,6 +246,7 @@ type SagepilotMobileBehaviorConfig = {
|
|
|
246
246
|
enableUnreadPolling?: boolean;
|
|
247
247
|
preloadWebView?: boolean;
|
|
248
248
|
unreadPollIntervalMs?: number;
|
|
249
|
+
waitForIdentifyBeforeComposer?: boolean;
|
|
249
250
|
};
|
|
250
251
|
type SagepilotTokenStorage = {
|
|
251
252
|
getItem: (key: string) => Promise<string | null>;
|
|
@@ -471,7 +472,7 @@ type SagepilotChatHookState = {
|
|
|
471
472
|
theme: SagepilotMobileThemeState;
|
|
472
473
|
present: () => boolean;
|
|
473
474
|
presentMessages: () => boolean;
|
|
474
|
-
presentMessageComposer: (message?: string, options?: SagepilotMessageComposerOptions) => boolean
|
|
475
|
+
presentMessageComposer: (message?: string, options?: SagepilotMessageComposerOptions) => boolean | Promise<boolean>;
|
|
475
476
|
dismiss: () => boolean;
|
|
476
477
|
hide: () => boolean;
|
|
477
478
|
toggle: () => boolean;
|
|
@@ -505,7 +506,7 @@ declare const SagepilotChat: {
|
|
|
505
506
|
stopUnreadPolling: () => void;
|
|
506
507
|
present: () => boolean;
|
|
507
508
|
presentMessages: () => boolean;
|
|
508
|
-
presentMessageComposer: (message?: string, options?: SagepilotMessageComposerOptions) => boolean
|
|
509
|
+
presentMessageComposer: (message?: string, options?: SagepilotMessageComposerOptions) => boolean | Promise<boolean>;
|
|
509
510
|
dismiss: () => boolean;
|
|
510
511
|
hide: () => boolean;
|
|
511
512
|
toggle: () => boolean;
|
package/dist/index.js
CHANGED
|
@@ -67,7 +67,7 @@ var SagepilotChatError = class extends Error {
|
|
|
67
67
|
|
|
68
68
|
// src/core/config/constants.ts
|
|
69
69
|
var SDK_NAME = "@sagepilot-ai/react-native-sdk";
|
|
70
|
-
var SDK_VERSION = "0.3.
|
|
70
|
+
var SDK_VERSION = "0.3.2";
|
|
71
71
|
var DEFAULT_HOST = "https://app.sagepilot.ai";
|
|
72
72
|
var DEFAULT_WIDGET_HOST = "https://app.sagepilot.ai";
|
|
73
73
|
var CUSTOMER_API_PREFIX = "/customer-api/v1";
|
|
@@ -490,6 +490,7 @@ function fetchUnreadCount(http, host, channelId, sessionId, authorizationHeader)
|
|
|
490
490
|
var PRESENTATION_CACHE_NAMESPACE = "sagepilot_rn_presentation";
|
|
491
491
|
var PRESENTATION_CACHE_KEY = "state";
|
|
492
492
|
var PRESENTATION_RESTORE_MAX_AGE_MS = 15 * 60 * 1e3;
|
|
493
|
+
var IDENTIFY_PENDING_COMPOSER_TIMEOUT_MS = 60 * 1e3;
|
|
493
494
|
var DEFAULT_MOBILE_LAUNCHER_CONFIG = {
|
|
494
495
|
label: "Chat",
|
|
495
496
|
buttonColor: "#173c2d",
|
|
@@ -808,7 +809,16 @@ var SagepilotReactNativeChat = class {
|
|
|
808
809
|
return this.showHostedChat();
|
|
809
810
|
}
|
|
810
811
|
presentMessageComposer(message, options) {
|
|
811
|
-
this.
|
|
812
|
+
const session = this.requireSession();
|
|
813
|
+
const shouldWaitForIdentify = this.runtimeOptions?.behavior?.waitForIdentifyBeforeComposer === true;
|
|
814
|
+
if (!shouldWaitForIdentify || !this.identifyPending) return this.openMessageComposer(message, options);
|
|
815
|
+
return this.waitForIdentifyOrTimeout().then(() => {
|
|
816
|
+
if (!this.session || this.session.session_id !== session.session_id) return false;
|
|
817
|
+
return this.openMessageComposer(message, options);
|
|
818
|
+
});
|
|
819
|
+
}
|
|
820
|
+
/** Opens the hosted composer with the latest session identity state. */
|
|
821
|
+
openMessageComposer(message, options) {
|
|
812
822
|
const chatId = normalizeOptionalString(options?.chatId);
|
|
813
823
|
this.hostedChatView = {
|
|
814
824
|
screen: "composer",
|
|
@@ -820,6 +830,24 @@ var SagepilotReactNativeChat = class {
|
|
|
820
830
|
};
|
|
821
831
|
return this.showHostedChat();
|
|
822
832
|
}
|
|
833
|
+
/** Waits for a successful identify response before falling back to the timeout. */
|
|
834
|
+
waitForIdentifyOrTimeout(timeoutMs = IDENTIFY_PENDING_COMPOSER_TIMEOUT_MS) {
|
|
835
|
+
if (!this.identifyPending) return Promise.resolve();
|
|
836
|
+
return new Promise((resolve) => {
|
|
837
|
+
let completed = false;
|
|
838
|
+
let timeout = null;
|
|
839
|
+
const finish = () => {
|
|
840
|
+
if (completed) return;
|
|
841
|
+
completed = true;
|
|
842
|
+
if (timeout) clearTimeout(timeout);
|
|
843
|
+
this.identifyCallbacks.delete(handleIdentify);
|
|
844
|
+
resolve();
|
|
845
|
+
};
|
|
846
|
+
const handleIdentify = () => finish();
|
|
847
|
+
timeout = setTimeout(finish, timeoutMs);
|
|
848
|
+
this.identifyCallbacks.add(handleIdentify);
|
|
849
|
+
});
|
|
850
|
+
}
|
|
823
851
|
dismiss() {
|
|
824
852
|
this.requireConfigured();
|
|
825
853
|
if (!this.presented) return true;
|
package/dist/index.mjs
CHANGED
|
@@ -18,7 +18,7 @@ var SagepilotChatError = class extends Error {
|
|
|
18
18
|
|
|
19
19
|
// src/core/config/constants.ts
|
|
20
20
|
var SDK_NAME = "@sagepilot-ai/react-native-sdk";
|
|
21
|
-
var SDK_VERSION = "0.3.
|
|
21
|
+
var SDK_VERSION = "0.3.2";
|
|
22
22
|
var DEFAULT_HOST = "https://app.sagepilot.ai";
|
|
23
23
|
var DEFAULT_WIDGET_HOST = "https://app.sagepilot.ai";
|
|
24
24
|
var CUSTOMER_API_PREFIX = "/customer-api/v1";
|
|
@@ -441,6 +441,7 @@ function fetchUnreadCount(http, host, channelId, sessionId, authorizationHeader)
|
|
|
441
441
|
var PRESENTATION_CACHE_NAMESPACE = "sagepilot_rn_presentation";
|
|
442
442
|
var PRESENTATION_CACHE_KEY = "state";
|
|
443
443
|
var PRESENTATION_RESTORE_MAX_AGE_MS = 15 * 60 * 1e3;
|
|
444
|
+
var IDENTIFY_PENDING_COMPOSER_TIMEOUT_MS = 60 * 1e3;
|
|
444
445
|
var DEFAULT_MOBILE_LAUNCHER_CONFIG = {
|
|
445
446
|
label: "Chat",
|
|
446
447
|
buttonColor: "#173c2d",
|
|
@@ -759,7 +760,16 @@ var SagepilotReactNativeChat = class {
|
|
|
759
760
|
return this.showHostedChat();
|
|
760
761
|
}
|
|
761
762
|
presentMessageComposer(message, options) {
|
|
762
|
-
this.
|
|
763
|
+
const session = this.requireSession();
|
|
764
|
+
const shouldWaitForIdentify = this.runtimeOptions?.behavior?.waitForIdentifyBeforeComposer === true;
|
|
765
|
+
if (!shouldWaitForIdentify || !this.identifyPending) return this.openMessageComposer(message, options);
|
|
766
|
+
return this.waitForIdentifyOrTimeout().then(() => {
|
|
767
|
+
if (!this.session || this.session.session_id !== session.session_id) return false;
|
|
768
|
+
return this.openMessageComposer(message, options);
|
|
769
|
+
});
|
|
770
|
+
}
|
|
771
|
+
/** Opens the hosted composer with the latest session identity state. */
|
|
772
|
+
openMessageComposer(message, options) {
|
|
763
773
|
const chatId = normalizeOptionalString(options?.chatId);
|
|
764
774
|
this.hostedChatView = {
|
|
765
775
|
screen: "composer",
|
|
@@ -771,6 +781,24 @@ var SagepilotReactNativeChat = class {
|
|
|
771
781
|
};
|
|
772
782
|
return this.showHostedChat();
|
|
773
783
|
}
|
|
784
|
+
/** Waits for a successful identify response before falling back to the timeout. */
|
|
785
|
+
waitForIdentifyOrTimeout(timeoutMs = IDENTIFY_PENDING_COMPOSER_TIMEOUT_MS) {
|
|
786
|
+
if (!this.identifyPending) return Promise.resolve();
|
|
787
|
+
return new Promise((resolve) => {
|
|
788
|
+
let completed = false;
|
|
789
|
+
let timeout = null;
|
|
790
|
+
const finish = () => {
|
|
791
|
+
if (completed) return;
|
|
792
|
+
completed = true;
|
|
793
|
+
if (timeout) clearTimeout(timeout);
|
|
794
|
+
this.identifyCallbacks.delete(handleIdentify);
|
|
795
|
+
resolve();
|
|
796
|
+
};
|
|
797
|
+
const handleIdentify = () => finish();
|
|
798
|
+
timeout = setTimeout(finish, timeoutMs);
|
|
799
|
+
this.identifyCallbacks.add(handleIdentify);
|
|
800
|
+
});
|
|
801
|
+
}
|
|
774
802
|
dismiss() {
|
|
775
803
|
this.requireConfigured();
|
|
776
804
|
if (!this.presented) return true;
|