Overview
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.
AIAssistantChatDemo.tsx
App.tsx
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} />
</>
);
}
A CometChat.User
(the AI agent) is required to start the assistant chat.
Actions
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).
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}
/>
);
}
2. onCloseButtonClicked
Called when the header close button is clicked (visible when showCloseButton
is true).
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}
/>
);
}
3. onSendButtonClick
Called when the composer send button is clicked.
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}
/>
);
}
4. onError
Listen to errors from the underlying header, list, or composer.
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}
/>
);
}
Customization
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.
Style
You can set the css of the Assistant Chat Component to customize the styling.
TypeScript
JavaScript
css
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 (
<div className="cometchat-assistant-chat__wrapper">
<CometChatAIAssistantChat user={agent} />
</div>
);
}
Functionality
These props tailor the experience. Most message options (copy/edit/delete/react, receipts, date separators, etc.) are disabled by default for assistant chats.
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}
/>
)
}
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). | emptyChatGreetingView={<h3/>} |
emptyChatIntroMessageView | Custom empty subtitle (default uses metadata.introductoryMessage). | emptyChatIntroMessageView={<p/>} |
emptyChatImageView | Custom empty image. | emptyChatImageView={<img/>} |
aiAssistantTools | Provide tool/function handlers for the assistant. | aiAssistantTools={tools} |
Advanced
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
.
Pass an instance of CometChatAIAssistantTools
to enable tool/function calls during assistant replies.
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} />
);
Empty Chat Image View
Provide a custom image for the empty state using emptyChatImageView
.
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} />}
/>
);
}
Empty Chat Greeting View
Override the empty state greeting message view using the emptyChatGreetingView
prop.
TypeScript
JavaScript
css
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}
emptyChatGreetingView={
<div className="cometchat-ai-assistant-chat__empty-chat-greeting">
<span className="plan-name">Free Plan</span> .
<span className="upgrade-button">Upgrade</span>
</div>
}
/>
);
}
Empty Chat Intro Message View
You can override the empty chat intro message view using the emptyChatIntroMessageView
prop.
TypeScript
JavaScript
css
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}
emptyChatIntroMessageView={
<div className="cometchat-ai-assistant-chat__empty-chat-intro">
Hey, nice to see you What’s new?
</div>
}
/>
);
}
Templates
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.
import React from "react";
import { CometChat } from "@cometchat/chat-sdk-javascript";
import {
CometChatAIAssistantChat,
ChatConfigurator
} from "@cometchat/chat-uikit-react";
export function AIAssistantChatDemo() {
const [chatUser, setChatUser] = React.useState<CometChat.User>();
React.useEffect(() => {
CometChat.getUser("uid").then((user) => {
setChatUser(user);
});
}, []);
const getTemplates = () => {
let templates = ChatConfigurator.getDataSource().getAllMessageTemplates();
templates.map((data) => {
data.footerView = (message) => {}; //custom view here
});
return templates;
};
return chatUser ? (
<div>
<CometChatAIAssistantChat user={chatUser} templates={getTemplates()} />
</div>
) : null;
}