diff --git a/sdks/dotnet/src/AGUI.Client/AGUIChatClient.cs b/sdks/dotnet/src/AGUI.Client/AGUIChatClient.cs index ada762a0ce..2d0302bc74 100644 --- a/sdks/dotnet/src/AGUI.Client/AGUIChatClient.cs +++ b/sdks/dotnet/src/AGUI.Client/AGUIChatClient.cs @@ -340,6 +340,16 @@ private static RunAgentInput BuildRunAgentInput( { input.ParentRunId = providedInput.ParentRunId; } + + if (providedInput.Context is { Count: > 0 }) + { + input.Context = providedInput.Context; + } + + if (providedInput.ForwardedProperties.ValueKind is not JsonValueKind.Undefined) + { + input.ForwardedProperties = providedInput.ForwardedProperties; + } } // Convert M.E.AI tools to AG-UI format diff --git a/sdks/dotnet/tests/AGUI.Client.UnitTests/AGUIChatClientTest.cs b/sdks/dotnet/tests/AGUI.Client.UnitTests/AGUIChatClientTest.cs index 4d252e66c3..270ad0c0b4 100644 --- a/sdks/dotnet/tests/AGUI.Client.UnitTests/AGUIChatClientTest.cs +++ b/sdks/dotnet/tests/AGUI.Client.UnitTests/AGUIChatClientTest.cs @@ -2,6 +2,7 @@ using System.Diagnostics; using System.Linq; using System.Runtime.CompilerServices; +using System.Text.Json; using System.Threading; using System.Threading.Tasks; using AGUI.Abstractions; @@ -143,6 +144,43 @@ public async Task GetStreamingResponse_SendsFullHistoryEveryTurn() Assert.Equal(3, transport.LastInput!.Messages.Count); } + // https://github.com/ag-ui-protocol/ag-ui/issues/2151 + // A caller-supplied RunAgentInput (via RawRepresentationFactory) must have its + // Context and ForwardedProperties carried through onto the request that is sent, + // just like Messages/Tools/State/ParentRunId already are. + [Fact] + public async Task GetStreamingResponse_WithRawRepresentationFactory_PreservesContextAndForwardedProperties() + { + var transport = new CapturingTransport(); + using var client = new AGUIChatClient(new() { Transport = transport }); + + var forwarded = JsonDocument.Parse("{\"tenant\":\"acme\"}").RootElement.Clone(); + var options = new ChatOptions + { + RawRepresentationFactory = _ => new RunAgentInput + { + Context = new List + { + new() { Description = "userId", Value = "u-123" } + }, + ForwardedProperties = forwarded, + } + }; + + await DrainAsync(client.GetStreamingResponseAsync( + [new ChatMessage(ChatRole.User, "Hello")], options)); + + var sent = transport.LastInput!; + + Assert.NotNull(sent.Context); + Assert.Single(sent.Context!); + Assert.Equal("userId", sent.Context![0].Description); + Assert.Equal("u-123", sent.Context[0].Value); + + Assert.Equal(JsonValueKind.Object, sent.ForwardedProperties.ValueKind); + Assert.Equal("acme", sent.ForwardedProperties.GetProperty("tenant").GetString()); + } + // https://github.com/microsoft/agent-framework/issues/5587 [Fact] public async Task AGUIChatClient_ToolCallResultWithPlainTextContent_DoesNotParseAsJson()