11using System ;
22using System . ClientModel ;
33using System . ClientModel . Primitives ;
4+ using System . Threading ;
45using System . Threading . Tasks ;
56
67namespace OpenAI . Realtime ;
@@ -10,43 +11,80 @@ namespace OpenAI.Realtime;
1011[ CodeGenSuppress ( "CreateStartRealtimeSessionRequest" , typeof ( BinaryContent ) , typeof ( RequestOptions ) ) ]
1112public partial class RealtimeClient
1213{
13- /// <summary>
14- /// <para>[Protocol Method]</para>
15- /// Creates a new realtime conversation operation instance, establishing a connection to the /realtime endpoint.
16- /// </summary>
17- /// <param name="model"></param>
18- /// <param name="options"></param>
19- /// <returns></returns>
20- public virtual async Task < RealtimeSession > StartConversationSessionAsync ( string model , RequestOptions options )
14+ /// <summary> Starts a new <see cref="RealtimeSession"/> for multimodal conversation. </summary>
15+ /// <remarks>
16+ /// The <see cref="RealtimeSession"/> abstracts bidirectional communication between the caller and service,
17+ /// simultaneously sending and receiving WebSocket messages.
18+ /// </remarks>
19+ public virtual async Task < RealtimeSession > StartConversationSessionAsync ( string model , RealtimeSessionOptions options = null , CancellationToken cancellationToken = default )
2120 {
2221 Argument . AssertNotNull ( model , nameof ( model ) ) ;
23- return await StartSessionAsync ( model , intent : null , options ) . ConfigureAwait ( false ) ;
22+
23+ return await StartSessionAsync (
24+ model : model ,
25+ intent : null ,
26+ options : options ,
27+ cancellationToken : cancellationToken ) . ConfigureAwait ( false ) ;
2428 }
2529
26- /// <summary>
27- /// <para>[Protocol Method]</para>
28- /// Creates a new realtime transcription operation instance, establishing a connection to the /realtime endpoint.
29- /// </summary>
30- /// <param name="options"></param>
31- /// <returns></returns>
32- public virtual Task < RealtimeSession > StartTranscriptionSessionAsync ( RequestOptions options )
33- => StartSessionAsync ( model : null , intent : "transcription" , options ) ;
30+ /// <summary> Starts a new <see cref="RealtimeSession"/> for multimodal conversation. </summary>
31+ /// <remarks>
32+ /// The <see cref="RealtimeSession"/> abstracts bidirectional communication between the caller and service,
33+ /// simultaneously sending and receiving WebSocket messages.
34+ /// </remarks>
35+ public RealtimeSession StartConversationSession ( string model , RealtimeSessionOptions options = null , CancellationToken cancellationToken = default )
36+ {
37+ Argument . AssertNotNull ( model , nameof ( model ) ) ;
38+
39+ return StartSession (
40+ model : model ,
41+ intent : null ,
42+ options : options ,
43+ cancellationToken : cancellationToken ) ;
44+ }
3445
35- /// <summary>
36- /// <para>[Protocol Method]</para>
37- /// Creates a new realtime operation instance, establishing a connection to the /realtime endpoint.
38- /// </summary>
39- /// <param name="model"></param>
40- /// <param name="intent"></param>
41- /// <param name="options"></param>
42- /// <returns></returns>
43- public virtual async Task < RealtimeSession > StartSessionAsync ( string model , string intent , RequestOptions options )
46+ /// <summary> Starts a new <see cref="RealtimeSession"/> for audio transcription.</summary>
47+ /// <remarks>
48+ /// The <see cref="RealtimeSession"/> abstracts bidirectional communication between the caller and service,
49+ /// simultaneously sending and receiving WebSocket messages.
50+ /// </remarks>
51+ public virtual async Task < RealtimeSession > StartTranscriptionSessionAsync ( RealtimeSessionOptions options = null , CancellationToken cancellationToken = default )
4452 {
45- Uri fullEndpoint = BuildSessionEndpoint ( _webSocketEndpoint , model , intent ) ;
46- RealtimeSession provisionalSession = new ( this , fullEndpoint , _keyCredential ) ;
53+ return await StartSessionAsync (
54+ model : null ,
55+ intent : "transcription" ,
56+ options : options ,
57+ cancellationToken : cancellationToken ) . ConfigureAwait ( false ) ;
58+ }
59+
60+ /// <summary> Starts a new <see cref="RealtimeSession"/> for audio transcription.</summary>
61+ /// <remarks>
62+ /// The <see cref="RealtimeSession"/> abstracts bidirectional communication between the caller and service,
63+ /// simultaneously sending and receiving WebSocket messages.
64+ /// </remarks>
65+ public RealtimeSession StartTranscriptionSession ( RealtimeSessionOptions options = null , CancellationToken cancellationToken = default )
66+ {
67+ return StartSession (
68+ model : null ,
69+ intent : "transcription" ,
70+ options : options ,
71+ cancellationToken : cancellationToken ) ;
72+ }
73+
74+ /// <summary> Starts a new <see cref="RealtimeSession"/>. </summary>
75+ /// <remarks>
76+ /// The <see cref="RealtimeSession"/> abstracts bidirectional communication between the caller and service,
77+ /// simultaneously sending and receiving WebSocket messages.
78+ /// </remarks>
79+ public virtual async Task < RealtimeSession > StartSessionAsync ( string model , string intent , RealtimeSessionOptions options = null , CancellationToken cancellationToken = default )
80+ {
81+ options ??= new ( ) ;
82+
83+ RealtimeSession provisionalSession = new ( _keyCredential , this , _webSocketEndpoint , model , intent ) ;
84+
4785 try
4886 {
49- await provisionalSession . ConnectAsync ( options ) . ConfigureAwait ( false ) ;
87+ await provisionalSession . ConnectAsync ( options . QueryString , options . Headers , cancellationToken ) . ConfigureAwait ( false ) ;
5088 RealtimeSession result = provisionalSession ;
5189 provisionalSession = null ;
5290 return result ;
@@ -57,18 +95,13 @@ public virtual async Task<RealtimeSession> StartSessionAsync(string model, strin
5795 }
5896 }
5997
60- private static Uri BuildSessionEndpoint ( Uri baseEndpoint , string model , string intent )
98+ /// <summary> Starts a new <see cref="RealtimeSession"/>. </summary>
99+ /// <remarks>
100+ /// The <see cref="RealtimeSession"/> abstracts bidirectional communication between the caller and service,
101+ /// simultaneously sending and receiving WebSocket messages.
102+ /// </remarks>
103+ public RealtimeSession StartSession ( string model , string intent , RealtimeSessionOptions options = null , CancellationToken cancellationToken = default )
61104 {
62- ClientUriBuilder builder = new ( ) ;
63- builder . Reset ( baseEndpoint ) ;
64- if ( ! string . IsNullOrEmpty ( model ) )
65- {
66- builder . AppendQuery ( "model" , model , escape : true ) ;
67- }
68- if ( ! string . IsNullOrEmpty ( intent ) )
69- {
70- builder . AppendQuery ( "intent" , intent , escape : true ) ;
71- }
72- return builder . ToUri ( ) ;
105+ return StartSessionAsync ( model , intent , options , cancellationToken ) . ConfigureAwait ( false ) . GetAwaiter ( ) . GetResult ( ) ;
73106 }
74107}
0 commit comments