feat: add support for file source and url source

This commit is contained in:
Stevan Freeborn
2025-07-14 23:30:02 -05:00
parent 267972ca94
commit 1f83899eb8
10 changed files with 469 additions and 27 deletions
@@ -16,6 +16,8 @@ class SourceConverter : JsonConverter<Source>
{
SourceType.Text => JsonSerializer.Deserialize<TextSource>(root.GetRawText(), options)!,
SourceType.Content => JsonSerializer.Deserialize<CustomSource>(root.GetRawText(), options)!,
SourceType.File => JsonSerializer.Deserialize<FileSource>(root.GetRawText(), options)!,
SourceType.Url => JsonSerializer.Deserialize<UrlSource>(root.GetRawText(), options)!,
SourceType.Base64 => DeserializeBase64Source(root, options),
_ => throw new JsonException($"Unknown source type: {type}")
};
@@ -54,6 +56,18 @@ class SourceConverter : JsonConverter<Source>
return;
}
if (value is FileSource fileSource)
{
JsonSerializer.Serialize(writer, fileSource, options);
return;
}
if (value is UrlSource urlSource)
{
JsonSerializer.Serialize(writer, urlSource, options);
return;
}
JsonSerializer.Serialize(writer, value, value.GetType(), options);
}
}
+33
View File
@@ -0,0 +1,33 @@
using System.Text.Json.Serialization;
namespace AnthropicClient.Models;
/// <summary>
/// Represents a file source in the Anthropic API.
/// </summary>
public class FileSource : Source
{
/// <summary>
/// Gets or sets the unique identifier for the file source.
/// </summary>
[JsonPropertyName("file_id")]
public string Id { get; init; } = string.Empty;
/// <summary>
/// Initializes a new instance of the <see cref="FileSource"/> class.
/// </summary>
/// <returns>A new instance of <see cref="FileSource"/> with the type set to "file".</returns>
public FileSource() : base(SourceType.File)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="FileSource"/> class with a specified file ID.
/// </summary>
/// <param name="id">The unique identifier for the file source.</param>
/// <returns>A new instance of <see cref="FileSource"/>.</returns>
public FileSource(string id) : base(SourceType.File)
{
Id = id;
}
}
@@ -53,4 +53,32 @@ public class ImageContent : Content
Source = new ImageSource(mediaType, data);
}
/// <summary>
/// Initializes a new instance of the <see cref="ImageContent"/> class.
/// </summary>
/// <param name="source">The source of the image.</param>
/// <returns>A new instance of the <see cref="ImageContent"/> class.</returns>
/// <exception cref="ArgumentNullException">Thrown when the source is null.</exception>
public ImageContent(Source source) : base(ContentType.Image)
{
ArgumentValidator.ThrowIfNull(source, nameof(source));
Source = source;
}
/// <summary>
/// Initializes a new instance of the <see cref="ImageContent"/> class.
/// </summary>
/// <param name="source">The source of the image.</param>
/// <param name="cacheControl">The cache control to be used for the content.</param>
/// <returns>A new instance of the <see cref="ImageContent"/> class.</returns>
/// <exception cref="ArgumentNullException">Thrown when the source or cache control is null.</exception>
public ImageContent(Source source, CacheControl cacheControl) : base(ContentType.Image, cacheControl)
{
ArgumentValidator.ThrowIfNull(source, nameof(source));
ArgumentValidator.ThrowIfNull(cacheControl, nameof(cacheControl));
Source = source;
}
}
+10
View File
@@ -19,4 +19,14 @@ public static class SourceType
/// The text document source type.
/// </summary>
public const string Text = "text";
/// <summary>
/// The file document source type.
/// </summary>
public const string File = "file";
/// <summary>
/// The URL document source type.
/// </summary>
public const string Url = "url";
}
+30
View File
@@ -0,0 +1,30 @@
namespace AnthropicClient.Models;
/// <summary>
/// Represents a URL source in the Anthropic API.
/// </summary>
public class UrlSource : Source
{
/// <summary>
/// Gets or sets the URL of the source document.
/// </summary>
public string Url { get; init; } = string.Empty;
/// <summary>
/// Initializes a new instance of the <see cref="UrlSource"/> class.
/// </summary>
/// <returns>A new instance of <see cref="UrlSource"/> with the type set to "url".</returns>
public UrlSource() : base(SourceType.Url)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="UrlSource"/> class with a specified URL.
/// </summary>
/// <param name="url">The URL of the source document.</param>
/// <returns>A new instance of <see cref="UrlSource"/>.</returns>
public UrlSource(string url) : base(SourceType.Url)
{
Url = url;
}
}