feat: implement GetMessageBatchResultsAsync method
This commit is contained in:
@@ -34,14 +34,21 @@ public interface IAnthropicApiClient
|
||||
/// <param name="request">The message batch request to create.</param>
|
||||
/// <returns>A task that represents the asynchronous operation. The task result contains the response as an <see cref="AnthropicResult{T}"/> where T is <see cref="MessageBatchResponse"/>.</returns>
|
||||
Task<AnthropicResult<MessageBatchResponse>> CreateMessageBatchAsync(MessageBatchRequest request);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Gets a message batch asynchronously.
|
||||
/// </summary>
|
||||
/// <param name="batchId">The ID of the message batch to get.</param>
|
||||
/// <returns>A task that represents the asynchronous operation. The task result contains the response as an <see cref="AnthropicResult{T}"/> where T is <see cref="MessageBatchResponse"/>.</returns>
|
||||
Task<AnthropicResult<MessageBatchResponse>> GetMessageBatchAsync(string batchId);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Gets the results of a message batch asynchronously.
|
||||
/// </summary>
|
||||
/// <param name="batchId">The ID of the message batch to get the results for.</param>
|
||||
/// <returns>A task that represents the asynchronous operation. The task result contains the response as an <see cref="AnthropicResult{T}"/> where T is <see cref="IAsyncEnumerable{T}"/> where T is <see cref="MessageBatchResultItem"/>.</returns>
|
||||
Task<AnthropicResult<IAsyncEnumerable<MessageBatchResultItem>>> GetMessageBatchResultsAsync(string batchId);
|
||||
|
||||
/// <summary>
|
||||
/// Counts the tokens in a message asynchronously.
|
||||
/// </summary>
|
||||
@@ -325,17 +332,49 @@ public class AnthropicApiClient : IAnthropicApiClient
|
||||
var response = await SendRequestAsync($"{MessageBatchesEndpoint}/{batchId}");
|
||||
var anthropicHeaders = new AnthropicHeaders(response.Headers);
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
|
||||
|
||||
if (response.IsSuccessStatusCode is false)
|
||||
{
|
||||
var error = Deserialize<AnthropicError>(responseContent) ?? new AnthropicError();
|
||||
return AnthropicResult<MessageBatchResponse>.Failure(error, anthropicHeaders);
|
||||
}
|
||||
|
||||
|
||||
var msgBatchResponse = Deserialize<MessageBatchResponse>(responseContent) ?? new MessageBatchResponse();
|
||||
return AnthropicResult<MessageBatchResponse>.Success(msgBatchResponse, anthropicHeaders);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task<AnthropicResult<IAsyncEnumerable<MessageBatchResultItem>>> GetMessageBatchResultsAsync(string batchId)
|
||||
{
|
||||
var response = await SendRequestAsync($"{MessageBatchesEndpoint}/{batchId}/results");
|
||||
var anthropicHeaders = new AnthropicHeaders(response.Headers);
|
||||
|
||||
if (response.IsSuccessStatusCode is false)
|
||||
{
|
||||
var content = await response.Content.ReadAsStringAsync();
|
||||
var error = Deserialize<AnthropicError>(content) ?? new AnthropicError();
|
||||
return AnthropicResult<IAsyncEnumerable<MessageBatchResultItem>>.Failure(error, anthropicHeaders);
|
||||
}
|
||||
|
||||
async IAsyncEnumerable<MessageBatchResultItem> ReadResults()
|
||||
{
|
||||
using var responseContent = await response.Content.ReadAsStreamAsync();
|
||||
using var streamReader = new StreamReader(responseContent);
|
||||
|
||||
var line = await streamReader.ReadLineAsync();
|
||||
|
||||
while (line is not null)
|
||||
{
|
||||
var resultItem = Deserialize<MessageBatchResultItem>(line) ?? new MessageBatchResultItem();
|
||||
yield return resultItem;
|
||||
|
||||
line = await streamReader.ReadLineAsync();
|
||||
}
|
||||
}
|
||||
|
||||
return AnthropicResult<IAsyncEnumerable<MessageBatchResultItem>>.Success(ReadResults(), anthropicHeaders);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task<AnthropicResult<TokenCountResponse>> CountMessageTokensAsync(CountMessageTokensRequest request)
|
||||
{
|
||||
|
||||
@@ -17,6 +17,7 @@ static class JsonSerializationOptions
|
||||
new EventDataConverter(),
|
||||
new ContentDeltaConverter(),
|
||||
new JsonStringEnumConverter(),
|
||||
new MessageBatchResultConverter(),
|
||||
},
|
||||
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
|
||||
};
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
using AnthropicClient.Models;
|
||||
|
||||
namespace AnthropicClient.Json;
|
||||
|
||||
class MessageBatchResultConverter : JsonConverter<MessageBatchResult>
|
||||
{
|
||||
public override MessageBatchResult 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
|
||||
{
|
||||
MessageBatchResultType.Succeeded => JsonSerializer.Deserialize<SucceededMessageBatchResult>(root.GetRawText(), options)!,
|
||||
MessageBatchResultType.Errored => JsonSerializer.Deserialize<ErroredMessageBatchResult>(root.GetRawText(), options)!,
|
||||
MessageBatchResultType.Canceled => JsonSerializer.Deserialize<CanceledMessageBatchResult>(root.GetRawText(), options)!,
|
||||
MessageBatchResultType.Expired => JsonSerializer.Deserialize<ExpiredMessageBatchResult>(root.GetRawText(), options)!,
|
||||
_ => throw new JsonException($"Unknown message batch result type: {type}")
|
||||
};
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, MessageBatchResult value, JsonSerializerOptions options)
|
||||
{
|
||||
JsonSerializer.Serialize(writer, value, value.GetType(), options);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
namespace AnthropicClient.Models;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a message batch result that was cancelled.
|
||||
/// </summary>
|
||||
public class CanceledMessageBatchResult : MessageBatchResult
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="CanceledMessageBatchResult"/> class.
|
||||
/// </summary>
|
||||
public CanceledMessageBatchResult() : base(MessageBatchResultType.Canceled)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
namespace AnthropicClient.Models;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a message batch result that contains an error response.
|
||||
/// </summary>
|
||||
public class ErroredMessageBatchResult : MessageBatchResult
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the error of the message batch result.
|
||||
/// </summary>
|
||||
public AnthropicError Error { get; init; } = new AnthropicError();
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ErroredMessageBatchResult"/> class.
|
||||
/// </summary>
|
||||
public ErroredMessageBatchResult() : base(MessageBatchResultType.Errored)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
namespace AnthropicClient.Models;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a message batch result that has expired.
|
||||
/// </summary>
|
||||
public class ExpiredMessageBatchResult : MessageBatchResult
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ExpiredMessageBatchResult"/> class.
|
||||
/// </summary>
|
||||
public ExpiredMessageBatchResult() : base(MessageBatchResultType.Expired)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using AnthropicClient.Utils;
|
||||
|
||||
namespace AnthropicClient.Models;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a message batch result.
|
||||
/// </summary>
|
||||
public abstract class MessageBatchResult
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the type of the message batch result.
|
||||
/// </summary>
|
||||
public string Type { get; init; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="MessageBatchResult"/> class.
|
||||
/// </summary>
|
||||
/// <param name="type">The type of the message batch result.</param>
|
||||
/// <returns>An instance of the <see cref="MessageBatchResult"/> class.</returns>
|
||||
public MessageBatchResult(string type)
|
||||
{
|
||||
ArgumentValidator.ThrowIfNullOrWhitespace(type, nameof(type));
|
||||
|
||||
Type = type;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace AnthropicClient.Models;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a message batch result item.
|
||||
/// </summary>
|
||||
public class MessageBatchResultItem
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the custom ID of the message batch result item.
|
||||
/// </summary>
|
||||
[JsonPropertyName("custom_id")]
|
||||
public string CustomId { get; init; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the result of the message batch result item.
|
||||
/// </summary>
|
||||
public MessageBatchResult Result { get; init; } = default!;
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
namespace AnthropicClient.Models;
|
||||
|
||||
/// <summary>
|
||||
/// Represents the types of message batch results.
|
||||
/// </summary>
|
||||
public static class MessageBatchResultType
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a succeeded message batch result.
|
||||
/// </summary>
|
||||
public const string Succeeded = "succeeded";
|
||||
|
||||
/// <summary>
|
||||
/// Represents an errored message batch result.
|
||||
/// </summary>
|
||||
public const string Errored = "errored";
|
||||
|
||||
/// <summary>
|
||||
/// Represents a canceled message batch result.
|
||||
/// </summary>
|
||||
public const string Canceled = "canceled";
|
||||
|
||||
/// <summary>
|
||||
/// Represents an expired message batch result.
|
||||
/// </summary>
|
||||
public const string Expired = "expired";
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
namespace AnthropicClient.Models;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a message batch result that contains a message response.
|
||||
/// </summary>
|
||||
public class SucceededMessageBatchResult : MessageBatchResult
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the message of the message batch result.
|
||||
/// </summary>
|
||||
public MessageResponse Message { get; init; } = new MessageResponse();
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="SucceededMessageBatchResult"/> class.
|
||||
/// </summary>
|
||||
/// <returns>An instance of the <see cref="SucceededMessageBatchResult"/> class.</returns>
|
||||
public SucceededMessageBatchResult() : base(MessageBatchResultType.Succeeded)
|
||||
{
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user