@memori.ai/memori-react 2.8.1 → 2.8.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -148,7 +148,6 @@ let audioContext: IAudioContext;
148
148
 
149
149
  let memoriPassword: string | undefined;
150
150
  let speakerMuted: boolean = false;
151
- let speakLanguage: string | undefined;
152
151
 
153
152
  export interface LayoutProps {
154
153
  Header?: typeof Header;
@@ -274,7 +273,7 @@ const MemoriWidget = ({
274
273
  multilingual !== undefined
275
274
  ? multilingual
276
275
  : !!integrationConfig?.multilanguage;
277
- const [userLang, _setUserLang] = useState(
276
+ const [userLang, setUserLang] = useState(
278
277
  memoriLang ??
279
278
  integrationConfig?.lang ??
280
279
  memori?.culture?.split('-')?.[0] ??
@@ -283,10 +282,6 @@ const MemoriWidget = ({
283
282
  i18n.language ??
284
283
  'IT'
285
284
  );
286
- const setUserLang: typeof _setUserLang = lang => {
287
- _setUserLang(lang);
288
- speakLanguage = lang;
289
- };
290
285
 
291
286
  const [loading, setLoading] = useState(false);
292
287
  const [memoriTyping, setMemoriTyping] = useState(false);
@@ -694,7 +689,7 @@ const MemoriWidget = ({
694
689
  ...params,
695
690
  additionalInfo: {
696
691
  ...(additionalInfo || {}),
697
- language: userLang,
692
+ language: getCultureCodeByLanguage(userLang),
698
693
  referral: referral,
699
694
  },
700
695
  });
@@ -781,7 +776,7 @@ const MemoriWidget = ({
781
776
  birthDate: birthDate || storageBirthDate || undefined,
782
777
  additionalInfo: {
783
778
  ...(additionalInfo || {}),
784
- language: userLang,
779
+ language: getCultureCodeByLanguage(userLang),
785
780
  referral: referral,
786
781
  },
787
782
  });
@@ -882,6 +877,15 @@ const MemoriWidget = ({
882
877
  undefined
883
878
  );
884
879
 
880
+ let referral;
881
+ try {
882
+ referral = (() => {
883
+ return window.location.href;
884
+ })();
885
+ } catch (err) {
886
+ console.error(err);
887
+ }
888
+
885
889
  fetchSession({
886
890
  memoriID: memori.engineMemoriID ?? '',
887
891
  password: secret || memoriPwd || memori.secretToken,
@@ -890,6 +894,11 @@ const MemoriWidget = ({
890
894
  initialContextVars,
891
895
  initialQuestion,
892
896
  birthDate: birthDate || storageBirthDate || undefined,
897
+ additionalInfo: {
898
+ ...(additionalInfo || {}),
899
+ language: getCultureCodeByLanguage(userLang),
900
+ referral: referral,
901
+ },
893
902
  });
894
903
  } else if (!!currentState) {
895
904
  return {
@@ -952,6 +961,7 @@ const MemoriWidget = ({
952
961
  !speechSynthesizer &&
953
962
  !isPlayingAudio &&
954
963
  !userMessage.length &&
964
+ !memoriTyping &&
955
965
  !listening
956
966
  )
957
967
  setInteractionTimeout();
@@ -961,6 +971,7 @@ const MemoriWidget = ({
961
971
  !!speechSynthesizer ||
962
972
  isPlayingAudio ||
963
973
  !!userMessage.length ||
974
+ memoriTyping ||
964
975
  listening
965
976
  ) {
966
977
  resetInteractionTimeout();
@@ -1021,16 +1032,13 @@ const MemoriWidget = ({
1021
1032
  }
1022
1033
  }
1023
1034
 
1024
- if (memori.enableCompletions) {
1025
- timeout = timeout + 60;
1026
- }
1027
-
1028
1035
  let uiTimeout = setTimeout(handleTimeout, timeout * 1000);
1029
1036
  setUserInteractionTimeout(uiTimeout);
1030
1037
  timeoutRef.current = uiTimeout;
1031
1038
  };
1032
1039
  useEffect(() => {
1033
- if (!!userMessage.length || isPlayingAudio) clearInteractionTimeout();
1040
+ if (!!userMessage.length || isPlayingAudio || memoriTyping)
1041
+ clearInteractionTimeout();
1034
1042
  if (sessionId && !!!userMessage.length) resetInteractionTimeout();
1035
1043
  // eslint-disable-next-line react-hooks/exhaustive-deps
1036
1044
  }, [
@@ -1041,6 +1049,7 @@ const MemoriWidget = ({
1041
1049
  sessionId,
1042
1050
  history,
1043
1051
  userMessage,
1052
+ memoriTyping,
1044
1053
  ]);
1045
1054
  useEffect(() => {
1046
1055
  return () => {
@@ -1283,182 +1292,177 @@ const MemoriWidget = ({
1283
1292
  // .replace(/qfe/gi, `<sub alias="Quota Filo Erba">QFE</sub>`)
1284
1293
  };
1285
1294
 
1286
- const speak = useCallback(
1287
- (text: string): void => {
1288
- if (!AZURE_COGNITIVE_SERVICES_TTS_KEY) return;
1289
- stopListening();
1290
- // stopAudio();
1291
-
1292
- if (preview) return;
1293
-
1294
- if (muteSpeaker || speakerMuted) {
1295
- // trigger start continuous listening if set, see MemoriChat
1296
- if (continuousSpeech) {
1297
- setListeningTimeout();
1298
- }
1299
- return;
1300
- }
1295
+ const speak = (text: string): void => {
1296
+ if (!AZURE_COGNITIVE_SERVICES_TTS_KEY) return;
1297
+ stopListening();
1298
+ // stopAudio();
1301
1299
 
1302
- if (audioDestination) audioDestination.pause();
1303
-
1304
- // if (audioContext?.state === 'running') {
1305
- // // audioContext.suspend();
1306
- // // }
1307
- // // if (audioContext) {
1308
- // audioContext.close().then(() => {
1309
- // audioContext = new AudioContext();
1310
- // let buffer = audioContext.createBuffer(1, 10000, 22050);
1311
- // let source = audioContext.createBufferSource();
1312
- // source.buffer = buffer;
1313
- // source.connect(audioContext.destination);
1314
- // });
1315
- // }
1300
+ if (preview) return;
1316
1301
 
1317
- let isSafari =
1318
- window.navigator.userAgent.includes('Safari') &&
1319
- !window.navigator.userAgent.includes('Chrome');
1320
- let isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent);
1321
- if ((audioContext.state as string) === 'interrupted') {
1322
- audioContext.resume().then(() => speak(text));
1323
- return;
1302
+ if (muteSpeaker || speakerMuted) {
1303
+ // trigger start continuous listening if set, see MemoriChat
1304
+ if (continuousSpeech) {
1305
+ setListeningTimeout();
1324
1306
  }
1325
- if (isIOS && isSafari) {
1326
- audioContext.suspend();
1307
+ return;
1308
+ }
1327
1309
 
1328
- if (isPlayingAudio) {
1329
- try {
1330
- if (speechSynthesizer) {
1331
- speechSynthesizer.close();
1332
- speechSynthesizer = null;
1333
- }
1334
- if (audioDestination) {
1335
- audioDestination.pause();
1336
- audioDestination.close();
1337
- }
1338
- if (audioContext) {
1339
- // audioContext.close().then(() => {
1340
- // audioContext = new AudioContext();
1341
- // let buffer = audioContext.createBuffer(1, 10000, 22050);
1342
- // let source = audioContext.createBufferSource();
1343
- // source.buffer = buffer;
1344
- // source.connect(audioContext.destination);
1345
- // });
1346
- audioContext.destination.disconnect();
1347
- }
1348
- } catch (e) {
1349
- console.error('stopAudio error: ', e);
1310
+ if (audioDestination) audioDestination.pause();
1311
+
1312
+ // if (audioContext?.state === 'running') {
1313
+ // // audioContext.suspend();
1314
+ // // }
1315
+ // // if (audioContext) {
1316
+ // audioContext.close().then(() => {
1317
+ // audioContext = new AudioContext();
1318
+ // let buffer = audioContext.createBuffer(1, 10000, 22050);
1319
+ // let source = audioContext.createBufferSource();
1320
+ // source.buffer = buffer;
1321
+ // source.connect(audioContext.destination);
1322
+ // });
1323
+ // }
1324
+
1325
+ let isSafari =
1326
+ window.navigator.userAgent.includes('Safari') &&
1327
+ !window.navigator.userAgent.includes('Chrome');
1328
+ let isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent);
1329
+ if ((audioContext.state as string) === 'interrupted') {
1330
+ audioContext.resume().then(() => speak(text));
1331
+ return;
1332
+ }
1333
+ if (isIOS && isSafari) {
1334
+ audioContext.suspend();
1335
+
1336
+ if (isPlayingAudio) {
1337
+ try {
1338
+ if (speechSynthesizer) {
1339
+ speechSynthesizer.close();
1340
+ speechSynthesizer = null;
1341
+ }
1342
+ if (audioDestination) {
1343
+ audioDestination.pause();
1344
+ audioDestination.close();
1350
1345
  }
1346
+ if (audioContext) {
1347
+ // audioContext.close().then(() => {
1348
+ // audioContext = new AudioContext();
1349
+ // let buffer = audioContext.createBuffer(1, 10000, 22050);
1350
+ // let source = audioContext.createBufferSource();
1351
+ // source.buffer = buffer;
1352
+ // source.connect(audioContext.destination);
1353
+ // });
1354
+ audioContext.destination.disconnect();
1355
+ }
1356
+ } catch (e) {
1357
+ console.error('stopAudio error: ', e);
1351
1358
  }
1352
1359
  }
1353
- if (audioContext.state === 'closed') {
1354
- audioContext = new AudioContext();
1355
- let buffer = audioContext.createBuffer(1, 10000, 22050);
1356
- let source = audioContext.createBufferSource();
1357
- source.buffer = buffer;
1358
- source.connect(audioContext.destination);
1359
- } else if (audioContext.state === 'suspended') {
1360
- stopAudio();
1361
-
1362
- audioContext = new AudioContext();
1363
- let buffer = audioContext.createBuffer(1, 10000, 22050);
1364
- let source = audioContext.createBufferSource();
1365
- source.buffer = buffer;
1366
- source.connect(audioContext.destination);
1367
- }
1368
-
1369
- if (!speechSynthesizer) {
1370
- audioDestination = new speechSdk.SpeakerAudioDestination();
1371
- let audioConfig =
1372
- speechSdk.AudioConfig.fromSpeakerOutput(audioDestination);
1373
- speechSynthesizer = new speechSdk.SpeechSynthesizer(
1374
- speechConfig,
1375
- audioConfig
1376
- );
1377
- }
1378
-
1379
- const source = audioContext.createBufferSource();
1380
- source.addEventListener('ended', () => {
1381
- setIsPlayingAudio(false);
1382
- });
1383
- audioDestination.onAudioEnd = () => {
1384
- setIsPlayingAudio(false);
1385
- source.disconnect();
1386
-
1387
- // trigger start continuous listening if set
1388
- // document.dispatchEvent(new Event('endSpeakStartListen'));
1389
- onEndSpeakStartListen();
1390
- };
1360
+ }
1361
+ if (audioContext.state === 'closed') {
1362
+ audioContext = new AudioContext();
1363
+ let buffer = audioContext.createBuffer(1, 10000, 22050);
1364
+ let source = audioContext.createBufferSource();
1365
+ source.buffer = buffer;
1366
+ source.connect(audioContext.destination);
1367
+ } else if (audioContext.state === 'suspended') {
1368
+ stopAudio();
1391
1369
 
1392
- const lang = speakLanguage || userLang;
1370
+ audioContext = new AudioContext();
1371
+ let buffer = audioContext.createBuffer(1, 10000, 22050);
1372
+ let source = audioContext.createBufferSource();
1373
+ source.buffer = buffer;
1374
+ source.connect(audioContext.destination);
1375
+ }
1393
1376
 
1394
- speechSynthesizer.speakSsmlAsync(
1395
- `<speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis" xmlns:mstts="https://www.w3.org/2001/mstts" xmlns:emo="http://www.w3.org/2009/10/emotionml" xml:lang="${getCultureCodeByLanguage(
1396
- lang
1397
- )}"><voice name="${getTTSVoice(lang)}"><s>${replaceTextWithPhonemes(
1398
- escapeHTML(text),
1399
- lang.toLowerCase()
1400
- )}</s></voice></speak>`,
1401
- result => {
1402
- if (result) {
1403
- setIsPlayingAudio(true);
1377
+ if (!speechSynthesizer) {
1378
+ audioDestination = new speechSdk.SpeakerAudioDestination();
1379
+ let audioConfig =
1380
+ speechSdk.AudioConfig.fromSpeakerOutput(audioDestination);
1381
+ speechSynthesizer = new speechSdk.SpeechSynthesizer(
1382
+ speechConfig,
1383
+ audioConfig
1384
+ );
1385
+ }
1404
1386
 
1405
- try {
1406
- // if (audioContext.destination.context.state === 'running') {
1407
- // audioContext.destination.disconnect();
1408
- // }
1409
- audioContext.decodeAudioData(result.audioData, function (buffer) {
1410
- source.buffer = buffer;
1411
- source.connect(audioContext.destination);
1412
-
1413
- if (history.length < 1 || (isSafari && isIOS)) {
1414
- source.start(0);
1415
- }
1416
- });
1387
+ const source = audioContext.createBufferSource();
1388
+ source.addEventListener('ended', () => {
1389
+ setIsPlayingAudio(false);
1390
+ });
1391
+ audioDestination.onAudioEnd = () => {
1392
+ setIsPlayingAudio(false);
1393
+ source.disconnect();
1417
1394
 
1418
- audioContext.onstatechange = () => {
1419
- if (
1420
- audioContext.state === 'suspended' ||
1421
- audioContext.state === 'closed'
1422
- ) {
1423
- source.disconnect();
1424
- setIsPlayingAudio(false);
1425
- } else if ((audioContext.state as string) === 'interrupted') {
1426
- audioContext.resume();
1427
- }
1428
- };
1395
+ // trigger start continuous listening if set
1396
+ // document.dispatchEvent(new Event('endSpeakStartListen'));
1397
+ onEndSpeakStartListen();
1398
+ };
1429
1399
 
1430
- audioContext.resume();
1400
+ speechSynthesizer.speakSsmlAsync(
1401
+ `<speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis" xmlns:mstts="https://www.w3.org/2001/mstts" xmlns:emo="http://www.w3.org/2009/10/emotionml" xml:lang="${getCultureCodeByLanguage(
1402
+ userLang
1403
+ )}"><voice name="${getTTSVoice(userLang)}"><s>${replaceTextWithPhonemes(
1404
+ escapeHTML(text),
1405
+ userLang.toLowerCase()
1406
+ )}</s></voice></speak>`,
1407
+ result => {
1408
+ if (result) {
1409
+ setIsPlayingAudio(true);
1431
1410
 
1432
- if (speechSynthesizer) {
1433
- speechSynthesizer.close();
1434
- speechSynthesizer = null;
1411
+ try {
1412
+ // if (audioContext.destination.context.state === 'running') {
1413
+ // audioContext.destination.disconnect();
1414
+ // }
1415
+ audioContext.decodeAudioData(result.audioData, function (buffer) {
1416
+ source.buffer = buffer;
1417
+ source.connect(audioContext.destination);
1418
+
1419
+ if (history.length < 1 || (isSafari && isIOS)) {
1420
+ source.start(0);
1435
1421
  }
1436
- } catch (e) {
1437
- console.error('speak error: ', e);
1438
- window.speechSynthesis.speak(new SpeechSynthesisUtterance(text));
1439
- setIsPlayingAudio(false);
1422
+ });
1440
1423
 
1441
- if (speechSynthesizer) {
1442
- speechSynthesizer.close();
1443
- speechSynthesizer = null;
1424
+ audioContext.onstatechange = () => {
1425
+ if (
1426
+ audioContext.state === 'suspended' ||
1427
+ audioContext.state === 'closed'
1428
+ ) {
1429
+ source.disconnect();
1430
+ setIsPlayingAudio(false);
1431
+ } else if ((audioContext.state as string) === 'interrupted') {
1432
+ audioContext.resume();
1444
1433
  }
1445
- }
1446
- } else {
1434
+ };
1435
+
1447
1436
  audioContext.resume();
1437
+
1438
+ if (speechSynthesizer) {
1439
+ speechSynthesizer.close();
1440
+ speechSynthesizer = null;
1441
+ }
1442
+ } catch (e) {
1443
+ console.error('speak error: ', e);
1444
+ window.speechSynthesis.speak(new SpeechSynthesisUtterance(text));
1448
1445
  setIsPlayingAudio(false);
1446
+
1447
+ if (speechSynthesizer) {
1448
+ speechSynthesizer.close();
1449
+ speechSynthesizer = null;
1450
+ }
1449
1451
  }
1450
- },
1451
- error => {
1452
- console.error('speak:', error);
1453
- window.speechSynthesis.speak(new SpeechSynthesisUtterance(text));
1452
+ } else {
1453
+ audioContext.resume();
1454
1454
  setIsPlayingAudio(false);
1455
1455
  }
1456
- );
1456
+ },
1457
+ error => {
1458
+ console.error('speak:', error);
1459
+ window.speechSynthesis.speak(new SpeechSynthesisUtterance(text));
1460
+ setIsPlayingAudio(false);
1461
+ }
1462
+ );
1457
1463
 
1458
- setMemoriTyping(false);
1459
- },
1460
- [userLang, speakerMuted]
1461
- );
1464
+ setMemoriTyping(false);
1465
+ };
1462
1466
  const stopAudio = () => {
1463
1467
  setIsPlayingAudio(false);
1464
1468
  try {