Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
3 changes: 2 additions & 1 deletion src/components/Chat/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const Chat: (props: ChatProps) => JSX.Element = ({
sseInitPromise,
sseInitializing,
clearMessages,
restartConversation,
error,
}: UseTock = useTock(
endPoint,
Expand Down Expand Up @@ -103,7 +104,7 @@ const Chat: (props: ChatProps) => JSX.Element = ({
disabled={sseInitializing}
onSubmit={sendMessage}
accessibility={accessibility}
clearMessages={clearMessages}
clearMessages={restartConversation}
/>
</Container>
);
Expand Down
5 changes: 5 additions & 0 deletions src/settings/TockSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
22 changes: 21 additions & 1 deletion src/useTock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ export interface UseTock {
) => void;
sendMessage: (message: string) => Promise<void>;
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<void>;
addCard: (
title: string,
imageUrl?: string,
Expand Down Expand Up @@ -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<Record<string, string>>,
disableSseArg?: boolean,
localStorageHistoryArg?: TockLocalStorage,
Expand Down Expand Up @@ -550,6 +560,15 @@ export const useTock0: (
return Promise.resolve();
};

const restartConversation: () => Promise<void> = () => {
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<void> = (msg) =>
sendMessage(msg, undefined, false);
Expand Down Expand Up @@ -729,6 +748,7 @@ export const useTock0: (
loading,
error,
clearMessages,
restartConversation,
addCard,
addCarousel,
addMessage,
Expand Down