@kontextso/sdk-react-native 0.0.7-rc.0 → 0.0.7-rc.3
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/dist/index.d.mts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +90 -54
- package/dist/index.mjs +76 -40
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -142,6 +142,7 @@ interface AdsProviderProps {
|
|
|
142
142
|
logLevel?: any;
|
|
143
143
|
conversationId: string;
|
|
144
144
|
styles?: AdStyles;
|
|
145
|
+
experimentErrorBoundary?: 'provider' | 'hook' | 'inlinead-top' | 'inlinead-content' | 'markdowntext' | 'log' | 'none';
|
|
145
146
|
}
|
|
146
147
|
declare const VISITOR_ID_KEY = "brain-visitor-id";
|
|
147
148
|
declare const AdsProvider: ({ children, messages, publisherToken, isLoading, adserverUrl, isDisabled, character, conversationId, userId, logLevel, onAdView, onAdClick, ...props }: AdsProviderProps) => react_jsx_runtime.JSX.Element;
|
package/dist/index.d.ts
CHANGED
|
@@ -142,6 +142,7 @@ interface AdsProviderProps {
|
|
|
142
142
|
logLevel?: any;
|
|
143
143
|
conversationId: string;
|
|
144
144
|
styles?: AdStyles;
|
|
145
|
+
experimentErrorBoundary?: 'provider' | 'hook' | 'inlinead-top' | 'inlinead-content' | 'markdowntext' | 'log' | 'none';
|
|
145
146
|
}
|
|
146
147
|
declare const VISITOR_ID_KEY = "brain-visitor-id";
|
|
147
148
|
declare const AdsProvider: ({ children, messages, publisherToken, isLoading, adserverUrl, isDisabled, character, conversationId, userId, logLevel, onAdView, onAdClick, ...props }: AdsProviderProps) => react_jsx_runtime.JSX.Element;
|
package/dist/index.js
CHANGED
|
@@ -39,19 +39,20 @@ __export(src_exports, {
|
|
|
39
39
|
module.exports = __toCommonJS(src_exports);
|
|
40
40
|
|
|
41
41
|
// src/formats/InlineAd.tsx
|
|
42
|
-
var
|
|
42
|
+
var import_react13 = require("react");
|
|
43
43
|
var import_react_native5 = require("react-native");
|
|
44
44
|
|
|
45
45
|
// src/hooks/useAdViewed.tsx
|
|
46
|
-
var
|
|
46
|
+
var import_react7 = require("react");
|
|
47
47
|
|
|
48
48
|
// src/context/AdsProvider.tsx
|
|
49
|
-
var
|
|
49
|
+
var import_react6 = __toESM(require("react"));
|
|
50
50
|
|
|
51
51
|
// src/hooks/useInitializeAds.tsx
|
|
52
|
-
var
|
|
52
|
+
var import_react2 = require("react");
|
|
53
53
|
|
|
54
54
|
// src/utils.ts
|
|
55
|
+
var import_react = require("react");
|
|
55
56
|
var UNRETRIABLE_ERRORS = [403, 429];
|
|
56
57
|
var fetchWithTimeout = async (input, init) => {
|
|
57
58
|
const { timeout = 16e3 } = init || {};
|
|
@@ -158,6 +159,17 @@ function mergeStyles(propStyles, defaultStyles, overridingStyles) {
|
|
|
158
159
|
mergedStyles = deepMerge(mergedStyles, overridingStyles || {});
|
|
159
160
|
return mergedStyles;
|
|
160
161
|
}
|
|
162
|
+
var useTestError = (boundarySetting, errorPosition, shouldThrow, delay) => {
|
|
163
|
+
const [throwError, setThrowError] = (0, import_react.useState)(false);
|
|
164
|
+
(0, import_react.useEffect)(() => {
|
|
165
|
+
setTimeout(() => {
|
|
166
|
+
setThrowError(true);
|
|
167
|
+
}, delay);
|
|
168
|
+
}, []);
|
|
169
|
+
if (boundarySetting === errorPosition && throwError && shouldThrow) {
|
|
170
|
+
throw new Error("test");
|
|
171
|
+
}
|
|
172
|
+
};
|
|
161
173
|
var parseMessageText = (text) => {
|
|
162
174
|
const parts = [];
|
|
163
175
|
let start = 0;
|
|
@@ -277,7 +289,7 @@ var log = new Logger();
|
|
|
277
289
|
var log_default = log;
|
|
278
290
|
|
|
279
291
|
// package.json
|
|
280
|
-
var version = "0.0.7-rc.
|
|
292
|
+
var version = "0.0.7-rc.2";
|
|
281
293
|
|
|
282
294
|
// src/hooks/useInitializeAds.tsx
|
|
283
295
|
var SINGLE_INIT_TIMEOUT_BUDGET_MS = 3e3;
|
|
@@ -336,18 +348,27 @@ function useInitializeAds({
|
|
|
336
348
|
conversationId,
|
|
337
349
|
isDisabled,
|
|
338
350
|
character,
|
|
339
|
-
legacyVisitorId
|
|
351
|
+
legacyVisitorId,
|
|
352
|
+
experimentErrorBoundary
|
|
340
353
|
}) {
|
|
341
|
-
const [enabledPlacements, setEnabledPlacements] = (0,
|
|
342
|
-
const [ads, setAds] = (0,
|
|
343
|
-
const [error, setError] = (0,
|
|
344
|
-
const [streamAdServerUrl, setStreamAdServerUrl] = (0,
|
|
345
|
-
const [onlyStream, setOnlyStream] = (0,
|
|
346
|
-
const [sessionId, setSessionId] = (0,
|
|
347
|
-
const [defaultReactNativeStyles, setDefaultReactNativeStyles] = (0,
|
|
348
|
-
const [overridingReactNativeStyles, setOverridingReactNativeStyles] = (0,
|
|
349
|
-
(
|
|
354
|
+
const [enabledPlacements, setEnabledPlacements] = (0, import_react2.useState)([]);
|
|
355
|
+
const [ads, setAds] = (0, import_react2.useState)([]);
|
|
356
|
+
const [error, setError] = (0, import_react2.useState)();
|
|
357
|
+
const [streamAdServerUrl, setStreamAdServerUrl] = (0, import_react2.useState)();
|
|
358
|
+
const [onlyStream, setOnlyStream] = (0, import_react2.useState)(false);
|
|
359
|
+
const [sessionId, setSessionId] = (0, import_react2.useState)();
|
|
360
|
+
const [defaultReactNativeStyles, setDefaultReactNativeStyles] = (0, import_react2.useState)();
|
|
361
|
+
const [overridingReactNativeStyles, setOverridingReactNativeStyles] = (0, import_react2.useState)();
|
|
362
|
+
useTestError(experimentErrorBoundary, "hook", true, 2e3);
|
|
363
|
+
(0, import_react2.useEffect)(() => {
|
|
350
364
|
log_default.setRemoteLevel("debug");
|
|
365
|
+
log_default.configureRemote(adServerUrl, {
|
|
366
|
+
publisherToken,
|
|
367
|
+
userId,
|
|
368
|
+
visitorId: userId,
|
|
369
|
+
conversationId,
|
|
370
|
+
character
|
|
371
|
+
});
|
|
351
372
|
setSessionId(void 0);
|
|
352
373
|
if (!isDisabled && userId) {
|
|
353
374
|
log_default.debug("[BRAIN] Initializing ads.");
|
|
@@ -371,6 +392,15 @@ function useInitializeAds({
|
|
|
371
392
|
remoteLogLevel
|
|
372
393
|
}) => {
|
|
373
394
|
log_default.setRemoteLevel(remoteLogLevel || "silent");
|
|
395
|
+
log_default.configureRemote(adServerUrl, {
|
|
396
|
+
publisherToken,
|
|
397
|
+
adServerUrl,
|
|
398
|
+
userId,
|
|
399
|
+
conversationId,
|
|
400
|
+
legacyVisitorId,
|
|
401
|
+
character,
|
|
402
|
+
sessionId: sessionId2
|
|
403
|
+
});
|
|
374
404
|
if (!sessionDisabled) {
|
|
375
405
|
setSessionId(sessionId2);
|
|
376
406
|
setEnabledPlacements(enabledPlacements2);
|
|
@@ -410,7 +440,7 @@ function useInitializeAds({
|
|
|
410
440
|
}
|
|
411
441
|
|
|
412
442
|
// src/hooks/usePreloadAds.tsx
|
|
413
|
-
var
|
|
443
|
+
var import_react3 = require("react");
|
|
414
444
|
function usePreloadAds({
|
|
415
445
|
userId,
|
|
416
446
|
sessionId,
|
|
@@ -421,10 +451,10 @@ function usePreloadAds({
|
|
|
421
451
|
character,
|
|
422
452
|
onlyStream
|
|
423
453
|
}) {
|
|
424
|
-
const [preloadDone, setPreloadDone] = (0,
|
|
425
|
-
const [ads, setAds] = (0,
|
|
454
|
+
const [preloadDone, setPreloadDone] = (0, import_react3.useState)(false);
|
|
455
|
+
const [ads, setAds] = (0, import_react3.useState)([]);
|
|
426
456
|
const userMessagesContent = messages.filter((m) => m.role === "user").map((m) => m.content).join(" ");
|
|
427
|
-
(0,
|
|
457
|
+
(0, import_react3.useEffect)(() => {
|
|
428
458
|
if (onlyStream) {
|
|
429
459
|
setPreloadDone(true);
|
|
430
460
|
log_default.log("[BRAIN] skipping preload ads");
|
|
@@ -473,7 +503,7 @@ function usePreloadAds({
|
|
|
473
503
|
}
|
|
474
504
|
|
|
475
505
|
// src/hooks/useStreamAds.tsx
|
|
476
|
-
var
|
|
506
|
+
var import_react4 = require("react");
|
|
477
507
|
|
|
478
508
|
// src/hooks/data-stream.ts
|
|
479
509
|
var textStreamPart = {
|
|
@@ -722,7 +752,7 @@ function useStreamAds({
|
|
|
722
752
|
character,
|
|
723
753
|
publisherToken
|
|
724
754
|
}) {
|
|
725
|
-
const [ads, setAds] = (0,
|
|
755
|
+
const [ads, setAds] = (0, import_react4.useState)([]);
|
|
726
756
|
const fetchStream = async (messages2, code) => {
|
|
727
757
|
const lastAssistantMessage = [...messages2].reverse().find((m) => m.role === "assistant");
|
|
728
758
|
if (!lastAssistantMessage) {
|
|
@@ -817,7 +847,7 @@ function useStreamAds({
|
|
|
817
847
|
setAds((oldAds) => [...oldAds, { isError: true, code }]);
|
|
818
848
|
}
|
|
819
849
|
};
|
|
820
|
-
(0,
|
|
850
|
+
(0, import_react4.useEffect)(() => {
|
|
821
851
|
if (!isLoading && sessionId && messages.length > 2 && messages[messages.length - 1]?.role === "assistant" && preloadDone) {
|
|
822
852
|
enabledPlacements.forEach((placement) => {
|
|
823
853
|
if (["QUERY_STREAM", "INLINE_AD", "BOX_AD"].includes(placement.format)) {
|
|
@@ -830,7 +860,7 @@ function useStreamAds({
|
|
|
830
860
|
}
|
|
831
861
|
|
|
832
862
|
// src/components/ErrorBoundary.tsx
|
|
833
|
-
var
|
|
863
|
+
var import_react5 = __toESM(require("react"));
|
|
834
864
|
var captureErrorFn = (adServerUrl, error, componentStack, context) => {
|
|
835
865
|
fetch(`${adServerUrl}/error`, {
|
|
836
866
|
method: "POST",
|
|
@@ -843,7 +873,7 @@ var captureErrorFn = (adServerUrl, error, componentStack, context) => {
|
|
|
843
873
|
log_default.warn("Error reporting client error", e);
|
|
844
874
|
});
|
|
845
875
|
};
|
|
846
|
-
var ErrorBoundary = class extends
|
|
876
|
+
var ErrorBoundary = class extends import_react5.default.Component {
|
|
847
877
|
constructor(props) {
|
|
848
878
|
super(props);
|
|
849
879
|
this.state = {
|
|
@@ -871,7 +901,7 @@ var ErrorBoundary = class extends import_react4.default.Component {
|
|
|
871
901
|
|
|
872
902
|
// src/context/AdsProvider.tsx
|
|
873
903
|
var import_jsx_runtime = require("react/jsx-runtime");
|
|
874
|
-
var AdsContext =
|
|
904
|
+
var AdsContext = import_react6.default.createContext(null);
|
|
875
905
|
AdsContext.displayName = "AdsContext";
|
|
876
906
|
var VISITOR_ID_KEY = "brain-visitor-id";
|
|
877
907
|
var AdsProviderWithoutBoundary = ({
|
|
@@ -889,8 +919,8 @@ var AdsProviderWithoutBoundary = ({
|
|
|
889
919
|
onAdClick,
|
|
890
920
|
...props
|
|
891
921
|
}) => {
|
|
892
|
-
const [viewedAds, setViewedAds] = (0,
|
|
893
|
-
const [clickedAds, setClickedAds] = (0,
|
|
922
|
+
const [viewedAds, setViewedAds] = (0, import_react6.useState)([]);
|
|
923
|
+
const [clickedAds, setClickedAds] = (0, import_react6.useState)([]);
|
|
894
924
|
const adServerUrlOrDefault = adserverUrl || "https://server.megabrain.co";
|
|
895
925
|
log_default.setLocalLevel(logLevel || "silent");
|
|
896
926
|
const {
|
|
@@ -909,7 +939,8 @@ var AdsProviderWithoutBoundary = ({
|
|
|
909
939
|
legacyVisitorId: void 0,
|
|
910
940
|
conversationId,
|
|
911
941
|
character,
|
|
912
|
-
isDisabled: !!isDisabled
|
|
942
|
+
isDisabled: !!isDisabled,
|
|
943
|
+
experimentErrorBoundary: props.experimentErrorBoundary
|
|
913
944
|
});
|
|
914
945
|
const styles = mergeStyles(props.styles, defaultReactNativeStyles, overridingReactNativeStyles);
|
|
915
946
|
const isInitialised = !!isDisabled || !!error || !!sessionId;
|
|
@@ -964,6 +995,7 @@ var AdsProviderWithoutBoundary = ({
|
|
|
964
995
|
}
|
|
965
996
|
setClickedAds((old) => [...old, ad.id]);
|
|
966
997
|
};
|
|
998
|
+
useTestError(props.experimentErrorBoundary, "provider", true, 2e3);
|
|
967
999
|
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
968
1000
|
AdsContext.Provider,
|
|
969
1001
|
{
|
|
@@ -984,7 +1016,8 @@ var AdsProviderWithoutBoundary = ({
|
|
|
984
1016
|
userId,
|
|
985
1017
|
markAdAsViewed,
|
|
986
1018
|
onlyStream,
|
|
987
|
-
styles
|
|
1019
|
+
styles,
|
|
1020
|
+
experimentErrorBoundary: props.experimentErrorBoundary
|
|
988
1021
|
},
|
|
989
1022
|
children
|
|
990
1023
|
}
|
|
@@ -1042,10 +1075,10 @@ var AdsProvider = ({
|
|
|
1042
1075
|
};
|
|
1043
1076
|
|
|
1044
1077
|
// src/hooks/useAdViewed.tsx
|
|
1045
|
-
var
|
|
1078
|
+
var import_react8 = require("react");
|
|
1046
1079
|
function useAdViewed(ad) {
|
|
1047
|
-
const context = (0,
|
|
1048
|
-
const [stillMounted, setStillMounted] = (0,
|
|
1080
|
+
const context = (0, import_react8.useContext)(AdsContext);
|
|
1081
|
+
const [stillMounted, setStillMounted] = (0, import_react7.useState)(false);
|
|
1049
1082
|
const sendRequest = async () => {
|
|
1050
1083
|
if (!context?.adserverUrl || !ad?.id) {
|
|
1051
1084
|
return;
|
|
@@ -1067,7 +1100,7 @@ function useAdViewed(ad) {
|
|
|
1067
1100
|
log_default.warn("[BRAIN] Error sending view request", e);
|
|
1068
1101
|
}
|
|
1069
1102
|
};
|
|
1070
|
-
(0,
|
|
1103
|
+
(0, import_react7.useEffect)(() => {
|
|
1071
1104
|
if (!ad || ad.isError || ad.isLoading || ad.isStreaming || ad.viewed || stillMounted) {
|
|
1072
1105
|
return;
|
|
1073
1106
|
}
|
|
@@ -1077,7 +1110,7 @@ function useAdViewed(ad) {
|
|
|
1077
1110
|
setStillMounted(true);
|
|
1078
1111
|
}, 1e3);
|
|
1079
1112
|
}, [ad]);
|
|
1080
|
-
(0,
|
|
1113
|
+
(0, import_react7.useEffect)(() => {
|
|
1081
1114
|
if (stillMounted) {
|
|
1082
1115
|
log_default.log("[BRAIN] sending request");
|
|
1083
1116
|
sendRequest();
|
|
@@ -1086,16 +1119,17 @@ function useAdViewed(ad) {
|
|
|
1086
1119
|
}
|
|
1087
1120
|
|
|
1088
1121
|
// src/components/MarkdownText.tsx
|
|
1089
|
-
var
|
|
1122
|
+
var import_react9 = require("react");
|
|
1090
1123
|
var import_react_native2 = require("react-native");
|
|
1091
1124
|
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
1092
1125
|
function MarkdownText({
|
|
1093
1126
|
content,
|
|
1094
1127
|
onLinkClick
|
|
1095
1128
|
}) {
|
|
1096
|
-
const context = (0,
|
|
1129
|
+
const context = (0, import_react9.useContext)(AdsContext);
|
|
1097
1130
|
const textParts = parseMessageText(content);
|
|
1098
1131
|
const styles = context?.styles?.markdownText;
|
|
1132
|
+
useTestError(context?.experimentErrorBoundary, "markdowntext", true, 2e3);
|
|
1099
1133
|
const linkClickHandler = (href) => {
|
|
1100
1134
|
onLinkClick();
|
|
1101
1135
|
import_react_native2.Linking.openURL(href).catch(
|
|
@@ -1126,17 +1160,17 @@ function MarkdownText({
|
|
|
1126
1160
|
|
|
1127
1161
|
// src/components/VideoPlayer.tsx
|
|
1128
1162
|
var import_expo_av = require("expo-av");
|
|
1129
|
-
var
|
|
1163
|
+
var import_react11 = require("react");
|
|
1130
1164
|
var import_react_native4 = require("react-native");
|
|
1131
1165
|
|
|
1132
1166
|
// src/components/VideoProgressBar.tsx
|
|
1133
|
-
var
|
|
1167
|
+
var import_react10 = require("react");
|
|
1134
1168
|
var import_react_native3 = require("react-native");
|
|
1135
1169
|
var import_jsx_runtime3 = require("react/jsx-runtime");
|
|
1136
1170
|
function VideoProgressBar({ videoRef }) {
|
|
1137
|
-
const [progress, setProgress] = (0,
|
|
1138
|
-
const styles = (0,
|
|
1139
|
-
(0,
|
|
1171
|
+
const [progress, setProgress] = (0, import_react10.useState)(0);
|
|
1172
|
+
const styles = (0, import_react10.useContext)(AdsContext)?.styles?.videoPlayer?.videoProgress;
|
|
1173
|
+
(0, import_react10.useEffect)(() => {
|
|
1140
1174
|
const interval = setInterval(() => {
|
|
1141
1175
|
if (!videoRef.current) {
|
|
1142
1176
|
return;
|
|
@@ -1169,14 +1203,14 @@ var VideoPlayer = ({
|
|
|
1169
1203
|
mediaPlacement,
|
|
1170
1204
|
onLinkClick
|
|
1171
1205
|
}) => {
|
|
1172
|
-
const videoRef = (0,
|
|
1173
|
-
const [isPlaying, setIsPlaying] = (0,
|
|
1174
|
-
const [isPaused, setIsPaused] = (0,
|
|
1175
|
-
const [isEnded, setIsEnded] = (0,
|
|
1176
|
-
const [showDownloadBadge, setShowDownloadBadge] = (0,
|
|
1177
|
-
const lastCallbackTimeRef = (0,
|
|
1178
|
-
const styles = (0,
|
|
1179
|
-
(0,
|
|
1206
|
+
const videoRef = (0, import_react11.useRef)(null);
|
|
1207
|
+
const [isPlaying, setIsPlaying] = (0, import_react11.useState)(false);
|
|
1208
|
+
const [isPaused, setIsPaused] = (0, import_react11.useState)(true);
|
|
1209
|
+
const [isEnded, setIsEnded] = (0, import_react11.useState)(false);
|
|
1210
|
+
const [showDownloadBadge, setShowDownloadBadge] = (0, import_react11.useState)(false);
|
|
1211
|
+
const lastCallbackTimeRef = (0, import_react11.useRef)(0);
|
|
1212
|
+
const styles = (0, import_react11.useContext)(AdsContext)?.styles?.videoPlayer;
|
|
1213
|
+
(0, import_react11.useEffect)(() => {
|
|
1180
1214
|
const interval = setInterval(() => {
|
|
1181
1215
|
if (videoRef.current) {
|
|
1182
1216
|
videoRef.current.getStatusAsync().then((status) => {
|
|
@@ -1259,9 +1293,9 @@ var VideoPlayer = ({
|
|
|
1259
1293
|
};
|
|
1260
1294
|
|
|
1261
1295
|
// src/hooks/useAd.tsx
|
|
1262
|
-
var
|
|
1296
|
+
var import_react12 = require("react");
|
|
1263
1297
|
function useAd({ code, messageId }) {
|
|
1264
|
-
const context = (0,
|
|
1298
|
+
const context = (0, import_react12.useContext)(AdsContext);
|
|
1265
1299
|
if (!context) {
|
|
1266
1300
|
return null;
|
|
1267
1301
|
}
|
|
@@ -1309,15 +1343,17 @@ function useAd({ code, messageId }) {
|
|
|
1309
1343
|
var import_jsx_runtime5 = require("react/jsx-runtime");
|
|
1310
1344
|
var InlineAd = ({ code, messageId, wrapper }) => {
|
|
1311
1345
|
const ad = useAd({ code, messageId });
|
|
1312
|
-
const [linkIncluded, setLinkIncluded] = (0,
|
|
1313
|
-
const context = (0,
|
|
1346
|
+
const [linkIncluded, setLinkIncluded] = (0, import_react13.useState)(false);
|
|
1347
|
+
const context = (0, import_react13.useContext)(AdsContext);
|
|
1314
1348
|
const styles = context?.styles?.inlineAd;
|
|
1315
1349
|
useAdViewed(ad);
|
|
1316
|
-
(
|
|
1350
|
+
useTestError(context?.experimentErrorBoundary, "inlinead-top", true, 2e3);
|
|
1351
|
+
(0, import_react13.useEffect)(() => {
|
|
1317
1352
|
if (ad?.content && !ad.isStreaming && !ad.content.match(/\[.*?\]\(.*?\)/) && !linkIncluded) {
|
|
1318
1353
|
setLinkIncluded(true);
|
|
1319
1354
|
}
|
|
1320
1355
|
}, [ad]);
|
|
1356
|
+
useTestError(context?.experimentErrorBoundary, "inlinead-content", !!ad, 2e3);
|
|
1321
1357
|
if (!ad || !ad.content || context?.isDisabled) return null;
|
|
1322
1358
|
if (ad.isLoading || ad.content?.trim().toLowerCase().includes("none"))
|
|
1323
1359
|
return null;
|
package/dist/index.mjs
CHANGED
|
@@ -6,7 +6,7 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
|
|
|
6
6
|
});
|
|
7
7
|
|
|
8
8
|
// src/formats/InlineAd.tsx
|
|
9
|
-
import { useContext as useContext6, useEffect as
|
|
9
|
+
import { useContext as useContext6, useEffect as useEffect9, useState as useState9 } from "react";
|
|
10
10
|
import {
|
|
11
11
|
Image,
|
|
12
12
|
Linking as Linking3,
|
|
@@ -17,15 +17,16 @@ import {
|
|
|
17
17
|
} from "react-native";
|
|
18
18
|
|
|
19
19
|
// src/hooks/useAdViewed.tsx
|
|
20
|
-
import { useEffect as
|
|
20
|
+
import { useEffect as useEffect6, useState as useState6 } from "react";
|
|
21
21
|
|
|
22
22
|
// src/context/AdsProvider.tsx
|
|
23
|
-
import React2, { useState as
|
|
23
|
+
import React2, { useState as useState5 } from "react";
|
|
24
24
|
|
|
25
25
|
// src/hooks/useInitializeAds.tsx
|
|
26
|
-
import { useEffect, useState } from "react";
|
|
26
|
+
import { useEffect as useEffect2, useState as useState2 } from "react";
|
|
27
27
|
|
|
28
28
|
// src/utils.ts
|
|
29
|
+
import { useEffect, useState } from "react";
|
|
29
30
|
var UNRETRIABLE_ERRORS = [403, 429];
|
|
30
31
|
var fetchWithTimeout = async (input, init) => {
|
|
31
32
|
const { timeout = 16e3 } = init || {};
|
|
@@ -132,6 +133,17 @@ function mergeStyles(propStyles, defaultStyles, overridingStyles) {
|
|
|
132
133
|
mergedStyles = deepMerge(mergedStyles, overridingStyles || {});
|
|
133
134
|
return mergedStyles;
|
|
134
135
|
}
|
|
136
|
+
var useTestError = (boundarySetting, errorPosition, shouldThrow, delay) => {
|
|
137
|
+
const [throwError, setThrowError] = useState(false);
|
|
138
|
+
useEffect(() => {
|
|
139
|
+
setTimeout(() => {
|
|
140
|
+
setThrowError(true);
|
|
141
|
+
}, delay);
|
|
142
|
+
}, []);
|
|
143
|
+
if (boundarySetting === errorPosition && throwError && shouldThrow) {
|
|
144
|
+
throw new Error("test");
|
|
145
|
+
}
|
|
146
|
+
};
|
|
135
147
|
var parseMessageText = (text) => {
|
|
136
148
|
const parts = [];
|
|
137
149
|
let start = 0;
|
|
@@ -251,7 +263,7 @@ var log = new Logger();
|
|
|
251
263
|
var log_default = log;
|
|
252
264
|
|
|
253
265
|
// package.json
|
|
254
|
-
var version = "0.0.7-rc.
|
|
266
|
+
var version = "0.0.7-rc.2";
|
|
255
267
|
|
|
256
268
|
// src/hooks/useInitializeAds.tsx
|
|
257
269
|
var SINGLE_INIT_TIMEOUT_BUDGET_MS = 3e3;
|
|
@@ -310,18 +322,27 @@ function useInitializeAds({
|
|
|
310
322
|
conversationId,
|
|
311
323
|
isDisabled,
|
|
312
324
|
character,
|
|
313
|
-
legacyVisitorId
|
|
325
|
+
legacyVisitorId,
|
|
326
|
+
experimentErrorBoundary
|
|
314
327
|
}) {
|
|
315
|
-
const [enabledPlacements, setEnabledPlacements] =
|
|
316
|
-
const [ads, setAds] =
|
|
317
|
-
const [error, setError] =
|
|
318
|
-
const [streamAdServerUrl, setStreamAdServerUrl] =
|
|
319
|
-
const [onlyStream, setOnlyStream] =
|
|
320
|
-
const [sessionId, setSessionId] =
|
|
321
|
-
const [defaultReactNativeStyles, setDefaultReactNativeStyles] =
|
|
322
|
-
const [overridingReactNativeStyles, setOverridingReactNativeStyles] =
|
|
323
|
-
|
|
328
|
+
const [enabledPlacements, setEnabledPlacements] = useState2([]);
|
|
329
|
+
const [ads, setAds] = useState2([]);
|
|
330
|
+
const [error, setError] = useState2();
|
|
331
|
+
const [streamAdServerUrl, setStreamAdServerUrl] = useState2();
|
|
332
|
+
const [onlyStream, setOnlyStream] = useState2(false);
|
|
333
|
+
const [sessionId, setSessionId] = useState2();
|
|
334
|
+
const [defaultReactNativeStyles, setDefaultReactNativeStyles] = useState2();
|
|
335
|
+
const [overridingReactNativeStyles, setOverridingReactNativeStyles] = useState2();
|
|
336
|
+
useTestError(experimentErrorBoundary, "hook", true, 2e3);
|
|
337
|
+
useEffect2(() => {
|
|
324
338
|
log_default.setRemoteLevel("debug");
|
|
339
|
+
log_default.configureRemote(adServerUrl, {
|
|
340
|
+
publisherToken,
|
|
341
|
+
userId,
|
|
342
|
+
visitorId: userId,
|
|
343
|
+
conversationId,
|
|
344
|
+
character
|
|
345
|
+
});
|
|
325
346
|
setSessionId(void 0);
|
|
326
347
|
if (!isDisabled && userId) {
|
|
327
348
|
log_default.debug("[BRAIN] Initializing ads.");
|
|
@@ -345,6 +366,15 @@ function useInitializeAds({
|
|
|
345
366
|
remoteLogLevel
|
|
346
367
|
}) => {
|
|
347
368
|
log_default.setRemoteLevel(remoteLogLevel || "silent");
|
|
369
|
+
log_default.configureRemote(adServerUrl, {
|
|
370
|
+
publisherToken,
|
|
371
|
+
adServerUrl,
|
|
372
|
+
userId,
|
|
373
|
+
conversationId,
|
|
374
|
+
legacyVisitorId,
|
|
375
|
+
character,
|
|
376
|
+
sessionId: sessionId2
|
|
377
|
+
});
|
|
348
378
|
if (!sessionDisabled) {
|
|
349
379
|
setSessionId(sessionId2);
|
|
350
380
|
setEnabledPlacements(enabledPlacements2);
|
|
@@ -384,7 +414,7 @@ function useInitializeAds({
|
|
|
384
414
|
}
|
|
385
415
|
|
|
386
416
|
// src/hooks/usePreloadAds.tsx
|
|
387
|
-
import { useEffect as
|
|
417
|
+
import { useEffect as useEffect3, useState as useState3 } from "react";
|
|
388
418
|
function usePreloadAds({
|
|
389
419
|
userId,
|
|
390
420
|
sessionId,
|
|
@@ -395,10 +425,10 @@ function usePreloadAds({
|
|
|
395
425
|
character,
|
|
396
426
|
onlyStream
|
|
397
427
|
}) {
|
|
398
|
-
const [preloadDone, setPreloadDone] =
|
|
399
|
-
const [ads, setAds] =
|
|
428
|
+
const [preloadDone, setPreloadDone] = useState3(false);
|
|
429
|
+
const [ads, setAds] = useState3([]);
|
|
400
430
|
const userMessagesContent = messages.filter((m) => m.role === "user").map((m) => m.content).join(" ");
|
|
401
|
-
|
|
431
|
+
useEffect3(() => {
|
|
402
432
|
if (onlyStream) {
|
|
403
433
|
setPreloadDone(true);
|
|
404
434
|
log_default.log("[BRAIN] skipping preload ads");
|
|
@@ -447,7 +477,7 @@ function usePreloadAds({
|
|
|
447
477
|
}
|
|
448
478
|
|
|
449
479
|
// src/hooks/useStreamAds.tsx
|
|
450
|
-
import { useEffect as
|
|
480
|
+
import { useEffect as useEffect4, useState as useState4 } from "react";
|
|
451
481
|
|
|
452
482
|
// src/hooks/data-stream.ts
|
|
453
483
|
var textStreamPart = {
|
|
@@ -696,7 +726,7 @@ function useStreamAds({
|
|
|
696
726
|
character,
|
|
697
727
|
publisherToken
|
|
698
728
|
}) {
|
|
699
|
-
const [ads, setAds] =
|
|
729
|
+
const [ads, setAds] = useState4([]);
|
|
700
730
|
const fetchStream = async (messages2, code) => {
|
|
701
731
|
const lastAssistantMessage = [...messages2].reverse().find((m) => m.role === "assistant");
|
|
702
732
|
if (!lastAssistantMessage) {
|
|
@@ -791,7 +821,7 @@ function useStreamAds({
|
|
|
791
821
|
setAds((oldAds) => [...oldAds, { isError: true, code }]);
|
|
792
822
|
}
|
|
793
823
|
};
|
|
794
|
-
|
|
824
|
+
useEffect4(() => {
|
|
795
825
|
if (!isLoading && sessionId && messages.length > 2 && messages[messages.length - 1]?.role === "assistant" && preloadDone) {
|
|
796
826
|
enabledPlacements.forEach((placement) => {
|
|
797
827
|
if (["QUERY_STREAM", "INLINE_AD", "BOX_AD"].includes(placement.format)) {
|
|
@@ -863,8 +893,8 @@ var AdsProviderWithoutBoundary = ({
|
|
|
863
893
|
onAdClick,
|
|
864
894
|
...props
|
|
865
895
|
}) => {
|
|
866
|
-
const [viewedAds, setViewedAds] =
|
|
867
|
-
const [clickedAds, setClickedAds] =
|
|
896
|
+
const [viewedAds, setViewedAds] = useState5([]);
|
|
897
|
+
const [clickedAds, setClickedAds] = useState5([]);
|
|
868
898
|
const adServerUrlOrDefault = adserverUrl || "https://server.megabrain.co";
|
|
869
899
|
log_default.setLocalLevel(logLevel || "silent");
|
|
870
900
|
const {
|
|
@@ -883,7 +913,8 @@ var AdsProviderWithoutBoundary = ({
|
|
|
883
913
|
legacyVisitorId: void 0,
|
|
884
914
|
conversationId,
|
|
885
915
|
character,
|
|
886
|
-
isDisabled: !!isDisabled
|
|
916
|
+
isDisabled: !!isDisabled,
|
|
917
|
+
experimentErrorBoundary: props.experimentErrorBoundary
|
|
887
918
|
});
|
|
888
919
|
const styles = mergeStyles(props.styles, defaultReactNativeStyles, overridingReactNativeStyles);
|
|
889
920
|
const isInitialised = !!isDisabled || !!error || !!sessionId;
|
|
@@ -938,6 +969,7 @@ var AdsProviderWithoutBoundary = ({
|
|
|
938
969
|
}
|
|
939
970
|
setClickedAds((old) => [...old, ad.id]);
|
|
940
971
|
};
|
|
972
|
+
useTestError(props.experimentErrorBoundary, "provider", true, 2e3);
|
|
941
973
|
return /* @__PURE__ */ jsx(
|
|
942
974
|
AdsContext.Provider,
|
|
943
975
|
{
|
|
@@ -958,7 +990,8 @@ var AdsProviderWithoutBoundary = ({
|
|
|
958
990
|
userId,
|
|
959
991
|
markAdAsViewed,
|
|
960
992
|
onlyStream,
|
|
961
|
-
styles
|
|
993
|
+
styles,
|
|
994
|
+
experimentErrorBoundary: props.experimentErrorBoundary
|
|
962
995
|
},
|
|
963
996
|
children
|
|
964
997
|
}
|
|
@@ -1019,7 +1052,7 @@ var AdsProvider = ({
|
|
|
1019
1052
|
import { useContext } from "react";
|
|
1020
1053
|
function useAdViewed(ad) {
|
|
1021
1054
|
const context = useContext(AdsContext);
|
|
1022
|
-
const [stillMounted, setStillMounted] =
|
|
1055
|
+
const [stillMounted, setStillMounted] = useState6(false);
|
|
1023
1056
|
const sendRequest = async () => {
|
|
1024
1057
|
if (!context?.adserverUrl || !ad?.id) {
|
|
1025
1058
|
return;
|
|
@@ -1041,7 +1074,7 @@ function useAdViewed(ad) {
|
|
|
1041
1074
|
log_default.warn("[BRAIN] Error sending view request", e);
|
|
1042
1075
|
}
|
|
1043
1076
|
};
|
|
1044
|
-
|
|
1077
|
+
useEffect6(() => {
|
|
1045
1078
|
if (!ad || ad.isError || ad.isLoading || ad.isStreaming || ad.viewed || stillMounted) {
|
|
1046
1079
|
return;
|
|
1047
1080
|
}
|
|
@@ -1051,7 +1084,7 @@ function useAdViewed(ad) {
|
|
|
1051
1084
|
setStillMounted(true);
|
|
1052
1085
|
}, 1e3);
|
|
1053
1086
|
}, [ad]);
|
|
1054
|
-
|
|
1087
|
+
useEffect6(() => {
|
|
1055
1088
|
if (stillMounted) {
|
|
1056
1089
|
log_default.log("[BRAIN] sending request");
|
|
1057
1090
|
sendRequest();
|
|
@@ -1073,6 +1106,7 @@ function MarkdownText({
|
|
|
1073
1106
|
const context = useContext2(AdsContext);
|
|
1074
1107
|
const textParts = parseMessageText(content);
|
|
1075
1108
|
const styles = context?.styles?.markdownText;
|
|
1109
|
+
useTestError(context?.experimentErrorBoundary, "markdowntext", true, 2e3);
|
|
1076
1110
|
const linkClickHandler = (href) => {
|
|
1077
1111
|
onLinkClick();
|
|
1078
1112
|
Linking.openURL(href).catch(
|
|
@@ -1103,17 +1137,17 @@ function MarkdownText({
|
|
|
1103
1137
|
|
|
1104
1138
|
// src/components/VideoPlayer.tsx
|
|
1105
1139
|
import { ResizeMode, Video } from "expo-av";
|
|
1106
|
-
import { useContext as useContext4, useEffect as
|
|
1140
|
+
import { useContext as useContext4, useEffect as useEffect8, useRef, useState as useState8 } from "react";
|
|
1107
1141
|
import { Linking as Linking2, Text as Text2, TouchableOpacity, View as View2 } from "react-native";
|
|
1108
1142
|
|
|
1109
1143
|
// src/components/VideoProgressBar.tsx
|
|
1110
|
-
import { useState as
|
|
1144
|
+
import { useState as useState7, useEffect as useEffect7, useContext as useContext3 } from "react";
|
|
1111
1145
|
import { View } from "react-native";
|
|
1112
1146
|
import { jsx as jsx3 } from "react/jsx-runtime";
|
|
1113
1147
|
function VideoProgressBar({ videoRef }) {
|
|
1114
|
-
const [progress, setProgress] =
|
|
1148
|
+
const [progress, setProgress] = useState7(0);
|
|
1115
1149
|
const styles = useContext3(AdsContext)?.styles?.videoPlayer?.videoProgress;
|
|
1116
|
-
|
|
1150
|
+
useEffect7(() => {
|
|
1117
1151
|
const interval = setInterval(() => {
|
|
1118
1152
|
if (!videoRef.current) {
|
|
1119
1153
|
return;
|
|
@@ -1147,13 +1181,13 @@ var VideoPlayer = ({
|
|
|
1147
1181
|
onLinkClick
|
|
1148
1182
|
}) => {
|
|
1149
1183
|
const videoRef = useRef(null);
|
|
1150
|
-
const [isPlaying, setIsPlaying] =
|
|
1151
|
-
const [isPaused, setIsPaused] =
|
|
1152
|
-
const [isEnded, setIsEnded] =
|
|
1153
|
-
const [showDownloadBadge, setShowDownloadBadge] =
|
|
1184
|
+
const [isPlaying, setIsPlaying] = useState8(false);
|
|
1185
|
+
const [isPaused, setIsPaused] = useState8(true);
|
|
1186
|
+
const [isEnded, setIsEnded] = useState8(false);
|
|
1187
|
+
const [showDownloadBadge, setShowDownloadBadge] = useState8(false);
|
|
1154
1188
|
const lastCallbackTimeRef = useRef(0);
|
|
1155
1189
|
const styles = useContext4(AdsContext)?.styles?.videoPlayer;
|
|
1156
|
-
|
|
1190
|
+
useEffect8(() => {
|
|
1157
1191
|
const interval = setInterval(() => {
|
|
1158
1192
|
if (videoRef.current) {
|
|
1159
1193
|
videoRef.current.getStatusAsync().then((status) => {
|
|
@@ -1286,15 +1320,17 @@ function useAd({ code, messageId }) {
|
|
|
1286
1320
|
import { jsx as jsx5, jsxs as jsxs2 } from "react/jsx-runtime";
|
|
1287
1321
|
var InlineAd = ({ code, messageId, wrapper }) => {
|
|
1288
1322
|
const ad = useAd({ code, messageId });
|
|
1289
|
-
const [linkIncluded, setLinkIncluded] =
|
|
1323
|
+
const [linkIncluded, setLinkIncluded] = useState9(false);
|
|
1290
1324
|
const context = useContext6(AdsContext);
|
|
1291
1325
|
const styles = context?.styles?.inlineAd;
|
|
1292
1326
|
useAdViewed(ad);
|
|
1293
|
-
|
|
1327
|
+
useTestError(context?.experimentErrorBoundary, "inlinead-top", true, 2e3);
|
|
1328
|
+
useEffect9(() => {
|
|
1294
1329
|
if (ad?.content && !ad.isStreaming && !ad.content.match(/\[.*?\]\(.*?\)/) && !linkIncluded) {
|
|
1295
1330
|
setLinkIncluded(true);
|
|
1296
1331
|
}
|
|
1297
1332
|
}, [ad]);
|
|
1333
|
+
useTestError(context?.experimentErrorBoundary, "inlinead-content", !!ad, 2e3);
|
|
1298
1334
|
if (!ad || !ad.content || context?.isDisabled) return null;
|
|
1299
1335
|
if (ad.isLoading || ad.content?.trim().toLowerCase().includes("none"))
|
|
1300
1336
|
return null;
|