@memori.ai/memori-react 2.9.2 → 2.10.0
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 +14 -0
- package/dist/components/ChatBubble/ChatBubble.js +31 -31
- package/dist/components/ChatBubble/ChatBubble.js.map +1 -1
- package/dist/components/MemoriWidget/MemoriWidget.d.ts +15 -1
- package/dist/components/MemoriWidget/MemoriWidget.js +238 -225
- package/dist/components/MemoriWidget/MemoriWidget.js.map +1 -1
- package/dist/components/StartPanel/StartPanel.js +6 -1
- package/dist/components/StartPanel/StartPanel.js.map +1 -1
- package/esm/components/ChatBubble/ChatBubble.js +31 -31
- package/esm/components/ChatBubble/ChatBubble.js.map +1 -1
- package/esm/components/MemoriWidget/MemoriWidget.d.ts +15 -1
- package/esm/components/MemoriWidget/MemoriWidget.js +238 -225
- package/esm/components/MemoriWidget/MemoriWidget.js.map +1 -1
- package/esm/components/StartPanel/StartPanel.js +6 -1
- package/esm/components/StartPanel/StartPanel.js.map +1 -1
- package/package.json +1 -1
- package/src/components/ChatBubble/ChatBubble.tsx +1 -1
- package/src/components/ChatBubble/__snapshots__/ChatBubble.test.tsx.snap +0 -3
- package/src/components/MemoriWidget/MemoriWidget.tsx +335 -289
- package/src/components/StartPanel/StartPanel.tsx +8 -3
|
@@ -46,41 +46,19 @@ const getMemoriState = (integrationId) => {
|
|
|
46
46
|
let dialogState = JSON.parse(engineState);
|
|
47
47
|
return dialogState;
|
|
48
48
|
};
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
prototypeValueSetter.call(element, value);
|
|
58
|
-
}
|
|
59
|
-
else if (valueSetter) {
|
|
60
|
-
valueSetter.call(element, value);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
const typeMessage = (message) => {
|
|
64
|
-
var _a, _b;
|
|
65
|
-
let textarea = document.querySelector('fieldset#chat-fieldset textarea') ||
|
|
66
|
-
((_b = (_a = document
|
|
67
|
-
.querySelector('memori-client')) === null || _a === void 0 ? void 0 : _a.shadowRoot) === null || _b === void 0 ? void 0 : _b.querySelector('fieldset#chat-fieldset textarea'));
|
|
68
|
-
if (!textarea)
|
|
69
|
-
return;
|
|
70
|
-
setNativeValue(textarea, message);
|
|
71
|
-
textarea.dispatchEvent(new Event('input', { bubbles: true }));
|
|
72
|
-
setTimeout(() => {
|
|
73
|
-
var _a, _b;
|
|
74
|
-
let sendButton = document.querySelector('button.memori-chat-inputs--send') ||
|
|
75
|
-
((_b = (_a = document
|
|
76
|
-
.querySelector('memori-client')) === null || _a === void 0 ? void 0 : _a.shadowRoot) === null || _b === void 0 ? void 0 : _b.querySelector('button.memori-chat-inputs--send'));
|
|
77
|
-
if (!sendButton)
|
|
78
|
-
return;
|
|
79
|
-
sendButton.click();
|
|
80
|
-
}, 100);
|
|
49
|
+
const typeMessage = (message, hidden = false) => {
|
|
50
|
+
const e = new CustomEvent('MemoriTextEntered', {
|
|
51
|
+
detail: {
|
|
52
|
+
text: message,
|
|
53
|
+
hidden,
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
document.dispatchEvent(e);
|
|
81
57
|
};
|
|
58
|
+
const typeMessageHidden = (message) => typeMessage(message, true);
|
|
82
59
|
window.getMemoriState = getMemoriState;
|
|
83
60
|
window.typeMessage = typeMessage;
|
|
61
|
+
window.typeMessageHidden = typeMessageHidden;
|
|
84
62
|
let recognizer;
|
|
85
63
|
let speechConfig;
|
|
86
64
|
let speechSynthesizer;
|
|
@@ -88,6 +66,7 @@ let audioDestination;
|
|
|
88
66
|
let audioContext;
|
|
89
67
|
let memoriPassword;
|
|
90
68
|
let speakerMuted = false;
|
|
69
|
+
let memoriSpeaking = false;
|
|
91
70
|
const MemoriWidget = ({ memori, memoriConfigs, memoriLang, multilingual, integration, layout = 'DEFAULT', customLayout, showInstruct = false, showShare, preview = false, embed = false, showInputs = true, showDates = false, showContextPerLine = false, showSettings = true, 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, additionalInfo, additionalSettings, customMediaRenderer, }) => {
|
|
92
71
|
var _a, _b, _c, _d, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _t, _u, _v, _w, _x, _y;
|
|
93
72
|
const { t, i18n } = (0, react_i18next_1.useTranslation)();
|
|
@@ -123,6 +102,7 @@ const MemoriWidget = ({ memori, memoriConfigs, memoriLang, multilingual, integra
|
|
|
123
102
|
const [hideEmissions, setHideEmissions] = (0, react_1.useState)(false);
|
|
124
103
|
(0, react_1.useEffect)(() => {
|
|
125
104
|
setIsPlayingAudio(!!speechSynthesizer);
|
|
105
|
+
memoriSpeaking = !!speechSynthesizer;
|
|
126
106
|
}, [speechSynthesizer]);
|
|
127
107
|
(0, react_1.useEffect)(() => {
|
|
128
108
|
let defaultControlsPosition = 'bottom';
|
|
@@ -169,199 +149,6 @@ const MemoriWidget = ({ memori, memoriConfigs, memoriLang, multilingual, integra
|
|
|
169
149
|
_setPosition(venue);
|
|
170
150
|
applyPosition(venue);
|
|
171
151
|
};
|
|
172
|
-
const [userMessage, setUserMessage] = (0, react_1.useState)('');
|
|
173
|
-
const onChangeUserMessage = (value) => {
|
|
174
|
-
if (!value || value === '\n' || value.trim() === '') {
|
|
175
|
-
setUserMessage('');
|
|
176
|
-
resetInteractionTimeout();
|
|
177
|
-
return;
|
|
178
|
-
}
|
|
179
|
-
setUserMessage(value);
|
|
180
|
-
clearInteractionTimeout();
|
|
181
|
-
};
|
|
182
|
-
const [listening, setListening] = (0, react_1.useState)(false);
|
|
183
|
-
const [history, setHistory] = (0, react_1.useState)([]);
|
|
184
|
-
const pushMessage = (message) => {
|
|
185
|
-
setHistory(history => [...history, { ...message }]);
|
|
186
|
-
};
|
|
187
|
-
const sendMessage = async (text, media, newSessionId, translate = true, translatedText) => {
|
|
188
|
-
var _a, _b, _c, _d, _f;
|
|
189
|
-
if (!sessionId || !(text === null || text === void 0 ? void 0 : text.length))
|
|
190
|
-
return;
|
|
191
|
-
pushMessage({
|
|
192
|
-
text: text,
|
|
193
|
-
translatedText,
|
|
194
|
-
fromUser: true,
|
|
195
|
-
media: media !== null && media !== void 0 ? media : [],
|
|
196
|
-
initial: !!newSessionId,
|
|
197
|
-
});
|
|
198
|
-
setMemoriTyping(true);
|
|
199
|
-
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';
|
|
200
|
-
let msg = text;
|
|
201
|
-
if (translate &&
|
|
202
|
-
!instruct &&
|
|
203
|
-
isMultilanguageEnabled &&
|
|
204
|
-
userLang.toUpperCase() !== language.toUpperCase()) {
|
|
205
|
-
const translation = await (0, translations_1.getTranslation)(text, language, userLang, baseUrl);
|
|
206
|
-
msg = translation.text;
|
|
207
|
-
}
|
|
208
|
-
const { currentState, ...response } = await postTextEnteredEvent({
|
|
209
|
-
sessionId: newSessionId !== null && newSessionId !== void 0 ? newSessionId : sessionId,
|
|
210
|
-
text: msg,
|
|
211
|
-
});
|
|
212
|
-
if (response.resultCode === 0 && currentState) {
|
|
213
|
-
const emission = (_f = currentState.emission) !== null && _f !== void 0 ? _f : currentDialogState === null || currentDialogState === void 0 ? void 0 : currentDialogState.emission;
|
|
214
|
-
if (currentState.state === 'X4' && memori.giverTag) {
|
|
215
|
-
const { currentState, ...resp } = await postTagChangedEvent(sessionId, memori.giverTag);
|
|
216
|
-
if (resp.resultCode === 0) {
|
|
217
|
-
setCurrentDialogState(currentState);
|
|
218
|
-
if (currentState.emission) {
|
|
219
|
-
pushMessage({
|
|
220
|
-
text: currentState.emission,
|
|
221
|
-
media: currentState.media,
|
|
222
|
-
fromUser: false,
|
|
223
|
-
});
|
|
224
|
-
speak(currentState.emission);
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
else {
|
|
228
|
-
console.error(response, resp);
|
|
229
|
-
Message_1.default.error(t((0, error_1.getErrori18nKey)(resp.resultCode)));
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
else if (currentState.state === 'X2d' && memori.giverTag) {
|
|
233
|
-
const { currentState, ...resp } = await postTextEnteredEvent({
|
|
234
|
-
sessionId: newSessionId !== null && newSessionId !== void 0 ? newSessionId : sessionId,
|
|
235
|
-
text: Math.random().toString().substring(2, 8),
|
|
236
|
-
});
|
|
237
|
-
if (resp.resultCode === 0) {
|
|
238
|
-
const { currentState, ...resp } = await postTagChangedEvent(sessionId, memori.giverTag);
|
|
239
|
-
if (resp.resultCode === 0) {
|
|
240
|
-
setCurrentDialogState(currentState);
|
|
241
|
-
if (currentState.emission) {
|
|
242
|
-
pushMessage({
|
|
243
|
-
text: currentState.emission,
|
|
244
|
-
media: currentState.media,
|
|
245
|
-
fromUser: false,
|
|
246
|
-
});
|
|
247
|
-
speak(currentState.emission);
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
else {
|
|
251
|
-
console.error(response, resp);
|
|
252
|
-
Message_1.default.error(t((0, error_1.getErrori18nKey)(resp.resultCode)));
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
else {
|
|
256
|
-
console.error(response, resp);
|
|
257
|
-
Message_1.default.error(t((0, error_1.getErrori18nKey)(resp.resultCode)));
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
else if (userLang.toLowerCase() !== language.toLowerCase() &&
|
|
261
|
-
emission &&
|
|
262
|
-
!instruct &&
|
|
263
|
-
isMultilanguageEnabled) {
|
|
264
|
-
translateDialogState(currentState, userLang).then(ts => {
|
|
265
|
-
if (ts.emission) {
|
|
266
|
-
speak(ts.emission);
|
|
267
|
-
}
|
|
268
|
-
});
|
|
269
|
-
}
|
|
270
|
-
else {
|
|
271
|
-
setCurrentDialogState({
|
|
272
|
-
...currentState,
|
|
273
|
-
emission,
|
|
274
|
-
});
|
|
275
|
-
if (emission) {
|
|
276
|
-
pushMessage({
|
|
277
|
-
text: emission,
|
|
278
|
-
media: currentState.media,
|
|
279
|
-
fromUser: false,
|
|
280
|
-
generatedByAI: !!currentState.completion,
|
|
281
|
-
});
|
|
282
|
-
speak(emission);
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
else if (response.resultCode === 404) {
|
|
287
|
-
setHistory(h => [...h.slice(0, h.length - 1)]);
|
|
288
|
-
reopenSession(false, memoriPwd || memori.secretToken, memoriTokens, instruct && memori.giverTag ? memori.giverTag : undefined, instruct && memori.giverPIN ? memori.giverPIN : undefined, initialContextVars, initialQuestion).then(state => {
|
|
289
|
-
console.info('session timeout');
|
|
290
|
-
if (state === null || state === void 0 ? void 0 : state.sessionID) {
|
|
291
|
-
setTimeout(() => {
|
|
292
|
-
sendMessage(text, media, state === null || state === void 0 ? void 0 : state.sessionID);
|
|
293
|
-
}, 500);
|
|
294
|
-
}
|
|
295
|
-
});
|
|
296
|
-
}
|
|
297
|
-
setMemoriTyping(false);
|
|
298
|
-
};
|
|
299
|
-
const translateDialogState = async (state, userLang) => {
|
|
300
|
-
var _a, _b, _c, _d, _f, _g, _h;
|
|
301
|
-
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';
|
|
302
|
-
const emission = (_f = state.emission) !== null && _f !== void 0 ? _f : currentDialogState === null || currentDialogState === void 0 ? void 0 : currentDialogState.emission;
|
|
303
|
-
let translatedState = { ...state };
|
|
304
|
-
let translatedMsg = null;
|
|
305
|
-
if (!emission ||
|
|
306
|
-
instruct ||
|
|
307
|
-
language.toUpperCase() === userLang.toUpperCase() ||
|
|
308
|
-
!isMultilanguageEnabled) {
|
|
309
|
-
translatedState = { ...state, emission };
|
|
310
|
-
if (emission) {
|
|
311
|
-
translatedMsg = {
|
|
312
|
-
text: emission,
|
|
313
|
-
media: state.media,
|
|
314
|
-
fromUser: false,
|
|
315
|
-
};
|
|
316
|
-
}
|
|
317
|
-
}
|
|
318
|
-
else {
|
|
319
|
-
const t = await (0, translations_1.getTranslation)(emission, userLang, language, baseUrl);
|
|
320
|
-
if (state.hints && state.hints.length > 0) {
|
|
321
|
-
const translatedHints = await Promise.all(((_g = state.hints) !== null && _g !== void 0 ? _g : []).map(async (hint) => {
|
|
322
|
-
var _a;
|
|
323
|
-
const tHint = await (0, translations_1.getTranslation)(hint, userLang, language, baseUrl);
|
|
324
|
-
return {
|
|
325
|
-
text: (_a = tHint === null || tHint === void 0 ? void 0 : tHint.text) !== null && _a !== void 0 ? _a : hint,
|
|
326
|
-
originalText: hint,
|
|
327
|
-
};
|
|
328
|
-
}));
|
|
329
|
-
translatedState = {
|
|
330
|
-
...state,
|
|
331
|
-
emission: t.text,
|
|
332
|
-
translatedHints,
|
|
333
|
-
};
|
|
334
|
-
}
|
|
335
|
-
else {
|
|
336
|
-
translatedState = {
|
|
337
|
-
...state,
|
|
338
|
-
emission: t.text,
|
|
339
|
-
hints: (_h = state.hints) !== null && _h !== void 0 ? _h : (state.state === 'G1' ? currentDialogState === null || currentDialogState === void 0 ? void 0 : currentDialogState.hints : []),
|
|
340
|
-
};
|
|
341
|
-
}
|
|
342
|
-
if (t.text.length > 0)
|
|
343
|
-
translatedMsg = {
|
|
344
|
-
text: t.text,
|
|
345
|
-
media: state.media,
|
|
346
|
-
fromUser: false,
|
|
347
|
-
generatedByAI: !!state.completion,
|
|
348
|
-
};
|
|
349
|
-
}
|
|
350
|
-
setCurrentDialogState(translatedState);
|
|
351
|
-
if (translatedMsg) {
|
|
352
|
-
pushMessage(translatedMsg);
|
|
353
|
-
}
|
|
354
|
-
return translatedState;
|
|
355
|
-
};
|
|
356
|
-
const minAge = memori.ageRestriction
|
|
357
|
-
? memori.ageRestriction
|
|
358
|
-
: memori.nsfw
|
|
359
|
-
? 18
|
|
360
|
-
: memori.enableCompletions
|
|
361
|
-
? 14
|
|
362
|
-
: 0;
|
|
363
|
-
const [birthDate, setBirthDate] = (0, react_1.useState)();
|
|
364
|
-
const [showAgeVerification, setShowAgeVerification] = (0, react_1.useState)(false);
|
|
365
152
|
const [sessionId, setSessionId] = (0, react_1.useState)(initialSessionID);
|
|
366
153
|
const [currentDialogState, _setCurrentDialogState] = (0, react_1.useState)();
|
|
367
154
|
const setCurrentDialogState = (state) => {
|
|
@@ -610,6 +397,201 @@ const MemoriWidget = ({ memori, memoriConfigs, memoriLang, multilingual, integra
|
|
|
610
397
|
restoreGiverTag();
|
|
611
398
|
};
|
|
612
399
|
}, []);
|
|
400
|
+
const [userMessage, setUserMessage] = (0, react_1.useState)('');
|
|
401
|
+
const onChangeUserMessage = (value) => {
|
|
402
|
+
if (!value || value === '\n' || value.trim() === '') {
|
|
403
|
+
setUserMessage('');
|
|
404
|
+
resetInteractionTimeout();
|
|
405
|
+
return;
|
|
406
|
+
}
|
|
407
|
+
setUserMessage(value);
|
|
408
|
+
clearInteractionTimeout();
|
|
409
|
+
};
|
|
410
|
+
const [listening, setListening] = (0, react_1.useState)(false);
|
|
411
|
+
const [history, setHistory] = (0, react_1.useState)([]);
|
|
412
|
+
const pushMessage = (message) => {
|
|
413
|
+
setHistory(history => [...history, { ...message }]);
|
|
414
|
+
};
|
|
415
|
+
const sendMessage = (0, react_1.useCallback)(async (text, media, newSessionId, translate = true, translatedText, hidden = false) => {
|
|
416
|
+
var _a, _b, _c, _d, _f;
|
|
417
|
+
const sessionID = newSessionId || sessionId;
|
|
418
|
+
if (!sessionID || !(text === null || text === void 0 ? void 0 : text.length))
|
|
419
|
+
return;
|
|
420
|
+
if (!hidden)
|
|
421
|
+
pushMessage({
|
|
422
|
+
text: text,
|
|
423
|
+
translatedText,
|
|
424
|
+
fromUser: true,
|
|
425
|
+
media: media !== null && media !== void 0 ? media : [],
|
|
426
|
+
initial: !!newSessionId,
|
|
427
|
+
});
|
|
428
|
+
setMemoriTyping(true);
|
|
429
|
+
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';
|
|
430
|
+
let msg = text;
|
|
431
|
+
if (translate &&
|
|
432
|
+
!instruct &&
|
|
433
|
+
isMultilanguageEnabled &&
|
|
434
|
+
userLang.toUpperCase() !== language.toUpperCase()) {
|
|
435
|
+
const translation = await (0, translations_1.getTranslation)(text, language, userLang, baseUrl);
|
|
436
|
+
msg = translation.text;
|
|
437
|
+
}
|
|
438
|
+
const { currentState, ...response } = await postTextEnteredEvent({
|
|
439
|
+
sessionId: sessionID,
|
|
440
|
+
text: msg,
|
|
441
|
+
});
|
|
442
|
+
if (response.resultCode === 0 && currentState) {
|
|
443
|
+
const emission = (_f = currentState.emission) !== null && _f !== void 0 ? _f : currentDialogState === null || currentDialogState === void 0 ? void 0 : currentDialogState.emission;
|
|
444
|
+
if (currentState.state === 'X4' && memori.giverTag) {
|
|
445
|
+
const { currentState, ...resp } = await postTagChangedEvent(sessionID, memori.giverTag);
|
|
446
|
+
if (resp.resultCode === 0) {
|
|
447
|
+
setCurrentDialogState(currentState);
|
|
448
|
+
if (currentState.emission) {
|
|
449
|
+
pushMessage({
|
|
450
|
+
text: currentState.emission,
|
|
451
|
+
media: currentState.media,
|
|
452
|
+
fromUser: false,
|
|
453
|
+
});
|
|
454
|
+
speak(currentState.emission);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
else {
|
|
458
|
+
console.error(response, resp);
|
|
459
|
+
Message_1.default.error(t((0, error_1.getErrori18nKey)(resp.resultCode)));
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
else if (currentState.state === 'X2d' && memori.giverTag) {
|
|
463
|
+
const { currentState, ...resp } = await postTextEnteredEvent({
|
|
464
|
+
sessionId: sessionID,
|
|
465
|
+
text: Math.random().toString().substring(2, 8),
|
|
466
|
+
});
|
|
467
|
+
if (resp.resultCode === 0) {
|
|
468
|
+
const { currentState, ...resp } = await postTagChangedEvent(sessionID, memori.giverTag);
|
|
469
|
+
if (resp.resultCode === 0) {
|
|
470
|
+
setCurrentDialogState(currentState);
|
|
471
|
+
if (currentState.emission) {
|
|
472
|
+
pushMessage({
|
|
473
|
+
text: currentState.emission,
|
|
474
|
+
media: currentState.media,
|
|
475
|
+
fromUser: false,
|
|
476
|
+
});
|
|
477
|
+
speak(currentState.emission);
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
else {
|
|
481
|
+
console.error(response, resp);
|
|
482
|
+
Message_1.default.error(t((0, error_1.getErrori18nKey)(resp.resultCode)));
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
else {
|
|
486
|
+
console.error(response, resp);
|
|
487
|
+
Message_1.default.error(t((0, error_1.getErrori18nKey)(resp.resultCode)));
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
else if (userLang.toLowerCase() !== language.toLowerCase() &&
|
|
491
|
+
emission &&
|
|
492
|
+
!instruct &&
|
|
493
|
+
isMultilanguageEnabled) {
|
|
494
|
+
translateDialogState(currentState, userLang).then(ts => {
|
|
495
|
+
if (ts.emission) {
|
|
496
|
+
speak(ts.emission);
|
|
497
|
+
}
|
|
498
|
+
});
|
|
499
|
+
}
|
|
500
|
+
else {
|
|
501
|
+
setCurrentDialogState({
|
|
502
|
+
...currentState,
|
|
503
|
+
emission,
|
|
504
|
+
});
|
|
505
|
+
if (emission) {
|
|
506
|
+
pushMessage({
|
|
507
|
+
text: emission,
|
|
508
|
+
media: currentState.media,
|
|
509
|
+
fromUser: false,
|
|
510
|
+
generatedByAI: !!currentState.completion,
|
|
511
|
+
});
|
|
512
|
+
speak(emission);
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
else if (response.resultCode === 404) {
|
|
517
|
+
setHistory(h => [...h.slice(0, h.length - 1)]);
|
|
518
|
+
reopenSession(false, memoriPwd || memori.secretToken, memoriTokens, instruct && memori.giverTag ? memori.giverTag : undefined, instruct && memori.giverPIN ? memori.giverPIN : undefined, initialContextVars, initialQuestion).then(state => {
|
|
519
|
+
console.info('session timeout');
|
|
520
|
+
if (state === null || state === void 0 ? void 0 : state.sessionID) {
|
|
521
|
+
setTimeout(() => {
|
|
522
|
+
sendMessage(text, media, state === null || state === void 0 ? void 0 : state.sessionID);
|
|
523
|
+
}, 500);
|
|
524
|
+
}
|
|
525
|
+
});
|
|
526
|
+
}
|
|
527
|
+
setMemoriTyping(false);
|
|
528
|
+
}, [sessionId]);
|
|
529
|
+
const translateDialogState = async (state, userLang) => {
|
|
530
|
+
var _a, _b, _c, _d, _f, _g, _h;
|
|
531
|
+
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';
|
|
532
|
+
const emission = (_f = state.emission) !== null && _f !== void 0 ? _f : currentDialogState === null || currentDialogState === void 0 ? void 0 : currentDialogState.emission;
|
|
533
|
+
let translatedState = { ...state };
|
|
534
|
+
let translatedMsg = null;
|
|
535
|
+
if (!emission ||
|
|
536
|
+
instruct ||
|
|
537
|
+
language.toUpperCase() === userLang.toUpperCase() ||
|
|
538
|
+
!isMultilanguageEnabled) {
|
|
539
|
+
translatedState = { ...state, emission };
|
|
540
|
+
if (emission) {
|
|
541
|
+
translatedMsg = {
|
|
542
|
+
text: emission,
|
|
543
|
+
media: state.media,
|
|
544
|
+
fromUser: false,
|
|
545
|
+
};
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
else {
|
|
549
|
+
const t = await (0, translations_1.getTranslation)(emission, userLang, language, baseUrl);
|
|
550
|
+
if (state.hints && state.hints.length > 0) {
|
|
551
|
+
const translatedHints = await Promise.all(((_g = state.hints) !== null && _g !== void 0 ? _g : []).map(async (hint) => {
|
|
552
|
+
var _a;
|
|
553
|
+
const tHint = await (0, translations_1.getTranslation)(hint, userLang, language, baseUrl);
|
|
554
|
+
return {
|
|
555
|
+
text: (_a = tHint === null || tHint === void 0 ? void 0 : tHint.text) !== null && _a !== void 0 ? _a : hint,
|
|
556
|
+
originalText: hint,
|
|
557
|
+
};
|
|
558
|
+
}));
|
|
559
|
+
translatedState = {
|
|
560
|
+
...state,
|
|
561
|
+
emission: t.text,
|
|
562
|
+
translatedHints,
|
|
563
|
+
};
|
|
564
|
+
}
|
|
565
|
+
else {
|
|
566
|
+
translatedState = {
|
|
567
|
+
...state,
|
|
568
|
+
emission: t.text,
|
|
569
|
+
hints: (_h = state.hints) !== null && _h !== void 0 ? _h : (state.state === 'G1' ? currentDialogState === null || currentDialogState === void 0 ? void 0 : currentDialogState.hints : []),
|
|
570
|
+
};
|
|
571
|
+
}
|
|
572
|
+
if (t.text.length > 0)
|
|
573
|
+
translatedMsg = {
|
|
574
|
+
text: t.text,
|
|
575
|
+
media: state.media,
|
|
576
|
+
fromUser: false,
|
|
577
|
+
generatedByAI: !!state.completion,
|
|
578
|
+
};
|
|
579
|
+
}
|
|
580
|
+
setCurrentDialogState(translatedState);
|
|
581
|
+
if (translatedMsg) {
|
|
582
|
+
pushMessage(translatedMsg);
|
|
583
|
+
}
|
|
584
|
+
return translatedState;
|
|
585
|
+
};
|
|
586
|
+
const minAge = memori.ageRestriction
|
|
587
|
+
? memori.ageRestriction
|
|
588
|
+
: memori.nsfw
|
|
589
|
+
? 18
|
|
590
|
+
: memori.enableCompletions
|
|
591
|
+
? 14
|
|
592
|
+
: 0;
|
|
593
|
+
const [birthDate, setBirthDate] = (0, react_1.useState)();
|
|
594
|
+
const [showAgeVerification, setShowAgeVerification] = (0, react_1.useState)(false);
|
|
613
595
|
const [userInteractionTimeout, setUserInteractionTimeout] = (0, react_1.useState)();
|
|
614
596
|
const timeoutRef = (0, react_1.useRef)();
|
|
615
597
|
const clearInteractionTimeout = () => {
|
|
@@ -879,6 +861,7 @@ const MemoriWidget = ({ memori, memoriConfigs, memoriLang, multilingual, integra
|
|
|
879
861
|
if (preview)
|
|
880
862
|
return;
|
|
881
863
|
if (muteSpeaker || speakerMuted) {
|
|
864
|
+
memoriSpeaking = false;
|
|
882
865
|
if (continuousSpeech) {
|
|
883
866
|
setListeningTimeout();
|
|
884
867
|
}
|
|
@@ -897,6 +880,7 @@ const MemoriWidget = ({ memori, memoriConfigs, memoriLang, multilingual, integra
|
|
|
897
880
|
audioContext.suspend();
|
|
898
881
|
if (isPlayingAudio) {
|
|
899
882
|
try {
|
|
883
|
+
memoriSpeaking = false;
|
|
900
884
|
if (speechSynthesizer) {
|
|
901
885
|
speechSynthesizer.close();
|
|
902
886
|
speechSynthesizer = null;
|
|
@@ -937,15 +921,18 @@ const MemoriWidget = ({ memori, memoriConfigs, memoriLang, multilingual, integra
|
|
|
937
921
|
const source = audioContext.createBufferSource();
|
|
938
922
|
source.addEventListener('ended', () => {
|
|
939
923
|
setIsPlayingAudio(false);
|
|
924
|
+
memoriSpeaking = false;
|
|
940
925
|
});
|
|
941
926
|
audioDestination.onAudioEnd = () => {
|
|
942
927
|
setIsPlayingAudio(false);
|
|
928
|
+
memoriSpeaking = false;
|
|
943
929
|
source.disconnect();
|
|
944
930
|
onEndSpeakStartListen();
|
|
945
931
|
};
|
|
946
932
|
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(escapeHTML(text), userLang.toLowerCase())}</s></voice></speak>`, result => {
|
|
947
933
|
if (result) {
|
|
948
934
|
setIsPlayingAudio(true);
|
|
935
|
+
memoriSpeaking = true;
|
|
949
936
|
try {
|
|
950
937
|
audioContext.decodeAudioData(result.audioData, function (buffer) {
|
|
951
938
|
source.buffer = buffer;
|
|
@@ -959,6 +946,7 @@ const MemoriWidget = ({ memori, memoriConfigs, memoriLang, multilingual, integra
|
|
|
959
946
|
audioContext.state === 'closed') {
|
|
960
947
|
source.disconnect();
|
|
961
948
|
setIsPlayingAudio(false);
|
|
949
|
+
memoriSpeaking = false;
|
|
962
950
|
}
|
|
963
951
|
else if (audioContext.state === 'interrupted') {
|
|
964
952
|
audioContext.resume();
|
|
@@ -974,6 +962,7 @@ const MemoriWidget = ({ memori, memoriConfigs, memoriLang, multilingual, integra
|
|
|
974
962
|
console.error('speak error: ', e);
|
|
975
963
|
window.speechSynthesis.speak(new SpeechSynthesisUtterance(text));
|
|
976
964
|
setIsPlayingAudio(false);
|
|
965
|
+
memoriSpeaking = false;
|
|
977
966
|
if (speechSynthesizer) {
|
|
978
967
|
speechSynthesizer.close();
|
|
979
968
|
speechSynthesizer = null;
|
|
@@ -983,16 +972,19 @@ const MemoriWidget = ({ memori, memoriConfigs, memoriLang, multilingual, integra
|
|
|
983
972
|
else {
|
|
984
973
|
audioContext.resume();
|
|
985
974
|
setIsPlayingAudio(false);
|
|
975
|
+
memoriSpeaking = false;
|
|
986
976
|
}
|
|
987
977
|
}, error => {
|
|
988
978
|
console.error('speak:', error);
|
|
989
979
|
window.speechSynthesis.speak(new SpeechSynthesisUtterance(text));
|
|
990
980
|
setIsPlayingAudio(false);
|
|
981
|
+
memoriSpeaking = false;
|
|
991
982
|
});
|
|
992
983
|
setMemoriTyping(false);
|
|
993
984
|
};
|
|
994
985
|
const stopAudio = () => {
|
|
995
986
|
setIsPlayingAudio(false);
|
|
987
|
+
memoriSpeaking = false;
|
|
996
988
|
try {
|
|
997
989
|
if (speechSynthesizer) {
|
|
998
990
|
speechSynthesizer.close();
|
|
@@ -1349,6 +1341,27 @@ const MemoriWidget = ({ memori, memoriConfigs, memoriLang, multilingual, integra
|
|
|
1349
1341
|
stopAudio();
|
|
1350
1342
|
sendMessage(text, undefined, undefined, false, translatedText);
|
|
1351
1343
|
};
|
|
1344
|
+
const memoriTextEnteredHandler = (0, react_1.useCallback)((e) => {
|
|
1345
|
+
var _a;
|
|
1346
|
+
const { text, hidden } = e.detail;
|
|
1347
|
+
const sessionID = sessionId || ((_a = window.getMemoriState()) === null || _a === void 0 ? void 0 : _a.sessionID);
|
|
1348
|
+
if (text) {
|
|
1349
|
+
if (!speakerMuted && (memoriSpeaking || memoriTyping)) {
|
|
1350
|
+
setTimeout(() => {
|
|
1351
|
+
memoriTextEnteredHandler(e);
|
|
1352
|
+
}, 1000);
|
|
1353
|
+
}
|
|
1354
|
+
else {
|
|
1355
|
+
sendMessage(text, undefined, sessionID, undefined, undefined, hidden);
|
|
1356
|
+
}
|
|
1357
|
+
}
|
|
1358
|
+
}, [sessionId, isPlayingAudio, memoriTyping]);
|
|
1359
|
+
(0, react_1.useEffect)(() => {
|
|
1360
|
+
document.addEventListener('MemoriTextEntered', memoriTextEnteredHandler);
|
|
1361
|
+
return () => {
|
|
1362
|
+
document.removeEventListener('MemoriTextEntered', memoriTextEnteredHandler);
|
|
1363
|
+
};
|
|
1364
|
+
}, []);
|
|
1352
1365
|
const onClickStart = (0, react_1.useCallback)(async (session) => {
|
|
1353
1366
|
const sessionID = (session === null || session === void 0 ? void 0 : session.sessionID) || sessionId;
|
|
1354
1367
|
const dialogState = (session === null || session === void 0 ? void 0 : session.dialogState) || currentDialogState;
|