diff --git a/README.md b/README.md index 5b688af..2de3a67 100644 --- a/README.md +++ b/README.md @@ -355,18 +355,25 @@ A `TockTheme` can be used as a value of a `ThemeProvider` of [`emotion-theming`] The main source of configuration for the chatbot interface. Objects implementing this interface can be passed to `renderChat` or to `TockContext`. -| Property name | Type | Description | -|----------------|-------------------------|-----------------------------------------------------------------------------------------------------------------------| -| `endpoint` | `string` | The URL of the web connector endpoint | -| `userId` | `(string\|null)?` | Unique user ID. `null` means no clientside ID. `undefined` (default) generates and stores a UUID in the localStorage. | -| `locale` | `string?` | Optional user language, as an *RFC 5646* code | -| `localStorage` | `LocalStorageSettings?` | Configuration for use of localStorage by the library | -| `network` | `NetworkSettings?` | If `true`, disables any SSE connection attempt | -| `renderers` | `RendererSettings?` | Configuration for custom image and text renderers | +| Property name | Type | Description | +|------------------|-------------------------|-----------------------------------------------------------------------------------------------------------------------| +| `endpoint` | `string` | The URL of the web connector endpoint | +| `userId` | `(string\|null)?` | Unique user ID. `null` means no clientside ID. `undefined` (default) generates and stores a UUID in the localStorage. | +| `locale` | `string?` | Optional user language, as an *RFC 5646* code | +| `restartKeyword` | `string?` | Keyword sent to the bot when the user clears the messages, to restart the conversation on the server side (see below) | +| `localStorage` | `LocalStorageSettings?` | Configuration for use of localStorage by the library | +| `network` | `NetworkSettings?` | If `true`, disables any SSE connection attempt | +| `renderers` | `RendererSettings?` | Configuration for custom image and text renderers | > Note: disabling clientside ID is more secure, but requires setting a different [Web Security Mode](https://github.com/theopenconversationkit/tock/tree/master/bot/connector-web#web-security-modes) > in the matching bot configuration. +By default, the clear messages button only erases the messages on the client side: the bot keeps the conversation context +(current story, entity values, RAG history...). Set `restartKeyword` to `_restart_` (the default value of the +`tock_bot_restart_keyword` property, see [tock#2164](https://github.com/theopenconversationkit/tock/pull/2164)) to also restart the conversation on the server side. +The keyword is not displayed, the answer of the bot is. The user ID is kept. +The same behaviour is available programmatically through the `restartConversation` method returned by `useTock`. + #### `LocalStorageSettings` | Property name | Type | Description | diff --git a/src/components/Chat/Chat.tsx b/src/components/Chat/Chat.tsx index 923ed63..07216ae 100644 --- a/src/components/Chat/Chat.tsx +++ b/src/components/Chat/Chat.tsx @@ -54,6 +54,7 @@ const Chat: (props: ChatProps) => JSX.Element = ({ sseInitPromise, sseInitializing, clearMessages, + restartConversation, error, }: UseTock = useTock( endPoint, @@ -103,7 +104,7 @@ const Chat: (props: ChatProps) => JSX.Element = ({ disabled={sseInitializing} onSubmit={sendMessage} accessibility={accessibility} - clearMessages={clearMessages} + clearMessages={restartConversation} /> ); diff --git a/src/settings/TockSettings.tsx b/src/settings/TockSettings.tsx index c33877e..17dce5d 100644 --- a/src/settings/TockSettings.tsx +++ b/src/settings/TockSettings.tsx @@ -19,6 +19,11 @@ export default interface TockSettings { endpoint?: string; // will be required in a future release userId?: string | null; locale?: string; + /** + * Keyword sent to the bot when the user clears the messages, to restart the conversation on the server side. + * The default Tock keyword is `_restart_`. Undefined (default) only clears the messages on the client side. + */ + restartKeyword?: string; localStorage: LocalStorageSettings; network: NetworkSettings; renderers: RendererSettings; diff --git a/src/useTock.ts b/src/useTock.ts index 59546d7..04e23df 100644 --- a/src/useTock.ts +++ b/src/useTock.ts @@ -49,6 +49,11 @@ export interface UseTock { ) => void; sendMessage: (message: string) => Promise; clearMessages: () => void; + /** + * Clears the messages, then sends the {@link TockSettings.restartKeyword} (if any) to the bot, + * so that the conversation also restarts on the server side. + */ + restartConversation: () => Promise; addCard: ( title: string, imageUrl?: string, @@ -142,7 +147,12 @@ export const useTock0: ( localStorageHistory?: TockLocalStorage, ) => UseTockInternal = ( tockEndPoint: string, - { locale, localStorage: localStorageSettings, network: networkSettings }, + { + locale, + restartKeyword, + localStorage: localStorageSettings, + network: networkSettings, + }, extraHeadersProvider?: () => Promise>, disableSseArg?: boolean, localStorageHistoryArg?: TockLocalStorage, @@ -550,6 +560,15 @@ export const useTock0: ( return Promise.resolve(); }; + const restartConversation: () => Promise = () => { + clearMessages(); + setQuickReplies([]); + // the keyword is not displayed, only the bot answer is + return restartKeyword + ? sendMessage(restartKeyword, undefined, false) + : Promise.resolve(); + }; + // Sends an initial message to the backend, to trigger a welcome message const sendOpeningMessage: (msg: string) => Promise = (msg) => sendMessage(msg, undefined, false); @@ -729,6 +748,7 @@ export const useTock0: ( loading, error, clearMessages, + restartConversation, addCard, addCarousel, addMessage,