Skip to content

Commit

Permalink
fix: Use RealtimeServerEvent for Realtime api.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed Oct 24, 2024
1 parent 4ade9b9 commit 837b253
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/libs/OpenAI/RealtimeConversationClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ partial void Authorized(
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async IAsyncEnumerable<RealtimeServerEventBase> ReceiveUpdatesAsync(
public async IAsyncEnumerable<RealtimeServerEvent> ReceiveUpdatesAsync(
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
if (!IsConnected)
Expand Down Expand Up @@ -53,7 +53,7 @@ public async IAsyncEnumerable<RealtimeServerEventBase> ReceiveUpdatesAsync(
if (result.MessageType == WebSocketMessageType.Text)
{
string json = global::System.Text.Encoding.UTF8.GetString(buffer, 0, result.Count);
var @event = global::OpenAI.RealtimeServerEventBase.FromJson(json, JsonSerializerContext) ??
var @event = global::OpenAI.RealtimeServerEvent.FromJson(json, JsonSerializerContext) ??
throw new global::System.InvalidOperationException($"Response deserialization failed for \"{json}\" ");

yield return @event;
Expand Down
27 changes: 25 additions & 2 deletions src/tests/OpenAI.IntegrationTests/Tests.Realtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,32 @@ await realtimeConversationClient.SendAsync(new RealtimeResponseCreate
}
}.ToJson(realtimeConversationClient.JsonSerializerContext), cancellationToken);

await foreach (RealtimeServerEventBase receivedMessage in realtimeConversationClient.ReceiveUpdatesAsync(cancellationToken))
await foreach (RealtimeServerEvent receivedMessage in realtimeConversationClient.ReceiveUpdatesAsync(cancellationToken))
{
Console.WriteLine(value: "Message received. Type:" + receivedMessage.Type);
if (receivedMessage.IsSessionCreated)
{
Console.WriteLine($"Session created. ID: {receivedMessage.SessionCreated.EventId}");
Console.WriteLine(receivedMessage.SessionCreated.Session?.ToJson(realtimeConversationClient.JsonSerializerContext));
}
else if (receivedMessage.IsResponseCreated)
{
Console.WriteLine($"Response created. ID: {receivedMessage.ResponseCreated.EventId}");
Console.WriteLine(receivedMessage.ResponseCreated.Response?.ToJson(realtimeConversationClient.JsonSerializerContext));
}
else if (receivedMessage.IsRateLimitsUpdated)
{
Console.WriteLine($"Response updated. ID: {receivedMessage.RateLimitsUpdated.EventId}");
Console.WriteLine(receivedMessage.RateLimitsUpdated.ToJson(realtimeConversationClient.JsonSerializerContext));
}
else if (receivedMessage.IsError)
{
Console.WriteLine($"Error. ID: {receivedMessage.Error.EventId}");
Console.WriteLine(receivedMessage.Error.Error?.ToJson(realtimeConversationClient.JsonSerializerContext));
}
else
{
Console.WriteLine($"Unknown message: {receivedMessage.ToJson(realtimeConversationClient.JsonSerializerContext)}");
}
}
}
}

0 comments on commit 837b253

Please sign in to comment.