From 2101d9d36d113bb5e0ac2ccff14719e54088f5a3 Mon Sep 17 00:00:00 2001 From: westey <164392973+westey-m@users.noreply.github.com> Date: Fri, 31 Oct 2025 14:12:20 +0000 Subject: [PATCH] Don't stamp unknown author name on output messages. (#1830) --- .../Microsoft.Agents.AI/ChatClient/ChatClientAgent.cs | 8 ++++---- .../ChatClient/ChatClientAgentTests.cs | 10 ++++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgent.cs b/dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgent.cs index 5ae6e59904..46f893e531 100644 --- a/dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgent.cs +++ b/dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgent.cs @@ -353,9 +353,9 @@ public sealed partial class ChatClientAgent : AIAgent chatClient = ApplyRunOptionsTransformations(options, chatClient); - var agentName = this.GetLoggingAgentName(); + var loggingAgentName = this.GetLoggingAgentName(); - this._logger.LogAgentChatClientInvokingAgent(nameof(RunAsync), this.Id, agentName, this._chatClientType); + this._logger.LogAgentChatClientInvokingAgent(nameof(RunAsync), this.Id, loggingAgentName, this._chatClientType); // Call the IChatClient and notify the AIContextProvider of any failures. TChatClientResponse chatResponse; @@ -369,7 +369,7 @@ public sealed partial class ChatClientAgent : AIAgent throw; } - this._logger.LogAgentChatClientInvokedAgent(nameof(RunAsync), this.Id, agentName, this._chatClientType, inputMessages.Count); + this._logger.LogAgentChatClientInvokedAgent(nameof(RunAsync), this.Id, loggingAgentName, this._chatClientType, inputMessages.Count); // We can derive the type of supported thread from whether we have a conversation id, // so let's update it and set the conversation id for the service thread case. @@ -378,7 +378,7 @@ public sealed partial class ChatClientAgent : AIAgent // Ensure that the author name is set for each message in the response. foreach (ChatMessage chatResponseMessage in chatResponse.Messages) { - chatResponseMessage.AuthorName ??= agentName; + chatResponseMessage.AuthorName ??= this.Name; } // Only notify the thread of new messages if the chatResponse was successful to avoid inconsistent message state in the thread. diff --git a/dotnet/tests/Microsoft.Agents.AI.UnitTests/ChatClient/ChatClientAgentTests.cs b/dotnet/tests/Microsoft.Agents.AI.UnitTests/ChatClient/ChatClientAgentTests.cs index 7c90e1a13e..862b9ef3b4 100644 --- a/dotnet/tests/Microsoft.Agents.AI.UnitTests/ChatClient/ChatClientAgentTests.cs +++ b/dotnet/tests/Microsoft.Agents.AI.UnitTests/ChatClient/ChatClientAgentTests.cs @@ -194,8 +194,10 @@ public partial class ChatClientAgentTests /// /// Verify that RunAsync sets AuthorName on all response messages. /// - [Fact] - public async Task RunAsyncSetsAuthorNameOnAllResponseMessagesAsync() + [Theory] + [InlineData("TestAgent")] + [InlineData(null)] + public async Task RunAsyncSetsAuthorNameOnAllResponseMessagesAsync(string? authorName) { // Arrange Mock mockService = new(); @@ -210,13 +212,13 @@ public partial class ChatClientAgentTests It.IsAny(), It.IsAny())).ReturnsAsync(new ChatResponse(responseMessages)); - ChatClientAgent agent = new(mockService.Object, options: new() { Instructions = "test instructions", Name = "TestAgent" }); + ChatClientAgent agent = new(mockService.Object, options: new() { Instructions = "test instructions", Name = authorName }); // Act var result = await agent.RunAsync([new(ChatRole.User, "test")]); // Assert - Assert.All(result.Messages, msg => Assert.Equal("TestAgent", msg.AuthorName)); + Assert.All(result.Messages, msg => Assert.Equal(authorName, msg.AuthorName)); } ///