Skip to main content
// Listen for real-time AI Agent events (streaming)
CometChat.addAIAssistantListener("LISTENER_ID", {
  onAIAssistantEventReceived: (event) => console.log("Event:", event)
});

// Listen for persisted agentic messages
CometChat.addMessageListener("LISTENER_ID", {
  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");
Event flow: Run Start → Tool Call(s) → Text Message Stream → Run Finished

AI Agents Overview

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 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.
const listnerId = "unique_listener_id";

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

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

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
const listnerId = "unique_listener_id";

// Adding the MessageListener
CometChat.addMessageListener(listnerId, {
    onAIAssistantMessageReceived: (message) => {
        console.log("AI Assistant message received successfully", message);
    },
    onAIToolResultReceived: (message) => {
        console.log("AI Tool result message received successfully", message);
    },
    onAIToolArgumentsReceived: (message) => {
        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