-
Notifications
You must be signed in to change notification settings - Fork 2.2k
.NET: Forward AG-UI context and additional properties #7742
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ | |
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using AGUI.Abstractions; | ||
| using AGUI.Client; | ||
| using AGUI.Server; | ||
| using FluentAssertions; | ||
| using Microsoft.AspNetCore.Builder; | ||
|
|
@@ -28,6 +29,37 @@ public sealed class ForwardedPropertiesTests : IAsyncDisposable | |
| private WebApplication? _app; | ||
| private HttpClient? _client; | ||
|
|
||
| [Fact] | ||
| public async Task ChatClient_ForwardsContextAndForwardedPropsFromRawRepresentationFactoryAsync() | ||
| { | ||
| // Arrange | ||
| FakeForwardedPropsAgent fakeAgent = new(); | ||
| await this.SetupTestServerAsync(fakeAgent); | ||
| var chatClient = new AGUIChatClient(new(this._client!, "/agent")); | ||
| JsonElement forwardedProperties = JsonSerializer.SerializeToElement(new { tenantId = "tenant-123" }); | ||
| ChatOptions options = new() | ||
| { | ||
| RawRepresentationFactory = _ => new RunAgentInput | ||
| { | ||
| Context = [new AGUIContext { Description = "Current user", Value = "Ada Lovelace" }], | ||
| ForwardedProperties = forwardedProperties, | ||
|
Comment on lines
+44
to
+45
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I realise this is just a regression test, so take my comment as a separate suggestion related to this approach, but not this PR. For A2A we propagate the AdditionalProperties dictionary (where serializable) to the service via the A2A metadata field. So on the client side the A2A Agent converts it to metadata while on the host side it converts the metadata back into additional properties and passes that into the remote agent. This is quite nice in that it allows a user to pass a value all the way down the stack, and even access it in tool calls on the service side, if the service has an AF agent with MEAI FICC. Not sure if either of these properties match this concept (perhaps forwarded properties?), but it could be a nice way of integrating well with the abstractions. // client side:
await agent.RunAsync(
"Do something.",
session,
options: new() { AdditionalProperties = new() { { "tenantId", "tenant-123" } } });
// since additional properties is propagated service side and down to the chat client stack from the MAF agent, we can then do this inside a server side tool:
FunctionInvokingChatClient
.CurrentContext
.Options
.AdditionalProperties
.TryGetValue("tenantId", out string tenantId); |
||
| }, | ||
| }; | ||
|
|
||
| // Act | ||
| await foreach (ChatResponseUpdate _ in chatClient.GetStreamingResponseAsync( | ||
| [new ChatMessage(ChatRole.User, "test client forwarding")], | ||
| options)) | ||
| { | ||
| } | ||
|
|
||
| // Assert | ||
| fakeAgent.ReceivedContext.Should().ContainSingle(); | ||
| fakeAgent.ReceivedContext![0].Description.Should().Be("Current user"); | ||
| fakeAgent.ReceivedContext[0].Value.Should().Be("Ada Lovelace"); | ||
| fakeAgent.ReceivedForwardedProperties.GetProperty("tenantId").GetString().Should().Be("tenant-123"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ForwardedProps_AreParsedAndPassedToAgent_WhenProvidedInRequestAsync() | ||
| { | ||
|
|
@@ -304,6 +336,8 @@ public FakeForwardedPropsAgent() | |
|
|
||
| public override string? Description => "Agent for forwarded properties testing"; | ||
|
|
||
| public IList<AGUIContext>? ReceivedContext { get; private set; } | ||
|
|
||
| public JsonElement ReceivedForwardedProperties { get; private set; } | ||
|
|
||
| protected override Task<AgentResponse> RunCoreAsync(IEnumerable<ChatMessage> messages, AgentSession? session = null, AgentRunOptions? options = null, CancellationToken cancellationToken = default) | ||
|
|
@@ -319,10 +353,14 @@ protected override async IAsyncEnumerable<AgentResponseUpdate> RunCoreStreamingA | |
| { | ||
| // Recover the originating AG-UI input from the request options (set by the hosting layer). | ||
| if (options is ChatClientAgentRunOptions { ChatOptions: { } chatOptions } && | ||
| chatOptions.TryGetRunAgentInput(out RunAgentInput? agentInput) && | ||
| agentInput.ForwardedProperties is { ValueKind: not JsonValueKind.Undefined } forwardedProps) | ||
| chatOptions.TryGetRunAgentInput(out RunAgentInput? agentInput)) | ||
| { | ||
| this.ReceivedForwardedProperties = forwardedProps; | ||
| this.ReceivedContext = agentInput.Context; | ||
|
|
||
| if (agentInput.ForwardedProperties is { ValueKind: not JsonValueKind.Undefined } forwardedProps) | ||
| { | ||
| this.ReceivedForwardedProperties = forwardedProps; | ||
| } | ||
| } | ||
|
|
||
| // Always return a text response | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.