@kontextso/sdk-react-native 0.0.6 → 0.0.7-rc.1
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 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +130 -46
- package/dist/index.mjs +130 -46
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -199,17 +199,91 @@ var parseMessageText = (text) => {
|
|
|
199
199
|
return parts;
|
|
200
200
|
};
|
|
201
201
|
|
|
202
|
-
// src/
|
|
203
|
-
var
|
|
202
|
+
// src/log.ts
|
|
203
|
+
var Logger = class {
|
|
204
|
+
localLevel = "log";
|
|
205
|
+
remoteLevel = "error";
|
|
206
|
+
remoteConfig = null;
|
|
207
|
+
levels = {
|
|
208
|
+
debug: 0,
|
|
209
|
+
info: 1,
|
|
210
|
+
log: 2,
|
|
211
|
+
warn: 3,
|
|
212
|
+
error: 4,
|
|
213
|
+
silent: 5
|
|
214
|
+
};
|
|
215
|
+
setLocalLevel(level) {
|
|
216
|
+
this.localLevel = level;
|
|
217
|
+
}
|
|
218
|
+
setRemoteLevel(level) {
|
|
219
|
+
this.remoteLevel = level;
|
|
220
|
+
}
|
|
221
|
+
configureRemote(url, params) {
|
|
222
|
+
this.remoteConfig = { url, params };
|
|
223
|
+
}
|
|
224
|
+
shouldLog(level, targetLevel) {
|
|
225
|
+
if (targetLevel === "silent") {
|
|
226
|
+
return false;
|
|
227
|
+
}
|
|
228
|
+
return this.levels[level] >= this.levels[targetLevel];
|
|
229
|
+
}
|
|
230
|
+
logToConsole(level, ...args) {
|
|
231
|
+
if (this.shouldLog(level, this.localLevel)) {
|
|
232
|
+
if (level === "silent") {
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
console[level](...args);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
logToRemote(level, ...args) {
|
|
239
|
+
if (this.remoteConfig && this.shouldLog(level, this.remoteLevel)) {
|
|
240
|
+
fetch(
|
|
241
|
+
`${this.remoteConfig.url}/log`,
|
|
242
|
+
{
|
|
243
|
+
method: "POST",
|
|
244
|
+
body: JSON.stringify({
|
|
245
|
+
...this.remoteConfig.params,
|
|
246
|
+
level,
|
|
247
|
+
message: args,
|
|
248
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
249
|
+
})
|
|
250
|
+
}
|
|
251
|
+
).catch((e) => {
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
debug(...args) {
|
|
256
|
+
this.logToConsole("debug", ...args);
|
|
257
|
+
this.logToRemote("debug", ...args);
|
|
258
|
+
}
|
|
259
|
+
info(...args) {
|
|
260
|
+
this.logToConsole("info", ...args);
|
|
261
|
+
this.logToRemote("info", ...args);
|
|
262
|
+
}
|
|
263
|
+
log(...args) {
|
|
264
|
+
this.logToConsole("log", ...args);
|
|
265
|
+
this.logToRemote("log", ...args);
|
|
266
|
+
}
|
|
267
|
+
warn(...args) {
|
|
268
|
+
this.logToConsole("warn", ...args);
|
|
269
|
+
this.logToRemote("warn", ...args);
|
|
270
|
+
}
|
|
271
|
+
error(...args) {
|
|
272
|
+
this.logToConsole("error", ...args);
|
|
273
|
+
this.logToRemote("error", ...args);
|
|
274
|
+
}
|
|
275
|
+
};
|
|
276
|
+
var log = new Logger();
|
|
277
|
+
var log_default = log;
|
|
204
278
|
|
|
205
279
|
// package.json
|
|
206
|
-
var version = "0.0.
|
|
280
|
+
var version = "0.0.7-rc.1";
|
|
207
281
|
|
|
208
282
|
// src/hooks/useInitializeAds.tsx
|
|
209
283
|
var SINGLE_INIT_TIMEOUT_BUDGET_MS = 3e3;
|
|
210
284
|
var SINGLE_INIT_RETRIES = 3;
|
|
211
285
|
async function initialize(adServerUrl, publisherToken, userId, conversationId, legacyVisitorId, character) {
|
|
212
|
-
|
|
286
|
+
log_default.log("[BRAIN] init ads started");
|
|
213
287
|
const response = await fetchRetry(
|
|
214
288
|
`${adServerUrl}/init`,
|
|
215
289
|
{
|
|
@@ -236,12 +310,13 @@ async function initialize(adServerUrl, publisherToken, userId, conversationId, l
|
|
|
236
310
|
streamAdServer,
|
|
237
311
|
onlyStream,
|
|
238
312
|
defaultReactNativeStyles,
|
|
239
|
-
overridingReactNativeStyles
|
|
313
|
+
overridingReactNativeStyles,
|
|
314
|
+
remoteLogLevel
|
|
240
315
|
} = await response.json();
|
|
241
316
|
const fixedAds = ads.map((ad) => {
|
|
242
317
|
return fixUrl(adServerUrl, ad);
|
|
243
318
|
});
|
|
244
|
-
|
|
319
|
+
log_default.log("[BRAIN] init ads done");
|
|
245
320
|
return {
|
|
246
321
|
sessionDisabled,
|
|
247
322
|
sessionId,
|
|
@@ -250,7 +325,8 @@ async function initialize(adServerUrl, publisherToken, userId, conversationId, l
|
|
|
250
325
|
streamAdServer,
|
|
251
326
|
onlyStream,
|
|
252
327
|
defaultReactNativeStylesResponse: defaultReactNativeStyles,
|
|
253
|
-
overridingReactNativeStylesResponse: overridingReactNativeStyles
|
|
328
|
+
overridingReactNativeStylesResponse: overridingReactNativeStyles,
|
|
329
|
+
remoteLogLevel
|
|
254
330
|
};
|
|
255
331
|
}
|
|
256
332
|
function useInitializeAds({
|
|
@@ -271,9 +347,17 @@ function useInitializeAds({
|
|
|
271
347
|
const [defaultReactNativeStyles, setDefaultReactNativeStyles] = (0, import_react.useState)();
|
|
272
348
|
const [overridingReactNativeStyles, setOverridingReactNativeStyles] = (0, import_react.useState)();
|
|
273
349
|
(0, import_react.useEffect)(() => {
|
|
350
|
+
log_default.setRemoteLevel("debug");
|
|
351
|
+
log_default.configureRemote(adServerUrl, {
|
|
352
|
+
publisherToken,
|
|
353
|
+
userId,
|
|
354
|
+
visitorId: userId,
|
|
355
|
+
conversationId,
|
|
356
|
+
character
|
|
357
|
+
});
|
|
274
358
|
setSessionId(void 0);
|
|
275
359
|
if (!isDisabled && userId) {
|
|
276
|
-
|
|
360
|
+
log_default.debug("[BRAIN] Initializing ads.");
|
|
277
361
|
initialize(
|
|
278
362
|
adServerUrl,
|
|
279
363
|
publisherToken,
|
|
@@ -290,8 +374,19 @@ function useInitializeAds({
|
|
|
290
374
|
streamAdServer,
|
|
291
375
|
onlyStream: onlyStream2,
|
|
292
376
|
defaultReactNativeStylesResponse,
|
|
293
|
-
overridingReactNativeStylesResponse
|
|
377
|
+
overridingReactNativeStylesResponse,
|
|
378
|
+
remoteLogLevel
|
|
294
379
|
}) => {
|
|
380
|
+
log_default.setRemoteLevel(remoteLogLevel || "silent");
|
|
381
|
+
log_default.configureRemote(adServerUrl, {
|
|
382
|
+
publisherToken,
|
|
383
|
+
adServerUrl,
|
|
384
|
+
userId,
|
|
385
|
+
conversationId,
|
|
386
|
+
legacyVisitorId,
|
|
387
|
+
character,
|
|
388
|
+
sessionId: sessionId2
|
|
389
|
+
});
|
|
295
390
|
if (!sessionDisabled) {
|
|
296
391
|
setSessionId(sessionId2);
|
|
297
392
|
setEnabledPlacements(enabledPlacements2);
|
|
@@ -301,15 +396,15 @@ function useInitializeAds({
|
|
|
301
396
|
setDefaultReactNativeStyles(defaultReactNativeStylesResponse);
|
|
302
397
|
setOverridingReactNativeStyles(overridingReactNativeStylesResponse);
|
|
303
398
|
} else {
|
|
304
|
-
|
|
399
|
+
log_default.debug("[BRAIN] Session is disabled by server.");
|
|
305
400
|
}
|
|
306
401
|
}
|
|
307
402
|
).catch((e) => {
|
|
308
|
-
|
|
403
|
+
log_default.warn("[BRAIN] Error initializing ads", e);
|
|
309
404
|
setError(e.message);
|
|
310
405
|
});
|
|
311
406
|
} else {
|
|
312
|
-
|
|
407
|
+
log_default.debug("[BRAIN] Ads are disabled.");
|
|
313
408
|
}
|
|
314
409
|
}, [
|
|
315
410
|
adServerUrl,
|
|
@@ -332,7 +427,6 @@ function useInitializeAds({
|
|
|
332
427
|
|
|
333
428
|
// src/hooks/usePreloadAds.tsx
|
|
334
429
|
var import_react2 = require("react");
|
|
335
|
-
var import_loglevel2 = __toESM(require("loglevel"));
|
|
336
430
|
function usePreloadAds({
|
|
337
431
|
userId,
|
|
338
432
|
sessionId,
|
|
@@ -349,7 +443,7 @@ function usePreloadAds({
|
|
|
349
443
|
(0, import_react2.useEffect)(() => {
|
|
350
444
|
if (onlyStream) {
|
|
351
445
|
setPreloadDone(true);
|
|
352
|
-
|
|
446
|
+
log_default.log("[BRAIN] skipping preload ads");
|
|
353
447
|
return;
|
|
354
448
|
}
|
|
355
449
|
async function preload() {
|
|
@@ -357,7 +451,7 @@ function usePreloadAds({
|
|
|
357
451
|
if (!sessionId) {
|
|
358
452
|
return;
|
|
359
453
|
}
|
|
360
|
-
|
|
454
|
+
log_default.log("[BRAIN] preload ads started");
|
|
361
455
|
try {
|
|
362
456
|
const response = await fetchRetry(
|
|
363
457
|
`${adserverUrl}/preload`,
|
|
@@ -384,9 +478,9 @@ function usePreloadAds({
|
|
|
384
478
|
return [...oldAds, ...newAds];
|
|
385
479
|
});
|
|
386
480
|
setPreloadDone(true);
|
|
387
|
-
|
|
481
|
+
log_default.log("[BRAIN] preload ads finished");
|
|
388
482
|
} catch (e) {
|
|
389
|
-
|
|
483
|
+
log_default.warn("[BRAIN] Error preloading ads", e);
|
|
390
484
|
}
|
|
391
485
|
}
|
|
392
486
|
preload();
|
|
@@ -619,7 +713,6 @@ async function* readDataStream(reader, {
|
|
|
619
713
|
var import_encoding = require("react-native-polyfill-globals/src/encoding");
|
|
620
714
|
var import_readable_stream = require("react-native-polyfill-globals/src/readable-stream");
|
|
621
715
|
var import_react_native = require("react-native");
|
|
622
|
-
var import_loglevel3 = __toESM(require("loglevel"));
|
|
623
716
|
var patchFetch = fetch;
|
|
624
717
|
if (import_react_native.Platform.OS !== "web") {
|
|
625
718
|
(0, import_encoding.polyfill)();
|
|
@@ -628,9 +721,9 @@ if (import_react_native.Platform.OS !== "web") {
|
|
|
628
721
|
}
|
|
629
722
|
ErrorUtils.setGlobalHandler((error, isFatal) => {
|
|
630
723
|
if (!isFatal) {
|
|
631
|
-
|
|
724
|
+
log_default.warn(error);
|
|
632
725
|
} else {
|
|
633
|
-
|
|
726
|
+
log_default.error(error);
|
|
634
727
|
}
|
|
635
728
|
});
|
|
636
729
|
function useStreamAds({
|
|
@@ -691,7 +784,7 @@ function useStreamAds({
|
|
|
691
784
|
);
|
|
692
785
|
let data = "";
|
|
693
786
|
let adData = {};
|
|
694
|
-
|
|
787
|
+
log_default.log(`[BRAIN] streaming ${code} ad started`);
|
|
695
788
|
const reader = response.body.getReader();
|
|
696
789
|
for await (const { type, value } of readDataStream(reader)) {
|
|
697
790
|
switch (type) {
|
|
@@ -734,9 +827,9 @@ function useStreamAds({
|
|
|
734
827
|
(ad) => ad.messageId === lastAssistantMessage.id && ad.code === code ? { ...ad, isStreaming: false } : ad
|
|
735
828
|
)
|
|
736
829
|
);
|
|
737
|
-
|
|
830
|
+
log_default.log(`[BRAIN] streaming ${code} ad done`);
|
|
738
831
|
} catch (e) {
|
|
739
|
-
|
|
832
|
+
log_default.warn("[BRAIN] Error streaming ad", e);
|
|
740
833
|
setAds((oldAds) => [...oldAds, { isError: true, code }]);
|
|
741
834
|
}
|
|
742
835
|
};
|
|
@@ -752,12 +845,8 @@ function useStreamAds({
|
|
|
752
845
|
return { ads };
|
|
753
846
|
}
|
|
754
847
|
|
|
755
|
-
// src/context/AdsProvider.tsx
|
|
756
|
-
var import_loglevel5 = __toESM(require("loglevel"));
|
|
757
|
-
|
|
758
848
|
// src/components/ErrorBoundary.tsx
|
|
759
849
|
var import_react4 = __toESM(require("react"));
|
|
760
|
-
var import_loglevel4 = __toESM(require("loglevel"));
|
|
761
850
|
var captureErrorFn = (adServerUrl, error, componentStack, context) => {
|
|
762
851
|
fetch(`${adServerUrl}/error`, {
|
|
763
852
|
method: "POST",
|
|
@@ -767,7 +856,7 @@ var captureErrorFn = (adServerUrl, error, componentStack, context) => {
|
|
|
767
856
|
context
|
|
768
857
|
})
|
|
769
858
|
}).catch((e) => {
|
|
770
|
-
|
|
859
|
+
log_default.warn("Error reporting client error", e);
|
|
771
860
|
});
|
|
772
861
|
};
|
|
773
862
|
var ErrorBoundary = class extends import_react4.default.Component {
|
|
@@ -819,7 +908,7 @@ var AdsProviderWithoutBoundary = ({
|
|
|
819
908
|
const [viewedAds, setViewedAds] = (0, import_react5.useState)([]);
|
|
820
909
|
const [clickedAds, setClickedAds] = (0, import_react5.useState)([]);
|
|
821
910
|
const adServerUrlOrDefault = adserverUrl || "https://server.megabrain.co";
|
|
822
|
-
|
|
911
|
+
log_default.setLocalLevel(logLevel || "silent");
|
|
823
912
|
const {
|
|
824
913
|
ads: initAds,
|
|
825
914
|
sessionId,
|
|
@@ -864,7 +953,7 @@ var AdsProviderWithoutBoundary = ({
|
|
|
864
953
|
});
|
|
865
954
|
const ads = mergeAds({ initAds, preloadAds, streamAds, viewedAds, clickedAds });
|
|
866
955
|
const markAdAsViewed = (ad) => {
|
|
867
|
-
|
|
956
|
+
log_default.debug("[Brain] Calling onAdView");
|
|
868
957
|
onAdView && onAdView({
|
|
869
958
|
id: ad.id,
|
|
870
959
|
code: ad.code,
|
|
@@ -876,7 +965,7 @@ var AdsProviderWithoutBoundary = ({
|
|
|
876
965
|
setViewedAds((old) => [...old, ad.id]);
|
|
877
966
|
};
|
|
878
967
|
const onAdClickInternal = (ad) => {
|
|
879
|
-
|
|
968
|
+
log_default.debug("[Brain] Calling onAdClick");
|
|
880
969
|
onAdClick && onAdClick({
|
|
881
970
|
id: ad.id,
|
|
882
971
|
code: ad.code,
|
|
@@ -904,6 +993,7 @@ var AdsProviderWithoutBoundary = ({
|
|
|
904
993
|
ads,
|
|
905
994
|
sessionId,
|
|
906
995
|
isInitialised,
|
|
996
|
+
conversationId,
|
|
907
997
|
isDisabled,
|
|
908
998
|
onAdClickInternal,
|
|
909
999
|
enabledPlacements,
|
|
@@ -969,7 +1059,6 @@ var AdsProvider = ({
|
|
|
969
1059
|
|
|
970
1060
|
// src/hooks/useAdViewed.tsx
|
|
971
1061
|
var import_react7 = require("react");
|
|
972
|
-
var import_loglevel6 = __toESM(require("loglevel"));
|
|
973
1062
|
function useAdViewed(ad) {
|
|
974
1063
|
const context = (0, import_react7.useContext)(AdsContext);
|
|
975
1064
|
const [stillMounted, setStillMounted] = (0, import_react6.useState)(false);
|
|
@@ -989,34 +1078,30 @@ function useAdViewed(ad) {
|
|
|
989
1078
|
if (!response.ok) {
|
|
990
1079
|
throw new Error("Error sending view request");
|
|
991
1080
|
}
|
|
992
|
-
|
|
1081
|
+
log_default.log("[BRAIN] ad marked as viewed", ad.id, ad.code);
|
|
993
1082
|
} catch (e) {
|
|
994
|
-
|
|
1083
|
+
log_default.warn("[BRAIN] Error sending view request", e);
|
|
995
1084
|
}
|
|
996
1085
|
};
|
|
997
1086
|
(0, import_react6.useEffect)(() => {
|
|
998
1087
|
if (!ad || ad.isError || ad.isLoading || ad.isStreaming || ad.viewed || stillMounted) {
|
|
999
1088
|
return;
|
|
1000
1089
|
}
|
|
1001
|
-
|
|
1090
|
+
log_default.log("[BRAIN] setting timeout", ad.id, ad.code);
|
|
1002
1091
|
setTimeout(() => {
|
|
1003
|
-
|
|
1092
|
+
log_default.log("[BRAIN] setting setStillMounted", ad.id, ad.code);
|
|
1004
1093
|
setStillMounted(true);
|
|
1005
1094
|
}, 1e3);
|
|
1006
1095
|
}, [ad]);
|
|
1007
1096
|
(0, import_react6.useEffect)(() => {
|
|
1008
1097
|
if (stillMounted) {
|
|
1009
|
-
|
|
1098
|
+
log_default.log("[BRAIN] sending request");
|
|
1010
1099
|
sendRequest();
|
|
1011
1100
|
}
|
|
1012
1101
|
}, [stillMounted]);
|
|
1013
1102
|
}
|
|
1014
1103
|
|
|
1015
|
-
// src/formats/InlineAd.tsx
|
|
1016
|
-
var import_loglevel9 = __toESM(require("loglevel"));
|
|
1017
|
-
|
|
1018
1104
|
// src/components/MarkdownText.tsx
|
|
1019
|
-
var import_loglevel7 = __toESM(require("loglevel"));
|
|
1020
1105
|
var import_react8 = require("react");
|
|
1021
1106
|
var import_react_native2 = require("react-native");
|
|
1022
1107
|
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
@@ -1030,7 +1115,7 @@ function MarkdownText({
|
|
|
1030
1115
|
const linkClickHandler = (href) => {
|
|
1031
1116
|
onLinkClick();
|
|
1032
1117
|
import_react_native2.Linking.openURL(href).catch(
|
|
1033
|
-
(err) =>
|
|
1118
|
+
(err) => log_default.warn("Failed to open URL:", err)
|
|
1034
1119
|
);
|
|
1035
1120
|
return false;
|
|
1036
1121
|
};
|
|
@@ -1057,7 +1142,6 @@ function MarkdownText({
|
|
|
1057
1142
|
|
|
1058
1143
|
// src/components/VideoPlayer.tsx
|
|
1059
1144
|
var import_expo_av = require("expo-av");
|
|
1060
|
-
var import_loglevel8 = __toESM(require("loglevel"));
|
|
1061
1145
|
var import_react10 = require("react");
|
|
1062
1146
|
var import_react_native4 = require("react-native");
|
|
1063
1147
|
|
|
@@ -1152,7 +1236,7 @@ var VideoPlayer = ({
|
|
|
1152
1236
|
};
|
|
1153
1237
|
const handleLinkClick = (url) => {
|
|
1154
1238
|
if (url) {
|
|
1155
|
-
import_react_native4.Linking.openURL(url).catch((err) =>
|
|
1239
|
+
import_react_native4.Linking.openURL(url).catch((err) => log_default.warn("Failed to open URL:", err));
|
|
1156
1240
|
onLinkClick();
|
|
1157
1241
|
}
|
|
1158
1242
|
};
|
|
@@ -1254,7 +1338,7 @@ var InlineAd = ({ code, messageId, wrapper }) => {
|
|
|
1254
1338
|
if (ad.isLoading || ad.content?.trim().toLowerCase().includes("none"))
|
|
1255
1339
|
return null;
|
|
1256
1340
|
const onProgress = (progress) => {
|
|
1257
|
-
|
|
1341
|
+
log_default.log(`Progress: ${progress}`);
|
|
1258
1342
|
};
|
|
1259
1343
|
const handleImageClick = (url) => {
|
|
1260
1344
|
if (url) {
|
|
@@ -1263,7 +1347,7 @@ var InlineAd = ({ code, messageId, wrapper }) => {
|
|
|
1263
1347
|
window.open(url, "_blank");
|
|
1264
1348
|
} else {
|
|
1265
1349
|
import_react_native5.Linking.openURL(url).catch(
|
|
1266
|
-
(err) =>
|
|
1350
|
+
(err) => log_default.warn("Failed to open URL:", err)
|
|
1267
1351
|
);
|
|
1268
1352
|
}
|
|
1269
1353
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -173,17 +173,91 @@ var parseMessageText = (text) => {
|
|
|
173
173
|
return parts;
|
|
174
174
|
};
|
|
175
175
|
|
|
176
|
-
// src/
|
|
177
|
-
|
|
176
|
+
// src/log.ts
|
|
177
|
+
var Logger = class {
|
|
178
|
+
localLevel = "log";
|
|
179
|
+
remoteLevel = "error";
|
|
180
|
+
remoteConfig = null;
|
|
181
|
+
levels = {
|
|
182
|
+
debug: 0,
|
|
183
|
+
info: 1,
|
|
184
|
+
log: 2,
|
|
185
|
+
warn: 3,
|
|
186
|
+
error: 4,
|
|
187
|
+
silent: 5
|
|
188
|
+
};
|
|
189
|
+
setLocalLevel(level) {
|
|
190
|
+
this.localLevel = level;
|
|
191
|
+
}
|
|
192
|
+
setRemoteLevel(level) {
|
|
193
|
+
this.remoteLevel = level;
|
|
194
|
+
}
|
|
195
|
+
configureRemote(url, params) {
|
|
196
|
+
this.remoteConfig = { url, params };
|
|
197
|
+
}
|
|
198
|
+
shouldLog(level, targetLevel) {
|
|
199
|
+
if (targetLevel === "silent") {
|
|
200
|
+
return false;
|
|
201
|
+
}
|
|
202
|
+
return this.levels[level] >= this.levels[targetLevel];
|
|
203
|
+
}
|
|
204
|
+
logToConsole(level, ...args) {
|
|
205
|
+
if (this.shouldLog(level, this.localLevel)) {
|
|
206
|
+
if (level === "silent") {
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
console[level](...args);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
logToRemote(level, ...args) {
|
|
213
|
+
if (this.remoteConfig && this.shouldLog(level, this.remoteLevel)) {
|
|
214
|
+
fetch(
|
|
215
|
+
`${this.remoteConfig.url}/log`,
|
|
216
|
+
{
|
|
217
|
+
method: "POST",
|
|
218
|
+
body: JSON.stringify({
|
|
219
|
+
...this.remoteConfig.params,
|
|
220
|
+
level,
|
|
221
|
+
message: args,
|
|
222
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
223
|
+
})
|
|
224
|
+
}
|
|
225
|
+
).catch((e) => {
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
debug(...args) {
|
|
230
|
+
this.logToConsole("debug", ...args);
|
|
231
|
+
this.logToRemote("debug", ...args);
|
|
232
|
+
}
|
|
233
|
+
info(...args) {
|
|
234
|
+
this.logToConsole("info", ...args);
|
|
235
|
+
this.logToRemote("info", ...args);
|
|
236
|
+
}
|
|
237
|
+
log(...args) {
|
|
238
|
+
this.logToConsole("log", ...args);
|
|
239
|
+
this.logToRemote("log", ...args);
|
|
240
|
+
}
|
|
241
|
+
warn(...args) {
|
|
242
|
+
this.logToConsole("warn", ...args);
|
|
243
|
+
this.logToRemote("warn", ...args);
|
|
244
|
+
}
|
|
245
|
+
error(...args) {
|
|
246
|
+
this.logToConsole("error", ...args);
|
|
247
|
+
this.logToRemote("error", ...args);
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
var log = new Logger();
|
|
251
|
+
var log_default = log;
|
|
178
252
|
|
|
179
253
|
// package.json
|
|
180
|
-
var version = "0.0.
|
|
254
|
+
var version = "0.0.7-rc.1";
|
|
181
255
|
|
|
182
256
|
// src/hooks/useInitializeAds.tsx
|
|
183
257
|
var SINGLE_INIT_TIMEOUT_BUDGET_MS = 3e3;
|
|
184
258
|
var SINGLE_INIT_RETRIES = 3;
|
|
185
259
|
async function initialize(adServerUrl, publisherToken, userId, conversationId, legacyVisitorId, character) {
|
|
186
|
-
|
|
260
|
+
log_default.log("[BRAIN] init ads started");
|
|
187
261
|
const response = await fetchRetry(
|
|
188
262
|
`${adServerUrl}/init`,
|
|
189
263
|
{
|
|
@@ -210,12 +284,13 @@ async function initialize(adServerUrl, publisherToken, userId, conversationId, l
|
|
|
210
284
|
streamAdServer,
|
|
211
285
|
onlyStream,
|
|
212
286
|
defaultReactNativeStyles,
|
|
213
|
-
overridingReactNativeStyles
|
|
287
|
+
overridingReactNativeStyles,
|
|
288
|
+
remoteLogLevel
|
|
214
289
|
} = await response.json();
|
|
215
290
|
const fixedAds = ads.map((ad) => {
|
|
216
291
|
return fixUrl(adServerUrl, ad);
|
|
217
292
|
});
|
|
218
|
-
|
|
293
|
+
log_default.log("[BRAIN] init ads done");
|
|
219
294
|
return {
|
|
220
295
|
sessionDisabled,
|
|
221
296
|
sessionId,
|
|
@@ -224,7 +299,8 @@ async function initialize(adServerUrl, publisherToken, userId, conversationId, l
|
|
|
224
299
|
streamAdServer,
|
|
225
300
|
onlyStream,
|
|
226
301
|
defaultReactNativeStylesResponse: defaultReactNativeStyles,
|
|
227
|
-
overridingReactNativeStylesResponse: overridingReactNativeStyles
|
|
302
|
+
overridingReactNativeStylesResponse: overridingReactNativeStyles,
|
|
303
|
+
remoteLogLevel
|
|
228
304
|
};
|
|
229
305
|
}
|
|
230
306
|
function useInitializeAds({
|
|
@@ -245,9 +321,17 @@ function useInitializeAds({
|
|
|
245
321
|
const [defaultReactNativeStyles, setDefaultReactNativeStyles] = useState();
|
|
246
322
|
const [overridingReactNativeStyles, setOverridingReactNativeStyles] = useState();
|
|
247
323
|
useEffect(() => {
|
|
324
|
+
log_default.setRemoteLevel("debug");
|
|
325
|
+
log_default.configureRemote(adServerUrl, {
|
|
326
|
+
publisherToken,
|
|
327
|
+
userId,
|
|
328
|
+
visitorId: userId,
|
|
329
|
+
conversationId,
|
|
330
|
+
character
|
|
331
|
+
});
|
|
248
332
|
setSessionId(void 0);
|
|
249
333
|
if (!isDisabled && userId) {
|
|
250
|
-
|
|
334
|
+
log_default.debug("[BRAIN] Initializing ads.");
|
|
251
335
|
initialize(
|
|
252
336
|
adServerUrl,
|
|
253
337
|
publisherToken,
|
|
@@ -264,8 +348,19 @@ function useInitializeAds({
|
|
|
264
348
|
streamAdServer,
|
|
265
349
|
onlyStream: onlyStream2,
|
|
266
350
|
defaultReactNativeStylesResponse,
|
|
267
|
-
overridingReactNativeStylesResponse
|
|
351
|
+
overridingReactNativeStylesResponse,
|
|
352
|
+
remoteLogLevel
|
|
268
353
|
}) => {
|
|
354
|
+
log_default.setRemoteLevel(remoteLogLevel || "silent");
|
|
355
|
+
log_default.configureRemote(adServerUrl, {
|
|
356
|
+
publisherToken,
|
|
357
|
+
adServerUrl,
|
|
358
|
+
userId,
|
|
359
|
+
conversationId,
|
|
360
|
+
legacyVisitorId,
|
|
361
|
+
character,
|
|
362
|
+
sessionId: sessionId2
|
|
363
|
+
});
|
|
269
364
|
if (!sessionDisabled) {
|
|
270
365
|
setSessionId(sessionId2);
|
|
271
366
|
setEnabledPlacements(enabledPlacements2);
|
|
@@ -275,15 +370,15 @@ function useInitializeAds({
|
|
|
275
370
|
setDefaultReactNativeStyles(defaultReactNativeStylesResponse);
|
|
276
371
|
setOverridingReactNativeStyles(overridingReactNativeStylesResponse);
|
|
277
372
|
} else {
|
|
278
|
-
|
|
373
|
+
log_default.debug("[BRAIN] Session is disabled by server.");
|
|
279
374
|
}
|
|
280
375
|
}
|
|
281
376
|
).catch((e) => {
|
|
282
|
-
|
|
377
|
+
log_default.warn("[BRAIN] Error initializing ads", e);
|
|
283
378
|
setError(e.message);
|
|
284
379
|
});
|
|
285
380
|
} else {
|
|
286
|
-
|
|
381
|
+
log_default.debug("[BRAIN] Ads are disabled.");
|
|
287
382
|
}
|
|
288
383
|
}, [
|
|
289
384
|
adServerUrl,
|
|
@@ -306,7 +401,6 @@ function useInitializeAds({
|
|
|
306
401
|
|
|
307
402
|
// src/hooks/usePreloadAds.tsx
|
|
308
403
|
import { useEffect as useEffect2, useState as useState2 } from "react";
|
|
309
|
-
import log2 from "loglevel";
|
|
310
404
|
function usePreloadAds({
|
|
311
405
|
userId,
|
|
312
406
|
sessionId,
|
|
@@ -323,7 +417,7 @@ function usePreloadAds({
|
|
|
323
417
|
useEffect2(() => {
|
|
324
418
|
if (onlyStream) {
|
|
325
419
|
setPreloadDone(true);
|
|
326
|
-
|
|
420
|
+
log_default.log("[BRAIN] skipping preload ads");
|
|
327
421
|
return;
|
|
328
422
|
}
|
|
329
423
|
async function preload() {
|
|
@@ -331,7 +425,7 @@ function usePreloadAds({
|
|
|
331
425
|
if (!sessionId) {
|
|
332
426
|
return;
|
|
333
427
|
}
|
|
334
|
-
|
|
428
|
+
log_default.log("[BRAIN] preload ads started");
|
|
335
429
|
try {
|
|
336
430
|
const response = await fetchRetry(
|
|
337
431
|
`${adserverUrl}/preload`,
|
|
@@ -358,9 +452,9 @@ function usePreloadAds({
|
|
|
358
452
|
return [...oldAds, ...newAds];
|
|
359
453
|
});
|
|
360
454
|
setPreloadDone(true);
|
|
361
|
-
|
|
455
|
+
log_default.log("[BRAIN] preload ads finished");
|
|
362
456
|
} catch (e) {
|
|
363
|
-
|
|
457
|
+
log_default.warn("[BRAIN] Error preloading ads", e);
|
|
364
458
|
}
|
|
365
459
|
}
|
|
366
460
|
preload();
|
|
@@ -593,7 +687,6 @@ async function* readDataStream(reader, {
|
|
|
593
687
|
import { polyfill as polyfillEncoding } from "react-native-polyfill-globals/src/encoding";
|
|
594
688
|
import { polyfill as polyfillReadableStream } from "react-native-polyfill-globals/src/readable-stream";
|
|
595
689
|
import { Platform } from "react-native";
|
|
596
|
-
import log3 from "loglevel";
|
|
597
690
|
var patchFetch = fetch;
|
|
598
691
|
if (Platform.OS !== "web") {
|
|
599
692
|
polyfillEncoding();
|
|
@@ -602,9 +695,9 @@ if (Platform.OS !== "web") {
|
|
|
602
695
|
}
|
|
603
696
|
ErrorUtils.setGlobalHandler((error, isFatal) => {
|
|
604
697
|
if (!isFatal) {
|
|
605
|
-
|
|
698
|
+
log_default.warn(error);
|
|
606
699
|
} else {
|
|
607
|
-
|
|
700
|
+
log_default.error(error);
|
|
608
701
|
}
|
|
609
702
|
});
|
|
610
703
|
function useStreamAds({
|
|
@@ -665,7 +758,7 @@ function useStreamAds({
|
|
|
665
758
|
);
|
|
666
759
|
let data = "";
|
|
667
760
|
let adData = {};
|
|
668
|
-
|
|
761
|
+
log_default.log(`[BRAIN] streaming ${code} ad started`);
|
|
669
762
|
const reader = response.body.getReader();
|
|
670
763
|
for await (const { type, value } of readDataStream(reader)) {
|
|
671
764
|
switch (type) {
|
|
@@ -708,9 +801,9 @@ function useStreamAds({
|
|
|
708
801
|
(ad) => ad.messageId === lastAssistantMessage.id && ad.code === code ? { ...ad, isStreaming: false } : ad
|
|
709
802
|
)
|
|
710
803
|
);
|
|
711
|
-
|
|
804
|
+
log_default.log(`[BRAIN] streaming ${code} ad done`);
|
|
712
805
|
} catch (e) {
|
|
713
|
-
|
|
806
|
+
log_default.warn("[BRAIN] Error streaming ad", e);
|
|
714
807
|
setAds((oldAds) => [...oldAds, { isError: true, code }]);
|
|
715
808
|
}
|
|
716
809
|
};
|
|
@@ -726,12 +819,8 @@ function useStreamAds({
|
|
|
726
819
|
return { ads };
|
|
727
820
|
}
|
|
728
821
|
|
|
729
|
-
// src/context/AdsProvider.tsx
|
|
730
|
-
import log5 from "loglevel";
|
|
731
|
-
|
|
732
822
|
// src/components/ErrorBoundary.tsx
|
|
733
823
|
import React from "react";
|
|
734
|
-
import log4 from "loglevel";
|
|
735
824
|
var captureErrorFn = (adServerUrl, error, componentStack, context) => {
|
|
736
825
|
fetch(`${adServerUrl}/error`, {
|
|
737
826
|
method: "POST",
|
|
@@ -741,7 +830,7 @@ var captureErrorFn = (adServerUrl, error, componentStack, context) => {
|
|
|
741
830
|
context
|
|
742
831
|
})
|
|
743
832
|
}).catch((e) => {
|
|
744
|
-
|
|
833
|
+
log_default.warn("Error reporting client error", e);
|
|
745
834
|
});
|
|
746
835
|
};
|
|
747
836
|
var ErrorBoundary = class extends React.Component {
|
|
@@ -793,7 +882,7 @@ var AdsProviderWithoutBoundary = ({
|
|
|
793
882
|
const [viewedAds, setViewedAds] = useState4([]);
|
|
794
883
|
const [clickedAds, setClickedAds] = useState4([]);
|
|
795
884
|
const adServerUrlOrDefault = adserverUrl || "https://server.megabrain.co";
|
|
796
|
-
|
|
885
|
+
log_default.setLocalLevel(logLevel || "silent");
|
|
797
886
|
const {
|
|
798
887
|
ads: initAds,
|
|
799
888
|
sessionId,
|
|
@@ -838,7 +927,7 @@ var AdsProviderWithoutBoundary = ({
|
|
|
838
927
|
});
|
|
839
928
|
const ads = mergeAds({ initAds, preloadAds, streamAds, viewedAds, clickedAds });
|
|
840
929
|
const markAdAsViewed = (ad) => {
|
|
841
|
-
|
|
930
|
+
log_default.debug("[Brain] Calling onAdView");
|
|
842
931
|
onAdView && onAdView({
|
|
843
932
|
id: ad.id,
|
|
844
933
|
code: ad.code,
|
|
@@ -850,7 +939,7 @@ var AdsProviderWithoutBoundary = ({
|
|
|
850
939
|
setViewedAds((old) => [...old, ad.id]);
|
|
851
940
|
};
|
|
852
941
|
const onAdClickInternal = (ad) => {
|
|
853
|
-
|
|
942
|
+
log_default.debug("[Brain] Calling onAdClick");
|
|
854
943
|
onAdClick && onAdClick({
|
|
855
944
|
id: ad.id,
|
|
856
945
|
code: ad.code,
|
|
@@ -878,6 +967,7 @@ var AdsProviderWithoutBoundary = ({
|
|
|
878
967
|
ads,
|
|
879
968
|
sessionId,
|
|
880
969
|
isInitialised,
|
|
970
|
+
conversationId,
|
|
881
971
|
isDisabled,
|
|
882
972
|
onAdClickInternal,
|
|
883
973
|
enabledPlacements,
|
|
@@ -943,7 +1033,6 @@ var AdsProvider = ({
|
|
|
943
1033
|
|
|
944
1034
|
// src/hooks/useAdViewed.tsx
|
|
945
1035
|
import { useContext } from "react";
|
|
946
|
-
import log6 from "loglevel";
|
|
947
1036
|
function useAdViewed(ad) {
|
|
948
1037
|
const context = useContext(AdsContext);
|
|
949
1038
|
const [stillMounted, setStillMounted] = useState5(false);
|
|
@@ -963,34 +1052,30 @@ function useAdViewed(ad) {
|
|
|
963
1052
|
if (!response.ok) {
|
|
964
1053
|
throw new Error("Error sending view request");
|
|
965
1054
|
}
|
|
966
|
-
|
|
1055
|
+
log_default.log("[BRAIN] ad marked as viewed", ad.id, ad.code);
|
|
967
1056
|
} catch (e) {
|
|
968
|
-
|
|
1057
|
+
log_default.warn("[BRAIN] Error sending view request", e);
|
|
969
1058
|
}
|
|
970
1059
|
};
|
|
971
1060
|
useEffect5(() => {
|
|
972
1061
|
if (!ad || ad.isError || ad.isLoading || ad.isStreaming || ad.viewed || stillMounted) {
|
|
973
1062
|
return;
|
|
974
1063
|
}
|
|
975
|
-
|
|
1064
|
+
log_default.log("[BRAIN] setting timeout", ad.id, ad.code);
|
|
976
1065
|
setTimeout(() => {
|
|
977
|
-
|
|
1066
|
+
log_default.log("[BRAIN] setting setStillMounted", ad.id, ad.code);
|
|
978
1067
|
setStillMounted(true);
|
|
979
1068
|
}, 1e3);
|
|
980
1069
|
}, [ad]);
|
|
981
1070
|
useEffect5(() => {
|
|
982
1071
|
if (stillMounted) {
|
|
983
|
-
|
|
1072
|
+
log_default.log("[BRAIN] sending request");
|
|
984
1073
|
sendRequest();
|
|
985
1074
|
}
|
|
986
1075
|
}, [stillMounted]);
|
|
987
1076
|
}
|
|
988
1077
|
|
|
989
|
-
// src/formats/InlineAd.tsx
|
|
990
|
-
import log9 from "loglevel";
|
|
991
|
-
|
|
992
1078
|
// src/components/MarkdownText.tsx
|
|
993
|
-
import log7 from "loglevel";
|
|
994
1079
|
import { useContext as useContext2 } from "react";
|
|
995
1080
|
import {
|
|
996
1081
|
Linking,
|
|
@@ -1007,7 +1092,7 @@ function MarkdownText({
|
|
|
1007
1092
|
const linkClickHandler = (href) => {
|
|
1008
1093
|
onLinkClick();
|
|
1009
1094
|
Linking.openURL(href).catch(
|
|
1010
|
-
(err) =>
|
|
1095
|
+
(err) => log_default.warn("Failed to open URL:", err)
|
|
1011
1096
|
);
|
|
1012
1097
|
return false;
|
|
1013
1098
|
};
|
|
@@ -1034,7 +1119,6 @@ function MarkdownText({
|
|
|
1034
1119
|
|
|
1035
1120
|
// src/components/VideoPlayer.tsx
|
|
1036
1121
|
import { ResizeMode, Video } from "expo-av";
|
|
1037
|
-
import log8 from "loglevel";
|
|
1038
1122
|
import { useContext as useContext4, useEffect as useEffect7, useRef, useState as useState7 } from "react";
|
|
1039
1123
|
import { Linking as Linking2, Text as Text2, TouchableOpacity, View as View2 } from "react-native";
|
|
1040
1124
|
|
|
@@ -1129,7 +1213,7 @@ var VideoPlayer = ({
|
|
|
1129
1213
|
};
|
|
1130
1214
|
const handleLinkClick = (url) => {
|
|
1131
1215
|
if (url) {
|
|
1132
|
-
Linking2.openURL(url).catch((err) =>
|
|
1216
|
+
Linking2.openURL(url).catch((err) => log_default.warn("Failed to open URL:", err));
|
|
1133
1217
|
onLinkClick();
|
|
1134
1218
|
}
|
|
1135
1219
|
};
|
|
@@ -1231,7 +1315,7 @@ var InlineAd = ({ code, messageId, wrapper }) => {
|
|
|
1231
1315
|
if (ad.isLoading || ad.content?.trim().toLowerCase().includes("none"))
|
|
1232
1316
|
return null;
|
|
1233
1317
|
const onProgress = (progress) => {
|
|
1234
|
-
|
|
1318
|
+
log_default.log(`Progress: ${progress}`);
|
|
1235
1319
|
};
|
|
1236
1320
|
const handleImageClick = (url) => {
|
|
1237
1321
|
if (url) {
|
|
@@ -1240,7 +1324,7 @@ var InlineAd = ({ code, messageId, wrapper }) => {
|
|
|
1240
1324
|
window.open(url, "_blank");
|
|
1241
1325
|
} else {
|
|
1242
1326
|
Linking3.openURL(url).catch(
|
|
1243
|
-
(err) =>
|
|
1327
|
+
(err) => log_default.warn("Failed to open URL:", err)
|
|
1244
1328
|
);
|
|
1245
1329
|
}
|
|
1246
1330
|
}
|