using System.ComponentModel.DataAnnotations; using System.Text.Json; using System.Text.Json.Serialization; namespace Yi.Framework.AiHub.Domain.Shared.Dtos.Anthropic; public class AnthropicMessageInput { [JsonPropertyName("role")] public string Role { get; set; } [JsonIgnore] public string? Content; [JsonPropertyName("content")] public object? ContentCalculated { get { if (Content is not null && Contents is not null) { throw new ValidationException("Messages 中 Content 和 Contents 字段不能同时有值"); } if (!string.IsNullOrEmpty(Content)) { return Content; } // 如果 Contents 为空或 null,返回空字符串而不是 null if (Contents == null || Contents.Count == 0) { return "_"; // 兼容客户端空值问题 } return Contents!; } set { if (value is JsonElement str) { if (str.ValueKind == JsonValueKind.String) { Content = value?.ToString(); } else if (str.ValueKind == JsonValueKind.Array) { Contents = JsonSerializer.Deserialize>(value?.ToString(),ThorJsonSerializer.DefaultOptions); } } else { Content = value?.ToString(); } } } [JsonIgnore] public IList? Contents; }