Skip to main content
FeatureDescription
AI AgentsIntelligent automated conversations with real-time streaming
AI ModerationAutomatic content moderation with pending/approved/disapproved flow
AI User CopilotSmart Replies, Conversation Starter, Conversation Summary (Dashboard-enabled)
// Listen for real-time AI Agent events (streaming)
CometChat.addAIAssistantListener("LISTENER_ID", self)

// Listen for persisted agentic messages
// Conform to CometChatMessageDelegate
func onAIAssistantMessageReceived(_ msg: AIAssistantMessage) { }
func onAIToolResultMessageReceived(_ msg: AIToolResultMessage) { }
func onAIToolArgumentsMessageReceived(_ msg: AIToolArgumentMessage) { }

// 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.

Sending a Message to an AI Agent

Send a text message to an agent’s UID like any other user:
let agentUID = "ai-agent-uid"
let textMessage = TextMessage(
    receiverUid: agentUID,
    text: "What's the weather today?",
    receiverType: .user
)

CometChat.sendTextMessage(message: textMessage) { sentMessage in
    print("Message sent to agent")
} onError: { error in
    print("Error: \(error?.errorDescription)")
}

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 AIAssistantEventsDelegate
  2. After the run completes, persisted Agentic Messages arrive via CometChatMessageDelegate

Real-time Events

Events are received via onAIAssistantEventReceived as AIAssistantBaseEvent objects, 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. Text Message events are always emitted and carry the assistant’s reply incrementally.
import CometChatSDK

class AIAssistantEventHandler: AIAssistantEventsDelegate {
    
    private let sdkStreamListenerId = "unique_listener_id"
    
    func addListener() {
        CometChat.addAIAssistantListener(sdkStreamListenerId, self)
    }
    
    func removeListener() {
        CometChat.removeAIAssistantListener(sdkStreamListenerId)
    }
    
    func onAIAssistantEventReceived(_ event: AIAssistantBaseEvent) {
        print("Received AI Event: \(event.type) for Run ID: \(event.id)")
    }
}

Agentic Messages

After the run completes, these messages arrive via CometChatMessageDelegate:
Message TypeDescription
AIAssistantMessageThe full assistant reply
AIToolResultMessageThe final output of a tool call
AIToolArgumentMessageThe arguments passed to a tool
import CometChatSDK

let listenerId = "unique_listener_id"

class MessageHandler: CometChatMessageDelegate {
    
    // CometChat.addMessageListener(listenerId, self)
    
    func onAIAssistantMessageReceived(_ aiAssistantMessage: AIAssistantMessage) {
        print("AI Assistant Message Received: \(aiAssistantMessage)")
    }
    
    func onAIToolResultMessageReceived(_ aiToolResultMessage: AIToolResultMessage) {
        print("AI Tool Result Message Received: \(aiToolResultMessage)")
    }
    
    func onAIToolArgumentsMessageReceived(_ aiToolArgumentMessage: AIToolArgumentMessage) {
        print("AI Tool Arguments Message Received: \(aiToolArgumentMessage)")
    }
}
Always remove listeners when they’re no longer needed (e.g., on view dismissal). 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