From 3f87dcfb87c7b798b646b698b567655c4cb930e8 Mon Sep 17 00:00:00 2001 From: Chris Rickman Date: Thu, 27 Jun 2024 09:21:18 -0700 Subject: [PATCH 1/2] Fix --- .../Contents/ChatMessageContent.cs | 8 +++++++- .../Contents/StreamingChatMessageContent.cs | 7 ++++++- .../Contents/ChatMessageContentTests.cs | 19 +++++++++++++++++++ .../StreamingChatMessageContentTests.cs | 19 +++++++++++++++++++ 4 files changed, 51 insertions(+), 2 deletions(-) diff --git a/dotnet/src/SemanticKernel.Abstractions/Contents/ChatMessageContent.cs b/dotnet/src/SemanticKernel.Abstractions/Contents/ChatMessageContent.cs index 24ff3cf19438..33472674c5f4 100644 --- a/dotnet/src/SemanticKernel.Abstractions/Contents/ChatMessageContent.cs +++ b/dotnet/src/SemanticKernel.Abstractions/Contents/ChatMessageContent.cs @@ -20,7 +20,12 @@ public class ChatMessageContent : KernelContent /// [Experimental("SKEXP0001")] [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] - public string? AuthorName { get; set; } + public string? AuthorName + { + get => this._authorName; + set => this._authorName = string.IsNullOrWhiteSpace(value) ? null : value; + } + /// /// Role of the author of the message @@ -171,4 +176,5 @@ public override string ToString() private ChatMessageContentItemCollection? _items; private Encoding _encoding; + private string? _authorName; } diff --git a/dotnet/src/SemanticKernel.Abstractions/Contents/StreamingChatMessageContent.cs b/dotnet/src/SemanticKernel.Abstractions/Contents/StreamingChatMessageContent.cs index 5cc7afb582ed..cc2b0c354284 100644 --- a/dotnet/src/SemanticKernel.Abstractions/Contents/StreamingChatMessageContent.cs +++ b/dotnet/src/SemanticKernel.Abstractions/Contents/StreamingChatMessageContent.cs @@ -64,7 +64,11 @@ public StreamingKernelContentItemCollection Items /// [Experimental("SKEXP0001")] [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] - public string? AuthorName { get; set; } + public string? AuthorName + { + get => this._authorName; + set => this._authorName = string.IsNullOrWhiteSpace(value) ? null : value; + } /// /// Role of the author of the message @@ -126,4 +130,5 @@ public StreamingChatMessageContent(AuthorRole? role, string? content, object? in private StreamingKernelContentItemCollection? _items; private Encoding _encoding; + private string? _authorName; } diff --git a/dotnet/src/SemanticKernel.UnitTests/Contents/ChatMessageContentTests.cs b/dotnet/src/SemanticKernel.UnitTests/Contents/ChatMessageContentTests.cs index fdbd4cae0524..759fec2b532b 100644 --- a/dotnet/src/SemanticKernel.UnitTests/Contents/ChatMessageContentTests.cs +++ b/dotnet/src/SemanticKernel.UnitTests/Contents/ChatMessageContentTests.cs @@ -128,6 +128,25 @@ public void ContentPropertyGetterShouldReturnContentOfTheFirstTextContentItem() Assert.Equal("fake-content-1", sut.Content); } + [Theory] + [InlineData(null)] + [InlineData("")] + [InlineData(" ")] + [InlineData("\t")] + [InlineData("\n")] + [InlineData("\r\n")] + public void ContentPropertySetterShouldConvertEmptyOrWhitespaceAuthorNameToNull(string? authorName) + { + // Arrange + var message = new ChatMessageContent(AuthorRole.User, content: null) + { + AuthorName = authorName + }; + + // Assert + Assert.Null(message.AuthorName); + } + [Fact] public void ItShouldBePossibleToSetAndGetEncodingEvenIfThereAreNoItems() { diff --git a/dotnet/src/SemanticKernel.UnitTests/Contents/StreamingChatMessageContentTests.cs b/dotnet/src/SemanticKernel.UnitTests/Contents/StreamingChatMessageContentTests.cs index f7f7c5e43be7..8fcec3d5d91b 100644 --- a/dotnet/src/SemanticKernel.UnitTests/Contents/StreamingChatMessageContentTests.cs +++ b/dotnet/src/SemanticKernel.UnitTests/Contents/StreamingChatMessageContentTests.cs @@ -118,6 +118,25 @@ public void ContentPropertyGetterShouldReturnContentOfTheFirstTextContentItem() Assert.Equal("fake-content-1", sut.Content); } + [Theory] + [InlineData(null)] + [InlineData("")] + [InlineData(" ")] + [InlineData("\t")] + [InlineData("\n")] + [InlineData("\r\n")] + public void ContentPropertySetterShouldConvertEmptyOrWhitespaceAuthorNameToNull(string? authorName) + { + // Arrange + var message = new ChatMessageContent(AuthorRole.User, content: null) + { + AuthorName = authorName + }; + + // Assert + Assert.Null(message.AuthorName); + } + [Fact] public void ItShouldBePossibleToSetAndGetEncodingEvenIfThereAreNoItems() { From f7d310bbaa26276c7720c89fb651007b394961fc Mon Sep 17 00:00:00 2001 From: Chris Rickman Date: Thu, 27 Jun 2024 09:32:59 -0700 Subject: [PATCH 2/2] Update --- .../SemanticKernel.Abstractions/Contents/ChatMessageContent.cs | 1 - .../Contents/StreamingChatMessageContentTests.cs | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/dotnet/src/SemanticKernel.Abstractions/Contents/ChatMessageContent.cs b/dotnet/src/SemanticKernel.Abstractions/Contents/ChatMessageContent.cs index 33472674c5f4..0042cf5a2948 100644 --- a/dotnet/src/SemanticKernel.Abstractions/Contents/ChatMessageContent.cs +++ b/dotnet/src/SemanticKernel.Abstractions/Contents/ChatMessageContent.cs @@ -26,7 +26,6 @@ public string? AuthorName set => this._authorName = string.IsNullOrWhiteSpace(value) ? null : value; } - /// /// Role of the author of the message /// diff --git a/dotnet/src/SemanticKernel.UnitTests/Contents/StreamingChatMessageContentTests.cs b/dotnet/src/SemanticKernel.UnitTests/Contents/StreamingChatMessageContentTests.cs index 8fcec3d5d91b..d510e6e42eeb 100644 --- a/dotnet/src/SemanticKernel.UnitTests/Contents/StreamingChatMessageContentTests.cs +++ b/dotnet/src/SemanticKernel.UnitTests/Contents/StreamingChatMessageContentTests.cs @@ -128,7 +128,7 @@ public void ContentPropertyGetterShouldReturnContentOfTheFirstTextContentItem() public void ContentPropertySetterShouldConvertEmptyOrWhitespaceAuthorNameToNull(string? authorName) { // Arrange - var message = new ChatMessageContent(AuthorRole.User, content: null) + var message = new StreamingChatMessageContent(AuthorRole.User, content: null) { AuthorName = authorName };