@memori.ai/memori-react 8.40.0 → 8.40.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.
@@ -205,6 +205,7 @@ const MemoriWidget = ({ memori, memoriConfigs, ownerUserID, ownerUserName, tenan
205
205
  }, [loginToken, user === null || user === void 0 ? void 0 : user.userID]);
206
206
  const [showLoginDrawer, setShowLoginDrawer] = useState(false);
207
207
  const [clickedStart, setClickedStart] = useState(false);
208
+ const sessionStartingRef = useRef(false);
208
209
  const language = ((_d = (_c = (_b = memori.culture) === null || _b === void 0 ? void 0 : _b.split('-')) === null || _c === void 0 ? void 0 : _c[0]) === null || _d === void 0 ? void 0 : _d.toUpperCase()) ||
209
210
  ((_j = (_h = (_g = (_f = memoriConfigs === null || memoriConfigs === void 0 ? void 0 : memoriConfigs.find(c => c.memoriConfigID === memori.memoriConfigurationID)) === null || _f === void 0 ? void 0 : _f.culture) === null || _g === void 0 ? void 0 : _g.split('-')) === null || _h === void 0 ? void 0 : _h[0]) === null || _j === void 0 ? void 0 : _j.toUpperCase());
210
211
  const integrationConfig = (integration === null || integration === void 0 ? void 0 : integration.customData)
@@ -1043,6 +1044,7 @@ const MemoriWidget = ({ memori, memoriConfigs, ownerUserID, ownerUserName, tenan
1043
1044
  return () => {
1044
1045
  setHasUserActivatedSpeak(false);
1045
1046
  setClickedStart(false);
1047
+ sessionStartingRef.current = false;
1046
1048
  clearInteractionTimeout();
1047
1049
  timeoutRef.current = undefined;
1048
1050
  };
@@ -1631,6 +1633,7 @@ const MemoriWidget = ({ memori, memoriConfigs, ownerUserID, ownerUserName, tenan
1631
1633
  if (!sessionID && !!minAge && !birth) {
1632
1634
  setShowAgeVerification(true);
1633
1635
  setClickedStart(false);
1636
+ return;
1634
1637
  }
1635
1638
  else if (!sessionID &&
1636
1639
  memori.privacyType !== 'PUBLIC' &&
@@ -1642,82 +1645,95 @@ const MemoriWidget = ({ memori, memoriConfigs, ownerUserID, ownerUserName, tenan
1642
1645
  return;
1643
1646
  }
1644
1647
  else if (!sessionID || initialSessionExpired) {
1645
- setClickedStart(false);
1646
- const session = await fetchSession({
1647
- memoriID: memori.engineMemoriID,
1648
- password: secret || memoriPwd || memori.secretToken,
1649
- tag: personification === null || personification === void 0 ? void 0 : personification.tag,
1650
- pin: personification === null || personification === void 0 ? void 0 : personification.pin,
1651
- continueFromChatLogID: chatLog === null || chatLog === void 0 ? void 0 : chatLog.chatLogID,
1652
- initialContextVars: {
1653
- LANG: userLang,
1654
- PATHNAME: (_a = window.location.pathname) === null || _a === void 0 ? void 0 : _a.toUpperCase(),
1655
- ROUTE: ((_d = (_c = (_b = window.location.pathname) === null || _b === void 0 ? void 0 : _b.split('/')) === null || _c === void 0 ? void 0 : _c.pop()) === null || _d === void 0 ? void 0 : _d.toUpperCase()) || '',
1656
- ...((!chatLog
1657
- ? initialContextVars
1658
- : chatLog.lines[chatLog.lines.length - 1].contextVars) || {}),
1659
- },
1660
- initialQuestion: chatLog ? undefined : initialQuestion,
1661
- birthDate: birth,
1662
- additionalInfo: {
1663
- ...(additionalInfo || {}),
1664
- loginToken: (_g = (_f = userToken !== null && userToken !== void 0 ? userToken : loginToken) !== null && _f !== void 0 ? _f : additionalInfo === null || additionalInfo === void 0 ? void 0 : additionalInfo.loginToken) !== null && _g !== void 0 ? _g : authToken,
1665
- language: ((_k = userLang !== null && userLang !== void 0 ? userLang : (_j = (_h = memori.culture) === null || _h === void 0 ? void 0 : _h.split('-')) === null || _j === void 0 ? void 0 : _j[0]) !== null && _k !== void 0 ? _k : 'IT').toLowerCase(),
1666
- timeZoneOffset: new Date().getTimezoneOffset().toString(),
1667
- },
1668
- });
1669
- if (session === null || session === void 0 ? void 0 : session.dialogState) {
1670
- if (!chatLog) {
1671
- setHistory([]);
1672
- await translateAndSpeak(session.dialogState, userLang);
1673
- setHasUserActivatedSpeak(true);
1674
- }
1675
- else {
1676
- const messages = chatLog.lines.map((l, i) => {
1677
- var _a, _b;
1678
- return ({
1679
- text: l.text,
1680
- media: (_b = (_a = l.media) === null || _a === void 0 ? void 0 : _a.filter(m => allowedMediaTypes.includes(m.mimeType))) === null || _b === void 0 ? void 0 : _b.map(m => ({
1681
- mediumID: `${i}-${m.mimeType}`,
1682
- ...m,
1683
- })),
1684
- fromUser: l.inbound,
1685
- llmUsage: l.llmUsage,
1686
- timestamp: l.timestamp,
1687
- emitter: l.emitter,
1688
- initial: i === 0,
1689
- });
1690
- });
1691
- translatedMessages = messages !== null && messages !== void 0 ? messages : [];
1692
- if (language.toUpperCase() !== userLang.toUpperCase() &&
1693
- isMultilanguageEnabled) {
1694
- try {
1695
- translatedMessages = await Promise.all(messages.map(async (m) => {
1696
- if ('originalText' in m && m.originalText) {
1697
- return m;
1698
- }
1699
- return {
1648
+ if (sessionStartingRef.current) {
1649
+ return;
1650
+ }
1651
+ sessionStartingRef.current = true;
1652
+ try {
1653
+ const session = await fetchSession({
1654
+ memoriID: memori.engineMemoriID,
1655
+ password: secret || memoriPwd || memori.secretToken,
1656
+ tag: personification === null || personification === void 0 ? void 0 : personification.tag,
1657
+ pin: personification === null || personification === void 0 ? void 0 : personification.pin,
1658
+ continueFromChatLogID: chatLog === null || chatLog === void 0 ? void 0 : chatLog.chatLogID,
1659
+ initialContextVars: {
1660
+ LANG: userLang,
1661
+ PATHNAME: (_a = window.location.pathname) === null || _a === void 0 ? void 0 : _a.toUpperCase(),
1662
+ ROUTE: ((_d = (_c = (_b = window.location.pathname) === null || _b === void 0 ? void 0 : _b.split('/')) === null || _c === void 0 ? void 0 : _c.pop()) === null || _d === void 0 ? void 0 : _d.toUpperCase()) ||
1663
+ '',
1664
+ ...((!chatLog
1665
+ ? initialContextVars
1666
+ : chatLog.lines[chatLog.lines.length - 1].contextVars) || {}),
1667
+ },
1668
+ initialQuestion: chatLog ? undefined : initialQuestion,
1669
+ birthDate: birth,
1670
+ additionalInfo: {
1671
+ ...(additionalInfo || {}),
1672
+ loginToken: (_g = (_f = userToken !== null && userToken !== void 0 ? userToken : loginToken) !== null && _f !== void 0 ? _f : additionalInfo === null || additionalInfo === void 0 ? void 0 : additionalInfo.loginToken) !== null && _g !== void 0 ? _g : authToken,
1673
+ language: ((_k = userLang !== null && userLang !== void 0 ? userLang : (_j = (_h = memori.culture) === null || _h === void 0 ? void 0 : _h.split('-')) === null || _j === void 0 ? void 0 : _j[0]) !== null && _k !== void 0 ? _k : 'IT').toLowerCase(),
1674
+ timeZoneOffset: new Date().getTimezoneOffset().toString(),
1675
+ },
1676
+ });
1677
+ if (session === null || session === void 0 ? void 0 : session.dialogState) {
1678
+ if (!chatLog) {
1679
+ setHistory([]);
1680
+ await translateAndSpeak(session.dialogState, userLang);
1681
+ setHasUserActivatedSpeak(true);
1682
+ setClickedStart(false);
1683
+ }
1684
+ else {
1685
+ const messages = chatLog.lines.map((l, i) => {
1686
+ var _a, _b;
1687
+ return ({
1688
+ text: l.text,
1689
+ media: (_b = (_a = l.media) === null || _a === void 0 ? void 0 : _a.filter(m => allowedMediaTypes.includes(m.mimeType))) === null || _b === void 0 ? void 0 : _b.map(m => ({
1690
+ mediumID: `${i}-${m.mimeType}`,
1700
1691
  ...m,
1701
- originalText: m.text,
1702
- text: (await getTranslation(m.text, userLang, language, baseUrl)).text,
1703
- };
1704
- }));
1705
- }
1706
- catch (e) {
1707
- console.error('[onClickStart] Error translating messages:', e);
1692
+ })),
1693
+ fromUser: l.inbound,
1694
+ llmUsage: l.llmUsage,
1695
+ timestamp: l.timestamp,
1696
+ emitter: l.emitter,
1697
+ initial: i === 0,
1698
+ });
1699
+ });
1700
+ translatedMessages = messages !== null && messages !== void 0 ? messages : [];
1701
+ if (language.toUpperCase() !== userLang.toUpperCase() &&
1702
+ isMultilanguageEnabled) {
1703
+ try {
1704
+ translatedMessages = await Promise.all(messages.map(async (m) => {
1705
+ if ('originalText' in m && m.originalText) {
1706
+ return m;
1707
+ }
1708
+ return {
1709
+ ...m,
1710
+ originalText: m.text,
1711
+ text: (await getTranslation(m.text, userLang, language, baseUrl)).text,
1712
+ };
1713
+ }));
1714
+ }
1715
+ catch (e) {
1716
+ console.error('[onClickStart] Error translating messages:', e);
1717
+ }
1708
1718
  }
1719
+ setHistory(translatedMessages);
1720
+ translateDialogState(session.dialogState, userLang, undefined, true).finally(() => {
1721
+ setHasUserActivatedSpeak(true);
1722
+ setClickedStart(false);
1723
+ });
1709
1724
  }
1710
- setHistory(translatedMessages);
1711
- translateDialogState(session.dialogState, userLang, undefined, true).finally(() => {
1712
- setHasUserActivatedSpeak(true);
1713
- });
1725
+ }
1726
+ else if ((session === null || session === void 0 ? void 0 : session.resultCode) === 0) {
1727
+ sessionStartingRef.current = false;
1728
+ await onClickStart(session || undefined);
1729
+ }
1730
+ else {
1731
+ setLoading(false);
1732
+ setClickedStart(false);
1714
1733
  }
1715
1734
  }
1716
- else if ((session === null || session === void 0 ? void 0 : session.resultCode) === 0) {
1717
- await onClickStart(session || undefined);
1718
- }
1719
- else {
1720
- setLoading(false);
1735
+ finally {
1736
+ sessionStartingRef.current = false;
1721
1737
  }
1722
1738
  return;
1723
1739
  }
@@ -1727,7 +1743,6 @@ const MemoriWidget = ({ memori, memoriConfigs, ownerUserID, ownerUserName, tenan
1727
1743
  if (response.resultCode !== 0 || !currentState) {
1728
1744
  const { chatLogs } = await getSessionChatLogs(sessionID, sessionID);
1729
1745
  setSessionId(undefined);
1730
- setClickedStart(false);
1731
1746
  await onClickStart(undefined, true, chatLogs === null || chatLogs === void 0 ? void 0 : chatLogs[0]);
1732
1747
  return;
1733
1748
  }
@@ -1754,6 +1769,7 @@ const MemoriWidget = ({ memori, memoriConfigs, ownerUserID, ownerUserName, tenan
1754
1769
  ...(initialContextVars || {}),
1755
1770
  }, initialQuestion, birth).then(() => {
1756
1771
  setHasUserActivatedSpeak(true);
1772
+ setClickedStart(false);
1757
1773
  });
1758
1774
  }
1759
1775
  }
@@ -1780,6 +1796,7 @@ const MemoriWidget = ({ memori, memoriConfigs, ownerUserID, ownerUserName, tenan
1780
1796
  ...(initialContextVars || {}),
1781
1797
  }, initialQuestion, birth).then(() => {
1782
1798
  setHasUserActivatedSpeak(true);
1799
+ setClickedStart(false);
1783
1800
  });
1784
1801
  }
1785
1802
  }
@@ -1823,6 +1840,7 @@ const MemoriWidget = ({ memori, memoriConfigs, ownerUserID, ownerUserName, tenan
1823
1840
  if ((!!(translatedMessages === null || translatedMessages === void 0 ? void 0 : translatedMessages.length) && translatedMessages.length > 1) ||
1824
1841
  !initialQuestion) {
1825
1842
  setHasUserActivatedSpeak(true);
1843
+ setClickedStart(false);
1826
1844
  await translateAndSpeak(currentState, userLang, undefined, !!(translatedMessages === null || translatedMessages === void 0 ? void 0 : translatedMessages.length));
1827
1845
  }
1828
1846
  else {
@@ -1893,10 +1911,14 @@ const MemoriWidget = ({ memori, memoriConfigs, ownerUserID, ownerUserName, tenan
1893
1911
  }
1894
1912
  }, [memoriPwd, memori, memoriTokens, birthDate, sessionId, userLang, position]);
1895
1913
  useEffect(() => {
1896
- if (!clickedStart && autoStart && selectedLayout !== 'HIDDEN_CHAT') {
1914
+ if (!clickedStart &&
1915
+ !sessionStartingRef.current &&
1916
+ !sessionId &&
1917
+ autoStart &&
1918
+ selectedLayout !== 'HIDDEN_CHAT') {
1897
1919
  onClickStart();
1898
1920
  }
1899
- }, [clickedStart, autoStart, selectedLayout]);
1921
+ }, [clickedStart, autoStart, selectedLayout, sessionId]);
1900
1922
  useEffect(() => {
1901
1923
  const targetNode = document.querySelector(`memori-client[memoriname="${memori.name}"]`) ||
1902
1924
  document.querySelector(`memori-client[memoriid="${memori.memoriID}"]`) ||