From 1bf55fe5c44a6dd459c737b9ac5c9b257f322d18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nathan=20=F0=9F=94=B6=20Tarbert?= <66887028+NathanTarbert@users.noreply.github.com> Date: Sat, 11 Jul 2026 17:36:41 -0400 Subject: [PATCH] fix(dotnet): preserve Context and ForwardedProperties from caller-supplied RunAgentInput AGUIChatClient.BuildRunAgentInput copied Messages, Tools, State, and ParentRunId from a RunAgentInput supplied via ChatOptions.RawRepresentationFactory, but never copied Context or ForwardedProperties. Both were silently dropped from the request sent over the wire, even though they are documented caller-supplied run parameters. Carry both through with the same guarded treatment as the other fields: copy Context when non-empty, and ForwardedProperties when the caller set it (ValueKind is not Undefined). Fixes #2151 --- sdks/dotnet/src/AGUI.Client/AGUIChatClient.cs | 10 +++++ .../AGUIChatClientTest.cs | 38 +++++++++++++++++++ 2 files changed, 48 insertions(+) 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()