ChatSession
An observable chat you can bind a SwiftUI view to.
Holds the message list, the in-flight status, and the send/stop/regenerate actions behind an observable object, so a view can bind to it and stay in sync without you writing the plumbing.
It handles the parts that are tedious to get right by hand: streaming deltas into the right message, keeping tool call and result parts paired, surfacing errors, and cancelling cleanly when someone hits stop.
@State private var chat = ChatSession(
model: AnthropicModel("claude-sonnet-5")
)
var body: some View {
MessageList(messages: chat.messages)
TextField("Message", text: $draft)
.onSubmit { Task { await chat.send(draft) } }
}Declaration
public class ChatSessionDefined in Sources/AI/UI/ChatSession.swift.
Initializer
init(
transport: any ChatTransport,
id: String = UUID().uuidString,
messages: [UIMessage] = []
)Properties
| Property | Type | Default |
|---|---|---|
let id | String | — |
var messages | [UIMessage] | — |
var status | Status | .ready |
var isLoading | Bool { status | `= .submitted |
Methods
func send(
_ text: String
)func sendMessage(
_ message: UIMessage
)func regenerate()func addToolResult(
toolCallID: String,
result: JSONValue
)func addToolApprovalResponse(
approvalID: String,
approved: Bool,
reason: String? = nil
)func resumeStream()func stop()func setMessages(
_ newMessages: [UIMessage]
)