> ## Documentation Index
> Fetch the complete documentation index at: https://www.cometchat.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# AI Agent Integration

> Build a chat with an AI agent in a CometChat iOS UI Kit v6 app: detect the agent, show the agent chat, start a new chat, and open past conversations.

> [accordion] AI Integration Quick Reference
>
> ```json theme={null}
> {
>   "products": ["CometChatSwiftUI", "CometChatUIKit"],
>   "components": ["CometChatMessageHeader", "CometChatMessageList", "CometChatMessageComposer"],
>   "agent detection": "a user whose role is \"@agentic\" (ignoring case)",
>   "header": "onNewChatClick / onChatHistoryClick (SwiftUI); onAiNewChatClicked / onAiChatHistoryClicked or the delegate's onNewChatClick(user:) / onChatHistoryClick(user:) (UIKit)",
>   "list": "loadLastAgentConversation; UIKit: set(suggestedMessages:), set(streamingSpeed:), set(emptyChatAIGreetingView:), hideSuggestedMessages",
>   "events": "agentChatThreadResolved(receiverId:parentMessageId:)",
>   "chat history": "CometChatAIAssistantChatHistory (/ui-kit/ios/v6/ai-assistant-chat-history)"
> }
> ```

An AI agent is a CometChat user your dashboard configures to answer with AI. Chatting with one
uses the same three components as any one-to-one chat; the Kit notices the agent and changes
how they behave.

## Overview

When the user is an agent — its `role` is `@agentic` — the components switch to agent mode on
their own:

| Component | In an agent chat |
| - | - |
| `CometChatMessageHeader` | "AI Assistant" under the name, **New Chat** and **Chat History** buttons, no call buttons or presence |
| `CometChatMessageList` | the agent's greeting, introduction and suggested messages in an empty chat; replies stream in — a thinking shimmer, then text, then markdown and cards; no long-press menu |
| `CometChatMessageComposer` | no attachment, voice, sticker or formatting buttons; an up-arrow send button that shows a stop icon while the reply streams |

The first message you send starts a conversation thread with the agent, and the rest of that
conversation goes into it. Agents work in groups too: there the agent is a member, and its
messages render like anyone else's.

## Prerequisites

* The Kit is set up and a user is logged in — see [Getting Started](https://www.cometchat.com/docs/ui-kit/ios/v6/getting-started-swiftui.md).
* An AI agent configured in the CometChat dashboard — see [AI Agents](https://www.cometchat.com/docs/sdk/ios/ai-agents.md).

## Integration Steps

### Step 1 — Detect the agent

```swift theme={null}
let agent = CometChatSDK.User(uid: "support-bot", name: "Nova")
agent.role = "@agentic"   // set by the dashboard; shown here only to make the check concrete
let isAgent = agent.role?.caseInsensitiveCompare("@agentic") == .orderedSame
print(isAgent)
```

Route an agent to your agent chat screen; it is an ordinary one-to-one chat otherwise, so the
same screen works for both.

### Step 2 — The agent chat screen

Handle the header's two buttons. **New Chat** starts a fresh conversation: build the list and
the composer again so they drop the current thread.

> [tab] SwiftUI
>
> ```swift AgentChatView.swift theme={null}
> import SwiftUI
> import CometChatSwiftUI
> import CometChatSDK
>
> struct AgentChatView: View {
>     let agent: CometChatSDK.User
>     /// Changing it gives the list and the composer new stores — a fresh conversation.
>     @State private var session = UUID()
>     @State private var showsHistory = false
>
>     var body: some View {
>         // … `header` and `composer` as bars over the list …
>         CometChatMessageList(user: agent)
>             .id(session)
>             .sheet(isPresented: $showsHistory) {
>                 NavigationStack {
>                     CometChatAIAssistantChatHistory(
>                         user: agent,
>                         onCloseClick: { showsHistory = false },
>                         onNewChatClick: { showsHistory = false; session = UUID() },
>                         onItemClick: { _ in showsHistory = false })   // then open it as in Step 4
>                 }
>             }
>     }
>
>     var header: some View {
>         CometChatMessageHeader(
>             user: agent,
>             onNewChatClick: { session = UUID() },
>             onChatHistoryClick: { showsHistory = true }
>         )
>     }
>
>     var composer: some View {
>         CometChatMessageComposer(user: agent).id(session)
>     }
> }
> ```

> [tab] UIKit
>
> ```swift AgentChatViewController.swift theme={null}
> import UIKit
> import CometChatUIKit
> import CometChatSDK
>
> final class AgentChatViewController: UIViewController {
>     let agent: CometChatSDK.User
>     let header = CometChatMessageHeader()
>     let messageList = CometChatMessageList()
>     let composer = CometChatMessageComposer()
>
>     init(agent: CometChatSDK.User) {
>         self.agent = agent
>         super.init(nibName: nil, bundle: nil)
>     }
>     required init?(coder: NSCoder) { fatalError() }
>
>     override func viewDidLoad() {
>         super.viewDidLoad()
>         // set(user:) with an agent turns the AI buttons on and the call buttons off.
>         header.set(user: agent)
>         messageList.set(user: agent)
>         composer.set(user: agent)
>
>         // A fresh conversation: replace this screen with a new one.
>         header.onAiNewChatClicked = { [weak self] agent in
>             guard let self, let navigation = self.navigationController else { return }
>             var stack = navigation.viewControllers
>             stack[stack.count - 1] = AgentChatViewController(agent: agent)
>             navigation.setViewControllers(stack, animated: false)
>         }
>         header.onAiChatHistoryClicked = { [weak self] agent in
>             let history = CometChatAIAssistantChatHistory()
>                 .set(user: agent)
>                 .set(onMessageClicked: { [weak self] _ in
>                     self?.dismiss(animated: true)   // then open it as in Step 4
>                 })
>             self?.present(UINavigationController(rootViewController: history), animated: true)
>         }
>         // … lay out the header, the list and the composer …
>     }
> }
> ```

Host the header and the composer over the list as [Conversation List + Message View](https://www.cometchat.com/docs/ui-kit/ios/v6/conversation-message-view.md#step-2-set-up-the-message-screen) shows.

### Step 3 — Reopen the last conversation (optional)

By default an agent chat opens on the greeting. To open the latest conversation with the agent
instead:

> [tab] SwiftUI
>
> ```swift theme={null}
> CometChatMessageList(user: guideUser, loadLastAgentConversation: true)
> ```

> [tab] UIKit
>
> ```swift theme={null}
> let list = CometChatMessageList()
> list.loadLastAgentConversation = true
> list.set(user: guideUser)
> ```

### Step 4 — Open a past conversation

Each conversation with an agent is a thread. To show one — from the chat history's
`onItemClick` (SwiftUI) or `set(onMessageClicked:)` (UIKit) — pass its first message as the
parent, as for any thread:

> [tab] SwiftUI
>
> ```swift theme={null}
> CometChatMessageList(user: guideUser, parentMessage: parentMessage)   // parentMessage: the conversation's first message
> CometChatMessageComposer(user: guideUser, parentMessageId: parentMessage.id)
> ```

> [tab] UIKit
>
> ```swift theme={null}
> let list = CometChatMessageList()
> list.set(user: guideUser, parentMessage: threadParent)   // threadParent: the conversation's first message
> let composer = CometChatMessageComposer()
> composer.set(user: guideUser).set(parentMessageId: threadParent.id)
> ```

> [!NOTE]
>
> Chat History opens [`CometChatAIAssistantChatHistory`](https://www.cometchat.com/docs/ui-kit/ios/v6/ai-assistant-chat-history),
> which lists the past conversations and hands you the first message of the one the user picks.

## Implementation Flow Summary

| Step | Action |
| - | - |
| 1 | The user picks the agent from a list or a conversation |
| 2 | You detect `@agentic` and open the agent chat |
| 3 | The header, list and composer switch to agent mode |
| 4 | New Chat rebuilds the list and the composer; Chat History opens `CometChatAIAssistantChatHistory` |
| 5 | A past conversation opens as a thread rooted at its first message |

## Customization Options

> [tab] SwiftUI
>
> * `hideAiAssistantSuggestedMessages: true` hides the suggested messages in an empty chat.
> * `loadLastAgentConversation: true` opens the latest conversation.
> * The composer's `sendButtonView` receives `isAIGenerating` — disable your own button while it
>   is `true`; the Kit does not block a custom button.

> [tab] UIKit
>
> ```swift theme={null}
> let list = CometChatMessageList()
> list.set(suggestedMessages: ["What can you do?", "Summarise my day"])
> list.set(streamingSpeed: 20)                     // the reply's typing pace
> list.set(emptyChatAIGreetingView: UILabel())     // your own empty-chat greeting
> list.hideSuggestedMessages = false
> ```

## Feature Matrix

| Feature | SwiftUI | UIKit |
| - | - | - |
| Agent chat | `CometChatMessageHeader/List/Composer(user: agent)` | `set(user: agent)` on each |
| New Chat button | `onNewChatClick:` | `onAiNewChatClicked` |
| Chat History button | `onChatHistoryClick:` | `onAiChatHistoryClicked` |
| Latest conversation | `loadLastAgentConversation: true` | `loadLastAgentConversation = true` |
| Suggested messages | the agent's; `hideAiAssistantSuggestedMessages:` | `set(suggestedMessages:)`, `hideSuggestedMessages` |
| Streaming speed | — | `set(streamingSpeed:)` |
| Empty-chat greeting | the agent's | `set(emptyChatAIGreetingView:)` |
| Chat history screen | `CometChatAIAssistantChatHistory(user:onItemClick:)` | `CometChatAIAssistantChatHistory().set(user:)` |

## Next Steps

> [card] [Message Composer](https://www.cometchat.com/docs/ui-kit/ios/v6/message-composer.md)
>
> The composer in agent chats

> [card] [Message List](https://www.cometchat.com/docs/ui-kit/ios/v6/message-list.md)
>
> Streaming replies and the greeting

> [card] [AI Features](https://www.cometchat.com/docs/ui-kit/ios/v6/ai-features.md)
>
> Conversation starters, smart replies and summaries

> [card] [All Guides](https://www.cometchat.com/docs/ui-kit/ios/v6/guide-overview.md)
>
> Browse the other guides


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.