Build an AI agent chat experience with streaming responses, quick suggestions, chat history, and custom tools using the CometChatAIAssistantChat component.
Build an AI assistant chat surface with streaming responses and suggestions.
What you’re changing: Overview.
Where to change it: Component props or CSS as shown below.
Default behavior: UI Kit defaults.
Override: Use the examples in this section.
Verify: The UI reflects the change shown below.CometChatAIAssistantChat is a composite component that assembles the message header, message list, and message composer to provide an AI agent chat experience. It supports streaming responses, quick starter suggestions, “New Chat” to reset context, and a chat history sidebar.
Before using this component: Ensure CometChatUIKit.init(UIKitSettings) has completed and the user is logged in via CometChatUIKit.login("UID"). See React.js Integration.
TypeScript
JavaScript
Report incorrect code
Copy
Ask AI
import React from "react";import { CometChat } from "@cometchat/chat-sdk-javascript";import { CometChatAIAssistantChat } from "@cometchat/chat-uikit-react";export function AIAssistantChatDemo() { const [agent, setAgent] = React.useState<CometChat.User>(); React.useEffect(() => { // Replace with your assistant UID CometChat.getUser("assistant_uid").then((u) => setAgent(u)); }, []); if (!agent) return null; return( <> <CometChatAIAssistantChat user={agent} /> </> ); }
What this does: Shows the code for this step.
Report incorrect code
Copy
Ask AI
import React, { useState, useEffect } from "react";import { CometChat } from "@cometchat/chat-sdk-javascript";import { CometChatAIAssistantChat } from "@cometchat/chat-uikit-react";export function AIAssistantChatDemo() { const [agent, setAgent] = useState(null); useEffect(() => { // Replace with your assistant UID CometChat.getUser("assistant_uid").then((u) => setAgent(u)); }, []); if (!agent) return null; return( <> <CometChatAIAssistantChat user={agent} /> </> ); }
What this does: Shows the code for this step.
A CometChat.User (the AI agent) is required to start the assistant chat.
What you’re changing: Actions.
Where to change it: Component props or CSS as shown below.
Default behavior: UI Kit defaults.
Override: Use the examples in this section.
Verify: The UI reflects the change shown below.Actions control how a component behaves. You can hook into the following callbacks:
1. onBackButtonClicked
Called when the header back button is clicked (visible when showBackButton is true).
TypeScript
JavaScript
Report incorrect code
Copy
Ask AI
import React from "react";import { CometChat } from "@cometchat/chat-sdk-javascript";import { CometChatAIAssistantChat } from "@cometchat/chat-uikit-react";export function AIAssistantChatDemo() { const [agent, setAgent] = React.useState<CometChat.User>(); React.useEffect(() => { CometChat.getUser("assistant_uid").then((u) => setAgent(u)); }, []); const handleBack = () => { // your custom action }; if (!agent) return null; return ( <CometChatAIAssistantChat user={agent} showBackButton={true} onBackButtonClicked={handleBack} /> ); }
What this does: Shows the code for this step.
Report incorrect code
Copy
Ask AI
import React from "react";import { CometChat } from "@cometchat/chat-sdk-javascript";import { CometChatAIAssistantChat } from "@cometchat/chat-uikit-react";export function AIAssistantChatDemo() { const [agent, setAgent] = React.useState(null); React.useEffect(() => { CometChat.getUser("assistant_uid").then((u) => setAgent(u)); }, []); const handleBack = () => { // your custom action }; if (!agent) return null; return ( <CometChatAIAssistantChat user={agent} showBackButton={true} onBackButtonClicked={handleBack} /> );}
What this does: Shows the code for this step.
2. onCloseButtonClicked
Called when the header close button is clicked (visible when showCloseButton is true).
TypeScript
JavaScript
Report incorrect code
Copy
Ask AI
import React from "react";import { CometChat } from "@cometchat/chat-sdk-javascript";import { CometChatAIAssistantChat } from "@cometchat/chat-uikit-react";export function AIAssistantChatDemo() { const [agent, setAgent] = React.useState<CometChat.User>(); React.useEffect(() => { CometChat.getUser("assistant_uid").then((u) => setAgent(u)); }, []); const handleClose = () => { // your custom action }; if (!agent) return null; return ( <CometChatAIAssistantChat user={agent} showCloseButton={true} onCloseButtonClicked={handleClose} /> );}
What this does: Shows the code for this step.
Report incorrect code
Copy
Ask AI
import React from "react";import { CometChat } from "@cometchat/chat-sdk-javascript";import { CometChatAIAssistantChat } from "@cometchat/chat-uikit-react";export function AIAssistantChatDemo() { const [agent, setAgent] = React.useState(null); React.useEffect(() => { CometChat.getUser("assistant_uid").then((u) => setAgent(u)); }, []); const handleClose = () => { // your custom action }; if (!agent) return null; return ( <CometChatAIAssistantChat user={agent} showCloseButton={true} onCloseButtonClicked={handleClose} /> );}
What this does: Shows the code for this step.
3. onSendButtonClick
Called when the composer send button is clicked.
TypeScript
JavaScript
Report incorrect code
Copy
Ask AI
import React from "react";import { CometChat } from "@cometchat/chat-sdk-javascript";import { CometChatAIAssistantChat } from "@cometchat/chat-uikit-react";export function AIAssistantChatDemo() { const [agent, setAgent] = React.useState<CometChat.User>(); React.useEffect(() => { CometChat.getUser("assistant_uid").then((u) => setAgent(u)); }, []); const handleSendButton = () => { // your custom action }; if (!agent) return null; return ( <CometChatAIAssistantChat user={agent} onSendButtonClick={handleSendButton} /> );}
What this does: Shows the code for this step.
Report incorrect code
Copy
Ask AI
import React from "react";import { CometChat } from "@cometchat/chat-sdk-javascript";import { CometChatAIAssistantChat } from "@cometchat/chat-uikit-react";export function AIAssistantChatDemo() { const [agent, setAgent] = React.useState(null); React.useEffect(() => { CometChat.getUser("assistant_uid").then((u) => setAgent(u)); }, []); const handleSendButton = () => { // your custom action }; if (!agent) return null; return ( <CometChatAIAssistantChat user={agent} onSendButtonClick={handleSendButton} /> );}
What this does: Shows the code for this step.
4. onError
Listen to errors from the underlying header, list, or composer.
TypeScript
JavaScript
Report incorrect code
Copy
Ask AI
import React from "react";import { CometChat } from "@cometchat/chat-sdk-javascript";import { CometChatAIAssistantChat } from "@cometchat/chat-uikit-react";export function AIAssistantChatDemo() { const [agent, setAgent] = React.useState<CometChat.User>(); React.useEffect(() => { CometChat.getUser("assistant_uid").then((u) => setAgent(u)); }, []); const handleError = (error: CometChat.CometChatException) => { // your error handling }; if (!agent) return null; return ( <CometChatAIAssistantChat user={agent} onError={handleError} /> );}
What this does: Shows the code for this step.
Report incorrect code
Copy
Ask AI
import React from "react";import { CometChat } from "@cometchat/chat-sdk-javascript";import { CometChatAIAssistantChat } from "@cometchat/chat-uikit-react";export function AIAssistantChatDemo() { const [agent, setAgent] = React.useState(null); React.useEffect(() => { CometChat.getUser("assistant_uid").then((u) => setAgent(u)); }, []); const handleError = (error: CometChat.CometChatException) => { // your error handling }; if (!agent) return null; return ( <CometChatAIAssistantChat user={agent} onError={handleError} /> )}
What you’re changing: Customization.
Where to change it: Component props or CSS as shown below.
Default behavior: UI Kit defaults.
Override: Use the examples in this section.
Verify: The UI reflects the change shown below.To fit your app’s design requirements, you can customize the appearance of the Assistant Chat component. We provide exposed properties that allow you to modify the experience and behavior according to your specific needs.
These props tailor the experience. Most message options (copy/edit/delete/react, receipts, date separators, etc.) are disabled by default for assistant chats.
TypeScript
JavaScript
Report incorrect code
Copy
Ask AI
import React from "react";import { CometChat } from "@cometchat/chat-sdk-javascript";import { CometChatAIAssistantChat } from "@cometchat/chat-uikit-react";export function AIAssistantChatDemo = () => { const [agent, setAgent] = React.useState<CometChat.User>(); React.useEffect(() => { CometChat.getUser("assistant_uid").then((u) => setAgent(u)); }, []); if (!agent) return null; // Example: Set streaming speed to 30 characters per second // and show close button // You can also customize suggestions, empty state, etc. return ( <CometChatAIAssistantChat user={agent} showCloseButton={true} setStreamingSpeed={30} /> )}
What this does: Shows the code for this step.
Report incorrect code
Copy
Ask AI
import React from "react";import { CometChat } from "@cometchat/chat-sdk-javascript";import { CometChatAIAssistantChat } from "@cometchat/chat-uikit-react";export function AIAssistantChatDemo = () => { const [agent, setAgent] = React.useState(null); React.useEffect(() => { CometChat.getUser("assistant_uid").then((u) => setAgent(u)); }, []); if (!agent) return null; // Example: Set streaming speed to 30 characters per second // and show close button // You can also customize suggestions, empty state, etc. return ( <CometChatAIAssistantChat user={agent} showCloseButton={true} setStreamingSpeed={30} /> );}
What this does: Shows the code for this step.
Property
Description
Example
user
Required CometChat.User representing the AI agent.
user={agent}
streamingSpeed
Characters-per-second speed for streaming replies. Default 30.
streamingSpeed={50}
suggestedMessages
Array of quick prompts for the empty state.
suggestedMessages=["Help", "Summarize"]
hideSuggestedMessages
Hide the suggestions section.
hideSuggestedMessages={true}
hideNewChat
Hide the New Chat button in header.
hideNewChat={true}
hideChatHistory
Hide the History button/sidebar.
hideChatHistory={true}
showBackButton
Show back button in header.
showBackButton
showCloseButton
Show close button in header.
showCloseButton
onBackButtonClicked
Back button handler.
onBackButtonClicked={() => {}}
onCloseButtonClicked
Close button handler.
onCloseButtonClicked={() => {}}
onSendButtonClick
Send button handler.
onSendButtonClick={() => {}}
onError
Error handler.
onError={handleError}
emptyView
Custom empty state for the list.
emptyView={<Empty/>}
loadingView
Custom loading view.
loadingView={<Loading/>}
errorView
Custom error view.
errorView={<Error/>}
emptyChatGreetingView
Custom empty title (default uses metadata.greetingMessage or user name).
Customize header sections using the following props: headerItemView, headerTitleView, headerSubtitleView, headerLeadingView, headerTrailingView, and headerAuxiliaryButtonView. These customizations are done in the similar way as the Message Header component.
The header’s “New Chat” and “History” buttons are built-in. You can override them by providing a custom headerAuxiliaryButtonView.
Assistant Tools
Pass an instance of CometChatAIAssistantTools to enable tool/function calls during assistant replies.
TypeScript
JavaScript
Report incorrect code
Copy
Ask AI
import React from "react";import { CometChat } from "@cometchat/chat-sdk-javascript";import { CometChatAIAssistantChat } from "@cometchat/chat-uikit-react";import { CometChatAIAssistantTools } from "@cometchat/chat-uikit-react";export function AIAssistantChatDemo = () => { const [agent, setAgent] = React.useState<CometChat.User>(); React.useEffect(() => { CometChat.getUser("assistant_uid").then((u) => setAgent(u)); }, []); const tools = new CometChatAIAssistantTools({ getCurrentWeather: ({ location }: { location: string }) => { // call your backend or a public API console.log("Fetching weather for", location); }, createTicket: ({ title }: { title: string }) => { // open your internal ticketing flow console.log("Create ticket:", title); }, }); if (!agent) return null; return ( <CometChatAIAssistantChat user={agent} aiAssistantTools={tools} /> );
What this does: Shows the code for this step.
Report incorrect code
Copy
Ask AI
import React from "react";import { CometChat } from "@cometchat/chat-sdk-javascript";import { CometChatAIAssistantChat } from "@cometchat/chat-uikit-react";import { CometChatAIAssistantTools } from "@cometchat/chat-uikit-react";export function AIAssistantChatDemo = () => { const [agent, setAgent] = React.useState(null); React.useEffect(() => { CometChat.getUser("assistant_uid").then((u) => setAgent(u)); }, []); const tools = new CometChatAIAssistantTools({ getCurrentWeather: ({ location }: { location: string }) => { // call your backend or a public API console.log("Fetching weather for", location); }, createTicket: ({ title }: { title: string }) => { // open your internal ticketing flow console.log("Create ticket:", title); }, }); if (!agent) return null; return ( <CometChatAIAssistantChat user={agent} aiAssistantTools={tools} /> );}
What this does: Shows the code for this step.
Empty Chat Image View
Provide a custom image for the empty state using emptyChatImageView.
TypeScript
JavaScript
Report incorrect code
Copy
Ask AI
import React from "react";import { CometChat } from "@cometchat/chat-sdk-javascript";import { CometChatAIAssistantChat } from "@cometchat/chat-uikit-react";export function AIAssistantChatDemo() { const [agent, setAgent] = React.useState<CometChat.User>(); React.useEffect(() => { CometChat.getUser("assistant_uid").then((u) => setAgent(u)); }, []); if (!agent) return null; return ( <CometChatAIAssistantChat user={agent} emptyChatImageView={<img src={"ICON_URL"} height={60} width={60} />} /> );}
What this does: Shows the code for this step.
Report incorrect code
Copy
Ask AI
import React from "react";import { CometChat } from "@cometchat/chat-sdk-javascript";import { CometChatAIAssistantChat } from "@cometchat/chat-uikit-react";export function AIAssistantChatDemo() { const [agent, setAgent] = React.useState(null); React.useEffect(() => { CometChat.getUser("assistant_uid").then((u) => setAgent(u)); }, []); if (!agent) return null; return ( <CometChatAIAssistantChat user={agent} emptyChatImageView={<img src={"ICON_URL"} height={60} width={60} />} /> )}
What this does: Shows the code for this step.
Empty Chat Greeting View
Override the empty state greeting message view using the emptyChatGreetingView prop.
CometChatMessageTemplate is a pre-defined structure for creating message views that can be used as a starting point or blueprint for creating message views often known as message bubbles. For more information, you can refer to CometChatMessageTemplate.You can set message Templates to AIAssistantChat by using the following code snippet.
Do I need a CometChat user for the agent?
Yes. Pass the AI agent as a CometChat.User.Can I change suggestions dynamically?
Yes. Update suggestedMessages prop.