Skip to content

Commit 37fb0c8

Browse files
committed
Refactor StartSession methods in RealtimeClient
1 parent d893a79 commit 37fb0c8

File tree

9 files changed

+182
-160
lines changed

9 files changed

+182
-160
lines changed

api/OpenAI.net8.0.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4341,13 +4341,12 @@ public class RealtimeClient {
43414341
public virtual Task<ClientResult> CreateEphemeralTokenAsync(BinaryContent content, RequestOptions options = null);
43424342
public virtual ClientResult CreateEphemeralTranscriptionToken(BinaryContent content, RequestOptions options = null);
43434343
public virtual Task<ClientResult> CreateEphemeralTranscriptionTokenAsync(BinaryContent content, RequestOptions options = null);
4344-
public RealtimeSession StartConversationSession(string model, CancellationToken cancellationToken = default);
4345-
public virtual Task<RealtimeSession> StartConversationSessionAsync(string model, RequestOptions options);
4346-
public virtual Task<RealtimeSession> StartConversationSessionAsync(string model, CancellationToken cancellationToken = default);
4347-
public virtual Task<RealtimeSession> StartSessionAsync(string model, string intent, RequestOptions options);
4348-
public RealtimeSession StartTranscriptionSession(CancellationToken cancellationToken = default);
4349-
public virtual Task<RealtimeSession> StartTranscriptionSessionAsync(RequestOptions options);
4350-
public virtual Task<RealtimeSession> StartTranscriptionSessionAsync(CancellationToken cancellationToken = default);
4344+
public RealtimeSession StartConversationSession(string model, RealtimeSessionOptions options = null, CancellationToken cancellationToken = default);
4345+
public virtual Task<RealtimeSession> StartConversationSessionAsync(string model, RealtimeSessionOptions options = null, CancellationToken cancellationToken = default);
4346+
public RealtimeSession StartSession(string model, string intent, RealtimeSessionOptions options = null, CancellationToken cancellationToken = default);
4347+
public virtual Task<RealtimeSession> StartSessionAsync(string model, string intent, RealtimeSessionOptions options = null, CancellationToken cancellationToken = default);
4348+
public RealtimeSession StartTranscriptionSession(RealtimeSessionOptions options = null, CancellationToken cancellationToken = default);
4349+
public virtual Task<RealtimeSession> StartTranscriptionSessionAsync(RealtimeSessionOptions options = null, CancellationToken cancellationToken = default);
43514350
}
43524351
[Experimental("OPENAI002")]
43534352
[Flags]
@@ -4387,7 +4386,7 @@ public class RealtimeItem : IJsonModel<RealtimeItem>, IPersistableModel<Realtime
43874386
}
43884387
[Experimental("OPENAI002")]
43894388
public class RealtimeSession : IDisposable {
4390-
protected internal RealtimeSession(RealtimeClient parentClient, Uri endpoint, ApiKeyCredential credential);
4389+
protected internal RealtimeSession(ApiKeyCredential credential, RealtimeClient parentClient, Uri endpoint, string model, string intent);
43914390
public Net.WebSockets.WebSocket WebSocket { get; protected set; }
43924391
public virtual void AddItem(RealtimeItem item, string previousItemId, CancellationToken cancellationToken = default);
43934392
public virtual void AddItem(RealtimeItem item, CancellationToken cancellationToken = default);
@@ -4403,8 +4402,8 @@ public class RealtimeSession : IDisposable {
44034402
public virtual void ConfigureSession(ConversationSessionOptions sessionOptions, CancellationToken cancellationToken = default);
44044403
public virtual void ConfigureTranscriptionSession(TranscriptionSessionOptions sessionOptions, CancellationToken cancellationToken = default);
44054404
public virtual Task ConfigureTranscriptionSessionAsync(TranscriptionSessionOptions sessionOptions, CancellationToken cancellationToken = default);
4406-
protected internal virtual void Connect(RequestOptions options);
4407-
protected internal virtual Task ConnectAsync(RequestOptions options);
4405+
protected internal virtual void Connect(IDictionary<string, string> headers = null, CancellationToken cancellationToken = default);
4406+
protected internal virtual Task ConnectAsync(IDictionary<string, string> headers = null, CancellationToken cancellationToken = default);
44084407
public virtual void DeleteItem(string itemId, CancellationToken cancellationToken = default);
44094408
public virtual Task DeleteItemAsync(string itemId, CancellationToken cancellationToken = default);
44104409
public void Dispose();
@@ -4429,6 +4428,10 @@ public class RealtimeSession : IDisposable {
44294428
public virtual void TruncateItem(string itemId, int contentPartIndex, TimeSpan audioDuration, CancellationToken cancellationToken = default);
44304429
public virtual Task TruncateItemAsync(string itemId, int contentPartIndex, TimeSpan audioDuration, CancellationToken cancellationToken = default);
44314430
}
4431+
public class RealtimeSessionOptions {
4432+
public Uri Endpoint { get; set; }
4433+
public IDictionary<string, string> Headers { get; }
4434+
}
44324435
[Experimental("OPENAI002")]
44334436
public class RealtimeUpdate : IJsonModel<RealtimeUpdate>, IPersistableModel<RealtimeUpdate> {
44344437
public string EventId { get; }

api/OpenAI.netstandard2.0.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3792,13 +3792,12 @@ public class RealtimeClient {
37923792
public virtual Task<ClientResult> CreateEphemeralTokenAsync(BinaryContent content, RequestOptions options = null);
37933793
public virtual ClientResult CreateEphemeralTranscriptionToken(BinaryContent content, RequestOptions options = null);
37943794
public virtual Task<ClientResult> CreateEphemeralTranscriptionTokenAsync(BinaryContent content, RequestOptions options = null);
3795-
public RealtimeSession StartConversationSession(string model, CancellationToken cancellationToken = default);
3796-
public virtual Task<RealtimeSession> StartConversationSessionAsync(string model, RequestOptions options);
3797-
public virtual Task<RealtimeSession> StartConversationSessionAsync(string model, CancellationToken cancellationToken = default);
3798-
public virtual Task<RealtimeSession> StartSessionAsync(string model, string intent, RequestOptions options);
3799-
public RealtimeSession StartTranscriptionSession(CancellationToken cancellationToken = default);
3800-
public virtual Task<RealtimeSession> StartTranscriptionSessionAsync(RequestOptions options);
3801-
public virtual Task<RealtimeSession> StartTranscriptionSessionAsync(CancellationToken cancellationToken = default);
3795+
public RealtimeSession StartConversationSession(string model, RealtimeSessionOptions options = null, CancellationToken cancellationToken = default);
3796+
public virtual Task<RealtimeSession> StartConversationSessionAsync(string model, RealtimeSessionOptions options = null, CancellationToken cancellationToken = default);
3797+
public RealtimeSession StartSession(string model, string intent, RealtimeSessionOptions options = null, CancellationToken cancellationToken = default);
3798+
public virtual Task<RealtimeSession> StartSessionAsync(string model, string intent, RealtimeSessionOptions options = null, CancellationToken cancellationToken = default);
3799+
public RealtimeSession StartTranscriptionSession(RealtimeSessionOptions options = null, CancellationToken cancellationToken = default);
3800+
public virtual Task<RealtimeSession> StartTranscriptionSessionAsync(RealtimeSessionOptions options = null, CancellationToken cancellationToken = default);
38023801
}
38033802
[Flags]
38043803
public enum RealtimeContentModalities {
@@ -3834,7 +3833,7 @@ public class RealtimeItem : IJsonModel<RealtimeItem>, IPersistableModel<Realtime
38343833
protected virtual BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options);
38353834
}
38363835
public class RealtimeSession : IDisposable {
3837-
protected internal RealtimeSession(RealtimeClient parentClient, Uri endpoint, ApiKeyCredential credential);
3836+
protected internal RealtimeSession(ApiKeyCredential credential, RealtimeClient parentClient, Uri endpoint, string model, string intent);
38383837
public Net.WebSockets.WebSocket WebSocket { get; protected set; }
38393838
public virtual void AddItem(RealtimeItem item, string previousItemId, CancellationToken cancellationToken = default);
38403839
public virtual void AddItem(RealtimeItem item, CancellationToken cancellationToken = default);
@@ -3850,8 +3849,8 @@ public class RealtimeSession : IDisposable {
38503849
public virtual void ConfigureSession(ConversationSessionOptions sessionOptions, CancellationToken cancellationToken = default);
38513850
public virtual void ConfigureTranscriptionSession(TranscriptionSessionOptions sessionOptions, CancellationToken cancellationToken = default);
38523851
public virtual Task ConfigureTranscriptionSessionAsync(TranscriptionSessionOptions sessionOptions, CancellationToken cancellationToken = default);
3853-
protected internal virtual void Connect(RequestOptions options);
3854-
protected internal virtual Task ConnectAsync(RequestOptions options);
3852+
protected internal virtual void Connect(IDictionary<string, string> headers = null, CancellationToken cancellationToken = default);
3853+
protected internal virtual Task ConnectAsync(IDictionary<string, string> headers = null, CancellationToken cancellationToken = default);
38553854
public virtual void DeleteItem(string itemId, CancellationToken cancellationToken = default);
38563855
public virtual Task DeleteItemAsync(string itemId, CancellationToken cancellationToken = default);
38573856
public void Dispose();
@@ -3876,6 +3875,10 @@ public class RealtimeSession : IDisposable {
38763875
public virtual void TruncateItem(string itemId, int contentPartIndex, TimeSpan audioDuration, CancellationToken cancellationToken = default);
38773876
public virtual Task TruncateItemAsync(string itemId, int contentPartIndex, TimeSpan audioDuration, CancellationToken cancellationToken = default);
38783877
}
3878+
public class RealtimeSessionOptions {
3879+
public Uri Endpoint { get; set; }
3880+
public IDictionary<string, string> Headers { get; }
3881+
}
38793882
public class RealtimeUpdate : IJsonModel<RealtimeUpdate>, IPersistableModel<RealtimeUpdate> {
38803883
public string EventId { get; }
38813884
public RealtimeUpdateKind Kind { get; }
Lines changed: 74 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.ClientModel;
33
using System.ClientModel.Primitives;
4+
using System.Threading;
45
using System.Threading.Tasks;
56

67
namespace OpenAI.Realtime;
@@ -10,43 +11,80 @@ namespace OpenAI.Realtime;
1011
[CodeGenSuppress("CreateStartRealtimeSessionRequest", typeof(BinaryContent), typeof(RequestOptions))]
1112
public 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, options.Endpoint ?? _webSocketEndpoint, model, intent);
84+
4785
try
4886
{
49-
await provisionalSession.ConnectAsync(options).ConfigureAwait(false);
87+
await provisionalSession.ConnectAsync(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

Comments
 (0)