6.4.4 • Published 21 hours ago

@memori.ai/memori-react v6.4.4

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
21 hours ago

Memori React

npm version Tests TypeScript Support

Library to integrate a Twin from Memori in a React app.

Web Platform: AIsuru

Installation

yarn add @memori.ai/memori-react
npm install @memori.ai/memori-react

Usage

Import the component:

import Memori from '@memori.ai/memori-react';

Import the CSS:

import '@memori.ai/memori-react/dist/styles.css';

Then use it in your app:

const App = () => (
  <Memori
    memoriName="Memori"
    ownerUserName="nunziofiore"
    tenantID="app.memorytwin.com"
    apiURL="https://backend.memori.ai"
    baseURL="https://app.memorytwin.com"
    uiLang="it"
    showShare
    height="100vh"
  />
);

Props

PropRequiredTypeDefaultDescription
memoriName* (see below)stringName of the Memori
ownerUserName* (see below)stringUsername of the Memori owner
memoriID* (see below)stringID of the Memori
ownerUserID* (see below)stringID of the Memori owner
tenantID✔️stringTenant ID, example: "aisuru.com" or "app.memorytwin.com"
sessionIDstringInitial Session ID, UUID which refers to the session to the Memori and identifies a conversation and its permissions (giver, receiver, anonymous). A session would be started autonomously with the params set, but if you have an existing and valid sessionID you can pass it as already opened one. Use this at your risk, as session recovery might break or start session as anon user. In most cases, you shoudn't use this prop.
authTokenstringAuthentication token from user login, needed for giver sessions to upload assets
integrationIDstringIntegration ID, UUID which refers to the public page layout
secretTokenstringSecret token, the password of a private or secret Memori
heightstring"100%"Height of the Memori
showSharebooltrueShow the share button
showSettingsbooltrueShow the settings panel button
showTypingTextboolfalseShow default sentences while loading text (see: Typing stories)
showInstructboolfalseShow the switch selecting between test mode or instruct mode, needs an administrative session as a giver
showLoginbooltrueShow the login button
showClearboolfalseShow clear chat history button
showOnlyLastMessagesbooltrue or false *Show only last 2 messages. (*) Defaults to true for TOTEM and WEBSITE_ASSISTANT layouts, false otherwise
baseURLstringBase URL of the Memori, example: "https://aisuru.com"
apiURLstring"https://backend.memori.ai"URL of the Memori API
tagstringTag of the person opening the session to the Memori, could be the giver or a receiver
pinstringPIN of the person opening the session to the Memori, could be the giver or a receiver
contextstringInitial context of the conversation, dictionary with "key: value" pairs as context variables
initialQuestionstringInitial question to ask to the Memori, starts the conversation as this would be sent to the Memori
uiLang'en' \| 'it'"en"Language of the UI, es: "en" or "it"
multilingualboolfalseEnable multilingual mode, if enabled the user can switch between spoken languages
spokenLangstringLanguage of the spoken text, as defaults to user selection. Example: "en" or "it"
onStateChangefunctionCallback function called when the state of the Memori changes
defaultSpeakerActivebooleantrueDefault value for the speaker activation
AZURE_COGNITIVE_SERVICES_TTS_KEYstringAzure Cognitive Services TTS key, used to generate the audio of the Memori and for STT recognition
layoutstringLayout of the Memori, can be "FULLPAGE" (default), "CHAT", "WEBSITE_ASSISTANT" or "TOTEM", see below
customLayoutReact.FC<LayoutProps>Custom layout component, see below
customMediaRenderer(mimeType: string) => JSX.Element \| nullCustom media renderer, see below
additionalSettingsJSX.ElementCustom JSX or component to render within the settings drawer
userAvatarstringCustom URL or React element to use as user avatar

*: one of these pairs is required: memoriName + ownerUserName, memoriID + ownerUserID

Layouts

The Memori can be displayed in three different layouts: FULLPAGE, CHAT, WEBSITE_ASSISTANT and TOTEM. If you don't specify a layout, the default one is FULLPAGE.

FULLPAGE

TOTEM

CHAT

WEBSITE_ASSISTANT

Custom layout

You can override the default layout by passing a custom layout component to the customLayout prop.

The custom layout component must be a React functional component that accepts a LayoutProps object as props.

import type { LayoutProps } from '@memori.ai/memori-react/components/MemoriWidget';

const MyCustomLayout: React.FC<LayoutProps> = ({
  Header,
  headerProps,
  Avatar,
  avatarProps,
  Chat,
  chatProps,
  StartPanel,
  startPanelProps,
  integrationStyle,
  integrationBackground,
  ChangeMode,
  changeModeProps,
  sessionId,
  hasUserActivatedSpeak,
  showInstruct = false,
  loading = false,
  poweredBy,
}) => (
  <>
    {integrationStyle}
    {integrationBackground}

    <Spin spinning={loading} className="memori-mycustom-layout">
      {poweredBy}

      <div className="memori-mycustom-layout--controls">
        {sessionId && hasUserActivatedSpeak && Chat && chatProps ? (
          <Chat {...chatProps} />
        ) : startPanelProps ? (
          <StartPanel {...startPanelProps} />
        ) : null}
      </div>
    </Spin>
  </>
);

And then pass it to the customLayout prop:

  <Memori
    ...
    customLayout={MyCustomLayout}
  />

Styling

You can override the default styles of the Memori by customizing the following CSS custom properties:

memori-client,
#headlessui-portal-root,
.memori-widget {
  --memori-primary: rgb(102, 103, 171);
  --memori-primary-text: #fff;
  --memori-inner-content-pad: 1rem;
  --memori-inner-bg: transparent;
  --memori-chat-bubble-bg: #ffffff60;
  --memori-text-color: #000;
  --memori-button-bg: #fff;
  --memori-button-text: #000;
  --memori-button-padding: 0.5rem 1.5rem;
  --memori-button-border-color: #d9d9d9;
  --memori-button-radius: 5px;
  --memori-button-box-shadow: 0 2px 0 rgba(0, 0, 0, 0.02);
  --memori-blur-background: 0px;
  --memori-drawer--width: 100%;
  --memori-drawer--width--md: 80%;
  --memori-drawer--width--lg: 60%;
  --memori-modal--width: 100%;
  --memori-modal--width--md: 80%;
  --memori-error-color: #ff4d4f;
}

You can review the default styles in the styles.css file.

Component overrides

When using the customLayout prop, you can also override the default components used by the client.

const MyCustomChat: LayoutProps['Chat'] = ({ history, sendMessage }) => {
  const [message, setMessage] = React.useState('');

  ...
}

const MyCustomAvatar: LayoutProps['Avatar'] = (props) => {
  ...
}

const CustomLayout: React.FC<LayoutProps> = ({
  avatarProps,
  chatProps,
  StartPanel,
  startPanelProps,
  sessionId,
  hasUserActivatedSpeak,
  loading = false,
  poweredBy,
}) => (
  <>
    <Spin spinning={loading} className="memori-mycustom-layout">
      {poweredBy}

      <div className="memori-mycustom-layout--avatar">
        <MyCustomAvatar {...avatarProps} />
      </div>

      <div className="memori-mycustom-layout--controls">
        {sessionId && hasUserActivatedSpeak && Chat && chatProps ? (
          <MyCustomChat {...chatProps} />
        ) : startPanelProps ? (
          <StartPanel {...startPanelProps} />
        ) : null}
      </div>
    </Spin>
  </>
);

Custom media renderer

You can override the default media renderer by passing a custom function to the customMediaRenderer prop. This can override the default media renderer for all media types or just for a specific one.

You can also use this to extend the default media renderer with additional media types.

<Memori
  ...
  customMediaRenderer={(mimeType: string) => {
    if (mimeType === 'custom/content-type') {
      return <MyCustomImageRenderer />;
    }

    return null;
  }}
/>

Global utilities

When rendered, the Memori widget exposes some global functions that can be used to interact with the Memori.

Get the state of the Twin

let dialogState = getMemoriState();
let sessionID = getMemoriState().sessionID;
let dialogState = getMemoriState(myWidgetIntegrationId); // in case you have multiple widgets on the same page

Otherwise, you can achieve the same result manually by reading from the HTML code of the widget the attribute data-memori-engine-state.

let dialogState = JSON.parse(
  document.querySelector('div[data-memori-engine-state]')?.dataset
    ?.memoriEngineState ?? '{}'
);

Write and send a message to the Twin

Write and send a message to the Twin. You can use this method to send a message to the Twin, such as to continue a conversation with a specific message or following an action.

typeMessage('Hello World!');

Additional parameters:

const waitForPrevious = true; // waits for previous message to be read, default: true
const hidden = true; // message is not visible to the user, only the response is, default: false
const typingText = "Asking the unicorns' opinion..."; // text to show in the loader while the Twin is answering, defaults to none
typeMessage('Hello World!', waitForPrevious, hidden, typingText);

There is also an alias function that does not show the message sent to the user, but only the Twin's response:

const waitForPrevious = true; // waits for previous message to be read, default: true
typeMessageHidden('Hello World!', waitForPrevious);

// alias to
typeMessage('Hello World!', waitForPrevious, true);

See also

6.4.4

21 hours ago

6.4.3

2 days ago

6.4.2

4 days ago

6.4.1

4 days ago

6.4.0

5 days ago

6.3.1

12 days ago

6.3.0

17 days ago

6.2.0

18 days ago

6.1.7

25 days ago

6.1.6

1 month ago

6.1.5

1 month ago

6.1.4

1 month ago

6.1.3

1 month ago

6.1.2

2 months ago

6.1.1

2 months ago

6.1.0

2 months ago

6.0.0-rc.3

2 months ago

6.0.0-rc.2

2 months ago

6.0.0

2 months ago

6.0.0-rc.1

2 months ago

6.0.0-rc.0

2 months ago

5.1.0

2 months ago

5.0.2

2 months ago

5.0.1

2 months ago

5.0.0

3 months ago

4.4.1

3 months ago

4.4.0

3 months ago

4.3.1

3 months ago

4.3.0

4 months ago

4.2.0

4 months ago

4.1.1

4 months ago

4.1.0

5 months ago

4.0.1

5 months ago

4.0.0

5 months ago

3.0.2

5 months ago

3.0.1

5 months ago

3.0.0

5 months ago

3.0.0-rc.0

5 months ago

2.23.0

5 months ago

2.22.0

5 months ago

2.21.0

5 months ago

2.20.2

5 months ago

2.11.0

9 months ago

2.8.1

9 months ago

2.8.0

10 months ago

2.19.2

6 months ago

2.19.0

6 months ago

2.9.2

9 months ago

2.19.1

6 months ago

2.9.1

9 months ago

2.15.0

8 months ago

2.15.1

8 months ago

2.10.1

9 months ago

2.10.2

9 months ago

2.10.0

9 months ago

2.7.0

10 months ago

2.7.2

10 months ago

2.7.1

10 months ago

2.18.9

7 months ago

2.18.7

7 months ago

2.18.8

7 months ago

2.18.5

7 months ago

2.18.6

7 months ago

2.18.3

7 months ago

2.18.4

7 months ago

2.18.1

7 months ago

2.8.3

9 months ago

2.18.2

7 months ago

2.8.2

9 months ago

2.18.0

8 months ago

2.14.0

8 months ago

2.6.3

10 months ago

2.6.2

10 months ago

2.17.0

8 months ago

2.17.1

8 months ago

2.13.0

8 months ago

2.13.1

8 months ago

2.20.0

5 months ago

2.20.1

5 months ago

2.12.0

9 months ago

2.9.0

9 months ago

2.6.5

10 months ago

2.6.4

10 months ago

2.16.1

8 months ago

2.6.6

10 months ago

2.16.0

8 months ago

2.2.1

11 months ago

2.2.0

11 months ago

2.4.1

11 months ago

2.4.0

11 months ago

2.2.2

11 months ago

2.6.1

11 months ago

2.6.0

11 months ago

2.4.2

11 months ago

2.0.9

1 year ago

2.3.0

11 months ago

2.5.0

11 months ago

2.3.1

11 months ago

2.0.11

12 months ago

2.5.1

11 months ago

2.0.10

12 months ago

2.1.0

12 months ago

1.2.0

1 year ago

1.0.2

1 year ago

1.1.0

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago

1.2.1

1 year ago

2.0.3

1 year ago

2.0.2

1 year ago

2.0.5

1 year ago

2.0.4

1 year ago

2.0.7

1 year ago

2.0.6

1 year ago

2.0.8

1 year ago

2.0.1

1 year ago

2.0.0

1 year ago

1.0.0-rc.5

1 year ago

1.0.0-rc.6

1 year ago

1.0.0-rc.4

1 year ago

1.0.0-rc.3

1 year ago

1.0.0-rc.1

1 year ago

1.0.0-rc.2

1 year ago

1.0.0-rc.0

1 year ago

1.0.0-alpha.24

1 year ago

1.0.0-alpha.23

1 year ago

1.0.0-alpha.22

1 year ago

1.0.0-alpha.21

1 year ago

1.0.0-alpha.20

1 year ago

1.0.0-alpha.19

1 year ago

1.0.0-alpha.18

1 year ago

1.0.0-alpha.17

1 year ago

1.0.0-alpha.16

1 year ago

1.0.0-alpha.15

1 year ago

1.0.0-alpha.14

1 year ago

1.0.0-alpha.13

1 year ago

1.0.0-alpha.12

1 year ago

1.0.0-alpha.11

1 year ago

1.0.0-alpha.10

1 year ago

1.0.0-alpha.9

1 year ago

1.0.0-alpha.8

1 year ago

1.0.0-alpha.7

1 year ago

1.0.0-alpha.6

1 year ago

1.0.0-alpha.5

1 year ago

1.0.0-alpha.4

1 year ago

1.0.0-alpha.3

1 year ago

1.0.0-alpha.2

1 year ago

1.0.0-alpha.1

1 year ago