// Copyright (c) Microsoft. All rights reserved.
using System.Text.Json.Serialization;
namespace Microsoft.Agents.AI.DurableTask.State;
///
/// Represents a durable agent state entry that is a response from the agent.
///
internal sealed class DurableAgentStateResponse : DurableAgentStateEntry
{
///
/// Gets the usage details for this state response.
///
[JsonPropertyName("usage")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public DurableAgentStateUsage? Usage { get; init; }
///
/// Creates a from an .
///
/// The correlation ID linking this response to its request.
/// The to convert.
/// A representing the original response.
public static DurableAgentStateResponse FromResponse(string correlationId, AgentResponse response)
{
return new DurableAgentStateResponse()
{
CorrelationId = correlationId,
CreatedAt = response.CreatedAt ?? response.Messages.Max(m => m.CreatedAt) ?? DateTimeOffset.UtcNow,
Messages = response.Messages.Select(DurableAgentStateMessage.FromChatMessage).ToList(),
Usage = DurableAgentStateUsage.FromUsage(response.Usage)
};
}
///
/// Converts this back to an .
///
/// A representing this response.
public AgentResponse ToResponse()
{
return new AgentResponse()
{
CreatedAt = this.CreatedAt,
Messages = this.Messages.Select(m => m.ToChatMessage()).ToList(),
Usage = this.Usage?.ToUsageDetails(),
};
}
}