feat: add models for streaming messages
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
using System.Diagnostics;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
using AnthropicClient.Models;
|
||||
|
||||
namespace AnthropicClient.Json;
|
||||
|
||||
class ContentDeltaConverter : JsonConverter<ContentDelta>
|
||||
{
|
||||
public override ContentDelta Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
using var jsonDocument = JsonDocument.ParseValue(ref reader);
|
||||
var root = jsonDocument.RootElement;
|
||||
var type = root.GetProperty("type").GetString();
|
||||
return type switch
|
||||
{
|
||||
ContentDeltaType.TextDelta => JsonSerializer.Deserialize<TextDelta>(root.GetRawText(), options)!,
|
||||
ContentDeltaType.JsonDelta => JsonSerializer.Deserialize<JsonDelta>(root.GetRawText(), options)!,
|
||||
_ => throw new JsonException($"Unknown content type: {type}")
|
||||
};
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, ContentDelta value, JsonSerializerOptions options)
|
||||
{
|
||||
JsonSerializer.Serialize(writer, value, value.GetType(), options);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
using System.Diagnostics;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
using AnthropicClient.Models;
|
||||
|
||||
namespace AnthropicClient.Json;
|
||||
|
||||
class EventDataConverter : JsonConverter<EventData>
|
||||
{
|
||||
public override EventData Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
using var jsonDocument = JsonDocument.ParseValue(ref reader);
|
||||
var root = jsonDocument.RootElement;
|
||||
var type = root.GetProperty("type").GetString();
|
||||
return type switch
|
||||
{
|
||||
EventType.Ping => JsonSerializer.Deserialize<PingEventData>(root.GetRawText(), options)!,
|
||||
EventType.Error => JsonSerializer.Deserialize<ErrorEventData>(root.GetRawText(), options)!,
|
||||
EventType.MessageStart => JsonSerializer.Deserialize<MessageStartEventData>(root.GetRawText(), options)!,
|
||||
EventType.MessageDelta => JsonSerializer.Deserialize<MessageDeltaEventData>(root.GetRawText(), options)!,
|
||||
EventType.MessageStop => JsonSerializer.Deserialize<MessageStopEventData>(root.GetRawText(), options)!,
|
||||
EventType.ContentBlockStart => JsonSerializer.Deserialize<ContentStartEventData>(root.GetRawText(), options)!,
|
||||
EventType.ContentBlockDelta => JsonSerializer.Deserialize<ContentDeltaEventData>(root.GetRawText(), options)!,
|
||||
EventType.ContentBlockStop => JsonSerializer.Deserialize<ContentStopEventData>(root.GetRawText(), options)!,
|
||||
_ => throw new JsonException($"Unknown content type: {type}")
|
||||
};
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, EventData value, JsonSerializerOptions options)
|
||||
{
|
||||
JsonSerializer.Serialize(writer, value, value.GetType(), options);
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,8 @@ static class JsonSerializationOptions
|
||||
new ContentConverter(),
|
||||
new ToolChoiceConverter(),
|
||||
new ErrorConverter(),
|
||||
new EventDataConverter(),
|
||||
new ContentDeltaConverter(),
|
||||
},
|
||||
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user