JavaScript/jQuery Chat - Implement an AI Chat Clear Button
AI agent chats often include a button that allows users to start a new conversation. This tutorial shows how to add and configure a Clear Chat button in your app. You can find the complete example code for this tutorial in the following GitHub repository:
Configure a Chat Reset Function
To clear messages in DevExtreme Chat, follow these steps:
- Get the component DataSource instance and call store.push() to clear messages in the UI.
- Empty the dxChat's data source array.
- If you use a separate array to store AI messages, empty this array.
- function clearChat(chatInstance) {
- const removals = chatInstance.getDataSource().items().map((item) => ({ type: 'remove', key: item.id }));
- chatInstance.getDataSource().store().push(removals);
- chatMessages.length = 0;
- aiMessages.length = 0;
- // Clear typingUsers indicators and alerts if any are displayed
- chatInstance.option({ alerts: [], typingUsers: [] });
- }
This tutorial uses the same approach as the following technical demo to store messages:
AI and Chatbot Integration Demo
Call clearChat() in the onClick handler of a DevExtreme Button to clear messages. You can initialize this button in the disabled state and enable the button in Chat's onMessageEntered handler:
- function handleClearChatButtonClick(e) {
- e.component.option('disabled', true);
- clearChat(chatInstance);
- }
- function handleMessageEntered() {
- clearChatButtonInstance.option('disabled', false);
- }
If a user clicks the Clear Chat button while an AI request is in progress, you must abort the request to ensure the button works as expected. This tutorial passes an AbortSignal from an AbortController to AzureOpenAI and calls abort() in the button's onClick handler:
- let controller = new AbortController();
- async function getAIResponse() {
- const params = {
- messages,
- model,
- // ...
- };
- const signalObj = {
- signal: controller.signal,
- };
- const response = await chatService.chat.completions.create(params, signalObj);
- return response.choices[0]?.message?.content;
- }
- function handleClearChatButtonClick() {
- // ...
- controller.abort();
- }
Assign a new AbortController to the controller variable in onMessageEntered to allow subsequent requests:
- function handleMessageEntered() {
- // ...
- controller = new AbortController();
- }
Configure the UI
Your Clear Chat button configuration depends on how you use the Chat component in your app. This tutorial includes steps for three usage scenarios.
Standalone
This section shows how to configure a Clear Chat button with a standalone Chat component.
Define DevExtreme Chat and Toolbar components in a container, and configure the Clear Chat button in the Toolbar as follows:
To visually combine the Chat and Toolbar components, define styles as follows:
- Container element: Define the background color and border styles to match DevExtreme component styling.
- Chat element: Disable the built-in border.
- Toolbar element: Add a bottom border to match DevExtreme component styling. Omit this style to match the built-in header appearance of the DevExpress Blazor AI Chat control.
- .demo-container {
- display: flex;
- justify-content: center;
- align-items: center;
- }
- .ai-chat-container {
- display: flex;
- justify-content: center;
- align-items: center;
- flex-direction: column;
- height: 100%;
- background-color: var(--dx-component-color-bg);
- width: 50%;
- min-width: 22.45em;
- max-width: 900px;
- border-radius: 12px;
- border: var(--dx-border-width) solid var(--dx-color-border);
- }
- .dx-chat {
- border: none;
- }
- #ai-chat-toolbar.dx-toolbar {
- border-bottom: var(--dx-border-width) solid var(--dx-color-border);
- padding: 0 20px;
- }
Within a Drawer
This section shows how to configure a Clear Chat button in a DevExtreme Drawer.
Define a Drawer component and configure DevExtreme Toolbar and Chat in the Drawer's template as follows:
To visually combine the Chat and Toolbar components within the Drawer, define styles as follows:
- Drawer container element: Define the background color and border styles following DevExtreme component styling.
- Chat element: Disable the built-in border.
- Toolbar element: Add a bottom border following DevExtreme component styling.
- .demo-container, .dx-drawer-content {
- display: flex;
- justify-content: center;
- align-items: center;
- flex-direction: column;
- }
- .drawer-ai-chat-container {
- display: flex;
- justify-content: center;
- align-items: center;
- flex-direction: column;
- height: 100%;
- min-width: 400px;
- background-color: var(--dx-component-color-bg);
- border-radius: 12px;
- border: var(--dx-border-width) solid var(--dx-color-border);
- }
- .drawer-ai-chat-container #ai-chat-drawer-toolbar.dx-toolbar {
- padding: 0 20px;
- border-bottom: var(--dx-border-width) solid var(--dx-color-border);
- }
- .drawer-ai-chat-container .dx-chat {
- border: none;
- }
- #toggle-drawer-button {
- display: inline-flex;
- }
Within a Popup
This section shows how to configure a Clear Chat button in the built-in toolbar of DevExtreme Popup.
Configure a DevExtreme Popup and define a Chat component within the Popup's contentTemplate as follows:
To visually integrate the Chat component in the Popup, define styles as follows:
- Popup content element: Set padding to 0.
- Chat element: Disable the built-in border.
- .demo-container {
- display: flex;
- justify-content: center;
- align-items: center;
- flex-direction: column;
- height: calc(100% - 24px);
- padding: 12px;
- }
- #open-popup-button {
- display: inline-flex;
- }
- #ai-chat-popup-wrapper .dx-popup-content {
- padding: 0;
- }
- #dx-ai-chat {
- border: none;
- }