@memori.ai/memori-react 2.10.1 → 2.10.2

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.
@@ -77,14 +77,15 @@ const MemoriWidget = ({ memori, memoriConfigs, memoriLang, multilingual, integra
77
77
  const [instruct, setInstruct] = useState(false);
78
78
  const [clickedStart, setClickedStart] = useState(false);
79
79
  const [gotErrorInOpening, setGotErrorInOpening] = useState(false);
80
- const language = (_d = (_c = (_b = (_a = memoriConfigs === null || memoriConfigs === void 0 ? void 0 : memoriConfigs.find(c => c.memoriConfigID === memori.memoriConfigurationID)) === null || _a === void 0 ? void 0 : _a.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();
80
+ const language = ((_c = (_b = (_a = memori.culture) === null || _a === void 0 ? void 0 : _a.split('-')) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.toUpperCase()) ||
81
+ ((_h = (_g = (_f = (_d = memoriConfigs === null || memoriConfigs === void 0 ? void 0 : memoriConfigs.find(c => c.memoriConfigID === memori.memoriConfigurationID)) === null || _d === void 0 ? void 0 : _d.culture) === null || _f === void 0 ? void 0 : _f.split('-')) === null || _g === void 0 ? void 0 : _g[0]) === null || _h === void 0 ? void 0 : _h.toUpperCase());
81
82
  const integrationConfig = (integration === null || integration === void 0 ? void 0 : integration.customData)
82
83
  ? JSON.parse(integration.customData)
83
84
  : null;
84
85
  const isMultilanguageEnabled = multilingual !== undefined
85
86
  ? multilingual
86
87
  : !!(integrationConfig === null || integrationConfig === void 0 ? void 0 : integrationConfig.multilanguage);
87
- const [userLang, setUserLang] = useState((_m = (_l = (_k = (_j = (_f = memoriLang !== null && memoriLang !== void 0 ? memoriLang : integrationConfig === null || integrationConfig === void 0 ? void 0 : integrationConfig.lang) !== null && _f !== void 0 ? _f : (_h = (_g = memori === null || memori === void 0 ? void 0 : memori.culture) === null || _g === void 0 ? void 0 : _g.split('-')) === null || _h === void 0 ? void 0 : _h[0]) !== null && _j !== void 0 ? _j : language) !== null && _k !== void 0 ? _k : integrationConfig === null || integrationConfig === void 0 ? void 0 : integrationConfig.uiLang) !== null && _l !== void 0 ? _l : i18n.language) !== null && _m !== void 0 ? _m : 'IT');
88
+ const [userLang, setUserLang] = useState((_m = (_l = (_k = (_j = memoriLang !== null && memoriLang !== void 0 ? memoriLang : integrationConfig === null || integrationConfig === void 0 ? void 0 : integrationConfig.lang) !== null && _j !== void 0 ? _j : language) !== null && _k !== void 0 ? _k : integrationConfig === null || integrationConfig === void 0 ? void 0 : integrationConfig.uiLang) !== null && _l !== void 0 ? _l : i18n.language) !== null && _m !== void 0 ? _m : 'IT');
88
89
  const [loading, setLoading] = useState(false);
89
90
  const [memoriTyping, setMemoriTyping] = useState(false);
90
91
  const selectedLayout = layout || (integrationConfig === null || integrationConfig === void 0 ? void 0 : integrationConfig.layout) || 'DEFAULT';
@@ -147,6 +148,203 @@ const MemoriWidget = ({ memori, memoriConfigs, memoriLang, multilingual, integra
147
148
  _setPosition(venue);
148
149
  applyPosition(venue);
149
150
  };
151
+ const [userMessage, setUserMessage] = useState('');
152
+ const onChangeUserMessage = (value) => {
153
+ if (!value || value === '\n' || value.trim() === '') {
154
+ setUserMessage('');
155
+ resetInteractionTimeout();
156
+ return;
157
+ }
158
+ setUserMessage(value);
159
+ clearInteractionTimeout();
160
+ };
161
+ const [listening, setListening] = useState(false);
162
+ const [history, setHistory] = useState([]);
163
+ const pushMessage = (message) => {
164
+ setHistory(history => [...history, { ...message }]);
165
+ };
166
+ const sendMessage = async (text, media, newSessionId, translate = true, translatedText, hidden = false) => {
167
+ var _a, _b;
168
+ const sessionID = newSessionId ||
169
+ sessionId ||
170
+ ((_a = window.getMemoriState()) === null || _a === void 0 ? void 0 : _a.sessionID);
171
+ if (!sessionID || !(text === null || text === void 0 ? void 0 : text.length))
172
+ return;
173
+ if (!hidden)
174
+ pushMessage({
175
+ text: text,
176
+ translatedText,
177
+ fromUser: true,
178
+ media: media !== null && media !== void 0 ? media : [],
179
+ initial: sessionId
180
+ ? !!newSessionId && newSessionId !== sessionId
181
+ : !!newSessionId,
182
+ });
183
+ setMemoriTyping(true);
184
+ let msg = text;
185
+ if (translate &&
186
+ !instruct &&
187
+ isMultilanguageEnabled &&
188
+ userLang.toUpperCase() !== language.toUpperCase()) {
189
+ const translation = await getTranslation(text, language, userLang, baseUrl);
190
+ msg = translation.text;
191
+ }
192
+ const { currentState, ...response } = await postTextEnteredEvent({
193
+ sessionId: sessionID,
194
+ text: msg,
195
+ });
196
+ if (response.resultCode === 0 && currentState) {
197
+ const emission = (_b = currentState.emission) !== null && _b !== void 0 ? _b : currentDialogState === null || currentDialogState === void 0 ? void 0 : currentDialogState.emission;
198
+ if (currentState.state === 'X4' && memori.giverTag) {
199
+ const { currentState, ...resp } = await postTagChangedEvent(sessionID, memori.giverTag);
200
+ if (resp.resultCode === 0) {
201
+ setCurrentDialogState(currentState);
202
+ if (currentState.emission) {
203
+ pushMessage({
204
+ text: currentState.emission,
205
+ media: currentState.media,
206
+ fromUser: false,
207
+ });
208
+ speak(currentState.emission);
209
+ }
210
+ }
211
+ else {
212
+ console.error(response, resp);
213
+ message.error(t(getErrori18nKey(resp.resultCode)));
214
+ }
215
+ }
216
+ else if (currentState.state === 'X2d' && memori.giverTag) {
217
+ const { currentState, ...resp } = await postTextEnteredEvent({
218
+ sessionId: sessionID,
219
+ text: Math.random().toString().substring(2, 8),
220
+ });
221
+ if (resp.resultCode === 0) {
222
+ const { currentState, ...resp } = await postTagChangedEvent(sessionID, memori.giverTag);
223
+ if (resp.resultCode === 0) {
224
+ setCurrentDialogState(currentState);
225
+ if (currentState.emission) {
226
+ pushMessage({
227
+ text: currentState.emission,
228
+ media: currentState.media,
229
+ fromUser: false,
230
+ });
231
+ speak(currentState.emission);
232
+ }
233
+ }
234
+ else {
235
+ console.error(response, resp);
236
+ message.error(t(getErrori18nKey(resp.resultCode)));
237
+ }
238
+ }
239
+ else {
240
+ console.error(response, resp);
241
+ message.error(t(getErrori18nKey(resp.resultCode)));
242
+ }
243
+ }
244
+ else if (userLang.toLowerCase() !== language.toLowerCase() &&
245
+ emission &&
246
+ !instruct &&
247
+ isMultilanguageEnabled) {
248
+ translateDialogState(currentState, userLang).then(ts => {
249
+ if (ts.emission) {
250
+ speak(ts.emission);
251
+ }
252
+ });
253
+ }
254
+ else {
255
+ setCurrentDialogState({
256
+ ...currentState,
257
+ emission,
258
+ });
259
+ if (emission) {
260
+ pushMessage({
261
+ text: emission,
262
+ media: currentState.media,
263
+ fromUser: false,
264
+ generatedByAI: !!currentState.completion,
265
+ });
266
+ speak(emission);
267
+ }
268
+ }
269
+ }
270
+ else if (response.resultCode === 404) {
271
+ setHistory(h => [...h.slice(0, h.length - 1)]);
272
+ reopenSession(false, memoriPwd || memori.secretToken, memoriTokens, instruct && memori.giverTag ? memori.giverTag : undefined, instruct && memori.giverPIN ? memori.giverPIN : undefined, initialContextVars, initialQuestion).then(state => {
273
+ console.info('session timeout');
274
+ if (state === null || state === void 0 ? void 0 : state.sessionID) {
275
+ setTimeout(() => {
276
+ sendMessage(text, media, state === null || state === void 0 ? void 0 : state.sessionID);
277
+ }, 500);
278
+ }
279
+ });
280
+ }
281
+ setMemoriTyping(false);
282
+ };
283
+ const translateDialogState = async (state, userLang) => {
284
+ var _a, _b, _c;
285
+ const emission = (_a = state.emission) !== null && _a !== void 0 ? _a : currentDialogState === null || currentDialogState === void 0 ? void 0 : currentDialogState.emission;
286
+ let translatedState = { ...state };
287
+ let translatedMsg = null;
288
+ if (!emission ||
289
+ instruct ||
290
+ language.toUpperCase() === userLang.toUpperCase() ||
291
+ !isMultilanguageEnabled) {
292
+ translatedState = { ...state, emission };
293
+ if (emission) {
294
+ translatedMsg = {
295
+ text: emission,
296
+ media: state.media,
297
+ fromUser: false,
298
+ };
299
+ }
300
+ }
301
+ else {
302
+ const t = await getTranslation(emission, userLang, language, baseUrl);
303
+ if (state.hints && state.hints.length > 0) {
304
+ const translatedHints = await Promise.all(((_b = state.hints) !== null && _b !== void 0 ? _b : []).map(async (hint) => {
305
+ var _a;
306
+ const tHint = await getTranslation(hint, userLang, language, baseUrl);
307
+ return {
308
+ text: (_a = tHint === null || tHint === void 0 ? void 0 : tHint.text) !== null && _a !== void 0 ? _a : hint,
309
+ originalText: hint,
310
+ };
311
+ }));
312
+ translatedState = {
313
+ ...state,
314
+ emission: t.text,
315
+ translatedHints,
316
+ };
317
+ }
318
+ else {
319
+ translatedState = {
320
+ ...state,
321
+ emission: t.text,
322
+ hints: (_c = state.hints) !== null && _c !== void 0 ? _c : (state.state === 'G1' ? currentDialogState === null || currentDialogState === void 0 ? void 0 : currentDialogState.hints : []),
323
+ };
324
+ }
325
+ if (t.text.length > 0)
326
+ translatedMsg = {
327
+ text: t.text,
328
+ media: state.media,
329
+ fromUser: false,
330
+ generatedByAI: !!state.completion,
331
+ };
332
+ }
333
+ setCurrentDialogState(translatedState);
334
+ if (translatedMsg) {
335
+ pushMessage(translatedMsg);
336
+ }
337
+ return translatedState;
338
+ };
339
+ const minAge = memori.ageRestriction
340
+ ? memori.ageRestriction
341
+ : memori.nsfw
342
+ ? 18
343
+ : memori.enableCompletions
344
+ ? 14
345
+ : 0;
346
+ const [birthDate, setBirthDate] = useState();
347
+ const [showAgeVerification, setShowAgeVerification] = useState(false);
150
348
  const [sessionId, setSessionId] = useState(initialSessionID);
151
349
  const [currentDialogState, _setCurrentDialogState] = useState();
152
350
  const setCurrentDialogState = (state) => {
@@ -395,201 +593,6 @@ const MemoriWidget = ({ memori, memoriConfigs, memoriLang, multilingual, integra
395
593
  restoreGiverTag();
396
594
  };
397
595
  }, []);
398
- const [userMessage, setUserMessage] = useState('');
399
- const onChangeUserMessage = (value) => {
400
- if (!value || value === '\n' || value.trim() === '') {
401
- setUserMessage('');
402
- resetInteractionTimeout();
403
- return;
404
- }
405
- setUserMessage(value);
406
- clearInteractionTimeout();
407
- };
408
- const [listening, setListening] = useState(false);
409
- const [history, setHistory] = useState([]);
410
- const pushMessage = (message) => {
411
- setHistory(history => [...history, { ...message }]);
412
- };
413
- const sendMessage = useCallback(async (text, media, newSessionId, translate = true, translatedText, hidden = false) => {
414
- var _a, _b, _c, _d, _f;
415
- const sessionID = newSessionId || sessionId;
416
- if (!sessionID || !(text === null || text === void 0 ? void 0 : text.length))
417
- return;
418
- if (!hidden)
419
- pushMessage({
420
- text: text,
421
- translatedText,
422
- fromUser: true,
423
- media: media !== null && media !== void 0 ? media : [],
424
- initial: !!newSessionId,
425
- });
426
- setMemoriTyping(true);
427
- const language = (_d = (_c = (_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) !== null && _d !== void 0 ? _d : 'IT';
428
- let msg = text;
429
- if (translate &&
430
- !instruct &&
431
- isMultilanguageEnabled &&
432
- userLang.toUpperCase() !== language.toUpperCase()) {
433
- const translation = await getTranslation(text, language, userLang, baseUrl);
434
- msg = translation.text;
435
- }
436
- const { currentState, ...response } = await postTextEnteredEvent({
437
- sessionId: sessionID,
438
- text: msg,
439
- });
440
- if (response.resultCode === 0 && currentState) {
441
- const emission = (_f = currentState.emission) !== null && _f !== void 0 ? _f : currentDialogState === null || currentDialogState === void 0 ? void 0 : currentDialogState.emission;
442
- if (currentState.state === 'X4' && memori.giverTag) {
443
- const { currentState, ...resp } = await postTagChangedEvent(sessionID, memori.giverTag);
444
- if (resp.resultCode === 0) {
445
- setCurrentDialogState(currentState);
446
- if (currentState.emission) {
447
- pushMessage({
448
- text: currentState.emission,
449
- media: currentState.media,
450
- fromUser: false,
451
- });
452
- speak(currentState.emission);
453
- }
454
- }
455
- else {
456
- console.error(response, resp);
457
- message.error(t(getErrori18nKey(resp.resultCode)));
458
- }
459
- }
460
- else if (currentState.state === 'X2d' && memori.giverTag) {
461
- const { currentState, ...resp } = await postTextEnteredEvent({
462
- sessionId: sessionID,
463
- text: Math.random().toString().substring(2, 8),
464
- });
465
- if (resp.resultCode === 0) {
466
- const { currentState, ...resp } = await postTagChangedEvent(sessionID, memori.giverTag);
467
- if (resp.resultCode === 0) {
468
- setCurrentDialogState(currentState);
469
- if (currentState.emission) {
470
- pushMessage({
471
- text: currentState.emission,
472
- media: currentState.media,
473
- fromUser: false,
474
- });
475
- speak(currentState.emission);
476
- }
477
- }
478
- else {
479
- console.error(response, resp);
480
- message.error(t(getErrori18nKey(resp.resultCode)));
481
- }
482
- }
483
- else {
484
- console.error(response, resp);
485
- message.error(t(getErrori18nKey(resp.resultCode)));
486
- }
487
- }
488
- else if (userLang.toLowerCase() !== language.toLowerCase() &&
489
- emission &&
490
- !instruct &&
491
- isMultilanguageEnabled) {
492
- translateDialogState(currentState, userLang).then(ts => {
493
- if (ts.emission) {
494
- speak(ts.emission);
495
- }
496
- });
497
- }
498
- else {
499
- setCurrentDialogState({
500
- ...currentState,
501
- emission,
502
- });
503
- if (emission) {
504
- pushMessage({
505
- text: emission,
506
- media: currentState.media,
507
- fromUser: false,
508
- generatedByAI: !!currentState.completion,
509
- });
510
- speak(emission);
511
- }
512
- }
513
- }
514
- else if (response.resultCode === 404) {
515
- setHistory(h => [...h.slice(0, h.length - 1)]);
516
- reopenSession(false, memoriPwd || memori.secretToken, memoriTokens, instruct && memori.giverTag ? memori.giverTag : undefined, instruct && memori.giverPIN ? memori.giverPIN : undefined, initialContextVars, initialQuestion).then(state => {
517
- console.info('session timeout');
518
- if (state === null || state === void 0 ? void 0 : state.sessionID) {
519
- setTimeout(() => {
520
- sendMessage(text, media, state === null || state === void 0 ? void 0 : state.sessionID);
521
- }, 500);
522
- }
523
- });
524
- }
525
- setMemoriTyping(false);
526
- }, [sessionId]);
527
- const translateDialogState = async (state, userLang) => {
528
- var _a, _b, _c, _d, _f, _g, _h;
529
- const language = (_d = (_c = (_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) !== null && _d !== void 0 ? _d : 'IT';
530
- const emission = (_f = state.emission) !== null && _f !== void 0 ? _f : currentDialogState === null || currentDialogState === void 0 ? void 0 : currentDialogState.emission;
531
- let translatedState = { ...state };
532
- let translatedMsg = null;
533
- if (!emission ||
534
- instruct ||
535
- language.toUpperCase() === userLang.toUpperCase() ||
536
- !isMultilanguageEnabled) {
537
- translatedState = { ...state, emission };
538
- if (emission) {
539
- translatedMsg = {
540
- text: emission,
541
- media: state.media,
542
- fromUser: false,
543
- };
544
- }
545
- }
546
- else {
547
- const t = await getTranslation(emission, userLang, language, baseUrl);
548
- if (state.hints && state.hints.length > 0) {
549
- const translatedHints = await Promise.all(((_g = state.hints) !== null && _g !== void 0 ? _g : []).map(async (hint) => {
550
- var _a;
551
- const tHint = await getTranslation(hint, userLang, language, baseUrl);
552
- return {
553
- text: (_a = tHint === null || tHint === void 0 ? void 0 : tHint.text) !== null && _a !== void 0 ? _a : hint,
554
- originalText: hint,
555
- };
556
- }));
557
- translatedState = {
558
- ...state,
559
- emission: t.text,
560
- translatedHints,
561
- };
562
- }
563
- else {
564
- translatedState = {
565
- ...state,
566
- emission: t.text,
567
- hints: (_h = state.hints) !== null && _h !== void 0 ? _h : (state.state === 'G1' ? currentDialogState === null || currentDialogState === void 0 ? void 0 : currentDialogState.hints : []),
568
- };
569
- }
570
- if (t.text.length > 0)
571
- translatedMsg = {
572
- text: t.text,
573
- media: state.media,
574
- fromUser: false,
575
- generatedByAI: !!state.completion,
576
- };
577
- }
578
- setCurrentDialogState(translatedState);
579
- if (translatedMsg) {
580
- pushMessage(translatedMsg);
581
- }
582
- return translatedState;
583
- };
584
- const minAge = memori.ageRestriction
585
- ? memori.ageRestriction
586
- : memori.nsfw
587
- ? 18
588
- : memori.enableCompletions
589
- ? 14
590
- : 0;
591
- const [birthDate, setBirthDate] = useState();
592
- const [showAgeVerification, setShowAgeVerification] = useState(false);
593
596
  const [userInteractionTimeout, setUserInteractionTimeout] = useState();
594
597
  const timeoutRef = useRef();
595
598
  const clearInteractionTimeout = () => {
@@ -1352,16 +1355,18 @@ const MemoriWidget = ({ memori, memoriConfigs, memoriLang, multilingual, integra
1352
1355
  }, 1000);
1353
1356
  }
1354
1357
  else {
1355
- sendMessage(text, undefined, sessionID, undefined, undefined, hidden);
1358
+ stopListening();
1359
+ stopAudio();
1360
+ sendMessage(text, undefined, undefined, undefined, undefined, hidden);
1356
1361
  }
1357
1362
  }
1358
- }, [sessionId, isPlayingAudio, memoriTyping]);
1363
+ }, [sessionId, isPlayingAudio, memoriTyping, userLang]);
1359
1364
  useEffect(() => {
1360
1365
  document.addEventListener('MemoriTextEntered', memoriTextEnteredHandler);
1361
1366
  return () => {
1362
1367
  document.removeEventListener('MemoriTextEntered', memoriTextEnteredHandler);
1363
1368
  };
1364
- }, []);
1369
+ }, [sessionId, userLang]);
1365
1370
  const onClickStart = useCallback(async (session) => {
1366
1371
  const sessionID = (session === null || session === void 0 ? void 0 : session.sessionID) || sessionId;
1367
1372
  const dialogState = (session === null || session === void 0 ? void 0 : session.dialogState) || currentDialogState;