Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
707 changes: 426 additions & 281 deletions sdk/ios/additional-message-filtering.mdx

Large diffs are not rendered by default.

178 changes: 177 additions & 1 deletion sdk/ios/ai-agents.mdx
Original file line number Diff line number Diff line change
@@ -1,14 +1,78 @@
---
title: "AI Agents"
description: "Guide to integrating AI Agents for intelligent automated interactions using the CometChat iOS SDK."
---

{/* TL;DR for Agents and Quick Reference */}
<Info>
**Quick Reference for AI Agents & Developers**

- **Send to agent:** Send text message to agent's UID like any other user
- **Agent responses:** Received via message listener as regular messages
- **Run events:** Real-time events during agent processing via `onAgentRunStarted`, `onAgentRunCompleted`
- **Note:** Agents currently only respond to text messages
- **Related:** [AI Agents Overview](/ai-agents) · [Send Message](/sdk/ios/send-message) · [Receive Message](/sdk/ios/receive-message)
</Info>

# AI Agents Overview

AI Agents enable intelligent, automated interactions within your application. They can process user messages, trigger tools, and respond with contextually relevant information. For a broader introduction, see the [AI Agents section](/ai-agents).

> **Note:**
> Currently, an Agent only responds to **Text Messages**.

## Sending a Message to an AI Agent

Send a text message to an agent's UID like any other user:

<Tabs>
<Tab title="Swift">
```swift
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")
// Agent will process and respond via listeners
} onError: { error in
print("Error: \(error?.errorDescription)")
}
```
</Tab>
</Tabs>

<Accordion title="Sample Payload - Send Message to AI Agent">

**Request Parameters:**

| Parameter | Type | Description |
| --------- | ---- | ----------- |
| agentUID | String | Unique identifier of the AI agent. Example: `"ai-agent-uid"` |
| text | String | Message content to send. Example: `"Hello AI Agent, how can you help me?"` |
| receiverType | ReceiverType | Type of receiver. Example: `.user` |

**Success Response (TextMessage Object):**

| Parameter | Type | Description |
| --------- | ---- | ----------- |
| id | Int | Unique message identifier. Example: `38255` |
| muid | String? | Message unique ID. Example: `nil` |
| senderUid | String? | UID of the sender. Example: `"cometchat-uid-2"` |
| receiverUid | String? | UID of the agent. Example: `"ai-agent-uid"` |

**Next Steps:**

| Event Source | Description |
| ------------ | ----------- |
| AIAssistantEventsDelegate | Real-time streaming events via `onAIAssistantEventReceived` |
| CometChatMessageDelegate | Persisted messages via `onAIAssistantMessageReceived`, `onAIToolResultMessageReceived` |

</Accordion>

## Agent Run Lifecycle and Message Flow

This section explains how a user's text message to an Agent becomes a structured "run" which emits real-time events and then produces agentic messages for historical retrieval.
Expand Down Expand Up @@ -63,6 +127,73 @@ class AIAssistantEventHandler: AIAssistantEventsDelegate {
}
```

<Accordion title="Sample Payload - AIAssistantBaseEvent Properties">

**Event Object (AIAssistantBaseEvent):**

| Parameter | Type | Description |
| --------- | ---- | ----------- |
| type | String | Event type identifier. Example: `"run_start"`, `"text_message_content"` |
| id | String | Run ID for the event. Example: `"run_abc123"` |

**Event Types:**

| Type | Description |
| ---- | ----------- |
| `run_start` | New run has begun for the user's message |
| `tool_call_start` | 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` | Agent started composing a reply |
| `text_message_content` | Streaming content chunk for progressive rendering |
| `text_message_end` | Agent reply is complete |
| `run_finished` | Run is finalized; persisted messages will follow |

</Accordion>

<Accordion title="Sample Payload - AI Agent Event Sequence">

**1. Run Start Event:**

| Parameter | Type | Description |
| --------- | ---- | ----------- |
| type | String | Event type. Example: `"run_start"` |
| id | String | Run identifier. Example: `"run_abc123"` |

**2. Tool Call Start Event (if agent uses tools):**

| Parameter | Type | Description |
| --------- | ---- | ----------- |
| type | String | Event type. Example: `"tool_call_start"` |
| id | String | Run identifier. Example: `"run_abc123"` |
| toolName | String | Name of the tool being invoked. Example: `"weather_lookup"` |

**3. Tool Call Result Event:**

| Parameter | Type | Description |
| --------- | ---- | ----------- |
| type | String | Event type. Example: `"tool_call_result"` |
| id | String | Run identifier. Example: `"run_abc123"` |
| result | String | Tool output. Example: `"72°F, Sunny"` |

**4. Text Message Content Event (streaming):**

| Parameter | Type | Description |
| --------- | ---- | ----------- |
| type | String | Event type. Example: `"text_message_content"` |
| id | String | Run identifier. Example: `"run_abc123"` |
| content | String | Streaming content chunk. Example: `"The weather today is "` |

**5. Run Finished Event:**

| Parameter | Type | Description |
| --------- | ---- | ----------- |
| type | String | Event type. Example: `"run_finished"` |
| id | String | Run identifier. Example: `"run_abc123"` |

</Accordion>

#### Event descriptions

- Run Start: A new run has begun for the user's message.
Expand Down Expand Up @@ -105,4 +236,49 @@ class MessageHandler: CometChatMessageDelegate {
print("AI Tool Arguments Message Received: \(aiToolArgumentMessage)")
}
}
```
```

<Accordion title="Sample Payload - AIAssistantMessage">

**Event Trigger:** Received via `CometChatMessageDelegate.onAIAssistantMessageReceived(_:)`

**AIAssistantMessage Object:**

| Parameter | Type | Description |
| --------- | ---- | ----------- |
| type | String | Message type. Example: `"ai_assistant_message"` |
| id | Int | Unique message identifier. Example: `12347` |
| senderUid | String | UID of the AI agent. Example: `"ai-agent-uid"` |
| text | String | Full assistant reply. Example: `"The weather today is 72°F and sunny!"` |

</Accordion>

<Accordion title="Sample Payload - AIToolResultMessage">

**Event Trigger:** Received via `CometChatMessageDelegate.onAIToolResultMessageReceived(_:)`

**AIToolResultMessage Object:**

| Parameter | Type | Description |
| --------- | ---- | ----------- |
| type | String | Message type. Example: `"ai_tool_result_message"` |
| id | Int | Unique message identifier. Example: `12348` |
| toolName | String | Name of the tool that was called. Example: `"weather_lookup"` |
| result | String | Final output of the tool call. Example: `"72°F, Sunny"` |

</Accordion>

<Accordion title="Sample Payload - AIToolArgumentMessage">

**Event Trigger:** Received via `CometChatMessageDelegate.onAIToolArgumentsMessageReceived(_:)`

**AIToolArgumentMessage Object:**

| Parameter | Type | Description |
| --------- | ---- | ----------- |
| type | String | Message type. Example: `"ai_tool_argument_message"` |
| id | Int | Unique message identifier. Example: `12349` |
| toolName | String | Name of the tool that was called. Example: `"weather_lookup"` |
| arguments | [String: Any] | Arguments passed to the tool. Example: `{"location": "San Francisco"}` |

</Accordion>
89 changes: 89 additions & 0 deletions sdk/ios/ai-moderation.mdx
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
---
title: "AI Moderation"
description: "Guide to implementing AI-powered message moderation using the CometChat iOS SDK for content safety."
---

{/* TL;DR for Agents and Quick Reference */}
<Info>
**Quick Reference for AI Agents & Developers**

- **Enable:** Configure AI Moderation in CometChat Dashboard → Extensions
- **Behavior:** Messages automatically reviewed before delivery
- **Flagged messages:** Access via `message.metadata` for moderation status
- **Related:** [Moderation Overview](/moderation/overview) · [Extensions Overview](/sdk/ios/extensions-overview) · [Messaging Overview](/sdk/ios/messaging-overview)
</Info>

## Overview

AI Moderation in the CometChat SDK helps ensure that your chat application remains safe and compliant by automatically reviewing messages for inappropriate content. This feature leverages AI to moderate messages in real-time, reducing manual intervention and improving user experience.
Expand Down Expand Up @@ -107,6 +118,37 @@ When you send a text, image, or video message, check the initial moderation stat
</Tab>
</Tabs>

<Accordion title="Sample Payload - Send Message with Moderation Check">

**Request Parameters:**

| Parameter | Type | Description |
| --------- | ---- | ----------- |
| receiverUid | String | Unique identifier of the receiver. Example: `"cometchat-uid-2"` |
| text | String | Message content to send. Example: `"Hello, this is a test message!"` |
| receiverType | ReceiverType | Type of receiver. Example: `.user` |

**Success Response (TextMessage Object):**

| Parameter | Type | Description |
| --------- | ---- | ----------- |
| id | Int | Unique message identifier. Example: `38254` |
| muid | String? | Message unique ID. Example: `nil` |
| senderUid | String? | UID of the sender. Example: `"cometchat-uid-2"` |
| receiverUid | String? | UID of the receiver. Example: `"cometchat-uid-2"` |
| getModerationStatus() | String | Current moderation status. Example: `"pending"` or `"unmoderated"` |

**Moderation Status Values:**

| Status | Description |
| ------ | ----------- |
| `"pending"` | Message is being processed by moderation service |
| `"approved"` | Message passed moderation and is visible |
| `"disapproved"` | Message violated rules and was blocked |
| `"unmoderated"` | Moderation is not enabled for this app |

</Accordion>

### Step 2: Listen for Moderation Results

Implement the `onMessageModerated` delegate method to receive moderation results in real-time:
Expand Down Expand Up @@ -189,6 +231,53 @@ Implement the `onMessageModerated` delegate method to receive moderation results
</Tab>
</Tabs>

<Accordion title="Sample Payload - onMessageModerated Event (Approved)">

**Event Trigger:** Received via `CometChatMessageDelegate.onMessageModerated(moderatedMessage:)`

**moderatedMessage (TextMessage Object):**

| Parameter | Type | Description |
| --------- | ---- | ----------- |
| id | Int | Unique message identifier. Example: `12345` |
| text | String | Original message content. Example: `"Hello, how are you?"` |
| senderUid | String? | UID of the message sender. Example: `"cometchat-uid-1"` |
| receiverUid | String? | UID of the receiver. Example: `"cometchat-uid-2"` |
| getModerationStatus() | String | Final moderation status. Example: `"approved"` |

**UI Action:**

| Action | Description |
| ------ | ----------- |
| Show message | Display message normally in the chat UI |
| Remove pending indicator | Clear any "under review" status |

</Accordion>

<Accordion title="Sample Payload - onMessageModerated Event (Disapproved)">

**Event Trigger:** Received via `CometChatMessageDelegate.onMessageModerated(moderatedMessage:)`

**moderatedMessage (TextMessage Object):**

| Parameter | Type | Description |
| --------- | ---- | ----------- |
| id | Int | Unique message identifier. Example: `12346` |
| text | String | Original message content. Example: `"[inappropriate content]"` |
| senderUid | String? | UID of the message sender. Example: `"cometchat-uid-1"` |
| receiverUid | String? | UID of the receiver. Example: `"cometchat-uid-2"` |
| getModerationStatus() | String | Final moderation status. Example: `"disapproved"` |

**Handling Options:**

| Option | Description |
| ------ | ----------- |
| Hide completely | Remove message from UI entirely |
| Show placeholder | Display "Message blocked by moderation" |
| Notify sender | Show notification about policy violation |

</Accordion>

### Step 3: Handle Disapproved Messages

When a message is disapproved, handle it appropriately in your UI:
Expand Down
Loading