@kontextso/sdk-react-native 0.0.7 → 0.0.8-rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +0 -1
- package/dist/index.d.ts +0 -1
- package/dist/index.js +55 -76
- package/dist/index.mjs +41 -62
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -142,7 +142,6 @@ 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';
|
|
146
145
|
}
|
|
147
146
|
declare const VISITOR_ID_KEY = "brain-visitor-id";
|
|
148
147
|
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,7 +142,6 @@ 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';
|
|
146
145
|
}
|
|
147
146
|
declare const VISITOR_ID_KEY = "brain-visitor-id";
|
|
148
147
|
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,20 +39,19 @@ __export(src_exports, {
|
|
|
39
39
|
module.exports = __toCommonJS(src_exports);
|
|
40
40
|
|
|
41
41
|
// src/formats/InlineAd.tsx
|
|
42
|
-
var
|
|
42
|
+
var import_react12 = require("react");
|
|
43
43
|
var import_react_native5 = require("react-native");
|
|
44
44
|
|
|
45
45
|
// src/hooks/useAdViewed.tsx
|
|
46
|
-
var
|
|
46
|
+
var import_react6 = require("react");
|
|
47
47
|
|
|
48
48
|
// src/context/AdsProvider.tsx
|
|
49
|
-
var
|
|
49
|
+
var import_react5 = __toESM(require("react"));
|
|
50
50
|
|
|
51
51
|
// src/hooks/useInitializeAds.tsx
|
|
52
|
-
var
|
|
52
|
+
var import_react = require("react");
|
|
53
53
|
|
|
54
54
|
// src/utils.ts
|
|
55
|
-
var import_react = require("react");
|
|
56
55
|
var UNRETRIABLE_ERRORS = [403, 429];
|
|
57
56
|
var fetchWithTimeout = async (input, init) => {
|
|
58
57
|
const { timeout = 16e3 } = init || {};
|
|
@@ -159,17 +158,6 @@ function mergeStyles(propStyles, defaultStyles, overridingStyles) {
|
|
|
159
158
|
mergedStyles = deepMerge(mergedStyles, overridingStyles || {});
|
|
160
159
|
return mergedStyles;
|
|
161
160
|
}
|
|
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
|
-
};
|
|
173
161
|
var parseMessageText = (text) => {
|
|
174
162
|
const parts = [];
|
|
175
163
|
let start = 0;
|
|
@@ -289,7 +277,7 @@ var log = new Logger();
|
|
|
289
277
|
var log_default = log;
|
|
290
278
|
|
|
291
279
|
// package.json
|
|
292
|
-
var version = "0.0.7
|
|
280
|
+
var version = "0.0.7";
|
|
293
281
|
|
|
294
282
|
// src/hooks/useInitializeAds.tsx
|
|
295
283
|
var SINGLE_INIT_TIMEOUT_BUDGET_MS = 3e3;
|
|
@@ -348,19 +336,17 @@ function useInitializeAds({
|
|
|
348
336
|
conversationId,
|
|
349
337
|
isDisabled,
|
|
350
338
|
character,
|
|
351
|
-
legacyVisitorId
|
|
352
|
-
experimentErrorBoundary
|
|
339
|
+
legacyVisitorId
|
|
353
340
|
}) {
|
|
354
|
-
const [enabledPlacements, setEnabledPlacements] = (0,
|
|
355
|
-
const [ads, setAds] = (0,
|
|
356
|
-
const [error, setError] = (0,
|
|
357
|
-
const [streamAdServerUrl, setStreamAdServerUrl] = (0,
|
|
358
|
-
const [onlyStream, setOnlyStream] = (0,
|
|
359
|
-
const [sessionId, setSessionId] = (0,
|
|
360
|
-
const [defaultReactNativeStyles, setDefaultReactNativeStyles] = (0,
|
|
361
|
-
const [overridingReactNativeStyles, setOverridingReactNativeStyles] = (0,
|
|
362
|
-
|
|
363
|
-
(0, import_react2.useEffect)(() => {
|
|
341
|
+
const [enabledPlacements, setEnabledPlacements] = (0, import_react.useState)([]);
|
|
342
|
+
const [ads, setAds] = (0, import_react.useState)([]);
|
|
343
|
+
const [error, setError] = (0, import_react.useState)();
|
|
344
|
+
const [streamAdServerUrl, setStreamAdServerUrl] = (0, import_react.useState)();
|
|
345
|
+
const [onlyStream, setOnlyStream] = (0, import_react.useState)(false);
|
|
346
|
+
const [sessionId, setSessionId] = (0, import_react.useState)();
|
|
347
|
+
const [defaultReactNativeStyles, setDefaultReactNativeStyles] = (0, import_react.useState)();
|
|
348
|
+
const [overridingReactNativeStyles, setOverridingReactNativeStyles] = (0, import_react.useState)();
|
|
349
|
+
(0, import_react.useEffect)(() => {
|
|
364
350
|
log_default.setRemoteLevel("debug");
|
|
365
351
|
log_default.configureRemote(adServerUrl, {
|
|
366
352
|
publisherToken,
|
|
@@ -440,7 +426,7 @@ function useInitializeAds({
|
|
|
440
426
|
}
|
|
441
427
|
|
|
442
428
|
// src/hooks/usePreloadAds.tsx
|
|
443
|
-
var
|
|
429
|
+
var import_react2 = require("react");
|
|
444
430
|
function usePreloadAds({
|
|
445
431
|
userId,
|
|
446
432
|
sessionId,
|
|
@@ -451,10 +437,10 @@ function usePreloadAds({
|
|
|
451
437
|
character,
|
|
452
438
|
onlyStream
|
|
453
439
|
}) {
|
|
454
|
-
const [preloadDone, setPreloadDone] = (0,
|
|
455
|
-
const [ads, setAds] = (0,
|
|
440
|
+
const [preloadDone, setPreloadDone] = (0, import_react2.useState)(false);
|
|
441
|
+
const [ads, setAds] = (0, import_react2.useState)([]);
|
|
456
442
|
const userMessagesContent = messages.filter((m) => m.role === "user").map((m) => m.content).join(" ");
|
|
457
|
-
(0,
|
|
443
|
+
(0, import_react2.useEffect)(() => {
|
|
458
444
|
if (onlyStream) {
|
|
459
445
|
setPreloadDone(true);
|
|
460
446
|
log_default.log("[BRAIN] skipping preload ads");
|
|
@@ -503,7 +489,7 @@ function usePreloadAds({
|
|
|
503
489
|
}
|
|
504
490
|
|
|
505
491
|
// src/hooks/useStreamAds.tsx
|
|
506
|
-
var
|
|
492
|
+
var import_react3 = require("react");
|
|
507
493
|
|
|
508
494
|
// src/hooks/data-stream.ts
|
|
509
495
|
var textStreamPart = {
|
|
@@ -752,7 +738,7 @@ function useStreamAds({
|
|
|
752
738
|
character,
|
|
753
739
|
publisherToken
|
|
754
740
|
}) {
|
|
755
|
-
const [ads, setAds] = (0,
|
|
741
|
+
const [ads, setAds] = (0, import_react3.useState)([]);
|
|
756
742
|
const fetchStream = async (messages2, code) => {
|
|
757
743
|
const lastAssistantMessage = [...messages2].reverse().find((m) => m.role === "assistant");
|
|
758
744
|
if (!lastAssistantMessage) {
|
|
@@ -847,7 +833,7 @@ function useStreamAds({
|
|
|
847
833
|
setAds((oldAds) => [...oldAds, { isError: true, code }]);
|
|
848
834
|
}
|
|
849
835
|
};
|
|
850
|
-
(0,
|
|
836
|
+
(0, import_react3.useEffect)(() => {
|
|
851
837
|
if (!isLoading && sessionId && messages.length > 2 && messages[messages.length - 1]?.role === "assistant" && preloadDone) {
|
|
852
838
|
enabledPlacements.forEach((placement) => {
|
|
853
839
|
if (["QUERY_STREAM", "INLINE_AD", "BOX_AD"].includes(placement.format)) {
|
|
@@ -860,7 +846,7 @@ function useStreamAds({
|
|
|
860
846
|
}
|
|
861
847
|
|
|
862
848
|
// src/components/ErrorBoundary.tsx
|
|
863
|
-
var
|
|
849
|
+
var import_react4 = __toESM(require("react"));
|
|
864
850
|
var captureErrorFn = (adServerUrl, error, componentStack, context) => {
|
|
865
851
|
fetch(`${adServerUrl}/error`, {
|
|
866
852
|
method: "POST",
|
|
@@ -873,7 +859,7 @@ var captureErrorFn = (adServerUrl, error, componentStack, context) => {
|
|
|
873
859
|
log_default.warn("Error reporting client error", e);
|
|
874
860
|
});
|
|
875
861
|
};
|
|
876
|
-
var ErrorBoundary = class extends
|
|
862
|
+
var ErrorBoundary = class extends import_react4.default.Component {
|
|
877
863
|
constructor(props) {
|
|
878
864
|
super(props);
|
|
879
865
|
this.state = {
|
|
@@ -901,7 +887,7 @@ var ErrorBoundary = class extends import_react5.default.Component {
|
|
|
901
887
|
|
|
902
888
|
// src/context/AdsProvider.tsx
|
|
903
889
|
var import_jsx_runtime = require("react/jsx-runtime");
|
|
904
|
-
var AdsContext =
|
|
890
|
+
var AdsContext = import_react5.default.createContext(null);
|
|
905
891
|
AdsContext.displayName = "AdsContext";
|
|
906
892
|
var VISITOR_ID_KEY = "brain-visitor-id";
|
|
907
893
|
var AdsProviderWithoutBoundary = ({
|
|
@@ -919,8 +905,8 @@ var AdsProviderWithoutBoundary = ({
|
|
|
919
905
|
onAdClick,
|
|
920
906
|
...props
|
|
921
907
|
}) => {
|
|
922
|
-
const [viewedAds, setViewedAds] = (0,
|
|
923
|
-
const [clickedAds, setClickedAds] = (0,
|
|
908
|
+
const [viewedAds, setViewedAds] = (0, import_react5.useState)([]);
|
|
909
|
+
const [clickedAds, setClickedAds] = (0, import_react5.useState)([]);
|
|
924
910
|
const adServerUrlOrDefault = adserverUrl || "https://server.megabrain.co";
|
|
925
911
|
log_default.setLocalLevel(logLevel || "silent");
|
|
926
912
|
const {
|
|
@@ -939,8 +925,7 @@ var AdsProviderWithoutBoundary = ({
|
|
|
939
925
|
legacyVisitorId: void 0,
|
|
940
926
|
conversationId,
|
|
941
927
|
character,
|
|
942
|
-
isDisabled: !!isDisabled
|
|
943
|
-
experimentErrorBoundary: props.experimentErrorBoundary
|
|
928
|
+
isDisabled: !!isDisabled
|
|
944
929
|
});
|
|
945
930
|
const styles = mergeStyles(props.styles, defaultReactNativeStyles, overridingReactNativeStyles);
|
|
946
931
|
const isInitialised = !!isDisabled || !!error || !!sessionId;
|
|
@@ -995,7 +980,6 @@ var AdsProviderWithoutBoundary = ({
|
|
|
995
980
|
}
|
|
996
981
|
setClickedAds((old) => [...old, ad.id]);
|
|
997
982
|
};
|
|
998
|
-
useTestError(props.experimentErrorBoundary, "provider", true, 2e3);
|
|
999
983
|
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
1000
984
|
AdsContext.Provider,
|
|
1001
985
|
{
|
|
@@ -1016,8 +1000,7 @@ var AdsProviderWithoutBoundary = ({
|
|
|
1016
1000
|
userId,
|
|
1017
1001
|
markAdAsViewed,
|
|
1018
1002
|
onlyStream,
|
|
1019
|
-
styles
|
|
1020
|
-
experimentErrorBoundary: props.experimentErrorBoundary
|
|
1003
|
+
styles
|
|
1021
1004
|
},
|
|
1022
1005
|
children
|
|
1023
1006
|
}
|
|
@@ -1067,8 +1050,7 @@ var AdsProvider = ({
|
|
|
1067
1050
|
userId,
|
|
1068
1051
|
logLevel,
|
|
1069
1052
|
onAdView,
|
|
1070
|
-
onAdClick
|
|
1071
|
-
...props
|
|
1053
|
+
onAdClick
|
|
1072
1054
|
}
|
|
1073
1055
|
)
|
|
1074
1056
|
}
|
|
@@ -1076,10 +1058,10 @@ var AdsProvider = ({
|
|
|
1076
1058
|
};
|
|
1077
1059
|
|
|
1078
1060
|
// src/hooks/useAdViewed.tsx
|
|
1079
|
-
var
|
|
1061
|
+
var import_react7 = require("react");
|
|
1080
1062
|
function useAdViewed(ad) {
|
|
1081
|
-
const context = (0,
|
|
1082
|
-
const [stillMounted, setStillMounted] = (0,
|
|
1063
|
+
const context = (0, import_react7.useContext)(AdsContext);
|
|
1064
|
+
const [stillMounted, setStillMounted] = (0, import_react6.useState)(false);
|
|
1083
1065
|
const sendRequest = async () => {
|
|
1084
1066
|
if (!context?.adserverUrl || !ad?.id) {
|
|
1085
1067
|
return;
|
|
@@ -1101,7 +1083,7 @@ function useAdViewed(ad) {
|
|
|
1101
1083
|
log_default.warn("[BRAIN] Error sending view request", e);
|
|
1102
1084
|
}
|
|
1103
1085
|
};
|
|
1104
|
-
(0,
|
|
1086
|
+
(0, import_react6.useEffect)(() => {
|
|
1105
1087
|
if (!ad || ad.isError || ad.isLoading || ad.isStreaming || ad.viewed || stillMounted) {
|
|
1106
1088
|
return;
|
|
1107
1089
|
}
|
|
@@ -1111,7 +1093,7 @@ function useAdViewed(ad) {
|
|
|
1111
1093
|
setStillMounted(true);
|
|
1112
1094
|
}, 1e3);
|
|
1113
1095
|
}, [ad]);
|
|
1114
|
-
(0,
|
|
1096
|
+
(0, import_react6.useEffect)(() => {
|
|
1115
1097
|
if (stillMounted) {
|
|
1116
1098
|
log_default.log("[BRAIN] sending request");
|
|
1117
1099
|
sendRequest();
|
|
@@ -1120,17 +1102,16 @@ function useAdViewed(ad) {
|
|
|
1120
1102
|
}
|
|
1121
1103
|
|
|
1122
1104
|
// src/components/MarkdownText.tsx
|
|
1123
|
-
var
|
|
1105
|
+
var import_react8 = require("react");
|
|
1124
1106
|
var import_react_native2 = require("react-native");
|
|
1125
1107
|
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
1126
1108
|
function MarkdownText({
|
|
1127
1109
|
content,
|
|
1128
1110
|
onLinkClick
|
|
1129
1111
|
}) {
|
|
1130
|
-
const context = (0,
|
|
1112
|
+
const context = (0, import_react8.useContext)(AdsContext);
|
|
1131
1113
|
const textParts = parseMessageText(content);
|
|
1132
1114
|
const styles = context?.styles?.markdownText;
|
|
1133
|
-
useTestError(context?.experimentErrorBoundary, "markdowntext", true, 2e3);
|
|
1134
1115
|
const linkClickHandler = (href) => {
|
|
1135
1116
|
onLinkClick();
|
|
1136
1117
|
import_react_native2.Linking.openURL(href).catch(
|
|
@@ -1161,17 +1142,17 @@ function MarkdownText({
|
|
|
1161
1142
|
|
|
1162
1143
|
// src/components/VideoPlayer.tsx
|
|
1163
1144
|
var import_expo_av = require("expo-av");
|
|
1164
|
-
var
|
|
1145
|
+
var import_react10 = require("react");
|
|
1165
1146
|
var import_react_native4 = require("react-native");
|
|
1166
1147
|
|
|
1167
1148
|
// src/components/VideoProgressBar.tsx
|
|
1168
|
-
var
|
|
1149
|
+
var import_react9 = require("react");
|
|
1169
1150
|
var import_react_native3 = require("react-native");
|
|
1170
1151
|
var import_jsx_runtime3 = require("react/jsx-runtime");
|
|
1171
1152
|
function VideoProgressBar({ videoRef }) {
|
|
1172
|
-
const [progress, setProgress] = (0,
|
|
1173
|
-
const styles = (0,
|
|
1174
|
-
(0,
|
|
1153
|
+
const [progress, setProgress] = (0, import_react9.useState)(0);
|
|
1154
|
+
const styles = (0, import_react9.useContext)(AdsContext)?.styles?.videoPlayer?.videoProgress;
|
|
1155
|
+
(0, import_react9.useEffect)(() => {
|
|
1175
1156
|
const interval = setInterval(() => {
|
|
1176
1157
|
if (!videoRef.current) {
|
|
1177
1158
|
return;
|
|
@@ -1204,14 +1185,14 @@ var VideoPlayer = ({
|
|
|
1204
1185
|
mediaPlacement,
|
|
1205
1186
|
onLinkClick
|
|
1206
1187
|
}) => {
|
|
1207
|
-
const videoRef = (0,
|
|
1208
|
-
const [isPlaying, setIsPlaying] = (0,
|
|
1209
|
-
const [isPaused, setIsPaused] = (0,
|
|
1210
|
-
const [isEnded, setIsEnded] = (0,
|
|
1211
|
-
const [showDownloadBadge, setShowDownloadBadge] = (0,
|
|
1212
|
-
const lastCallbackTimeRef = (0,
|
|
1213
|
-
const styles = (0,
|
|
1214
|
-
(0,
|
|
1188
|
+
const videoRef = (0, import_react10.useRef)(null);
|
|
1189
|
+
const [isPlaying, setIsPlaying] = (0, import_react10.useState)(false);
|
|
1190
|
+
const [isPaused, setIsPaused] = (0, import_react10.useState)(true);
|
|
1191
|
+
const [isEnded, setIsEnded] = (0, import_react10.useState)(false);
|
|
1192
|
+
const [showDownloadBadge, setShowDownloadBadge] = (0, import_react10.useState)(false);
|
|
1193
|
+
const lastCallbackTimeRef = (0, import_react10.useRef)(0);
|
|
1194
|
+
const styles = (0, import_react10.useContext)(AdsContext)?.styles?.videoPlayer;
|
|
1195
|
+
(0, import_react10.useEffect)(() => {
|
|
1215
1196
|
const interval = setInterval(() => {
|
|
1216
1197
|
if (videoRef.current) {
|
|
1217
1198
|
videoRef.current.getStatusAsync().then((status) => {
|
|
@@ -1294,9 +1275,9 @@ var VideoPlayer = ({
|
|
|
1294
1275
|
};
|
|
1295
1276
|
|
|
1296
1277
|
// src/hooks/useAd.tsx
|
|
1297
|
-
var
|
|
1278
|
+
var import_react11 = require("react");
|
|
1298
1279
|
function useAd({ code, messageId }) {
|
|
1299
|
-
const context = (0,
|
|
1280
|
+
const context = (0, import_react11.useContext)(AdsContext);
|
|
1300
1281
|
if (!context) {
|
|
1301
1282
|
return null;
|
|
1302
1283
|
}
|
|
@@ -1344,17 +1325,15 @@ function useAd({ code, messageId }) {
|
|
|
1344
1325
|
var import_jsx_runtime5 = require("react/jsx-runtime");
|
|
1345
1326
|
var InlineAd = ({ code, messageId, wrapper }) => {
|
|
1346
1327
|
const ad = useAd({ code, messageId });
|
|
1347
|
-
const [linkIncluded, setLinkIncluded] = (0,
|
|
1348
|
-
const context = (0,
|
|
1328
|
+
const [linkIncluded, setLinkIncluded] = (0, import_react12.useState)(false);
|
|
1329
|
+
const context = (0, import_react12.useContext)(AdsContext);
|
|
1349
1330
|
const styles = context?.styles?.inlineAd;
|
|
1350
1331
|
useAdViewed(ad);
|
|
1351
|
-
|
|
1352
|
-
(0, import_react13.useEffect)(() => {
|
|
1332
|
+
(0, import_react12.useEffect)(() => {
|
|
1353
1333
|
if (ad?.content && !ad.isStreaming && !ad.content.match(/\[.*?\]\(.*?\)/) && !linkIncluded) {
|
|
1354
1334
|
setLinkIncluded(true);
|
|
1355
1335
|
}
|
|
1356
1336
|
}, [ad]);
|
|
1357
|
-
useTestError(context?.experimentErrorBoundary, "inlinead-content", !!ad, 2e3);
|
|
1358
1337
|
if (!ad || !ad.content || context?.isDisabled) return null;
|
|
1359
1338
|
if (ad.isLoading || ad.content?.trim().toLowerCase().includes("none"))
|
|
1360
1339
|
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 useEffect8, useState as useState8 } from "react";
|
|
10
10
|
import {
|
|
11
11
|
Image,
|
|
12
12
|
Linking as Linking3,
|
|
@@ -17,16 +17,15 @@ import {
|
|
|
17
17
|
} from "react-native";
|
|
18
18
|
|
|
19
19
|
// src/hooks/useAdViewed.tsx
|
|
20
|
-
import { useEffect as
|
|
20
|
+
import { useEffect as useEffect5, useState as useState5 } from "react";
|
|
21
21
|
|
|
22
22
|
// src/context/AdsProvider.tsx
|
|
23
|
-
import React2, { useState as
|
|
23
|
+
import React2, { useState as useState4 } from "react";
|
|
24
24
|
|
|
25
25
|
// src/hooks/useInitializeAds.tsx
|
|
26
|
-
import { useEffect
|
|
26
|
+
import { useEffect, useState } from "react";
|
|
27
27
|
|
|
28
28
|
// src/utils.ts
|
|
29
|
-
import { useEffect, useState } from "react";
|
|
30
29
|
var UNRETRIABLE_ERRORS = [403, 429];
|
|
31
30
|
var fetchWithTimeout = async (input, init) => {
|
|
32
31
|
const { timeout = 16e3 } = init || {};
|
|
@@ -133,17 +132,6 @@ function mergeStyles(propStyles, defaultStyles, overridingStyles) {
|
|
|
133
132
|
mergedStyles = deepMerge(mergedStyles, overridingStyles || {});
|
|
134
133
|
return mergedStyles;
|
|
135
134
|
}
|
|
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
|
-
};
|
|
147
135
|
var parseMessageText = (text) => {
|
|
148
136
|
const parts = [];
|
|
149
137
|
let start = 0;
|
|
@@ -263,7 +251,7 @@ var log = new Logger();
|
|
|
263
251
|
var log_default = log;
|
|
264
252
|
|
|
265
253
|
// package.json
|
|
266
|
-
var version = "0.0.7
|
|
254
|
+
var version = "0.0.7";
|
|
267
255
|
|
|
268
256
|
// src/hooks/useInitializeAds.tsx
|
|
269
257
|
var SINGLE_INIT_TIMEOUT_BUDGET_MS = 3e3;
|
|
@@ -322,19 +310,17 @@ function useInitializeAds({
|
|
|
322
310
|
conversationId,
|
|
323
311
|
isDisabled,
|
|
324
312
|
character,
|
|
325
|
-
legacyVisitorId
|
|
326
|
-
experimentErrorBoundary
|
|
313
|
+
legacyVisitorId
|
|
327
314
|
}) {
|
|
328
|
-
const [enabledPlacements, setEnabledPlacements] =
|
|
329
|
-
const [ads, setAds] =
|
|
330
|
-
const [error, setError] =
|
|
331
|
-
const [streamAdServerUrl, setStreamAdServerUrl] =
|
|
332
|
-
const [onlyStream, setOnlyStream] =
|
|
333
|
-
const [sessionId, setSessionId] =
|
|
334
|
-
const [defaultReactNativeStyles, setDefaultReactNativeStyles] =
|
|
335
|
-
const [overridingReactNativeStyles, setOverridingReactNativeStyles] =
|
|
336
|
-
|
|
337
|
-
useEffect2(() => {
|
|
315
|
+
const [enabledPlacements, setEnabledPlacements] = useState([]);
|
|
316
|
+
const [ads, setAds] = useState([]);
|
|
317
|
+
const [error, setError] = useState();
|
|
318
|
+
const [streamAdServerUrl, setStreamAdServerUrl] = useState();
|
|
319
|
+
const [onlyStream, setOnlyStream] = useState(false);
|
|
320
|
+
const [sessionId, setSessionId] = useState();
|
|
321
|
+
const [defaultReactNativeStyles, setDefaultReactNativeStyles] = useState();
|
|
322
|
+
const [overridingReactNativeStyles, setOverridingReactNativeStyles] = useState();
|
|
323
|
+
useEffect(() => {
|
|
338
324
|
log_default.setRemoteLevel("debug");
|
|
339
325
|
log_default.configureRemote(adServerUrl, {
|
|
340
326
|
publisherToken,
|
|
@@ -414,7 +400,7 @@ function useInitializeAds({
|
|
|
414
400
|
}
|
|
415
401
|
|
|
416
402
|
// src/hooks/usePreloadAds.tsx
|
|
417
|
-
import { useEffect as
|
|
403
|
+
import { useEffect as useEffect2, useState as useState2 } from "react";
|
|
418
404
|
function usePreloadAds({
|
|
419
405
|
userId,
|
|
420
406
|
sessionId,
|
|
@@ -425,10 +411,10 @@ function usePreloadAds({
|
|
|
425
411
|
character,
|
|
426
412
|
onlyStream
|
|
427
413
|
}) {
|
|
428
|
-
const [preloadDone, setPreloadDone] =
|
|
429
|
-
const [ads, setAds] =
|
|
414
|
+
const [preloadDone, setPreloadDone] = useState2(false);
|
|
415
|
+
const [ads, setAds] = useState2([]);
|
|
430
416
|
const userMessagesContent = messages.filter((m) => m.role === "user").map((m) => m.content).join(" ");
|
|
431
|
-
|
|
417
|
+
useEffect2(() => {
|
|
432
418
|
if (onlyStream) {
|
|
433
419
|
setPreloadDone(true);
|
|
434
420
|
log_default.log("[BRAIN] skipping preload ads");
|
|
@@ -477,7 +463,7 @@ function usePreloadAds({
|
|
|
477
463
|
}
|
|
478
464
|
|
|
479
465
|
// src/hooks/useStreamAds.tsx
|
|
480
|
-
import { useEffect as
|
|
466
|
+
import { useEffect as useEffect3, useState as useState3 } from "react";
|
|
481
467
|
|
|
482
468
|
// src/hooks/data-stream.ts
|
|
483
469
|
var textStreamPart = {
|
|
@@ -726,7 +712,7 @@ function useStreamAds({
|
|
|
726
712
|
character,
|
|
727
713
|
publisherToken
|
|
728
714
|
}) {
|
|
729
|
-
const [ads, setAds] =
|
|
715
|
+
const [ads, setAds] = useState3([]);
|
|
730
716
|
const fetchStream = async (messages2, code) => {
|
|
731
717
|
const lastAssistantMessage = [...messages2].reverse().find((m) => m.role === "assistant");
|
|
732
718
|
if (!lastAssistantMessage) {
|
|
@@ -821,7 +807,7 @@ function useStreamAds({
|
|
|
821
807
|
setAds((oldAds) => [...oldAds, { isError: true, code }]);
|
|
822
808
|
}
|
|
823
809
|
};
|
|
824
|
-
|
|
810
|
+
useEffect3(() => {
|
|
825
811
|
if (!isLoading && sessionId && messages.length > 2 && messages[messages.length - 1]?.role === "assistant" && preloadDone) {
|
|
826
812
|
enabledPlacements.forEach((placement) => {
|
|
827
813
|
if (["QUERY_STREAM", "INLINE_AD", "BOX_AD"].includes(placement.format)) {
|
|
@@ -893,8 +879,8 @@ var AdsProviderWithoutBoundary = ({
|
|
|
893
879
|
onAdClick,
|
|
894
880
|
...props
|
|
895
881
|
}) => {
|
|
896
|
-
const [viewedAds, setViewedAds] =
|
|
897
|
-
const [clickedAds, setClickedAds] =
|
|
882
|
+
const [viewedAds, setViewedAds] = useState4([]);
|
|
883
|
+
const [clickedAds, setClickedAds] = useState4([]);
|
|
898
884
|
const adServerUrlOrDefault = adserverUrl || "https://server.megabrain.co";
|
|
899
885
|
log_default.setLocalLevel(logLevel || "silent");
|
|
900
886
|
const {
|
|
@@ -913,8 +899,7 @@ var AdsProviderWithoutBoundary = ({
|
|
|
913
899
|
legacyVisitorId: void 0,
|
|
914
900
|
conversationId,
|
|
915
901
|
character,
|
|
916
|
-
isDisabled: !!isDisabled
|
|
917
|
-
experimentErrorBoundary: props.experimentErrorBoundary
|
|
902
|
+
isDisabled: !!isDisabled
|
|
918
903
|
});
|
|
919
904
|
const styles = mergeStyles(props.styles, defaultReactNativeStyles, overridingReactNativeStyles);
|
|
920
905
|
const isInitialised = !!isDisabled || !!error || !!sessionId;
|
|
@@ -969,7 +954,6 @@ var AdsProviderWithoutBoundary = ({
|
|
|
969
954
|
}
|
|
970
955
|
setClickedAds((old) => [...old, ad.id]);
|
|
971
956
|
};
|
|
972
|
-
useTestError(props.experimentErrorBoundary, "provider", true, 2e3);
|
|
973
957
|
return /* @__PURE__ */ jsx(
|
|
974
958
|
AdsContext.Provider,
|
|
975
959
|
{
|
|
@@ -990,8 +974,7 @@ var AdsProviderWithoutBoundary = ({
|
|
|
990
974
|
userId,
|
|
991
975
|
markAdAsViewed,
|
|
992
976
|
onlyStream,
|
|
993
|
-
styles
|
|
994
|
-
experimentErrorBoundary: props.experimentErrorBoundary
|
|
977
|
+
styles
|
|
995
978
|
},
|
|
996
979
|
children
|
|
997
980
|
}
|
|
@@ -1041,8 +1024,7 @@ var AdsProvider = ({
|
|
|
1041
1024
|
userId,
|
|
1042
1025
|
logLevel,
|
|
1043
1026
|
onAdView,
|
|
1044
|
-
onAdClick
|
|
1045
|
-
...props
|
|
1027
|
+
onAdClick
|
|
1046
1028
|
}
|
|
1047
1029
|
)
|
|
1048
1030
|
}
|
|
@@ -1053,7 +1035,7 @@ var AdsProvider = ({
|
|
|
1053
1035
|
import { useContext } from "react";
|
|
1054
1036
|
function useAdViewed(ad) {
|
|
1055
1037
|
const context = useContext(AdsContext);
|
|
1056
|
-
const [stillMounted, setStillMounted] =
|
|
1038
|
+
const [stillMounted, setStillMounted] = useState5(false);
|
|
1057
1039
|
const sendRequest = async () => {
|
|
1058
1040
|
if (!context?.adserverUrl || !ad?.id) {
|
|
1059
1041
|
return;
|
|
@@ -1075,7 +1057,7 @@ function useAdViewed(ad) {
|
|
|
1075
1057
|
log_default.warn("[BRAIN] Error sending view request", e);
|
|
1076
1058
|
}
|
|
1077
1059
|
};
|
|
1078
|
-
|
|
1060
|
+
useEffect5(() => {
|
|
1079
1061
|
if (!ad || ad.isError || ad.isLoading || ad.isStreaming || ad.viewed || stillMounted) {
|
|
1080
1062
|
return;
|
|
1081
1063
|
}
|
|
@@ -1085,7 +1067,7 @@ function useAdViewed(ad) {
|
|
|
1085
1067
|
setStillMounted(true);
|
|
1086
1068
|
}, 1e3);
|
|
1087
1069
|
}, [ad]);
|
|
1088
|
-
|
|
1070
|
+
useEffect5(() => {
|
|
1089
1071
|
if (stillMounted) {
|
|
1090
1072
|
log_default.log("[BRAIN] sending request");
|
|
1091
1073
|
sendRequest();
|
|
@@ -1107,7 +1089,6 @@ function MarkdownText({
|
|
|
1107
1089
|
const context = useContext2(AdsContext);
|
|
1108
1090
|
const textParts = parseMessageText(content);
|
|
1109
1091
|
const styles = context?.styles?.markdownText;
|
|
1110
|
-
useTestError(context?.experimentErrorBoundary, "markdowntext", true, 2e3);
|
|
1111
1092
|
const linkClickHandler = (href) => {
|
|
1112
1093
|
onLinkClick();
|
|
1113
1094
|
Linking.openURL(href).catch(
|
|
@@ -1138,17 +1119,17 @@ function MarkdownText({
|
|
|
1138
1119
|
|
|
1139
1120
|
// src/components/VideoPlayer.tsx
|
|
1140
1121
|
import { ResizeMode, Video } from "expo-av";
|
|
1141
|
-
import { useContext as useContext4, useEffect as
|
|
1122
|
+
import { useContext as useContext4, useEffect as useEffect7, useRef, useState as useState7 } from "react";
|
|
1142
1123
|
import { Linking as Linking2, Text as Text2, TouchableOpacity, View as View2 } from "react-native";
|
|
1143
1124
|
|
|
1144
1125
|
// src/components/VideoProgressBar.tsx
|
|
1145
|
-
import { useState as
|
|
1126
|
+
import { useState as useState6, useEffect as useEffect6, useContext as useContext3 } from "react";
|
|
1146
1127
|
import { View } from "react-native";
|
|
1147
1128
|
import { jsx as jsx3 } from "react/jsx-runtime";
|
|
1148
1129
|
function VideoProgressBar({ videoRef }) {
|
|
1149
|
-
const [progress, setProgress] =
|
|
1130
|
+
const [progress, setProgress] = useState6(0);
|
|
1150
1131
|
const styles = useContext3(AdsContext)?.styles?.videoPlayer?.videoProgress;
|
|
1151
|
-
|
|
1132
|
+
useEffect6(() => {
|
|
1152
1133
|
const interval = setInterval(() => {
|
|
1153
1134
|
if (!videoRef.current) {
|
|
1154
1135
|
return;
|
|
@@ -1182,13 +1163,13 @@ var VideoPlayer = ({
|
|
|
1182
1163
|
onLinkClick
|
|
1183
1164
|
}) => {
|
|
1184
1165
|
const videoRef = useRef(null);
|
|
1185
|
-
const [isPlaying, setIsPlaying] =
|
|
1186
|
-
const [isPaused, setIsPaused] =
|
|
1187
|
-
const [isEnded, setIsEnded] =
|
|
1188
|
-
const [showDownloadBadge, setShowDownloadBadge] =
|
|
1166
|
+
const [isPlaying, setIsPlaying] = useState7(false);
|
|
1167
|
+
const [isPaused, setIsPaused] = useState7(true);
|
|
1168
|
+
const [isEnded, setIsEnded] = useState7(false);
|
|
1169
|
+
const [showDownloadBadge, setShowDownloadBadge] = useState7(false);
|
|
1189
1170
|
const lastCallbackTimeRef = useRef(0);
|
|
1190
1171
|
const styles = useContext4(AdsContext)?.styles?.videoPlayer;
|
|
1191
|
-
|
|
1172
|
+
useEffect7(() => {
|
|
1192
1173
|
const interval = setInterval(() => {
|
|
1193
1174
|
if (videoRef.current) {
|
|
1194
1175
|
videoRef.current.getStatusAsync().then((status) => {
|
|
@@ -1321,17 +1302,15 @@ function useAd({ code, messageId }) {
|
|
|
1321
1302
|
import { jsx as jsx5, jsxs as jsxs2 } from "react/jsx-runtime";
|
|
1322
1303
|
var InlineAd = ({ code, messageId, wrapper }) => {
|
|
1323
1304
|
const ad = useAd({ code, messageId });
|
|
1324
|
-
const [linkIncluded, setLinkIncluded] =
|
|
1305
|
+
const [linkIncluded, setLinkIncluded] = useState8(false);
|
|
1325
1306
|
const context = useContext6(AdsContext);
|
|
1326
1307
|
const styles = context?.styles?.inlineAd;
|
|
1327
1308
|
useAdViewed(ad);
|
|
1328
|
-
|
|
1329
|
-
useEffect9(() => {
|
|
1309
|
+
useEffect8(() => {
|
|
1330
1310
|
if (ad?.content && !ad.isStreaming && !ad.content.match(/\[.*?\]\(.*?\)/) && !linkIncluded) {
|
|
1331
1311
|
setLinkIncluded(true);
|
|
1332
1312
|
}
|
|
1333
1313
|
}, [ad]);
|
|
1334
|
-
useTestError(context?.experimentErrorBoundary, "inlinead-content", !!ad, 2e3);
|
|
1335
1314
|
if (!ad || !ad.content || context?.isDisabled) return null;
|
|
1336
1315
|
if (ad.isLoading || ad.content?.trim().toLowerCase().includes("none"))
|
|
1337
1316
|
return null;
|