@memori.ai/memori-react 2.6.4 → 2.6.5

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.
@@ -146,6 +146,8 @@ let speechSynthesizer: SpeechSynthesizer | null;
146
146
  let audioDestination: SpeakerAudioDestination;
147
147
  let audioContext: IAudioContext;
148
148
 
149
+ let memoriPassword: string | undefined;
150
+
149
151
  export interface LayoutProps {
150
152
  Header?: typeof Header;
151
153
  headerProps?: HeaderProps;
@@ -646,7 +648,6 @@ const MemoriWidget = ({
646
648
  setAuthModalState('password');
647
649
  return;
648
650
  }
649
-
650
651
  let storageBirthDate = getLocalConfig<string | undefined>(
651
652
  'birthDate',
652
653
  undefined
@@ -712,15 +713,31 @@ const MemoriWidget = ({
712
713
  ) => {
713
714
  setLoading(true);
714
715
  try {
716
+ if (
717
+ memori.privacyType !== 'PUBLIC' &&
718
+ !password &&
719
+ !memori.secretToken &&
720
+ !memoriPwd &&
721
+ !recoveryTokens &&
722
+ !memoriTokens
723
+ ) {
724
+ setAuthModalState('password');
725
+ return;
726
+ }
727
+
715
728
  let storageBirthDate = getLocalConfig<string | undefined>(
716
729
  'birthDate',
717
730
  undefined
718
731
  );
732
+ if (!(birthDate || storageBirthDate) && !!minAge) {
733
+ setShowAgeVerification(true);
734
+ return;
735
+ }
719
736
 
720
737
  const { sessionID, currentState, ...response } = await initSession({
721
738
  memoriID: memori.engineMemoriID ?? '',
722
- password,
723
- recoveryTokens,
739
+ password: password || memoriPwd || memori.secretToken,
740
+ recoveryTokens: recoveryTokens || memoriTokens,
724
741
  tag,
725
742
  pin,
726
743
  initialContextVars,
@@ -826,7 +843,7 @@ const MemoriWidget = ({
826
843
 
827
844
  fetchSession({
828
845
  memoriID: memori.engineMemoriID ?? '',
829
- password: secret || memori.secretToken,
846
+ password: secret || memoriPwd || memori.secretToken,
830
847
  tag: memori.giverTag,
831
848
  pin: memori.giverPIN,
832
849
  initialContextVars,
@@ -1845,241 +1862,254 @@ const MemoriWidget = ({
1845
1862
  sendMessage(text, undefined, undefined, false, translatedText);
1846
1863
  };
1847
1864
 
1848
- const onClickStart = async (session?: {
1849
- dialogState: DialogState;
1850
- sessionID: string;
1851
- }) => {
1852
- const sessionID = session?.sessionID || sessionId;
1853
- const dialogState = session?.dialogState || currentDialogState;
1854
- setClickedStart(true);
1855
-
1856
- let memoriAudioElement = document.getElementById(
1857
- 'memori-audio'
1858
- ) as HTMLAudioElement;
1859
- let isSafari =
1860
- window.navigator.userAgent.includes('Safari') &&
1861
- !window.navigator.userAgent.includes('Chrome');
1862
- if (memoriAudioElement && isSafari) {
1863
- memoriAudioElement.muted = false;
1864
- memoriAudioElement.play().catch((e: any) => {
1865
- console.error('error playing intro audio', e);
1866
- });
1867
- }
1865
+ const onClickStart = useCallback(
1866
+ async (session?: { dialogState: DialogState; sessionID: string }) => {
1867
+ const sessionID = session?.sessionID || sessionId;
1868
+ const dialogState = session?.dialogState || currentDialogState;
1869
+ setClickedStart(true);
1870
+
1871
+ let memoriAudioElement = document.getElementById(
1872
+ 'memori-audio'
1873
+ ) as HTMLAudioElement;
1874
+ let isSafari =
1875
+ window.navigator.userAgent.includes('Safari') &&
1876
+ !window.navigator.userAgent.includes('Chrome');
1877
+ if (memoriAudioElement && isSafari) {
1878
+ memoriAudioElement.muted = false;
1879
+ memoriAudioElement.play().catch((e: any) => {
1880
+ console.error('error playing intro audio', e);
1881
+ });
1882
+ }
1868
1883
 
1869
- let storageBirthDate = getLocalConfig<string | undefined>(
1870
- 'birthDate',
1871
- undefined
1872
- );
1873
- let birth = birthDate || storageBirthDate || undefined;
1884
+ let storageBirthDate = getLocalConfig<string | undefined>(
1885
+ 'birthDate',
1886
+ undefined
1887
+ );
1888
+ let birth = birthDate || storageBirthDate || undefined;
1874
1889
 
1875
- if (
1876
- (!sessionID &&
1877
- memori.privacyType !== 'PUBLIC' &&
1878
- !memori.secretToken &&
1879
- !memoriPwd &&
1880
- !memoriTokens) ||
1881
- (!sessionID && gotErrorInOpening)
1882
- ) {
1883
- setAuthModalState('password');
1884
- setClickedStart(false);
1885
- return;
1886
- } else if (!sessionID && !!minAge && !birth) {
1887
- setShowAgeVerification(true);
1888
- setClickedStart(false);
1889
- } else if (!sessionID) {
1890
- setClickedStart(false);
1891
- setGotErrorInOpening(false);
1892
- const session = await fetchSession({
1893
- memoriID: memori.engineMemoriID!,
1894
- password: secret || memori.secretToken,
1895
- tag: personification?.tag,
1896
- pin: personification?.pin,
1897
- initialContextVars,
1898
- initialQuestion,
1899
- birthDate: birth,
1900
- });
1890
+ if (
1891
+ (!sessionID &&
1892
+ memori.privacyType !== 'PUBLIC' &&
1893
+ !memori.secretToken &&
1894
+ !memoriPwd &&
1895
+ !memoriTokens) ||
1896
+ (!sessionID && gotErrorInOpening)
1897
+ ) {
1898
+ setAuthModalState('password');
1899
+ setClickedStart(false);
1900
+ return;
1901
+ } else if (!sessionID && !!minAge && !birth) {
1902
+ setShowAgeVerification(true);
1903
+ setClickedStart(false);
1904
+ } else if (!sessionID) {
1905
+ setClickedStart(false);
1906
+ setGotErrorInOpening(false);
1907
+ const session = await fetchSession({
1908
+ memoriID: memori.engineMemoriID!,
1909
+ password: secret || memoriPwd || memori.secretToken,
1910
+ tag: personification?.tag,
1911
+ pin: personification?.pin,
1912
+ initialContextVars,
1913
+ initialQuestion,
1914
+ birthDate: birth,
1915
+ });
1901
1916
 
1902
- if (session?.dialogState) {
1903
- // reset history
1904
- setHistory([]);
1917
+ if (session?.dialogState) {
1918
+ // reset history
1919
+ setHistory([]);
1905
1920
 
1906
- translateDialogState(session.dialogState, userLang)
1907
- .then(ts => {
1908
- if (ts.emission) {
1909
- speak(ts.emission);
1910
- }
1911
- })
1912
- .finally(() => {
1913
- setHasUserActivatedSpeak(true);
1914
- });
1915
- } else {
1916
- await onClickStart(session || undefined);
1917
- }
1921
+ translateDialogState(session.dialogState, userLang)
1922
+ .then(ts => {
1923
+ if (ts.emission) {
1924
+ speak(ts.emission);
1925
+ }
1926
+ })
1927
+ .finally(() => {
1928
+ setHasUserActivatedSpeak(true);
1929
+ });
1930
+ } else {
1931
+ await onClickStart(session || undefined);
1932
+ }
1918
1933
 
1919
- return;
1920
- } else if (initialSessionID) {
1921
- // check if session is valid and not expired
1922
- const { currentState, ...response } = await getSession(sessionID);
1923
- if (response.resultCode !== 0 || !currentState) {
1924
- console.debug('session expired, opening new session');
1925
- setGotErrorInOpening(true);
1926
- setSessionId(undefined);
1927
- setClickedStart(false);
1928
- await onClickStart();
1929
1934
  return;
1930
- }
1935
+ } else if (initialSessionID) {
1936
+ // check if session is valid and not expired
1937
+ const { currentState, ...response } = await getSession(sessionID);
1938
+ if (response.resultCode !== 0 || !currentState) {
1939
+ console.debug('session expired, opening new session');
1940
+ setGotErrorInOpening(true);
1941
+ setSessionId(undefined);
1942
+ setClickedStart(false);
1943
+ await onClickStart();
1944
+ return;
1945
+ }
1931
1946
 
1932
- // reset history
1933
- setHistory([]);
1947
+ // reset history
1948
+ setHistory([]);
1934
1949
 
1935
- // checks engine state for current tag
1936
- // opening session would have already correct tag
1937
- // otherwise change tag to anonymous for test, giver for instruct, receiver if set
1950
+ // checks engine state for current tag
1951
+ // opening session would have already correct tag
1952
+ // otherwise change tag to anonymous for test, giver for instruct, receiver if set
1938
1953
 
1939
- // test if current tag is giver on instruct
1940
- if (
1941
- instruct &&
1942
- memori.giverTag &&
1943
- currentDialogState?.currentTag !== memori.giverTag
1944
- ) {
1945
- try {
1946
- console.debug('change tag #0');
1947
- // reset tag
1948
- await changeTag(memori.engineMemoriID!, sessionID, '-');
1949
- // change tag to giver
1950
- const session = await changeTag(
1951
- memori.engineMemoriID!,
1952
- sessionID,
1953
- memori.giverTag,
1954
- memori.giverPIN
1955
- );
1954
+ // test if current tag is giver on instruct
1955
+ if (
1956
+ instruct &&
1957
+ memori.giverTag &&
1958
+ currentDialogState?.currentTag !== memori.giverTag
1959
+ ) {
1960
+ try {
1961
+ console.debug('change tag #0');
1962
+ // reset tag
1963
+ await changeTag(memori.engineMemoriID!, sessionID, '-');
1964
+ // change tag to giver
1965
+ const session = await changeTag(
1966
+ memori.engineMemoriID!,
1967
+ sessionID,
1968
+ memori.giverTag,
1969
+ memori.giverPIN
1970
+ );
1956
1971
 
1957
- if (session && session.resultCode === 0) {
1958
- translateDialogState(session.currentState, userLang)
1959
- .then(ts => {
1960
- if (ts.emission) {
1961
- speak(ts.emission);
1962
- }
1963
- })
1964
- .finally(() => {
1965
- setHasUserActivatedSpeak(true);
1966
- });
1967
- } else {
1968
- console.error('session #1', session);
1969
- throw new Error('No session');
1972
+ if (session && session.resultCode === 0) {
1973
+ translateDialogState(session.currentState, userLang)
1974
+ .then(ts => {
1975
+ if (ts.emission) {
1976
+ speak(ts.emission);
1977
+ }
1978
+ })
1979
+ .finally(() => {
1980
+ setHasUserActivatedSpeak(true);
1981
+ });
1982
+ } else {
1983
+ console.error('session #1', session);
1984
+ throw new Error('No session');
1985
+ }
1986
+ } catch (e) {
1987
+ console.error('session #2', e);
1988
+ reopenSession(
1989
+ true,
1990
+ memori?.secretToken,
1991
+ undefined,
1992
+ memori?.giverTag,
1993
+ memori?.giverPIN,
1994
+ initialContextVars,
1995
+ initialQuestion,
1996
+ birth
1997
+ ).then(() => {
1998
+ setHasUserActivatedSpeak(true);
1999
+ });
1970
2000
  }
1971
- } catch (e) {
1972
- console.error('session #2', e);
1973
- reopenSession(
1974
- true,
1975
- memori?.secretToken,
1976
- undefined,
1977
- memori?.giverTag,
1978
- memori?.giverPIN,
1979
- initialContextVars,
1980
- initialQuestion,
1981
- birth
1982
- ).then(() => {
1983
- setHasUserActivatedSpeak(true);
1984
- });
1985
- }
1986
- } else if (
1987
- // test if current tag is receiver on test as it is requested
1988
- !instruct &&
1989
- personification &&
1990
- currentDialogState?.currentTag !== personification.tag
1991
- ) {
1992
- try {
1993
- console.debug('change tag #3');
1994
- // reset tag
1995
- await changeTag(memori.engineMemoriID!, sessionID, '-');
1996
- // change tag to receiver
1997
- const session = await changeTag(
1998
- memori.engineMemoriID!,
1999
- sessionID,
2000
- personification.tag,
2001
- personification.pin
2002
- );
2001
+ } else if (
2002
+ // test if current tag is receiver on test as it is requested
2003
+ !instruct &&
2004
+ personification &&
2005
+ currentDialogState?.currentTag !== personification.tag
2006
+ ) {
2007
+ try {
2008
+ console.debug('change tag #3');
2009
+ // reset tag
2010
+ await changeTag(memori.engineMemoriID!, sessionID, '-');
2011
+ // change tag to receiver
2012
+ const session = await changeTag(
2013
+ memori.engineMemoriID!,
2014
+ sessionID,
2015
+ personification.tag,
2016
+ personification.pin
2017
+ );
2003
2018
 
2004
- if (session && session.resultCode === 0) {
2005
- translateDialogState(session.currentState, userLang)
2006
- .then(ts => {
2007
- if (ts.emission) {
2008
- speak(ts.emission);
2009
- }
2010
- })
2011
- .finally(() => {
2012
- setHasUserActivatedSpeak(true);
2013
- });
2014
- } else {
2015
- console.error('session #4', session);
2016
- throw new Error('No session');
2019
+ if (session && session.resultCode === 0) {
2020
+ translateDialogState(session.currentState, userLang)
2021
+ .then(ts => {
2022
+ if (ts.emission) {
2023
+ speak(ts.emission);
2024
+ }
2025
+ })
2026
+ .finally(() => {
2027
+ setHasUserActivatedSpeak(true);
2028
+ });
2029
+ } else {
2030
+ console.error('session #4', session);
2031
+ throw new Error('No session');
2032
+ }
2033
+ } catch (e) {
2034
+ console.error('session #5', e);
2035
+ reopenSession(
2036
+ true,
2037
+ memori?.secretToken,
2038
+ undefined,
2039
+ personification.tag,
2040
+ personification.pin,
2041
+ initialContextVars,
2042
+ initialQuestion,
2043
+ birth
2044
+ ).then(() => {
2045
+ setHasUserActivatedSpeak(true);
2046
+ });
2017
2047
  }
2018
- } catch (e) {
2019
- console.error('session #5', e);
2020
- reopenSession(
2021
- true,
2022
- memori?.secretToken,
2023
- undefined,
2024
- personification.tag,
2025
- personification.pin,
2026
- initialContextVars,
2027
- initialQuestion,
2028
- birth
2029
- ).then(() => {
2030
- setHasUserActivatedSpeak(true);
2031
- });
2032
- }
2033
- } else if (
2034
- // test if current tag is anonymous on test without personification
2035
- // this is the default case with anonymous tag
2036
- !instruct &&
2037
- !personification &&
2038
- currentDialogState?.currentTag !== anonTag
2039
- ) {
2040
- try {
2041
- console.debug('change tag #6');
2042
- // reset tag
2043
- await changeTag(memori.engineMemoriID!, sessionID, '-');
2044
- // change tag to anonymous
2045
- const session = await changeTag(
2046
- memori.engineMemoriID!,
2047
- sessionID,
2048
- anonTag
2049
- );
2048
+ } else if (
2049
+ // test if current tag is anonymous on test without personification
2050
+ // this is the default case with anonymous tag
2051
+ !instruct &&
2052
+ !personification &&
2053
+ currentDialogState?.currentTag !== anonTag
2054
+ ) {
2055
+ try {
2056
+ console.debug('change tag #6');
2057
+ // reset tag
2058
+ await changeTag(memori.engineMemoriID!, sessionID, '-');
2059
+ // change tag to anonymous
2060
+ const session = await changeTag(
2061
+ memori.engineMemoriID!,
2062
+ sessionID,
2063
+ anonTag
2064
+ );
2050
2065
 
2051
- if (session && session.resultCode === 0) {
2052
- translateDialogState(session.currentState, userLang)
2053
- .then(ts => {
2054
- if (ts.emission) {
2055
- speak(ts.emission);
2056
- }
2057
- })
2058
- .finally(() => {
2059
- setHasUserActivatedSpeak(true);
2060
- });
2061
- } else {
2062
- console.error('session #7', session);
2063
- throw new Error('No session');
2066
+ if (session && session.resultCode === 0) {
2067
+ translateDialogState(session.currentState, userLang)
2068
+ .then(ts => {
2069
+ if (ts.emission) {
2070
+ speak(ts.emission);
2071
+ }
2072
+ })
2073
+ .finally(() => {
2074
+ setHasUserActivatedSpeak(true);
2075
+ });
2076
+ } else {
2077
+ console.error('session #7', session);
2078
+ throw new Error('No session');
2079
+ }
2080
+ } catch (e) {
2081
+ console.error('session #8', e);
2082
+ reopenSession(
2083
+ true,
2084
+ memori?.secretToken,
2085
+ undefined,
2086
+ undefined,
2087
+ undefined,
2088
+ initialContextVars,
2089
+ initialQuestion,
2090
+ birth
2091
+ ).then(() => {
2092
+ setHasUserActivatedSpeak(true);
2093
+ });
2064
2094
  }
2065
- } catch (e) {
2066
- console.error('session #8', e);
2067
- reopenSession(
2068
- true,
2069
- memori?.secretToken,
2070
- undefined,
2071
- undefined,
2072
- undefined,
2073
- initialContextVars,
2074
- initialQuestion,
2075
- birth
2076
- ).then(() => {
2077
- setHasUserActivatedSpeak(true);
2078
- });
2095
+ } else {
2096
+ // no need to change tag
2097
+ translateDialogState(currentState, userLang)
2098
+ .then(ts => {
2099
+ if (ts.emission) {
2100
+ speak(ts.emission);
2101
+ }
2102
+ })
2103
+ .finally(() => {
2104
+ setHasUserActivatedSpeak(true);
2105
+ });
2079
2106
  }
2080
2107
  } else {
2081
- // no need to change tag
2082
- translateDialogState(currentState, userLang)
2108
+ // reset history
2109
+ setHistory([]);
2110
+
2111
+ // everything is fine, just translate dialog state and activate chat
2112
+ translateDialogState(dialogState!, userLang)
2083
2113
  .then(ts => {
2084
2114
  if (ts.emission) {
2085
2115
  speak(ts.emission);
@@ -2089,22 +2119,9 @@ const MemoriWidget = ({
2089
2119
  setHasUserActivatedSpeak(true);
2090
2120
  });
2091
2121
  }
2092
- } else {
2093
- // reset history
2094
- setHistory([]);
2095
-
2096
- // everything is fine, just translate dialog state and activate chat
2097
- translateDialogState(dialogState!, userLang)
2098
- .then(ts => {
2099
- if (ts.emission) {
2100
- speak(ts.emission);
2101
- }
2102
- })
2103
- .finally(() => {
2104
- setHasUserActivatedSpeak(true);
2105
- });
2106
- }
2107
- };
2122
+ },
2123
+ [memoriPwd, memori, memoriTokens, birthDate, sessionId]
2124
+ );
2108
2125
 
2109
2126
  const [loginToken, setLoginToken] = useState<string | undefined>(authToken);
2110
2127
  useEffect(() => {
@@ -2362,6 +2379,7 @@ const MemoriWidget = ({
2362
2379
  showTokens={memori.privacyType === 'SECRET'}
2363
2380
  onFinish={async (values: any) => {
2364
2381
  if (values['password']) setMemoriPwd(values['password']);
2382
+ if (values['password']) memoriPassword = values['password'];
2365
2383
  if (values['tokens']) setMemoriTokens(values['tokens']);
2366
2384
 
2367
2385
  reopenSession(
@@ -2401,8 +2419,8 @@ const MemoriWidget = ({
2401
2419
 
2402
2420
  reopenSession(
2403
2421
  !sessionId,
2404
- memori?.secretToken,
2405
- undefined,
2422
+ memoriPassword || memoriPwd || memori?.secretToken,
2423
+ memoriTokens,
2406
2424
  instruct ? memori.giverTag : undefined,
2407
2425
  instruct ? memori.giverPIN : undefined,
2408
2426
  initialContextVars,
@@ -2411,6 +2429,7 @@ const MemoriWidget = ({
2411
2429
  )
2412
2430
  .then(state => {
2413
2431
  setShowAgeVerification(false);
2432
+ setAuthModalState(null);
2414
2433
  onClickStart(state || undefined);
2415
2434
  })
2416
2435
  .catch(() => {