iOS SDK
Conversations & History
Start and continue conversations, list them, and page through their messages with the iOS SDK
The SDK keeps track of the conversation you're in, and can list the user's past conversations and load their messages a page at a time. This page covers newConversation, listConversations, listMessages and the Page they return.
Starting and continuing conversations
- Send a message
The first
sendon a new client starts a new conversation. The server creates it.let response = try await client.send("Hello!") - Keep the conversation ID if you need it
let conversationId = response.conversationIdThe client also keeps it as
currentConversationId. - Continue the conversation
The next
sendcontinuescurrentConversationIdby itself. Pass an ID only to continue a different conversation:let followUp = try await client.send("Tell me more") let elsewhere = try await client.send("Any update?", conversationId: olderConversationId)
newConversation
public func newConversation()Clears currentConversationId, so the next send starts a new conversation.
await client.newConversation()
try await client.send("Brand new conversation!")listConversations
public func listConversations(
cursor: String? = nil,
limit: Int = 20
) async throws -> Page<Conversation>Lists the signed-in user's conversations, or this device's while anonymous, most recently active first.
| Parameter | Type | Required | Description |
|---|---|---|---|
cursor | String? | No | The cursor from the previous page. Leave it out to start with the most recent conversations. |
limit | Int | No | How many per page, from 1 to 100. Defaults to 20. |
let page = try await client.listConversations(limit: 20)
for conversation in page.items {
print("\(conversation.title ?? "New conversation")")
print(" Status: \(conversation.status)")
print(" Updated: \(conversation.updatedAt.formatted())")
}
print("Total: \(page.total)")
print("Has more: \(page.hasMore)")See Conversation for the full type.
listMessages
public func listMessages(
conversationId: String,
cursor: String? = nil,
limit: Int = 20
) async throws -> Page<Message>Loads a page of a conversation's messages.
| Parameter | Type | Required | Description |
|---|---|---|---|
conversationId | String | Yes | The conversation to load. It must belong to the current user or device. |
cursor | String? | No | The cursor from the previous page. Leave it out to start with the latest messages. |
limit | Int | No | How many per page, from 1 to 100. Defaults to 20. |
let page = try await client.listMessages(conversationId: conversationId, limit: 50)
for message in page.items {
let who = message.role == .user ? "You" : "Agent"
print("\(who): \(message.text)")
}Each Message has a plain text (all its text parts joined) and a parts list, so you can show tool activity alongside the text:
for part in message.parts {
switch part {
case .text(let text):
renderBubble(text)
case .toolCall(let call):
renderToolCard(call.name, input: call.input)
case .toolResult(let result):
attachToolOutput(result.output)
}
}Private notes your team adds in the Inbox are never returned. Replies a person sends after taking over the conversation come back with the role .assistant.
See Message for the full type.
Page
public struct Page<Item: Sendable & Decodable>: Sendable, Decodable {
public let items: [Item]
public let cursor: String?
public let hasMore: Bool
public let total: Int
}| Property | Type | Description |
|---|---|---|
items | [Item] | The items on this page. |
cursor | String? | Pass it back to get the next page. nil on the last page. |
hasMore | Bool | Whether there are more pages. |
total | Int | How many items there are in all. |
Loading the next page
Pass cursor back with the same method to get the next page. Each page holds only its own items; add them to your own array, at the bottom for conversations or the top for older messages.
var page = try await client.listConversations(limit: 20)
var all = page.items
while page.hasMore, let cursor = page.cursor {
page = try await client.listConversations(cursor: cursor, limit: 20)
all.append(contentsOf: page.items)
}
print("Loaded \(all.count) of \(page.total)")Infinite scroll in SwiftUI
struct ConversationList: View {
@State var list: ConversationListState
var body: some View {
List {
ForEach(list.conversations) { conversation in
ConversationRow(conversation)
}
if list.hasMore {
ProgressView()
.task { await list.loadMore() }
}
}
.task { await list.load() }
}
}Conversation status
A conversation that has ended, or that a person has taken over, can't take new messages. Sending to one fails with 403 CHAT_CONVERSATION_NOT_ONGOING. Check the status before showing a text field:
if conversation.status == .ongoing {
showComposer()
} else {
showReadOnlyBanner()
}| Status | What it means |
|---|---|
.ongoing | Open, and answered by your agent. |
.ended | Closed. Start a new conversation. |
.takenOver | A person on your team took it over from the Inbox. |