Skip to content

Commit 5e830f4

Browse files
authored
.NET: Only use the output from the last message for structured output (#6499)
* Only use the output from the last message for structured output * Address PR comments * Address PR comment * Address PR comments
1 parent 1acd242 commit 5e830f4

1 file changed

Lines changed: 30 additions & 7 deletions

File tree

dotnet/samples/02-agents/Harness/Harness_Shared_Console/Observers/PlanningOutputObserver.cs

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public sealed class PlanningOutputObserver : ConsoleObserver
2121
private readonly string _planModeName;
2222
private readonly string _executionModeName;
2323
private readonly IReadOnlyDictionary<string, ConsoleColor>? _modeColors;
24+
private string? _lastResponseId;
25+
private string? _lastMessageId;
2426

2527
/// <summary>
2628
/// Initializes a new instance of the <see cref="PlanningOutputObserver"/> class.
@@ -47,17 +49,38 @@ public override void ConfigureRunOptions(AgentRunOptions options, AIAgent agent,
4749
}
4850

4951
/// <inheritdoc/>
50-
public override Task OnTextAsync(IUXStateDriver ux, string text, AIAgent agent, AgentSession session)
52+
public override async Task OnResponseUpdateAsync(IUXStateDriver ux, AgentResponseUpdate update, AIAgent agent, AgentSession session)
5153
{
52-
if (this.IsPlanningMode(ux.CurrentMode))
54+
// We aren't in planning mode, so we can just stream the output directly.
55+
if (!this.IsPlanningMode(ux.CurrentMode))
56+
{
57+
if (!string.IsNullOrWhiteSpace(update.Text))
58+
{
59+
await ux.WriteTextAsync(update.Text).ConfigureAwait(false);
60+
}
61+
62+
return;
63+
}
64+
65+
// We are still accumulating the same response/message.
66+
if (this._lastResponseId == update.ResponseId && this._lastMessageId == update.MessageId)
5367
{
54-
// Planning mode: collect text silently for JSON parsing after the stream.
55-
this._textCollector.Append(text);
56-
return Task.CompletedTask;
68+
this._textCollector.Append(update.Text);
69+
return;
5770
}
5871

59-
// Execution mode: stream text directly to the console.
60-
return ux.WriteTextAsync(text);
72+
// New response/message, write the previous response/message and
73+
// clear the text collector for the next JSON response/message.
74+
string collectedText = this._textCollector.ToString();
75+
if (!string.IsNullOrWhiteSpace(collectedText))
76+
{
77+
await ux.WriteTextAsync(collectedText).ConfigureAwait(false);
78+
}
79+
80+
this._textCollector.Clear();
81+
this._textCollector.Append(update.Text);
82+
this._lastResponseId = update.ResponseId;
83+
this._lastMessageId = update.MessageId;
6184
}
6285

6386
/// <inheritdoc/>

0 commit comments

Comments
 (0)