@sagepilot-ai/react-native-sdk 0.3.2 → 0.3.4

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 CHANGED
@@ -164,9 +164,12 @@ When the app user signs out, clear Sagepilot identity/session state:
164
164
 
165
165
  ```ts
166
166
  await SagepilotChat.logout();
167
+ // Optional: only call destroy() when your app is tearing down Sagepilot entirely.
167
168
  SagepilotChat.destroy();
168
169
  ```
169
170
 
171
+ `logout()` clears hosted WebView identity, removes the persisted customer session, and rotates the SDK onto a fresh anonymous session. The next signed-in user can call `identify()` without reusing the previous user's conversation context.
172
+
170
173
  More details: [Identity](https://docs.sagepilot.ai/sdks/react-native/identity).
171
174
 
172
175
  ## Attachments
@@ -8,13 +8,11 @@ import java.util.Collections;
8
8
  import java.util.List;
9
9
 
10
10
  public class SagepilotReactNativeSdkPackage implements ReactPackage {
11
- /** Registers SDK-owned native modules exposed to JavaScript. */
12
11
  @Override
13
12
  public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
14
13
  return Collections.emptyList();
15
14
  }
16
15
 
17
- /** Registers the existing SDK view managers without changing their surface. */
18
16
  @Override
19
17
  public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
20
18
  return Collections.singletonList(new SagepilotInsetsViewManager());
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.2";
70
+ var SDK_VERSION = "0.3.4";
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";
@@ -587,6 +587,7 @@ var SagepilotReactNativeChat = class {
587
587
  this.session = null;
588
588
  this.customer = null;
589
589
  this.legacyWidgetJwt = null;
590
+ this.hostedIdentityLogoutVersion = 0;
590
591
  this.lastIdentifyResult = null;
591
592
  this.identified = false;
592
593
  this.identifyPending = false;
@@ -619,10 +620,13 @@ var SagepilotReactNativeChat = class {
619
620
  }
620
621
  const { workspaceId, channelId } = parseKey(config.key);
621
622
  this.runtimeOptions = {
623
+ anonymousId: config.anonymousId,
622
624
  behavior: config.behavior,
623
625
  cacheStorage: config.cacheStorage,
626
+ deviceInfo: config.deviceInfo,
624
627
  filePicker: config.filePicker,
625
628
  fileStore: config.fileStore,
629
+ metadata: config.metadata,
626
630
  presentation: config.presentation,
627
631
  theme: config.theme,
628
632
  hostedClaimsStorageKeyPrefix: config.hostedClaimsStorageKeyPrefix || DEFAULT_HOSTED_CLAIMS_STORAGE_KEY_PREFIX
@@ -695,6 +699,12 @@ var SagepilotReactNativeChat = class {
695
699
  this.requireSessionManager().getAuthorizationHeader(session.session_token),
696
700
  apiIdentity
697
701
  );
702
+ if (!this.session || this.session.session_id !== session.session_id) {
703
+ throw new SagepilotChatError(
704
+ "session_expired",
705
+ `${SDK_NAME} identify() was superseded by a newer session.`
706
+ );
707
+ }
698
708
  const updatedSession = await this.requireSessionManager().setSession({
699
709
  ...session,
700
710
  session_token: result.session_token,
@@ -705,6 +715,7 @@ var SagepilotReactNativeChat = class {
705
715
  this.session = updatedSession;
706
716
  this.customer = result.customer;
707
717
  this.legacyWidgetJwt = result.legacy_jwt ?? null;
718
+ this.hostedIdentityLogoutVersion = 0;
708
719
  this.identified = true;
709
720
  this.identifyPending = false;
710
721
  const { session_token: _sessionToken, legacy_jwt: _legacyWidgetJwt, ...safeResult } = result;
@@ -725,24 +736,55 @@ var SagepilotReactNativeChat = class {
725
736
  }
726
737
  async logout() {
727
738
  const session = this.requireSession();
728
- const result = await logoutSession(
729
- this.requireHttp(),
730
- this.apiHost,
731
- session.session_id,
732
- this.requireSessionManager().getAuthorizationHeader(session.session_token)
733
- );
739
+ const wasUnreadPolling = this.unreadPollTimer !== null;
740
+ let result = null;
741
+ let logoutError = null;
742
+ this.stopUnreadPolling();
743
+ try {
744
+ result = await logoutSession(
745
+ this.requireHttp(),
746
+ this.apiHost,
747
+ session.session_id,
748
+ this.requireSessionManager().getAuthorizationHeader(session.session_token)
749
+ );
750
+ } catch (error) {
751
+ logoutError = error;
752
+ }
734
753
  this.identified = false;
754
+ this.identifyPending = false;
735
755
  this.customer = null;
736
756
  this.legacyWidgetJwt = null;
757
+ this.hostedIdentityLogoutVersion += 1;
737
758
  this.lastIdentifyResult = null;
738
- this.session = await this.requireSessionManager().setSession({
739
- ...session,
740
- customer_id: null,
741
- conversation_id: null,
742
- authenticated: false
743
- });
759
+ this.hostedChatView = { screen: "home" };
760
+ this.session = null;
761
+ await this.requireSessionManager().clearSession();
762
+ let freshSession;
763
+ try {
764
+ freshSession = await this.createCustomerSession(this.requireRuntimeOptions());
765
+ } catch (error) {
766
+ this.isConfigured = false;
767
+ this.presented = false;
768
+ this.setUnreadCount(0);
769
+ this.emitState();
770
+ this.emitError(error);
771
+ if (logoutError) {
772
+ this.emitError(logoutError);
773
+ throw logoutError;
774
+ }
775
+ throw error;
776
+ }
777
+ this.session = freshSession;
744
778
  this.setUnreadCount(0);
745
779
  this.emitState();
780
+ this.persistPresentationState();
781
+ if (wasUnreadPolling) {
782
+ this.startUnreadPolling(this.runtimeOptions?.behavior?.unreadPollIntervalMs);
783
+ }
784
+ if (logoutError) {
785
+ this.emitError(logoutError);
786
+ throw logoutError;
787
+ }
746
788
  return result;
747
789
  }
748
790
  getIdentityState() {
@@ -974,8 +1016,8 @@ var SagepilotReactNativeChat = class {
974
1016
  return preloadUrl;
975
1017
  }
976
1018
  getHostedAuthScript() {
977
- if (!this.legacyWidgetJwt || !this.workspaceId) return "";
978
- const storageKey = `${this.runtimeOptions?.hostedClaimsStorageKeyPrefix || DEFAULT_HOSTED_CLAIMS_STORAGE_KEY_PREFIX}_${this.workspaceId}`;
1019
+ const storageKey = this.getHostedClaimsStorageKey();
1020
+ if (!this.legacyWidgetJwt || !storageKey) return "";
979
1021
  return [
980
1022
  "(function(){",
981
1023
  "try {",
@@ -984,6 +1026,21 @@ var SagepilotReactNativeChat = class {
984
1026
  "})();"
985
1027
  ].join("\n");
986
1028
  }
1029
+ getHostedIdentityCleanupScript() {
1030
+ if (this.legacyWidgetJwt || this.hostedIdentityLogoutVersion === 0) return "";
1031
+ return this.buildHostedIdentityLogoutScript(false);
1032
+ }
1033
+ getHostedIdentityLogoutVersion() {
1034
+ return this.hostedIdentityLogoutVersion;
1035
+ }
1036
+ getHostedIdentityLogoutMessage() {
1037
+ if (this.legacyWidgetJwt || !this.workspaceId || this.hostedIdentityLogoutVersion === 0) return null;
1038
+ return { type: "identity_logout" };
1039
+ }
1040
+ getHostedIdentityLogoutScript() {
1041
+ if (!this.getHostedIdentityLogoutMessage()) return "";
1042
+ return this.buildHostedIdentityLogoutScript(true);
1043
+ }
987
1044
  getActiveHostedChatId() {
988
1045
  if (this.hostedChatView.screen === "composer" && this.hostedChatView.chatId) {
989
1046
  return this.hostedChatView.chatId;
@@ -1172,6 +1229,15 @@ var SagepilotReactNativeChat = class {
1172
1229
  }
1173
1230
  return this.sessionManager;
1174
1231
  }
1232
+ requireRuntimeOptions() {
1233
+ if (!this.runtimeOptions) {
1234
+ throw new SagepilotChatError(
1235
+ "not_initialized",
1236
+ `${SDK_NAME} is not configured. Call configure() first.`
1237
+ );
1238
+ }
1239
+ return this.runtimeOptions;
1240
+ }
1175
1241
  async resolveCustomerSession(config) {
1176
1242
  const sessionManager = this.requireSessionManager();
1177
1243
  const storedSession = await sessionManager.hydrate();
@@ -1183,7 +1249,7 @@ var SagepilotReactNativeChat = class {
1183
1249
  storedSession.session_id,
1184
1250
  sessionManager.getAuthorizationHeader(storedSession.session_token)
1185
1251
  );
1186
- const session2 = await sessionManager.setSession({
1252
+ const session = await sessionManager.setSession({
1187
1253
  ...storedSession,
1188
1254
  customer_id: liveSession.customer_id,
1189
1255
  conversation_id: liveSession.conversation_id,
@@ -1191,12 +1257,16 @@ var SagepilotReactNativeChat = class {
1191
1257
  });
1192
1258
  return {
1193
1259
  ...liveSession,
1194
- session_token: session2.session_token
1260
+ session_token: session.session_token
1195
1261
  };
1196
1262
  } catch {
1197
1263
  await sessionManager.clearSession();
1198
1264
  }
1199
1265
  }
1266
+ return this.createCustomerSession(config);
1267
+ }
1268
+ async createCustomerSession(config) {
1269
+ const sessionManager = this.requireSessionManager();
1200
1270
  const deviceInfo = await resolveDeviceInfo(config.deviceInfo);
1201
1271
  const session = await createSession(
1202
1272
  this.requireHttp(),
@@ -1216,6 +1286,32 @@ var SagepilotReactNativeChat = class {
1216
1286
  await sessionManager.setSession(session);
1217
1287
  return session;
1218
1288
  }
1289
+ getHostedClaimsStorageKey() {
1290
+ if (!this.workspaceId) return null;
1291
+ return `${this.runtimeOptions?.hostedClaimsStorageKeyPrefix || DEFAULT_HOSTED_CLAIMS_STORAGE_KEY_PREFIX}_${this.workspaceId}`;
1292
+ }
1293
+ buildHostedIdentityLogoutScript(dispatchLogout) {
1294
+ const storageKey = this.getHostedClaimsStorageKey();
1295
+ if (!storageKey || !this.workspaceId) return "";
1296
+ const legacyIdentityKey = `sg_identity_${this.workspaceId}`;
1297
+ const logoutMarkerKey = `sg_rn_identity_logout_${this.workspaceId}_${this.hostedIdentityLogoutVersion}`;
1298
+ return [
1299
+ "(function(){",
1300
+ "try {",
1301
+ `var storageKey = ${JSON.stringify(storageKey)};`,
1302
+ `var legacyIdentityKey = ${JSON.stringify(legacyIdentityKey)};`,
1303
+ `var logoutMarkerKey = ${JSON.stringify(logoutMarkerKey)};`,
1304
+ "if (!window.localStorage.getItem(logoutMarkerKey)) {",
1305
+ "window.localStorage.removeItem(storageKey);",
1306
+ "window.localStorage.removeItem(legacyIdentityKey);",
1307
+ "window.localStorage.setItem(logoutMarkerKey, '1');",
1308
+ "}",
1309
+ dispatchLogout ? "window.dispatchEvent(new MessageEvent('message', { data: { type: 'identity_logout' }, origin: window.location.origin, source: window.parent || window }));" : "",
1310
+ "} catch (_) {}",
1311
+ "true;",
1312
+ "})();"
1313
+ ].filter(Boolean).join("\n");
1314
+ }
1219
1315
  async fetchUnreadCount() {
1220
1316
  const session = this.requireSession();
1221
1317
  const response = await fetchUnreadCount(
@@ -1240,6 +1336,7 @@ var SagepilotReactNativeChat = class {
1240
1336
  this.session = null;
1241
1337
  this.customer = null;
1242
1338
  this.legacyWidgetJwt = null;
1339
+ this.hostedIdentityLogoutVersion = 0;
1243
1340
  this.lastIdentifyResult = null;
1244
1341
  this.identified = false;
1245
1342
  this.identifyPending = false;
@@ -1670,7 +1767,6 @@ function createSagepilotFilePicker(options) {
1670
1767
 
1671
1768
  // src/ui/SagepilotChatProvider.ts
1672
1769
  var import_react = require("react");
1673
- var import_lucide_react_native = require("lucide-react-native");
1674
1770
  var import_react_native3 = require("react-native");
1675
1771
  var import_react_native_webview = require("react-native-webview");
1676
1772
 
@@ -1845,18 +1941,22 @@ function readPresentationState() {
1845
1941
  conversationUrl: internalSagepilotChat.getState().configured ? internalSagepilotChat.getConversationUrl() : null,
1846
1942
  preloadUrl: internalSagepilotChat.getState().configured ? internalSagepilotChat.getPreloadUrl() : null,
1847
1943
  shouldPreload: internalSagepilotChat.getConfig()?.behavior?.preloadWebView ?? true,
1944
+ hostedIdentityLogoutVersion: internalSagepilotChat.getHostedIdentityLogoutVersion(),
1848
1945
  presentation: internalSagepilotChat.getConfig()?.presentation
1849
1946
  };
1850
1947
  }
1851
1948
  function getBridgeCapabilities() {
1949
+ const filePicker = internalSagepilotChat.getConfig()?.filePicker;
1852
1950
  return {
1853
- nativeFilePicker: Boolean(internalSagepilotChat.getConfig()?.filePicker)
1951
+ nativeFilePicker: Boolean(filePicker),
1952
+ nativeFilePickerSources: filePicker?.sources ?? []
1854
1953
  };
1855
1954
  }
1856
1955
  function getInjectedWebViewScript() {
1857
1956
  const capabilities = getBridgeCapabilities();
1858
1957
  return [
1859
1958
  internalSagepilotChat.getHostedAuthScript(),
1959
+ internalSagepilotChat.getHostedIdentityCleanupScript(),
1860
1960
  buildMobileWebViewBridgeScript(capabilities),
1861
1961
  buildBridgeCapabilitiesScript(capabilities)
1862
1962
  ].filter(Boolean).join("\n");
@@ -1866,21 +1966,13 @@ var FILE_PICKER_SOURCE_LABELS = {
1866
1966
  library: "Choose from gallery",
1867
1967
  documents: "Browse files"
1868
1968
  };
1869
- var FILE_PICKER_SOURCE_DESCRIPTIONS = {
1870
- camera: "Use the in-app camera",
1871
- library: "Photos and videos on this device",
1872
- documents: "PDFs, documents, and Drive files"
1873
- };
1874
- var FILE_PICKER_SOURCE_ICONS = {
1875
- camera: import_lucide_react_native.Camera,
1876
- library: import_lucide_react_native.Images,
1877
- documents: import_lucide_react_native.FileText
1878
- };
1879
- var FILE_PICKER_SOURCE_ICON_COLORS = {
1880
- camera: "#0284c7",
1881
- library: "#16a34a",
1882
- documents: "#7c3aed"
1883
- };
1969
+ var RN_FILE_PICKER_LOG_PREFIX = "[SagepilotRNFilePicker]";
1970
+ function logNativeFilePicker(event, details) {
1971
+ console.info(`${RN_FILE_PICKER_LOG_PREFIX} ${event}`, details ?? {});
1972
+ }
1973
+ function warnNativeFilePicker(event, details) {
1974
+ console.warn(`${RN_FILE_PICKER_LOG_PREFIX} ${event}`, details ?? {});
1975
+ }
1884
1976
  var DELIVERY_RETRY_MS = 1500;
1885
1977
  var DELIVERY_LEGACY_FALLBACK_ATTEMPTS = 2;
1886
1978
  var DELIVERY_MAX_ATTEMPTS = 8;
@@ -1904,29 +1996,6 @@ function totalBase64Bytes(files) {
1904
1996
  function storageKeyFor(batchId, index) {
1905
1997
  return `${batchId}_${index}.bin`;
1906
1998
  }
1907
- function renderFilePickerSourceIcon(source) {
1908
- const Icon = FILE_PICKER_SOURCE_ICONS[source];
1909
- return (0, import_react.createElement)(
1910
- import_react_native3.View,
1911
- { style: getFilePickerSourceIconStyle(source) },
1912
- (0, import_react.createElement)(Icon, {
1913
- color: FILE_PICKER_SOURCE_ICON_COLORS[source],
1914
- size: 23,
1915
- strokeWidth: 2.35
1916
- })
1917
- );
1918
- }
1919
- function getFilePickerSourceIconStyle(source) {
1920
- if (source === "camera") return [styles.sheetOptionIcon, styles.cameraOptionIcon];
1921
- if (source === "library") return [styles.sheetOptionIcon, styles.libraryOptionIcon];
1922
- return [styles.sheetOptionIcon, styles.documentsOptionIcon];
1923
- }
1924
- function getSheetOptionStyle(pressed) {
1925
- return [styles.sheetOption, pressed && styles.sheetOptionPressed];
1926
- }
1927
- function getSheetCancelStyle(pressed) {
1928
- return [styles.sheetCancelButton, pressed && styles.sheetCancelButtonPressed];
1929
- }
1930
1999
  function buildDispatchScript(messageLiteral) {
1931
2000
  return [
1932
2001
  "(function(){",
@@ -2177,35 +2246,70 @@ function SagepilotChatProvider({
2177
2246
  }, []);
2178
2247
  const runNativeFilePicker = (0, import_react.useCallback)((source, multiple) => {
2179
2248
  const filePicker = internalSagepilotChat.getConfig()?.filePicker;
2249
+ logNativeFilePicker("run_requested", {
2250
+ source,
2251
+ multiple,
2252
+ availableSources: filePicker?.sources ?? []
2253
+ });
2180
2254
  if (!filePicker) {
2255
+ warnNativeFilePicker("run_without_file_picker", { source });
2181
2256
  return;
2182
2257
  }
2183
2258
  if (pickerInFlightRef.current) {
2259
+ warnNativeFilePicker("run_ignored_picker_in_flight", { source });
2184
2260
  return;
2185
2261
  }
2186
- pauseLivenessAfterPicker(6e4);
2187
2262
  pickerInFlightRef.current = true;
2188
- filePicker.pickFiles({ source, multiple }).then((files) => {
2189
- if (files.length === 0) {
2190
- nativeWebViewRef.current?.injectJavaScript(buildFilePickerCancelledScript());
2191
- return;
2263
+ pauseLivenessAfterPicker(6e4);
2264
+ void (async () => {
2265
+ try {
2266
+ const files = await filePicker.pickFiles({ source, multiple });
2267
+ logNativeFilePicker("native_pick_resolved", { source, fileCount: files.length });
2268
+ if (files.length === 0) {
2269
+ nativeWebViewRef.current?.injectJavaScript(buildFilePickerCancelledScript());
2270
+ return;
2271
+ }
2272
+ queuePickedFiles(files);
2273
+ } catch (error) {
2274
+ const { message, code } = readFilePickerError(error);
2275
+ warnNativeFilePicker("native_pick_failed", { source, code, message });
2276
+ nativeWebViewRef.current?.injectJavaScript(buildFilePickerErrorScript(message, code));
2277
+ } finally {
2278
+ pickerInFlightRef.current = false;
2279
+ pauseLivenessAfterPicker(LIVENESS_PICKER_GRACE_MS);
2192
2280
  }
2193
- queuePickedFiles(files);
2194
- }).catch((error) => {
2195
- const { message, code } = readFilePickerError(error);
2196
- nativeWebViewRef.current?.injectJavaScript(buildFilePickerErrorScript(message, code));
2197
- }).finally(() => {
2198
- pickerInFlightRef.current = false;
2199
- pauseLivenessAfterPicker(LIVENESS_PICKER_GRACE_MS);
2200
- });
2281
+ })();
2201
2282
  }, [pauseLivenessAfterPicker, queuePickedFiles]);
2202
- const openNativeFilePicker = (0, import_react.useCallback)((multiple) => {
2283
+ const openNativeFilePicker = (0, import_react.useCallback)((multiple, source) => {
2203
2284
  const filePicker = internalSagepilotChat.getConfig()?.filePicker;
2285
+ logNativeFilePicker("open_requested", {
2286
+ source: source ?? "source_chooser",
2287
+ multiple,
2288
+ hasFilePicker: Boolean(filePicker),
2289
+ availableSources: filePicker?.sources ?? []
2290
+ });
2204
2291
  if (!filePicker || filePicker.sources.length === 0) {
2292
+ warnNativeFilePicker("open_unavailable", { source: source ?? "source_chooser" });
2293
+ nativeWebViewRef.current?.injectJavaScript(buildFilePickerErrorScript(
2294
+ source === "camera" ? "CameraX is not available in this Expo runtime. Use a dev build with the CameraX addon linked." : "Native attachment picker is not available in this build.",
2295
+ "camera_unavailable"
2296
+ ));
2297
+ return;
2298
+ }
2299
+ if (source) {
2300
+ if (filePicker.sources.includes(source)) {
2301
+ runNativeFilePicker(source, multiple);
2302
+ } else {
2303
+ warnNativeFilePicker("source_not_available", { source, availableSources: filePicker.sources });
2304
+ nativeWebViewRef.current?.injectJavaScript(buildFilePickerErrorScript(
2305
+ source === "camera" ? "CameraX is not available in this build." : `Native ${source} picker is not available in this build.`,
2306
+ "camera_unavailable"
2307
+ ));
2308
+ }
2205
2309
  return;
2206
2310
  }
2207
2311
  const [onlySource] = filePicker.sources;
2208
- if (filePicker.sources.length === 1 && onlySource && onlySource !== "camera") {
2312
+ if (filePicker.sources.length === 1 && onlySource) {
2209
2313
  runNativeFilePicker(onlySource, multiple);
2210
2314
  return;
2211
2315
  }
@@ -2385,7 +2489,11 @@ function SagepilotChatProvider({
2385
2489
  const handleWebViewMessage = (event) => {
2386
2490
  const message = parseHostedBridgeMessage(event.nativeEvent?.data);
2387
2491
  if (message?.type === "sagepilot:open_file_picker") {
2388
- openNativeFilePicker(message.multiple ?? true);
2492
+ logNativeFilePicker("bridge_message_open_file_picker", {
2493
+ source: message.source ?? "source_chooser",
2494
+ multiple: message.multiple ?? true
2495
+ });
2496
+ openNativeFilePicker(message.multiple ?? true, message.source);
2389
2497
  return;
2390
2498
  }
2391
2499
  if (message?.type === "sagepilot:widget_listener_ready") {
@@ -2425,17 +2533,29 @@ function SagepilotChatProvider({
2425
2533
  if (!message || !targetOrigin) return;
2426
2534
  webFrameRef.current?.contentWindow?.postMessage(message, targetOrigin);
2427
2535
  };
2536
+ const postIdentityLogoutToWebFrame = () => {
2537
+ const message = internalSagepilotChat.getHostedIdentityLogoutMessage();
2538
+ const targetOrigin = readUrlOrigin(state.conversationUrl);
2539
+ if (!message || !targetOrigin) return;
2540
+ webFrameRef.current?.contentWindow?.postMessage(message, targetOrigin);
2541
+ };
2428
2542
  const postIdentityToNativeWebView = () => {
2429
2543
  const script = getHostedIdentityDispatchScript();
2430
2544
  if (!script || !nativeWebViewRef.current) return;
2431
2545
  nativeWebViewRef.current.injectJavaScript(script);
2432
2546
  };
2547
+ const postIdentityLogoutToNativeWebView = () => {
2548
+ const script = internalSagepilotChat.getHostedIdentityLogoutScript();
2549
+ if (!script || !nativeWebViewRef.current) return;
2550
+ nativeWebViewRef.current.injectJavaScript(script);
2551
+ };
2433
2552
  const injectNativeBridgeToNativeWebView = () => {
2434
2553
  if (!nativeWebViewRef.current) return;
2435
2554
  nativeWebViewRef.current.injectJavaScript(getInjectedWebViewScript());
2436
2555
  };
2437
2556
  const handleNativeWebViewLoadEnd = () => {
2438
2557
  injectNativeBridgeToNativeWebView();
2558
+ postIdentityLogoutToNativeWebView();
2439
2559
  postIdentityToNativeWebView();
2440
2560
  widgetReadyRef.current = false;
2441
2561
  startDelivery();
@@ -2467,10 +2587,22 @@ function SagepilotChatProvider({
2467
2587
  if (import_react_native3.Platform.OS !== "web" || !state.isPresented) return;
2468
2588
  postIdentityToWebFrame();
2469
2589
  }, [state.isPresented, state.conversationUrl, state.configured]);
2590
+ (0, import_react.useEffect)(() => {
2591
+ if (import_react_native3.Platform.OS !== "web" || !state.isPresented) return;
2592
+ postIdentityLogoutToWebFrame();
2593
+ }, [state.hostedIdentityLogoutVersion, state.isPresented, state.conversationUrl, state.configured]);
2470
2594
  (0, import_react.useEffect)(() => {
2471
2595
  if (import_react_native3.Platform.OS === "web" || !state.isPresented) return;
2472
2596
  postIdentityToNativeWebView();
2473
2597
  }, [state.isPresented, state.conversationUrl, state.configured]);
2598
+ (0, import_react.useEffect)(() => {
2599
+ if (import_react_native3.Platform.OS === "web" || !state.isPresented) return;
2600
+ postIdentityLogoutToNativeWebView();
2601
+ }, [state.hostedIdentityLogoutVersion, state.isPresented, state.conversationUrl, state.configured]);
2602
+ const handleWebFrameLoad = () => {
2603
+ postIdentityLogoutToWebFrame();
2604
+ postIdentityToWebFrame();
2605
+ };
2474
2606
  if (import_react_native3.Platform.OS === "web") {
2475
2607
  return (0, import_react.createElement)(
2476
2608
  import_react_native3.View,
@@ -2502,7 +2634,7 @@ function SagepilotChatProvider({
2502
2634
  src: state.conversationUrl,
2503
2635
  style: styles.webFrame,
2504
2636
  title: "Sagepilot chat",
2505
- onLoad: postIdentityToWebFrame
2637
+ onLoad: handleWebFrameLoad
2506
2638
  })
2507
2639
  )
2508
2640
  ) : null
@@ -2603,7 +2735,6 @@ function SagepilotChatProvider({
2603
2735
  (0, import_react.createElement)(
2604
2736
  import_react_native3.View,
2605
2737
  { style: styles.sheetCard },
2606
- (0, import_react.createElement)(import_react_native3.View, { style: styles.sheetHandle }),
2607
2738
  (0, import_react.createElement)(import_react_native3.Text, { style: styles.sheetTitle }, "Add attachment"),
2608
2739
  ...(internalSagepilotChat.getConfig()?.filePicker?.sources ?? []).map(
2609
2740
  (source) => (0, import_react.createElement)(
@@ -2611,29 +2742,19 @@ function SagepilotChatProvider({
2611
2742
  {
2612
2743
  key: source,
2613
2744
  accessibilityRole: "button",
2614
- android_ripple: { color: "rgba(15, 23, 42, 0.06)" },
2615
- hitSlop: 4,
2616
- style: ({ pressed }) => getSheetOptionStyle(pressed),
2745
+ style: styles.sheetButton,
2617
2746
  onPress: () => handleSourceChoice(source)
2618
2747
  },
2619
- renderFilePickerSourceIcon(source),
2620
- (0, import_react.createElement)(
2621
- import_react_native3.View,
2622
- { style: styles.sheetOptionText },
2623
- (0, import_react.createElement)(import_react_native3.Text, { style: styles.sheetButtonText }, FILE_PICKER_SOURCE_LABELS[source]),
2624
- (0, import_react.createElement)(import_react_native3.Text, { style: styles.sheetButtonDescription }, FILE_PICKER_SOURCE_DESCRIPTIONS[source])
2625
- )
2748
+ (0, import_react.createElement)(import_react_native3.Text, { style: styles.sheetButtonText }, FILE_PICKER_SOURCE_LABELS[source])
2626
2749
  )
2627
2750
  ),
2628
2751
  (0, import_react.createElement)(
2629
2752
  import_react_native3.Pressable,
2630
2753
  {
2631
2754
  accessibilityRole: "button",
2632
- android_ripple: { color: "rgba(15, 23, 42, 0.06)" },
2633
- style: ({ pressed }) => getSheetCancelStyle(pressed),
2755
+ style: [styles.sheetButton, styles.sheetCancelButton],
2634
2756
  onPress: () => setSourceChooser(null)
2635
2757
  },
2636
- (0, import_react.createElement)(import_lucide_react_native.X, { color: "#334155", size: 18, strokeWidth: 2.5, style: styles.sheetCancelIcon }),
2637
2758
  (0, import_react.createElement)(import_react_native3.Text, { style: styles.sheetCancelText }, "Cancel")
2638
2759
  )
2639
2760
  )
@@ -2739,109 +2860,43 @@ var styles = import_react_native3.StyleSheet.create({
2739
2860
  },
2740
2861
  sheetBackdrop: {
2741
2862
  flex: 1,
2742
- alignItems: "center",
2743
2863
  justifyContent: "flex-end",
2744
- backgroundColor: "rgba(15, 23, 42, 0.48)"
2864
+ backgroundColor: "rgba(17, 24, 39, 0.36)"
2745
2865
  },
2746
2866
  sheetCard: {
2747
- width: "100%",
2748
- maxWidth: 540,
2749
2867
  backgroundColor: "#ffffff",
2750
- borderTopLeftRadius: 26,
2751
- borderTopRightRadius: 26,
2752
- paddingTop: 10,
2753
- paddingBottom: 18,
2754
- paddingHorizontal: 14,
2755
- shadowColor: "#0f172a",
2756
- shadowOpacity: 0.22,
2757
- shadowRadius: 28,
2758
- shadowOffset: { width: 0, height: -10 },
2759
- elevation: 18
2760
- },
2761
- sheetHandle: {
2762
- alignSelf: "center",
2763
- width: 42,
2764
- height: 5,
2765
- borderRadius: 999,
2766
- backgroundColor: "#d1d5db",
2767
- marginBottom: 14
2868
+ borderTopLeftRadius: 16,
2869
+ borderTopRightRadius: 16,
2870
+ paddingTop: 8,
2871
+ paddingBottom: 24,
2872
+ paddingHorizontal: 8
2768
2873
  },
2769
2874
  sheetTitle: {
2770
- color: "#111827",
2771
- fontSize: 17,
2772
- fontWeight: "700",
2773
- paddingBottom: 12,
2774
- paddingHorizontal: 4
2775
- },
2776
- sheetOption: {
2777
- minHeight: 70,
2778
- flexDirection: "row",
2779
- alignItems: "center",
2780
- borderRadius: 18,
2781
- paddingHorizontal: 12,
2782
- marginBottom: 6,
2783
- backgroundColor: "#ffffff"
2784
- },
2785
- sheetOptionPressed: {
2786
- backgroundColor: "#f8fafc",
2787
- transform: [{ scale: 0.985 }]
2875
+ textAlign: "center",
2876
+ color: "#6b7280",
2877
+ fontSize: 13,
2878
+ fontWeight: "600",
2879
+ paddingVertical: 10
2788
2880
  },
2789
- sheetOptionIcon: {
2790
- width: 46,
2791
- height: 46,
2792
- borderRadius: 16,
2793
- marginRight: 14,
2881
+ sheetButton: {
2882
+ minHeight: 52,
2794
2883
  alignItems: "center",
2795
2884
  justifyContent: "center",
2796
- position: "relative",
2797
- overflow: "hidden"
2798
- },
2799
- sheetOptionText: {
2800
- flex: 1,
2801
- justifyContent: "center"
2885
+ borderRadius: 12
2802
2886
  },
2803
2887
  sheetButtonText: {
2804
- color: "#0f172a",
2888
+ color: "#111827",
2805
2889
  fontSize: 16,
2806
- fontWeight: "700"
2807
- },
2808
- sheetButtonDescription: {
2809
- color: "#64748b",
2810
- fontSize: 13,
2811
- fontWeight: "500",
2812
- marginTop: 3
2813
- },
2814
- cameraOptionIcon: {
2815
- backgroundColor: "#e0f2fe"
2816
- },
2817
- libraryOptionIcon: {
2818
- backgroundColor: "#ecfdf5"
2819
- },
2820
- documentsOptionIcon: {
2821
- backgroundColor: "#f5f3ff"
2890
+ fontWeight: "500"
2822
2891
  },
2823
2892
  sheetCancelButton: {
2824
- minHeight: 56,
2825
- flexDirection: "row",
2826
- alignItems: "center",
2827
- justifyContent: "center",
2828
- borderRadius: 18,
2829
- marginTop: 8,
2830
- backgroundColor: "#f1f5f9"
2831
- },
2832
- sheetCancelButtonPressed: {
2833
- backgroundColor: "#e2e8f0",
2834
- transform: [{ scale: 0.985 }]
2835
- },
2836
- sheetCancelIcon: {
2837
- width: 18,
2838
- height: 18,
2839
- marginRight: 8
2893
+ marginTop: 6,
2894
+ backgroundColor: "#f3f4f6"
2840
2895
  },
2841
2896
  sheetCancelText: {
2842
- color: "#0f172a",
2897
+ color: "#111827",
2843
2898
  fontSize: 16,
2844
- fontWeight: "700"
2899
+ fontWeight: "600"
2845
2900
  }
2846
2901
  });
2847
2902
 
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.2";
21
+ var SDK_VERSION = "0.3.4";
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";
@@ -538,6 +538,7 @@ var SagepilotReactNativeChat = class {
538
538
  this.session = null;
539
539
  this.customer = null;
540
540
  this.legacyWidgetJwt = null;
541
+ this.hostedIdentityLogoutVersion = 0;
541
542
  this.lastIdentifyResult = null;
542
543
  this.identified = false;
543
544
  this.identifyPending = false;
@@ -570,10 +571,13 @@ var SagepilotReactNativeChat = class {
570
571
  }
571
572
  const { workspaceId, channelId } = parseKey(config.key);
572
573
  this.runtimeOptions = {
574
+ anonymousId: config.anonymousId,
573
575
  behavior: config.behavior,
574
576
  cacheStorage: config.cacheStorage,
577
+ deviceInfo: config.deviceInfo,
575
578
  filePicker: config.filePicker,
576
579
  fileStore: config.fileStore,
580
+ metadata: config.metadata,
577
581
  presentation: config.presentation,
578
582
  theme: config.theme,
579
583
  hostedClaimsStorageKeyPrefix: config.hostedClaimsStorageKeyPrefix || DEFAULT_HOSTED_CLAIMS_STORAGE_KEY_PREFIX
@@ -646,6 +650,12 @@ var SagepilotReactNativeChat = class {
646
650
  this.requireSessionManager().getAuthorizationHeader(session.session_token),
647
651
  apiIdentity
648
652
  );
653
+ if (!this.session || this.session.session_id !== session.session_id) {
654
+ throw new SagepilotChatError(
655
+ "session_expired",
656
+ `${SDK_NAME} identify() was superseded by a newer session.`
657
+ );
658
+ }
649
659
  const updatedSession = await this.requireSessionManager().setSession({
650
660
  ...session,
651
661
  session_token: result.session_token,
@@ -656,6 +666,7 @@ var SagepilotReactNativeChat = class {
656
666
  this.session = updatedSession;
657
667
  this.customer = result.customer;
658
668
  this.legacyWidgetJwt = result.legacy_jwt ?? null;
669
+ this.hostedIdentityLogoutVersion = 0;
659
670
  this.identified = true;
660
671
  this.identifyPending = false;
661
672
  const { session_token: _sessionToken, legacy_jwt: _legacyWidgetJwt, ...safeResult } = result;
@@ -676,24 +687,55 @@ var SagepilotReactNativeChat = class {
676
687
  }
677
688
  async logout() {
678
689
  const session = this.requireSession();
679
- const result = await logoutSession(
680
- this.requireHttp(),
681
- this.apiHost,
682
- session.session_id,
683
- this.requireSessionManager().getAuthorizationHeader(session.session_token)
684
- );
690
+ const wasUnreadPolling = this.unreadPollTimer !== null;
691
+ let result = null;
692
+ let logoutError = null;
693
+ this.stopUnreadPolling();
694
+ try {
695
+ result = await logoutSession(
696
+ this.requireHttp(),
697
+ this.apiHost,
698
+ session.session_id,
699
+ this.requireSessionManager().getAuthorizationHeader(session.session_token)
700
+ );
701
+ } catch (error) {
702
+ logoutError = error;
703
+ }
685
704
  this.identified = false;
705
+ this.identifyPending = false;
686
706
  this.customer = null;
687
707
  this.legacyWidgetJwt = null;
708
+ this.hostedIdentityLogoutVersion += 1;
688
709
  this.lastIdentifyResult = null;
689
- this.session = await this.requireSessionManager().setSession({
690
- ...session,
691
- customer_id: null,
692
- conversation_id: null,
693
- authenticated: false
694
- });
710
+ this.hostedChatView = { screen: "home" };
711
+ this.session = null;
712
+ await this.requireSessionManager().clearSession();
713
+ let freshSession;
714
+ try {
715
+ freshSession = await this.createCustomerSession(this.requireRuntimeOptions());
716
+ } catch (error) {
717
+ this.isConfigured = false;
718
+ this.presented = false;
719
+ this.setUnreadCount(0);
720
+ this.emitState();
721
+ this.emitError(error);
722
+ if (logoutError) {
723
+ this.emitError(logoutError);
724
+ throw logoutError;
725
+ }
726
+ throw error;
727
+ }
728
+ this.session = freshSession;
695
729
  this.setUnreadCount(0);
696
730
  this.emitState();
731
+ this.persistPresentationState();
732
+ if (wasUnreadPolling) {
733
+ this.startUnreadPolling(this.runtimeOptions?.behavior?.unreadPollIntervalMs);
734
+ }
735
+ if (logoutError) {
736
+ this.emitError(logoutError);
737
+ throw logoutError;
738
+ }
697
739
  return result;
698
740
  }
699
741
  getIdentityState() {
@@ -925,8 +967,8 @@ var SagepilotReactNativeChat = class {
925
967
  return preloadUrl;
926
968
  }
927
969
  getHostedAuthScript() {
928
- if (!this.legacyWidgetJwt || !this.workspaceId) return "";
929
- const storageKey = `${this.runtimeOptions?.hostedClaimsStorageKeyPrefix || DEFAULT_HOSTED_CLAIMS_STORAGE_KEY_PREFIX}_${this.workspaceId}`;
970
+ const storageKey = this.getHostedClaimsStorageKey();
971
+ if (!this.legacyWidgetJwt || !storageKey) return "";
930
972
  return [
931
973
  "(function(){",
932
974
  "try {",
@@ -935,6 +977,21 @@ var SagepilotReactNativeChat = class {
935
977
  "})();"
936
978
  ].join("\n");
937
979
  }
980
+ getHostedIdentityCleanupScript() {
981
+ if (this.legacyWidgetJwt || this.hostedIdentityLogoutVersion === 0) return "";
982
+ return this.buildHostedIdentityLogoutScript(false);
983
+ }
984
+ getHostedIdentityLogoutVersion() {
985
+ return this.hostedIdentityLogoutVersion;
986
+ }
987
+ getHostedIdentityLogoutMessage() {
988
+ if (this.legacyWidgetJwt || !this.workspaceId || this.hostedIdentityLogoutVersion === 0) return null;
989
+ return { type: "identity_logout" };
990
+ }
991
+ getHostedIdentityLogoutScript() {
992
+ if (!this.getHostedIdentityLogoutMessage()) return "";
993
+ return this.buildHostedIdentityLogoutScript(true);
994
+ }
938
995
  getActiveHostedChatId() {
939
996
  if (this.hostedChatView.screen === "composer" && this.hostedChatView.chatId) {
940
997
  return this.hostedChatView.chatId;
@@ -1123,6 +1180,15 @@ var SagepilotReactNativeChat = class {
1123
1180
  }
1124
1181
  return this.sessionManager;
1125
1182
  }
1183
+ requireRuntimeOptions() {
1184
+ if (!this.runtimeOptions) {
1185
+ throw new SagepilotChatError(
1186
+ "not_initialized",
1187
+ `${SDK_NAME} is not configured. Call configure() first.`
1188
+ );
1189
+ }
1190
+ return this.runtimeOptions;
1191
+ }
1126
1192
  async resolveCustomerSession(config) {
1127
1193
  const sessionManager = this.requireSessionManager();
1128
1194
  const storedSession = await sessionManager.hydrate();
@@ -1134,7 +1200,7 @@ var SagepilotReactNativeChat = class {
1134
1200
  storedSession.session_id,
1135
1201
  sessionManager.getAuthorizationHeader(storedSession.session_token)
1136
1202
  );
1137
- const session2 = await sessionManager.setSession({
1203
+ const session = await sessionManager.setSession({
1138
1204
  ...storedSession,
1139
1205
  customer_id: liveSession.customer_id,
1140
1206
  conversation_id: liveSession.conversation_id,
@@ -1142,12 +1208,16 @@ var SagepilotReactNativeChat = class {
1142
1208
  });
1143
1209
  return {
1144
1210
  ...liveSession,
1145
- session_token: session2.session_token
1211
+ session_token: session.session_token
1146
1212
  };
1147
1213
  } catch {
1148
1214
  await sessionManager.clearSession();
1149
1215
  }
1150
1216
  }
1217
+ return this.createCustomerSession(config);
1218
+ }
1219
+ async createCustomerSession(config) {
1220
+ const sessionManager = this.requireSessionManager();
1151
1221
  const deviceInfo = await resolveDeviceInfo(config.deviceInfo);
1152
1222
  const session = await createSession(
1153
1223
  this.requireHttp(),
@@ -1167,6 +1237,32 @@ var SagepilotReactNativeChat = class {
1167
1237
  await sessionManager.setSession(session);
1168
1238
  return session;
1169
1239
  }
1240
+ getHostedClaimsStorageKey() {
1241
+ if (!this.workspaceId) return null;
1242
+ return `${this.runtimeOptions?.hostedClaimsStorageKeyPrefix || DEFAULT_HOSTED_CLAIMS_STORAGE_KEY_PREFIX}_${this.workspaceId}`;
1243
+ }
1244
+ buildHostedIdentityLogoutScript(dispatchLogout) {
1245
+ const storageKey = this.getHostedClaimsStorageKey();
1246
+ if (!storageKey || !this.workspaceId) return "";
1247
+ const legacyIdentityKey = `sg_identity_${this.workspaceId}`;
1248
+ const logoutMarkerKey = `sg_rn_identity_logout_${this.workspaceId}_${this.hostedIdentityLogoutVersion}`;
1249
+ return [
1250
+ "(function(){",
1251
+ "try {",
1252
+ `var storageKey = ${JSON.stringify(storageKey)};`,
1253
+ `var legacyIdentityKey = ${JSON.stringify(legacyIdentityKey)};`,
1254
+ `var logoutMarkerKey = ${JSON.stringify(logoutMarkerKey)};`,
1255
+ "if (!window.localStorage.getItem(logoutMarkerKey)) {",
1256
+ "window.localStorage.removeItem(storageKey);",
1257
+ "window.localStorage.removeItem(legacyIdentityKey);",
1258
+ "window.localStorage.setItem(logoutMarkerKey, '1');",
1259
+ "}",
1260
+ dispatchLogout ? "window.dispatchEvent(new MessageEvent('message', { data: { type: 'identity_logout' }, origin: window.location.origin, source: window.parent || window }));" : "",
1261
+ "} catch (_) {}",
1262
+ "true;",
1263
+ "})();"
1264
+ ].filter(Boolean).join("\n");
1265
+ }
1170
1266
  async fetchUnreadCount() {
1171
1267
  const session = this.requireSession();
1172
1268
  const response = await fetchUnreadCount(
@@ -1191,6 +1287,7 @@ var SagepilotReactNativeChat = class {
1191
1287
  this.session = null;
1192
1288
  this.customer = null;
1193
1289
  this.legacyWidgetJwt = null;
1290
+ this.hostedIdentityLogoutVersion = 0;
1194
1291
  this.lastIdentifyResult = null;
1195
1292
  this.identified = false;
1196
1293
  this.identifyPending = false;
@@ -1621,7 +1718,6 @@ function createSagepilotFilePicker(options) {
1621
1718
 
1622
1719
  // src/ui/SagepilotChatProvider.ts
1623
1720
  import { createElement, useCallback, useEffect, useRef, useState } from "react";
1624
- import { Camera, FileText, Images, X } from "lucide-react-native";
1625
1721
  import {
1626
1722
  ActivityIndicator,
1627
1723
  AppState,
@@ -1808,18 +1904,22 @@ function readPresentationState() {
1808
1904
  conversationUrl: internalSagepilotChat.getState().configured ? internalSagepilotChat.getConversationUrl() : null,
1809
1905
  preloadUrl: internalSagepilotChat.getState().configured ? internalSagepilotChat.getPreloadUrl() : null,
1810
1906
  shouldPreload: internalSagepilotChat.getConfig()?.behavior?.preloadWebView ?? true,
1907
+ hostedIdentityLogoutVersion: internalSagepilotChat.getHostedIdentityLogoutVersion(),
1811
1908
  presentation: internalSagepilotChat.getConfig()?.presentation
1812
1909
  };
1813
1910
  }
1814
1911
  function getBridgeCapabilities() {
1912
+ const filePicker = internalSagepilotChat.getConfig()?.filePicker;
1815
1913
  return {
1816
- nativeFilePicker: Boolean(internalSagepilotChat.getConfig()?.filePicker)
1914
+ nativeFilePicker: Boolean(filePicker),
1915
+ nativeFilePickerSources: filePicker?.sources ?? []
1817
1916
  };
1818
1917
  }
1819
1918
  function getInjectedWebViewScript() {
1820
1919
  const capabilities = getBridgeCapabilities();
1821
1920
  return [
1822
1921
  internalSagepilotChat.getHostedAuthScript(),
1922
+ internalSagepilotChat.getHostedIdentityCleanupScript(),
1823
1923
  buildMobileWebViewBridgeScript(capabilities),
1824
1924
  buildBridgeCapabilitiesScript(capabilities)
1825
1925
  ].filter(Boolean).join("\n");
@@ -1829,21 +1929,13 @@ var FILE_PICKER_SOURCE_LABELS = {
1829
1929
  library: "Choose from gallery",
1830
1930
  documents: "Browse files"
1831
1931
  };
1832
- var FILE_PICKER_SOURCE_DESCRIPTIONS = {
1833
- camera: "Use the in-app camera",
1834
- library: "Photos and videos on this device",
1835
- documents: "PDFs, documents, and Drive files"
1836
- };
1837
- var FILE_PICKER_SOURCE_ICONS = {
1838
- camera: Camera,
1839
- library: Images,
1840
- documents: FileText
1841
- };
1842
- var FILE_PICKER_SOURCE_ICON_COLORS = {
1843
- camera: "#0284c7",
1844
- library: "#16a34a",
1845
- documents: "#7c3aed"
1846
- };
1932
+ var RN_FILE_PICKER_LOG_PREFIX = "[SagepilotRNFilePicker]";
1933
+ function logNativeFilePicker(event, details) {
1934
+ console.info(`${RN_FILE_PICKER_LOG_PREFIX} ${event}`, details ?? {});
1935
+ }
1936
+ function warnNativeFilePicker(event, details) {
1937
+ console.warn(`${RN_FILE_PICKER_LOG_PREFIX} ${event}`, details ?? {});
1938
+ }
1847
1939
  var DELIVERY_RETRY_MS = 1500;
1848
1940
  var DELIVERY_LEGACY_FALLBACK_ATTEMPTS = 2;
1849
1941
  var DELIVERY_MAX_ATTEMPTS = 8;
@@ -1867,29 +1959,6 @@ function totalBase64Bytes(files) {
1867
1959
  function storageKeyFor(batchId, index) {
1868
1960
  return `${batchId}_${index}.bin`;
1869
1961
  }
1870
- function renderFilePickerSourceIcon(source) {
1871
- const Icon = FILE_PICKER_SOURCE_ICONS[source];
1872
- return createElement(
1873
- View,
1874
- { style: getFilePickerSourceIconStyle(source) },
1875
- createElement(Icon, {
1876
- color: FILE_PICKER_SOURCE_ICON_COLORS[source],
1877
- size: 23,
1878
- strokeWidth: 2.35
1879
- })
1880
- );
1881
- }
1882
- function getFilePickerSourceIconStyle(source) {
1883
- if (source === "camera") return [styles.sheetOptionIcon, styles.cameraOptionIcon];
1884
- if (source === "library") return [styles.sheetOptionIcon, styles.libraryOptionIcon];
1885
- return [styles.sheetOptionIcon, styles.documentsOptionIcon];
1886
- }
1887
- function getSheetOptionStyle(pressed) {
1888
- return [styles.sheetOption, pressed && styles.sheetOptionPressed];
1889
- }
1890
- function getSheetCancelStyle(pressed) {
1891
- return [styles.sheetCancelButton, pressed && styles.sheetCancelButtonPressed];
1892
- }
1893
1962
  function buildDispatchScript(messageLiteral) {
1894
1963
  return [
1895
1964
  "(function(){",
@@ -2140,35 +2209,70 @@ function SagepilotChatProvider({
2140
2209
  }, []);
2141
2210
  const runNativeFilePicker = useCallback((source, multiple) => {
2142
2211
  const filePicker = internalSagepilotChat.getConfig()?.filePicker;
2212
+ logNativeFilePicker("run_requested", {
2213
+ source,
2214
+ multiple,
2215
+ availableSources: filePicker?.sources ?? []
2216
+ });
2143
2217
  if (!filePicker) {
2218
+ warnNativeFilePicker("run_without_file_picker", { source });
2144
2219
  return;
2145
2220
  }
2146
2221
  if (pickerInFlightRef.current) {
2222
+ warnNativeFilePicker("run_ignored_picker_in_flight", { source });
2147
2223
  return;
2148
2224
  }
2149
- pauseLivenessAfterPicker(6e4);
2150
2225
  pickerInFlightRef.current = true;
2151
- filePicker.pickFiles({ source, multiple }).then((files) => {
2152
- if (files.length === 0) {
2153
- nativeWebViewRef.current?.injectJavaScript(buildFilePickerCancelledScript());
2154
- return;
2226
+ pauseLivenessAfterPicker(6e4);
2227
+ void (async () => {
2228
+ try {
2229
+ const files = await filePicker.pickFiles({ source, multiple });
2230
+ logNativeFilePicker("native_pick_resolved", { source, fileCount: files.length });
2231
+ if (files.length === 0) {
2232
+ nativeWebViewRef.current?.injectJavaScript(buildFilePickerCancelledScript());
2233
+ return;
2234
+ }
2235
+ queuePickedFiles(files);
2236
+ } catch (error) {
2237
+ const { message, code } = readFilePickerError(error);
2238
+ warnNativeFilePicker("native_pick_failed", { source, code, message });
2239
+ nativeWebViewRef.current?.injectJavaScript(buildFilePickerErrorScript(message, code));
2240
+ } finally {
2241
+ pickerInFlightRef.current = false;
2242
+ pauseLivenessAfterPicker(LIVENESS_PICKER_GRACE_MS);
2155
2243
  }
2156
- queuePickedFiles(files);
2157
- }).catch((error) => {
2158
- const { message, code } = readFilePickerError(error);
2159
- nativeWebViewRef.current?.injectJavaScript(buildFilePickerErrorScript(message, code));
2160
- }).finally(() => {
2161
- pickerInFlightRef.current = false;
2162
- pauseLivenessAfterPicker(LIVENESS_PICKER_GRACE_MS);
2163
- });
2244
+ })();
2164
2245
  }, [pauseLivenessAfterPicker, queuePickedFiles]);
2165
- const openNativeFilePicker = useCallback((multiple) => {
2246
+ const openNativeFilePicker = useCallback((multiple, source) => {
2166
2247
  const filePicker = internalSagepilotChat.getConfig()?.filePicker;
2248
+ logNativeFilePicker("open_requested", {
2249
+ source: source ?? "source_chooser",
2250
+ multiple,
2251
+ hasFilePicker: Boolean(filePicker),
2252
+ availableSources: filePicker?.sources ?? []
2253
+ });
2167
2254
  if (!filePicker || filePicker.sources.length === 0) {
2255
+ warnNativeFilePicker("open_unavailable", { source: source ?? "source_chooser" });
2256
+ nativeWebViewRef.current?.injectJavaScript(buildFilePickerErrorScript(
2257
+ source === "camera" ? "CameraX is not available in this Expo runtime. Use a dev build with the CameraX addon linked." : "Native attachment picker is not available in this build.",
2258
+ "camera_unavailable"
2259
+ ));
2260
+ return;
2261
+ }
2262
+ if (source) {
2263
+ if (filePicker.sources.includes(source)) {
2264
+ runNativeFilePicker(source, multiple);
2265
+ } else {
2266
+ warnNativeFilePicker("source_not_available", { source, availableSources: filePicker.sources });
2267
+ nativeWebViewRef.current?.injectJavaScript(buildFilePickerErrorScript(
2268
+ source === "camera" ? "CameraX is not available in this build." : `Native ${source} picker is not available in this build.`,
2269
+ "camera_unavailable"
2270
+ ));
2271
+ }
2168
2272
  return;
2169
2273
  }
2170
2274
  const [onlySource] = filePicker.sources;
2171
- if (filePicker.sources.length === 1 && onlySource && onlySource !== "camera") {
2275
+ if (filePicker.sources.length === 1 && onlySource) {
2172
2276
  runNativeFilePicker(onlySource, multiple);
2173
2277
  return;
2174
2278
  }
@@ -2348,7 +2452,11 @@ function SagepilotChatProvider({
2348
2452
  const handleWebViewMessage = (event) => {
2349
2453
  const message = parseHostedBridgeMessage(event.nativeEvent?.data);
2350
2454
  if (message?.type === "sagepilot:open_file_picker") {
2351
- openNativeFilePicker(message.multiple ?? true);
2455
+ logNativeFilePicker("bridge_message_open_file_picker", {
2456
+ source: message.source ?? "source_chooser",
2457
+ multiple: message.multiple ?? true
2458
+ });
2459
+ openNativeFilePicker(message.multiple ?? true, message.source);
2352
2460
  return;
2353
2461
  }
2354
2462
  if (message?.type === "sagepilot:widget_listener_ready") {
@@ -2388,17 +2496,29 @@ function SagepilotChatProvider({
2388
2496
  if (!message || !targetOrigin) return;
2389
2497
  webFrameRef.current?.contentWindow?.postMessage(message, targetOrigin);
2390
2498
  };
2499
+ const postIdentityLogoutToWebFrame = () => {
2500
+ const message = internalSagepilotChat.getHostedIdentityLogoutMessage();
2501
+ const targetOrigin = readUrlOrigin(state.conversationUrl);
2502
+ if (!message || !targetOrigin) return;
2503
+ webFrameRef.current?.contentWindow?.postMessage(message, targetOrigin);
2504
+ };
2391
2505
  const postIdentityToNativeWebView = () => {
2392
2506
  const script = getHostedIdentityDispatchScript();
2393
2507
  if (!script || !nativeWebViewRef.current) return;
2394
2508
  nativeWebViewRef.current.injectJavaScript(script);
2395
2509
  };
2510
+ const postIdentityLogoutToNativeWebView = () => {
2511
+ const script = internalSagepilotChat.getHostedIdentityLogoutScript();
2512
+ if (!script || !nativeWebViewRef.current) return;
2513
+ nativeWebViewRef.current.injectJavaScript(script);
2514
+ };
2396
2515
  const injectNativeBridgeToNativeWebView = () => {
2397
2516
  if (!nativeWebViewRef.current) return;
2398
2517
  nativeWebViewRef.current.injectJavaScript(getInjectedWebViewScript());
2399
2518
  };
2400
2519
  const handleNativeWebViewLoadEnd = () => {
2401
2520
  injectNativeBridgeToNativeWebView();
2521
+ postIdentityLogoutToNativeWebView();
2402
2522
  postIdentityToNativeWebView();
2403
2523
  widgetReadyRef.current = false;
2404
2524
  startDelivery();
@@ -2430,10 +2550,22 @@ function SagepilotChatProvider({
2430
2550
  if (Platform3.OS !== "web" || !state.isPresented) return;
2431
2551
  postIdentityToWebFrame();
2432
2552
  }, [state.isPresented, state.conversationUrl, state.configured]);
2553
+ useEffect(() => {
2554
+ if (Platform3.OS !== "web" || !state.isPresented) return;
2555
+ postIdentityLogoutToWebFrame();
2556
+ }, [state.hostedIdentityLogoutVersion, state.isPresented, state.conversationUrl, state.configured]);
2433
2557
  useEffect(() => {
2434
2558
  if (Platform3.OS === "web" || !state.isPresented) return;
2435
2559
  postIdentityToNativeWebView();
2436
2560
  }, [state.isPresented, state.conversationUrl, state.configured]);
2561
+ useEffect(() => {
2562
+ if (Platform3.OS === "web" || !state.isPresented) return;
2563
+ postIdentityLogoutToNativeWebView();
2564
+ }, [state.hostedIdentityLogoutVersion, state.isPresented, state.conversationUrl, state.configured]);
2565
+ const handleWebFrameLoad = () => {
2566
+ postIdentityLogoutToWebFrame();
2567
+ postIdentityToWebFrame();
2568
+ };
2437
2569
  if (Platform3.OS === "web") {
2438
2570
  return createElement(
2439
2571
  View,
@@ -2465,7 +2597,7 @@ function SagepilotChatProvider({
2465
2597
  src: state.conversationUrl,
2466
2598
  style: styles.webFrame,
2467
2599
  title: "Sagepilot chat",
2468
- onLoad: postIdentityToWebFrame
2600
+ onLoad: handleWebFrameLoad
2469
2601
  })
2470
2602
  )
2471
2603
  ) : null
@@ -2566,7 +2698,6 @@ function SagepilotChatProvider({
2566
2698
  createElement(
2567
2699
  View,
2568
2700
  { style: styles.sheetCard },
2569
- createElement(View, { style: styles.sheetHandle }),
2570
2701
  createElement(Text, { style: styles.sheetTitle }, "Add attachment"),
2571
2702
  ...(internalSagepilotChat.getConfig()?.filePicker?.sources ?? []).map(
2572
2703
  (source) => createElement(
@@ -2574,29 +2705,19 @@ function SagepilotChatProvider({
2574
2705
  {
2575
2706
  key: source,
2576
2707
  accessibilityRole: "button",
2577
- android_ripple: { color: "rgba(15, 23, 42, 0.06)" },
2578
- hitSlop: 4,
2579
- style: ({ pressed }) => getSheetOptionStyle(pressed),
2708
+ style: styles.sheetButton,
2580
2709
  onPress: () => handleSourceChoice(source)
2581
2710
  },
2582
- renderFilePickerSourceIcon(source),
2583
- createElement(
2584
- View,
2585
- { style: styles.sheetOptionText },
2586
- createElement(Text, { style: styles.sheetButtonText }, FILE_PICKER_SOURCE_LABELS[source]),
2587
- createElement(Text, { style: styles.sheetButtonDescription }, FILE_PICKER_SOURCE_DESCRIPTIONS[source])
2588
- )
2711
+ createElement(Text, { style: styles.sheetButtonText }, FILE_PICKER_SOURCE_LABELS[source])
2589
2712
  )
2590
2713
  ),
2591
2714
  createElement(
2592
2715
  Pressable,
2593
2716
  {
2594
2717
  accessibilityRole: "button",
2595
- android_ripple: { color: "rgba(15, 23, 42, 0.06)" },
2596
- style: ({ pressed }) => getSheetCancelStyle(pressed),
2718
+ style: [styles.sheetButton, styles.sheetCancelButton],
2597
2719
  onPress: () => setSourceChooser(null)
2598
2720
  },
2599
- createElement(X, { color: "#334155", size: 18, strokeWidth: 2.5, style: styles.sheetCancelIcon }),
2600
2721
  createElement(Text, { style: styles.sheetCancelText }, "Cancel")
2601
2722
  )
2602
2723
  )
@@ -2702,109 +2823,43 @@ var styles = StyleSheet.create({
2702
2823
  },
2703
2824
  sheetBackdrop: {
2704
2825
  flex: 1,
2705
- alignItems: "center",
2706
2826
  justifyContent: "flex-end",
2707
- backgroundColor: "rgba(15, 23, 42, 0.48)"
2827
+ backgroundColor: "rgba(17, 24, 39, 0.36)"
2708
2828
  },
2709
2829
  sheetCard: {
2710
- width: "100%",
2711
- maxWidth: 540,
2712
2830
  backgroundColor: "#ffffff",
2713
- borderTopLeftRadius: 26,
2714
- borderTopRightRadius: 26,
2715
- paddingTop: 10,
2716
- paddingBottom: 18,
2717
- paddingHorizontal: 14,
2718
- shadowColor: "#0f172a",
2719
- shadowOpacity: 0.22,
2720
- shadowRadius: 28,
2721
- shadowOffset: { width: 0, height: -10 },
2722
- elevation: 18
2723
- },
2724
- sheetHandle: {
2725
- alignSelf: "center",
2726
- width: 42,
2727
- height: 5,
2728
- borderRadius: 999,
2729
- backgroundColor: "#d1d5db",
2730
- marginBottom: 14
2831
+ borderTopLeftRadius: 16,
2832
+ borderTopRightRadius: 16,
2833
+ paddingTop: 8,
2834
+ paddingBottom: 24,
2835
+ paddingHorizontal: 8
2731
2836
  },
2732
2837
  sheetTitle: {
2733
- color: "#111827",
2734
- fontSize: 17,
2735
- fontWeight: "700",
2736
- paddingBottom: 12,
2737
- paddingHorizontal: 4
2738
- },
2739
- sheetOption: {
2740
- minHeight: 70,
2741
- flexDirection: "row",
2742
- alignItems: "center",
2743
- borderRadius: 18,
2744
- paddingHorizontal: 12,
2745
- marginBottom: 6,
2746
- backgroundColor: "#ffffff"
2747
- },
2748
- sheetOptionPressed: {
2749
- backgroundColor: "#f8fafc",
2750
- transform: [{ scale: 0.985 }]
2838
+ textAlign: "center",
2839
+ color: "#6b7280",
2840
+ fontSize: 13,
2841
+ fontWeight: "600",
2842
+ paddingVertical: 10
2751
2843
  },
2752
- sheetOptionIcon: {
2753
- width: 46,
2754
- height: 46,
2755
- borderRadius: 16,
2756
- marginRight: 14,
2844
+ sheetButton: {
2845
+ minHeight: 52,
2757
2846
  alignItems: "center",
2758
2847
  justifyContent: "center",
2759
- position: "relative",
2760
- overflow: "hidden"
2761
- },
2762
- sheetOptionText: {
2763
- flex: 1,
2764
- justifyContent: "center"
2848
+ borderRadius: 12
2765
2849
  },
2766
2850
  sheetButtonText: {
2767
- color: "#0f172a",
2851
+ color: "#111827",
2768
2852
  fontSize: 16,
2769
- fontWeight: "700"
2770
- },
2771
- sheetButtonDescription: {
2772
- color: "#64748b",
2773
- fontSize: 13,
2774
- fontWeight: "500",
2775
- marginTop: 3
2776
- },
2777
- cameraOptionIcon: {
2778
- backgroundColor: "#e0f2fe"
2779
- },
2780
- libraryOptionIcon: {
2781
- backgroundColor: "#ecfdf5"
2782
- },
2783
- documentsOptionIcon: {
2784
- backgroundColor: "#f5f3ff"
2853
+ fontWeight: "500"
2785
2854
  },
2786
2855
  sheetCancelButton: {
2787
- minHeight: 56,
2788
- flexDirection: "row",
2789
- alignItems: "center",
2790
- justifyContent: "center",
2791
- borderRadius: 18,
2792
- marginTop: 8,
2793
- backgroundColor: "#f1f5f9"
2794
- },
2795
- sheetCancelButtonPressed: {
2796
- backgroundColor: "#e2e8f0",
2797
- transform: [{ scale: 0.985 }]
2798
- },
2799
- sheetCancelIcon: {
2800
- width: 18,
2801
- height: 18,
2802
- marginRight: 8
2856
+ marginTop: 6,
2857
+ backgroundColor: "#f3f4f6"
2803
2858
  },
2804
2859
  sheetCancelText: {
2805
- color: "#0f172a",
2860
+ color: "#111827",
2806
2861
  fontSize: 16,
2807
- fontWeight: "700"
2862
+ fontWeight: "600"
2808
2863
  }
2809
2864
  });
2810
2865
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sagepilot-ai/react-native-sdk",
3
- "version": "0.3.2",
3
+ "version": "0.3.4",
4
4
  "description": "Sagepilot AI React Native chat SDK",
5
5
  "keywords": [
6
6
  "sagepilot",
@@ -46,9 +46,6 @@
46
46
  "typecheck": "tsc --noEmit",
47
47
  "prepublishOnly": "npm run typecheck && npm run build"
48
48
  },
49
- "dependencies": {
50
- "lucide-react-native": "^1.21.0"
51
- },
52
49
  "peerDependencies": {
53
50
  "@react-native-async-storage/async-storage": ">=1.23",
54
51
  "@react-native-documents/picker": ">=10",
@@ -59,7 +56,6 @@
59
56
  "react-native-device-info": ">=11",
60
57
  "react-native-image-picker": ">=7",
61
58
  "react-native-keychain": ">=9",
62
- "react-native-svg": ">=15",
63
59
  "react-native-webview": ">=13"
64
60
  },
65
61
  "peerDependenciesMeta": {
@@ -86,7 +82,6 @@
86
82
  }
87
83
  },
88
84
  "devDependencies": {
89
- "react-native-svg": "^15.15.5",
90
85
  "tsup": "^8.3.5",
91
86
  "typescript": "^5.7.2"
92
87
  }