Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenHodgson committed Nov 8, 2024
1 parent 6ca133b commit 670d84c
Show file tree
Hide file tree
Showing 41 changed files with 606 additions and 295 deletions.
3 changes: 2 additions & 1 deletion OpenAI/Packages/com.openai.unity/Runtime/OpenAIClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ protected override void ValidateAuthentication()
Converters = new List<JsonConverter>
{
new StringEnumConverter(new SnakeCaseNamingStrategy()),
new RealtimeServerEventConverter()
new RealtimeClientEventConverter(),
new RealtimeServerEventConverter(),
}
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using Newtonsoft.Json;
using UnityEngine.Scripting;

namespace OpenAI.Realtime
{
public abstract class BaseRealtimeEvent
[Preserve]
public abstract class BaseRealtimeEvent : IRealtimeEvent
{
public string ToJsonString() => JsonConvert.SerializeObject(this, OpenAIClient.JsonSerializationOptions);
/// <inheritdoc />
public abstract string EventId { get; internal set; }

/// <inheritdoc />
public abstract string Type { get; }

/// <inheritdoc />
[Preserve]
public virtual string ToJsonString()
=> JsonConvert.SerializeObject(this, OpenAIClient.JsonSerializationOptions);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public ConversationItem(RealtimeContent content)
/// The object type, must be "realtime.item".
/// </summary>
[Preserve]
[JsonProperty("object", DefaultValueHandling = DefaultValueHandling.Ignore)]
[JsonProperty("object")]
public string Object { get; private set; }

/// <summary>
Expand All @@ -71,14 +71,14 @@ public ConversationItem(RealtimeContent content)
/// The status of the item ("completed", "in_progress", "incomplete").
/// </summary>
[Preserve]
[JsonProperty("status", DefaultValueHandling = DefaultValueHandling.Ignore)]
[JsonProperty("status")]
public RealtimeResponseStatus Status { get; private set; }

/// <summary>
/// The role associated with the item ("user", "assistant", "system").
/// </summary>
[Preserve]
[JsonProperty("role", DefaultValueHandling = DefaultValueHandling.Ignore)]
[JsonProperty("role")]
public Role Role { get; private set; }

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ public ConversationItemCreateRequest(ConversationItem item, string previousItemI
Item = item;
}

/// <inheritdoc />
[Preserve]
[JsonProperty("event_id")]
public string EventId { get; }
public override string EventId { get; internal set; }

/// <inheritdoc />
[Preserve]
[JsonProperty("type")]
public string Type { get; } = "conversation.item.create";
public override string Type { get; } = "conversation.item.create";

/// <summary>
/// The ID of the preceding item after which the new item will be inserted.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,42 @@ namespace OpenAI.Realtime
[Preserve]
public sealed class ConversationItemCreatedResponse : BaseRealtimeEvent, IServerEvent
{
[Preserve]
[JsonConstructor]
internal ConversationItemCreatedResponse(
[JsonProperty("event_id")] string eventId,
[JsonProperty("type")] string type,
[JsonProperty("previous_item_id")] string previousItemId,
[JsonProperty("item")] ConversationItem item)
{
EventId = eventId;
Type = type;
PreviousItemId = previousItemId;
Item = item;
}

/// <inheritdoc />
[Preserve]
[JsonProperty("event_id")]
public string EventId { get; private set; }
public override string EventId { get; internal set; }

/// <summary>
/// The event type, must be "conversation.item.created".
/// </summary>
/// <inheritdoc />
[Preserve]
[JsonProperty("type")]
public string Type { get; private set; }
public override string Type { get; }

/// <summary>
/// The ID of the preceding item.
/// </summary>
[Preserve]
[JsonProperty("previous_item_id")]
public string PreviousItemId { get; private set; }
public string PreviousItemId { get; }

/// <summary>
/// The item that was created.
/// </summary>
[Preserve]
[JsonProperty("item")]
public ConversationItem Item { get; private set; }
public ConversationItem Item { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ public ConversationItemDeleteRequest(string itemId)
ItemId = itemId;
}

/// <inheritdoc />
[Preserve]
[JsonProperty("event_id")]
public string EventId { get; }
public override string EventId { get; internal set; }

/// <inheritdoc />
[Preserve]
[JsonProperty("type")]
public string Type { get; } = "conversation.item.delete";
public override string Type { get; } = "conversation.item.delete";

/// <summary>
/// The ID of the item to delete.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,33 @@ namespace OpenAI.Realtime
[Preserve]
public sealed class ConversationItemDeletedResponse : BaseRealtimeEvent, IServerEvent
{
[Preserve]
[JsonConstructor]
internal ConversationItemDeletedResponse(
[JsonProperty("event_id")] string eventId,
[JsonProperty("type")] string type,
[JsonProperty("item_id")] string itemId)
{
EventId = eventId;
Type = type;
ItemId = itemId;
}

/// <inheritdoc />
[Preserve]
[JsonProperty("event_id")]
public string EventId { get; private set; }
public override string EventId { get; internal set; }

/// <summary>
/// The event type, must be "conversation.item.deleted".
/// </summary>
/// <inheritdoc />
[Preserve]
[JsonProperty("type")]
public string Type { get; private set; }
public override string Type { get; }

/// <summary>
/// The ID of the item that was deleted.
/// </summary>
[Preserve]
[JsonProperty("item_id")]
public string ItemId { get; private set; }
public string ItemId { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,59 @@ namespace OpenAI.Realtime
[Preserve]
public sealed class ConversationItemInputAudioTranscriptionResponse : BaseRealtimeEvent, IServerEvent
{
[Preserve]
[JsonConstructor]
internal ConversationItemInputAudioTranscriptionResponse(
[JsonProperty("event_id")] string eventId,
[JsonProperty("type")] string type,
[JsonProperty("item_id")] string itemId,
[JsonProperty("content_index")] int contentIndex,
[JsonProperty("transcription")] string transcription,
[JsonProperty("error")] Error error)
{
EventId = eventId;
Type = type;
ItemId = itemId;
ContentIndex = contentIndex;
Transcription = transcription;
Error = error;
}

/// <inheritdoc />
[Preserve]
[JsonProperty("event_id")]
public string EventId { get; private set; }
public override string EventId { get; internal set; }

/// <summary>
/// "conversation.item.input_audio_transcription.completed" or "conversation.item.input_audio_transcription.failed"
/// </summary>
/// <inheritdoc />
[Preserve]
[JsonProperty("type")]
public string Type { get; private set; }
public override string Type { get; }

/// <summary>
/// The ID of the user message item.
/// </summary>
[Preserve]
[JsonProperty("item_id")]
public string ItemId { get; private set; }
public string ItemId { get; }

/// <summary>
/// The index of the content part containing the audio.
/// </summary>
[Preserve]
[JsonProperty("content_index")]
public int ContentIndex { get; private set; }
public int ContentIndex { get; }

/// <summary>
/// The transcribed text.
/// </summary>
[Preserve]
[JsonProperty("transcription")]
public string Transcription { get; private set; }
public string Transcription { get; }

/// <summary>
/// Details of the transcription error.
/// </summary>
[Preserve]
public Error Error { get; private set; }
public Error Error { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ public ConversationItemTruncateRequest(string itemId, int contentIndex, int audi
AudioEndMs = audioEndMs;
}

/// <inheritdoc />
[Preserve]
[JsonProperty("event_id")]
public string EventId { get; }
public override string EventId { get; internal set; }

/// <inheritdoc />
[Preserve]
[JsonProperty("type")]
public string Type { get; } = "conversation.item.truncate";
public override string Type { get; } = "conversation.item.truncate";

/// <summary>
/// The ID of the assistant message item to truncate. Only assistant message items can be truncated.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,51 @@ namespace OpenAI.Realtime
[Preserve]
public sealed class ConversationItemTruncatedResponse : BaseRealtimeEvent, IServerEvent
{
[Preserve]
[JsonConstructor]
internal ConversationItemTruncatedResponse(
[JsonProperty("event_id")] string eventId,
[JsonProperty("type")] string type,
[JsonProperty("item_id")] string itemId,
[JsonProperty("content_index")] int contentIndex,
[JsonProperty("audio_end_ms")] int audioEndMs)
{
EventId = eventId;
Type = type;
ItemId = itemId;
ContentIndex = contentIndex;
AudioEndMs = audioEndMs;
}

/// <inheritdoc />
[Preserve]
[JsonProperty("event_id")]
public string EventId { get; private set; }
public override string EventId { get; internal set; }

/// <summary>
/// The event type, must be "conversation.item.truncated".
/// </summary>
/// <inheritdoc />
[Preserve]
[JsonProperty("type")]
public string Type { get; private set; }
public override string Type { get; }

/// <summary>
/// The ID of the assistant message item that was truncated.
/// </summary>
[Preserve]
[JsonProperty("item_id")]
public string ItemId { get; private set; }
public string ItemId { get; }

/// <summary>
/// The index of the content part that was truncated.
/// </summary>
[Preserve]
[JsonProperty("content_index")]
public int ContentIndex { get; private set; }
public int ContentIndex { get; }

/// <summary>
/// The duration up to which the audio was truncated, in milliseconds.
/// </summary>
[Preserve]
[JsonProperty("audio_end_ms")]
public int AudioEndMs { get; private set; }
public int AudioEndMs { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public interface IRealtimeEvent
[JsonProperty("type")]
public string Type { get; }

[Preserve]
public string ToJsonString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ public InputAudioBufferAppendRequest(byte[] audioBytes)
Audio = System.Convert.ToBase64String(audioBytes);
}

/// <inheritdoc />
[Preserve]
[JsonProperty("event_id")]
public string EventId { get; }
public override string EventId { get; internal set; }

/// <inheritdoc />
[Preserve]
[JsonProperty("type")]
public string Type { get; } = "input_audio_buffer.append";
public override string Type { get; } = "input_audio_buffer.append";

/// <summary>
/// Base64-encoded audio bytes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ namespace OpenAI.Realtime
[Preserve]
public sealed class InputAudioBufferClearRequest : BaseRealtimeEvent, IClientEvent
{
/// <inheritdoc />
[Preserve]
[JsonProperty("event_id")]
public string EventId { get; }
public override string EventId { get; internal set; }

/// <inheritdoc />
[Preserve]
[JsonProperty("type")]
public string Type { get; } = "input_audio_buffer.clear";
public override string Type { get; } = "input_audio_buffer.clear";
}
}
Loading

0 comments on commit 670d84c

Please sign in to comment.