| 1 | using System.Text.Json; |
| 2 | using System.Text.Json.Serialization; |
| 3 | |
| 4 | namespace AgentClientProtocol; |
| 5 | |
| 6 | [JsonConverter(typeof(SessionUpdateJsonConverter))] |
| 7 | public abstract record SessionUpdate |
| 8 | { |
| 9 | [JsonPropertyName("sessionUpdate")] |
| 10 | public abstract string Update { get; } |
| 11 | } |
| 12 | |
| 13 | public class SessionUpdateJsonConverter : JsonConverter<SessionUpdate> |
| 14 | { |
| 15 | public override SessionUpdate? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| 16 | { |
| 17 | using var doc = JsonDocument.ParseValue(ref reader); |
| 18 | var root = doc.RootElement; |
| 19 | |
| 20 | if (!root.TryGetProperty("sessionUpdate", out var sessionUpdateProperty)) |
| 21 | { |
| 22 | throw new JsonException("Missing 'sessionUpdate' property in SessionUpdate"); |
| 23 | } |
| 24 | |
| 25 | var type = sessionUpdateProperty.GetString(); |
| 26 | return type switch |
| 27 | { |
| 28 | "user_message_chunk" => root.Deserialize<UserMessageChunkSessionUpdate>(options), |
| 29 | "agent_message_chunk" => root.Deserialize<AgentMessageChunkSessionUpdate>(options), |
| 30 | "agent_thought_chunk" => root.Deserialize<AgentThoughtChunkSessionUpdate>(options), |
| 31 | "tool_call" => root.Deserialize<ToolCallSessionUpdate>(options), |
| 32 | "tool_call_update" => root.Deserialize<ToolCallUpdateSessionUpdate>(options), |
| 33 | "plan" => root.Deserialize<PlanSessionUpdate>(options), |
| 34 | "available_commands_update" => root.Deserialize<AvailableCommandsUpdateSessionUpdate>(options), |
| 35 | "current_mode_update" => root.Deserialize<CurrentModeUpdateSessionUpdate>(options), |
| 36 | _ => throw new JsonException($"Unknown SessionUpdate type: {type}") |
| 37 | }; |
| 38 | } |
| 39 | |
| 40 | public override void Write(Utf8JsonWriter writer, SessionUpdate value, JsonSerializerOptions options) |
| 41 | { |
| 42 | JsonSerializer.Serialize(writer, value, value.GetType(), options); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | public record UserMessageChunkSessionUpdate : SessionUpdate |
| 47 | { |
| 48 | [JsonPropertyName("sessionUpdate")] |
| 49 | public override string Update => "user_message_chunk"; |
| 50 | |
| 51 | [JsonPropertyName("content")] |
| 52 | public required ContentBlock Content { get; init; } |
| 53 | } |
| 54 | |
| 55 | public record AgentMessageChunkSessionUpdate : SessionUpdate |
| 56 | { |
| 57 | [JsonPropertyName("sessionUpdate")] |
| 58 | public override string Update => "agent_message_chunk"; |
| 59 | |
| 60 | [JsonPropertyName("content")] |
| 61 | public required ContentBlock Content { get; init; } |
| 62 | } |
| 63 | |
| 64 | public record AgentThoughtChunkSessionUpdate : SessionUpdate |
| 65 | { |
| 66 | [JsonPropertyName("sessionUpdate")] |
| 67 | public override string Update => "agent_thought_chunk"; |
| 68 | |
| 69 | [JsonPropertyName("content")] |
| 70 | public required ContentBlock Content { get; init; } |
| 71 | } |
| 72 | |
| 73 | public record ToolCallSessionUpdate : SessionUpdate |
| 74 | { |
| 75 | [JsonPropertyName("sessionUpdate")] |
| 76 | public override string Update => "tool_call"; |
| 77 | |
| 78 | [JsonPropertyName("toolCallId")] |
| 79 | public required string ToolCallId { get; init; } |
| 80 | |
| 81 | [JsonPropertyName("title")] |
| 82 | public required string Title { get; init; } |
| 83 | |
| 84 | [JsonPropertyName("content")] |
| 85 | public ToolCallContent[] Content { get; init; } = []; |
| 86 | |
| 87 | [JsonPropertyName("kind")] |
| 88 | public ToolKind Kind { get; init; } |
| 89 | |
| 90 | [JsonPropertyName("locations")] |
| 91 | public ToolCallLocation[] Locations { get; init; } = []; |
| 92 | |
| 93 | [JsonPropertyName("rawInput")] |
| 94 | public JsonElement? RawInput { get; init; } |
| 95 | |
| 96 | [JsonPropertyName("rawOutput")] |
| 97 | public JsonElement? RawOutput { get; init; } |
| 98 | |
| 99 | [JsonPropertyName("status")] |
| 100 | public ToolCallStatus Status { get; init; } |
| 101 | } |
| 102 | |
| 103 | public record ToolCallUpdateSessionUpdate : SessionUpdate |
| 104 | { |
| 105 | [JsonPropertyName("sessionUpdate")] |
| 106 | public override string Update => "tool_call_update"; |
| 107 | |
| 108 | [JsonPropertyName("toolCallId")] |
| 109 | public required string ToolCallId { get; init; } |
| 110 | |
| 111 | [JsonPropertyName("content")] |
| 112 | public ToolCallContent[]? Content { get; init; } |
| 113 | |
| 114 | [JsonPropertyName("kind")] |
| 115 | public ToolKind? Kind { get; init; } |
| 116 | |
| 117 | [JsonPropertyName("locations")] |
| 118 | public ToolCallLocation[]? Locations { get; init; } |
| 119 | |
| 120 | [JsonPropertyName("rawInput")] |
| 121 | public JsonElement? RawInput { get; init; } |
| 122 | |
| 123 | [JsonPropertyName("rawOutput")] |
| 124 | public JsonElement? RawOutput { get; init; } |
| 125 | |
| 126 | [JsonPropertyName("status")] |
| 127 | public ToolCallStatus? Status { get; init; } |
| 128 | |
| 129 | [JsonPropertyName("title")] |
| 130 | public string? Title { get; init; } |
| 131 | } |
| 132 | |
| 133 | public record PlanSessionUpdate : SessionUpdate |
| 134 | { |
| 135 | [JsonPropertyName("sessionUpdate")] |
| 136 | public override string Update => "plan"; |
| 137 | |
| 138 | [JsonPropertyName("entries")] |
| 139 | public required PlanEntry[] Entries { get; init; } |
| 140 | } |
| 141 | |
| 142 | public record AvailableCommandsUpdateSessionUpdate : SessionUpdate |
| 143 | { |
| 144 | [JsonPropertyName("sessionUpdate")] |
| 145 | public override string Update => "available_commands_update"; |
| 146 | |
| 147 | [JsonPropertyName("availableCommands")] |
| 148 | public required AvailableCommand[] AvailableCommands { get; init; } |
| 149 | } |
| 150 | |
| 151 | public record CurrentModeUpdateSessionUpdate : SessionUpdate |
| 152 | { |
| 153 | [JsonPropertyName("sessionUpdate")] |
| 154 | public override string Update => "current_mode_update"; |
| 155 | |
| 156 | [JsonPropertyName("currentModeId")] |
| 157 | public required string CurrentModeId { get; init; } |
| 158 | } |
| 159 | |