Class GoogleGenAIRealtimeSession
- Namespace
- Microsoft.Extensions.AI
- Assembly
- Google.GenAI.dll
Provides an Microsoft.Extensions.AI.IRealtimeClientSession implementation for Google GenAI's Live API, wrapping an AsyncSession WebSocket connection.
public sealed class GoogleGenAIRealtimeSession : IRealtimeClientSession, IAsyncDisposable
- Inheritance
-
GoogleGenAIRealtimeSession
- Implements
-
IRealtimeClientSession
- Inherited Members
Constructors
GoogleGenAIRealtimeSession(AsyncSession, string, RealtimeSessionOptions?)
Initializes a new instance wrapping a connected AsyncSession.
public GoogleGenAIRealtimeSession(AsyncSession asyncSession, string model, RealtimeSessionOptions? initialOptions)
Parameters
asyncSessionAsyncSessionThe connected AsyncSession for WebSocket communication.
modelstringThe model name for metadata.
initialOptionsRealtimeSessionOptionsOptional initial session options.
Properties
Options
Gets the current session options.
public RealtimeSessionOptions? Options { get; }
Property Value
- RealtimeSessionOptions
Methods
DisposeAsync()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources asynchronously.
public ValueTask DisposeAsync()
Returns
GetService(Type, object?)
Asks the Microsoft.Extensions.AI.IRealtimeClientSession for an object of the specified type serviceType.
public object? GetService(Type serviceType, object? serviceKey = null)
Parameters
serviceTypeTypeThe type of object being requested.
serviceKeyobjectAn optional key that can be used to help identify the target service.
Returns
Remarks
The purpose of this method is to allow for the retrieval of strongly typed services that might be provided by the Microsoft.Extensions.AI.IRealtimeClientSession, including itself or any services it might be wrapping.
Exceptions
- ArgumentNullException
serviceTypeis null.
GetStreamingResponseAsync(CancellationToken)
Streams the response from the real-time session.
public IAsyncEnumerable<RealtimeServerMessage> GetStreamingResponseAsync(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationTokenA token to cancel the operation.
Returns
- IAsyncEnumerable<RealtimeServerMessage>
The response messages generated by the session.
Remarks
This method cannot be called multiple times concurrently on the same session instance.
SendAsync(RealtimeClientMessage, CancellationToken)
Sends a client message to the session.
public Task SendAsync(RealtimeClientMessage message, CancellationToken cancellationToken = default)
Parameters
messageRealtimeClientMessageThe client message to send.
cancellationTokenCancellationTokenA token to cancel the operation.
Returns
- Task
A task that represents the asynchronous send operation.
Remarks
This method allows for sending client messages to the session at any time, which can be used to influence the session's behavior or state.
Concurrency note for provider implementers: SendAsync(RealtimeClientMessage, CancellationToken) may be called concurrently
from multiple sources. For example, a caller may stream audio via SendAsync(RealtimeClientMessage, CancellationToken) on one thread while
middleware such as FunctionInvokingRealtimeClientSession calls SendAsync(RealtimeClientMessage, CancellationToken) to return tool results
from within GetStreamingResponseAsync(CancellationToken) enumeration on another thread. If the underlying transport
(e.g., a WebSocket) does not support concurrent sends, provider implementations must serialize access — for
example by using a SemaphoreSlim — to prevent protocol violations.