// Copyright (c) Microsoft. All rights reserved.
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.Extensions.AI;
namespace Microsoft.Agents.AI.DurableTask.State;
///
/// Represents a single message within a durable agent state entry.
///
internal sealed class DurableAgentStateMessage
{
///
/// Gets the name of the author of this message.
///
[JsonPropertyName("authorName")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? AuthorName { get; init; }
///
/// Gets the timestamp when this message was created.
///
[JsonPropertyName("createdAt")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public DateTimeOffset? CreatedAt { get; init; }
///
/// Gets the contents of this message.
///
[JsonPropertyName("contents")]
public IReadOnlyList Contents { get; init; } = [];
///
/// Gets the role of the message sender (e.g., "user", "assistant", "system").
///
[JsonPropertyName("role")]
public required string Role { get; init; }
///
/// Gets any additional data found during deserialization that does not map to known properties.
///
[JsonExtensionData]
public IDictionary? ExtensionData { get; set; }
///
/// Creates a from a .
///
/// The to convert.
/// A representing the original message.
public static DurableAgentStateMessage FromChatMessage(ChatMessage message)
{
return new DurableAgentStateMessage()
{
CreatedAt = message.CreatedAt,
AuthorName = message.AuthorName,
Role = message.Role.ToString(),
Contents = message.Contents.Select(DurableAgentStateContent.FromAIContent).ToList()
};
}
///
/// Converts this to a .
///
/// A representing this message.
public ChatMessage ToChatMessage()
{
return new ChatMessage()
{
CreatedAt = this.CreatedAt,
AuthorName = this.AuthorName,
Contents = this.Contents.Select(c => c.ToAIContent()).ToList(),
Role = new(this.Role)
};
}
}