Skip to content

Commit 79b662a

Browse files
CopilotCopilot
andcommitted
Fix non-streaming response path for SessionConfig.Streaming=false (microsoft#4732)
The config-copy fix (preserving Streaming=false via null-coalescing) was already in place, but ConvertToAgentResponseUpdate(AssistantMessageEvent) always emitted raw AIContent without text—assuming delta events had already delivered it. When streaming is disabled there are no delta events, so the assistant's final text was silently dropped. Changes: - Add isStreaming parameter to ConvertToAgentResponseUpdate for AssistantMessageEvent so it emits TextContent in non-streaming mode. - Capture the resolved streaming flag in RunCoreStreamingAsync and pass it through the event subscription closure. - Add/update unit tests for both streaming and non-streaming paths. Co-authored-by: Copilot <[email protected]>
1 parent 6435671 commit 79b662a

2 files changed

Lines changed: 70 additions & 11 deletions

File tree

‎dotnet/src/Microsoft.Agents.AI.GitHub.Copilot/GitHubCopilotAgent.cs‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,12 @@ protected override async IAsyncEnumerable<AgentResponseUpdate> RunCoreStreamingA
145145
// Ensure the client is started
146146
await this.EnsureClientStartedAsync(cancellationToken).ConfigureAwait(false);
147147

148-
// Create or resume a session with streaming enabled
148+
// Create or resume a session with streaming enabled by default
149149
SessionConfig sessionConfig = this._sessionConfig != null
150150
? CopySessionConfig(this._sessionConfig)
151151
: new SessionConfig { Streaming = true };
152152

153+
bool isStreaming = sessionConfig.Streaming ?? true;
153154
CopilotSession copilotSession;
154155
if (typedSession.SessionId is not null)
155156
{
@@ -178,7 +179,7 @@ protected override async IAsyncEnumerable<AgentResponseUpdate> RunCoreStreamingA
178179
break;
179180

180181
case AssistantMessageEvent assistantMessage:
181-
channel.Writer.TryWrite(this.ConvertToAgentResponseUpdate(assistantMessage));
182+
channel.Writer.TryWrite(this.ConvertToAgentResponseUpdate(assistantMessage, isStreaming));
182183
break;
183184

184185
case AssistantUsageEvent usageEvent:
@@ -326,12 +327,18 @@ private AgentResponseUpdate ConvertToAgentResponseUpdate(AssistantMessageDeltaEv
326327
};
327328
}
328329

329-
internal AgentResponseUpdate ConvertToAgentResponseUpdate(AssistantMessageEvent assistantMessage)
330+
/// <summary>
331+
/// Converts an <see cref="AssistantMessageEvent"/> to an <see cref="AgentResponseUpdate"/>.
332+
/// When streaming is enabled, text was already delivered via delta events, so only raw metadata is emitted.
333+
/// When streaming is disabled, the full message text is emitted as <see cref="TextContent"/>.
334+
/// </summary>
335+
internal AgentResponseUpdate ConvertToAgentResponseUpdate(AssistantMessageEvent assistantMessage, bool isStreaming)
330336
{
331-
AIContent content = new()
332-
{
333-
RawRepresentation = assistantMessage
334-
};
337+
// When streaming, text was already delivered via AssistantMessageDeltaEvent.
338+
// When not streaming, this is the only opportunity to emit the response text.
339+
AIContent content = isStreaming
340+
? new AIContent { RawRepresentation = assistantMessage }
341+
: new TextContent(assistantMessage.Data?.Content ?? string.Empty) { RawRepresentation = assistantMessage };
335342

336343
return new AgentResponseUpdate(ChatRole.Assistant, [content])
337344
{

‎dotnet/tests/Microsoft.Agents.AI.GitHub.Copilot.UnitTests/GitHubCopilotAgentTests.cs‎

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
using System;
44
using System.Collections.Generic;
5+
using System.Linq;
56
using System.Threading.Tasks;
67
using GitHub.Copilot;
78
using GitHub.Copilot.Rpc;
@@ -288,7 +289,7 @@ public void CopyResumeSessionConfig_WithStreamingNull_DefaultsToTrue()
288289
}
289290

290291
[Fact]
291-
public void ConvertToAgentResponseUpdate_AssistantMessageEvent_DoesNotEmitTextContent()
292+
public void ConvertToAgentResponseUpdate_AssistantMessageEventWhenStreaming_DoesNotEmitTextContent()
292293
{
293294
var assistantMessage = new AssistantMessageEvent
294295
{
@@ -301,11 +302,62 @@ public void ConvertToAgentResponseUpdate_AssistantMessageEvent_DoesNotEmitTextCo
301302
CopilotClient copilotClient = new(new CopilotClientOptions());
302303
const string TestId = "agent-id";
303304
var agent = new GitHubCopilotAgent(copilotClient, ownsClient: false, id: TestId, tools: null);
304-
AgentResponseUpdate result = agent.ConvertToAgentResponseUpdate(assistantMessage);
305+
AgentResponseUpdate result = agent.ConvertToAgentResponseUpdate(assistantMessage, isStreaming: true);
305306

306-
// result.Text need to be empty because the content was already delivered via delta events, and we want to avoid emitting duplicate content in the response update.
307-
// The content should be delivered through TextContent in the Contents collection instead.
307+
// result.Text should be empty because content was already delivered via delta events.
308308
Assert.Empty(result.Text);
309309
Assert.DoesNotContain(result.Contents, c => c is TextContent);
310310
}
311+
312+
[Fact]
313+
public void ConvertToAgentResponseUpdate_AssistantMessageEventWhenNotStreaming_EmitsTextContent()
314+
{
315+
// Arrange
316+
const string ExpectedContent = "Full response text from non-streaming session";
317+
var assistantMessage = new AssistantMessageEvent
318+
{
319+
Data = new AssistantMessageData
320+
{
321+
MessageId = "msg-789",
322+
Content = ExpectedContent
323+
}
324+
};
325+
CopilotClient copilotClient = new(new CopilotClientOptions());
326+
const string TestId = "agent-id";
327+
var agent = new GitHubCopilotAgent(copilotClient, ownsClient: false, id: TestId, tools: null);
328+
329+
// Act
330+
AgentResponseUpdate result = agent.ConvertToAgentResponseUpdate(assistantMessage, isStreaming: false);
331+
332+
// Assert - text must be emitted since no delta events precede it in non-streaming mode.
333+
Assert.Equal(ExpectedContent, result.Text);
334+
Assert.Contains(result.Contents, c => c is TextContent);
335+
TextContent textContent = (TextContent)result.Contents.Single(c => c is TextContent);
336+
Assert.Equal(ExpectedContent, textContent.Text);
337+
Assert.Same(assistantMessage, textContent.RawRepresentation);
338+
}
339+
340+
[Fact]
341+
public void ConvertToAgentResponseUpdate_AssistantMessageEventWhenNotStreaming_HandlesEmptyContent()
342+
{
343+
// Arrange
344+
var assistantMessage = new AssistantMessageEvent
345+
{
346+
Data = new AssistantMessageData
347+
{
348+
MessageId = "msg-000",
349+
Content = string.Empty
350+
}
351+
};
352+
CopilotClient copilotClient = new(new CopilotClientOptions());
353+
const string TestId = "agent-id";
354+
var agent = new GitHubCopilotAgent(copilotClient, ownsClient: false, id: TestId, tools: null);
355+
356+
// Act
357+
AgentResponseUpdate result = agent.ConvertToAgentResponseUpdate(assistantMessage, isStreaming: false);
358+
359+
// Assert - should emit empty TextContent rather than throwing.
360+
Assert.Empty(result.Text);
361+
Assert.Contains(result.Contents, c => c is TextContent);
362+
}
311363
}

0 commit comments

Comments
 (0)