feat: add model for DocumentContent and DocumentSource

This commit is contained in:
Stevan Freeborn
2024-11-21 13:43:21 -06:00
parent c4d4d8a15b
commit f3a7040cb9
5 changed files with 274 additions and 0 deletions
@@ -16,6 +16,7 @@ class ContentConverter : JsonConverter<Content>
{
ContentType.Text => JsonSerializer.Deserialize<TextContent>(root.GetRawText(), options)!,
ContentType.Image => JsonSerializer.Deserialize<ImageContent>(root.GetRawText(), options)!,
ContentType.Document => JsonSerializer.Deserialize<DocumentContent>(root.GetRawText(), options)!,
ContentType.ToolUse => JsonSerializer.Deserialize<ToolUseContent>(root.GetRawText(), options)!,
ContentType.ToolResult => JsonSerializer.Deserialize<ToolResultContent>(root.GetRawText(), options)!,
_ => throw new JsonException($"Unknown content type: {type}")
@@ -0,0 +1,56 @@
using System.Text.Json.Serialization;
using AnthropicClient.Utils;
namespace AnthropicClient.Models;
/// <summary>
/// Represents content from a document that is part of a message.
/// </summary>
public class DocumentContent : Content
{
/// <summary>
/// Gets the source of the document.
/// </summary>
public DocumentSource Source { get; init; } = new();
[JsonConstructor]
internal DocumentContent()
{
}
private void Validate(string mediaType, string data)
{
ArgumentValidator.ThrowIfNull(mediaType, nameof(mediaType));
ArgumentValidator.ThrowIfNull(data, nameof(data));
}
/// <summary>
/// Initializes a new instance of the <see cref="DocumentContent"/> class.
/// </summary>
/// <param name="mediaType">The media type of the document.</param>
/// <param name="data">The data of the document.</param>
/// <exception cref="ArgumentNullException">Thrown when the media type or data is null.</exception>
/// <returns>A new instance of the <see cref="DocumentContent"/> class.</returns>
public DocumentContent(string mediaType, string data) : base(ContentType.Document)
{
Validate(mediaType, data);
Source = new(mediaType, data);
}
/// <summary>
/// Initializes a new instance of the <see cref="DocumentContent"/> class.
/// </summary>
/// <param name="mediaType">The media type of the document.</param>
/// <param name="data">The data of the document.</param>
/// <param name="cacheControl">The cache control to be used for the content.</param>
/// <returns>A new instance of the <see cref="DocumentContent"/> class.</returns>
/// <exception cref="ArgumentNullException">Thrown when the media type, data, or cache control is null.</exception>
public DocumentContent(string mediaType, string data, CacheControl cacheControl) : base(ContentType.Document, cacheControl)
{
Validate(mediaType, data);
Source = new(mediaType, data);
}
}
@@ -0,0 +1,49 @@
using System.Text.Json.Serialization;
using AnthropicClient.Utils;
namespace AnthropicClient.Models;
/// <summary>
/// Represents a document source.
/// </summary>
public class DocumentSource
{
/// <summary>
/// Gets the media type of the document.
/// </summary>
[JsonPropertyName("media_type")]
public string MediaType { get; init; } = string.Empty;
/// <summary>
/// Gets the data of the document.
/// </summary>
public string Data { get; init; } = string.Empty;
/// <summary>
/// Gets the type of encoding of the document data.
/// </summary>
public string Type { get; init; } = "base64";
[JsonConstructor]
internal DocumentSource()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="DocumentSource"/> class.
/// </summary>
/// <param name="mediaType">The media type of the document.</param>
/// <param name="data">The data of the document.</param>
/// <exception cref="ArgumentException">Thrown when the media type is invalid.</exception>
/// <exception cref="ArgumentNullException">Thrown when the media type or data is null.</exception>
/// <returns>A new instance of the <see cref="DocumentSource"/> class.</returns>
public DocumentSource(string mediaType, string data)
{
ArgumentValidator.ThrowIfNull(mediaType, nameof(mediaType));
ArgumentValidator.ThrowIfNull(data, nameof(data));
MediaType = mediaType;
Data = data;
}
}