VoiceLiveSessionAsyncClient Class

  • java.lang.Object
    • com.azure.ai.voicelive.VoiceLiveSessionAsyncClient

Implements

public final class VoiceLiveSessionAsyncClient
implements AsyncCloseable, AutoCloseable

Represents a WebSocket-based session for real-time voice communication with the Azure VoiceLive service.

This class abstracts bidirectional communication between the caller and service, simultaneously sending and receiving WebSocket messages.

Users can obtain a VoiceLiveSessionAsyncClient instance from startSession(), VoiceLiveAsyncClient#startSession(String, com.azure.ai.voicelive.models.VoiceLiveRequestOptions), or VoiceLiveAsyncClient#startSession(com.azure.ai.voicelive.models.AgentSessionConfig, com.azure.ai.voicelive.models.VoiceLiveRequestOptions) and work directly with it for optimal performance. Alternatively, users can use the convenience methods on VoiceLiveAsyncClient which delegate to the internal session.

Thread Safety:

This class is thread-safe and supports concurrent operations.

Resource Management:

Sessions should be properly closed using close() or closeAsync() to release resources.

Method Summary

Modifier and Type Method and Description
reactor.core.publisher.Mono<Void> addItem(ConversationRequestItem item)

Adds an item to the conversation.

reactor.core.publisher.Mono<Void> addItem(ConversationRequestItem item, String previousItemId)

Adds an item to the conversation at a specific position.

reactor.core.publisher.Mono<Void> appendAudioToTurn(String turnId, BinaryData audio)

Appends audio data to an ongoing input turn.

reactor.core.publisher.Mono<Void> cancelAudioTurn(String turnId)

Cancels an in-progress input audio turn.

reactor.core.publisher.Mono<Void> cancelResponse()

Cancels the current response generation.

reactor.core.publisher.Mono<Void> clearInputAudio()

Clears the input audio buffer.

reactor.core.publisher.Mono<Void> clearStreamingAudio()

Clears all input audio currently being streamed.

void close()
reactor.core.publisher.Mono<Void> closeAsync()
reactor.core.publisher.Mono<Void> commitInputAudio()

Commits the input audio buffer.

reactor.core.publisher.Mono<Void> configureSession(VoiceLiveSessionOptions sessionOptions)

Updates the session configuration.

reactor.core.publisher.Mono<Void> connectAvatar(String clientSdp)

Connects and provides the client's SDP (Session Description Protocol) for avatar-related media negotiation.

reactor.core.publisher.Mono<Void> deleteItem(String itemId)

Deletes an item from the conversation.

reactor.core.publisher.Mono<Void> endAudioTurn(String turnId)

Marks the end of an audio input turn.

boolean isConnected()

Checks if the session is currently connected.

reactor.core.publisher.Flux<SessionServerEvent> receiveEvents()

Receives parsed events from the service as strongly-typed SessionServerEvent objects.

reactor.core.publisher.Mono<Void> requestItemRetrieval(String itemId)

Sends a request to retrieve an item from the conversation.

reactor.core.publisher.Mono<Void> send(BinaryData data)

Sends binary data to the service.

reactor.core.publisher.Mono<Void> sendEvent(SessionClientEvent event)

Sends an event to the service.

reactor.core.publisher.Mono<Void> sendInputAudio(BinaryData audio)

Transmits audio data from BinaryData.

reactor.core.publisher.Mono<Void> startAudioTurn(String turnId)

Starts a new audio input turn.

reactor.core.publisher.Mono<Void> startResponse()

Starts a new response generation.

reactor.core.publisher.Mono<Void> startResponse(String additionalInstructions)

Starts a new response generation with additional instructions.

reactor.core.publisher.Mono<Void> startResponse(VoiceLiveSessionOptions responseOptions)

Starts a new response generation with specific options.

reactor.core.publisher.Mono<Void> truncateConversation(String itemId, int contentIndex)

Truncates the conversation history.

reactor.core.publisher.Mono<Void> truncateConversation(String itemId, int contentIndex, Duration audioEnd)

Truncates the conversation history.

Methods inherited from java.lang.Object

Method Details

addItem

public Mono<Void> addItem(ConversationRequestItem item)

Adds an item to the conversation.

Parameters:

item - The item to add to the conversation.

Returns:

A Mono that completes when the item is added.

addItem

public Mono<Void> addItem(ConversationRequestItem item, String previousItemId)

Adds an item to the conversation at a specific position.

Parameters:

item - The item to add to the conversation.
previousItemId - The ID of the item after which to insert the new item.

Returns:

A Mono that completes when the item is added.

appendAudioToTurn

public Mono<Void> appendAudioToTurn(String turnId, BinaryData audio)

Appends audio data to an ongoing input turn.

To append a raw byte array, wrap it with BinaryData#fromBytes(byte[]).

Parameters:

turnId - The ID of the turn this audio is part of.
audio - The audio data to append.

Returns:

A Mono that completes when the audio is appended.

cancelAudioTurn

public Mono<Void> cancelAudioTurn(String turnId)

Cancels an in-progress input audio turn.

Parameters:

turnId - The ID of the turn to cancel.

Returns:

A Mono that completes when the turn is cancelled.

cancelResponse

public Mono<Void> cancelResponse()

Cancels the current response generation.

Returns:

A Mono that completes when the response is cancelled.

clearInputAudio

public Mono<Void> clearInputAudio()

Clears the input audio buffer.

Returns:

A Mono that completes when the buffer is cleared.

clearStreamingAudio

public Mono<Void> clearStreamingAudio()

Clears all input audio currently being streamed.

Returns:

A Mono that completes when the streaming audio is cleared.

close

public void close()

closeAsync

public Mono<Void> closeAsync()

commitInputAudio

public Mono<Void> commitInputAudio()

Commits the input audio buffer.

Returns:

A Mono that completes when the buffer is committed.

configureSession

public Mono<Void> configureSession(VoiceLiveSessionOptions sessionOptions)

Updates the session configuration.

Parameters:

sessionOptions - The session configuration options.

Returns:

A Mono that completes when the configuration is updated.

connectAvatar

public Mono<Void> connectAvatar(String clientSdp)

Connects and provides the client's SDP (Session Description Protocol) for avatar-related media negotiation.

Parameters:

clientSdp - The client's SDP offer.

Returns:

A Mono that completes when the avatar connection is established.

deleteItem

public Mono<Void> deleteItem(String itemId)

Deletes an item from the conversation.

Parameters:

itemId - The ID of the item to delete.

Returns:

A Mono that completes when the item is deleted.

endAudioTurn

public Mono<Void> endAudioTurn(String turnId)

Marks the end of an audio input turn.

Parameters:

turnId - The ID of the audio turn being ended.

Returns:

A Mono that completes when the turn is ended.

isConnected

public boolean isConnected()

Checks if the session is currently connected.

Returns:

true if connected, false otherwise.

receiveEvents

public Flux<SessionServerEvent> receiveEvents()

Receives parsed events from the service as strongly-typed SessionServerEvent objects.

This method provides a higher-level alternative to #receive() by automatically parsing the raw BinaryData into the appropriate SessionServerEvent subclass based on the event type. This enables type-safe event handling and better developer experience.

Events that cannot be parsed will be logged and skipped, ensuring the stream continues to operate even if unknown event types are received.

Returns:

A Flux of SessionServerEvent objects representing parsed server events.

requestItemRetrieval

public Mono<Void> requestItemRetrieval(String itemId)

Sends a request to retrieve an item from the conversation.

This method only sends the retrieval request; the requested item is delivered asynchronously by the service as a server event on the receiveEvents() stream. Subscribe to receiveEvents() to observe the response.

Parameters:

itemId - The ID of the item to retrieve.

Returns:

A Mono that completes when the retrieval request is sent.

send

public Mono<Void> send(BinaryData data)

Sends binary data to the service.

Parameters:

data - The binary data to send.

Returns:

A Mono that completes when the data is sent.

sendEvent

public Mono<Void> sendEvent(SessionClientEvent event)

Sends an event to the service.

Parameters:

event - The client event to send.

Returns:

A Mono that completes when the command is sent.

sendInputAudio

public Mono<Void> sendInputAudio(BinaryData audio)

Transmits audio data from BinaryData.

Parameters:

audio - The audio data to transmit.

Returns:

A Mono that completes when the audio is sent.

startAudioTurn

public Mono<Void> startAudioTurn(String turnId)

Starts a new audio input turn.

Parameters:

turnId - Unique identifier for the input audio turn.

Returns:

A Mono that completes when the turn is started.

startResponse

public Mono<Void> startResponse()

Starts a new response generation.

Returns:

A Mono that completes when the response generation starts.

startResponse

public Mono<Void> startResponse(String additionalInstructions)

Starts a new response generation with additional instructions.

Parameters:

additionalInstructions - Additional instructions for this response.

Returns:

A Mono that completes when the response generation starts.

startResponse

public Mono<Void> startResponse(VoiceLiveSessionOptions responseOptions)

Starts a new response generation with specific options.

Parameters:

responseOptions - The options for response generation.

Returns:

A Mono that completes when the response generation starts.

truncateConversation

public Mono<Void> truncateConversation(String itemId, int contentIndex)

Truncates the conversation history.

Parameters:

itemId - The ID of the item up to which to truncate the conversation.
contentIndex - The content index within the item to truncate to.

Returns:

A Mono that completes when the conversation is truncated.

truncateConversation

public Mono<Void> truncateConversation(String itemId, int contentIndex, Duration audioEnd)

Truncates the conversation history.

Parameters:

itemId - The ID of the item up to which to truncate the conversation.
contentIndex - The content index within the item to truncate to.
audioEnd - Inclusive duration up to which audio is truncated. May be null or Duration#ZERO to indicate no audio truncation.

Returns:

A Mono that completes when the conversation is truncated.

Applies to