Skip to main content
FeatureDescription
AI AgentsIntelligent automated conversations with real-time streaming
AI ModerationAutomatic content moderation with PENDINGAPPROVED / DISAPPROVED flow
AI User CopilotSmart Replies, Conversation Starter, Conversation Summary (Dashboard-enabled)
// Listen for real-time AI Agent events (streaming)
CometChat.addAIAssistantListener("LISTENER_ID", new CometChat.AIAssistantListener({
  onAIAssistantEventReceived: (event) => console.log("Event:", event)
}));

// Listen for persisted agentic messages
CometChat.addMessageListener("LISTENER_ID", new CometChat.MessageListener({
  onAIAssistantMessageReceived: (msg) => console.log("Assistant reply:", msg),
  onAIToolResultReceived: (msg) => console.log("Tool result:", msg),
  onAIToolArgumentsReceived: (msg) => console.log("Tool args:", msg)
}));

// Cleanup
CometChat.removeAIAssistantListener("LISTENER_ID");
CometChat.removeMessageListener("LISTENER_ID");
Prerequisites: CometChat.init() + CometChat.login() completed, AI features enabled in Dashboard Event flow: Run Start → Tool Call(s) → Text Message Stream → Run Finished
AI Agents enable intelligent, automated interactions within your application. They process user messages, trigger tools, and respond with contextually relevant information. For a broader introduction, see the AI Agents section.
Agents only respond to text messages.

Agent Run Lifecycle and Message Flow

When a user sends a text message to an Agent:
  1. The platform starts a run and streams real-time events via AIAssistantListener
  2. After the run completes, persisted Agentic Messages arrive via MessageListener

Real-time Events

Events are received via the onAIAssistantEventReceived method of the AIAssistantListener class as AIAssistantBaseEvent objects, in this general order: Events arrive via onAIAssistantEventReceived in this order:
OrderEventDescription
1Run StartA new run has begun
2Tool Call StartAgent decided to invoke a tool
3Tool Call ArgumentsArguments being passed to the tool
4Tool Call EndTool execution completed
5Tool Call ResultTool’s output is available
6Text Message StartAgent started composing a reply
7Text Message ContentStreaming content chunks (multiple)
8Text Message EndAgent reply is complete
9Run FinishedRun finalized; persisted messages follow
Run Start and Run Finished are always emitted. Tool Call events only appear when tools are invoked — there can be multiple tool call cycles in a single run. Text Message events are always emitted and carry the assistant’s reply incrementally.

Event Object Properties

Every event is an AIAssistantBaseEvent with these common properties:
GetterReturn TypeDescription
getType()stringEvent type (e.g., run_started, text_message_content)
getConversationId()stringThe conversation this event belongs to
getMessageId()stringThe message ID associated with the event
getParentMessageId()stringParent message ID (for threaded messages)
getRunId()stringThe run ID for this agent execution
getThreadId()stringThe thread ID for this agent execution
getTimestamp()numberTimestamp of the event
getData()objectFull event data payload
Some events carry additional data:
EventExtra GetterDescription
Text Message ContentgetDelta()The streaming text chunk for progressive rendering
Tool Call ArgumentsgetToolCallId(), getDelta()Tool call ID and argument chunk
Tool Call ResultgetToolCallId(), getContent(), getRole()Tool call ID, result content, and role
        const listnerId: string = "unique_listener_id";

        // Adding the AIAssistantListener
        CometChat.addAIAssistantListener(listnerId, {
            onAIAssistantEventReceived: (message: CometChat.AIAssistantBaseEvent) => {
                console.log("AIAssistant event received successfully", message);
            }
        });

        // Removing the AIAssistantListener
        CometChat.removeAIAssistantListener(listnerId);

Event descriptions

  • Run Start: A new run has begun for the user’s message.
  • Tool Call Start: The agent decided to invoke a tool.
  • Tool Call Arguments: Arguments being passed to the tool.
  • Tool Call End: Tool execution completed.
  • Tool Call Result: Tool’s output is available.
  • Text Message Start: The agent started composing a reply.
  • Text Message Content: Streaming content chunks for progressive rendering.
  • Text Message End: The agent reply is complete.
  • Run Finished: The run is finalized; persisted messages will follow.

Agentic Messages

After the run completes, these messages arrive via MessageListener:
Message TypeDescription
AIAssistantMessageThe full assistant reply
AIToolResultMessageThe final output of a tool call
AIToolArgumentMessageThe arguments passed to a tool
Each message type extends BaseMessage and has a typed data accessor:
Message TypeData GetterData Properties
AIAssistantMessagegetAssistantMessageData()getRunId(), getThreadId(), getText()
AIToolResultMessagegetToolResultMessageData()getRunId(), getThreadId(), getText(), getToolCallId()
AIToolArgumentMessagegetToolArgumentMessageData()getRunId(), getThreadId(), getToolCalls()
The getToolCalls() method on AIToolArgumentMessage returns an array of AIToolCall objects, each with:
GetterReturn TypeDescription
getId()stringUnique tool call ID
getType()stringTool call type
getFunction()AIToolCallFunctionFunction object with getName() and getArguments()
getDisplayName()stringDisplay name of the tool
getExecutionText()stringExecution description text
const listnerId: string = "unique_listener_id";

// Adding the MessageListener
CometChat.addMessageListener(listnerId, {
    onAIAssistantMessageReceived: (message: CometChat.AIAssistantMessage) => {
        console.log("AI Assistant message received successfully", message);
    },
    onAIToolResultReceived: (message: CometChat.AIToolResultMessage) => {
        console.log("AI Tool result message received successfully", message);
    },
    onAIToolArgumentsReceived: (message: CometChat.AIToolArgumentMessage) => {
        console.log("AI Tool argument message received successfully", message);
    },
});

// Removing the MessageListener
CometChat.removeMessageListener(listnerId);
Always remove listeners when they’re no longer needed (e.g., on component unmount or page navigation). Failing to remove listeners can cause memory leaks and duplicate event handling.

Next Steps

AI Chatbots

Set up AI-powered chatbots for automated conversations

AI Moderation

Automatically moderate messages with AI

AI User Copilot

AI-powered features like smart replies and conversation summaries

Send Messages

Send text messages that trigger AI Agent responses