@sagepilot-ai/react-native-sdk 0.3.3 → 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 +3 -0
- package/dist/index.js +141 -18
- package/dist/index.mjs +141 -18
- package/package.json +1 -3
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
|
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.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
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
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.
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
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
|
-
|
|
978
|
-
|
|
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
|
|
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:
|
|
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;
|
|
@@ -1844,6 +1941,7 @@ function readPresentationState() {
|
|
|
1844
1941
|
conversationUrl: internalSagepilotChat.getState().configured ? internalSagepilotChat.getConversationUrl() : null,
|
|
1845
1942
|
preloadUrl: internalSagepilotChat.getState().configured ? internalSagepilotChat.getPreloadUrl() : null,
|
|
1846
1943
|
shouldPreload: internalSagepilotChat.getConfig()?.behavior?.preloadWebView ?? true,
|
|
1944
|
+
hostedIdentityLogoutVersion: internalSagepilotChat.getHostedIdentityLogoutVersion(),
|
|
1847
1945
|
presentation: internalSagepilotChat.getConfig()?.presentation
|
|
1848
1946
|
};
|
|
1849
1947
|
}
|
|
@@ -1858,6 +1956,7 @@ function getInjectedWebViewScript() {
|
|
|
1858
1956
|
const capabilities = getBridgeCapabilities();
|
|
1859
1957
|
return [
|
|
1860
1958
|
internalSagepilotChat.getHostedAuthScript(),
|
|
1959
|
+
internalSagepilotChat.getHostedIdentityCleanupScript(),
|
|
1861
1960
|
buildMobileWebViewBridgeScript(capabilities),
|
|
1862
1961
|
buildBridgeCapabilitiesScript(capabilities)
|
|
1863
1962
|
].filter(Boolean).join("\n");
|
|
@@ -2434,17 +2533,29 @@ function SagepilotChatProvider({
|
|
|
2434
2533
|
if (!message || !targetOrigin) return;
|
|
2435
2534
|
webFrameRef.current?.contentWindow?.postMessage(message, targetOrigin);
|
|
2436
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
|
+
};
|
|
2437
2542
|
const postIdentityToNativeWebView = () => {
|
|
2438
2543
|
const script = getHostedIdentityDispatchScript();
|
|
2439
2544
|
if (!script || !nativeWebViewRef.current) return;
|
|
2440
2545
|
nativeWebViewRef.current.injectJavaScript(script);
|
|
2441
2546
|
};
|
|
2547
|
+
const postIdentityLogoutToNativeWebView = () => {
|
|
2548
|
+
const script = internalSagepilotChat.getHostedIdentityLogoutScript();
|
|
2549
|
+
if (!script || !nativeWebViewRef.current) return;
|
|
2550
|
+
nativeWebViewRef.current.injectJavaScript(script);
|
|
2551
|
+
};
|
|
2442
2552
|
const injectNativeBridgeToNativeWebView = () => {
|
|
2443
2553
|
if (!nativeWebViewRef.current) return;
|
|
2444
2554
|
nativeWebViewRef.current.injectJavaScript(getInjectedWebViewScript());
|
|
2445
2555
|
};
|
|
2446
2556
|
const handleNativeWebViewLoadEnd = () => {
|
|
2447
2557
|
injectNativeBridgeToNativeWebView();
|
|
2558
|
+
postIdentityLogoutToNativeWebView();
|
|
2448
2559
|
postIdentityToNativeWebView();
|
|
2449
2560
|
widgetReadyRef.current = false;
|
|
2450
2561
|
startDelivery();
|
|
@@ -2476,10 +2587,22 @@ function SagepilotChatProvider({
|
|
|
2476
2587
|
if (import_react_native3.Platform.OS !== "web" || !state.isPresented) return;
|
|
2477
2588
|
postIdentityToWebFrame();
|
|
2478
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]);
|
|
2479
2594
|
(0, import_react.useEffect)(() => {
|
|
2480
2595
|
if (import_react_native3.Platform.OS === "web" || !state.isPresented) return;
|
|
2481
2596
|
postIdentityToNativeWebView();
|
|
2482
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
|
+
};
|
|
2483
2606
|
if (import_react_native3.Platform.OS === "web") {
|
|
2484
2607
|
return (0, import_react.createElement)(
|
|
2485
2608
|
import_react_native3.View,
|
|
@@ -2511,7 +2634,7 @@ function SagepilotChatProvider({
|
|
|
2511
2634
|
src: state.conversationUrl,
|
|
2512
2635
|
style: styles.webFrame,
|
|
2513
2636
|
title: "Sagepilot chat",
|
|
2514
|
-
onLoad:
|
|
2637
|
+
onLoad: handleWebFrameLoad
|
|
2515
2638
|
})
|
|
2516
2639
|
)
|
|
2517
2640
|
) : null
|
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.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
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
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.
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
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
|
-
|
|
929
|
-
|
|
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
|
|
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:
|
|
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;
|
|
@@ -1807,6 +1904,7 @@ function readPresentationState() {
|
|
|
1807
1904
|
conversationUrl: internalSagepilotChat.getState().configured ? internalSagepilotChat.getConversationUrl() : null,
|
|
1808
1905
|
preloadUrl: internalSagepilotChat.getState().configured ? internalSagepilotChat.getPreloadUrl() : null,
|
|
1809
1906
|
shouldPreload: internalSagepilotChat.getConfig()?.behavior?.preloadWebView ?? true,
|
|
1907
|
+
hostedIdentityLogoutVersion: internalSagepilotChat.getHostedIdentityLogoutVersion(),
|
|
1810
1908
|
presentation: internalSagepilotChat.getConfig()?.presentation
|
|
1811
1909
|
};
|
|
1812
1910
|
}
|
|
@@ -1821,6 +1919,7 @@ function getInjectedWebViewScript() {
|
|
|
1821
1919
|
const capabilities = getBridgeCapabilities();
|
|
1822
1920
|
return [
|
|
1823
1921
|
internalSagepilotChat.getHostedAuthScript(),
|
|
1922
|
+
internalSagepilotChat.getHostedIdentityCleanupScript(),
|
|
1824
1923
|
buildMobileWebViewBridgeScript(capabilities),
|
|
1825
1924
|
buildBridgeCapabilitiesScript(capabilities)
|
|
1826
1925
|
].filter(Boolean).join("\n");
|
|
@@ -2397,17 +2496,29 @@ function SagepilotChatProvider({
|
|
|
2397
2496
|
if (!message || !targetOrigin) return;
|
|
2398
2497
|
webFrameRef.current?.contentWindow?.postMessage(message, targetOrigin);
|
|
2399
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
|
+
};
|
|
2400
2505
|
const postIdentityToNativeWebView = () => {
|
|
2401
2506
|
const script = getHostedIdentityDispatchScript();
|
|
2402
2507
|
if (!script || !nativeWebViewRef.current) return;
|
|
2403
2508
|
nativeWebViewRef.current.injectJavaScript(script);
|
|
2404
2509
|
};
|
|
2510
|
+
const postIdentityLogoutToNativeWebView = () => {
|
|
2511
|
+
const script = internalSagepilotChat.getHostedIdentityLogoutScript();
|
|
2512
|
+
if (!script || !nativeWebViewRef.current) return;
|
|
2513
|
+
nativeWebViewRef.current.injectJavaScript(script);
|
|
2514
|
+
};
|
|
2405
2515
|
const injectNativeBridgeToNativeWebView = () => {
|
|
2406
2516
|
if (!nativeWebViewRef.current) return;
|
|
2407
2517
|
nativeWebViewRef.current.injectJavaScript(getInjectedWebViewScript());
|
|
2408
2518
|
};
|
|
2409
2519
|
const handleNativeWebViewLoadEnd = () => {
|
|
2410
2520
|
injectNativeBridgeToNativeWebView();
|
|
2521
|
+
postIdentityLogoutToNativeWebView();
|
|
2411
2522
|
postIdentityToNativeWebView();
|
|
2412
2523
|
widgetReadyRef.current = false;
|
|
2413
2524
|
startDelivery();
|
|
@@ -2439,10 +2550,22 @@ function SagepilotChatProvider({
|
|
|
2439
2550
|
if (Platform3.OS !== "web" || !state.isPresented) return;
|
|
2440
2551
|
postIdentityToWebFrame();
|
|
2441
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]);
|
|
2442
2557
|
useEffect(() => {
|
|
2443
2558
|
if (Platform3.OS === "web" || !state.isPresented) return;
|
|
2444
2559
|
postIdentityToNativeWebView();
|
|
2445
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
|
+
};
|
|
2446
2569
|
if (Platform3.OS === "web") {
|
|
2447
2570
|
return createElement(
|
|
2448
2571
|
View,
|
|
@@ -2474,7 +2597,7 @@ function SagepilotChatProvider({
|
|
|
2474
2597
|
src: state.conversationUrl,
|
|
2475
2598
|
style: styles.webFrame,
|
|
2476
2599
|
title: "Sagepilot chat",
|
|
2477
|
-
onLoad:
|
|
2600
|
+
onLoad: handleWebFrameLoad
|
|
2478
2601
|
})
|
|
2479
2602
|
)
|
|
2480
2603
|
) : null
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sagepilot-ai/react-native-sdk",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.4",
|
|
4
4
|
"description": "Sagepilot AI React Native chat SDK",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"sagepilot",
|
|
@@ -56,7 +56,6 @@
|
|
|
56
56
|
"react-native-device-info": ">=11",
|
|
57
57
|
"react-native-image-picker": ">=7",
|
|
58
58
|
"react-native-keychain": ">=9",
|
|
59
|
-
"react-native-svg": ">=15",
|
|
60
59
|
"react-native-webview": ">=13"
|
|
61
60
|
},
|
|
62
61
|
"peerDependenciesMeta": {
|
|
@@ -83,7 +82,6 @@
|
|
|
83
82
|
}
|
|
84
83
|
},
|
|
85
84
|
"devDependencies": {
|
|
86
|
-
"react-native-svg": "^15.15.5",
|
|
87
85
|
"tsup": "^8.3.5",
|
|
88
86
|
"typescript": "^5.7.2"
|
|
89
87
|
}
|