| 1 | using System.Text.Json; |
| 2 | using System.Text.Json.Serialization; |
| 3 | |
| 4 | namespace AgentClientProtocol; |
| 5 | |
| 6 | [JsonConverter(typeof(ContentBlockJsonConverter))] |
| 7 | public abstract record ContentBlock |
| 8 | { |
| 9 | [JsonPropertyName("type")] |
| 10 | public abstract string Type { get; } |
| 11 | |
| 12 | [JsonPropertyName("_meta")] |
| 13 | public JsonElement? Meta { get; init; } |
| 14 | |
| 15 | [JsonPropertyName("annotations")] |
| 16 | public Annotations? Annotations { get; init; } |
| 17 | } |
| 18 | |
| 19 | public record TextContentBlock : ContentBlock |
| 20 | { |
| 21 | [JsonPropertyName("type")] |
| 22 | public override string Type => "text"; |
| 23 | |
| 24 | [JsonPropertyName("text")] |
| 25 | public required string Text { get; init; } |
| 26 | } |
| 27 | |
| 28 | public record ImageContentBlock : ContentBlock |
| 29 | { |
| 30 | [JsonPropertyName("type")] |
| 31 | public override string Type => "image"; |
| 32 | |
| 33 | [JsonPropertyName("data")] |
| 34 | public required string Data { get; init; } |
| 35 | |
| 36 | [JsonPropertyName("mimeType")] |
| 37 | public required string MimeType { get; init; } |
| 38 | |
| 39 | [JsonPropertyName("uri")] |
| 40 | public string? Uri { get; init; } |
| 41 | } |
| 42 | |
| 43 | public record AudioContentBlock : ContentBlock |
| 44 | { |
| 45 | [JsonPropertyName("type")] |
| 46 | public override string Type => "audio"; |
| 47 | |
| 48 | [JsonPropertyName("data")] |
| 49 | public required string Data { get; init; } |
| 50 | |
| 51 | [JsonPropertyName("mimeType")] |
| 52 | public required string MimeType { get; init; } |
| 53 | } |
| 54 | |
| 55 | public record ResourceLinkContentBlock : ContentBlock |
| 56 | { |
| 57 | [JsonPropertyName("type")] |
| 58 | public override string Type => "resource_link"; |
| 59 | |
| 60 | [JsonPropertyName("name")] |
| 61 | public required string Name { get; init; } |
| 62 | |
| 63 | [JsonPropertyName("uri")] |
| 64 | public required string Uri { get; init; } |
| 65 | |
| 66 | [JsonPropertyName("description")] |
| 67 | public string? Description { get; init; } |
| 68 | |
| 69 | [JsonPropertyName("mimeType")] |
| 70 | public string? MimeType { get; init; } |
| 71 | |
| 72 | [JsonPropertyName("size")] |
| 73 | public long? Size { get; init; } |
| 74 | |
| 75 | [JsonPropertyName("title")] |
| 76 | public string? Title { get; init; } |
| 77 | } |
| 78 | |
| 79 | public record ResourceContentBlock : ContentBlock |
| 80 | { |
| 81 | [JsonPropertyName("type")] |
| 82 | public override string Type => "resource"; |
| 83 | |
| 84 | [JsonPropertyName("resource")] |
| 85 | public required EmbeddedResourceResource Resource { get; init; } |
| 86 | } |
| 87 | |
| 88 | public class ContentBlockJsonConverter : JsonConverter<ContentBlock> |
| 89 | { |
| 90 | public override ContentBlock? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| 91 | { |
| 92 | using var doc = JsonDocument.ParseValue(ref reader); |
| 93 | var root = doc.RootElement; |
| 94 | |
| 95 | if (!root.TryGetProperty("type", out var typeProperty)) |
| 96 | { |
| 97 | throw new JsonException("Missing 'type' property in ContentBlock"); |
| 98 | } |
| 99 | |
| 100 | var type = typeProperty.GetString(); |
| 101 | return type switch |
| 102 | { |
| 103 | "text" => root.Deserialize<TextContentBlock>(options), |
| 104 | "image" => root.Deserialize<ImageContentBlock>(options), |
| 105 | "audio" => root.Deserialize<AudioContentBlock>(options), |
| 106 | "resource_link" => root.Deserialize<ResourceLinkContentBlock>(options), |
| 107 | "resource" => root.Deserialize<ResourceContentBlock>(options), |
| 108 | _ => throw new JsonException($"Unknown ContentBlock type: {type}") |
| 109 | }; |
| 110 | } |
| 111 | |
| 112 | public override void Write(Utf8JsonWriter writer, ContentBlock value, JsonSerializerOptions options) |
| 113 | { |
| 114 | JsonSerializer.Serialize(writer, value, value.GetType(), options); |
| 115 | } |
| 116 | } |