|
| 1 | +using System; |
| 2 | +using Newtonsoft.Json; |
| 3 | +using Newtonsoft.Json.Linq; |
| 4 | + |
| 5 | +namespace LlmTornado.Responses; |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// Represents the conversation that a response belongs to. |
| 9 | +/// Can be a plain string (conversation ID) or an object with an <c>id</c> field. |
| 10 | +/// </summary> |
| 11 | +[JsonConverter(typeof(ResponseConversationJsonConverter))] |
| 12 | +public interface IResponseConversation |
| 13 | +{ |
| 14 | + /// <summary> |
| 15 | + /// The unique ID of the conversation. |
| 16 | + /// </summary> |
| 17 | + string Id { get; } |
| 18 | +} |
| 19 | + |
| 20 | +/// <summary> |
| 21 | +/// String-form conversation reference — serializes as a plain string. |
| 22 | +/// </summary> |
| 23 | +public class StringResponseConversation : IResponseConversation |
| 24 | +{ |
| 25 | + /// <inheritdoc/> |
| 26 | + public string Id { get; set; } |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// Creates a new string conversation reference. |
| 30 | + /// </summary> |
| 31 | + public StringResponseConversation(string id) |
| 32 | + { |
| 33 | + Id = id; |
| 34 | + } |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Creates an empty string conversation reference. |
| 38 | + /// </summary> |
| 39 | + public StringResponseConversation() |
| 40 | + { |
| 41 | + Id = string.Empty; |
| 42 | + } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Implicit conversion from string. |
| 46 | + /// </summary> |
| 47 | + public static implicit operator StringResponseConversation(string id) => new(id); |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// Implicit conversion to string. |
| 51 | + /// </summary> |
| 52 | + public static implicit operator string(StringResponseConversation conversation) => conversation.Id; |
| 53 | +} |
| 54 | + |
| 55 | +/// <summary> |
| 56 | +/// Object-form conversation reference — serializes as <c>{ "id": "..." }</c>. |
| 57 | +/// This is the form returned in API responses. |
| 58 | +/// </summary> |
| 59 | +public class ObjectResponseConversation : IResponseConversation |
| 60 | +{ |
| 61 | + /// <inheritdoc/> |
| 62 | + [JsonProperty("id")] |
| 63 | + public string Id { get; set; } |
| 64 | + |
| 65 | + /// <summary> |
| 66 | + /// Creates a new object conversation reference. |
| 67 | + /// </summary> |
| 68 | + public ObjectResponseConversation(string id) |
| 69 | + { |
| 70 | + Id = id; |
| 71 | + } |
| 72 | + |
| 73 | + /// <summary> |
| 74 | + /// Creates an empty object conversation reference. |
| 75 | + /// </summary> |
| 76 | + public ObjectResponseConversation() |
| 77 | + { |
| 78 | + Id = string.Empty; |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +/// <summary> |
| 83 | +/// Handles serialization and deserialization of <see cref="IResponseConversation"/>. |
| 84 | +/// Writes <see cref="StringResponseConversation"/> as a plain JSON string, |
| 85 | +/// <see cref="ObjectResponseConversation"/> as <c>{ "id": "..." }</c>. |
| 86 | +/// Reads a JSON string token as <see cref="StringResponseConversation"/>, |
| 87 | +/// a JSON object token as <see cref="ObjectResponseConversation"/>. |
| 88 | +/// </summary> |
| 89 | +internal class ResponseConversationJsonConverter : JsonConverter<IResponseConversation> |
| 90 | +{ |
| 91 | + public override void WriteJson(JsonWriter writer, IResponseConversation? value, JsonSerializer serializer) |
| 92 | + { |
| 93 | + if (value is null) |
| 94 | + { |
| 95 | + writer.WriteNull(); |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + switch (value) |
| 100 | + { |
| 101 | + case StringResponseConversation str: |
| 102 | + writer.WriteValue(str.Id); |
| 103 | + break; |
| 104 | + case ObjectResponseConversation obj: |
| 105 | + writer.WriteStartObject(); |
| 106 | + writer.WritePropertyName("id"); |
| 107 | + writer.WriteValue(obj.Id); |
| 108 | + writer.WriteEndObject(); |
| 109 | + break; |
| 110 | + default: |
| 111 | + throw new JsonSerializationException($"Unexpected conversation type: {value.GetType()}"); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + public override IResponseConversation? ReadJson(JsonReader reader, Type objectType, IResponseConversation? existingValue, bool hasExistingValue, JsonSerializer serializer) |
| 116 | + { |
| 117 | + if (reader.TokenType is JsonToken.Null) |
| 118 | + { |
| 119 | + return null; |
| 120 | + } |
| 121 | + |
| 122 | + if (reader.TokenType is JsonToken.String) |
| 123 | + { |
| 124 | + string? id = reader.Value?.ToString(); |
| 125 | + return id is not null ? new StringResponseConversation(id) : null; |
| 126 | + } |
| 127 | + |
| 128 | + if (reader.TokenType is JsonToken.StartObject) |
| 129 | + { |
| 130 | + JObject jo = JObject.Load(reader); |
| 131 | + string? id = jo["id"]?.ToString(); |
| 132 | + return id is not null ? new ObjectResponseConversation(id) : null; |
| 133 | + } |
| 134 | + |
| 135 | + throw new JsonSerializationException($"Unexpected token type for conversation: {reader.TokenType}"); |
| 136 | + } |
| 137 | + |
| 138 | + public override bool CanRead => true; |
| 139 | + public override bool CanWrite => true; |
| 140 | +} |
0 commit comments