修复 AnthropicMessageInput 中对 Content/Contents 的判空处理: - 当 Contents 为 null 或 Count==0 时返回 "_",以兼容客户端对空值的特殊处理。 - 修正对 Content 的判空逻辑,使用 !string.IsNullOrEmpty(...) 确保非空字符串优先返回,避免将空字符串当作有效内容。
58 lines
1.6 KiB
C#
58 lines
1.6 KiB
C#
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<IList<AnthropicMessageContent>>(value?.ToString(),ThorJsonSerializer.DefaultOptions);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
Content = value?.ToString();
|
||
}
|
||
}
|
||
}
|
||
|
||
[JsonIgnore]
|
||
public IList<AnthropicMessageContent>? Contents;
|
||
} |