.NET: Add MapAGUI overload that resolves the agent per request via a factory#6251
Open
darthmolen wants to merge 3 commits into
Open
.NET: Add MapAGUI overload that resolves the agent per request via a factory#6251darthmolen wants to merge 3 commits into
darthmolen wants to merge 3 commits into
Conversation
The existing MapAGUI overloads resolve a single AIAgent at startup from
the root service provider. Hosts that build agents per request -- for
per-request authentication, scoped tool/MCP sessions, or conversation-
scoped configuration -- cannot use them. This adds:
MapAGUI(this IEndpointRouteBuilder endpoints, string agentName,
string pattern, Func<IServiceProvider, string, AIAgent> createAgentDelegate)
The factory is invoked once per request with the request's
IServiceProvider (HttpContext.RequestServices) and the agent name, so
scoped services resolve correctly within the request. The keyed
AgentSessionStore lookup and the ThreadId trust model are unchanged --
the store is resolved per request from HttpContext.RequestServices using
agentName as the key. The shared request-handling body is factored into
a private HandleRunAsync helper reused by both overloads.
Addresses microsoft#2988.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: Steven Molen <steven.molen@csat.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds a new MapAGUI overload that creates an AIAgent per request via a factory delegate, and refactors the shared request-handling logic into a helper to reduce duplication.
Changes:
- Added
MapAGUIoverload acceptingFunc<IServiceProvider, string, AIAgent>for per-request agent resolution. - Refactored endpoint handler body into
HandleRunAsyncand reused it from existing overload(s). - Added unit tests for the new overload (mapping, deferral behavior, and null delegate guard).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| dotnet/tests/Microsoft.Agents.AI.Hosting.AGUI.AspNetCore.UnitTests/AGUIEndpointRouteBuilderExtensionsTests.cs | Adds new unit tests targeting the new factory-delegate MapAGUI overload and its mapping-time behavior. |
| dotnet/src/Microsoft.Agents.AI.Hosting.AGUI.AspNetCore/AGUIEndpointRouteBuilderExtensions.cs | Introduces the per-request factory-delegate overload and extracts shared request processing into HandleRunAsync. |
Covers the per-request factory invocation (the factory is invoked once per request rather than captured at startup) and the factory-returns-null failure path (a clear InvalidOperationException naming the agent). The latter addresses review feedback that the null-return branch was uncovered. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Signed-off-by: Steven Molen <steven.molen@csat.com>
Author
|
@microsoft-github-policy-service agree |
|
This would be helpful! I had to add |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a
MapAGUIoverload that resolves the agent per request via a factory delegate, instead of capturing a singleAIAgentat startup from the root service provider.Motivation
The existing
MapAGUIoverloads resolve one keyedAIAgentonce at map time (endpoints.ServiceProvider.GetRequiredKeyedService<AIAgent>(name)). Hosts that must build the agent per request — for per-request authentication, scoped tool/MCP sessions, or conversation-scoped configuration — have no supported entry point and must work around it (e.g. a singleton facadeAIAgentthat re-resolves a real agent on each run).This overload invokes the factory once per request with the request's
IServiceProvider(HttpContext.RequestServices) and the agent name, so request-scoped services resolve correctly. It mirrors the existingAddAIAgent(name, Func<IServiceProvider, string, AIAgent>)delegate shape.Details
AgentSessionStorelookup and theThreadIdtrust model are unchanged; the store is resolved per request fromHttpContext.RequestServicesusingagentNameas the key.HandleRunAsyncreused by both the instance and factory overloads (no behavior change to the existing path).endpoints,agentName, andcreateAgentDelegate; the factory returning null throws a clearInvalidOperationException.Tests
Adds unit tests: the overload maps an endpoint; it defers resolution (factory not invoked and no keyed
AIAgentresolved at map time, unlike the startup-capture overloads); and a null-factory guard. All 38 tests inMicrosoft.Agents.AI.Hosting.AGUI.AspNetCore.UnitTestspass on net8.0/net9.0/net10.0.Closes #2988
Related to #5209