iOS SDK
ChatResponse
Reference for ChatResponse, Message, MessagePart and FinishReason, the types send, retry and sendNonStreaming return
Every answer comes back as a ChatResponse. It holds the agent's Message, the conversation it belongs to, why it ended and how many credits it used.
ChatResponse
struct ChatResponse: Sendable, Equatable
What send, retry and sendNonStreaming give you back.
public struct ChatResponse: Sendable, Equatable {
public let message: Message
public let conversationId: String
public let userMessageId: String?
public let userId: String?
public let finishReason: FinishReason
public let credits: Double
}| Property | Type | Description |
|---|---|---|
message | Message | The agent's answer. See Message. |
conversationId | String | The conversation this answer belongs to. The client also keeps it as currentConversationId. |
userMessageId | String? | The ID the server gave the message you sent. |
userId | String? | The signed-in user, or nil while anonymous. |
finishReason | FinishReason | Why the answer ended. See FinishReason. |
credits | Double | Message credits this answer used. 0 when the server reports none. |
let response = try await client.send("Hello")
print(response.message.text) // the answer
print(response.message.id) // pass to retry(conversationId:messageId:)
print(response.conversationId) // the conversation it belongs to
print(response.credits)Message
struct Message: Sendable, Equatable, Identifiable, Decodable
Used both for the answer inside ChatResponse and for each item from listMessages.
public struct Message: Sendable, Equatable, Identifiable, Decodable {
public enum Role: String, Sendable, Codable { case user, assistant }
public enum Feedback: String, Sendable, Codable { case positive, negative }
public let id: String
public let role: Role
public var parts: [MessagePart]
public let createdAt: Date?
public let feedback: Feedback?
public let score: Double?
public var text: String { get }
}| Property | Type | Description |
|---|---|---|
id | String | The message ID. |
role | Role | .user or .assistant. |
parts | [MessagePart] | The message in pieces, in order: text, tool calls and tool results. |
createdAt | Date? | When the message was created. For a fresh answer, the time it arrived on the device. |
feedback | Feedback? | .positive or .negative when someone rated the answer, otherwise nil. Set on messages loaded from history. |
score | Double? | The agent's confidence in this answer, when it reported one. Set on messages loaded from history. |
text | String | All the text parts joined together. Handy for a plain bubble. |
Message.Role
public enum Role: String, Sendable, Codable {
case user
case assistant
}Anything not from the user is .assistant, including replies typed by a person who took over the conversation.
Message.Feedback
public enum Feedback: String, Sendable, Codable {
case positive // "positive"
case negative // "negative"
}MessagePart
public enum MessagePart: Sendable, Equatable, Codable {
case text(String)
case toolCall(ToolCall)
case toolResult(ToolResult)
}.text
Text the agent wrote, or the user sent.
case text(String)The value is the text itself.
.toolCall
The agent calling a tool.
case toolCall(ToolCall)A ToolCall holds the call's id, the tool's name (the action's Action name) and the input the agent passed, as a JSONValue. input is .null when the agent passed nothing.
.toolResult
What the tool returned.
case toolResult(ToolResult)A ToolResult holds the id of the call it answers, the tool's name and its output. An object with an error key means the tool failed.
for part in message.parts {
switch part {
case .text(let text):
renderBubble(text)
case .toolCall(let call):
beginToolCard(id: call.id, name: call.name, input: call.input)
case .toolResult(let result):
completeToolCard(id: result.id, output: result.output)
}
}FinishReason
public enum FinishReason: String, Sendable {
case stop // "stop" finished normally
case error // "error" the answer failed
case toolCalls = "tool-calls" // "tool-calls" waiting for tool results
}ChatResponse.FinishReason is nested in ChatResponse.