UNPKG

@memori.ai/memori-react

Version:

[![npm version](https://img.shields.io/github/package-json/v/memori-ai/memori-react)](https://www.npmjs.com/package/@memori.ai/memori-react) ![Tests](https://github.com/memori-ai/memori-react/workflows/CI/badge.svg?branch=main) ![TypeScript Support](https

221 lines 12.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const jsx_runtime_1 = require("react/jsx-runtime"); const react_1 = require("react"); const prop_types_1 = tslib_1.__importDefault(require("prop-types")); const memori_api_client_1 = tslib_1.__importDefault(require("@memori.ai/memori-api-client")); const MemoriWidget_1 = tslib_1.__importDefault(require("./components/MemoriWidget/MemoriWidget")); const visemeContext_1 = require("./context/visemeContext"); const react_hot_toast_1 = require("react-hot-toast"); const utils_1 = require("./helpers/utils"); const i18n_1 = tslib_1.__importDefault(require("./i18n")); const react_i18next_1 = require("react-i18next"); const I18nWrapper_1 = tslib_1.__importDefault(require("./I18nWrapper")); const getPreferredLanguages = () => { const browserLanguage = navigator.language; if (browserLanguage) { let lng = browserLanguage.split('-')[0]; if (['en', 'it'].includes(lng)) { return { lng, fallbackLng: lng === 'en' ? 'it' : 'en', }; } } return { lng: 'en', fallbackLng: 'it', }; }; const getParsedContext = (context) => { var _a; if (!context) return {}; const parsedContext = (_a = context === null || context === void 0 ? void 0 : context.split(',')) === null || _a === void 0 ? void 0 : _a.reduce((acc, cur) => { const [key, value] = cur.split(':').map(t => t.trim()); return { ...acc, [key]: value }; }, {}); return parsedContext; }; const Memori = ({ ownerUserName, ownerUserID, memoriName, memoriID, integration, integrationID, tenantID, secretToken, sessionID, layout, customLayout, showShare, showCopyButton = true, showTranslationOriginal = false, showSettings, showTypingText = false, showClear = false, showOnlyLastMessages, showInputs = true, showDates = false, showContextPerLine = false, showUpload, showLogin, showReasoning, height = '100%', baseURL, apiURL = 'https://backend.memori.ai', engineURL = 'https://engine.memori.ai', tag, pin, context, initialQuestion, showChatHistory = true, uiLang, spokenLang, multilingual, authToken, enableAudio, defaultSpeakerActive = true, disableTextEnteredEvents = false, onStateChange, additionalInfo, customMediaRenderer, additionalSettings, userAvatar, useMathFormatting = false, autoStart, applyVarsToRoot = false, }) => { var _a, _b, _c, _d; const [memori, setMemori] = (0, react_1.useState)(); const [tenant, setTenant] = (0, react_1.useState)(); const [speechKey, setSpeechKey] = (0, react_1.useState)(); const { t } = (0, react_i18next_1.useTranslation)(); if (!((memoriID && ownerUserID) || (memoriName && ownerUserName))) { throw new Error('Identifier pair required: please provide either memoriID and ownerUserID or memoriName and ownerUserName'); } const client = (0, memori_api_client_1.default)(apiURL, engineURL); const fetchSpeechKey = (0, react_1.useCallback)(async () => { const url = baseURL || (tenantID.startsWith('https://') ? tenantID : `https://${tenantID}`); try { const result = await fetch(`${url}/api/speechkey`); const data = await result.json(); if (data.AZURE_COGNITIVE_SERVICES_TTS_KEY) { setSpeechKey(data.AZURE_COGNITIVE_SERVICES_TTS_KEY); } else { console.log('AZURE_COGNITIVE_SERVICES_TTS_KEY not found'); } } catch (error) { console.error('Error fetching speech key', error); } }, []); (0, react_1.useEffect)(() => { fetchSpeechKey(); }, []); const fetchMemori = (0, react_1.useCallback)(async () => { if (memoriID && ownerUserID) { const { memori, ...resp } = await client.backend.getMemoriByUserAndId(tenantID, ownerUserID, memoriID); if (resp.resultCode === 0 && !!memori) { setMemori(memori); } else { console.error('[MEMORI]', resp, memori); } } else if (memoriName && ownerUserName) { const { memori, ...resp } = await client.backend.getMemori(tenantID, ownerUserName, memoriName); if (resp.resultCode === 0 && !!memori) { if (!memori.ownerUserID && ownerUserID) { memori.ownerUserID = ownerUserID; } setMemori(memori); } else { console.error('[MEMORI]', resp, memori); } } }, [memoriID, ownerUserID, memoriName, ownerUserName, tenantID]); (0, react_1.useEffect)(() => { fetchMemori(); }, [fetchMemori, tenantID]); const fetchTenant = (0, react_1.useCallback)(async () => { const { tenant, ...resp } = await client.backend.tenant.getTenant(tenantID); if (tenant && resp.resultCode === 0) setTenant(tenant); else console.debug('[TENANT]', resp, tenant); }, [tenantID, apiURL]); (0, react_1.useEffect)(() => { fetchTenant(); }, [fetchTenant]); (0, react_1.useEffect)(() => { if (uiLang) { i18n_1.default.changeLanguage(uiLang.toLowerCase()); } else { const { lng, fallbackLng } = getPreferredLanguages(); i18n_1.default.changeLanguage(lng).catch(() => { i18n_1.default.changeLanguage(fallbackLng); }); } }, [uiLang]); const layoutIntegration = integration !== null && integration !== void 0 ? integration : (_a = memori === null || memori === void 0 ? void 0 : memori.integrations) === null || _a === void 0 ? void 0 : _a.find(i => integrationID ? i.integrationID === integrationID : !!i.publish && i.type === 'LANDING_EXPERIENCE'); const layoutIntegrationConfig = (0, utils_1.safeParseJSON)((_b = layoutIntegration === null || layoutIntegration === void 0 ? void 0 : layoutIntegration.customData) !== null && _b !== void 0 ? _b : '{}'); const whiteListedDomains = layoutIntegrationConfig.whiteListedDomains; if (whiteListedDomains) { if (typeof window !== 'undefined') { if (!whiteListedDomains.some((domain) => new RegExp(domain).test(window.location.hostname))) { return null; } } } const initialContextVars = context !== null && context !== void 0 ? context : getParsedContext(layoutIntegrationConfig.contextVars); const initialQuestionLayout = initialQuestion !== null && initialQuestion !== void 0 ? initialQuestion : layoutIntegrationConfig.initialQuestion; return ((0, jsx_runtime_1.jsx)(I18nWrapper_1.default, { children: (0, jsx_runtime_1.jsxs)(visemeContext_1.VisemeProvider, { children: [(0, jsx_runtime_1.jsx)(react_hot_toast_1.Toaster, { position: "top-center", reverseOrder: true }), memori ? ((0, jsx_runtime_1.jsx)(MemoriWidget_1.default, { layout: layout, customLayout: customLayout, height: height, baseUrl: baseURL || (tenantID.startsWith('https://') || tenantID.startsWith('http://') ? tenantID : `https://${tenantID}`), apiURL: apiURL, engineURL: engineURL, memori: { ...memori, secretToken, }, ownerUserName: ownerUserName !== null && ownerUserName !== void 0 ? ownerUserName : memori.ownerUserName, ownerUserID: ownerUserID !== null && ownerUserID !== void 0 ? ownerUserID : memori.ownerUserID, tenantID: tenantID, memoriLang: spokenLang !== null && spokenLang !== void 0 ? spokenLang : (_d = (_c = memori.culture) === null || _c === void 0 ? void 0 : _c.split('-')) === null || _d === void 0 ? void 0 : _d[0], multilingual: multilingual, showChatHistory: showChatHistory, tenant: tenant, secret: secretToken, sessionID: sessionID, showShare: showShare, showCopyButton: showCopyButton, showTranslationOriginal: showTranslationOriginal, showSettings: showSettings, showTypingText: showTypingText, showClear: showClear, showOnlyLastMessages: showOnlyLastMessages, showInputs: showInputs, showDates: showDates, showContextPerLine: showContextPerLine, showLogin: showLogin !== null && showLogin !== void 0 ? showLogin : memori === null || memori === void 0 ? void 0 : memori.enableDeepThought, showUpload: showUpload, showReasoning: showReasoning, integration: layoutIntegration, initialContextVars: initialContextVars, initialQuestion: initialQuestionLayout, authToken: authToken, AZURE_COGNITIVE_SERVICES_TTS_KEY: speechKey, autoStart: autoStart !== undefined ? autoStart : layout === 'HIDDEN_CHAT' ? true : autoStart, enableAudio: enableAudio && !!speechKey, defaultSpeakerActive: defaultSpeakerActive, disableTextEnteredEvents: disableTextEnteredEvents, onStateChange: onStateChange, additionalInfo: additionalInfo, customMediaRenderer: customMediaRenderer, additionalSettings: additionalSettings, userAvatar: userAvatar, useMathFormatting: useMathFormatting, applyVarsToRoot: applyVarsToRoot, ...(tag && pin ? { personification: { tag, pin } } : {}) })) : ((0, jsx_runtime_1.jsx)("div", { style: { display: 'flex', alignItems: 'center', justifyContent: 'center', }, children: (0, jsx_runtime_1.jsxs)("p", { style: { textAlign: 'center', margin: '2rem auto', textTransform: 'capitalize', }, children: [t('loading') || 'Loading', "..."] }) }))] }) })); }; Memori.propTypes = { memoriName: prop_types_1.default.string, memoriID: prop_types_1.default.string, ownerUserName: prop_types_1.default.string, ownerUserID: prop_types_1.default.string, integrationID: prop_types_1.default.string, integration: prop_types_1.default.any, tenantID: prop_types_1.default.string.isRequired, secretToken: prop_types_1.default.string, sessionID: prop_types_1.default.string, layout: prop_types_1.default.oneOf([ 'DEFAULT', 'FULLPAGE', 'TOTEM', 'WEBSITE_ASSISTANT', 'CHAT', 'HIDDEN_CHAT', 'ZOOMED_FULL_BODY', ]), customLayout: prop_types_1.default.any, showShare: prop_types_1.default.bool, showCopyButton: prop_types_1.default.bool, showTranslationOriginal: prop_types_1.default.bool, showInputs: prop_types_1.default.bool, showDates: prop_types_1.default.bool, showContextPerLine: prop_types_1.default.bool, showSettings: prop_types_1.default.bool, showClear: prop_types_1.default.bool, showOnlyLastMessages: prop_types_1.default.bool, showTypingText: prop_types_1.default.bool, showLogin: prop_types_1.default.bool, showUpload: prop_types_1.default.bool, showReasoning: prop_types_1.default.bool, height: prop_types_1.default.oneOfType([prop_types_1.default.string, prop_types_1.default.number]), baseURL: prop_types_1.default.string, apiURL: prop_types_1.default.string, engineURL: prop_types_1.default.string, tag: prop_types_1.default.string, pin: prop_types_1.default.string, context: prop_types_1.default.objectOf(prop_types_1.default.any), initialQuestion: prop_types_1.default.string, uiLang: prop_types_1.default.oneOf([ 'en', 'it', 'fr', 'es', 'de', 'EN', 'IT', 'FR', 'ES', 'DE', ]), spokenLang: prop_types_1.default.string, multilingual: prop_types_1.default.bool, authToken: prop_types_1.default.string, enableAudio: prop_types_1.default.bool, defaultSpeakerActive: prop_types_1.default.bool, disableTextEnteredEvents: prop_types_1.default.bool, onStateChange: prop_types_1.default.func, additionalInfo: prop_types_1.default.objectOf(prop_types_1.default.any), customMediaRenderer: prop_types_1.default.func, additionalSettings: prop_types_1.default.any, userAvatar: prop_types_1.default.oneOfType([prop_types_1.default.string, prop_types_1.default.any]), useMathFormatting: prop_types_1.default.bool, autoStart: prop_types_1.default.bool, applyVarsToRoot: prop_types_1.default.bool, }; exports.default = Memori; //# sourceMappingURL=index.js.map