-
Notifications
You must be signed in to change notification settings - Fork 1.5k
.NET: Update AGUI service to support session storage #5193
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
base: main
Are you sure you want to change the base?
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 | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,9 +1,12 @@ | ||||||||||
| // Copyright (c) Microsoft. All rights reserved. | ||||||||||
|
|
||||||||||
| using System; | ||||||||||
| using System.Collections.Generic; | ||||||||||
| using System.Diagnostics.CodeAnalysis; | ||||||||||
| using System.Linq; | ||||||||||
| using System.Runtime.CompilerServices; | ||||||||||
| using System.Threading; | ||||||||||
| using System.Threading.Tasks; | ||||||||||
| using Microsoft.Agents.AI.Hosting.AGUI.AspNetCore.Shared; | ||||||||||
| using Microsoft.AspNetCore.Builder; | ||||||||||
| using Microsoft.AspNetCore.Http; | ||||||||||
|
|
@@ -21,18 +24,66 @@ namespace Microsoft.Agents.AI.Hosting.AGUI.AspNetCore; | |||||||||
| /// </summary> | ||||||||||
| public static class AGUIEndpointRouteBuilderExtensions | ||||||||||
| { | ||||||||||
| /// <summary> | ||||||||||
| /// Maps an AG-UI agent endpoint using an agent registered in dependency injection via <see cref="IHostedAgentBuilder"/>. | ||||||||||
| /// </summary> | ||||||||||
| /// <param name="endpoints">The endpoint route builder.</param> | ||||||||||
| /// <param name="agentBuilder">The hosted agent builder that identifies the agent registration.</param> | ||||||||||
| /// <param name="pattern">The URL pattern for the endpoint.</param> | ||||||||||
| /// <returns>An <see cref="IEndpointConventionBuilder"/> for the mapped endpoint.</returns> | ||||||||||
| public static IEndpointConventionBuilder MapAGUI( | ||||||||||
| this IEndpointRouteBuilder endpoints, | ||||||||||
| IHostedAgentBuilder agentBuilder, | ||||||||||
| [StringSyntax("route")] string pattern) | ||||||||||
| { | ||||||||||
|
||||||||||
| { | |
| { | |
| ArgumentNullException.ThrowIfNull(endpoints); |
Copilot
AI
Apr 9, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
aiAgent.Name is nullable. Passing a null/whitespace key into GetKeyedService<AgentSessionStore>(aiAgent.Name) can lead to surprising behavior (e.g., resolving an unkeyed store, or later failing when creating sessions). Consider guarding with string.IsNullOrWhiteSpace(aiAgent.Name) and treating that as "no store registered" (use NoopAgentSessionStore).
| var agentSessionStore = endpoints.ServiceProvider.GetKeyedService<AgentSessionStore>(aiAgent.Name); | |
| var agentSessionStore = string.IsNullOrWhiteSpace(aiAgent.Name) | |
| ? null | |
| : endpoints.ServiceProvider.GetKeyedService<AgentSessionStore>(aiAgent.Name); |
Copilot
AI
Apr 9, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The session store (and AIHostAgent) are resolved/constructed at endpoint-mapping time via endpoints.ServiceProvider. If a user registers an AgentSessionStore with Scoped/Transient lifetime (supported by WithSessionStore(..., lifetime)), resolving it from the root provider can throw ("Cannot resolve scoped service...") or capture the wrong lifetime. Resolve the store from HttpContext.RequestServices inside the request handler (or otherwise create the AIHostAgent per request) to respect DI lifetimes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
using System.Threading.Tasks;appears unused in this file and will trigger CS8019 (unnecessary using directive) in typical builds. Please remove it (and any other unused usings) to keep builds warning-free.