feat: yield events for streaming including custom message complete event
This commit is contained in:
@@ -2,7 +2,7 @@ using System.Text.Json.Serialization;
|
||||
|
||||
namespace AnthropicClient.Models;
|
||||
|
||||
public record AnthropicEvent
|
||||
public class AnthropicEvent
|
||||
{
|
||||
public string Type { get; init; } = string.Empty;
|
||||
public EventData Data { get; init; } = default!;
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
using System.Net.Http.Headers;
|
||||
|
||||
namespace AnthropicClient.Models;
|
||||
|
||||
/// <summary>
|
||||
/// Represents headers included in Anthropic API responses.
|
||||
/// </summary>
|
||||
public class AnthropicHeaders
|
||||
{
|
||||
private const string RequestIdHeaderKey = "request-id";
|
||||
private const string RateLimitRequestsLimitHeaderKey = "anthropic-ratelimit-requests-limit";
|
||||
private const string RateLimitRequestsRemainingHeaderKey = "anthropic-ratelimit-requests-remaining";
|
||||
private const string RateLimitRequestsResetHeaderKey = "anthropic-ratelimit-requests-reset";
|
||||
private const string RateLimitTokensLimitHeaderKey = "anthropic-ratelimit-tokens-limit";
|
||||
private const string RateLimitTokensRemainingHeaderKey = "anthropic-ratelimit-tokens-remaining";
|
||||
private const string RateLimitTokensResetHeaderKey = "anthropic-ratelimit-tokens-reset";
|
||||
private const string RetryAfterHeaderKey = "retry-after";
|
||||
|
||||
/// <summary>
|
||||
/// Gets the request ID.
|
||||
/// </summary>
|
||||
public string RequestId { get; init; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the rate limit requests limit.
|
||||
/// </summary>
|
||||
public int RateLimitRequestsLimit { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the rate limit requests remaining.
|
||||
/// </summary>
|
||||
public int RateLimitRequestsRemaining { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the time of the rate limit requests reset.
|
||||
/// </summary>
|
||||
public DateTimeOffset RateLimitRequestsReset { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the rate limit tokens limit.
|
||||
/// </summary>
|
||||
public int RateLimitTokensLimit { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the rate limit tokens remaining.
|
||||
/// </summary>
|
||||
public int RateLimitTokensRemaining { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the time of the rate limit tokens reset.
|
||||
/// </summary>
|
||||
public DateTimeOffset RateLimitTokensReset { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the retry after value.
|
||||
/// </summary>
|
||||
public int RetryAfter { get; init; }
|
||||
|
||||
internal AnthropicHeaders()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="AnthropicHeaders"/> class.
|
||||
/// </summary>
|
||||
/// <param name="headers">The HTTP response headers.</param>
|
||||
/// <returns>A new instance of the <see cref="AnthropicHeaders"/> class.</returns>
|
||||
public AnthropicHeaders(HttpResponseHeaders headers)
|
||||
{
|
||||
RequestId = headers.GetValues(RequestIdHeaderKey).FirstOrDefault() ?? string.Empty;
|
||||
RateLimitRequestsLimit = int.Parse(headers.GetValues(RateLimitRequestsLimitHeaderKey).FirstOrDefault() ?? "0");
|
||||
RateLimitRequestsRemaining = int.Parse(headers.GetValues(RateLimitRequestsRemainingHeaderKey).FirstOrDefault() ?? "0");
|
||||
RateLimitRequestsReset = DateTimeOffset.Parse(headers.GetValues(RateLimitRequestsResetHeaderKey).FirstOrDefault() ?? DateTimeOffset.MinValue.ToString());
|
||||
RateLimitTokensLimit = int.Parse(headers.GetValues(RateLimitTokensLimitHeaderKey).FirstOrDefault() ?? "0");
|
||||
RateLimitTokensRemaining = int.Parse(headers.GetValues(RateLimitTokensRemainingHeaderKey).FirstOrDefault() ?? "0");
|
||||
RateLimitTokensReset = DateTimeOffset.Parse(headers.GetValues(RateLimitTokensResetHeaderKey).FirstOrDefault() ?? DateTimeOffset.MinValue.ToString());
|
||||
RetryAfter = int.Parse(headers.GetValues(RetryAfterHeaderKey).FirstOrDefault() ?? "0");
|
||||
}
|
||||
}
|
||||
@@ -24,7 +24,7 @@ public class AnthropicResult<T>
|
||||
/// <summary>
|
||||
/// The request ID of the operation.
|
||||
/// </summary>
|
||||
public string RequestId { get; }
|
||||
public AnthropicHeaders Headers { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="AnthropicResult{T}"/> class.
|
||||
@@ -32,35 +32,35 @@ public class AnthropicResult<T>
|
||||
/// <param name="value">The value of the result.</param>
|
||||
/// <param name="error">The error of the result.</param>
|
||||
/// <param name="isSuccess">Indicates whether the operation was successful.</param>
|
||||
/// <param name="requestId">The request ID of the operation.</param>
|
||||
/// <param name="headers">The Anthropic headers for the result.</param>
|
||||
/// <returns>A new instance of the <see cref="AnthropicResult{T}"/> class.</returns>
|
||||
protected AnthropicResult(T value, AnthropicError error, bool isSuccess, string requestId)
|
||||
protected AnthropicResult(T value, AnthropicError error, bool isSuccess, AnthropicHeaders headers)
|
||||
{
|
||||
Value = value;
|
||||
Error = error;
|
||||
IsSuccess = isSuccess;
|
||||
RequestId = requestId;
|
||||
Headers = headers;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a successful result.
|
||||
/// </summary>
|
||||
/// <param name="value">The value of the result.</param>
|
||||
/// <param name="requestId">The request ID of the operation.</param>
|
||||
/// <param name="headers">The Anthropic headers for the result.</param>
|
||||
/// <returns>A new instance of the <see cref="AnthropicResult{T}"/> class.</returns>
|
||||
public static AnthropicResult<T> Success(T value, string requestId)
|
||||
public static AnthropicResult<T> Success(T value, AnthropicHeaders headers)
|
||||
{
|
||||
return new AnthropicResult<T>(value, null!, true, requestId);
|
||||
return new AnthropicResult<T>(value, null!, true, headers);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a failed result.
|
||||
/// </summary>
|
||||
/// <param name="error">The error of the result.</param>
|
||||
/// <param name="requestId">The request ID of the operation.</param>
|
||||
/// <param name="headers">The Anthropic headers for the result.</param>
|
||||
/// <returns>A new instance of the <see cref="AnthropicResult{T}"/> class.</returns>
|
||||
public static AnthropicResult<T> Failure(AnthropicError error, string requestId)
|
||||
public static AnthropicResult<T> Failure(AnthropicError error, AnthropicHeaders headers)
|
||||
{
|
||||
return new AnthropicResult<T>(default!, error, false, requestId);
|
||||
return new AnthropicResult<T>(default!, error, false, headers);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ public class ContentStartEventData : EventData
|
||||
public int Index { get; init; }
|
||||
|
||||
[JsonPropertyName("content_block")]
|
||||
public Content ContentBlock { get; init; }
|
||||
public Content ContentBlock { get; init; } = default!;
|
||||
|
||||
[JsonConstructor]
|
||||
internal ContentStartEventData() : base(EventType.ContentBlockStart)
|
||||
|
||||
@@ -7,6 +7,7 @@ public static class EventType
|
||||
public const string MessageStart = "message_start";
|
||||
public const string MessageDelta = "message_delta";
|
||||
public const string MessageStop = "message_stop";
|
||||
public const string MessageComplete = "message_complete";
|
||||
public const string ContentBlockStart = "content_block_start";
|
||||
public const string ContentBlockDelta = "content_block_delta";
|
||||
public const string ContentBlockStop = "content_block_stop";
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace AnthropicClient.Models;
|
||||
|
||||
public class MessageCompleteEventData : EventData
|
||||
{
|
||||
public AnthropicHeaders Headers { get; init; } = new();
|
||||
public ChatResponse Message { get; init; } = new();
|
||||
|
||||
public MessageCompleteEventData(ChatResponse message, AnthropicHeaders headers) : base(EventType.MessageComplete)
|
||||
{
|
||||
Message = message;
|
||||
Headers = headers;
|
||||
}
|
||||
}
|
||||
@@ -4,8 +4,8 @@ namespace AnthropicClient.Models;
|
||||
|
||||
public class MessageDeltaEventData : EventData
|
||||
{
|
||||
public MessageDelta Delta { get; init; }
|
||||
public ChatUsage Usage { get; init; }
|
||||
public MessageDelta Delta { get; init; } = new();
|
||||
public ChatUsage Usage { get; init; } = new();
|
||||
|
||||
[JsonConstructor]
|
||||
internal MessageDeltaEventData() : base(EventType.MessageDelta)
|
||||
|
||||
@@ -4,14 +4,14 @@ namespace AnthropicClient.Models;
|
||||
|
||||
public class MessageStartEventData : EventData
|
||||
{
|
||||
public ChatMessage Message { get; init; } = new();
|
||||
public ChatResponse Message { get; init; } = new();
|
||||
|
||||
[JsonConstructor]
|
||||
internal MessageStartEventData() : base(EventType.MessageStart)
|
||||
{
|
||||
}
|
||||
|
||||
public MessageStartEventData(ChatMessage message) : base(EventType.MessageStart)
|
||||
public MessageStartEventData(ChatResponse message) : base(EventType.MessageStart)
|
||||
{
|
||||
Message = message;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user