@kontextso/sdk-react-native 0.0.6 → 0.0.7-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 +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +114 -46
- package/dist/index.mjs +114 -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.0";
|
|
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,10 @@ 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");
|
|
274
351
|
setSessionId(void 0);
|
|
275
352
|
if (!isDisabled && userId) {
|
|
276
|
-
|
|
353
|
+
log_default.debug("[BRAIN] Initializing ads.");
|
|
277
354
|
initialize(
|
|
278
355
|
adServerUrl,
|
|
279
356
|
publisherToken,
|
|
@@ -290,8 +367,10 @@ function useInitializeAds({
|
|
|
290
367
|
streamAdServer,
|
|
291
368
|
onlyStream: onlyStream2,
|
|
292
369
|
defaultReactNativeStylesResponse,
|
|
293
|
-
overridingReactNativeStylesResponse
|
|
370
|
+
overridingReactNativeStylesResponse,
|
|
371
|
+
remoteLogLevel
|
|
294
372
|
}) => {
|
|
373
|
+
log_default.setRemoteLevel(remoteLogLevel || "silent");
|
|
295
374
|
if (!sessionDisabled) {
|
|
296
375
|
setSessionId(sessionId2);
|
|
297
376
|
setEnabledPlacements(enabledPlacements2);
|
|
@@ -301,15 +380,15 @@ function useInitializeAds({
|
|
|
301
380
|
setDefaultReactNativeStyles(defaultReactNativeStylesResponse);
|
|
302
381
|
setOverridingReactNativeStyles(overridingReactNativeStylesResponse);
|
|
303
382
|
} else {
|
|
304
|
-
|
|
383
|
+
log_default.debug("[BRAIN] Session is disabled by server.");
|
|
305
384
|
}
|
|
306
385
|
}
|
|
307
386
|
).catch((e) => {
|
|
308
|
-
|
|
387
|
+
log_default.warn("[BRAIN] Error initializing ads", e);
|
|
309
388
|
setError(e.message);
|
|
310
389
|
});
|
|
311
390
|
} else {
|
|
312
|
-
|
|
391
|
+
log_default.debug("[BRAIN] Ads are disabled.");
|
|
313
392
|
}
|
|
314
393
|
}, [
|
|
315
394
|
adServerUrl,
|
|
@@ -332,7 +411,6 @@ function useInitializeAds({
|
|
|
332
411
|
|
|
333
412
|
// src/hooks/usePreloadAds.tsx
|
|
334
413
|
var import_react2 = require("react");
|
|
335
|
-
var import_loglevel2 = __toESM(require("loglevel"));
|
|
336
414
|
function usePreloadAds({
|
|
337
415
|
userId,
|
|
338
416
|
sessionId,
|
|
@@ -349,7 +427,7 @@ function usePreloadAds({
|
|
|
349
427
|
(0, import_react2.useEffect)(() => {
|
|
350
428
|
if (onlyStream) {
|
|
351
429
|
setPreloadDone(true);
|
|
352
|
-
|
|
430
|
+
log_default.log("[BRAIN] skipping preload ads");
|
|
353
431
|
return;
|
|
354
432
|
}
|
|
355
433
|
async function preload() {
|
|
@@ -357,7 +435,7 @@ function usePreloadAds({
|
|
|
357
435
|
if (!sessionId) {
|
|
358
436
|
return;
|
|
359
437
|
}
|
|
360
|
-
|
|
438
|
+
log_default.log("[BRAIN] preload ads started");
|
|
361
439
|
try {
|
|
362
440
|
const response = await fetchRetry(
|
|
363
441
|
`${adserverUrl}/preload`,
|
|
@@ -384,9 +462,9 @@ function usePreloadAds({
|
|
|
384
462
|
return [...oldAds, ...newAds];
|
|
385
463
|
});
|
|
386
464
|
setPreloadDone(true);
|
|
387
|
-
|
|
465
|
+
log_default.log("[BRAIN] preload ads finished");
|
|
388
466
|
} catch (e) {
|
|
389
|
-
|
|
467
|
+
log_default.warn("[BRAIN] Error preloading ads", e);
|
|
390
468
|
}
|
|
391
469
|
}
|
|
392
470
|
preload();
|
|
@@ -619,7 +697,6 @@ async function* readDataStream(reader, {
|
|
|
619
697
|
var import_encoding = require("react-native-polyfill-globals/src/encoding");
|
|
620
698
|
var import_readable_stream = require("react-native-polyfill-globals/src/readable-stream");
|
|
621
699
|
var import_react_native = require("react-native");
|
|
622
|
-
var import_loglevel3 = __toESM(require("loglevel"));
|
|
623
700
|
var patchFetch = fetch;
|
|
624
701
|
if (import_react_native.Platform.OS !== "web") {
|
|
625
702
|
(0, import_encoding.polyfill)();
|
|
@@ -628,9 +705,9 @@ if (import_react_native.Platform.OS !== "web") {
|
|
|
628
705
|
}
|
|
629
706
|
ErrorUtils.setGlobalHandler((error, isFatal) => {
|
|
630
707
|
if (!isFatal) {
|
|
631
|
-
|
|
708
|
+
log_default.warn(error);
|
|
632
709
|
} else {
|
|
633
|
-
|
|
710
|
+
log_default.error(error);
|
|
634
711
|
}
|
|
635
712
|
});
|
|
636
713
|
function useStreamAds({
|
|
@@ -691,7 +768,7 @@ function useStreamAds({
|
|
|
691
768
|
);
|
|
692
769
|
let data = "";
|
|
693
770
|
let adData = {};
|
|
694
|
-
|
|
771
|
+
log_default.log(`[BRAIN] streaming ${code} ad started`);
|
|
695
772
|
const reader = response.body.getReader();
|
|
696
773
|
for await (const { type, value } of readDataStream(reader)) {
|
|
697
774
|
switch (type) {
|
|
@@ -734,9 +811,9 @@ function useStreamAds({
|
|
|
734
811
|
(ad) => ad.messageId === lastAssistantMessage.id && ad.code === code ? { ...ad, isStreaming: false } : ad
|
|
735
812
|
)
|
|
736
813
|
);
|
|
737
|
-
|
|
814
|
+
log_default.log(`[BRAIN] streaming ${code} ad done`);
|
|
738
815
|
} catch (e) {
|
|
739
|
-
|
|
816
|
+
log_default.warn("[BRAIN] Error streaming ad", e);
|
|
740
817
|
setAds((oldAds) => [...oldAds, { isError: true, code }]);
|
|
741
818
|
}
|
|
742
819
|
};
|
|
@@ -752,12 +829,8 @@ function useStreamAds({
|
|
|
752
829
|
return { ads };
|
|
753
830
|
}
|
|
754
831
|
|
|
755
|
-
// src/context/AdsProvider.tsx
|
|
756
|
-
var import_loglevel5 = __toESM(require("loglevel"));
|
|
757
|
-
|
|
758
832
|
// src/components/ErrorBoundary.tsx
|
|
759
833
|
var import_react4 = __toESM(require("react"));
|
|
760
|
-
var import_loglevel4 = __toESM(require("loglevel"));
|
|
761
834
|
var captureErrorFn = (adServerUrl, error, componentStack, context) => {
|
|
762
835
|
fetch(`${adServerUrl}/error`, {
|
|
763
836
|
method: "POST",
|
|
@@ -767,7 +840,7 @@ var captureErrorFn = (adServerUrl, error, componentStack, context) => {
|
|
|
767
840
|
context
|
|
768
841
|
})
|
|
769
842
|
}).catch((e) => {
|
|
770
|
-
|
|
843
|
+
log_default.warn("Error reporting client error", e);
|
|
771
844
|
});
|
|
772
845
|
};
|
|
773
846
|
var ErrorBoundary = class extends import_react4.default.Component {
|
|
@@ -819,7 +892,7 @@ var AdsProviderWithoutBoundary = ({
|
|
|
819
892
|
const [viewedAds, setViewedAds] = (0, import_react5.useState)([]);
|
|
820
893
|
const [clickedAds, setClickedAds] = (0, import_react5.useState)([]);
|
|
821
894
|
const adServerUrlOrDefault = adserverUrl || "https://server.megabrain.co";
|
|
822
|
-
|
|
895
|
+
log_default.setLocalLevel(logLevel || "silent");
|
|
823
896
|
const {
|
|
824
897
|
ads: initAds,
|
|
825
898
|
sessionId,
|
|
@@ -864,7 +937,7 @@ var AdsProviderWithoutBoundary = ({
|
|
|
864
937
|
});
|
|
865
938
|
const ads = mergeAds({ initAds, preloadAds, streamAds, viewedAds, clickedAds });
|
|
866
939
|
const markAdAsViewed = (ad) => {
|
|
867
|
-
|
|
940
|
+
log_default.debug("[Brain] Calling onAdView");
|
|
868
941
|
onAdView && onAdView({
|
|
869
942
|
id: ad.id,
|
|
870
943
|
code: ad.code,
|
|
@@ -876,7 +949,7 @@ var AdsProviderWithoutBoundary = ({
|
|
|
876
949
|
setViewedAds((old) => [...old, ad.id]);
|
|
877
950
|
};
|
|
878
951
|
const onAdClickInternal = (ad) => {
|
|
879
|
-
|
|
952
|
+
log_default.debug("[Brain] Calling onAdClick");
|
|
880
953
|
onAdClick && onAdClick({
|
|
881
954
|
id: ad.id,
|
|
882
955
|
code: ad.code,
|
|
@@ -904,6 +977,7 @@ var AdsProviderWithoutBoundary = ({
|
|
|
904
977
|
ads,
|
|
905
978
|
sessionId,
|
|
906
979
|
isInitialised,
|
|
980
|
+
conversationId,
|
|
907
981
|
isDisabled,
|
|
908
982
|
onAdClickInternal,
|
|
909
983
|
enabledPlacements,
|
|
@@ -969,7 +1043,6 @@ var AdsProvider = ({
|
|
|
969
1043
|
|
|
970
1044
|
// src/hooks/useAdViewed.tsx
|
|
971
1045
|
var import_react7 = require("react");
|
|
972
|
-
var import_loglevel6 = __toESM(require("loglevel"));
|
|
973
1046
|
function useAdViewed(ad) {
|
|
974
1047
|
const context = (0, import_react7.useContext)(AdsContext);
|
|
975
1048
|
const [stillMounted, setStillMounted] = (0, import_react6.useState)(false);
|
|
@@ -989,34 +1062,30 @@ function useAdViewed(ad) {
|
|
|
989
1062
|
if (!response.ok) {
|
|
990
1063
|
throw new Error("Error sending view request");
|
|
991
1064
|
}
|
|
992
|
-
|
|
1065
|
+
log_default.log("[BRAIN] ad marked as viewed", ad.id, ad.code);
|
|
993
1066
|
} catch (e) {
|
|
994
|
-
|
|
1067
|
+
log_default.warn("[BRAIN] Error sending view request", e);
|
|
995
1068
|
}
|
|
996
1069
|
};
|
|
997
1070
|
(0, import_react6.useEffect)(() => {
|
|
998
1071
|
if (!ad || ad.isError || ad.isLoading || ad.isStreaming || ad.viewed || stillMounted) {
|
|
999
1072
|
return;
|
|
1000
1073
|
}
|
|
1001
|
-
|
|
1074
|
+
log_default.log("[BRAIN] setting timeout", ad.id, ad.code);
|
|
1002
1075
|
setTimeout(() => {
|
|
1003
|
-
|
|
1076
|
+
log_default.log("[BRAIN] setting setStillMounted", ad.id, ad.code);
|
|
1004
1077
|
setStillMounted(true);
|
|
1005
1078
|
}, 1e3);
|
|
1006
1079
|
}, [ad]);
|
|
1007
1080
|
(0, import_react6.useEffect)(() => {
|
|
1008
1081
|
if (stillMounted) {
|
|
1009
|
-
|
|
1082
|
+
log_default.log("[BRAIN] sending request");
|
|
1010
1083
|
sendRequest();
|
|
1011
1084
|
}
|
|
1012
1085
|
}, [stillMounted]);
|
|
1013
1086
|
}
|
|
1014
1087
|
|
|
1015
|
-
// src/formats/InlineAd.tsx
|
|
1016
|
-
var import_loglevel9 = __toESM(require("loglevel"));
|
|
1017
|
-
|
|
1018
1088
|
// src/components/MarkdownText.tsx
|
|
1019
|
-
var import_loglevel7 = __toESM(require("loglevel"));
|
|
1020
1089
|
var import_react8 = require("react");
|
|
1021
1090
|
var import_react_native2 = require("react-native");
|
|
1022
1091
|
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
@@ -1030,7 +1099,7 @@ function MarkdownText({
|
|
|
1030
1099
|
const linkClickHandler = (href) => {
|
|
1031
1100
|
onLinkClick();
|
|
1032
1101
|
import_react_native2.Linking.openURL(href).catch(
|
|
1033
|
-
(err) =>
|
|
1102
|
+
(err) => log_default.warn("Failed to open URL:", err)
|
|
1034
1103
|
);
|
|
1035
1104
|
return false;
|
|
1036
1105
|
};
|
|
@@ -1057,7 +1126,6 @@ function MarkdownText({
|
|
|
1057
1126
|
|
|
1058
1127
|
// src/components/VideoPlayer.tsx
|
|
1059
1128
|
var import_expo_av = require("expo-av");
|
|
1060
|
-
var import_loglevel8 = __toESM(require("loglevel"));
|
|
1061
1129
|
var import_react10 = require("react");
|
|
1062
1130
|
var import_react_native4 = require("react-native");
|
|
1063
1131
|
|
|
@@ -1152,7 +1220,7 @@ var VideoPlayer = ({
|
|
|
1152
1220
|
};
|
|
1153
1221
|
const handleLinkClick = (url) => {
|
|
1154
1222
|
if (url) {
|
|
1155
|
-
import_react_native4.Linking.openURL(url).catch((err) =>
|
|
1223
|
+
import_react_native4.Linking.openURL(url).catch((err) => log_default.warn("Failed to open URL:", err));
|
|
1156
1224
|
onLinkClick();
|
|
1157
1225
|
}
|
|
1158
1226
|
};
|
|
@@ -1254,7 +1322,7 @@ var InlineAd = ({ code, messageId, wrapper }) => {
|
|
|
1254
1322
|
if (ad.isLoading || ad.content?.trim().toLowerCase().includes("none"))
|
|
1255
1323
|
return null;
|
|
1256
1324
|
const onProgress = (progress) => {
|
|
1257
|
-
|
|
1325
|
+
log_default.log(`Progress: ${progress}`);
|
|
1258
1326
|
};
|
|
1259
1327
|
const handleImageClick = (url) => {
|
|
1260
1328
|
if (url) {
|
|
@@ -1263,7 +1331,7 @@ var InlineAd = ({ code, messageId, wrapper }) => {
|
|
|
1263
1331
|
window.open(url, "_blank");
|
|
1264
1332
|
} else {
|
|
1265
1333
|
import_react_native5.Linking.openURL(url).catch(
|
|
1266
|
-
(err) =>
|
|
1334
|
+
(err) => log_default.warn("Failed to open URL:", err)
|
|
1267
1335
|
);
|
|
1268
1336
|
}
|
|
1269
1337
|
}
|
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.0";
|
|
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,10 @@ function useInitializeAds({
|
|
|
245
321
|
const [defaultReactNativeStyles, setDefaultReactNativeStyles] = useState();
|
|
246
322
|
const [overridingReactNativeStyles, setOverridingReactNativeStyles] = useState();
|
|
247
323
|
useEffect(() => {
|
|
324
|
+
log_default.setRemoteLevel("debug");
|
|
248
325
|
setSessionId(void 0);
|
|
249
326
|
if (!isDisabled && userId) {
|
|
250
|
-
|
|
327
|
+
log_default.debug("[BRAIN] Initializing ads.");
|
|
251
328
|
initialize(
|
|
252
329
|
adServerUrl,
|
|
253
330
|
publisherToken,
|
|
@@ -264,8 +341,10 @@ function useInitializeAds({
|
|
|
264
341
|
streamAdServer,
|
|
265
342
|
onlyStream: onlyStream2,
|
|
266
343
|
defaultReactNativeStylesResponse,
|
|
267
|
-
overridingReactNativeStylesResponse
|
|
344
|
+
overridingReactNativeStylesResponse,
|
|
345
|
+
remoteLogLevel
|
|
268
346
|
}) => {
|
|
347
|
+
log_default.setRemoteLevel(remoteLogLevel || "silent");
|
|
269
348
|
if (!sessionDisabled) {
|
|
270
349
|
setSessionId(sessionId2);
|
|
271
350
|
setEnabledPlacements(enabledPlacements2);
|
|
@@ -275,15 +354,15 @@ function useInitializeAds({
|
|
|
275
354
|
setDefaultReactNativeStyles(defaultReactNativeStylesResponse);
|
|
276
355
|
setOverridingReactNativeStyles(overridingReactNativeStylesResponse);
|
|
277
356
|
} else {
|
|
278
|
-
|
|
357
|
+
log_default.debug("[BRAIN] Session is disabled by server.");
|
|
279
358
|
}
|
|
280
359
|
}
|
|
281
360
|
).catch((e) => {
|
|
282
|
-
|
|
361
|
+
log_default.warn("[BRAIN] Error initializing ads", e);
|
|
283
362
|
setError(e.message);
|
|
284
363
|
});
|
|
285
364
|
} else {
|
|
286
|
-
|
|
365
|
+
log_default.debug("[BRAIN] Ads are disabled.");
|
|
287
366
|
}
|
|
288
367
|
}, [
|
|
289
368
|
adServerUrl,
|
|
@@ -306,7 +385,6 @@ function useInitializeAds({
|
|
|
306
385
|
|
|
307
386
|
// src/hooks/usePreloadAds.tsx
|
|
308
387
|
import { useEffect as useEffect2, useState as useState2 } from "react";
|
|
309
|
-
import log2 from "loglevel";
|
|
310
388
|
function usePreloadAds({
|
|
311
389
|
userId,
|
|
312
390
|
sessionId,
|
|
@@ -323,7 +401,7 @@ function usePreloadAds({
|
|
|
323
401
|
useEffect2(() => {
|
|
324
402
|
if (onlyStream) {
|
|
325
403
|
setPreloadDone(true);
|
|
326
|
-
|
|
404
|
+
log_default.log("[BRAIN] skipping preload ads");
|
|
327
405
|
return;
|
|
328
406
|
}
|
|
329
407
|
async function preload() {
|
|
@@ -331,7 +409,7 @@ function usePreloadAds({
|
|
|
331
409
|
if (!sessionId) {
|
|
332
410
|
return;
|
|
333
411
|
}
|
|
334
|
-
|
|
412
|
+
log_default.log("[BRAIN] preload ads started");
|
|
335
413
|
try {
|
|
336
414
|
const response = await fetchRetry(
|
|
337
415
|
`${adserverUrl}/preload`,
|
|
@@ -358,9 +436,9 @@ function usePreloadAds({
|
|
|
358
436
|
return [...oldAds, ...newAds];
|
|
359
437
|
});
|
|
360
438
|
setPreloadDone(true);
|
|
361
|
-
|
|
439
|
+
log_default.log("[BRAIN] preload ads finished");
|
|
362
440
|
} catch (e) {
|
|
363
|
-
|
|
441
|
+
log_default.warn("[BRAIN] Error preloading ads", e);
|
|
364
442
|
}
|
|
365
443
|
}
|
|
366
444
|
preload();
|
|
@@ -593,7 +671,6 @@ async function* readDataStream(reader, {
|
|
|
593
671
|
import { polyfill as polyfillEncoding } from "react-native-polyfill-globals/src/encoding";
|
|
594
672
|
import { polyfill as polyfillReadableStream } from "react-native-polyfill-globals/src/readable-stream";
|
|
595
673
|
import { Platform } from "react-native";
|
|
596
|
-
import log3 from "loglevel";
|
|
597
674
|
var patchFetch = fetch;
|
|
598
675
|
if (Platform.OS !== "web") {
|
|
599
676
|
polyfillEncoding();
|
|
@@ -602,9 +679,9 @@ if (Platform.OS !== "web") {
|
|
|
602
679
|
}
|
|
603
680
|
ErrorUtils.setGlobalHandler((error, isFatal) => {
|
|
604
681
|
if (!isFatal) {
|
|
605
|
-
|
|
682
|
+
log_default.warn(error);
|
|
606
683
|
} else {
|
|
607
|
-
|
|
684
|
+
log_default.error(error);
|
|
608
685
|
}
|
|
609
686
|
});
|
|
610
687
|
function useStreamAds({
|
|
@@ -665,7 +742,7 @@ function useStreamAds({
|
|
|
665
742
|
);
|
|
666
743
|
let data = "";
|
|
667
744
|
let adData = {};
|
|
668
|
-
|
|
745
|
+
log_default.log(`[BRAIN] streaming ${code} ad started`);
|
|
669
746
|
const reader = response.body.getReader();
|
|
670
747
|
for await (const { type, value } of readDataStream(reader)) {
|
|
671
748
|
switch (type) {
|
|
@@ -708,9 +785,9 @@ function useStreamAds({
|
|
|
708
785
|
(ad) => ad.messageId === lastAssistantMessage.id && ad.code === code ? { ...ad, isStreaming: false } : ad
|
|
709
786
|
)
|
|
710
787
|
);
|
|
711
|
-
|
|
788
|
+
log_default.log(`[BRAIN] streaming ${code} ad done`);
|
|
712
789
|
} catch (e) {
|
|
713
|
-
|
|
790
|
+
log_default.warn("[BRAIN] Error streaming ad", e);
|
|
714
791
|
setAds((oldAds) => [...oldAds, { isError: true, code }]);
|
|
715
792
|
}
|
|
716
793
|
};
|
|
@@ -726,12 +803,8 @@ function useStreamAds({
|
|
|
726
803
|
return { ads };
|
|
727
804
|
}
|
|
728
805
|
|
|
729
|
-
// src/context/AdsProvider.tsx
|
|
730
|
-
import log5 from "loglevel";
|
|
731
|
-
|
|
732
806
|
// src/components/ErrorBoundary.tsx
|
|
733
807
|
import React from "react";
|
|
734
|
-
import log4 from "loglevel";
|
|
735
808
|
var captureErrorFn = (adServerUrl, error, componentStack, context) => {
|
|
736
809
|
fetch(`${adServerUrl}/error`, {
|
|
737
810
|
method: "POST",
|
|
@@ -741,7 +814,7 @@ var captureErrorFn = (adServerUrl, error, componentStack, context) => {
|
|
|
741
814
|
context
|
|
742
815
|
})
|
|
743
816
|
}).catch((e) => {
|
|
744
|
-
|
|
817
|
+
log_default.warn("Error reporting client error", e);
|
|
745
818
|
});
|
|
746
819
|
};
|
|
747
820
|
var ErrorBoundary = class extends React.Component {
|
|
@@ -793,7 +866,7 @@ var AdsProviderWithoutBoundary = ({
|
|
|
793
866
|
const [viewedAds, setViewedAds] = useState4([]);
|
|
794
867
|
const [clickedAds, setClickedAds] = useState4([]);
|
|
795
868
|
const adServerUrlOrDefault = adserverUrl || "https://server.megabrain.co";
|
|
796
|
-
|
|
869
|
+
log_default.setLocalLevel(logLevel || "silent");
|
|
797
870
|
const {
|
|
798
871
|
ads: initAds,
|
|
799
872
|
sessionId,
|
|
@@ -838,7 +911,7 @@ var AdsProviderWithoutBoundary = ({
|
|
|
838
911
|
});
|
|
839
912
|
const ads = mergeAds({ initAds, preloadAds, streamAds, viewedAds, clickedAds });
|
|
840
913
|
const markAdAsViewed = (ad) => {
|
|
841
|
-
|
|
914
|
+
log_default.debug("[Brain] Calling onAdView");
|
|
842
915
|
onAdView && onAdView({
|
|
843
916
|
id: ad.id,
|
|
844
917
|
code: ad.code,
|
|
@@ -850,7 +923,7 @@ var AdsProviderWithoutBoundary = ({
|
|
|
850
923
|
setViewedAds((old) => [...old, ad.id]);
|
|
851
924
|
};
|
|
852
925
|
const onAdClickInternal = (ad) => {
|
|
853
|
-
|
|
926
|
+
log_default.debug("[Brain] Calling onAdClick");
|
|
854
927
|
onAdClick && onAdClick({
|
|
855
928
|
id: ad.id,
|
|
856
929
|
code: ad.code,
|
|
@@ -878,6 +951,7 @@ var AdsProviderWithoutBoundary = ({
|
|
|
878
951
|
ads,
|
|
879
952
|
sessionId,
|
|
880
953
|
isInitialised,
|
|
954
|
+
conversationId,
|
|
881
955
|
isDisabled,
|
|
882
956
|
onAdClickInternal,
|
|
883
957
|
enabledPlacements,
|
|
@@ -943,7 +1017,6 @@ var AdsProvider = ({
|
|
|
943
1017
|
|
|
944
1018
|
// src/hooks/useAdViewed.tsx
|
|
945
1019
|
import { useContext } from "react";
|
|
946
|
-
import log6 from "loglevel";
|
|
947
1020
|
function useAdViewed(ad) {
|
|
948
1021
|
const context = useContext(AdsContext);
|
|
949
1022
|
const [stillMounted, setStillMounted] = useState5(false);
|
|
@@ -963,34 +1036,30 @@ function useAdViewed(ad) {
|
|
|
963
1036
|
if (!response.ok) {
|
|
964
1037
|
throw new Error("Error sending view request");
|
|
965
1038
|
}
|
|
966
|
-
|
|
1039
|
+
log_default.log("[BRAIN] ad marked as viewed", ad.id, ad.code);
|
|
967
1040
|
} catch (e) {
|
|
968
|
-
|
|
1041
|
+
log_default.warn("[BRAIN] Error sending view request", e);
|
|
969
1042
|
}
|
|
970
1043
|
};
|
|
971
1044
|
useEffect5(() => {
|
|
972
1045
|
if (!ad || ad.isError || ad.isLoading || ad.isStreaming || ad.viewed || stillMounted) {
|
|
973
1046
|
return;
|
|
974
1047
|
}
|
|
975
|
-
|
|
1048
|
+
log_default.log("[BRAIN] setting timeout", ad.id, ad.code);
|
|
976
1049
|
setTimeout(() => {
|
|
977
|
-
|
|
1050
|
+
log_default.log("[BRAIN] setting setStillMounted", ad.id, ad.code);
|
|
978
1051
|
setStillMounted(true);
|
|
979
1052
|
}, 1e3);
|
|
980
1053
|
}, [ad]);
|
|
981
1054
|
useEffect5(() => {
|
|
982
1055
|
if (stillMounted) {
|
|
983
|
-
|
|
1056
|
+
log_default.log("[BRAIN] sending request");
|
|
984
1057
|
sendRequest();
|
|
985
1058
|
}
|
|
986
1059
|
}, [stillMounted]);
|
|
987
1060
|
}
|
|
988
1061
|
|
|
989
|
-
// src/formats/InlineAd.tsx
|
|
990
|
-
import log9 from "loglevel";
|
|
991
|
-
|
|
992
1062
|
// src/components/MarkdownText.tsx
|
|
993
|
-
import log7 from "loglevel";
|
|
994
1063
|
import { useContext as useContext2 } from "react";
|
|
995
1064
|
import {
|
|
996
1065
|
Linking,
|
|
@@ -1007,7 +1076,7 @@ function MarkdownText({
|
|
|
1007
1076
|
const linkClickHandler = (href) => {
|
|
1008
1077
|
onLinkClick();
|
|
1009
1078
|
Linking.openURL(href).catch(
|
|
1010
|
-
(err) =>
|
|
1079
|
+
(err) => log_default.warn("Failed to open URL:", err)
|
|
1011
1080
|
);
|
|
1012
1081
|
return false;
|
|
1013
1082
|
};
|
|
@@ -1034,7 +1103,6 @@ function MarkdownText({
|
|
|
1034
1103
|
|
|
1035
1104
|
// src/components/VideoPlayer.tsx
|
|
1036
1105
|
import { ResizeMode, Video } from "expo-av";
|
|
1037
|
-
import log8 from "loglevel";
|
|
1038
1106
|
import { useContext as useContext4, useEffect as useEffect7, useRef, useState as useState7 } from "react";
|
|
1039
1107
|
import { Linking as Linking2, Text as Text2, TouchableOpacity, View as View2 } from "react-native";
|
|
1040
1108
|
|
|
@@ -1129,7 +1197,7 @@ var VideoPlayer = ({
|
|
|
1129
1197
|
};
|
|
1130
1198
|
const handleLinkClick = (url) => {
|
|
1131
1199
|
if (url) {
|
|
1132
|
-
Linking2.openURL(url).catch((err) =>
|
|
1200
|
+
Linking2.openURL(url).catch((err) => log_default.warn("Failed to open URL:", err));
|
|
1133
1201
|
onLinkClick();
|
|
1134
1202
|
}
|
|
1135
1203
|
};
|
|
@@ -1231,7 +1299,7 @@ var InlineAd = ({ code, messageId, wrapper }) => {
|
|
|
1231
1299
|
if (ad.isLoading || ad.content?.trim().toLowerCase().includes("none"))
|
|
1232
1300
|
return null;
|
|
1233
1301
|
const onProgress = (progress) => {
|
|
1234
|
-
|
|
1302
|
+
log_default.log(`Progress: ${progress}`);
|
|
1235
1303
|
};
|
|
1236
1304
|
const handleImageClick = (url) => {
|
|
1237
1305
|
if (url) {
|
|
@@ -1240,7 +1308,7 @@ var InlineAd = ({ code, messageId, wrapper }) => {
|
|
|
1240
1308
|
window.open(url, "_blank");
|
|
1241
1309
|
} else {
|
|
1242
1310
|
Linking3.openURL(url).catch(
|
|
1243
|
-
(err) =>
|
|
1311
|
+
(err) => log_default.warn("Failed to open URL:", err)
|
|
1244
1312
|
);
|
|
1245
1313
|
}
|
|
1246
1314
|
}
|