diff --git a/dotnet/src/Agents/Core/ChatCompletionAgent.cs b/dotnet/src/Agents/Core/ChatCompletionAgent.cs index 56c8712ab50f..1975027630ba 100644 --- a/dotnet/src/Agents/Core/ChatCompletionAgent.cs +++ b/dotnet/src/Agents/Core/ChatCompletionAgent.cs @@ -391,6 +391,7 @@ private async IAsyncEnumerable InternalInvokeStream this.Logger.LogAgentChatServiceInvokedStreamingAgent(nameof(InvokeAsync), this.Id, agentName, serviceType); + int messageIndex = messageCount; AuthorRole? role = null; StringBuilder builder = new(); await foreach (StreamingChatMessageContent message in messages.ConfigureAwait(false)) @@ -401,18 +402,18 @@ private async IAsyncEnumerable InternalInvokeStream builder.Append(message.ToString()); - yield return message; - } + // Capture mutated messages related function calling / tools + for (; messageIndex < chat.Count; messageIndex++) + { + ChatMessageContent chatMessage = chat[messageIndex]; - // Capture mutated messages related function calling / tools - for (int messageIndex = messageCount; messageIndex < chat.Count; messageIndex++) - { - ChatMessageContent message = chat[messageIndex]; + chatMessage.AuthorName = this.Name; - message.AuthorName = this.Name; + await onNewMessage(chatMessage).ConfigureAwait(false); + history.Add(chatMessage); + } - await onNewMessage(message).ConfigureAwait(false); - history.Add(message); + yield return message; } // Do not duplicate terminated function result to history