diff --git a/dotnet/samples/02-agents/Harness/Harness_Shared_Console/Observers/PlanningOutputObserver.cs b/dotnet/samples/02-agents/Harness/Harness_Shared_Console/Observers/PlanningOutputObserver.cs index 153c11e755..bf534c7d6e 100644 --- a/dotnet/samples/02-agents/Harness/Harness_Shared_Console/Observers/PlanningOutputObserver.cs +++ b/dotnet/samples/02-agents/Harness/Harness_Shared_Console/Observers/PlanningOutputObserver.cs @@ -21,6 +21,8 @@ public sealed class PlanningOutputObserver : ConsoleObserver private readonly string _planModeName; private readonly string _executionModeName; private readonly IReadOnlyDictionary? _modeColors; + private string? _lastResponseId; + private string? _lastMessageId; /// /// Initializes a new instance of the class. @@ -47,17 +49,38 @@ public sealed class PlanningOutputObserver : ConsoleObserver } /// - public override Task OnTextAsync(IUXStateDriver ux, string text, AIAgent agent, AgentSession session) + public override async Task OnResponseUpdateAsync(IUXStateDriver ux, AgentResponseUpdate update, AIAgent agent, AgentSession session) { - if (this.IsPlanningMode(ux.CurrentMode)) + // We aren't in planning mode, so we can just stream the output directly. + if (!this.IsPlanningMode(ux.CurrentMode)) { - // Planning mode: collect text silently for JSON parsing after the stream. - this._textCollector.Append(text); - return Task.CompletedTask; + if (!string.IsNullOrWhiteSpace(update.Text)) + { + await ux.WriteTextAsync(update.Text).ConfigureAwait(false); + } + + return; } - // Execution mode: stream text directly to the console. - return ux.WriteTextAsync(text); + // We are still accumulating the same response/message. + if (this._lastResponseId == update.ResponseId && this._lastMessageId == update.MessageId) + { + this._textCollector.Append(update.Text); + return; + } + + // New response/message, write the previous response/message and + // clear the text collector for the next JSON response/message. + string collectedText = this._textCollector.ToString(); + if (!string.IsNullOrWhiteSpace(collectedText)) + { + await ux.WriteTextAsync(collectedText).ConfigureAwait(false); + } + + this._textCollector.Clear(); + this._textCollector.Append(update.Text); + this._lastResponseId = update.ResponseId; + this._lastMessageId = update.MessageId; } ///