@memori.ai/memori-react 1.0.0-alpha.21 → 1.0.0-alpha.23

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/CHANGELOG.md CHANGED
@@ -1,5 +1,24 @@
1
1
 
2
2
 
3
+ ## [1.0.0-alpha.23](https://github.com/memori-ai/memori-react/compare/v1.0.0-alpha.22...v1.0.0-alpha.23) (2023-01-19)
4
+
5
+
6
+ ### Changes
7
+
8
+ * fixes for listening events ([76bc5f6](https://github.com/memori-ai/memori-react/commit/76bc5f616538d6621ff51b7ad41a9b07b8ad59ba))
9
+
10
+ ## [1.0.0-alpha.22](https://github.com/memori-ai/memori-react/compare/v1.0.0-alpha.21...v1.0.0-alpha.22) (2023-01-17)
11
+
12
+
13
+ ### Bug Fixes
14
+
15
+ * audio issues, refactor tts ([e66c83a](https://github.com/memori-ai/memori-react/commit/e66c83a35e1a98ae7e24ee532923653d9032c3f3))
16
+
17
+
18
+ ### Maintenance
19
+
20
+ * cleanup ([bd755c7](https://github.com/memori-ai/memori-react/commit/bd755c7de3736f4685e66076f058ff7ca6d513d2))
21
+
3
22
  ## [1.0.0-alpha.21](https://github.com/memori-ai/memori-react/compare/v1.0.0-alpha.20...v1.0.0-alpha.21) (2023-01-14)
4
23
 
5
24
 
@@ -17,7 +17,10 @@ const ChatInputs = ({ dialogState, instruct = false, sendOnEnter = 'click', setS
17
17
  (instruct || (dialogState === null || dialogState === void 0 ? void 0 : dialogState.acceptsMedia)) && (react_1.default.createElement(UploadMenu_1.default, { attachmentsMenuOpen: attachmentsMenuOpen, setAttachmentsMenuOpen: setAttachmentsMenuOpen, authToken: authToken, disabled: !(dialogState === null || dialogState === void 0 ? void 0 : dialogState.acceptsMedia) })),
18
18
  react_1.default.createElement(SendOnEnterMenu_1.default, { sendOnEnter: sendOnEnter, setSendOnEnter: setSendOnEnter }),
19
19
  react_1.default.createElement(ChatTextArea_1.default, { value: userMessage, onChange: onChangeUserMessage, onPressEnter: onTextareaPressEnter, onFocus: onTextareaFocus, onBlur: onTextareaBlur, disabled: ['R2', 'R3', 'R4', 'G3', 'X3'].includes((dialogState === null || dialogState === void 0 ? void 0 : dialogState.state) || '') }),
20
- react_1.default.createElement(Button_1.default, { shape: "circle", primary: !!(userMessage === null || userMessage === void 0 ? void 0 : userMessage.length), disabled: !userMessage || userMessage.length === 0, className: "memori-chat-inputs--send", onClick: () => sendMessage(userMessage), title: t('send') || 'Send', icon: react_1.default.createElement(Send_1.default, null) }),
20
+ react_1.default.createElement(Button_1.default, { shape: "circle", primary: !!(userMessage === null || userMessage === void 0 ? void 0 : userMessage.length), disabled: !userMessage || userMessage.length === 0, className: "memori-chat-inputs--send", onClick: () => {
21
+ sendMessage(userMessage);
22
+ speechSynthesis.speak(new SpeechSynthesisUtterance(''));
23
+ }, title: t('send') || 'Send', icon: react_1.default.createElement(Send_1.default, null) }),
21
24
  showMicrophone && (react_1.default.createElement(Button_1.default, { className: (0, classnames_1.default)('memori-chat-inputs--mic', {
22
25
  'memori-chat-inputs--mic--listening': listening,
23
26
  }), title: listening
@@ -1 +1 @@
1
- {"version":3,"file":"ChatInputs.js","sourceRoot":"","sources":["../../../src/components/ChatInputs/ChatInputs.tsx"],"names":[],"mappings":";;;AAAA,0DAA0B;AAE1B,kFAAkD;AAClD,iGAAiE;AACjE,wFAAwD;AACxD,kEAAkC;AAClC,iDAA+C;AAC/C,oEAA4B;AAC5B,iEAAiC;AACjC,6EAA6C;AAE7C,4BAA0B;AAyB1B,MAAM,UAAU,GAAoB,CAAC,EACnC,WAAW,EACX,QAAQ,GAAG,KAAK,EAChB,WAAW,GAAG,OAAO,EACrB,cAAc,EACd,mBAAmB,EACnB,sBAAsB,EACtB,WAAW,GAAG,EAAE,EAChB,mBAAmB,EACnB,WAAW,EACX,eAAe,EACf,cAAc,EACd,oBAAoB,EACpB,cAAc,GAAG,KAAK,EACtB,SAAS,GAAG,KAAK,EACjB,cAAc,GAAG,KAAK,EACtB,SAA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EACpC,OAAO,EAAE,GAAG,EAAE,CAAC,WAAW,CAAC,WAAW,CAAC,EACvC,KAAK,EAAE,CAAC,CAAC,MAAM,CAAC,IAAI,MAAM,EAC1B,IAAI,EAAE,8BAAC,cAAI,OAAG,GACd;QACD,cAAc,IAAI,CACjB,8BAAC,gBAAM,IACL,SAAS,EAAE,IAAA,oBAAE,EAAC,yBAAyB,EAAE;gBACvC,oCAAoC,EAAE,SAAS;aAChD,CAAC,EACF,KAAK,EACH,SAAS;gBACP,CAAC,CAAC,CAAC,CAAC,2CAA2C,CAAC,IAAI,WAAW;gBAC/D,CAAC,CAAC,CAAC,CAAC,kCAAkC,CAAC,IAAI,iBAAiB,EAEhE,QAAQ,EAAE,cAAc,EACxB,OAAO,EAAE,CAAC,CAAC,EAAE;gBACX,OAAO,CAAC,GAAG,CAAC,YAAY,EAAE,SAAS,EAAE,CAAC,CAAC,CAAC;gBACxC,IAAI,SAAS,EAAE;oBACb,aAAa,EAAE,CAAC;iBACjB;qBAAM;oBACL,SAAS,EAAE,CAAC;oBACZ,cAAc,EAAE,CAAC;iBAClB;YACH,CAAC,EACD,KAAK,EAAC,QAAQ,EACd,IAAI,EAAE,8BAAC,oBAAU,OAAG,GACpB,CACH,CACQ,CACZ,CAAC;AACJ,CAAC,CAAC;AAEF,kBAAe,UAAU,CAAC"}
1
+ {"version":3,"file":"ChatInputs.js","sourceRoot":"","sources":["../../../src/components/ChatInputs/ChatInputs.tsx"],"names":[],"mappings":";;;AAAA,0DAA0B;AAE1B,kFAAkD;AAClD,iGAAiE;AACjE,wFAAwD;AACxD,kEAAkC;AAClC,iDAA+C;AAC/C,oEAA4B;AAC5B,iEAAiC;AACjC,6EAA6C;AAE7C,4BAA0B;AAyB1B,MAAM,UAAU,GAAoB,CAAC,EACnC,WAAW,EACX,QAAQ,GAAG,KAAK,EAChB,WAAW,GAAG,OAAO,EACrB,cAAc,EACd,mBAAmB,EACnB,sBAAsB,EACtB,WAAW,GAAG,EAAE,EAChB,mB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X,WAAW,uBAAX,WAAW,CAAE,MAAM,CAAA,EAC9B,QAAQ,EAAE,CAAC,WAAW,IAAI,WAAW,CAAC,MAAM,KAAK,CAAC,EAClD,SAAS,EAAC,0BAA0B,EACpC,OAAO,EAAE,GAAG,EAAE;gBACZ,WAAW,CAAC,WAAW,CAAC,CAAC;gBACzB,eAAe,CAAC,KAAK,CAAC,IAAI,wBAAwB,CAAC,EAAE,CAAC,CAAC,CAAC;YAC1D,CAAC,EACD,KAAK,EAAE,CAAC,CAAC,MAAM,CAAC,IAAI,MAAM,EAC1B,IAAI,EAAE,8BAAC,cAAI,OAAG,GACd;QACD,cAAc,IAAI,CACjB,8BAAC,gBAAM,IACL,SAAS,EAAE,IAAA,oBAAE,EAAC,yBAAyB,EAAE;gBACvC,oCAAoC,EAAE,SAAS;aAChD,CAAC,EACF,KAAK,EACH,SAAS;gBACP,CAAC,CAAC,CAAC,CAAC,2CAA2C,CAAC,IAAI,WAAW;gBAC/D,CAAC,CAAC,CAAC,CAAC,kCAAkC,CAAC,IAAI,iBAAiB,EAEhE,QAAQ,EAAE,cAAc,EACxB,OAAO,EAAE,CAAC,CAAC,EAAE;gBACX,OAAO,CAAC,GAAG,CAAC,YAAY,EAAE,SAAS,EAAE,CAAC,CAAC,CAAC;gBACxC,IAAI,SAAS,EAAE;oBACb,aAAa,EAAE,CAAC;iBACjB;qBAAM;oBACL,SAAS,EAAE,CAAC;oBACZ,cAAc,EAAE,CAAC;iBAClB;YACH,CAAC,EACD,KAAK,EAAC,QAAQ,EACd,IAAI,EAAE,8BAAC,oBAAU,OAAG,GACpB,CACH,CACQ,CACZ,CAAC;AACJ,CAAC,CAAC;AAEF,kBAAe,UAAU,CAAC"}
@@ -81,6 +81,7 @@ let recognizer;
81
81
  let speechConfig;
82
82
  let speechSynthesizer;
83
83
  let audioDestination;
84
+ let audioContext;
84
85
  const MemoriWidget = ({ memori, memoriConfigs, memoriLang, integration, showInstruct = false, showShare = true, preview = false, embed = false, showInputs = true, showDates = false, showContextPerLine = false, showSettings = false, height = '100vh', secret, baseUrl = 'https://app.twincreator.com', apiUrl = 'https://backend.memori.ai', initialContextVars, initialQuestion, ogImage, sessionID: initialSessionID, tenant, personification, authToken, AZURE_COGNITIVE_SERVICES_TTS_KEY, onStateChange, }) => {
85
86
  var _a, _b, _c, _d, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _t, _u, _v;
86
87
  const { t, i18n } = (0, react_i18next_1.useTranslation)();
@@ -107,7 +108,7 @@ const MemoriWidget = ({ memori, memoriConfigs, memoriLang, integration, showInst
107
108
  const [showPositionDrawer, setShowPositionDrawer] = (0, react_1.useState)(false);
108
109
  const [showSettingsDrawer, setShowSettingsDrawer] = (0, react_1.useState)(false);
109
110
  const [muteSpeaker, setMuteSpeaker] = (0, react_1.useState)(false);
110
- const [continuousSpeech, setContinuousSpeech] = (0, react_1.useState)(false);
111
+ const [continuousSpeech, setContinuousSpeech] = (0, react_1.useState)(true);
111
112
  const [continuousSpeechTimeout, setContinuousSpeechTimeout] = (0, react_1.useState)(3);
112
113
  const [isPlayingAudio, setIsPlayingAudio] = (0, react_1.useState)(false);
113
114
  (0, react_1.useEffect)(() => {
@@ -115,7 +116,7 @@ const MemoriWidget = ({ memori, memoriConfigs, memoriLang, integration, showInst
115
116
  }, [speechSynthesizer]);
116
117
  (0, react_1.useEffect)(() => {
117
118
  setMuteSpeaker((0, configuration_1.getLocalConfig)('muteSpeaker', false));
118
- setContinuousSpeech((0, configuration_1.getLocalConfig)('continuousSpeech', false));
119
+ setContinuousSpeech((0, configuration_1.getLocalConfig)('continuousSpeech', true));
119
120
  setContinuousSpeechTimeout((0, configuration_1.getLocalConfig)('continuousSpeechTimeout', 3));
120
121
  }, []);
121
122
  const [memoriPwd, setMemoriPwd] = (0, react_1.useState)(secret);
@@ -194,6 +195,7 @@ const MemoriWidget = ({ memori, memoriConfigs, memoriLang, integration, showInst
194
195
  media: currentState.media,
195
196
  fromUser: false,
196
197
  });
198
+ speak(currentState.emission);
197
199
  }
198
200
  }
199
201
  else {
@@ -216,6 +218,7 @@ const MemoriWidget = ({ memori, memoriConfigs, memoriLang, integration, showInst
216
218
  media: currentState.media,
217
219
  fromUser: false,
218
220
  });
221
+ speak(currentState.emission);
219
222
  }
220
223
  }
221
224
  else {
@@ -232,28 +235,34 @@ const MemoriWidget = ({ memori, memoriConfigs, memoriLang, integration, showInst
232
235
  emission &&
233
236
  !instruct &&
234
237
  isMultilanguageEnabled) {
235
- translateDialogState(currentState, userLang);
238
+ translateDialogState(currentState, userLang).then(ts => {
239
+ if (ts.emission) {
240
+ speak(ts.emission);
241
+ }
242
+ });
236
243
  }
237
244
  else {
238
245
  setCurrentDialogState({
239
246
  ...currentState,
240
247
  emission,
241
248
  });
242
- if (emission)
249
+ if (emission) {
243
250
  pushMessage({
244
251
  text: emission,
245
252
  media: currentState.media,
246
253
  fromUser: false,
247
254
  });
255
+ speak(emission);
256
+ }
248
257
  }
249
258
  }
250
259
  else if (response.resultCode === 404) {
251
260
  setHistory(h => [...h.slice(0, h.length - 1)]);
252
- reopenSession(false, memoriPwd || memori.secretToken, memoriTokens, instruct && memori.giverTag ? memori.giverTag : undefined, instruct && memori.giverPIN ? memori.giverPIN : undefined, initialContextVars, initialQuestion).then(sessionID => {
261
+ reopenSession(false, memoriPwd || memori.secretToken, memoriTokens, instruct && memori.giverTag ? memori.giverTag : undefined, instruct && memori.giverPIN ? memori.giverPIN : undefined, initialContextVars, initialQuestion).then(state => {
253
262
  console.info('session timeout');
254
- if (sessionID) {
263
+ if (state === null || state === void 0 ? void 0 : state.sessionID) {
255
264
  setTimeout(() => {
256
- sendMessage(text, media, sessionID);
265
+ sendMessage(text, media, state === null || state === void 0 ? void 0 : state.sessionID);
257
266
  }, 500);
258
267
  }
259
268
  });
@@ -324,7 +333,7 @@ const MemoriWidget = ({ memori, memoriConfigs, memoriLang, integration, showInst
324
333
  }
325
334
  };
326
335
  const fetchSession = async (params) => {
327
- var _a, _b, _c, _d, _f;
336
+ var _a;
328
337
  if (memori.privacyType !== 'PUBLIC' &&
329
338
  !memori.secretToken &&
330
339
  !memoriPwd &&
@@ -346,50 +355,6 @@ const MemoriWidget = ({ memori, memoriConfigs, memoriLang, integration, showInst
346
355
  (session === null || session === void 0 ? void 0 : session.currentState) &&
347
356
  session.resultCode === 0) {
348
357
  setSessionId(session.sessionID);
349
- const language = (_f = (_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 ? _d : i18n.language) !== null && _f !== void 0 ? _f : 'IT';
350
- if (!instruct &&
351
- isMultilanguageEnabled &&
352
- userLang.toLowerCase() !== language.toLowerCase()) {
353
- translateDialogState(session.currentState, userLang).then(state => {
354
- if (state === null || state === void 0 ? void 0 : state.emission) {
355
- history.length <= 1
356
- ? setHistory([
357
- {
358
- text: state.emission,
359
- media: state.media,
360
- fromUser: false,
361
- initial: true,
362
- },
363
- ])
364
- : pushMessage({
365
- text: state.emission,
366
- media: state.media,
367
- fromUser: false,
368
- initial: true,
369
- });
370
- }
371
- });
372
- }
373
- else {
374
- setCurrentDialogState(session.currentState);
375
- if (session.currentState.emission) {
376
- history.length <= 1
377
- ? setHistory([
378
- {
379
- text: session.currentState.emission,
380
- media: session.currentState.media,
381
- fromUser: false,
382
- initial: true,
383
- },
384
- ])
385
- : pushMessage({
386
- text: session.currentState.emission,
387
- media: session.currentState.media,
388
- fromUser: false,
389
- initial: true,
390
- });
391
- }
392
- }
393
358
  if (position)
394
359
  applyPosition(position, session.sessionID);
395
360
  setLoading(false);
@@ -445,7 +410,10 @@ const MemoriWidget = ({ memori, memoriConfigs, memoriLang, integration, showInst
445
410
  if (position)
446
411
  applyPosition(position, sessionID);
447
412
  setLoading(false);
448
- return sessionID;
413
+ return {
414
+ dialogState: currentState,
415
+ sessionID,
416
+ };
449
417
  }
450
418
  else {
451
419
  console.error(response);
@@ -568,7 +536,11 @@ const MemoriWidget = ({ memori, memoriConfigs, memoriLang, integration, showInst
568
536
  userLang !== (i18n === null || i18n === void 0 ? void 0 : i18n.language) &&
569
537
  emission &&
570
538
  emission.length > 0) {
571
- translateDialogState({ ...currentState, emission: emission }, userLang);
539
+ translateDialogState({ ...currentState, emission: emission }, userLang).then(ts => {
540
+ if (ts.emission) {
541
+ speak(ts.emission);
542
+ }
543
+ });
572
544
  }
573
545
  else if (emission && emission.length > 0) {
574
546
  pushMessage({
@@ -576,6 +548,7 @@ const MemoriWidget = ({ memori, memoriConfigs, memoriLang, integration, showInst
576
548
  media: currentState.media,
577
549
  fromUser: false,
578
550
  });
551
+ speak(emission);
579
552
  setCurrentDialogState(currentState);
580
553
  }
581
554
  }
@@ -630,39 +603,15 @@ const MemoriWidget = ({ memori, memoriConfigs, memoriLang, integration, showInst
630
603
  if ((0, utils_1.hasTouchscreen)())
631
604
  speechConfig.speechSynthesisOutputFormat =
632
605
  speechSdk.SpeechSynthesisOutputFormat.Audio16Khz32KBitRateMonoMp3;
633
- let memoriAudioElement = document.getElementById('memori-audio');
634
- if (memoriAudioElement && window.navigator.userAgent.includes('Safari')) {
635
- memoriAudioElement.muted = false;
636
- memoriAudioElement
637
- .play()
638
- .then(() => {
639
- console.log('played intro audio');
640
- try {
641
- const context = new standardized_audio_context_1.AudioContext();
642
- let buffer = context.createBuffer(1, 1, 22050);
643
- let source = context.createBufferSource();
644
- source.buffer = buffer;
645
- source.connect(context.destination);
646
- }
647
- catch (e) {
648
- console.error(e);
649
- }
650
- })
651
- .catch((e) => {
652
- console.error('error playing intro audio', e);
653
- });
654
- }
606
+ audioContext = new standardized_audio_context_1.AudioContext();
607
+ let buffer = audioContext.createBuffer(1, 10000, 22050);
608
+ let source = audioContext.createBufferSource();
609
+ source.buffer = buffer;
610
+ source.connect(audioContext.destination);
611
+ audioDestination = new speechSdk.SpeakerAudioDestination();
612
+ let audioConfig = speechSdk.AudioConfig.fromSpeakerOutput(audioDestination);
613
+ speechSynthesizer = new speechSdk.SpeechSynthesizer(speechConfig, audioConfig);
655
614
  };
656
- (0, react_1.useEffect)(() => {
657
- return () => {
658
- if (audioDestination)
659
- audioDestination.pause();
660
- if (speechSynthesizer) {
661
- speechSynthesizer.close();
662
- speechSynthesizer = null;
663
- }
664
- };
665
- }, []);
666
615
  const getTTSVoice = (0, react_1.useCallback)((lang) => {
667
616
  var _a, _b, _c, _d;
668
617
  let voice = '';
@@ -717,9 +666,11 @@ const MemoriWidget = ({ memori, memoriConfigs, memoriLang, integration, showInst
717
666
  return voice;
718
667
  }, [memori.voiceType, i18n.language, memori.culture]);
719
668
  const getCultureCodeByLanguage = (lang) => {
720
- var _a, _b, _c;
669
+ var _a, _b;
721
670
  let voice = '';
722
- let voiceLang = ((_c = lang !== null && lang !== void 0 ? lang : (_b = (_a = memori.culture) === null || _a === void 0 ? void 0 : _a.split('-')) === null || _b === void 0 ? void 0 : _b[0]) !== null && _c !== void 0 ? _c : i18n.language,
671
+ let voiceLang = (lang ||
672
+ ((_b = (_a = memori.culture) === null || _a === void 0 ? void 0 : _a.split('-')) === null || _b === void 0 ? void 0 : _b[0]) ||
673
+ i18n.language ||
723
674
  'IT').toUpperCase();
724
675
  switch (voiceLang) {
725
676
  case 'IT':
@@ -786,45 +737,66 @@ const MemoriWidget = ({ memori, memoriConfigs, memoriLang, integration, showInst
786
737
  }, text);
787
738
  return ssmlText;
788
739
  };
789
- const speak = (text, fireListeningEvent = true) => {
740
+ const speak = (text) => {
790
741
  console.log(AZURE_COGNITIVE_SERVICES_TTS_KEY, hasUserActivatedSpeak, preview);
791
742
  if (!AZURE_COGNITIVE_SERVICES_TTS_KEY)
792
743
  return;
793
- if (preview || !hasUserActivatedSpeak)
744
+ if (listening) {
745
+ stopListening();
746
+ }
747
+ if (preview)
794
748
  return;
795
749
  if (audioDestination)
796
750
  audioDestination.pause();
797
- if (speechSynthesizer) {
798
- speechSynthesizer.close();
799
- speechSynthesizer = null;
751
+ let isSafari = window.navigator.userAgent.includes('Safari') &&
752
+ !window.navigator.userAgent.includes('Chrome');
753
+ let isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent);
754
+ if (isIOS && isSafari) {
755
+ audioContext.suspend();
800
756
  }
801
- if (muteSpeaker && fireListeningEvent) {
802
- setTimeout(() => {
803
- document.dispatchEvent(new Event('endSpeakStartListen'));
804
- }, 3000);
805
- return;
757
+ else if (audioContext.state === 'suspended') {
758
+ stopAudio();
759
+ audioContext = new standardized_audio_context_1.AudioContext();
760
+ let buffer = audioContext.createBuffer(1, 10000, 22050);
761
+ let source = audioContext.createBufferSource();
762
+ source.buffer = buffer;
763
+ source.connect(audioContext.destination);
806
764
  }
807
- else if (muteSpeaker) {
765
+ if (!speechSynthesizer) {
766
+ audioDestination = new speechSdk.SpeakerAudioDestination();
767
+ let audioConfig = speechSdk.AudioConfig.fromSpeakerOutput(audioDestination);
768
+ speechSynthesizer = new speechSdk.SpeechSynthesizer(speechConfig, audioConfig);
769
+ }
770
+ if (muteSpeaker) {
771
+ if (continuousSpeech) {
772
+ setListeningTimeout();
773
+ }
808
774
  return;
809
775
  }
810
- audioDestination = new speechSdk.SpeakerAudioDestination();
811
- let audioConfig = speechSdk.AudioConfig.fromSpeakerOutput(audioDestination);
812
- speechSynthesizer = new speechSdk.SpeechSynthesizer(speechConfig, audioConfig);
813
776
  audioDestination.onAudioEnd = () => {
814
777
  setIsPlayingAudio(false);
815
- if (fireListeningEvent) {
778
+ if (continuousSpeech) {
816
779
  document.dispatchEvent(new Event('endSpeakStartListen'));
817
780
  }
818
781
  };
819
782
  setIsPlayingAudio(true);
820
- console.log('speaking', text);
821
- console.log('speechSynthesizer', speechSynthesizer);
822
- console.log('audioDestination', audioDestination);
823
- console.log('speechConfig', speechConfig);
824
- console.log('audioConfig', audioConfig);
783
+ speechSynthesizer.synthesisCompleted = (s, e) => {
784
+ console.log('synthesisCompleted', s, e);
785
+ setIsPlayingAudio(false);
786
+ };
825
787
  speechSynthesizer.speakSsmlAsync(`<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(userLang)}"><voice name="${getTTSVoice(userLang)}"><s>${replaceTextWithPhonemes(text, userLang.toLowerCase())}</s></voice></speak>`, result => {
826
788
  if (result) {
789
+ console.log('result', result);
827
790
  try {
791
+ audioContext.decodeAudioData(result.audioData, function (buffer) {
792
+ const source = audioContext.createBufferSource();
793
+ source.buffer = buffer;
794
+ source.connect(audioContext.destination);
795
+ if (history.length < 1 || (isSafari && isIOS)) {
796
+ source.start(0);
797
+ }
798
+ });
799
+ audioContext.resume();
828
800
  if (speechSynthesizer) {
829
801
  speechSynthesizer.close();
830
802
  speechSynthesizer = null;
@@ -834,10 +806,14 @@ const MemoriWidget = ({ memori, memoriConfigs, memoriLang, integration, showInst
834
806
  console.error('speak error: ', e);
835
807
  window.speechSynthesis.speak(new SpeechSynthesisUtterance(text));
836
808
  setIsPlayingAudio(false);
809
+ if (speechSynthesizer) {
810
+ speechSynthesizer.close();
811
+ speechSynthesizer = null;
812
+ }
837
813
  }
838
814
  }
839
815
  else {
840
- window.speechSynthesis.speak(new SpeechSynthesisUtterance(text));
816
+ audioContext.resume();
841
817
  setIsPlayingAudio(false);
842
818
  }
843
819
  }, error => {
@@ -846,12 +822,20 @@ const MemoriWidget = ({ memori, memoriConfigs, memoriLang, integration, showInst
846
822
  setIsPlayingAudio(false);
847
823
  });
848
824
  setIsPlayingAudio(false);
825
+ setMemoriTyping(false);
849
826
  };
850
827
  const stopAudio = () => {
851
828
  if (speechSynthesizer) {
852
829
  speechSynthesizer.close();
853
830
  speechSynthesizer = null;
854
831
  }
832
+ if (audioContext) {
833
+ audioContext.close();
834
+ }
835
+ if (audioDestination) {
836
+ audioDestination.pause();
837
+ audioDestination.close();
838
+ }
855
839
  };
856
840
  (0, react_1.useEffect)(() => {
857
841
  let textarea = document.querySelector('#chat-fieldset textarea');
@@ -890,11 +874,6 @@ const MemoriWidget = ({ memori, memoriConfigs, memoriLang, integration, showInst
890
874
  (0, react_1.useEffect)(() => {
891
875
  resetListeningTimeout();
892
876
  resetInteractionTimeout();
893
- if ((transcript === null || transcript === void 0 ? void 0 : transcript.length) > 0) {
894
- const transcriptMessage = (0, utils_1.stripDuplicates)(transcript);
895
- if (transcriptMessage.length > 0)
896
- setUserMessage(transcriptMessage);
897
- }
898
877
  }, [transcript]);
899
878
  const startListening = () => {
900
879
  console.log('start listening');
@@ -920,9 +899,14 @@ const MemoriWidget = ({ memori, memoriConfigs, memoriLang, integration, showInst
920
899
  recognizer = new speechSdk.SpeechRecognizer(speechConfig, audioConfig);
921
900
  setListening(true);
922
901
  recognizer.recognized = (_s, e) => {
923
- var _a;
924
902
  if (e.result.reason === speechSdk.ResultReason.RecognizedSpeech) {
925
- setTranscript((_a = e.result.text) !== null && _a !== void 0 ? _a : '');
903
+ let transcript = e.result.text;
904
+ setTranscript(transcript || '');
905
+ if ((transcript === null || transcript === void 0 ? void 0 : transcript.length) > 0) {
906
+ const transcriptMessage = (0, utils_1.stripDuplicates)(transcript);
907
+ if (transcriptMessage.length > 0)
908
+ setUserMessage(transcriptMessage);
909
+ }
926
910
  }
927
911
  else if (e.result.reason === speechSdk.ResultReason.NoMatch) {
928
912
  console.debug('NOMATCH: Speech could not be recognized.');
@@ -937,8 +921,7 @@ const MemoriWidget = ({ memori, memoriConfigs, memoriLang, integration, showInst
937
921
  stopListening();
938
922
  };
939
923
  recognizer.sessionStopped = (_s, _e) => {
940
- if (recognizer)
941
- recognizer.stopContinuousRecognitionAsync();
924
+ stopListening();
942
925
  };
943
926
  recognizer.startContinuousRecognitionAsync();
944
927
  })
@@ -953,8 +936,8 @@ const MemoriWidget = ({ memori, memoriConfigs, memoriLang, integration, showInst
953
936
  recognizer.stopContinuousRecognitionAsync();
954
937
  recognizer.close();
955
938
  recognizer = null;
956
- setListening(false);
957
939
  }
940
+ setListening(false);
958
941
  };
959
942
  const clearListening = () => {
960
943
  setHasUserActivatedListening(false);
@@ -976,15 +959,6 @@ const MemoriWidget = ({ memori, memoriConfigs, memoriLang, integration, showInst
976
959
  if ((currentDialogState === null || currentDialogState === void 0 ? void 0 : currentDialogState.state) === 'Z0')
977
960
  clearListening();
978
961
  }, [currentDialogState === null || currentDialogState === void 0 ? void 0 : currentDialogState.state]);
979
- (0, react_1.useEffect)(() => {
980
- if (hasUserActivatedSpeak &&
981
- !preview &&
982
- !muteSpeaker &&
983
- history.length > 0 &&
984
- (currentDialogState === null || currentDialogState === void 0 ? void 0 : currentDialogState.emission)) {
985
- speak(currentDialogState.emission, currentDialogState.state !== 'Z0');
986
- }
987
- }, [currentDialogState, hasUserActivatedSpeak]);
988
962
  const [requestedListening, setRequestedListening] = (0, react_1.useState)(false);
989
963
  const onEndSpeakStartListen = (0, react_1.useCallback)((_e) => {
990
964
  if (isPlayingAudio && speechSynthesizer) {
@@ -1188,7 +1162,15 @@ const MemoriWidget = ({ memori, memoriConfigs, memoriLang, integration, showInst
1188
1162
  const sessionID = (session === null || session === void 0 ? void 0 : session.sessionID) || sessionId;
1189
1163
  const dialogState = (session === null || session === void 0 ? void 0 : session.dialogState) || currentDialogState;
1190
1164
  setClickedStart(true);
1191
- console.log('onClickStart');
1165
+ let memoriAudioElement = document.getElementById('memori-audio');
1166
+ let isSafari = window.navigator.userAgent.includes('Safari') &&
1167
+ !window.navigator.userAgent.includes('Chrome');
1168
+ if (memoriAudioElement && isSafari) {
1169
+ memoriAudioElement.muted = false;
1170
+ memoriAudioElement.play().catch((e) => {
1171
+ console.error('error playing intro audio', e);
1172
+ });
1173
+ }
1192
1174
  if ((!sessionID &&
1193
1175
  memori.privacyType !== 'PUBLIC' &&
1194
1176
  !memori.secretToken &&
@@ -1210,7 +1192,7 @@ const MemoriWidget = ({ memori, memoriConfigs, memoriLang, integration, showInst
1210
1192
  initialContextVars,
1211
1193
  initialQuestion,
1212
1194
  });
1213
- onClickStart(session || undefined);
1195
+ await onClickStart(session || undefined);
1214
1196
  return;
1215
1197
  }
1216
1198
  else if (initialSessionID) {
@@ -1220,7 +1202,7 @@ const MemoriWidget = ({ memori, memoriConfigs, memoriLang, integration, showInst
1220
1202
  setGotErrorInOpening(true);
1221
1203
  setSessionId(undefined);
1222
1204
  setClickedStart(false);
1223
- onClickStart();
1205
+ await onClickStart();
1224
1206
  return;
1225
1207
  }
1226
1208
  setHistory([]);
@@ -1232,7 +1214,13 @@ const MemoriWidget = ({ memori, memoriConfigs, memoriLang, integration, showInst
1232
1214
  await changeTag(memori.engineMemoriID, sessionID, '-');
1233
1215
  const session = await changeTag(memori.engineMemoriID, sessionID, memori.giverTag, memori.giverPIN);
1234
1216
  if (session && session.resultCode === 0) {
1235
- translateDialogState(session.currentState, userLang).finally(() => {
1217
+ translateDialogState(session.currentState, userLang)
1218
+ .then(ts => {
1219
+ if (ts.emission) {
1220
+ speak(ts.emission);
1221
+ }
1222
+ })
1223
+ .finally(() => {
1236
1224
  setHasUserActivatedSpeak(true);
1237
1225
  });
1238
1226
  }
@@ -1256,7 +1244,13 @@ const MemoriWidget = ({ memori, memoriConfigs, memoriLang, integration, showInst
1256
1244
  await changeTag(memori.engineMemoriID, sessionID, '-');
1257
1245
  const session = await changeTag(memori.engineMemoriID, sessionID, personification.tag, personification.pin);
1258
1246
  if (session && session.resultCode === 0) {
1259
- translateDialogState(session.currentState, userLang).finally(() => {
1247
+ translateDialogState(session.currentState, userLang)
1248
+ .then(ts => {
1249
+ if (ts.emission) {
1250
+ speak(ts.emission);
1251
+ }
1252
+ })
1253
+ .finally(() => {
1260
1254
  setHasUserActivatedSpeak(true);
1261
1255
  });
1262
1256
  }
@@ -1280,7 +1274,13 @@ const MemoriWidget = ({ memori, memoriConfigs, memoriLang, integration, showInst
1280
1274
  await changeTag(memori.engineMemoriID, sessionID, '-');
1281
1275
  const session = await changeTag(memori.engineMemoriID, sessionID, constants_1.anonTag);
1282
1276
  if (session && session.resultCode === 0) {
1283
- translateDialogState(session.currentState, userLang).finally(() => {
1277
+ translateDialogState(session.currentState, userLang)
1278
+ .then(ts => {
1279
+ if (ts.emission) {
1280
+ speak(ts.emission);
1281
+ }
1282
+ })
1283
+ .finally(() => {
1284
1284
  setHasUserActivatedSpeak(true);
1285
1285
  });
1286
1286
  }
@@ -1297,14 +1297,26 @@ const MemoriWidget = ({ memori, memoriConfigs, memoriLang, integration, showInst
1297
1297
  }
1298
1298
  }
1299
1299
  else {
1300
- translateDialogState(currentState, userLang).finally(() => {
1300
+ translateDialogState(currentState, userLang)
1301
+ .then(ts => {
1302
+ if (ts.emission) {
1303
+ speak(ts.emission);
1304
+ }
1305
+ })
1306
+ .finally(() => {
1301
1307
  setHasUserActivatedSpeak(true);
1302
1308
  });
1303
1309
  }
1304
1310
  }
1305
1311
  else {
1306
1312
  setHistory([]);
1307
- translateDialogState(dialogState, userLang).finally(() => {
1313
+ translateDialogState(dialogState, userLang)
1314
+ .then(ts => {
1315
+ if (ts.emission) {
1316
+ speak(ts.emission);
1317
+ }
1318
+ })
1319
+ .finally(() => {
1308
1320
  setHasUserActivatedSpeak(true);
1309
1321
  });
1310
1322
  }
@@ -1340,7 +1352,19 @@ const MemoriWidget = ({ memori, memoriConfigs, memoriLang, integration, showInst
1340
1352
  observer.disconnect();
1341
1353
  };
1342
1354
  }, []);
1343
- const header = (react_1.default.createElement(Header_1.default, { memori: memori, history: history, showShare: showShare, position: position, setShowPositionDrawer: setShowPositionDrawer, setShowSettingsDrawer: setShowSettingsDrawer, speakerMuted: muteSpeaker, setSpeakerMuted: setMuteSpeaker, showSettings: showSettings, hasUserActivatedSpeak: hasUserActivatedSpeak }));
1355
+ const header = (react_1.default.createElement(Header_1.default, { memori: memori, history: history, showShare: showShare, position: position, setShowPositionDrawer: setShowPositionDrawer, setShowSettingsDrawer: setShowSettingsDrawer, speakerMuted: muteSpeaker, setSpeakerMuted: mute => {
1356
+ setMuteSpeaker(mute);
1357
+ if (mute) {
1358
+ stopAudio();
1359
+ }
1360
+ else {
1361
+ audioContext = new standardized_audio_context_1.AudioContext();
1362
+ let buffer = audioContext.createBuffer(1, 10000, 22050);
1363
+ let source = audioContext.createBufferSource();
1364
+ source.buffer = buffer;
1365
+ source.connect(audioContext.destination);
1366
+ }
1367
+ }, showSettings: showSettings, hasUserActivatedSpeak: hasUserActivatedSpeak }));
1344
1368
  const avatar = (react_1.default.createElement(Avatar_1.default, { memori: memori, integration: integration, integrationConfig: integrationConfig, tenant: tenant, instruct: instruct, avatar3dVisible: avatar3dVisible, setAvatar3dVisible: setAvatar3dVisible, hasUserActivatedSpeak: hasUserActivatedSpeak, isPlayingAudio: isPlayingAudio, baseUrl: baseUrl }));
1345
1369
  const startPanel = (react_1.default.createElement(StartPanel_1.default, { memori: memori, tenant: tenant, gamificationLevel: gamificationLevel, language: language, userLang: userLang, setUserLang: setUserLang, baseUrl: baseUrl, position: position, openPositionDrawer: () => setShowPositionDrawer(true), integrationConfig: integrationConfig, instruct: instruct, sessionId: sessionId, clickedStart: clickedStart, onClickStart: onClickStart, initializeTTS: initializeTTS }));
1346
1370
  const chat = sessionId ? (react_1.default.createElement(Chat_1.default, { memori: memori, sessionID: sessionId, tenant: tenant, translateTo: isMultilanguageEnabled &&
@@ -1379,9 +1403,9 @@ const MemoriWidget = ({ memori, memoriConfigs, memoriLang, integration, showInst
1379
1403
  if (values['tokens'])
1380
1404
  setMemoriTokens(values['tokens']);
1381
1405
  reopenSession(!sessionId, values['password'], values['tokens'], instruct ? memori.giverTag : undefined, instruct ? memori.giverPIN : undefined, initialContextVars, initialQuestion)
1382
- .then(() => {
1406
+ .then(state => {
1383
1407
  setAuthModalState(null);
1384
- setHasUserActivatedSpeak(true);
1408
+ onClickStart(state || undefined);
1385
1409
  })
1386
1410
  .catch(() => {
1387
1411
  setAuthModalState(null);
@@ -1415,6 +1439,7 @@ const MemoriWidget = ({ memori, memoriConfigs, memoriLang, integration, showInst
1415
1439
  media: currentState.media,
1416
1440
  fromUser: false,
1417
1441
  });
1442
+ speak(currentState.emission);
1418
1443
  }
1419
1444
  }
1420
1445
  else {
@@ -1453,6 +1478,7 @@ const MemoriWidget = ({ memori, memoriConfigs, memoriLang, integration, showInst
1453
1478
  media: currentState.media,
1454
1479
  fromUser: false,
1455
1480
  });
1481
+ speak(currentState.emission);
1456
1482
  }
1457
1483
  }
1458
1484
  else {