How the Conversation class works
mainThe Conversation class is a high-level wrapper around RealtimeAPI. It manages:
- Sending and receiving messages.
- Conversation history.
- Automatic microphone recording.
- Automatic playback of model responses.
Accessing Messages
messages: Returns only the messages between the user and the model (excludes function calls/responses).entries: Returns the full conversation history, including all event types.
Customizing the Session
To modify session settings (like system instructions or transcription), use updateSession(withChanges:) after the connection is established. It is recommended to do this within a whenConnected callback.
Sending Content
- Text:
send(from:text:response:)using roles.user,.assistant, or.system. - Audio:
send(audioDelta:commit:)to send raw audio chunks. Settingcommit: truetells the model the message is finished. - Events:
send(event:)allows sending rawRealtimeAPI.ClientEventobjects, though this bypasses automatic interrupt handling.
// Customizing session settings
try await conversation.whenConnected {
try await conversation.updateSession { session in
session.instructions = "You are a helpful assistant."
session.inputAudioTranscription = Session.InputAudioTranscription()
}
}
// Sending a text message
try await conversation.send(from: .user, text: "Hello!")
// Sending audio chunks
try await conversation.send(audioDelta: audioData, commit: true)